]>
Commit | Line | Data |
---|---|---|
24330d27 | 1 | /* |
793e946c | 2 | * $RCSId: xc/lib/fontconfig/src/fccfg.c,v 1.23 2002/08/31 22:17:32 keithp Exp $ |
24330d27 | 3 | * |
46b51147 | 4 | * Copyright © 2000 Keith Packard |
24330d27 KP |
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 Keith Packard not be used in | |
11 | * advertising or publicity pertaining to distribution of the software without | |
12 | * specific, written prior permission. Keith Packard makes 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 | * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, | |
17 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO | |
18 | * EVENT SHALL KEITH PACKARD 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 | ||
24330d27 KP |
25 | #include "fcint.h" |
26 | ||
92af858f | 27 | #if defined (_WIN32) && (defined (PIC) || defined (DLL_EXPORT)) |
daeed6e0 TL |
28 | #define STRICT |
29 | #include <windows.h> | |
30 | #undef STRICT | |
31 | #endif | |
32 | ||
e5206dbc TL |
33 | #if defined (_WIN32) && !defined (R_OK) |
34 | #define R_OK 4 | |
35 | #endif | |
36 | ||
6e9fc5de | 37 | FcConfig *_fcConfig; |
24330d27 KP |
38 | |
39 | FcConfig * | |
40 | FcConfigCreate (void) | |
41 | { | |
42 | FcSetName set; | |
43 | FcConfig *config; | |
44 | ||
45 | config = malloc (sizeof (FcConfig)); | |
46 | if (!config) | |
47 | goto bail0; | |
179c3995 | 48 | FcMemAlloc (FC_MEM_CONFIG, sizeof (FcConfig)); |
24330d27 | 49 | |
179c3995 KP |
50 | config->configDirs = FcStrSetCreate (); |
51 | if (!config->configDirs) | |
24330d27 | 52 | goto bail1; |
24330d27 | 53 | |
179c3995 | 54 | config->configFiles = FcStrSetCreate (); |
24330d27 KP |
55 | if (!config->configFiles) |
56 | goto bail2; | |
179c3995 KP |
57 | |
58 | config->fontDirs = FcStrSetCreate (); | |
59 | if (!config->fontDirs) | |
60 | goto bail3; | |
24330d27 | 61 | |
d47c9d6e KP |
62 | config->acceptGlobs = FcStrSetCreate (); |
63 | if (!config->acceptGlobs) | |
64 | goto bail4; | |
65 | ||
66 | config->rejectGlobs = FcStrSetCreate (); | |
67 | if (!config->rejectGlobs) | |
68 | goto bail5; | |
69 | ||
4f27c1c0 KP |
70 | config->acceptPatterns = FcFontSetCreate (); |
71 | if (!config->acceptPatterns) | |
72 | goto bail6; | |
73 | ||
74 | config->rejectPatterns = FcFontSetCreate (); | |
75 | if (!config->rejectPatterns) | |
76 | goto bail7; | |
77 | ||
24330d27 | 78 | config->cache = 0; |
ff3f1f98 KP |
79 | if (FcConfigHome()) |
80 | if (!FcConfigSetCache (config, (FcChar8 *) ("~/" FC_USER_CACHE_FILE))) | |
4f27c1c0 | 81 | goto bail8; |
24330d27 | 82 | |
f4c52909 TL |
83 | #ifdef _WIN32 |
84 | if (config->cache == 0) | |
85 | { | |
86 | /* If no home, use the temp folder. */ | |
87 | FcChar8 dummy[1]; | |
88 | int templen = GetTempPath (1, dummy); | |
89 | FcChar8 *temp = malloc (templen + 1); | |
90 | ||
91 | if (temp) | |
92 | { | |
93 | FcChar8 *cache_dir; | |
94 | ||
95 | GetTempPath (templen + 1, temp); | |
96 | cache_dir = FcStrPlus (temp, FC_USER_CACHE_FILE); | |
97 | free (temp); | |
98 | if (!FcConfigSetCache (config, cache_dir)) | |
99 | { | |
100 | FcStrFree (cache_dir); | |
101 | goto bail6; | |
102 | } | |
103 | FcStrFree (cache_dir); | |
104 | } | |
105 | } | |
106 | #endif | |
107 | ||
24330d27 KP |
108 | config->blanks = 0; |
109 | ||
110 | config->substPattern = 0; | |
111 | config->substFont = 0; | |
112 | config->maxObjects = 0; | |
113 | for (set = FcSetSystem; set <= FcSetApplication; set++) | |
114 | config->fonts[set] = 0; | |
179c3995 KP |
115 | |
116 | config->rescanTime = time(0); | |
117 | config->rescanInterval = 30; | |
24330d27 KP |
118 | |
119 | return config; | |
120 | ||
4f27c1c0 KP |
121 | bail8: |
122 | FcFontSetDestroy (config->rejectPatterns); | |
123 | bail7: | |
124 | FcFontSetDestroy (config->acceptPatterns); | |
d47c9d6e KP |
125 | bail6: |
126 | FcStrSetDestroy (config->rejectGlobs); | |
127 | bail5: | |
128 | FcStrSetDestroy (config->acceptGlobs); | |
179c3995 KP |
129 | bail4: |
130 | FcStrSetDestroy (config->fontDirs); | |
24330d27 | 131 | bail3: |
179c3995 | 132 | FcStrSetDestroy (config->configFiles); |
24330d27 | 133 | bail2: |
179c3995 | 134 | FcStrSetDestroy (config->configDirs); |
24330d27 KP |
135 | bail1: |
136 | free (config); | |
179c3995 | 137 | FcMemFree (FC_MEM_CONFIG, sizeof (FcConfig)); |
24330d27 KP |
138 | bail0: |
139 | return 0; | |
140 | } | |
141 | ||
4645eedf KP |
142 | typedef struct _FcFileTime { |
143 | time_t time; | |
144 | FcBool set; | |
145 | } FcFileTime; | |
146 | ||
147 | static FcFileTime | |
179c3995 KP |
148 | FcConfigNewestFile (FcStrSet *files) |
149 | { | |
150 | FcStrList *list = FcStrListCreate (files); | |
4645eedf | 151 | FcFileTime newest = { 0, FcFalse }; |
179c3995 KP |
152 | FcChar8 *file; |
153 | struct stat statb; | |
154 | ||
155 | if (list) | |
156 | { | |
157 | while ((file = FcStrListNext (list))) | |
179c3995 | 158 | if (stat ((char *) file, &statb) == 0) |
4645eedf | 159 | if (!newest.set || statb.st_mtime - newest.time > 0) |
b68b9646 KP |
160 | { |
161 | newest.set = FcTrue; | |
4645eedf | 162 | newest.time = statb.st_mtime; |
b68b9646 | 163 | } |
179c3995 KP |
164 | FcStrListDone (list); |
165 | } | |
166 | return newest; | |
167 | } | |
168 | ||
169 | FcBool | |
170 | FcConfigUptoDate (FcConfig *config) | |
171 | { | |
4645eedf KP |
172 | FcFileTime config_time, font_time; |
173 | time_t now = time(0); | |
179c3995 KP |
174 | if (!config) |
175 | { | |
176 | config = FcConfigGetCurrent (); | |
177 | if (!config) | |
178 | return FcFalse; | |
179 | } | |
180 | config_time = FcConfigNewestFile (config->configFiles); | |
28f93bc4 | 181 | font_time = FcConfigNewestFile (config->fontDirs); |
4645eedf | 182 | if ((config_time.set && config_time.time - config->rescanTime > 0) || |
28f93bc4 | 183 | (font_time.set && (font_time.time - config->rescanTime) > 0)) |
179c3995 KP |
184 | { |
185 | return FcFalse; | |
186 | } | |
187 | config->rescanTime = now; | |
188 | return FcTrue; | |
189 | } | |
190 | ||
24330d27 KP |
191 | static void |
192 | FcSubstDestroy (FcSubst *s) | |
193 | { | |
194 | FcSubst *n; | |
195 | ||
196 | while (s) | |
197 | { | |
198 | n = s->next; | |
f4007a67 KP |
199 | if (s->test) |
200 | FcTestDestroy (s->test); | |
201 | if (s->edit) | |
202 | FcEditDestroy (s->edit); | |
34cd0514 CW |
203 | free (s); |
204 | FcMemFree (FC_MEM_SUBST, sizeof (FcSubst)); | |
24330d27 KP |
205 | s = n; |
206 | } | |
207 | } | |
208 | ||
24330d27 KP |
209 | void |
210 | FcConfigDestroy (FcConfig *config) | |
211 | { | |
212 | FcSetName set; | |
24330d27 | 213 | |
179c3995 KP |
214 | if (config == _fcConfig) |
215 | _fcConfig = 0; | |
216 | ||
217 | FcStrSetDestroy (config->configDirs); | |
218 | FcStrSetDestroy (config->fontDirs); | |
219 | FcStrSetDestroy (config->configFiles); | |
d47c9d6e KP |
220 | FcStrSetDestroy (config->acceptGlobs); |
221 | FcStrSetDestroy (config->rejectGlobs); | |
4f27c1c0 KP |
222 | FcFontSetDestroy (config->acceptPatterns); |
223 | FcFontSetDestroy (config->rejectPatterns); | |
179c3995 | 224 | |
34cd0514 CW |
225 | if (config->blanks) |
226 | FcBlanksDestroy (config->blanks); | |
227 | ||
ee1debfd KP |
228 | if (config->cache) |
229 | FcStrFree (config->cache); | |
24330d27 KP |
230 | |
231 | FcSubstDestroy (config->substPattern); | |
232 | FcSubstDestroy (config->substFont); | |
233 | for (set = FcSetSystem; set <= FcSetApplication; set++) | |
234 | if (config->fonts[set]) | |
235 | FcFontSetDestroy (config->fonts[set]); | |
34cd0514 | 236 | |
179c3995 KP |
237 | free (config); |
238 | FcMemFree (FC_MEM_CONFIG, sizeof (FcConfig)); | |
24330d27 KP |
239 | } |
240 | ||
241 | /* | |
242 | * Scan the current list of directories in the configuration | |
243 | * and build the set of available fonts. Update the | |
244 | * per-user cache file to reflect the new configuration | |
245 | */ | |
246 | ||
247 | FcBool | |
248 | FcConfigBuildFonts (FcConfig *config) | |
249 | { | |
4262e0b3 | 250 | FcFontSet *fonts, *cached_fonts; |
327a7fd4 KP |
251 | FcGlobalCache *cache; |
252 | FcStrList *list; | |
253 | FcChar8 *dir; | |
24330d27 | 254 | |
212c9f43 PL |
255 | #if 0 |
256 | if (config->cache) | |
257 | FcGlobalCacheLoad (cache, config->cache); | |
258 | #endif | |
259 | if (config->fonts[FcSetSystem]) | |
260 | return FcTrue; | |
261 | ||
24330d27 KP |
262 | fonts = FcFontSetCreate (); |
263 | if (!fonts) | |
264 | goto bail0; | |
212c9f43 PL |
265 | |
266 | #if 0 | |
327a7fd4 | 267 | cache = FcGlobalCacheCreate (); |
24330d27 KP |
268 | if (!cache) |
269 | goto bail1; | |
212c9f43 | 270 | #endif |
24330d27 | 271 | |
4262e0b3 PL |
272 | cached_fonts = FcCacheRead(config); |
273 | if (!cached_fonts) | |
274 | { | |
275 | list = FcConfigGetFontDirs (config); | |
276 | if (!list) | |
277 | goto bail1; | |
278 | ||
279 | while ((dir = FcStrListNext (list))) | |
280 | { | |
281 | if (FcDebug () & FC_DBG_FONTSET) | |
282 | printf ("scan dir %s\n", dir); | |
283 | FcDirScanConfig (fonts, config->fontDirs, cache, | |
284 | config->blanks, dir, FcFalse, config); | |
285 | } | |
286 | ||
287 | FcStrListDone (list); | |
288 | } | |
289 | else | |
24330d27 | 290 | { |
4262e0b3 PL |
291 | int i; |
292 | ||
293 | for (i = 0; i < cached_fonts->nfont; i++) | |
294 | { | |
295 | if (FcConfigAcceptFont (config, cached_fonts->fonts[i])) | |
296 | FcFontSetAdd (fonts, cached_fonts->fonts[i]); | |
297 | } | |
298 | FcFontSetDestroy (cached_fonts); | |
24330d27 KP |
299 | } |
300 | ||
301 | if (FcDebug () & FC_DBG_FONTSET) | |
302 | FcFontSetPrint (fonts); | |
303 | ||
212c9f43 | 304 | #if 0 |
ee1debfd KP |
305 | if (config->cache) |
306 | FcGlobalCacheSave (cache, config->cache); | |
327a7fd4 | 307 | FcGlobalCacheDestroy (cache); |
212c9f43 | 308 | #endif |
24330d27 KP |
309 | |
310 | FcConfigSetFonts (config, fonts, FcSetSystem); | |
311 | ||
312 | return FcTrue; | |
313 | bail1: | |
314 | FcFontSetDestroy (fonts); | |
315 | bail0: | |
316 | return FcFalse; | |
317 | } | |
318 | ||
319 | FcBool | |
320 | FcConfigSetCurrent (FcConfig *config) | |
321 | { | |
322 | if (!config->fonts) | |
323 | if (!FcConfigBuildFonts (config)) | |
324 | return FcFalse; | |
325 | ||
6e9fc5de KP |
326 | if (_fcConfig) |
327 | FcConfigDestroy (_fcConfig); | |
328 | _fcConfig = config; | |
24330d27 KP |
329 | return FcTrue; |
330 | } | |
331 | ||
332 | FcConfig * | |
333 | FcConfigGetCurrent (void) | |
334 | { | |
6e9fc5de KP |
335 | if (!_fcConfig) |
336 | if (!FcInit ()) | |
337 | return 0; | |
338 | return _fcConfig; | |
24330d27 KP |
339 | } |
340 | ||
341 | FcBool | |
179c3995 KP |
342 | FcConfigAddConfigDir (FcConfig *config, |
343 | const FcChar8 *d) | |
24330d27 | 344 | { |
179c3995 KP |
345 | return FcStrSetAddFilename (config->configDirs, d); |
346 | } | |
24330d27 | 347 | |
179c3995 KP |
348 | FcStrList * |
349 | FcConfigGetConfigDirs (FcConfig *config) | |
350 | { | |
351 | if (!config) | |
24330d27 | 352 | { |
179c3995 KP |
353 | config = FcConfigGetCurrent (); |
354 | if (!config) | |
355 | return 0; | |
24330d27 | 356 | } |
179c3995 KP |
357 | return FcStrListCreate (config->configDirs); |
358 | } | |
359 | ||
360 | FcBool | |
361 | FcConfigAddFontDir (FcConfig *config, | |
362 | const FcChar8 *d) | |
363 | { | |
364 | return FcStrSetAddFilename (config->fontDirs, d); | |
24330d27 KP |
365 | } |
366 | ||
179c3995 KP |
367 | FcBool |
368 | FcConfigAddDir (FcConfig *config, | |
369 | const FcChar8 *d) | |
370 | { | |
371 | return (FcConfigAddConfigDir (config, d) && | |
372 | FcConfigAddFontDir (config, d)); | |
373 | } | |
374 | ||
375 | FcStrList * | |
376 | FcConfigGetFontDirs (FcConfig *config) | |
24330d27 KP |
377 | { |
378 | if (!config) | |
379 | { | |
380 | config = FcConfigGetCurrent (); | |
381 | if (!config) | |
382 | return 0; | |
383 | } | |
179c3995 | 384 | return FcStrListCreate (config->fontDirs); |
24330d27 KP |
385 | } |
386 | ||
387 | FcBool | |
388 | FcConfigAddConfigFile (FcConfig *config, | |
ccb3e93b | 389 | const FcChar8 *f) |
24330d27 | 390 | { |
179c3995 KP |
391 | FcBool ret; |
392 | FcChar8 *file = FcConfigFilename (f); | |
393 | ||
24330d27 KP |
394 | if (!file) |
395 | return FcFalse; | |
179c3995 KP |
396 | |
397 | ret = FcStrSetAdd (config->configFiles, file); | |
398 | FcStrFree (file); | |
399 | return ret; | |
24330d27 KP |
400 | } |
401 | ||
179c3995 | 402 | FcStrList * |
24330d27 KP |
403 | FcConfigGetConfigFiles (FcConfig *config) |
404 | { | |
405 | if (!config) | |
406 | { | |
407 | config = FcConfigGetCurrent (); | |
408 | if (!config) | |
409 | return 0; | |
410 | } | |
179c3995 | 411 | return FcStrListCreate (config->configFiles); |
24330d27 KP |
412 | } |
413 | ||
414 | FcBool | |
415 | FcConfigSetCache (FcConfig *config, | |
ccb3e93b | 416 | const FcChar8 *c) |
24330d27 | 417 | { |
179c3995 KP |
418 | FcChar8 *new = FcStrCopyFilename (c); |
419 | ||
420 | if (!new) | |
421 | return FcFalse; | |
24330d27 | 422 | if (config->cache) |
179c3995 | 423 | FcStrFree (config->cache); |
24330d27 KP |
424 | config->cache = new; |
425 | return FcTrue; | |
426 | } | |
427 | ||
ccb3e93b | 428 | FcChar8 * |
24330d27 KP |
429 | FcConfigGetCache (FcConfig *config) |
430 | { | |
431 | if (!config) | |
432 | { | |
433 | config = FcConfigGetCurrent (); | |
434 | if (!config) | |
435 | return 0; | |
436 | } | |
437 | return config->cache; | |
438 | } | |
439 | ||
440 | FcFontSet * | |
441 | FcConfigGetFonts (FcConfig *config, | |
442 | FcSetName set) | |
443 | { | |
444 | if (!config) | |
445 | { | |
446 | config = FcConfigGetCurrent (); | |
447 | if (!config) | |
448 | return 0; | |
449 | } | |
450 | return config->fonts[set]; | |
451 | } | |
452 | ||
453 | void | |
454 | FcConfigSetFonts (FcConfig *config, | |
455 | FcFontSet *fonts, | |
456 | FcSetName set) | |
457 | { | |
458 | if (config->fonts[set]) | |
459 | FcFontSetDestroy (config->fonts[set]); | |
460 | config->fonts[set] = fonts; | |
461 | } | |
462 | ||
179c3995 KP |
463 | |
464 | ||
24330d27 KP |
465 | FcBlanks * |
466 | FcConfigGetBlanks (FcConfig *config) | |
467 | { | |
468 | if (!config) | |
469 | { | |
470 | config = FcConfigGetCurrent (); | |
471 | if (!config) | |
472 | return 0; | |
473 | } | |
474 | return config->blanks; | |
475 | } | |
476 | ||
477 | FcBool | |
478 | FcConfigAddBlank (FcConfig *config, | |
479 | FcChar32 blank) | |
480 | { | |
481 | FcBlanks *b; | |
482 | ||
483 | b = config->blanks; | |
484 | if (!b) | |
485 | { | |
486 | b = FcBlanksCreate (); | |
487 | if (!b) | |
488 | return FcFalse; | |
489 | } | |
490 | if (!FcBlanksAdd (b, blank)) | |
491 | return FcFalse; | |
492 | config->blanks = b; | |
493 | return FcTrue; | |
494 | } | |
495 | ||
179c3995 KP |
496 | int |
497 | FcConfigGetRescanInverval (FcConfig *config) | |
498 | { | |
499 | if (!config) | |
500 | { | |
501 | config = FcConfigGetCurrent (); | |
502 | if (!config) | |
503 | return 0; | |
504 | } | |
505 | return config->rescanInterval; | |
506 | } | |
507 | ||
508 | FcBool | |
509 | FcConfigSetRescanInverval (FcConfig *config, int rescanInterval) | |
510 | { | |
511 | if (!config) | |
512 | { | |
513 | config = FcConfigGetCurrent (); | |
514 | if (!config) | |
515 | return FcFalse; | |
516 | } | |
517 | config->rescanInterval = rescanInterval; | |
518 | return FcTrue; | |
519 | } | |
520 | ||
24330d27 KP |
521 | FcBool |
522 | FcConfigAddEdit (FcConfig *config, | |
523 | FcTest *test, | |
524 | FcEdit *edit, | |
525 | FcMatchKind kind) | |
526 | { | |
527 | FcSubst *subst, **prev; | |
528 | FcTest *t; | |
529 | int num; | |
530 | ||
531 | subst = (FcSubst *) malloc (sizeof (FcSubst)); | |
532 | if (!subst) | |
533 | return FcFalse; | |
9dac3c59 | 534 | FcMemAlloc (FC_MEM_SUBST, sizeof (FcSubst)); |
24330d27 KP |
535 | if (kind == FcMatchPattern) |
536 | prev = &config->substPattern; | |
537 | else | |
538 | prev = &config->substFont; | |
539 | for (; *prev; prev = &(*prev)->next); | |
540 | *prev = subst; | |
541 | subst->next = 0; | |
542 | subst->test = test; | |
543 | subst->edit = edit; | |
24330d27 KP |
544 | num = 0; |
545 | for (t = test; t; t = t->next) | |
938bc633 KP |
546 | { |
547 | if (t->kind == FcMatchDefault) | |
548 | t->kind = kind; | |
24330d27 | 549 | num++; |
938bc633 | 550 | } |
24330d27 KP |
551 | if (config->maxObjects < num) |
552 | config->maxObjects = num; | |
938bc633 KP |
553 | if (FcDebug () & FC_DBG_EDIT) |
554 | { | |
555 | printf ("Add Subst "); | |
556 | FcSubstPrint (subst); | |
557 | } | |
24330d27 KP |
558 | return FcTrue; |
559 | } | |
560 | ||
561 | typedef struct _FcSubState { | |
562 | FcPatternElt *elt; | |
563 | FcValueList *value; | |
564 | } FcSubState; | |
565 | ||
24330d27 KP |
566 | static FcValue |
567 | FcConfigPromote (FcValue v, FcValue u) | |
568 | { | |
569 | if (v.type == FcTypeInteger) | |
570 | { | |
571 | v.type = FcTypeDouble; | |
572 | v.u.d = (double) v.u.i; | |
573 | } | |
574 | else if (v.type == FcTypeVoid && u.type == FcTypeMatrix) | |
575 | { | |
4262e0b3 | 576 | v.u.m = &FcIdentityMatrix; |
327a7fd4 | 577 | v.type = FcTypeMatrix; |
24330d27 | 578 | } |
d8d73958 KP |
579 | else if (v.type == FcTypeString && u.type == FcTypeLangSet) |
580 | { | |
4262e0b3 | 581 | v.u.l = FcLangSetPromote (v.u.s); |
d8d73958 KP |
582 | v.type = FcTypeLangSet; |
583 | } | |
24330d27 KP |
584 | return v; |
585 | } | |
586 | ||
587 | FcBool | |
4262e0b3 | 588 | FcConfigCompareValue (const FcValue *left_o, |
ca4339b8 | 589 | FcOp op, |
4262e0b3 | 590 | const FcValue *right_o) |
24330d27 | 591 | { |
4262e0b3 PL |
592 | FcValue left = FcValueCanonicalize(left_o); |
593 | FcValue right = FcValueCanonicalize(right_o); | |
ca4339b8 | 594 | FcBool ret = FcFalse; |
24330d27 | 595 | |
74a623e0 KP |
596 | left = FcConfigPromote (left, right); |
597 | right = FcConfigPromote (right, left); | |
598 | if (left.type == right.type) | |
24330d27 | 599 | { |
74a623e0 | 600 | switch (left.type) { |
24330d27 KP |
601 | case FcTypeInteger: |
602 | break; /* FcConfigPromote prevents this from happening */ | |
603 | case FcTypeDouble: | |
604 | switch (op) { | |
605 | case FcOpEqual: | |
606 | case FcOpContains: | |
74a623e0 KP |
607 | case FcOpListing: |
608 | ret = left.u.d == right.u.d; | |
24330d27 | 609 | break; |
47d4f950 KP |
610 | case FcOpNotEqual: |
611 | case FcOpNotContains: | |
74a623e0 | 612 | ret = left.u.d != right.u.d; |
24330d27 KP |
613 | break; |
614 | case FcOpLess: | |
74a623e0 | 615 | ret = left.u.d < right.u.d; |
24330d27 KP |
616 | break; |
617 | case FcOpLessEqual: | |
74a623e0 | 618 | ret = left.u.d <= right.u.d; |
24330d27 KP |
619 | break; |
620 | case FcOpMore: | |
74a623e0 | 621 | ret = left.u.d > right.u.d; |
24330d27 KP |
622 | break; |
623 | case FcOpMoreEqual: | |
74a623e0 | 624 | ret = left.u.d >= right.u.d; |
24330d27 KP |
625 | break; |
626 | default: | |
627 | break; | |
628 | } | |
629 | break; | |
630 | case FcTypeBool: | |
631 | switch (op) { | |
632 | case FcOpEqual: | |
633 | case FcOpContains: | |
74a623e0 KP |
634 | case FcOpListing: |
635 | ret = left.u.b == right.u.b; | |
24330d27 | 636 | break; |
47d4f950 KP |
637 | case FcOpNotEqual: |
638 | case FcOpNotContains: | |
74a623e0 | 639 | ret = left.u.b != right.u.b; |
24330d27 KP |
640 | break; |
641 | default: | |
642 | break; | |
643 | } | |
644 | break; | |
645 | case FcTypeString: | |
646 | switch (op) { | |
647 | case FcOpEqual: | |
74a623e0 | 648 | case FcOpListing: |
4262e0b3 | 649 | ret = FcStrCmpIgnoreCase (left.u.s, right.u.s) == 0; |
24330d27 | 650 | break; |
d4d1e8bc | 651 | case FcOpContains: |
4262e0b3 | 652 | ret = FcStrStrIgnoreCase (left.u.s, right.u.s) != 0; |
d4d1e8bc | 653 | break; |
47d4f950 | 654 | case FcOpNotEqual: |
4262e0b3 | 655 | ret = FcStrCmpIgnoreCase (left.u.s, right.u.s) != 0; |
d4d1e8bc | 656 | break; |
f1a42f6b | 657 | case FcOpNotContains: |
4262e0b3 | 658 | ret = FcStrCmpIgnoreCase (left.u.s, right.u.s) == 0; |
f1a42f6b | 659 | break; |
24330d27 KP |
660 | default: |
661 | break; | |
662 | } | |
663 | break; | |
664 | case FcTypeMatrix: | |
665 | switch (op) { | |
666 | case FcOpEqual: | |
667 | case FcOpContains: | |
74a623e0 | 668 | case FcOpListing: |
4262e0b3 | 669 | ret = FcMatrixEqual (left.u.m, right.u.m); |
24330d27 KP |
670 | break; |
671 | case FcOpNotEqual: | |
47d4f950 | 672 | case FcOpNotContains: |
4262e0b3 | 673 | ret = !FcMatrixEqual (left.u.m, right.u.m); |
24330d27 KP |
674 | break; |
675 | default: | |
676 | break; | |
677 | } | |
678 | break; | |
679 | case FcTypeCharSet: | |
680 | switch (op) { | |
681 | case FcOpContains: | |
74a623e0 KP |
682 | case FcOpListing: |
683 | /* left contains right if right is a subset of left */ | |
4262e0b3 | 684 | ret = FcCharSetIsSubset (right.u.c, left.u.c); |
24330d27 | 685 | break; |
47d4f950 | 686 | case FcOpNotContains: |
74a623e0 | 687 | /* left contains right if right is a subset of left */ |
4262e0b3 | 688 | ret = !FcCharSetIsSubset (right.u.c, left.u.c); |
47d4f950 | 689 | break; |
24330d27 | 690 | case FcOpEqual: |
4262e0b3 | 691 | ret = FcCharSetEqual (left.u.c, right.u.c); |
24330d27 KP |
692 | break; |
693 | case FcOpNotEqual: | |
4262e0b3 | 694 | ret = !FcCharSetEqual (left.u.c, right.u.c); |
24330d27 KP |
695 | break; |
696 | default: | |
697 | break; | |
698 | } | |
699 | break; | |
d8d73958 KP |
700 | case FcTypeLangSet: |
701 | switch (op) { | |
702 | case FcOpContains: | |
74a623e0 | 703 | case FcOpListing: |
4262e0b3 | 704 | ret = FcLangSetContains (left.u.l, right.u.l); |
d8d73958 | 705 | break; |
47d4f950 | 706 | case FcOpNotContains: |
4262e0b3 | 707 | ret = !FcLangSetContains (left.u.l, right.u.l); |
47d4f950 | 708 | break; |
d8d73958 | 709 | case FcOpEqual: |
4262e0b3 | 710 | ret = FcLangSetEqual (left.u.l, right.u.l); |
d8d73958 KP |
711 | break; |
712 | case FcOpNotEqual: | |
4262e0b3 | 713 | ret = !FcLangSetEqual (left.u.l, right.u.l); |
d8d73958 KP |
714 | break; |
715 | default: | |
716 | break; | |
717 | } | |
718 | break; | |
24330d27 KP |
719 | case FcTypeVoid: |
720 | switch (op) { | |
721 | case FcOpEqual: | |
722 | case FcOpContains: | |
74a623e0 | 723 | case FcOpListing: |
24330d27 KP |
724 | ret = FcTrue; |
725 | break; | |
726 | default: | |
727 | break; | |
728 | } | |
729 | break; | |
8ec077f2 KP |
730 | case FcTypeFTFace: |
731 | switch (op) { | |
732 | case FcOpEqual: | |
47d4f950 | 733 | case FcOpContains: |
74a623e0 KP |
734 | case FcOpListing: |
735 | ret = left.u.f == right.u.f; | |
8ec077f2 KP |
736 | break; |
737 | case FcOpNotEqual: | |
47d4f950 | 738 | case FcOpNotContains: |
74a623e0 | 739 | ret = left.u.f != right.u.f; |
8ec077f2 KP |
740 | break; |
741 | default: | |
742 | break; | |
743 | } | |
938bc633 | 744 | break; |
24330d27 KP |
745 | } |
746 | } | |
747 | else | |
748 | { | |
47d4f950 | 749 | if (op == FcOpNotEqual || op == FcOpNotContains) |
24330d27 KP |
750 | ret = FcTrue; |
751 | } | |
752 | return ret; | |
753 | } | |
754 | ||
755 | ||
3f7653c2 KP |
756 | #define _FcDoubleFloor(d) ((int) (d)) |
757 | #define _FcDoubleCeil(d) ((double) (int) (d) == (d) ? (int) (d) : (int) ((d) + 1)) | |
758 | #define FcDoubleFloor(d) ((d) >= 0 ? _FcDoubleFloor(d) : -_FcDoubleCeil(-(d))) | |
759 | #define FcDoubleCeil(d) ((d) >= 0 ? _FcDoubleCeil(d) : -_FcDoubleFloor(-(d))) | |
760 | #define FcDoubleRound(d) FcDoubleFloor ((d) + 0.5) | |
761 | #define FcDoubleTrunc(d) ((d) >= 0 ? _FcDoubleFloor (d) : -_FcDoubleFloor (-(d))) | |
762 | ||
24330d27 KP |
763 | static FcValue |
764 | FcConfigEvaluate (FcPattern *p, FcExpr *e) | |
765 | { | |
766 | FcValue v, vl, vr; | |
767 | FcResult r; | |
768 | FcMatrix *m; | |
24330d27 KP |
769 | |
770 | switch (e->op) { | |
771 | case FcOpInteger: | |
772 | v.type = FcTypeInteger; | |
773 | v.u.i = e->u.ival; | |
774 | break; | |
775 | case FcOpDouble: | |
776 | v.type = FcTypeDouble; | |
777 | v.u.d = e->u.dval; | |
778 | break; | |
779 | case FcOpString: | |
780 | v.type = FcTypeString; | |
4262e0b3 | 781 | v.u.s = FcObjectStaticName(e->u.sval); |
24330d27 KP |
782 | v = FcValueSave (v); |
783 | break; | |
784 | case FcOpMatrix: | |
785 | v.type = FcTypeMatrix; | |
4262e0b3 | 786 | v.u.m = e->u.mval; |
24330d27 KP |
787 | v = FcValueSave (v); |
788 | break; | |
789 | case FcOpCharSet: | |
790 | v.type = FcTypeCharSet; | |
4262e0b3 | 791 | v.u.c = e->u.cval; |
24330d27 KP |
792 | v = FcValueSave (v); |
793 | break; | |
794 | case FcOpBool: | |
795 | v.type = FcTypeBool; | |
796 | v.u.b = e->u.bval; | |
797 | break; | |
798 | case FcOpField: | |
799 | r = FcPatternGet (p, e->u.field, 0, &v); | |
800 | if (r != FcResultMatch) | |
801 | v.type = FcTypeVoid; | |
802 | break; | |
803 | case FcOpConst: | |
804 | if (FcNameConstant (e->u.constant, &v.u.i)) | |
805 | v.type = FcTypeInteger; | |
806 | else | |
807 | v.type = FcTypeVoid; | |
808 | break; | |
809 | case FcOpQuest: | |
810 | vl = FcConfigEvaluate (p, e->u.tree.left); | |
811 | if (vl.type == FcTypeBool) | |
812 | { | |
813 | if (vl.u.b) | |
814 | v = FcConfigEvaluate (p, e->u.tree.right->u.tree.left); | |
815 | else | |
816 | v = FcConfigEvaluate (p, e->u.tree.right->u.tree.right); | |
817 | } | |
818 | else | |
819 | v.type = FcTypeVoid; | |
820 | FcValueDestroy (vl); | |
821 | break; | |
47d4f950 | 822 | case FcOpEqual: |
24330d27 KP |
823 | case FcOpNotEqual: |
824 | case FcOpLess: | |
825 | case FcOpLessEqual: | |
826 | case FcOpMore: | |
827 | case FcOpMoreEqual: | |
47d4f950 KP |
828 | case FcOpContains: |
829 | case FcOpNotContains: | |
74a623e0 | 830 | case FcOpListing: |
938bc633 KP |
831 | vl = FcConfigEvaluate (p, e->u.tree.left); |
832 | vr = FcConfigEvaluate (p, e->u.tree.right); | |
833 | v.type = FcTypeBool; | |
4262e0b3 | 834 | v.u.b = FcConfigCompareValue (&vl, e->op, &vr); |
938bc633 KP |
835 | FcValueDestroy (vl); |
836 | FcValueDestroy (vr); | |
837 | break; | |
838 | case FcOpOr: | |
839 | case FcOpAnd: | |
24330d27 KP |
840 | case FcOpPlus: |
841 | case FcOpMinus: | |
842 | case FcOpTimes: | |
843 | case FcOpDivide: | |
844 | vl = FcConfigEvaluate (p, e->u.tree.left); | |
845 | vr = FcConfigEvaluate (p, e->u.tree.right); | |
846 | vl = FcConfigPromote (vl, vr); | |
847 | vr = FcConfigPromote (vr, vl); | |
848 | if (vl.type == vr.type) | |
849 | { | |
850 | switch (vl.type) { | |
851 | case FcTypeDouble: | |
852 | switch (e->op) { | |
853 | case FcOpPlus: | |
854 | v.type = FcTypeDouble; | |
855 | v.u.d = vl.u.d + vr.u.d; | |
856 | break; | |
857 | case FcOpMinus: | |
858 | v.type = FcTypeDouble; | |
859 | v.u.d = vl.u.d - vr.u.d; | |
860 | break; | |
861 | case FcOpTimes: | |
862 | v.type = FcTypeDouble; | |
863 | v.u.d = vl.u.d * vr.u.d; | |
864 | break; | |
865 | case FcOpDivide: | |
866 | v.type = FcTypeDouble; | |
867 | v.u.d = vl.u.d / vr.u.d; | |
868 | break; | |
24330d27 KP |
869 | default: |
870 | v.type = FcTypeVoid; | |
871 | break; | |
872 | } | |
873 | if (v.type == FcTypeDouble && | |
874 | v.u.d == (double) (int) v.u.d) | |
875 | { | |
876 | v.type = FcTypeInteger; | |
877 | v.u.i = (int) v.u.d; | |
878 | } | |
879 | break; | |
880 | case FcTypeBool: | |
881 | switch (e->op) { | |
882 | case FcOpOr: | |
883 | v.type = FcTypeBool; | |
884 | v.u.b = vl.u.b || vr.u.b; | |
885 | break; | |
886 | case FcOpAnd: | |
887 | v.type = FcTypeBool; | |
888 | v.u.b = vl.u.b && vr.u.b; | |
889 | break; | |
24330d27 KP |
890 | default: |
891 | v.type = FcTypeVoid; | |
892 | break; | |
893 | } | |
894 | break; | |
895 | case FcTypeString: | |
896 | switch (e->op) { | |
24330d27 KP |
897 | case FcOpPlus: |
898 | v.type = FcTypeString; | |
4262e0b3 | 899 | v.u.s = FcObjectStaticName (FcStrPlus (vl.u.s, vr.u.s)); |
cd2ec1a9 | 900 | |
4262e0b3 | 901 | if (!v.u.s) |
24330d27 KP |
902 | v.type = FcTypeVoid; |
903 | break; | |
904 | default: | |
905 | v.type = FcTypeVoid; | |
906 | break; | |
907 | } | |
8c96d1fc | 908 | break; |
24330d27 KP |
909 | case FcTypeMatrix: |
910 | switch (e->op) { | |
24330d27 KP |
911 | case FcOpTimes: |
912 | v.type = FcTypeMatrix; | |
913 | m = malloc (sizeof (FcMatrix)); | |
914 | if (m) | |
915 | { | |
916 | FcMemAlloc (FC_MEM_MATRIX, sizeof (FcMatrix)); | |
4262e0b3 PL |
917 | FcMatrixMultiply (m, vl.u.m, vr.u.m); |
918 | v.u.m = m; | |
24330d27 KP |
919 | } |
920 | else | |
921 | { | |
922 | v.type = FcTypeVoid; | |
923 | } | |
924 | break; | |
925 | default: | |
926 | v.type = FcTypeVoid; | |
927 | break; | |
928 | } | |
929 | break; | |
24330d27 KP |
930 | default: |
931 | v.type = FcTypeVoid; | |
932 | break; | |
933 | } | |
934 | } | |
935 | else | |
936 | v.type = FcTypeVoid; | |
937 | FcValueDestroy (vl); | |
938 | FcValueDestroy (vr); | |
939 | break; | |
940 | case FcOpNot: | |
941 | vl = FcConfigEvaluate (p, e->u.tree.left); | |
942 | switch (vl.type) { | |
943 | case FcTypeBool: | |
944 | v.type = FcTypeBool; | |
945 | v.u.b = !vl.u.b; | |
946 | break; | |
947 | default: | |
948 | v.type = FcTypeVoid; | |
949 | break; | |
950 | } | |
951 | FcValueDestroy (vl); | |
952 | break; | |
3f7653c2 KP |
953 | case FcOpFloor: |
954 | vl = FcConfigEvaluate (p, e->u.tree.left); | |
955 | switch (vl.type) { | |
956 | case FcTypeInteger: | |
957 | v = vl; | |
958 | break; | |
959 | case FcTypeDouble: | |
960 | v.type = FcTypeInteger; | |
961 | v.u.i = FcDoubleFloor (vl.u.d); | |
962 | break; | |
963 | default: | |
964 | v.type = FcTypeVoid; | |
965 | break; | |
966 | } | |
967 | FcValueDestroy (vl); | |
968 | break; | |
969 | case FcOpCeil: | |
970 | vl = FcConfigEvaluate (p, e->u.tree.left); | |
971 | switch (vl.type) { | |
972 | case FcTypeInteger: | |
973 | v = vl; | |
974 | break; | |
975 | case FcTypeDouble: | |
976 | v.type = FcTypeInteger; | |
977 | v.u.i = FcDoubleCeil (vl.u.d); | |
978 | break; | |
979 | default: | |
980 | v.type = FcTypeVoid; | |
981 | break; | |
982 | } | |
983 | FcValueDestroy (vl); | |
984 | break; | |
985 | case FcOpRound: | |
986 | vl = FcConfigEvaluate (p, e->u.tree.left); | |
987 | switch (vl.type) { | |
988 | case FcTypeInteger: | |
989 | v = vl; | |
990 | break; | |
991 | case FcTypeDouble: | |
992 | v.type = FcTypeInteger; | |
993 | v.u.i = FcDoubleRound (vl.u.d); | |
994 | break; | |
995 | default: | |
996 | v.type = FcTypeVoid; | |
997 | break; | |
998 | } | |
999 | FcValueDestroy (vl); | |
1000 | break; | |
1001 | case FcOpTrunc: | |
1002 | vl = FcConfigEvaluate (p, e->u.tree.left); | |
1003 | switch (vl.type) { | |
1004 | case FcTypeInteger: | |
1005 | v = vl; | |
1006 | break; | |
1007 | case FcTypeDouble: | |
1008 | v.type = FcTypeInteger; | |
1009 | v.u.i = FcDoubleTrunc (vl.u.d); | |
1010 | break; | |
1011 | default: | |
1012 | v.type = FcTypeVoid; | |
1013 | break; | |
1014 | } | |
1015 | FcValueDestroy (vl); | |
1016 | break; | |
24330d27 KP |
1017 | default: |
1018 | v.type = FcTypeVoid; | |
1019 | break; | |
1020 | } | |
1021 | return v; | |
1022 | } | |
1023 | ||
1024 | static FcValueList * | |
1025 | FcConfigMatchValueList (FcPattern *p, | |
1026 | FcTest *t, | |
f534109f | 1027 | FcValueList *values) |
24330d27 | 1028 | { |
179c3995 KP |
1029 | FcValueList *ret = 0; |
1030 | FcExpr *e = t->expr; | |
1031 | FcValue value; | |
f534109f | 1032 | FcValueList *v; |
24330d27 | 1033 | |
179c3995 | 1034 | while (e) |
24330d27 | 1035 | { |
74a623e0 | 1036 | /* Compute the value of the match expression */ |
179c3995 | 1037 | if (e->op == FcOpComma) |
24330d27 | 1038 | { |
179c3995 KP |
1039 | value = FcConfigEvaluate (p, e->u.tree.left); |
1040 | e = e->u.tree.right; | |
24330d27 KP |
1041 | } |
1042 | else | |
1043 | { | |
179c3995 KP |
1044 | value = FcConfigEvaluate (p, e); |
1045 | e = 0; | |
1046 | } | |
1047 | ||
cd2ec1a9 | 1048 | for (v = values; v; v = FcValueListPtrU(v->next)) |
179c3995 | 1049 | { |
74a623e0 | 1050 | /* Compare the pattern value to the match expression value */ |
4262e0b3 | 1051 | if (FcConfigCompareValue (&v->value, t->op, &value)) |
24330d27 | 1052 | { |
179c3995 KP |
1053 | if (!ret) |
1054 | ret = v; | |
1055 | } | |
1056 | else | |
1057 | { | |
1058 | if (t->qual == FcQualAll) | |
1059 | { | |
1060 | ret = 0; | |
1061 | break; | |
1062 | } | |
24330d27 KP |
1063 | } |
1064 | } | |
d0f07b8d | 1065 | FcValueDestroy (value); |
24330d27 | 1066 | } |
24330d27 KP |
1067 | return ret; |
1068 | } | |
1069 | ||
1070 | static FcValueList * | |
6fff2cda | 1071 | FcConfigValues (FcPattern *p, FcExpr *e, FcValueBinding binding) |
24330d27 KP |
1072 | { |
1073 | FcValueList *l; | |
cd2ec1a9 | 1074 | FcValueListPtr lp; |
24330d27 KP |
1075 | |
1076 | if (!e) | |
1077 | return 0; | |
1078 | l = (FcValueList *) malloc (sizeof (FcValueList)); | |
1079 | if (!l) | |
1080 | return 0; | |
1081 | FcMemAlloc (FC_MEM_VALLIST, sizeof (FcValueList)); | |
1082 | if (e->op == FcOpComma) | |
1083 | { | |
1084 | l->value = FcConfigEvaluate (p, e->u.tree.left); | |
cd2ec1a9 | 1085 | l->next = FcValueListPtrCreateDynamic(FcConfigValues (p, e->u.tree.right, binding)); |
24330d27 KP |
1086 | } |
1087 | else | |
1088 | { | |
1089 | l->value = FcConfigEvaluate (p, e); | |
cd2ec1a9 | 1090 | l->next = FcValueListPtrCreateDynamic(0); |
24330d27 | 1091 | } |
6fff2cda | 1092 | l->binding = binding; |
cd2ec1a9 PL |
1093 | lp = FcValueListPtrCreateDynamic(l); |
1094 | while (FcValueListPtrU(lp) && FcValueListPtrU(lp)->value.type == FcTypeVoid) | |
24330d27 | 1095 | { |
cd2ec1a9 PL |
1096 | FcValueListPtr next = FcValueListPtrU(lp)->next; |
1097 | ||
4262e0b3 | 1098 | if (lp.bank == FC_BANK_DYNAMIC) |
cd2ec1a9 PL |
1099 | { |
1100 | FcMemFree (FC_MEM_VALLIST, sizeof (FcValueList)); | |
1101 | free (l); | |
1102 | } | |
1103 | lp = next; | |
24330d27 KP |
1104 | } |
1105 | return l; | |
1106 | } | |
1107 | ||
1108 | static FcBool | |
cd2ec1a9 | 1109 | FcConfigAdd (FcValueListPtr *head, |
24330d27 KP |
1110 | FcValueList *position, |
1111 | FcBool append, | |
1112 | FcValueList *new) | |
1113 | { | |
cd2ec1a9 | 1114 | FcValueListPtr *prev, last, v; |
dda7794f | 1115 | FcValueBinding sameBinding; |
24330d27 | 1116 | |
dda7794f KP |
1117 | if (position) |
1118 | sameBinding = position->binding; | |
1119 | else | |
1120 | sameBinding = FcValueBindingWeak; | |
cd2ec1a9 PL |
1121 | for (v = FcValueListPtrCreateDynamic(new); FcValueListPtrU(v); |
1122 | v = FcValueListPtrU(v)->next) | |
1123 | if (FcValueListPtrU(v)->binding == FcValueBindingSame) | |
1124 | FcValueListPtrU(v)->binding = sameBinding; | |
24330d27 KP |
1125 | if (append) |
1126 | { | |
1127 | if (position) | |
1128 | prev = &position->next; | |
1129 | else | |
cd2ec1a9 PL |
1130 | for (prev = head; FcValueListPtrU(*prev); |
1131 | prev = &(FcValueListPtrU(*prev)->next)) | |
24330d27 KP |
1132 | ; |
1133 | } | |
1134 | else | |
1135 | { | |
1136 | if (position) | |
1137 | { | |
cd2ec1a9 PL |
1138 | for (prev = head; FcValueListPtrU(*prev); |
1139 | prev = &(FcValueListPtrU(*prev)->next)) | |
24330d27 | 1140 | { |
cd2ec1a9 | 1141 | if (FcValueListPtrU(*prev) == position) |
24330d27 KP |
1142 | break; |
1143 | } | |
1144 | } | |
1145 | else | |
1146 | prev = head; | |
1147 | ||
1148 | if (FcDebug () & FC_DBG_EDIT) | |
1149 | { | |
cd2ec1a9 | 1150 | if (!FcValueListPtrU(*prev)) |
24330d27 KP |
1151 | printf ("position not on list\n"); |
1152 | } | |
1153 | } | |
1154 | ||
1155 | if (FcDebug () & FC_DBG_EDIT) | |
1156 | { | |
1157 | printf ("%s list before ", append ? "Append" : "Prepend"); | |
1158 | FcValueListPrint (*head); | |
1159 | printf ("\n"); | |
1160 | } | |
1161 | ||
1162 | if (new) | |
1163 | { | |
cd2ec1a9 PL |
1164 | last = FcValueListPtrCreateDynamic(new); |
1165 | while (FcValueListPtrU(FcValueListPtrU(last)->next)) | |
1166 | last = FcValueListPtrU(last)->next; | |
24330d27 | 1167 | |
cd2ec1a9 PL |
1168 | FcValueListPtrU(last)->next = *prev; |
1169 | *prev = FcValueListPtrCreateDynamic(new); | |
24330d27 KP |
1170 | } |
1171 | ||
1172 | if (FcDebug () & FC_DBG_EDIT) | |
1173 | { | |
1174 | printf ("%s list after ", append ? "Append" : "Prepend"); | |
1175 | FcValueListPrint (*head); | |
1176 | printf ("\n"); | |
1177 | } | |
1178 | ||
1179 | return FcTrue; | |
1180 | } | |
1181 | ||
1182 | static void | |
cd2ec1a9 | 1183 | FcConfigDel (FcValueListPtr *head, |
24330d27 KP |
1184 | FcValueList *position) |
1185 | { | |
cd2ec1a9 | 1186 | FcValueListPtr *prev; |
24330d27 | 1187 | |
cd2ec1a9 PL |
1188 | for (prev = head; FcValueListPtrU(*prev); |
1189 | prev = &(FcValueListPtrU(*prev)->next)) | |
24330d27 | 1190 | { |
cd2ec1a9 | 1191 | if (FcValueListPtrU(*prev) == position) |
24330d27 KP |
1192 | { |
1193 | *prev = position->next; | |
cd2ec1a9 PL |
1194 | position->next = FcValueListPtrCreateDynamic(0); |
1195 | FcValueListDestroy (FcValueListPtrCreateDynamic(position)); | |
24330d27 KP |
1196 | break; |
1197 | } | |
1198 | } | |
1199 | } | |
1200 | ||
1201 | static void | |
1202 | FcConfigPatternAdd (FcPattern *p, | |
1203 | const char *object, | |
1204 | FcValueList *list, | |
1205 | FcBool append) | |
1206 | { | |
1207 | if (list) | |
1208 | { | |
e9be9cd1 | 1209 | FcPatternElt *e = FcPatternInsertElt (p, object); |
24330d27 KP |
1210 | |
1211 | if (!e) | |
1212 | return; | |
1213 | FcConfigAdd (&e->values, 0, append, list); | |
1214 | } | |
1215 | } | |
1216 | ||
1217 | /* | |
1218 | * Delete all values associated with a field | |
1219 | */ | |
1220 | static void | |
1221 | FcConfigPatternDel (FcPattern *p, | |
1222 | const char *object) | |
1223 | { | |
e9be9cd1 | 1224 | FcPatternElt *e = FcPatternFindElt (p, object); |
24330d27 KP |
1225 | if (!e) |
1226 | return; | |
cd2ec1a9 PL |
1227 | while (FcValueListPtrU(e->values)) |
1228 | FcConfigDel (&e->values, FcValueListPtrU(e->values)); | |
24330d27 KP |
1229 | } |
1230 | ||
1231 | static void | |
1232 | FcConfigPatternCanon (FcPattern *p, | |
1233 | const char *object) | |
1234 | { | |
e9be9cd1 | 1235 | FcPatternElt *e = FcPatternFindElt (p, object); |
24330d27 KP |
1236 | if (!e) |
1237 | return; | |
cd2ec1a9 | 1238 | if (!FcValueListPtrU(e->values)) |
24330d27 KP |
1239 | FcPatternDel (p, object); |
1240 | } | |
1241 | ||
1242 | FcBool | |
fa244f3d KP |
1243 | FcConfigSubstituteWithPat (FcConfig *config, |
1244 | FcPattern *p, | |
1245 | FcPattern *p_pat, | |
1246 | FcMatchKind kind) | |
24330d27 KP |
1247 | { |
1248 | FcSubst *s; | |
1249 | FcSubState *st; | |
1250 | int i; | |
1251 | FcTest *t; | |
1252 | FcEdit *e; | |
1253 | FcValueList *l; | |
938bc633 | 1254 | FcPattern *m; |
24330d27 KP |
1255 | |
1256 | if (!config) | |
1257 | { | |
1258 | config = FcConfigGetCurrent (); | |
1259 | if (!config) | |
1260 | return FcFalse; | |
1261 | } | |
1262 | ||
1263 | st = (FcSubState *) malloc (config->maxObjects * sizeof (FcSubState)); | |
1264 | if (!st && config->maxObjects) | |
1265 | return FcFalse; | |
1266 | FcMemAlloc (FC_MEM_SUBSTATE, config->maxObjects * sizeof (FcSubState)); | |
1267 | ||
1268 | if (FcDebug () & FC_DBG_EDIT) | |
1269 | { | |
1270 | printf ("FcConfigSubstitute "); | |
1271 | FcPatternPrint (p); | |
1272 | } | |
1273 | if (kind == FcMatchPattern) | |
1274 | s = config->substPattern; | |
1275 | else | |
1276 | s = config->substFont; | |
1277 | for (; s; s = s->next) | |
1278 | { | |
1279 | /* | |
1280 | * Check the tests to see if | |
1281 | * they all match the pattern | |
1282 | */ | |
1283 | for (t = s->test, i = 0; t; t = t->next, i++) | |
1284 | { | |
1285 | if (FcDebug () & FC_DBG_EDIT) | |
1286 | { | |
1287 | printf ("FcConfigSubstitute test "); | |
1288 | FcTestPrint (t); | |
1289 | } | |
938bc633 KP |
1290 | st[i].elt = 0; |
1291 | if (kind == FcMatchFont && t->kind == FcMatchPattern) | |
1292 | m = p_pat; | |
1293 | else | |
1294 | m = p; | |
1295 | if (m) | |
1296 | st[i].elt = FcPatternFindElt (m, t->field); | |
1297 | else | |
1298 | st[i].elt = 0; | |
24330d27 KP |
1299 | /* |
1300 | * If there's no such field in the font, | |
1301 | * then FcQualAll matches while FcQualAny does not | |
1302 | */ | |
1303 | if (!st[i].elt) | |
1304 | { | |
1305 | if (t->qual == FcQualAll) | |
1306 | { | |
1307 | st[i].value = 0; | |
1308 | continue; | |
1309 | } | |
1310 | else | |
1311 | break; | |
1312 | } | |
1313 | /* | |
1314 | * Check to see if there is a match, mark the location | |
1315 | * to apply match-relative edits | |
1316 | */ | |
cd2ec1a9 | 1317 | st[i].value = FcConfigMatchValueList (m, t, FcValueListPtrU(st[i].elt->values)); |
24330d27 KP |
1318 | if (!st[i].value) |
1319 | break; | |
cd2ec1a9 | 1320 | if (t->qual == FcQualFirst && st[i].value != FcValueListPtrU(st[i].elt->values)) |
6f6563ed | 1321 | break; |
cd2ec1a9 | 1322 | if (t->qual == FcQualNotFirst && st[i].value == FcValueListPtrU(st[i].elt->values)) |
6f6563ed | 1323 | break; |
24330d27 KP |
1324 | } |
1325 | if (t) | |
1326 | { | |
1327 | if (FcDebug () & FC_DBG_EDIT) | |
1328 | printf ("No match\n"); | |
1329 | continue; | |
1330 | } | |
1331 | if (FcDebug () & FC_DBG_EDIT) | |
1332 | { | |
1333 | printf ("Substitute "); | |
1334 | FcSubstPrint (s); | |
1335 | } | |
1336 | for (e = s->edit; e; e = e->next) | |
1337 | { | |
1338 | /* | |
1339 | * Evaluate the list of expressions | |
1340 | */ | |
6fff2cda | 1341 | l = FcConfigValues (p, e->expr, e->binding); |
24330d27 | 1342 | /* |
938bc633 KP |
1343 | * Locate any test associated with this field, skipping |
1344 | * tests associated with the pattern when substituting in | |
1345 | * the font | |
24330d27 KP |
1346 | */ |
1347 | for (t = s->test, i = 0; t; t = t->next, i++) | |
938bc633 KP |
1348 | { |
1349 | if ((t->kind == FcMatchFont || kind == FcMatchPattern) && | |
1350 | !FcStrCmpIgnoreCase ((FcChar8 *) t->field, | |
1351 | (FcChar8 *) e->field)) | |
432913ea | 1352 | { |
5f84b65a KP |
1353 | /* |
1354 | * KLUDGE - the pattern may have been reallocated or | |
1355 | * things may have been inserted or deleted above | |
1356 | * this element by other edits. Go back and find | |
1357 | * the element again | |
1358 | */ | |
1359 | if (e != s->edit && st[i].elt) | |
1360 | st[i].elt = FcPatternFindElt (p, t->field); | |
432913ea DD |
1361 | if (!st[i].elt) |
1362 | t = 0; | |
24330d27 | 1363 | break; |
432913ea | 1364 | } |
938bc633 | 1365 | } |
24330d27 KP |
1366 | switch (e->op) { |
1367 | case FcOpAssign: | |
1368 | /* | |
1369 | * If there was a test, then replace the matched | |
1370 | * value with the new list of values | |
1371 | */ | |
e9be9cd1 | 1372 | if (t) |
24330d27 KP |
1373 | { |
1374 | FcValueList *thisValue = st[i].value; | |
cd2ec1a9 | 1375 | FcValueList *nextValue = thisValue ? FcValueListPtrU(thisValue->next) : 0; |
24330d27 KP |
1376 | |
1377 | /* | |
1378 | * Append the new list of values after the current value | |
1379 | */ | |
1380 | FcConfigAdd (&st[i].elt->values, thisValue, FcTrue, l); | |
ccb3e93b KP |
1381 | /* |
1382 | * Delete the marked value | |
1383 | */ | |
1384 | FcConfigDel (&st[i].elt->values, thisValue); | |
24330d27 KP |
1385 | /* |
1386 | * Adjust any pointers into the value list to ensure | |
1387 | * future edits occur at the same place | |
1388 | */ | |
1389 | for (t = s->test, i = 0; t; t = t->next, i++) | |
1390 | { | |
1391 | if (st[i].value == thisValue) | |
1392 | st[i].value = nextValue; | |
1393 | } | |
24330d27 KP |
1394 | break; |
1395 | } | |
1396 | /* fall through ... */ | |
1397 | case FcOpAssignReplace: | |
1398 | /* | |
1399 | * Delete all of the values and insert | |
1400 | * the new set | |
1401 | */ | |
1402 | FcConfigPatternDel (p, e->field); | |
1403 | FcConfigPatternAdd (p, e->field, l, FcTrue); | |
1404 | /* | |
1405 | * Adjust any pointers into the value list as they no | |
1406 | * longer point to anything valid | |
1407 | */ | |
1408 | if (t) | |
1409 | { | |
1410 | FcPatternElt *thisElt = st[i].elt; | |
1411 | for (t = s->test, i = 0; t; t = t->next, i++) | |
1412 | { | |
1413 | if (st[i].elt == thisElt) | |
1414 | st[i].value = 0; | |
1415 | } | |
1416 | } | |
1417 | break; | |
1418 | case FcOpPrepend: | |
1419 | if (t) | |
1420 | { | |
1421 | FcConfigAdd (&st[i].elt->values, st[i].value, FcFalse, l); | |
1422 | break; | |
1423 | } | |
1424 | /* fall through ... */ | |
1425 | case FcOpPrependFirst: | |
1426 | FcConfigPatternAdd (p, e->field, l, FcFalse); | |
1427 | break; | |
1428 | case FcOpAppend: | |
1429 | if (t) | |
1430 | { | |
1431 | FcConfigAdd (&st[i].elt->values, st[i].value, FcTrue, l); | |
1432 | break; | |
1433 | } | |
1434 | /* fall through ... */ | |
1435 | case FcOpAppendLast: | |
1436 | FcConfigPatternAdd (p, e->field, l, FcTrue); | |
1437 | break; | |
1438 | default: | |
1439 | break; | |
1440 | } | |
1441 | } | |
1442 | /* | |
1443 | * Now go through the pattern and eliminate | |
1444 | * any properties without data | |
1445 | */ | |
1446 | for (e = s->edit; e; e = e->next) | |
1447 | FcConfigPatternCanon (p, e->field); | |
1448 | ||
1449 | if (FcDebug () & FC_DBG_EDIT) | |
1450 | { | |
1451 | printf ("FcConfigSubstitute edit"); | |
1452 | FcPatternPrint (p); | |
1453 | } | |
1454 | } | |
1455 | FcMemFree (FC_MEM_SUBSTATE, config->maxObjects * sizeof (FcSubState)); | |
1456 | free (st); | |
1457 | if (FcDebug () & FC_DBG_EDIT) | |
1458 | { | |
1459 | printf ("FcConfigSubstitute done"); | |
1460 | FcPatternPrint (p); | |
1461 | } | |
1462 | return FcTrue; | |
1463 | } | |
1464 | ||
fa244f3d KP |
1465 | FcBool |
1466 | FcConfigSubstitute (FcConfig *config, | |
1467 | FcPattern *p, | |
1468 | FcMatchKind kind) | |
1469 | { | |
1470 | return FcConfigSubstituteWithPat (config, p, 0, kind); | |
1471 | } | |
1472 | ||
e5206dbc | 1473 | #if defined (_WIN32) && (defined (PIC) || defined (DLL_EXPORT)) |
daeed6e0 TL |
1474 | |
1475 | static FcChar8 fontconfig_path[1000] = ""; | |
1476 | ||
1477 | BOOL WINAPI | |
1478 | DllMain (HINSTANCE hinstDLL, | |
1479 | DWORD fdwReason, | |
1480 | LPVOID lpvReserved) | |
1481 | { | |
1482 | FcChar8 *p; | |
1483 | ||
1484 | switch (fdwReason) { | |
1485 | case DLL_PROCESS_ATTACH: | |
1486 | if (!GetModuleFileName ((HMODULE) hinstDLL, fontconfig_path, | |
1487 | sizeof (fontconfig_path))) | |
1488 | break; | |
1489 | ||
1490 | /* If the fontconfig DLL is in a "bin" or "lib" subfolder, | |
1491 | * assume it's a Unix-style installation tree, and use | |
1492 | * "etc/fonts" in there as FONTCONFIG_PATH. Otherwise use the | |
1493 | * folder where the DLL is as FONTCONFIG_PATH. | |
1494 | */ | |
1495 | p = strrchr (fontconfig_path, '\\'); | |
1496 | if (p) | |
1497 | { | |
1498 | *p = '\0'; | |
1499 | p = strrchr (fontconfig_path, '\\'); | |
1500 | if (p && (FcStrCmpIgnoreCase (p + 1, "bin") == 0 || | |
1501 | FcStrCmpIgnoreCase (p + 1, "lib") == 0)) | |
1502 | *p = '\0'; | |
1503 | strcat (fontconfig_path, "\\etc\\fonts"); | |
1504 | } | |
1505 | else | |
1506 | fontconfig_path[0] = '\0'; | |
1507 | ||
1508 | break; | |
1509 | } | |
1510 | ||
1511 | return TRUE; | |
1512 | } | |
1513 | ||
1514 | #undef FONTCONFIG_PATH | |
1515 | #define FONTCONFIG_PATH fontconfig_path | |
1516 | ||
1517 | #else /* !(_WIN32 && PIC) */ | |
1518 | ||
daeed6e0 TL |
1519 | #endif /* !(_WIN32 && PIC) */ |
1520 | ||
24330d27 KP |
1521 | #ifndef FONTCONFIG_FILE |
1522 | #define FONTCONFIG_FILE "fonts.conf" | |
1523 | #endif | |
1524 | ||
ccb3e93b KP |
1525 | static FcChar8 * |
1526 | FcConfigFileExists (const FcChar8 *dir, const FcChar8 *file) | |
24330d27 | 1527 | { |
ccb3e93b | 1528 | FcChar8 *path; |
24330d27 KP |
1529 | |
1530 | if (!dir) | |
ccb3e93b KP |
1531 | dir = (FcChar8 *) ""; |
1532 | path = malloc (strlen ((char *) dir) + 1 + strlen ((char *) file) + 1); | |
24330d27 KP |
1533 | if (!path) |
1534 | return 0; | |
1535 | ||
9c8e07f1 | 1536 | strcpy ((char *) path, (const char *) dir); |
daeed6e0 TL |
1537 | /* make sure there's a single separator */ |
1538 | #ifdef _WIN32 | |
1539 | if ((!path[0] || (path[strlen((char *) path)-1] != '/' && | |
1540 | path[strlen((char *) path)-1] != '\\')) && | |
79da4fe9 TL |
1541 | !(file[0] == '/' || |
1542 | file[0] == '\\' || | |
1543 | (isalpha (file[0]) && file[1] == ':' && (file[2] == '/' || file[2] == '\\')))) | |
daeed6e0 TL |
1544 | strcat ((char *) path, "\\"); |
1545 | #else | |
ccb3e93b KP |
1546 | if ((!path[0] || path[strlen((char *) path)-1] != '/') && file[0] != '/') |
1547 | strcat ((char *) path, "/"); | |
daeed6e0 | 1548 | #endif |
ccb3e93b | 1549 | strcat ((char *) path, (char *) file); |
24330d27 | 1550 | |
9dac3c59 | 1551 | FcMemAlloc (FC_MEM_STRING, strlen ((char *) path) + 1); |
ccb3e93b | 1552 | if (access ((char *) path, R_OK) == 0) |
24330d27 KP |
1553 | return path; |
1554 | ||
9dac3c59 | 1555 | FcStrFree (path); |
24330d27 KP |
1556 | return 0; |
1557 | } | |
1558 | ||
ccb3e93b | 1559 | static FcChar8 ** |
24330d27 KP |
1560 | FcConfigGetPath (void) |
1561 | { | |
ccb3e93b KP |
1562 | FcChar8 **path; |
1563 | FcChar8 *env, *e, *colon; | |
1564 | FcChar8 *dir; | |
24330d27 KP |
1565 | int npath; |
1566 | int i; | |
1567 | ||
1568 | npath = 2; /* default dir + null */ | |
ccb3e93b | 1569 | env = (FcChar8 *) getenv ("FONTCONFIG_PATH"); |
24330d27 KP |
1570 | if (env) |
1571 | { | |
1572 | e = env; | |
1573 | npath++; | |
1574 | while (*e) | |
daeed6e0 | 1575 | if (*e++ == FC_SEARCH_PATH_SEPARATOR) |
24330d27 KP |
1576 | npath++; |
1577 | } | |
ccb3e93b | 1578 | path = calloc (npath, sizeof (FcChar8 *)); |
24330d27 KP |
1579 | if (!path) |
1580 | goto bail0; | |
1581 | i = 0; | |
1582 | ||
1583 | if (env) | |
1584 | { | |
1585 | e = env; | |
1586 | while (*e) | |
1587 | { | |
daeed6e0 | 1588 | colon = (FcChar8 *) strchr ((char *) e, FC_SEARCH_PATH_SEPARATOR); |
24330d27 | 1589 | if (!colon) |
ccb3e93b | 1590 | colon = e + strlen ((char *) e); |
24330d27 KP |
1591 | path[i] = malloc (colon - e + 1); |
1592 | if (!path[i]) | |
1593 | goto bail1; | |
9c8e07f1 | 1594 | strncpy ((char *) path[i], (const char *) e, colon - e); |
24330d27 KP |
1595 | path[i][colon - e] = '\0'; |
1596 | if (*colon) | |
1597 | e = colon + 1; | |
1598 | else | |
1599 | e = colon; | |
1600 | i++; | |
1601 | } | |
1602 | } | |
1603 | ||
ccb3e93b KP |
1604 | dir = (FcChar8 *) FONTCONFIG_PATH; |
1605 | path[i] = malloc (strlen ((char *) dir) + 1); | |
24330d27 KP |
1606 | if (!path[i]) |
1607 | goto bail1; | |
9c8e07f1 | 1608 | strcpy ((char *) path[i], (const char *) dir); |
24330d27 KP |
1609 | return path; |
1610 | ||
1611 | bail1: | |
1612 | for (i = 0; path[i]; i++) | |
1613 | free (path[i]); | |
1614 | free (path); | |
1615 | bail0: | |
1616 | return 0; | |
1617 | } | |
1618 | ||
1619 | static void | |
ccb3e93b | 1620 | FcConfigFreePath (FcChar8 **path) |
24330d27 | 1621 | { |
ccb3e93b | 1622 | FcChar8 **p; |
24330d27 KP |
1623 | |
1624 | for (p = path; *p; p++) | |
1625 | free (*p); | |
1626 | free (path); | |
1627 | } | |
1628 | ||
ff3f1f98 KP |
1629 | static FcBool _FcConfigHomeEnabled = FcTrue; |
1630 | ||
1631 | FcChar8 * | |
1632 | FcConfigHome (void) | |
1633 | { | |
1634 | if (_FcConfigHomeEnabled) | |
daeed6e0 TL |
1635 | { |
1636 | char *home = getenv ("HOME"); | |
1637 | ||
1638 | #ifdef _WIN32 | |
1639 | if (home == NULL) | |
1640 | home = getenv ("USERPROFILE"); | |
1641 | #endif | |
1642 | ||
1643 | return home; | |
1644 | } | |
ff3f1f98 KP |
1645 | return 0; |
1646 | } | |
1647 | ||
1648 | FcBool | |
1649 | FcConfigEnableHome (FcBool enable) | |
1650 | { | |
1651 | FcBool prev = _FcConfigHomeEnabled; | |
1652 | _FcConfigHomeEnabled = enable; | |
1653 | return prev; | |
1654 | } | |
1655 | ||
ccb3e93b KP |
1656 | FcChar8 * |
1657 | FcConfigFilename (const FcChar8 *url) | |
24330d27 | 1658 | { |
ccb3e93b | 1659 | FcChar8 *file, *dir, **path, **p; |
24330d27 KP |
1660 | |
1661 | if (!url || !*url) | |
1662 | { | |
ccb3e93b | 1663 | url = (FcChar8 *) getenv ("FONTCONFIG_FILE"); |
24330d27 | 1664 | if (!url) |
ccb3e93b | 1665 | url = (FcChar8 *) FONTCONFIG_FILE; |
24330d27 | 1666 | } |
ccb3e93b | 1667 | file = 0; |
daeed6e0 TL |
1668 | |
1669 | #ifdef _WIN32 | |
1670 | if (isalpha (*url) && | |
1671 | url[1] == ':' && | |
1672 | (url[2] == '/' || url[2] == '\\')) | |
1673 | goto absolute_path; | |
1674 | #endif | |
1675 | ||
24330d27 KP |
1676 | switch (*url) { |
1677 | case '~': | |
ff3f1f98 | 1678 | dir = FcConfigHome (); |
24330d27 KP |
1679 | if (dir) |
1680 | file = FcConfigFileExists (dir, url + 1); | |
1681 | else | |
1682 | file = 0; | |
1683 | break; | |
daeed6e0 TL |
1684 | #ifdef _WIN32 |
1685 | case '\\': | |
1686 | absolute_path: | |
1687 | #endif | |
24330d27 KP |
1688 | case '/': |
1689 | file = FcConfigFileExists (0, url); | |
1690 | break; | |
1691 | default: | |
1692 | path = FcConfigGetPath (); | |
1693 | if (!path) | |
1694 | return 0; | |
1695 | for (p = path; *p; p++) | |
1696 | { | |
1697 | file = FcConfigFileExists (*p, url); | |
1698 | if (file) | |
1699 | break; | |
1700 | } | |
1701 | FcConfigFreePath (path); | |
1702 | break; | |
1703 | } | |
1704 | return file; | |
1705 | } | |
1706 | ||
1707 | /* | |
1708 | * Manage the application-specific fonts | |
1709 | */ | |
1710 | ||
1711 | FcBool | |
1712 | FcConfigAppFontAddFile (FcConfig *config, | |
ccb3e93b | 1713 | const FcChar8 *file) |
24330d27 KP |
1714 | { |
1715 | FcFontSet *set; | |
179c3995 KP |
1716 | FcStrSet *subdirs; |
1717 | FcStrList *sublist; | |
1718 | FcChar8 *subdir; | |
24330d27 KP |
1719 | |
1720 | if (!config) | |
1721 | { | |
1722 | config = FcConfigGetCurrent (); | |
1723 | if (!config) | |
1724 | return FcFalse; | |
1725 | } | |
1726 | ||
179c3995 KP |
1727 | subdirs = FcStrSetCreate (); |
1728 | if (!subdirs) | |
1729 | return FcFalse; | |
1730 | ||
24330d27 KP |
1731 | set = FcConfigGetFonts (config, FcSetApplication); |
1732 | if (!set) | |
1733 | { | |
1734 | set = FcFontSetCreate (); | |
1735 | if (!set) | |
179c3995 KP |
1736 | { |
1737 | FcStrSetDestroy (subdirs); | |
24330d27 | 1738 | return FcFalse; |
179c3995 | 1739 | } |
24330d27 KP |
1740 | FcConfigSetFonts (config, set, FcSetApplication); |
1741 | } | |
179c3995 | 1742 | |
d47c9d6e | 1743 | if (!FcFileScanConfig (set, subdirs, 0, config->blanks, file, FcFalse, config)) |
179c3995 KP |
1744 | { |
1745 | FcStrSetDestroy (subdirs); | |
1746 | return FcFalse; | |
1747 | } | |
1748 | if ((sublist = FcStrListCreate (subdirs))) | |
1749 | { | |
1750 | while ((subdir = FcStrListNext (sublist))) | |
1751 | { | |
1752 | FcConfigAppFontAddDir (config, subdir); | |
1753 | } | |
1754 | FcStrListDone (sublist); | |
1755 | } | |
1756 | return FcTrue; | |
24330d27 KP |
1757 | } |
1758 | ||
1759 | FcBool | |
1760 | FcConfigAppFontAddDir (FcConfig *config, | |
ccb3e93b | 1761 | const FcChar8 *dir) |
24330d27 KP |
1762 | { |
1763 | FcFontSet *set; | |
179c3995 KP |
1764 | FcStrSet *subdirs; |
1765 | FcStrList *sublist; | |
1766 | FcChar8 *subdir; | |
24330d27 KP |
1767 | |
1768 | if (!config) | |
1769 | { | |
1770 | config = FcConfigGetCurrent (); | |
1771 | if (!config) | |
1772 | return FcFalse; | |
1773 | } | |
179c3995 KP |
1774 | subdirs = FcStrSetCreate (); |
1775 | if (!subdirs) | |
1776 | return FcFalse; | |
1777 | ||
24330d27 KP |
1778 | set = FcConfigGetFonts (config, FcSetApplication); |
1779 | if (!set) | |
1780 | { | |
1781 | set = FcFontSetCreate (); | |
1782 | if (!set) | |
179c3995 KP |
1783 | { |
1784 | FcStrSetDestroy (subdirs); | |
24330d27 | 1785 | return FcFalse; |
179c3995 | 1786 | } |
24330d27 KP |
1787 | FcConfigSetFonts (config, set, FcSetApplication); |
1788 | } | |
179c3995 | 1789 | |
d47c9d6e | 1790 | if (!FcDirScanConfig (set, subdirs, 0, config->blanks, dir, FcFalse, config)) |
179c3995 KP |
1791 | { |
1792 | FcStrSetDestroy (subdirs); | |
1793 | return FcFalse; | |
1794 | } | |
1795 | if ((sublist = FcStrListCreate (subdirs))) | |
1796 | { | |
1797 | while ((subdir = FcStrListNext (sublist))) | |
1798 | { | |
1799 | FcConfigAppFontAddDir (config, subdir); | |
1800 | } | |
1801 | FcStrListDone (sublist); | |
1802 | } | |
1803 | return FcTrue; | |
24330d27 KP |
1804 | } |
1805 | ||
1806 | void | |
1807 | FcConfigAppFontClear (FcConfig *config) | |
1808 | { | |
3ef32bcd MS |
1809 | if (!config) |
1810 | { | |
1811 | config = FcConfigGetCurrent (); | |
1812 | if (!config) | |
1813 | return; | |
1814 | } | |
1815 | ||
24330d27 KP |
1816 | FcConfigSetFonts (config, 0, FcSetApplication); |
1817 | } | |
d47c9d6e KP |
1818 | |
1819 | /* | |
1820 | * Manage filename-based font source selectors | |
1821 | */ | |
1822 | ||
1823 | FcBool | |
1824 | FcConfigGlobAdd (FcConfig *config, | |
1825 | const FcChar8 *glob, | |
1826 | FcBool accept) | |
1827 | { | |
1828 | FcStrSet *set = accept ? config->acceptGlobs : config->rejectGlobs; | |
1829 | ||
1830 | return FcStrSetAdd (set, glob); | |
1831 | } | |
1832 | ||
1833 | static FcBool | |
1834 | FcConfigGlobMatch (const FcChar8 *glob, | |
1835 | const FcChar8 *string) | |
1836 | { | |
1837 | FcChar8 c; | |
1838 | ||
1839 | while ((c = *glob++)) | |
1840 | { | |
1841 | switch (c) { | |
1842 | case '*': | |
1843 | /* short circuit common case */ | |
1844 | if (!*glob) | |
1845 | return FcTrue; | |
1846 | /* short circuit another common case */ | |
1847 | if (strchr ((char *) glob, '*') == 0) | |
1848 | string += strlen ((char *) string) - strlen ((char *) glob); | |
1849 | while (*string) | |
1850 | { | |
1851 | if (FcConfigGlobMatch (glob, string)) | |
1852 | return FcTrue; | |
1853 | string++; | |
1854 | } | |
1855 | return FcFalse; | |
1856 | case '?': | |
1857 | if (*string++ == '\0') | |
1858 | return FcFalse; | |
1859 | break; | |
1860 | default: | |
1861 | if (*string++ != c) | |
1862 | return FcFalse; | |
1863 | break; | |
1864 | } | |
1865 | } | |
1866 | return *string == '\0'; | |
1867 | } | |
1868 | ||
1869 | static FcBool | |
1870 | FcConfigGlobsMatch (const FcStrSet *globs, | |
1871 | const FcChar8 *string) | |
1872 | { | |
1873 | int i; | |
1874 | ||
1875 | for (i = 0; i < globs->num; i++) | |
4262e0b3 | 1876 | if (FcConfigGlobMatch (globs->strs[i], string)) |
d47c9d6e KP |
1877 | return FcTrue; |
1878 | return FcFalse; | |
1879 | } | |
1880 | ||
1881 | FcBool | |
1882 | FcConfigAcceptFilename (FcConfig *config, | |
1883 | const FcChar8 *filename) | |
1884 | { | |
1885 | if (FcConfigGlobsMatch (config->acceptGlobs, filename)) | |
1886 | return FcTrue; | |
1887 | if (FcConfigGlobsMatch (config->rejectGlobs, filename)) | |
1888 | return FcFalse; | |
1889 | return FcTrue; | |
1890 | } | |
4f27c1c0 KP |
1891 | |
1892 | /* | |
1893 | * Manage font-pattern based font source selectors | |
1894 | */ | |
1895 | ||
1896 | FcBool | |
1897 | FcConfigPatternsAdd (FcConfig *config, | |
1898 | FcPattern *pattern, | |
1899 | FcBool accept) | |
1900 | { | |
1901 | FcFontSet *set = accept ? config->acceptPatterns : config->rejectPatterns; | |
1902 | ||
1903 | return FcFontSetAdd (set, pattern); | |
1904 | } | |
1905 | ||
1906 | static FcBool | |
1907 | FcConfigPatternsMatch (const FcFontSet *patterns, | |
1908 | const FcPattern *font) | |
1909 | { | |
1910 | int i; | |
1911 | ||
1912 | for (i = 0; i < patterns->nfont; i++) | |
1913 | if (FcListPatternMatchAny (patterns->fonts[i], font)) | |
1914 | return FcTrue; | |
1915 | return FcFalse; | |
1916 | } | |
1917 | ||
1918 | FcBool | |
1919 | FcConfigAcceptFont (FcConfig *config, | |
1920 | const FcPattern *font) | |
1921 | { | |
1922 | if (FcConfigPatternsMatch (config->acceptPatterns, font)) | |
1923 | return FcTrue; | |
1924 | if (FcConfigPatternsMatch (config->rejectPatterns, font)) | |
1925 | return FcFalse; | |
1926 | return FcTrue; | |
1927 | } |