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