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