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