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