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