]> git.wh0rd.org - fontconfig.git/blob - src/fccfg.c
Fix additional memory leaks reported by Ronny V. Vindenes: don't invoke
[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 break;
831 case FcOpMatrix:
832 v.type = FcTypeMatrix;
833 v.u.m = e->u.mval;
834 v = FcValueSave (v);
835 break;
836 case FcOpCharSet:
837 v.type = FcTypeCharSet;
838 v.u.c = e->u.cval;
839 v = FcValueSave (v);
840 break;
841 case FcOpBool:
842 v.type = FcTypeBool;
843 v.u.b = e->u.bval;
844 break;
845 case FcOpField:
846 r = FcPatternGet (p, e->u.field, 0, &v);
847 if (r != FcResultMatch)
848 v.type = FcTypeVoid;
849 v = FcValueSave (v);
850 break;
851 case FcOpConst:
852 if (FcNameConstant (e->u.constant, &v.u.i))
853 v.type = FcTypeInteger;
854 else
855 v.type = FcTypeVoid;
856 break;
857 case FcOpQuest:
858 vl = FcConfigEvaluate (p, e->u.tree.left);
859 if (vl.type == FcTypeBool)
860 {
861 if (vl.u.b)
862 v = FcConfigEvaluate (p, e->u.tree.right->u.tree.left);
863 else
864 v = FcConfigEvaluate (p, e->u.tree.right->u.tree.right);
865 }
866 else
867 v.type = FcTypeVoid;
868 FcValueDestroy (vl);
869 break;
870 case FcOpEqual:
871 case FcOpNotEqual:
872 case FcOpLess:
873 case FcOpLessEqual:
874 case FcOpMore:
875 case FcOpMoreEqual:
876 case FcOpContains:
877 case FcOpNotContains:
878 case FcOpListing:
879 vl = FcConfigEvaluate (p, e->u.tree.left);
880 vr = FcConfigEvaluate (p, e->u.tree.right);
881 v.type = FcTypeBool;
882 v.u.b = FcConfigCompareValue (&vl, e->op, &vr);
883 FcValueDestroy (vl);
884 FcValueDestroy (vr);
885 break;
886 case FcOpOr:
887 case FcOpAnd:
888 case FcOpPlus:
889 case FcOpMinus:
890 case FcOpTimes:
891 case FcOpDivide:
892 vl = FcConfigEvaluate (p, e->u.tree.left);
893 vr = FcConfigEvaluate (p, e->u.tree.right);
894 vl = FcConfigPromote (vl, vr);
895 vr = FcConfigPromote (vr, vl);
896 if (vl.type == vr.type)
897 {
898 switch (vl.type) {
899 case FcTypeDouble:
900 switch (e->op) {
901 case FcOpPlus:
902 v.type = FcTypeDouble;
903 v.u.d = vl.u.d + vr.u.d;
904 break;
905 case FcOpMinus:
906 v.type = FcTypeDouble;
907 v.u.d = vl.u.d - vr.u.d;
908 break;
909 case FcOpTimes:
910 v.type = FcTypeDouble;
911 v.u.d = vl.u.d * vr.u.d;
912 break;
913 case FcOpDivide:
914 v.type = FcTypeDouble;
915 v.u.d = vl.u.d / vr.u.d;
916 break;
917 default:
918 v.type = FcTypeVoid;
919 break;
920 }
921 if (v.type == FcTypeDouble &&
922 v.u.d == (double) (int) v.u.d)
923 {
924 v.type = FcTypeInteger;
925 v.u.i = (int) v.u.d;
926 }
927 break;
928 case FcTypeBool:
929 switch (e->op) {
930 case FcOpOr:
931 v.type = FcTypeBool;
932 v.u.b = vl.u.b || vr.u.b;
933 break;
934 case FcOpAnd:
935 v.type = FcTypeBool;
936 v.u.b = vl.u.b && vr.u.b;
937 break;
938 default:
939 v.type = FcTypeVoid;
940 break;
941 }
942 break;
943 case FcTypeString:
944 switch (e->op) {
945 case FcOpPlus:
946 v.type = FcTypeString;
947 v.u.s = FcStrStaticName (FcStrPlus (vl.u.s, vr.u.s));
948
949 if (!v.u.s)
950 v.type = FcTypeVoid;
951 break;
952 default:
953 v.type = FcTypeVoid;
954 break;
955 }
956 break;
957 case FcTypeMatrix:
958 switch (e->op) {
959 case FcOpTimes:
960 v.type = FcTypeMatrix;
961 m = malloc (sizeof (FcMatrix));
962 if (m)
963 {
964 FcMemAlloc (FC_MEM_MATRIX, sizeof (FcMatrix));
965 FcMatrixMultiply (m, vl.u.m, vr.u.m);
966 v.u.m = m;
967 }
968 else
969 {
970 v.type = FcTypeVoid;
971 }
972 break;
973 default:
974 v.type = FcTypeVoid;
975 break;
976 }
977 break;
978 default:
979 v.type = FcTypeVoid;
980 break;
981 }
982 }
983 else
984 v.type = FcTypeVoid;
985 FcValueDestroy (vl);
986 FcValueDestroy (vr);
987 break;
988 case FcOpNot:
989 vl = FcConfigEvaluate (p, e->u.tree.left);
990 switch (vl.type) {
991 case FcTypeBool:
992 v.type = FcTypeBool;
993 v.u.b = !vl.u.b;
994 break;
995 default:
996 v.type = FcTypeVoid;
997 break;
998 }
999 FcValueDestroy (vl);
1000 break;
1001 case FcOpFloor:
1002 vl = FcConfigEvaluate (p, e->u.tree.left);
1003 switch (vl.type) {
1004 case FcTypeInteger:
1005 v = vl;
1006 break;
1007 case FcTypeDouble:
1008 v.type = FcTypeInteger;
1009 v.u.i = FcDoubleFloor (vl.u.d);
1010 break;
1011 default:
1012 v.type = FcTypeVoid;
1013 break;
1014 }
1015 FcValueDestroy (vl);
1016 break;
1017 case FcOpCeil:
1018 vl = FcConfigEvaluate (p, e->u.tree.left);
1019 switch (vl.type) {
1020 case FcTypeInteger:
1021 v = vl;
1022 break;
1023 case FcTypeDouble:
1024 v.type = FcTypeInteger;
1025 v.u.i = FcDoubleCeil (vl.u.d);
1026 break;
1027 default:
1028 v.type = FcTypeVoid;
1029 break;
1030 }
1031 FcValueDestroy (vl);
1032 break;
1033 case FcOpRound:
1034 vl = FcConfigEvaluate (p, e->u.tree.left);
1035 switch (vl.type) {
1036 case FcTypeInteger:
1037 v = vl;
1038 break;
1039 case FcTypeDouble:
1040 v.type = FcTypeInteger;
1041 v.u.i = FcDoubleRound (vl.u.d);
1042 break;
1043 default:
1044 v.type = FcTypeVoid;
1045 break;
1046 }
1047 FcValueDestroy (vl);
1048 break;
1049 case FcOpTrunc:
1050 vl = FcConfigEvaluate (p, e->u.tree.left);
1051 switch (vl.type) {
1052 case FcTypeInteger:
1053 v = vl;
1054 break;
1055 case FcTypeDouble:
1056 v.type = FcTypeInteger;
1057 v.u.i = FcDoubleTrunc (vl.u.d);
1058 break;
1059 default:
1060 v.type = FcTypeVoid;
1061 break;
1062 }
1063 FcValueDestroy (vl);
1064 break;
1065 default:
1066 v.type = FcTypeVoid;
1067 break;
1068 }
1069 return v;
1070 }
1071
1072 static FcValueList *
1073 FcConfigMatchValueList (FcPattern *p,
1074 FcTest *t,
1075 FcValueList *values)
1076 {
1077 FcValueList *ret = 0;
1078 FcExpr *e = t->expr;
1079 FcValue value;
1080 FcValueList *v;
1081
1082 while (e)
1083 {
1084 /* Compute the value of the match expression */
1085 if (e->op == FcOpComma)
1086 {
1087 value = FcConfigEvaluate (p, e->u.tree.left);
1088 e = e->u.tree.right;
1089 }
1090 else
1091 {
1092 value = FcConfigEvaluate (p, e);
1093 e = 0;
1094 }
1095
1096 for (v = values; v; v = FcValueListPtrU(v->next))
1097 {
1098 /* Compare the pattern value to the match expression value */
1099 if (FcConfigCompareValue (&v->value, t->op, &value))
1100 {
1101 if (!ret)
1102 ret = v;
1103 }
1104 else
1105 {
1106 if (t->qual == FcQualAll)
1107 {
1108 ret = 0;
1109 break;
1110 }
1111 }
1112 }
1113 FcValueDestroy (value);
1114 }
1115 return ret;
1116 }
1117
1118 static FcValueList *
1119 FcConfigValues (FcPattern *p, FcExpr *e, FcValueBinding binding)
1120 {
1121 FcValueList *l;
1122
1123 if (!e)
1124 return 0;
1125 l = (FcValueList *) malloc (sizeof (FcValueList));
1126 if (!l)
1127 return 0;
1128 FcMemAlloc (FC_MEM_VALLIST, sizeof (FcValueList));
1129 if (e->op == FcOpComma)
1130 {
1131 l->value = FcConfigEvaluate (p, e->u.tree.left);
1132 l->next = FcValueListPtrCreateDynamic(FcConfigValues (p, e->u.tree.right, binding));
1133 }
1134 else
1135 {
1136 l->value = FcConfigEvaluate (p, e);
1137 l->next = FcValueListPtrCreateDynamic(0);
1138 }
1139 l->binding = binding;
1140 if (l->value.type == FcTypeVoid)
1141 {
1142 FcValueList *next = FcValueListPtrU(l->next);
1143
1144 FcMemFree (FC_MEM_VALLIST, sizeof (FcValueList));
1145 free (l);
1146 l = next;
1147 }
1148
1149 return l;
1150 }
1151
1152 static FcBool
1153 FcConfigAdd (FcValueListPtr *head,
1154 FcValueList *position,
1155 FcBool append,
1156 FcValueList *new)
1157 {
1158 FcValueListPtr *prev, last, v;
1159 FcValueBinding sameBinding;
1160
1161 if (position)
1162 sameBinding = position->binding;
1163 else
1164 sameBinding = FcValueBindingWeak;
1165 for (v = FcValueListPtrCreateDynamic(new); FcValueListPtrU(v);
1166 v = FcValueListPtrU(v)->next)
1167 if (FcValueListPtrU(v)->binding == FcValueBindingSame)
1168 FcValueListPtrU(v)->binding = sameBinding;
1169 if (append)
1170 {
1171 if (position)
1172 prev = &position->next;
1173 else
1174 for (prev = head; FcValueListPtrU(*prev);
1175 prev = &(FcValueListPtrU(*prev)->next))
1176 ;
1177 }
1178 else
1179 {
1180 if (position)
1181 {
1182 for (prev = head; FcValueListPtrU(*prev);
1183 prev = &(FcValueListPtrU(*prev)->next))
1184 {
1185 if (FcValueListPtrU(*prev) == position)
1186 break;
1187 }
1188 }
1189 else
1190 prev = head;
1191
1192 if (FcDebug () & FC_DBG_EDIT)
1193 {
1194 if (!FcValueListPtrU(*prev))
1195 printf ("position not on list\n");
1196 }
1197 }
1198
1199 if (FcDebug () & FC_DBG_EDIT)
1200 {
1201 printf ("%s list before ", append ? "Append" : "Prepend");
1202 FcValueListPrint (*head);
1203 printf ("\n");
1204 }
1205
1206 if (new)
1207 {
1208 last = FcValueListPtrCreateDynamic(new);
1209 while (FcValueListPtrU(FcValueListPtrU(last)->next))
1210 last = FcValueListPtrU(last)->next;
1211
1212 FcValueListPtrU(last)->next = *prev;
1213 *prev = FcValueListPtrCreateDynamic(new);
1214 }
1215
1216 if (FcDebug () & FC_DBG_EDIT)
1217 {
1218 printf ("%s list after ", append ? "Append" : "Prepend");
1219 FcValueListPrint (*head);
1220 printf ("\n");
1221 }
1222
1223 return FcTrue;
1224 }
1225
1226 static void
1227 FcConfigDel (FcValueListPtr *head,
1228 FcValueList *position)
1229 {
1230 FcValueListPtr *prev;
1231
1232 for (prev = head; FcValueListPtrU(*prev);
1233 prev = &(FcValueListPtrU(*prev)->next))
1234 {
1235 if (FcValueListPtrU(*prev) == position)
1236 {
1237 *prev = position->next;
1238 position->next = FcValueListPtrCreateDynamic(0);
1239 FcValueListDestroy (FcValueListPtrCreateDynamic(position));
1240 break;
1241 }
1242 }
1243 }
1244
1245 static void
1246 FcConfigPatternAdd (FcPattern *p,
1247 const char *object,
1248 FcValueList *list,
1249 FcBool append)
1250 {
1251 if (list)
1252 {
1253 FcPatternElt *e = FcPatternInsertElt (p, object);
1254
1255 if (!e)
1256 return;
1257 FcConfigAdd (&e->values, 0, append, list);
1258 }
1259 }
1260
1261 /*
1262 * Delete all values associated with a field
1263 */
1264 static void
1265 FcConfigPatternDel (FcPattern *p,
1266 const char *object)
1267 {
1268 FcPatternElt *e = FcPatternFindElt (p, object);
1269 if (!e)
1270 return;
1271 while (FcValueListPtrU(e->values))
1272 FcConfigDel (&e->values, FcValueListPtrU(e->values));
1273 }
1274
1275 static void
1276 FcConfigPatternCanon (FcPattern *p,
1277 const char *object)
1278 {
1279 FcPatternElt *e = FcPatternFindElt (p, object);
1280 if (!e)
1281 return;
1282 if (!FcValueListPtrU(e->values))
1283 FcPatternDel (p, object);
1284 }
1285
1286 FcBool
1287 FcConfigSubstituteWithPat (FcConfig *config,
1288 FcPattern *p,
1289 FcPattern *p_pat,
1290 FcMatchKind kind)
1291 {
1292 FcSubst *s;
1293 FcSubState *st;
1294 int i;
1295 FcTest *t;
1296 FcEdit *e;
1297 FcValueList *l;
1298 FcPattern *m;
1299
1300 if (!config)
1301 {
1302 config = FcConfigGetCurrent ();
1303 if (!config)
1304 return FcFalse;
1305 }
1306
1307 st = (FcSubState *) malloc (config->maxObjects * sizeof (FcSubState));
1308 if (!st && config->maxObjects)
1309 return FcFalse;
1310 FcMemAlloc (FC_MEM_SUBSTATE, config->maxObjects * sizeof (FcSubState));
1311
1312 if (FcDebug () & FC_DBG_EDIT)
1313 {
1314 printf ("FcConfigSubstitute ");
1315 FcPatternPrint (p);
1316 }
1317 if (kind == FcMatchPattern)
1318 s = config->substPattern;
1319 else
1320 s = config->substFont;
1321 for (; s; s = s->next)
1322 {
1323 /*
1324 * Check the tests to see if
1325 * they all match the pattern
1326 */
1327 for (t = s->test, i = 0; t; t = t->next, i++)
1328 {
1329 if (FcDebug () & FC_DBG_EDIT)
1330 {
1331 printf ("FcConfigSubstitute test ");
1332 FcTestPrint (t);
1333 }
1334 st[i].elt = 0;
1335 if (kind == FcMatchFont && t->kind == FcMatchPattern)
1336 m = p_pat;
1337 else
1338 m = p;
1339 if (m)
1340 st[i].elt = FcPatternFindElt (m, t->field);
1341 else
1342 st[i].elt = 0;
1343 /*
1344 * If there's no such field in the font,
1345 * then FcQualAll matches while FcQualAny does not
1346 */
1347 if (!st[i].elt)
1348 {
1349 if (t->qual == FcQualAll)
1350 {
1351 st[i].value = 0;
1352 continue;
1353 }
1354 else
1355 break;
1356 }
1357 /*
1358 * Check to see if there is a match, mark the location
1359 * to apply match-relative edits
1360 */
1361 st[i].value = FcConfigMatchValueList (m, t, FcValueListPtrU(st[i].elt->values));
1362 if (!st[i].value)
1363 break;
1364 if (t->qual == FcQualFirst && st[i].value != FcValueListPtrU(st[i].elt->values))
1365 break;
1366 if (t->qual == FcQualNotFirst && st[i].value == FcValueListPtrU(st[i].elt->values))
1367 break;
1368 }
1369 if (t)
1370 {
1371 if (FcDebug () & FC_DBG_EDIT)
1372 printf ("No match\n");
1373 continue;
1374 }
1375 if (FcDebug () & FC_DBG_EDIT)
1376 {
1377 printf ("Substitute ");
1378 FcSubstPrint (s);
1379 }
1380 for (e = s->edit; e; e = e->next)
1381 {
1382 /*
1383 * Evaluate the list of expressions
1384 */
1385 l = FcConfigValues (p, e->expr, e->binding);
1386 /*
1387 * Locate any test associated with this field, skipping
1388 * tests associated with the pattern when substituting in
1389 * the font
1390 */
1391 for (t = s->test, i = 0; t; t = t->next, i++)
1392 {
1393 if ((t->kind == FcMatchFont || kind == FcMatchPattern) &&
1394 !FcStrCmpIgnoreCase ((FcChar8 *) t->field,
1395 (FcChar8 *) e->field))
1396 {
1397 /*
1398 * KLUDGE - the pattern may have been reallocated or
1399 * things may have been inserted or deleted above
1400 * this element by other edits. Go back and find
1401 * the element again
1402 */
1403 if (e != s->edit && st[i].elt)
1404 st[i].elt = FcPatternFindElt (p, t->field);
1405 if (!st[i].elt)
1406 t = 0;
1407 break;
1408 }
1409 }
1410 switch (e->op) {
1411 case FcOpAssign:
1412 /*
1413 * If there was a test, then replace the matched
1414 * value with the new list of values
1415 */
1416 if (t)
1417 {
1418 FcValueList *thisValue = st[i].value;
1419 FcValueList *nextValue = thisValue ? FcValueListPtrU(thisValue->next) : 0;
1420
1421 /*
1422 * Append the new list of values after the current value
1423 */
1424 FcConfigAdd (&st[i].elt->values, thisValue, FcTrue, l);
1425 /*
1426 * Delete the marked value
1427 */
1428 FcConfigDel (&st[i].elt->values, thisValue);
1429 /*
1430 * Adjust any pointers into the value list to ensure
1431 * future edits occur at the same place
1432 */
1433 for (t = s->test, i = 0; t; t = t->next, i++)
1434 {
1435 if (st[i].value == thisValue)
1436 st[i].value = nextValue;
1437 }
1438 break;
1439 }
1440 /* fall through ... */
1441 case FcOpAssignReplace:
1442 /*
1443 * Delete all of the values and insert
1444 * the new set
1445 */
1446 FcConfigPatternDel (p, e->field);
1447 FcConfigPatternAdd (p, e->field, l, FcTrue);
1448 /*
1449 * Adjust any pointers into the value list as they no
1450 * longer point to anything valid
1451 */
1452 if (t)
1453 {
1454 FcPatternElt *thisElt = st[i].elt;
1455 for (t = s->test, i = 0; t; t = t->next, i++)
1456 {
1457 if (st[i].elt == thisElt)
1458 st[i].value = 0;
1459 }
1460 }
1461 break;
1462 case FcOpPrepend:
1463 if (t)
1464 {
1465 FcConfigAdd (&st[i].elt->values, st[i].value, FcFalse, l);
1466 break;
1467 }
1468 /* fall through ... */
1469 case FcOpPrependFirst:
1470 FcConfigPatternAdd (p, e->field, l, FcFalse);
1471 break;
1472 case FcOpAppend:
1473 if (t)
1474 {
1475 FcConfigAdd (&st[i].elt->values, st[i].value, FcTrue, l);
1476 break;
1477 }
1478 /* fall through ... */
1479 case FcOpAppendLast:
1480 FcConfigPatternAdd (p, e->field, l, FcTrue);
1481 break;
1482 default:
1483 break;
1484 }
1485 }
1486 /*
1487 * Now go through the pattern and eliminate
1488 * any properties without data
1489 */
1490 for (e = s->edit; e; e = e->next)
1491 FcConfigPatternCanon (p, e->field);
1492
1493 if (FcDebug () & FC_DBG_EDIT)
1494 {
1495 printf ("FcConfigSubstitute edit");
1496 FcPatternPrint (p);
1497 }
1498 }
1499 FcMemFree (FC_MEM_SUBSTATE, config->maxObjects * sizeof (FcSubState));
1500 free (st);
1501 if (FcDebug () & FC_DBG_EDIT)
1502 {
1503 printf ("FcConfigSubstitute done");
1504 FcPatternPrint (p);
1505 }
1506 return FcTrue;
1507 }
1508
1509 FcBool
1510 FcConfigSubstitute (FcConfig *config,
1511 FcPattern *p,
1512 FcMatchKind kind)
1513 {
1514 return FcConfigSubstituteWithPat (config, p, 0, kind);
1515 }
1516
1517 #if defined (_WIN32) && (defined (PIC) || defined (DLL_EXPORT))
1518
1519 static FcChar8 fontconfig_path[1000] = "";
1520
1521 BOOL WINAPI
1522 DllMain (HINSTANCE hinstDLL,
1523 DWORD fdwReason,
1524 LPVOID lpvReserved)
1525 {
1526 FcChar8 *p;
1527
1528 switch (fdwReason) {
1529 case DLL_PROCESS_ATTACH:
1530 if (!GetModuleFileName ((HMODULE) hinstDLL, fontconfig_path,
1531 sizeof (fontconfig_path)))
1532 break;
1533
1534 /* If the fontconfig DLL is in a "bin" or "lib" subfolder,
1535 * assume it's a Unix-style installation tree, and use
1536 * "etc/fonts" in there as FONTCONFIG_PATH. Otherwise use the
1537 * folder where the DLL is as FONTCONFIG_PATH.
1538 */
1539 p = strrchr (fontconfig_path, '\\');
1540 if (p)
1541 {
1542 *p = '\0';
1543 p = strrchr (fontconfig_path, '\\');
1544 if (p && (FcStrCmpIgnoreCase (p + 1, "bin") == 0 ||
1545 FcStrCmpIgnoreCase (p + 1, "lib") == 0))
1546 *p = '\0';
1547 strcat (fontconfig_path, "\\etc\\fonts");
1548 }
1549 else
1550 fontconfig_path[0] = '\0';
1551
1552 break;
1553 }
1554
1555 return TRUE;
1556 }
1557
1558 #undef FONTCONFIG_PATH
1559 #define FONTCONFIG_PATH fontconfig_path
1560
1561 #else /* !(_WIN32 && PIC) */
1562
1563 #endif /* !(_WIN32 && PIC) */
1564
1565 #ifndef FONTCONFIG_FILE
1566 #define FONTCONFIG_FILE "fonts.conf"
1567 #endif
1568
1569 static FcChar8 *
1570 FcConfigFileExists (const FcChar8 *dir, const FcChar8 *file)
1571 {
1572 FcChar8 *path;
1573
1574 if (!dir)
1575 dir = (FcChar8 *) "";
1576 path = malloc (strlen ((char *) dir) + 1 + strlen ((char *) file) + 1);
1577 if (!path)
1578 return 0;
1579
1580 strcpy ((char *) path, (const char *) dir);
1581 /* make sure there's a single separator */
1582 #ifdef _WIN32
1583 if ((!path[0] || (path[strlen((char *) path)-1] != '/' &&
1584 path[strlen((char *) path)-1] != '\\')) &&
1585 !(file[0] == '/' ||
1586 file[0] == '\\' ||
1587 (isalpha (file[0]) && file[1] == ':' && (file[2] == '/' || file[2] == '\\'))))
1588 strcat ((char *) path, "\\");
1589 #else
1590 if ((!path[0] || path[strlen((char *) path)-1] != '/') && file[0] != '/')
1591 strcat ((char *) path, "/");
1592 #endif
1593 strcat ((char *) path, (char *) file);
1594
1595 FcMemAlloc (FC_MEM_STRING, strlen ((char *) path) + 1);
1596 if (access ((char *) path, R_OK) == 0)
1597 return path;
1598
1599 FcStrFree (path);
1600 return 0;
1601 }
1602
1603 static FcChar8 **
1604 FcConfigGetPath (void)
1605 {
1606 FcChar8 **path;
1607 FcChar8 *env, *e, *colon;
1608 FcChar8 *dir;
1609 int npath;
1610 int i;
1611
1612 npath = 2; /* default dir + null */
1613 env = (FcChar8 *) getenv ("FONTCONFIG_PATH");
1614 if (env)
1615 {
1616 e = env;
1617 npath++;
1618 while (*e)
1619 if (*e++ == FC_SEARCH_PATH_SEPARATOR)
1620 npath++;
1621 }
1622 path = calloc (npath, sizeof (FcChar8 *));
1623 if (!path)
1624 goto bail0;
1625 i = 0;
1626
1627 if (env)
1628 {
1629 e = env;
1630 while (*e)
1631 {
1632 colon = (FcChar8 *) strchr ((char *) e, FC_SEARCH_PATH_SEPARATOR);
1633 if (!colon)
1634 colon = e + strlen ((char *) e);
1635 path[i] = malloc (colon - e + 1);
1636 if (!path[i])
1637 goto bail1;
1638 strncpy ((char *) path[i], (const char *) e, colon - e);
1639 path[i][colon - e] = '\0';
1640 if (*colon)
1641 e = colon + 1;
1642 else
1643 e = colon;
1644 i++;
1645 }
1646 }
1647
1648 dir = (FcChar8 *) FONTCONFIG_PATH;
1649 path[i] = malloc (strlen ((char *) dir) + 1);
1650 if (!path[i])
1651 goto bail1;
1652 strcpy ((char *) path[i], (const char *) dir);
1653 return path;
1654
1655 bail1:
1656 for (i = 0; path[i]; i++)
1657 free (path[i]);
1658 free (path);
1659 bail0:
1660 return 0;
1661 }
1662
1663 static void
1664 FcConfigFreePath (FcChar8 **path)
1665 {
1666 FcChar8 **p;
1667
1668 for (p = path; *p; p++)
1669 free (*p);
1670 free (path);
1671 }
1672
1673 static FcBool _FcConfigHomeEnabled = FcTrue;
1674
1675 FcChar8 *
1676 FcConfigHome (void)
1677 {
1678 if (_FcConfigHomeEnabled)
1679 {
1680 char *home = getenv ("HOME");
1681
1682 #ifdef _WIN32
1683 if (home == NULL)
1684 home = getenv ("USERPROFILE");
1685 #endif
1686
1687 return (FcChar8 *) home;
1688 }
1689 return 0;
1690 }
1691
1692 FcBool
1693 FcConfigEnableHome (FcBool enable)
1694 {
1695 FcBool prev = _FcConfigHomeEnabled;
1696 _FcConfigHomeEnabled = enable;
1697 return prev;
1698 }
1699
1700 FcChar8 *
1701 FcConfigFilename (const FcChar8 *url)
1702 {
1703 FcChar8 *file, *dir, **path, **p;
1704
1705 if (!url || !*url)
1706 {
1707 url = (FcChar8 *) getenv ("FONTCONFIG_FILE");
1708 if (!url)
1709 url = (FcChar8 *) FONTCONFIG_FILE;
1710 }
1711 file = 0;
1712
1713 #ifdef _WIN32
1714 if (isalpha (*url) &&
1715 url[1] == ':' &&
1716 (url[2] == '/' || url[2] == '\\'))
1717 goto absolute_path;
1718 #endif
1719
1720 switch (*url) {
1721 case '~':
1722 dir = FcConfigHome ();
1723 if (dir)
1724 file = FcConfigFileExists (dir, url + 1);
1725 else
1726 file = 0;
1727 break;
1728 #ifdef _WIN32
1729 case '\\':
1730 absolute_path:
1731 #endif
1732 case '/':
1733 file = FcConfigFileExists (0, url);
1734 break;
1735 default:
1736 path = FcConfigGetPath ();
1737 if (!path)
1738 return 0;
1739 for (p = path; *p; p++)
1740 {
1741 file = FcConfigFileExists (*p, url);
1742 if (file)
1743 break;
1744 }
1745 FcConfigFreePath (path);
1746 break;
1747 }
1748 return file;
1749 }
1750
1751 /*
1752 * Manage the application-specific fonts
1753 */
1754
1755 FcBool
1756 FcConfigAppFontAddFile (FcConfig *config,
1757 const FcChar8 *file)
1758 {
1759 FcFontSet *set;
1760 FcStrSet *subdirs;
1761 FcStrList *sublist;
1762 FcChar8 *subdir;
1763
1764 if (!config)
1765 {
1766 config = FcConfigGetCurrent ();
1767 if (!config)
1768 return FcFalse;
1769 }
1770
1771 subdirs = FcStrSetCreate ();
1772 if (!subdirs)
1773 return FcFalse;
1774
1775 set = FcConfigGetFonts (config, FcSetApplication);
1776 if (!set)
1777 {
1778 set = FcFontSetCreate ();
1779 if (!set)
1780 {
1781 FcStrSetDestroy (subdirs);
1782 return FcFalse;
1783 }
1784 FcConfigSetFonts (config, set, FcSetApplication);
1785 }
1786
1787 if (!FcFileScanConfig (set, subdirs, 0, config->blanks, file, FcFalse, config))
1788 {
1789 FcStrSetDestroy (subdirs);
1790 return FcFalse;
1791 }
1792 if ((sublist = FcStrListCreate (subdirs)))
1793 {
1794 while ((subdir = FcStrListNext (sublist)))
1795 {
1796 FcConfigAppFontAddDir (config, subdir);
1797 }
1798 FcStrListDone (sublist);
1799 }
1800 return FcTrue;
1801 }
1802
1803 FcBool
1804 FcConfigAppFontAddDir (FcConfig *config,
1805 const FcChar8 *dir)
1806 {
1807 FcFontSet *set;
1808 FcStrSet *subdirs;
1809 FcStrList *sublist;
1810 FcChar8 *subdir;
1811
1812 if (!config)
1813 {
1814 config = FcConfigGetCurrent ();
1815 if (!config)
1816 return FcFalse;
1817 }
1818 subdirs = FcStrSetCreate ();
1819 if (!subdirs)
1820 return FcFalse;
1821
1822 set = FcConfigGetFonts (config, FcSetApplication);
1823 if (!set)
1824 {
1825 set = FcFontSetCreate ();
1826 if (!set)
1827 {
1828 FcStrSetDestroy (subdirs);
1829 return FcFalse;
1830 }
1831 FcConfigSetFonts (config, set, FcSetApplication);
1832 }
1833
1834 if (!FcDirScanConfig (set, subdirs, 0, config->blanks, dir, FcFalse, config))
1835 {
1836 FcStrSetDestroy (subdirs);
1837 return FcFalse;
1838 }
1839 if ((sublist = FcStrListCreate (subdirs)))
1840 {
1841 while ((subdir = FcStrListNext (sublist)))
1842 {
1843 FcConfigAppFontAddDir (config, subdir);
1844 }
1845 FcStrListDone (sublist);
1846 }
1847 return FcTrue;
1848 }
1849
1850 void
1851 FcConfigAppFontClear (FcConfig *config)
1852 {
1853 if (!config)
1854 {
1855 config = FcConfigGetCurrent ();
1856 if (!config)
1857 return;
1858 }
1859
1860 FcConfigSetFonts (config, 0, FcSetApplication);
1861 }
1862
1863 /*
1864 * Manage filename-based font source selectors
1865 */
1866
1867 FcBool
1868 FcConfigGlobAdd (FcConfig *config,
1869 const FcChar8 *glob,
1870 FcBool accept)
1871 {
1872 FcStrSet *set = accept ? config->acceptGlobs : config->rejectGlobs;
1873
1874 return FcStrSetAdd (set, glob);
1875 }
1876
1877 static FcBool
1878 FcConfigGlobMatch (const FcChar8 *glob,
1879 const FcChar8 *string)
1880 {
1881 FcChar8 c;
1882
1883 while ((c = *glob++))
1884 {
1885 switch (c) {
1886 case '*':
1887 /* short circuit common case */
1888 if (!*glob)
1889 return FcTrue;
1890 /* short circuit another common case */
1891 if (strchr ((char *) glob, '*') == 0)
1892 string += strlen ((char *) string) - strlen ((char *) glob);
1893 while (*string)
1894 {
1895 if (FcConfigGlobMatch (glob, string))
1896 return FcTrue;
1897 string++;
1898 }
1899 return FcFalse;
1900 case '?':
1901 if (*string++ == '\0')
1902 return FcFalse;
1903 break;
1904 default:
1905 if (*string++ != c)
1906 return FcFalse;
1907 break;
1908 }
1909 }
1910 return *string == '\0';
1911 }
1912
1913 static FcBool
1914 FcConfigGlobsMatch (const FcStrSet *globs,
1915 const FcChar8 *string)
1916 {
1917 int i;
1918
1919 for (i = 0; i < globs->num; i++)
1920 if (FcConfigGlobMatch (globs->strs[i], string))
1921 return FcTrue;
1922 return FcFalse;
1923 }
1924
1925 FcBool
1926 FcConfigAcceptFilename (FcConfig *config,
1927 const FcChar8 *filename)
1928 {
1929 if (FcConfigGlobsMatch (config->acceptGlobs, filename))
1930 return FcTrue;
1931 if (FcConfigGlobsMatch (config->rejectGlobs, filename))
1932 return FcFalse;
1933 return FcTrue;
1934 }
1935
1936 /*
1937 * Manage font-pattern based font source selectors
1938 */
1939
1940 FcBool
1941 FcConfigPatternsAdd (FcConfig *config,
1942 FcPattern *pattern,
1943 FcBool accept)
1944 {
1945 FcFontSet *set = accept ? config->acceptPatterns : config->rejectPatterns;
1946
1947 return FcFontSetAdd (set, pattern);
1948 }
1949
1950 static FcBool
1951 FcConfigPatternsMatch (const FcFontSet *patterns,
1952 const FcPattern *font)
1953 {
1954 int i;
1955
1956 for (i = 0; i < patterns->nfont; i++)
1957 if (FcListPatternMatchAny (patterns->fonts[i], font))
1958 return FcTrue;
1959 return FcFalse;
1960 }
1961
1962 FcBool
1963 FcConfigAcceptFont (FcConfig *config,
1964 const FcPattern *font)
1965 {
1966 if (FcConfigPatternsMatch (config->acceptPatterns, font))
1967 return FcTrue;
1968 if (FcConfigPatternsMatch (config->rejectPatterns, font))
1969 return FcFalse;
1970 return FcTrue;
1971 }