Mercurial > mplayer.hg
annotate libass/ass_fontconfig.c @ 29162:aaff2410e8a8
Use ADDSUFFIXES function to simplify version.h dependency declaration.
author | diego |
---|---|
date | Mon, 20 Apr 2009 00:19:39 +0000 |
parents | 39006a122466 |
children | 0f1b5b68af32 |
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 | |
27393 | 40 #ifdef CONFIG_FONTCONFIG |
18937 | 41 #include <fontconfig/fontconfig.h> |
21460 | 42 #include <fontconfig/fcfreetype.h> |
18937 | 43 #endif |
44 | |
45 struct fc_instance_s { | |
27393 | 46 #ifdef CONFIG_FONTCONFIG |
18937 | 47 FcConfig* config; |
48 #endif | |
49 char* family_default; | |
50 char* path_default; | |
51 int index_default; | |
52 }; | |
53 | |
27393 | 54 #ifdef CONFIG_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 | |
28860
7fcc0bf5b27a
Treat -font/-subfont as Fontconfig pattern in libass.
eugeni
parents:
27842
diff
changeset
|
69 * \param treat_family_as_pattern treat family as fontconfig pattern |
18937 | 70 * \param bold font weight value |
71 * \param italic font slant value | |
72 * \param index out: font index inside a file | |
23980 | 73 * \param code: the character that should be present in the font, can be 0 |
18937 | 74 * \return font file path |
75 */ | |
28860
7fcc0bf5b27a
Treat -font/-subfont as Fontconfig pattern in libass.
eugeni
parents:
27842
diff
changeset
|
76 static char* _select_font(fc_instance_t* priv, const char* family, int treat_family_as_pattern, |
7fcc0bf5b27a
Treat -font/-subfont as Fontconfig pattern in libass.
eugeni
parents:
27842
diff
changeset
|
77 unsigned bold, unsigned italic, int* index, uint32_t code) |
18937 | 78 { |
79 FcBool rc; | |
80 FcResult result; | |
27842 | 81 FcPattern *pat = NULL, *rpat = NULL; |
26616 | 82 int r_index, r_slant, r_weight; |
26615 | 83 FcChar8 *r_family, *r_style, *r_file, *r_fullname; |
26616 | 84 FcBool r_outline, r_embolden; |
26613
92939846ff49
Cosmetics: rename local variables to better reflect their contents.
eugeni
parents:
26592
diff
changeset
|
85 FcCharSet* r_charset; |
27842 | 86 FcFontSet* fset = NULL; |
23980 | 87 int curf; |
27842 | 88 char* retval = NULL; |
26658
1e1ebebc8f5b
Remove extra family names from the search pattern after FcFontSort and
eugeni
parents:
26620
diff
changeset
|
89 int family_cnt; |
18937 | 90 |
91 *index = 0; | |
92 | |
28860
7fcc0bf5b27a
Treat -font/-subfont as Fontconfig pattern in libass.
eugeni
parents:
27842
diff
changeset
|
93 if (treat_family_as_pattern) |
7fcc0bf5b27a
Treat -font/-subfont as Fontconfig pattern in libass.
eugeni
parents:
27842
diff
changeset
|
94 pat = FcNameParse((const FcChar8*)family); |
7fcc0bf5b27a
Treat -font/-subfont as Fontconfig pattern in libass.
eugeni
parents:
27842
diff
changeset
|
95 else |
7fcc0bf5b27a
Treat -font/-subfont as Fontconfig pattern in libass.
eugeni
parents:
27842
diff
changeset
|
96 pat = FcPatternCreate(); |
7fcc0bf5b27a
Treat -font/-subfont as Fontconfig pattern in libass.
eugeni
parents:
27842
diff
changeset
|
97 |
18937 | 98 if (!pat) |
23215 | 99 goto error; |
18937 | 100 |
28860
7fcc0bf5b27a
Treat -font/-subfont as Fontconfig pattern in libass.
eugeni
parents:
27842
diff
changeset
|
101 if (!treat_family_as_pattern) { |
28861 | 102 FcPatternAddString(pat, FC_FAMILY, (const FcChar8*)family); |
26591 | 103 |
28861 | 104 // In SSA/ASS fonts are sometimes referenced by their "full name", |
105 // which is usually a concatenation of family name and font | |
106 // style (ex. Ottawa Bold). Full name is available from | |
107 // FontConfig pattern element FC_FULLNAME, but it is never | |
108 // used for font matching. | |
109 // Therefore, I'm removing words from the end of the name one | |
110 // by one, and adding shortened names to the pattern. It seems | |
111 // that the first value (full name in this case) has | |
112 // precedence in matching. | |
113 // An alternative approach could be to reimplement FcFontSort | |
114 // using FC_FULLNAME instead of FC_FAMILY. | |
115 family_cnt = 1; | |
116 { | |
117 char* s = strdup(family); | |
118 char* p = s + strlen(s); | |
119 while (--p > s) | |
120 if (*p == ' ' || *p == '-') { | |
121 *p = '\0'; | |
122 FcPatternAddString(pat, FC_FAMILY, (const FcChar8*)s); | |
123 ++ family_cnt; | |
124 } | |
125 free(s); | |
126 } | |
28860
7fcc0bf5b27a
Treat -font/-subfont as Fontconfig pattern in libass.
eugeni
parents:
27842
diff
changeset
|
127 } |
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
|
128 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
|
129 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
|
130 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
|
131 |
18937 | 132 FcDefaultSubstitute(pat); |
133 | |
134 rc = FcConfigSubstitute(priv->config, pat, FcMatchPattern); | |
135 if (!rc) | |
23215 | 136 goto error; |
21351
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
137 |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
138 fset = FcFontSort(priv->config, pat, FcTrue, NULL, &result); |
27096
a2c8efd917ea
Check if the font set returned from FcFontSort in not NULL.
eugeni
parents:
27095
diff
changeset
|
139 if (!fset) |
a2c8efd917ea
Check if the font set returned from FcFontSort in not NULL.
eugeni
parents:
27095
diff
changeset
|
140 goto error; |
21351
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
141 |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
142 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
|
143 FcPattern* curp = fset->fonts[curf]; |
1e1ebebc8f5b
Remove extra family names from the search pattern after FcFontSort and
eugeni
parents:
26620
diff
changeset
|
144 |
1e1ebebc8f5b
Remove extra family names from the search pattern after FcFontSort and
eugeni
parents:
26620
diff
changeset
|
145 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
|
146 if (result != FcResultMatch) |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
147 continue; |
26613
92939846ff49
Cosmetics: rename local variables to better reflect their contents.
eugeni
parents:
26592
diff
changeset
|
148 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
|
149 continue; |
23980 | 150 if (!code) |
21351
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
151 break; |
26658
1e1ebebc8f5b
Remove extra family names from the search pattern after FcFontSort and
eugeni
parents:
26620
diff
changeset
|
152 result = FcPatternGetCharSet(curp, FC_CHARSET, 0, &r_charset); |
23980 | 153 if (result != FcResultMatch) |
154 continue; | |
26613
92939846ff49
Cosmetics: rename local variables to better reflect their contents.
eugeni
parents:
26592
diff
changeset
|
155 if (FcCharSetHasChar(r_charset, code)) |
23980 | 156 break; |
21351
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
157 } |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
158 |
23980 | 159 if (curf >= fset->nfont) |
23215 | 160 goto error; |
21351
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
161 |
26819 | 162 #if (FC_VERSION >= 20297) |
28941
a038ab949e7e
Fix uninitialized memory access in ass_fontconfig.
eugeni
parents:
28861
diff
changeset
|
163 if (!treat_family_as_pattern) { |
28942 | 164 // Remove all extra family names from original pattern. |
165 // After this, FcFontRenderPrepare will select the most relevant family | |
166 // name in case there are more than one of them. | |
167 for (; family_cnt > 1; --family_cnt) | |
168 FcPatternRemove(pat, FC_FAMILY, family_cnt - 1); | |
28941
a038ab949e7e
Fix uninitialized memory access in ass_fontconfig.
eugeni
parents:
28861
diff
changeset
|
169 } |
26819 | 170 #endif |
26658
1e1ebebc8f5b
Remove extra family names from the search pattern after FcFontSort and
eugeni
parents:
26620
diff
changeset
|
171 |
1e1ebebc8f5b
Remove extra family names from the search pattern after FcFontSort and
eugeni
parents:
26620
diff
changeset
|
172 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
|
173 if (!rpat) |
1e1ebebc8f5b
Remove extra family names from the search pattern after FcFontSort and
eugeni
parents:
26620
diff
changeset
|
174 goto error; |
1e1ebebc8f5b
Remove extra family names from the search pattern after FcFontSort and
eugeni
parents:
26620
diff
changeset
|
175 |
26613
92939846ff49
Cosmetics: rename local variables to better reflect their contents.
eugeni
parents:
26592
diff
changeset
|
176 result = FcPatternGetInteger(rpat, FC_INDEX, 0, &r_index); |
18937 | 177 if (result != FcResultMatch) |
23215 | 178 goto error; |
26613
92939846ff49
Cosmetics: rename local variables to better reflect their contents.
eugeni
parents:
26592
diff
changeset
|
179 *index = r_index; |
18937 | 180 |
26614 | 181 result = FcPatternGetString(rpat, FC_FILE, 0, &r_file); |
182 if (result != FcResultMatch) | |
183 goto error; | |
184 retval = strdup((const char*)r_file); | |
185 | |
26613
92939846ff49
Cosmetics: rename local variables to better reflect their contents.
eugeni
parents:
26592
diff
changeset
|
186 result = FcPatternGetString(rpat, FC_FAMILY, 0, &r_family); |
18937 | 187 if (result != FcResultMatch) |
26615 | 188 r_family = NULL; |
18937 | 189 |
26615 | 190 result = FcPatternGetString(rpat, FC_FULLNAME, 0, &r_fullname); |
191 if (result != FcResultMatch) | |
192 r_fullname = NULL; | |
18937 | 193 |
28860
7fcc0bf5b27a
Treat -font/-subfont as Fontconfig pattern in libass.
eugeni
parents:
27842
diff
changeset
|
194 if (!treat_family_as_pattern && |
7fcc0bf5b27a
Treat -font/-subfont as Fontconfig pattern in libass.
eugeni
parents:
27842
diff
changeset
|
195 !(r_family && strcasecmp((const char*)r_family, family) == 0) && |
26615 | 196 !(r_fullname && strcasecmp((const char*)r_fullname, family) == 0)) |
197 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
|
198 (const char*)(r_fullname ? r_fullname : r_family), family); |
26616 | 199 |
200 result = FcPatternGetString(rpat, FC_STYLE, 0, &r_style); | |
201 if (result != FcResultMatch) | |
202 r_style = NULL; | |
203 | |
204 result = FcPatternGetInteger(rpat, FC_SLANT, 0, &r_slant); | |
205 if (result != FcResultMatch) | |
206 r_slant = 0; | |
207 | |
208 result = FcPatternGetInteger(rpat, FC_WEIGHT, 0, &r_weight); | |
209 if (result != FcResultMatch) | |
210 r_weight = 0; | |
211 | |
212 result = FcPatternGetBool(rpat, FC_EMBOLDEN, 0, &r_embolden); | |
213 if (result != FcResultMatch) | |
214 r_embolden = 0; | |
215 | |
216 mp_msg(MSGT_ASS, MSGL_V, "[ass] Font info: family '%s', style '%s', fullname '%s'," | |
217 " slant %d, weight %d%s\n", | |
218 (const char*)r_family, (const char*)r_style, (const char*)r_fullname, | |
219 r_slant, r_weight, r_embolden ? ", embolden" : ""); | |
220 | |
23215 | 221 error: |
222 if (pat) FcPatternDestroy(pat); | |
26658
1e1ebebc8f5b
Remove extra family names from the search pattern after FcFontSort and
eugeni
parents:
26620
diff
changeset
|
223 if (rpat) FcPatternDestroy(rpat); |
23215 | 224 if (fset) FcFontSetDestroy(fset); |
225 return retval; | |
18937 | 226 } |
227 | |
228 /** | |
229 * \brief Find a font. Use default family or path if necessary. | |
230 * \param priv_ private data | |
231 * \param family font family | |
28860
7fcc0bf5b27a
Treat -font/-subfont as Fontconfig pattern in libass.
eugeni
parents:
27842
diff
changeset
|
232 * \param treat_family_as_pattern treat family as fontconfig pattern |
18937 | 233 * \param bold font weight value |
234 * \param italic font slant value | |
235 * \param index out: font index inside a file | |
23980 | 236 * \param code: the character that should be present in the font, can be 0 |
18937 | 237 * \return font file path |
238 */ | |
28860
7fcc0bf5b27a
Treat -font/-subfont as Fontconfig pattern in libass.
eugeni
parents:
27842
diff
changeset
|
239 char* fontconfig_select(fc_instance_t* priv, const char* family, int treat_family_as_pattern, |
7fcc0bf5b27a
Treat -font/-subfont as Fontconfig pattern in libass.
eugeni
parents:
27842
diff
changeset
|
240 unsigned bold, unsigned italic, int* index, uint32_t code) |
18937 | 241 { |
242 char* res = 0; | |
26582
62ac4f8062ee
Remove libass dependency on global font_fontconfig variable.
eugeni
parents:
25351
diff
changeset
|
243 if (!priv->config) { |
25351 | 244 *index = priv->index_default; |
245 return priv->path_default; | |
246 } | |
18937 | 247 if (family && *family) |
28860
7fcc0bf5b27a
Treat -font/-subfont as Fontconfig pattern in libass.
eugeni
parents:
27842
diff
changeset
|
248 res = _select_font(priv, family, treat_family_as_pattern, bold, italic, index, code); |
18937 | 249 if (!res && priv->family_default) { |
28860
7fcc0bf5b27a
Treat -font/-subfont as Fontconfig pattern in libass.
eugeni
parents:
27842
diff
changeset
|
250 res = _select_font(priv, priv->family_default, 0, bold, italic, index, code); |
21279 | 251 if (res) |
21066 | 252 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_UsingDefaultFontFamily, |
18937 | 253 family, bold, italic, res, *index); |
254 } | |
255 if (!res && priv->path_default) { | |
256 res = priv->path_default; | |
257 *index = priv->index_default; | |
21279 | 258 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_UsingDefaultFont, |
259 family, bold, italic, res, *index); | |
18937 | 260 } |
261 if (!res) { | |
28860
7fcc0bf5b27a
Treat -font/-subfont as Fontconfig pattern in libass.
eugeni
parents:
27842
diff
changeset
|
262 res = _select_font(priv, "Arial", 0, bold, italic, index, code); |
21279 | 263 if (res) |
21066 | 264 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_UsingArialFontFamily, |
18937 | 265 family, bold, italic, res, *index); |
266 } | |
267 if (res) | |
20629
e8885ec63928
Introduce MSGT_ASS, use it for all libass messages.
eugeni
parents:
20105
diff
changeset
|
268 mp_msg(MSGT_ASS, MSGL_V, "fontconfig_select: (%s, %d, %d) -> %s, %d\n", |
18937 | 269 family, bold, italic, res, *index); |
270 return res; | |
271 } | |
272 | |
23216
b863ed752149
Move variables and a function under #ifdef FC_VERSION to avoid warnings.
eugeni
parents:
23215
diff
changeset
|
273 #if (FC_VERSION < 20402) |
21458
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
274 static char* validate_fname(char* name) |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
275 { |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
276 char* fname; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
277 char* p; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
278 char* q; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
279 unsigned code; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
280 int sz = strlen(name); |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
281 |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
282 q = fname = malloc(sz + 1); |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
283 p = name; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
284 while (*p) { |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
285 code = utf8_get_char(&p); |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
286 if (code == 0) |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
287 break; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
288 if ( (code > 0x7F) || |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
289 (code == '\\') || |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
290 (code == '/') || |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
291 (code == ':') || |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
292 (code == '*') || |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
293 (code == '?') || |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
294 (code == '<') || |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
295 (code == '>') || |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
296 (code == '|') || |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
297 (code == 0)) |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
298 { |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
299 *q++ = '_'; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
300 } else { |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
301 *q++ = code; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
302 } |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
303 if (p - name > sz) |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
304 break; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
305 } |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
306 *q = 0; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
307 return fname; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
308 } |
23216
b863ed752149
Move variables and a function under #ifdef FC_VERSION to avoid warnings.
eugeni
parents:
23215
diff
changeset
|
309 #endif |
21458
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
310 |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
311 /** |
21630 | 312 * \brief Process memory font. |
313 * \param priv private data | |
314 * \param library library object | |
315 * \param ftlibrary freetype library object | |
316 * \param idx index of the processed font in library->fontdata | |
317 * With FontConfig >= 2.4.2, builds a font pattern in memory via FT_New_Memory_Face/FcFreeTypeQueryFace. | |
318 * 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
|
319 */ |
21460 | 320 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
|
321 { |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
322 int rc; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
323 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
|
324 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
|
325 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
|
326 |
21460 | 327 #if (FC_VERSION < 20402) |
23216
b863ed752149
Move variables and a function under #ifdef FC_VERSION to avoid warnings.
eugeni
parents:
23215
diff
changeset
|
328 struct stat st; |
b863ed752149
Move variables and a function under #ifdef FC_VERSION to avoid warnings.
eugeni
parents:
23215
diff
changeset
|
329 char* fname; |
b863ed752149
Move variables and a function under #ifdef FC_VERSION to avoid warnings.
eugeni
parents:
23215
diff
changeset
|
330 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
|
331 char buf[1000]; |
27842 | 332 FILE* fp = NULL; |
23216
b863ed752149
Move variables and a function under #ifdef FC_VERSION to avoid warnings.
eugeni
parents:
23215
diff
changeset
|
333 |
21458
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
334 if (!fonts_dir) |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
335 return; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
336 rc = stat(fonts_dir, &st); |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
337 if (rc) { |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
338 int res; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
339 #ifndef __MINGW32__ |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
340 res = mkdir(fonts_dir, 0700); |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
341 #else |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
342 res = mkdir(fonts_dir); |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
343 #endif |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
344 if (res) { |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
345 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
|
346 } |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
347 } 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
|
348 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
|
349 } |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
350 |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
351 fname = validate_fname((char*)name); |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
352 |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
353 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
|
354 free(fname); |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
355 |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
356 fp = fopen(buf, "wb"); |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
357 if (!fp) return; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
358 |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
359 fwrite(data, data_size, 1, fp); |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
360 fclose(fp); |
21460 | 361 |
362 #else // (FC_VERSION >= 20402) | |
23216
b863ed752149
Move variables and a function under #ifdef FC_VERSION to avoid warnings.
eugeni
parents:
23215
diff
changeset
|
363 FT_Face face; |
b863ed752149
Move variables and a function under #ifdef FC_VERSION to avoid warnings.
eugeni
parents:
23215
diff
changeset
|
364 FcPattern* pattern; |
b863ed752149
Move variables and a function under #ifdef FC_VERSION to avoid warnings.
eugeni
parents:
23215
diff
changeset
|
365 FcFontSet* fset; |
b863ed752149
Move variables and a function under #ifdef FC_VERSION to avoid warnings.
eugeni
parents:
23215
diff
changeset
|
366 FcBool res; |
26824
6112b693fab9
Read all faces of a memory font, not just the first one.
eugeni
parents:
26819
diff
changeset
|
367 int face_index, num_faces = 1; |
21460 | 368 |
26824
6112b693fab9
Read all faces of a memory font, not just the first one.
eugeni
parents:
26819
diff
changeset
|
369 for (face_index = 0; face_index < num_faces; ++face_index) { |
26825 | 370 rc = FT_New_Memory_Face(ftlibrary, (unsigned char*)data, data_size, face_index, &face); |
371 if (rc) { | |
372 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_ErrorOpeningMemoryFont, name); | |
373 return; | |
374 } | |
375 num_faces = face->num_faces; | |
21460 | 376 |
26825 | 377 pattern = FcFreeTypeQueryFace(face, (unsigned char*)name, 0, FcConfigGetBlanks(priv->config)); |
378 if (!pattern) { | |
379 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FunctionCallFailed, "FcFreeTypeQueryFace"); | |
380 FT_Done_Face(face); | |
381 return; | |
382 } | |
21460 | 383 |
26825 | 384 fset = FcConfigGetFonts(priv->config, FcSetSystem); // somehow it failes when asked for FcSetApplication |
385 if (!fset) { | |
386 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FunctionCallFailed, "FcConfigGetFonts"); | |
387 FT_Done_Face(face); | |
388 return; | |
389 } | |
21460 | 390 |
26825 | 391 res = FcFontSetAdd(fset, pattern); |
392 if (!res) { | |
393 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FunctionCallFailed, "FcFontSetAdd"); | |
394 FT_Done_Face(face); | |
395 return; | |
396 } | |
397 | |
21460 | 398 FT_Done_Face(face); |
26824
6112b693fab9
Read all faces of a memory font, not just the first one.
eugeni
parents:
26819
diff
changeset
|
399 } |
21460 | 400 #endif |
21458
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
401 } |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
402 |
18937 | 403 /** |
404 * \brief Init fontconfig. | |
21630 | 405 * \param library libass library object |
406 * \param ftlibrary freetype library object | |
18937 | 407 * \param family default font family |
408 * \param path default font path | |
409 * \return pointer to fontconfig private data | |
410 */ | |
26582
62ac4f8062ee
Remove libass dependency on global font_fontconfig variable.
eugeni
parents:
25351
diff
changeset
|
411 fc_instance_t* fontconfig_init(ass_library_t* library, FT_Library ftlibrary, const char* family, const char* path, int fc) |
18937 | 412 { |
413 int rc; | |
414 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
|
415 const char* dir = library->fonts_dir; |
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
416 int i; |
18937 | 417 |
26582
62ac4f8062ee
Remove libass dependency on global font_fontconfig variable.
eugeni
parents:
25351
diff
changeset
|
418 if (!fc) { |
25351 | 419 mp_msg(MSGT_ASS, MSGL_WARN, |
420 MSGTR_LIBASS_FontconfigDisabledDefaultFontWillBeUsed); | |
27841
29d72a47da0c
Fallback to non-fontconfig behaviour when fontconfig initialization fails.
reimar
parents:
27393
diff
changeset
|
421 goto exit; |
25351 | 422 } |
423 | |
18937 | 424 rc = FcInit(); |
425 assert(rc); | |
426 | |
427 priv->config = FcConfigGetCurrent(); | |
428 if (!priv->config) { | |
21066 | 429 mp_msg(MSGT_ASS, MSGL_FATAL, MSGTR_LIBASS_FcInitLoadConfigAndFontsFailed); |
27841
29d72a47da0c
Fallback to non-fontconfig behaviour when fontconfig initialization fails.
reimar
parents:
27393
diff
changeset
|
430 goto exit; |
18937 | 431 } |
432 | |
21458
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21351
diff
changeset
|
433 for (i = 0; i < library->num_fontdata; ++i) |
21460 | 434 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
|
435 |
27094
bafda9ff0221
Only use application font dir if library->fonts_dir is not NULL.
eugeni
parents:
26825
diff
changeset
|
436 if (dir) { |
27095 | 437 if (FcDirCacheValid((const FcChar8 *)dir) == FcFalse) |
438 { | |
439 mp_msg(MSGT_ASS, MSGL_INFO, MSGTR_LIBASS_UpdatingFontCache); | |
440 if (FcGetVersion() >= 20390 && FcGetVersion() < 20400) | |
441 mp_msg(MSGT_ASS, MSGL_WARN, | |
442 MSGTR_LIBASS_BetaVersionsOfFontconfigAreNotSupported); | |
443 // FontConfig >= 2.4.0 updates cache automatically in FcConfigAppFontAddDir() | |
444 if (FcGetVersion() < 20390) { | |
445 FcFontSet* fcs; | |
446 FcStrSet* fss; | |
447 fcs = FcFontSetCreate(); | |
448 fss = FcStrSetCreate(); | |
449 rc = FcStrSetAdd(fss, (const FcChar8*)dir); | |
450 if (!rc) { | |
451 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FcStrSetAddFailed); | |
452 goto ErrorFontCache; | |
453 } | |
454 | |
455 rc = FcDirScan(fcs, fss, NULL, FcConfigGetBlanks(priv->config), | |
456 (const FcChar8 *)dir, FcFalse); | |
457 if (!rc) { | |
458 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FcDirScanFailed); | |
459 goto ErrorFontCache; | |
460 } | |
461 | |
462 rc = FcDirSave(fcs, fss, (const FcChar8 *)dir); | |
463 if (!rc) { | |
464 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FcDirSave); | |
465 goto ErrorFontCache; | |
466 } | |
467 ErrorFontCache: | |
468 ; | |
469 } | |
19902 | 470 } |
19340 | 471 |
27095 | 472 rc = FcConfigAppFontAddDir(priv->config, (const FcChar8*)dir); |
473 if (!rc) { | |
474 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FcConfigAppFontAddDirFailed); | |
19901
27b87a9dc19a
Don't call FcDirScan/FcDirSave with FontConfig >= 2.4.
eugeni
parents:
19481
diff
changeset
|
475 } |
19340 | 476 } |
477 | |
27842 | 478 priv->family_default = family ? strdup(family) : NULL; |
27841
29d72a47da0c
Fallback to non-fontconfig behaviour when fontconfig initialization fails.
reimar
parents:
27393
diff
changeset
|
479 exit: |
27842 | 480 priv->path_default = path ? strdup(path) : NULL; |
18937 | 481 priv->index_default = 0; |
482 | |
483 return priv; | |
484 } | |
485 | |
27393 | 486 #else /* CONFIG_FONTCONFIG */ |
18937 | 487 |
28860
7fcc0bf5b27a
Treat -font/-subfont as Fontconfig pattern in libass.
eugeni
parents:
27842
diff
changeset
|
488 char* fontconfig_select(fc_instance_t* priv, const char* family, int treat_family_as_pattern, |
7fcc0bf5b27a
Treat -font/-subfont as Fontconfig pattern in libass.
eugeni
parents:
27842
diff
changeset
|
489 unsigned bold, unsigned italic, int* index, uint32_t code) |
18937 | 490 { |
491 *index = priv->index_default; | |
492 return priv->path_default; | |
493 } | |
494 | |
26620
c64b5434f2de
Fix the second fontconfig_init function as the declaration in the .h file.
ulion
parents:
26616
diff
changeset
|
495 fc_instance_t* fontconfig_init(ass_library_t* library, FT_Library ftlibrary, const char* family, const char* path, int fc) |
18937 | 496 { |
19481 | 497 fc_instance_t* priv; |
498 | |
21066 | 499 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FontconfigDisabledDefaultFontWillBeUsed); |
18937 | 500 |
19481 | 501 priv = calloc(1, sizeof(fc_instance_t)); |
18937 | 502 |
503 priv->path_default = strdup(path); | |
504 priv->index_default = 0; | |
505 return priv; | |
506 } | |
507 | |
508 #endif | |
509 | |
510 void fontconfig_done(fc_instance_t* priv) | |
511 { | |
512 // don't call FcFini() here, library can still be used by some code | |
513 if (priv && priv->path_default) free(priv->path_default); | |
514 if (priv && priv->family_default) free(priv->family_default); | |
515 if (priv) free(priv); | |
516 } | |
517 | |
518 |