changeset 28860:7fcc0bf5b27a

Treat -font/-subfont as Fontconfig pattern in libass. Patch by Adrian Stutz (adrian sttz ch).
author eugeni
date Sun, 08 Mar 2009 16:13:21 +0000
parents 15a7ea3fe2e0
children 5e03bb996d31
files 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
diffstat 9 files changed, 37 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/DOCS/man/en/mplayer.1	Sun Mar 08 15:56:20 2009 +0000
+++ b/DOCS/man/en/mplayer.1	Sun Mar 08 16:13:21 2009 +0000
@@ -2253,14 +2253,14 @@
 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 @@
 \-font ~/\:.mplayer/\:arialuni.ttf
 .br
 \-font 'Bitstream Vera Sans'
+.br
+\-font 'Bitstream Vera Sans:style=Bold'
 .RE
 .PD 1
 .
@@ -2508,8 +2510,8 @@
 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
--- a/libass/ass_cache.c	Sun Mar 08 15:56:20 2009 +0000
+++ b/libass/ass_cache.c	Sun Mar 08 16:13:21 2009 +0000
@@ -192,6 +192,8 @@
 		return 0;
 	if (a->italic != b->italic)
 		return 0;
+	if (a->treat_family_as_pattern != b->treat_family_as_pattern)
+		return 0;
 	return 1;
 }
 
--- a/libass/ass_font.c	Sun Mar 08 15:56:20 2009 +0000
+++ b/libass/ass_font.c	Sun Mar 08 16:13:21 2009 +0000
@@ -125,7 +125,7 @@
 	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 @@
 	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;
 
--- a/libass/ass_font.h	Sun Mar 08 15:56:20 2009 +0000
+++ b/libass/ass_font.h	Sun Mar 08 16:13:21 2009 +0000
@@ -33,6 +33,7 @@
 	char* family;
 	unsigned bold;
 	unsigned italic;
+	int treat_family_as_pattern;
 } ass_font_desc_t;
 
 #define ASS_FONT_MAX_FACES 10
--- a/libass/ass_fontconfig.c	Sun Mar 08 15:56:20 2009 +0000
+++ b/libass/ass_fontconfig.c	Sun Mar 08 16:13:21 2009 +0000
@@ -66,14 +66,15 @@
  * \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 @@
 	
 	*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 @@
 			}
 		free(s);
 	}
+	}
 	FcPatternAddBool(pat, FC_OUTLINE, FcTrue);
 	FcPatternAddInteger(pat, FC_SLANT, italic);
 	FcPatternAddInteger(pat, FC_WEIGHT, bold);
@@ -182,7 +189,8 @@
 	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 @@
  * \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 @@
 		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 @@
 		       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 @@
 
 #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;
--- a/libass/ass_fontconfig.h	Sun Mar 08 15:56:20 2009 +0000
+++ b/libass/ass_fontconfig.h	Sun Mar 08 16:13:21 2009 +0000
@@ -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 */
--- a/libass/ass_mp.c	Sun Mar 08 15:56:20 2009 +0000
+++ b/libass/ass_mp.c	Sun Mar 08 16:13:21 2009 +0000
@@ -94,6 +94,7 @@
 		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
--- a/libass/ass_render.c	Sun Mar 08 15:56:20 2009 +0000
+++ b/libass/ass_render.c	Sun Mar 08 16:13:21 2009 +0000
@@ -184,6 +184,7 @@
 	char* family;
 	unsigned bold;
 	unsigned italic;
+	int treat_family_as_pattern;
 	
 } render_context_t;
 
@@ -676,6 +677,7 @@
 	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 @@
 	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();
--- a/libass/ass_types.h	Sun Mar 08 15:56:20 2009 +0000
+++ b/libass/ass_types.h	Sun Mar 08 16:13:21 2009 +0000
@@ -58,6 +58,7 @@
 	int MarginV;
 //        int AlphaLevel;
 	int Encoding;
+	int treat_fontname_as_pattern;
 } ass_style_t;
 
 typedef struct render_priv_s render_priv_t;