font->v.x = font->v.y = 0;
font->size = 0;
+#ifdef HAVE_FONTCONFIG
+ font->charset = FcCharSetCreate();
+#endif
+
ass_font_cache_add(font);
return font;
}
}
+#ifdef HAVE_FONTCONFIG
+static void ass_font_reselect(void* fontconfig_priv, ass_font_t* font)
+{
+ char* path;
+ int index;
+ FT_Face face;
+ int error;
+
+ path = fontconfig_select_with_charset(fontconfig_priv, font->desc.family, font->desc.bold,
+ font->desc.italic, &index, font->charset);
+ if (strcasecmp(path, font->path) == 0 && index == font->index) {
+ free(path);
+ return;
+ }
+
+ error = FT_New_Face(font->ftlibrary, path, index, &face);
+ if (error) {
+ mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_ErrorOpeningFont, path, index);
+ return;
+ }
+ charmap_magic(face);
+
+ if (font->face) FT_Done_Face(font->face);
+ free(font->path);
+
+ font->face = face;
+ font->path = strdup(path);
+ font->index = index;
+
+ FT_Set_Transform(font->face, &font->m, &font->v);
+ FT_Set_Pixel_Sizes(font->face, 0, font->size);
+}
+#endif
+
FT_Glyph ass_font_get_glyph(void* fontconfig_priv, ass_font_t* font, uint32_t ch)
{
int error;
return 0;
index = FT_Get_Char_Index(font->face, ch);
+#ifdef HAVE_FONTCONFIG
+ FcCharSetAddChar(font->charset, ch);
+ if (index == 0) {
+ mp_msg(MSGT_ASS, MSGL_INFO, MSGTR_LIBASS_GlyphNotFoundReselectingFont,
+ ch, font->desc.family, font->desc.bold, font->desc.italic);
+ ass_font_reselect(fontconfig_priv, font);
+ index = FT_Get_Char_Index(font->face, ch);
+ if (index == 0) {
+ mp_msg(MSGT_ASS, MSGL_ERR, MSGTR_LIBASS_GlyphNotFound,
+ ch, font->desc.family, font->desc.bold, font->desc.italic);
+ }
+ }
+#endif
+
error = FT_Load_Glyph(font->face, index, FT_LOAD_NO_BITMAP );
if (error) {
mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_ErrorLoadingGlyph);
if (font->face) FT_Done_Face(font->face);
if (font->path) free(font->path);
if (font->desc.family) free(font->desc.family);
+#ifdef HAVE_FONTCONFIG
+ if (font->charset) FcCharSetDestroy(font->charset);
+#endif
free(font);
}
* \param index out: font index inside a file
* \return font file path
*/
-static char* _select_font(fc_instance_t* priv, const char* family, unsigned bold, unsigned italic, int* index)
+static char* _select_font(fc_instance_t* priv, const char* family, unsigned bold, unsigned italic, int* index,
+ FcCharSet* charset)
{
FcBool rc;
FcResult result;
int val_i;
FcChar8* val_s;
FcBool val_b;
+ FcCharSet* val_cs;
+ FcFontSet* fset;
+ int curf, bestf, bestdiff = 0;
*index = 0;
rc = FcConfigSubstitute(priv->config, pat, FcMatchPattern);
if (!rc)
return 0;
-
- rpat = FcFontMatch(priv->config, pat, &result);
- if (!rpat)
- return 0;
-
- result = FcPatternGetBool(rpat, FC_OUTLINE, 0, &val_b);
- if (result != FcResultMatch)
- return 0;
- if (val_b != FcTrue)
+
+ fset = FcFontSort(priv->config, pat, FcTrue, NULL, &result);
+
+ bestf = -1;
+ if (charset)
+ bestdiff = FcCharSetCount(charset) + 1;
+ for (curf = 0; curf < fset->nfont; ++curf) {
+ rpat = fset->fonts[curf];
+
+ result = FcPatternGetBool(rpat, FC_OUTLINE, 0, &val_b);
+ if (result != FcResultMatch)
+ continue;
+ if (val_b != FcTrue)
+ continue;
+
+ if (charset) {
+ int diff;
+ result = FcPatternGetCharSet(rpat, FC_CHARSET, 0, &val_cs);
+ if (result != FcResultMatch)
+ continue;
+ diff = FcCharSetSubtractCount(charset, val_cs);
+ if (diff < bestdiff) {
+ bestdiff = diff;
+ bestf = curf;
+ }
+ if (diff == 0)
+ break;
+ } else {
+ bestf = curf;
+ break;
+ }
+ }
+
+ if (bestf < 0)
return 0;
+
+ rpat = fset->fonts[bestf];
result = FcPatternGetInteger(rpat, FC_INDEX, 0, &val_i);
if (result != FcResultMatch)
* \param index out: font index inside a file
* \return font file path
*/
-char* fontconfig_select(fc_instance_t* priv, const char* family, unsigned bold, unsigned italic, int* index)
+char* fontconfig_select_with_charset(fc_instance_t* priv, const char* family, unsigned bold, unsigned italic, int* index,
+ FcCharSet* charset)
{
char* res = 0;
if (family && *family)
- res = _select_font(priv, family, bold, italic, index);
+ res = _select_font(priv, family, bold, italic, index, charset);
if (!res && priv->family_default) {
- res = _select_font(priv, priv->family_default, bold, italic, index);
+ res = _select_font(priv, priv->family_default, bold, italic, index, charset);
if (res)
mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_UsingDefaultFontFamily,
family, bold, italic, res, *index);
family, bold, italic, res, *index);
}
if (!res) {
- res = _select_font(priv, "Arial", bold, italic, index);
+ res = _select_font(priv, "Arial", bold, italic, index, charset);
if (res)
mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_UsingArialFontFamily,
family, bold, italic, res, *index);
return res;
}
+char* fontconfig_select(fc_instance_t* priv, const char* family, unsigned bold, unsigned italic, int* index)
+{
+ return fontconfig_select_with_charset(priv, family, bold, italic, index, 0);
+}
+
/**
* \brief Init fontconfig.
* \param dir additional directoryu for fonts