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