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