/work/workdir/UnpackedTarball/fontconfig/src/fcdefault.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * fontconfig/src/fcdefault.c |
3 | | * |
4 | | * Copyright © 2001 Keith Packard |
5 | | * |
6 | | * Permission to use, copy, modify, distribute, and sell this software and its |
7 | | * documentation for any purpose is hereby granted without fee, provided that |
8 | | * the above copyright notice appear in all copies and that both that |
9 | | * copyright notice and this permission notice appear in supporting |
10 | | * documentation, and that the name of the author(s) not be used in |
11 | | * advertising or publicity pertaining to distribution of the software without |
12 | | * specific, written prior permission. The authors make no |
13 | | * representations about the suitability of this software for any purpose. It |
14 | | * is provided "as is" without express or implied warranty. |
15 | | * |
16 | | * THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, |
17 | | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO |
18 | | * EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY SPECIAL, INDIRECT OR |
19 | | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, |
20 | | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
21 | | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
22 | | * PERFORMANCE OF THIS SOFTWARE. |
23 | | */ |
24 | | |
25 | | #include "fcint.h" |
26 | | |
27 | | #include <limits.h> |
28 | | #include <string.h> |
29 | | |
30 | | /* MT-safe */ |
31 | | |
32 | | static const struct { |
33 | | FcObject field; |
34 | | FcBool value; |
35 | | } FcBoolDefaults[] = { |
36 | | { FC_HINTING_OBJECT, FcTrue }, /* !FT_LOAD_NO_HINTING */ |
37 | | { FC_VERTICAL_LAYOUT_OBJECT, FcFalse }, /* FC_LOAD_VERTICAL_LAYOUT */ |
38 | | { FC_AUTOHINT_OBJECT, FcFalse }, /* FC_LOAD_FORCE_AUTOHINT */ |
39 | | { FC_GLOBAL_ADVANCE_OBJECT, FcTrue }, /* !FC_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH */ |
40 | | { FC_EMBEDDED_BITMAP_OBJECT, FcTrue }, /* !FC_LOAD_NO_BITMAP */ |
41 | | { FC_DECORATIVE_OBJECT, FcFalse }, |
42 | | { FC_SYMBOL_OBJECT, FcFalse }, |
43 | | { FC_VARIABLE_OBJECT, FcFalse }, |
44 | | }; |
45 | | |
46 | 2.16k | #define NUM_FC_BOOL_DEFAULTS (int)(sizeof FcBoolDefaults / sizeof FcBoolDefaults[0]) |
47 | | |
48 | | FcStrSet *default_langs; |
49 | | |
50 | | FcStrSet * |
51 | | FcConfigGetDefaultLangs (FcConfig *config) |
52 | 243 | { |
53 | 243 | FcStrSet *result; |
54 | | |
55 | 243 | if (!config) { |
56 | 0 | config = FcConfigGetCurrent(); |
57 | 0 | } |
58 | 243 | FcConfigReference (config); |
59 | 243 | retry: |
60 | 243 | result = (FcStrSet *)fc_atomic_ptr_get (&config->default_langs); |
61 | 243 | if (!result) { |
62 | 2 | char *langs; |
63 | | |
64 | 2 | result = FcStrSetCreate(); |
65 | | |
66 | 2 | langs = getenv ("FC_LANG"); |
67 | 2 | if (!langs || !langs[0]) |
68 | 2 | langs = getenv ("LC_ALL"); |
69 | 2 | if (!langs || !langs[0]) { |
70 | 2 | langs = getenv ("LC_CTYPE"); |
71 | | // On some macOS systems, LC_CTYPE is set to "UTF-8", which doesn't |
72 | | // give any languge information. In this case, ignore LC_CTYPE and |
73 | | // continue the search with LANG. |
74 | 2 | if (langs && (FcStrCmpIgnoreCase ((const FcChar8 *)langs, |
75 | 0 | (const FcChar8 *)"UTF-8") == 0)) { |
76 | 0 | langs = NULL; |
77 | 0 | } |
78 | 2 | } |
79 | 2 | if (!langs || !langs[0]) |
80 | 2 | langs = getenv ("LANG"); |
81 | 2 | if (langs && langs[0]) { |
82 | 0 | if (!FcStrSetAddLangs (result, langs)) |
83 | 0 | FcStrSetAdd (result, (const FcChar8 *)"en"); |
84 | 0 | } else |
85 | 2 | FcStrSetAdd (result, (const FcChar8 *)"en"); |
86 | | |
87 | 2 | FcRefSetConst (&result->ref); |
88 | 2 | if (!fc_atomic_ptr_cmpexch (&config->default_langs, NULL, result)) { |
89 | 0 | FcRefInit (&result->ref, 1); |
90 | 0 | FcStrSetDestroy (result); |
91 | 0 | goto retry; |
92 | 0 | } |
93 | 2 | } |
94 | 243 | FcConfigDestroy (config); |
95 | | |
96 | 243 | return result; |
97 | 243 | } |
98 | | |
99 | | FcStrSet * |
100 | | FcGetDefaultLangs (void) |
101 | 0 | { |
102 | 0 | return FcConfigGetDefaultLangs (NULL); |
103 | 0 | } |
104 | | |
105 | | FcChar8 * |
106 | | FcConfigGetDefaultLang (FcConfig *config) |
107 | 241 | { |
108 | 241 | FcChar8 *lang; |
109 | | |
110 | 241 | if (!config) { |
111 | 0 | config = FcConfigGetCurrent(); |
112 | 0 | } |
113 | 241 | FcConfigReference (config); |
114 | 241 | retry: |
115 | 241 | lang = fc_atomic_ptr_get (&config->default_lang); |
116 | 241 | if (!lang) { |
117 | 2 | FcStrSet *langs = FcConfigGetDefaultLangs (config); |
118 | 2 | lang = FcStrdup (langs->strs[0]); |
119 | | |
120 | 2 | if (!fc_atomic_ptr_cmpexch (&config->default_lang, NULL, lang)) { |
121 | 0 | free (lang); |
122 | 0 | goto retry; |
123 | 0 | } |
124 | 2 | } |
125 | 241 | FcConfigDestroy (config); |
126 | | |
127 | 241 | return lang; |
128 | 241 | } |
129 | | |
130 | | FcChar8 * |
131 | | FcGetDefaultLang (void) |
132 | 0 | { |
133 | 0 | return FcConfigGetDefaultLang (NULL); |
134 | 0 | } |
135 | | |
136 | | FcChar8 * |
137 | | FcConfigGetPrgname (FcConfig *config) |
138 | 241 | { |
139 | 241 | FcChar8 *prgname; |
140 | | |
141 | 241 | if (!config) { |
142 | 0 | config = FcConfigGetCurrent(); |
143 | 0 | } |
144 | 241 | FcConfigReference (config); |
145 | 241 | retry: |
146 | 241 | prgname = fc_atomic_ptr_get (&config->prgname); |
147 | 241 | if (!prgname) { |
148 | | #ifdef _WIN32 |
149 | | char buf[MAX_PATH + 1]; |
150 | | |
151 | | /* TODO This is ASCII-only; fix it. */ |
152 | | if (GetModuleFileNameA (GetModuleHandle (NULL), buf, sizeof (buf) / sizeof (buf[0])) > 0) { |
153 | | char *p; |
154 | | unsigned int len; |
155 | | |
156 | | p = strrchr (buf, '\\'); |
157 | | if (p) |
158 | | p++; |
159 | | else |
160 | | p = buf; |
161 | | |
162 | | len = strlen (p); |
163 | | |
164 | | if (len > 4 && 0 == strcmp (p + len - 4, ".exe")) { |
165 | | len -= 4; |
166 | | buf[len] = '\0'; |
167 | | } |
168 | | |
169 | | prgname = FcStrdup (p); |
170 | | } |
171 | | #elif defined(HAVE_GETPROGNAME) |
172 | | const char *q = getprogname(); |
173 | | if (q) |
174 | | prgname = FcStrdup (q); |
175 | | else |
176 | | prgname = FcStrdup (""); |
177 | | #else |
178 | | # if defined(HAVE_GETEXECNAME) |
179 | | char *p = FcStrdup (getexecname()); |
180 | | # elif defined(HAVE_READLINK) |
181 | 2 | size_t size = FC_PATH_MAX; |
182 | 2 | char *p = NULL; |
183 | | |
184 | 2 | while (1) { |
185 | 2 | char *buf = malloc (size); |
186 | 2 | ssize_t len; |
187 | | |
188 | 2 | if (!buf) |
189 | 0 | break; |
190 | | |
191 | 2 | len = readlink ("/proc/self/exe", buf, size - 1); |
192 | 2 | if (len < 0) { |
193 | 0 | free (buf); |
194 | 0 | break; |
195 | 0 | } |
196 | 2 | if (len < size - 1) { |
197 | 2 | buf[len] = 0; |
198 | 2 | p = buf; |
199 | 2 | break; |
200 | 2 | } |
201 | | |
202 | 0 | free (buf); |
203 | 0 | size *= 2; |
204 | 0 | } |
205 | | # else |
206 | | char *p = NULL; |
207 | | # endif |
208 | 2 | if (p) { |
209 | 2 | char *r = strrchr (p, '/'); |
210 | 2 | if (r) |
211 | 2 | r++; |
212 | 0 | else |
213 | 0 | r = p; |
214 | | |
215 | 2 | prgname = FcStrdup (r); |
216 | 2 | } |
217 | | |
218 | 2 | if (!prgname) |
219 | 0 | prgname = FcStrdup (""); |
220 | | |
221 | 2 | if (p) |
222 | 2 | free (p); |
223 | 2 | #endif |
224 | | |
225 | 2 | if (!fc_atomic_ptr_cmpexch (&config->prgname, NULL, prgname)) { |
226 | 0 | free (prgname); |
227 | 0 | goto retry; |
228 | 0 | } |
229 | 2 | } |
230 | | |
231 | 241 | if (prgname && !prgname[0]) { |
232 | 0 | prgname = NULL; |
233 | 0 | } |
234 | 241 | FcConfigDestroy (config); |
235 | | |
236 | 241 | return prgname; |
237 | 241 | } |
238 | | |
239 | | FcChar8 * |
240 | | FcGetPrgname (void) |
241 | 0 | { |
242 | 0 | return FcConfigGetPrgname (NULL); |
243 | 0 | } |
244 | | |
245 | | FcChar8 * |
246 | | FcConfigGetDesktopName (FcConfig *config) |
247 | 241 | { |
248 | 241 | FcChar8 *desktop_name; |
249 | | |
250 | 241 | if (!config) { |
251 | 0 | config = FcConfigGetCurrent(); |
252 | 0 | } |
253 | 241 | FcConfigReference (config); |
254 | 241 | retry: |
255 | 241 | desktop_name = fc_atomic_ptr_get (&config->desktop_name); |
256 | 241 | if (!desktop_name) { |
257 | 2 | char *s = getenv ("XDG_CURRENT_DESKTOP"); |
258 | | |
259 | 2 | if (!s) |
260 | 2 | desktop_name = FcStrdup (""); |
261 | 0 | else |
262 | 0 | desktop_name = FcStrdup (s); |
263 | 2 | if (!desktop_name) { |
264 | 0 | fprintf (stderr, "Fontconfig error: out of memory in %s\n", |
265 | 0 | __FUNCTION__); |
266 | 0 | return NULL; |
267 | 0 | } |
268 | | |
269 | 2 | if (!fc_atomic_ptr_cmpexch (&config->desktop_name, NULL, desktop_name)) { |
270 | 0 | free (desktop_name); |
271 | 0 | goto retry; |
272 | 0 | } |
273 | 2 | } |
274 | 241 | if (desktop_name && !desktop_name[0]) { |
275 | 241 | desktop_name = NULL; |
276 | 241 | } |
277 | 241 | FcConfigDestroy (config); |
278 | | |
279 | 241 | return desktop_name; |
280 | 241 | } |
281 | | |
282 | | FcChar8 * |
283 | | FcGetDesktopName (void) |
284 | 0 | { |
285 | 0 | return FcConfigGetDesktopName (NULL); |
286 | 0 | } |
287 | | |
288 | | void |
289 | | FcConfigSetDefaultSubstitute (FcConfig *config, |
290 | | FcPattern *pattern) |
291 | 241 | { |
292 | 241 | FcPatternIter iter; |
293 | 241 | FcValue v, namelang, v2; |
294 | 241 | int i; |
295 | 241 | double dpi, size, scale, pixelsize; |
296 | | |
297 | 241 | config = FcConfigReference (config); |
298 | | |
299 | 241 | if (!FcPatternFindObjectIter (pattern, &iter, FC_WEIGHT_OBJECT)) |
300 | 0 | FcPatternObjectAddInteger (pattern, FC_WEIGHT_OBJECT, FC_WEIGHT_NORMAL); |
301 | | |
302 | 241 | if (!FcPatternFindObjectIter (pattern, &iter, FC_SLANT_OBJECT)) |
303 | 0 | FcPatternObjectAddInteger (pattern, FC_SLANT_OBJECT, FC_SLANT_ROMAN); |
304 | | |
305 | 241 | if (!FcPatternFindObjectIter (pattern, &iter, FC_WIDTH_OBJECT)) |
306 | 0 | FcPatternObjectAddInteger (pattern, FC_WIDTH_OBJECT, FC_WIDTH_NORMAL); |
307 | | |
308 | 2.16k | for (i = 0; i < NUM_FC_BOOL_DEFAULTS; i++) |
309 | 1.92k | if (!FcPatternFindObjectIter (pattern, &iter, FcBoolDefaults[i].field)) |
310 | 1.92k | FcPatternObjectAddBool (pattern, FcBoolDefaults[i].field, FcBoolDefaults[i].value); |
311 | | |
312 | 241 | if (FcPatternObjectGetDouble (pattern, FC_SIZE_OBJECT, 0, &size) != FcResultMatch) { |
313 | 241 | FcRange *r; |
314 | 241 | double b, e; |
315 | 241 | if (FcPatternObjectGetRange (pattern, FC_SIZE_OBJECT, 0, &r) == FcResultMatch && FcRangeGetDouble (r, &b, &e)) |
316 | 0 | size = (b + e) * .5; |
317 | 241 | else |
318 | 241 | size = 12.0L; |
319 | 241 | } |
320 | 241 | if (FcPatternObjectGetDouble (pattern, FC_SCALE_OBJECT, 0, &scale) != FcResultMatch) |
321 | 241 | scale = 1.0; |
322 | 241 | if (FcPatternObjectGetDouble (pattern, FC_DPI_OBJECT, 0, &dpi) != FcResultMatch) |
323 | 241 | dpi = 75.0; |
324 | | |
325 | 241 | if (!FcPatternFindObjectIter (pattern, &iter, FC_PIXEL_SIZE_OBJECT)) { |
326 | 0 | (void)FcPatternObjectDel (pattern, FC_SCALE_OBJECT); |
327 | 0 | FcPatternObjectAddDouble (pattern, FC_SCALE_OBJECT, scale); |
328 | 0 | pixelsize = size * scale; |
329 | 0 | (void)FcPatternObjectDel (pattern, FC_DPI_OBJECT); |
330 | 0 | FcPatternObjectAddDouble (pattern, FC_DPI_OBJECT, dpi); |
331 | 0 | pixelsize *= dpi / 72.0; |
332 | 0 | FcPatternObjectAddDouble (pattern, FC_PIXEL_SIZE_OBJECT, pixelsize); |
333 | 241 | } else { |
334 | 241 | FcPatternIterGetValue (pattern, &iter, 0, &v, NULL); |
335 | 241 | if (v.type == FcTypeInteger) |
336 | 0 | size = (double)v.u.i; |
337 | 241 | else |
338 | 241 | size = v.u.d; |
339 | 241 | size = size / dpi * 72.0 / scale; |
340 | 241 | } |
341 | 241 | (void)FcPatternObjectDel (pattern, FC_SIZE_OBJECT); |
342 | 241 | FcPatternObjectAddDouble (pattern, FC_SIZE_OBJECT, size); |
343 | | |
344 | 241 | if (!FcPatternFindObjectIter (pattern, &iter, FC_FONTVERSION_OBJECT)) |
345 | 241 | FcPatternObjectAddInteger (pattern, FC_FONTVERSION_OBJECT, 0x7fffffff); |
346 | | |
347 | 241 | if (!FcPatternFindObjectIter (pattern, &iter, FC_HINT_STYLE_OBJECT)) |
348 | 241 | FcPatternObjectAddInteger (pattern, FC_HINT_STYLE_OBJECT, FC_HINT_FULL); |
349 | | |
350 | 241 | if (!FcPatternFindObjectIter (pattern, &iter, FC_NAMELANG_OBJECT)) |
351 | 241 | FcPatternObjectAddString (pattern, FC_NAMELANG_OBJECT, FcConfigGetDefaultLang (config)); |
352 | | |
353 | | /* shouldn't be failed. */ |
354 | 241 | FcPatternObjectGet (pattern, FC_NAMELANG_OBJECT, 0, &namelang); |
355 | | /* Add a fallback to ensure the english name when the requested language |
356 | | * isn't available. this would helps for the fonts that have non-English |
357 | | * name at the beginning. |
358 | | */ |
359 | | /* Set "en-us" instead of "en" to avoid giving higher score to "en". |
360 | | * This is a hack for the case that the orth is not like ll-cc, because, |
361 | | * if no namelang isn't explicitly set, it will has something like ll-cc |
362 | | * according to current locale. which may causes FcLangDifferentTerritory |
363 | | * at FcLangCompare(). thus, the English name is selected so that |
364 | | * exact matched "en" has higher score than ll-cc. |
365 | | */ |
366 | 241 | v2.type = FcTypeString; |
367 | 241 | v2.u.s = (FcChar8 *)"en-us"; |
368 | 241 | if (!FcPatternFindObjectIter (pattern, &iter, FC_FAMILYLANG_OBJECT)) { |
369 | 241 | FcPatternObjectAdd (pattern, FC_FAMILYLANG_OBJECT, namelang, FcTrue); |
370 | 241 | FcPatternObjectAddWithBinding (pattern, FC_FAMILYLANG_OBJECT, v2, FcValueBindingWeak, FcTrue); |
371 | 241 | } |
372 | 241 | if (!FcPatternFindObjectIter (pattern, &iter, FC_STYLELANG_OBJECT)) { |
373 | 241 | FcPatternObjectAdd (pattern, FC_STYLELANG_OBJECT, namelang, FcTrue); |
374 | 241 | FcPatternObjectAddWithBinding (pattern, FC_STYLELANG_OBJECT, v2, FcValueBindingWeak, FcTrue); |
375 | 241 | } |
376 | 241 | if (!FcPatternFindObjectIter (pattern, &iter, FC_FULLNAMELANG_OBJECT)) { |
377 | 241 | FcPatternObjectAdd (pattern, FC_FULLNAMELANG_OBJECT, namelang, FcTrue); |
378 | 241 | FcPatternObjectAddWithBinding (pattern, FC_FULLNAMELANG_OBJECT, v2, FcValueBindingWeak, FcTrue); |
379 | 241 | } |
380 | | |
381 | 241 | if (FcPatternObjectGet (pattern, FC_PRGNAME_OBJECT, 0, &v) == FcResultNoMatch) { |
382 | 0 | FcChar8 *prgname = FcConfigGetPrgname (config); |
383 | 0 | if (prgname) |
384 | 0 | FcPatternObjectAddString (pattern, FC_PRGNAME_OBJECT, prgname); |
385 | 0 | } |
386 | | |
387 | 241 | if (FcPatternObjectGet (pattern, FC_DESKTOP_NAME_OBJECT, 0, &v) == FcResultNoMatch) { |
388 | 241 | FcChar8 *desktop = FcConfigGetDesktopName (config); |
389 | 241 | if (desktop) |
390 | 0 | FcPatternObjectAddString (pattern, FC_DESKTOP_NAME_OBJECT, desktop); |
391 | 241 | } |
392 | | |
393 | 241 | if (!FcPatternFindObjectIter (pattern, &iter, FC_ORDER_OBJECT)) |
394 | 241 | FcPatternObjectAddInteger (pattern, FC_ORDER_OBJECT, 0); |
395 | | |
396 | 241 | FcConfigDestroy (config); |
397 | 241 | } |
398 | | |
399 | | void |
400 | | FcDefaultSubstitute (FcPattern *pattern) |
401 | 241 | { |
402 | 241 | FcConfigSetDefaultSubstitute (NULL, pattern); |
403 | 241 | } |
404 | | #define __fcdefault__ |
405 | | #include "fcaliastail.h" |
406 | | #undef __fcdefault__ |