]> git.sesse.net Git - vlc/blobdiff - modules/misc/svg.c
macosx: Fix controller playlist toggling to use the contentRect and not the window...
[vlc] / modules / misc / svg.c
index 79737a57497a1197e69e53b2c5928d8f15677cc1..d601cd7ca4e1b35694b9b71c6dd190087950cef8 100644 (file)
 /*****************************************************************************
  * Preamble
  *****************************************************************************/
-#include <stdlib.h>                                      /* malloc( ), free( ) */
-#include <string.h>
 
-#include <vlc/vlc.h>
-#include <vlc/vout.h>
-#include "vlc_osd.h"
-#include "vlc_block.h"
-#include "vlc_filter.h"
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <vlc_common.h>
+#include <vlc_plugin.h>
+#include <vlc_charset.h>
+#include <vlc_vout.h>
+#include <vlc_osd.h>
+#include <vlc_block.h>
+#include <vlc_filter.h>
 
 #ifdef HAVE_SYS_TYPES_H
 #   include <sys/types.h>
 #endif
-#include <sys/stat.h>
 
 #ifdef HAVE_UNISTD_H
 #    include <unistd.h>
@@ -44,6 +47,8 @@
 #   include <io.h>
 #endif
 
+#include <glib.h>
+#include <glib/gstdio.h>
 #include <glib-object.h>                                  /* g_object_unref( ) */
 #include <librsvg-2/librsvg/rsvg.h>
 
@@ -56,7 +61,7 @@ static int  Create    ( vlc_object_t * );
 static void Destroy   ( vlc_object_t * );
 static int  RenderText( filter_t *p_filter, subpicture_region_t *p_region_out,
                         subpicture_region_t *p_region_in );
-
+static char *svg_GetTemplate( vlc_object_t *p_this );
 
 /*****************************************************************************
  * Module descriptor
@@ -71,7 +76,7 @@ vlc_module_begin();
  set_category( SUBCAT_INPUT_SCODEC );
  set_capability( "text renderer", 99 );
  add_shortcut( "svg" );
- add_string( "svg-template-file", "", NULL, TEMPLATE_TEXT, TEMPLATE_LONGTEXT, VLC_TRUE );
+ add_string( "svg-template-file", "", NULL, TEMPLATE_TEXT, TEMPLATE_LONGTEXT, true );
  set_callbacks( Create, Destroy );
 vlc_module_end();
 
@@ -126,16 +131,13 @@ static int Create( vlc_object_t *p_this )
     /* Allocate structure */
     p_sys = malloc( sizeof( filter_sys_t ) );
     if( !p_sys )
-    {
-        msg_Err( p_filter, "out of memory" );
         return VLC_ENOMEM;
-    }
 
     /* Initialize psz_template */
     p_sys->psz_template = svg_GetTemplate( p_this );
     if( !p_sys->psz_template )
     {
-        msg_Err( p_filter, "out of memory" );
+        free( p_sys );
         return VLC_ENOMEM;
     }
 
@@ -143,10 +145,11 @@ static int Create( vlc_object_t *p_this )
     p_sys->i_height = p_filter->fmt_out.video.i_height;
 
     p_filter->pf_render_text = RenderText;
+    p_filter->pf_render_html = NULL;
     p_filter->p_sys = p_sys;
 
     /* MUST call this before any RSVG funcs */
-    g_type_init ();
+    rsvg_init( );
 
     return VLC_SUCCESS;
 }
@@ -159,7 +162,7 @@ static char *svg_GetTemplate( vlc_object_t *p_this )
     FILE *file;
 
     psz_filename = config_GetPsz( p_filter, "svg-template-file" );
-    if( !psz_filename || psz_filename[0] == 0 )
+    if( !psz_filename || (psz_filename[0] == 0) )
     {
         /* No filename. Use a default value. */
         psz_template = NULL;
@@ -177,10 +180,8 @@ static char *svg_GetTemplate( vlc_object_t *p_this )
         else
         {
             struct stat s;
-            int i_ret;
 
-            i_ret = utf8_stat( psz_filename, &s );
-            if( i_ret )
+            if( fstat( fileno( file ), &s ) )
             {
                 /* Problem accessing file information. Should not
                    happen as we could open it. */
@@ -200,15 +201,17 @@ static char *svg_GetTemplate( vlc_object_t *p_this )
                 psz_template = malloc( ( s.st_size + 42 ) * sizeof( char ) );
                 if( !psz_template )
                 {
-                    msg_Err( p_filter, "out of memory" );
+                    fclose( file );
+                    free( psz_filename );
                     return NULL;
                 }
                 memset( psz_template, 0, s.st_size + 1 );
                 fread( psz_template, s.st_size, 1, file );
-                fclose( file );
             }
+            fclose( file );
         }
     }
