]> git.wh0rd.org - fontconfig.git/blame - src/fccfg.c
Treat zh-hk fonts differently from zh-tw fonts. This patch may cause
[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 v = FcValueSave (v);
831 break;
832 case FcOpMatrix:
833 v.type = FcTypeMatrix;
4262e0b3 834 v.u.m = e->u.mval;
24330d27
KP
835 v = FcValueSave (v);
836 break;
837 case FcOpCharSet:
838 v.type = FcTypeCharSet;
4262e0b3 839 v.u.c = e->u.cval;
24330d27
KP
840 v = FcValueSave (v);
841 break;
842 case FcOpBool:
843 v.type = FcTypeBool;
844 v.u.b = e->u.bval;
845 break;
846 case FcOpField:
847 r = FcPatternGet (p, e->u.field, 0, &v);
848 if (r != FcResultMatch)
849 v.type = FcTypeVoid;
52ac91f7 850 v = FcValueSave (v);
24330d27
KP
851 break;
852 case FcOpConst:
853 if (FcNameConstant (e->u.constant, &v.u.i))
854 v.type = FcTypeInteger;
855 else
856 v.type = FcTypeVoid;
857 break;
858 case FcOpQuest:
859 vl = FcConfigEvaluate (p, e->u.tree.left);
860 if (vl.type == FcTypeBool)
861 {
862 if (vl.u.b)
863 v = FcConfigEvaluate (p, e->u.tree.right->u.tree.left);
864 else
865 v = FcConfigEvaluate (p, e->u.tree.right->u.tree.right);
866 }
867 else
868 v.type = FcTypeVoid;
869 FcValueDestroy (vl);
870 break;
47d4f950 871 case FcOpEqual:
24330d27
KP
872 case FcOpNotEqual:
873 case FcOpLess:
874 case FcOpLessEqual:
875 case FcOpMore:
876 case FcOpMoreEqual:
47d4f950
KP
877 case FcOpContains:
878 case FcOpNotContains:
74a623e0 879 case FcOpListing:
938bc633
KP
880 vl = FcConfigEvaluate (p, e->u.tree.left);
881 vr = FcConfigEvaluate (p, e->u.tree.right);
882 v.type = FcTypeBool;
4262e0b3 883 v.u.b = FcConfigCompareValue (&vl, e->op, &vr);
938bc633
KP
884 FcValueDestroy (vl);
885 FcValueDestroy (vr);
886 break;
887 case FcOpOr:
888 case FcOpAnd:
24330d27
KP
889 case FcOpPlus:
890 case FcOpMinus:
891 case FcOpTimes:
892 case FcOpDivide:
893 vl = FcConfigEvaluate (p, e->u.tree.left);
894 vr = FcConfigEvaluate (p, e->u.tree.right);
895 vl = FcConfigPromote (vl, vr);
896 vr = FcConfigPromote (vr, vl);
897 if (vl.type == vr.type)
898 {
899 switch (vl.type) {
900 case FcTypeDouble:
901 switch (e->op) {
902 case FcOpPlus:
903 v.type = FcTypeDouble;
904 v.u.d = vl.u.d + vr.u.d;
905 break;
906 case FcOpMinus:
907 v.type = FcTypeDouble;
908 v.u.d = vl.u.d - vr.u.d;
909 break;
910 case FcOpTimes:
911 v.type = FcTypeDouble;
912 v.u.d = vl.u.d * vr.u.d;
913 break;
914 case FcOpDivide:
915 v.type = FcTypeDouble;
916 v.u.d = vl.u.d / vr.u.d;
917 break;
24330d27
KP
918 default:
919 v.type = FcTypeVoid;
920 break;
921 }
922 if (v.type == FcTypeDouble &&
923 v.u.d == (double) (int) v.u.d)
924 {
925 v.type = FcTypeInteger;
926 v.u.i = (int) v.u.d;
927 }
928 break;
929 case FcTypeBool:
930 switch (e->op) {
931 case FcOpOr:
932 v.type = FcTypeBool;
933 v.u.b = vl.u.b || vr.u.b;
934 break;
935 case FcOpAnd:
936 v.type = FcTypeBool;
937 v.u.b = vl.u.b && vr.u.b;
938 break;
24330d27
KP
939 default:
940 v.type = FcTypeVoid;
941 break;
942 }
943 break;
944 case FcTypeString:
945 switch (e->op) {
24330d27
KP
946 case FcOpPlus:
947 v.type = FcTypeString;
7f37423d 948 v.u.s = FcStrStaticName (FcStrPlus (vl.u.s, vr.u.s));
cd2ec1a9 949
4262e0b3 950 if (!v.u.s)
24330d27
KP
951 v.type = FcTypeVoid;
952 break;
953 default:
954 v.type = FcTypeVoid;
955 break;
956 }
8c96d1fc 957 break;
24330d27
KP
958 case FcTypeMatrix:
959 switch (e->op) {
24330d27
KP
960 case FcOpTimes:
961 v.type = FcTypeMatrix;
962 m = malloc (sizeof (FcMatrix));
963 if (m)
964 {
965 FcMemAlloc (FC_MEM_MATRIX, sizeof (FcMatrix));
4262e0b3
PL
966 FcMatrixMultiply (m, vl.u.m, vr.u.m);
967 v.u.m = m;
24330d27
KP
968 }
969 else
970 {
971 v.type = FcTypeVoid;
972 }
973 break;
974 default:
975 v.type = FcTypeVoid;
976 break;
977 }
978 break;
24330d27
KP
979 default:
980 v.type = FcTypeVoid;
981 break;
982 }
983 }
984 else
985 v.type = FcTypeVoid;
986 FcValueDestroy (vl);
987 FcValueDestroy (vr);
988 break;
989 case FcOpNot:
990 vl = FcConfigEvaluate (p, e->u.tree.left);
991 switch (vl.type) {
992 case FcTypeBool:
993 v.type = FcTypeBool;
994 v.u.b = !vl.u.b;
995 break;
996 default:
997 v.type = FcTypeVoid;
998 break;
999 }
1000 FcValueDestroy (vl);
1001 break;
3f7653c2
KP
1002 case FcOpFloor:
1003 vl = FcConfigEvaluate (p, e->u.tree.left);
1004 switch (vl.type) {
1005 case FcTypeInteger:
1006 v = vl;
1007 break;
1008 case FcTypeDouble:
1009 v.type = FcTypeInteger;
1010 v.u.i = FcDoubleFloor (vl.u.d);
1011 break;
1012 default:
1013 v.type = FcTypeVoid;
1014 break;
1015 }
1016 FcValueDestroy (vl);
1017 break;
1018 case FcOpCeil:
1019 vl = FcConfigEvaluate (p, e->u.tree.left);
1020 switch (vl.type) {
1021 case FcTypeInteger:
1022 v = vl;
1023 break;
1024 case FcTypeDouble:
1025 v.type = FcTypeInteger;
1026 v.u.i = FcDoubleCeil (vl.u.d);
1027 break;
1028 default:
1029 v.type = FcTypeVoid;
1030 break;
1031 }
1032 FcValueDestroy (vl);
1033 break;
1034 case FcOpRound:
1035 vl = FcConfigEvaluate (p, e->u.tree.left);
1036 switch (vl.type) {
1037 case FcTypeInteger:
1038 v = vl;
1039 break;
1040 case FcTypeDouble:
1041 v.type = FcTypeInteger;
1042 v.u.i = FcDoubleRound (vl.u.d);
1043 break;
1044 default:
1045 v.type = FcTypeVoid;
1046 break;
1047 }
1048 FcValueDestroy (vl);
1049 break;
1050 case FcOpTrunc:
1051 vl = FcConfigEvaluate (p, e->u.tree.left);
1052 switch (vl.type) {
1053 case FcTypeInteger:
1054 v = vl;
1055 break;
1056 case FcTypeDouble:
1057 v.type = FcTypeInteger;
1058 v.u.i = FcDoubleTrunc (vl.u.d);
1059 break;
1060 default:
1061 v.type = FcTypeVoid;
1062 break;
1063 }
1064 FcValueDestroy (vl);
1065 break;
24330d27
KP
1066 default:
1067 v.type = FcTypeVoid;
1068 break;
1069 }
1070 return v;
1071}
1072
1073static FcValueList *
1074FcConfigMatchValueList (FcPattern *p,
1075 FcTest *t,
f534109f 1076 FcValueList *values)
24330d27 1077{
179c3995
KP
1078 FcValueList *ret = 0;
1079 FcExpr *e = t->expr;
1080 FcValue value;
f534109f 1081 FcValueList *v;
24330d27 1082
179c3995 1083 while (e)
24330d27 1084 {
74a623e0 1085 /* Compute the value of the match expression */
179c3995 1086 if (e->op == FcOpComma)
24330d27 1087 {
179c3995
KP
1088 value = FcConfigEvaluate (p, e->u.tree.left);
1089 e = e->u.tree.right;
24330d27
KP
1090 }
1091 else
1092 {
179c3995
KP
1093 value = FcConfigEvaluate (p, e);
1094 e = 0;
1095 }
1096
cd2ec1a9 1097 for (v = values; v; v = FcValueListPtrU(v->next))
179c3995 1098 {
74a623e0 1099 /* Compare the pattern value to the match expression value */
4262e0b3 1100 if (FcConfigCompareValue (&v->value, t->op, &value))
24330d27 1101 {
179c3995
KP
1102 if (!ret)
1103 ret = v;
1104 }
1105 else
1106 {
1107 if (t->qual == FcQualAll)
1108 {
1109 ret = 0;
1110 break;
1111 }
24330d27
KP
1112 }
1113 }
d0f07b8d 1114 FcValueDestroy (value);
24330d27 1115 }
24330d27
KP
1116 return ret;
1117}
1118
1119static FcValueList *
6fff2cda 1120FcConfigValues (FcPattern *p, FcExpr *e, FcValueBinding binding)
24330d27
KP
1121{
1122 FcValueList *l;
1123
1124 if (!e)
1125 return 0;
1126 l = (FcValueList *) malloc (sizeof (FcValueList));
1127 if (!l)
1128 return 0;
1129 FcMemAlloc (FC_MEM_VALLIST, sizeof (FcValueList));
1130 if (e->op == FcOpComma)
1131 {
1132 l->value = FcConfigEvaluate (p, e->u.tree.left);
cd2ec1a9 1133 l->next = FcValueListPtrCreateDynamic(FcConfigValues (p, e->u.tree.right, binding));
24330d27
KP
1134 }
1135 else
1136 {
1137 l->value = FcConfigEvaluate (p, e);
cd2ec1a9 1138 l->next = FcValueListPtrCreateDynamic(0);
24330d27 1139 }
6fff2cda 1140 l->binding = binding;
82912b06 1141 if (l->value.type == FcTypeVoid)
24330d27 1142 {
82912b06 1143 FcValueList *next = FcValueListPtrU(l->next);
cd2ec1a9 1144
82912b06
PL
1145 FcMemFree (FC_MEM_VALLIST, sizeof (FcValueList));
1146 free (l);
1147 l = next;
24330d27 1148 }
82912b06 1149
24330d27
KP
1150 return l;
1151}
1152
1153static FcBool
cd2ec1a9 1154FcConfigAdd (FcValueListPtr *head,
24330d27
KP
1155 FcValueList *position,
1156 FcBool append,
1157 FcValueList *new)
1158{
cd2ec1a9 1159 FcValueListPtr *prev, last, v;
dda7794f 1160 FcValueBinding sameBinding;
24330d27 1161
dda7794f
KP
1162 if (position)
1163 sameBinding = position->binding;
1164 else
1165 sameBinding = FcValueBindingWeak;
cd2ec1a9
PL
1166 for (v = FcValueListPtrCreateDynamic(new); FcValueListPtrU(v);
1167 v = FcValueListPtrU(v)->next)
1168 if (FcValueListPtrU(v)->binding == FcValueBindingSame)
1169 FcValueListPtrU(v)->binding = sameBinding;
24330d27
KP
1170 if (append)
1171 {
1172 if (position)
1173 prev = &position->next;
1174 else
cd2ec1a9
PL
1175 for (prev = head; FcValueListPtrU(*prev);
1176 prev = &(FcValueListPtrU(*prev)->next))
24330d27
KP
1177 ;
1178 }
1179 else
1180 {
1181 if (position)
1182 {
cd2ec1a9
PL
1183 for (prev = head; FcValueListPtrU(*prev);
1184 prev = &(FcValueListPtrU(*prev)->next))
24330d27 1185 {
cd2ec1a9 1186 if (FcValueListPtrU(*prev) == position)
24330d27
KP
1187 break;
1188 }
1189 }
1190 else
1191 prev = head;
1192
1193 if (FcDebug () & FC_DBG_EDIT)
1194 {
cd2ec1a9 1195 if (!FcValueListPtrU(*prev))
24330d27
KP
1196 printf ("position not on list\n");
1197 }
1198 }
1199
1200 if (FcDebug () & FC_DBG_EDIT)
1201 {
1202 printf ("%s list before ", append ? "Append" : "Prepend");
1203 FcValueListPrint (*head);
1204 printf ("\n");
1205 }
1206
1207 if (new)
1208 {
cd2ec1a9
PL
1209 last = FcValueListPtrCreateDynamic(new);
1210 while (FcValueListPtrU(FcValueListPtrU(last)->next))
1211 last = FcValueListPtrU(last)->next;
24330d27 1212
cd2ec1a9
PL
1213 FcValueListPtrU(last)->next = *prev;
1214 *prev = FcValueListPtrCreateDynamic(new);
24330d27
KP
1215 }
1216
1217 if (FcDebug () & FC_DBG_EDIT)
1218 {
1219 printf ("%s list after ", append ? "Append" : "Prepend");
1220 FcValueListPrint (*head);
1221 printf ("\n");
1222 }
1223
1224 return FcTrue;
1225}
1226
1227static void
cd2ec1a9 1228FcConfigDel (FcValueListPtr *head,
24330d27
KP
1229 FcValueList *position)
1230{
cd2ec1a9 1231 FcValueListPtr *prev;
24330d27 1232
cd2ec1a9
PL
1233 for (prev = head; FcValueListPtrU(*prev);
1234 prev = &(FcValueListPtrU(*prev)->next))
24330d27 1235 {
cd2ec1a9 1236 if (FcValueListPtrU(*prev) == position)
24330d27
KP
1237 {
1238 *prev = position->next;
cd2ec1a9
PL
1239 position->next = FcValueListPtrCreateDynamic(0);
1240 FcValueListDestroy (FcValueListPtrCreateDynamic(position));
24330d27
KP
1241 break;
1242 }
1243 }
1244}
1245
1246static void
1247FcConfigPatternAdd (FcPattern *p,
1248 const char *object,
1249 FcValueList *list,
1250 FcBool append)
1251{
1252 if (list)
1253 {
e9be9cd1 1254 FcPatternElt *e = FcPatternInsertElt (p, object);
24330d27
KP
1255
1256 if (!e)
1257 return;
1258 FcConfigAdd (&e->values, 0, append, list);
1259 }
1260}
1261
1262/*
1263 * Delete all values associated with a field
1264 */
1265static void
1266FcConfigPatternDel (FcPattern *p,
1267 const char *object)
1268{
e9be9cd1 1269 FcPatternElt *e = FcPatternFindElt (p, object);
24330d27
KP
1270 if (!e)
1271 return;
cd2ec1a9
PL
1272 while (FcValueListPtrU(e->values))
1273 FcConfigDel (&e->values, FcValueListPtrU(e->values));
24330d27
KP
1274}
1275
1276static void
1277FcConfigPatternCanon (FcPattern *p,
1278 const char *object)
1279{
e9be9cd1 1280 FcPatternElt *e = FcPatternFindElt (p, object);
24330d27
KP
1281 if (!e)
1282 return;
cd2ec1a9 1283 if (!FcValueListPtrU(e->values))
24330d27
KP
1284 FcPatternDel (p, object);
1285}
1286
1287FcBool
fa244f3d
KP
1288FcConfigSubstituteWithPat (FcConfig *config,
1289 FcPattern *p,
1290 FcPattern *p_pat,
1291 FcMatchKind kind)
24330d27
KP
1292{
1293 FcSubst *s;
1294 FcSubState *st;
1295 int i;
1296 FcTest *t;
1297 FcEdit *e;
1298 FcValueList *l;
938bc633 1299 FcPattern *m;
24330d27
KP
1300
1301 if (!config)
1302 {
1303 config = FcConfigGetCurrent ();
1304 if (!config)
1305 return FcFalse;
1306 }
1307
1308 st = (FcSubState *) malloc (config->maxObjects * sizeof (FcSubState));
1309 if (!st && config->maxObjects)
1310 return FcFalse;
1311 FcMemAlloc (FC_MEM_SUBSTATE, config->maxObjects * sizeof (FcSubState));
1312
1313 if (FcDebug () & FC_DBG_EDIT)
1314 {
1315 printf ("FcConfigSubstitute ");
1316 FcPatternPrint (p);
1317 }
1318 if (kind == FcMatchPattern)
1319 s = config->substPattern;
1320 else
1321 s = config->substFont;
1322 for (; s; s = s->next)
1323 {
1324 /*
1325 * Check the tests to see if
1326 * they all match the pattern
1327 */
1328 for (t = s->test, i = 0; t; t = t->next, i++)
1329 {
1330 if (FcDebug () & FC_DBG_EDIT)
1331 {
1332 printf ("FcConfigSubstitute test ");
1333 FcTestPrint (t);
1334 }
938bc633
KP
1335 st[i].elt = 0;
1336 if (kind == FcMatchFont && t->kind == FcMatchPattern)
1337 m = p_pat;
1338 else
1339 m = p;
1340 if (m)
1341 st[i].elt = FcPatternFindElt (m, t->field);
1342 else
1343 st[i].elt = 0;
24330d27
KP
1344 /*
1345 * If there's no such field in the font,
1346 * then FcQualAll matches while FcQualAny does not
1347 */
1348 if (!st[i].elt)
1349 {
1350 if (t->qual == FcQualAll)
1351 {
1352 st[i].value = 0;
1353 continue;
1354 }
1355 else
1356 break;
1357 }
1358 /*
1359 * Check to see if there is a match, mark the location
1360 * to apply match-relative edits
1361 */
cd2ec1a9 1362 st[i].value = FcConfigMatchValueList (m, t, FcValueListPtrU(st[i].elt->values));
24330d27
KP
1363 if (!st[i].value)
1364 break;
cd2ec1a9 1365 if (t->qual == FcQualFirst && st[i].value != FcValueListPtrU(st[i].elt->values))
6f6563ed 1366 break;
cd2ec1a9 1367 if (t->qual == FcQualNotFirst && st[i].value == FcValueListPtrU(st[i].elt->values))
6f6563ed 1368 break;
24330d27
KP
1369 }
1370 if (t)
1371 {
1372 if (FcDebug () & FC_DBG_EDIT)
1373 printf ("No match\n");
1374 continue;
1375 }
1376 if (FcDebug () & FC_DBG_EDIT)
1377 {
1378 printf ("Substitute ");
1379 FcSubstPrint (s);
1380 }
1381 for (e = s->edit; e; e = e->next)
1382 {
1383 /*
1384 * Evaluate the list of expressions
1385 */
6fff2cda 1386 l = FcConfigValues (p, e->expr, e->binding);
24330d27 1387 /*
938bc633
KP
1388 * Locate any test associated with this field, skipping
1389 * tests associated with the pattern when substituting in
1390 * the font
24330d27
KP
1391 */
1392 for (t = s->test, i = 0; t; t = t->next, i++)
938bc633
KP
1393 {
1394 if ((t->kind == FcMatchFont || kind == FcMatchPattern) &&
1395 !FcStrCmpIgnoreCase ((FcChar8 *) t->field,
1396 (FcChar8 *) e->field))
432913ea 1397 {
5f84b65a
KP
1398 /*
1399 * KLUDGE - the pattern may have been reallocated or
1400 * things may have been inserted or deleted above
1401 * this element by other edits. Go back and find
1402 * the element again
1403 */
1404 if (e != s->edit && st[i].elt)
1405 st[i].elt = FcPatternFindElt (p, t->field);
432913ea
DD
1406 if (!st[i].elt)
1407 t = 0;
24330d27 1408 break;
432913ea 1409 }
938bc633 1410 }
24330d27
KP
1411 switch (e->op) {
1412 case FcOpAssign:
1413 /*
1414 * If there was a test, then replace the matched
1415 * value with the new list of values
1416 */
e9be9cd1 1417 if (t)
24330d27
KP
1418 {
1419 FcValueList *thisValue = st[i].value;
cd2ec1a9 1420 FcValueList *nextValue = thisValue ? FcValueListPtrU(thisValue->next) : 0;
24330d27
KP
1421
1422 /*
1423 * Append the new list of values after the current value
1424 */
1425 FcConfigAdd (&st[i].elt->values, thisValue, FcTrue, l);
ccb3e93b
KP
1426 /*
1427 * Delete the marked value
1428 */
1429 FcConfigDel (&st[i].elt->values, thisValue);
24330d27
KP
1430 /*
1431 * Adjust any pointers into the value list to ensure
1432 * future edits occur at the same place
1433 */
1434 for (t = s->test, i = 0; t; t = t->next, i++)
1435 {
1436 if (st[i].value == thisValue)
1437 st[i].value = nextValue;
1438 }
24330d27
KP
1439 break;
1440 }
1441 /* fall through ... */
1442 case FcOpAssignReplace:
1443 /*
1444 * Delete all of the values and insert
1445 * the new set
1446 */
1447 FcConfigPatternDel (p, e->field);
1448 FcConfigPatternAdd (p, e->field, l, FcTrue);
1449 /*
1450 * Adjust any pointers into the value list as they no
1451 * longer point to anything valid
1452 */
1453 if (t)
1454 {
1455 FcPatternElt *thisElt = st[i].elt;
1456 for (t = s->test, i = 0; t; t = t->next, i++)
1457 {
1458 if (st[i].elt == thisElt)
1459 st[i].value = 0;
1460 }
1461 }
1462 break;
1463 case FcOpPrepend:
1464 if (t)
1465 {
1466 FcConfigAdd (&st[i].elt->values, st[i].value, FcFalse, l);
1467 break;
1468 }
1469 /* fall through ... */
1470 case FcOpPrependFirst:
1471 FcConfigPatternAdd (p, e->field, l, FcFalse);
1472 break;
1473 case FcOpAppend:
1474 if (t)
1475 {
1476 FcConfigAdd (&st[i].elt->values, st[i].value, FcTrue, l);
1477 break;
1478 }
1479 /* fall through ... */
1480 case FcOpAppendLast:
1481 FcConfigPatternAdd (p, e->field, l, FcTrue);
1482 break;
1483 default:
1484 break;
1485 }
1486 }
1487 /*
1488 * Now go through the pattern and eliminate
1489 * any properties without data
1490 */
1491 for (e = s->edit; e; e = e->next)
1492 FcConfigPatternCanon (p, e->field);
1493
1494 if (FcDebug () & FC_DBG_EDIT)
1495 {
1496 printf ("FcConfigSubstitute edit");
1497 FcPatternPrint (p);
1498 }
1499 }
1500 FcMemFree (FC_MEM_SUBSTATE, config->maxObjects * sizeof (FcSubState));
1501 free (st);
1502 if (FcDebug () & FC_DBG_EDIT)
1503 {
1504 printf ("FcConfigSubstitute done");
1505 FcPatternPrint (p);
1506 }
1507 return FcTrue;
1508}
1509
fa244f3d
KP
1510FcBool
1511FcConfigSubstitute (FcConfig *config,
1512 FcPattern *p,
1513 FcMatchKind kind)
1514{
1515 return FcConfigSubstituteWithPat (config, p, 0, kind);
1516}
1517
e5206dbc 1518#if defined (_WIN32) && (defined (PIC) || defined (DLL_EXPORT))
daeed6e0
TL
1519
1520static FcChar8 fontconfig_path[1000] = "";
1521
1522BOOL WINAPI
1523DllMain (HINSTANCE hinstDLL,
1524 DWORD fdwReason,
1525 LPVOID lpvReserved)
1526{
1527 FcChar8 *p;
1528
1529 switch (fdwReason) {
1530 case DLL_PROCESS_ATTACH:
1531 if (!GetModuleFileName ((HMODULE) hinstDLL, fontconfig_path,
1532 sizeof (fontconfig_path)))
1533 break;
1534
1535 /* If the fontconfig DLL is in a "bin" or "lib" subfolder,
1536 * assume it's a Unix-style installation tree, and use
1537 * "etc/fonts" in there as FONTCONFIG_PATH. Otherwise use the
1538 * folder where the DLL is as FONTCONFIG_PATH.
1539 */
1540 p = strrchr (fontconfig_path, '\\');
1541 if (p)
1542 {
1543 *p = '\0';
1544 p = strrchr (fontconfig_path, '\\');
1545 if (p && (FcStrCmpIgnoreCase (p + 1, "bin") == 0 ||
1546 FcStrCmpIgnoreCase (p + 1, "lib") == 0))
1547 *p = '\0';
1548 strcat (fontconfig_path, "\\etc\\fonts");
1549 }
1550 else
1551 fontconfig_path[0] = '\0';
1552
1553 break;
1554 }
1555
1556 return TRUE;
1557}
1558
1559#undef FONTCONFIG_PATH
1560#define FONTCONFIG_PATH fontconfig_path
1561
1562#else /* !(_WIN32 && PIC) */
1563
daeed6e0
TL
1564#endif /* !(_WIN32 && PIC) */
1565
24330d27
KP
1566#ifndef FONTCONFIG_FILE
1567#define FONTCONFIG_FILE "fonts.conf"
1568#endif
1569
ccb3e93b
KP
1570static FcChar8 *
1571FcConfigFileExists (const FcChar8 *dir, const FcChar8 *file)
24330d27 1572{
ccb3e93b 1573 FcChar8 *path;
24330d27
KP
1574
1575 if (!dir)
ccb3e93b
KP
1576 dir = (FcChar8 *) "";
1577 path = malloc (strlen ((char *) dir) + 1 + strlen ((char *) file) + 1);
24330d27
KP
1578 if (!path)
1579 return 0;
1580
9c8e07f1 1581 strcpy ((char *) path, (const char *) dir);
daeed6e0
TL
1582 /* make sure there's a single separator */
1583#ifdef _WIN32
1584 if ((!path[0] || (path[strlen((char *) path)-1] != '/' &&
1585 path[strlen((char *) path)-1] != '\\')) &&
79da4fe9
TL
1586 !(file[0] == '/' ||
1587 file[0] == '\\' ||
1588 (isalpha (file[0]) && file[1] == ':' && (file[2] == '/' || file[2] == '\\'))))
daeed6e0
TL
1589 strcat ((char *) path, "\\");
1590#else
ccb3e93b
KP
1591 if ((!path[0] || path[strlen((char *) path)-1] != '/') && file[0] != '/')
1592 strcat ((char *) path, "/");
daeed6e0 1593#endif
ccb3e93b 1594 strcat ((char *) path, (char *) file);
24330d27 1595
9dac3c59 1596 FcMemAlloc (FC_MEM_STRING, strlen ((char *) path) + 1);
ccb3e93b 1597 if (access ((char *) path, R_OK) == 0)
24330d27
KP
1598 return path;
1599
9dac3c59 1600 FcStrFree (path);
24330d27
KP
1601 return 0;
1602}
1603
ccb3e93b 1604static FcChar8 **
24330d27
KP
1605FcConfigGetPath (void)
1606{
ccb3e93b
KP
1607 FcChar8 **path;
1608 FcChar8 *env, *e, *colon;
1609 FcChar8 *dir;
24330d27
KP
1610 int npath;
1611 int i;
1612
1613 npath = 2; /* default dir + null */
ccb3e93b 1614 env = (FcChar8 *) getenv ("FONTCONFIG_PATH");
24330d27
KP
1615 if (env)
1616 {
1617 e = env;
1618 npath++;
1619 while (*e)
daeed6e0 1620 if (*e++ == FC_SEARCH_PATH_SEPARATOR)
24330d27
KP
1621 npath++;
1622 }
ccb3e93b 1623 path = calloc (npath, sizeof (FcChar8 *));
24330d27
KP
1624 if (!path)
1625 goto bail0;
1626 i = 0;
1627
1628 if (env)
1629 {
1630 e = env;
1631 while (*e)
1632 {
daeed6e0 1633 colon = (FcChar8 *) strchr ((char *) e, FC_SEARCH_PATH_SEPARATOR);
24330d27 1634 if (!colon)
ccb3e93b 1635 colon = e + strlen ((char *) e);
24330d27
KP
1636 path[i] = malloc (colon - e + 1);
1637 if (!path[i])
1638 goto bail1;
9c8e07f1 1639 strncpy ((char *) path[i], (const char *) e, colon - e);
24330d27
KP
1640 path[i][colon - e] = '\0';
1641 if (*colon)
1642 e = colon + 1;
1643 else
1644 e = colon;
1645 i++;
1646 }
1647 }
1648
ccb3e93b
KP
1649 dir = (FcChar8 *) FONTCONFIG_PATH;
1650 path[i] = malloc (strlen ((char *) dir) + 1);
24330d27
KP
1651 if (!path[i])
1652 goto bail1;
9c8e07f1 1653 strcpy ((char *) path[i], (const char *) dir);
24330d27
KP
1654 return path;
1655
1656bail1:
1657 for (i = 0; path[i]; i++)
1658 free (path[i]);
1659 free (path);
1660bail0:
1661 return 0;
1662}
1663
1664static void
ccb3e93b 1665FcConfigFreePath (FcChar8 **path)
24330d27 1666{
ccb3e93b 1667 FcChar8 **p;
24330d27
KP
1668
1669 for (p = path; *p; p++)
1670 free (*p);
1671 free (path);
1672}
1673
ff3f1f98
KP
1674static FcBool _FcConfigHomeEnabled = FcTrue;
1675
1676FcChar8 *
1677FcConfigHome (void)
1678{
1679 if (_FcConfigHomeEnabled)
daeed6e0
TL
1680 {
1681 char *home = getenv ("HOME");
1682
1683#ifdef _WIN32
1684 if (home == NULL)
1685 home = getenv ("USERPROFILE");
1686#endif
1687
8245771d 1688 return (FcChar8 *) home;
daeed6e0 1689 }
ff3f1f98
KP
1690 return 0;
1691}
1692
1693FcBool
1694FcConfigEnableHome (FcBool enable)
1695{
1696 FcBool prev = _FcConfigHomeEnabled;
1697 _FcConfigHomeEnabled = enable;
1698 return prev;
1699}
1700
ccb3e93b
KP
1701FcChar8 *
1702FcConfigFilename (const FcChar8 *url)
24330d27 1703{
ccb3e93b 1704 FcChar8 *file, *dir, **path, **p;
24330d27
KP
1705
1706 if (!url || !*url)
1707 {
ccb3e93b 1708 url = (FcChar8 *) getenv ("FONTCONFIG_FILE");
24330d27 1709 if (!url)
ccb3e93b 1710 url = (FcChar8 *) FONTCONFIG_FILE;
24330d27 1711 }
ccb3e93b 1712 file = 0;
daeed6e0
TL
1713
1714#ifdef _WIN32
1715 if (isalpha (*url) &&
1716 url[1] == ':' &&
1717 (url[2] == '/' || url[2] == '\\'))
1718 goto absolute_path;
1719#endif
1720
24330d27
KP
1721 switch (*url) {
1722 case '~':
ff3f1f98 1723 dir = FcConfigHome ();
24330d27
KP
1724 if (dir)
1725 file = FcConfigFileExists (dir, url + 1);
1726 else
1727 file = 0;
1728 break;
daeed6e0
TL
1729#ifdef _WIN32
1730 case '\\':
1731 absolute_path:
1732#endif
24330d27
KP
1733 case '/':
1734 file = FcConfigFileExists (0, url);
1735 break;
1736 default:
1737 path = FcConfigGetPath ();
1738 if (!path)
1739 return 0;
1740 for (p = path; *p; p++)
1741 {
1742 file = FcConfigFileExists (*p, url);
1743 if (file)
1744 break;
1745 }
1746 FcConfigFreePath (path);
1747 break;
1748 }
1749 return file;
1750}
1751
1752/*
1753 * Manage the application-specific fonts
1754 */
1755
1756FcBool
1757FcConfigAppFontAddFile (FcConfig *config,
ccb3e93b 1758 const FcChar8 *file)
24330d27
KP
1759{
1760 FcFontSet *set;
179c3995
KP
1761 FcStrSet *subdirs;
1762 FcStrList *sublist;
1763 FcChar8 *subdir;
24330d27
KP
1764
1765 if (!config)
1766 {
1767 config = FcConfigGetCurrent ();
1768 if (!config)
1769 return FcFalse;
1770 }
1771
179c3995
KP
1772 subdirs = FcStrSetCreate ();
1773 if (!subdirs)
1774 return FcFalse;
1775
24330d27
KP
1776 set = FcConfigGetFonts (config, FcSetApplication);
1777 if (!set)
1778 {
1779 set = FcFontSetCreate ();
1780 if (!set)
179c3995
KP
1781 {
1782 FcStrSetDestroy (subdirs);
24330d27 1783 return FcFalse;
179c3995 1784 }
24330d27
KP
1785 FcConfigSetFonts (config, set, FcSetApplication);
1786 }
179c3995 1787
d47c9d6e 1788 if (!FcFileScanConfig (set, subdirs, 0, config->blanks, file, FcFalse, config))
179c3995
KP
1789 {
1790 FcStrSetDestroy (subdirs);
1791 return FcFalse;
1792 }
1793 if ((sublist = FcStrListCreate (subdirs)))
1794 {
1795 while ((subdir = FcStrListNext (sublist)))
1796 {
1797 FcConfigAppFontAddDir (config, subdir);
1798 }
1799 FcStrListDone (sublist);
1800 }
1801 return FcTrue;
24330d27
KP
1802}
1803
1804FcBool
1805FcConfigAppFontAddDir (FcConfig *config,
ccb3e93b 1806 const FcChar8 *dir)
24330d27
KP
1807{
1808 FcFontSet *set;
179c3995
KP
1809 FcStrSet *subdirs;
1810 FcStrList *sublist;
1811 FcChar8 *subdir;
24330d27
KP
1812
1813 if (!config)
1814 {
1815 config = FcConfigGetCurrent ();
1816 if (!config)
1817 return FcFalse;
1818 }
179c3995
KP
1819 subdirs = FcStrSetCreate ();
1820 if (!subdirs)
1821 return FcFalse;
1822
24330d27
KP
1823 set = FcConfigGetFonts (config, FcSetApplication);
1824 if (!set)
1825 {
1826 set = FcFontSetCreate ();
1827 if (!set)
179c3995
KP
1828 {
1829 FcStrSetDestroy (subdirs);
24330d27 1830 return FcFalse;
179c3995 1831 }
24330d27
KP
1832 FcConfigSetFonts (config, set, FcSetApplication);
1833 }
179c3995 1834
d47c9d6e 1835 if (!FcDirScanConfig (set, subdirs, 0, config->blanks, dir, FcFalse, config))
179c3995
KP
1836 {
1837 FcStrSetDestroy (subdirs);
1838 return FcFalse;
1839 }
1840 if ((sublist = FcStrListCreate (subdirs)))
1841 {
1842 while ((subdir = FcStrListNext (sublist)))
1843 {
1844 FcConfigAppFontAddDir (config, subdir);
1845 }
1846 FcStrListDone (sublist);
1847 }
1848 return FcTrue;
24330d27
KP
1849}
1850
1851void
1852FcConfigAppFontClear (FcConfig *config)
1853{
3ef32bcd
MS
1854 if (!config)
1855 {
1856 config = FcConfigGetCurrent ();
1857 if (!config)
1858 return;
1859 }
1860
24330d27
KP
1861 FcConfigSetFonts (config, 0, FcSetApplication);
1862}
d47c9d6e
KP
1863
1864/*
1865 * Manage filename-based font source selectors
1866 */
1867
1868FcBool
1869FcConfigGlobAdd (FcConfig *config,
1870 const FcChar8 *glob,
1871 FcBool accept)
1872{
1873 FcStrSet *set = accept ? config->acceptGlobs : config->rejectGlobs;
1874
1875 return FcStrSetAdd (set, glob);
1876}
1877
1878static FcBool
1879FcConfigGlobMatch (const FcChar8 *glob,
1880 const FcChar8 *string)
1881{
1882 FcChar8 c;
1883
1884 while ((c = *glob++))
1885 {
1886 switch (c) {
1887 case '*':
1888 /* short circuit common case */
1889 if (!*glob)
1890 return FcTrue;
1891 /* short circuit another common case */
1892 if (strchr ((char *) glob, '*') == 0)
1893 string += strlen ((char *) string) - strlen ((char *) glob);
1894 while (*string)
1895 {
1896 if (FcConfigGlobMatch (glob, string))
1897 return FcTrue;
1898 string++;
1899 }
1900 return FcFalse;
1901 case '?':
1902 if (*string++ == '\0')
1903 return FcFalse;
1904 break;
1905 default:
1906 if (*string++ != c)
1907 return FcFalse;
1908 break;
1909 }
1910 }
1911 return *string == '\0';
1912}
1913
1914static FcBool
1915FcConfigGlobsMatch (const FcStrSet *globs,
1916 const FcChar8 *string)
1917{
1918 int i;
1919
1920 for (i = 0; i < globs->num; i++)
4262e0b3 1921 if (FcConfigGlobMatch (globs->strs[i], string))
d47c9d6e
KP
1922 return FcTrue;
1923 return FcFalse;
1924}
1925
1926FcBool
1927FcConfigAcceptFilename (FcConfig *config,
1928 const FcChar8 *filename)
1929{
1930 if (FcConfigGlobsMatch (config->acceptGlobs, filename))
1931 return FcTrue;
1932 if (FcConfigGlobsMatch (config->rejectGlobs, filename))
1933 return FcFalse;
1934 return FcTrue;
1935}
4f27c1c0
KP
1936
1937/*
1938 * Manage font-pattern based font source selectors
1939 */
1940
1941FcBool
1942FcConfigPatternsAdd (FcConfig *config,
1943 FcPattern *pattern,
1944 FcBool accept)
1945{
1946 FcFontSet *set = accept ? config->acceptPatterns : config->rejectPatterns;
1947
1948 return FcFontSetAdd (set, pattern);
1949}
1950
1951static FcBool
1952FcConfigPatternsMatch (const FcFontSet *patterns,
1953 const FcPattern *font)
1954{
1955 int i;
1956
1957 for (i = 0; i < patterns->nfont; i++)
1958 if (FcListPatternMatchAny (patterns->fonts[i], font))
1959 return FcTrue;
1960 return FcFalse;
1961}
1962
1963FcBool
1964FcConfigAcceptFont (FcConfig *config,
1965 const FcPattern *font)
1966{
1967 if (FcConfigPatternsMatch (config->acceptPatterns, font))
1968 return FcTrue;
1969 if (FcConfigPatternsMatch (config->rejectPatterns, font))
1970 return FcFalse;
1971 return FcTrue;
1972}