2006-10-01 17:09:04 +00:00
|
|
|
// -*- c-basic-offset: 8; indent-tabs-mode: t -*-
|
|
|
|
// vim:ts=8:sw=8:noet:ai:
|
|
|
|
/*
|
2008-05-13 08:02:23 +00:00
|
|
|
* Copyright (C) 2006 Evgeniy Stepanov <eugeni.stepanov@gmail.com>
|
|
|
|
*
|
2008-05-14 17:21:58 +00:00
|
|
|
* This file is part of libass.
|
2008-05-13 08:02:23 +00:00
|
|
|
*
|
2008-05-14 17:21:58 +00:00
|
|
|
* libass is free software; you can redistribute it and/or modify
|
2008-05-13 08:02:23 +00:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
2008-05-14 17:21:58 +00:00
|
|
|
* libass is distributed in the hope that it will be useful,
|
2008-05-13 08:02:23 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
2008-05-14 17:21:58 +00:00
|
|
|
* with libass; if not, write to the Free Software Foundation, Inc.,
|
2008-05-13 08:02:23 +00:00
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
2006-10-01 17:09:04 +00:00
|
|
|
|
2006-07-07 18:26:51 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2007-02-22 15:48:37 +00:00
|
|
|
#include <inttypes.h>
|
|
|
|
#include <ft2build.h>
|
|
|
|
#include FT_FREETYPE_H
|
2006-07-07 18:26:51 +00:00
|
|
|
|
2006-11-19 00:41:00 +00:00
|
|
|
#include "mputils.h"
|
2006-12-03 17:59:13 +00:00
|
|
|
#include "ass.h"
|
|
|
|
#include "ass_library.h"
|
2006-07-07 18:26:51 +00:00
|
|
|
#include "ass_fontconfig.h"
|
|
|
|
|
2008-08-07 10:36:07 +00:00
|
|
|
#ifdef CONFIG_FONTCONFIG
|
2006-07-07 18:26:51 +00:00
|
|
|
#include <fontconfig/fontconfig.h>
|
2006-12-03 18:24:11 +00:00
|
|
|
#include <fontconfig/fcfreetype.h>
|
2006-07-07 18:26:51 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
struct fc_instance_s {
|
2008-08-07 10:36:07 +00:00
|
|
|
#ifdef CONFIG_FONTCONFIG
|
2006-07-07 18:26:51 +00:00
|
|
|
FcConfig* config;
|
|
|
|
#endif
|
|
|
|
char* family_default;
|
|
|
|
char* path_default;
|
|
|
|
int index_default;
|
|
|
|
};
|
|
|
|
|
2008-08-07 10:36:07 +00:00
|
|
|
#ifdef CONFIG_FONTCONFIG
|
2008-05-08 13:54:00 +00:00
|
|
|
|
|
|
|
// 4yo fontconfig does not have these.
|
|
|
|
// They are only needed for debug output, anyway.
|
|
|
|
#ifndef FC_FULLNAME
|
|
|
|
#define FC_FULLNAME "fullname"
|
|
|
|
#endif
|
|
|
|
#ifndef FC_EMBOLDEN
|
|
|
|
#define FC_EMBOLDEN "embolden"
|
|
|
|
#endif
|
|
|
|
|
2006-07-07 18:26:51 +00:00
|
|
|
/**
|
|
|
|
* \brief Low-level font selection.
|
|
|
|
* \param priv private data
|
|
|
|
* \param family font family
|
|
|
|
* \param bold font weight value
|
|
|
|
* \param italic font slant value
|
|
|
|
* \param index out: font index inside a file
|
2007-08-03 13:43:11 +00:00
|
|
|
* \param code: the character that should be present in the font, can be 0
|
2006-07-07 18:26:51 +00:00
|
|
|
* \return font file path
|
|
|
|
*/
|
2006-11-28 22:50:02 +00:00
|
|
|
static char* _select_font(fc_instance_t* priv, const char* family, unsigned bold, unsigned italic, int* index,
|
2007-08-03 13:43:11 +00:00
|
|
|
uint32_t code)
|
2006-07-07 18:26:51 +00:00
|
|
|
{
|
|
|
|
FcBool rc;
|
|
|
|
FcResult result;
|
2008-11-02 11:48:20 +00:00
|
|
|
FcPattern *pat = NULL, *rpat = NULL;
|
2008-05-02 23:32:29 +00:00
|
|
|
int r_index, r_slant, r_weight;
|
2008-05-02 23:32:28 +00:00
|
|
|
FcChar8 *r_family, *r_style, *r_file, *r_fullname;
|
2008-05-02 23:32:29 +00:00
|
|
|
FcBool r_outline, r_embolden;
|
2008-05-02 23:32:25 +00:00
|
|
|
FcCharSet* r_charset;
|
2008-11-02 11:48:20 +00:00
|
|
|
FcFontSet* fset = NULL;
|
2007-08-03 13:43:11 +00:00
|
|
|
int curf;
|
2008-11-02 11:48:20 +00:00
|
|
|
char* retval = NULL;
|
2008-05-08 13:53:56 +00:00
|
|
|
int family_cnt;
|
2006-07-07 18:26:51 +00:00
|
|
|
|
|
|
|
*index = 0;
|
|
|
|
|
2006-07-13 18:49:06 +00:00
|
|
|
pat = FcPatternCreate();
|
2006-07-07 18:26:51 +00:00
|
|
|
if (!pat)
|
2007-05-03 19:52:46 +00:00
|
|
|
goto error;
|
2006-07-07 18:26:51 +00:00
|
|
|
|
2006-07-13 18:49:06 +00:00
|
|
|
FcPatternAddString(pat, FC_FAMILY, (const FcChar8*)family);
|
2008-05-01 00:34:26 +00:00
|
|
|
|
|
|
|
// 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.
|
2008-05-08 13:53:56 +00:00
|
|
|
family_cnt = 1;
|
2008-05-11 20:17:21 +00:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
2008-05-01 00:34:26 +00:00
|
|
|
free(s);
|
|
|
|
}
|
2006-07-13 18:49:06 +00:00
|
|
|
FcPatternAddBool(pat, FC_OUTLINE, FcTrue);
|
|
|
|
FcPatternAddInteger(pat, FC_SLANT, italic);
|
|
|
|
FcPatternAddInteger(pat, FC_WEIGHT, bold);
|
|
|
|
|
2006-07-07 18:26:51 +00:00
|
|
|
FcDefaultSubstitute(pat);
|
|
|
|
|
|
|
|
rc = FcConfigSubstitute(priv->config, pat, FcMatchPattern);
|
|
|
|
if (!rc)
|
2007-05-03 19:52:46 +00:00
|
|
|
goto error;
|
2006-11-28 22:50:02 +00:00
|
|
|
|
|
|
|
fset = FcFontSort(priv->config, pat, FcTrue, NULL, &result);
|
2008-06-23 14:46:55 +00:00
|
|
|
if (!fset)
|
|
|
|
goto error;
|
2006-11-28 22:50:02 +00:00
|
|
|
|
|
|
|
for (curf = 0; curf < fset->nfont; ++curf) {
|
2008-05-08 13:53:56 +00:00
|
|
|
FcPattern* curp = fset->fonts[curf];
|
|
|
|
|
|
|
|
result = FcPatternGetBool(curp, FC_OUTLINE, 0, &r_outline);
|
2006-11-28 22:50:02 +00:00
|
|
|
if (result != FcResultMatch)
|
|
|
|
continue;
|
2008-05-02 23:32:25 +00:00
|
|
|
if (r_outline != FcTrue)
|
2006-11-28 22:50:02 +00:00
|
|
|
continue;
|
2007-08-03 13:43:11 +00:00
|
|
|
if (!code)
|
|
|
|
break;
|
2008-05-08 13:53:56 +00:00
|
|
|
result = FcPatternGetCharSet(curp, FC_CHARSET, 0, &r_charset);
|
2007-08-03 13:43:11 +00:00
|
|
|
if (result != FcResultMatch)
|
|
|
|
continue;
|
2008-05-02 23:32:25 +00:00
|
|
|
if (FcCharSetHasChar(r_charset, code))
|
2006-11-28 22:50:02 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2007-08-03 13:43:11 +00:00
|
|
|
if (curf >= fset->nfont)
|
2007-05-03 19:52:46 +00:00
|
|
|
goto error;
|
2006-11-28 22:50:02 +00:00
|
|
|
|
2008-05-22 08:58:06 +00:00
|
|
|
#if (FC_VERSION >= 20297)
|
2008-05-08 13:53:56 +00:00
|
|
|
// 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-05-22 08:58:06 +00:00
|
|
|
#endif
|
2008-05-08 13:53:56 +00:00
|
|
|
|
|
|
|
rpat = FcFontRenderPrepare(priv->config, pat, fset->fonts[curf]);
|
|
|
|
if (!rpat)
|
|
|
|
goto error;
|
|
|
|
|
2008-05-02 23:32:25 +00:00
|
|
|
result = FcPatternGetInteger(rpat, FC_INDEX, 0, &r_index);
|
2006-07-07 18:26:51 +00:00
|
|
|
if (result != FcResultMatch)
|
2007-05-03 19:52:46 +00:00
|
|
|
goto error;
|
2008-05-02 23:32:25 +00:00
|
|
|
*index = r_index;
|
2006-07-07 18:26:51 +00:00
|
|
|
|
2008-05-02 23:32:26 +00:00
|
|
|
result = FcPatternGetString(rpat, FC_FILE, 0, &r_file);
|
|
|
|
if (result != FcResultMatch)
|
|
|
|
goto error;
|
|
|
|
retval = strdup((const char*)r_file);
|
|
|
|
|
2008-05-02 23:32:25 +00:00
|
|
|
result = FcPatternGetString(rpat, FC_FAMILY, 0, &r_family);
|
2006-07-07 18:26:51 +00:00
|
|
|
if (result != FcResultMatch)
|
2008-05-02 23:32:28 +00:00
|
|
|
r_family = NULL;
|
2006-07-07 18:26:51 +00:00
|
|
|
|
2008-05-02 23:32:28 +00:00
|
|
|
result = FcPatternGetString(rpat, FC_FULLNAME, 0, &r_fullname);
|
|
|
|
if (result != FcResultMatch)
|
|
|
|
r_fullname = NULL;
|
2006-07-07 18:26:51 +00:00
|
|
|
|
2008-05-02 23:32:28 +00:00
|
|
|
if (!(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,
|
2008-05-08 13:53:58 +00:00
|
|
|
(const char*)(r_fullname ? r_fullname : r_family), family);
|
2008-05-02 23:32:29 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
mp_msg(MSGT_ASS, MSGL_V, "[ass] Font info: family '%s', style '%s', fullname '%s',"
|
|
|
|
" slant %d, weight %d%s\n",
|
|
|
|
(const char*)r_family, (const char*)r_style, (const char*)r_fullname,
|
|
|
|
r_slant, r_weight, r_embolden ? ", embolden" : "");
|
|
|
|
|
2007-05-03 19:52:46 +00:00
|
|
|
error:
|
|
|
|
if (pat) FcPatternDestroy(pat);
|
2008-05-08 13:53:56 +00:00
|
|
|
if (rpat) FcPatternDestroy(rpat);
|
2007-05-03 19:52:46 +00:00
|
|
|
if (fset) FcFontSetDestroy(fset);
|
|
|
|
return retval;
|
2006-07-07 18:26:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Find a font. Use default family or path if necessary.
|
|
|
|
* \param priv_ private data
|
|
|
|
* \param family font family
|
|
|
|
* \param bold font weight value
|
|
|
|
* \param italic font slant value
|
|
|
|
* \param index out: font index inside a file
|
2007-08-03 13:43:11 +00:00
|
|
|
* \param code: the character that should be present in the font, can be 0
|
2006-07-07 18:26:51 +00:00
|
|
|
* \return font file path
|
|
|
|
*/
|
2007-08-03 13:43:11 +00:00
|
|
|
char* fontconfig_select(fc_instance_t* priv, const char* family, unsigned bold, unsigned italic, int* index,
|
|
|
|
uint32_t code)
|
2006-07-07 18:26:51 +00:00
|
|
|
{
|
|
|
|
char* res = 0;
|
2008-04-30 14:16:22 +00:00
|
|
|
if (!priv->config) {
|
2007-12-12 10:00:34 +00:00
|
|
|
*index = priv->index_default;
|
|
|
|
return priv->path_default;
|
|
|
|
}
|
2006-07-07 18:26:51 +00:00
|
|
|
if (family && *family)
|
2007-08-03 13:43:11 +00:00
|
|
|
res = _select_font(priv, family, bold, italic, index, code);
|
2006-07-07 18:26:51 +00:00
|
|
|
if (!res && priv->family_default) {
|
2007-08-03 13:43:11 +00:00
|
|
|
res = _select_font(priv, priv->family_default, bold, italic, index, code);
|
2006-11-26 22:53:52 +00:00
|
|
|
if (res)
|
2006-11-19 22:35:17 +00:00
|
|
|
mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_UsingDefaultFontFamily,
|
2006-07-07 18:26:51 +00:00
|
|
|
family, bold, italic, res, *index);
|
|
|
|
}
|
|
|
|
if (!res && priv->path_default) {
|
|
|
|
res = priv->path_default;
|
|
|
|
*index = priv->index_default;
|
2006-11-26 22:53:52 +00:00
|
|
|
mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_UsingDefaultFont,
|
|
|
|
family, bold, italic, res, *index);
|
2006-07-07 18:26:51 +00:00
|
|
|
}
|
|
|
|
if (!res) {
|
2007-08-03 13:43:11 +00:00
|
|
|
res = _select_font(priv, "Arial", bold, italic, index, code);
|
2006-11-26 22:53:52 +00:00
|
|
|
if (res)
|
2006-11-19 22:35:17 +00:00
|
|
|
mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_UsingArialFontFamily,
|
2006-07-07 18:26:51 +00:00
|
|
|
family, bold, italic, res, *index);
|
|
|
|
}
|
|
|
|
if (res)
|
2006-11-03 18:15:33 +00:00
|
|
|
mp_msg(MSGT_ASS, MSGL_V, "fontconfig_select: (%s, %d, %d) -> %s, %d\n",
|
2006-07-07 18:26:51 +00:00
|
|
|
family, bold, italic, res, *index);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2007-05-03 20:10:14 +00:00
|
|
|
#if (FC_VERSION < 20402)
|
2006-12-03 17:59:13 +00:00
|
|
|
static char* validate_fname(char* name)
|
|
|
|
{
|
|
|
|
char* fname;
|
|
|
|
char* p;
|
|
|
|
char* q;
|
|
|
|
unsigned code;
|
|
|
|
int sz = strlen(name);
|
|
|
|
|
|
|
|
q = fname = malloc(sz + 1);
|
|
|
|
p = name;
|
|
|
|
while (*p) {
|
|
|
|
code = utf8_get_char(&p);
|
|
|
|
if (code == 0)
|
|
|
|
break;
|
|
|
|
if ( (code > 0x7F) ||
|
|
|
|
(code == '\\') ||
|
|
|
|
(code == '/') ||
|
|
|
|
(code == ':') ||
|
|
|
|
(code == '*') ||
|
|
|
|
(code == '?') ||
|
|
|
|
(code == '<') ||
|
|
|
|
(code == '>') ||
|
|
|
|
(code == '|') ||
|
|
|
|
(code == 0))
|
|
|
|
{
|
|
|
|
*q++ = '_';
|
|
|
|
} else {
|
|
|
|
*q++ = code;
|
|
|
|
}
|
|
|
|
if (p - name > sz)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
*q = 0;
|
|
|
|
return fname;
|
|
|
|
}
|
2007-05-03 20:10:14 +00:00
|
|
|
#endif
|
2006-12-03 17:59:13 +00:00
|
|
|
|
|
|
|
/**
|
2006-12-17 12:00:55 +00: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
|
|
|
|
* With FontConfig >= 2.4.2, builds a font pattern in memory via FT_New_Memory_Face/FcFreeTypeQueryFace.
|
|
|
|
* With older FontConfig versions, save the font to ~/.mplayer/fonts.
|
2006-12-03 17:59:13 +00:00
|
|
|
*/
|
2006-12-03 18:24:11 +00:00
|
|
|
static void process_fontdata(fc_instance_t* priv, ass_library_t* library, FT_Library ftlibrary, int idx)
|
2006-12-03 17:59:13 +00:00
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
const char* name = library->fontdata[idx].name;
|
|
|
|
const char* data = library->fontdata[idx].data;
|
|
|
|
int data_size = library->fontdata[idx].size;
|
|
|
|
|
2006-12-03 18:24:11 +00:00
|
|
|
#if (FC_VERSION < 20402)
|
2007-05-03 20:10:14 +00:00
|
|
|
struct stat st;
|
|
|
|
char* fname;
|
|
|
|
const char* fonts_dir = library->fonts_dir;
|
|
|
|
char buf[1000];
|
2008-11-02 11:48:20 +00:00
|
|
|
FILE* fp = NULL;
|
2007-05-03 20:10:14 +00:00
|
|
|
|
2006-12-03 17:59:13 +00:00
|
|
|
if (!fonts_dir)
|
|
|
|
return;
|
|
|
|
rc = stat(fonts_dir, &st);
|
|
|
|
if (rc) {
|
|
|
|
int res;
|
|
|
|
#ifndef __MINGW32__
|
|
|
|
res = mkdir(fonts_dir, 0700);
|
|
|
|
#else
|
|
|
|
res = mkdir(fonts_dir);
|
|
|
|
#endif
|
|
|
|
if (res) {
|
|
|
|
mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FailedToCreateDirectory, fonts_dir);
|
|
|
|
}
|
|
|
|
} else if (!S_ISDIR(st.st_mode)) {
|
|
|
|
mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_NotADirectory, fonts_dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
fname = validate_fname((char*)name);
|
|
|
|
|
|
|
|
snprintf(buf, 1000, "%s/%s", fonts_dir, fname);
|
|
|
|
free(fname);
|
|
|
|
|
|
|
|
fp = fopen(buf, "wb");
|
|
|
|
if (!fp) return;
|
|
|
|
|
|
|
|
fwrite(data, data_size, 1, fp);
|
|
|
|
fclose(fp);
|
2006-12-03 18:24:11 +00:00
|
|
|
|
|
|
|
#else // (FC_VERSION >= 20402)
|
2007-05-03 20:10:14 +00:00
|
|
|
FT_Face face;
|
|
|
|
FcPattern* pattern;
|
|
|
|
FcFontSet* fset;
|
|
|
|
FcBool res;
|
2008-05-22 22:52:57 +00:00
|
|
|
int face_index, num_faces = 1;
|
2006-12-03 18:24:11 +00:00
|
|
|
|
2008-05-22 22:52:57 +00:00
|
|
|
for (face_index = 0; face_index < num_faces; ++face_index) {
|
2008-05-22 22:52:58 +00:00
|
|
|
rc = FT_New_Memory_Face(ftlibrary, (unsigned char*)data, data_size, face_index, &face);
|
|
|
|
if (rc) {
|
|
|
|
mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_ErrorOpeningMemoryFont, name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
num_faces = face->num_faces;
|
2006-12-03 18:24:11 +00:00
|
|
|
|
2008-05-22 22:52:58 +00:00
|
|
|
pattern = FcFreeTypeQueryFace(face, (unsigned char*)name, 0, FcConfigGetBlanks(priv->config));
|
|
|
|
if (!pattern) {
|
|
|
|
mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FunctionCallFailed, "FcFreeTypeQueryFace");
|
|
|
|
FT_Done_Face(face);
|
|
|
|
return;
|
|
|
|
}
|
2006-12-03 18:24:11 +00:00
|
|
|
|
2008-05-22 22:52:58 +00:00
|
|
|
fset = FcConfigGetFonts(priv->config, FcSetSystem); // somehow it failes when asked for FcSetApplication
|
|
|
|
if (!fset) {
|
|
|
|
mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FunctionCallFailed, "FcConfigGetFonts");
|
|
|
|
FT_Done_Face(face);
|
|
|
|
return;
|
|
|
|
}
|
2006-12-03 18:24:11 +00:00
|
|
|
|
2008-05-22 22:52:58 +00:00
|
|
|
res = FcFontSetAdd(fset, pattern);
|
|
|
|
if (!res) {
|
|
|
|
mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FunctionCallFailed, "FcFontSetAdd");
|
|
|
|
FT_Done_Face(face);
|
|
|
|
return;
|
|
|
|
}
|
2006-12-03 18:24:11 +00:00
|
|
|
|
2008-05-22 22:52:58 +00:00
|
|
|
FT_Done_Face(face);
|
2008-05-22 22:52:57 +00:00
|
|
|
}
|
2006-12-03 18:24:11 +00:00
|
|
|
#endif
|
2006-12-03 17:59:13 +00:00
|
|
|
}
|
|
|
|
|
2006-07-07 18:26:51 +00:00
|
|
|
/**
|
|
|
|
* \brief Init fontconfig.
|
2006-12-17 12:00:55 +00:00
|
|
|
* \param library libass library object
|
|
|
|
* \param ftlibrary freetype library object
|
2006-07-07 18:26:51 +00:00
|
|
|
* \param family default font family
|
|
|
|
* \param path default font path
|
|
|
|
* \return pointer to fontconfig private data
|
|
|
|
*/
|
2008-04-30 14:16:22 +00:00
|
|
|
fc_instance_t* fontconfig_init(ass_library_t* library, FT_Library ftlibrary, const char* family, const char* path, int fc)
|
2006-07-07 18:26:51 +00:00
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
fc_instance_t* priv = calloc(1, sizeof(fc_instance_t));
|
2006-12-03 17:59:13 +00:00
|
|
|
const char* dir = library->fonts_dir;
|
|
|
|
int i;
|
2006-07-07 18:26:51 +00:00
|
|
|
|
2008-04-30 14:16:22 +00:00
|
|
|
if (!fc) {
|
2007-12-12 10:00:34 +00:00
|
|
|
mp_msg(MSGT_ASS, MSGL_WARN,
|
|
|
|
MSGTR_LIBASS_FontconfigDisabledDefaultFontWillBeUsed);
|
2008-11-02 09:57:22 +00:00
|
|
|
goto exit;
|
2007-12-12 10:00:34 +00:00
|
|
|
}
|
|
|
|
|
2006-07-07 18:26:51 +00:00
|
|
|
rc = FcInit();
|
|
|
|
assert(rc);
|
|
|
|
|
|
|
|
priv->config = FcConfigGetCurrent();
|
|
|
|
if (!priv->config) {
|
2006-11-19 22:35:17 +00:00
|
|
|
mp_msg(MSGT_ASS, MSGL_FATAL, MSGTR_LIBASS_FcInitLoadConfigAndFontsFailed);
|
2008-11-02 09:57:22 +00:00
|
|
|
goto exit;
|
2006-07-07 18:26:51 +00:00
|
|
|
}
|
|
|
|
|
2006-12-03 17:59:13 +00:00
|
|
|
for (i = 0; i < library->num_fontdata; ++i)
|
2006-12-03 18:24:11 +00:00
|
|
|
process_fontdata(priv, library, ftlibrary, i);
|
2006-12-03 17:59:13 +00:00
|
|
|
|
2008-06-23 14:46:50 +00:00
|
|
|
if (dir) {
|
2008-06-23 14:46:52 +00:00
|
|
|
if (FcDirCacheValid((const FcChar8 *)dir) == FcFalse)
|
|
|
|
{
|
|
|
|
mp_msg(MSGT_ASS, MSGL_INFO, MSGTR_LIBASS_UpdatingFontCache);
|
|
|
|
if (FcGetVersion() >= 20390 && FcGetVersion() < 20400)
|
|
|
|
mp_msg(MSGT_ASS, MSGL_WARN,
|
|
|
|
MSGTR_LIBASS_BetaVersionsOfFontconfigAreNotSupported);
|
|
|
|
// FontConfig >= 2.4.0 updates cache automatically in FcConfigAppFontAddDir()
|
|
|
|
if (FcGetVersion() < 20390) {
|
|
|
|
FcFontSet* fcs;
|
|
|
|
FcStrSet* fss;
|
|
|
|
fcs = FcFontSetCreate();
|
|
|
|
fss = FcStrSetCreate();
|
|
|
|
rc = FcStrSetAdd(fss, (const FcChar8*)dir);
|
|
|
|
if (!rc) {
|
|
|
|
mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FcStrSetAddFailed);
|
|
|
|
goto ErrorFontCache;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = FcDirScan(fcs, fss, NULL, FcConfigGetBlanks(priv->config),
|
|
|
|
(const FcChar8 *)dir, FcFalse);
|
|
|
|
if (!rc) {
|
|
|
|
mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FcDirScanFailed);
|
|
|
|
goto ErrorFontCache;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = FcDirSave(fcs, fss, (const FcChar8 *)dir);
|
|
|
|
if (!rc) {
|
|
|
|
mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FcDirSave);
|
|
|
|
goto ErrorFontCache;
|
|
|
|
}
|
|
|
|
ErrorFontCache:
|
|
|
|
;
|
|
|
|
}
|
2006-09-19 13:07:45 +00:00
|
|
|
}
|
|
|
|
|
2008-06-23 14:46:52 +00:00
|
|
|
rc = FcConfigAppFontAddDir(priv->config, (const FcChar8*)dir);
|
|
|
|
if (!rc) {
|
|
|
|
mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FcConfigAppFontAddDirFailed);
|
2006-09-19 13:06:44 +00:00
|
|
|
}
|
2006-08-06 17:56:01 +00:00
|
|
|
}
|
|
|
|
|
2008-11-02 11:48:20 +00:00
|
|
|
priv->family_default = family ? strdup(family) : NULL;
|
2008-11-02 09:57:22 +00:00
|
|
|
exit:
|
2008-11-02 11:48:20 +00:00
|
|
|
priv->path_default = path ? strdup(path) : NULL;
|
2006-07-07 18:26:51 +00:00
|
|
|
priv->index_default = 0;
|
|
|
|
|
|
|
|
return priv;
|
|
|
|
}
|
|
|
|
|
2008-08-07 10:36:07 +00:00
|
|
|
#else /* CONFIG_FONTCONFIG */
|
2006-07-07 18:26:51 +00:00
|
|
|
|
2007-08-04 10:46:14 +00:00
|
|
|
char* fontconfig_select(fc_instance_t* priv, const char* family, unsigned bold, unsigned italic, int* index,
|
|
|
|
uint32_t code)
|
2006-07-07 18:26:51 +00:00
|
|
|
{
|
|
|
|
*index = priv->index_default;
|
|
|
|
return priv->path_default;
|
|
|
|
}
|
|
|
|
|
2008-05-03 12:44:16 +00:00
|
|
|
fc_instance_t* fontconfig_init(ass_library_t* library, FT_Library ftlibrary, const char* family, const char* path, int fc)
|
2006-07-07 18:26:51 +00:00
|
|
|
{
|
2006-08-21 21:46:59 +00:00
|
|
|
fc_instance_t* priv;
|
|
|
|
|
2006-11-19 22:35:17 +00:00
|
|
|
mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FontconfigDisabledDefaultFontWillBeUsed);
|
2006-07-07 18:26:51 +00:00
|
|
|
|
2006-08-21 21:46:59 +00:00
|
|
|
priv = calloc(1, sizeof(fc_instance_t));
|
2006-07-07 18:26:51 +00:00
|
|
|
|
|
|
|
priv->path_default = strdup(path);
|
|
|
|
priv->index_default = 0;
|
|
|
|
return priv;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void fontconfig_done(fc_instance_t* priv)
|
|
|
|
{
|
|
|
|
// don't call FcFini() here, library can still be used by some code
|
|
|
|
if (priv && priv->path_default) free(priv->path_default);
|
|
|
|
if (priv && priv->family_default) free(priv->family_default);
|
|
|
|
if (priv) free(priv);
|
|
|
|
}
|
|
|
|
|
|
|
|
|