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