fccfg.c 41.7 KB
Newer Older
Keith Packard's avatar
Keith Packard committed
1
/*
2
 * $RCSId: xc/lib/fontconfig/src/fccfg.c,v 1.23 2002/08/31 22:17:32 keithp Exp $
Keith Packard's avatar
Keith Packard committed
3
 *
4
 * Copyright © 2000 Keith Packard
Keith Packard's avatar
Keith Packard committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation, and that the name of Keith Packard not be used in
 * advertising or publicity pertaining to distribution of the software without
 * specific, written prior permission.  Keith Packard makes no
 * representations about the suitability of this software for any purpose.  It
 * is provided "as is" without express or implied warranty.
 *
 * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
 * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
 */

25
26
#include <dirent.h>
#include <sys/types.h>
Keith Packard's avatar
Keith Packard committed
27
28
#include "fcint.h"

Tor Lillqvist's avatar
Tor Lillqvist committed
29
#if defined (_WIN32) && (defined (PIC) || defined (DLL_EXPORT))
Tor Lillqvist's avatar
Tor Lillqvist committed
30
31
32
33
34
#define STRICT
#include <windows.h>
#undef STRICT
#endif

35
36
37
38
#if defined (_WIN32) && !defined (R_OK)
#define R_OK 4
#endif

39
FcConfig    *_fcConfig;
Keith Packard's avatar
Keith Packard committed
40
41
42
43
44
45
46
47
48
49

FcConfig *
FcConfigCreate (void)
{
    FcSetName	set;
    FcConfig	*config;

    config = malloc (sizeof (FcConfig));
    if (!config)
	goto bail0;
50
    FcMemAlloc (FC_MEM_CONFIG, sizeof (FcConfig));
Keith Packard's avatar
Keith Packard committed
51
    
52
53
    config->configDirs = FcStrSetCreate ();
    if (!config->configDirs)
Keith Packard's avatar
Keith Packard committed
54
55
	goto bail1;
    
56
    config->configFiles = FcStrSetCreate ();
Keith Packard's avatar
Keith Packard committed
57
58
    if (!config->configFiles)
	goto bail2;
59
60
61
62
    
    config->fontDirs = FcStrSetCreate ();
    if (!config->fontDirs)
	goto bail3;
Keith Packard's avatar
Keith Packard committed
63
    
64
65
66
67
68
69
70
71
    config->acceptGlobs = FcStrSetCreate ();
    if (!config->acceptGlobs)
	goto bail4;

    config->rejectGlobs = FcStrSetCreate ();
    if (!config->rejectGlobs)
	goto bail5;

72
73
74
75
76
77
78
79
    config->acceptPatterns = FcFontSetCreate ();
    if (!config->acceptPatterns)
	goto bail6;
    
    config->rejectPatterns = FcFontSetCreate ();
    if (!config->rejectPatterns)
	goto bail7;

Keith Packard's avatar
Keith Packard committed
80
    config->cache = 0;
81
82
    if (FcConfigHome())
	if (!FcConfigSetCache (config, (FcChar8 *) ("~/" FC_USER_CACHE_FILE)))
83
	    goto bail8;
Keith Packard's avatar
Keith Packard committed
84

85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#ifdef _WIN32
    if (config->cache == 0)
    {
	/* If no home, use the temp folder. */
	FcChar8	    dummy[1];
	int	    templen = GetTempPath (1, dummy);
	FcChar8     *temp = malloc (templen + 1);

	if (temp)
	{
	    FcChar8 *cache_dir;

	    GetTempPath (templen + 1, temp);
	    cache_dir = FcStrPlus (temp, FC_USER_CACHE_FILE);
	    free (temp);
	    if (!FcConfigSetCache (config, cache_dir))
	    {
		FcStrFree (cache_dir);
		goto bail6;
	    }
	    FcStrFree (cache_dir);
	}
    }
#endif

Keith Packard's avatar
Keith Packard committed
110
111
112
113
114
115
116
    config->blanks = 0;

    config->substPattern = 0;
    config->substFont = 0;
    config->maxObjects = 0;
    for (set = FcSetSystem; set <= FcSetApplication; set++)
	config->fonts[set] = 0;
117
118
119

    config->rescanTime = time(0);
    config->rescanInterval = 30;    
Keith Packard's avatar
Keith Packard committed
120
121
122
    
    return config;

123
124
125
126
bail8:
    FcFontSetDestroy (config->rejectPatterns);
bail7:
    FcFontSetDestroy (config->acceptPatterns);
127
128
129
130
bail6:
    FcStrSetDestroy (config->rejectGlobs);
bail5:
    FcStrSetDestroy (config->acceptGlobs);
131
132
bail4:
    FcStrSetDestroy (config->fontDirs);
Keith Packard's avatar
Keith Packard committed
133
bail3:
134
    FcStrSetDestroy (config->configFiles);
Keith Packard's avatar
Keith Packard committed
135
bail2:
136
    FcStrSetDestroy (config->configDirs);
Keith Packard's avatar
Keith Packard committed
137
138
bail1:
    free (config);
139
    FcMemFree (FC_MEM_CONFIG, sizeof (FcConfig));
Keith Packard's avatar
Keith Packard committed
140
141
142
143
bail0:
    return 0;
}

144
static FcFileTime
145
146
147
FcConfigNewestFile (FcStrSet *files)
{
    FcStrList	    *list = FcStrListCreate (files);
148
    FcFileTime	    newest = { 0, FcFalse };
149
150
151
152
153
154
155
    FcChar8	    *file;
    struct  stat    statb;

    if (list)
    {
	while ((file = FcStrListNext (list)))
	    if (stat ((char *) file, &statb) == 0)
156
		if (!newest.set || statb.st_mtime - newest.time > 0)
157
158
		{
		    newest.set = FcTrue;
159
		    newest.time = statb.st_mtime;
160
		}
161
162
163
164
165
	FcStrListDone (list);
    }
    return newest;
}

166
167
168
169
170
171
172
173
174
175
176
177
178
FcFileTime
FcConfigModifiedTime (FcConfig *config)
{
    if (!config)
    {
	FcFileTime v = { 0, FcFalse };
	config = FcConfigGetCurrent ();
	if (!config)
	    return v;
    }
    return FcConfigNewestFile (config->configFiles);
}

