]> git.wh0rd.org - fontconfig.git/blob - src/fccfg.c
Rewrite global cache handling code in fontconfig to eliminate per-file
[fontconfig.git] / src / fccfg.c
1 /*
2 * $XFree86: xc/lib/fontconfig/src/fccfg.c,v 1.15 2002/06/21 06:14:45 keithp Exp $
3 *
4 * Copyright © 2000 Keith Packard, member of The XFree86 Project, Inc.
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 FcConfig *_fcConfig;
28
29 FcConfig *
30 FcConfigCreate (void)
31 {
32 FcSetName set;
33 FcConfig *config;
34
35 config = malloc (sizeof (FcConfig));
36 if (!config)
37 goto bail0;
38 FcMemAlloc (FC_MEM_CONFIG, sizeof (FcConfig));
39
40 config->configDirs = FcStrSetCreate ();
41 if (!config->configDirs)
42 goto bail1;
43
44 config->configFiles = FcStrSetCreate ();
45 if (!config->configFiles)
46 goto bail2;
47
48 config->fontDirs = FcStrSetCreate ();
49 if (!config->fontDirs)
50 goto bail3;
51
52 config->cache = 0;
53 if (!FcConfigSetCache (config, (FcChar8 *) ("~/" FC_USER_CACHE_FILE)))
54 goto bail4;
55
56 config->blanks = 0;
57
58 config->substPattern = 0;
59 config->substFont = 0;
60 config->maxObjects = 0;
61 for (set = FcSetSystem; set <= FcSetApplication; set++)
62 config->fonts[set] = 0;
63
64 config->rescanTime = time(0);
65 config->rescanInterval = 30;
66
67 return config;
68
69 bail4:
70 FcStrSetDestroy (config->fontDirs);
71 bail3:
72 FcStrSetDestroy (config->configFiles);
73 bail2:
74 FcStrSetDestroy (config->configDirs);
75 bail1:
76 free (config);
77 FcMemFree (FC_MEM_CONFIG, sizeof (FcConfig));
78 bail0:
79 return 0;
80 }
81
82 typedef struct _FcFileTime {
83 time_t time;
84 FcBool set;
85 } FcFileTime;
86
87 static FcFileTime
88 FcConfigNewestFile (FcStrSet *files)
89 {
90 FcStrList *list = FcStrListCreate (files);
91 FcFileTime newest = { 0, FcFalse };
92 FcChar8 *file;
93 struct stat statb;
94
95 if (list)
96 {
97 while ((file = FcStrListNext (list)))
98 if (stat ((char *) file, &statb) == 0)
99 if (!newest.set || statb.st_mtime - newest.time > 0)
100 newest.time = statb.st_mtime;
101 FcStrListDone (list);
102 }
103 return newest;
104 }
105
106 FcBool
107 FcConfigUptoDate (FcConfig *config)
108 {
109 FcFileTime config_time, font_time;
110 time_t now = time(0);
111 if (!config)
112 {
113 config = FcConfigGetCurrent ();
114 if (!config)
115 return FcFalse;
116 }
117 config_time = FcConfigNewestFile (config->configFiles);
118 font_time = FcConfigNewestFile (config->configDirs);
119 if ((config_time.set && config_time.time - config->rescanTime > 0) ||
120 (font_time.set && font_time.time - config->rescanTime) > 0)
121 {
122 return FcFalse;
123 }
124 config->rescanTime = now;
125 return FcTrue;
126 }
127
128 static void
129 FcSubstDestroy (FcSubst *s)
130 {
131 FcSubst *n;
132
133 while (s)
134 {
135 n = s->next;
136 FcTestDestroy (s->test);
137 FcEditDestroy (s->edit);
138 s = n;
139 }
140 }
141
142 void
143 FcConfigDestroy (FcConfig *config)
144 {
145 FcSetName set;
146
147 if (config == _fcConfig)
148 _fcConfig = 0;
149
150 FcStrSetDestroy (config->configDirs);
151 FcStrSetDestroy (config->fontDirs);
152 FcStrSetDestroy (config->configFiles);
153
154 FcStrFree (config->cache);
155
156 FcSubstDestroy (config->substPattern);
157 FcSubstDestroy (config->substFont);
158 for (set = FcSetSystem; set <= FcSetApplication; set++)
159 if (config->fonts[set])
160 FcFontSetDestroy (config->fonts[set]);
161 free (config);
162 FcMemFree (FC_MEM_CONFIG, sizeof (FcConfig));
163 }
164
165 /*
166 * Scan the current list of directories in the configuration
167 * and build the set of available fonts. Update the
168 * per-user cache file to reflect the new configuration
169 */
170
171 FcBool
172 FcConfigBuildFonts (FcConfig *config)
173 {
174 FcFontSet *fonts;
175 FcGlobalCache *cache;
176 FcStrList *list;
177 FcChar8 *dir;
178
179 fonts = FcFontSetCreate ();
180 if (!fonts)
181 goto bail0;
182
183 cache = FcGlobalCacheCreate ();
184 if (!cache)
185 goto bail1;
186
187 FcGlobalCacheLoad (cache, config->cache);
188
189 list = FcConfigGetFontDirs (config);
190 if (!list)
191 goto bail1;
192
193 while ((dir = FcStrListNext (list)))
194 {
195 if (FcDebug () & FC_DBG_FONTSET)
196 printf ("scan dir %s\n", dir);
197 FcDirScan (fonts, config->fontDirs, cache, config->blanks, dir, FcFalse);
198 }
199
200 FcStrListDone (list);
201
202 if (FcDebug () & FC_DBG_FONTSET)
203 FcFontSetPrint (fonts);
204
205 FcGlobalCacheSave (cache, config->cache);
206 FcGlobalCacheDestroy (cache);
207
208 FcConfigSetFonts (config, fonts, FcSetSystem);
209
210 return FcTrue;
211 bail1:
212 FcFontSetDestroy (fonts);
213 bail0:
214 return FcFalse;
215 }
216
217 FcBool
218 FcConfigSetCurrent (FcConfig *config)
219 {
220 if (!config->fonts)
221 if (!FcConfigBuildFonts (config))
222 return FcFalse;
223
224 if (_fcConfig)
225 FcConfigDestroy (_fcConfig);
226 _fcConfig = config;
227 return FcTrue;
228 }
229
230 FcConfig *
231 FcConfigGetCurrent (void)
232 {
233 if (!_fcConfig)
234 if (!FcInit ())
235 return 0;
236 return _fcConfig;
237 }
238
239 FcBool
240 FcConfigAddConfigDir (FcConfig *config,
241 const FcChar8 *d)
242 {
243 return FcStrSetAddFilename (config->configDirs, d);
244 }
245
246 FcStrList *
247 FcConfigGetConfigDirs (FcConfig *config)
248 {
249 if (!config)
250 {
251 config = FcConfigGetCurrent ();
252 if (!config)
253 return 0;
254 }
255 return FcStrListCreate (config->configDirs);
256 }
257
258 FcBool
259 FcConfigAddFontDir (FcConfig *config,
260 const FcChar8 *d)
261 {
262 return FcStrSetAddFilename (config->fontDirs, d);
263 }
264
265 FcBool
266 FcConfigAddDir (FcConfig *config,
267 const FcChar8 *d)
268 {
269 return (FcConfigAddConfigDir (config, d) &&
270 FcConfigAddFontDir (config, d));
271 }
272
273 FcStrList *
274 FcConfigGetFontDirs (FcConfig *config)
275 {
276 if (!config)
277 {
278 config = FcConfigGetCurrent ();
279 if (!config)
280 return 0;
281 }
282 return FcStrListCreate (config->fontDirs);
283 }
284
285 FcBool
286 FcConfigAddConfigFile (FcConfig *config,
287 const FcChar8 *f)
288 {
289 FcBool ret;
290 FcChar8 *file = FcConfigFilename (f);
291
292 if (!file)
293 return FcFalse;
294
295 ret = FcStrSetAdd (config->configFiles, file);
296 FcStrFree (file);
297 return ret;
298 }
299
300 FcStrList *
301 FcConfigGetConfigFiles (FcConfig *config)
302 {
303 if (!config)
304 {
305 config = FcConfigGetCurrent ();
306 if (!config)
307 return 0;
308 }
309 return FcStrListCreate (config->configFiles);
310 }
311
312 FcBool
313 FcConfigSetCache (FcConfig *config,
314 const FcChar8 *c)
315 {
316 FcChar8 *new = FcStrCopyFilename (c);
317
318 if (!new)
319 return FcFalse;
320 if (config->cache)
321 FcStrFree (config->cache);
322 config->cache = new;
323 return FcTrue;
324 }
325
326 FcChar8 *
327 FcConfigGetCache (FcConfig *config)
328 {
329 if (!config)
330 {
331 config = FcConfigGetCurrent ();
332 if (!config)
333 return 0;
334 }
335 return config->cache;
336 }
337
338 FcFontSet *
339 FcConfigGetFonts (FcConfig *config,
340 FcSetName set)
341 {
342 if (!config)
343 {
344 config = FcConfigGetCurrent ();
345 if (!config)
346 return 0;
347 }
348 return config->fonts[set];
349 }
350
351 void
352 FcConfigSetFonts (FcConfig *config,
353 FcFontSet *fonts,
354 FcSetName set)
355 {
356 if (config->fonts[set])
357 FcFontSetDestroy (config->fonts[set]);
358 config->fonts[set] = fonts;
359 }
360
361
362
363 FcBlanks *
364 FcConfigGetBlanks (FcConfig *config)
365 {
366 if (!config)
367 {
368 config = FcConfigGetCurrent ();
369 if (!config)
370 return 0;
371 }
372 return config->blanks;
373 }
374
375 FcBool
376 FcConfigAddBlank (FcConfig *config,
377 FcChar32 blank)
378 {
379 FcBlanks *b;
380
381 b = config->blanks;
382 if (!b)
383 {
384 b = FcBlanksCreate ();
385 if (!b)
386 return FcFalse;
387 }
388 if (!FcBlanksAdd (b, blank))
389 return FcFalse;
390 config->blanks = b;
391 return FcTrue;
392 }
393
394 int
395 FcConfigGetRescanInverval (FcConfig *config)
396 {
397 if (!config)
398 {
399 config = FcConfigGetCurrent ();
400 if (!config)
401 return 0;
402 }
403 return config->rescanInterval;
404 }
405
406 FcBool
407 FcConfigSetRescanInverval (FcConfig *config, int rescanInterval)
408 {
409 if (!config)
410 {
411 config = FcConfigGetCurrent ();
412 if (!config)
413 return FcFalse;
414 }
415 config->rescanInterval = rescanInterval;
416 return FcTrue;
417 }
418
419 FcBool
420 FcConfigAddEdit (FcConfig *config,
421 FcTest *test,
422 FcEdit *edit,
423 FcMatchKind kind)
424 {
425 FcSubst *subst, **prev;
426 FcTest *t;
427 int num;
428
429 subst = (FcSubst *) malloc (sizeof (FcSubst));
430 if (!subst)
431 return FcFalse;
432 if (kind == FcMatchPattern)
433 prev = &config->substPattern;
434 else
435 prev = &config->substFont;
436 for (; *prev; prev = &(*prev)->next);
437 *prev = subst;
438 subst->next = 0;
439 subst->test = test;
440 subst->edit = edit;
441 if (FcDebug () & FC_DBG_EDIT)
442 {
443 printf ("Add Subst ");
444 FcSubstPrint (subst);
445 }
446 num = 0;
447 for (t = test; t; t = t->next)
448 num++;
449 if (config->maxObjects < num)
450 config->maxObjects = num;
451 return FcTrue;
452 }
453
454 typedef struct _FcSubState {
455 FcPatternElt *elt;
456 FcValueList *value;
457 } FcSubState;
458
459 static FcValue
460 FcConfigPromote (FcValue v, FcValue u)
461 {
462 if (v.type == FcTypeInteger)
463 {
464 v.type = FcTypeDouble;
465 v.u.d = (double) v.u.i;
466 }
467 else if (v.type == FcTypeVoid && u.type == FcTypeMatrix)
468 {
469 v.u.m = &FcIdentityMatrix;
470 v.type = FcTypeMatrix;
471 }
472 return v;
473 }
474
475 FcBool
476 FcConfigCompareValue (FcValue m,
477 FcOp op,
478 FcValue v)
479 {
480 FcBool ret = FcFalse;
481
482 m = FcConfigPromote (m, v);
483 v = FcConfigPromote (v, m);
484 if (m.type == v.type)
485 {
486 ret = FcFalse;
487 switch (m.type) {
488 case FcTypeInteger:
489 break; /* FcConfigPromote prevents this from happening */
490 case FcTypeDouble:
491 switch (op) {
492 case FcOpEqual:
493 case FcOpContains:
494 ret = m.u.d == v.u.d;
495 break;
496 case FcOpNotEqual:
497 ret = m.u.d != v.u.d;
498 break;
499 case FcOpLess:
500 ret = m.u.d < v.u.d;
501 break;
502 case FcOpLessEqual:
503 ret = m.u.d <= v.u.d;
504 break;
505 case FcOpMore:
506 ret = m.u.d > v.u.d;
507 break;
508 case FcOpMoreEqual:
509 ret = m.u.d >= v.u.d;
510 break;
511 default:
512 break;
513 }
514 break;
515 case FcTypeBool:
516 switch (op) {
517 case FcOpEqual:
518 case FcOpContains:
519 ret = m.u.b == v.u.b;
520 break;
521 case FcOpNotEqual:
522 ret = m.u.b != v.u.b;
523 break;
524 default:
525 break;
526 }
527 break;
528 case FcTypeString:
529 switch (op) {
530 case FcOpEqual:
531 case FcOpContains:
532 ret = FcStrCmpIgnoreCase (m.u.s, v.u.s) == 0;
533 break;
534 case FcOpNotEqual:
535 ret = FcStrCmpIgnoreCase (m.u.s, v.u.s) != 0;
536 break;
537 default:
538 break;
539 }
540 break;
541 case FcTypeMatrix:
542 switch (op) {
543 case FcOpEqual:
544 case FcOpContains:
545 ret = FcMatrixEqual (m.u.m, v.u.m);
546 break;
547 case FcOpNotEqual:
548 ret = !FcMatrixEqual (m.u.m, v.u.m);
549 break;
550 default:
551 break;
552 }
553 break;
554 case FcTypeCharSet:
555 switch (op) {
556 case FcOpContains:
557 /* m contains v if v is a subset of m */
558 ret = FcCharSetIsSubset (v.u.c, m.u.c);
559 break;
560 case FcOpEqual:
561 ret = FcCharSetEqual (m.u.c, v.u.c);
562 break;
563 case FcOpNotEqual:
564 ret = !FcCharSetEqual (m.u.c, v.u.c);
565 break;
566 default:
567 break;
568 }
569 break;
570 case FcTypeVoid:
571 switch (op) {
572 case FcOpEqual:
573 case FcOpContains:
574 ret = FcTrue;
575 break;
576 default:
577 break;
578 }
579 break;
580 case FcTypeFTFace:
581 switch (op) {
582 case FcOpEqual:
583 ret = m.u.f == v.u.f;
584 break;
585 case FcOpNotEqual:
586 ret = m.u.f != v.u.f;
587 break;
588 default:
589 break;
590 }
591 }
592 }
593 else
594 {
595 if (op == FcOpNotEqual)
596 ret = FcTrue;
597 }
598 return ret;
599 }
600
601
602 static FcValue
603 FcConfigEvaluate (FcPattern *p, FcExpr *e)
604 {
605 FcValue v, vl, vr;
606 FcResult r;
607 FcMatrix *m;
608
609 switch (e->op) {
610 case FcOpInteger:
611 v.type = FcTypeInteger;
612 v.u.i = e->u.ival;
613 break;
614 case FcOpDouble:
615 v.type = FcTypeDouble;
616 v.u.d = e->u.dval;
617 break;
618 case FcOpString:
619 v.type = FcTypeString;
620 v.u.s = e->u.sval;
621 v = FcValueSave (v);
622 break;
623 case FcOpMatrix:
624 v.type = FcTypeMatrix;
625 v.u.m = e->u.mval;
626 v = FcValueSave (v);
627 break;
628 case FcOpCharSet:
629 v.type = FcTypeCharSet;
630 v.u.c = e->u.cval;
631 v = FcValueSave (v);
632 break;
633 case FcOpBool:
634 v.type = FcTypeBool;
635 v.u.b = e->u.bval;
636 break;
637 case FcOpField:
638 r = FcPatternGet (p, e->u.field, 0, &v);
639 if (r != FcResultMatch)
640 v.type = FcTypeVoid;
641 break;
642 case FcOpConst:
643 if (FcNameConstant (e->u.constant, &v.u.i))
644 v.type = FcTypeInteger;
645 else
646 v.type = FcTypeVoid;
647 break;
648 case FcOpQuest:
649 vl = FcConfigEvaluate (p, e->u.tree.left);
650 if (vl.type == FcTypeBool)
651 {
652 if (vl.u.b)
653 v = FcConfigEvaluate (p, e->u.tree.right->u.tree.left);
654 else
655 v = FcConfigEvaluate (p, e->u.tree.right->u.tree.right);
656 }
657 else
658 v.type = FcTypeVoid;
659 FcValueDestroy (vl);
660 break;
661 case FcOpOr:
662 case FcOpAnd:
663 case FcOpEqual:
664 case FcOpContains:
665 case FcOpNotEqual:
666 case FcOpLess:
667 case FcOpLessEqual:
668 case FcOpMore:
669 case FcOpMoreEqual:
670 case FcOpPlus:
671 case FcOpMinus:
672 case FcOpTimes:
673 case FcOpDivide:
674 vl = FcConfigEvaluate (p, e->u.tree.left);
675 vr = FcConfigEvaluate (p, e->u.tree.right);
676 vl = FcConfigPromote (vl, vr);
677 vr = FcConfigPromote (vr, vl);
678 if (vl.type == vr.type)
679 {
680 switch (vl.type) {
681 case FcTypeDouble:
682 switch (e->op) {
683 case FcOpPlus:
684 v.type = FcTypeDouble;
685 v.u.d = vl.u.d + vr.u.d;
686 break;
687 case FcOpMinus:
688 v.type = FcTypeDouble;
689 v.u.d = vl.u.d - vr.u.d;
690 break;
691 case FcOpTimes:
692 v.type = FcTypeDouble;
693 v.u.d = vl.u.d * vr.u.d;
694 break;
695 case FcOpDivide:
696 v.type = FcTypeDouble;
697 v.u.d = vl.u.d / vr.u.d;
698 break;
699 case FcOpEqual:
700 case FcOpContains:
701 v.type = FcTypeBool;
702 v.u.b = vl.u.d == vr.u.d;
703 break;
704 case FcOpNotEqual:
705 v.type = FcTypeBool;
706 v.u.b = vl.u.d != vr.u.d;
707 break;
708 case FcOpLess:
709 v.type = FcTypeBool;
710 v.u.b = vl.u.d < vr.u.d;
711 break;
712 case FcOpLessEqual:
713 v.type = FcTypeBool;
714 v.u.b = vl.u.d <= vr.u.d;
715 break;
716 case FcOpMore:
717 v.type = FcTypeBool;
718 v.u.b = vl.u.d > vr.u.d;
719 break;
720 case FcOpMoreEqual:
721 v.type = FcTypeBool;
722 v.u.b = vl.u.d >= vr.u.d;
723 break;
724 default:
725 v.type = FcTypeVoid;
726 break;
727 }
728 if (v.type == FcTypeDouble &&
729 v.u.d == (double) (int) v.u.d)
730 {
731 v.type = FcTypeInteger;
732 v.u.i = (int) v.u.d;
733 }
734 break;
735 case FcTypeBool:
736 switch (e->op) {
737 case FcOpOr:
738 v.type = FcTypeBool;
739 v.u.b = vl.u.b || vr.u.b;
740 break;
741 case FcOpAnd:
742 v.type = FcTypeBool;
743 v.u.b = vl.u.b && vr.u.b;
744 break;
745 case FcOpEqual:
746 case FcOpContains:
747 v.type = FcTypeBool;
748 v.u.b = vl.u.b == vr.u.b;
749 break;
750 case FcOpNotEqual:
751 v.type = FcTypeBool;
752 v.u.b = vl.u.b != vr.u.b;
753 break;
754 default:
755 v.type = FcTypeVoid;
756 break;
757 }
758 break;
759 case FcTypeString:
760 switch (e->op) {
761 case FcOpEqual:
762 case FcOpContains:
763 v.type = FcTypeBool;
764 v.u.b = FcStrCmpIgnoreCase (vl.u.s, vr.u.s) == 0;
765 break;
766 case FcOpNotEqual:
767 v.type = FcTypeBool;
768 v.u.b = FcStrCmpIgnoreCase (vl.u.s, vr.u.s) != 0;
769 break;
770 case FcOpPlus:
771 v.type = FcTypeString;
772 v.u.s = FcStrPlus (vl.u.s, vr.u.s);
773 if (!v.u.s)
774 v.type = FcTypeVoid;
775 break;
776 default:
777 v.type = FcTypeVoid;
778 break;
779 }
780 break;
781 case FcTypeMatrix:
782 switch (e->op) {
783 case FcOpEqual:
784 case FcOpContains:
785 v.type = FcTypeBool;
786 v.u.b = FcMatrixEqual (vl.u.m, vr.u.m);
787 break;
788 case FcOpNotEqual:
789 v.type = FcTypeBool;
790 v.u.b = FcMatrixEqual (vl.u.m, vr.u.m);
791 break;
792 case FcOpTimes:
793 v.type = FcTypeMatrix;
794 m = malloc (sizeof (FcMatrix));
795 if (m)
796 {
797 FcMemAlloc (FC_MEM_MATRIX, sizeof (FcMatrix));
798 FcMatrixMultiply (m, vl.u.m, vr.u.m);
799 v.u.m = m;
800 }
801 else
802 {
803 v.type = FcTypeVoid;
804 }
805 break;
806 default:
807 v.type = FcTypeVoid;
808 break;
809 }
810 break;
811 case FcTypeCharSet:
812 switch (e->op) {
813 case FcOpContains:
814 /* vl contains vr if vr is a subset of vl */
815 v.type = FcTypeBool;
816 v.u.b = FcCharSetIsSubset (vr.u.c, vl.u.c);
817 break;
818 case FcOpEqual:
819 v.type = FcTypeBool;
820 v.u.b = FcCharSetEqual (vl.u.c, vr.u.c);
821 break;
822 case FcOpNotEqual:
823 v.type = FcTypeBool;
824 v.u.b = !FcCharSetEqual (vl.u.c, vr.u.c);
825 break;
826 default:
827 v.type = FcTypeVoid;
828 break;
829 }
830 break;
831 default:
832 v.type = FcTypeVoid;
833 break;
834 }
835 }
836 else
837 v.type = FcTypeVoid;
838 FcValueDestroy (vl);
839 FcValueDestroy (vr);
840 break;
841 case FcOpNot:
842 vl = FcConfigEvaluate (p, e->u.tree.left);
843 switch (vl.type) {
844 case FcTypeBool:
845 v.type = FcTypeBool;
846 v.u.b = !vl.u.b;
847 break;
848 default:
849 v.type = FcTypeVoid;
850 break;
851 }
852 FcValueDestroy (vl);
853 break;
854 default:
855 v.type = FcTypeVoid;
856 break;
857 }
858 return v;
859 }
860
861 static FcValueList *
862 FcConfigMatchValueList (FcPattern *p,
863 FcTest *t,
864 FcValueList *values)
865 {
866 FcValueList *ret = 0;
867 FcExpr *e = t->expr;
868 FcValue value;
869 FcValueList *v;
870
871 while (e)
872 {
873 if (e->op == FcOpComma)
874 {
875 value = FcConfigEvaluate (p, e->u.tree.left);
876 e = e->u.tree.right;
877 }
878 else
879 {
880 value = FcConfigEvaluate (p, e);
881 e = 0;
882 }
883
884 for (v = values; v; v = v->next)
885 {
886 if (FcConfigCompareValue (v->value, t->op, value))
887 {
888 if (!ret)
889 ret = v;
890 }
891 else
892 {
893 if (t->qual == FcQualAll)
894 {
895 ret = 0;
896 break;
897 }
898 }
899 }
900 FcValueDestroy (value);
901 }
902 return ret;
903 }
904
905 static FcValueList *
906 FcConfigValues (FcPattern *p, FcExpr *e)
907 {
908 FcValueList *l;
909
910 if (!e)
911 return 0;
912 l = (FcValueList *) malloc (sizeof (FcValueList));
913 if (!l)
914 return 0;
915 FcMemAlloc (FC_MEM_VALLIST, sizeof (FcValueList));
916 if (e->op == FcOpComma)
917 {
918 l->value = FcConfigEvaluate (p, e->u.tree.left);
919 l->next = FcConfigValues (p, e->u.tree.right);
920 }
921 else
922 {
923 l->value = FcConfigEvaluate (p, e);
924 l->next = 0;
925 }
926 l->binding = FcValueBindingWeak;
927 while (l && l->value.type == FcTypeVoid)
928 {
929 FcValueList *next = l->next;
930
931 FcMemFree (FC_MEM_VALLIST, sizeof (FcValueList));
932 free (l);
933 l = next;
934 }
935 return l;
936 }
937
938 static FcBool
939 FcConfigAdd (FcValueList **head,
940 FcValueList *position,
941 FcBool append,
942 FcValueList *new)
943 {
944 FcValueList **prev, *last;
945
946 if (append)
947 {
948 if (position)
949 prev = &position->next;
950 else
951 for (prev = head; *prev; prev = &(*prev)->next)
952 ;
953 }
954 else
955 {
956 if (position)
957 {
958 for (prev = head; *prev; prev = &(*prev)->next)
959 {
960 if (*prev == position)
961 break;
962 }
963 }
964 else
965 prev = head;
966
967 if (FcDebug () & FC_DBG_EDIT)
968 {
969 if (!*prev)
970 printf ("position not on list\n");
971 }
972 }
973
974 if (FcDebug () & FC_DBG_EDIT)
975 {
976 printf ("%s list before ", append ? "Append" : "Prepend");
977 FcValueListPrint (*head);
978 printf ("\n");
979 }
980
981 if (new)
982 {
983 last = new;
984 while (last->next)
985 last = last->next;
986
987 last->next = *prev;
988 *prev = new;
989 }
990
991 if (FcDebug () & FC_DBG_EDIT)
992 {
993 printf ("%s list after ", append ? "Append" : "Prepend");
994 FcValueListPrint (*head);
995 printf ("\n");
996 }
997
998 return FcTrue;
999 }
1000
1001 static void
1002 FcConfigDel (FcValueList **head,
1003 FcValueList *position)
1004 {
1005 FcValueList **prev;
1006
1007 for (prev = head; *prev; prev = &(*prev)->next)
1008 {
1009 if (*prev == position)
1010 {
1011 *prev = position->next;
1012 position->next = 0;
1013 FcValueListDestroy (position);
1014 break;
1015 }
1016 }
1017 }
1018
1019 static void
1020 FcConfigPatternAdd (FcPattern *p,
1021 const char *object,
1022 FcValueList *list,
1023 FcBool append)
1024 {
1025 if (list)
1026 {
1027 FcPatternElt *e = FcPatternInsertElt (p, object);
1028
1029 if (!e)
1030 return;
1031 FcConfigAdd (&e->values, 0, append, list);
1032 }
1033 }
1034
1035 /*
1036 * Delete all values associated with a field
1037 */
1038 static void
1039 FcConfigPatternDel (FcPattern *p,
1040 const char *object)
1041 {
1042 FcPatternElt *e = FcPatternFindElt (p, object);
1043 if (!e)
1044 return;
1045 while (e->values)
1046 FcConfigDel (&e->values, e->values);
1047 }
1048
1049 static void
1050 FcConfigPatternCanon (FcPattern *p,
1051 const char *object)
1052 {
1053 FcPatternElt *e = FcPatternFindElt (p, object);
1054 if (!e)
1055 return;
1056 if (!e->values)
1057 FcPatternDel (p, object);
1058 }
1059
1060 FcBool
1061 FcConfigSubstitute (FcConfig *config,
1062 FcPattern *p,
1063 FcMatchKind kind)
1064 {
1065 FcSubst *s;
1066 FcSubState *st;
1067 int i;
1068 FcTest *t;
1069 FcEdit *e;
1070 FcValueList *l;
1071
1072 if (!config)
1073 {
1074 config = FcConfigGetCurrent ();
1075 if (!config)
1076 return FcFalse;
1077 }
1078
1079 st = (FcSubState *) malloc (config->maxObjects * sizeof (FcSubState));
1080 if (!st && config->maxObjects)
1081 return FcFalse;
1082 FcMemAlloc (FC_MEM_SUBSTATE, config->maxObjects * sizeof (FcSubState));
1083
1084 if (FcDebug () & FC_DBG_EDIT)
1085 {
1086 printf ("FcConfigSubstitute ");
1087 FcPatternPrint (p);
1088 }
1089 if (kind == FcMatchPattern)
1090 s = config->substPattern;
1091 else
1092 s = config->substFont;
1093 for (; s; s = s->next)
1094 {
1095 /*
1096 * Check the tests to see if
1097 * they all match the pattern
1098 */
1099 for (t = s->test, i = 0; t; t = t->next, i++)
1100 {
1101 if (FcDebug () & FC_DBG_EDIT)
1102 {
1103 printf ("FcConfigSubstitute test ");
1104 FcTestPrint (t);
1105 }
1106 st[i].elt = FcPatternFindElt (p, t->field);
1107 /*
1108 * If there's no such field in the font,
1109 * then FcQualAll matches while FcQualAny does not
1110 */
1111 if (!st[i].elt)
1112 {
1113 if (t->qual == FcQualAll)
1114 {
1115 st[i].value = 0;
1116 continue;
1117 }
1118 else
1119 break;
1120 }
1121 /*
1122 * Check to see if there is a match, mark the location
1123 * to apply match-relative edits
1124 */
1125 st[i].value = FcConfigMatchValueList (p, t, st[i].elt->values);
1126 if (!st[i].value)
1127 break;
1128 if (t->qual == FcQualFirst && st[i].value != st[i].elt->values)
1129 break;
1130 if (t->qual == FcQualNotFirst && st[i].value == st[i].elt->values)
1131 break;
1132 }
1133 if (t)
1134 {
1135 if (FcDebug () & FC_DBG_EDIT)
1136 printf ("No match\n");
1137 continue;
1138 }
1139 if (FcDebug () & FC_DBG_EDIT)
1140 {
1141 printf ("Substitute ");
1142 FcSubstPrint (s);
1143 }
1144 for (e = s->edit; e; e = e->next)
1145 {
1146 /*
1147 * Evaluate the list of expressions
1148 */
1149 l = FcConfigValues (p, e->expr);
1150 /*
1151 * Locate any test associated with this field
1152 */
1153 for (t = s->test, i = 0; t; t = t->next, i++)
1154 if (!FcStrCmpIgnoreCase ((FcChar8 *) t->field, (FcChar8 *) e->field))
1155 break;
1156 switch (e->op) {
1157 case FcOpAssign:
1158 /*
1159 * If there was a test, then replace the matched
1160 * value with the new list of values
1161 */
1162 if (t)
1163 {
1164 FcValueList *thisValue = st[i].value;
1165 FcValueList *nextValue = thisValue ? thisValue->next : 0;
1166
1167 /*
1168 * Append the new list of values after the current value
1169 */
1170 FcConfigAdd (&st[i].elt->values, thisValue, FcTrue, l);
1171 /*
1172 * Delete the marked value
1173 */
1174 FcConfigDel (&st[i].elt->values, thisValue);
1175 /*
1176 * Adjust any pointers into the value list to ensure
1177 * future edits occur at the same place
1178 */
1179 for (t = s->test, i = 0; t; t = t->next, i++)
1180 {
1181 if (st[i].value == thisValue)
1182 st[i].value = nextValue;
1183 }
1184 break;
1185 }
1186 /* fall through ... */
1187 case FcOpAssignReplace:
1188 /*
1189 * Delete all of the values and insert
1190 * the new set
1191 */
1192 FcConfigPatternDel (p, e->field);
1193 FcConfigPatternAdd (p, e->field, l, FcTrue);
1194 /*
1195 * Adjust any pointers into the value list as they no
1196 * longer point to anything valid
1197 */
1198 if (t)
1199 {
1200 FcPatternElt *thisElt = st[i].elt;
1201 for (t = s->test, i = 0; t; t = t->next, i++)
1202 {
1203 if (st[i].elt == thisElt)
1204 st[i].value = 0;
1205 }
1206 }
1207 break;
1208 case FcOpPrepend:
1209 if (t)
1210 {
1211 FcConfigAdd (&st[i].elt->values, st[i].value, FcFalse, l);
1212 break;
1213 }
1214 /* fall through ... */
1215 case FcOpPrependFirst:
1216 FcConfigPatternAdd (p, e->field, l, FcFalse);
1217 break;
1218 case FcOpAppend:
1219 if (t)
1220 {
1221 FcConfigAdd (&st[i].elt->values, st[i].value, FcTrue, l);
1222 break;
1223 }
1224 /* fall through ... */
1225 case FcOpAppendLast:
1226 FcConfigPatternAdd (p, e->field, l, FcTrue);
1227 break;
1228 default:
1229 break;
1230 }
1231 }
1232 /*
1233 * Now go through the pattern and eliminate
1234 * any properties without data
1235 */
1236 for (e = s->edit; e; e = e->next)
1237 FcConfigPatternCanon (p, e->field);
1238
1239 if (FcDebug () & FC_DBG_EDIT)
1240 {
1241 printf ("FcConfigSubstitute edit");
1242 FcPatternPrint (p);
1243 }
1244 }
1245 FcMemFree (FC_MEM_SUBSTATE, config->maxObjects * sizeof (FcSubState));
1246 free (st);
1247 if (FcDebug () & FC_DBG_EDIT)
1248 {
1249 printf ("FcConfigSubstitute done");
1250 FcPatternPrint (p);
1251 }
1252 return FcTrue;
1253 }
1254
1255 #ifndef FONTCONFIG_PATH
1256 #define FONTCONFIG_PATH "/etc/fonts"
1257 #endif
1258
1259 #ifndef FONTCONFIG_FILE
1260 #define FONTCONFIG_FILE "fonts.conf"
1261 #endif
1262
1263 static FcChar8 *
1264 FcConfigFileExists (const FcChar8 *dir, const FcChar8 *file)
1265 {
1266 FcChar8 *path;
1267
1268 if (!dir)
1269 dir = (FcChar8 *) "";
1270 path = malloc (strlen ((char *) dir) + 1 + strlen ((char *) file) + 1);
1271 if (!path)
1272 return 0;
1273
1274 strcpy ((char *) path, (const char *) dir);
1275 /* make sure there's a single separating / */
1276 if ((!path[0] || path[strlen((char *) path)-1] != '/') && file[0] != '/')
1277 strcat ((char *) path, "/");
1278 strcat ((char *) path, (char *) file);
1279
1280 if (access ((char *) path, R_OK) == 0)
1281 return path;
1282
1283 free (path);
1284 return 0;
1285 }
1286
1287 static FcChar8 **
1288 FcConfigGetPath (void)
1289 {
1290 FcChar8 **path;
1291 FcChar8 *env, *e, *colon;
1292 FcChar8 *dir;
1293 int npath;
1294 int i;
1295
1296 npath = 2; /* default dir + null */
1297 env = (FcChar8 *) getenv ("FONTCONFIG_PATH");
1298 if (env)
1299 {
1300 e = env;
1301 npath++;
1302 while (*e)
1303 if (*e++ == ':')
1304 npath++;
1305 }
1306 path = calloc (npath, sizeof (FcChar8 *));
1307 if (!path)
1308 goto bail0;
1309 i = 0;
1310
1311 if (env)
1312 {
1313 e = env;
1314 while (*e)
1315 {
1316 colon = (FcChar8 *) strchr ((char *) e, ':');
1317 if (!colon)
1318 colon = e + strlen ((char *) e);
1319 path[i] = malloc (colon - e + 1);
1320 if (!path[i])
1321 goto bail1;
1322 strncpy ((char *) path[i], (const char *) e, colon - e);
1323 path[i][colon - e] = '\0';
1324 if (*colon)
1325 e = colon + 1;
1326 else
1327 e = colon;
1328 i++;
1329 }
1330 }
1331
1332 dir = (FcChar8 *) FONTCONFIG_PATH;
1333 path[i] = malloc (strlen ((char *) dir) + 1);
1334 if (!path[i])
1335 goto bail1;
1336 strcpy ((char *) path[i], (const char *) dir);
1337 return path;
1338
1339 bail1:
1340 for (i = 0; path[i]; i++)
1341 free (path[i]);
1342 free (path);
1343 bail0:
1344 return 0;
1345 }
1346
1347 static void
1348 FcConfigFreePath (FcChar8 **path)
1349 {
1350 FcChar8 **p;
1351
1352 for (p = path; *p; p++)
1353 free (*p);
1354 free (path);
1355 }
1356
1357 FcChar8 *
1358 FcConfigFilename (const FcChar8 *url)
1359 {
1360 FcChar8 *file, *dir, **path, **p;
1361
1362 if (!url || !*url)
1363 {
1364 url = (FcChar8 *) getenv ("FONTCONFIG_FILE");
1365 if (!url)
1366 url = (FcChar8 *) FONTCONFIG_FILE;
1367 }
1368 file = 0;
1369 switch (*url) {
1370 case '~':
1371 dir = (FcChar8 *) getenv ("HOME");
1372 if (dir)
1373 file = FcConfigFileExists (dir, url + 1);
1374 else
1375 file = 0;
1376 break;
1377 case '/':
1378 file = FcConfigFileExists (0, url);
1379 break;
1380 default:
1381 path = FcConfigGetPath ();
1382 if (!path)
1383 return 0;
1384 for (p = path; *p; p++)
1385 {
1386 file = FcConfigFileExists (*p, url);
1387 if (file)
1388 break;
1389 }
1390 FcConfigFreePath (path);
1391 break;
1392 }
1393 return file;
1394 }
1395
1396 /*
1397 * Manage the application-specific fonts
1398 */
1399
1400 FcBool
1401 FcConfigAppFontAddFile (FcConfig *config,
1402 const FcChar8 *file)
1403 {
1404 FcFontSet *set;
1405 FcStrSet *subdirs;
1406 FcStrList *sublist;
1407 FcChar8 *subdir;
1408
1409 if (!config)
1410 {
1411 config = FcConfigGetCurrent ();
1412 if (!config)
1413 return FcFalse;
1414 }
1415
1416 subdirs = FcStrSetCreate ();
1417 if (!subdirs)
1418 return FcFalse;
1419
1420 set = FcConfigGetFonts (config, FcSetApplication);
1421 if (!set)
1422 {
1423 set = FcFontSetCreate ();
1424 if (!set)
1425 {
1426 FcStrSetDestroy (subdirs);
1427 return FcFalse;
1428 }
1429 FcConfigSetFonts (config, set, FcSetApplication);
1430 }
1431
1432 if (!FcFileScan (set, subdirs, 0, config->blanks, file, FcFalse))
1433 {
1434 FcStrSetDestroy (subdirs);
1435 return FcFalse;
1436 }
1437 if ((sublist = FcStrListCreate (subdirs)))
1438 {
1439 while ((subdir = FcStrListNext (sublist)))
1440 {
1441 FcConfigAppFontAddDir (config, subdir);
1442 }
1443 FcStrListDone (sublist);
1444 }
1445 return FcTrue;
1446 }
1447
1448 FcBool
1449 FcConfigAppFontAddDir (FcConfig *config,
1450 const FcChar8 *dir)
1451 {
1452 FcFontSet *set;
1453 FcStrSet *subdirs;
1454 FcStrList *sublist;
1455 FcChar8 *subdir;
1456
1457 if (!config)
1458 {
1459 config = FcConfigGetCurrent ();
1460 if (!config)
1461 return FcFalse;
1462 }
1463 subdirs = FcStrSetCreate ();
1464 if (!subdirs)
1465 return FcFalse;
1466
1467 set = FcConfigGetFonts (config, FcSetApplication);
1468 if (!set)
1469 {
1470 set = FcFontSetCreate ();
1471 if (!set)
1472 {
1473 FcStrSetDestroy (subdirs);
1474 return FcFalse;
1475 }
1476 FcConfigSetFonts (config, set, FcSetApplication);
1477 }
1478
1479 if (!FcDirScan (set, subdirs, 0, config->blanks, dir, FcFalse))
1480 {
1481 FcStrSetDestroy (subdirs);
1482 return FcFalse;
1483 }
1484 if ((sublist = FcStrListCreate (subdirs)))
1485 {
1486 while ((subdir = FcStrListNext (sublist)))
1487 {
1488 FcConfigAppFontAddDir (config, subdir);
1489 }
1490 FcStrListDone (sublist);
1491 }
1492 return FcTrue;
1493 }
1494
1495 void
1496 FcConfigAppFontClear (FcConfig *config)
1497 {
1498 FcConfigSetFonts (config, 0, FcSetApplication);
1499 }