Remove mapdev.vxd. It is a non-free Win9x/DOS binary and its usage remains
[mplayer/greg.git] / libass / ass_fontconfig.c
blob63e62f0074704c2aae5baf52bb5373a6deba0d44
1 // -*- c-basic-offset: 8; indent-tabs-mode: t -*-
2 // vim:ts=8:sw=8:noet:ai:
3 /*
4 Copyright (C) 2006 Evgeniy Stepanov <eugeni.stepanov@gmail.com>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "config.h"
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <assert.h>
26 #include <string.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <inttypes.h>
30 #include <ft2build.h>
31 #include FT_FREETYPE_H
33 #include "mputils.h"
34 #include "ass.h"
35 #include "ass_library.h"
36 #include "ass_fontconfig.h"
38 #ifdef HAVE_FONTCONFIG
39 #include <fontconfig/fontconfig.h>
40 #include <fontconfig/fcfreetype.h>
41 #endif
43 struct fc_instance_s {
44 #ifdef HAVE_FONTCONFIG
45 FcConfig* config;
46 #endif
47 char* family_default;
48 char* path_default;
49 int index_default;
52 #ifdef HAVE_FONTCONFIG
53 /**
54 * \brief Low-level font selection.
55 * \param priv private data
56 * \param family font family
57 * \param bold font weight value
58 * \param italic font slant value
59 * \param index out: font index inside a file
60 * \param code: the character that should be present in the font, can be 0
61 * \return font file path
62 */
63 static char* _select_font(fc_instance_t* priv, const char* family, unsigned bold, unsigned italic, int* index,
64 uint32_t code)
66 FcBool rc;
67 FcResult result;
68 FcPattern *pat = 0, *rpat;
69 int val_i;
70 FcChar8* val_s;
71 FcBool val_b;
72 FcCharSet* val_cs;
73 FcFontSet* fset = 0;
74 int curf;
75 char* retval = 0;
77 *index = 0;
79 pat = FcPatternCreate();
80 if (!pat)
81 goto error;
83 FcPatternAddString(pat, FC_FAMILY, (const FcChar8*)family);
84 FcPatternAddBool(pat, FC_OUTLINE, FcTrue);
85 FcPatternAddInteger(pat, FC_SLANT, italic);
86 FcPatternAddInteger(pat, FC_WEIGHT, bold);
88 FcDefaultSubstitute(pat);
90 rc = FcConfigSubstitute(priv->config, pat, FcMatchPattern);
91 if (!rc)
92 goto error;
94 fset = FcFontSort(priv->config, pat, FcTrue, NULL, &result);
96 for (curf = 0; curf < fset->nfont; ++curf) {
97 rpat = fset->fonts[curf];
99 result = FcPatternGetBool(rpat, FC_OUTLINE, 0, &val_b);
100 if (result != FcResultMatch)
101 continue;
102 if (val_b != FcTrue)
103 continue;
104 if (!code)
105 break;
106 result = FcPatternGetCharSet(rpat, FC_CHARSET, 0, &val_cs);
107 if (result != FcResultMatch)
108 continue;
109 if (FcCharSetHasChar(val_cs, code))
110 break;
113 if (curf >= fset->nfont)
114 goto error;
116 rpat = fset->fonts[curf];
118 result = FcPatternGetInteger(rpat, FC_INDEX, 0, &val_i);
119 if (result != FcResultMatch)
120 goto error;
121 *index = val_i;
123 result = FcPatternGetString(rpat, FC_FAMILY, 0, &val_s);
124 if (result != FcResultMatch)
125 goto error;
127 if (strcasecmp((const char*)val_s, family) != 0)
128 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_SelectedFontFamilyIsNotTheRequestedOne,
129 (const char*)val_s, family);
131 result = FcPatternGetString(rpat, FC_FILE, 0, &val_s);
132 if (result != FcResultMatch)
133 goto error;
135 retval = strdup((const char*)val_s);
136 error:
137 if (pat) FcPatternDestroy(pat);
138 if (fset) FcFontSetDestroy(fset);
139 return retval;
143 * \brief Find a font. Use default family or path if necessary.
144 * \param priv_ private data
145 * \param family font family
146 * \param bold font weight value
147 * \param italic font slant value
148 * \param index out: font index inside a file
149 * \param code: the character that should be present in the font, can be 0
150 * \return font file path
152 char* fontconfig_select(fc_instance_t* priv, const char* family, unsigned bold, unsigned italic, int* index,
153 uint32_t code)
155 char* res = 0;
156 if (family && *family)
157 res = _select_font(priv, family, bold, italic, index, code);
158 if (!res && priv->family_default) {
159 res = _select_font(priv, priv->family_default, bold, italic, index, code);
160 if (res)
161 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_UsingDefaultFontFamily,
162 family, bold, italic, res, *index);
164 if (!res && priv->path_default) {
165 res = priv->path_default;
166 *index = priv->index_default;
167 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_UsingDefaultFont,
168 family, bold, italic, res, *index);
170 if (!res) {
171 res = _select_font(priv, "Arial", bold, italic, index, code);
172 if (res)
173 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_UsingArialFontFamily,
174 family, bold, italic, res, *index);
176 if (res)
177 mp_msg(MSGT_ASS, MSGL_V, "fontconfig_select: (%s, %d, %d) -> %s, %d\n",
178 family, bold, italic, res, *index);
179 return res;
182 #if (FC_VERSION < 20402)
183 static char* validate_fname(char* name)
185 char* fname;
186 char* p;
187 char* q;
188 unsigned code;
189 int sz = strlen(name);
191 q = fname = malloc(sz + 1);
192 p = name;
193 while (*p) {
194 code = utf8_get_char(&p);
195 if (code == 0)
196 break;
197 if ( (code > 0x7F) ||
198 (code == '\\') ||
199 (code == '/') ||
200 (code == ':') ||
201 (code == '*') ||
202 (code == '?') ||
203 (code == '<') ||
204 (code == '>') ||
205 (code == '|') ||
206 (code == 0))
208 *q++ = '_';
209 } else {
210 *q++ = code;
212 if (p - name > sz)
213 break;
215 *q = 0;
216 return fname;
218 #endif
221 * \brief Process memory font.
222 * \param priv private data
223 * \param library library object
224 * \param ftlibrary freetype library object
225 * \param idx index of the processed font in library->fontdata
226 * With FontConfig >= 2.4.2, builds a font pattern in memory via FT_New_Memory_Face/FcFreeTypeQueryFace.
227 * With older FontConfig versions, save the font to ~/.mplayer/fonts.
229 static void process_fontdata(fc_instance_t* priv, ass_library_t* library, FT_Library ftlibrary, int idx)
231 int rc;
232 const char* name = library->fontdata[idx].name;
233 const char* data = library->fontdata[idx].data;
234 int data_size = library->fontdata[idx].size;
236 #if (FC_VERSION < 20402)
237 struct stat st;
238 char* fname;
239 const char* fonts_dir = library->fonts_dir;
240 char buf[1000];
241 FILE* fp = 0;
243 if (!fonts_dir)
244 return;
245 rc = stat(fonts_dir, &st);
246 if (rc) {
247 int res;
248 #ifndef __MINGW32__
249 res = mkdir(fonts_dir, 0700);
250 #else
251 res = mkdir(fonts_dir);
252 #endif
253 if (res) {
254 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FailedToCreateDirectory, fonts_dir);
256 } else if (!S_ISDIR(st.st_mode)) {
257 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_NotADirectory, fonts_dir);
260 fname = validate_fname((char*)name);
262 snprintf(buf, 1000, "%s/%s", fonts_dir, fname);
263 free(fname);
265 fp = fopen(buf, "wb");
266 if (!fp) return;
268 fwrite(data, data_size, 1, fp);
269 fclose(fp);
271 #else // (FC_VERSION >= 20402)
272 FT_Face face;
273 FcPattern* pattern;
274 FcFontSet* fset;
275 FcBool res;
277 rc = FT_New_Memory_Face(ftlibrary, (unsigned char*)data, data_size, 0, &face);
278 if (rc) {
279 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_ErrorOpeningMemoryFont, name);
280 return;
283 pattern = FcFreeTypeQueryFace(face, (unsigned char*)name, 0, FcConfigGetBlanks(priv->config));
284 if (!pattern) {
285 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FunctionCallFailed, "FcFreeTypeQueryFace");
286 FT_Done_Face(face);
287 return;
290 fset = FcConfigGetFonts(priv->config, FcSetSystem); // somehow it failes when asked for FcSetApplication
291 if (!fset) {
292 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FunctionCallFailed, "FcConfigGetFonts");
293 FT_Done_Face(face);
294 return;
297 res = FcFontSetAdd(fset, pattern);
298 if (!res) {
299 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FunctionCallFailed, "FcFontSetAdd");
300 FT_Done_Face(face);
301 return;
304 FT_Done_Face(face);
305 #endif
309 * \brief Init fontconfig.
310 * \param library libass library object
311 * \param ftlibrary freetype library object
312 * \param family default font family
313 * \param path default font path
314 * \return pointer to fontconfig private data
316 fc_instance_t* fontconfig_init(ass_library_t* library, FT_Library ftlibrary, const char* family, const char* path)
318 int rc;
319 fc_instance_t* priv = calloc(1, sizeof(fc_instance_t));
320 const char* dir = library->fonts_dir;
321 int i;
323 rc = FcInit();
324 assert(rc);
326 priv->config = FcConfigGetCurrent();
327 if (!priv->config) {
328 mp_msg(MSGT_ASS, MSGL_FATAL, MSGTR_LIBASS_FcInitLoadConfigAndFontsFailed);
329 return 0;
332 for (i = 0; i < library->num_fontdata; ++i)
333 process_fontdata(priv, library, ftlibrary, i);
335 if (FcDirCacheValid((const FcChar8 *)dir) == FcFalse)
337 mp_msg(MSGT_ASS, MSGL_INFO, MSGTR_LIBASS_UpdatingFontCache);
338 if (FcGetVersion() >= 20390 && FcGetVersion() < 20400)
339 mp_msg(MSGT_ASS, MSGL_WARN,
340 MSGTR_LIBASS_BetaVersionsOfFontconfigAreNotSupported);
341 // FontConfig >= 2.4.0 updates cache automatically in FcConfigAppFontAddDir()
342 if (FcGetVersion() < 20390) {
343 FcFontSet* fcs;
344 FcStrSet* fss;
345 fcs = FcFontSetCreate();
346 fss = FcStrSetCreate();
347 rc = FcStrSetAdd(fss, (const FcChar8*)dir);
348 if (!rc) {
349 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FcStrSetAddFailed);
350 goto ErrorFontCache;
353 rc = FcDirScan(fcs, fss, NULL, FcConfigGetBlanks(priv->config), (const FcChar8 *)dir, FcFalse);
354 if (!rc) {
355 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FcDirScanFailed);
356 goto ErrorFontCache;
359 rc = FcDirSave(fcs, fss, (const FcChar8 *)dir);
360 if (!rc) {
361 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FcDirSave);
362 goto ErrorFontCache;
364 ErrorFontCache:
369 rc = FcConfigAppFontAddDir(priv->config, (const FcChar8*)dir);
370 if (!rc) {
371 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FcConfigAppFontAddDirFailed);
374 priv->family_default = family ? strdup(family) : 0;
375 priv->path_default = path ? strdup(path) : 0;
376 priv->index_default = 0;
378 return priv;
381 #else // HAVE_FONTCONFIG
383 char* fontconfig_select(fc_instance_t* priv, const char* family, unsigned bold, unsigned italic, int* index,
384 uint32_t code)
386 *index = priv->index_default;
387 return priv->path_default;
390 fc_instance_t* fontconfig_init(ass_library_t* library, FT_Library ftlibrary, const char* family, const char* path)
392 fc_instance_t* priv;
394 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FontconfigDisabledDefaultFontWillBeUsed);
396 priv = calloc(1, sizeof(fc_instance_t));
398 priv->path_default = strdup(path);
399 priv->index_default = 0;
400 return priv;
403 #endif
405 void fontconfig_done(fc_instance_t* priv)
407 // don't call FcFini() here, library can still be used by some code
408 if (priv && priv->path_default) free(priv->path_default);
409 if (priv && priv->family_default) free(priv->family_default);
410 if (priv) free(priv);