179
180
181
FcBool
FcConfigUptoDate (FcConfig *config)
{
182
183
    FcFileTime	config_time, font_time;
    time_t	now = time(0);
184
185
186
187
188
189
190
    if (!config)
    {
	config = FcConfigGetCurrent ();
	if (!config)
	    return FcFalse;
    }
    config_time = FcConfigNewestFile (config->configFiles);
191
    font_time = FcConfigNewestFile (config->fontDirs);
192
    if ((config_time.set && config_time.time - config->rescanTime > 0) ||
193
	(font_time.set && (font_time.time - config->rescanTime) > 0))
194
195
196
197
198
199
200
    {
	return FcFalse;
    }
    config->rescanTime = now;
    return FcTrue;
}

Keith Packard's avatar
Keith Packard committed
201
202
203
204
205
206
207
208
static void
FcSubstDestroy (FcSubst *s)
{
    FcSubst *n;
    
    while (s)
    {
	n = s->next;
209
210
211
212
	if (s->test)
	    FcTestDestroy (s->test);
	if (s->edit)
	    FcEditDestroy (s->edit);
213
214
	free (s);
	FcMemFree (FC_MEM_SUBST, sizeof (FcSubst));
Keith Packard's avatar
Keith Packard committed
215
216
217
218
219
220
221
222
223
	s = n;
    }
}

void
FcConfigDestroy (FcConfig *config)
{
    FcSetName	set;

224
225
226
227
228
229
    if (config == _fcConfig)
	_fcConfig = 0;

    FcStrSetDestroy (config->configDirs);
    FcStrSetDestroy (config->fontDirs);
    FcStrSetDestroy (config->configFiles);
230
231
    FcStrSetDestroy (config->acceptGlobs);
    FcStrSetDestroy (config->rejectGlobs);
232
233
    FcFontSetDestroy (config->acceptPatterns);
    FcFontSetDestroy (config->rejectPatterns);
234

235
236
237
    if (config->blanks)
	FcBlanksDestroy (config->blanks);

238
239
    if (config->cache)
	FcStrFree (config->cache);
Keith Packard's avatar
Keith Packard committed
240
241
242
243
244
245

    FcSubstDestroy (config->substPattern);
    FcSubstDestroy (config->substFont);
    for (set = FcSetSystem; set <= FcSetApplication; set++)
	if (config->fonts[set])
	    FcFontSetDestroy (config->fonts[set]);
246

247
248
    free (config);
    FcMemFree (FC_MEM_CONFIG, sizeof (FcConfig));
Keith Packard's avatar
Keith Packard committed
249
250
251
252
253
254
255
256
257
258
259
}

/*
 * Scan the current list of directories in the configuration
 * and build the set of available fonts. Update the
 * per-user cache file to reflect the new configuration
 */

FcBool
FcConfigBuildFonts (FcConfig *config)
{
260
    FcFontSet	    *fonts, *cached_fonts;
261
262
    FcGlobalCache   *cache;
    FcStrList	    *list;
263
    FcStrSet	    *oldDirs;
264
    FcChar8	    *dir;
Keith Packard's avatar
Keith Packard committed
265
266
267
268

    fonts = FcFontSetCreate ();
    if (!fonts)
	goto bail0;
269
    
270
    cache = FcGlobalCacheCreate ();
Keith Packard's avatar
Keith Packard committed
271
272
273
    if (!cache)
	goto bail1;

274
275
276
277
    oldDirs = FcStrSetCreate ();
    if (!oldDirs)
        goto bail2;

278
    if (config->cache)
279
	FcGlobalCacheLoad (cache, oldDirs, config->cache, config);
280
281

    cached_fonts = FcCacheRead(config, cache);
282
283
284
285
    if (!cached_fonts)
    {
	list = FcConfigGetFontDirs (config);
	if (!list)
286
	    goto bail2;
287
288
289
290
	
	while ((dir = FcStrListNext (list)))
	{
	    if (FcDebug () & FC_DBG_FONTSET)
291
		printf ("build scan dir %s\n", dir);
292
293
294
295
296
297
298
	    FcDirScanConfig (fonts, config->fontDirs, cache, 
			     config->blanks, dir, FcFalse, config);
	}
	
	FcStrListDone (list);
    }
    else
Keith Packard's avatar
Keith Packard committed
299
    {
300
301
	int i;

302
303
304
        for (i = 0; i < oldDirs->num; i++)
        {
	    if (FcDebug () & FC_DBG_FONTSET)
305
		printf ("scan dir %s\n", oldDirs->strs[i]);
306
307
308
309
310
	    FcDirScanConfig (fonts, config->fontDirs, cache, 
			     config->blanks, oldDirs->strs[i], 
                             FcFalse, config);
	}

311
312
	for (i = 0; i < cached_fonts->nfont; i++)
	{
313
	    FcChar8 	*cfn; 
314
	    FcPatternGetString (cached_fonts->fonts[i], FC_FILE, 0, &cfn);
Patrick Lam's avatar
Patrick Lam committed
315

316
	    if (FcConfigAcceptFont (config, cached_fonts->fonts[i]) &&
317
                (cfn && FcConfigAcceptFilename (config, cfn)))
318
		FcFontSetAdd (fonts, cached_fonts->fonts[i]);
319
320

	    cached_fonts->fonts[i] = 0; /* prevent free in FcFontSetDestroy */
321
	}
322
	cached_fonts->nfont = 0;
323
	FcFontSetDestroy (cached_fonts);
Keith Packard's avatar
Keith Packard committed
324
325
326
327
328
    }
    
    if (FcDebug () & FC_DBG_FONTSET)
	FcFontSetPrint (fonts);

329
    if (config->cache)
330
	FcGlobalCacheSave (cache, config->cache, config);
331
    FcGlobalCacheDestroy (cache);
332
    FcStrSetDestroy (oldDirs);
Keith Packard's avatar
Keith Packard committed
333
334
335
336

    FcConfigSetFonts (config, fonts, FcSetSystem);
    
    return FcTrue;
337
338
bail2:
    FcStrSetDestroy (oldDirs);
Keith Packard's avatar
Keith Packard committed
339
340
341
342
343
344
345
346
347
348
349
350
351
bail1:
    FcFontSetDestroy (fonts);
bail0:
    return FcFalse;
}

