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