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