]> git.sesse.net Git - vlc/blobdiff - modules/text_renderer/freetype.c
l10n: Simplified Chinese update
[vlc] / modules / text_renderer / freetype.c
index b6e2fe7382ce3ee0fe600da2ea1b9b73dcc14611..cdc92430c65fc224b628fdb268a71f9fa0fa2887 100644 (file)
@@ -51,7 +51,7 @@
 # define SYSTEM_DEFAULT_FAMILY "Arial Unicode MS"
 # define SYSTEM_DEFAULT_MONOSPACE_FONT_FILE "/System/Library/Fonts/Monaco.dfont"
 # define SYSTEM_DEFAULT_MONOSPACE_FAMILY "Monaco"
-#elif defined( WIN32 )
+#elif defined( _WIN32 )
 # define SYSTEM_DEFAULT_FONT_FILE "arial.ttf" /* Default path font found at run-time */
 # define SYSTEM_DEFAULT_FAMILY "Arial"
 # define SYSTEM_DEFAULT_MONOSPACE_FONT_FILE "cour.ttf"
 
 /* apple stuff */
 #ifdef __APPLE__
+#include <TargetConditionals.h>
+#if !TARGET_OS_IPHONE
 #include <Carbon/Carbon.h>
+#endif
 #include <sys/param.h>                         /* for MAXPATHLEN */
 #undef HAVE_FONTCONFIG
 #define HAVE_STYLES
 #endif
 
 /* Win32 GDI */
-#ifdef WIN32
+#ifdef _WIN32
 # include <windows.h>
 # include <shlobj.h>
 # define HAVE_STYLES
@@ -147,7 +150,6 @@ typedef uint32_t uni_char_t;
  *****************************************************************************/
 static int  Create ( vlc_object_t * );
 static void Destroy( vlc_object_t * );
-static int OptionCallback( vlc_object_t *, char const *, vlc_value_t, vlc_value_t, void *);
 
 #define FONT_TEXT N_("Font")
 #define MONOSPACE_FONT_TEXT N_("Monospace Font")
@@ -343,14 +345,9 @@ struct filter_sys_t
     FT_Face        p_face;      /* handle to face object */
     FT_Stroker     p_stroker;
     uint8_t        i_font_opacity;
-    int            i_font_color;
     int            i_font_size;
     bool           b_font_bold;
 
-    uint8_t        i_background_opacity;
-    int            i_background_color;
-
-    double         f_outline_thickness;
     uint8_t        i_outline_opacity;
     int            i_outline_color;
 
@@ -364,7 +361,7 @@ struct filter_sys_t
     char*          psz_fontfamily;
     char*          psz_monofontfamily;
     xml_reader_t  *p_xml;
-#ifdef WIN32
+#ifdef _WIN32
     char*          psz_win_fonts_path;
 #endif
 
@@ -443,7 +440,7 @@ static int GetFontSize( filter_t *p_filter )
     }
     else
     {
-        int i_ratio = var_GetInteger( p_filter, "freetype-rel-fontsize" );
+        int i_ratio = var_InheritInteger( p_filter, "freetype-rel-fontsize" );
         if( i_ratio > 0 )
         {
             i_size = (int)p_filter->fmt_out.video.i_height / i_ratio;
@@ -493,7 +490,7 @@ static void FontConfig_BuildCache( filter_t *p_filter )
     FcInit();
 #endif
 
-#if defined( WIN32 ) || defined( __APPLE__ )
+#if defined( _WIN32 ) || defined( __APPLE__ )
     dialog_progress_bar_t *p_dialog = NULL;
     FcConfig *fcConfig = FcInitLoadConfig();
 
@@ -601,7 +598,7 @@ static char* FontConfig_Select( FcConfig* config, const char* family,
 }
 #endif
 
-#ifdef WIN32
+#ifdef _WIN32
 #define FONT_DIR_NT _T("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Fonts")
 
 static int GetFileFontByName( LPCTSTR font_name, char **psz_filename )
@@ -728,9 +725,10 @@ fail:
             return psz_tmp;
     }
 }
-#endif /* HAVE_WIN32 */
+#endif /* _WIN32 */
 
 #ifdef __APPLE__