FcBool
FcConfigSetCurrent (FcConfig *config)
{
    if (!config->fonts)
	if (!FcConfigBuildFonts (config))
	    return FcFalse;

352
353
354
    if (_fcConfig)
	FcConfigDestroy (_fcConfig);
    _fcConfig = config;
Keith Packard's avatar
Keith Packard committed
355
356
357
358
359
360
    return FcTrue;
}

FcConfig *
FcConfigGetCurrent (void)
{
361
362
363
364
    if (!_fcConfig)
	if (!FcInit ())
	    return 0;
    return _fcConfig;
Keith Packard's avatar
Keith Packard committed
365
366
367
}

FcBool
368
369
FcConfigAddConfigDir (FcConfig	    *config,
		      const FcChar8 *d)
Keith Packard's avatar
Keith Packard committed
370
{
371
372
    return FcStrSetAddFilename (config->configDirs, d);
}
Keith Packard's avatar
Keith Packard committed
373

374
375
376
377
FcStrList *
FcConfigGetConfigDirs (FcConfig   *config)
{
    if (!config)
Keith Packard's avatar
Keith Packard committed
378
    {
379
380
381
	config = FcConfigGetCurrent ();
	if (!config)
	    return 0;
Keith Packard's avatar
Keith Packard committed
382
    }
383
384
385
386
387
388
389
390
    return FcStrListCreate (config->configDirs);
}

FcBool
FcConfigAddFontDir (FcConfig	    *config,
		    const FcChar8   *d)
{
    return FcStrSetAddFilename (config->fontDirs, d);
Keith Packard's avatar
Keith Packard committed
391
392
}

393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
static FcBool
FcConfigAddFontDirSubdirs (FcConfig        *config,
			   const FcChar8   *d)
{
    DIR *dir;
    struct dirent *e;
    FcChar8 *subdir;
    
    if (!(dir = opendir ((char *) d)))
	return FcFalse;
    if (!(subdir = (FcChar8 *) malloc (strlen ((char *) d) + FC_MAX_FILE_LEN + 2)))
    {
	fprintf (stderr, "out of memory");
	return FcFalse;
    }
    while ((e = readdir (dir)))
    {
	if (strcmp (e->d_name, ".") && strcmp (e->d_name, "..") &&
	    strlen (e->d_name) < FC_MAX_FILE_LEN)
	{
	    strcpy ((char *)subdir, (char *)d);
	    strcat ((char *)subdir, "/");
	    strcat ((char *)subdir, e->d_name);
	    if (FcFileIsDir (subdir))
	    {
		FcConfigAddFontDir (config, subdir);
		FcConfigAddFontDirSubdirs (config, subdir);
	    }
	}
    }
    free (subdir);
    closedir (dir);
    return FcTrue;
}

428
429
430
431
432
433
const FcChar8 *
FcConfigNormalizeFontDir (FcConfig  	*config, 
			  const FcChar8 *d)
{
    /* If this is a bottleneck, we can cache the fontDir inodes. */
    ino_t	di;
434
    dev_t	dd;
435
    int		n, n0;
436
437
438
439
    struct stat s;

    if (stat ((char *)d, &s) == -1)
	return 0;
440
    di = s.st_ino; dd = s.st_dev;
441
442
443
444
445

    for (n = 0; n < config->fontDirs->num; n++)
    {
	if (stat ((char *)config->fontDirs->strs[n], &s) == -1)
	    continue;
446
	if (di == s.st_ino && dd == s.st_dev)
447
448
	    return config->fontDirs->strs[n];
    }
449
450

    /* Ok, we didn't find it in fontDirs; let's add subdirs.... */
451
    for (n = 0, n0 = config->fontDirs->num; n < n0; n++)
452
453
454
455
456
457
458
459
460
461
462
463
	FcConfigAddFontDirSubdirs (config, config->fontDirs->strs[n]);

    /* ... and try again. */
    for (n = 0; n < config->fontDirs->num; n++)
    {
	if (stat ((char *)config->fontDirs->strs[n], &s) == -1)
	    continue;
	if (di == s.st_ino && dd == s.st_dev)
	    return config->fontDirs->strs[n];
    }

    /* if it fails, then really give up. */
464
465
466
    return 0;
}

467
468
469
470
471
472
473
474
475
476
FcBool
FcConfigAddDir (FcConfig	    *config,
		const FcChar8	    *d)
{
    return (FcConfigAddConfigDir (config, d) && 
	    FcConfigAddFontDir (config, d));
}

FcStrList *
FcConfigGetFontDirs (FcConfig	*config)
Keith Packard's avatar
Keith Packard committed
477
478
479
480
481
482
483
{
    if (!config)
    {
	config = FcConfigGetCurrent ();
	if (!config)
	    return 0;
    }
484
    return FcStrListCreate (config->fontDirs);
Keith Packard's avatar
Keith Packard committed
485
486
487
488
}

FcBool
FcConfigAddConfigFile (FcConfig	    *config,
489
		       const FcChar8   *f)
Keith Packard's avatar
Keith Packard committed
490
{
491
492
493
    FcBool	ret;
    FcChar8	*file = FcConfigFilename (f);
    
Keith Packard's avatar
Keith Packard committed
494
495
    if (!file)
	return FcFalse;
496
497
498
499
    
    ret = FcStrSetAdd (config->configFiles, file);
    FcStrFree (file);
    return ret;
Keith Packard's avatar
Keith Packard committed
500
501
}

502
FcStrList *
Keith Packard's avatar
Keith Packard committed
503
504
505
506
507
508
509
510
FcConfigGetConfigFiles (FcConfig    *config)
{
    if (!config)
    {
	config = FcConfigGetCurrent ();
	if (!config)
	    return 0;
    }
511
    return FcStrListCreate (config->configFiles);
Keith Packard's avatar
Keith Packard committed
512
513
514
515
}

