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