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