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