Debian Codegraph @

src/fccfg.c

raw
1 /*
2 * fontconfig/src/fccfg.c
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 the author(s) not be used in
11 * advertising or publicity pertaining to distribution of the software without
12 * specific, written prior permission. The authors make 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 * THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18 * EVENT SHALL THE AUTHOR(S) 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 /* Objects MT-safe for readonly access. */
26
27 #include "fcint.h"
28
29 #include "fontconfig/fontconfig.h"
30 #ifdef HAVE_DIRENT_H
31 # include <dirent.h>
32 #endif
33 #include <sys/types.h>
34
35 #if defined(_WIN32) && !defined(R_OK)
36 # define R_OK 4
37 #endif
38
39 #if defined(_WIN32) && !defined(S_ISFIFO)
40 # define S_ISFIFO(m) 0
41 #endif
42
43 static FcConfig *_fcConfig; /* MT-safe */
44 static FcMutex *_lock;
45
46 static void
47 lock_config (void)
48 {
49 FcMutex *lock;
50 retry:
51 lock = fc_atomic_ptr_get (&_lock);
52 if (!lock) {
53 lock = (FcMutex *)malloc (sizeof (FcMutex));
54 FcMutexInit (lock);
55 if (!fc_atomic_ptr_cmpexch (&_lock, NULL, lock)) {
56 FcMutexFinish (lock);
57 free (lock);
58 goto retry;
59 }
60 FcMutexLock (lock);
61 /* Initialize random state */
62 FcRandom();
63 return;
64 }
65 FcMutexLock (lock);
66 }
67
68 static void
69 unlock_config (void)
70 {
71 FcMutex *lock;
72 lock = fc_atomic_ptr_get (&_lock);
73 if (lock)
74 FcMutexUnlock (lock);
75 }
76
77 static void
78 free_lock (void)
79 {
80 FcMutex *lock;
81 lock = fc_atomic_ptr_get (&_lock);
82 if (lock && fc_atomic_ptr_cmpexch (&_lock, lock, NULL)) {
83 FcMutexFinish (lock);
84 free (lock);
85 }
86 }
87
88 static FcConfig *
89 FcConfigEnsure (void)
90 {
91 FcConfig *config;
92 retry:
93 config = fc_atomic_ptr_get (&_fcConfig);
94 if (!config) {
95 config = FcInitLoadConfigAndFonts();
96
97 if (!config || !fc_atomic_ptr_cmpexch (&_fcConfig, NULL, config)) {
98 if (config)
99 FcConfigDestroy (config);
100 goto retry;
101 }
102 }
103 return config;
104 }
105
106 static void
107 FcDestroyAsRule (void *data)
108 {
109 FcRuleDestroy (data);
110 }
111
112 static void
113 FcDestroyAsRuleSet (void *data)
114 {
115 FcRuleSetDestroy (data);
116 }
117
118 FcBool
119 FcConfigInit (void)
120 {
121 FcBool is_new = !!(_fcConfig == NULL);
122 FcBool ret;
123
124 ret = FcConfigEnsure() ? FcTrue : FcFalse;
125 if (ret && !is_new)
126 FcConfigReference (_fcConfig);
127 return ret;
128 }
129
130 void
131 FcConfigFini (void)
132 {
133 FcConfig *cfg;
134
135 FcConfigDestroy (_fcConfig);
136 cfg = fc_atomic_ptr_get (&_fcConfig);
137 if (!cfg)
138 free_lock();
139 }
140
141 FcConfig *
142 FcConfigCreate (void)
143 {
144 FcSetName set;
145 FcConfig *config;
146 FcMatchKind k;
147 FcBool err = FcFalse;
148
149 config = malloc (sizeof (FcConfig));
150 if (!config)
151 goto bail0;
152
153 config->configDirs = FcStrSetCreate();
154 if (!config->configDirs)
155 goto bail1;
156
157 config->configFiles = FcStrSetCreate();
158 if (!config->configFiles)
159 goto bail2;
160
161 config->fontDirs = FcStrSetCreate();
162 if (!config->fontDirs)
163 goto bail3;
164
165 config->acceptGlobs = FcStrSetCreate();
166 if (!config->acceptGlobs)
167 goto bail4;
168
169 config->rejectGlobs = FcStrSetCreate();
170 if (!config->rejectGlobs)
171 goto bail5;
172
173 config->acceptPatterns = FcFontSetCreate();
174 if (!config->acceptPatterns)
175 goto bail6;
176
177 config->rejectPatterns = FcFontSetCreate();
178 if (!config->rejectPatterns)
179 goto bail7;
180
181 config->cacheDirs = FcStrSetCreate();
182 if (!config->cacheDirs)
183 goto bail8;
184
185 for (k = FcMatchKindBegin; k < FcMatchKindEnd; k++) {
186 config->subst[k] = FcPtrListCreate (FcDestroyAsRuleSet);
187 if (!config->subst[k])
188 err = FcTrue;
189 }
190 if (err)
191 goto bail9;
192
193 config->maxObjects = 0;
194 for (set = FcSetSystem; set <= FcSetApplication; set++)
195 config->fonts[set] = 0;
196
197 config->rescanTime = time (0);
198 config->rescanInterval = 30;
199
200 config->expr_pool = NULL;
201
202 config->sysRoot = FcStrRealPath ((const FcChar8 *)getenv ("FONTCONFIG_SYSROOT"));
203
204 config->rulesetList = FcPtrListCreate (FcDestroyAsRuleSet);
205 if (!config->rulesetList)
206 goto bail9;
207 config->availConfigFiles = FcStrSetCreate();
208 if (!config->availConfigFiles)
209 goto bail10;
210
211 config->filter_func = NULL;
212 config->filter_data = NULL;
213 config->destroy_data_func = NULL;
214 config->default_lang = NULL;
215 config->default_langs = NULL;
216 config->prgname = NULL;
217 config->desktop_name = NULL;
218
219 config->prefer_app_fonts = FcFalse;
220
221 FcRefInit (&config->ref, 1);
222 FcObjectInit();
223
224 return config;
225
226 bail10:
227 FcPtrListDestroy (config->rulesetList);
228 bail9:
229 for (k = FcMatchKindBegin; k < FcMatchKindEnd; k++)
230 if (config->subst[k])
231 FcPtrListDestroy (config->subst[k]);
232 FcStrSetDestroy (config->cacheDirs);
233 bail8:
234 FcFontSetDestroy (config->rejectPatterns);
235 bail7:
236 FcFontSetDestroy (config->acceptPatterns);
237 bail6:
238 FcStrSetDestroy (config->rejectGlobs);
239 bail5:
240 FcStrSetDestroy (config->acceptGlobs);
241 bail4:
242 FcStrSetDestroy (config->fontDirs);
243 bail3:
244 FcStrSetDestroy (config->configFiles);
245 bail2:
246 FcStrSetDestroy (config->configDirs);
247 bail1:
248 free (config);
249 bail0:
250 return 0;
251 }
252
253 static FcFileTime
254 FcConfigNewestFile (FcStrSet *files)
255 {
256 FcStrList *list = FcStrListCreate (files);
257 FcFileTime newest = { 0, FcFalse };
258 FcChar8 *file;
259 struct stat statb;
260
261 if (list) {
262 while ((file = FcStrListNext (list)))
263 if (FcStat (file, &statb) == 0)
264 if (!newest.set || statb.st_mtime - newest.time > 0) {
265 newest.set = FcTrue;
266 newest.time = statb.st_mtime;
267 }
268 FcStrListDone (list);
269 }
270 return newest;
271 }
272
273 FcBool
274 FcConfigUptoDate (FcConfig *config)
275 {
276 FcFileTime config_time, config_dir_time, font_time;
277 time_t now = time (0);
278 FcBool ret = FcTrue;
279
280 config = FcConfigReference (config);
281 if (!config)
282 return FcFalse;
283
284 config_time = FcConfigNewestFile (config->configFiles);
285 config_dir_time = FcConfigNewestFile (config->configDirs);
286 font_time = FcConfigNewestFile (config->fontDirs);
287 if ((config_time.set && config_time.time - config->rescanTime > 0) ||
288 (config_dir_time.set && (config_dir_time.time - config->rescanTime) > 0) ||
289 (font_time.set && (font_time.time - config->rescanTime) > 0)) {
290 /* We need to check for potential clock problems here (OLPC ticket #6046) */
291 if ((config_time.set && (config_time.time - now) > 0) ||
292 (config_dir_time.set && (config_dir_time.time - now) > 0) ||
293 (font_time.set && (font_time.time - now) > 0)) {
294 fprintf (stderr,
295 "Fontconfig warning: Directory/file mtime in the future. New fonts may not be detected.\n");
296 config->rescanTime = now;
297 goto bail;
298 } else {
299 ret = FcFalse;
300 goto bail;
301 }
302 }
303 config->rescanTime = now;
304 bail:
305 FcConfigDestroy (config);
306
307 return ret;
308 }
309
310 FcExpr *
311 FcConfigAllocExpr (FcConfig *config)
312 {
313 if (!config->expr_pool || config->expr_pool->next == config->expr_pool->end) {
314 FcExprPage *new_page;
315
316 new_page = malloc (sizeof (FcExprPage));
317 if (!new_page)
318 return 0;
319
320 new_page->next_page = config->expr_pool;
321 new_page->next = new_page->exprs;
322 config->expr_pool = new_page;
323 }
324
325 return config->expr_pool->next++;
326 }
327
328 FcConfig *
329 FcConfigReference (FcConfig *config)
330 {
331 if (!config) {
332 /* lock during obtaining the value from _fcConfig and count up refcount there,
333 * there are the race between them.
334 */
335 lock_config();
336 retry:
337 config = fc_atomic_ptr_get (&_fcConfig);
338 if (!config) {
339 unlock_config();
340
341 config = FcInitLoadConfigAndFonts();
342 lock_config();
343 if (!config)
344 goto retry;
345 if (!fc_atomic_ptr_cmpexch (&_fcConfig, NULL, config)) {
346 FcConfigDestroy (config);
347 goto retry;
348 }
349 }
350 FcRefInc (&config->ref);
351 unlock_config();
352 } else
353 FcRefInc (&config->ref);
354
355 return config;
356 }
357
358 void
359 FcConfigDestroy (FcConfig *config)
360 {
361 FcSetName set;
362 FcExprPage *page;
363 FcMatchKind k;
364
365 if (config) {
366 if (FcRefDec (&config->ref) != 1)
367 return;
368
369 FcObjectFini();
370 (void)fc_atomic_ptr_cmpexch (&_fcConfig, config, NULL);
371
372 FcStrSetDestroy (config->configDirs);
373 FcStrSetDestroy (config->fontDirs);
374 FcStrSetDestroy (config->cacheDirs);
375 FcStrSetDestroy (config->configFiles);
376 FcStrSetDestroy (config->acceptGlobs);
377 FcStrSetDestroy (config->rejectGlobs);
378 FcFontSetDestroy (config->acceptPatterns);
379 FcFontSetDestroy (config->rejectPatterns);
380
381 for (k = FcMatchKindBegin; k < FcMatchKindEnd; k++)
382 FcPtrListDestroy (config->subst[k]);
383 FcPtrListDestroy (config->rulesetList);
384 FcStrSetDestroy (config->availConfigFiles);
385 for (set = FcSetSystem; set <= FcSetApplication; set++)
386 if (config->fonts[set])
387 FcFontSetDestroy (config->fonts[set]);
388
389 page = config->expr_pool;
390 while (page) {
391 FcExprPage *next = page->next_page;
392 free (page);
393 page = next;
394 }
395 if (config->sysRoot)
396 FcStrFree (config->sysRoot);
397
398 if (config->filter_data && config->destroy_data_func)
399 config->destroy_data_func (config->filter_data);
400
401 if (config->default_lang)
402 FcStrFree (config->default_lang);
403 if (config->default_langs) {
404 FcRefInit (&config->default_langs->ref, 1);
405 FcStrSetDestroy (config->default_langs);
406 }
407 if (config->prgname)
408 FcStrFree (config->prgname);
409 if (config->desktop_name)
410 FcStrFree (config->desktop_name);
411
412 free (config);
413 }
414 }
415
416 /*
417 * Add cache to configuration, adding fonts and directories
418 */
419
420 FcBool
421 FcConfigAddCache (FcConfig *config, FcCache *cache,
422 FcSetName set, FcStrSet *dirSet, FcChar8 *forDir)
423 {
424 FcFontSet *fs;
425 intptr_t *dirs;
426 int i;
427 FcBool relocated = FcFalse;
428
429 if (strcmp ((char *)FcCacheDir (cache), (char *)forDir) != 0)
430 relocated = FcTrue;
431
432 /*
433 * Add fonts
434 */
435 fs = FcCacheSet (cache);
436 if (fs) {
437 int nref = 0;
438
439 for (i = 0; i < fs->nfont; i++) {
440 FcPattern *font = FcFontSetFont (fs, i);
441 FcChar8 *font_file;
442 FcChar8 *relocated_font_file = NULL;
443
444 if (FcPatternObjectGetString (font, FC_FILE_OBJECT,
445 0, &font_file) == FcResultMatch) {
446 if (relocated) {
447 FcChar8 *slash = FcStrLastSlash (font_file);
448 relocated_font_file = FcStrBuildFilename (forDir, slash + 1, NULL);
449 font_file = relocated_font_file;
450 }
451
452 /*
453 * Check to see if font is banned by filename
454 */
455 if (!FcConfigAcceptFilename (config, font_file)) {
456 free (relocated_font_file);
457 continue;
458 }
459 }
460
461 /*
462 * Check to see if font is banned by pattern
463 */
464 if (!FcConfigAcceptFont (config, font)) {
465 free (relocated_font_file);
466 continue;
467 }
468
469 /*
470 * Check to see if font is banned by client
471 */
472 if (!FcConfigAcceptFilter (config, font)) {
473 free (relocated_font_file);
474 continue;
475 }
476 if (relocated_font_file) {
477 font = FcPatternCacheRewriteFile (font, cache, relocated_font_file);
478 free (relocated_font_file);
479 }
480
481 if (FcFontSetAdd (config->fonts[set], font))
482 nref++;
483 }
484 FcDirCacheReference (cache, nref);
485 }
486
487 /*
488 * Add directories
489 */
490 dirs = FcCacheDirs (cache);
491 if (dirs) {
492 for (i = 0; i < cache->dirs_count; i++) {
493 const FcChar8 *dir = FcCacheSubdir (cache, i);
494 FcChar8 *s = NULL;
495
496 if (relocated) {
497 FcChar8 *base = FcStrBasename (dir);
498 dir = s = FcStrBuildFilename (forDir, base, NULL);
499 FcStrFree (base);
500 }
501 if (FcConfigAcceptFilename (config, dir))
502 FcStrSetAddFilename (dirSet, dir);
503 if (s)
504 FcStrFree (s);
505 }
506 }
507 return FcTrue;
508 }
509
510 static FcBool
511 FcConfigAddDirList (FcConfig *config, FcSetName set, FcStrSet *dirSet)
512 {
513 FcStrList *dirlist;
514 FcChar8 *dir;
515 FcCache *cache;
516
517 dirlist = FcStrListCreate (dirSet);
518 if (!dirlist)
519 return FcFalse;
520
521 while ((dir = FcStrListNext (dirlist))) {
522 if (FcDebug() & FC_DBG_FONTSET)
523 printf ("adding fonts from %s\n", dir);
524 cache = FcDirCacheRead (dir, FcFalse, config);
525 if (!cache)
526 continue;
527 FcConfigAddCache (config, cache, set, dirSet, dir);
528 FcDirCacheUnload (cache);
529 }
530 FcStrListDone (dirlist);
531 return FcTrue;
532 }
533
534 /*
535 * Scan the current list of directories in the configuration
536 * and build the set of available fonts.
537 */
538
539 FcBool
540 FcConfigBuildFonts (FcConfig *config)
541 {
542 FcFontSet *fonts;
543 FcBool ret = FcTrue;
544
545 config = FcConfigReference (config);
546 if (!config)
547 return FcFalse;
548
549 fonts = FcFontSetCreate();
550 if (!fonts) {
551 ret = FcFalse;
552 goto bail;
553 }
554
555 FcConfigSetFonts (config, fonts, FcSetSystem);
556
557 if (!FcConfigAddDirList (config, FcSetSystem, config->fontDirs)) {
558 ret = FcFalse;
559 goto bail;
560 }
561 if (FcDebug() & FC_DBG_FONTSET)
562 FcFontSetPrint (fonts);
563 bail:
564 FcConfigDestroy (config);
565
566 return ret;
567 }
568
569 FcBool
570 FcConfigSetCurrent (FcConfig *config)
571 {
572 FcConfig *cfg;
573
574 if (config) {
575 if (!config->fonts[FcSetSystem])
576 if (!FcConfigBuildFonts (config))
577 return FcFalse;
578 FcRefInc (&config->ref);
579 }
580
581 lock_config();
582 retry:
583 cfg = fc_atomic_ptr_get (&_fcConfig);
584
585 if (config == cfg) {
586 unlock_config();
587 if (config)
588 FcConfigDestroy (config);
589 return FcTrue;
590 }
591
592 if (!fc_atomic_ptr_cmpexch (&_fcConfig, cfg, config))
593 goto retry;
594 unlock_config();
595 if (cfg)
596 FcConfigDestroy (cfg);
597
598 return FcTrue;
599 }
600
601 FcConfig *
602 FcConfigGetCurrent (void)
603 {
604 return FcConfigEnsure();
605 }
606
607 FcBool
608 FcConfigAddConfigDir (FcConfig *config,
609 const FcChar8 *d)
610 {
611 return FcStrSetAddFilename (config->configDirs, d);
612 }
613
614 FcStrList *
615 FcConfigGetConfigDirs (FcConfig *config)
616 {
617 FcStrList *ret;
618
619 config = FcConfigReference (config);
620 if (!config)
621 return NULL;
622 ret = FcStrListCreate (config->configDirs);
623 FcConfigDestroy (config);
624
625 return ret;
626 }
627
628 FcBool
629 FcConfigAddFontDir (FcConfig *config,
630 const FcChar8 *d,
631 const FcChar8 *m,
632 const FcChar8 *salt)
633 {
634 if (FcDebug() & FC_DBG_CACHE) {
635 if (m) {
636 printf ("%s -> %s%s%s%s\n", d, m, salt ? " (salt: " : "", salt ? (const char *)salt : "", salt ? ")" : "");
637 } else if (salt) {
638 printf ("%s%s%s%s\n", d, salt ? " (salt: " : "", salt ? (const char *)salt : "", salt ? ")" : "");
639 }
640 }
641 return FcStrSetAddFilenamePairWithSalt (config->fontDirs, d, m, salt);
642 }
643
644 FcBool
645 FcConfigResetFontDirs (FcConfig *config)
646 {
647 if (FcDebug() & FC_DBG_CACHE) {
648 printf ("Reset font directories!\n");
649 }
650 return FcStrSetDeleteAll (config->fontDirs);
651 }
652
653 FcStrList *
654 FcConfigGetFontDirs (FcConfig *config)
655 {
656 FcStrList *ret;
657
658 config = FcConfigReference (config);
659 if (!config)
660 return NULL;
661 ret = FcStrListCreate (config->fontDirs);
662 FcConfigDestroy (config);
663
664 return ret;
665 }
666
667 static FcBool
668 FcConfigPathStartsWith (const FcChar8 *path,
669 const FcChar8 *start)
670 {
671 int len = strlen ((char *)start);
672
673 if (strncmp ((char *)path, (char *)start, len) != 0)
674 return FcFalse;
675
676 switch (path[len]) {
677 case '\0':
678 case FC_DIR_SEPARATOR:
679 return FcTrue;
680 default:
681 return FcFalse;
682 }
683 }
684
685 FcChar8 *
686 FcConfigMapFontPath (FcConfig *config,
687 const FcChar8 *path)
688 {
689 FcStrList *list;
690 FcChar8 *dir;
691 const FcChar8 *map, *rpath;
692 FcChar8 *retval;
693
694 list = FcConfigGetFontDirs (config);
695 if (!list)
696 return 0;
697 while ((dir = FcStrListNext (list)))
698 if (FcConfigPathStartsWith (path, dir))
699 break;
700 FcStrListDone (list);
701 if (!dir)
702 return 0;
703 map = FcStrTripleSecond (dir);
704 if (!map)
705 return 0;
706 rpath = path + strlen ((char *)dir);
707 while (*rpath == '/')
708 rpath++;
709 retval = FcStrBuildFilename (map, rpath, NULL);
710 if (retval) {
711 size_t len = strlen ((const char *)retval);
712 while (len > 0 && retval[len - 1] == '/')
713 len--;
714 /* trim the last slash */
715 retval[len] = 0;
716 }
717 return retval;
718 }
719
720 const FcChar8 *
721 FcConfigMapSalt (FcConfig *config,
722 const FcChar8 *path)
723 {
724 FcStrList *list;
725 FcChar8 *dir;
726
727 list = FcConfigGetFontDirs (config);
728 if (!list)
729 return NULL;
730 while ((dir = FcStrListNext (list)))
731 if (FcConfigPathStartsWith (path, dir))
732 break;
733 FcStrListDone (list);
734 if (!dir)
735 return NULL;
736
737 return FcStrTripleThird (dir);
738 }
739
740 FcBool
741 FcConfigAddCacheDir (FcConfig *config,
742 const FcChar8 *d)
743 {
744 return FcStrSetAddFilename (config->cacheDirs, d);
745 }
746
747 FcStrList *
748 FcConfigGetCacheDirs (FcConfig *config)
749 {
750 FcStrList *ret;
751
752 config = FcConfigReference (config);
753 if (!config)
754 return NULL;
755 ret = FcStrListCreate (config->cacheDirs);
756 FcConfigDestroy (config);
757
758 return ret;
759 }
760
761 FcBool
762 FcConfigAddConfigFile (FcConfig *config,
763 const FcChar8 *f)
764 {
765 FcBool ret;
766 FcChar8 *file = FcConfigGetFilename (config, f);
767
768 if (!file)
769 return FcFalse;
770
771 ret = FcStrSetAdd (config->configFiles, file);
772 FcStrFree (file);
773 return ret;
774 }
775
776 FcStrList *
777 FcConfigGetConfigFiles (FcConfig *config)
778 {
779 FcStrList *ret;
780
781 config = FcConfigReference (config);
782 if (!config)
783 return NULL;
784 ret = FcStrListCreate (config->configFiles);
785 FcConfigDestroy (config);
786
787 return ret;
788 }
789
790 FcChar8 *
791 FcConfigGetCache (FcConfig *config FC_UNUSED)
792 {
793 return NULL;
794 }
795
796 FcFontSet *
797 FcConfigGetFonts (FcConfig *config,
798 FcSetName set)
799 {
800 if (!config) {
801 config = FcConfigGetCurrent();
802 if (!config)
803 return 0;
804 }
805 return config->fonts[set];
806 }
807
808 void
809 FcConfigSetFonts (FcConfig *config,
810 FcFontSet *fonts,
811 FcSetName set)
812 {
813 if (config->fonts[set])
814 FcFontSetDestroy (config->fonts[set]);
815 config->fonts[set] = fonts;
816 }
817
818 FcConfig *
819 FcConfigSetFontSetFilter (FcConfig *config,
820 FcFilterFontSetFunc filter_func,
821 FcDestroyFunc destroy_data_func,
822 void *user_data)
823 {
824 FcBool rebuild = FcFalse;
825
826 if (!config) {
827 /* Do not use FcConfigEnsure() here for optimization */
828 retry:
829 config = fc_atomic_ptr_get (&_fcConfig);
830 if (!config)
831 config = FcConfigCreate();
832 else
833 rebuild = FcTrue;
834 } else
835 rebuild = FcTrue;
836 if (config->filter_data == user_data &&
837 config->filter_func == filter_func) {
838 /* No need to update */
839 rebuild = FcFalse;
840 } else {
841 if (config->filter_data && config->destroy_data_func) {
842 config->destroy_data_func (config->filter_data);
843 }
844 config->filter_func = filter_func;
845 config->destroy_data_func = destroy_data_func;
846 config->filter_data = user_data;
847 }
848
849 if (rebuild) {
850 /* Rebuild FontSet */
851 FcConfigBuildFonts (config);
852 } else {
853 /* Initialize FcConfig with regular procedure */
854 config = FcInitLoadOwnConfigAndFonts (config);
855
856 if (!config || !fc_atomic_ptr_cmpexch (&_fcConfig, NULL, config)) {
857 if (config)
858 FcConfigDestroy (config);
859 goto retry;
860 }
861 }
862
863 return config;
864 }
865
866 FcBlanks *
867 FcBlanksCreate (void)
868 {
869 /* Deprecated. */
870 return NULL;
871 }
872
873 void
874 FcBlanksDestroy (FcBlanks *b FC_UNUSED)
875 {
876 /* Deprecated. */
877 }
878
879 FcBool
880 FcBlanksAdd (FcBlanks *b FC_UNUSED, FcChar32 ucs4 FC_UNUSED)
881 {
882 /* Deprecated. */
883 return FcFalse;
884 }
885
886 FcBool
887 FcBlanksIsMember (FcBlanks *b FC_UNUSED, FcChar32 ucs4 FC_UNUSED)
888 {
889 /* Deprecated. */
890 return FcFalse;
891 }
892
893 FcBlanks *
894 FcConfigGetBlanks (FcConfig *config FC_UNUSED)
895 {
896 /* Deprecated. */
897 return NULL;
898 }
899
900 FcBool
901 FcConfigAddBlank (FcConfig *config FC_UNUSED,
902 FcChar32 blank FC_UNUSED)
903 {
904 /* Deprecated. */
905 return FcFalse;
906 }
907
908 int
909 FcConfigGetRescanInterval (FcConfig *config)
910 {
911 int ret;
912
913 config = FcConfigReference (config);
914 if (!config)
915 return 0;
916 ret = config->rescanInterval;
917 FcConfigDestroy (config);
918
919 return ret;
920 }
921
922 FcBool
923 FcConfigSetRescanInterval (FcConfig *config, int rescanInterval)
924 {
925 config = FcConfigReference (config);
926 if (!config)
927 return FcFalse;
928 config->rescanInterval = rescanInterval;
929 FcConfigDestroy (config);
930
931 return FcTrue;
932 }
933
934 /*
935 * A couple of typos escaped into the library
936 */
937 int
938 FcConfigGetRescanInverval (FcConfig *config)
939 {
940 return FcConfigGetRescanInterval (config);
941 }
942
943 FcBool
944 FcConfigSetRescanInverval (FcConfig *config, int rescanInterval)
945 {
946 return FcConfigSetRescanInterval (config, rescanInterval);
947 }
948
949 FcBool
950 FcConfigAddRule (FcConfig *config,
951 FcRule *rule,
952 FcMatchKind kind)
953 {
954 /* deprecated */
955 return FcFalse;
956 }
957
958 static FcValue
959 FcConfigPromote (FcValue v, FcValue u, FcValuePromotionBuffer *buf)
960 {
961 switch (v.type) {
962 case FcTypeInteger:
963 v.type = FcTypeDouble;
964 v.u.d = (double)v.u.i;
965 /* Fallthrough */
966 case FcTypeDouble:
967 if (u.type == FcTypeRange && buf) {
968 v.u.r = FcRangePromote (v.u.d, buf);
969 v.type = FcTypeRange;
970 }
971 break;
972 case FcTypeVoid:
973 if (u.type == FcTypeMatrix) {
974 v.u.m = &FcIdentityMatrix;
975 v.type = FcTypeMatrix;
976 } else if (u.type == FcTypeLangSet && buf) {
977 v.u.l = FcLangSetPromote (NULL, buf);
978 v.type = FcTypeLangSet;
979 } else if (u.type == FcTypeCharSet && buf) {
980 v.u.c = FcCharSetPromote (buf);
981 v.type = FcTypeCharSet;
982 }
983 break;
984 case FcTypeString:
985 if (u.type == FcTypeLangSet && buf) {
986 v.u.l = FcLangSetPromote (v.u.s, buf);
987 v.type = FcTypeLangSet;
988 }
989 break;
990 default:
991 break;
992 }
993 return v;
994 }
995
996 FcBool
997 FcConfigCompareValue (const FcValue *left_o,
998 unsigned int op_,
999 const FcValue *right_o)
1000 {
1001 FcValue left;
1002 FcValue right;
1003 FcBool ret = FcFalse;
1004 FcOp op = FC_OP_GET_OP (op_);
1005 int flags = FC_OP_GET_FLAGS (op_);
1006 FcValuePromotionBuffer buf1, buf2;
1007
1008 if (left_o->type != right_o->type) {
1009 left = FcValueCanonicalize (left_o);
1010 right = FcValueCanonicalize (right_o);
1011 left = FcConfigPromote (left, right, &buf1);
1012 right = FcConfigPromote (right, left, &buf2);
1013 left_o = &left;
1014 right_o = &right;
1015 if (left_o->type != right_o->type) {
1016 if (op == FcOpNotEqual || op == FcOpNotContains)
1017 ret = FcTrue;
1018 return ret;
1019 }
1020 }
1021 switch (left_o->type) {
1022 case FcTypeUnknown:
1023 break; /* No way to guess how to compare for this object */
1024 case FcTypeInteger: {
1025 int l = left_o->u.i;
1026 int r = right_o->u.i;
1027 switch ((int)op) {
1028 case FcOpEqual:
1029 case FcOpContains:
1030 case FcOpListing:
1031 ret = l == r;
1032 break;
1033 case FcOpNotEqual:
1034 case FcOpNotContains:
1035 ret = l != r;
1036 break;
1037 case FcOpLess:
1038 ret = l < r;
1039 break;
1040 case FcOpLessEqual:
1041 ret = l <= r;
1042 break;
1043 case FcOpMore:
1044 ret = l > r;
1045 break;
1046 case FcOpMoreEqual:
1047 ret = l >= r;
1048 break;
1049 default:
1050 break;
1051 }
1052 break;
1053 }
1054 case FcTypeDouble: {
1055 double l = left_o->u.d;
1056 double r = right_o->u.d;
1057 switch ((int)op) {
1058 case FcOpEqual:
1059 case FcOpContains:
1060 case FcOpListing:
1061 ret = l == r;
1062 break;
1063 case FcOpNotEqual:
1064 case FcOpNotContains:
1065 ret = l != r;
1066 break;
1067 case FcOpLess:
1068 ret = l < r;
1069 break;
1070 case FcOpLessEqual:
1071 ret = l <= r;
1072 break;
1073 case FcOpMore:
1074 ret = l > r;
1075 break;
1076 case FcOpMoreEqual:
1077 ret = l >= r;
1078 break;
1079 default:
1080 break;
1081 }
1082 break;
1083 }
1084 case FcTypeBool: {
1085 FcBool l = left_o->u.b;
1086 FcBool r = right_o->u.b;
1087 switch ((int)op) {
1088 case FcOpEqual:
1089 ret = l == r;
1090 break;
1091 case FcOpContains:
1092 case FcOpListing:
1093 ret = l == r || l >= FcDontCare;
1094 break;
1095 case FcOpNotEqual:
1096 ret = l != r;
1097 break;
1098 case FcOpNotContains:
1099 ret = !(l == r || l >= FcDontCare);
1100 break;
1101 case FcOpLess:
1102 ret = l != r && r >= FcDontCare;
1103 break;
1104 case FcOpLessEqual:
1105 ret = l == r || r >= FcDontCare;
1106 break;
1107 case FcOpMore:
1108 ret = l != r && l >= FcDontCare;
1109 break;
1110 case FcOpMoreEqual:
1111 ret = l == r || l >= FcDontCare;
1112 break;
1113 default:
1114 break;
1115 }
1116 break;
1117 }
1118 case FcTypeString: {
1119 const FcChar8 *l = FcValueString (left_o);
1120 const FcChar8 *r = FcValueString (right_o);
1121 switch ((int)op) {
1122 case FcOpEqual:
1123 case FcOpListing:
1124 if (flags & FcOpFlagIgnoreBlanks)
1125 ret = FcStrCmpIgnoreBlanksAndCase (l, r) == 0;
1126 else
1127 ret = FcStrCmpIgnoreCase (l, r) == 0;
1128 break;
1129 case FcOpContains:
1130 ret = FcStrStrIgnoreCase (l, r) != 0;
1131 break;
1132 case FcOpNotEqual:
1133 if (flags & FcOpFlagIgnoreBlanks)
1134 ret = FcStrCmpIgnoreBlanksAndCase (l, r) != 0;
1135 else
1136 ret = FcStrCmpIgnoreCase (l, r) != 0;
1137 break;
1138 case FcOpNotContains:
1139 ret = FcStrStrIgnoreCase (l, r) == 0;
1140 break;
1141 default:
1142 break;
1143 }
1144 break;
1145 }
1146 case FcTypeMatrix: {
1147 switch ((int)op) {
1148 case FcOpEqual:
1149 case FcOpContains:
1150 case FcOpListing:
1151 ret = FcMatrixEqual (left_o->u.m, right_o->u.m);
1152 break;
1153 case FcOpNotEqual:
1154 case FcOpNotContains:
1155 ret = !FcMatrixEqual (left_o->u.m, right_o->u.m);
1156 break;
1157 default:
1158 break;
1159 }
1160 break;
1161 }
1162 case FcTypeCharSet: {
1163 const FcCharSet *l = FcValueCharSet (left_o);
1164 const FcCharSet *r = FcValueCharSet (right_o);
1165 switch ((int)op) {
1166 case FcOpContains:
1167 case FcOpListing:
1168 /* left contains right if right is a subset of left */
1169 ret = FcCharSetIsSubset (r, l);
1170 break;
1171 case FcOpNotContains:
1172 /* left contains right if right is a subset of left */
1173 ret = !FcCharSetIsSubset (r, l);
1174 break;
1175 case FcOpEqual:
1176 ret = FcCharSetEqual (l, r);
1177 break;
1178 case FcOpNotEqual:
1179 ret = !FcCharSetEqual (l, r);
1180 break;
1181 default:
1182 break;
1183 }
1184 break;
1185 }
1186 case FcTypeLangSet: {
1187 const FcLangSet *l = FcValueLangSet (left_o);
1188 const FcLangSet *r = FcValueLangSet (right_o);
1189 switch ((int)op) {
1190 case FcOpContains:
1191 case FcOpListing:
1192 ret = FcLangSetContains (l, r);
1193 break;
1194 case FcOpNotContains:
1195 ret = !FcLangSetContains (l, r);
1196 break;
1197 case FcOpEqual:
1198 ret = FcLangSetEqual (l, r);
1199 break;
1200 case FcOpNotEqual:
1201 ret = !FcLangSetEqual (l, r);
1202 break;
1203 default:
1204 break;
1205 }
1206 break;
1207 }
1208 case FcTypeVoid:
1209 switch ((int)op) {
1210 case FcOpEqual:
1211 case FcOpContains:
1212 case FcOpListing:
1213 ret = FcTrue;
1214 break;
1215 default:
1216 break;
1217 }
1218 break;
1219 case FcTypeFTFace:
1220 switch ((int)op) {
1221 case FcOpEqual:
1222 case FcOpContains:
1223 case FcOpListing:
1224 ret = left_o->u.f == right_o->u.f;
1225 break;
1226 case FcOpNotEqual:
1227 case FcOpNotContains:
1228 ret = left_o->u.f != right_o->u.f;
1229 break;
1230 default:
1231 break;
1232 }
1233 break;
1234 case FcTypeRange: {
1235 const FcRange *l = FcValueRange (left_o);
1236 const FcRange *r = FcValueRange (right_o);
1237 ret = FcRangeCompare (op, l, r);
1238 break;
1239 }
1240 }
1241 return ret;
1242 }
1243
1244 #define _FcDoubleFloor(d) ((int)(d))
1245 #define _FcDoubleCeil(d) ((double)(int)(d) == (d) ? (int)(d) : (int)((d) + 1))
1246 #define FcDoubleFloor(d) ((d) >= 0 ? _FcDoubleFloor (d) : -_FcDoubleCeil (-(d)))
1247 #define FcDoubleCeil(d) ((d) >= 0 ? _FcDoubleCeil (d) : -_FcDoubleFloor (-(d)))
1248 #define FcDoubleRound(d) FcDoubleFloor ((d) + 0.5)
1249 #define FcDoubleTrunc(d) ((d) >= 0 ? _FcDoubleFloor (d) : -_FcDoubleFloor (-(d)))
1250
1251 static FcValue
1252 FcConfigEvaluate (FcPattern *p, FcPattern *p_pat, FcMatchKind kind, FcExpr *e)
1253 {
1254 FcValue v, vl, vr, vle, vre;
1255 FcMatrix *m;
1256 FcChar8 *str;
1257 FcOp op = FC_OP_GET_OP (e->op);
1258 FcValuePromotionBuffer buf1, buf2;
1259
1260 switch ((int)op) {
1261 case FcOpInteger:
1262 v.type = FcTypeInteger;
1263 v.u.i = e->u.ival;
1264 break;
1265 case FcOpDouble:
1266 v.type = FcTypeDouble;
1267 v.u.d = e->u.dval;
1268 break;
1269 case FcOpString:
1270 v.type = FcTypeString;
1271 v.u.s = e->u.sval;
1272 v = FcValueSave (v);
1273 break;
1274 case FcOpMatrix: {
1275 FcMatrix m;
1276 FcValue xx, xy, yx, yy;
1277 v.type = FcTypeMatrix;
1278 xx = FcConfigPromote (FcConfigEvaluate (p, p_pat, kind, e->u.mexpr->xx), v, NULL);
1279 xy = FcConfigPromote (FcConfigEvaluate (p, p_pat, kind, e->u.mexpr->xy), v, NULL);
1280 yx = FcConfigPromote (FcConfigEvaluate (p, p_pat, kind, e->u.mexpr->yx), v, NULL);
1281 yy = FcConfigPromote (FcConfigEvaluate (p, p_pat, kind, e->u.mexpr->yy), v, NULL);
1282 if (xx.type == FcTypeDouble && xy.type == FcTypeDouble &&
1283 yx.type == FcTypeDouble && yy.type == FcTypeDouble) {
1284 m.xx = xx.u.d;
1285 m.xy = xy.u.d;
1286 m.yx = yx.u.d;
1287 m.yy = yy.u.d;
1288 v.u.m = &m;
1289 } else
1290 v.type = FcTypeVoid;
1291 v = FcValueSave (v);
1292 } break;
1293 case FcOpCharSet:
1294 v.type = FcTypeCharSet;
1295 v.u.c = e->u.cval;
1296 v = FcValueSave (v);
1297 break;
1298 case FcOpLangSet:
1299 v.type = FcTypeLangSet;
1300 v.u.l = e->u.lval;
1301 v = FcValueSave (v);
1302 break;
1303 case FcOpRange:
1304 v.type = FcTypeRange;
1305 v.u.r = e->u.rval;
1306 v = FcValueSave (v);
1307 break;
1308 case FcOpBool:
1309 v.type = FcTypeBool;
1310 v.u.b = e->u.bval;
1311 break;
1312 case FcOpField:
1313 if (kind == FcMatchFont && e->u.name.kind == FcMatchPattern) {
1314 if (FcResultMatch != FcPatternObjectGet (p_pat, e->u.name.object, 0, &v))
1315 v.type = FcTypeVoid;
1316 } else if (kind == FcMatchPattern && e->u.name.kind == FcMatchFont) {
1317 fprintf (stderr,
1318 "Fontconfig warning: <name> tag has target=\"font\" in a <match target=\"pattern\">.\n");
1319 v.type = FcTypeVoid;
1320 } else {
1321 if (FcResultMatch != FcPatternObjectGet (p, e->u.name.object, 0, &v))
1322 v.type = FcTypeVoid;
1323 }
1324 v = FcValueSave (v);
1325 break;
1326 case FcOpConst:
1327 if (FcNameConstant (e->u.constant, &v.u.i))
1328 v.type = FcTypeInteger;
1329 else
1330 v.type = FcTypeVoid;
1331 break;
1332 case FcOpQuest:
1333 vl = FcConfigEvaluate (p, p_pat, kind, e->u.tree.left);
1334 if (vl.type == FcTypeBool) {
1335 if (vl.u.b)
1336 v = FcConfigEvaluate (p, p_pat, kind, e->u.tree.right->u.tree.left);
1337 else
1338 v = FcConfigEvaluate (p, p_pat, kind, e->u.tree.right->u.tree.right);
1339 } else
1340 v.type = FcTypeVoid;
1341 FcValueDestroy (vl);
1342 break;
1343 case FcOpEqual:
1344 case FcOpNotEqual:
1345 case FcOpLess:
1346 case FcOpLessEqual:
1347 case FcOpMore:
1348 case FcOpMoreEqual:
1349 case FcOpContains:
1350 case FcOpNotContains:
1351 case FcOpListing:
1352 vl = FcConfigEvaluate (p, p_pat, kind, e->u.tree.left);
1353 vr = FcConfigEvaluate (p, p_pat, kind, e->u.tree.right);
1354 v.type = FcTypeBool;
1355 v.u.b = FcConfigCompareValue (&vl, e->op, &vr);
1356 FcValueDestroy (vl);
1357 FcValueDestroy (vr);
1358 break;
1359 case FcOpOr:
1360 case FcOpAnd:
1361 case FcOpPlus:
1362 case FcOpMinus:
1363 case FcOpTimes:
1364 case FcOpDivide:
1365 vl = FcConfigEvaluate (p, p_pat, kind, e->u.tree.left);
1366 vr = FcConfigEvaluate (p, p_pat, kind, e->u.tree.right);
1367 vle = FcConfigPromote (vl, vr, &buf1);
1368 vre = FcConfigPromote (vr, vle, &buf2);
1369 if (vle.type == vre.type) {
1370 switch ((int)vle.type) {
1371 case FcTypeDouble:
1372 switch ((int)op) {
1373 case FcOpPlus:
1374 v.type = FcTypeDouble;
1375 v.u.d = vle.u.d + vre.u.d;
1376 break;
1377 case FcOpMinus:
1378 v.type = FcTypeDouble;
1379 v.u.d = vle.u.d - vre.u.d;
1380 break;
1381 case FcOpTimes:
1382 v.type = FcTypeDouble;
1383 v.u.d = vle.u.d * vre.u.d;
1384 break;
1385 case FcOpDivide:
1386 v.type = FcTypeDouble;
1387 v.u.d = vle.u.d / vre.u.d;
1388 break;
1389 default:
1390 v.type = FcTypeVoid;
1391 break;
1392 }
1393 if (v.type == FcTypeDouble &&
1394 v.u.d == (double)(int)v.u.d) {
1395 v.type = FcTypeInteger;
1396 v.u.i = (int)v.u.d;
1397 }
1398 break;
1399 case FcTypeBool:
1400 switch ((int)op) {
1401 case FcOpOr:
1402 v.type = FcTypeBool;
1403 v.u.b = vle.u.b || vre.u.b;
1404 break;
1405 case FcOpAnd:
1406 v.type = FcTypeBool;
1407 v.u.b = vle.u.b && vre.u.b;
1408 break;
1409 default:
1410 v.type = FcTypeVoid;
1411 break;
1412 }
1413 break;
1414 case FcTypeString:
1415 switch ((int)op) {
1416 case FcOpPlus:
1417 v.type = FcTypeString;
1418 str = FcStrPlus (vle.u.s, vre.u.s);
1419 v.u.s = FcStrdup (str);
1420 FcStrFree (str);
1421
1422 if (!v.u.s)
1423 v.type = FcTypeVoid;
1424 break;
1425 default:
1426 v.type = FcTypeVoid;
1427 break;
1428 }
1429 break;
1430 case FcTypeMatrix:
1431 switch ((int)op) {
1432 case FcOpTimes:
1433 v.type = FcTypeMatrix;
1434 m = malloc (sizeof (FcMatrix));
1435 if (m) {
1436 FcMatrixMultiply (m, vle.u.m, vre.u.m);
1437 v.u.m = m;
1438 } else {
1439 v.type = FcTypeVoid;
1440 }
1441 break;
1442 default:
1443 v.type = FcTypeVoid;
1444 break;
1445 }
1446 break;
1447 case FcTypeCharSet:
1448 switch ((int)op) {
1449 case FcOpPlus:
1450 v.type = FcTypeCharSet;
1451 v.u.c = FcCharSetUnion (vle.u.c, vre.u.c);
1452 if (!v.u.c)
1453 v.type = FcTypeVoid;
1454 break;
1455 case FcOpMinus:
1456 v.type = FcTypeCharSet;
1457 v.u.c = FcCharSetSubtract (vle.u.c, vre.u.c);
1458 if (!v.u.c)
1459 v.type = FcTypeVoid;
1460 break;
1461 default:
1462 v.type = FcTypeVoid;
1463 break;
1464 }
1465 break;
1466 case FcTypeLangSet:
1467 switch ((int)op) {
1468 case FcOpPlus:
1469 v.type = FcTypeLangSet;
1470 v.u.l = FcLangSetUnion (vle.u.l, vre.u.l);
1471 if (!v.u.l)
1472 v.type = FcTypeVoid;
1473 break;
1474 case FcOpMinus:
1475 v.type = FcTypeLangSet;
1476 v.u.l = FcLangSetSubtract (vle.u.l, vre.u.l);
1477 if (!v.u.l)
1478 v.type = FcTypeVoid;
1479 break;
1480 default:
1481 v.type = FcTypeVoid;
1482 break;
1483 }
1484 break;
1485 default:
1486 v.type = FcTypeVoid;
1487 break;
1488 }
1489 } else
1490 v.type = FcTypeVoid;
1491 FcValueDestroy (vl);
1492 FcValueDestroy (vr);
1493 break;
1494 case FcOpNot:
1495 vl = FcConfigEvaluate (p, p_pat, kind, e->u.tree.left);
1496 switch ((int)vl.type) {
1497 case FcTypeBool:
1498 v.type = FcTypeBool;
1499 v.u.b = !vl.u.b;
1500 break;
1501 default:
1502 v.type = FcTypeVoid;
1503 break;
1504 }
1505 FcValueDestroy (vl);
1506 break;
1507 case FcOpFloor:
1508 vl = FcConfigEvaluate (p, p_pat, kind, e->u.tree.left);
1509 switch ((int)vl.type) {
1510 case FcTypeInteger:
1511 v = vl;
1512 break;
1513 case FcTypeDouble:
1514 v.type = FcTypeInteger;
1515 v.u.i = FcDoubleFloor (vl.u.d);
1516 break;
1517 default:
1518 v.type = FcTypeVoid;
1519 break;
1520 }
1521 FcValueDestroy (vl);
1522 break;
1523 case FcOpCeil:
1524 vl = FcConfigEvaluate (p, p_pat, kind, e->u.tree.left);
1525 switch ((int)vl.type) {
1526 case FcTypeInteger:
1527 v = vl;
1528 break;
1529 case FcTypeDouble:
1530 v.type = FcTypeInteger;
1531 v.u.i = FcDoubleCeil (vl.u.d);
1532 break;
1533 default:
1534 v.type = FcTypeVoid;
1535 break;
1536 }
1537 FcValueDestroy (vl);
1538 break;
1539 case FcOpRound:
1540 vl = FcConfigEvaluate (p, p_pat, kind, e->u.tree.left);
1541 switch ((int)vl.type) {
1542 case FcTypeInteger:
1543 v = vl;
1544 break;
1545 case FcTypeDouble:
1546 v.type = FcTypeInteger;
1547 v.u.i = FcDoubleRound (vl.u.d);
1548 break;
1549 default:
1550 v.type = FcTypeVoid;
1551 break;
1552 }
1553 FcValueDestroy (vl);
1554 break;
1555 case FcOpTrunc:
1556 vl = FcConfigEvaluate (p, p_pat, kind, e->u.tree.left);
1557 switch ((int)vl.type) {
1558 case FcTypeInteger:
1559 v = vl;
1560 break;
1561 case FcTypeDouble:
1562 v.type = FcTypeInteger;
1563 v.u.i = FcDoubleTrunc (vl.u.d);
1564 break;
1565 default:
1566 v.type = FcTypeVoid;
1567 break;
1568 }
1569 FcValueDestroy (vl);
1570 break;
1571 default:
1572 v.type = FcTypeVoid;
1573 break;
1574 }
1575 return v;
1576 }
1577
1578 /* The bulk of the time in FcConfigSubstitute is spent walking
1579 * lists of family names. We speed this up with a hash table.
1580 * Since we need to take the ignore-blanks option into account,
1581 * we use two separate hash tables.
1582 */
1583 typedef struct
1584 {
1585 int count;
1586 } FamilyTableEntry;
1587
1588 typedef struct
1589 {
1590 FcHashTable *family_blank_hash;
1591 FcHashTable *family_hash;
1592 } FamilyTable;
1593
1594 static FcBool
1595 FamilyTableLookup (FamilyTable *table,
1596 FcOp _op,
1597 const FcChar8 *s)
1598 {
1599 FamilyTableEntry *fe;
1600 int flags = FC_OP_GET_FLAGS (_op);
1601 FcHashTable *hash;
1602
1603 if (flags & FcOpFlagIgnoreBlanks)
1604 hash = table->family_blank_hash;
1605 else
1606 hash = table->family_hash;
1607
1608 return FcHashTableFind (hash, (const void *)s, (void **)&fe);
1609 }
1610
1611 static void
1612 FamilyTableAdd (FamilyTable *table,
1613 FcValueListPtr values)
1614 {
1615 FcValueListPtr ll;
1616 for (ll = values; ll; ll = FcValueListNext (ll)) {
1617 const FcChar8 *s = FcValueString (&ll->value);
1618 FamilyTableEntry *fe;
1619
1620 if (!FcHashTableFind (table->family_hash, (const void *)s, (void **)&fe)) {
1621 fe = malloc (sizeof (FamilyTableEntry));
1622 fe->count = 0;
1623 FcHashTableAdd (table->family_hash, (void *)s, fe);
1624 }
1625 fe->count++;
1626
1627 if (!FcHashTableFind (table->family_blank_hash, (const void *)s, (void **)&fe)) {
1628 fe = malloc (sizeof (FamilyTableEntry));
1629 fe->count = 0;
1630 FcHashTableAdd (table->family_blank_hash, (void *)s, fe);
1631 }
1632 fe->count++;
1633 }
1634 }
1635
1636 static void
1637 FamilyTableDel (FamilyTable *table,
1638 const FcChar8 *s)
1639 {
1640 FamilyTableEntry *fe;
1641
1642 if (FcHashTableFind (table->family_hash, (void *)s, (void **)&fe)) {
1643 fe->count--;
1644 if (fe->count == 0)
1645 FcHashTableRemove (table->family_hash, (void *)s);
1646 }
1647
1648 if (FcHashTableFind (table->family_blank_hash, (void *)s, (void **)&fe)) {
1649 fe->count--;
1650 if (fe->count == 0)
1651 FcHashTableRemove (table->family_blank_hash, (void *)s);
1652 }
1653 }
1654
1655 static FcBool
1656 copy_string (const void *src, void **dest)
1657 {
1658 *dest = strdup ((char *)src);
1659 return FcTrue;
1660 }
1661
1662 static void
1663 FamilyTableInit (FamilyTable *table,
1664 FcPattern *p)
1665 {
1666 FcPatternElt *e;
1667
1668 table->family_blank_hash = FcHashTableCreate ((FcHashFunc)FcStrHashIgnoreBlanksAndCase,
1669 (FcCompareFunc)FcStrCmpIgnoreBlanksAndCase,
1670 (FcCopyFunc)copy_string,
1671 NULL,
1672 free,
1673 free);
1674 table->family_hash = FcHashTableCreate ((FcHashFunc)FcStrHashIgnoreCase,
1675 (FcCompareFunc)FcStrCmpIgnoreCase,
1676 (FcCopyFunc)copy_string,
1677 NULL,
1678 free,
1679 free);
1680 e = FcPatternObjectFindElt (p, FC_FAMILY_OBJECT);
1681 if (e)
1682 FamilyTableAdd (table, FcPatternEltValues (e));
1683 }
1684
1685 static void
1686 FamilyTableClear (FamilyTable *table)
1687 {
1688 if (table->family_blank_hash)
1689 FcHashTableDestroy (table->family_blank_hash);
1690 if (table->family_hash)
1691 FcHashTableDestroy (table->family_hash);
1692 }
1693
1694 static FcValueList *
1695 FcConfigMatchValueList (FcPattern *p,
1696 FcPattern *p_pat,
1697 FcMatchKind kind,
1698 FcTest *t,
1699 FcValueList *values,
1700 FamilyTable *table)
1701 {
1702 FcValueList *ret = 0;
1703 FcExpr *e = t->expr;
1704 FcValue value;
1705 FcValueList *v;
1706 FcOp op;
1707
1708 while (e) {
1709 /* Compute the value of the match expression */
1710 if (FC_OP_GET_OP (e->op) == FcOpComma) {
1711 value = FcConfigEvaluate (p, p_pat, kind, e->u.tree.left);
1712 e = e->u.tree.right;
1713 } else {
1714 value = FcConfigEvaluate (p, p_pat, kind, e);
1715 e = 0;
1716 }
1717
1718 if (t->object == FC_FAMILY_OBJECT && table) {
1719 op = FC_OP_GET_OP (t->op);
1720 if (op == FcOpEqual || op == FcOpListing) {
1721 if (!FamilyTableLookup (table, t->op, FcValueString (&value))) {
1722 ret = 0;
1723 goto done;
1724 }
1725 }
1726 if (op == FcOpNotEqual && t->qual == FcQualAll) {
1727 ret = 0;
1728 if (!FamilyTableLookup (table, t->op, FcValueString (&value))) {
1729 ret = values;
1730 }
1731 goto done;
1732 }
1733 }
1734 for (v = values; v; v = FcValueListNext (v)) {
1735 /* Compare the pattern value to the match expression value */
1736 if (FcConfigCompareValue (&v->value, t->op, &value)) {
1737 if (!ret)
1738 ret = v;
1739 if (t->qual != FcQualAll)
1740 break;
1741 } else {
1742 if (t->qual == FcQualAll) {
1743 ret = 0;
1744 break;
1745 }
1746 }
1747 }
1748 done:
1749 FcValueDestroy (value);
1750 }
1751 return ret;
1752 }
1753
1754 static FcValueList *
1755 FcConfigValues (FcPattern *p, FcPattern *p_pat, FcMatchKind kind, FcExpr *e, FcValueBinding binding)
1756 {
1757 FcValueList *l;
1758
1759 if (!e)
1760 return 0;
1761 l = (FcValueList *)malloc (sizeof (FcValueList));
1762 if (!l)
1763 return 0;
1764 if (FC_OP_GET_OP (e->op) == FcOpComma) {
1765 l->value = FcConfigEvaluate (p, p_pat, kind, e->u.tree.left);
1766 l->next = FcConfigValues (p, p_pat, kind, e->u.tree.right, binding);
1767 } else {
1768 l->value = FcConfigEvaluate (p, p_pat, kind, e);
1769 l->next = NULL;
1770 }
1771 l->binding = binding;
1772 if (l->value.type == FcTypeVoid) {
1773 FcValueList *next = FcValueListNext (l);
1774
1775 free (l);
1776 l = next;
1777 }
1778
1779 return l;
1780 }
1781
1782 static FcBool
1783 FcConfigAdd (FcValueListPtr *head,
1784 FcValueList *position,
1785 FcBool append,
1786 FcValueList *newp,
1787 FcObject object,
1788 FamilyTable *table)
1789 {
1790 FcValueListPtr *prev, l, last, v;
1791 FcValueBinding sameBinding;
1792
1793 /*
1794 * Make sure the stored type is valid for built-in objects
1795 */
1796 for (l = newp; l != NULL; l = FcValueListNext (l)) {
1797 if (!FcObjectValidType (object, l->value.type)) {
1798 fprintf (stderr,
1799 "Fontconfig warning: FcPattern object %s does not accept value", FcObjectName (object));
1800 FcValuePrintFile (stderr, l->value);
1801 fprintf (stderr, "\n");
1802
1803 if (FcDebug() & FC_DBG_EDIT) {
1804 printf ("Not adding\n");
1805 }
1806
1807 return FcFalse;
1808 }
1809 }
1810
1811 if (object == FC_FAMILY_OBJECT && table) {
1812 FamilyTableAdd (table, newp);
1813 }
1814
1815 if (position)
1816 sameBinding = position->binding;
1817 else
1818 sameBinding = FcValueBindingWeak;
1819 for (v = newp; v != NULL; v = FcValueListNext (v))
1820 if (v->binding == FcValueBindingSame)
1821 v->binding = sameBinding;
1822 if (append) {
1823 if (position)
1824 prev = &position->next;
1825 else
1826 for (prev = head; *prev != NULL;
1827 prev = &(*prev)->next)
1828 ;
1829 } else {
1830 if (position) {
1831 for (prev = head; *prev != NULL;
1832 prev = &(*prev)->next) {
1833 if (*prev == position)
1834 break;
1835 }
1836 } else
1837 prev = head;
1838
1839 if (FcDebug() & FC_DBG_EDIT) {
1840 if (*prev == NULL)
1841 printf ("position not on list\n");
1842 }
1843 }
1844
1845 if (FcDebug() & FC_DBG_EDIT) {
1846 printf ("%s list before ", append ? "Append" : "Prepend");
1847 FcValueListPrintWithPosition (*head, *prev);
1848 printf ("\n");
1849 }
1850
1851 if (newp) {
1852 last = newp;
1853 while (last->next != NULL)
1854 last = last->next;
1855
1856 last->next = *prev;
1857 *prev = newp;
1858 }
1859
1860 if (FcDebug() & FC_DBG_EDIT) {
1861 printf ("%s list after ", append ? "Append" : "Prepend");
1862 FcValueListPrint (*head);
1863 printf ("\n");
1864 }
1865
1866 return FcTrue;
1867 }
1868
1869 static void
1870 FcConfigDel (FcValueListPtr *head,
1871 FcValueList *position,
1872 FcObject object,
1873 FamilyTable *table)
1874 {
1875 FcValueListPtr *prev;
1876
1877 if (object == FC_FAMILY_OBJECT && table) {
1878 FamilyTableDel (table, FcValueString (&position->value));
1879 }
1880
1881 for (prev = head; *prev != NULL; prev = &(*prev)->next) {
1882 if (*prev == position) {
1883 *prev = position->next;
1884 position->next = NULL;
1885 FcValueListDestroy (position);
1886 break;
1887 }
1888 }
1889 }
1890
1891 static void
1892 FcConfigPatternAdd (FcPattern *p,
1893 FcObject object,
1894 FcValueList *list,
1895 FcBool append,
1896 FamilyTable *table)
1897 {
1898 if (list) {
1899 FcPatternElt *e = FcPatternObjectInsertElt (p, object);
1900
1901 if (!e)
1902 return;
1903 FcConfigAdd (&e->values, 0, append, list, object, table);
1904 }
1905 }
1906
1907 /*
1908 * Delete all values associated with a field
1909 */
1910 static void
1911 FcConfigPatternDel (FcPattern *p,
1912 FcObject object,
1913 FamilyTable *table)
1914 {
1915 FcPatternElt *e = FcPatternObjectFindElt (p, object);
1916 if (!e)
1917 return;
1918 while (e->values != NULL)
1919 FcConfigDel (&e->values, e->values, object, table);
1920 }
1921
1922 static void
1923 FcConfigPatternCanon (FcPattern *p,
1924 FcObject object)
1925 {
1926 FcPatternElt *e = FcPatternObjectFindElt (p, object);
1927 if (!e)
1928 return;
1929 if (e->values == NULL)
1930 FcPatternObjectDel (p, object);
1931 }
1932
1933 FcBool
1934 FcConfigSubstituteWithPat (FcConfig *config,
1935 FcPattern *p,
1936 FcPattern *p_pat,
1937 FcMatchKind kind)
1938 {
1939 FcValue v;
1940 FcPtrList *s;
1941 FcPtrListIter iter, iter2;
1942 FcRule *r;
1943 FcRuleSet *rs;
1944 FcValueList *l, **value = NULL, *vl;
1945 FcPattern *m;
1946 FcStrSet *strs;
1947 FcObject object = FC_INVALID_OBJECT;
1948 FcPatternElt **elt = NULL, *e;
1949 int i, nobjs;
1950 FcBool retval = FcTrue;
1951 FcTest **tst = NULL;
1952 FamilyTable data;
1953 FamilyTable *table = &data;
1954
1955 if (kind < FcMatchKindBegin || kind >= FcMatchKindEnd)
1956 return FcFalse;
1957
1958 config = FcConfigReference (config);
1959 if (!config)
1960 return FcFalse;
1961
1962 s = config->subst[kind];
1963 if (kind == FcMatchPattern) {
1964 strs = FcConfigGetDefaultLangs (config);
1965 if (strs) {
1966 FcStrList *l = FcStrListCreate (strs);
1967 FcChar8 *lang;
1968 FcValue v;
1969 FcLangSet *lsund = FcLangSetCreate();
1970
1971 FcLangSetAdd (lsund, (const FcChar8 *)"und");
1972 FcStrSetDestroy (strs);
1973 while (l && (lang = FcStrListNext (l))) {
1974 FcPatternElt *e = FcPatternObjectFindElt (p, FC_LANG_OBJECT);
1975
1976 if (e) {
1977 FcValueListPtr ll;
1978
1979 for (ll = FcPatternEltValues (e); ll; ll = FcValueListNext (ll)) {
1980 FcValue vv = FcValueCanonicalize (&ll->value);
1981
1982 if (vv.type == FcTypeLangSet) {
1983 FcLangSet *ls = FcLangSetCreate();
1984 FcBool b;
1985
1986 FcLangSetAdd (ls, lang);
1987 b = FcLangSetContains (vv.u.l, ls);
1988 FcLangSetDestroy (ls);
1989 if (b)
1990 goto bail_lang;
1991 if (FcLangSetContains (vv.u.l, lsund))
1992 goto bail_lang;
1993 } else {
1994 if (FcStrCmpIgnoreCase (vv.u.s, lang) == 0)
1995 goto bail_lang;
1996 if (FcStrCmpIgnoreCase (vv.u.s, (const FcChar8 *)"und") == 0)
1997 goto bail_lang;
1998 }
1999 }
2000 }
2001 v.type = FcTypeString;
2002 v.u.s = lang;
2003
2004 FcPatternObjectAddWithBinding (p, FC_LANG_OBJECT, v, FcValueBindingWeak, FcTrue);
2005 }
2006 bail_lang:
2007 FcStrListDone (l);
2008 FcLangSetDestroy (lsund);
2009 }
2010 if (FcPatternObjectGet (p, FC_PRGNAME_OBJECT, 0, &v) == FcResultNoMatch) {
2011 FcChar8 *prgname = FcConfigGetPrgname (config);
2012 if (prgname)
2013 FcPatternObjectAddString (p, FC_PRGNAME_OBJECT, prgname);
2014 }
2015 }
2016
2017 nobjs = FC_MAX_BASE_OBJECT + config->maxObjects + 2;
2018 value = (FcValueList **)malloc (SIZEOF_VOID_P * nobjs);
2019 if (!value) {
2020 retval = FcFalse;
2021 goto bail1;
2022 }
2023 elt = (FcPatternElt **)malloc (SIZEOF_VOID_P * nobjs);
2024 if (!elt) {
2025 retval = FcFalse;
2026 goto bail1;
2027 }
2028 tst = (FcTest **)malloc (SIZEOF_VOID_P * nobjs);
2029 if (!tst) {
2030 retval = FcFalse;
2031 goto bail1;
2032 }
2033
2034 if (FcDebug() & FC_DBG_EDIT) {
2035 printf ("FcConfigSubstitute ");
2036 FcPatternPrint (p);
2037 }
2038
2039 FamilyTableInit (&data, p);
2040
2041 FcPtrListIterInit (s, &iter);
2042 for (; FcPtrListIterIsValid (s, &iter); FcPtrListIterNext (s, &iter)) {
2043 rs = (FcRuleSet *)FcPtrListIterGetValue (s, &iter);
2044 if (FcDebug() & FC_DBG_EDIT) {
2045 printf ("\nRule Set: %s\n", rs->name);
2046 }
2047 FcPtrListIterInit (rs->subst[kind], &iter2);
2048 for (; FcPtrListIterIsValid (rs->subst[kind], &iter2); FcPtrListIterNext (rs->subst[kind], &iter2)) {
2049 r = (FcRule *)FcPtrListIterGetValue (rs->subst[kind], &iter2);
2050 for (i = 0; i < nobjs; i++) {
2051 elt[i] = NULL;
2052 value[i] = NULL;
2053 tst[i] = NULL;
2054 }
2055 for (; r; r = r->next) {
2056 switch (r->type) {
2057 case FcRuleUnknown:
2058 /* shouldn't be reached */
2059 break;
2060 case FcRuleTest:
2061 object = FC_OBJ_ID (r->u.test->object);
2062 /*
2063 * Check the tests to see if
2064 * they all match the pattern
2065 */
2066 if (FcDebug() & FC_DBG_EDIT) {
2067 printf ("FcConfigSubstitute test ");
2068 FcTestPrint (r->u.test);
2069 }
2070 if (kind == FcMatchFont && r->u.test->kind == FcMatchPattern) {
2071 m = p_pat;
2072 table = NULL;
2073 } else {
2074 m = p;
2075 table = &data;
2076 }
2077 if (m)
2078 e = FcPatternObjectFindElt (m, r->u.test->object);
2079 else
2080 e = NULL;
2081 /* different 'kind' won't be the target of edit */
2082 if (!elt[object] && kind == r->u.test->kind) {
2083 elt[object] = e;
2084 tst[object] = r->u.test;
2085 }
2086 /*
2087 * If there's no such field in the font,
2088 * then FcQualAll matches while FcQualAny does not
2089 */
2090 if (!e) {
2091 if (r->u.test->qual == FcQualAll) {
2092 value[object] = NULL;
2093 continue;
2094 } else {
2095 if (FcDebug() & FC_DBG_EDIT)
2096 printf ("No match\n");
2097 goto bail;
2098 }
2099 }
2100 /*
2101 * Check to see if there is a match, mark the location
2102 * to apply match-relative edits
2103 */
2104 vl = FcConfigMatchValueList (m, p_pat, kind, r->u.test, e->values, table);
2105 /* different 'kind' won't be the target of edit */
2106 if (!value[object] && kind == r->u.test->kind)
2107 value[object] = vl;
2108 if (!vl ||
2109 (r->u.test->qual == FcQualFirst && vl != e->values) ||
2110 (r->u.test->qual == FcQualNotFirst && vl == e->values)) {
2111 if (FcDebug() & FC_DBG_EDIT)
2112 printf ("No match\n");
2113 goto bail;
2114 }
2115 break;
2116 case FcRuleEdit:
2117 object = FC_OBJ_ID (r->u.edit->object);
2118 if (FcDebug() & FC_DBG_EDIT) {
2119 printf ("Substitute ");
2120 FcEditPrint (r->u.edit);
2121 printf ("\n\n");
2122 }
2123 /*
2124 * Evaluate the list of expressions
2125 */
2126 l = FcConfigValues (p, p_pat, kind, r->u.edit->expr, r->u.edit->binding);
2127 if (tst[object] && (tst[object]->kind == FcMatchFont || kind == FcMatchPattern))
2128 elt[object] = FcPatternObjectFindElt (p, tst[object]->object);
2129
2130 switch (FC_OP_GET_OP (r->u.edit->op)) {
2131 case FcOpAssign:
2132 /*
2133 * If there was a test, then replace the matched
2134 * value with the new list of values
2135 */
2136 if (value[object]) {
2137 FcValueList *thisValue = value[object];
2138 FcValueList *nextValue = l;
2139
2140 /*
2141 * Append the new list of values after the current value
2142 */
2143 FcConfigAdd (&elt[object]->values, thisValue, FcTrue, l, r->u.edit->object, table);
2144 /*
2145 * Delete the marked value
2146 */
2147 if (thisValue)
2148 FcConfigDel (&elt[object]->values, thisValue, object, table);
2149 /*
2150 * Adjust a pointer into the value list to ensure
2151 * future edits occur at the same place
2152 */
2153 value[object] = nextValue;
2154 break;
2155 }
2156 /* fall through ... */
2157 case FcOpAssignReplace:
2158 /*
2159 * Delete all of the values and insert
2160 * the new set
2161 */
2162 FcConfigPatternDel (p, r->u.edit->object, table);
2163 FcConfigPatternAdd (p, r->u.edit->object, l, FcTrue, table);
2164 /*
2165 * Adjust a pointer into the value list as they no
2166 * longer point to anything valid
2167 */
2168 value[object] = NULL;
2169 break;
2170 case FcOpPrepend:
2171 if (value[object]) {
2172 FcConfigAdd (&elt[object]->values, value[object], FcFalse, l, r->u.edit->object, table);
2173 break;
2174 }
2175 /* fall through ... */
2176 case FcOpPrependFirst:
2177 FcConfigPatternAdd (p, r->u.edit->object, l, FcFalse, table);
2178 break;
2179 case FcOpAppend:
2180 if (value[object]) {
2181 FcConfigAdd (&elt[object]->values, value[object], FcTrue, l, r->u.edit->object, table);
2182 break;
2183 }
2184 /* fall through ... */
2185 case FcOpAppendLast:
2186 FcConfigPatternAdd (p, r->u.edit->object, l, FcTrue, table);
2187 break;
2188 case FcOpDelete:
2189 if (value[object]) {
2190 FcConfigDel (&elt[object]->values, value[object], object, table);
2191 FcValueListDestroy (l);
2192 break;
2193 }
2194 /* fall through ... */
2195 case FcOpDeleteAll:
2196 FcConfigPatternDel (p, r->u.edit->object, table);
2197 FcValueListDestroy (l);
2198 break;
2199 default:
2200 FcValueListDestroy (l);
2201 break;
2202 }
2203 /*
2204 * Now go through the pattern and eliminate
2205 * any properties without data
2206 */
2207 FcConfigPatternCanon (p, r->u.edit->object);
2208
2209 if (FcDebug() & FC_DBG_EDIT) {
2210 printf ("FcConfigSubstitute edit");
2211 FcPatternPrint (p);
2212 }
2213 break;
2214 }
2215 }
2216 bail:;
2217 }
2218 }
2219 if (FcDebug() & FC_DBG_EDIT) {
2220 printf ("FcConfigSubstitute done");
2221 FcPatternPrint (p);
2222 }
2223 FamilyTableClear (&data);
2224 bail1:
2225 if (elt)
2226 free (elt);
2227 if (value)
2228 free (value);
2229 if (tst)
2230 free (tst);
2231 FcConfigDestroy (config);
2232
2233 return retval;
2234 }
2235
2236 FcBool
2237 FcConfigSubstitute (FcConfig *config,
2238 FcPattern *p,
2239 FcMatchKind kind)
2240 {
2241 return FcConfigSubstituteWithPat (config, p, 0, kind);
2242 }
2243
2244 #if defined(_WIN32)
2245
2246 static FcChar8 fontconfig_path[1000] = ""; /* MT-dontcare */
2247 FcChar8 fontconfig_instprefix[1000] = ""; /* MT-dontcare */
2248
2249 # if (defined(PIC) || defined(DLL_EXPORT))
2250
2251 BOOL WINAPI
2252 DllMain (HINSTANCE hinstDLL,
2253 DWORD fdwReason,
2254 LPVOID lpvReserved);
2255
2256 BOOL WINAPI
2257 DllMain (HINSTANCE hinstDLL,
2258 DWORD fdwReason,
2259 LPVOID lpvReserved)
2260 {
2261 FcChar8 *p;
2262
2263 switch (fdwReason) {
2264 case DLL_PROCESS_ATTACH:
2265 if (!GetModuleFileName ((HMODULE)hinstDLL, (LPCH)fontconfig_path,
2266 sizeof (fontconfig_path)))
2267 break;
2268
2269 /* If the fontconfig DLL is in a "bin" or "lib" subfolder,
2270 * assume it's a Unix-style installation tree, and use
2271 * "etc/fonts" in there as FONTCONFIG_PATH. Otherwise use the
2272 * folder where the DLL is as FONTCONFIG_PATH.
2273 */
2274 p = (FcChar8 *)strrchr ((const char *)fontconfig_path, '\\');
2275 if (p) {
2276 *p = '\0';
2277 p = (FcChar8 *)strrchr ((const char *)fontconfig_path, '\\');
2278 if (p && (FcStrCmpIgnoreCase (p + 1, (const FcChar8 *)"bin") == 0 ||
2279 FcStrCmpIgnoreCase (p + 1, (const FcChar8 *)"lib") == 0))
2280 *p = '\0';
2281 strcat ((char *)fontconfig_instprefix, (char *)fontconfig_path);
2282 strcat ((char *)fontconfig_path, "\\etc\\fonts");
2283 } else
2284 fontconfig_path[0] = '\0';
2285
2286 break;
2287 }
2288
2289 return TRUE;
2290 }
2291
2292 # endif /* !PIC */
2293
2294 # undef FONTCONFIG_PATH
2295 # define FONTCONFIG_PATH fontconfig_path
2296
2297 #endif /* !_WIN32 */
2298
2299 #ifndef FONTCONFIG_FILE
2300 # define FONTCONFIG_FILE "fonts.conf"
2301 #endif
2302
2303 static FcChar8 *
2304 FcConfigFileExists (const FcChar8 *dir, const FcChar8 *file)
2305 {
2306 FcChar8 *path;
2307 int size, osize;
2308
2309 if (!dir)
2310 dir = (FcChar8 *)"";
2311
2312 osize = strlen ((char *)dir) + 1 + strlen ((char *)file) + 1;
2313 /*
2314 * workaround valgrind warning because glibc takes advantage of how it knows memory is
2315 * allocated to implement strlen by reading in groups of 4
2316 */
2317 size = (osize + 3) & ~3;
2318
2319 path = malloc (size);
2320 if (!path)
2321 return 0;
2322
2323 strcpy ((char *)path, (const char *)dir);
2324 /* make sure there's a single separator */
2325 #ifdef _WIN32
2326 if ((!path[0] || (path[strlen ((char *)path) - 1] != '/' &&
2327 path[strlen ((char *)path) - 1] != '\\')) &&
2328 !(file[0] == '/' ||
2329 file[0] == '\\' ||
2330 (isalpha (file[0]) && file[1] == ':' && (file[2] == '/' || file[2] == '\\'))))
2331 strcat ((char *)path, "\\");
2332 #else
2333 if ((!path[0] || path[strlen ((char *)path) - 1] != '/') && file[0] != '/')
2334 strcat ((char *)path, "/");
2335 else
2336 osize--;
2337 #endif
2338 strcat ((char *)path, (char *)file);
2339
2340 if (access ((char *)path, R_OK) == 0)
2341 return path;
2342
2343 FcStrFree (path);
2344
2345 return 0;
2346 }
2347
2348 static FcChar8 **
2349 FcConfigGetPath (void)
2350 {
2351 FcChar8 **path;
2352 FcChar8 *env, *e, *colon;
2353 FcChar8 *dir;
2354 int npath;
2355 int i;
2356
2357 npath = 2; /* default dir + null */
2358 env = (FcChar8 *)getenv ("FONTCONFIG_PATH");
2359 if (env) {
2360 e = env;
2361 npath++;
2362 while (*e)
2363 if (*e++ == FC_SEARCH_PATH_SEPARATOR)
2364 npath++;
2365 }
2366 path = calloc (npath, sizeof (FcChar8 *));
2367 if (!path)
2368 goto bail0;
2369 i = 0;
2370
2371 if (env) {
2372 e = env;
2373 while (*e) {
2374 colon = (FcChar8 *)strchr ((char *)e, FC_SEARCH_PATH_SEPARATOR);
2375 if (!colon)
2376 colon = e + strlen ((char *)e);
2377 path[i] = malloc (colon - e + 1);
2378 if (!path[i])
2379 goto bail1;
2380 strncpy ((char *)path[i], (const char *)e, colon - e);
2381 path[i][colon - e] = '\0';
2382 if (*colon)
2383 e = colon + 1;
2384 else
2385 e = colon;
2386 i++;
2387 }
2388 }
2389
2390 #ifdef _WIN32
2391 if (fontconfig_path[0] == '\0') {
2392 char *p;
2393 if (!GetModuleFileName (NULL, (LPCH)fontconfig_path, sizeof (fontconfig_path)))
2394 goto bail1;
2395 p = strrchr ((const char *)fontconfig_path, '\\');
2396 if (p)
2397 *p = '\0';
2398 strcat ((char *)fontconfig_path, "\\fonts");
2399 }
2400 #endif
2401 dir = (FcChar8 *)FONTCONFIG_PATH;
2402 path[i] = malloc (strlen ((char *)dir) + 1);
2403 if (!path[i])
2404 goto bail1;
2405 strcpy ((char *)path[i], (const char *)dir);
2406 return path;
2407
2408 bail1:
2409 for (i = 0; path[i]; i++)
2410 free (path[i]);
2411 free (path);
2412 bail0:
2413 return 0;
2414 }
2415
2416 static void
2417 FcConfigFreePath (FcChar8 **path)
2418 {
2419 FcChar8 **p;
2420
2421 for (p = path; *p; p++)
2422 free (*p);
2423 free (path);
2424 }
2425
2426 static FcBool _FcConfigHomeEnabled = FcTrue; /* MT-goodenough */
2427
2428 FcChar8 *
2429 FcConfigHome (void)
2430 {
2431 if (_FcConfigHomeEnabled) {
2432 char *home = getenv ("HOME");
2433
2434 #ifdef _WIN32
2435 if (home == NULL)
2436 home = getenv ("USERPROFILE");
2437 #endif
2438
2439 return (FcChar8 *)home;
2440 }
2441 return 0;
2442 }
2443
2444 FcChar8 *
2445 FcConfigXdgCacheHome (void)
2446 {
2447 const char *env = getenv ("XDG_CACHE_HOME");
2448 FcChar8 *ret = NULL;
2449
2450 if (!_FcConfigHomeEnabled)
2451 return NULL;
2452 if (env && env[0])
2453 ret = FcStrCopy ((const FcChar8 *)env);
2454 else {
2455 const FcChar8 *home = FcConfigHome();
2456 size_t len = home ? strlen ((const char *)home) : 0;
2457
2458 ret = malloc (len + 7 + 1);
2459 if (ret) {
2460 if (home)
2461 memcpy (ret, home, len);
2462 memcpy (&ret[len], FC_DIR_SEPARATOR_S ".cache", 7);
2463 ret[len + 7] = 0;
2464 }
2465 }
2466
2467 return ret;
2468 }
2469
2470 FcChar8 *
2471 FcConfigXdgConfigHome (void)
2472 {
2473 const char *env = getenv ("XDG_CONFIG_HOME");
2474 FcChar8 *ret = NULL;
2475
2476 if (!_FcConfigHomeEnabled)
2477 return NULL;
2478 if (env)
2479 ret = FcStrCopy ((const FcChar8 *)env);
2480 else {
2481 const FcChar8 *home = FcConfigHome();
2482 size_t len = home ? strlen ((const char *)home) : 0;
2483
2484 ret = malloc (len + 8 + 1);
2485 if (ret) {
2486 if (home)
2487 memcpy (ret, home, len);
2488 memcpy (&ret[len], FC_DIR_SEPARATOR_S ".config", 8);
2489 ret[len + 8] = 0;
2490 }
2491 }
2492
2493 return ret;
2494 }
2495
2496 FcChar8 *
2497 FcConfigXdgDataHome (void)
2498 {
2499 const char *env = getenv ("XDG_DATA_HOME");
2500 FcChar8 *ret = NULL;
2501
2502 if (!_FcConfigHomeEnabled)
2503 return NULL;
2504 if (env)
2505 ret = FcStrCopy ((const FcChar8 *)env);
2506 else {
2507 const FcChar8 *home = FcConfigHome();
2508 size_t len = home ? strlen ((const char *)home) : 0;
2509
2510 ret = malloc (len + 13 + 1);
2511 if (ret) {
2512 if (home)
2513 memcpy (ret, home, len);
2514 memcpy (&ret[len], FC_DIR_SEPARATOR_S ".local" FC_DIR_SEPARATOR_S "share", 13);
2515 ret[len + 13] = 0;
2516 }
2517 }
2518
2519 return ret;
2520 }
2521
2522 FcStrSet *
2523 FcConfigXdgDataDirs (void)
2524 {
2525 const char *env = getenv ("XDG_DATA_DIRS");
2526 FcStrSet *ret = FcStrSetCreate();
2527
2528 if (env && *env) {
2529 FcChar8 *ee, *e = ee = FcStrCopy ((const FcChar8 *)env);
2530
2531 /* We don't intentionally use FC_SEARCH_PATH_SEPARATOR here because of:
2532 * The directories in $XDG_DATA_DIRS should be seperated with a colon ':'.
2533 * in doc.
2534 */
2535 while (e) {
2536 FcChar8 *p = (FcChar8 *)strchr ((const char *)e, ':');
2537 FcChar8 *s;
2538 size_t len;
2539
2540 if (!p) {
2541 s = FcStrCopy (e);
2542 e = NULL;
2543 } else {
2544 *p = 0;
2545 s = FcStrCopy (e);
2546 e = p + 1;
2547 }
2548 len = strlen ((const char *)s);
2549 if (len == 0) {
2550 FcStrFree (s);
2551 continue;
2552 }
2553
2554 if (s[len - 1] == FC_DIR_SEPARATOR) {
2555 do {
2556 len--;
2557 } while (len > 1 && s[len - 1] == FC_DIR_SEPARATOR);
2558 s[len] = 0;
2559 }
2560 FcStrSetAdd (ret, s);
2561 FcStrFree (s);
2562 }
2563 FcStrFree (ee);
2564 } else {
2565 /* From spec doc at https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html#variables
2566 *
2567 * If $XDG_DATA_DIRS is either not set or empty, a value equal to /usr/local/share/:/usr/share/ should be used.
2568 */
2569 FcStrSetAdd (ret, (const FcChar8 *)"/usr/local/share");
2570 FcStrSetAdd (ret, (const FcChar8 *)"/usr/share");
2571 }
2572
2573 return ret;
2574 }
2575
2576 FcBool
2577 FcConfigEnableHome (FcBool enable)
2578 {
2579 FcBool prev = _FcConfigHomeEnabled;
2580 _FcConfigHomeEnabled = enable;
2581 return prev;
2582 }
2583
2584 FcChar8 *
2585 FcConfigGetFilename (FcConfig *config,
2586 const FcChar8 *url)
2587 {
2588 FcChar8 *file, *dir, **path, **p;
2589 const FcChar8 *sysroot;
2590
2591 config = FcConfigReference (config);
2592 if (!config)
2593 return NULL;
2594 sysroot = FcConfigGetSysRoot (config);
2595 if (!url || !*url) {
2596 url = (FcChar8 *)getenv ("FONTCONFIG_FILE");
2597 if (!url)
2598 url = (FcChar8 *)FONTCONFIG_FILE;
2599 }
2600 file = 0;
2601
2602 if (FcStrIsAbsoluteFilename (url)) {
2603 if (sysroot) {
2604 size_t len = strlen ((const char *)sysroot);
2605
2606 /* Workaround to avoid adding sysroot repeatedly */
2607 if (strncmp ((const char *)url, (const char *)sysroot, len) == 0)
2608 sysroot = NULL;
2609 }
2610 file = FcConfigFileExists (sysroot, url);
2611 goto bail;
2612 }
2613
2614 if (*url == '~') {
2615 dir = FcConfigHome();
2616 if (dir) {
2617 FcChar8 *s;
2618
2619 if (sysroot)
2620 s = FcStrBuildFilename (sysroot, dir, NULL);
2621 else
2622 s = dir;
2623 file = FcConfigFileExists (s, url + 1);
2624 if (sysroot)
2625 FcStrFree (s);
2626 } else
2627 file = 0;
2628 } else {
2629 path = FcConfigGetPath();
2630 if (!path) {
2631 file = NULL;
2632 goto bail;
2633 }
2634 for (p = path; *p; p++) {
2635 FcChar8 *s;
2636
2637 if (sysroot)
2638 s = FcStrBuildFilename (sysroot, *p, NULL);
2639 else
2640 s = *p;
2641 file = FcConfigFileExists (s, url);
2642 if (sysroot)
2643 FcStrFree (s);
2644 if (file)
2645 break;
2646 }
2647 FcConfigFreePath (path);
2648 }
2649 bail:
2650 FcConfigDestroy (config);
2651
2652 return file;
2653 }
2654
2655 FcChar8 *
2656 FcConfigFilename (const FcChar8 *url)
2657 {
2658 return FcConfigGetFilename (NULL, url);
2659 }
2660
2661 FcChar8 *
2662 FcConfigRealFilename (FcConfig *config,
2663 const FcChar8 *url)
2664 {
2665 FcChar8 *n = FcConfigGetFilename (config, url);
2666
2667 if (n) {
2668 FcChar8 buf[FC_PATH_MAX];
2669 ssize_t len;
2670 struct stat sb;
2671
2672 if ((len = FcReadLink (n, buf, sizeof (buf) - 1)) != -1) {
2673 buf[len] = 0;
2674
2675 /* We try to pick up a config from FONTCONFIG_FILE
2676 * when url is null. don't try to address the real filename
2677 * if it is a named pipe.
2678 */
2679 if (!url && FcStat (n, &sb) == 0 && S_ISFIFO (sb.st_mode))
2680 return n;
2681 else if (!FcStrIsAbsoluteFilename (buf)) {
2682 FcChar8 *dirname = FcStrDirname (n);
2683 FcStrFree (n);
2684 if (!dirname)
2685 return NULL;
2686
2687 FcChar8 *path = FcStrBuildFilename (dirname, buf, NULL);
2688 FcStrFree (dirname);
2689 if (!path)
2690 return NULL;
2691
2692 n = FcStrCanonFilename (path);
2693 FcStrFree (path);
2694 } else {
2695 FcStrFree (n);
2696 n = FcStrdup (buf);
2697 }
2698 }
2699 }
2700
2701 return n;
2702 }
2703
2704 /*
2705 * Manage the application-specific fonts
2706 */
2707
2708 FcBool
2709 FcConfigAppFontAddFile (FcConfig *config,
2710 const FcChar8 *file)
2711 {
2712 FcFontSet *set;
2713 FcStrSet *subdirs;
2714 FcStrList *sublist;
2715 FcChar8 *subdir;
2716 FcBool ret = FcTrue;
2717
2718 config = FcConfigReference (config);
2719 if (!config)
2720 return FcFalse;
2721
2722 subdirs = FcStrSetCreateEx (FCSS_GROW_BY_64);
2723 if (!subdirs) {
2724 ret = FcFalse;
2725 goto bail;
2726 }
2727
2728 set = FcConfigGetFonts (config, FcSetApplication);
2729 if (!set) {
2730 set = FcFontSetCreate();
2731 if (!set) {
2732 FcStrSetDestroy (subdirs);
2733 ret = FcFalse;
2734 goto bail;
2735 }
2736 FcConfigSetFonts (config, set, FcSetApplication);
2737 }
2738
2739 if (!FcFileScanConfig (set, subdirs, file, config)) {
2740 FcStrSetDestroy (subdirs);
2741 ret = FcFalse;
2742 goto bail;
2743 }
2744 if ((sublist = FcStrListCreate (subdirs))) {
2745 while ((subdir = FcStrListNext (sublist))) {
2746 FcConfigAppFontAddDir (config, subdir);
2747 }
2748 FcStrListDone (sublist);
2749 }
2750 FcStrSetDestroy (subdirs);
2751 bail:
2752 FcConfigDestroy (config);
2753
2754 return ret;
2755 }
2756
2757 FcBool
2758 FcConfigAppFontAddDir (FcConfig *config,
2759 const FcChar8 *dir)
2760 {
2761 FcFontSet *set;
2762 FcStrSet *dirs;
2763 FcBool ret = FcTrue;
2764
2765 config = FcConfigReference (config);
2766 if (!config)
2767 return FcFalse;
2768
2769 dirs = FcStrSetCreateEx (FCSS_GROW_BY_64);
2770 if (!dirs) {
2771 ret = FcFalse;
2772 goto bail;
2773 }
2774
2775 set = FcConfigGetFonts (config, FcSetApplication);
2776 if (!set) {
2777 set = FcFontSetCreate();
2778 if (!set) {
2779 FcStrSetDestroy (dirs);
2780 ret = FcFalse;
2781 goto bail;
2782 }
2783 FcConfigSetFonts (config, set, FcSetApplication);
2784 }
2785
2786 FcStrSetAddFilename (dirs, dir);
2787
2788 if (!FcConfigAddDirList (config, FcSetApplication, dirs)) {
2789 FcStrSetDestroy (dirs);
2790 ret = FcFalse;
2791 goto bail;
2792 }
2793 FcStrSetDestroy (dirs);
2794 bail:
2795 FcConfigDestroy (config);
2796
2797 return ret;
2798 }
2799
2800 void
2801 FcConfigAppFontClear (FcConfig *config)
2802 {
2803 config = FcConfigReference (config);
2804 if (!config)
2805 return;
2806
2807 FcConfigSetFonts (config, 0, FcSetApplication);
2808
2809 FcConfigDestroy (config);
2810 }
2811
2812 void
2813 FcConfigPreferAppFont (FcConfig *config, FcBool flag)
2814 {
2815 config = FcConfigReference (config);
2816 if (!config)
2817 return;
2818
2819 config->prefer_app_fonts = flag;
2820
2821 FcConfigDestroy (config);
2822 }
2823
2824 /*
2825 * Manage filename-based font source selectors
2826 */
2827
2828 FcBool
2829 FcConfigGlobAdd (FcConfig *config,
2830 const FcChar8 *glob,
2831 FcBool accept)
2832 {
2833 FcStrSet *set = accept ? config->acceptGlobs : config->rejectGlobs;
2834 FcChar8 *realglob = FcStrCopyFilename (glob);
2835 FcChar8 *cwd = FcStrCopyFilename ((const FcChar8 *)".");
2836 const FcChar8 *s;
2837 FcBool ret;
2838 size_t len = 0;
2839
2840 /*
2841 * FcStrCopyFilename canonicalize a path string and prepend
2842 * current directory name if no path included in a string.
2843 * This isn't a desired behavior here.
2844 * So drop the extra path name if they have. Otherwise use it as it is.
2845 */
2846 if (cwd == NULL)
2847 s = glob;
2848 else {
2849 len = strlen ((const char *)cwd);
2850 /* No need to use FC_DIR_SEPARATOR because '\\' will be
2851 * replaced with / by FcConvertDosPath in FcStrCanonFilename
2852 */
2853 if (strncmp ((const char *)cwd, (const char *)realglob, len) == 0 &&
2854 realglob[len] == '/')
2855 s = &realglob[len + 1];
2856 else
2857 s = realglob;
2858 }
2859 if (!s)
2860 return FcFalse;
2861
2862 ret = FcStrSetAdd (set, s);
2863 FcStrFree (realglob);
2864 FcStrFree (cwd);
2865 return ret;
2866 }
2867
2868 static FcBool
2869 FcConfigGlobsMatch (const FcStrSet *globs,
2870 const FcChar8 *string)
2871 {
2872 int i;
2873
2874 for (i = 0; i < globs->num; i++)
2875 if (FcStrGlobMatch (globs->strs[i], string))
2876 return FcTrue;
2877 return FcFalse;
2878 }
2879
2880 FcBool
2881 FcConfigAcceptFilename (FcConfig *config,
2882 const FcChar8 *filename)
2883 {
2884 if (FcConfigGlobsMatch (config->acceptGlobs, filename))
2885 return FcTrue;
2886 if (FcConfigGlobsMatch (config->rejectGlobs, filename))
2887 return FcFalse;
2888 return FcTrue;
2889 }
2890
2891 /*
2892 * Manage font-pattern based font source selectors
2893 */
2894
2895 FcBool
2896 FcConfigPatternsAdd (FcConfig *config,
2897 FcPattern *pattern,
2898 FcBool accept)
2899 {
2900 FcFontSet *set = accept ? config->acceptPatterns : config->rejectPatterns;
2901
2902 return FcFontSetAdd (set, pattern);
2903 }
2904
2905 static FcBool
2906 FcConfigPatternsMatch (const FcFontSet *patterns,
2907 const FcPattern *font)
2908 {
2909 int i;
2910
2911 for (i = 0; i < patterns->nfont; i++)
2912 if (FcListPatternMatchAny (patterns->fonts[i], font))
2913 return FcTrue;
2914 return FcFalse;
2915 }
2916
2917 FcBool
2918 FcConfigAcceptFont (FcConfig *config,
2919 const FcPattern *font)
2920 {
2921 if (FcConfigPatternsMatch (config->acceptPatterns, font))
2922 return FcTrue;
2923 if (FcConfigPatternsMatch (config->rejectPatterns, font))
2924 return FcFalse;
2925 return FcTrue;
2926 }
2927
2928 FcBool
2929 FcConfigAcceptFilter (FcConfig *config,
2930 const FcPattern *font)
2931 {
2932 if (config && config->filter_func) {
2933 return config->filter_func (font, config->filter_data);
2934 }
2935 return FcTrue;
2936 }
2937 const FcChar8 *
2938 FcConfigGetSysRoot (const FcConfig *config)
2939 {
2940 if (!config) {
2941 config = FcConfigGetCurrent();
2942 if (!config)
2943 return NULL;
2944 }
2945 return config->sysRoot;
2946 }
2947
2948 void
2949 FcConfigSetSysRoot (FcConfig *config,
2950 const FcChar8 *sysroot)
2951 {
2952 FcChar8 *s = NULL;
2953 FcBool init = FcFalse;
2954 int nretry = 3;
2955
2956 retry:
2957 if (!config) {
2958 /* We can't use FcConfigGetCurrent() here to ensure
2959 * the sysroot is set prior to initialize FcConfig,
2960 * to avoid loading caches from non-sysroot dirs.
2961 * So postpone the initialization later.
2962 */
2963 config = fc_atomic_ptr_get (&_fcConfig);
2964 if (!config) {
2965 config = FcConfigCreate();
2966 if (!config)
2967 return;
2968 init = FcTrue;
2969 }
2970 }
2971
2972 if (sysroot) {
2973 s = FcStrRealPath (sysroot);
2974 if (!s)
2975 return;
2976 }
2977
2978 if (config->sysRoot)
2979 FcStrFree (config->sysRoot);
2980
2981 config->sysRoot = s;
2982 if (init) {
2983 config = FcInitLoadOwnConfigAndFonts (config);
2984 if (!config) {
2985 /* Something failed. this is usually unlikely. so retrying */
2986 init = FcFalse;
2987 if (--nretry == 0) {
2988 fprintf (stderr, "Fontconfig warning: Unable to initialize config and retry limit exceeded. sysroot functionality may not work as expected.\n");
2989 return;
2990 }
2991 goto retry;
2992 }
2993 FcConfigSetCurrent (config);
2994 /* FcConfigSetCurrent() increases the refcount.
2995 * decrease it here to avoid the memory leak.
2996 */
2997 FcConfigDestroy (config);
2998 }
2999 }
3000
3001 FcRuleSet *
3002 FcRuleSetCreate (const FcChar8 *name)
3003 {
3004 FcRuleSet *ret = (FcRuleSet *)malloc (sizeof (FcRuleSet));
3005 FcMatchKind k;
3006 const FcChar8 *p;
3007
3008 if (!name)
3009 p = (const FcChar8 *)"";
3010 else
3011 p = name;
3012
3013 if (ret) {
3014 ret->name = FcStrdup (p);
3015 ret->description = NULL;
3016 ret->domain = NULL;
3017 for (k = FcMatchKindBegin; k < FcMatchKindEnd; k++)
3018 ret->subst[k] = FcPtrListCreate (FcDestroyAsRule);
3019 FcRefInit (&ret->ref, 1);
3020 }
3021
3022 return ret;
3023 }
3024
3025 void
3026 FcRuleSetDestroy (FcRuleSet *rs)
3027 {
3028 FcMatchKind k;
3029
3030 if (!rs)
3031 return;
3032 if (FcRefDec (&rs->ref) != 1)
3033 return;
3034
3035 if (rs->name)
3036 FcStrFree (rs->name);
3037 if (rs->description)
3038 FcStrFree (rs->description);
3039 if (rs->domain)
3040 FcStrFree (rs->domain);
3041 for (k = FcMatchKindBegin; k < FcMatchKindEnd; k++)
3042 FcPtrListDestroy (rs->subst[k]);
3043
3044 free (rs);
3045 }
3046
3047 void
3048 FcRuleSetReference (FcRuleSet *rs)
3049 {
3050 if (!FcRefIsConst (&rs->ref))
3051 FcRefInc (&rs->ref);
3052 }
3053
3054 void
3055 FcRuleSetEnable (FcRuleSet *rs,
3056 FcBool flag)
3057 {
3058 if (rs) {
3059 rs->enabled = flag;
3060 /* XXX: we may want to provide a feature
3061 * to enable/disable rulesets through API
3062 * in the future?
3063 */
3064 }
3065 }
3066
3067 void
3068 FcRuleSetAddDescription (FcRuleSet *rs,
3069 const FcChar8 *domain,
3070 const FcChar8 *description)
3071 {
3072 if (rs->domain)
3073 FcStrFree (rs->domain);
3074 if (rs->description)
3075 FcStrFree (rs->description);
3076
3077 rs->domain = domain ? FcStrdup (domain) : NULL;
3078 rs->description = description ? FcStrdup (description) : NULL;
3079 }
3080
3081 int
3082 FcRuleSetAdd (FcRuleSet *rs,
3083 FcRule *rule,
3084 FcMatchKind kind)
3085 {
3086 FcPtrListIter iter;
3087 FcRule *r;
3088 int n = 0, ret;
3089
3090 if (!rs ||
3091 kind < FcMatchKindBegin || kind >= FcMatchKindEnd)
3092 return -1;
3093 FcPtrListIterInitAtLast (rs->subst[kind], &iter);
3094 if (!FcPtrListIterAdd (rs->subst[kind], &iter, rule))
3095 return -1;
3096
3097 for (r = rule; r; r = r->next) {
3098 switch (r->type) {
3099 case FcRuleTest:
3100 if (r->u.test) {
3101 if (r->u.test->kind == FcMatchDefault)
3102 r->u.test->kind = kind;
3103 if (n < r->u.test->object)
3104 n = r->u.test->object;
3105 }
3106 break;
3107 case FcRuleEdit:
3108 if (n < r->u.edit->object)
3109 n = r->u.edit->object;
3110 break;
3111 default:
3112 break;
3113 }
3114 }
3115 if (FcDebug() & FC_DBG_EDIT) {
3116 printf ("Add Rule(kind:%d, name: %s) ", kind, rs->name);
3117 FcRulePrint (rule);
3118 }
3119 ret = FC_OBJ_ID (n) - FC_MAX_BASE_OBJECT;
3120
3121 return ret < 0 ? 0 : ret;
3122 }
3123
3124 void
3125 FcConfigFileInfoIterInit (FcConfig *config,
3126 FcConfigFileInfoIter *iter)
3127 {
3128 FcConfig *c;
3129 FcPtrListIter *i = (FcPtrListIter *)iter;
3130
3131 if (!config)
3132 c = FcConfigGetCurrent();
3133 else
3134 c = config;
3135 FcPtrListIterInit (c->rulesetList, i);
3136 }
3137
3138 FcBool
3139 FcConfigFileInfoIterNext (FcConfig *config,
3140 FcConfigFileInfoIter *iter)
3141 {
3142 FcConfig *c;
3143 FcPtrListIter *i = (FcPtrListIter *)iter;
3144
3145 if (!config)
3146 c = FcConfigGetCurrent();
3147 else
3148 c = config;
3149 if (FcPtrListIterIsValid (c->rulesetList, i)) {
3150 FcPtrListIterNext (c->rulesetList, i);
3151 } else
3152 return FcFalse;
3153
3154 return FcTrue;
3155 }
3156
3157 FcBool
3158 FcConfigFileInfoIterGet (FcConfig *config,
3159 FcConfigFileInfoIter *iter,
3160 FcChar8 **name,
3161 FcChar8 **description,
3162 FcBool *enabled)
3163 {
3164 FcConfig *c;
3165 FcRuleSet *r;
3166 FcPtrListIter *i = (FcPtrListIter *)iter;
3167
3168 if (!config)
3169 c = FcConfigGetCurrent();
3170 else
3171 c = config;
3172 if (!FcPtrListIterIsValid (c->rulesetList, i))
3173 return FcFalse;
3174 r = FcPtrListIterGetValue (c->rulesetList, i);
3175 if (name)
3176 *name = FcStrdup (r->name && r->name[0] ? r->name : (const FcChar8 *)"fonts.conf");
3177 if (description)
3178 *description = FcStrdup (!r->description ? _ ("No description") : dgettext (r->domain ? (const char *)r->domain : GETTEXT_PACKAGE "-conf", (const char *)r->description));
3179 if (enabled)
3180 *enabled = r->enabled;
3181
3182 return FcTrue;
3183 }
3184
3185 #define __fccfg__
3186 #include "fcaliastail.h"
3187 #undef __fccfg__