Treat -font/-subfont as Fontconfig pattern in libass.
authoreugeni <eugeni@b3059339-0415-0410-9bf9-f77b7e298cf2>
Sun, 8 Mar 2009 16:13:21 +0000 (16:13 +0000)
committereugeni <eugeni@b3059339-0415-0410-9bf9-f77b7e298cf2>
Sun, 8 Mar 2009 16:13:21 +0000 (16:13 +0000)
Patch by Adrian Stutz (adrian sttz ch).

git-svn-id: svn://git.mplayerhq.hu/mplayer/trunk@28895 b3059339-0415-0410-9bf9-f77b7e298cf2

DOCS/man/en/mplayer.1
libass/ass_cache.c
libass/ass_font.c
libass/ass_font.h
libass/ass_fontconfig.c
libass/ass_fontconfig.h
libass/ass_mp.c
libass/ass_render.c
libass/ass_types.h

index dcb4214..a66134e 100644 (file)
@@ -2253,14 +2253,14 @@ Use this if commas in subtitles are shown at the start of a sentence
 instead of at the end.
 .
 .TP
-.B \-font <path to font.desc file>
+.B \-font <path to font.desc file, path to font (FreeType), font pattern (Fontconfig)>
 Search for the OSD/\:SUB fonts in an alternative directory (default for normal
 fonts: ~/\:.mplayer/\:font/\:font.desc, default for FreeType fonts:
 ~/.mplayer/\:subfont.ttf).
 .br
 .I NOTE:
 With FreeType, this option determines the path to the text font file.
-With fontconfig, this option determines the fontconfig font name.
+With Fontconfig, this option determines the Fontconfig font pattern.
 .sp 1
 .I EXAMPLE:
 .PD 0
@@ -2270,6 +2270,8 @@ With fontconfig, this option determines the fontconfig font name.
 \-font ~/\:.mplayer/\:arialuni.ttf
 .br
 \-font 'Bitstream Vera Sans'
+.br
+\-font 'Bitstream Vera Sans:style=Bold'
 .RE
 .PD 1
 .
@@ -2508,8 +2510,8 @@ Currently useless.
 Same as \-audiofile, but for subtitle streams (OggDS?).
 .
 .TP
-.B \-subfont <filename> (FreeType only)
-Sets the subtitle font.
+.B \-subfont <path to font (FreeType), font pattern (Fontconfig)> (FreeType only)
+Sets the subtitle font (see \-font).
 If no \-subfont is given, \-font is used.
 .
 .TP
index 0561874..3b90e66 100644 (file)
@@ -192,6 +192,8 @@ static int font_compare(void* key1, void* key2, size_t key_size) {
                return 0;
        if (a->italic != b->italic)
                return 0;
+       if (a->treat_family_as_pattern != b->treat_family_as_pattern)
+               return 0;
        return 1;
 }
 
index f2214a3..579e658 100644 (file)
@@ -125,7 +125,7 @@ static int add_face(void* fc_priv, ass_font_t* font, uint32_t ch)
        if (font->n_faces == ASS_FONT_MAX_FACES)
                return -1;
        
