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