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