Mercurial > mplayer.hg
annotate libass/ass_fontconfig.c @ 21351:c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
author | eugeni |
---|---|
date | Tue, 28 Nov 2006 22:50:02 +0000 |
parents | 9040bce9f768 |
children | 7af6c25a0cfc |
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 /* |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19902
diff
changeset
|
4 Copyright (C) 2006 Evgeniy Stepanov <eugeni.stepanov@gmail.com> |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19902
diff
changeset
|
5 |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19902
diff
changeset
|
6 This program is free software; you can redistribute it and/or modify |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19902
diff
changeset
|
7 it under the terms of the GNU General Public License as published by |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19902
diff
changeset
|
8 the Free Software Foundation; either version 2 of the License, or |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19902
diff
changeset
|
9 (at your option) any later version. |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19902
diff
changeset
|
10 |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19902
diff
changeset
|
11 This program is distributed in the hope that it will be useful, |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19902
diff
changeset
|
12 but WITHOUT ANY WARRANTY; without even the implied warranty of |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19902
diff
changeset
|
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19902
diff
changeset
|
14 GNU General Public License for more details. |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19902
diff
changeset
|
15 |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19902
diff
changeset
|
16 You should have received a copy of the GNU General Public License |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19902
diff
changeset
|
17 along with this program; if not, write to the Free Software |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19902
diff
changeset
|
18 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19902
diff
changeset
|
19 */ |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19902
diff
changeset
|
20 |
18937 | 21 #include "config.h" |
22 | |
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 | |
21026
d138463e820b
Collect all includes of mplayer headers in libass in a single file (mputils.h).
eugeni
parents:
20629
diff
changeset
|
30 #include "mputils.h" |
18937 | 31 #include "ass_fontconfig.h" |
32 | |
33 #ifdef HAVE_FONTCONFIG | |
34 #include <fontconfig/fontconfig.h> | |
35 #endif | |
36 | |
37 struct fc_instance_s { | |
38 #ifdef HAVE_FONTCONFIG | |
39 FcConfig* config; | |
40 #endif | |
41 char* family_default; | |
42 char* path_default; | |
43 int index_default; | |
44 }; | |
45 | |
46 #ifdef HAVE_FONTCONFIG | |
47 /** | |
48 * \brief Low-level font selection. | |
49 * \param priv private data | |
50 * \param family font family | |
51 * \param bold font weight value | |
52 * \param italic font slant value | |
53 * \param index out: font index inside a file | |
54 * \return font file path | |
55 */ | |
21351
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
56 static char* _select_font(fc_instance_t* priv, const char* family, unsigned bold, unsigned italic, int* index, |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
57 FcCharSet* charset) |
18937 | 58 { |
59 FcBool rc; | |
60 FcResult result; | |
61 FcPattern *pat, *rpat; | |
62 int val_i; | |
63 FcChar8* val_s; | |
19001 | 64 FcBool val_b; |
21351
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
65 FcCharSet* val_cs; |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
66 FcFontSet* fset; |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
67 int curf, bestf, bestdiff = 0; |
18937 | 68 |
69 *index = 0; | |
70 | |
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
|
71 pat = FcPatternCreate(); |
18937 | 72 if (!pat) |
73 return 0; | |
74 | |
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
|
75 FcPatternAddString(pat, FC_FAMILY, (const FcChar8*)family); |
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
|
76 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
|
77 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
|
78 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
|
79 |
18937 | 80 FcDefaultSubstitute(pat); |
81 | |
82 rc = FcConfigSubstitute(priv->config, pat, FcMatchPattern); | |
83 if (!rc) | |
84 return 0; | |
21351
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
85 |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
86 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
|
87 |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
88 bestf = -1; |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
89 if (charset) |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
90 bestdiff = FcCharSetCount(charset) + 1; |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
91 for (curf = 0; curf < fset->nfont; ++curf) { |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
92 rpat = fset->fonts[curf]; |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
93 |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
94 result = FcPatternGetBool(rpat, FC_OUTLINE, 0, &val_b); |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
95 if (result != FcResultMatch) |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
96 continue; |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
97 if (val_b != FcTrue) |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
98 continue; |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
99 |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
100 if (charset) { |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
101 int diff; |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
102 result = FcPatternGetCharSet(rpat, FC_CHARSET, 0, &val_cs); |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
103 if (result != FcResultMatch) |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
104 continue; |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
105 diff = FcCharSetSubtractCount(charset, val_cs); |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
106 if (diff < bestdiff) { |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
107 bestdiff = diff; |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
108 bestf = curf; |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
109 } |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
110 if (diff == 0) |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
111 break; |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
112 } else { |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
113 bestf = curf; |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
114 break; |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
115 } |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
116 } |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
117 |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
118 if (bestf < 0) |
18937 | 119 return 0; |
21351
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
120 |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
121 rpat = fset->fonts[bestf]; |
19001 | 122 |
18937 | 123 result = FcPatternGetInteger(rpat, FC_INDEX, 0, &val_i); |
124 if (result != FcResultMatch) | |
125 return 0; | |
126 *index = val_i; | |
127 | |
128 result = FcPatternGetString(rpat, FC_FAMILY, 0, &val_s); | |
129 if (result != FcResultMatch) | |
130 return 0; | |
131 | |
132 if (strcasecmp((const char*)val_s, family) != 0) | |
21066 | 133 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_SelectedFontFamilyIsNotTheRequestedOne, |
18937 | 134 (const char*)val_s, family); |
135 | |
136 result = FcPatternGetString(rpat, FC_FILE, 0, &val_s); | |
137 if (result != FcResultMatch) | |
138 return 0; | |
139 | |
140 return strdup((const char*)val_s); | |
141 } | |
142 | |
143 /** | |
144 * \brief Find a font. Use default family or path if necessary. | |
145 * \param priv_ private data | |
146 * \param family font family | |
147 * \param bold font weight value | |
148 * \param italic font slant value | |
149 * \param index out: font index inside a file | |
150 * \return font file path | |
151 */ | |
21351
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
152 char* fontconfig_select_with_charset(fc_instance_t* priv, const char* family, unsigned bold, unsigned italic, int* index, |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
153 FcCharSet* charset) |
18937 | 154 { |
155 char* res = 0; | |
156 if (family && *family) | |
21351
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
157 res = _select_font(priv, family, bold, italic, index, charset); |
18937 | 158 if (!res && priv->family_default) { |
21351
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
159 res = _select_font(priv, priv->family_default, bold, italic, index, charset); |
21279 | 160 if (res) |
21066 | 161 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_UsingDefaultFontFamily, |
18937 | 162 family, bold, italic, res, *index); |
163 } | |
164 if (!res && priv->path_default) { | |
165 res = priv->path_default; | |
166 *index = priv->index_default; | |
21279 | 167 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_UsingDefaultFont, |
168 family, bold, italic, res, *index); | |
18937 | 169 } |
170 if (!res) { | |
21351
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
171 res = _select_font(priv, "Arial", bold, italic, index, charset); |
21279 | 172 if (res) |
21066 | 173 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_UsingArialFontFamily, |
18937 | 174 family, bold, italic, res, *index); |
175 } | |
176 if (res) | |
20629
e8885ec63928
Introduce MSGT_ASS, use it for all libass messages.
eugeni
parents:
20105
diff
changeset
|
177 mp_msg(MSGT_ASS, MSGL_V, "fontconfig_select: (%s, %d, %d) -> %s, %d\n", |
18937 | 178 family, bold, italic, res, *index); |
179 return res; | |
180 } | |
181 | |
21351
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
182 char* fontconfig_select(fc_instance_t* priv, const char* family, unsigned bold, unsigned italic, int* index) |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
183 { |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
184 return fontconfig_select_with_charset(priv, family, bold, italic, index, 0); |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
185 } |
c611dfc4cb85
If a glyph is not found in the current font, switch to another one.
eugeni
parents:
21279
diff
changeset
|
186 |
18937 | 187 /** |
188 * \brief Init fontconfig. | |
189 * \param dir additional directoryu for fonts | |
190 * \param family default font family | |
191 * \param path default font path | |
192 * \return pointer to fontconfig private data | |
193 */ | |
194 fc_instance_t* fontconfig_init(const char* dir, const char* family, const char* path) | |
195 { | |
196 int rc; | |
197 struct stat st; | |
198 fc_instance_t* priv = calloc(1, sizeof(fc_instance_t)); | |
199 | |
200 rc = FcInit(); | |
201 assert(rc); | |
202 | |
203 priv->config = FcConfigGetCurrent(); | |
204 if (!priv->config) { | |
21066 | 205 mp_msg(MSGT_ASS, MSGL_FATAL, MSGTR_LIBASS_FcInitLoadConfigAndFontsFailed); |
18937 | 206 return 0; |
207 } | |
208 | |
19340 | 209 if (FcDirCacheValid((const FcChar8 *)dir) == FcFalse) |
210 { | |
21066 | 211 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
|
212 if (FcGetVersion() >= 20390 && FcGetVersion() < 20400) |
20629
e8885ec63928
Introduce MSGT_ASS, use it for all libass messages.
eugeni
parents:
20105
diff
changeset
|
213 mp_msg(MSGT_ASS, MSGL_WARN, |
21066 | 214 MSGTR_LIBASS_BetaVersionsOfFontconfigAreNotSupported); |
19901
27b87a9dc19a
Don't call FcDirScan/FcDirSave with FontConfig >= 2.4.
eugeni
parents:
19481
diff
changeset
|
215 // 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
|
216 if (FcGetVersion() < 20390) { |
19902 | 217 FcFontSet* fcs; |
218 FcStrSet* fss; | |
219 fcs = FcFontSetCreate(); | |
220 fss = FcStrSetCreate(); | |
221 rc = FcStrSetAdd(fss, (const FcChar8*)dir); | |
222 if (!rc) { | |
21066 | 223 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FcStrSetAddFailed); |
19902 | 224 goto ErrorFontCache; |
225 } | |
19340 | 226 |
19902 | 227 rc = FcDirScan(fcs, fss, NULL, FcConfigGetBlanks(priv->config), (const FcChar8 *)dir, FcFalse); |
228 if (!rc) { | |
21066 | 229 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FcDirScanFailed); |
19902 | 230 goto ErrorFontCache; |
231 } | |
19340 | 232 |
19902 | 233 rc = FcDirSave(fcs, fss, (const FcChar8 *)dir); |
234 if (!rc) { | |
21066 | 235 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FcDirSave); |
19902 | 236 goto ErrorFontCache; |
237 } | |
238 ErrorFontCache: | |
239 ; | |
19901
27b87a9dc19a
Don't call FcDirScan/FcDirSave with FontConfig >= 2.4.
eugeni
parents:
19481
diff
changeset
|
240 } |
19340 | 241 } |
242 | |
18937 | 243 rc = FcConfigAppFontAddDir(priv->config, (const FcChar8*)dir); |
244 if (!rc) { | |
21066 | 245 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FcConfigAppFontAddDirFailed); |
18937 | 246 } |
247 | |
248 priv->family_default = family ? strdup(family) : 0; | |
249 priv->index_default = 0; | |
250 | |
251 rc = stat(path, &st); | |
252 if (!rc && S_ISREG(st.st_mode)) | |
253 priv->path_default = path ? strdup(path) : 0; | |
254 else | |
255 priv->path_default = 0; | |
256 | |
257 return priv; | |
258 } | |
259 | |
260 #else | |
261 | |
262 char* fontconfig_select(fc_instance_t* priv, const char* family, unsigned bold, unsigned italic, int* index) | |
263 { | |
264 *index = priv->index_default; | |
265 return priv->path_default; | |
266 } | |
267 | |
268 fc_instance_t* fontconfig_init(const char* dir, const char* family, const char* path) | |
269 { | |
19481 | 270 fc_instance_t* priv; |
271 | |
21066 | 272 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FontconfigDisabledDefaultFontWillBeUsed); |
18937 | 273 |
19481 | 274 priv = calloc(1, sizeof(fc_instance_t)); |
18937 | 275 |
276 priv->path_default = strdup(path); | |
277 priv->index_default = 0; | |
278 return priv; | |
279 } | |
280 | |
281 #endif | |
282 | |
283 void fontconfig_done(fc_instance_t* priv) | |
284 { | |
285 // don't call FcFini() here, library can still be used by some code | |
286 if (priv && priv->path_default) free(priv->path_default); | |
287 if (priv && priv->family_default) free(priv->family_default); | |
288 if (priv) free(priv); | |
289 } | |
290 | |
291 |