+    free( psz_filename );
     if( !psz_template )
     {
         /* Either there was no file, or there was an error.
@@ -234,6 +237,7 @@ static void Destroy( vlc_object_t *p_this )
 
     free( p_sys->psz_template );
     free( p_sys );
+    rsvg_term( );
 }
 
 /*****************************************************************************
@@ -261,7 +265,7 @@ static int Render( filter_t *p_filter, subpicture_region_t *p_region,
 
     if( p_svg->p_rendition == NULL ) {
         svg_RenderPicture( p_filter, p_svg );
-        if( ! p_svg )
+        if( ! p_svg->p_rendition )
         {
             msg_Err( p_filter, "Cannot render SVG" );
             return VLC_EGENERIC;
@@ -397,22 +401,37 @@ static void svg_RenderPicture( filter_t *p_filter,
     RsvgHandle *p_handle;
     GError *error = NULL;
 
+    p_svg->p_rendition = NULL;
+
     p_handle = rsvg_handle_new();
 
+    if( !p_handle )
+    {
+        msg_Err( p_filter, "Error creating SVG reader: %s", error->message );
+        return;
+    }
+
     rsvg_handle_set_size_callback( p_handle, svg_SizeCallback, p_filter, NULL );
 
-    rsvg_handle_write( p_handle,
-                       ( guchar* )p_svg->psz_text, strlen( p_svg->psz_text ) + 1,
-                       &error );
-    if( error != NULL )
+    if( ! rsvg_handle_write( p_handle,
+                 ( guchar* )p_svg->psz_text, strlen( p_svg->psz_text ),
+                 &error ) )
     {
         msg_Err( p_filter, "error while rendering SVG: %s\n", error->message );
+        g_object_unref( G_OBJECT( p_handle ) );
+        return;
+    }
+
+    if( ! rsvg_handle_close( p_handle, &error ) )
+    {
+        msg_Err( p_filter, "error while rendering SVG (close): %s\n", error->message );
+        g_object_unref( G_OBJECT( p_handle ) );
         return;
     }
-    rsvg_handle_close( p_handle, &error );
 
     p_svg->p_rendition = rsvg_handle_get_pixbuf( p_handle );
-    rsvg_handle_free( p_handle );
+
+    g_object_unref( G_OBJECT( p_handle ) );
 }
 
 
@@ -430,10 +449,7 @@ static int RenderText( filter_t *p_filter, subpicture_region_t *p_region_out,
 
     p_svg = ( svg_rendition_t * )malloc( sizeof( svg_rendition_t ) );
     if( !p_svg )
-    {
-        msg_Err( p_filter, "out of memory" );
         return VLC_ENOMEM;
-    }
 
     p_region_out->i_x = p_region_in->i_x;
     p_region_out->i_y = p_region_in->i_y;
@@ -446,7 +462,6 @@ static int RenderText( filter_t *p_filter, subpicture_region_t *p_region_out,
         p_svg->psz_text = strdup( psz_string );
         if( !p_svg->psz_text )
         {
-            msg_Err( p_filter, "out of memory" );
             free( p_svg );
             return VLC_ENOMEM;
         }
@@ -461,7 +476,6 @@ static int RenderText( filter_t *p_filter, subpicture_region_t *p_region_out,
         p_svg->psz_text = malloc( ( length + 1 ) * sizeof( char ) );
         if( !p_svg->psz_text )
         {
-            msg_Err( p_filter, "out of memory" );
             free( p_svg );
             return VLC_ENOMEM;
         }
@@ -485,8 +499,7 @@ static int RenderText( filter_t *p_filter, subpicture_region_t *p_region_out,
 
 static void FreeString( svg_rendition_t *p_svg )
 {
-    if( p_svg->psz_text )
-        free( p_svg->psz_text );
+    free( p_svg->psz_text );
     /* p_svg->p_rendition is a GdkPixbuf, and its allocation is
        managed through ref. counting */
     if( p_svg->p_rendition )