From 93c0fb563e992ad16b05f3977ea41327126e0cb3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Felix=20Paul=20K=C3=BChne?= <fkuehne@videolan.org>
Date: Mon, 23 Nov 2015 16:09:24 +0100
Subject: [PATCH 09/15] libvlc: expose a base set of freetype options to
 overwrite the default font

---
 include/vlc/libvlc_media_player.h         | 26 ++++++++++++
 lib/libvlc.sym                            |  6 +++
 lib/media_player.c                        |  6 +++
 lib/video.c                               | 68 ++++++++++++++++++++++++++++++-
 modules/text_renderer/freetype/freetype.c | 59 +++++++++++++++++++++++++--
 modules/text_renderer/freetype/freetype.h |  2 +
 6 files changed, 163 insertions(+), 4 deletions(-)

diff --git a/include/vlc/libvlc_media_player.h b/include/vlc/libvlc_media_player.h
index 79a0b1f..3a37d3b 100644
--- a/include/vlc/libvlc_media_player.h
+++ b/include/vlc/libvlc_media_player.h
@@ -1496,6 +1496,32 @@ LIBVLC_API float libvlc_video_get_adjust_float( libvlc_media_player_t *p_mi,
 LIBVLC_API void libvlc_video_set_adjust_float( libvlc_media_player_t *p_mi,
                                                    unsigned option, float value );
 
+enum libvlc_video_textrenderer_option_t {
+    libvlc_textrender_font = 0,
+    libvlc_textrender_fontsize,
+    libvlc_textrender_fontcolor,
+    libvlc_textrender_fontforcebold,
+};
+
+LIBVLC_API bool libvlc_video_get_textrenderer_bool( libvlc_media_player_t *p_mi,
+                                                    unsigned option );
+
+LIBVLC_API void libvlc_video_set_textrenderer_bool( libvlc_media_player_t *p_mi,
+                                                    unsigned option, bool value );
+
+LIBVLC_API int libvlc_video_get_textrenderer_int( libvlc_media_player_t *p_mi,
+                                                  unsigned option );
+
+LIBVLC_API void libvlc_video_set_textrenderer_int( libvlc_media_player_t *p_mi,
+                                                   unsigned option, int value );
+
+LIBVLC_API char *libvlc_video_get_textrenderer_string( libvlc_media_player_t *p_mi,
+                                                       unsigned option );
+
+LIBVLC_API void libvlc_video_set_textrenderer_string( libvlc_media_player_t *p_mi,
+                                                      unsigned option,
+                                                      const char *psz_text );
+
 /** @} video */
 
 /** \defgroup libvlc_audio LibVLC audio controls
diff --git a/lib/libvlc.sym b/lib/libvlc.sym
index 733a4dd..f297970 100644
--- a/lib/libvlc.sym
+++ b/lib/libvlc.sym
@@ -245,6 +245,9 @@ libvlc_video_get_spu_count
 libvlc_video_get_spu_delay
 libvlc_video_get_spu_description
 libvlc_video_get_teletext
+libvlc_video_get_textrenderer_bool
+libvlc_video_get_textrenderer_int
+libvlc_video_get_textrenderer_string
 libvlc_video_get_title_description
 libvlc_video_get_track
 libvlc_video_get_track_count
@@ -269,6 +272,9 @@ libvlc_video_set_spu
 libvlc_video_set_spu_delay
 libvlc_video_set_subtitle_file
 libvlc_video_set_teletext
+libvlc_video_set_textrenderer_bool
+libvlc_video_set_textrenderer_int
+libvlc_video_set_textrenderer_string
 libvlc_video_set_track
 libvlc_video_take_snapshot
 libvlc_vlm_add_broadcast
diff --git a/lib/media_player.c b/lib/media_player.c
index f2e9d03..8d54c93 100644
--- a/lib/media_player.c
+++ b/lib/media_player.c
@@ -670,6 +670,12 @@ libvlc_media_player_new( libvlc_instance_t *instance )
     var_Create (mp, "saturation", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT);
     var_Create (mp, "gamma", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT);
 
+    /* SPU */
+    var_Create (mp, "freetype-font", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
+    var_Create (mp, "freetype-rel-fontsize", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
+    var_Create (mp, "freetype-color", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
+    var_Create (mp, "freetype-bold", VLC_VAR_BOOL | VLC_VAR_DOINHERIT);
+
      /* Audio */
     var_Create (mp, "aout", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
     var_Create (mp, "audio-device", VLC_VAR_STRING);
diff --git a/lib/video.c b/lib/video.c
index b2c9b34..6af4e1c 100644
--- a/lib/video.c
+++ b/lib/video.c
@@ -635,7 +635,7 @@ static bool find_sub_source_by_name( libvlc_media_player_t *p_mi, const char *re
 }
 
 typedef const struct {
-    const char name[20];
+    const char name[25];
     unsigned type;
 } opt_t;
 
@@ -664,6 +664,9 @@ set_int( libvlc_media_player_t *p_mi, const char *restrict name,
         case VLC_VAR_FLOAT:
             var_SetFloat( p_mi, opt->name, value );
             break;
+        case VLC_VAR_BOOL:
+            var_SetBool( p_mi, opt->name, value );
+            break;
         default:
             libvlc_printerr( "Invalid argument to %s in %s", name, "set int" );
             return;
@@ -687,6 +690,8 @@ get_int( libvlc_media_player_t *p_mi, const char *restrict name,
         return var_GetInteger(p_mi, opt->name);
     case VLC_VAR_FLOAT:
         return lroundf(var_GetFloat(p_mi, opt->name));
+    case VLC_VAR_BOOL:
+        return var_GetBool(p_mi, opt->name);
     default:
         libvlc_printerr( "Invalid argument to %s in %s", name, "get int" );
         return 0;
@@ -910,3 +915,64 @@ float libvlc_video_get_adjust_float( libvlc_media_player_t *p_mi,
 {
     return get_float( p_mi, "adjust", adjust_option_bynumber(option) );
 }
+
+static const opt_t *
+textrenderer_option_bynumber( unsigned option )
+{
+    static const opt_t optlist[] =
+    {
+    { "freetype-font",            VLC_VAR_STRING },
+    { "freetype-rel-fontsize",    VLC_VAR_INTEGER },
+    { "freetype-color",           VLC_VAR_INTEGER },
+    { "freetype-bold",            VLC_VAR_BOOL },
+    };
+    enum { num_opts = sizeof(optlist) / sizeof(*optlist) };
+
+    const opt_t *r = option < num_opts ? optlist+option : NULL;
+    if( !r )
+        libvlc_printerr( "Unknown freetype option" );
+    return r;
+}
+
+/* basic text renderer support */
+
+void libvlc_video_set_textrenderer_bool( libvlc_media_player_t *p_mi,
+                                        unsigned option, bool value )
+{
+    set_int( p_mi, "freetype", textrenderer_option_bynumber(option), value );
+}
+
+
+bool libvlc_video_get_textrenderer_bool( libvlc_media_player_t *p_mi,
+                                         unsigned option )
+{
+    return get_int( p_mi, "freetype", textrenderer_option_bynumber(option) );
+}
+
+
+void libvlc_video_set_textrenderer_int( libvlc_media_player_t *p_mi,
+                                       unsigned option, int value )
+{
+    set_int( p_mi, "freetype", textrenderer_option_bynumber(option), value );
+}
+
+
+int libvlc_video_get_textrenderer_int( libvlc_media_player_t *p_mi,
+                                      unsigned option )
+{
+    return get_int( p_mi, "freetype", textrenderer_option_bynumber(option) );
+}
+
+
+void libvlc_video_set_textrenderer_string( libvlc_media_player_t *p_mi,
+                                          unsigned option, const char *psz_value )
+{
+    set_string( p_mi, "freetype", textrenderer_option_bynumber(option), psz_value );
+}
+
+
+char * libvlc_video_get_textrenderer_string( libvlc_media_player_t *p_mi,
+                                            unsigned option )
+{
+    return get_string( p_mi, "freetype", textrenderer_option_bynumber(option) );
+}
diff --git a/modules/text_renderer/freetype/freetype.c b/modules/text_renderer/freetype/freetype.c
index c75770a..66a4b62 100644
--- a/modules/text_renderer/freetype/freetype.c
+++ b/modules/text_renderer/freetype/freetype.c
@@ -77,6 +77,9 @@
  *****************************************************************************/
 static int  Create ( vlc_object_t * );
 static void Destroy( vlc_object_t * );
+static int DefaultFontStyleCallback( vlc_object_t *p_this, char const *psz_var,
+                                     vlc_value_t oldval, vlc_value_t newval,
+                                     void *p_data );
 
 #define FONT_TEXT N_("Font")
 #define MONOSPACE_FONT_TEXT N_("Monospace Font")
@@ -929,7 +932,7 @@ static void FillDefaultStyles( filter_t *p_filter )
 {
     filter_sys_t *p_sys = p_filter->p_sys;
 
-    p_sys->p_default_style->psz_fontname = var_InheritString( p_filter, "freetype-font" );
+    p_sys->p_default_style->psz_fontname = var_CreateGetString( p_filter, "freetype-font" );
     p_sys->p_default_style->psz_monofontname = var_InheritString( p_filter, "freetype-monofont" );
 
     p_sys->p_default_style->i_font_alpha = var_InheritInteger( p_filter,"freetype-opacity" );
@@ -937,6 +940,8 @@ static void FillDefaultStyles( filter_t *p_filter )
     p_sys->p_default_style->i_font_color = var_InheritInteger( p_filter,"freetype-color" );
     p_sys->p_default_style->i_font_color = VLC_CLIP( p_sys->p_default_style->i_font_color, 0, 0xFFFFFF );
 
+    p_sys->p_default_style->i_font_color = VLC_CLIP(var_CreateGetIntegerCommand( p_filter, "freetype-color") , 0, 0xFFFFFF);
+
     p_sys->p_default_style->i_outline_alpha = var_InheritInteger( p_filter, "freetype-outline-opacity" );
     p_sys->p_default_style->i_outline_alpha = VLC_CLIP( p_sys->p_default_style->i_outline_alpha, 0, 255 );
     p_sys->p_default_style->i_outline_color = var_InheritInteger( p_filter, "freetype-outline-color" );
@@ -952,11 +957,11 @@ static void FillDefaultStyles( filter_t *p_filter )
     p_sys->p_default_style->i_features |= STYLE_HAS_FLAGS;
 
     p_sys->p_forced_style->i_font_size = var_InheritInteger( p_filter, "freetype-fontsize" );
-    p_sys->p_forced_style->f_font_relsize = var_InheritInteger( p_filter, "freetype-rel-fontsize" );
+    p_sys->p_forced_style->f_font_relsize = var_CreateGetIntegerCommand( p_filter, "freetype-rel-fontsize" );
     if( p_sys->p_forced_style->f_font_relsize )
         p_sys->p_forced_style->f_font_relsize = 1.0 / p_sys->p_forced_style->f_font_relsize;
 
-    if( var_InheritBool( p_filter, "freetype-bold" ) )
+    if( var_CreateGetBoolCommand( p_filter, "freetype-bold" ) )
     {
         p_sys->p_forced_style->i_style_flags |= STYLE_BOLD;
         p_sys->p_forced_style->i_features |= STYLE_HAS_FLAGS;
@@ -964,6 +969,12 @@ static void FillDefaultStyles( filter_t *p_filter )
 
     /* Apply forced styles to defaults, if any */
     text_style_Merge( p_sys->p_default_style, p_sys->p_forced_style, true );
+
+    vlc_mutex_init( &p_sys->lock );
+    var_AddCallback( p_filter, "freetype-font", DefaultFontStyleCallback, p_sys );
+    var_AddCallback( p_filter, "freetype-rel-fontsize", DefaultFontStyleCallback, p_sys );
+    var_AddCallback( p_filter, "freetype-color", DefaultFontStyleCallback, p_sys );
+    var_AddCallback( p_filter, "freetype-bold", DefaultFontStyleCallback, p_sys );
 }
 
 static void FreeStylesArray( text_style_t **pp_styles, size_t i_styles )
@@ -1326,6 +1337,17 @@ static void Destroy( vlc_object_t *p_this )
     DumpDictionary( p_filter, &p_sys->fallback_map, true, -1 );
 #endif
 
+    var_DelCallback( p_filter, "freetype-font", DefaultFontStyleCallback, p_sys );
+    var_DelCallback( p_filter, "freetype-rel-fontsize", DefaultFontStyleCallback, p_sys );
+    var_DelCallback( p_filter, "freetype-color", DefaultFontStyleCallback, p_sys );
+    var_DelCallback( p_filter, "freetype-bold", DefaultFontStyleCallback, p_sys );
+    vlc_mutex_destroy( &p_sys->lock );
+
+    var_Destroy( p_filter, "freetype-font" ) ;
+    var_Destroy( p_filter, "freetype-rel-fontsize" );
+    var_Destroy( p_filter, "freetype-color" );
+    var_Destroy( p_filter, "freetype-bold" );
+
     /* Attachments */
     if( p_sys->pp_font_attachments )
     {
@@ -1355,3 +1377,34 @@ static void Destroy( vlc_object_t *p_this )
     free( p_sys );
 }
 
+static int DefaultFontStyleCallback( vlc_object_t *p_this, char const *psz_var,
+                                     vlc_value_t oldval, vlc_value_t newval,
+                                     void *p_data )
+{
+    VLC_UNUSED(oldval);
+    filter_t *p_filter = (filter_t *)p_this;
+    filter_sys_t *p_sys = (filter_sys_t *)p_data;
+
+    vlc_mutex_lock( &p_sys->lock );
+    if( !strcmp( psz_var, "freetype-font" ) ) {
+        FREENULL( p_sys->p_default_style->psz_fontname );
+        p_sys->p_default_style->psz_fontname = strdup( newval.psz_string );
+    } else  if( !strcmp( psz_var, "freetype-rel-fontsize" ) ) {
+        p_sys->p_forced_style->f_font_relsize = 1.0 / newval.i_int;
+    } else if( !strcmp( psz_var, "freetype-color" ) )
+        p_sys->p_default_style->i_font_color = VLC_CLIP(newval.i_int, 0, 0xFFFFFF);
+    else if ( !strcmp( psz_var, "freetype-bold" ) )
+    {
+        if (newval.b_bool) {
+            p_sys->p_forced_style->i_style_flags |= STYLE_BOLD;
+            p_sys->p_forced_style->i_features |= STYLE_HAS_FLAGS;
+        } else {
+            p_sys->p_forced_style->i_style_flags &= ~STYLE_BOLD;
+            p_sys->p_forced_style->i_features &= ~STYLE_HAS_FLAGS;
+        }
+    }
+    text_style_Merge( p_sys->p_default_style, p_sys->p_forced_style, true );
+    vlc_mutex_unlock( &p_sys->lock );
+
+    return VLC_SUCCESS;
+}
diff --git a/modules/text_renderer/freetype/freetype.h b/modules/text_renderer/freetype/freetype.h
index 44b2731..7f1abec 100644
--- a/modules/text_renderer/freetype/freetype.h
+++ b/modules/text_renderer/freetype/freetype.h
@@ -137,6 +137,8 @@ struct filter_sys_t
      */
     vlc_family_t * (*pf_get_fallbacks) ( filter_t *p_filter, const char *psz_family,
                                          uni_char_t codepoint );
+
+    vlc_mutex_t       lock;
 };
 
 /**
-- 
2.9.2

