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