FcBool
FcConfigSetCache (FcConfig	*config,
516
		  const FcChar8	*c)
Keith Packard's avatar
Keith Packard committed
517
{
518
519
520
521
    FcChar8    *new = FcStrCopyFilename (c);
    
    if (!new)
	return FcFalse;
Keith Packard's avatar
Keith Packard committed
522
    if (config->cache)
523
	FcStrFree (config->cache);
Keith Packard's avatar
Keith Packard committed
524
525
526
527
    config->cache = new;
    return FcTrue;
}

528
FcChar8 *
Keith Packard's avatar
Keith Packard committed
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
FcConfigGetCache (FcConfig  *config)
{
    if (!config)
    {
	config = FcConfigGetCurrent ();
	if (!config)
	    return 0;
    }
    return config->cache;
}

FcFontSet *
FcConfigGetFonts (FcConfig	*config,
		  FcSetName	set)
{
    if (!config)
    {
	config = FcConfigGetCurrent ();
	if (!config)
	    return 0;
    }
    return config->fonts[set];
}

void
FcConfigSetFonts (FcConfig	*config,
		  FcFontSet	*fonts,
		  FcSetName	set)
{
    if (config->fonts[set])
	FcFontSetDestroy (config->fonts[set]);
    config->fonts[set] = fonts;
}

563
564


Keith Packard's avatar
Keith Packard committed
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
FcBlanks *
FcConfigGetBlanks (FcConfig	*config)
{
    if (!config)
    {
	config = FcConfigGetCurrent ();
	if (!config)
	    return 0;
    }
    return config->blanks;
}

FcBool
FcConfigAddBlank (FcConfig	*config,
		  FcChar32    	blank)
{
    FcBlanks	*b;
    
    b = config->blanks;
    if (!b)
    {
	b = FcBlanksCreate ();
	if (!b)
	    return FcFalse;
    }
    if (!FcBlanksAdd (b, blank))
	return FcFalse;
    config->blanks = b;
    return FcTrue;
}

596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
int
FcConfigGetRescanInverval (FcConfig *config)
{
    if (!config)
    {
	config = FcConfigGetCurrent ();
	if (!config)
	    return 0;
    }
    return config->rescanInterval;
}

FcBool
FcConfigSetRescanInverval (FcConfig *config, int rescanInterval)
{
    if (!config)
    {
	config = FcConfigGetCurrent ();
	if (!config)
	    return FcFalse;
    }
    config->rescanInterval = rescanInterval;
    return FcTrue;
}

Keith Packard's avatar
Keith Packard committed
621
622
623
624
625
626
627
628
629
630
631
632
633
FcBool
FcConfigAddEdit (FcConfig	*config,
		 FcTest		*test,
		 FcEdit		*edit,
		 FcMatchKind	kind)
{
    FcSubst	*subst, **prev;
    FcTest	*t;
    int		num;

    subst = (FcSubst *) malloc (sizeof (FcSubst));
    if (!subst)
	return FcFalse;
634
    FcMemAlloc (FC_MEM_SUBST, sizeof (FcSubst));
Keith Packard's avatar
Keith Packard committed
635
636
637
638
639
640
641
642
643
644
645
    if (kind == FcMatchPattern)
	prev = &config->substPattern;
    else
	prev = &config->substFont;
    for (; *prev; prev = &(*prev)->next);
    *prev = subst;
    subst->next = 0;
    subst->test = test;
    subst->edit = edit;
    num = 0;
    for (t = test; t; t = t->next)
646
647
648
    {
	if (t->kind == FcMatchDefault)
	    t->kind = kind;
Keith Packard's avatar
Keith Packard committed
649
	num++;
650
    }
Keith Packard's avatar
Keith Packard committed
651
652
    if (config->maxObjects < num)
	config->maxObjects = num;
653
654
655
656
657
    if (FcDebug () & FC_DBG_EDIT)
    {
	printf ("Add Subst ");
	FcSubstPrint (subst);
    }
Keith Packard's avatar
Keith Packard committed
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
    return FcTrue;
}

typedef struct _FcSubState {
    FcPatternElt   *elt;
    FcValueList    *value;
} FcSubState;

static FcValue
FcConfigPromote (FcValue v, FcValue u)
{
    if (v.type == FcTypeInteger)
    {
	v.type = FcTypeDouble;
	v.u.d = (double) v.u.i;
    }
    else if (v.type == FcTypeVoid && u.type == FcTypeMatrix)
    {
676
	v.u.m = &FcIdentityMatrix;
677
	v.type = FcTypeMatrix;
Keith Packard's avatar
Keith Packard committed
678
    }
679
680
    else if (v.type == FcTypeString && u.type == FcTypeLangSet)
    {
681
	v.u.l = FcLangSetPromote (v.u.s);
682
683
	v.type = FcTypeLangSet;
    }
Keith Packard's avatar
Keith Packard committed
684
685
686
687
    return v;
}

FcBool
688
FcConfigCompareValue (const FcValue	*left_o,
689
		      FcOp		op,
690
		      const FcValue	*right_o)
