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