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