Mercurial > mplayer.hg
annotate libass/ass_fontconfig.c @ 21323:578fc5ca6a4f
Don't include freetype headers in other headers.
author | eugeni |
---|---|
date | Mon, 27 Nov 2006 17:56:49 +0000 |
parents | 9040bce9f768 |
children | c611dfc4cb85 |
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 */ | |
56 static char* _select_font(fc_instance_t* priv, const char* family, unsigned bold, unsigned italic, int* index) | |
57 { | |
58 FcBool rc; | |
59 FcResult result; | |
60 FcPattern *pat, *rpat; | |
61 int val_i; | |
62 FcChar8* val_s; | |
19001 | 63 FcBool val_b; |
18937 | 64 |
65 *index = 0; | |
66 | |
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
|
67 pat = FcPatternCreate(); |
18937 | 68 if (!pat) |
69 return 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 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
|
72 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
|
73 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
|
74 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
|
75 |
18937 | 76 FcDefaultSubstitute(pat); |
77 | |
78 rc = FcConfigSubstitute(priv->config, pat, FcMatchPattern); | |
79 if (!rc) | |
80 return 0; | |
81 | |
82 rpat = FcFontMatch(priv->config, pat, &result); | |
83 if (!rpat) | |
84 return 0; | |
85 | |
19001 | 86 result = FcPatternGetBool(rpat, FC_OUTLINE, 0, &val_b); |
87 if (result != FcResultMatch) | |
88 return 0; | |
19064 | 89 if (val_b != FcTrue) |
19001 | 90 return 0; |
91 | |
18937 | 92 result = FcPatternGetInteger(rpat, FC_INDEX, 0, &val_i); |
93 if (result != FcResultMatch) | |
94 return 0; | |
95 *index = val_i; | |
96 | |
97 result = FcPatternGetString(rpat, FC_FAMILY, 0, &val_s); | |
98 if (result != FcResultMatch) | |
99 return 0; | |
100 | |
101 if (strcasecmp((const char*)val_s, family) != 0) | |
21066 | 102 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_SelectedFontFamilyIsNotTheRequestedOne, |
18937 | 103 (const char*)val_s, family); |
104 | |
105 result = FcPatternGetString(rpat, FC_FILE, 0, &val_s); | |
106 if (result != FcResultMatch) | |
107 return 0; | |
108 | |
109 return strdup((const char*)val_s); | |
110 } | |
111 | |
112 /** | |
113 * \brief Find a font. Use default family or path if necessary. | |
114 * \param priv_ private data | |
115 * \param family font family | |
116 * \param bold font weight value | |
117 * \param italic font slant value | |
118 * \param index out: font index inside a file | |
119 * \return font file path | |
120 */ | |
121 char* fontconfig_select(fc_instance_t* priv, const char* family, unsigned bold, unsigned italic, int* index) | |
122 { | |
123 char* res = 0; | |
124 if (family && *family) | |
125 res = _select_font(priv, family, bold, italic, index); | |
126 if (!res && priv->family_default) { | |
127 res = _select_font(priv, priv->family_default, bold, italic, index); | |
21279 | 128 if (res) |
21066 | 129 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_UsingDefaultFontFamily, |
18937 | 130 family, bold, italic, res, *index); |
131 } | |
132 if (!res && priv->path_default) { | |
133 res = priv->path_default; | |
134 *index = priv->index_default; | |
21279 | 135 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_UsingDefaultFont, |
136 family, bold, italic, res, *index); | |
18937 | 137 } |
138 if (!res) { | |
139 res = _select_font(priv, "Arial", bold, italic, index); | |
21279 | 140 if (res) |
21066 | 141 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_UsingArialFontFamily, |
18937 | 142 family, bold, italic, res, *index); |
143 } | |
144 if (res) | |
20629
e8885ec63928
Introduce MSGT_ASS, use it for all libass messages.
eugeni
parents:
20105
diff
changeset
|
145 mp_msg(MSGT_ASS, MSGL_V, "fontconfig_select: (%s, %d, %d) -> %s, %d\n", |
18937 | 146 family, bold, italic, res, *index); |
147 return res; | |
148 } | |
149 | |
150 /** | |
151 * \brief Init fontconfig. | |
152 * \param dir additional directoryu for fonts | |
153 * \param family default font family | |
154 * \param path default font path | |
155 * \return pointer to fontconfig private data | |
156 */ | |
157 fc_instance_t* fontconfig_init(const char* dir, const char* family, const char* path) | |
158 { | |
159 int rc; | |
160 struct stat st; | |
161 fc_instance_t* priv = calloc(1, sizeof(fc_instance_t)); | |
162 | |
163 rc = FcInit(); | |
164 assert(rc); | |
165 | |
166 priv->config = FcConfigGetCurrent(); | |
167 if (!priv->config) { | |
21066 | 168 mp_msg(MSGT_ASS, MSGL_FATAL, MSGTR_LIBASS_FcInitLoadConfigAndFontsFailed); |
18937 | 169 return 0; |
170 } | |
171 | |
19340 | 172 if (FcDirCacheValid((const FcChar8 *)dir) == FcFalse) |
173 { | |
21066 | 174 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
|
175 if (FcGetVersion() >= 20390 && FcGetVersion() < 20400) |
20629
e8885ec63928
Introduce MSGT_ASS, use it for all libass messages.
eugeni
parents:
20105
diff
changeset
|
176 mp_msg(MSGT_ASS, MSGL_WARN, |
21066 | 177 MSGTR_LIBASS_BetaVersionsOfFontconfigAreNotSupported); |
19901
27b87a9dc19a
Don't call FcDirScan/FcDirSave with FontConfig >= 2.4.
eugeni
parents:
19481
diff
changeset
|
178 // 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
|
179 if (FcGetVersion() < 20390) { |
19902 | 180 FcFontSet* fcs; |
181 FcStrSet* fss; | |
182 fcs = FcFontSetCreate(); | |
183 fss = FcStrSetCreate(); | |
184 rc = FcStrSetAdd(fss, (const FcChar8*)dir); | |
185 if (!rc) { | |
21066 | 186 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FcStrSetAddFailed); |
19902 | 187 goto ErrorFontCache; |
188 } | |
19340 | 189 |
19902 | 190 rc = FcDirScan(fcs, fss, NULL, FcConfigGetBlanks(priv->config), (const FcChar8 *)dir, FcFalse); |
191 if (!rc) { | |
21066 | 192 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FcDirScanFailed); |
19902 | 193 goto ErrorFontCache; |
194 } | |
19340 | 195 |
19902 | 196 rc = FcDirSave(fcs, fss, (const FcChar8 *)dir); |
197 if (!rc) { | |
21066 | 198 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FcDirSave); |
19902 | 199 goto ErrorFontCache; |
200 } | |
201 ErrorFontCache: | |
202 ; | |
19901
27b87a9dc19a
Don't call FcDirScan/FcDirSave with FontConfig >= 2.4.
eugeni
parents:
19481
diff
changeset
|
203 } |
19340 | 204 } |
205 | |
18937 | 206 rc = FcConfigAppFontAddDir(priv->config, (const FcChar8*)dir); |
207 if (!rc) { | |
21066 | 208 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FcConfigAppFontAddDirFailed); |
18937 | 209 } |
210 | |
211 priv->family_default = family ? strdup(family) : 0; | |
212 priv->index_default = 0; | |
213 | |
214 rc = stat(path, &st); | |
215 if (!rc && S_ISREG(st.st_mode)) | |
216 priv->path_default = path ? strdup(path) : 0; | |
217 else | |
218 priv->path_default = 0; | |
219 | |
220 return priv; | |
221 } | |
222 | |
223 #else | |
224 | |
225 char* fontconfig_select(fc_instance_t* priv, const char* family, unsigned bold, unsigned italic, int* index) | |
226 { | |
227 *index = priv->index_default; | |
228 return priv->path_default; | |
229 } | |
230 | |
231 fc_instance_t* fontconfig_init(const char* dir, const char* family, const char* path) | |
232 { | |
19481 | 233 fc_instance_t* priv; |
234 | |
21066 | 235 mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FontconfigDisabledDefaultFontWillBeUsed); |
18937 | 236 |
19481 | 237 priv = calloc(1, sizeof(fc_instance_t)); |
18937 | 238 |
239 priv->path_default = strdup(path); | |
240 priv->index_default = 0; | |
241 return priv; | |
242 } | |
243 | |
244 #endif | |
245 | |
246 void fontconfig_done(fc_instance_t* priv) | |
247 { | |
248 // don't call FcFini() here, library can still be used by some code | |
249 if (priv && priv->path_default) free(priv->path_default); | |
250 if (priv && priv->family_default) free(priv->family_default); | |
251 if (priv) free(priv); | |
252 } | |
253 | |
254 |