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