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