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