]> git.wh0rd.org - fontconfig.git/blob - src/fccfg.c
Fix automatic file time checking, transcoding table searches. Actually add
[fontconfig.git] / src / fccfg.c
1 /*
2 * $XFree86: xc/lib/fontconfig/src/fccfg.c,v 1.14 2002/06/20 03:43:09 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 FcFileCache *cache;
176 FcStrList *list;
177 FcChar8 *dir;
178
179 fonts = FcFontSetCreate ();
180 if (!fonts)
181 goto bail0;
182
183 cache = FcFileCacheCreate ();
184 if (!cache)
185 goto bail1;
186
187 FcFileCacheLoad (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 FcFileCacheSave (cache, config->cache);
206 FcFileCacheDestroy (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 const FcMatrix FcIdentityMatrix = { 1, 0, 0, 1 };
460
461 static FcValue
462 FcConfigPromote (FcValue v, FcValue u)
463 {
464 if (v.type == FcTypeInteger)
465 {
466 v.type = FcTypeDouble;
467 v.u.d = (double) v.u.i;
468 }
469 else if (v.type == FcTypeVoid && u.type == FcTypeMatrix)
470 {
471 v.u.m = FcMatrixCopy (&FcIdentityMatrix);
472 if (v.u.m)
473 v.type = FcTypeMatrix;
474 }
475 return v;
476 }
477
478 FcBool
479 FcConfigCompareValue (FcValue m,
480 FcOp op,
481 FcValue v)
482 {
483 FcBool ret = FcFalse;
484
485 m = FcConfigPromote (m, v);
486 v = FcConfigPromote (v, m);
487 if (m.type == v.type)
488 {
489 ret = FcFalse;
490 switch (m.type) {
491 case FcTypeInteger:
492 break; /* FcConfigPromote prevents this from happening */
493 case FcTypeDouble:
494 switch (op) {
495 case FcOpEqual:
496 case FcOpContains:
497 ret = m.u.d == v.u.d;
498 break;
499 case FcOpNotEqual:
500 ret = m.u.d != v.u.d;
501 break;
502 case FcOpLess:
503 ret = m.u.d < v.u.d;
504 break;
505 case FcOpLessEqual:
506 ret = m.u.d <= v.u.d;
507 break;
508 case FcOpMore:
509 ret = m.u.d > v.u.d;
510 break;
511 case FcOpMoreEqual:
512 ret = m.u.d >= v.u.d;
513 break;
514 default:
515 break;
516 }
517 break;
518 case FcTypeBool:
519 switch (op) {
520 case FcOpEqual:
521 case FcOpContains:
522 ret = m.u.b == v.u.b;
523 break;
524 case FcOpNotEqual:
525 ret = m.u.b != v.u.b;
526 break;
527 default:
528 break;
529 }
530 break;
531 case FcTypeString:
532 switch (op) {
533 case FcOpEqual:
534 case FcOpContains:
535 ret = FcStrCmpIgnoreCase (m.u.s, v.u.s) == 0;
536 break;
537 case FcOpNotEqual:
538 ret = FcStrCmpIgnoreCase (m.u.s, v.u.s) != 0;
539 break;
540 default:
541 break;
542 }
543 break;
544 case FcTypeMatrix:
545 switch (op) {
546 case FcOpEqual:
547 case FcOpContains:
548 ret = FcMatrixEqual (m.u.m, v.u.m);
549 break;
550 case FcOpNotEqual:
551 ret = !FcMatrixEqual (m.u.m, v.u.m);
552 break;
553 default:
554 break;
555 }
556 break;
557 case FcTypeCharSet:
558 switch (op) {
559 case FcOpContains:
560 /* m contains v if v is a subset of m */
561 ret = FcCharSetIsSubset (v.u.c, m.u.c);
562 break;
563 case FcOpEqual:
564 ret = FcCharSetEqual (m.u.c, v.u.c);
565 break;
566 case FcOpNotEqual:
567 ret = !FcCharSetEqual (m.u.c, v.u.c);
568 break;
569 default:
570 break;
571 }
572 break;
573 case FcTypeVoid:
574 switch (op) {
575 case FcOpEqual:
576 case FcOpContains:
577 ret = FcTrue;
578 break;
579 default:
580 break;
581 }
582 break;
583 case FcTypeFTFace:
584 switch (op) {
585 case FcOpEqual:
586 ret = m.u.f == v.u.f;
587 break;
588 case FcOpNotEqual:
589 ret = m.u.f != v.u.f;
590 break;
591 default:
592 break;
593 }
594 }
595 }
596 else
597 {
598 if (op == FcOpNotEqual)
599 ret = FcTrue;
600 }
601 return ret;
602 }
603
604
605 static FcValue
606 FcConfigEvaluate (FcPattern *p, FcExpr *e)
607 {
608 FcValue v, vl, vr;
609 FcResult r;
610 FcMatrix *m;
611
612 switch (e->op) {
613 case FcOpInteger:
614 v.type = FcTypeInteger;
615 v.u.i = e->u.ival;
616 break;
617 case FcOpDouble:
618 v.type = FcTypeDouble;
619 v.u.d = e->u.dval;
620 break;
621 case FcOpString:
622 v.type = FcTypeString;
623 v.u.s = e->u.sval;
624 v = FcValueSave (v);
625 break;
626 case FcOpMatrix:
627 v.type = FcTypeMatrix;
628 v.u.m = e->u.mval;
629 v = FcValueSave (v);
630 break;
631 case FcOpCharSet:
632 v.type = FcTypeCharSet;
633 v.u.c = e->u.cval;
634 v = FcValueSave (v);
635 break;
636 case FcOpBool:
637 v.type = FcTypeBool;
638 v.u.b = e->u.bval;
639 break;
640 case FcOpField:
641 r = FcPatternGet (p, e->u.field, 0, &v);
642 if (r != FcResultMatch)
643 v.type = FcTypeVoid;
644 break;
645 case FcOpConst:
646 if (FcNameConstant (e->u.constant, &v.u.i))
647 v.type = FcTypeInteger;
648 else
649 v.type = FcTypeVoid;
650 break;
651 case FcOpQuest:
652 vl = FcConfigEvaluate (p, e->u.tree.left);
653 if (vl.type == FcTypeBool)
654 {
655 if (vl.u.b)
656 v = FcConfigEvaluate (p, e->u.tree.right->u.tree.left);
657 else
658 v = FcConfigEvaluate (p, e->u.tree.right->u.tree.right);
659 }
660 else
661 v.type = FcTypeVoid;
662 FcValueDestroy (vl);
663 break;
664 case FcOpOr:
665 case FcOpAnd:
666 case FcOpEqual:
667 case FcOpContains:
668 case FcOpNotEqual:
669 case FcOpLess:
670 case FcOpLessEqual:
671 case FcOpMore:
672 case FcOpMoreEqual:
673 case FcOpPlus:
674 case FcOpMinus:
675 case FcOpTimes:
676 case FcOpDivide:
677 vl = FcConfigEvaluate (p, e->u.tree.left);
678 vr = FcConfigEvaluate (p, e->u.tree.right);
679 vl = FcConfigPromote (vl, vr);
680 vr = FcConfigPromote (vr, vl);
681 if (vl.type == vr.type)
682 {
683 switch (vl.type) {
684 case FcTypeDouble:
685 switch (e->op) {
686 case FcOpPlus:
687 v.type = FcTypeDouble;
688 v.u.d = vl.u.d + vr.u.d;
689 break;
690 case FcOpMinus:
691 v.type = FcTypeDouble;
692 v.u.d = vl.u.d - vr.u.d;
693 break;
694 case FcOpTimes:
695 v.type = FcTypeDouble;
696 v.u.d = vl.u.d * vr.u.d;
697 break;
698 case FcOpDivide:
699 v.type = FcTypeDouble;
700 v.u.d = vl.u.d / vr.u.d;
701 break;
702 case FcOpEqual:
703 case FcOpContains:
704 v.type = FcTypeBool;
705 v.u.b = vl.u.d == vr.u.d;
706 break;
707 case FcOpNotEqual:
708 v.type = FcTypeBool;
709 v.u.b = vl.u.d != vr.u.d;
710 break;
711 case FcOpLess:
712 v.type = FcTypeBool;
713 v.u.b = vl.u.d < vr.u.d;
714 break;
715 case FcOpLessEqual:
716 v.type = FcTypeBool;
717 v.u.b = vl.u.d <= vr.u.d;
718 break;
719 case FcOpMore:
720 v.type = FcTypeBool;
721 v.u.b = vl.u.d > vr.u.d;
722 break;
723 case FcOpMoreEqual:
724 v.type = FcTypeBool;
725 v.u.b = vl.u.d >= vr.u.d;
726 break;
727 default:
728 v.type = FcTypeVoid;
729 break;
730 }
731 if (v.type == FcTypeDouble &&
732 v.u.d == (double) (int) v.u.d)
733 {
734 v.type = FcTypeInteger;
735 v.u.i = (int) v.u.d;
736 }
737 break;
738 case FcTypeBool:
739 switch (e->op) {
740 case FcOpOr:
741 v.type = FcTypeBool;
742 v.u.b = vl.u.b || vr.u.b;
743 break;
744 case FcOpAnd:
745 v.type = FcTypeBool;
746 v.u.b = vl.u.b && vr.u.b;
747 break;
748 case FcOpEqual:
749 case FcOpContains:
750 v.type = FcTypeBool;
751 v.u.b = vl.u.b == vr.u.b;
752 break;
753 case FcOpNotEqual:
754 v.type = FcTypeBool;
755 v.u.b = vl.u.b != vr.u.b;
756 break;
757 default:
758 v.type = FcTypeVoid;
759 break;
760 }
761 break;
762 case FcTypeString:
763 switch (e->op) {
764 case FcOpEqual:
765 case FcOpContains:
766 v.type = FcTypeBool;
767 v.u.b = FcStrCmpIgnoreCase (vl.u.s, vr.u.s) == 0;
768 break;
769 case FcOpNotEqual:
770 v.type = FcTypeBool;
771 v.u.b = FcStrCmpIgnoreCase (vl.u.s, vr.u.s) != 0;
772 break;
773 case FcOpPlus:
774 v.type = FcTypeString;
775 v.u.s = FcStrPlus (vl.u.s, vr.u.s);
776 if (!v.u.s)
777 v.type = FcTypeVoid;
778 break;
779 default:
780 v.type = FcTypeVoid;
781 break;
782 }
783 break;
784 case FcTypeMatrix:
785 switch (e->op) {
786 case FcOpEqual:
787 case FcOpContains:
788 v.type = FcTypeBool;
789 v.u.b = FcMatrixEqual (vl.u.m, vr.u.m);
790 break;
791 case FcOpNotEqual:
792 v.type = FcTypeBool;
793 v.u.b = FcMatrixEqual (vl.u.m, vr.u.m);
794 break;
795 case FcOpTimes:
796 v.type = FcTypeMatrix;
797 m = malloc (sizeof (FcMatrix));
798 if (m)
799 {
800 FcMemAlloc (FC_MEM_MATRIX, sizeof (FcMatrix));
801 FcMatrixMultiply (m, vl.u.m, vr.u.m);
802 v.u.m = m;
803 }
804 else
805 {
806 v.type = FcTypeVoid;
807 }
808 break;
809 default:
810 v.type = FcTypeVoid;
811 break;
812 }
813 break;
814 case FcTypeCharSet:
815 switch (e->op) {
816 case FcOpContains:
817 /* vl contains vr if vr is a subset of vl */
818 v.type = FcTypeBool;
819 v.u.b = FcCharSetIsSubset (vr.u.c, vl.u.c);
820 break;
821 case FcOpEqual:
822 v.type = FcTypeBool;
823 v.u.b = FcCharSetEqual (vl.u.c, vr.u.c);
824 break;
825 case FcOpNotEqual:
826 v.type = FcTypeBool;
827 v.u.b = !FcCharSetEqual (vl.u.c, vr.u.c);
828 break;
829 default:
830 v.type = FcTypeVoid;
831 break;
832 }
833 break;
834 default:
835 v.type = FcTypeVoid;
836 break;
837 }
838 }
839 else
840 v.type = FcTypeVoid;
841 FcValueDestroy (vl);
842 FcValueDestroy (vr);
843 break;
844 case FcOpNot:
845 vl = FcConfigEvaluate (p, e->u.tree.left);
846 switch (vl.type) {
847 case FcTypeBool:
848 v.type = FcTypeBool;
849 v.u.b = !vl.u.b;
850 break;
851 default:
852 v.type = FcTypeVoid;
853 break;
854 }
855 FcValueDestroy (vl);
856 break;
857 default:
858 v.type = FcTypeVoid;
859 break;
860 }
861 return v;
862 }
863
864 static FcValueList *
865 FcConfigMatchValueList (FcPattern *p,
866 FcTest *t,
867 FcValueList *values)
868 {
869 FcValueList *ret = 0;
870 FcExpr *e = t->expr;
871 FcValue value;
872 FcValueList *v;
873
874 while (e)
875 {
876 if (e->op == FcOpComma)
877 {
878 value = FcConfigEvaluate (p, e->u.tree.left);
879 e = e->u.tree.right;
880 }
881 else
882 {
883 value = FcConfigEvaluate (p, e);
884 e = 0;
885 }
886
887 for (v = values; v; v = v->next)
888 {
889 if (FcConfigCompareValue (v->value, t->op, value))
890 {
891 if (!ret)
892 ret = v;
893 }
894 else
895 {
896 if (t->qual == FcQualAll)
897 {
898 ret = 0;
899 break;
900 }
901 }
902 }
903 FcValueDestroy (value);
904 }
905 return ret;
906 }
907
908 static FcValueList *
909 FcConfigValues (FcPattern *p, FcExpr *e)
910 {
911 FcValueList *l;
912
913 if (!e)
914 return 0;
915 l = (FcValueList *) malloc (sizeof (FcValueList));
916 if (!l)
917 return 0;
918 FcMemAlloc (FC_MEM_VALLIST, sizeof (FcValueList));
919 if (e->op == FcOpComma)
920 {
921 l->value = FcConfigEvaluate (p, e->u.tree.left);
922 l->next = FcConfigValues (p, e->u.tree.right);
923 }
924 else
925 {
926 l->value = FcConfigEvaluate (p, e);
927 l->next = 0;
928 }
929 while (l && l->value.type == FcTypeVoid)
930 {
931 FcValueList *next = l->next;
932
933 FcMemFree (FC_MEM_VALLIST, sizeof (FcValueList));
934 free (l);
935 l = next;
936 }
937 return l;
938 }
939
940 static FcBool
941 FcConfigAdd (FcValueList **head,
942 FcValueList *position,
943 FcBool append,
944 FcValueList *new)
945 {
946 FcValueList **prev, *last;
947
948 if (append)
949 {
950 if (position)
951 prev = &position->next;
952 else
953 for (prev = head; *prev; prev = &(*prev)->next)
954 ;
955 }
956 else
957 {
958 if (position)
959 {
960 for (prev = head; *prev; prev = &(*prev)->next)
961 {
962 if (*prev == position)
963 break;
964 }
965 }
966 else
967 prev = head;
968
969 if (FcDebug () & FC_DBG_EDIT)
970 {
971 if (!*prev)
972 printf ("position not on list\n");
973 }
974 }
975
976 if (FcDebug () & FC_DBG_EDIT)
977 {
978 printf ("%s list before ", append ? "Append" : "Prepend");
979 FcValueListPrint (*head);
980 printf ("\n");
981 }
982
983 if (new)
984 {
985 last = new;
986 while (last->next)
987 last = last->next;
988
989 last->next = *prev;
990 *prev = new;
991 }
992
993 if (FcDebug () & FC_DBG_EDIT)
994 {
995 printf ("%s list after ", append ? "Append" : "Prepend");
996 FcValueListPrint (*head);
997 printf ("\n");
998 }
999
1000 return FcTrue;
1001 }
1002
1003 static void
1004 FcConfigDel (FcValueList **head,
1005 FcValueList *position)
1006 {
1007 FcValueList **prev;
1008
1009 for (prev = head; *prev; prev = &(*prev)->next)
1010 {
1011 if (*prev == position)
1012 {
1013 *prev = position->next;
1014 position->next = 0;
1015 FcValueListDestroy (position);
1016 break;
1017 }
1018 }
1019 }
1020
1021 static void
1022 FcConfigPatternAdd (FcPattern *p,
1023 const char *object,
1024 FcValueList *list,
1025 FcBool append)
1026 {
1027 if (list)
1028 {
1029 FcPatternElt *e = FcPatternInsertElt (p, object);
1030
1031 if (!e)
1032 return;
1033 FcConfigAdd (&e->values, 0, append, list);
1034 }
1035 }
1036
1037 /*
1038 * Delete all values associated with a field
1039 */
1040 static void
1041 FcConfigPatternDel (FcPattern *p,
1042 const char *object)
1043 {
1044 FcPatternElt *e = FcPatternFindElt (p, object);
1045 if (!e)
1046 return;
1047 while (e->values)
1048 FcConfigDel (&e->values, e->values);
1049 }
1050
1051 static void
1052 FcConfigPatternCanon (FcPattern *p,
1053 const char *object)
1054 {
1055 FcPatternElt *e = FcPatternFindElt (p, object);
1056 if (!e)
1057 return;
1058 if (!e->values)
1059 FcPatternDel (p, object);
1060 }
1061
1062 FcBool
1063 FcConfigSubstitute (FcConfig *config,
1064 FcPattern *p,
1065 FcMatchKind kind)
1066 {
1067 FcSubst *s;
1068 FcSubState *st;
1069 int i;
1070 FcTest *t;
1071 FcEdit *e;
1072 FcValueList *l;
1073
1074 if (!config)
1075 {
1076 config = FcConfigGetCurrent ();
1077 if (!config)
1078 return FcFalse;
1079 }
1080
1081 st = (FcSubState *) malloc (config->maxObjects * sizeof (FcSubState));
1082 if (!st && config->maxObjects)
1083 return FcFalse;
1084 FcMemAlloc (FC_MEM_SUBSTATE, config->maxObjects * sizeof (FcSubState));
1085
1086 if (FcDebug () & FC_DBG_EDIT)
1087 {
1088 printf ("FcConfigSubstitute ");
1089 FcPatternPrint (p);
1090 }
1091 if (kind == FcMatchPattern)
1092 s = config->substPattern;
1093 else
1094 s = config->substFont;
1095 for (; s; s = s->next)
1096 {
1097 /*
1098 * Check the tests to see if
1099 * they all match the pattern
1100 */
1101 for (t = s->test, i = 0; t; t = t->next, i++)
1102 {
1103 if (FcDebug () & FC_DBG_EDIT)
1104 {
1105 printf ("FcConfigSubstitute test ");
1106 FcTestPrint (t);
1107 }
1108 st[i].elt = FcPatternFindElt (p, t->field);
1109 /*
1110 * If there's no such field in the font,
1111 * then FcQualAll matches while FcQualAny does not
1112 */
1113 if (!st[i].elt)
1114 {
1115 if (t->qual == FcQualAll)
1116 {
1117 st[i].value = 0;
1118 continue;
1119 }
1120 else
1121 break;
1122 }
1123 /*
1124 * Check to see if there is a match, mark the location
1125 * to apply match-relative edits
1126 */
1127 st[i].value = FcConfigMatchValueList (p, t, st[i].elt->values);
1128 if (!st[i].value)
1129 break;
1130 if (t->qual == FcQualFirst && st[i].value != st[i].elt->values)
1131 break;
1132 if (t->qual == FcQualNotFirst && st[i].value == st[i].elt->values)
1133 break;
1134 }
1135 if (t)
1136 {
1137 if (FcDebug () & FC_DBG_EDIT)
1138 printf ("No match\n");
1139 continue;
1140 }
1141 if (FcDebug () & FC_DBG_EDIT)
1142 {
1143 printf ("Substitute ");
1144 FcSubstPrint (s);
1145 }
1146 for (e = s->edit; e; e = e->next)
1147 {
1148 /*
1149 * Evaluate the list of expressions
1150 */
1151 l = FcConfigValues (p, e->expr);
1152 /*
1153 * Locate any test associated with this field
1154 */
1155 for (t = s->test, i = 0; t; t = t->next, i++)
1156 if (!FcStrCmpIgnoreCase ((FcChar8 *) t->field, (FcChar8 *) e->field))
1157 break;
1158 switch (e->op) {
1159 case FcOpAssign:
1160 /*
1161 * If there was a test, then replace the matched
1162 * value with the new list of values
1163 */
1164 if (t)
1165 {
1166 FcValueList *thisValue = st[i].value;
1167 FcValueList *nextValue = thisValue ? thisValue->next : 0;
1168
1169 /*
1170 * Append the new list of values after the current value
1171 */
1172 FcConfigAdd (&st[i].elt->values, thisValue, FcTrue, l);
1173 /*
1174 * Delete the marked value
1175 */
1176 FcConfigDel (&st[i].elt->values, thisValue);
1177 /*
1178 * Adjust any pointers into the value list to ensure
1179 * future edits occur at the same place
1180 */
1181 for (t = s->test, i = 0; t; t = t->next, i++)
1182 {
1183 if (st[i].value == thisValue)
1184 st[i].value = nextValue;
1185 }
1186 break;
1187 }
1188 /* fall through ... */
1189 case FcOpAssignReplace:
1190 /*
1191 * Delete all of the values and insert
1192 * the new set
1193 */
1194 FcConfigPatternDel (p, e->field);
1195 FcConfigPatternAdd (p, e->field, l, FcTrue);
1196 /*
1197 * Adjust any pointers into the value list as they no
1198 * longer point to anything valid
1199 */
1200 if (t)
1201 {
1202 FcPatternElt *thisElt = st[i].elt;
1203 for (t = s->test, i = 0; t; t = t->next, i++)
1204 {
1205 if (st[i].elt == thisElt)
1206 st[i].value = 0;
1207 }
1208 }
1209 break;
1210 case FcOpPrepend:
1211 if (t)
1212 {
1213 FcConfigAdd (&st[i].elt->values, st[i].value, FcFalse, l);
1214 break;
1215 }
1216 /* fall through ... */
1217 case FcOpPrependFirst:
1218 FcConfigPatternAdd (p, e->field, l, FcFalse);
1219 break;
1220 case FcOpAppend:
1221 if (t)
1222 {
1223 FcConfigAdd (&st[i].elt->values, st[i].value, FcTrue, l);
1224 break;
1225 }
1226 /* fall through ... */
1227 case FcOpAppendLast:
1228 FcConfigPatternAdd (p, e->field, l, FcTrue);
1229 break;
1230 default:
1231 break;
1232 }
1233 }
1234 /*
1235 * Now go through the pattern and eliminate
1236 * any properties without data
1237 */
1238 for (e = s->edit; e; e = e->next)
1239 FcConfigPatternCanon (p, e->field);
1240
1241 if (FcDebug () & FC_DBG_EDIT)
1242 {
1243 printf ("FcConfigSubstitute edit");
1244 FcPatternPrint (p);
1245 }
1246 }
1247 FcMemFree (FC_MEM_SUBSTATE, config->maxObjects * sizeof (FcSubState));
1248 free (st);
1249 if (FcDebug () & FC_DBG_EDIT)
1250 {
1251 printf ("FcConfigSubstitute done");
1252 FcPatternPrint (p);
1253 }
1254 return FcTrue;
1255 }
1256
1257 #ifndef FONTCONFIG_PATH
1258 #define FONTCONFIG_PATH "/etc/fonts"
1259 #endif
1260
1261 #ifndef FONTCONFIG_FILE
1262 #define FONTCONFIG_FILE "fonts.conf"
1263 #endif
1264
1265 static FcChar8 *
1266 FcConfigFileExists (const FcChar8 *dir, const FcChar8 *file)
1267 {
1268 FcChar8 *path;
1269
1270 if (!dir)
1271 dir = (FcChar8 *) "";
1272 path = malloc (strlen ((char *) dir) + 1 + strlen ((char *) file) + 1);
1273 if (!path)
1274 return 0;
1275
1276 strcpy ((char *) path, (const char *) dir);
1277 /* make sure there's a single separating / */
1278 if ((!path[0] || path[strlen((char *) path)-1] != '/') && file[0] != '/')
1279 strcat ((char *) path, "/");
1280 strcat ((char *) path, (char *) file);
1281
1282 if (access ((char *) path, R_OK) == 0)
1283 return path;
1284
1285 free (path);
1286 return 0;
1287 }
1288
1289 static FcChar8 **
1290 FcConfigGetPath (void)
1291 {
1292 FcChar8 **path;
1293 FcChar8 *env, *e, *colon;
1294 FcChar8 *dir;
1295 int npath;
1296 int i;
1297
1298 npath = 2; /* default dir + null */
1299 env = (FcChar8 *) getenv ("FONTCONFIG_PATH");
1300 if (env)
1301 {
1302 e = env;
1303 npath++;
1304 while (*e)
1305 if (*e++ == ':')
1306 npath++;
1307 }
1308 path = calloc (npath, sizeof (FcChar8 *));
1309 if (!path)
1310 goto bail0;
1311 i = 0;
1312
1313 if (env)
1314 {
1315 e = env;
1316 while (*e)
1317 {
1318 colon = (FcChar8 *) strchr ((char *) e, ':');
1319 if (!colon)
1320 colon = e + strlen ((char *) e);
1321 path[i] = malloc (colon - e + 1);
1322 if (!path[i])
1323 goto bail1;
1324 strncpy ((char *) path[i], (const char *) e, colon - e);
1325 path[i][colon - e] = '\0';
1326 if (*colon)
1327 e = colon + 1;
1328 else
1329 e = colon;
1330 i++;
1331 }
1332 }
1333
1334 dir = (FcChar8 *) FONTCONFIG_PATH;
1335 path[i] = malloc (strlen ((char *) dir) + 1);
1336 if (!path[i])
1337 goto bail1;
1338 strcpy ((char *) path[i], (const char *) dir);
1339 return path;
1340
1341 bail1:
1342 for (i = 0; path[i]; i++)
1343 free (path[i]);
1344 free (path);
1345 bail0:
1346 return 0;
1347 }
1348
1349 static void
1350 FcConfigFreePath (FcChar8 **path)
1351 {
1352 FcChar8 **p;
1353
1354 for (p = path; *p; p++)
1355 free (*p);
1356 free (path);
1357 }
1358
1359 FcChar8 *
1360 FcConfigFilename (const FcChar8 *url)
1361 {
1362 FcChar8 *file, *dir, **path, **p;
1363
1364 if (!url || !*url)
1365 {
1366 url = (FcChar8 *) getenv ("FONTCONFIG_FILE");
1367 if (!url)
1368 url = (FcChar8 *) FONTCONFIG_FILE;
1369 }
1370 file = 0;
1371 switch (*url) {
1372 case '~':
1373 dir = (FcChar8 *) getenv ("HOME");
1374 if (dir)
1375 file = FcConfigFileExists (dir, url + 1);
1376 else
1377 file = 0;
1378 break;
1379 case '/':
1380 file = FcConfigFileExists (0, url);
1381 break;
1382 default:
1383 path = FcConfigGetPath ();
1384 if (!path)
1385 return 0;
1386 for (p = path; *p; p++)
1387 {
1388 file = FcConfigFileExists (*p, url);
1389 if (file)
1390 break;
1391 }
1392 FcConfigFreePath (path);
1393 break;
1394 }
1395 return file;
1396 }
1397
1398 /*
1399 * Manage the application-specific fonts
1400 */
1401
1402 FcBool
1403 FcConfigAppFontAddFile (FcConfig *config,
1404 const FcChar8 *file)
1405 {
1406 FcFontSet *set;
1407 FcStrSet *subdirs;
1408 FcStrList *sublist;
1409 FcChar8 *subdir;
1410
1411 if (!config)
1412 {
1413 config = FcConfigGetCurrent ();
1414 if (!config)
1415 return FcFalse;
1416 }
1417
1418 subdirs = FcStrSetCreate ();
1419 if (!subdirs)
1420 return FcFalse;
1421
1422 set = FcConfigGetFonts (config, FcSetApplication);
1423 if (!set)
1424 {
1425 set = FcFontSetCreate ();
1426 if (!set)
1427 {
1428 FcStrSetDestroy (subdirs);
1429 return FcFalse;
1430 }
1431 FcConfigSetFonts (config, set, FcSetApplication);
1432 }
1433
1434 if (!FcFileScan (set, subdirs, 0, config->blanks, file, FcFalse))
1435 {
1436 FcStrSetDestroy (subdirs);
1437 return FcFalse;
1438 }
1439 if ((sublist = FcStrListCreate (subdirs)))
1440 {
1441 while ((subdir = FcStrListNext (sublist)))
1442 {
1443 FcConfigAppFontAddDir (config, subdir);
1444 }
1445 FcStrListDone (sublist);
1446 }
1447 return FcTrue;
1448 }
1449
1450 FcBool
1451 FcConfigAppFontAddDir (FcConfig *config,
1452 const FcChar8 *dir)
1453 {
1454 FcFontSet *set;
1455 FcStrSet *subdirs;
1456 FcStrList *sublist;
1457 FcChar8 *subdir;
1458
1459 if (!config)
1460 {
1461 config = FcConfigGetCurrent ();
1462 if (!config)
1463 return FcFalse;
1464 }
1465 subdirs = FcStrSetCreate ();
1466 if (!subdirs)
1467 return FcFalse;
1468
1469 set = FcConfigGetFonts (config, FcSetApplication);
1470 if (!set)
1471 {
1472 set = FcFontSetCreate ();
1473 if (!set)
1474 {
1475 FcStrSetDestroy (subdirs);
1476 return FcFalse;
1477 }
1478 FcConfigSetFonts (config, set, FcSetApplication);
1479 }
1480
1481 if (!FcDirScan (set, subdirs, 0, config->blanks, dir, FcFalse))
1482 {
1483 FcStrSetDestroy (subdirs);
1484 return FcFalse;
1485 }
1486 if ((sublist = FcStrListCreate (subdirs)))
1487 {
1488 while ((subdir = FcStrListNext (sublist)))
1489 {
1490 FcConfigAppFontAddDir (config, subdir);
1491 }
1492 FcStrListDone (sublist);
1493 }
1494 return FcTrue;
1495 }
1496
1497 void
1498 FcConfigAppFontClear (FcConfig *config)
1499 {
1500 FcConfigSetFonts (config, 0, FcSetApplication);
1501 }