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