+#if !TARGET_OS_IPHONE
 static char* MacLegacy_Select( filter_t *p_filter, const char* psz_fontname,
                           bool b_bold, bool b_italic, int i_size, int *i_idx )
 {
@@ -810,6 +808,7 @@ static char* MacLegacy_Select( filter_t *p_filter, const char* psz_fontname,
     return psz_path;
 }
 #endif
+#endif
 
 #endif /* HAVE_STYLES */
 
@@ -1185,7 +1184,8 @@ static inline int RenderAXYZ( filter_t *p_filter,
     p_region->fmt = fmt;
 
     /* Initialize the picture background */
-    uint8_t i_a = p_sys->i_background_opacity;
+    uint8_t i_a = var_InheritInteger( p_filter, "freetype-background-opacity" );
+    i_a = VLC_CLIP( i_a, 0, 255 );
     uint8_t i_x, i_y, i_z;
 
     if (p_region->b_renderbg) {
@@ -1195,7 +1195,9 @@ static inline int RenderAXYZ( filter_t *p_filter,
                          ExtractComponents, BlendPixel);
     } else {
         /* Render background under entire subpicture block */
-        ExtractComponents( p_sys->i_background_color, &i_x, &i_y, &i_z );
+        int i_background_color = var_InheritInteger( p_filter, "freetype-background-color" );
+        i_background_color = VLC_CLIP( i_background_color, 0, 0xFFFFFF );
+        ExtractComponents( i_background_color, &i_x, &i_y, &i_z );
         FillPicture( p_picture, i_a, i_x, i_y, i_z );
     }
 
@@ -1677,14 +1679,16 @@ static void HandleWhiteSpace( char *psz_node )
 }
 
 
-static text_style_t *GetStyleFromFontStack( filter_sys_t *p_sys,
+static text_style_t *GetStyleFromFontStack( filter_t *p_filter,
                                             font_stack_t **p_fonts,
                                             int i_style_flags )
 {
     char       *psz_fontname = NULL;
-    uint32_t    i_font_color = p_sys->i_font_color & 0x00ffffff;
+    uint32_t    i_font_color = var_InheritInteger( p_filter, "freetype-color" );
+    i_font_color = VLC_CLIP( i_font_color, 0, 0xFFFFFF );
+    i_font_color = i_font_color & 0x00ffffff;
+    int         i_font_size  = p_filter->p_sys->i_font_size;
     uint32_t    i_karaoke_bg_color = i_font_color;
-    int         i_font_size  = p_sys->i_font_size;
 
     if( PeekFont( p_fonts, &psz_fontname, &i_font_size,
                   &i_font_color, &i_karaoke_bg_color ) )
@@ -1775,11 +1779,15 @@ static int ProcessNodes( filter_t *p_filter,
     }
     else
     {
+        uint32_t i_font_size = p_sys->i_font_size;
+        uint32_t i_font_color = var_InheritInteger( p_filter, "freetype-color" );
+        i_font_color = VLC_CLIP( i_font_color, 0, 0xFFFFFF );
+        uint32_t i_font_opacity = p_sys->i_font_opacity;
         rv = PushFont( &p_fonts,
                        p_sys->psz_fontfamily,
-                       p_sys->i_font_size,
-                       (p_sys->i_font_color & 0xffffff) |
-                          ((p_sys->i_font_opacity & 0xff) << 24),
+                       i_font_size,
+                       (i_font_color & 0xffffff) |
+                          ((i_font_opacity & 0xff) << 24),
                        0x00ffffff );
     }
     if( p_sys->b_font_bold )
@@ -1830,7 +1838,7 @@ static int ProcessNodes( filter_t *p_filter,
                                                 &pp_styles[i_text_length],
                                                 pi_k_dates ? &pi_k_dates[i_text_length] : NULL,
                                                 "\n",
-                                                GetStyleFromFontStack( p_sys,
+                                                GetStyleFromFontStack( p_filter,
                                                                        &p_fonts,
                                                                        i_style_flags ),
                                                 i_k_date );
@@ -1861,7 +1869,7 @@ static int ProcessNodes( filter_t *p_filter,
                                             &pp_styles[i_text_length],
                                             pi_k_dates ? &pi_k_dates[i_text_length] : NULL,
                                             psz_node,
-                                            GetStyleFromFontStack( p_sys,
+                                            GetStyleFromFontStack( p_filter,
                                                                    &p_fonts,
                                                                    i_style_flags ),
                                             i_k_date );
@@ -1978,8 +1986,10 @@ static FT_Face LoadFace( filter_t *p_filter,
                                           -1,
                                           &i_idx );
 #elif defined( __APPLE__ )
+#if !TARGET_OS_IPHONE
         psz_fontfile = MacLegacy_Select( p_filter, p_style->psz_fontname, false, false, -1, &i_idx );
-#elif defined( WIN32 )
+#endif
+#elif defined( _WIN32 )
         psz_fontfile = Win32_Select( p_filter,
                                     p_style->psz_fontname,
                                     (p_style->i_style_flags & STYLE_BOLD) != 0,
@@ -2329,7 +2339,9 @@ static int ProcessLines( filter_t *p_filter,
                     msg_Err( p_filter, "Failed to set font size to %d", p_current_style->i_font_size );
                 if( p_sys->p_stroker )
                 {
-                    int i_radius = (p_current_style->i_font_size << 6) * p_sys->f_outline_thickness;
+                    double f_outline_thickness = var_InheritInteger( p_filter, "freetype-outline-thickness" ) / 100.0;
+                    f_outline_thickness = VLC_CLIP( f_outline_thickness, 0.0, 0.5 );
+                    int i_radius = (p_current_style->i_font_size << 6) * f_outline_thickness;
                     FT_Stroker_Set( p_sys->p_stroker,
                                     i_radius,
                                     FT_STROKER_LINECAP_ROUND,
@@ -2673,11 +2685,15 @@ static int RenderCommon( filter_t *p_filter, subpicture_region_t *p_region_out,
                                                                           STYLE_UNDERLINE |
                                                                           STYLE_STRIKEOUT) );
         else
+        {
+            uint32_t i_font_color = var_InheritInteger( p_filter, "freetype-color" );
+            i_font_color = VLC_CLIP( i_font_color, 0, 0xFFFFFF );
             p_style = CreateStyle( p_sys->psz_fontfamily,
                                    p_sys->i_font_size,
-                                   (p_sys->i_font_color & 0xffffff) |
+                                   (i_font_color & 0xffffff) |
                                    ((p_sys->i_font_opacity & 0xff) << 24),
                                    0x00ffffff, 0);
+        }
         if( p_sys->b_font_bold )
             p_style->i_style_flags |= STYLE_BOLD;
 
@@ -2710,7 +2726,9 @@ static int RenderCommon( filter_t *p_filter, subpicture_region_t *p_region_out,
         else if( !p_chroma_list || *p_chroma_list == 0 )
             p_chroma_list = p_chroma_list_rgba;
 
-        const int i_margin = p_sys->i_background_opacity > 0 ? i_max_face_height / 4 : 0;
+        uint8_t i_background_opacity = var_InheritInteger( p_filter, "freetype-background-opacity" );
+        i_background_opacity = VLC_CLIP( i_background_opacity, 0, 255 );
+        const int i_margin = i_background_opacity > 0 ? i_max_face_height / 4 : 0;
         for( const vlc_fourcc_t *p_chroma = p_chroma_list; *p_chroma != 0; p_chroma++ )
         {
             rv = VLC_EGENERIC;
@@ -2780,7 +2798,7 @@ static int Create( vlc_object_t *p_this )
     char          *psz_fontfamily = NULL;
     char          *psz_monofontfile   = NULL;
     char          *psz_monofontfamily = NULL;
-    int            i_error = 0, fontindex = 0;
+    int            i_error = 0, fontindex = 0, monofontindex = 0;
 
     /* Allocate structure */
     p_filter->p_sys = p_sys = malloc( sizeof(*p_sys) );
@@ -2794,33 +2812,22 @@ static int Create( vlc_object_t *p_this )
     p_sys->i_font_size      = 0;
     p_sys->i_display_height = 0;
 
-    var_Create( p_filter, "freetype-rel-fontsize",
-                VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
-    var_Create( p_filter, "freetype-background-opacity",
-                VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
-    var_Create( p_filter, "freetype-background-color",
-                VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
-    var_Create( p_filter, "freetype-outline-thickness",
-                VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
-    var_Create( p_filter, "freetype-color",
-               VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
+    /*
+     * The following variables should not be cached, as they might be changed on-the-fly:
+     * freetype-rel-fontsize, freetype-background-opacity, freetype-background-color,
+     * freetype-outline-thickness, freetype-color
+     *
+     */
 
     psz_fontfamily = var_InheritString( p_filter, "freetype-font" );
     psz_monofontfamily = var_InheritString( p_filter, "freetype-monofont" );
     p_sys->i_default_font_size = var_InheritInteger( p_filter, "freetype-fontsize" );
     p_sys->i_font_opacity = var_InheritInteger( p_filter,"freetype-opacity" );
     p_sys->i_font_opacity = VLC_CLIP( p_sys->i_font_opacity, 0, 255 );
-    p_sys->i_font_color = var_InheritInteger( p_filter, "freetype-color" );
-    p_sys->i_font_color = VLC_CLIP( p_sys->i_font_color, 0, 0xFFFFFF );
     p_sys->b_font_bold = var_InheritBool( p_filter, "freetype-bold" );
 
-    p_sys->i_background_opacity = var_InheritInteger( p_filter,"freetype-background-opacity" );;
-    p_sys->i_background_opacity = VLC_CLIP( p_sys->i_background_opacity, 0, 255 );
-    p_sys->i_background_color = var_InheritInteger( p_filter, "freetype-background-color" );
-    p_sys->i_background_color = VLC_CLIP( p_sys->i_background_color, 0, 0xFFFFFF );
-
-    p_sys->f_outline_thickness = var_InheritInteger( p_filter, "freetype-outline-thickness" ) / 100.0;
-    p_sys->f_outline_thickness = VLC_CLIP( p_sys->f_outline_thickness, 0.0, 0.5 );
+    double f_outline_thickness = var_InheritInteger( p_filter, "freetype-outline-thickness" ) / 100.0;
+    f_outline_thickness = VLC_CLIP( f_outline_thickness, 0.0, 0.5 );
     p_sys->i_outline_opacity = var_InheritInteger( p_filter, "freetype-outline-opacity" );
     p_sys->i_outline_opacity = VLC_CLIP( p_sys->i_outline_opacity, 0, 255 );
     p_sys->i_outline_color = var_InheritInteger( p_filter, "freetype-outline-color" );
@@ -2836,7 +2843,7 @@ static int Create( vlc_object_t *p_this )
     p_sys->f_shadow_vector_x = f_shadow_distance * cos(2 * M_PI * f_shadow_angle / 360);
     p_sys->f_shadow_vector_y = f_shadow_distance * sin(2 * M_PI * f_shadow_angle / 360);
 
-#ifdef WIN32
+#ifdef _WIN32
     /* Get Windows Font folder */
     wchar_t wdir[MAX_PATH];
     if( S_OK != SHGetFolderPathW( NULL, CSIDL_FONTS, NULL, SHGFP_TYPE_CURRENT, wdir ) )
@@ -2854,9 +2861,12 @@ static int Create( vlc_object_t *p_this )
 #ifdef HAVE_STYLES
         psz_fontfamily = strdup( DEFAULT_FAMILY );
 #else
-# ifdef WIN32
+# ifdef _WIN32
         if( asprintf( &psz_fontfamily, "%s"DEFAULT_FONT_FILE, p_sys->psz_win_fonts_path ) == -1 )
+        {
+            psz_fontfamily = NULL;
             goto error;
+        }
 # else
         psz_fontfamily = strdup( DEFAULT_FONT_FILE );
 # endif
@@ -2877,8 +2887,10 @@ static int Create( vlc_object_t *p_this )
                                           false, p_sys->i_default_font_size,
                                           &monofontindex );
 #elif defined(__APPLE__)
+#if !TARGET_OS_IPHONE
     psz_fontfile = MacLegacy_Select( p_filter, psz_fontfamily, false, false, 0, &fontindex );
-#elif defined(WIN32)
+#endif
+#elif defined(_WIN32)
     psz_fontfile = Win32_Select( p_filter, psz_fontfamily, false, false,
                                  p_sys->i_default_font_size, &fontindex );
 
@@ -2932,7 +2944,7 @@ static int Create( vlc_object_t *p_this )
     if( SetFontSize( p_filter, 0 ) != VLC_SUCCESS ) goto error;
 
     p_sys->p_stroker = NULL;
-    if( p_sys->f_outline_thickness > 0.001 )
+    if( f_outline_thickness > 0.001 )
     {
         i_error = FT_Stroker_New( p_sys->p_library, &p_sys->p_stroker );
         if( i_error )
@@ -2947,12 +2959,6 @@ static int Create( vlc_object_t *p_this )
 
     LoadFontsFromAttachments( p_filter );
 
-    var_AddCallback(p_this, "freetype-rel-fontsize", OptionCallback, p_this);
-    var_AddCallback(p_this, "freetype-background-opacity", OptionCallback, p_this);
-    var_AddCallback(p_this, "freetype-background-color", OptionCallback, p_this);
-    var_AddCallback(p_this, "freetype-outline-thickness", OptionCallback, p_this);
-    var_AddCallback(p_this, "freetype-color", OptionCallback, p_this);
-
 #ifdef HAVE_STYLES
     free( psz_fontfile );
     free( psz_monofontfile );
@@ -2983,12 +2989,6 @@ static void Destroy( vlc_object_t *p_this )
     filter_t *p_filter = (filter_t *)p_this;
     filter_sys_t *p_sys = p_filter->p_sys;
 
-    var_DelCallback(p_this, "freetype-rel-fontsize", OptionCallback, p_this);
-    var_DelCallback(p_this, "freetype-background-opacity", OptionCallback, p_this);
-    var_DelCallback(p_this, "freetype-background-color", OptionCallback, p_this);
-    var_DelCallback(p_this, "freetype-outline-thickness", OptionCallback, p_this);
-    var_DelCallback(p_this, "freetype-color", OptionCallback, p_this);
-
     if( p_sys->pp_font_attachments )
     {
         for( int k = 0; k < p_sys->i_font_attachments; k++ )
@@ -2999,8 +2999,9 @@ static void Destroy( vlc_object_t *p_this )
 
     if( p_sys->p_xml ) xml_ReaderDelete( p_sys->p_xml );
     free( p_sys->psz_fontfamily );
+    free( p_sys->psz_monofontfamily );
 
-#ifdef WIN32
+#ifdef _WIN32
     free( p_sys->psz_win_fonts_path );
 #endif
 
@@ -3014,35 +3015,3 @@ static void Destroy( vlc_object_t *p_this )
     FT_Done_FreeType( p_sys->p_library );
     free( p_sys );
 }
-
-static int OptionCallback( vlc_object_t *p_this, char const *psz_var,
-                         vlc_value_t oldval, vlc_value_t newval, void *p_data)
-{
-    VLC_UNUSED(oldval);
-    VLC_UNUSED(p_data);
-
-    filter_t *p_filter = (filter_t *)p_this;
-    filter_sys_t *p_sys = p_filter->p_sys;
-
-    if( !strcmp(psz_var, "freetype-rel-fontsize") )
-        msg_Dbg( p_this, "changed relative font size to %lli", newval.i_int);
-    else if( !strcmp(psz_var, "freetype-color") ) {
-        p_sys->i_font_color = newval.i_int;
-        p_sys->i_font_color = VLC_CLIP( p_sys->i_font_color, 0, 0xFFFFFF );
-    } else if( !strcmp(psz_var, "freetype-background-opacity") ) {
-        p_sys->i_background_opacity = newval.i_int;
-        msg_Dbg( p_filter, "changed background opacity to %lli", newval.i_int);
-    } else if( !strcmp(psz_var, "freetype-outline-thickness") ) {
-        p_sys->f_outline_thickness = newval.i_int / 100.;
-        p_sys->f_outline_thickness = VLC_CLIP( p_sys->f_outline_thickness, 0.0, 0.5 );
-        msg_Dbg( p_filter, "changed outline thickness to %lli", newval.i_int);
-    } else if( !strcmp(psz_var, "freetype-background-color") ) {
-        p_sys->i_background_color = newval.i_int;
-        p_sys->i_background_color = VLC_CLIP( p_sys->i_background_color, 0, 0xFFFFFF );
-        msg_Dbg( p_filter, "changed background color to %lli", newval.i_int);
-    } else
-        return VLC_EGENERIC;
-
-    return VLC_SUCCESS;
-}
-