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