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