summaryrefslogtreecommitdiffstats
path: root/libass/ass_fontconfig.c
diff options
context:
space:
mode:
Diffstat (limited to 'libass/ass_fontconfig.c')
-rw-r--r--libass/ass_fontconfig.c705
1 files changed, 224 insertions, 481 deletions
diff --git a/libass/ass_fontconfig.c b/libass/ass_fontconfig.c
index f0e9000..184f090 100644
--- a/libass/ass_fontconfig.c
+++ b/libass/ass_fontconfig.c
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2006 Evgeniy Stepanov <eugeni.stepanov@gmail.com>
+ * Copyright (C) 2011 Grigori Goronzy <greg@chown.ath.cx>
*
* This file is part of libass.
*
@@ -18,545 +18,288 @@
#include "config.h"
-#include <stdlib.h>
+#ifdef CONFIG_FONTCONFIG
+
#include <stdio.h>
-#include <assert.h>
-#include <string.h>
-#include <strings.h>
+#include <stdlib.h>
#include <sys/types.h>
-#include <sys/stat.h>
-#include <inttypes.h>
-#include <ft2build.h>
-#include FT_FREETYPE_H
-#include "ass_utils.h"
-#include "ass.h"
-#include "ass_library.h"
-#include "ass_fontconfig.h"
-
-#ifdef CONFIG_FONTCONFIG
#include <fontconfig/fontconfig.h>
#include <fontconfig/fcfreetype.h>
-#endif
-struct fc_instance {
-#ifdef CONFIG_FONTCONFIG
- FcConfig *config;
-#endif
- char *family_default;
- char *path_default;
- int index_default;
-};
+#include "ass_fontconfig.h"
+#include "ass_fontselect.h"
+#include "ass_utils.h"
-#ifdef CONFIG_FONTCONFIG
+#define MAX_NAME 100
-/**
- * \brief Case-insensitive match ASS/SSA font family against full name. (also
- * known as "name for humans")
- *
- * \param lib library instance
- * \param priv fontconfig instance
- * \param family font fullname
- * \param bold weight attribute
- * \param italic italic attribute
- * \return font set
- */
-static FcFontSet *
-match_fullname(ASS_Library *lib, FCInstance *priv, const char *family,
- unsigned bold, unsigned italic)
+typedef struct fc_private {
+ FcConfig *config;
+ FcFontSet *fallbacks;
+ FcCharSet *fallback_chars;
+} ProviderPrivate;
+
+static int check_glyph(void *priv, uint32_t code)
{
- FcFontSet *sets[2];
- FcFontSet *result = FcFontSetCreate();
- int nsets = 0;
- int i, fi;
+ FcPattern *pat = (FcPattern *)priv;
+ FcCharSet *charset;
- if (!result)
- return NULL;
+ if (!pat)
+ return 1;
- if ((sets[nsets] = FcConfigGetFonts(priv->config, FcSetSystem)))
- nsets++;
- if ((sets[nsets] = FcConfigGetFonts(priv->config, FcSetApplication)))
- nsets++;
-
- // Run over font sets and patterns and try to match against full name
- for (i = 0; i < nsets; i++) {
- FcFontSet *set = sets[i];
- for (fi = 0; fi < set->nfont; fi++) {
- FcPattern *pat = set->fonts[fi];
- char *fullname;
- int pi = 0, at;
- FcBool ol;
- while (FcPatternGetString(pat, FC_FULLNAME, pi++,
- (FcChar8 **) &fullname) == FcResultMatch) {
- if (FcPatternGetBool(pat, FC_OUTLINE, 0, &ol) != FcResultMatch
- || ol != FcTrue)
- continue;
- if (FcPatternGetInteger(pat, FC_SLANT, 0, &at) != FcResultMatch
- || at < italic)
- continue;
- if (FcPatternGetInteger(pat, FC_WEIGHT, 0, &at) != FcResultMatch
- || at < bold)
- continue;
- if (strcasecmp(fullname, family) == 0) {
- FcFontSetAdd(result, FcPatternDuplicate(pat));
- break;
- }
- }
- }
- }
+ if (code == 0)
+ return 1;
- return result;
+ FcResult result = FcPatternGetCharSet(pat, FC_CHARSET, 0, &charset);
+ if (result != FcResultMatch)
+ return 0;
+ if (FcCharSetHasChar(charset, code) == FcTrue)
+ return 1;
+ return 0;
}
-/**
- * \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(ASS_Library *library, FCInstance *priv,
- const char *family, int treat_family_as_pattern,
- unsigned bold, unsigned italic, int *index,
- uint32_t code)
+static void destroy(void *priv)
{
- FcBool rc;
- FcResult result;
- FcPattern *pat = NULL, *rpat = NULL;
- int r_index, r_slant, r_weight;
- FcChar8 *r_family, *r_style, *r_file, *r_fullname;
- FcBool r_outline, r_embolden;
- FcCharSet *r_charset;
- FcFontSet *ffullname = NULL, *fsorted = NULL, *fset = NULL;
- int curf;
- char *retval = NULL;
- int family_cnt = 0;
-
- *index = 0;
-
- 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",
- // which is usually a concatenation of family name and font
- // style (ex. Ottawa Bold). Full name is available from
- // FontConfig pattern element FC_FULLNAME, but it is never
- // used for font matching.
- // Therefore, I'm removing words from the end of the name one
- // by one, and adding shortened names to the pattern. It seems
- // that the first value (full name in this case) has
- // precedence in matching.
- // An alternative approach could be to reimplement FcFontSort
- // using FC_FULLNAME instead of FC_FAMILY.
- family_cnt = 1;
- {
- char *s = strdup(family);
- if (!s)
- goto error;
- char *p = s + strlen(s);
- while (--p > s)
- if (*p == ' ' || *p == '-') {
- *p = '\0';
- FcPatternAddString(pat, FC_FAMILY, (const FcChar8 *) s);
- ++family_cnt;
- }
- free(s);
- }
- }
- FcPatternAddBool(pat, FC_OUTLINE, FcTrue);
- FcPatternAddInteger(pat, FC_SLANT, italic);
- FcPatternAddInteger(pat, FC_WEIGHT, bold);
-
- FcDefaultSubstitute(pat);
-
- rc = FcConfigSubstitute(priv->config, pat, FcMatchPattern);
- if (!rc)
- goto error;
- /* Fontconfig defaults include a language setting, which it sets based on
- * some environment variables or defaults to "en". Unset this as we don't
- * know the real language, and because some some attached fonts lack
- * non-ascii characters included in fontconfig's list of characters
- * required for English support and therefore don't match the lang=en
- * criterion.
- */
- FcPatternDel(pat, "lang");
-
- fsorted = FcFontSort(priv->config, pat, FcFalse, NULL, &result);
- ffullname = match_fullname(library, priv, family, bold, italic);
- if (!fsorted || !ffullname)
- goto error;
-
- fset = FcFontSetCreate();
- for (curf = 0; curf < ffullname->nfont; ++curf) {
- FcPattern *curp = ffullname->fonts[curf];
- FcPatternReference(curp);
- FcFontSetAdd(fset, curp);
- }
- for (curf = 0; curf < fsorted->nfont; ++curf) {
- FcPattern *curp = fsorted->fonts[curf];
- FcPatternReference(curp);
- FcFontSetAdd(fset, curp);
- }
+ ProviderPrivate *fc = (ProviderPrivate *)priv;
+
+ if (fc->fallback_chars)
+ FcCharSetDestroy(fc->fallback_chars);
+ if (fc->fallbacks)
+ FcFontSetDestroy(fc->fallbacks);
+ FcConfigDestroy(fc->config);
+ free(fc);
+}
- for (curf = 0; curf < fset->nfont; ++curf) {
- FcPattern *curp = fset->fonts[curf];
+static void scan_fonts(FcConfig *config, ASS_FontProvider *provider)
+{
+ int i;
+ FcFontSet *fonts;
+ ASS_FontProviderMetaData meta;
+
+ // get list of fonts
+ fonts = FcConfigGetFonts(config, FcSetSystem);
+
+ // fill font_info list
+ for (i = 0; i < fonts->nfont; i++) {
+ FcPattern *pat = fonts->fonts[i];
+ FcBool outline;
+ int index, weight;
+ char *path;
+ char *fullnames[MAX_NAME];
+ char *families[MAX_NAME];
+
+ // skip non-outline fonts
+ FcResult result = FcPatternGetBool(pat, FC_OUTLINE, 0, &outline);
+ if (result != FcResultMatch || outline != FcTrue)
+ continue;
- result = FcPatternGetBool(curp, FC_OUTLINE, 0, &r_outline);
+ // simple types
+ result = FcPatternGetInteger(pat, FC_SLANT, 0, &meta.slant);
+ result |= FcPatternGetInteger(pat, FC_WIDTH, 0, &meta.width);
+ result |= FcPatternGetInteger(pat, FC_WEIGHT, 0, &weight);
+ result |= FcPatternGetInteger(pat, FC_INDEX, 0, &index);
if (result != FcResultMatch)
continue;
- if (r_outline != FcTrue)
- continue;
- if (!code)
- break;
- result = FcPatternGetCharSet(curp, FC_CHARSET, 0, &r_charset);
+
+ // fontconfig uses its own weight scale, apparently derived
+ // from typographical weight. we're using truetype weights, so
+ // convert appropriately
+ if (weight <= FC_WEIGHT_LIGHT)
+ meta.weight = FONT_WEIGHT_LIGHT;
+ else if (weight <= FC_WEIGHT_MEDIUM)
+ meta.weight = FONT_WEIGHT_MEDIUM;
+ else
+ meta.weight = FONT_WEIGHT_BOLD;
+
+ // path
+ result = FcPatternGetString(pat, FC_FILE, 0, (FcChar8 **)&path);
if (result != FcResultMatch)
continue;
- if (FcCharSetHasChar(r_charset, code))
- break;
- }
- if (curf >= fset->nfont)
- goto error;
-
- if (!treat_family_as_pattern) {
- // Remove all extra family names from original pattern.
- // After this, FcFontRenderPrepare will select the most relevant family
- // name in case there are more than one of them.
- for (; family_cnt > 1; --family_cnt)
- FcPatternRemove(pat, FC_FAMILY, family_cnt - 1);
+ // read and strdup fullnames
+ meta.n_family = 0;
+ while (FcPatternGetString(pat, FC_FAMILY, meta.n_family,
+ (FcChar8 **)&families[meta.n_family]) == FcResultMatch
+ && meta.n_family < MAX_NAME)
+ meta.n_family++;
+ meta.families = families;
+
+ // read and strdup fullnames
+ meta.n_fullname = 0;
+ while (FcPatternGetString(pat, FC_FULLNAME, meta.n_fullname,
+ (FcChar8 **)&fullnames[meta.n_fullname]) == FcResultMatch
+ && meta.n_fullname < MAX_NAME)
+ meta.n_fullname++;
+ meta.fullnames = fullnames;
+
+ ass_font_provider_add_font(provider, &meta, path, index, NULL,
+ (void *)pat);
}
+}
- rpat = FcFontRenderPrepare(priv->config, pat, fset->fonts[curf]);
- if (!rpat)
- goto error;
-
- result = FcPatternGetInteger(rpat, FC_INDEX, 0, &r_index);
- if (result != FcResultMatch)
- goto error;
- *index = r_index;
-
- result = FcPatternGetString(rpat, FC_FILE, 0, &r_file);
- if (result != FcResultMatch)
- goto error;
- retval = strdup((const char *) r_file);
- if (!retval)
- goto error;
+static void cache_fallbacks(ProviderPrivate *fc)
+{
+ FcResult result;
- result = FcPatternGetString(rpat, FC_FAMILY, 0, &r_family);
- if (result != FcResultMatch)
- r_family = NULL;
+ if (fc->fallbacks)
+ return;
- result = FcPatternGetString(rpat, FC_FULLNAME, 0, &r_fullname);
- if (result != FcResultMatch)
- r_fullname = NULL;
-
- if (!treat_family_as_pattern &&
- !(r_family && strcasecmp((const char *) r_family, family) == 0) &&
- !(r_fullname && strcasecmp((const char *) r_fullname, family) == 0)) {
- char *fallback = (char *) (r_fullname ? r_fullname : r_family);
- if (code) {
- ass_msg(library, MSGL_WARN,
- "fontconfig: cannot find glyph U+%04X in font '%s', falling back to '%s'",
- (unsigned int)code, family, fallback);
- } else {
- ass_msg(library, MSGL_WARN,
- "fontconfig: cannot find font '%s', falling back to '%s'",
- family, fallback);
- }
- }
+ // Create a suitable pattern
+ FcPattern *pat = FcPatternCreate();
+ FcPatternAddString(pat, FC_FAMILY, (FcChar8 *)"sans-serif");
+ FcPatternAddBool(pat, FC_OUTLINE, FcTrue);
+ FcConfigSubstitute (fc->config, pat, FcMatchPattern);
+ FcDefaultSubstitute (pat);
- result = FcPatternGetString(rpat, FC_STYLE, 0, &r_style);
- if (result != FcResultMatch)
- r_style = NULL;
+ // FC_LANG is automatically set according to locale, but this results
+ // in strange sorting sometimes, so remove the attribute completely.
+ FcPatternDel(pat, FC_LANG);
- result = FcPatternGetInteger(rpat, FC_SLANT, 0, &r_slant);
- if (result != FcResultMatch)
- r_slant = 0;
+ // Sort installed fonts and eliminate duplicates; this can be very
+ // expensive.
+ fc->fallbacks = FcFontSort(fc->config, pat, FcTrue, &fc->fallback_chars,
+ &result);
- result = FcPatternGetInteger(rpat, FC_WEIGHT, 0, &r_weight);
+ // If this fails, just add an empty set
if (result != FcResultMatch)
- r_weight = 0;
+ fc->fallbacks = FcFontSetCreate();
- result = FcPatternGetBool(rpat, FC_EMBOLDEN, 0, &r_embolden);
- if (result != FcResultMatch)
- r_embolden = 0;
-
- ass_msg(library, MSGL_V,
- "Font info: family '%s', style '%s', fullname '%s',"
- " slant %d, weight %d%s", (const char *) r_family,
- (const char *) r_style, (const char *) r_fullname, r_slant,
- r_weight, r_embolden ? ", embolden" : "");
-
- error:
- if (pat)
- FcPatternDestroy(pat);
- if (rpat)
- FcPatternDestroy(rpat);
- if (fsorted)
- FcFontSetDestroy(fsorted);
- if (ffullname)
- FcFontSetDestroy(ffullname);
- if (fset)
- FcFontSetDestroy(fset);
- return retval;
+ FcPatternDestroy(pat);
}
-/**
- * \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(ASS_Library *library, FCInstance *priv,
- const char *family, int treat_family_as_pattern,
- unsigned bold, unsigned italic, int *index,
- uint32_t code)
+static char *get_fallback(void *priv, const char *family, uint32_t codepoint)
{
- char *res = 0;
- if (!priv->config) {
- *index = priv->index_default;
- res = priv->path_default ? strdup(priv->path_default) : 0;
- return res;
- }
- if (family && *family)
- res =
- select_font(library, priv, family, treat_family_as_pattern,
- bold, italic, index, code);
- if (!res && priv->family_default) {
- res =
- select_font(library, priv, priv->family_default, 0, bold,
- italic, index, code);
- if (res)
- ass_msg(library, MSGL_WARN, "fontconfig_select: Using default "
- "font family: (%s, %d, %d) -> %s, %d",
- family, bold, italic, res, *index);
- }
- if (!res && priv->path_default) {
- res = strdup(priv->path_default);
- *index = priv->index_default;
- if (res)
- ass_msg(library, MSGL_WARN, "fontconfig_select: Using default font: "
- "(%s, %d, %d) -> %s, %d", family, bold, italic,
- res, *index);
- }
- if (!res) {
- res = select_font(library, priv, "Arial", 0, bold, italic,
- index, code);
- if (res)
- ass_msg(library, MSGL_WARN, "fontconfig_select: Using 'Arial' "
- "font family: (%s, %d, %d) -> %s, %d", family, bold,
- italic, res, *index);
- }
- if (res)
- ass_msg(library, MSGL_V,
- "fontconfig_select: (%s, %d, %d) -> %s, %d", family, bold,
- italic, res, *index);
- return res;
-}
+ ProviderPrivate *fc = (ProviderPrivate *)priv;
+ FcResult result;
-/**
- * \brief Process memory font.
- * \param priv private data
- * \param library library object
- * \param ftlibrary freetype library object
- * \param idx index of the processed font in library->fontdata
- *
- * Builds a font pattern in memory via FT_New_Memory_Face/FcFreeTypeQueryFace.
-*/
-static void process_fontdata(FCInstance *priv, ASS_Library *library,
- FT_Library ftlibrary, int idx)
-{
- int rc;
- const char *name = library->fontdata[idx].name;
- const char *data = library->fontdata[idx].data;
- int data_size = library->fontdata[idx].size;
-
- FT_Face face;
- FcPattern *pattern;
- FcFontSet *fset;
- FcBool res;
- int face_index, num_faces = 1;
-
- for (face_index = 0; face_index < num_faces; ++face_index) {
- ass_msg(library, MSGL_V, "Adding memory font '%s'", name);
-
- rc = FT_New_Memory_Face(ftlibrary, (unsigned char *) data,
- data_size, face_index, &face);
- if (rc) {
- ass_msg(library, MSGL_WARN, "Error opening memory font: %s",
- name);
- return;
- }
- num_faces = face->num_faces;
-
- pattern =
- FcFreeTypeQueryFace(face, (unsigned char *) name, face_index,
- FcConfigGetBlanks(priv->config));
- if (!pattern) {
- ass_msg(library, MSGL_WARN, "%s failed", "FcFreeTypeQueryFace");
- FT_Done_Face(face);
- return;
- }
+ cache_fallbacks(fc);
- fset = FcConfigGetFonts(priv->config, FcSetSystem); // somehow it failes when asked for FcSetApplication
- if (!fset) {
- ass_msg(library, MSGL_WARN, "%s failed", "FcConfigGetFonts");
- FT_Done_Face(face);
- return;
- }
+ if (!fc->fallbacks || fc->fallbacks->nfont == 0)
+ return NULL;
- res = FcFontSetAdd(fset, pattern);
- if (!res) {
- ass_msg(library, MSGL_WARN, "%s failed", "FcFontSetAdd");
- FT_Done_Face(face);
- return;
+ if (codepoint == 0) {
+ char *family = NULL;
+ result = FcPatternGetString(fc->fallbacks->fonts[0], FC_FAMILY, 0,
+ (FcChar8 **)&family);
+ if (result == FcResultMatch) {
+ return strdup(family);
+ } else {
+ return NULL;
}
-
- FT_Done_Face(face);
}
-}
-/**
- * \brief Init fontconfig.
- * \param library libass library object
- * \param ftlibrary freetype library object
- * \param family default font family
- * \param path default font path
- * \param fc whether fontconfig should be used
- * \param config path to a fontconfig configuration file, or NULL
- * \param update whether the fontconfig cache should be built/updated
- * \return pointer to fontconfig private data
-*/
-FCInstance *fontconfig_init(ASS_Library *library,
- FT_Library ftlibrary, const char *family,
- const char *path, int fc, const char *config,
- int update)
-{
- int rc;
- FCInstance *priv = calloc(1, sizeof(FCInstance));
- const char *dir = library->fonts_dir;
- int i;
-
- if (!priv)
+ // fallback_chars is the union of all available charsets, so
+ // if we can't find the glyph in there, the system does not
+ // have any font to render this glyph.
+ if (FcCharSetHasChar(fc->fallback_chars, codepoint) == FcFalse)
return NULL;
- if (!fc) {
- ass_msg(library, MSGL_WARN,
- "Fontconfig disabled, only default font will be used.");
- goto exit;
- }
-
- priv->config = FcConfigCreate();
- rc = FcConfigParseAndLoad(priv->config, (unsigned char *) config, FcTrue);
- if (!rc) {
- ass_msg(library, MSGL_WARN, "No usable fontconfig configuration "
- "file found, using fallback.");
- FcConfigDestroy(priv->config);
- priv->config = FcInitLoadConfig();
- rc++;
- }
- if (rc && update) {
- FcConfigBuildFonts(priv->config);
+ for (int j = 0; j < fc->fallbacks->nfont; j++) {
+ FcPattern *pattern = fc->fallbacks->fonts[j];
+
+ FcCharSet *charset;
+ result = FcPatternGetCharSet(pattern, FC_CHARSET, 0, &charset);
+
+ if (result == FcResultMatch && FcCharSetHasChar(charset,
+ codepoint)) {
+ char *family = NULL;
+ result = FcPatternGetString(pattern, FC_FAMILY, 0,
+ (FcChar8 **)&family);
+ if (result == FcResultMatch) {
+ return strdup(family);
+ } else {
+ return NULL;
+ }
+ }
}
- if (!rc || !priv->config) {
- ass_msg(library, MSGL_FATAL,
- "No valid fontconfig configuration found!");
- FcConfigDestroy(priv->config);
- goto exit;
- }
+ // we shouldn't get here
+ return NULL;
+}
- for (i = 0; i < library->num_fontdata; ++i)
- process_fontdata(priv, library, ftlibrary, i);
+static void get_substitutions(void *priv, const char *name,
+ ASS_FontProviderMetaData *meta)
+{
+ ProviderPrivate *fc = (ProviderPrivate *)priv;
- if (dir) {
- ass_msg(library, MSGL_V, "Updating font cache");
+ FcPattern *pat = FcPatternCreate();
+ if (!pat)
+ return;
- rc = FcConfigAppFontAddDir(priv->config, (const FcChar8 *) dir);
- if (!rc) {
- ass_msg(library, MSGL_WARN, "%s failed", "FcConfigAppFontAddDir");
- }
+ FcPatternAddString(pat, FC_FAMILY, (FcChar8 *)name);
+ FcPatternAddString(pat, FC_FAMILY, (FcChar8 *)"__libass_delimiter");
+ FcPatternAddBool(pat, FC_OUTLINE, FcTrue);
+ if (!FcConfigSubstitute(fc->config, pat, FcMatchPattern))
+ goto cleanup;
+
+ // read and strdup fullnames
+ meta->n_fullname = 0;
+ meta->fullnames = calloc(MAX_NAME, sizeof(char *));
+ if (!meta->fullnames)
+ goto cleanup;
+
+ char *alias = NULL;
+ while (FcPatternGetString(pat, FC_FAMILY, meta->n_fullname,
+ (FcChar8 **)&alias) == FcResultMatch
+ && meta->n_fullname < MAX_NAME
+ && strcmp(alias, "__libass_delimiter") != 0) {
+ alias = strdup(alias);
+ if (!alias)
+ goto cleanup;
+ meta->fullnames[meta->n_fullname] = alias;
+ meta->n_fullname++;
}
- priv->family_default = family ? strdup(family) : NULL;
-exit:
- priv->path_default = path ? strdup(path) : NULL;
- priv->index_default = 0;
-
- return priv;
+cleanup:
+ FcPatternDestroy(pat);
}
-int fontconfig_update(FCInstance *priv)
-{
- return FcConfigBuildFonts(priv->config);
-}
-
-#else /* CONFIG_FONTCONFIG */
+static ASS_FontProviderFuncs fontconfig_callbacks = {
+ .check_glyph = check_glyph,
+ .destroy_provider = destroy,
+ .get_substitutions = get_substitutions,
+ .get_fallback = get_fallback,
+};
-char *fontconfig_select(ASS_Library *library, FCInstance *priv,
- const char *family, int treat_family_as_pattern,
- unsigned bold, unsigned italic, int *index,
- uint32_t code)
+ASS_FontProvider *
+ass_fontconfig_add_provider(ASS_Library *lib, ASS_FontSelector *selector,
+ const char *config)
{
- *index = priv->index_default;
- char* res = priv->path_default ? strdup(priv->path_default) : 0;
- return res;
-}
+ int rc;
+ ProviderPrivate *fc = NULL;
+ ASS_FontProvider *provider = NULL;
-FCInstance *fontconfig_init(ASS_Library *library,
- FT_Library ftlibrary, const char *family,
- const char *path, int fc, const char *config,
- int update)
-{
- FCInstance *priv;
+ fc = calloc(1, sizeof(ProviderPrivate));
+ if (fc == NULL)
+ return NULL;
- ass_msg(library, MSGL_WARN,
- "Fontconfig disabled, only default font will be used.");
+ // build and load fontconfig configuration
+ fc->config = FcConfigCreate();
+ rc = FcConfigParseAndLoad(fc->config, (unsigned char *) config, FcTrue);
+ if (!rc) {
+ ass_msg(lib, MSGL_WARN, "No usable fontconfig configuration "
+ "file found, using fallback.");
+ FcConfigDestroy(fc->config);
+ fc->config = FcInitLoadConfig();
+ }
+ if (fc->config)
+ rc = FcConfigBuildFonts(fc->config);
- priv = calloc(1, sizeof(FCInstance));
- if (!priv)
+ if (!rc || !fc->config) {
+ ass_msg(lib, MSGL_FATAL,
+ "No valid fontconfig configuration found!");
+ FcConfigDestroy(fc->config);
+ free(fc);
return NULL;
+ }
- priv->path_default = path ? strdup(path) : 0;
- priv->index_default = 0;
- return priv;
-}
-
-int fontconfig_update(FCInstance *priv)
-{
- // Do nothing
- return 1;
-}
+ // create font provider
+ provider = ass_font_provider_new(selector, &fontconfig_callbacks, fc);
-#endif
+ // build database from system fonts
+ scan_fonts(fc->config, provider);
-void fontconfig_done(FCInstance *priv)
-{
+ return provider;
+}
- if (priv) {
-#ifdef CONFIG_FONTCONFIG
- if (priv->config)
- FcConfigDestroy(priv->config);
#endif
- free(priv->path_default);
- free(priv->family_default);
- }
- free(priv);
-}