Keith Packard's avatar
Keith Packard committed
691
{
692
693
    FcValue	left = FcValueCanonicalize(left_o);
    FcValue	right = FcValueCanonicalize(right_o);
694
    FcBool	ret = FcFalse;
Keith Packard's avatar
Keith Packard committed
695
    
696
697
698
    left = FcConfigPromote (left, right);
    right = FcConfigPromote (right, left);
    if (left.type == right.type) 
Keith Packard's avatar
Keith Packard committed
699
    {
700
	switch (left.type) {
Keith Packard's avatar
Keith Packard committed
701
702
703
704
705
706
	case FcTypeInteger:
	    break;	/* FcConfigPromote prevents this from happening */
	case FcTypeDouble:
	    switch (op) {
	    case FcOpEqual:
	    case FcOpContains:
707
708
	    case FcOpListing:
		ret = left.u.d == right.u.d;
Keith Packard's avatar
Keith Packard committed
709
		break;
710
711
	    case FcOpNotEqual:
	    case FcOpNotContains:
712
		ret = left.u.d != right.u.d;
Keith Packard's avatar
Keith Packard committed
713
714
		break;
	    case FcOpLess:    
715
		ret = left.u.d < right.u.d;
Keith Packard's avatar
Keith Packard committed
716
717
		break;
	    case FcOpLessEqual:    
718
		ret = left.u.d <= right.u.d;
Keith Packard's avatar
Keith Packard committed
719
720
		break;
	    case FcOpMore:    
721
		ret = left.u.d > right.u.d;
Keith Packard's avatar
Keith Packard committed
722
723
		break;
	    case FcOpMoreEqual:    
724
		ret = left.u.d >= right.u.d;
Keith Packard's avatar
Keith Packard committed
725
726
727
728
729
730
731
732
733
		break;
	    default:
		break;
	    }
	    break;
	case FcTypeBool:
	    switch (op) {
	    case FcOpEqual:    
	    case FcOpContains:
734
735
	    case FcOpListing:
		ret = left.u.b == right.u.b;
Keith Packard's avatar
Keith Packard committed
736
		break;
737
738
	    case FcOpNotEqual:
	    case FcOpNotContains:
739
		ret = left.u.b != right.u.b;
Keith Packard's avatar
Keith Packard committed
740
741
742
743
744
745
746
747
		break;
	    default:
		break;
	    }
	    break;
	case FcTypeString:
	    switch (op) {
	    case FcOpEqual:    
748
	    case FcOpListing:
749
		ret = FcStrCmpIgnoreCase (left.u.s, right.u.s) == 0;
Keith Packard's avatar
Keith Packard committed
750
		break;
751
	    case FcOpContains:
752
		ret = FcStrStrIgnoreCase (left.u.s, right.u.s) != 0;
753
		break;
754
	    case FcOpNotEqual:
755
		ret = FcStrCmpIgnoreCase (left.u.s, right.u.s) != 0;
756
		break;
757
	    case FcOpNotContains:
758
		ret = FcStrCmpIgnoreCase (left.u.s, right.u.s) == 0;
759
		break;
Keith Packard's avatar
Keith Packard committed
760
761
762
763
764
765
766
767
	    default:
		break;
	    }
	    break;
	case FcTypeMatrix:
	    switch (op) {
	    case FcOpEqual:
	    case FcOpContains:
768
	    case FcOpListing:
769
		ret = FcMatrixEqual (left.u.m, right.u.m);
Keith Packard's avatar
Keith Packard committed
770
771
		break;
	    case FcOpNotEqual:
772
	    case FcOpNotContains:
773
		ret = !FcMatrixEqual (left.u.m, right.u.m);
Keith Packard's avatar
Keith Packard committed
774
775
776
777
778
779
780
781
		break;
	    default:
		break;
	    }
	    break;
	case FcTypeCharSet:
	    switch (op) {
	    case FcOpContains:
782
783
	    case FcOpListing:
		/* left contains right if right is a subset of left */
784
		ret = FcCharSetIsSubset (right.u.c, left.u.c);
Keith Packard's avatar
Keith Packard committed
785
		break;
786
	    case FcOpNotContains:
787
		/* left contains right if right is a subset of left */
788
		ret = !FcCharSetIsSubset (right.u.c, left.u.c);
789
		break;
Keith Packard's avatar
Keith Packard committed
790
	    case FcOpEqual:
791
		ret = FcCharSetEqual (left.u.c, right.u.c);
Keith Packard's avatar
Keith Packard committed
792
793
		break;
	    case FcOpNotEqual:
794
		ret = !FcCharSetEqual (left.u.c, right.u.c);
Keith Packard's avatar
Keith Packard committed
795
796
797
798
799
		break;
	    default:
		break;
	    }
	    break;
800
801
802
	case FcTypeLangSet:
	    switch (op) {
	    case FcOpContains:
803
	    case FcOpListing:
804
		ret = FcLangSetContains (left.u.l, right.u.l);
805
		break;
806
	    case FcOpNotContains:
807
		ret = !FcLangSetContains (left.u.l, right.u.l);
808
		break;
809
	    case FcOpEqual:
810
		ret = FcLangSetEqual (left.u.l, right.u.l);
811
812
		break;
	    case FcOpNotEqual:
813
		ret = !FcLangSetEqual (left.u.l, right.u.l);
814
815
816
817
818
		break;
	    default:
		break;
	    }
	    break;
Keith Packard's avatar
Keith Packard committed
819
820
821
822
	case FcTypeVoid:
	    switch (op) {
	    case FcOpEqual:
	    case FcOpContains:
823
	    case FcOpListing:
Keith Packard's avatar
Keith Packard committed
824
825
826
827
828
829
		ret = FcTrue;
		break;
	    default:
		break;
	    }
	    break;
830
831
832
	case FcTypeFTFace:
	    switch (op) {
	    case FcOpEqual:
833
	    case FcOpContains:
834
835
	    case FcOpListing:
		ret = left.u.f == right.u.f;
836
837
		break;
	    case FcOpNotEqual:
838
	    case FcOpNotContains:
839
		ret = left.u.f != right.u.f;
840
841
842
843
		break;
	    default:
		break;
	    }
844
	    break;
Keith Packard's avatar
Keith Packard committed
845
846
847
848
	}
    }
    else
    {
849
	if (op == FcOpNotEqual || op == FcOpNotContains)
Keith Packard's avatar
Keith Packard committed
850
851
852
853
854
855
	    ret = FcTrue;
    }
    return ret;
}


856
857
858
859
860
861
862
#define _FcDoubleFloor(d)	((int) (d))
#define _FcDoubleCeil(d)	((double) (int) (d) == (d) ? (int) (d) : (int) ((d) + 1))
#define FcDoubleFloor(d)	((d) >= 0 ? _FcDoubleFloor(d) : -_FcDoubleCeil(-(d)))
#define FcDoubleCeil(d)		((d) >= 0 ? _FcDoubleCeil(d) : -_FcDoubleFloor(-(d)))
#define FcDoubleRound(d)	FcDoubleFloor ((d) + 0.5)
#define FcDoubleTrunc(d)	((d) >= 0 ? _FcDoubleFloor (d) : -_FcDoubleFloor (-(d)))

