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