Mercurial > mplayer.hg
annotate libass/ass_fontconfig.c @ 27043:c333185b1c3e
Add missing #includes to fix 'make checkheaders'.
author | diego |
---|---|
date | Mon, 16 Jun 2008 08:40:01 +0000 |
parents | 13ef1f050cfc |
children | bafda9ff0221 |
rev | line source |
---|---|
20008
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19902
diff
changeset
|
1 // -*- c-basic-offset: 8; indent-tabs-mode: t -*- |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19902
diff
changeset
|
2 // vim:ts=8:sw=8:noet:ai: |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19902
diff
changeset
|
3 /* |
26723 | 4 * Copyright (C) 2006 Evgeniy Stepanov <eugeni.stepanov@gmail.com> |
5 * | |
26738
588ce97b44f2
Speak of libass instead of MPlayer in the libass license headers.
diego
parents:
26723
diff
changeset
|
6 * This file is part of libass. |
26723 | 7 * |
26738
588ce97b44f2
Speak of libass instead of MPlayer in the libass license headers.
diego
parents:
26723
diff
changeset
|
8 * libass is free software; you can redistribute it and/or modify |
26723 | 9 * it under the terms of the GNU General Public License as published by |
10 * the Free Software Foundation; either version 2 of the License, or | |
11 * (at your option) any later version. | |
12 * | |
26738
588ce97b44f2
Speak of libass instead of MPlayer in the libass license headers.
diego
parents:
26723
diff
changeset
|
13 * libass is distributed in the hope that it will be useful, |
26723 | 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 * GNU General Public License for more details. | |
17 * | |
18 * You should have received a copy of the GNU General Public License along | |
26738
588ce97b44f2
Speak of libass instead of MPlayer in the libass license headers.
diego
parents:
26723
diff
changeset
|
19 * with libass; if not, write to the Free Software Foundation, Inc., |
26723 | 20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
21 */ | |
20008
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19902
diff
changeset
|
22 |
18937 | 23 #include "config.h" |
24 | |
25 #include <stdlib.h> | |
26 #include <stdio.h> | |
27 #include <assert.h> | |
28 #include <string.h> | |
29 #include <sys/types.h> | |
30 #include <sys/stat.h> | |
22292 | 31 #include <inttypes.h> |
32 #include <ft2build.h> | |
33 #include FT_FREETYPE_H | |
18937 | 34 |
21026
d138463e820b
Collect all includes of mplayer headers in libass in a single file (mputils.h).
eugeni
parents:
20629
diff
changeset
|
35 #include "mputils.h" |
21458
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
36 #include "ass.h" |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
37 #include "ass_library.h" |
18937 | 38 #include "ass_fontconfig.h" |
39 | |
40 #ifdef HAVE_FONTCONFIG | |
41 #include <fontconfig/fontconfig.h> | |
21460 | 42 #include <fontconfig/fcfreetype.h> |
18937 | 43 #endif |
44 | |
45 struct fc_instance_s { | |
46 #ifdef HAVE_FONTCONFIG | |
47 FcConfig* config; | |
48 #endif | |
49 char* family_default; | |
50 char* path_default; | |
51 int index_default; | |
52 }; | |
53 | |
54 #ifdef HAVE_FONTCONFIG | |
26660
ab5729095d68
Define FC_FULLNAME and FC_EMBOLDEN to fix compilation with ancient fontconfig.
eugeni
parents:
26659
diff
changeset
|
55 |
ab5729095d68
Define FC_FULLNAME and FC_EMBOLDEN to fix compilation with ancient fontconfig.
eugeni
parents:
26659
diff
changeset
|
56 // 4yo fontconfig does not have these. |
ab5729095d68
Define FC_FULLNAME and FC_EMBOLDEN to fix compilation with ancient fontconfig.
eugeni
parents:
26659
diff
changeset
|
57 // They are only needed for debug output, anyway. |
ab5729095d68
Define FC_FULLNAME and FC_EMBOLDEN to fix compilation with ancient fontconfig.
eugeni
parents:
26659
diff
changeset
|
58 #ifndef FC_FULLNAME |
ab5729095d68
Define FC_FULLNAME and FC_EMBOLDEN to fix compilation with ancient fontconfig.
eugeni
parents:
26659
diff
changeset
|
59 #define FC_FULLNAME "fullname" |
ab5729095d68
Define FC_FULLNAME and FC_EMBOLDEN to fix compilation with ancient fontconfig.
eugeni
parents:
26659
diff
changeset
|
60 #endif |
ab5729095d68
Define FC_FULLNAME and FC_EMBOLDEN to fix compilation with ancient fontconfig.
eugeni
parents:
26659
diff
changeset
|
61 #ifndef FC_EMBOLDEN |
ab5729095d68
Define FC_FULLNAME and FC_EMBOLDEN to fix compilation with ancient fontconfig.
eugeni
parents:
26659
diff
changeset
|
62 #define FC_EMBOLDEN "embolden" |
ab5729095d68
Define FC_FULLNAME and FC_EMBOLDEN to fix compilation with ancient fontconfig.
eugeni
parents:
26659
diff
changeset
|
63 #endif |
ab5729095d68
Define FC_FULLNAME and FC_EMBOLDEN to fix compilation with ancient fontconfig.
eugeni
parents:
26659
diff
changeset
|
64 |
18937 | 65 /** |
66 * \brief Low-level font selection. | |
67 * \param priv private data | |
68 * \param family font family | |
69 * \param bold font weight value | |
70 * \param italic font slant value | |
71 * \param index out: font index inside a file | |
23980 | 72 * \param code: the character that should be present in the font, can be 0 |
18937 | 73 * \return font file path |
74 */ | |
21351
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
75 static char* _select_font(fc_instance_t* priv, const char* family, unsigned bold, unsigned italic, int* index, |
23980 | 76 uint32_t code) |
18937 | 77 { |
78 FcBool rc; | |
79 FcResult result; | |
26709 | 80 FcPattern *pat = 0, *rpat = 0; |
26616 | 81 int r_index, r_slant, r_weight; |
26615 | 82 FcChar8 *r_family, *r_style, *r_file, *r_fullname; |
26616 | 83 FcBool r_outline, r_embolden; |
26613
92939846ff49
Cosmetics: rename local variables to better reflect their contents.
eugeni
parents:
26592
diff
changeset
|
84 FcCharSet* r_charset; |
23215 | 85 FcFontSet* fset = 0; |
23980 | 86 int curf; |
23215 | 87 char* retval = 0; |
26658
1e1ebebc8f5b
Remove extra family names from the search pattern after FcFontSort and
eugeni
parents:
26620
diff
changeset
|
88 int family_cnt; |
18937 | 89 |
90 *index = 0; | |
91 | |
19063
5525af2ee4c7
Use FcPatternAdd-Type instead of FcNameParse. The latter, as it turns out, requires escaping of some characters ('-', maybe more).
eugeni
parents:
19001
diff
changeset
|
92 pat = FcPatternCreate(); |
18937 | 93 if (!pat) |
23215 | 94 goto error; |
18937 | 95 |
19063
5525af2ee4c7
Use FcPatternAdd-Type instead of FcNameParse. The latter, as it turns out, requires escaping of some characters ('-', maybe more).
eugeni
parents:
19001
diff
changeset
|
96 FcPatternAddString(pat, FC_FAMILY, (const FcChar8*)family); |
26591 | 97 |
98 // In SSA/ASS fonts are sometimes referenced by their "full name", | |
99 // which is usually a concatenation of family name and font | |
100 // style (ex. Ottawa Bold). Full name is available from | |
101 // FontConfig pattern element FC_FULLNAME, but it is never | |
102 // used for font matching. | |
103 // Therefore, I'm removing words from the end of the name one | |
104 // by one, and adding shortened names to the pattern. It seems | |
105 // that the first value (full name in this case) has | |
106 // precedence in matching. | |
107 // An alternative approach could be to reimplement FcFontSort | |
108 // using FC_FULLNAME instead of FC_FAMILY. | |
26658
1e1ebebc8f5b
Remove extra family names from the search pattern after FcFontSort and
eugeni
parents:
26620
diff
changeset
|
109 family_cnt = 1; |
26710
605722f2c1b6
When building font pattern, treat both ' ' and '-' as word separators.
eugeni
parents:
26709
diff
changeset
|
110 { |
605722f2c1b6
When building font pattern, treat both ' ' and '-' as word separators.
eugeni
parents:
26709
diff
changeset
|
111 char* s = strdup(family); |
605722f2c1b6
When building font pattern, treat both ' ' and '-' as word separators.
eugeni
parents:
26709
diff
changeset
|
112 char* p = s + strlen(s); |
605722f2c1b6
When building font pattern, treat both ' ' and '-' as word separators.
eugeni
parents:
26709
diff
changeset
|
113 while (--p > s) |
605722f2c1b6
When building font pattern, treat both ' ' and '-' as word separators.
eugeni
parents:
26709
diff
changeset
|
114 if (*p == ' ' || *p == '-') { |
605722f2c1b6
When building font pattern, treat both ' ' and '-' as word separators.
eugeni
parents:
26709
diff
changeset
|
115 *p = '\0'; |
605722f2c1b6
When building font pattern, treat both ' ' and '-' as word separators.
eugeni
parents:
26709
diff
changeset
|
116 FcPatternAddString(pat, FC_FAMILY, (const FcChar8*)s); |
605722f2c1b6
When building font pattern, treat both ' ' and '-' as word separators.
eugeni
parents:
26709
diff
changeset
|
117 ++ family_cnt; |
605722f2c1b6
When building font pattern, treat both ' ' and '-' as word separators.
eugeni
parents:
26709
diff
changeset
|
118 } |
26591 | 119 free(s); |
120 } | |
19063
5525af2ee4c7
Use FcPatternAdd-Type instead of FcNameParse. The latter, as it turns out, requires escaping of some characters ('-', maybe more).
eugeni
parents:
19001
diff
changeset
|
121 FcPatternAddBool(pat, FC_OUTLINE, FcTrue); |
5525af2ee4c7
Use FcPatternAdd-Type instead of FcNameParse. The latter, as it turns out, requires escaping of some characters ('-', maybe more).
eugeni
parents:
19001
diff
changeset
|
122 FcPatternAddInteger(pat, FC_SLANT, italic); |
5525af2ee4c7
Use FcPatternAdd-Type instead of FcNameParse. The latter, as it turns out, requires escaping of some characters ('-', maybe more).
eugeni
parents:
19001
diff
changeset
|
123 FcPatternAddInteger(pat, FC_WEIGHT, bold); |
5525af2ee4c7
Use FcPatternAdd-Type instead of FcNameParse. The latter, as it turns out, requires escaping of some characters ('-', maybe more).
eugeni
parents:
19001
diff
changeset
|
124 |
18937 | 125 FcDefaultSubstitute(pat); |
126 | |
127 rc = FcConfigSubstitute(priv->config, pat, FcMatchPattern); | |
128 if (!rc) | |
23215 | 129 goto error; |
21351
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
130 |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
131 fset = FcFontSort(priv->config, pat, FcTrue, NULL, &result); |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
132 |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
133 for (curf = 0; curf < fset->nfont; ++curf) { |
26658
1e1ebebc8f5b
Remove extra family names from the search pattern after FcFontSort and
eugeni
parents:
26620
diff
changeset
|
134 FcPattern* curp = fset->fonts[curf]; |
1e1ebebc8f5b
Remove extra family names from the search pattern after FcFontSort and
eugeni
parents:
26620
diff
changeset
|
135 |
1e1ebebc8f5b
Remove extra family names from the search pattern after FcFontSort and
eugeni
parents:
26620
diff
changeset
|
136 result = FcPatternGetBool(curp, FC_OUTLINE, 0, &r_outline); |
21351
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
137 if (result != FcResultMatch) |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
138 continue; |
26613
92939846ff49
Cosmetics: rename local variables to better reflect their contents.
eugeni
parents:
26592
diff
changeset
|
139 if (r_outline != FcTrue) |
21351
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
140 continue; |
23980 | 141 if (!code) |
21351
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
142 break; |
26658
1e1ebebc8f5b
Remove extra family names from the search pattern after FcFontSort and
eugeni
parents:
26620
diff
changeset
|
143 result = FcPatternGetCharSet(curp, FC_CHARSET, 0, &r_charset); |
23980 | 144 if (result != FcResultMatch) |
145 continue; | |
26613
92939846ff49
Cosmetics: rename local variables to better reflect their contents.
eugeni
parents:
26592
diff
changeset
|
146 if (FcCharSetHasChar(r_charset, code)) |
23980 | 147 break; |
21351
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
148 } |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
149 |
23980 | 150 if (curf >= fset->nfont) |
23215 | 151 goto error; |
21351
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
152 |
26819 | 153 #if (FC_VERSION >= 20297) |
26658
1e1ebebc8f5b
Remove extra family names from the search pattern after FcFontSort and
eugeni
parents:
26620
diff
changeset
|
154 // Remove all extra family names from original pattern. |
1e1ebebc8f5b
Remove extra family names from the search pattern after FcFontSort and
eugeni
parents:
26620
diff
changeset
|
155 // After this, FcFontRenderPrepare will select the most relevant family |
1e1ebebc8f5b
Remove extra family names from the search pattern after FcFontSort and
eugeni
parents:
26620
diff
changeset
|
156 // name in case there are more than one of them. |
1e1ebebc8f5b
Remove extra family names from the search pattern after FcFontSort and
eugeni
parents:
26620
diff
changeset
|
157 for (; family_cnt > 1; --family_cnt) |
1e1ebebc8f5b
Remove extra family names from the search pattern after FcFontSort and
eugeni
parents:
26620
diff
changeset
|
158 FcPatternRemove(pat, FC_FAMILY, family_cnt - 1); |
26819 | 159 #endif |
26658
1e1ebebc8f5b
Remove extra family names from the search pattern after FcFontSort and
eugeni
parents:
26620
diff
changeset
|
160 |
1e1ebebc8f5b
Remove extra family names from the search pattern after FcFontSort and
eugeni
parents:
26620
diff
changeset
|
161 rpat = FcFontRenderPrepare(priv->config, pat, fset->fonts[curf]); |
1e1ebebc8f5b
Remove extra family names from the search pattern after FcFontSort and
eugeni
parents:
26620
diff
changeset
|
162 if (!rpat) |
1e1ebebc8f5b
Remove extra family names from the search pattern after FcFontSort and
eugeni
parents:
26620
diff
changeset
|
163 goto error; |
1e1ebebc8f5b
Remove extra family names from the search pattern after FcFontSort and
eugeni
parents:
26620
diff
changeset
|
164 |
26613
92939846ff49
Cosmetics: rename local variables to better reflect their contents.
eugeni
parents:
26592
diff
changeset
|
165 result = FcPatternGetInteger(rpat, FC_INDEX, 0, &r_index); |
18937 | 166 if (result != FcResultMatch) |
23215 | 167 goto error; |
26613
92939846ff49
Cosmetics: rename local variables to better reflect their contents.
eugeni
parents:
26592
diff
changeset
|
168 *index = r_index; |
18937 | 169 |
26614 | 170 result = FcPatternGetString(rpat, FC_FILE, 0, &r_file); |
171 if (result != FcResultMatch) | |
172 goto error; | |
173 retval = strdup((const char*)r_file); | |
174 | |
26613
92939846ff49
Cosmetics: rename local variables to better reflect their contents.
eugeni
parents:
26592
diff
changeset
|
175 result = FcPatternGetString(rpat, FC_FAMILY, 0, &r_family); |
18937 | 176 if (result != FcResultMatch) |
26615 | 177 r_family = NULL; |
18937 | 178 |
26615 | 179 result = FcPatternGetString(rpat, FC_FULLNAME, 0, &r_fullname); |
180 if (result != FcResultMatch) | |
181 r_fullname = NULL; | |
18937 | 182 |
26615 | 183 if (!(r_family && strcasecmp((const char*)r_family, family) == 0) && |
184 !(r_fullname && strcasecmp((const char*)r_fullname, family) == 0)) | |
185 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_SelectedFontFamilyIsNotTheRequestedOne, | |
26659
9e1d66c990fa
If both full name and family are available, use the former in inexact match warning.
eugeni
parents:
26658
diff
changeset
|
186 (const char*)(r_fullname ? r_fullname : r_family), family); |
26616 | 187 |
188 result = FcPatternGetString(rpat, FC_STYLE, 0, &r_style); | |
189 if (result != FcResultMatch) | |
190 r_style = NULL; | |
191 | |
192 result = FcPatternGetInteger(rpat, FC_SLANT, 0, &r_slant); | |
193 if (result != FcResultMatch) | |
194 r_slant = 0; | |
195 | |
196 result = FcPatternGetInteger(rpat, FC_WEIGHT, 0, &r_weight); | |
197 if (result != FcResultMatch) | |
198 r_weight = 0; | |
199 | |
200 result = FcPatternGetBool(rpat, FC_EMBOLDEN, 0, &r_embolden); | |
201 if (result != FcResultMatch) | |
202 r_embolden = 0; | |
203 | |
204 mp_msg(MSGT_ASS, MSGL_V, "[ass] Font info: family '%s', style '%s', fullname '%s'," | |
205 " slant %d, weight %d%s\n", | |
206 (const char*)r_family, (const char*)r_style, (const char*)r_fullname, | |
207 r_slant, r_weight, r_embolden ? ", embolden" : ""); | |
208 | |
23215 | 209 error: |
210 if (pat) FcPatternDestroy(pat); | |
26658
1e1ebebc8f5b
Remove extra family names from the search pattern after FcFontSort and
eugeni
parents:
26620
diff
changeset
|
211 if (rpat) FcPatternDestroy(rpat); |
23215 | 212 if (fset) FcFontSetDestroy(fset); |
213 return retval; | |
18937 | 214 } |
215 | |
216 /** | |
217 * \brief Find a font. Use default family or path if necessary. | |
218 * \param priv_ private data | |
219 * \param family font family | |
220 * \param bold font weight value | |
221 * \param italic font slant value | |
222 * \param index out: font index inside a file | |
23980 | 223 * \param code: the character that should be present in the font, can be 0 |
18937 | 224 * \return font file path |
225 */ | |
23980 | 226 char* fontconfig_select(fc_instance_t* priv, const char* family, unsigned bold, unsigned italic, int* index, |
227 uint32_t code) | |
18937 | 228 { |
229 char* res = 0; | |
26582
62ac4f8062ee
Remove libass dependency on global font_fontconfig variable.
eugeni
parents:
25351
diff
changeset
|
230 if (!priv->config) { |
25351 | 231 *index = priv->index_default; |
232 return priv->path_default; | |
233 } | |
18937 | 234 if (family && *family) |
23980 | 235 res = _select_font(priv, family, bold, italic, index, code); |
18937 | 236 if (!res && priv->family_default) { |
23980 | 237 res = _select_font(priv, priv->family_default, bold, italic, index, code); |
21279 | 238 if (res) |
21066 | 239 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_UsingDefaultFontFamily, |
18937 | 240 family, bold, italic, res, *index); |
241 } | |
242 if (!res && priv->path_default) { | |
243 res = priv->path_default; | |
244 *index = priv->index_default; | |
21279 | 245 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_UsingDefaultFont, |
246 family, bold, italic, res, *index); | |
18937 | 247 } |
248 if (!res) { | |
23980 | 249 res = _select_font(priv, "Arial", bold, italic, index, code); |
21279 | 250 if (res) |
21066 | 251 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_UsingArialFontFamily, |
18937 | 252 family, bold, italic, res, *index); |
253 } | |
254 if (res) | |
20629
e8885ec63928
Introduce MSGT_ASS, use it for all libass messages.
eugeni
parents:
20105
diff
changeset
|
255 mp_msg(MSGT_ASS, MSGL_V, "fontconfig_select: (%s, %d, %d) -> %s, %d\n", |
18937 | 256 family, bold, italic, res, *index); |
257 return res; | |
258 } | |
259 | |
23216
b863ed752149
Move variables and a function under #ifdef FC_VERSION to avoid warnings.
eugeni
parents:
23215
diff
changeset
|
260 #if (FC_VERSION < 20402) |
21458
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
261 static char* validate_fname(char* name) |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
262 { |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
263 char* fname; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
264 char* p; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
265 char* q; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
266 unsigned code; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
267 int sz = strlen(name); |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
268 |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
269 q = fname = malloc(sz + 1); |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
270 p = name; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
271 while (*p) { |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
272 code = utf8_get_char(&p); |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
273 if (code == 0) |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
274 break; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
275 if ( (code > 0x7F) || |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
276 (code == '\\') || |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
277 (code == '/') || |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
278 (code == ':') || |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
279 (code == '*') || |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
280 (code == '?') || |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
281 (code == '<') || |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
282 (code == '>') || |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
283 (code == '|') || |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
284 (code == 0)) |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
285 { |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
286 *q++ = '_'; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
287 } else { |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
288 *q++ = code; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
289 } |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
290 if (p - name > sz) |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
291 break; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
292 } |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
293 *q = 0; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
294 return fname; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
295 } |
23216
b863ed752149
Move variables and a function under #ifdef FC_VERSION to avoid warnings.
eugeni
parents:
23215
diff
changeset
|
296 #endif |
21458
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
297 |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
298 /** |
21630 | 299 * \brief Process memory font. |
300 * \param priv private data | |
301 * \param library library object | |
302 * \param ftlibrary freetype library object | |
303 * \param idx index of the processed font in library->fontdata | |
304 * With FontConfig >= 2.4.2, builds a font pattern in memory via FT_New_Memory_Face/FcFreeTypeQueryFace. | |
305 * With older FontConfig versions, save the font to ~/.mplayer/fonts. | |
21458
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
306 */ |
21460 | 307 static void process_fontdata(fc_instance_t* priv, ass_library_t* library, FT_Library ftlibrary, int idx) |
21458
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
308 { |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
309 int rc; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
310 const char* name = library->fontdata[idx].name; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
311 const char* data = library->fontdata[idx].data; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
312 int data_size = library->fontdata[idx].size; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
313 |
21460 | 314 #if (FC_VERSION < 20402) |
23216
b863ed752149
Move variables and a function under #ifdef FC_VERSION to avoid warnings.
eugeni
parents:
23215
diff
changeset
|
315 struct stat st; |
b863ed752149
Move variables and a function under #ifdef FC_VERSION to avoid warnings.
eugeni
parents:
23215
diff
changeset
|
316 char* fname; |
b863ed752149
Move variables and a function under #ifdef FC_VERSION to avoid warnings.
eugeni
parents:
23215
diff
changeset
|
317 const char* fonts_dir = library->fonts_dir; |
b863ed752149
Move variables and a function under #ifdef FC_VERSION to avoid warnings.
eugeni
parents:
23215
diff
changeset
|
318 char buf[1000]; |
b863ed752149
Move variables and a function under #ifdef FC_VERSION to avoid warnings.
eugeni
parents:
23215
diff
changeset
|
319 FILE* fp = 0; |
b863ed752149
Move variables and a function under #ifdef FC_VERSION to avoid warnings.
eugeni
parents:
23215
diff
changeset
|
320 |
21458
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
321 if (!fonts_dir) |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
322 return; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
323 rc = stat(fonts_dir, &st); |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
324 if (rc) { |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
325 int res; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
326 #ifndef __MINGW32__ |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
327 res = mkdir(fonts_dir, 0700); |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
328 #else |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
329 res = mkdir(fonts_dir); |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
330 #endif |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
331 if (res) { |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
332 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FailedToCreateDirectory, fonts_dir); |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
333 } |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
334 } else if (!S_ISDIR(st.st_mode)) { |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
335 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_NotADirectory, fonts_dir); |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
336 } |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
337 |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
338 fname = validate_fname((char*)name); |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
339 |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
340 snprintf(buf, 1000, "%s/%s", fonts_dir, fname); |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
341 free(fname); |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
342 |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
343 fp = fopen(buf, "wb"); |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
344 if (!fp) return; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
345 |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
346 fwrite(data, data_size, 1, fp); |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
347 fclose(fp); |
21460 | 348 |
349 #else // (FC_VERSION >= 20402) | |
23216
b863ed752149
Move variables and a function under #ifdef FC_VERSION to avoid warnings.
eugeni
parents:
23215
diff
changeset
|
350 FT_Face face; |
b863ed752149
Move variables and a function under #ifdef FC_VERSION to avoid warnings.
eugeni
parents:
23215
diff
changeset
|
351 FcPattern* pattern; |
b863ed752149
Move variables and a function under #ifdef FC_VERSION to avoid warnings.
eugeni
parents:
23215
diff
changeset
|
352 FcFontSet* fset; |
b863ed752149
Move variables and a function under #ifdef FC_VERSION to avoid warnings.
eugeni
parents:
23215
diff
changeset
|
353 FcBool res; |
26824
6112b693fab9
Read all faces of a memory font, not just the first one.
eugeni
parents:
26819
diff
changeset
|
354 int face_index, num_faces = 1; |
21460 | 355 |
26824
6112b693fab9
Read all faces of a memory font, not just the first one.
eugeni
parents:
26819
diff
changeset
|
356 for (face_index = 0; face_index < num_faces; ++face_index) { |
26825 | 357 rc = FT_New_Memory_Face(ftlibrary, (unsigned char*)data, data_size, face_index, &face); |
358 if (rc) { | |
359 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_ErrorOpeningMemoryFont, name); | |
360 return; | |
361 } | |
362 num_faces = face->num_faces; | |
21460 | 363 |
26825 | 364 pattern = FcFreeTypeQueryFace(face, (unsigned char*)name, 0, FcConfigGetBlanks(priv->config)); |
365 if (!pattern) { | |
366 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FunctionCallFailed, "FcFreeTypeQueryFace"); | |
367 FT_Done_Face(face); | |
368 return; | |
369 } | |
21460 | 370 |
26825 | 371 fset = FcConfigGetFonts(priv->config, FcSetSystem); // somehow it failes when asked for FcSetApplication |
372 if (!fset) { | |
373 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FunctionCallFailed, "FcConfigGetFonts"); | |
374 FT_Done_Face(face); | |
375 return; | |
376 } | |
21460 | 377 |
26825 | 378 res = FcFontSetAdd(fset, pattern); |
379 if (!res) { | |
380 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FunctionCallFailed, "FcFontSetAdd"); | |
381 FT_Done_Face(face); | |
382 return; | |
383 } | |
384 | |
21460 | 385 FT_Done_Face(face); |
26824
6112b693fab9
Read all faces of a memory font, not just the first one.
eugeni
parents:
26819
diff
changeset
|
386 } |
21460 | 387 #endif |
21458
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
388 } |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
389 |
18937 | 390 /** |
391 * \brief Init fontconfig. | |
21630 | 392 * \param library libass library object |
393 * \param ftlibrary freetype library object | |
18937 | 394 * \param family default font family |
395 * \param path default font path | |
396 * \return pointer to fontconfig private data | |
397 */ | |
26582
62ac4f8062ee
Remove libass dependency on global font_fontconfig variable.
eugeni
parents:
25351
diff
changeset
|
398 fc_instance_t* fontconfig_init(ass_library_t* library, FT_Library ftlibrary, const char* family, const char* path, int fc) |
18937 | 399 { |
400 int rc; | |
401 fc_instance_t* priv = calloc(1, sizeof(fc_instance_t)); | |
21458
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
402 const char* dir = library->fonts_dir; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
403 int i; |
18937 | 404 |
26582
62ac4f8062ee
Remove libass dependency on global font_fontconfig variable.
eugeni
parents:
25351
diff
changeset
|
405 if (!fc) { |
25351 | 406 mp_msg(MSGT_ASS, MSGL_WARN, |
407 MSGTR_LIBASS_FontconfigDisabledDefaultFontWillBeUsed); | |
26582
62ac4f8062ee
Remove libass dependency on global font_fontconfig variable.
eugeni
parents:
25351
diff
changeset
|
408 priv->config = NULL; |
25351 | 409 priv->path_default = strdup(path); |
410 priv->index_default = 0; | |
411 return priv; | |
412 } | |
413 | |
18937 | 414 rc = FcInit(); |
415 assert(rc); | |
416 | |
417 priv->config = FcConfigGetCurrent(); | |
418 if (!priv->config) { | |
21066 | 419 mp_msg(MSGT_ASS, MSGL_FATAL, MSGTR_LIBASS_FcInitLoadConfigAndFontsFailed); |
18937 | 420 return 0; |
421 } | |
422 | |
21458
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
423 for (i = 0; i < library->num_fontdata; ++i) |
21460 | 424 process_fontdata(priv, library, ftlibrary, i); |
21458
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
425 |
19340 | 426 if (FcDirCacheValid((const FcChar8 *)dir) == FcFalse) |
427 { | |
21066 | 428 mp_msg(MSGT_ASS, MSGL_INFO, MSGTR_LIBASS_UpdatingFontCache); |
20105
bcb586a0800c
Avoid crash with fontconfig 2.3.9x (as shipped with SuSE 10.1, FcDirScan is broken)
reimar
parents:
20008
diff
changeset
|
429 if (FcGetVersion() >= 20390 && FcGetVersion() < 20400) |
20629
e8885ec63928
Introduce MSGT_ASS, use it for all libass messages.
eugeni
parents:
20105
diff
changeset
|
430 mp_msg(MSGT_ASS, MSGL_WARN, |
21066 | 431 MSGTR_LIBASS_BetaVersionsOfFontconfigAreNotSupported); |
19901
27b87a9dc19a
Don't call FcDirScan/FcDirSave with FontConfig >= 2.4.
eugeni
parents:
19481
diff
changeset
|
432 // FontConfig >= 2.4.0 updates cache automatically in FcConfigAppFontAddDir() |
20105
bcb586a0800c
Avoid crash with fontconfig 2.3.9x (as shipped with SuSE 10.1, FcDirScan is broken)
reimar
parents:
20008
diff
changeset
|
433 if (FcGetVersion() < 20390) { |
19902 | 434 FcFontSet* fcs; |
435 FcStrSet* fss; | |
436 fcs = FcFontSetCreate(); | |
437 fss = FcStrSetCreate(); | |
438 rc = FcStrSetAdd(fss, (const FcChar8*)dir); | |
439 if (!rc) { | |
21066 | 440 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FcStrSetAddFailed); |
19902 | 441 goto ErrorFontCache; |
442 } | |
19340 | 443 |
19902 | 444 rc = FcDirScan(fcs, fss, NULL, FcConfigGetBlanks(priv->config), (const FcChar8 *)dir, FcFalse); |
445 if (!rc) { | |
21066 | 446 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FcDirScanFailed); |
19902 | 447 goto ErrorFontCache; |
448 } | |
19340 | 449 |
19902 | 450 rc = FcDirSave(fcs, fss, (const FcChar8 *)dir); |
451 if (!rc) { | |
21066 | 452 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FcDirSave); |
19902 | 453 goto ErrorFontCache; |
454 } | |
455 ErrorFontCache: | |
456 ; | |
19901
27b87a9dc19a
Don't call FcDirScan/FcDirSave with FontConfig >= 2.4.
eugeni
parents:
19481
diff
changeset
|
457 } |
19340 | 458 } |
459 | |
18937 | 460 rc = FcConfigAppFontAddDir(priv->config, (const FcChar8*)dir); |
461 if (!rc) { | |
21066 | 462 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FcConfigAppFontAddDirFailed); |
18937 | 463 } |
464 | |
465 priv->family_default = family ? strdup(family) : 0; | |
22946
e7f1374cac88
Don't check if user-supplied default font path exists.
eugeni
parents:
22292
diff
changeset
|
466 priv->path_default = path ? strdup(path) : 0; |
18937 | 467 priv->index_default = 0; |
468 | |
469 return priv; | |
470 } | |
471 | |
21630 | 472 #else // HAVE_FONTCONFIG |
18937 | 473 |
23988
3b830c7e0eb2
Fix compilation of ass_fontconfig.c when fontconfig is disabled.
iive
parents:
23980
diff
changeset
|
474 char* fontconfig_select(fc_instance_t* priv, const char* family, unsigned bold, unsigned italic, int* index, |
3b830c7e0eb2
Fix compilation of ass_fontconfig.c when fontconfig is disabled.
iive
parents:
23980
diff
changeset
|
475 uint32_t code) |
18937 | 476 { |
477 *index = priv->index_default; | |
478 return priv->path_default; | |
479 } | |
480 | |
26620
c64b5434f2de
Fix the second fontconfig_init function as the declaration in the .h file.
ulion
parents:
26616
diff
changeset
|
481 fc_instance_t* fontconfig_init(ass_library_t* library, FT_Library ftlibrary, const char* family, const char* path, int fc) |
18937 | 482 { |
19481 | 483 fc_instance_t* priv; |
484 | |
21066 | 485 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FontconfigDisabledDefaultFontWillBeUsed); |
18937 | 486 |
19481 | 487 priv = calloc(1, sizeof(fc_instance_t)); |
18937 | 488 |
489 priv->path_default = strdup(path); | |
490 priv->index_default = 0; | |
491 return priv; | |
492 } | |
493 | |
494 #endif | |
495 | |
496 void fontconfig_done(fc_instance_t* priv) | |
497 { | |
498 // don't call FcFini() here, library can still be used by some code | |
499 if (priv && priv->path_default) free(priv->path_default); | |
500 if (priv && priv->family_default) free(priv->family_default); | |
501 if (priv) free(priv); | |
502 } | |
503 | |
504 |