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