]> git.wh0rd.org - fontconfig.git/blame - src/fcmatch.c
Let make expand fc_cachedir/FC_CACHEDIR (bug #18675)
[fontconfig.git] / src / fcmatch.c
CommitLineData
24330d27 1/*
317b8492 2 * fontconfig/src/fcmatch.c
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"
24330d27
KP
26#include <string.h>
27#include <ctype.h>
24330d27
KP
28#include <stdio.h>
29
30static double
9ab79bdf 31FcCompareNumber (FcValue *value1, FcValue *value2)
24330d27 32{
2d39321f 33 double v1, v2, v;
24330d27 34
4262e0b3 35 switch (value1->type) {
2d39321f 36 case FcTypeInteger:
4262e0b3 37 v1 = (double) value1->u.i;
2d39321f
KP
38 break;
39 case FcTypeDouble:
4262e0b3 40 v1 = value1->u.d;
2d39321f
KP
41 break;
42 default:
24330d27 43 return -1.0;
2d39321f 44 }
4262e0b3 45 switch (value2->type) {
2d39321f 46 case FcTypeInteger:
4262e0b3 47 v2 = (double) value2->u.i;
2d39321f
KP
48 break;
49 case FcTypeDouble:
4262e0b3 50 v2 = value2->u.d;
2d39321f
KP
51 break;
52 default:
53 return -1.0;
54 }
55 v = v2 - v1;
24330d27
KP
56 if (v < 0)
57 v = -v;
4f8b266f 58 return v;
24330d27
KP
59}
60
61static double
9ab79bdf 62FcCompareString (FcValue *v1, FcValue *v2)
24330d27 63{
1c9fdcca 64 return (double) FcStrCmpIgnoreCase (fc_value_string(v1), fc_value_string(v2)) != 0;
24330d27
KP
65}
66
82f4243f 67static double
9ab79bdf 68FcCompareFamily (FcValue *v1, FcValue *v2)
82f4243f 69{
4f8b266f
PL
70 /* rely on the guarantee in FcPatternAddWithBinding that
71 * families are always FcTypeString. */
4f8b266f
PL
72 const FcChar8* v1_string = fc_value_string(v1);
73 const FcChar8* v2_string = fc_value_string(v2);
74
a291cfc7
BE
75 if (FcToLower(*v1_string) != FcToLower(*v2_string) &&
76 *v1_string != ' ' && *v2_string != ' ')
4f8b266f
PL
77 return 1.0;
78
79 return (double) FcStrCmpIgnoreBlanksAndCase (v1_string, v2_string) != 0;
82f4243f
KP
80}
81
82static double
9ab79bdf 83FcCompareLang (FcValue *v1, FcValue *v2)
82f4243f
KP
84{
85 FcLangResult result;
4262e0b3 86 FcValue value1 = FcValueCanonicalize(v1), value2 = FcValueCanonicalize(v2);
82f4243f 87
d8d73958
KP
88 switch (value1.type) {
89 case FcTypeLangSet:
90 switch (value2.type) {
91 case FcTypeLangSet:
4262e0b3 92 result = FcLangSetCompare (value1.u.l, value2.u.l);
d8d73958
KP
93 break;
94 case FcTypeString:
4262e0b3
PL
95 result = FcLangSetHasLang (value1.u.l,
96 value2.u.s);
d8d73958
KP
97 break;
98 default:
99 return -1.0;
100 }
101 break;
102 case FcTypeString:
103 switch (value2.type) {
104 case FcTypeLangSet:
4262e0b3 105 result = FcLangSetHasLang (value2.u.l, value1.u.s);
d8d73958
KP
106 break;
107 case FcTypeString:
4262e0b3
PL
108 result = FcLangCompare (value1.u.s,
109 value2.u.s);
d8d73958
KP
110 break;
111 default:
112 return -1.0;
113 }
114 break;
115 default:
82f4243f 116 return -1.0;
d8d73958 117 }
82f4243f
KP
118 switch (result) {
119 case FcLangEqual:
120 return 0;
121 case FcLangDifferentCountry:
122 return 1;
123 case FcLangDifferentLang:
124 default:
125 return 2;
126 }
127}
128
24330d27 129static double
9ab79bdf 130FcCompareBool (FcValue *v1, FcValue *v2)
24330d27 131{
1c9fdcca 132 if (fc_storage_type(v2) != FcTypeBool || fc_storage_type(v1) != FcTypeBool)
24330d27 133 return -1.0;
1c9fdcca 134 return (double) v2->u.b != v1->u.b;
24330d27
KP
135}
136
137static double
9ab79bdf 138FcCompareCharSet (FcValue *v1, FcValue *v2)
24330d27 139{
1c9fdcca 140 return (double) FcCharSetSubtractCount (fc_value_charset(v1), fc_value_charset(v2));
24330d27
KP
141}
142
143static double
9ab79bdf 144FcCompareSize (FcValue *value1, FcValue *value2)
24330d27
KP
145{
146 double v1, v2, v;
147
4262e0b3 148 switch (value1->type) {
24330d27 149 case FcTypeInteger:
4262e0b3 150 v1 = value1->u.i;
24330d27
KP
151 break;
152 case FcTypeDouble:
4262e0b3 153 v1 = value1->u.d;
24330d27
KP
154 break;
155 default:
156 return -1;
157 }
4262e0b3 158 switch (value2->type) {
24330d27 159 case FcTypeInteger:
4262e0b3 160 v2 = value2->u.i;
24330d27
KP
161 break;
162 case FcTypeDouble:
4262e0b3 163 v2 = value2->u.d;
24330d27
KP
164 break;
165 default:
166 return -1;
167 }
168 if (v2 == 0)
169 return 0;
170 v = v2 - v1;
171 if (v < 0)
172 v = -v;
173 return v;
174}
175
4c003605 176typedef struct _FcMatcher {
7ce19673 177 FcObject object;
9ab79bdf 178 double (*compare) (FcValue *value1, FcValue *value2);
4c003605
KP
179 int strong, weak;
180} FcMatcher;
181
24330d27
KP
182/*
183 * Order is significant, it defines the precedence of
184 * each value, earlier values are more significant than
185 * later values
186 */
187static FcMatcher _FcMatchers [] = {
7ce19673 188 { FC_FOUNDRY_OBJECT, FcCompareString, 0, 0 },
4c003605 189#define MATCH_FOUNDRY 0
5cf8c536 190#define MATCH_FOUNDRY_INDEX 0
bc9469ba 191
7ce19673 192 { FC_CHARSET_OBJECT, FcCompareCharSet, 1, 1 },
4c003605 193#define MATCH_CHARSET 1
5cf8c536 194#define MATCH_CHARSET_INDEX 1
bc9469ba 195
7ce19673 196 { FC_FAMILY_OBJECT, FcCompareFamily, 2, 4 },
4c003605 197#define MATCH_FAMILY 2
5cf8c536
KP
198#define MATCH_FAMILY_STRONG_INDEX 2
199#define MATCH_FAMILY_WEAK_INDEX 4
bc9469ba 200
7ce19673 201 { FC_LANG_OBJECT, FcCompareLang, 3, 3 },
4c003605 202#define MATCH_LANG 3
5cf8c536 203#define MATCH_LANG_INDEX 3
bc9469ba 204
7ce19673 205 { FC_SPACING_OBJECT, FcCompareNumber, 5, 5 },
4c003605 206#define MATCH_SPACING 4
5cf8c536 207#define MATCH_SPACING_INDEX 5
bc9469ba 208
7ce19673 209 { FC_PIXEL_SIZE_OBJECT, FcCompareSize, 6, 6 },
4c003605 210#define MATCH_PIXEL_SIZE 5
5cf8c536 211#define MATCH_PIXEL_SIZE_INDEX 6
bc9469ba 212
7ce19673 213 { FC_STYLE_OBJECT, FcCompareString, 7, 7 },
4c003605 214#define MATCH_STYLE 6
5cf8c536 215#define MATCH_STYLE_INDEX 7
bc9469ba 216
7ce19673 217 { FC_SLANT_OBJECT, FcCompareNumber, 8, 8 },
4c003605 218#define MATCH_SLANT 7
5cf8c536 219#define MATCH_SLANT_INDEX 8
bc9469ba 220
7ce19673 221 { FC_WEIGHT_OBJECT, FcCompareNumber, 9, 9 },
4c003605 222#define MATCH_WEIGHT 8
5cf8c536 223#define MATCH_WEIGHT_INDEX 9
f534109f 224
7ce19673 225 { FC_WIDTH_OBJECT, FcCompareNumber, 10, 10 },
81fa16c3 226#define MATCH_WIDTH 9
5cf8c536 227#define MATCH_WIDTH_INDEX 10
bc9469ba 228
c2c6976d 229 { FC_DECORATIVE_OBJECT, FcCompareBool, 11, 11 },
b8082040
ES
230#define MATCH_DECORATIVE 10
231#define MATCH_DECORATIVE_INDEX 11
c2c6976d
KP
232
233 { FC_ANTIALIAS_OBJECT, FcCompareBool, 12, 12 },
c2c6976d
KP
234#define MATCH_ANTIALIAS 11
235#define MATCH_ANTIALIAS_INDEX 12
bc9469ba 236
c2c6976d
KP
237 { FC_RASTERIZER_OBJECT, FcCompareString, 13, 13 },
238#define MATCH_RASTERIZER 12
b8082040 239#define MATCH_RASTERIZER_INDEX 13
5cf8c536 240
c2c6976d
KP
241 { FC_OUTLINE_OBJECT, FcCompareBool, 14, 14 },
242#define MATCH_OUTLINE 13
243#define MATCH_OUTLINE_INDEX 14
a342e87d 244
c2c6976d
KP
245 { FC_FONTVERSION_OBJECT, FcCompareNumber, 15, 15 },
246#define MATCH_FONTVERSION 14
247#define MATCH_FONTVERSION_INDEX 15
24330d27
KP
248};
249
c2c6976d 250#define NUM_MATCH_VALUES 16
24330d27 251
aa472e5f 252static FcMatcher*
7ce19673 253FcObjectToMatcher (FcObject object)
24330d27 254{
aa472e5f 255 int i;
d854eaf8 256
bc9469ba 257 i = -1;
7ce19673
KP
258 switch (object) {
259 case FC_FOUNDRY_OBJECT:
260 i = MATCH_FOUNDRY; break;
261 case FC_FONTVERSION_OBJECT:
262 i = MATCH_FONTVERSION; break;
263 case FC_FAMILY_OBJECT:
264 i = MATCH_FAMILY; break;
265 case FC_CHARSET_OBJECT:
bc9469ba 266 i = MATCH_CHARSET; break;
7ce19673 267 case FC_ANTIALIAS_OBJECT:
bc9469ba 268 i = MATCH_ANTIALIAS; break;
7ce19673 269 case FC_LANG_OBJECT:
bc9469ba 270 i = MATCH_LANG; break;
7ce19673
KP
271 case FC_SPACING_OBJECT:
272 i = MATCH_SPACING; break;
273 case FC_STYLE_OBJECT:
274 i = MATCH_STYLE; break;
275 case FC_SLANT_OBJECT:
276 i = MATCH_SLANT; break;
277 case FC_PIXEL_SIZE_OBJECT:
bc9469ba 278 i = MATCH_PIXEL_SIZE; break;
7ce19673
KP
279 case FC_WIDTH_OBJECT:
280 i = MATCH_WIDTH; break;
281 case FC_WEIGHT_OBJECT:
282 i = MATCH_WEIGHT; break;
283 case FC_RASTERIZER_OBJECT:
bc9469ba 284 i = MATCH_RASTERIZER; break;
7ce19673 285 case FC_OUTLINE_OBJECT:
bc9469ba 286 i = MATCH_OUTLINE; break;
c2c6976d
KP
287 case FC_DECORATIVE_OBJECT:
288 i = MATCH_DECORATIVE; break;
bc9469ba 289 }
cbe1df81 290
aa472e5f 291 if (i < 0)
7ce19673 292 return NULL;
aa472e5f
PL
293
294 return _FcMatchers+i;
295}
296
297static FcBool
7ce19673 298FcCompareValueList (FcObject object,
aa472e5f
PL
299 FcValueListPtr v1orig, /* pattern */
300 FcValueListPtr v2orig, /* target */
301 FcValue *bestValue,
302 double *value,
303 FcResult *result)
304{
305 FcValueListPtr v1, v2;
aa472e5f
PL
306 double v, best, bestStrong, bestWeak;
307 int j;
7ce19673 308 FcMatcher *match = FcObjectToMatcher(object);
aa472e5f
PL
309
310 if (!match)
bc9469ba
KP
311 {
312 if (bestValue)
7ce19673 313 *bestValue = FcValueCanonicalize(&v2orig->value);
bc9469ba
KP
314 return FcTrue;
315 }
aa472e5f 316
24330d27 317 best = 1e99;
4c003605
KP
318 bestStrong = 1e99;
319 bestWeak = 1e99;
a5a384c5 320 j = 1;
7ce19673 321 for (v1 = v1orig; v1; v1 = FcValueListNext(v1))
24330d27 322 {
09f9f6f6 323 for (v2 = v2orig; v2; v2 = FcValueListNext(v2))
24330d27 324 {
7ce19673 325 v = (match->compare) (&v1->value, &v2->value);
24330d27
KP
326 if (v < 0)
327 {
328 *result = FcResultTypeMismatch;
329 return FcFalse;
330 }
c7641f28 331 v = v * 1000 + j;
24330d27
KP
332 if (v < best)
333 {
334 if (bestValue)
7ce19673 335 *bestValue = FcValueCanonicalize(&v2->value);
24330d27
KP
336 best = v;
337 }
7ce19673 338 if (v1->binding == FcValueBindingStrong)
4c003605
KP
339 {
340 if (v < bestStrong)
341 bestStrong = v;
342 }
343 else
344 {
345 if (v < bestWeak)
346 bestWeak = v;
347 }
24330d27
KP
348 }
349 j++;
350 }
351 if (FcDebug () & FC_DBG_MATCHV)
352 {
7ce19673 353 printf (" %s: %g ", FcObjectName (object), best);
24330d27
KP
354 FcValueListPrint (v1orig);
355 printf (", ");
356 FcValueListPrint (v2orig);
357 printf ("\n");
358 }
d0f07b8d 359 if (value)
4c003605 360 {
aa472e5f
PL
361 int weak = match->weak;
362 int strong = match->strong;
4c003605
KP
363 if (weak == strong)
364 value[strong] += best;
365 else
366 {
367 value[weak] += bestWeak;
368 value[strong] += bestStrong;
369 }
370 }
24330d27
KP
371 return FcTrue;
372}
373
374/*
375 * Return a value indicating the distance between the two lists of
376 * values
377 */
378
379static FcBool
380FcCompare (FcPattern *pat,
381 FcPattern *fnt,
382 double *value,
383 FcResult *result)
384{
385 int i, i1, i2;
386
4c003605 387 for (i = 0; i < NUM_MATCH_VALUES; i++)
24330d27
KP
388 value[i] = 0.0;
389
bc9469ba
KP
390 i1 = 0;
391 i2 = 0;
392 while (i1 < pat->num && i2 < fnt->num)
24330d27 393 {
7ce19673
KP
394 FcPatternElt *elt_i1 = &FcPatternElts(pat)[i1];
395 FcPatternElt *elt_i2 = &FcPatternElts(fnt)[i2];
9ab79bdf 396
7ce19673 397 i = FcObjectCompare(elt_i1->object, elt_i2->object);
bc9469ba
KP
398 if (i > 0)
399 i2++;
400 else if (i < 0)
401 i1++;
402 else
24330d27 403 {
d854eaf8 404 if (!FcCompareValueList (elt_i1->object,
7ce19673
KP
405 FcPatternEltValues(elt_i1),
406 FcPatternEltValues(elt_i2),
9ab79bdf 407 0, value, result))
bc9469ba
KP
408 return FcFalse;
409 i1++;
410 i2++;
24330d27 411 }
bc9469ba
KP
412 }
413 return FcTrue;
24330d27
KP
414}
415
216fac98
KP
416FcPattern *
417FcFontRenderPrepare (FcConfig *config,
418 FcPattern *pat,
419 FcPattern *font)
420{
421 FcPattern *new;
422 int i;
423 FcPatternElt *fe, *pe;
424 FcValue v;
216fac98
KP
425 FcResult result;
426
427 new = FcPatternCreate ();
428 if (!new)
429 return 0;
430 for (i = 0; i < font->num; i++)
431 {
7ce19673
KP
432 fe = &FcPatternElts(font)[i];
433 pe = FcPatternObjectFindElt (pat, fe->object);
216fac98
KP
434 if (pe)
435 {
7ce19673
KP
436 if (!FcCompareValueList (pe->object, FcPatternEltValues(pe),
437 FcPatternEltValues(fe), &v, 0, &result))
216fac98
KP
438 {
439 FcPatternDestroy (new);
440 return 0;
441 }
442 }
443 else
7ce19673
KP
444 v = FcValueCanonicalize(&FcPatternEltValues (fe)->value);
445 FcPatternObjectAdd (new, fe->object, v, FcFalse);
216fac98
KP
446 }
447 for (i = 0; i < pat->num; i++)
448 {
7ce19673
KP
449 pe = &FcPatternElts(pat)[i];
450 fe = FcPatternObjectFindElt (font, pe->object);
216fac98 451 if (!fe)
7ce19673
KP
452 {
453 v = FcValueCanonicalize(&FcPatternEltValues(pe)->value);
454 FcPatternObjectAdd (new, pe->object, v, FcTrue);
455 }
216fac98 456 }
793154ed 457
fa244f3d 458 FcConfigSubstituteWithPat (config, new, pat, FcMatchFont);
216fac98
KP
459 return new;
460}
461
6d764a3f
BE
462static FcPattern *
463FcFontSetMatchInternal (FcConfig *config,
464 FcFontSet **sets,
465 int nsets,
466 FcPattern *p,
467 FcResult *result)
24330d27 468{
2a9179d8 469 double score[NUM_MATCH_VALUES], bestscore[NUM_MATCH_VALUES];
24330d27
KP
470 int f;
471 FcFontSet *s;
472 FcPattern *best;
2a9179d8 473 int i;
216fac98 474 int set;
24330d27 475
2a9179d8
KP
476 for (i = 0; i < NUM_MATCH_VALUES; i++)
477 bestscore[i] = 0;
478 best = 0;
24330d27
KP
479 if (FcDebug () & FC_DBG_MATCH)
480 {
481 printf ("Match ");
482 FcPatternPrint (p);
483 }
2a9179d8 484 for (set = 0; set < nsets; set++)
24330d27 485 {
2a9179d8
KP
486 s = sets[set];
487 if (!s)
24330d27 488 continue;
2a9179d8 489 for (f = 0; f < s->nfont; f++)
5576a587 490 {
5576a587
PL
491 if (FcDebug () & FC_DBG_MATCHV)
492 {
2a9179d8
KP
493 printf ("Font %d ", f);
494 FcPatternPrint (s->fonts[f]);
24330d27 495 }
2a9179d8
KP
496 if (!FcCompare (p, s->fonts[f], score, result))
497 return 0;
498 if (FcDebug () & FC_DBG_MATCHV)
5576a587 499 {
2a9179d8
KP
500 printf ("Score");
501 for (i = 0; i < NUM_MATCH_VALUES; i++)
5576a587 502 {
2a9179d8 503 printf (" %g", score[i]);
24330d27 504 }
2a9179d8 505 printf ("\n");
5576a587 506 }
2a9179d8 507 for (i = 0; i < NUM_MATCH_VALUES; i++)
5576a587 508 {
2a9179d8
KP
509 if (best && bestscore[i] < score[i])
510 break;
511 if (!best || score[i] < bestscore[i])
512 {
513 for (i = 0; i < NUM_MATCH_VALUES; i++)
514 bestscore[i] = score[i];
515 best = s->fonts[f];
516 break;
517 }
5576a587
PL
518 }
519 }
24330d27 520 }
2a9179d8 521 if (FcDebug () & FC_DBG_MATCH)
1ed98a0c 522 {
2a9179d8
KP
523 printf ("Best score");
524 for (i = 0; i < NUM_MATCH_VALUES; i++)
525 printf (" %g", bestscore[i]);
1b43ccc8 526 printf ("\n");
1ed98a0c
PL
527 FcPatternPrint (best);
528 }
24330d27
KP
529 if (!best)
530 {
531 *result = FcResultNoMatch;
532 return 0;
533 }
6d764a3f
BE
534 return best;
535}
536
537FcPattern *
538FcFontSetMatch (FcConfig *config,
539 FcFontSet **sets,
540 int nsets,
541 FcPattern *p,
542 FcResult *result)
543{
544 FcPattern *best;
545
546 if (!config)
547 {
548 config = FcConfigGetCurrent ();
549 if (!config)
550 return 0;
551 }
552 best = FcFontSetMatchInternal (config, sets, nsets, p, result);
216fac98 553 return FcFontRenderPrepare (config, p, best);
24330d27 554}
80c053b7
KP
555
556FcPattern *
557FcFontMatch (FcConfig *config,
558 FcPattern *p,
559 FcResult *result)
560{
561 FcFontSet *sets[2];
562 int nsets;
6d764a3f 563 FcPattern *best;
80c053b7
KP
564
565 if (!config)
566 {
567 config = FcConfigGetCurrent ();
568 if (!config)
569 return 0;
570 }
571 nsets = 0;
572 if (config->fonts[FcSetSystem])
573 sets[nsets++] = config->fonts[FcSetSystem];
574 if (config->fonts[FcSetApplication])
575 sets[nsets++] = config->fonts[FcSetApplication];
6d764a3f
BE
576
577 best = FcFontSetMatchInternal (config, sets, nsets, p, result);
578 return FcFontRenderPrepare (config, p, best);
80c053b7 579}
216fac98 580
216fac98 581typedef struct _FcSortNode {
216fac98 582 FcPattern *pattern;
4c003605 583 double score[NUM_MATCH_VALUES];
216fac98
KP
584} FcSortNode;
585
0ab36ca8
KP
586static int
587FcSortCompare (const void *aa, const void *ab)
216fac98 588{
0ab36ca8
KP
589 FcSortNode *a = *(FcSortNode **) aa;
590 FcSortNode *b = *(FcSortNode **) ab;
bc9469ba
KP
591 double *as = &a->score[0];
592 double *bs = &b->score[0];
88c747e2 593 double ad = 0, bd = 0;
0ab36ca8 594 int i;
216fac98 595
4c003605 596 i = NUM_MATCH_VALUES;
bc9469ba
KP
597 while (i-- && (ad = *as++) == (bd = *bs++))
598 ;
599 return ad < bd ? -1 : ad > bd ? 1 : 0;
216fac98
KP
600}
601
602static FcBool
2f02e383 603FcSortWalk (FcSortNode **n, int nnode, FcFontSet *fs, FcCharSet **cs, FcBool trim, FcBool build_cs)
216fac98
KP
604{
605 FcCharSet *ncs;
0ab36ca8
KP
606 FcSortNode *node;
607
608 while (nnode--)
216fac98 609 {
0ab36ca8 610 node = *n++;
355ed50b
BE
611
612 /*
613 * Only fetch node charset if we'd need it
614 */
615 if (trim || build_cs)
616 {
617 if (FcPatternGetCharSet (node->pattern, FC_CHARSET, 0, &ncs) !=
618 FcResultMatch)
619 continue;
620 }
621
622 /*
623 * If this font isn't a subset of the previous fonts,
624 * add it to the list
625 */
626 if (!trim || !*cs || !FcCharSetIsSubset (ncs, *cs))
216fac98 627 {
355ed50b 628 if (trim || build_cs)
216fac98 629 {
355ed50b
BE
630 *cs = FcCharSetMerge (*cs, ncs);
631 if (*cs == NULL)
216fac98 632 return FcFalse;
355ed50b
BE
633 }
634
635 FcPatternReference (node->pattern);
636 if (FcDebug () & FC_DBG_MATCHV)
637 {
638 printf ("Add ");
639 FcPatternPrint (node->pattern);
640 }
641 if (!FcFontSetAdd (fs, node->pattern))
642 {
643 FcPatternDestroy (node->pattern);
644 return FcFalse;
216fac98 645 }
216fac98
KP
646 }
647 }
216fac98
KP
648 return FcTrue;
649}
650
1412a699
KP
651void
652FcFontSetSortDestroy (FcFontSet *fs)
653{
1412a699
KP
654 FcFontSetDestroy (fs);
655}
656
216fac98
KP
657FcFontSet *
658FcFontSetSort (FcConfig *config,
659 FcFontSet **sets,
660 int nsets,
661 FcPattern *p,
662 FcBool trim,
663 FcCharSet **csp,
664 FcResult *result)
665{
666 FcFontSet *ret;
667 FcFontSet *s;
668 FcSortNode *nodes;
0ab36ca8 669 FcSortNode **nodeps, **nodep;
216fac98 670 int nnodes;
216fac98
KP
671 FcSortNode *new;
672 FcCharSet *cs;
673 int set;
674 int f;
675 int i;
5cf8c536
KP
676 int nPatternLang;
677 FcBool *patternLangSat;
678 FcValue patternLang;
216fac98 679
82f4243f
KP
680 if (FcDebug () & FC_DBG_MATCH)
681 {
682 printf ("Sort ");
683 FcPatternPrint (p);
684 }
216fac98
KP
685 nnodes = 0;
686 for (set = 0; set < nsets; set++)
687 {
688 s = sets[set];
689 if (!s)
690 continue;
691 nnodes += s->nfont;
692 }
693 if (!nnodes)
694 goto bail0;
5cf8c536
KP
695
696 for (nPatternLang = 0;
697 FcPatternGet (p, FC_LANG, nPatternLang, &patternLang) == FcResultMatch;
698 nPatternLang++)
699 ;
700
9dac3c59 701 /* freed below */
5cf8c536
KP
702 nodes = malloc (nnodes * sizeof (FcSortNode) +
703 nnodes * sizeof (FcSortNode *) +
704 nPatternLang * sizeof (FcBool));
216fac98
KP
705 if (!nodes)
706 goto bail0;
1412a699 707 nodeps = (FcSortNode **) (nodes + nnodes);
5cf8c536 708 patternLangSat = (FcBool *) (nodeps + nnodes);
216fac98 709
216fac98 710 new = nodes;
0ab36ca8 711 nodep = nodeps;
216fac98
KP
712 for (set = 0; set < nsets; set++)
713 {
714 s = sets[set];
715 if (!s)
716 continue;
717 for (f = 0; f < s->nfont; f++)
718 {
719 if (FcDebug () & FC_DBG_MATCHV)
720 {
721 printf ("Font %d ", f);
722 FcPatternPrint (s->fonts[f]);
723 }
724 new->pattern = s->fonts[f];
725 if (!FcCompare (p, new->pattern, new->score, result))
726 goto bail1;
727 if (FcDebug () & FC_DBG_MATCHV)
728 {
729 printf ("Score");
4c003605 730 for (i = 0; i < NUM_MATCH_VALUES; i++)
216fac98
KP
731 {
732 printf (" %g", new->score[i]);
733 }
734 printf ("\n");
735 }
0ab36ca8 736 *nodep = new;
216fac98 737 new++;
0ab36ca8 738 nodep++;
216fac98
KP
739 }
740 }
741
0ab36ca8
KP
742 nnodes = new - nodes;
743
1412a699 744 qsort (nodeps, nnodes, sizeof (FcSortNode *),
0ab36ca8 745 FcSortCompare);
5cf8c536
KP
746
747 for (i = 0; i < nPatternLang; i++)
748 patternLangSat[i] = FcFalse;
749
750 for (f = 0; f < nnodes; f++)
751 {
752 FcBool satisfies = FcFalse;
753 /*
754 * If this node matches any language, go check
755 * which ones and satisfy those entries
756 */
3bb1812f 757 if (nodeps[f]->score[MATCH_LANG_INDEX] < 200)
5cf8c536
KP
758 {
759 for (i = 0; i < nPatternLang; i++)
760 {
761 FcValue nodeLang;
762
763 if (!patternLangSat[i] &&
764 FcPatternGet (p, FC_LANG, i, &patternLang) == FcResultMatch &&
765 FcPatternGet (nodeps[f]->pattern, FC_LANG, 0, &nodeLang) == FcResultMatch)
766 {
9ab79bdf 767 double compare = FcCompareLang (&patternLang, &nodeLang);
5cf8c536
KP
768 if (compare >= 0 && compare < 2)
769 {
770 if (FcDebug () & FC_DBG_MATCHV)
771 {
772 FcChar8 *family;
773 FcChar8 *style;
774
775 if (FcPatternGetString (nodeps[f]->pattern, FC_FAMILY, 0, &family) == FcResultMatch &&
776 FcPatternGetString (nodeps[f]->pattern, FC_STYLE, 0, &style) == FcResultMatch)
777 printf ("Font %s:%s matches language %d\n", family, style, i);
778 }
779 patternLangSat[i] = FcTrue;
780 satisfies = FcTrue;
781 break;
782 }
783 }
784 }
785 }
786 if (!satisfies)
c7641f28 787 nodeps[f]->score[MATCH_LANG_INDEX] = 10000.0;
5cf8c536
KP
788 }
789
790 /*
791 * Re-sort once the language issues have been settled
792 */
793 qsort (nodeps, nnodes, sizeof (FcSortNode *),
794 FcSortCompare);
0ab36ca8 795
216fac98
KP
796 ret = FcFontSetCreate ();
797 if (!ret)
798 goto bail1;
799
800 cs = 0;
801
2f02e383 802 if (!FcSortWalk (nodeps, nnodes, ret, &cs, trim, (csp!=0)))
216fac98
KP
803 goto bail2;
804
d0f07b8d
KP
805 if (csp)
806 *csp = cs;
807 else
2f02e383
PL
808 {
809 if (cs)
810 FcCharSetDestroy (cs);
811 }
216fac98 812
1412a699
KP
813 free (nodes);
814
09f9f6f6
KP
815 if (FcDebug() & FC_DBG_MATCH)
816 {
817 printf ("First font ");
818 FcPatternPrint (ret->fonts[0]);
819 }
216fac98
KP
820 return ret;
821
822bail2:
823 if (cs)
824 FcCharSetDestroy (cs);
825 FcFontSetDestroy (ret);
826bail1:
827 free (nodes);
828bail0:
829 return 0;
830}
20ac65ab
KP
831
832FcFontSet *
833FcFontSort (FcConfig *config,
834 FcPattern *p,
835 FcBool trim,
836 FcCharSet **csp,
837 FcResult *result)
838{
839 FcFontSet *sets[2];
840 int nsets;
841
842 if (!config)
843 {
844 config = FcConfigGetCurrent ();
845 if (!config)
846 return 0;
847 }
848 nsets = 0;
849 if (config->fonts[FcSetSystem])
850 sets[nsets++] = config->fonts[FcSetSystem];
851 if (config->fonts[FcSetApplication])
852 sets[nsets++] = config->fonts[FcSetApplication];
853 return FcFontSetSort (config, sets, nsets, p, trim, csp, result);
854}
23816bf9
KP
855#define __fcmatch__
856#include "fcaliastail.h"
857#undef __fcmatch__