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 (const char *object, 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 (const char *object, FcValue *v1, FcValue *v2)
64 return (double) FcStrCmpIgnoreCase (fc_value_string(v1), fc_value_string(v2)) != 0;
68 FcCompareFamily (const char *object, 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 (const char *object, 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 (const char *object, 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 (const char *object, FcValue *v1, FcValue *v2)
139 return (double) FcCharSetSubtractCount (fc_value_charset(v1), fc_value_charset(v2));
143 FcCompareSize (const char *object, FcValue *value1, FcValue *value2)
147 switch (value1->type) {
157 switch (value2->type) {
175 typedef struct _FcMatcher {
177 double (*compare) (const char *object, 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, FcCompareString, 0, 0 },
188 #define MATCH_FOUNDRY 0
189 #define MATCH_FOUNDRY_INDEX 0
191 { FC_CHARSET, FcCompareCharSet, 1, 1 },
192 #define MATCH_CHARSET 1
193 #define MATCH_CHARSET_INDEX 1
195 { FC_FAMILY, 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, FcCompareLang, 3, 3 },
202 #define MATCH_LANG_INDEX 3
204 { FC_SPACING, FcCompareNumber, 5, 5 },
205 #define MATCH_SPACING 4
206 #define MATCH_SPACING_INDEX 5
208 { FC_PIXEL_SIZE, FcCompareSize, 6, 6 },
209 #define MATCH_PIXEL_SIZE 5
210 #define MATCH_PIXEL_SIZE_INDEX 6
212 { FC_STYLE, FcCompareString, 7, 7 },
213 #define MATCH_STYLE 6
214 #define MATCH_STYLE_INDEX 7
216 { FC_SLANT, FcCompareNumber, 8, 8 },
217 #define MATCH_SLANT 7
218 #define MATCH_SLANT_INDEX 8
220 { FC_WEIGHT, FcCompareNumber, 9, 9 },
221 #define MATCH_WEIGHT 8
222 #define MATCH_WEIGHT_INDEX 9
224 { FC_WIDTH, FcCompareNumber, 10, 10 },
225 #define MATCH_WIDTH 9
226 #define MATCH_WIDTH_INDEX 10
228 { FC_ANTIALIAS, FcCompareBool, 11, 11 },
229 #define MATCH_ANTIALIAS 10
230 #define MATCH_ANTIALIAS_INDEX 11
232 { FC_RASTERIZER, FcCompareString, 12, 12 },
233 #define MATCH_RASTERIZER 11
234 #define MATCH_RASTERIZER_INDEX 12
236 { FC_OUTLINE, FcCompareBool, 13, 13 },
237 #define MATCH_OUTLINE 12
238 #define MATCH_OUTLINE_INDEX 13
240 { FC_FONTVERSION, FcCompareNumber, 14, 14 },
241 #define MATCH_FONTVERSION 13
242 #define MATCH_FONTVERSION_INDEX 14
245 #define NUM_MATCH_VALUES 15
248 FcCompareValueList (const char *object,
249 FcValueListPtr v1orig, /* pattern */
250 FcValueListPtr v2orig, /* target */
255 FcValueListPtr v1, v2;
256 FcValueList *v1_ptrU, *v2_ptrU;
257 double v, best, bestStrong, bestWeak;
262 * Locate the possible matching entry by examining the
263 * first few characters in object
266 switch (FcToLower (object[0])) {
268 switch (FcToLower (object[1])) {
270 switch (FcToLower (object[2])) {
272 i = MATCH_FOUNDRY; break;
274 i = MATCH_FONTVERSION; break;
278 i = MATCH_FAMILY; break;
282 i = MATCH_CHARSET; break;
284 i = MATCH_ANTIALIAS; break;
286 i = MATCH_LANG; break;
288 switch (FcToLower (object[1])) {
290 i = MATCH_SPACING; break;
292 i = MATCH_STYLE; break;
294 i = MATCH_SLANT; break;
298 i = MATCH_PIXEL_SIZE; break;
300 switch (FcToLower (object[1])) {
302 i = MATCH_WIDTH; break;
304 i = MATCH_WEIGHT; break;
308 i = MATCH_RASTERIZER; break;
310 i = MATCH_OUTLINE; break;
313 FcStrCmpIgnoreCase ((FcChar8 *) _FcMatchers[i].object,
314 (FcChar8 *) object) != 0)
317 *bestValue = FcValueCanonicalize(&FcValueListPtrU(v2orig)->value);
321 for (i = 0; i < NUM_MATCHER; i++)
323 if (!FcStrCmpIgnoreCase ((FcChar8 *) _FcMatchers[i].object,
327 if (i == NUM_MATCHER)
330 *bestValue = v2orig->value;
338 for (v1 = v1orig, v1_ptrU = FcValueListPtrU(v1); v1_ptrU;
339 v1 = FcValueListPtrU(v1)->next, v1_ptrU = FcValueListPtrU(v1))
341 for (v2 = v2orig, v2_ptrU = FcValueListPtrU(v2); FcValueListPtrU(v2);
342 v2 = FcValueListPtrU(v2)->next)
344 v = (*_FcMatchers[i].compare) (_FcMatchers[i].object,
349 *result = FcResultTypeMismatch;
352 if (FcDebug () & FC_DBG_MATCHV)
353 printf (" v %g j %d ", v, j);
358 *bestValue = FcValueCanonicalize(&v2_ptrU->value);
361 if (v1_ptrU->binding == FcValueBindingStrong)
374 if (FcDebug () & FC_DBG_MATCHV)
376 printf (" %s: %g ", object, best);
377 FcValueListPrint (v1orig);
379 FcValueListPrint (v2orig);
384 int weak = _FcMatchers[i].weak;
385 int strong = _FcMatchers[i].strong;
387 value[strong] += best;
390 value[weak] += bestWeak;
391 value[strong] += bestStrong;
398 * Return a value indicating the distance between the two lists of
403 FcCompare (FcPattern *pat,
410 for (i = 0; i < NUM_MATCH_VALUES; i++)
415 while (i1 < pat->num && i2 < fnt->num)
417 i = FcObjectPtrCompare((FcPatternEltU(pat->elts)+i1)->object,
418 (FcPatternEltU(fnt->elts)+i2)->object);
425 if (!FcCompareValueList (FcObjectPtrU((FcPatternEltU(pat->elts)+i1)->object),
426 (FcPatternEltU(pat->elts)+i1)->values,
427 (FcPatternEltU(fnt->elts)+i2)->values,
438 for (i1 = 0; i1 < pat->num; i1++)
440 for (i2 = 0; i2 < fnt->num; i2++)
442 if (!strcmp (pat->elts[i1].object, fnt->elts[i2].object))
453 FcFontRenderPrepare (FcConfig *config,
459 FcPatternElt *fe, *pe;
463 new = FcPatternCreate ();
466 for (i = 0; i < font->num; i++)
468 fe = FcPatternEltU(font->elts)+i;
469 pe = FcPatternFindElt (pat, FcObjectPtrU(fe->object));
472 if (!FcCompareValueList (FcObjectPtrU(pe->object), pe->values,
473 fe->values, &v, 0, &result))
475 FcPatternDestroy (new);
480 v = FcValueCanonicalize(&FcValueListPtrU(fe->values)->value);
481 FcPatternAdd (new, FcObjectPtrU(fe->object), v, FcFalse);
483 for (i = 0; i < pat->num; i++)
485 pe = FcPatternEltU(pat->elts)+i;
486 fe = FcPatternFindElt (font, FcObjectPtrU(pe->object));
488 FcPatternAdd (new, FcObjectPtrU(pe->object),
489 FcValueCanonicalize(&FcValueListPtrU(pe->values)->value), FcTrue);
492 if (FcPatternFindElt (font, FC_FILE))
493 FcPatternTransferFullFname (new, font);
495 FcConfigSubstituteWithPat (config, new, pat, FcMatchFont);
500 FcFontSetMatch (FcConfig *config,
506 double score[NUM_MATCH_VALUES], bestscore[NUM_MATCH_VALUES];
513 for (i = 0; i < NUM_MATCH_VALUES; i++)
516 if (FcDebug () & FC_DBG_MATCH)
523 config = FcConfigGetCurrent ();
526 *result = FcResultOutOfMemory;
530 for (set = 0; set < nsets; set++)
535 for (f = 0; f < s->nfont; f++)
537 if (FcDebug () & FC_DBG_MATCHV)
539 printf ("Font %d ", f);
540 FcPatternPrint (s->fonts[f]);
542 if (!FcCompare (p, s->fonts[f], score, result))
544 if (FcDebug () & FC_DBG_MATCHV)
547 for (i = 0; i < NUM_MATCH_VALUES; i++)
549 printf (" %g", score[i]);
553 for (i = 0; i < NUM_MATCH_VALUES; i++)
555 if (best && bestscore[i] < score[i])
557 if (!best || score[i] < bestscore[i])
559 for (i = 0; i < NUM_MATCH_VALUES; i++)
560 bestscore[i] = score[i];
567 if (FcDebug () & FC_DBG_MATCH)
569 printf ("Best score");
570 for (i = 0; i < NUM_MATCH_VALUES; i++)
571 printf (" %g", bestscore[i]);
572 FcPatternPrint (best);
576 *result = FcResultNoMatch;
579 return FcFontRenderPrepare (config, p, best);
583 FcFontMatch (FcConfig *config,
592 config = FcConfigGetCurrent ();
597 if (config->fonts[FcSetSystem])
598 sets[nsets++] = config->fonts[FcSetSystem];
599 if (config->fonts[FcSetApplication])
600 sets[nsets++] = config->fonts[FcSetApplication];
601 return FcFontSetMatch (config, sets, nsets, p, result);
604 typedef struct _FcSortNode {
606 double score[NUM_MATCH_VALUES];
610 FcSortCompare (const void *aa, const void *ab)
612 FcSortNode *a = *(FcSortNode **) aa;
613 FcSortNode *b = *(FcSortNode **) ab;
614 double *as = &a->score[0];
615 double *bs = &b->score[0];
616 double ad = 0, bd = 0;
619 i = NUM_MATCH_VALUES;
620 while (i-- && (ad = *as++) == (bd = *bs++))
622 return ad < bd ? -1 : ad > bd ? 1 : 0;
626 FcSortWalk (FcSortNode **n, int nnode, FcFontSet *fs, FcCharSet **cs, FcBool trim)
634 if (FcPatternGetCharSet (node->pattern, FC_CHARSET, 0, &ncs) ==
638 * If this font isn't a subset of the previous fonts,
641 if (!trim || !*cs || !FcCharSetIsSubset (ncs, *cs))
645 ncs = FcCharSetUnion (ncs, *cs);
648 FcCharSetDestroy (*cs);
651 ncs = FcCharSetCopy (ncs);
653 FcPatternReference (node->pattern);
654 if (FcDebug () & FC_DBG_MATCH)
657 FcPatternPrint (node->pattern);
659 if (!FcFontSetAdd (fs, node->pattern))
661 FcPatternDestroy (node->pattern);
671 FcFontSetSortDestroy (FcFontSet *fs)
673 FcFontSetDestroy (fs);
677 FcFontSetSort (FcConfig *config,
688 FcSortNode **nodeps, **nodep;
696 FcBool *patternLangSat;
699 if (FcDebug () & FC_DBG_MATCH)
705 for (set = 0; set < nsets; set++)
715 for (nPatternLang = 0;
716 FcPatternGet (p, FC_LANG, nPatternLang, &patternLang) == FcResultMatch;
721 nodes = malloc (nnodes * sizeof (FcSortNode) +
722 nnodes * sizeof (FcSortNode *) +
723 nPatternLang * sizeof (FcBool));
726 nodeps = (FcSortNode **) (nodes + nnodes);
727 patternLangSat = (FcBool *) (nodeps + nnodes);
731 for (set = 0; set < nsets; set++)
736 for (f = 0; f < s->nfont; f++)
738 if (FcDebug () & FC_DBG_MATCHV)
740 printf ("Font %d ", f);
741 FcPatternPrint (s->fonts[f]);
743 new->pattern = s->fonts[f];
744 if (!FcCompare (p, new->pattern, new->score, result))
746 if (FcDebug () & FC_DBG_MATCHV)
749 for (i = 0; i < NUM_MATCH_VALUES; i++)
751 printf (" %g", new->score[i]);
761 nnodes = new - nodes;
763 qsort (nodeps, nnodes, sizeof (FcSortNode *),
766 for (i = 0; i < nPatternLang; i++)
767 patternLangSat[i] = FcFalse;
769 for (f = 0; f < nnodes; f++)
771 FcBool satisfies = FcFalse;
773 * If this node matches any language, go check
774 * which ones and satisfy those entries
776 if (nodeps[f]->score[MATCH_LANG_INDEX] < nPatternLang)
778 for (i = 0; i < nPatternLang; i++)
782 if (!patternLangSat[i] &&
783 FcPatternGet (p, FC_LANG, i, &patternLang) == FcResultMatch &&
784 FcPatternGet (nodeps[f]->pattern, FC_LANG, 0, &nodeLang) == FcResultMatch)
786 double compare = FcCompareLang (FC_LANG, &patternLang,
788 if (compare >= 0 && compare < 2)
790 if (FcDebug () & FC_DBG_MATCHV)
795 if (FcPatternGetString (nodeps[f]->pattern, FC_FAMILY, 0, &family) == FcResultMatch &&
796 FcPatternGetString (nodeps[f]->pattern, FC_STYLE, 0, &style) == FcResultMatch)
797 printf ("Font %s:%s matches language %d\n", family, style, i);
799 patternLangSat[i] = FcTrue;
807 nodeps[f]->score[MATCH_LANG_INDEX] = 1000.0;
811 * Re-sort once the language issues have been settled
813 qsort (nodeps, nnodes, sizeof (FcSortNode *),
816 ret = FcFontSetCreate ();
822 if (!FcSortWalk (nodeps, nnodes, ret, &cs, trim))
828 FcCharSetDestroy (cs);
836 FcCharSetDestroy (cs);
837 FcFontSetDestroy (ret);
845 FcFontSort (FcConfig *config,
856 config = FcConfigGetCurrent ();
861 if (config->fonts[FcSetSystem])
862 sets[nsets++] = config->fonts[FcSetSystem];
863 if (config->fonts[FcSetApplication])
864 sets[nsets++] = config->fonts[FcSetApplication];
865 return FcFontSetSort (config, sets, nsets, p, trim, csp, result);