Keith Packard's avatar
Keith Packard committed
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
static FcValue
FcConfigEvaluate (FcPattern *p, FcExpr *e)
{
    FcValue	v, vl, vr;
    FcResult	r;
    FcMatrix	*m;
    
    switch (e->op) {
    case FcOpInteger:
	v.type = FcTypeInteger;
	v.u.i = e->u.ival;
	break;
    case FcOpDouble:
	v.type = FcTypeDouble;
	v.u.d = e->u.dval;
	break;
    case FcOpString:
	v.type = FcTypeString;
881
	v.u.s = FcStrStaticName(e->u.sval);
Keith Packard's avatar
Keith Packard committed
882
883
884
	break;
    case FcOpMatrix:
	v.type = FcTypeMatrix;
885
	v.u.m = e->u.mval;
Keith Packard's avatar
Keith Packard committed
886
887
888
889
	v = FcValueSave (v);
	break;
    case FcOpCharSet:
	v.type = FcTypeCharSet;
890
	v.u.c = e->u.cval;
Keith Packard's avatar
Keith Packard committed
891
892
893
894
895
896
897
898
899
900
	v = FcValueSave (v);
	break;
    case FcOpBool:
	v.type = FcTypeBool;
	v.u.b = e->u.bval;
	break;
    case FcOpField:
	r = FcPatternGet (p, e->u.field, 0, &v);
	if (r != FcResultMatch)
	    v.type = FcTypeVoid;
901
	v = FcValueSave (v);
Keith Packard's avatar
Keith Packard committed
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
	break;
    case FcOpConst:
	if (FcNameConstant (e->u.constant, &v.u.i))
	    v.type = FcTypeInteger;
	else
	    v.type = FcTypeVoid;
	break;
    case FcOpQuest:
	vl = FcConfigEvaluate (p, e->u.tree.left);
	if (vl.type == FcTypeBool)
	{
	    if (vl.u.b)
		v = FcConfigEvaluate (p, e->u.tree.right->u.tree.left);
	    else
		v = FcConfigEvaluate (p, e->u.tree.right->u.tree.right);
	}
	else
	    v.type = FcTypeVoid;
	FcValueDestroy (vl);
	break;
922
    case FcOpEqual:
Keith Packard's avatar
Keith Packard committed
923
924
925
926
927
    case FcOpNotEqual:
    case FcOpLess:
    case FcOpLessEqual:
    case FcOpMore:
    case FcOpMoreEqual:
928
929
    case FcOpContains:
    case FcOpNotContains:
930
    case FcOpListing:
931
932
933
	vl = FcConfigEvaluate (p, e->u.tree.left);
	vr = FcConfigEvaluate (p, e->u.tree.right);
	v.type = FcTypeBool;
934
	v.u.b = FcConfigCompareValue (&vl, e->op, &vr);
935
936
937
938
939
	FcValueDestroy (vl);
	FcValueDestroy (vr);
	break;	
    case FcOpOr:
    case FcOpAnd:
Keith Packard's avatar
Keith Packard committed
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
    case FcOpPlus:
    case FcOpMinus:
    case FcOpTimes:
    case FcOpDivide:
	vl = FcConfigEvaluate (p, e->u.tree.left);
	vr = FcConfigEvaluate (p, e->u.tree.right);
	vl = FcConfigPromote (vl, vr);
	vr = FcConfigPromote (vr, vl);
	if (vl.type == vr.type)
	{
	    switch (vl.type) {
	    case FcTypeDouble:
		switch (e->op) {
		case FcOpPlus:	   
		    v.type = FcTypeDouble;
		    v.u.d = vl.u.d + vr.u.d; 
		    break;
		case FcOpMinus:
		    v.type = FcTypeDouble;
		    v.u.d = vl.u.d - vr.u.d; 
		    break;
		case FcOpTimes:
		    v.type = FcTypeDouble;
		    v.u.d = vl.u.d * vr.u.d; 
		    break;
		case FcOpDivide:
		    v.type = FcTypeDouble;
		    v.u.d = vl.u.d / vr.u.d; 
		    break;
		default:
		    v.type = FcTypeVoid; 
		    break;
		}
		if (v.type == FcTypeDouble &&
		    v.u.d == (double) (int) v.u.d)
		{
		    v.type = FcTypeInteger;
		    v.u.i = (int) v.u.d;
		}
		break;
	    case FcTypeBool:
		switch (e->op) {
		case FcOpOr:
		    v.type = FcTypeBool;
		    v.u.b = vl.u.b || vr.u.b;
		    break;
		case FcOpAnd:
		    v.type = FcTypeBool;
		    v.u.b = vl.u.b && vr.u.b;
		    break;
		default:
		    v.type = FcTypeVoid; 
		    break;
		}
		break;
	    case FcTypeString:
		switch (e->op) {
		case FcOpPlus:
		    v.type = FcTypeString;
999
		    v.u.s = FcStrStaticName (FcStrPlus (vl.u.s, vr.u.s));
1000
			 
1001
		    if (!v.u.s)
Keith Packard's avatar
Keith Packard committed
1002
1003
1004
1005
1006
1007
			v.type = FcTypeVoid;
		    break;
		default:
		    v.type = FcTypeVoid;
		    break;
		}
1008
		break;
Keith Packard's avatar
Keith Packard committed
1009
1010
1011
1012
1013
1014
1015
1016
	    case FcTypeMatrix:
		switch (e->op) {
		case FcOpTimes:
		    v.type = FcTypeMatrix;
		    m = malloc (sizeof (FcMatrix));
		    if (m)
		    {
			FcMemAlloc (FC_MEM_MATRIX, sizeof (FcMatrix));
1017
1018
			FcMatrixMultiply (m, vl.u.m, vr.u.m);
			v.u.m = m;
Keith Packard's avatar
Keith Packard committed
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
		    }
		    else
		    {
			v.type = FcTypeVoid;
		    }
		    break;
		default:
		    v.type = FcTypeVoid;
		    break;
		}
		break;
	    default:
		v.type = FcTypeVoid;
		break;
	    }
	}
	else
	    v.type = FcTypeVoid;
	FcValueDestroy (vl);
	FcValueDestroy (vr);
	break;
    case FcOpNot:
	vl = FcConfigEvaluate (p, e->u.tree.left);
	switch (vl.type) {
	case FcTypeBool:
	    v.type = FcTypeBool;
	    v.u.b = !vl.u.b;
	    break;
	default:
	    v.type = FcTypeVoid;
	    break;
	}
	FcValueDestroy (vl);
	break;
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
    case FcOpFloor:
	vl = FcConfigEvaluate (p, e->u.tree.left);
	switch (vl.type) {
	case FcTypeInteger:
	    v = vl;
	    break;
	case FcTypeDouble:
	    v.type = FcTypeInteger;
	    v.u.i = FcDoubleFloor (vl.u.d);
	    break;
	default:
	    v.type = FcTypeVoid;
	    break;
	}
	FcValueDestroy (vl);
	break;
    case FcOpCeil:
	vl = FcConfigEvaluate (p, e->u.tree.left);
	switch (vl.type) {
	case FcTypeInteger:
	    v = vl;
	    break;
	case FcTypeDouble:
	    v.type = FcTypeInteger;
	    v.u.i = FcDoubleCeil (vl.u.d);
	    break;
	default:
	    v.type = FcTypeVoid;
	    break;
	}
	FcValueDestroy (vl);
	break;
    case FcOpRound:
	vl = FcConfigEvaluate (p, e->u.tree.left);
	switch (vl.type) {
	case FcTypeInteger:
	    v = vl;
	    break;
	case FcTypeDouble:
	    v.type = FcTypeInteger;
	    v.u.i = FcDoubleRound (vl.u.d);
	    break;
	default:
	    v.type = FcTypeVoid;
	    break;
	}
	FcValueDestroy (vl);
	break;
    case FcOpTrunc:
	vl = FcConfigEvaluate (p, e->u.tree.left);
	switch (vl.type) {
	case FcTypeInteger:
	    v = vl;
	    break;
	case FcTypeDouble:
	    v.type = FcTypeInteger;
	    v.u.i = FcDoubleTrunc (vl.u.d);
	    break;
	default:
	    v.type = FcTypeVoid;
	    break;
	}
	FcValueDestroy (vl);
	break;
Keith Packard's avatar
Keith Packard committed
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
    default:
	v.type = FcTypeVoid;
	break;
    }
    return v;
}

