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