2 * $RCSId: xc/lib/fontconfig/src/fcmatch.c,v 1.20 2002/08/31 22:17:32 keithp Exp $
4 * Copyright © 2000 Keith Packard
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.
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.
31 FcCompareNumber (FcValue *value1, FcValue *value2)
35 switch (value1->type) {
37 v1 = (double) value1->u.i;
45 switch (value2->type) {
47 v2 = (double) value2->u.i;
62 FcCompareString (FcValue *v1, FcValue *v2)
64 return (double) FcStrCmpIgnoreCase (fc_value_string(v1), fc_value_string(v2)) != 0;
68 FcCompareFamily (FcValue *v1, FcValue *v2)
70 /* rely on the guarantee in FcPatternAddWithBinding that
71 * families are always FcTypeString. */
72 const FcChar8* v1_string = fc_value_string(v1);
73 const FcChar8* v2_string = fc_value_string(v2);
75 if (FcToLower(*v1_string) != FcToLower(*v2_string))
78 return (double) FcStrCmpIgnoreBlanksAndCase (v1_string, v2_string) != 0;
82 FcCompareLang (FcValue *v1, FcValue *v2)
85 FcValue value1 = FcValueCanonicalize(v1), value2 = FcValueCanonicalize(v2);
87 switch (value1.type) {
89 switch (value2.type) {
91 result = FcLangSetCompare (value1.u.l, value2.u.l);
94 result = FcLangSetHasLang (value1.u.l,
102 switch (value2.type) {
104 result = FcLangSetHasLang (value2.u.l, value1.u.s);
107 result = FcLangCompare (value1.u.s,
120 case FcLangDifferentCountry:
122 case FcLangDifferentLang:
129 FcCompareBool (FcValue *v1, FcValue *v2)
131 if (fc_storage_type(v2) != FcTypeBool || fc_storage_type(v1) != FcTypeBool)
133 return (double) v2->u.b != v1->u.b;
137 FcCompareCharSet (FcValue *v1, FcValue *v2)
139 return (double) FcCharSetSubtractCount (fc_value_charset(v1), fc_value_charset(v2));
143 FcCompareSize (FcValue *value1, FcValue *value2)
147 switch (value1->type) {
157 switch (value2->type) {
175 typedef struct _FcMatcher {
177 double (*compare) (FcValue *value1, FcValue *value2);
182 * Order is significant, it defines the precedence of
183 * each value, earlier values are more significant than
186 static FcMatcher _FcMatchers [] = {
187 { FC_FOUNDRY_OBJECT, FcCompareString, 0, 0 },
188 #define MATCH_FOUNDRY 0
189 #define MATCH_FOUNDRY_INDEX 0
191 { FC_CHARSET_OBJECT, FcCompareCharSet, 1, 1 },
192 #define MATCH_CHARSET 1
193 #define MATCH_CHARSET_INDEX 1
195 { FC_FAMILY_OBJECT, FcCompareFamily, 2, 4 },
196 #define MATCH_FAMILY 2
197 #define MATCH_FAMILY_STRONG_INDEX 2
198 #define MATCH_FAMILY_WEAK_INDEX 4
200 { FC_LANG_OBJECT, FcCompareLang, 3, 3 },
202 #define MATCH_LANG_INDEX 3
204 { FC_SPACING_OBJECT, FcCompareNumber, 5, 5 },
205 #define MATCH_SPACING 4
206 #define MATCH_SPACING_INDEX 5
208 { FC_PIXEL_SIZE_OBJECT, FcCompareSize, 6, 6 },
209 #define MATCH_PIXEL_SIZE 5
210 #define MATCH_PIXEL_SIZE_INDEX 6
212 { FC_STYLE_OBJECT, FcCompareString, 7, 7 },
213 #define MATCH_STYLE 6
214 #define MATCH_STYLE_INDEX 7
216 { FC_SLANT_OBJECT, FcCompareNumber, 8, 8 },
217 #define MATCH_SLANT 7
218 #define MATCH_SLANT_INDEX 8
220 { FC_WEIGHT_OBJECT, FcCompareNumber, 9, 9 },
221 #define MATCH_WEIGHT 8
222 #define MATCH_WEIGHT_INDEX 9
224 { FC_WIDTH_OBJECT, FcCompareNumber, 10, 10 },
225 #define MATCH_WIDTH 9
226 #define MATCH_WIDTH_INDEX 10
228 { FC_DECORATIVE_OBJECT, FcCompareBool, 11, 11 },
229 #define MATCH_DECORATIVE 11
230 #define MATCH_DECORATIVE_INDEX 12
232 { FC_ANTIALIAS_OBJECT, FcCompareBool, 12, 12 },
234 #define MATCH_ANTIALIAS 11
235 #define MATCH_ANTIALIAS_INDEX 12
237 { FC_RASTERIZER_OBJECT, FcCompareString, 13, 13 },
238 #define MATCH_RASTERIZER 12
239 #define MATCH_RASTERIZER_INDEX 12
241 { FC_OUTLINE_OBJECT, FcCompareBool, 14, 14 },
242 #define MATCH_OUTLINE 13
243 #define MATCH_OUTLINE_INDEX 14
245 { FC_FONTVERSION_OBJECT, FcCompareNumber, 15, 15 },
246 #define MATCH_FONTVERSION 14
247 #define MATCH_FONTVERSION_INDEX 15
250 #define NUM_MATCH_VALUES 16
253 FcObjectToMatcher (FcObject object)
259 case FC_FOUNDRY_OBJECT:
260 i = MATCH_FOUNDRY; break;
261 case FC_FONTVERSION_OBJECT:
262 i = MATCH_FONTVERSION; break;
263 case FC_FAMILY_OBJECT:
264 i = MATCH_FAMILY; break;
265 case FC_CHARSET_OBJECT:
266 i = MATCH_CHARSET; break;
267 case FC_ANTIALIAS_OBJECT:
268 i = MATCH_ANTIALIAS; break;
270 i = MATCH_LANG; break;
271 case FC_SPACING_OBJECT:
272 i = MATCH_SPACING; break;
273 case FC_STYLE_OBJECT:
274 i = MATCH_STYLE; break;
275 case FC_SLANT_OBJECT:
276 i = MATCH_SLANT; break;
277 case FC_PIXEL_SIZE_OBJECT:
278 i = MATCH_PIXEL_SIZE; break;
279 case FC_WIDTH_OBJECT:
280 i = MATCH_WIDTH; break;
281 case FC_WEIGHT_OBJECT:
282 i = MATCH_WEIGHT; break;
283 case FC_RASTERIZER_OBJECT:
284 i = MATCH_RASTERIZER; break;
285 case FC_OUTLINE_OBJECT:
286 i = MATCH_OUTLINE; break;
287 case FC_DECORATIVE_OBJECT:
288 i = MATCH_DECORATIVE; break;
294 return _FcMatchers+i;
298 FcCompareValueList (FcObject object,
299 FcValueListPtr v1orig, /* pattern */
300 FcValueListPtr v2orig, /* target */
305 FcValueListPtr v1, v2;
306 double v, best, bestStrong, bestWeak;
308 FcMatcher *match = FcObjectToMatcher(object);
313 *bestValue = FcValueCanonicalize(&v2orig->value);
321 for (v1 = v1orig; v1; v1 = FcValueListNext(v1))
323 for (v2 = v2orig; v2; v2 = FcValueListNext(v2))
325 v = (match->compare) (&v1->value, &v2->value);
328 *result = FcResultTypeMismatch;
335 *bestValue = FcValueCanonicalize(&v2->value);
338 if (v1->binding == FcValueBindingStrong)
351 if (FcDebug () & FC_DBG_MATCHV)
353 printf (" %s: %g ", FcObjectName (object), best);
354 FcValueListPrint (v1orig);
356 FcValueListPrint (v2orig);
361 int weak = match->weak;
362 int strong = match->strong;
364 value[strong] += best;
367 value[weak] += bestWeak;
368 value[strong] += bestStrong;
375 * Return a value indicating the distance between the two lists of
380 FcCompare (FcPattern *pat,
387 for (i = 0; i < NUM_MATCH_VALUES; i++)
392 while (i1 < pat->num && i2 < fnt->num)
394 FcPatternElt *elt_i1 = &FcPatternElts(pat)[i1];
395 FcPatternElt *elt_i2 = &FcPatternElts(fnt)[i2];
397 i = FcObjectCompare(elt_i1->object, elt_i2->object);
404 if (!FcCompareValueList (elt_i1->object,
405 FcPatternEltValues(elt_i1),
406 FcPatternEltValues(elt_i2),
417 FcFontRenderPrepare (FcConfig *config,
423 FcPatternElt *fe, *pe;
427 new = FcPatternCreate ();
430 for (i = 0; i < font->num; i++)
432 fe = &FcPatternElts(font)[i];
433 pe = FcPatternObjectFindElt (pat, fe->object);
436 if (!FcCompareValueList (pe->object, FcPatternEltValues(pe),
437 FcPatternEltValues(fe), &v, 0, &result))
439 FcPatternDestroy (new);
444 v = FcValueCanonicalize(&FcPatternEltValues (fe)->value);
445 FcPatternObjectAdd (new, fe->object, v, FcFalse);
447 for (i = 0; i < pat->num; i++)
449 pe = &FcPatternElts(pat)[i];
450 fe = FcPatternObjectFindElt (font, pe->object);
453 v = FcValueCanonicalize(&FcPatternEltValues(pe)->value);
454 FcPatternObjectAdd (new, pe->object, v, FcTrue);
458 FcConfigSubstituteWithPat (config, new, pat, FcMatchFont);
463 FcFontSetMatch (FcConfig *config,
469 double score[NUM_MATCH_VALUES], bestscore[NUM_MATCH_VALUES];
476 for (i = 0; i < NUM_MATCH_VALUES; i++)
479 if (FcDebug () & FC_DBG_MATCH)
486 config = FcConfigGetCurrent ();
490 for (set = 0; set < nsets; set++)
495 for (f = 0; f < s->nfont; f++)
497 if (FcDebug () & FC_DBG_MATCHV)
499 printf ("Font %d ", f);
500 FcPatternPrint (s->fonts[f]);
502 if (!FcCompare (p, s->fonts[f], score, result))
504 if (FcDebug () & FC_DBG_MATCHV)
507 for (i = 0; i < NUM_MATCH_VALUES; i++)
509 printf (" %g", score[i]);
513 for (i = 0; i < NUM_MATCH_VALUES; i++)
515 if (best && bestscore[i] < score[i])
517 if (!best || score[i] < bestscore[i])
519 for (i = 0; i < NUM_MATCH_VALUES; i++)
520 bestscore[i] = score[i];
527 if (FcDebug () & FC_DBG_MATCH)
529 printf ("Best score");
530 for (i = 0; i < NUM_MATCH_VALUES; i++)
531 printf (" %g", bestscore[i]);
532 FcPatternPrint (best);
536 *result = FcResultNoMatch;
539 return FcFontRenderPrepare (config, p, best);
543 FcFontMatch (FcConfig *config,
552 config = FcConfigGetCurrent ();
557 if (config->fonts[FcSetSystem])
558 sets[nsets++] = config->fonts[FcSetSystem];
559 if (config->fonts[FcSetApplication])
560 sets[nsets++] = config->fonts[FcSetApplication];
561 return FcFontSetMatch (config, sets, nsets, p, result);
564 typedef struct _FcSortNode {
566 double score[NUM_MATCH_VALUES];
570 FcSortCompare (const void *aa, const void *ab)
572 FcSortNode *a = *(FcSortNode **) aa;
573 FcSortNode *b = *(FcSortNode **) ab;
574 double *as = &a->score[0];
575 double *bs = &b->score[0];
576 double ad = 0, bd = 0;
579 i = NUM_MATCH_VALUES;
580 while (i-- && (ad = *as++) == (bd = *bs++))
582 return ad < bd ? -1 : ad > bd ? 1 : 0;
586 FcSortWalk (FcSortNode **n, int nnode, FcFontSet *fs, FcCharSet **cs, FcBool trim, FcBool build_cs)
594 if (FcPatternGetCharSet (node->pattern, FC_CHARSET, 0, &ncs) ==
598 * If this font isn't a subset of the previous fonts,
601 if (!trim || !*cs || !FcCharSetIsSubset (ncs, *cs))
603 if (trim || build_cs)
607 ncs = FcCharSetUnion (ncs, *cs);
610 FcCharSetDestroy (*cs);
613 ncs = FcCharSetCopy (ncs);
617 FcPatternReference (node->pattern);
618 if (FcDebug () & FC_DBG_MATCHV)
621 FcPatternPrint (node->pattern);
623 if (!FcFontSetAdd (fs, node->pattern))
625 FcPatternDestroy (node->pattern);
635 FcFontSetSortDestroy (FcFontSet *fs)
637 FcFontSetDestroy (fs);
641 FcFontSetSort (FcConfig *config,
652 FcSortNode **nodeps, **nodep;
660 FcBool *patternLangSat;
663 if (FcDebug () & FC_DBG_MATCH)
669 for (set = 0; set < nsets; set++)
679 for (nPatternLang = 0;
680 FcPatternGet (p, FC_LANG, nPatternLang, &patternLang) == FcResultMatch;
685 nodes = malloc (nnodes * sizeof (FcSortNode) +
686 nnodes * sizeof (FcSortNode *) +
687 nPatternLang * sizeof (FcBool));
690 nodeps = (FcSortNode **) (nodes + nnodes);
691 patternLangSat = (FcBool *) (nodeps + nnodes);
695 for (set = 0; set < nsets; set++)
700 for (f = 0; f < s->nfont; f++)
702 if (FcDebug () & FC_DBG_MATCHV)
704 printf ("Font %d ", f);
705 FcPatternPrint (s->fonts[f]);
707 new->pattern = s->fonts[f];
708 if (!FcCompare (p, new->pattern, new->score, result))
710 if (FcDebug () & FC_DBG_MATCHV)
713 for (i = 0; i < NUM_MATCH_VALUES; i++)
715 printf (" %g", new->score[i]);
725 nnodes = new - nodes;
727 qsort (nodeps, nnodes, sizeof (FcSortNode *),
730 for (i = 0; i < nPatternLang; i++)
731 patternLangSat[i] = FcFalse;
733 for (f = 0; f < nnodes; f++)
735 FcBool satisfies = FcFalse;
737 * If this node matches any language, go check
738 * which ones and satisfy those entries
740 if (nodeps[f]->score[MATCH_LANG_INDEX] < 200)
742 for (i = 0; i < nPatternLang; i++)
746 if (!patternLangSat[i] &&
747 FcPatternGet (p, FC_LANG, i, &patternLang) == FcResultMatch &&
748 FcPatternGet (nodeps[f]->pattern, FC_LANG, 0, &nodeLang) == FcResultMatch)
750 double compare = FcCompareLang (&patternLang, &nodeLang);
751 if (compare >= 0 && compare < 2)
753 if (FcDebug () & FC_DBG_MATCHV)
758 if (FcPatternGetString (nodeps[f]->pattern, FC_FAMILY, 0, &family) == FcResultMatch &&
759 FcPatternGetString (nodeps[f]->pattern, FC_STYLE, 0, &style) == FcResultMatch)
760 printf ("Font %s:%s matches language %d\n", family, style, i);
762 patternLangSat[i] = FcTrue;
770 nodeps[f]->score[MATCH_LANG_INDEX] = 1000.0;
774 * Re-sort once the language issues have been settled
776 qsort (nodeps, nnodes, sizeof (FcSortNode *),
779 ret = FcFontSetCreate ();
785 if (!FcSortWalk (nodeps, nnodes, ret, &cs, trim, (csp!=0)))
793 FcCharSetDestroy (cs);
798 if (FcDebug() & FC_DBG_MATCH)
800 printf ("First font ");
801 FcPatternPrint (ret->fonts[0]);
807 FcCharSetDestroy (cs);
808 FcFontSetDestroy (ret);
816 FcFontSort (FcConfig *config,
827 config = FcConfigGetCurrent ();
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);
839 #include "fcaliastail.h"