static FcValueList *
FcConfigMatchValueList (FcPattern	*p,
			FcTest		*t,
1127
			FcValueList	*values)
Keith Packard's avatar
Keith Packard committed
1128
{
1129
1130
1131
    FcValueList	    *ret = 0;
    FcExpr	    *e = t->expr;
    FcValue	    value;
1132
    FcValueList	    *v;
Keith Packard's avatar
Keith Packard committed
1133
    
1134
    while (e)
Keith Packard's avatar
Keith Packard committed
1135
    {
1136
	/* Compute the value of the match expression */
1137
	if (e->op == FcOpComma)
Keith Packard's avatar
Keith Packard committed
1138
	{
1139
1140
	    value = FcConfigEvaluate (p, e->u.tree.left);
	    e = e->u.tree.right;
Keith Packard's avatar
Keith Packard committed
1141
1142
1143
	}
	else
	{
1144
1145
1146
1147
	    value = FcConfigEvaluate (p, e);
	    e = 0;
	}

1148
	for (v = values; v; v = FcValueListPtrU(v->next))
1149
	{
1150
	    /* Compare the pattern value to the match expression value */
1151
	    if (FcConfigCompareValue (&v->value, t->op, &value))
Keith Packard's avatar
Keith Packard committed
1152
	    {
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
		if (!ret)
		    ret = v;
	    }
	    else
	    {
		if (t->qual == FcQualAll)
		{
		    ret = 0;
		    break;
		}
Keith Packard's avatar
Keith Packard committed
1163
1164
	    }
	}
1165
	FcValueDestroy (value);
Keith Packard's avatar
Keith Packard committed
1166
1167
1168
1169
1170
    }
    return ret;
}

static FcValueList *
1171
FcConfigValues (FcPattern *p, FcExpr *e, FcValueBinding binding)
Keith Packard's avatar
Keith Packard committed
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
{
    FcValueList	*l;
    
    if (!e)
	return 0;
    l = (FcValueList *) malloc (sizeof (FcValueList));
    if (!l)
	return 0;
    FcMemAlloc (FC_MEM_VALLIST, sizeof (FcValueList));
    if (e->op == FcOpComma)
    {
	l->value = FcConfigEvaluate (p, e->u.tree.left);
1184
	l->next  = FcValueListPtrCreateDynamic(FcConfigValues (p, e->u.tree.right, binding));
Keith Packard's avatar
Keith Packard committed
1185
1186
1187
1188
    }
    else
    {
	l->value = FcConfigEvaluate (p, e);
1189
	l->next  = FcValueListPtrCreateDynamic(0);
Keith Packard's avatar
Keith Packard committed
1190
    }
1191
    l->binding = binding;
1192
    if (l->value.type == FcTypeVoid)
Keith Packard's avatar
Keith Packard committed
1193
    {
1194
	FcValueList  *next = FcValueListPtrU(l->next);
1195

1196
1197
1198
	FcMemFree (FC_MEM_VALLIST, sizeof (FcValueList));
	free (l);
	l = next;
Keith Packard's avatar
Keith Packard committed
1199
    }
1200

Keith Packard's avatar
Keith Packard committed
1201
1202
1203
1204
    return l;
}