-       path = fontconfig_select(fc_priv, font->desc.family, font->desc.bold,
+       path = fontconfig_select(fc_priv, font->desc.family, font->desc.treat_family_as_pattern, font->desc.bold,
                                              font->desc.italic, &index, ch);
 
        mem_idx = find_font(font->library, path);
@@ -169,6 +169,7 @@ ass_font_t* ass_font_new(ass_library_t* library, FT_Library ftlibrary, void* fc_
        font.ftlibrary = ftlibrary;
        font.n_faces = 0;
        font.desc.family = strdup(desc->family);
+       font.desc.treat_family_as_pattern = desc->treat_family_as_pattern;
        font.desc.bold = desc->bold;
        font.desc.italic = desc->italic;
 
index 03396a6..5204318 100644 (file)
@@ -33,6 +33,7 @@ typedef struct ass_font_desc_s {
        char* family;
        unsigned bold;
        unsigned italic;
+       int treat_family_as_pattern;
 } ass_font_desc_t;
 
 #define ASS_FONT_MAX_FACES 10
index 9a7bdde..9bdca86 100644 (file)
@@ -66,14 +66,15 @@ struct fc_instance_s {
  * \brief Low-level font selection.
  * \param priv private data
  * \param family font family
+ * \param treat_family_as_pattern treat family as fontconfig pattern
  * \param bold font weight value
  * \param italic font slant value
  * \param index out: font index inside a file
  * \param code: the character that should be present in the font, can be 0
  * \return font file path
 */ 
-static char* _select_font(fc_instance_t* priv, const char* family, unsigned bold, unsigned italic, int* index,
-                         uint32_t code)
+static char* _select_font(fc_instance_t* priv, const char* family, int treat_family_as_pattern,
+                         unsigned bold, unsigned italic, int* index, uint32_t code)
 {
        FcBool rc;
        FcResult result;
@@ -89,10 +90,15 @@ static char* _select_font(fc_instance_t* priv, const char* family, unsigned bold
        
        *index = 0;
 
-       pat = FcPatternCreate();
+       if (treat_family_as_pattern)
+               pat = FcNameParse((const FcChar8*)family);
+       else
+               pat = FcPatternCreate();
+
        if (!pat)
                goto error;
        
+       if (!treat_family_as_pattern) {
        FcPatternAddString(pat, FC_FAMILY, (const FcChar8*)family);
 
        // In SSA/ASS fonts are sometimes referenced by their "full name",
@@ -118,6 +124,7 @@ static char* _select_font(fc_instance_t* priv, const char* family, unsigned bold
                        }
                free(s);
        }
+       }
        FcPatternAddBool(pat, FC_OUTLINE, FcTrue);
        FcPatternAddInteger(pat, FC_SLANT, italic);
        FcPatternAddInteger(pat, FC_WEIGHT, bold);
@@ -182,7 +189,8 @@ static char* _select_font(fc_instance_t* priv, const char* family, unsigned bold
        if (result != FcResultMatch)
                r_fullname = NULL;
 
-       if (!(r_family && strcasecmp((const char*)r_family, family) == 0) &&
+       if (!treat_family_as_pattern &&
+               !(r_family && strcasecmp((const char*)r_family, family) == 0) &&
            !(r_fullname && strcasecmp((const char*)r_fullname, family) == 0))
                mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_SelectedFontFamilyIsNotTheRequestedOne,
                       (const char*)(r_fullname ? r_fullname : r_family), family);
@@ -219,14 +227,15 @@ static char* _select_font(fc_instance_t* priv, const char* family, unsigned bold
  * \brief Find a font. Use default family or path if necessary.
  * \param priv_ private data
  * \param family font family
+ * \param treat_family_as_pattern treat family as fontconfig pattern
  * \param bold font weight value
  * \param italic font slant value
  * \param index out: font index inside a file
  * \param code: the character that should be present in the font, can be 0
  * \return font file path
 */ 
-char* fontconfig_select(fc_instance_t* priv, const char* family, unsigned bold, unsigned italic, int* index,
-                       uint32_t code)
+char* fontconfig_select(fc_instance_t* priv, const char* family, int treat_family_as_pattern,
+                       unsigned bold, unsigned italic, int* index, uint32_t code)
 {
        char* res = 0;
        if (!priv->config) {
@@ -234,9 +243,9 @@ char* fontconfig_select(fc_instance_t* priv, const char* family, unsigned bold,
                return priv->path_default;
        }
        if (family && *family)
-               res = _select_font(priv, family, bold, italic, index, code);
+               res = _select_font(priv, family, treat_family_as_pattern, bold, italic, index, code);
        if (!res && priv->family_default) {
-               res = _select_font(priv, priv->family_default, bold, italic, index, code);
+               res = _select_font(priv, priv->family_default, 0, bold, italic, index, code);
                if (res)
                        mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_UsingDefaultFontFamily, 
                                        family, bold, italic, res, *index);
@@ -248,7 +257,7 @@ char* fontconfig_select(fc_instance_t* priv, const char* family, unsigned bold,
                       family, bold, italic, res, *index);
        }
        if (!res) {
-               res = _select_font(priv, "Arial", bold, italic, index, code);
+               res = _select_font(priv, "Arial", 0, bold, italic, index, code);
                if (res)
                        mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_UsingArialFontFamily, 
                                        family, bold, italic, res, *index);
@@ -474,8 +483,8 @@ exit:
 
 #else /* CONFIG_FONTCONFIG */
 
-char* fontconfig_select(fc_instance_t* priv, const char* family, unsigned bold, unsigned italic, int* index,
-                       uint32_t code)
+char* fontconfig_select(fc_instance_t* priv, const char* family, int treat_family_as_pattern,
+                       unsigned bold, unsigned italic, int* index, uint32_t code)
 {
        *index = priv->index_default;
        return priv->path_default;
index e9a1ce1..7780690 100644 (file)
@@ -35,7 +35,7 @@
 typedef struct fc_instance_s fc_instance_t;
 
 fc_instance_t* fontconfig_init(ass_library_t* library, FT_Library ftlibrary, const char* family, const char* path, int fc);
-char* fontconfig_select(fc_instance_t* priv, const char* family, unsigned bold, unsigned italic, int* index, uint32_t code);
+char* fontconfig_select(fc_instance_t* priv, const char* family, int treat_family_as_pattern, unsigned bold, unsigned italic, int* index, uint32_t code);
 void fontconfig_done(fc_instance_t* priv);
 
 #endif /* LIBASS_FONTCONFIG_H */
index 40da63c..7e25b8e 100644 (file)
@@ -94,6 +94,7 @@ ass_track_t* ass_default_track(ass_library_t* library) {
                style = track->styles + sid;
                style->Name = strdup("Default");
                style->FontName = (font_fontconfig >= 0 && sub_font_name) ? strdup(sub_font_name) : (font_fontconfig >= 0 && font_name) ? strdup(font_name) : strdup("Sans");
+               style->treat_fontname_as_pattern = 1;
 
                fs = track->PlayResY * text_font_scale_factor / 100.;
                // approximate autoscale coefficients
index dec08e5..b561b2a 100644 (file)
@@ -184,6 +184,7 @@ typedef struct render_context_s {
        char* family;
        unsigned bold;
        unsigned italic;
+       int treat_family_as_pattern;
        
 } render_context_t;
 
@@ -676,6 +677,7 @@ static void update_font(void)
        ass_renderer_t* priv = frame_context.ass_priv;
        ass_font_desc_t desc;
        desc.family = strdup(render_context.family);
+       desc.treat_family_as_pattern = render_context.treat_family_as_pattern;
 
        val = render_context.bold;
        // 0 = normal, 1 = bold, >1 = exact weight
@@ -1359,6 +1361,7 @@ static void reset_render_context(void)
        if (render_context.family)
                free(render_context.family);
        render_context.family = strdup(render_context.style->FontName);
+       render_context.treat_family_as_pattern = render_context.style->treat_fontname_as_pattern;
        render_context.bold = render_context.style->Bold;
        render_context.italic = render_context.style->Italic;
        update_font();
index 52aff0f..01c36c5 100644 (file)
@@ -58,6 +58,7 @@ typedef struct ass_style_s {
        int MarginV;
 //        int AlphaLevel;
        int Encoding;
+       int treat_fontname_as_pattern;
 } ass_style_t;
 
 typedef struct render_priv_s render_priv_t;