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