[vlc-devel] [PATCH 08/11] Add support for monospace font in HTML renderer

Devin Heitmueller dheitmueller at kernellabs.com
Thu Dec 27 07:08:20 CET 2012


The EIA-608 decoder expects fonts to be rendered with a monospace font,
so add the ability for decoders to make use of the "tt" tag, and tweak
the EIA-608 decoder to use that tag.
---
 modules/codec/cc.c               |    5 +++
 modules/text_renderer/freetype.c |   64 +++++++++++++++++++++++++++++++++++++-
 2 files changed, 68 insertions(+), 1 deletion(-)

diff --git a/modules/codec/cc.c b/modules/codec/cc.c
index d9fba54..7e5bbca 100644
--- a/modules/codec/cc.c
+++ b/modules/codec/cc.c
@@ -951,6 +951,10 @@ static void Eia608TextLine( struct eia608_screen *screen, char *psz_text, int i_
     /* Search the start */
     i_start = 0;
 
+    /* Ensure we get a monospaced font (required for accurate positioning */
+    if( b_html )
+        CAT( "<tt>" );
+
     /* Convert leading spaces to non-breaking so that they don't get
        stripped by the RenderHtml routine as regular whitespace */
     while( i_start < EIA608_SCREEN_COLUMNS && p_char[i_start] == ' ' ) {
@@ -1056,6 +1060,7 @@ static void Eia608TextLine( struct eia608_screen *screen, char *psz_text, int i_
             CAT( "</i>" );
         if( last_color != EIA608_COLOR_DEFAULT )
             CAT( "</font>" );
+        CAT( "</tt>" );
     }
 #undef CAT
 }
diff --git a/modules/text_renderer/freetype.c b/modules/text_renderer/freetype.c
index 6cd93af..c9b8025 100644
--- a/modules/text_renderer/freetype.c
+++ b/modules/text_renderer/freetype.c
@@ -64,6 +64,8 @@
 #else
 # define DEFAULT_FONT_FILE "/usr/share/fonts/truetype/freefont/FreeSerifBold.ttf"
 # define DEFAULT_FAMILY "Serif Bold"
+# define DEFAULT_MONOSPACE_FONT_FILE "/usr/share/fonts/truetype/freefont/FreeMono.ttf"
+# define DEFAULT_MONOSPACE_FAMILY "Monospace"
 #endif
 
 /* Freetype */
@@ -126,6 +128,7 @@ static int  Create ( vlc_object_t * );
 static void Destroy( vlc_object_t * );
 
 #define FONT_TEXT N_("Font")
+#define MONOSPACE_FONT_TEXT N_("Monospace Font")
 
 #define FAMILY_LONGTEXT N_("Font family for the font you want to use")
 #define FONT_LONGTEXT N_("Font file for the font you want to use")
@@ -195,8 +198,10 @@ vlc_module_begin ()
 
 #ifdef HAVE_STYLES
     add_font( "freetype-font", DEFAULT_FAMILY, FONT_TEXT, FAMILY_LONGTEXT, false )
+    add_font( "freetype-monofont", DEFAULT_MONOSPACE_FAMILY, MONOSPACE_FONT_TEXT, FAMILY_LONGTEXT, false )
 #else
     add_loadfile( "freetype-font", DEFAULT_FONT_FILE, FONT_TEXT, FONT_LONGTEXT, false )
+    add_loadfile( "freetype-monofont", DEFAULT_MONOSPACE_FONT_FILE, MONOSPACE_FONT_TEXT, FONT_LONGTEXT, false )
 #endif
 
     add_integer( "freetype-fontsize", 0, FONTSIZE_TEXT,
@@ -334,6 +339,7 @@ struct filter_sys_t
     int            i_default_font_size;
     int            i_display_height;
     char*          psz_fontfamily;
+    char*          psz_monofontfamily;
     xml_reader_t  *p_xml;
 #ifdef WIN32
     char*          psz_win_fonts_path;
@@ -1511,6 +1517,46 @@ static int HandleFontAttributes( xml_reader_t *p_xml_reader,
     return rv;
 }
 
+static int HandleTT(font_stack_t **p_fonts, const char *p_fontfamily )
+{
+    int        rv;
+    char      *psz_fontname = NULL;
+    uint32_t   i_font_color = 0xffffff;
+    int        i_font_alpha = 255;
+    uint32_t   i_karaoke_bg_color = 0x00ffffff;
+    int        i_font_size  = 24;
+
+    /* Default all attributes to the top font in the stack -- in case not
+     * all attributes are specified in the sub-font
+     */
+    if( VLC_SUCCESS == PeekFont( p_fonts,
+                                 &psz_fontname,
+                                 &i_font_size,
+                                 &i_font_color,
+                                 &i_karaoke_bg_color ))
+    {
+        psz_fontname = strdup( psz_fontname );
+    }
+    i_font_alpha = (i_font_color >> 24) & 0xff;
+    i_font_color &= 0x00ffffff;
+
+    /* Just keep all the parent's font attributes, but change to 
+       a monospace font */
+    free( psz_fontname );
+    psz_fontname = strdup(p_fontfamily);
+
+    rv = PushFont( p_fonts,
+                   psz_fontname,
+                   i_font_size,
+                   (i_font_color & 0xffffff) | ((i_font_alpha & 0xff) << 24),
+                   i_karaoke_bg_color );
+
+    free( psz_fontname );
+
+    return rv;
+}
+
+
 /* Turn any multiple-whitespaces into single spaces */
 static void HandleWhiteSpace( char *psz_node )
 {
@@ -1647,6 +1693,8 @@ static int ProcessNodes( filter_t *p_filter,
             case XML_READER_ENDELEM:
                 if( !strcasecmp( "font", node ) )
                     PopFont( &p_fonts );
+                else if( !strcasecmp( "tt", node ) )
+                    PopFont( &p_fonts );
                 else if( !strcasecmp( "b", node ) )
                     i_style_flags &= ~STYLE_BOLD;
                else if( !strcasecmp( "i", node ) )
@@ -1660,6 +1708,8 @@ static int ProcessNodes( filter_t *p_filter,
             case XML_READER_STARTELEM:
                 if( !strcasecmp( "font", node ) )
                     HandleFontAttributes( p_xml_reader, &p_fonts );
+                else if( !strcasecmp( "tt", node ) )
+                    HandleTT( &p_fonts, p_sys->psz_monofontfamily );
                 else if( !strcasecmp( "b", node ) )
                     i_style_flags |= STYLE_BOLD;
                 else if( !strcasecmp( "i", node ) )
@@ -2620,7 +2670,9 @@ static int Create( vlc_object_t *p_this )
     filter_sys_t  *p_sys;
     char          *psz_fontfile   = NULL;
     char          *psz_fontfamily = NULL;
-    int            i_error = 0, fontindex = 0;
+    char          *psz_monofontfile   = NULL;
+    char          *psz_monofontfamily = NULL;
+    int            i_error = 0, fontindex = 0, monofontindex = 0;
 
     /* Allocate structure */
     p_filter->p_sys = p_sys = malloc( sizeof(*p_sys) );
@@ -2638,6 +2690,7 @@ static int Create( vlc_object_t *p_this )
                 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
 
     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 );
@@ -2704,6 +2757,9 @@ static int Create( vlc_object_t *p_this )
     /* */
     psz_fontfile = FontConfig_Select( NULL, psz_fontfamily, false, false,
                                       p_sys->i_default_font_size, &fontindex );
+    psz_monofontfile = FontConfig_Select( NULL, psz_monofontfamily, false,
+					  false, p_sys->i_default_font_size,
+					  &monofontindex );
 #elif defined(__APPLE__)
     psz_fontfile = MacLegacy_Select( p_filter, psz_fontfamily, false, false, 0, &fontindex );
 #elif defined(WIN32)
@@ -2716,11 +2772,15 @@ static int Create( vlc_object_t *p_this )
     /* If nothing is found, use the default family */
     if( !psz_fontfile )
         psz_fontfile = strdup( psz_fontfamily );
+    if( !psz_monofontfile )
+        psz_monofontfile = strdup( psz_monofontfamily );
 
 #else /* !HAVE_STYLES */
     /* Use the default file */
     psz_fontfile = psz_fontfamily;
+    psz_monofontfile = psz_monofontfamily;
 #endif
+    p_sys->psz_monofontfamily = psz_monofontfamily;
 
     /* */
     i_error = FT_Init_FreeType( &p_sys->p_library );
@@ -2782,8 +2842,10 @@ error:
     if( p_sys->p_library ) FT_Done_FreeType( p_sys->p_library );
 #ifdef HAVE_STYLES
     free( psz_fontfile );
+    free( psz_monofontfile );
 #endif
     free( psz_fontfamily );
+    free( psz_monofontfamily );
     free( p_sys );
     return VLC_EGENERIC;
 }
-- 
1.7.9.5




More information about the vlc-devel mailing list