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