]> git.wh0rd.org - fontconfig.git/blob - src/fccfg.c
Compare device numbers as well as inodes. Always normalize directory names
[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, config);
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 const FcChar8 *
392 FcConfigNormalizeFontDir (FcConfig *config,
393 const FcChar8 *d)
394 {
395 /* If this is a bottleneck, we can cache the fontDir inodes. */
396 ino_t di;
397 dev_t dd;
398 int n;
399 struct stat s;
400
401 if (stat ((char *)d, &s) == -1)
402 return 0;
403 di = s.st_ino; dd = s.st_dev;
404
405 for (n = 0; n < config->fontDirs->num; n++)
406 {
407 if (stat ((char *)config->fontDirs->strs[n], &s) == -1)
408 continue;
409 if (di == s.st_ino && dd == s.st_dev)
410 return config->fontDirs->strs[n];
411 }
412 return 0;
413 }
414
415 FcBool
416 FcConfigAddDir (FcConfig *config,
417 const FcChar8 *d)
418 {
419 return (FcConfigAddConfigDir (config, d) &&
420 FcConfigAddFontDir (config, d));
421 }
422
423 FcStrList *
424 FcConfigGetFontDirs (FcConfig *config)
425 {
426 if (!config)
427 {
428 config = FcConfigGetCurrent ();
429 if (!config)
430 return 0;
431 }
432 return FcStrListCreate (config->fontDirs);
433 }
434
435 FcBool
436 FcConfigAddConfigFile (FcConfig *config,
437 const FcChar8 *f)
438 {
439 FcBool ret;
440 FcChar8 *file = FcConfigFilename (f);
441
442 if (!file)
443 return FcFalse;
444
445 ret = FcStrSetAdd (config->configFiles, file);
446 FcStrFree (file);
447 return ret;
448 }
449
450 FcStrList *
451 FcConfigGetConfigFiles (FcConfig *config)
452 {
453 if (!config)
454 {
455 config = FcConfigGetCurrent ();
456 if (!config)
457 return 0;
458 }
459 return FcStrListCreate (config->configFiles);
460 }
461
462 FcBool
463 FcConfigSetCache (FcConfig *config,
464 const FcChar8 *c)
465 {
466 FcChar8 *new = FcStrCopyFilename (c);
467
468 if (!new)
469 return FcFalse;
470 if (config->cache)
471 FcStrFree (config->cache);
472 config->cache = new;
473 return FcTrue;
474 }
475
476 FcChar8 *
477 FcConfigGetCache (FcConfig *config)
478 {
479 if (!config)
480 {
481 config = FcConfigGetCurrent ();
482 if (!config)
483 return 0;
484 }
485 return config->cache;
486 }
487
488 FcFontSet *
489 FcConfigGetFonts (FcConfig *config,
490 FcSetName set)
491 {
492 if (!config)
493 {
494 config = FcConfigGetCurrent ();
495 if (!config)
496 return 0;
497 }
498 return config->fonts[set];
499 }
500
501 void
502 FcConfigSetFonts (FcConfig *config,
503 FcFontSet *fonts,
504 FcSetName set)
505 {
506 if (config->fonts[set])
507 FcFontSetDestroy (config->fonts[set]);
508 config->fonts[set] = fonts;
509 }
510
511
512
513 FcBlanks *
514 FcConfigGetBlanks (FcConfig *config)
515 {
516 if (!config)
517 {
518 config = FcConfigGetCurrent ();
519 if (!config)
520 return 0;
521 }
522 return config->blanks;
523 }
524
525 FcBool
526 FcConfigAddBlank (FcConfig *config,
527 FcChar32 blank)
528 {
529 FcBlanks *b;
530
531 b = config->blanks;
532 if (!b)
533 {
534 b = FcBlanksCreate ();
535 if (!b)
536 return FcFalse;
537 }
538 if (!FcBlanksAdd (b, blank))
539 return FcFalse;
540 config->blanks = b;
541 return FcTrue;
542 }
543
544 int
545 FcConfigGetRescanInverval (FcConfig *config)
546 {
547 if (!config)
548 {
549 config = FcConfigGetCurrent ();
550 if (!config)
551 return 0;
552 }
553 return config->rescanInterval;
554 }
555
556 FcBool
557 FcConfigSetRescanInverval (FcConfig *config, int rescanInterval)
558 {
559 if (!config)
560 {
561 config = FcConfigGetCurrent ();
562 if (!config)
563 return FcFalse;
564 }
565 config->rescanInterval = rescanInterval;
566 return FcTrue;
567 }
568
569 FcBool
570 FcConfigAddEdit (FcConfig *config,
571 FcTest *test,
572 FcEdit *edit,
573 FcMatchKind kind)
574 {
575 FcSubst *subst, **prev;
576 FcTest *t;
577 int num;
578
579 subst = (FcSubst *) malloc (sizeof (FcSubst));
580 if (!subst)
581 return FcFalse;
582 FcMemAlloc (FC_MEM_SUBST, sizeof (FcSubst));
583 if (kind == FcMatchPattern)
584 prev = &config->substPattern;
585 else
586 prev = &config->substFont;
587 for (; *prev; prev = &(*prev)->next);
588 *prev = subst;
589 subst->next = 0;
590 subst->test = test;
591 subst->edit = edit;
592 num = 0;
593 for (t = test; t; t = t->next)
594 {
595 if (t->kind == FcMatchDefault)
596 t->kind = kind;
597 num++;
598 }
599 if (config->maxObjects < num)
600 config->maxObjects = num;
601 if (FcDebug () & FC_DBG_EDIT)
602 {
603 printf ("Add Subst ");
604 FcSubstPrint (subst);
605 }
606 return FcTrue;
607 }
608
609 typedef struct _FcSubState {
610 FcPatternElt *elt;
611 FcValueList *value;
612 } FcSubState;
613
614 static FcValue
615 FcConfigPromote (FcValue v, FcValue u)
616 {
617 if (v.type == FcTypeInteger)
618 {
619 v.type = FcTypeDouble;
620 v.u.d = (double) v.u.i;
621 }
622 else if (v.type == FcTypeVoid && u.type == FcTypeMatrix)
623 {
624 v.u.m = &FcIdentityMatrix;
625 v.type = FcTypeMatrix;
626 }
627 else if (v.type == FcTypeString && u.type == FcTypeLangSet)
628 {
629 v.u.l = FcLangSetPromote (v.u.s);
630 v.type = FcTypeLangSet;
631 }
632 return v;
633 }
634
635 FcBool
636 FcConfigCompareValue (const FcValue *left_o,
637 FcOp op,
638 const FcValue *right_o)
639 {
640 FcValue left = FcValueCanonicalize(left_o);
641 FcValue right = FcValueCanonicalize(right_o);
642 FcBool ret = FcFalse;
643
644 left = FcConfigPromote (left, right);
645 right = FcConfigPromote (right, left);
646 if (left.type == right.type)
647 {
648 switch (left.type) {
649 case FcTypeInteger:
650 break; /* FcConfigPromote prevents this from happening */
651 case FcTypeDouble:
652 switch (op) {
653 case FcOpEqual:
654 case FcOpContains:
655 case FcOpListing:
656 ret = left.u.d == right.u.d;
657 break;
658 case FcOpNotEqual:
659 case FcOpNotContains:
660 ret = left.u.d != right.u.d;
661 break;
662 case FcOpLess:
663 ret = left.u.d < right.u.d;
664 break;
665 case FcOpLessEqual:
666 ret = left.u.d <= right.u.d;
667 break;
668 case FcOpMore:
669 ret = left.u.d > right.u.d;
670 break;
671 case FcOpMoreEqual:
672 ret = left.u.d >= right.u.d;
673 break;
674 default:
675 break;
676 }
677 break;
678 case FcTypeBool:
679 switch (op) {
680 case FcOpEqual:
681 case FcOpContains:
682 case FcOpListing:
683 ret = left.u.b == right.u.b;
684 break;
685 case FcOpNotEqual:
686 case FcOpNotContains:
687 ret = left.u.b != right.u.b;
688 break;
689 default:
690 break;
691 }
692 break;
693 case FcTypeString:
694 switch (op) {
695 case FcOpEqual:
696 case FcOpListing:
697 ret = FcStrCmpIgnoreCase (left.u.s, right.u.s) == 0;
698 break;
699 case FcOpContains:
700 ret = FcStrStrIgnoreCase (left.u.s, right.u.s) != 0;
701 break;
702 case FcOpNotEqual:
703 ret = FcStrCmpIgnoreCase (left.u.s, right.u.s) != 0;
704 break;
705 case FcOpNotContains:
706 ret = FcStrCmpIgnoreCase (left.u.s, right.u.s) == 0;
707 break;
708 default:
709 break;
710 }
711 break;
712 case FcTypeMatrix:
713 switch (op) {
714 case FcOpEqual:
715 case FcOpContains:
716 case FcOpListing:
717 ret = FcMatrixEqual (left.u.m, right.u.m);
718 break;
719 case FcOpNotEqual:
720 case FcOpNotContains:
721 ret = !FcMatrixEqual (left.u.m, right.u.m);
722 break;
723 default:
724 break;
725 }
726 break;
727 case FcTypeCharSet:
728 switch (op) {
729 case FcOpContains:
730 case FcOpListing:
731 /* left contains right if right is a subset of left */
732 ret = FcCharSetIsSubset (right.u.c, left.u.c);
733 break;
734 case FcOpNotContains:
735 /* left contains right if right is a subset of left */
736 ret = !FcCharSetIsSubset (right.u.c, left.u.c);
737 break;
738 case FcOpEqual:
739 ret = FcCharSetEqual (left.u.c, right.u.c);
740 break;
741 case FcOpNotEqual:
742 ret = !FcCharSetEqual (left.u.c, right.u.c);
743 break;
744 default:
745 break;
746 }
747 break;
748 case FcTypeLangSet:
749 switch (op) {
750 case FcOpContains:
751 case FcOpListing:
752 ret = FcLangSetContains (left.u.l, right.u.l);
753 break;
754 case FcOpNotContains:
755 ret = !FcLangSetContains (left.u.l, right.u.l);
756 break;
757 case FcOpEqual:
758 ret = FcLangSetEqual (left.u.l, right.u.l);
759 break;
760 case FcOpNotEqual:
761 ret = !FcLangSetEqual (left.u.l, right.u.l);
762 break;
763 default:
764 break;
765 }
766 break;
767 case FcTypeVoid:
768 switch (op) {
769 case FcOpEqual:
770 case FcOpContains:
771 case FcOpListing:
772 ret = FcTrue;
773 break;
774 default:
775 break;
776 }
777 break;
778 case FcTypeFTFace:
779 switch (op) {
780 case FcOpEqual:
781 case FcOpContains:
782 case FcOpListing:
783 ret = left.u.f == right.u.f;
784 break;
785 case FcOpNotEqual:
786 case FcOpNotContains:
787 ret = left.u.f != right.u.f;
788 break;
789 default:
790 break;
791 }
792 break;
793 }
794 }
795 else
796 {
797 if (op == FcOpNotEqual || op == FcOpNotContains)
798 ret = FcTrue;
799 }
800 return ret;
801 }
802
803
804 #define _FcDoubleFloor(d) ((int) (d))
805 #define _FcDoubleCeil(d) ((double) (int) (d) == (d) ? (int) (d) : (int) ((d) + 1))
806 #define FcDoubleFloor(d) ((d) >= 0 ? _FcDoubleFloor(d) : -_FcDoubleCeil(-(d)))
807 #define FcDoubleCeil(d) ((d) >= 0 ? _FcDoubleCeil(d) : -_FcDoubleFloor(-(d)))
808 #define FcDoubleRound(d) FcDoubleFloor ((d) + 0.5)
809 #define FcDoubleTrunc(d) ((d) >= 0 ? _FcDoubleFloor (d) : -_FcDoubleFloor (-(d)))
810
811 static FcValue
812 FcConfigEvaluate (FcPattern *p, FcExpr *e)
813 {
814 FcValue v, vl, vr;
815 FcResult r;
816 FcMatrix *m;
817
818 switch (e->op) {
819 case FcOpInteger:
820 v.type = FcTypeInteger;
821 v.u.i = e->u.ival;
822 break;
823 case FcOpDouble:
824 v.type = FcTypeDouble;
825 v.u.d = e->u.dval;
826 break;
827 case FcOpString:
828 v.type = FcTypeString;
829 v.u.s = FcStrStaticName(e->u.sval);
830 v = FcValueSave (v);
831 break;
832 case FcOpMatrix:
833 v.type = FcTypeMatrix;
834 v.u.m = e->u.mval;
835 v = FcValueSave (v);
836 break;
837 case FcOpCharSet:
838 v.type = FcTypeCharSet;
839 v.u.c = e->u.cval;
840 v = FcValueSave (v);
841 break;
842 case FcOpBool:
843 v.type = FcTypeBool;
844 v.u.b = e->u.bval;
845 break;
846 case FcOpField:
847 r = FcPatternGet (p, e->u.field, 0, &v);
848 if (r != FcResultMatch)
849 v.type = FcTypeVoid;
850 v = FcValueSave (v);
851 break;
852 case FcOpConst:
853 if (FcNameConstant (e->u.constant, &v.u.i))
854 v.type = FcTypeInteger;
855 else
856 v.type = FcTypeVoid;
857 break;
858 case FcOpQuest:
859 vl = FcConfigEvaluate (p, e->u.tree.left);
860 if (vl.type == FcTypeBool)
861 {
862 if (vl.u.b)
863 v = FcConfigEvaluate (p, e->u.tree.right->u.tree.left);
864 else
865 v = FcConfigEvaluate (p, e->u.tree.right->u.tree.right);
866 }
867 else
868 v.type = FcTypeVoid;
869 FcValueDestroy (vl);
870 break;
871 case FcOpEqual:
872 case FcOpNotEqual:
873 case FcOpLess:
874 case FcOpLessEqual:
875 case FcOpMore:
876 case FcOpMoreEqual:
877 case FcOpContains:
878 case FcOpNotContains:
879 case FcOpListing:
880 vl = FcConfigEvaluate (p, e->u.tree.left);
881 vr = FcConfigEvaluate (p, e->u.tree.right);
882 v.type = FcTypeBool;
883 v.u.b = FcConfigCompareValue (&vl, e->op, &vr);
884 FcValueDestroy (vl);
885 FcValueDestroy (vr);
886 break;
887 case FcOpOr:
888 case FcOpAnd:
889 case FcOpPlus:
890 case FcOpMinus:
891 case FcOpTimes:
892 case FcOpDivide:
893 vl = FcConfigEvaluate (p, e->u.tree.left);
894 vr = FcConfigEvaluate (p, e->u.tree.right);
895 vl = FcConfigPromote (vl, vr);
896 vr = FcConfigPromote (vr, vl);
897 if (vl.type == vr.type)
898 {
899 switch (vl.type) {
900 case FcTypeDouble:
901 switch (e->op) {
902 case FcOpPlus:
903 v.type = FcTypeDouble;
904 v.u.d = vl.u.d + vr.u.d;
905 break;
906 case FcOpMinus:
907 v.type = FcTypeDouble;
908 v.u.d = vl.u.d - vr.u.d;
909 break;
910 case FcOpTimes:
911 v.type = FcTypeDouble;
912 v.u.d = vl.u.d * vr.u.d;
913 break;
914 case FcOpDivide:
915 v.type = FcTypeDouble;
916 v.u.d = vl.u.d / vr.u.d;
917 break;
918 default:
919 v.type = FcTypeVoid;
920 break;
921 }
922 if (v.type == FcTypeDouble &&
923 v.u.d == (double) (int) v.u.d)
924 {
925 v.type = FcTypeInteger;
926 v.u.i = (int) v.u.d;
927 }
928 break;
929 case FcTypeBool:
930 switch (e->op) {
931 case FcOpOr:
932 v.type = FcTypeBool;
933 v.u.b = vl.u.b || vr.u.b;
934 break;
935 case FcOpAnd:
936 v.type = FcTypeBool;
937 v.u.b = vl.u.b && vr.u.b;
938 break;
939 default:
940 v.type = FcTypeVoid;
941 break;
942 }
943 break;
944 case FcTypeString:
945 switch (e->op) {
946 case FcOpPlus:
947 v.type = FcTypeString;
948 v.u.s = FcStrStaticName (FcStrPlus (vl.u.s, vr.u.s));
949
950 if (!v.u.s)
951 v.type = FcTypeVoid;
952 break;
953 default:
954 v.type = FcTypeVoid;
955 break;
956 }
957 break;
958 case FcTypeMatrix:
959 switch (e->op) {
960 case FcOpTimes:
961 v.type = FcTypeMatrix;
962 m = malloc (sizeof (FcMatrix));
963 if (m)
964 {
965 FcMemAlloc (FC_MEM_MATRIX, sizeof (FcMatrix));
966 FcMatrixMultiply (m, vl.u.m, vr.u.m);
967 v.u.m = m;
968 }
969 else
970 {
971 v.type = FcTypeVoid;
972 }
973 break;
974 default:
975 v.type = FcTypeVoid;
976 break;
977 }
978 break;
979 default:
980 v.type = FcTypeVoid;
981 break;
982 }
983 }
984 else
985 v.type = FcTypeVoid;
986 FcValueDestroy (vl);
987 FcValueDestroy (vr);
988 break;
989 case FcOpNot:
990 vl = FcConfigEvaluate (p, e->u.tree.left);
991 switch (vl.type) {
992 case FcTypeBool:
993 v.type = FcTypeBool;
994 v.u.b = !vl.u.b;
995 break;
996 default:
997 v.type = FcTypeVoid;
998 break;
999 }
1000 FcValueDestroy (vl);
1001 break;
1002 case FcOpFloor:
1003 vl = FcConfigEvaluate (p, e->u.tree.left);
1004 switch (vl.type) {
1005 case FcTypeInteger:
1006 v = vl;
1007 break;
1008 case FcTypeDouble:
1009 v.type = FcTypeInteger;
1010 v.u.i = FcDoubleFloor (vl.u.d);
1011 break;
1012 default:
1013 v.type = FcTypeVoid;
1014 break;
1015 }
1016 FcValueDestroy (vl);
1017 break;
1018 case FcOpCeil:
1019 vl = FcConfigEvaluate (p, e->u.tree.left);
1020 switch (vl.type) {
1021 case FcTypeInteger:
1022 v = vl;
1023 break;
1024 case FcTypeDouble:
1025 v.type = FcTypeInteger;
1026 v.u.i = FcDoubleCeil (vl.u.d);
1027 break;
1028 default:
1029 v.type = FcTypeVoid;
1030 break;
1031 }
1032 FcValueDestroy (vl);
1033 break;
1034 case FcOpRound:
1035 vl = FcConfigEvaluate (p, e->u.tree.left);
1036 switch (vl.type) {
1037 case FcTypeInteger:
1038 v = vl;
1039 break;
1040 case FcTypeDouble:
1041 v.type = FcTypeInteger;
1042 v.u.i = FcDoubleRound (vl.u.d);
1043 break;
1044 default:
1045 v.type = FcTypeVoid;
1046 break;
1047 }
1048 FcValueDestroy (vl);
1049 break;
1050 case FcOpTrunc:
1051 vl = FcConfigEvaluate (p, e->u.tree.left);
1052 switch (vl.type) {
1053 case FcTypeInteger:
1054 v = vl;
1055 break;
1056 case FcTypeDouble:
1057 v.type = FcTypeInteger;
1058 v.u.i = FcDoubleTrunc (vl.u.d);
1059 break;
1060 default:
1061 v.type = FcTypeVoid;
1062 break;
1063 }
1064 FcValueDestroy (vl);
1065 break;
1066 default:
1067 v.type = FcTypeVoid;
1068 break;
1069 }
1070 return v;
1071 }
1072
1073 static FcValueList *
1074 FcConfigMatchValueList (FcPattern *p,
1075 FcTest *t,
1076 FcValueList *values)
1077 {
1078 FcValueList *ret = 0;
1079 FcExpr *e = t->expr;
1080 FcValue value;
1081 FcValueList *v;
1082
1083 while (e)
1084 {
1085 /* Compute the value of the match expression */
1086 if (e->op == FcOpComma)
1087 {
1088 value = FcConfigEvaluate (p, e->u.tree.left);
1089 e = e->u.tree.right;
1090 }
1091 else
1092 {
1093 value = FcConfigEvaluate (p, e);
1094 e = 0;
1095 }
1096
1097 for (v = values; v; v = FcValueListPtrU(v->next))
1098 {
1099 /* Compare the pattern value to the match expression value */
1100 if (FcConfigCompareValue (&v->value, t->op, &value))
1101 {
1102 if (!ret)
1103 ret = v;
1104 }
1105 else
1106 {
1107 if (t->qual == FcQualAll)
1108 {
1109 ret = 0;
1110 break;
1111 }
1112 }
1113 }
1114 FcValueDestroy (value);
1115 }
1116 return ret;
1117 }
1118
1119 static FcValueList *
1120 FcConfigValues (FcPattern *p, FcExpr *e, FcValueBinding binding)
1121 {
1122 FcValueList *l;
1123
1124 if (!e)
1125 return 0;
1126 l = (FcValueList *) malloc (sizeof (FcValueList));
1127 if (!l)
1128 return 0;
1129 FcMemAlloc (FC_MEM_VALLIST, sizeof (FcValueList));
1130 if (e->op == FcOpComma)
1131 {
1132 l->value = FcConfigEvaluate (p, e->u.tree.left);
1133 l->next = FcValueListPtrCreateDynamic(FcConfigValues (p, e->u.tree.right, binding));
1134 }
1135 else
1136 {
1137 l->value = FcConfigEvaluate (p, e);
1138 l->next = FcValueListPtrCreateDynamic(0);
1139 }
1140 l->binding = binding;
1141 if (l->value.type == FcTypeVoid)
1142 {
1143 FcValueList *next = FcValueListPtrU(l->next);
1144
1145 FcMemFree (FC_MEM_VALLIST, sizeof (FcValueList));
1146 free (l);
1147 l = next;
1148 }
1149
1150 return l;
1151 }
1152
1153 static FcBool
1154 FcConfigAdd (FcValueListPtr *head,
1155 FcValueList *position,
1156 FcBool append,
1157 FcValueList *new)
1158 {
1159 FcValueListPtr *prev, last, v;
1160 FcValueBinding sameBinding;
1161
1162 if (position)
1163 sameBinding = position->binding;
1164 else
1165 sameBinding = FcValueBindingWeak;
1166 for (v = FcValueListPtrCreateDynamic(new); FcValueListPtrU(v);
1167 v = FcValueListPtrU(v)->next)
1168 if (FcValueListPtrU(v)->binding == FcValueBindingSame)
1169 FcValueListPtrU(v)->binding = sameBinding;
1170 if (append)
1171 {
1172 if (position)
1173 prev = &position->next;
1174 else
1175 for (prev = head; FcValueListPtrU(*prev);
1176 prev = &(FcValueListPtrU(*prev)->next))
1177 ;
1178 }
1179 else
1180 {
1181 if (position)
1182 {
1183 for (prev = head; FcValueListPtrU(*prev);
1184 prev = &(FcValueListPtrU(*prev)->next))
1185 {
1186 if (FcValueListPtrU(*prev) == position)
1187 break;
1188 }
1189 }
1190 else
1191 prev = head;
1192
1193 if (FcDebug () & FC_DBG_EDIT)
1194 {
1195 if (!FcValueListPtrU(*prev))
1196 printf ("position not on list\n");
1197 }
1198 }
1199
1200 if (FcDebug () & FC_DBG_EDIT)
1201 {
1202 printf ("%s list before ", append ? "Append" : "Prepend");
1203 FcValueListPrint (*head);
1204 printf ("\n");
1205 }
1206
1207 if (new)
1208 {
1209 last = FcValueListPtrCreateDynamic(new);
1210 while (FcValueListPtrU(FcValueListPtrU(last)->next))
1211 last = FcValueListPtrU(last)->next;
1212
1213 FcValueListPtrU(last)->next = *prev;
1214 *prev = FcValueListPtrCreateDynamic(new);
1215 }
1216
1217 if (FcDebug () & FC_DBG_EDIT)
1218 {
1219 printf ("%s list after ", append ? "Append" : "Prepend");
1220 FcValueListPrint (*head);
1221 printf ("\n");
1222 }
1223
1224 return FcTrue;
1225 }
1226
1227 static void
1228 FcConfigDel (FcValueListPtr *head,
1229 FcValueList *position)
1230 {
1231 FcValueListPtr *prev;
1232
1233 for (prev = head; FcValueListPtrU(*prev);
1234 prev = &(FcValueListPtrU(*prev)->next))
1235 {
1236 if (FcValueListPtrU(*prev) == position)
1237 {
1238 *prev = position->next;
1239 position->next = FcValueListPtrCreateDynamic(0);
1240 FcValueListDestroy (FcValueListPtrCreateDynamic(position));
1241 break;
1242 }
1243 }
1244 }
1245
1246 static void
1247 FcConfigPatternAdd (FcPattern *p,
1248 const char *object,
1249 FcValueList *list,
1250 FcBool append)
1251 {
1252 if (list)
1253 {
1254 FcPatternElt *e = FcPatternInsertElt (p, object);
1255
1256 if (!e)
1257 return;
1258 FcConfigAdd (&e->values, 0, append, list);
1259 }
1260 }
1261
1262 /*
1263 * Delete all values associated with a field
1264 */
1265 static void
1266 FcConfigPatternDel (FcPattern *p,
1267 const char *object)
1268 {
1269 FcPatternElt *e = FcPatternFindElt (p, object);
1270 if (!e)
1271 return;
1272 while (FcValueListPtrU(e->values))
1273 FcConfigDel (&e->values, FcValueListPtrU(e->values));
1274 }
1275
1276 static void
1277 FcConfigPatternCanon (FcPattern *p,
1278 const char *object)
1279 {
1280 FcPatternElt *e = FcPatternFindElt (p, object);
1281 if (!e)
1282 return;
1283 if (!FcValueListPtrU(e->values))
1284 FcPatternDel (p, object);
1285 }
1286
1287 FcBool
1288 FcConfigSubstituteWithPat (FcConfig *config,
1289 FcPattern *p,
1290 FcPattern *p_pat,
1291 FcMatchKind kind)
1292 {
1293 FcSubst *s;
1294 FcSubState *st;
1295 int i;
1296 FcTest *t;
1297 FcEdit *e;
1298 FcValueList *l;
1299 FcPattern *m;
1300
1301 if (!config)
1302 {
1303 config = FcConfigGetCurrent ();
1304 if (!config)
1305 return FcFalse;
1306 }
1307
1308 st = (FcSubState *) malloc (config->maxObjects * sizeof (FcSubState));
1309 if (!st && config->maxObjects)
1310 return FcFalse;
1311 FcMemAlloc (FC_MEM_SUBSTATE, config->maxObjects * sizeof (FcSubState));
1312
1313 if (FcDebug () & FC_DBG_EDIT)
1314 {
1315 printf ("FcConfigSubstitute ");
1316 FcPatternPrint (p);
1317 }
1318 if (kind == FcMatchPattern)
1319 s = config->substPattern;
1320 else
1321 s = config->substFont;
1322 for (; s; s = s->next)
1323 {
1324 /*
1325 * Check the tests to see if
1326 * they all match the pattern
1327 */
1328 for (t = s->test, i = 0; t; t = t->next, i++)
1329 {
1330 if (FcDebug () & FC_DBG_EDIT)
1331 {
1332 printf ("FcConfigSubstitute test ");
1333 FcTestPrint (t);
1334 }
1335 st[i].elt = 0;
1336 if (kind == FcMatchFont && t->kind == FcMatchPattern)
1337 m = p_pat;
1338 else
1339 m = p;
1340 if (m)
1341 st[i].elt = FcPatternFindElt (m, t->field);
1342 else
1343 st[i].elt = 0;
1344 /*
1345 * If there's no such field in the font,
1346 * then FcQualAll matches while FcQualAny does not
1347 */
1348 if (!st[i].elt)
1349 {
1350 if (t->qual == FcQualAll)
1351 {
1352 st[i].value = 0;
1353 continue;
1354 }
1355 else
1356 break;
1357 }
1358 /*
1359 * Check to see if there is a match, mark the location
1360 * to apply match-relative edits
1361 */
1362 st[i].value = FcConfigMatchValueList (m, t, FcValueListPtrU(st[i].elt->values));
1363 if (!st[i].value)
1364 break;
1365 if (t->qual == FcQualFirst && st[i].value != FcValueListPtrU(st[i].elt->values))
1366 break;
1367 if (t->qual == FcQualNotFirst && st[i].value == FcValueListPtrU(st[i].elt->values))
1368 break;
1369 }
1370 if (t)
1371 {
1372 if (FcDebug () & FC_DBG_EDIT)
1373 printf ("No match\n");
1374 continue;
1375 }
1376 if (FcDebug () & FC_DBG_EDIT)
1377 {
1378 printf ("Substitute ");
1379 FcSubstPrint (s);
1380 }
1381 for (e = s->edit; e; e = e->next)
1382 {
1383 /*
1384 * Evaluate the list of expressions
1385 */
1386 l = FcConfigValues (p, e->expr, e->binding);
1387 /*
1388 * Locate any test associated with this field, skipping
1389 * tests associated with the pattern when substituting in
1390 * the font
1391 */
1392 for (t = s->test, i = 0; t; t = t->next, i++)
1393 {
1394 if ((t->kind == FcMatchFont || kind == FcMatchPattern) &&
1395 !FcStrCmpIgnoreCase ((FcChar8 *) t->field,
1396 (FcChar8 *) e->field))
1397 {
1398 /*
1399 * KLUDGE - the pattern may have been reallocated or
1400 * things may have been inserted or deleted above
1401 * this element by other edits. Go back and find
1402 * the element again
1403 */
1404 if (e != s->edit && st[i].elt)
1405 st[i].elt = FcPatternFindElt (p, t->field);
1406 if (!st[i].elt)
1407 t = 0;
1408 break;
1409 }
1410 }
1411 switch (e->op) {
1412 case FcOpAssign:
1413 /*
1414 * If there was a test, then replace the matched
1415 * value with the new list of values
1416 */
1417 if (t)
1418 {
1419 FcValueList *thisValue = st[i].value;
1420 FcValueList *nextValue = thisValue ? FcValueListPtrU(thisValue->next) : 0;
1421
1422 /*
1423 * Append the new list of values after the current value
1424 */
1425 FcConfigAdd (&st[i].elt->values, thisValue, FcTrue, l);
1426 /*
1427 * Delete the marked value
1428 */
1429 FcConfigDel (&st[i].elt->values, thisValue);
1430 /*
1431 * Adjust any pointers into the value list to ensure
1432 * future edits occur at the same place
1433 */
1434 for (t = s->test, i = 0; t; t = t->next, i++)
1435 {
1436 if (st[i].value == thisValue)
1437 st[i].value = nextValue;
1438 }
1439 break;
1440 }
1441 /* fall through ... */
1442 case FcOpAssignReplace:
1443 /*
1444 * Delete all of the values and insert
1445 * the new set
1446 */
1447 FcConfigPatternDel (p, e->field);
1448 FcConfigPatternAdd (p, e->field, l, FcTrue);
1449 /*
1450 * Adjust any pointers into the value list as they no
1451 * longer point to anything valid
1452 */
1453 if (t)
1454 {
1455 FcPatternElt *thisElt = st[i].elt;
1456 for (t = s->test, i = 0; t; t = t->next, i++)
1457 {
1458 if (st[i].elt == thisElt)
1459 st[i].value = 0;
1460 }
1461 }
1462 break;
1463 case FcOpPrepend:
1464 if (t)
1465 {
1466 FcConfigAdd (&st[i].elt->values, st[i].value, FcFalse, l);
1467 break;
1468 }
1469 /* fall through ... */
1470 case FcOpPrependFirst:
1471 FcConfigPatternAdd (p, e->field, l, FcFalse);
1472 break;
1473 case FcOpAppend:
1474 if (t)
1475 {
1476 FcConfigAdd (&st[i].elt->values, st[i].value, FcTrue, l);
1477 break;
1478 }
1479 /* fall through ... */
1480 case FcOpAppendLast:
1481 FcConfigPatternAdd (p, e->field, l, FcTrue);
1482 break;
1483 default:
1484 break;
1485 }
1486 }
1487 /*
1488 * Now go through the pattern and eliminate
1489 * any properties without data
1490 */
1491 for (e = s->edit; e; e = e->next)
1492 FcConfigPatternCanon (p, e->field);
1493
1494 if (FcDebug () & FC_DBG_EDIT)
1495 {
1496 printf ("FcConfigSubstitute edit");
1497 FcPatternPrint (p);
1498 }
1499 }
1500 FcMemFree (FC_MEM_SUBSTATE, config->maxObjects * sizeof (FcSubState));
1501 free (st);
1502 if (FcDebug () & FC_DBG_EDIT)
1503 {
1504 printf ("FcConfigSubstitute done");
1505 FcPatternPrint (p);
1506 }
1507 return FcTrue;
1508 }
1509
1510 FcBool
1511 FcConfigSubstitute (FcConfig *config,
1512 FcPattern *p,
1513 FcMatchKind kind)
1514 {
1515 return FcConfigSubstituteWithPat (config, p, 0, kind);
1516 }
1517
1518 #if defined (_WIN32) && (defined (PIC) || defined (DLL_EXPORT))
1519
1520 static FcChar8 fontconfig_path[1000] = "";
1521
1522 BOOL WINAPI
1523 DllMain (HINSTANCE hinstDLL,
1524 DWORD fdwReason,
1525 LPVOID lpvReserved)
1526 {
1527 FcChar8 *p;
1528
1529 switch (fdwReason) {
1530 case DLL_PROCESS_ATTACH:
1531 if (!GetModuleFileName ((HMODULE) hinstDLL, fontconfig_path,
1532 sizeof (fontconfig_path)))
1533 break;
1534
1535 /* If the fontconfig DLL is in a "bin" or "lib" subfolder,
1536 * assume it's a Unix-style installation tree, and use
1537 * "etc/fonts" in there as FONTCONFIG_PATH. Otherwise use the
1538 * folder where the DLL is as FONTCONFIG_PATH.
1539 */
1540 p = strrchr (fontconfig_path, '\\');
1541 if (p)
1542 {
1543 *p = '\0';
1544 p = strrchr (fontconfig_path, '\\');
1545 if (p && (FcStrCmpIgnoreCase (p + 1, "bin") == 0 ||
1546 FcStrCmpIgnoreCase (p + 1, "lib") == 0))
1547 *p = '\0';
1548 strcat (fontconfig_path, "\\etc\\fonts");
1549 }
1550 else
1551 fontconfig_path[0] = '\0';
1552
1553 break;
1554 }
1555
1556 return TRUE;
1557 }
1558
1559 #undef FONTCONFIG_PATH
1560 #define FONTCONFIG_PATH fontconfig_path
1561
1562 #else /* !(_WIN32 && PIC) */
1563
1564 #endif /* !(_WIN32 && PIC) */
1565
1566 #ifndef FONTCONFIG_FILE
1567 #define FONTCONFIG_FILE "fonts.conf"
1568 #endif
1569
1570 static FcChar8 *
1571 FcConfigFileExists (const FcChar8 *dir, const FcChar8 *file)
1572 {
1573 FcChar8 *path;
1574
1575 if (!dir)
1576 dir = (FcChar8 *) "";
1577 path = malloc (strlen ((char *) dir) + 1 + strlen ((char *) file) + 1);
1578 if (!path)
1579 return 0;
1580
1581 strcpy ((char *) path, (const char *) dir);
1582 /* make sure there's a single separator */
1583 #ifdef _WIN32
1584 if ((!path[0] || (path[strlen((char *) path)-1] != '/' &&
1585 path[strlen((char *) path)-1] != '\\')) &&
1586 !(file[0] == '/' ||
1587 file[0] == '\\' ||
1588 (isalpha (file[0]) && file[1] == ':' && (file[2] == '/' || file[2] == '\\'))))
1589 strcat ((char *) path, "\\");
1590 #else
1591 if ((!path[0] || path[strlen((char *) path)-1] != '/') && file[0] != '/')
1592 strcat ((char *) path, "/");
1593 #endif
1594 strcat ((char *) path, (char *) file);
1595
1596 FcMemAlloc (FC_MEM_STRING, strlen ((char *) path) + 1);
1597 if (access ((char *) path, R_OK) == 0)
1598 return path;
1599
1600 FcStrFree (path);
1601 return 0;
1602 }
1603
1604 static FcChar8 **
1605 FcConfigGetPath (void)
1606 {
1607 FcChar8 **path;
1608 FcChar8 *env, *e, *colon;
1609 FcChar8 *dir;
1610 int npath;
1611 int i;
1612
1613 npath = 2; /* default dir + null */
1614 env = (FcChar8 *) getenv ("FONTCONFIG_PATH");
1615 if (env)
1616 {
1617 e = env;
1618 npath++;
1619 while (*e)
1620 if (*e++ == FC_SEARCH_PATH_SEPARATOR)
1621 npath++;
1622 }
1623 path = calloc (npath, sizeof (FcChar8 *));
1624 if (!path)
1625 goto bail0;
1626 i = 0;
1627
1628 if (env)
1629 {
1630 e = env;
1631 while (*e)
1632 {
1633 colon = (FcChar8 *) strchr ((char *) e, FC_SEARCH_PATH_SEPARATOR);
1634 if (!colon)
1635 colon = e + strlen ((char *) e);
1636 path[i] = malloc (colon - e + 1);
1637 if (!path[i])
1638 goto bail1;
1639 strncpy ((char *) path[i], (const char *) e, colon - e);
1640 path[i][colon - e] = '\0';
1641 if (*colon)
1642 e = colon + 1;
1643 else
1644 e = colon;
1645 i++;
1646 }
1647 }
1648
1649 dir = (FcChar8 *) FONTCONFIG_PATH;
1650 path[i] = malloc (strlen ((char *) dir) + 1);
1651 if (!path[i])
1652 goto bail1;
1653 strcpy ((char *) path[i], (const char *) dir);
1654 return path;
1655
1656 bail1:
1657 for (i = 0; path[i]; i++)
1658 free (path[i]);
1659 free (path);
1660 bail0:
1661 return 0;
1662 }
1663
1664 static void
1665 FcConfigFreePath (FcChar8 **path)
1666 {
1667 FcChar8 **p;
1668
1669 for (p = path; *p; p++)
1670 free (*p);
1671 free (path);
1672 }
1673
1674 static FcBool _FcConfigHomeEnabled = FcTrue;
1675
1676 FcChar8 *
1677 FcConfigHome (void)
1678 {
1679 if (_FcConfigHomeEnabled)
1680 {
1681 char *home = getenv ("HOME");
1682
1683 #ifdef _WIN32
1684 if (home == NULL)
1685 home = getenv ("USERPROFILE");
1686 #endif
1687
1688 return (FcChar8 *) home;
1689 }
1690 return 0;
1691 }
1692
1693 FcBool
1694 FcConfigEnableHome (FcBool enable)
1695 {
1696 FcBool prev = _FcConfigHomeEnabled;
1697 _FcConfigHomeEnabled = enable;
1698 return prev;
1699 }
1700
1701 FcChar8 *
1702 FcConfigFilename (const FcChar8 *url)
1703 {
1704 FcChar8 *file, *dir, **path, **p;
1705
1706 if (!url || !*url)
1707 {
1708 url = (FcChar8 *) getenv ("FONTCONFIG_FILE");
1709 if (!url)
1710 url = (FcChar8 *) FONTCONFIG_FILE;
1711 }
1712 file = 0;
1713
1714 #ifdef _WIN32
1715 if (isalpha (*url) &&
1716 url[1] == ':' &&
1717 (url[2] == '/' || url[2] == '\\'))
1718 goto absolute_path;
1719 #endif
1720
1721 switch (*url) {
1722 case '~':
1723 dir = FcConfigHome ();
1724 if (dir)
1725 file = FcConfigFileExists (dir, url + 1);
1726 else
1727 file = 0;
1728 break;
1729 #ifdef _WIN32
1730 case '\\':
1731 absolute_path:
1732 #endif
1733 case '/':
1734 file = FcConfigFileExists (0, url);
1735 break;
1736 default:
1737 path = FcConfigGetPath ();
1738 if (!path)
1739 return 0;
1740 for (p = path; *p; p++)
1741 {
1742 file = FcConfigFileExists (*p, url);
1743 if (file)
1744 break;
1745 }
1746 FcConfigFreePath (path);
1747 break;
1748 }
1749 return file;
1750 }
1751
1752 /*
1753 * Manage the application-specific fonts
1754 */
1755
1756 FcBool
1757 FcConfigAppFontAddFile (FcConfig *config,
1758 const FcChar8 *file)
1759 {
1760 FcFontSet *set;
1761 FcStrSet *subdirs;
1762 FcStrList *sublist;
1763 FcChar8 *subdir;
1764
1765 if (!config)
1766 {
1767 config = FcConfigGetCurrent ();
1768 if (!config)
1769 return FcFalse;
1770 }
1771
1772 subdirs = FcStrSetCreate ();
1773 if (!subdirs)
1774 return FcFalse;
1775
1776 set = FcConfigGetFonts (config, FcSetApplication);
1777 if (!set)
1778 {
1779 set = FcFontSetCreate ();
1780 if (!set)
1781 {
1782 FcStrSetDestroy (subdirs);
1783 return FcFalse;
1784 }
1785 FcConfigSetFonts (config, set, FcSetApplication);
1786 }
1787
1788 if (!FcFileScanConfig (set, subdirs, 0, config->blanks, file, FcFalse, config))
1789 {
1790 FcStrSetDestroy (subdirs);
1791 return FcFalse;
1792 }
1793 if ((sublist = FcStrListCreate (subdirs)))
1794 {
1795 while ((subdir = FcStrListNext (sublist)))
1796 {
1797 FcConfigAppFontAddDir (config, subdir);
1798 }
1799 FcStrListDone (sublist);
1800 }
1801 return FcTrue;
1802 }
1803
1804 FcBool
1805 FcConfigAppFontAddDir (FcConfig *config,
1806 const FcChar8 *dir)
1807 {
1808 FcFontSet *set;
1809 FcStrSet *subdirs;
1810 FcStrList *sublist;
1811 FcChar8 *subdir;
1812
1813 if (!config)
1814 {
1815 config = FcConfigGetCurrent ();
1816 if (!config)
1817 return FcFalse;
1818 }
1819 subdirs = FcStrSetCreate ();
1820 if (!subdirs)
1821 return FcFalse;
1822
1823 set = FcConfigGetFonts (config, FcSetApplication);
1824 if (!set)
1825 {
1826 set = FcFontSetCreate ();
1827 if (!set)
1828 {
1829 FcStrSetDestroy (subdirs);
1830 return FcFalse;
1831 }
1832 FcConfigSetFonts (config, set, FcSetApplication);
1833 }
1834
1835 if (!FcDirScanConfig (set, subdirs, 0, config->blanks, dir, FcFalse, config))
1836 {
1837 FcStrSetDestroy (subdirs);
1838 return FcFalse;
1839 }
1840 if ((sublist = FcStrListCreate (subdirs)))
1841 {
1842 while ((subdir = FcStrListNext (sublist)))
1843 {
1844 FcConfigAppFontAddDir (config, subdir);
1845 }
1846 FcStrListDone (sublist);
1847 }
1848 return FcTrue;
1849 }
1850
1851 void
1852 FcConfigAppFontClear (FcConfig *config)
1853 {
1854 if (!config)
1855 {
1856 config = FcConfigGetCurrent ();
1857 if (!config)
1858 return;
1859 }
1860
1861 FcConfigSetFonts (config, 0, FcSetApplication);
1862 }
1863
1864 /*
1865 * Manage filename-based font source selectors
1866 */
1867
1868 FcBool
1869 FcConfigGlobAdd (FcConfig *config,
1870 const FcChar8 *glob,
1871 FcBool accept)
1872 {
1873 FcStrSet *set = accept ? config->acceptGlobs : config->rejectGlobs;
1874
1875 return FcStrSetAdd (set, glob);
1876 }
1877
1878 static FcBool
1879 FcConfigGlobMatch (const FcChar8 *glob,
1880 const FcChar8 *string)
1881 {
1882 FcChar8 c;
1883
1884 while ((c = *glob++))
1885 {
1886 switch (c) {
1887 case '*':
1888 /* short circuit common case */
1889 if (!*glob)
1890 return FcTrue;
1891 /* short circuit another common case */
1892 if (strchr ((char *) glob, '*') == 0)
1893 string += strlen ((char *) string) - strlen ((char *) glob);
1894 while (*string)
1895 {
1896 if (FcConfigGlobMatch (glob, string))
1897 return FcTrue;
1898 string++;
1899 }
1900 return FcFalse;
1901 case '?':
1902 if (*string++ == '\0')
1903 return FcFalse;
1904 break;
1905 default:
1906 if (*string++ != c)
1907 return FcFalse;
1908 break;
1909 }
1910 }
1911 return *string == '\0';
1912 }
1913
1914 static FcBool
1915 FcConfigGlobsMatch (const FcStrSet *globs,
1916 const FcChar8 *string)
1917 {
1918 int i;
1919
1920 for (i = 0; i < globs->num; i++)
1921 if (FcConfigGlobMatch (globs->strs[i], string))
1922 return FcTrue;
1923 return FcFalse;
1924 }
1925
1926 FcBool
1927 FcConfigAcceptFilename (FcConfig *config,
1928 const FcChar8 *filename)
1929 {
1930 if (FcConfigGlobsMatch (config->acceptGlobs, filename))
1931 return FcTrue;
1932 if (FcConfigGlobsMatch (config->rejectGlobs, filename))
1933 return FcFalse;
1934 return FcTrue;
1935 }
1936
1937 /*
1938 * Manage font-pattern based font source selectors
1939 */
1940
1941 FcBool
1942 FcConfigPatternsAdd (FcConfig *config,
1943 FcPattern *pattern,
1944 FcBool accept)
1945 {
1946 FcFontSet *set = accept ? config->acceptPatterns : config->rejectPatterns;
1947
1948 return FcFontSetAdd (set, pattern);
1949 }
1950
1951 static FcBool
1952 FcConfigPatternsMatch (const FcFontSet *patterns,
1953 const FcPattern *font)
1954 {
1955 int i;
1956
1957 for (i = 0; i < patterns->nfont; i++)
1958 if (FcListPatternMatchAny (patterns->fonts[i], font))
1959 return FcTrue;
1960 return FcFalse;
1961 }
1962
1963 FcBool
1964 FcConfigAcceptFont (FcConfig *config,
1965 const FcPattern *font)
1966 {
1967 if (FcConfigPatternsMatch (config->acceptPatterns, font))
1968 return FcTrue;
1969 if (FcConfigPatternsMatch (config->rejectPatterns, font))
1970 return FcFalse;
1971 return FcTrue;
1972 }