Mercurial > mplayer.hg
annotate libass/ass_fontconfig.c @ 19939:1eb8f5470cba
Whitespace-only cosmetics
author | reimar |
---|---|
date | Fri, 22 Sep 2006 20:26:12 +0000 |
parents | 2c43912bb46a |
children | fa122b7c71c6 |
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 { | |
19901
27b87a9dc19a
Don't call FcDirScan/FcDirSave with FontConfig >= 2.4.
eugeni
parents:
19481
diff
changeset
|
157 mp_msg(MSGT_GLOBAL, MSGL_INFO, "[ass] Updating font cache\n"); |
27b87a9dc19a
Don't call FcDirScan/FcDirSave with FontConfig >= 2.4.
eugeni
parents:
19481
diff
changeset
|
158 // FontConfig >= 2.4.0 updates cache automatically in FcConfigAppFontAddDir() |
27b87a9dc19a
Don't call FcDirScan/FcDirSave with FontConfig >= 2.4.
eugeni
parents:
19481
diff
changeset
|
159 if (FcGetVersion() < 20400) { |
19902 | 160 FcFontSet* fcs; |
161 FcStrSet* fss; | |
162 fcs = FcFontSetCreate(); | |
163 fss = FcStrSetCreate(); | |
164 rc = FcStrSetAdd(fss, (const FcChar8*)dir); | |
165 if (!rc) { | |
166 mp_msg(MSGT_GLOBAL, MSGL_WARN, "FcStrSetAdd failed\n"); | |
167 goto ErrorFontCache; | |
168 } | |
19340 | 169 |
19902 | 170 rc = FcDirScan(fcs, fss, NULL, FcConfigGetBlanks(priv->config), (const FcChar8 *)dir, FcFalse); |
171 if (!rc) { | |
172 mp_msg(MSGT_GLOBAL, MSGL_WARN, "FcDirScan failed\n"); | |
173 goto ErrorFontCache; | |
174 } | |
19340 | 175 |
19902 | 176 rc = FcDirSave(fcs, fss, (const FcChar8 *)dir); |
177 if (!rc) { | |
178 mp_msg(MSGT_GLOBAL, MSGL_WARN, "FcDirSave failed\n"); | |
179 goto ErrorFontCache; | |
180 } | |
181 ErrorFontCache: | |
182 ; | |
19901
27b87a9dc19a
Don't call FcDirScan/FcDirSave with FontConfig >= 2.4.
eugeni
parents:
19481
diff
changeset
|
183 } |
19340 | 184 } |
185 | |
18937 | 186 rc = FcConfigAppFontAddDir(priv->config, (const FcChar8*)dir); |
187 if (!rc) { | |
188 mp_msg(MSGT_GLOBAL, MSGL_WARN, "FcConfigAppFontAddDir failed\n"); | |
189 } | |
190 | |
191 priv->family_default = family ? strdup(family) : 0; | |
192 priv->index_default = 0; | |
193 | |
194 rc = stat(path, &st); | |
195 if (!rc && S_ISREG(st.st_mode)) | |
196 priv->path_default = path ? strdup(path) : 0; | |
197 else | |
198 priv->path_default = 0; | |
199 | |
200 return priv; | |
201 } | |
202 | |
203 #else | |
204 | |
205 char* fontconfig_select(fc_instance_t* priv, const char* family, unsigned bold, unsigned italic, int* index) | |
206 { | |
207 *index = priv->index_default; | |
208 return priv->path_default; | |
209 } | |
210 | |
211 fc_instance_t* fontconfig_init(const char* dir, const char* family, const char* path) | |
212 { | |
19481 | 213 fc_instance_t* priv; |
214 | |
18937 | 215 mp_msg(MSGT_GLOBAL, MSGL_WARN, "Fontconfig disabled, only default font will be used\n"); |
216 | |
19481 | 217 priv = calloc(1, sizeof(fc_instance_t)); |
18937 | 218 |
219 priv->path_default = strdup(path); | |
220 priv->index_default = 0; | |
221 return priv; | |
222 } | |
223 | |
224 #endif | |
225 | |
226 void fontconfig_done(fc_instance_t* priv) | |
227 { | |
228 // don't call FcFini() here, library can still be used by some code | |
229 if (priv && priv->path_default) free(priv->path_default); | |
230 if (priv && priv->family_default) free(priv->family_default); | |
231 if (priv) free(priv); | |
232 } | |
233 | |
234 |