2007-04-09 04:02:40 +02:00
|
|
|
/*
|
2008-12-28 09:09:59 +01:00
|
|
|
* Copyright (C) 2006 Evgeniy Stepanov <eugeni.stepanov@gmail.com>
|
|
|
|
*
|
|
|
|
* This file is part of libass.
|
|
|
|
*
|
2010-11-11 05:48:08 +01:00
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
2008-12-28 09:09:59 +01:00
|
|
|
*
|
2010-11-11 05:48:08 +01:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
2008-12-28 09:09:59 +01:00
|
|
|
*/
|
2007-04-09 04:02:40 +02:00
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <string.h>
|
2010-11-11 05:48:08 +01:00
|
|
|
#include <strings.h>
|
2007-04-09 04:02:40 +02:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include <ft2build.h>
|
|
|
|
#include FT_FREETYPE_H
|
|
|
|
|
2009-07-11 17:48:50 +02:00
|
|
|
#include "ass_utils.h"
|
2007-04-09 04:02:40 +02:00
|
|
|
#include "ass.h"
|
|
|
|
#include "ass_library.h"
|
|
|
|
#include "ass_fontconfig.h"
|
|
|
|
|
2008-12-28 09:09:59 +01:00
|
|
|
#ifdef CONFIG_FONTCONFIG
|
2007-04-09 04:02:40 +02:00
|
|
|
#include <fontconfig/fontconfig.h>
|
|
|
|
#include <fontconfig/fcfreetype.h>
|
|
|
|
#endif
|
|
|
|
|
2009-09-02 03:23:51 +02:00
|
|
|
struct fc_instance {
|
2008-12-28 09:09:59 +01:00
|
|
|
#ifdef CONFIG_FONTCONFIG
|
2009-07-11 17:48:50 +02:00
|
|
|
FcConfig *config;
|
2007-04-09 04:02:40 +02:00
|
|
|
#endif
|
2009-07-11 17:48:50 +02:00
|
|
|
char *family_default;
|
|
|
|
char *path_default;
|
|
|
|
int index_default;
|
2007-04-09 04:02:40 +02:00
|
|
|
};
|
|
|
|
|
2008-12-28 09:09:59 +01:00
|
|
|
#ifdef CONFIG_FONTCONFIG
|
|
|
|
|
2010-11-11 05:48:08 +01:00
|
|
|
/**
|
|
|
|
* \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)
|
|
|
|
{
|
|
|
|
FcFontSet *sets[2];
|
|
|
|
FcFontSet *result = FcFontSetCreate();
|
|
|
|
int nsets = 0;
|
|
|
|
int i, fi;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2007-04-09 04:02:40 +02:00
|
|
|
/**
|
|
|
|
* \brief Low-level font selection.
|
|
|
|
* \param priv private data
|
|
|
|
* \param family font family
|
2009-03-12 04:46:44 +01:00
|
|
|
* \param treat_family_as_pattern treat family as fontconfig pattern
|
2007-04-09 04:02:40 +02:00
|
|
|
* \param bold font weight value
|
|
|
|
* \param italic font slant value
|
|
|
|
* \param index out: font index inside a file
|
2008-03-21 06:32:23 +01:00
|
|
|
* \param code: the character that should be present in the font, can be 0
|
2007-04-09 04:02:40 +02:00
|
|
|
* \return font file path
|
2009-07-11 17:48:50 +02:00
|
|
|
*/
|
2010-11-11 05:48:08 +01:00
|
|
|
static char *select_font(ASS_Library *library, FCInstance *priv,
|
2009-07-14 00:43:25 +02:00
|
|
|
const char *family, int treat_family_as_pattern,
|
|
|
|
unsigned bold, unsigned italic, int *index,
|
|
|
|
uint32_t code)
|
2007-04-09 04:02:40 +02:00
|
|
|
{
|
2009-07-11 17:48:50 +02:00
|
|
|
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;
|
2010-11-11 05:48:08 +01:00
|
|
|
FcFontSet *ffullname = NULL, *fsorted = NULL, *fset = NULL;
|
2009-07-11 17:48:50 +02:00
|
|
|
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);
|
|
|
|
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;
|
|
|
|
|
2010-11-11 05:48:08 +01:00
|
|
|
fsorted = FcFontSort(priv->config, pat, FcTrue, NULL, &result);
|
|
|
|
ffullname = match_fullname(library, priv, family, bold, italic);
|
|
|
|
if (!fsorted || !ffullname)
|
2009-07-11 17:48:50 +02:00
|
|
|
goto error;
|
|
|
|
|
2010-11-11 05:48:08 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2009-07-11 17:48:50 +02:00
|
|
|
for (curf = 0; curf < fset->nfont; ++curf) {
|
|
|
|
FcPattern *curp = fset->fonts[curf];
|
|
|
|
|
|
|
|
result = FcPatternGetBool(curp, FC_OUTLINE, 0, &r_outline);
|
|
|
|
if (result != FcResultMatch)
|
|
|
|
continue;
|
|
|
|
if (r_outline != FcTrue)
|
|
|
|
continue;
|
|
|
|
if (!code)
|
|
|
|
break;
|
|
|
|
result = FcPatternGetCharSet(curp, FC_CHARSET, 0, &r_charset);
|
|
|
|
if (result != FcResultMatch)
|
|
|
|
continue;
|
|
|
|
if (FcCharSetHasChar(r_charset, code))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (curf >= fset->nfont)
|
|
|
|
goto error;
|
2007-04-09 04:02:40 +02:00
|
|
|
|
2009-07-11 17:48:50 +02:00
|
|
|
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);
|
|
|
|
}
|
2008-12-28 09:09:59 +01:00
|
|
|
|
2009-07-11 17:48:50 +02:00
|
|
|
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);
|
|
|
|
|
|
|
|
result = FcPatternGetString(rpat, FC_FAMILY, 0, &r_family);
|
|
|
|
if (result != FcResultMatch)
|
|
|
|
r_family = NULL;
|
|
|
|
|
|
|
|
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))
|
2009-07-14 00:43:25 +02:00
|
|
|
ass_msg(library, MSGL_WARN,
|
|
|
|
"fontconfig: Selected font is not the requested one: "
|
|
|
|
"'%s' != '%s'",
|
2009-07-11 17:48:50 +02:00
|
|
|
(const char *) (r_fullname ? r_fullname : r_family), family);
|
|
|
|
|
|
|
|
result = FcPatternGetString(rpat, FC_STYLE, 0, &r_style);
|
|
|
|
if (result != FcResultMatch)
|
|
|
|
r_style = NULL;
|
|
|
|
|
|
|
|
result = FcPatternGetInteger(rpat, FC_SLANT, 0, &r_slant);
|
|
|
|
if (result != FcResultMatch)
|
|
|
|
r_slant = 0;
|
|
|
|
|
|
|
|
result = FcPatternGetInteger(rpat, FC_WEIGHT, 0, &r_weight);
|
|
|
|
if (result != FcResultMatch)
|
|
|
|
r_weight = 0;
|
|
|
|
|
|
|
|
result = FcPatternGetBool(rpat, FC_EMBOLDEN, 0, &r_embolden);
|
|
|
|
if (result != FcResultMatch)
|
|
|
|
r_embolden = 0;
|
|
|
|
|
2009-07-14 00:43:25 +02:00
|
|
|
ass_msg(library, MSGL_V,
|
|
|
|
"Font info: family '%s', style '%s', fullname '%s',"
|
|
|
|
" slant %d, weight %d%s", (const char *) r_family,
|
2009-07-11 17:48:50 +02:00
|
|
|
(const char *) r_style, (const char *) r_fullname, r_slant,
|
|
|
|
r_weight, r_embolden ? ", embolden" : "");
|
|
|
|
|
|
|
|
error:
|
|
|
|
if (pat)
|
|
|
|
FcPatternDestroy(pat);
|
|
|
|
if (rpat)
|
|
|
|
FcPatternDestroy(rpat);
|
2010-11-11 05:48:08 +01:00
|
|
|
if (fsorted)
|
|
|
|
FcFontSetDestroy(fsorted);
|
|
|
|
if (ffullname)
|
|
|
|
FcFontSetDestroy(ffullname);
|
2009-07-11 17:48:50 +02:00
|
|
|
if (fset)
|
|
|
|
FcFontSetDestroy(fset);
|
|
|
|
return retval;
|
2007-04-09 04:02:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Find a font. Use default family or path if necessary.
|
|
|
|
* \param priv_ private data
|
|
|
|
* \param family font family
|
2009-03-12 04:46:44 +01:00
|
|
|
* \param treat_family_as_pattern treat family as fontconfig pattern
|
2007-04-09 04:02:40 +02:00
|
|
|
* \param bold font weight value
|
|
|
|
* \param italic font slant value
|
|
|
|
* \param index out: font index inside a file
|
2008-03-21 06:32:23 +01:00
|
|
|
* \param code: the character that should be present in the font, can be 0
|
2007-04-09 04:02:40 +02:00
|
|
|
* \return font file path
|
2009-07-11 17:48:50 +02:00
|
|
|
*/
|
2009-09-02 03:23:51 +02:00
|
|
|
char *fontconfig_select(ASS_Library *library, FCInstance *priv,
|
2009-07-14 00:43:25 +02:00
|
|
|
const char *family, int treat_family_as_pattern,
|
|
|
|
unsigned bold, unsigned italic, int *index,
|
|
|
|
uint32_t code)
|
2007-04-09 04:02:40 +02:00
|
|
|
{
|
2009-07-11 17:48:50 +02:00
|
|
|
char *res = 0;
|
|
|
|
if (!priv->config) {
|
|
|
|
*index = priv->index_default;
|
2010-01-10 22:09:48 +01:00
|
|
|
res = priv->path_default ? strdup(priv->path_default) : 0;
|
|
|
|
return res;
|
2009-07-11 17:48:50 +02:00
|
|
|
}
|
|
|
|
if (family && *family)
|
|
|
|
res =
|
2010-11-11 05:48:08 +01:00
|
|
|
select_font(library, priv, family, treat_family_as_pattern,
|
2009-07-14 00:43:25 +02:00
|
|
|
bold, italic, index, code);
|
2009-07-11 17:48:50 +02:00
|
|
|
if (!res && priv->family_default) {
|
|
|
|
res =
|
2010-11-11 05:48:08 +01:00
|
|
|
select_font(library, priv, priv->family_default, 0, bold,
|
2009-07-14 00:43:25 +02:00
|
|
|
italic, index, code);
|
2009-07-11 17:48:50 +02:00
|
|
|
if (res)
|
2009-07-14 00:43:25 +02:00
|
|
|
ass_msg(library, MSGL_WARN, "fontconfig_select: Using default "
|
|
|
|
"font family: (%s, %d, %d) -> %s, %d",
|
|
|
|
family, bold, italic, res, *index);
|
2009-07-11 17:48:50 +02:00
|
|
|
}
|
|
|
|
if (!res && priv->path_default) {
|
2010-01-10 22:09:48 +01:00
|
|
|
res = strdup(priv->path_default);
|
2009-07-11 17:48:50 +02:00
|
|
|
*index = priv->index_default;
|
2009-07-14 00:43:25 +02:00
|
|
|
ass_msg(library, MSGL_WARN, "fontconfig_select: Using default font: "
|
|
|
|
"(%s, %d, %d) -> %s, %d", family, bold, italic,
|
|
|
|
res, *index);
|
2009-07-11 17:48:50 +02:00
|
|
|
}
|
|
|
|
if (!res) {
|
2010-11-11 05:48:08 +01:00
|
|
|
res = select_font(library, priv, "Arial", 0, bold, italic,
|
2009-07-14 00:43:25 +02:00
|
|
|
index, code);
|
2009-07-11 17:48:50 +02:00
|
|
|
if (res)
|
2009-07-14 00:43:25 +02:00
|
|
|
ass_msg(library, MSGL_WARN, "fontconfig_select: Using 'Arial' "
|
|
|
|
"font family: (%s, %d, %d) -> %s, %d", family, bold,
|
|
|
|
italic, res, *index);
|
2009-07-11 17:48:50 +02:00
|
|
|
}
|
|
|
|
if (res)
|
2009-07-14 00:43:25 +02:00
|
|
|
ass_msg(library, MSGL_V,
|
|
|
|
"fontconfig_select: (%s, %d, %d) -> %s, %d", family, bold,
|
|
|
|
italic, res, *index);
|
2009-07-11 17:48:50 +02:00
|
|
|
return res;
|
2007-04-09 04:02:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \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
|
2010-11-11 05:48:08 +01:00
|
|
|
*
|
|
|
|
* Builds a font pattern in memory via FT_New_Memory_Face/FcFreeTypeQueryFace.
|
2009-07-11 17:48:50 +02:00
|
|
|
*/
|
2009-09-02 03:23:51 +02:00
|
|
|
static void process_fontdata(FCInstance *priv, ASS_Library *library,
|
2009-07-11 17:48:50 +02:00
|
|
|
FT_Library ftlibrary, int idx)
|
2007-04-09 04:02:40 +02:00
|
|
|
{
|
2009-07-11 17:48:50 +02:00
|
|
|
int rc;
|
|
|
|
const char *name = library->fontdata[idx].name;
|
|
|
|
const char *data = library->fontdata[idx].data;
|
|
|
|
int data_size = library->fontdata[idx].size;
|
2007-04-09 04:02:40 +02:00
|
|
|
|
2009-07-11 17:48:50 +02:00
|
|
|
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) {
|
|
|
|
rc = FT_New_Memory_Face(ftlibrary, (unsigned char *) data,
|
|
|
|
data_size, face_index, &face);
|
|
|
|
if (rc) {
|
2009-07-14 00:43:25 +02:00
|
|
|
ass_msg(library, MSGL_WARN, "Error opening memory font: %s",
|
2009-07-11 17:48:50 +02:00
|
|
|
name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
num_faces = face->num_faces;
|
|
|
|
|
|
|
|
pattern =
|
2010-11-11 05:48:08 +01:00
|
|
|
FcFreeTypeQueryFace(face, (unsigned char *) name, face_index,
|
2009-07-11 17:48:50 +02:00
|
|
|
FcConfigGetBlanks(priv->config));
|
|
|
|
if (!pattern) {
|
2009-07-14 00:43:25 +02:00
|
|
|
ass_msg(library, MSGL_WARN, "%s failed", "FcFreeTypeQueryFace");
|
2009-07-11 17:48:50 +02:00
|
|
|
FT_Done_Face(face);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fset = FcConfigGetFonts(priv->config, FcSetSystem); // somehow it failes when asked for FcSetApplication
|
|
|
|
if (!fset) {
|
2009-07-14 00:43:25 +02:00
|
|
|
ass_msg(library, MSGL_WARN, "%s failed", "FcConfigGetFonts");
|
2009-07-11 17:48:50 +02:00
|
|
|
FT_Done_Face(face);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
res = FcFontSetAdd(fset, pattern);
|
|
|
|
if (!res) {
|
2009-07-14 00:43:25 +02:00
|
|
|
ass_msg(library, MSGL_WARN, "%s failed", "FcFontSetAdd");
|
2009-07-11 17:48:50 +02:00
|
|
|
FT_Done_Face(face);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
FT_Done_Face(face);
|
|
|
|
}
|
2007-04-09 04:02:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Init fontconfig.
|
|
|
|
* \param library libass library object
|
|
|
|
* \param ftlibrary freetype library object
|
|
|
|
* \param family default font family
|
|
|
|
* \param path default font path
|
2009-07-19 21:33:29 +02:00
|
|
|
* \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
|
2007-04-09 04:02:40 +02:00
|
|
|
* \return pointer to fontconfig private data
|
2009-07-11 17:48:50 +02:00
|
|
|
*/
|
2009-09-02 03:23:51 +02:00
|
|
|
FCInstance *fontconfig_init(ASS_Library *library,
|
|
|
|
FT_Library ftlibrary, const char *family,
|
|
|
|
const char *path, int fc, const char *config,
|
|
|
|
int update)
|
2007-04-09 04:02:40 +02:00
|
|
|
{
|
2009-07-11 17:48:50 +02:00
|
|
|
int rc;
|
2009-09-02 03:23:51 +02:00
|
|
|
FCInstance *priv = calloc(1, sizeof(FCInstance));
|
2009-07-11 17:48:50 +02:00
|
|
|
const char *dir = library->fonts_dir;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (!fc) {
|
2009-07-14 00:43:25 +02:00
|
|
|
ass_msg(library, MSGL_WARN,
|
|
|
|
"Fontconfig disabled, only default font will be used.");
|
2009-07-11 17:48:50 +02:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2009-07-19 21:33:29 +02:00
|
|
|
priv->config = FcConfigCreate();
|
|
|
|
rc = FcConfigParseAndLoad(priv->config, (unsigned char *) config, FcTrue);
|
2009-09-02 03:23:51 +02:00
|
|
|
if (!rc) {
|
|
|
|
ass_msg(library, MSGL_WARN, "No usable fontconfig configuration "
|
|
|
|
"file found, using fallback.");
|
|
|
|
FcConfigDestroy(priv->config);
|
|
|
|
priv->config = FcInitLoadConfig();
|
|
|
|
rc++;
|
|
|
|
}
|
2009-07-19 21:33:29 +02:00
|
|
|
if (rc && update) {
|
2009-07-11 17:48:50 +02:00
|
|
|
FcConfigBuildFonts(priv->config);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!rc || !priv->config) {
|
2009-09-02 03:23:51 +02:00
|
|
|
ass_msg(library, MSGL_FATAL,
|
|
|
|
"No valid fontconfig configuration found!");
|
2009-07-19 21:33:29 +02:00
|
|
|
FcConfigDestroy(priv->config);
|
2009-07-11 17:48:50 +02:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < library->num_fontdata; ++i)
|
|
|
|
process_fontdata(priv, library, ftlibrary, i);
|
|
|
|
|
|
|
|
if (dir) {
|
2010-11-11 05:48:08 +01:00
|
|
|
ass_msg(library, MSGL_V, "Updating font cache");
|
2009-07-11 17:48:50 +02:00
|
|
|
|
|
|
|
rc = FcConfigAppFontAddDir(priv->config, (const FcChar8 *) dir);
|
|
|
|
if (!rc) {
|
2009-07-14 00:43:25 +02:00
|
|
|
ass_msg(library, MSGL_WARN, "%s failed", "FcConfigAppFontAddDir");
|
2009-07-11 17:48:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
priv->family_default = family ? strdup(family) : NULL;
|
2009-07-19 21:33:29 +02:00
|
|
|
exit:
|
2009-07-11 17:48:50 +02:00
|
|
|
priv->path_default = path ? strdup(path) : NULL;
|
|
|
|
priv->index_default = 0;
|
|
|
|
|
|
|
|
return priv;
|
2007-04-09 04:02:40 +02:00
|
|
|
}
|
|
|
|
|
2009-09-02 03:23:51 +02:00
|
|
|
int fontconfig_update(FCInstance *priv)
|
2009-07-19 21:33:29 +02:00
|
|
|
{
|
|
|
|
return FcConfigBuildFonts(priv->config);
|
|
|
|
}
|
|
|
|
|
2009-07-11 17:48:50 +02:00
|
|
|
#else /* CONFIG_FONTCONFIG */
|
2007-04-09 04:02:40 +02:00
|
|
|
|
2009-09-02 03:23:51 +02:00
|
|
|
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)
|
2007-04-09 04:02:40 +02:00
|
|
|
{
|
2009-07-11 17:48:50 +02:00
|
|
|
*index = priv->index_default;
|
2010-01-10 22:09:48 +01:00
|
|
|
char* res = priv->path_default ? strdup(priv->path_default) : 0;
|
|
|
|
return res;
|
2007-04-09 04:02:40 +02:00
|
|
|
}
|
|
|
|
|
2009-09-02 03:23:51 +02:00
|
|
|
FCInstance *fontconfig_init(ASS_Library *library,
|
|
|
|
FT_Library ftlibrary, const char *family,
|
|
|
|
const char *path, int fc, const char *config,
|
|
|
|
int update)
|
2007-04-09 04:02:40 +02:00
|
|
|
{
|
2009-09-02 03:23:51 +02:00
|
|
|
FCInstance *priv;
|
2009-07-11 17:48:50 +02:00
|
|
|
|
2009-07-14 00:43:25 +02:00
|
|
|
ass_msg(library, MSGL_WARN,
|
|
|
|
"Fontconfig disabled, only default font will be used.");
|
2009-07-11 17:48:50 +02:00
|
|
|
|
2009-09-02 03:23:51 +02:00
|
|
|
priv = calloc(1, sizeof(FCInstance));
|
2009-07-11 17:48:50 +02:00
|
|
|
|
2010-01-10 22:09:48 +01:00
|
|
|
priv->path_default = path ? strdup(path) : 0;
|
2009-07-11 17:48:50 +02:00
|
|
|
priv->index_default = 0;
|
|
|
|
return priv;
|
2007-04-09 04:02:40 +02:00
|
|
|
}
|
|
|
|
|
2009-09-02 03:23:51 +02:00
|
|
|
int fontconfig_update(FCInstance *priv)
|
2009-07-19 21:33:29 +02:00
|
|
|
{
|
|
|
|
// Do nothing
|
2009-09-02 03:23:51 +02:00
|
|
|
return 1;
|
2009-07-19 21:33:29 +02:00
|
|
|
}
|
|
|
|
|
2007-04-09 04:02:40 +02:00
|
|
|
#endif
|
|
|
|
|
2009-09-02 03:23:51 +02:00
|
|
|
void fontconfig_done(FCInstance *priv)
|
2007-04-09 04:02:40 +02:00
|
|
|
{
|
2010-11-11 05:48:08 +01:00
|
|
|
|
|
|
|
if (priv) {
|
2009-09-02 03:23:51 +02:00
|
|
|
#ifdef CONFIG_FONTCONFIG
|
|
|
|
FcConfigDestroy(priv->config);
|
|
|
|
#endif
|
2009-07-11 17:48:50 +02:00
|
|
|
free(priv->path_default);
|
|
|
|
free(priv->family_default);
|
2010-11-11 05:48:08 +01:00
|
|
|
}
|
|
|
|
free(priv);
|
2007-04-09 04:02:40 +02:00
|
|
|
}
|