]> git.wh0rd.org - fontconfig.git/blob - src/fccfg.c
Add strong/weak pattern value binding, add known charsets for automatic
[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 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 l->binding = FcValueBindingWeak;
930 while (l && l->value.type == FcTypeVoid)
931 {
932 FcValueList *next = l->next;
933
934 FcMemFree (FC_MEM_VALLIST, sizeof (FcValueList));
935 free (l);
936 l = next;
937 }
938 return l;
939 }
940
941 static FcBool
942 FcConfigAdd (FcValueList **head,
943 FcValueList *position,
944 FcBool append,
945 FcValueList *new)
946 {
947 FcValueList **prev, *last;
948
949 if (append)
950 {
951 if (position)
952 prev = &position->next;
953 else
954 for (prev = head; *prev; prev = &(*prev)->next)
955 ;
956 }
957 else
958 {
959 if (position)
960 {
961 for (prev = head; *prev; prev = &(*prev)->next)
962 {
963 if (*prev == position)
964 break;
965 }
966 }
967 else
968 prev = head;
969
970 if (FcDebug () & FC_DBG_EDIT)
971 {
972 if (!*prev)
973 printf ("position not on list\n");
974 }
975 }
976
977 if (FcDebug () & FC_DBG_EDIT)
978 {
979 printf ("%s list before ", append ? "Append" : "Prepend");
980 FcValueListPrint (*head);
981 printf ("\n");
982 }
983
984 if (new)
985 {
986 last = new;
987 while (last->next)
988 last = last->next;
989
990 last->next = *prev;
991 *prev = new;
992 }
993
994 if (FcDebug () & FC_DBG_EDIT)
995 {
996 printf ("%s list after ", append ? "Append" : "Prepend");
997 FcValueListPrint (*head);
998 printf ("\n");
999 }
1000
1001 return FcTrue;
1002 }
1003
1004 static void
1005 FcConfigDel (FcValueList **head,
1006 FcValueList *position)
1007 {
1008 FcValueList **prev;
1009
1010 for (prev = head; *prev; prev = &(*prev)->next)
1011 {
1012 if (*prev == position)
1013 {
1014 *prev = position->next;
1015 position->next = 0;
1016 FcValueListDestroy (position);
1017 break;
1018 }
1019 }
1020 }
1021
1022 static void
1023 FcConfigPatternAdd (FcPattern *p,
1024 const char *object,
1025 FcValueList *list,
1026 FcBool append)
1027 {
1028 if (list)
1029 {
1030 FcPatternElt *e = FcPatternInsertElt (p, object);
1031
1032 if (!e)
1033 return;
1034 FcConfigAdd (&e->values, 0, append, list);
1035 }
1036 }
1037
1038 /*
1039 * Delete all values associated with a field
1040 */
1041 static void
1042 FcConfigPatternDel (FcPattern *p,
1043 const char *object)
1044 {
1045 FcPatternElt *e = FcPatternFindElt (p, object);
1046 if (!e)
1047 return;
1048 while (e->values)
1049 FcConfigDel (&e->values, e->values);
1050 }
1051
1052 static void
1053 FcConfigPatternCanon (FcPattern *p,
1054 const char *object)
1055 {
1056 FcPatternElt *e = FcPatternFindElt (p, object);
1057 if (!e)
1058 return;
1059 if (!e->values)
1060 FcPatternDel (p, object);
1061 }
1062
1063 FcBool
1064 FcConfigSubstitute (FcConfig *config,
1065 FcPattern *p,
1066 FcMatchKind kind)
1067 {
1068 FcSubst *s;
1069 FcSubState *st;
1070 int i;
1071 FcTest *t;
1072 FcEdit *e;
1073 FcValueList *l;
1074
1075 if (!config)
1076 {
1077 config = FcConfigGetCurrent ();
1078 if (!config)
1079 return FcFalse;
1080 }
1081
1082 st = (FcSubState *) malloc (config->maxObjects * sizeof (FcSubState));
1083 if (!st && config->maxObjects)
1084 return FcFalse;
1085 FcMemAlloc (FC_MEM_SUBSTATE, config->maxObjects * sizeof (FcSubState));
1086
1087 if (FcDebug () & FC_DBG_EDIT)
1088 {
1089 printf ("FcConfigSubstitute ");
1090 FcPatternPrint (p);
1091 }
1092 if (kind == FcMatchPattern)
1093 s = config->substPattern;
1094 else
1095 s = config->substFont;
1096 for (; s; s = s->next)
1097 {
1098 /*
1099 * Check the tests to see if
1100 * they all match the pattern
1101 */
1102 for (t = s->test, i = 0; t; t = t->next, i++)
1103 {
1104 if (FcDebug () & FC_DBG_EDIT)
1105 {
1106 printf ("FcConfigSubstitute test ");
1107 FcTestPrint (t);
1108 }
1109 st[i].elt = FcPatternFindElt (p, t->field);
1110 /*
1111 * If there's no such field in the font,
1112 * then FcQualAll matches while FcQualAny does not
1113 */
1114 if (!st[i].elt)
1115 {
1116 if (t->qual == FcQualAll)
1117 {
1118 st[i].value = 0;
1119 continue;
1120 }
1121 else
1122 break;
1123 }
1124 /*
1125 * Check to see if there is a match, mark the location
1126 * to apply match-relative edits
1127 */
1128 st[i].value = FcConfigMatchValueList (p, t, st[i].elt->values);
1129 if (!st[i].value)
1130 break;
1131 if (t->qual == FcQualFirst && st[i].value != st[i].elt->values)
1132 break;
1133 if (t->qual == FcQualNotFirst && st[i].value == st[i].elt->values)
1134 break;
1135 }
1136 if (t)
1137 {
1138 if (FcDebug () & FC_DBG_EDIT)
1139 printf ("No match\n");
1140 continue;
1141 }
1142 if (FcDebug () & FC_DBG_EDIT)
1143 {
1144 printf ("Substitute ");
1145 FcSubstPrint (s);
1146 }
1147 for (e = s->edit; e; e = e->next)
1148 {
1149 /*
1150 * Evaluate the list of expressions
1151 */
1152 l = FcConfigValues (p, e->expr);
1153 /*
1154 * Locate any test associated with this field
1155 */
1156 for (t = s->test, i = 0; t; t = t->next, i++)
1157 if (!FcStrCmpIgnoreCase ((FcChar8 *) t->field, (FcChar8 *) e->field))
1158 break;
1159 switch (e->op) {
1160 case FcOpAssign:
1161 /*
1162 * If there was a test, then replace the matched
1163 * value with the new list of values
1164 */
1165 if (t)
1166 {
1167 FcValueList *thisValue = st[i].value;
1168 FcValueList *nextValue = thisValue ? thisValue->next : 0;
1169
1170 /*
1171 * Append the new list of values after the current value
1172 */
1173 FcConfigAdd (&st[i].elt->values, thisValue, FcTrue, l);
1174 /*
1175 * Delete the marked value
1176 */
1177 FcConfigDel (&st[i].elt->values, thisValue);
1178 /*
1179 * Adjust any pointers into the value list to ensure
1180 * future edits occur at the same place
1181 */
1182 for (t = s->test, i = 0; t; t = t->next, i++)
1183 {
1184 if (st[i].value == thisValue)
1185 st[i].value = nextValue;
1186 }
1187 break;
1188 }
1189 /* fall through ... */
1190 case FcOpAssignReplace:
1191 /*
1192 * Delete all of the values and insert
1193 * the new set
1194 */
1195 FcConfigPatternDel (p, e->field);
1196 FcConfigPatternAdd (p, e->field, l, FcTrue);
1197 /*
1198 * Adjust any pointers into the value list as they no
1199 * longer point to anything valid
1200 */
1201 if (t)
1202 {
1203 FcPatternElt *thisElt = st[i].elt;
1204 for (t = s->test, i = 0; t; t = t->next, i++)
1205 {
1206 if (st[i].elt == thisElt)
1207 st[i].value = 0;
1208 }
1209 }
1210 break;
1211 case FcOpPrepend:
1212 if (t)
1213 {
1214 FcConfigAdd (&st[i].elt->values, st[i].value, FcFalse, l);
1215 break;
1216 }
1217 /* fall through ... */
1218 case FcOpPrependFirst:
1219 FcConfigPatternAdd (p, e->field, l, FcFalse);
1220 break;
1221 case FcOpAppend:
1222 if (t)
1223 {
1224 FcConfigAdd (&st[i].elt->values, st[i].value, FcTrue, l);
1225 break;
1226 }
1227 /* fall through ... */
1228 case FcOpAppendLast:
1229 FcConfigPatternAdd (p, e->field, l, FcTrue);
1230 break;
1231 default:
1232 break;
1233 }
1234 }
1235 /*
1236 * Now go through the pattern and eliminate
1237 * any properties without data
1238 */
1239 for (e = s->edit; e; e = e->next)
1240 FcConfigPatternCanon (p, e->field);
1241
1242 if (FcDebug () & FC_DBG_EDIT)
1243 {
1244 printf ("FcConfigSubstitute edit");
1245 FcPatternPrint (p);
1246 }
1247 }
1248 FcMemFree (FC_MEM_SUBSTATE, config->maxObjects * sizeof (FcSubState));
1249 free (st);
1250 if (FcDebug () & FC_DBG_EDIT)
1251 {
1252 printf ("FcConfigSubstitute done");
1253 FcPatternPrint (p);
1254 }
1255 return FcTrue;
1256 }
1257
1258 #ifndef FONTCONFIG_PATH
1259 #define FONTCONFIG_PATH "/etc/fonts"
1260 #endif
1261
1262 #ifndef FONTCONFIG_FILE
1263 #define FONTCONFIG_FILE "fonts.conf"
1264 #endif
1265
1266 static FcChar8 *
1267 FcConfigFileExists (const FcChar8 *dir, const FcChar8 *file)
1268 {
1269 FcChar8 *path;
1270
1271 if (!dir)
1272 dir = (FcChar8 *) "";
1273 path = malloc (strlen ((char *) dir) + 1 + strlen ((char *) file) + 1);
1274 if (!path)
1275 return 0;
1276
1277 strcpy ((char *) path, (const char *) dir);
1278 /* make sure there's a single separating / */
1279 if ((!path[0] || path[strlen((char *) path)-1] != '/') && file[0] != '/')
1280 strcat ((char *) path, "/");
1281 strcat ((char *) path, (char *) file);
1282
1283 if (access ((char *) path, R_OK) == 0)
1284 return path;
1285
1286 free (path);
1287 return 0;
1288 }
1289
1290 static FcChar8 **
1291 FcConfigGetPath (void)
1292 {
1293 FcChar8 **path;
1294 FcChar8 *env, *e, *colon;
1295 FcChar8 *dir;
1296 int npath;
1297 int i;
1298
1299 npath = 2; /* default dir + null */
1300 env = (FcChar8 *) getenv ("FONTCONFIG_PATH");
1301 if (env)
1302 {
1303 e = env;
1304 npath++;
1305 while (*e)
1306 if (*e++ == ':')
1307 npath++;
1308 }
1309 path = calloc (npath, sizeof (FcChar8 *));
1310 if (!path)
1311 goto bail0;
1312 i = 0;
1313
1314 if (env)
1315 {
1316 e = env;
1317 while (*e)
1318 {
1319 colon = (FcChar8 *) strchr ((char *) e, ':');
1320 if (!colon)
1321 colon = e + strlen ((char *) e);
1322 path[i] = malloc (colon - e + 1);
1323 if (!path[i])
1324 goto bail1;
1325 strncpy ((char *) path[i], (const char *) e, colon - e);
1326 path[i][colon - e] = '\0';
1327 if (*colon)
1328 e = colon + 1;
1329 else
1330 e = colon;
1331 i++;
1332 }
1333 }
1334
1335 dir = (FcChar8 *) FONTCONFIG_PATH;
1336 path[i] = malloc (strlen ((char *) dir) + 1);
1337 if (!path[i])
1338 goto bail1;
1339 strcpy ((char *) path[i], (const char *) dir);
1340 return path;
1341
1342 bail1:
1343 for (i = 0; path[i]; i++)
1344 free (path[i]);
1345 free (path);
1346 bail0:
1347 return 0;
1348 }
1349
1350 static void
1351 FcConfigFreePath (FcChar8 **path)
1352 {
1353 FcChar8 **p;
1354
1355 for (p = path; *p; p++)
1356 free (*p);
1357 free (path);
1358 }
1359
1360 FcChar8 *
1361 FcConfigFilename (const FcChar8 *url)
1362 {
1363 FcChar8 *file, *dir, **path, **p;
1364
1365 if (!url || !*url)
1366 {
1367 url = (FcChar8 *) getenv ("FONTCONFIG_FILE");
1368 if (!url)
1369 url = (FcChar8 *) FONTCONFIG_FILE;
1370 }
1371 file = 0;
1372 switch (*url) {
1373 case '~':
1374 dir = (FcChar8 *) getenv ("HOME");
1375 if (dir)
1376 file = FcConfigFileExists (dir, url + 1);
1377 else
1378 file = 0;
1379 break;
1380 case '/':
1381 file = FcConfigFileExists (0, url);
1382 break;
1383 default:
1384 path = FcConfigGetPath ();
1385 if (!path)
1386 return 0;
1387 for (p = path; *p; p++)
1388 {
1389 file = FcConfigFileExists (*p, url);
1390 if (file)
1391 break;
1392 }
1393 FcConfigFreePath (path);
1394 break;
1395 }
1396 return file;
1397 }
1398
1399 /*
1400 * Manage the application-specific fonts
1401 */
1402
1403 FcBool
1404 FcConfigAppFontAddFile (FcConfig *config,
1405 const FcChar8 *file)
1406 {
1407 FcFontSet *set;
1408 FcStrSet *subdirs;
1409 FcStrList *sublist;
1410 FcChar8 *subdir;
1411
1412 if (!config)
1413 {
1414 config = FcConfigGetCurrent ();
1415 if (!config)
1416 return FcFalse;
1417 }
1418
1419 subdirs = FcStrSetCreate ();
1420 if (!subdirs)
1421 return FcFalse;
1422
1423 set = FcConfigGetFonts (config, FcSetApplication);
1424 if (!set)
1425 {
1426 set = FcFontSetCreate ();
1427 if (!set)
1428 {
1429 FcStrSetDestroy (subdirs);
1430 return FcFalse;
1431 }
1432 FcConfigSetFonts (config, set, FcSetApplication);
1433 }
1434
1435 if (!FcFileScan (set, subdirs, 0, config->blanks, file, FcFalse))
1436 {
1437 FcStrSetDestroy (subdirs);
1438 return FcFalse;
1439 }
1440 if ((sublist = FcStrListCreate (subdirs)))
1441 {
1442 while ((subdir = FcStrListNext (sublist)))
1443 {
1444 FcConfigAppFontAddDir (config, subdir);
1445 }
1446 FcStrListDone (sublist);
1447 }
1448 return FcTrue;
1449 }
1450
1451 FcBool
1452 FcConfigAppFontAddDir (FcConfig *config,
1453 const FcChar8 *dir)
1454 {
1455 FcFontSet *set;
1456 FcStrSet *subdirs;
1457 FcStrList *sublist;
1458 FcChar8 *subdir;
1459
1460 if (!config)
1461 {
1462 config = FcConfigGetCurrent ();
1463 if (!config)
1464 return FcFalse;
1465 }
1466 subdirs = FcStrSetCreate ();
1467 if (!subdirs)
1468 return FcFalse;
1469
1470 set = FcConfigGetFonts (config, FcSetApplication);
1471 if (!set)
1472 {
1473 set = FcFontSetCreate ();
1474 if (!set)
1475 {
1476 FcStrSetDestroy (subdirs);
1477 return FcFalse;
1478 }
1479 FcConfigSetFonts (config, set, FcSetApplication);
1480 }
1481
1482 if (!FcDirScan (set, subdirs, 0, config->blanks, dir, FcFalse))
1483 {
1484 FcStrSetDestroy (subdirs);
1485 return FcFalse;
1486 }
1487 if ((sublist = FcStrListCreate (subdirs)))
1488 {
1489 while ((subdir = FcStrListNext (sublist)))
1490 {
1491 FcConfigAppFontAddDir (config, subdir);
1492 }
1493 FcStrListDone (sublist);
1494 }
1495 return FcTrue;
1496 }
1497
1498 void
1499 FcConfigAppFontClear (FcConfig *config)
1500 {
1501 FcConfigSetFonts (config, 0, FcSetApplication);
1502 }