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