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