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