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