[vlc-commits] freetype: use flags for font style

Francois Cartegnie git at videolan.org
Mon Aug 24 17:39:02 CEST 2020


vlc | branch: master | Francois Cartegnie <fcvlcdev at free.fr> | Wed Aug 12 17:06:28 2020 +0200| [a9678ca64e73b50cf1214de5bdddb89eb444f690] | committer: Francois Cartegnie

freetype: use flags for font style

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=a9678ca64e73b50cf1214de5bdddb89eb444f690
---

 modules/text_renderer/freetype/fonts/android.c    | 25 +++++---------
 modules/text_renderer/freetype/fonts/darwin.c     | 13 ++++----
 modules/text_renderer/freetype/fonts/dwrite.cpp   |  9 +++--
 modules/text_renderer/freetype/fonts/fontconfig.c |  4 +--
 modules/text_renderer/freetype/fonts/win32.c      | 11 ++++---
 modules/text_renderer/freetype/freetype.c         | 10 +++---
 modules/text_renderer/freetype/platform_fonts.c   | 40 ++++++++++++-----------
 modules/text_renderer/freetype/platform_fonts.h   | 14 ++++----
 8 files changed, 66 insertions(+), 60 deletions(-)

diff --git a/modules/text_renderer/freetype/fonts/android.c b/modules/text_renderer/freetype/fonts/android.c
index d1651ed083..40e47752b8 100644
--- a/modules/text_renderer/freetype/fonts/android.c
+++ b/modules/text_renderer/freetype/fonts/android.c
@@ -50,8 +50,7 @@
 static int Android_ParseFont( vlc_font_select_t *fs, xml_reader_t *p_xml,
                               vlc_family_t *p_family )
 {
-    bool              b_bold      = false;
-    bool              b_italic    = false;
+    int               i_flags     = 0;
     const char       *psz_val     = NULL;
     const char       *psz_attr    = NULL;
     int               i_type      = 0;
@@ -63,11 +62,11 @@ static int Android_ParseFont( vlc_font_select_t *fs, xml_reader_t *p_xml,
             i_weight = atoi( psz_val );
         else if( !strcasecmp( "style", psz_attr ) && psz_val && *psz_val )
             if( !strcasecmp( "italic", psz_val ) )
-                b_italic = true;
+                i_flags |= VLC_FONT_FLAG_ITALIC;
     }
 
     if( i_weight == 700 )
-        b_bold = true;
+        i_flags |= VLC_FONT_FLAG_BOLD;
 
     i_type = xml_ReaderNextNode( p_xml, &psz_val );
 
@@ -84,7 +83,7 @@ static int Android_ParseFont( vlc_font_select_t *fs, xml_reader_t *p_xml,
      */
     if( i_weight == 400 || i_weight == 700 )
         if( asprintf( &psz_fontfile, "%s/%s", SYSTEM_FONT_PATH, psz_val ) < 0
-         || !NewFont( psz_fontfile, 0, b_bold, b_italic, p_family ) )
+         || !NewFont( psz_fontfile, 0, i_flags, p_family ) )
             return VLC_ENOMEM;
 
     return VLC_SUCCESS;
@@ -224,8 +223,6 @@ static int Android_Legacy_ParseFamily( vlc_font_select_t *fs, xml_reader_t *p_xm
     vlc_family_t     *p_family    = NULL;
     char             *psz_lc      = NULL;
     int               i_counter   = 0;
-    bool              b_bold      = false;
-    bool              b_italic    = false;
     const char       *p_node      = NULL;
     int               i_type      = 0;
 
@@ -301,23 +298,19 @@ static int Android_Legacy_ParseFamily( vlc_font_select_t *fs, xml_reader_t *p_xm
                 if( unlikely( !p_family ) )
                     return VLC_ENOMEM;
 
+                int i_flags = 0;
                 switch( i_counter )
                 {
                 case 0:
-                    b_bold = false;
-                    b_italic = false;
                     break;
                 case 1:
-                    b_bold = true;
-                    b_italic = false;
+                    i_flags = VLC_FONT_FLAG_BOLD;
                     break;
                 case 2:
-                    b_bold = false;
-                    b_italic = true;
+                    i_flags = VLC_FONT_FLAG_ITALIC;
                     break;
                 case 3:
-                    b_bold = true;
-                    b_italic = true;
+                    i_flags = VLC_FONT_FLAG_BOLD | VLC_FONT_FLAG_ITALIC;
                     break;
                 default:
                     msg_Warn( fs->p_obj, "Android_ParseFamily: too many files" );
@@ -326,7 +319,7 @@ static int Android_Legacy_ParseFamily( vlc_font_select_t *fs, xml_reader_t *p_xm
 
                 char *psz_fontfile = NULL;
                 if( asprintf( &psz_fontfile, "%s/%s", SYSTEM_FONT_PATH, p_node ) < 0
-                 || !NewFont( psz_fontfile, 0, b_bold, b_italic, p_family ) )
+                 || !NewFont( psz_fontfile, 0, i_flags, p_family ) )
                     return VLC_ENOMEM;
 
                 ++i_counter;
diff --git a/modules/text_renderer/freetype/fonts/darwin.c b/modules/text_renderer/freetype/fonts/darwin.c
index 08a9f1c88d..53c8e5e724 100644
--- a/modules/text_renderer/freetype/fonts/darwin.c
+++ b/modules/text_renderer/freetype/fonts/darwin.c
@@ -58,29 +58,30 @@ static char* getPathForFontDescription(CTFontDescriptorRef fontDescriptor)
 
 static void addNewFontToFamily(vlc_font_select_t *fs, CTFontDescriptorRef iter, char *path, vlc_family_t *p_family)
 {
-    bool b_bold = false;
-    bool b_italic = false;
+    int i_flags = 0;
     CFDictionaryRef fontTraits = CTFontDescriptorCopyAttribute(iter, kCTFontTraitsAttribute);
     CFNumberRef trait = CFDictionaryGetValue(fontTraits, kCTFontWeightTrait);
     float traitValue = 0.;
     CFNumberGetValue(trait, kCFNumberFloatType, &traitValue);
-    b_bold = traitValue > 0.23;
+    if( traitValue > 0.23 )
+        i_flags |= VLC_FONT_FLAG_BOLD;
     trait = CFDictionaryGetValue(fontTraits, kCTFontSlantTrait);
     traitValue = 0.;
     CFNumberGetValue(trait, kCFNumberFloatType, &traitValue);
-    b_italic = traitValue > 0.03;
+    if( traitValue > 0.03 )
+        i_flags |= VLC_FONT_FLAG_ITALIC;
 
 #ifndef NDEBUG
     CFStringRef name = CTFontDescriptorCopyAttribute(iter, kCTFontNameAttribute);
     char *psz_name = name ? FromCFString(name, kCFStringEncodingUTF8) : 0;
-    msg_Dbg(fs->p_obj, "New font: (%s) bold %i italic %i path '%s'", psz_name, b_bold, b_italic, path);
+    msg_Dbg(fs->p_obj, "New font: (%s) %x path '%s'", psz_name, i_flags, path);
     free(psz_name);
     if(name)
         CFRelease(name);
 #else
     VLC_UNUSED(fs);
 #endif
-    NewFont(path, 0, b_bold, b_italic, p_family);
+    NewFont(path, 0, i_flags, p_family);
 
     CFRelease(fontTraits);
 }
diff --git a/modules/text_renderer/freetype/fonts/dwrite.cpp b/modules/text_renderer/freetype/fonts/dwrite.cpp
index 0d7092e604..3072ad1293 100644
--- a/modules/text_renderer/freetype/fonts/dwrite.cpp
+++ b/modules/text_renderer/freetype/fonts/dwrite.cpp
@@ -519,8 +519,11 @@ static void DWrite_ParseFamily( vlc_font_select_t *fs, IDWriteFontFamily *p_dw_f
         if( !p_dw_font )
             continue;
 
-        bool b_bold = i & 1;
-        bool b_italic = i & 2;
+        int i_flags = 0;
+        if( i & 1 )
+            i_flags |= VLC_FONT_FLAG_BOLD;
+        if( i & 2 )
+            i_flags |= VLC_FONT_FLAG_ITALIC;
 
         if( p_dw_font->CreateFontFace( p_dw_face.GetAddressOf() ) )
             throw runtime_error( "CreateFontFace() failed" );
@@ -567,7 +570,7 @@ static void DWrite_ParseFamily( vlc_font_select_t *fs, IDWriteFontFamily *p_dw_f
         if( asprintf( &psz_font_path, ":dw/%d", ( int ) streams.size() - 1 ) < 0 )
             throw bad_alloc();
 
-        NewFont( psz_font_path, i_font_index, b_bold, b_italic, p_family );
+        NewFont( psz_font_path, i_font_index, i_flags, p_family );
     }
 }
 
diff --git a/modules/text_renderer/freetype/fonts/fontconfig.c b/modules/text_renderer/freetype/fonts/fontconfig.c
index a1bd17b25b..acc048d31c 100644
--- a/modules/text_renderer/freetype/fonts/fontconfig.c
+++ b/modules/text_renderer/freetype/fonts/fontconfig.c
@@ -131,8 +131,8 @@ static void FontConfig_AddFromFcPattern( FcPattern *p_pat,  vlc_family_t *p_fami
     psz_fontfile = strdup( (const char*)val_s );
     if( psz_fontfile )
         NewFont( psz_fontfile, i_index,
-                 bold > FC_WEIGHT_NORMAL,
-                 italic != FC_SLANT_ROMAN,
+                 ((bold > FC_WEIGHT_NORMAL) ? VLC_FONT_FLAG_BOLD : 0) |
+                 ((italic != FC_SLANT_ROMAN) ? VLC_FONT_FLAG_ITALIC : 0),
                  p_family );
 }
 
diff --git a/modules/text_renderer/freetype/fonts/win32.c b/modules/text_renderer/freetype/fonts/win32.c
index 39e58b7cfc..3073bf2789 100644
--- a/modules/text_renderer/freetype/fonts/win32.c
+++ b/modules/text_renderer/freetype/fonts/win32.c
@@ -345,8 +345,11 @@ static int CALLBACK EnumFontCallback(const ENUMLOGFONTEX *lpelfe, const NEWTEXTM
     struct enumFontCallbackContext *ctx = ( struct enumFontCallbackContext * ) lParam;
     vlc_family_t *p_family = ctx->p_family;
 
-    bool b_bold = ( lpelfe->elfLogFont.lfWeight == FW_BOLD );
-    bool b_italic = ( lpelfe->elfLogFont.lfItalic != 0 );
+    int i_flags = 0;
+    if( lpelfe->elfLogFont.lfWeight == FW_BOLD )
+        i_flags |= VLC_FONT_FLAG_BOLD;
+    if( lpelfe->elfLogFont.lfItalic != 0 )
+        i_flags |= VLC_FONT_FLAG_ITALIC;
 
     /*
      * This function will be called by Windows as many times for each font
@@ -354,7 +357,7 @@ static int CALLBACK EnumFontCallback(const ENUMLOGFONTEX *lpelfe, const NEWTEXTM
      * Check to avoid duplicates.
      */
     for( vlc_font_t *p_font = p_family->p_fonts; p_font; p_font = p_font->p_next )
-        if( !!p_font->b_bold == !!b_bold && !!p_font->b_italic == !!b_italic )
+        if( p_font->i_flags == i_flags )
             return 1;
 
     char *psz_filename = NULL;
@@ -387,7 +390,7 @@ static int CALLBACK EnumFontCallback(const ENUMLOGFONTEX *lpelfe, const NEWTEXTM
             return 1;
     }
 
-    NewFont( psz_fontfile, i_index, b_bold, b_italic, p_family );
+    NewFont( psz_fontfile, i_index, i_flags, p_family );
 
     return 1;
 }
diff --git a/modules/text_renderer/freetype/freetype.c b/modules/text_renderer/freetype/freetype.c
index 36fa044f73..5326f3467c 100644
--- a/modules/text_renderer/freetype/freetype.c
+++ b/modules/text_renderer/freetype/freetype.c
@@ -315,9 +315,11 @@ static int LoadFontsFromAttachments( filter_t *p_filter )
                                             i_font_idx,
                                             &p_face ))
             {
-
-                bool b_bold = p_face->style_flags & FT_STYLE_FLAG_BOLD;
-                bool b_italic = p_face->style_flags & FT_STYLE_FLAG_ITALIC;
+                int i_flags = 0;
+                if( p_face->style_flags & FT_STYLE_FLAG_BOLD )
+                    i_flags |= VLC_FONT_FLAG_BOLD;
+                if( p_face->style_flags & FT_STYLE_FLAG_ITALIC )
+                    i_flags |= VLC_FONT_FLAG_ITALIC;
 
                 vlc_family_t *p_family = DeclareNewFamily( p_sys->fs, p_face->family_name );
                 if( unlikely( !p_family ) )
@@ -326,7 +328,7 @@ static int LoadFontsFromAttachments( filter_t *p_filter )
                 char *psz_fontfile;
                 if( asprintf( &psz_fontfile, ":/%d",
                               p_sys->i_font_attachments - 1 ) < 0
-                 || !NewFont( psz_fontfile, i_font_idx, b_bold, b_italic, p_family ) )
+                 || !NewFont( psz_fontfile, i_font_idx, i_flags, p_family ) )
                     goto error;
 
                 FT_Done_Face( p_face );
diff --git a/modules/text_renderer/freetype/platform_fonts.c b/modules/text_renderer/freetype/platform_fonts.c
index 8d601fd142..81fd3a1fc8 100644
--- a/modules/text_renderer/freetype/platform_fonts.c
+++ b/modules/text_renderer/freetype/platform_fonts.c
@@ -121,7 +121,7 @@ bool CheckFace( vlc_font_select_t *fs, vlc_font_t *p_font, uni_char_t codepoint
  * be returned, because the italic font of Arial has no Arabic support.
  */
 static vlc_font_t *GetBestFont( vlc_font_select_t *fs, const vlc_family_t *p_family,
-                                bool b_bold, bool b_italic, uni_char_t codepoint )
+                                int i_flags, uni_char_t codepoint )
 {
     int i_best_score = 0;
     vlc_font_t *p_best_font = p_family->p_fonts;
@@ -133,9 +133,10 @@ static vlc_font_t *GetBestFont( vlc_font_select_t *fs, const vlc_family_t *p_fam
         if( codepoint && CheckFace( fs, p_font, codepoint ) )
             i_score += 1000;
 
-        if( !!p_font->b_bold == !!b_bold )
+        int i_sameflags = !(p_font->i_flags ^ i_flags);
+        if( i_sameflags & VLC_FONT_FLAG_BOLD )
             i_score += 100;
-        if( !!p_font->b_italic == !!b_italic )
+        if( i_sameflags & VLC_FONT_FLAG_ITALIC )
             i_score += 10;
 
         if( i_score > i_best_score )
@@ -323,8 +324,7 @@ int DeclareFamilyAsAttachMenFallback( vlc_font_select_t *fs, vlc_family_t *p_fam
 }
 
 vlc_font_t *NewFont( char *psz_fontfile, int i_index,
-                     bool b_bold, bool b_italic,
-                     vlc_family_t *p_parent )
+                     int i_flags, vlc_family_t *p_parent )
 {
     vlc_font_t *p_font = calloc( 1, sizeof( *p_font ) );
 
@@ -336,15 +336,14 @@ vlc_font_t *NewFont( char *psz_fontfile, int i_index,
 
     p_font->psz_fontfile = psz_fontfile;
     p_font->i_index = i_index;
-    p_font->b_bold = b_bold;
-    p_font->b_italic = b_italic;
+    p_font->i_flags = i_flags;
 
     if( p_parent )
     {
         /* Keep regular faces first */
         if( p_parent->p_fonts
-         && ( p_parent->p_fonts->b_bold || p_parent->p_fonts->b_italic )
-         && !b_bold && !b_italic )
+         && p_parent->p_fonts->i_flags
+         && !i_flags )
         {
             p_font->p_next = p_parent->p_fonts;
             p_parent->p_fonts = p_font;
@@ -441,13 +440,13 @@ static void DumpFamily( vlc_object_t *p_obj, const vlc_family_t *p_family,
             for( vlc_font_t *p_font = p_family->p_fonts; p_font; p_font = p_font->p_next )
             {
                 const char *psz_style = NULL;
-                if( !p_font->b_bold && !p_font->b_italic )
+                if( p_font->i_flags == 0 )
                     psz_style = "Regular";
-                else if( p_font->b_bold && !p_font->b_italic )
+                else if( p_font->i_flags == VLC_FONT_FLAG_BOLD )
                     psz_style = "Bold";
-                else if( !p_font->b_bold && p_font->b_italic )
+                else if( p_font->i_flags == VLC_FONT_FLAG_ITALIC )
                     psz_style = "Italic";
-                else if( p_font->b_bold && p_font->b_italic )
+                else if( p_font->i_flags == (VLC_FONT_FLAG_ITALIC|VLC_FONT_FLAG_BOLD) )
                     psz_style = "Bold Italic";
 
                 msg_Dbg( p_obj, "\t\t[%p] (%s): %s - %d", (void *)p_font,
@@ -612,8 +611,6 @@ static char* SelectFontWithFamilyFallback( vlc_font_select_t *fs,
                                     const text_style_t *p_style,
                                     int *pi_idx, uni_char_t codepoint )
 {
-    const bool b_bold = p_style->i_style_flags & STYLE_BOLD;
-    const bool b_italic = p_style->i_style_flags & STYLE_ITALIC;
     const vlc_family_t *p_family = NULL;
 
     if( codepoint && !p_family )
@@ -714,11 +711,16 @@ static char* SelectFontWithFamilyFallback( vlc_font_select_t *fs,
         free(lc);
     }
 
+    int i_flags = 0;
+    if( p_style->i_style_flags & STYLE_BOLD )
+        i_flags |= VLC_FONT_FLAG_BOLD;
+    if( p_style->i_style_flags & STYLE_ITALIC )
+        i_flags |= VLC_FONT_FLAG_ITALIC;
+
     vlc_font_t *p_font;
-    if( p_family && ( p_font = GetBestFont( fs, p_family, b_bold,
-                                            b_italic, codepoint ) ) )
+    if( p_family && ( p_font = GetBestFont( fs, p_family, i_flags, codepoint ) ) )
     {
-        Debug( fs->p_obj, "Selected best font file \"%s\" %d %d", p_font->psz_fontfile, b_bold, b_italic );
+        Debug( fs->p_obj, "Selected best font file \"%s\" %x", p_font->psz_fontfile, i_flags );
         *pi_idx = p_font->i_index;
         return strdup( p_font->psz_fontfile );
     }
@@ -829,7 +831,7 @@ static int StaticMap_GetFamily( vlc_font_select_t *fs, const char *psz_lcname,
 
     char *psz_font_file = MakeFilePath( fs, psz_file );
     if( psz_font_file )
-        NewFont( psz_font_file, 0, false, false, p_family );
+        NewFont( psz_font_file, 0, 0, p_family );
 
     *pp_result = p_family;
     return VLC_SUCCESS;
diff --git a/modules/text_renderer/freetype/platform_fonts.h b/modules/text_renderer/freetype/platform_fonts.h
index 3d93ad0d59..7b91250e2b 100644
--- a/modules/text_renderer/freetype/platform_fonts.h
+++ b/modules/text_renderer/freetype/platform_fonts.h
@@ -120,6 +120,11 @@ extern "C" {
 /**
  * Representation of the fonts (linked-list)
  */
+enum
+{
+    VLC_FONT_FLAG_BOLD    = 1 << 0,
+    VLC_FONT_FLAG_ITALIC  = 1 << 1,
+};
 typedef struct vlc_font_t vlc_font_t;
 struct vlc_font_t
 {
@@ -130,8 +135,7 @@ struct vlc_font_t
      */
     char       *psz_fontfile;
     int         i_index;   /**< index of the font in the font file, starts at 0 */
-    bool        b_bold;    /**< if the font is a bold version */
-    bool        b_italic;  /**< if the font is an italic version */
+    int         i_flags;
     void       *faceid;    /* fontloader ref to font */
 };
 
@@ -224,8 +228,7 @@ char *CreateUniqueFamilyKey( vlc_font_select_t * );
  * \param psz_fontfile font file, or ":/x" for font attachments, where x is the attachment index
  *        within \ref filter_sys_t::pp_font_attachments [IN]
  * \param i_index index of the font in the font file [IN]
- * \param b_bold is a bold font or not [IN]
- * \param b_bold is an italic or not [IN]
+ * \param i_flags is font style flags [IN]
  * \param p_parent parent family.
  *                 If not NULL, the font will be associated to this family, and
  *                 appended to the font list in that family [IN]
@@ -234,8 +237,7 @@ char *CreateUniqueFamilyKey( vlc_font_select_t * );
  * \return the new font
  */
 vlc_font_t *NewFont( char *psz_fontfile, int i_index,
-                     bool b_bold, bool b_italic,
-                     vlc_family_t *p_parent );
+                     int i_flags, vlc_family_t *p_parent );
 
 /**
  * Free families and fonts associated.



More information about the vlc-commits mailing list