2 * fontconfig/src/fcmatch.c
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 the author(s) not be used in
11 * advertising or publicity pertaining to distribution of the software without
12 * specific, written prior permission. The authors make no
13 * representations about the suitability of this software for any purpose. It
14 * is provided "as is" without express or implied warranty.
16 * THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18 * EVENT SHALL THE AUTHOR(S) 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.
28 FcCompareNumber (FcValue *value1, FcValue *value2)
32 switch ((int) value1->type) {
34 v1 = (double) value1->u.i;
42 switch ((int) value2->type) {
44 v2 = (double) value2->u.i;
59 FcCompareString (FcValue *v1, FcValue *v2)
61 return (double) FcStrCmpIgnoreCase (FcValueString(v1), FcValueString(v2)) != 0;
65 FcCompareFamily (FcValue *v1, FcValue *v2)
67 /* rely on the guarantee in FcPatternObjectAddWithBinding that
68 * families are always FcTypeString. */
69 const FcChar8* v1_string = FcValueString(v1);
70 const FcChar8* v2_string = FcValueString(v2);
72 if (FcToLower(*v1_string) != FcToLower(*v2_string) &&
73 *v1_string != ' ' && *v2_string != ' ')
76 return (double) FcStrCmpIgnoreBlanksAndCase (v1_string, v2_string) != 0;
80 FcComparePostScript (FcValue *v1, FcValue *v2)
82 const FcChar8 *v1_string = FcValueString (v1);
83 const FcChar8 *v2_string = FcValueString (v2);
87 if (FcToLower (*v1_string) != FcToLower (*v2_string) &&
88 *v1_string != ' ' && *v2_string != ' ')
91 n = FcStrMatchIgnoreCaseAndDelims (v1_string, v2_string, (const FcChar8 *)" -");
92 len = strlen ((const char *)v1_string);
94 return (double)(len - n) / (double)len;
98 FcCompareLang (FcValue *v1, FcValue *v2)
101 FcValue value1 = FcValueCanonicalize(v1), value2 = FcValueCanonicalize(v2);
103 switch ((int) value1.type) {
105 switch ((int) value2.type) {
107 result = FcLangSetCompare (value1.u.l, value2.u.l);
110 result = FcLangSetHasLang (value1.u.l,
118 switch ((int) value2.type) {
120 result = FcLangSetHasLang (value2.u.l, value1.u.s);
123 result = FcLangCompare (value1.u.s,
136 case FcLangDifferentCountry:
138 case FcLangDifferentLang:
145 FcCompareBool (FcValue *v1, FcValue *v2)
147 if (v2->type != FcTypeBool || v1->type != FcTypeBool)
149 return (double) v2->u.b != v1->u.b;
153 FcCompareCharSet (FcValue *v1, FcValue *v2)
155 return (double) FcCharSetSubtractCount (FcValueCharSet(v1), FcValueCharSet(v2));
159 FcCompareSize (FcValue *value1, FcValue *value2)
163 switch ((int) value1->type) {
173 switch ((int) value2->type) {
192 FcCompareSizeRange (FcValue *v1, FcValue *v2)
194 FcValue value1 = FcValueCanonicalize (v1);
195 FcValue value2 = FcValueCanonicalize (v2);
196 FcRange *r1 = NULL, *r2 = NULL;
199 switch ((int) value1.type) {
201 r1 = FcRangeCreateDouble (value1.u.d, value1.u.d);
204 r1 = FcRangeCopy (value1.u.r);
209 switch ((int) value2.type) {
211 r2 = FcRangeCreateDouble (value2.u.d, value2.u.d);
214 r2 = FcRangeCopy (value2.u.r);
220 if (FcRangeIsInRange (r1, r2))
223 ret = FC_MIN (fabs (r1->u.d.end - r2->u.d.begin), fabs (r1->u.d.begin - r2->u.d.end));
235 FcCompareFilename (FcValue *v1, FcValue *v2)
237 const FcChar8 *s1 = FcValueString (v1), *s2 = FcValueString (v2);
238 if (FcStrCmp (s1, s2) == 0)
240 else if (FcStrCmpIgnoreCase (s1, s2) == 0)
242 else if (FcStrGlobMatch (s1, s2))
249 /* Define priorities to -1 for objects that don't have a compare function. */
251 #define PRI_NULL(n) \
252 PRI_ ## n ## _STRONG = -1, \
253 PRI_ ## n ## _WEAK = -1,
255 #define PRI_FcCompareFamily(n) PRI1(n)
256 #define PRI_FcCompareString(n) PRI1(n)
257 #define PRI_FcCompareNumber(n) PRI1(n)
258 #define PRI_FcCompareSize(n) PRI1(n)
259 #define PRI_FcCompareBool(n) PRI1(n)
260 #define PRI_FcCompareFilename(n) PRI1(n)
261 #define PRI_FcCompareCharSet(n) PRI1(n)
262 #define PRI_FcCompareLang(n) PRI1(n)
263 #define PRI_FcComparePostScript(n) PRI1(n)
264 #define PRI_FcCompareSizeRange(n) PRI1(n)
266 #define FC_OBJECT(NAME, Type, Cmp) PRI_##Cmp(NAME)
268 typedef enum _FcMatcherPriorityDummy {
270 } FcMatcherPriorityDummy;
275 /* Canonical match priority order. */
280 PRI_ ## n ## _STRONG = PRI_ ## n, \
281 PRI_ ## n ## _WEAK = PRI_ ## n
283 typedef enum _FcMatcherPriority {
291 PRI_POSTSCRIPT_NAME_STRONG,
294 PRI_POSTSCRIPT_NAME_WEAK,
312 typedef struct _FcMatcher {
314 double (*compare) (FcValue *value1, FcValue *value2);
319 * Order is significant, it defines the precedence of
320 * each value, earlier values are more significant than
323 #define FC_OBJECT(NAME, Type, Cmp) { FC_##NAME##_OBJECT, Cmp, PRI_##NAME##_STRONG, PRI_##NAME##_WEAK },
324 static const FcMatcher _FcMatchers [] = {
325 { FC_INVALID_OBJECT, NULL, -1, -1 },
330 static const FcMatcher*
331 FcObjectToMatcher (FcObject object,
337 case FC_FAMILYLANG_OBJECT:
338 case FC_STYLELANG_OBJECT:
339 case FC_FULLNAMELANG_OBJECT:
340 object = FC_LANG_OBJECT;
344 if (object > FC_MAX_BASE_OBJECT ||
345 !_FcMatchers[object].compare ||
346 _FcMatchers[object].strong == -1 ||
347 _FcMatchers[object].weak == -1)
350 return _FcMatchers + object;
354 FcCompareValueList (FcObject object,
355 const FcMatcher *match,
356 FcValueListPtr v1orig, /* pattern */
357 FcValueListPtr v2orig, /* target */
363 FcValueListPtr v1, v2;
364 double v, best, bestStrong, bestWeak;
370 *bestValue = FcValueCanonicalize(&v2orig->value);
380 for (v1 = v1orig; v1; v1 = FcValueListNext(v1))
382 for (v2 = v2orig, k = 0; v2; v2 = FcValueListNext(v2), k++)
384 v = (match->compare) (&v1->value, &v2->value);
387 *result = FcResultTypeMismatch;
394 *bestValue = FcValueCanonicalize(&v2->value);
398 if (v1->binding == FcValueBindingStrong)
411 if (FcDebug () & FC_DBG_MATCHV)
413 printf (" %s: %g ", FcObjectName (object), best);
414 FcValueListPrint (v1orig);
416 FcValueListPrint (v2orig);
421 int weak = match->weak;
422 int strong = match->strong;
424 value[strong] += best;
427 value[weak] += bestWeak;
428 value[strong] += bestStrong;
438 * Return a value indicating the distance between the two lists of
443 FcCompare (FcPattern *pat,
450 for (i = 0; i < PRI_END; i++)
455 while (i1 < pat->num && i2 < fnt->num)
457 FcPatternElt *elt_i1 = &FcPatternElts(pat)[i1];
458 FcPatternElt *elt_i2 = &FcPatternElts(fnt)[i2];
460 i = FcObjectCompare(elt_i1->object, elt_i2->object);
467 const FcMatcher *match = FcObjectToMatcher (elt_i1->object, FcFalse);
468 if (!FcCompareValueList (elt_i1->object, match,
469 FcPatternEltValues(elt_i1),
470 FcPatternEltValues(elt_i2),
471 NULL, value, NULL, result))
481 FcFontRenderPrepare (FcConfig *config,
487 FcPatternElt *fe, *pe;
491 assert (pat != NULL);
492 assert (font != NULL);
494 new = FcPatternCreate ();
497 for (i = 0; i < font->num; i++)
499 fe = &FcPatternElts(font)[i];
500 if (fe->object == FC_FAMILYLANG_OBJECT ||
501 fe->object == FC_STYLELANG_OBJECT ||
502 fe->object == FC_FULLNAMELANG_OBJECT)
504 /* ignore those objects. we need to deal with them
508 if (fe->object == FC_FAMILY_OBJECT ||
509 fe->object == FC_STYLE_OBJECT ||
510 fe->object == FC_FULLNAME_OBJECT)
512 FcPatternElt *fel, *pel;
514 FC_ASSERT_STATIC ((FC_FAMILY_OBJECT + 1) == FC_FAMILYLANG_OBJECT);
515 FC_ASSERT_STATIC ((FC_STYLE_OBJECT + 1) == FC_STYLELANG_OBJECT);
516 FC_ASSERT_STATIC ((FC_FULLNAME_OBJECT + 1) == FC_FULLNAMELANG_OBJECT);
518 fel = FcPatternObjectFindElt (font, fe->object + 1);
519 pel = FcPatternObjectFindElt (pat, fe->object + 1);
523 /* The font has name languages, and pattern asks for specific language(s).
524 * Match on language and and prefer that result.
525 * Note: Currently the code only give priority to first matching language.
528 FcValueListPtr l1, l2, ln = NULL, ll = NULL;
529 const FcMatcher *match = FcObjectToMatcher (pel->object, FcTrue);
531 if (!FcCompareValueList (pel->object, match,
532 FcPatternEltValues (pel),
533 FcPatternEltValues (fel), NULL, NULL, &n, &result))
535 FcPatternDestroy (new);
539 for (j = 0, l1 = FcPatternEltValues (fe), l2 = FcPatternEltValues (fel);
540 l1 != NULL || l2 != NULL;
541 j++, l1 = l1 ? FcValueListNext (l1) : NULL, l2 = l2 ? FcValueListNext (l2) : NULL)
546 ln = FcValueListPrepend (ln,
547 FcValueCanonicalize (&l1->value),
548 FcValueBindingStrong);
550 ll = FcValueListPrepend (ll,
551 FcValueCanonicalize (&l2->value),
552 FcValueBindingStrong);
557 ln = FcValueListAppend (ln,
558 FcValueCanonicalize (&l1->value),
559 FcValueBindingStrong);
561 ll = FcValueListAppend (ll,
562 FcValueCanonicalize (&l2->value),
563 FcValueBindingStrong);
566 FcPatternObjectListAdd (new, fe->object, ln, FcFalse);
567 FcPatternObjectListAdd (new, fel->object, ll, FcFalse);
573 /* Pattern doesn't ask for specific language. Copy all for name and
575 FcValueListPtr l1, l2;
577 l1 = FcValueListDuplicate (FcPatternEltValues (fe));
578 l2 = FcValueListDuplicate (FcPatternEltValues (fel));
579 FcPatternObjectListAdd (new, fe->object, l1, FcFalse);
580 FcPatternObjectListAdd (new, fel->object, l2, FcFalse);
586 pe = FcPatternObjectFindElt (pat, fe->object);
589 const FcMatcher *match = FcObjectToMatcher (pe->object, FcFalse);
590 if (!FcCompareValueList (pe->object, match,
591 FcPatternEltValues(pe),
592 FcPatternEltValues(fe), &v, NULL, NULL, &result))
594 FcPatternDestroy (new);
597 FcPatternObjectAdd (new, fe->object, v, FcFalse);
601 FcPatternObjectListAdd (new, fe->object,
602 FcValueListDuplicate (FcPatternEltValues (fe)),
606 for (i = 0; i < pat->num; i++)
608 pe = &FcPatternElts(pat)[i];
609 fe = FcPatternObjectFindElt (font, pe->object);
611 pe->object != FC_FAMILYLANG_OBJECT &&
612 pe->object != FC_STYLELANG_OBJECT &&
613 pe->object != FC_FULLNAMELANG_OBJECT)
615 FcPatternObjectListAdd (new, pe->object,
616 FcValueListDuplicate (FcPatternEltValues(pe)),
621 FcConfigSubstituteWithPat (config, new, pat, FcMatchFont);
626 FcFontSetMatchInternal (FcFontSet **sets,
631 double score[PRI_END], bestscore[PRI_END];
638 for (i = 0; i < PRI_END; i++)
641 if (FcDebug () & FC_DBG_MATCH)
646 for (set = 0; set < nsets; set++)
651 for (f = 0; f < s->nfont; f++)
653 if (FcDebug () & FC_DBG_MATCHV)
655 printf ("Font %d ", f);
656 FcPatternPrint (s->fonts[f]);
658 if (!FcCompare (p, s->fonts[f], score, result))
660 if (FcDebug () & FC_DBG_MATCHV)
663 for (i = 0; i < PRI_END; i++)
665 printf (" %g", score[i]);
669 for (i = 0; i < PRI_END; i++)
671 if (best && bestscore[i] < score[i])
673 if (!best || score[i] < bestscore[i])
675 for (i = 0; i < PRI_END; i++)
676 bestscore[i] = score[i];
683 if (FcDebug () & FC_DBG_MATCH)
685 printf ("Best score");
686 for (i = 0; i < PRI_END; i++)
687 printf (" %g", bestscore[i]);
689 FcPatternPrint (best);
691 /* assuming that 'result' is initialized with FcResultNoMatch
692 * outside this function */
694 *result = FcResultMatch;
700 FcFontSetMatch (FcConfig *config,
708 assert (sets != NULL);
710 assert (result != NULL);
712 *result = FcResultNoMatch;
716 config = FcConfigGetCurrent ();
720 best = FcFontSetMatchInternal (sets, nsets, p, result);
722 return FcFontRenderPrepare (config, p, best);
728 FcFontMatch (FcConfig *config,
737 assert (result != NULL);
739 *result = FcResultNoMatch;
743 config = FcConfigGetCurrent ();
748 if (config->fonts[FcSetSystem])
749 sets[nsets++] = config->fonts[FcSetSystem];
750 if (config->fonts[FcSetApplication])
751 sets[nsets++] = config->fonts[FcSetApplication];
753 best = FcFontSetMatchInternal (sets, nsets, p, result);
755 return FcFontRenderPrepare (config, p, best);
760 typedef struct _FcSortNode {
762 double score[PRI_END];
766 FcSortCompare (const void *aa, const void *ab)
768 FcSortNode *a = *(FcSortNode **) aa;
769 FcSortNode *b = *(FcSortNode **) ab;
770 double *as = &a->score[0];
771 double *bs = &b->score[0];
772 double ad = 0, bd = 0;
776 while (i-- && (ad = *as++) == (bd = *bs++))
778 return ad < bd ? -1 : ad > bd ? 1 : 0;
782 FcSortWalk (FcSortNode **n, int nnode, FcFontSet *fs, FcCharSet **csp, FcBool trim)
784 FcBool ret = FcFalse;
791 cs = FcCharSetCreate ();
796 for (i = 0; i < nnode; i++)
798 FcSortNode *node = *n++;
799 FcBool adds_chars = FcFalse;
802 * Only fetch node charset if we'd need it
808 if (FcPatternGetCharSet (node->pattern, FC_CHARSET, 0, &ncs) !=
812 if (!FcCharSetMerge (cs, ncs, &adds_chars))
817 * If this font isn't a subset of the previous fonts,
820 if (!i || !trim || adds_chars)
822 FcPatternReference (node->pattern);
823 if (FcDebug () & FC_DBG_MATCHV)
826 FcPatternPrint (node->pattern);
828 if (!FcFontSetAdd (fs, node->pattern))
830 FcPatternDestroy (node->pattern);
845 FcCharSetDestroy (cs);
851 FcFontSetSortDestroy (FcFontSet *fs)
853 FcFontSetDestroy (fs);
857 FcFontSetSort (FcConfig *config FC_UNUSED,
868 FcSortNode **nodeps, **nodep;
875 FcBool *patternLangSat;
878 assert (sets != NULL);
880 assert (result != NULL);
882 /* There are some implementation that relying on the result of
883 * "result" to check if the return value of FcFontSetSort
885 * So we should initialize it to the conservative way since
886 * this function doesn't return NULL anymore.
889 *result = FcResultNoMatch;
891 if (FcDebug () & FC_DBG_MATCH)
897 for (set = 0; set < nsets; set++)
905 return FcFontSetCreate ();
907 for (nPatternLang = 0;
908 FcPatternGet (p, FC_LANG, nPatternLang, &patternLang) == FcResultMatch;
913 nodes = malloc (nnodes * sizeof (FcSortNode) +
914 nnodes * sizeof (FcSortNode *) +
915 nPatternLang * sizeof (FcBool));
918 nodeps = (FcSortNode **) (nodes + nnodes);
919 patternLangSat = (FcBool *) (nodeps + nnodes);
923 for (set = 0; set < nsets; set++)
928 for (f = 0; f < s->nfont; f++)
930 if (FcDebug () & FC_DBG_MATCHV)
932 printf ("Font %d ", f);
933 FcPatternPrint (s->fonts[f]);
935 new->pattern = s->fonts[f];
936 if (!FcCompare (p, new->pattern, new->score, result))
938 if (FcDebug () & FC_DBG_MATCHV)
941 for (i = 0; i < PRI_END; i++)
943 printf (" %g", new->score[i]);
953 nnodes = new - nodes;
955 qsort (nodeps, nnodes, sizeof (FcSortNode *),
958 for (i = 0; i < nPatternLang; i++)
959 patternLangSat[i] = FcFalse;
961 for (f = 0; f < nnodes; f++)
963 FcBool satisfies = FcFalse;
965 * If this node matches any language, go check
966 * which ones and satisfy those entries
968 if (nodeps[f]->score[PRI_LANG] < 2000)
970 for (i = 0; i < nPatternLang; i++)
974 if (!patternLangSat[i] &&
975 FcPatternGet (p, FC_LANG, i, &patternLang) == FcResultMatch &&
976 FcPatternGet (nodeps[f]->pattern, FC_LANG, 0, &nodeLang) == FcResultMatch)
978 double compare = FcCompareLang (&patternLang, &nodeLang);
979 if (compare >= 0 && compare < 2)
981 if (FcDebug () & FC_DBG_MATCHV)
986 if (FcPatternGetString (nodeps[f]->pattern, FC_FAMILY, 0, &family) == FcResultMatch &&
987 FcPatternGetString (nodeps[f]->pattern, FC_STYLE, 0, &style) == FcResultMatch)
988 printf ("Font %s:%s matches language %d\n", family, style, i);
990 patternLangSat[i] = FcTrue;
999 nodeps[f]->score[PRI_LANG] = 10000.0;
1004 * Re-sort once the language issues have been settled
1006 qsort (nodeps, nnodes, sizeof (FcSortNode *),
1009 ret = FcFontSetCreate ();
1013 if (!FcSortWalk (nodeps, nnodes, ret, csp, trim))
1018 if (FcDebug() & FC_DBG_MATCH)
1020 printf ("First font ");
1021 FcPatternPrint (ret->fonts[0]);
1024 *result = FcResultMatch;
1029 FcFontSetDestroy (ret);
1037 FcFontSort (FcConfig *config,
1047 assert (result != NULL);
1049 *result = FcResultNoMatch;
1053 config = FcConfigGetCurrent ();
1058 if (config->fonts[FcSetSystem])
1059 sets[nsets++] = config->fonts[FcSetSystem];
1060 if (config->fonts[FcSetApplication])
1061 sets[nsets++] = config->fonts[FcSetApplication];
1062 return FcFontSetSort (config, sets, nsets, p, trim, csp, result);
1065 #include "fcaliastail.h"