Mercurial > mplayer.hg
annotate libass/ass_fontconfig.c @ 19696:0b07e95ca082
FFmpeg sync: Fall back on grep if svn client is not installed.
author | diego |
---|---|
date | Tue, 05 Sep 2006 23:19:22 +0000 |
parents | 5890c54b755c |
children | 27b87a9dc19a |
rev | line source |
---|---|
18937 | 1 #include "config.h" |
2 | |
3 #include <stdlib.h> | |
4 #include <stdio.h> | |
5 #include <assert.h> | |
6 #include <string.h> | |
7 #include <sys/types.h> | |
8 #include <sys/stat.h> | |
9 | |
10 #include "mp_msg.h" | |
11 #include "ass_fontconfig.h" | |
12 | |
13 #ifdef HAVE_FONTCONFIG | |
14 #include <fontconfig/fontconfig.h> | |
15 #endif | |
16 | |
17 struct fc_instance_s { | |
18 #ifdef HAVE_FONTCONFIG | |
19 FcConfig* config; | |
20 #endif | |
21 char* family_default; | |
22 char* path_default; | |
23 int index_default; | |
24 }; | |
25 | |
26 extern int no_more_font_messages; | |
27 | |
28 #ifdef HAVE_FONTCONFIG | |
29 /** | |
30 * \brief Low-level font selection. | |
31 * \param priv private data | |
32 * \param family font family | |
33 * \param bold font weight value | |
34 * \param italic font slant value | |
35 * \param index out: font index inside a file | |
36 * \return font file path | |
37 */ | |
38 static char* _select_font(fc_instance_t* priv, const char* family, unsigned bold, unsigned italic, int* index) | |
39 { | |
40 FcBool rc; | |
41 FcResult result; | |
42 FcPattern *pat, *rpat; | |
43 int val_i; | |
44 FcChar8* val_s; | |
19001 | 45 FcBool val_b; |
18937 | 46 |
47 *index = 0; | |
48 | |
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
|
49 pat = FcPatternCreate(); |
18937 | 50 if (!pat) |
51 return 0; | |
52 | |
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
|
53 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
|
54 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
|
55 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
|
56 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
|
57 |
18937 | 58 FcDefaultSubstitute(pat); |
59 | |
60 rc = FcConfigSubstitute(priv->config, pat, FcMatchPattern); | |
61 if (!rc) | |
62 return 0; | |
63 | |
64 rpat = FcFontMatch(priv->config, pat, &result); | |
65 if (!rpat) | |
66 return 0; | |
67 | |
19001 | 68 result = FcPatternGetBool(rpat, FC_OUTLINE, 0, &val_b); |
69 if (result != FcResultMatch) | |
70 return 0; | |
19064 | 71 if (val_b != FcTrue) |
19001 | 72 return 0; |
73 | |
18937 | 74 result = FcPatternGetInteger(rpat, FC_INDEX, 0, &val_i); |
75 if (result != FcResultMatch) | |
76 return 0; | |
77 *index = val_i; | |
78 | |
79 result = FcPatternGetString(rpat, FC_FAMILY, 0, &val_s); | |
80 if (result != FcResultMatch) | |
81 return 0; | |
82 | |
83 if (strcasecmp((const char*)val_s, family) != 0) | |
84 mp_msg(MSGT_GLOBAL, MSGL_WARN, "fontconfig: selected font family is not the requested one: '%s' != '%s'\n", | |
85 (const char*)val_s, family); | |
86 | |
87 result = FcPatternGetString(rpat, FC_FILE, 0, &val_s); | |
88 if (result != FcResultMatch) | |
89 return 0; | |
90 | |
91 return strdup((const char*)val_s); | |
92 } | |
93 | |
94 /** | |
95 * \brief Find a font. Use default family or path if necessary. | |
96 * \param priv_ private data | |
97 * \param family font family | |
98 * \param bold font weight value | |
99 * \param italic font slant value | |
100 * \param index out: font index inside a file | |
101 * \return font file path | |
102 */ | |
103 char* fontconfig_select(fc_instance_t* priv, const char* family, unsigned bold, unsigned italic, int* index) | |
104 { | |
105 char* res = 0; | |
106 if (family && *family) | |
107 res = _select_font(priv, family, bold, italic, index); | |
108 if (!res && priv->family_default) { | |
109 res = _select_font(priv, priv->family_default, bold, italic, index); | |
110 if (res && !no_more_font_messages) | |
111 mp_msg(MSGT_GLOBAL, MSGL_WARN, "fontconfig_select: using default font family: (%s, %d, %d) -> %s, %d\n", | |
112 family, bold, italic, res, *index); | |
113 } | |
114 if (!res && priv->path_default) { | |
115 res = priv->path_default; | |
116 *index = priv->index_default; | |
117 if (!no_more_font_messages) | |
118 mp_msg(MSGT_GLOBAL, MSGL_WARN, "fontconfig_select: using default font: (%s, %d, %d) -> %s, %d\n", | |
119 family, bold, italic, res, *index); | |
120 } | |
121 if (!res) { | |
122 res = _select_font(priv, "Arial", bold, italic, index); | |
123 if (res && !no_more_font_messages) | |
124 mp_msg(MSGT_GLOBAL, MSGL_WARN, "fontconfig_select: using 'Arial' font family: (%s, %d, %d) -> %s, %d\n", | |
125 family, bold, italic, res, *index); | |
126 } | |
127 if (res) | |
128 mp_msg(MSGT_GLOBAL, MSGL_V, "fontconfig_select: (%s, %d, %d) -> %s, %d\n", | |
129 family, bold, italic, res, *index); | |
130 return res; | |
131 } | |
132 | |
133 /** | |
134 * \brief Init fontconfig. | |
135 * \param dir additional directoryu for fonts | |
136 * \param family default font family | |
137 * \param path default font path | |
138 * \return pointer to fontconfig private data | |
139 */ | |
140 fc_instance_t* fontconfig_init(const char* dir, const char* family, const char* path) | |
141 { | |
142 int rc; | |
143 struct stat st; | |
144 fc_instance_t* priv = calloc(1, sizeof(fc_instance_t)); | |
145 | |
146 rc = FcInit(); | |
147 assert(rc); | |
148 | |
149 priv->config = FcConfigGetCurrent(); | |
150 if (!priv->config) { | |
151 mp_msg(MSGT_GLOBAL, MSGL_FATAL, "FcInitLoadConfigAndFonts failed\n"); | |
152 return 0; | |
153 } | |
154 | |
19340 | 155 if (FcDirCacheValid((const FcChar8 *)dir) == FcFalse) |
156 { | |
157 FcFontSet* fcs; | |
158 FcStrSet* fss; | |
159 mp_msg(MSGT_GLOBAL, MSGL_INFO, "[ass] Updating font cache\n"); | |
160 fcs = FcFontSetCreate(); | |
161 fss = FcStrSetCreate(); | |
162 rc = FcStrSetAdd(fss, (const FcChar8*)dir); | |
163 if (!rc) { | |
164 mp_msg(MSGT_GLOBAL, MSGL_WARN, "FcStrSetAdd failed\n"); | |
165 goto ErrorFontCache; | |
166 } | |
167 | |
168 rc = FcDirScan(fcs, fss, NULL, FcConfigGetBlanks(priv->config), (const FcChar8 *)dir, FcFalse); | |
169 if (!rc) { | |
170 mp_msg(MSGT_GLOBAL, MSGL_WARN, "FcDirScan failed\n"); | |
171 goto ErrorFontCache; | |
172 } | |
173 | |
174 rc = FcDirSave(fcs, fss, (const FcChar8 *)dir); | |
175 if (!rc) { | |
176 mp_msg(MSGT_GLOBAL, MSGL_WARN, "FcDirSave failed\n"); | |
177 goto ErrorFontCache; | |
178 } | |
179 ErrorFontCache: | |
180 ; | |
181 } | |
182 | |
18937 | 183 rc = FcConfigAppFontAddDir(priv->config, (const FcChar8*)dir); |
184 if (!rc) { | |
185 mp_msg(MSGT_GLOBAL, MSGL_WARN, "FcConfigAppFontAddDir failed\n"); | |
186 } | |
187 | |
188 priv->family_default = family ? strdup(family) : 0; | |
189 priv->index_default = 0; | |
190 | |
191 rc = stat(path, &st); | |
192 if (!rc && S_ISREG(st.st_mode)) | |
193 priv->path_default = path ? strdup(path) : 0; | |
194 else | |
195 priv->path_default = 0; | |
196 | |
197 return priv; | |
198 } | |
199 | |
200 #else | |
201 | |
202 char* fontconfig_select(fc_instance_t* priv, const char* family, unsigned bold, unsigned italic, int* index) | |
203 { | |
204 *index = priv->index_default; | |
205 return priv->path_default; | |
206 } | |
207 | |
208 fc_instance_t* fontconfig_init(const char* dir, const char* family, const char* path) | |
209 { | |
19481 | 210 fc_instance_t* priv; |
211 | |
18937 | 212 mp_msg(MSGT_GLOBAL, MSGL_WARN, "Fontconfig disabled, only default font will be used\n"); |
213 | |
19481 | 214 priv = calloc(1, sizeof(fc_instance_t)); |
18937 | 215 |
216 priv->path_default = strdup(path); | |
217 priv->index_default = 0; | |
218 return priv; | |
219 } | |
220 | |
221 #endif | |
222 | |
223 void fontconfig_done(fc_instance_t* priv) | |
224 { | |
225 // don't call FcFini() here, library can still be used by some code | |
226 if (priv && priv->path_default) free(priv->path_default); | |
227 if (priv && priv->family_default) free(priv->family_default); | |
228 if (priv) free(priv); | |
229 } | |
230 | |
231 |