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