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