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