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