static FcBool
1205
FcConfigAdd (FcValueListPtr *head,
Keith Packard's avatar
Keith Packard committed
1206
1207
1208
1209
	     FcValueList    *position,
	     FcBool	    append,
	     FcValueList    *new)
{
1210
    FcValueListPtr  *prev, last, v;
1211
    FcValueBinding  sameBinding;
Keith Packard's avatar
Keith Packard committed
1212
    
1213
1214
1215
1216
    if (position)
	sameBinding = position->binding;
    else
	sameBinding = FcValueBindingWeak;
1217
1218
1219
1220
    for (v = FcValueListPtrCreateDynamic(new); FcValueListPtrU(v); 
	 v = FcValueListPtrU(v)->next)
	if (FcValueListPtrU(v)->binding == FcValueBindingSame)
	    FcValueListPtrU(v)->binding = sameBinding;
Keith Packard's avatar
Keith Packard committed
1221
1222
1223
1224
1225
    if (append)
    {
	if (position)
	    prev = &position->next;
	else
1226
1227
	    for (prev = head; FcValueListPtrU(*prev); 
		 prev = &(FcValueListPtrU(*prev)->next))
Keith Packard's avatar
Keith Packard committed
1228
1229
1230
1231
1232
1233
		;
    }
    else
    {
	if (position)
	{
1234
1235
	    for (prev = head; FcValueListPtrU(*prev); 
		 prev = &(FcValueListPtrU(*prev)->next))
Keith Packard's avatar
Keith Packard committed
1236
	    {
1237
		if (FcValueListPtrU(*prev) == position)
Keith Packard's avatar
Keith Packard committed
1238
1239
1240
1241
1242
1243
1244
1245
		    break;
	    }
	}
	else
	    prev = head;

	if (FcDebug () & FC_DBG_EDIT)
	{
1246
	    if (!FcValueListPtrU(*prev))
Keith Packard's avatar
Keith Packard committed
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
		printf ("position not on list\n");
	}
    }

    if (FcDebug () & FC_DBG_EDIT)
    {
	printf ("%s list before ", append ? "Append" : "Prepend");
	FcValueListPrint (*head);
	printf ("\n");
    }
    
    if (new)
    {
1260
1261
1262
	last = FcValueListPtrCreateDynamic(new);
	while (FcValueListPtrU(FcValueListPtrU(last)->next))
	    last = FcValueListPtrU(last)->next;
Keith Packard's avatar
Keith Packard committed
1263
    
1264
1265
	FcValueListPtrU(last)->next = *prev;
	*prev = FcValueListPtrCreateDynamic(new);
Keith Packard's avatar
Keith Packard committed
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
    }
    
    if (FcDebug () & FC_DBG_EDIT)
    {
	printf ("%s list after ", append ? "Append" : "Prepend");
	FcValueListPrint (*head);
	printf ("\n");
    }
    
    return FcTrue;
}

static void
1279
FcConfigDel (FcValueListPtr *head,
Keith Packard's avatar
Keith Packard committed
1280
1281
	     FcValueList    *position)
{
1282
    FcValueListPtr *prev;
Keith Packard's avatar
Keith Packard committed
1283

1284
1285
    for (prev = head; FcValueListPtrU(*prev); 
	 prev = &(FcValueListPtrU(*prev)->next))
Keith Packard's avatar
Keith Packard committed
1286
    {
1287
	if (FcValueListPtrU(*prev) == position)
Keith Packard's avatar
Keith Packard committed
1288
1289
	{
	    *prev = position->next;
1290
1291
	    position->next = FcValueListPtrCreateDynamic(0);
	    FcValueListDestroy (FcValueListPtrCreateDynamic(position));
Keith Packard's avatar
Keith Packard committed
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
	    break;
	}
    }
}

static void
FcConfigPatternAdd (FcPattern	*p,
		    const char	*object,
		    FcValueList	*list,
		    FcBool	append)
{
    if (list)
    {
1305
	FcPatternElt    *e = FcPatternInsertElt (p, object);
Keith Packard's avatar
Keith Packard committed
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
    
	if (!e)
	    return;
	FcConfigAdd (&e->values, 0, append, list);
    }
}

/*
 * Delete all values associated with a field
 */
static void
FcConfigPatternDel (FcPattern	*p,
		    const char	*object)
{
1320
    FcPatternElt    *e = FcPatternFindElt (p, object);
Keith Packard's avatar
Keith Packard committed
1321
1322
    if (!e)
	return;
1323
1324
    while (FcValueListPtrU(e->values))
	FcConfigDel (&e->values, FcValueListPtrU(e->values));
Keith Packard's avatar
Keith Packard committed
1325
1326
1327
1328
1329
1330
}

static void
FcConfigPatternCanon (FcPattern	    *p,
		      const char    *object)
{
1331
    FcPatternElt    *e = FcPatternFindElt (p, object);
Keith Packard's avatar
Keith Packard committed
1332
1333
    if (!e)
	return;
1334
    if (!FcValueListPtrU(e->values))
Keith Packard's avatar
Keith Packard committed
1335
1336
1337
1338
	FcPatternDel (p, object);
}

FcBool
1339
1340
1341
1342
FcConfigSubstituteWithPat (FcConfig    *config,
			   FcPattern   *p,
			   FcPattern   *p_pat,
			   FcMatchKind kind)
Keith Packard's avatar
Keith Packard committed
1343
1344
1345
1346
1347
1348
1349
{
    FcSubst	    *s;
    FcSubState	    *st;
    int		    i;
    FcTest	    *t;
    FcEdit	    *e;
    FcValueList	    *l;
1350
    FcPattern	    *m;
Keith Packard's avatar
Keith Packard committed
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385

    if (!config)
    {
	config = FcConfigGetCurrent ();
	if (!config)
	    return FcFalse;
    }

    st = (FcSubState *) malloc (config->maxObjects * sizeof (FcSubState));
    if (!st && config->maxObjects)
	return FcFalse;
    FcMemAlloc (FC_MEM_SUBSTATE, config->maxObjects * sizeof (FcSubState));

    if (FcDebug () & FC_DBG_EDIT)
    {
	printf ("FcConfigSubstitute ");
	FcPatternPrint (p);
    }
    if (kind == FcMatchPattern)
	s = config->substPattern;
    else
	s = config->substFont;
    for (; s; s = s->next)
    {
	/*
	 * Check the tests to see if
	 * they all match the pattern
	 */
	for (t = s->test, i = 0; t; t = t->next, i++)
	{
	    if (FcDebug () & FC_DBG_EDIT)
	    {
		printf ("FcConfigSubstitute test ");
		FcTestPrint (t);
	    }
1386
1387
1388
1389
1390
1391
1392
1393
1394
	    st[i].elt = 0;
	    if (kind == FcMatchFont && t->kind == FcMatchPattern)
		m = p_pat;
	    else