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->end - r2->begin), fabs (r1->begin - r2->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,
313 typedef struct _FcMatcher {
315 double (*compare) (FcValue *value1, FcValue *value2);
320 * Order is significant, it defines the precedence of
321 * each value, earlier values are more significant than
324 #define FC_OBJECT(NAME, Type, Cmp) { FC_##NAME##_OBJECT, Cmp, PRI_##NAME##_STRONG, PRI_##NAME##_WEAK },
325 static const FcMatcher _FcMatchers [] = {
326 { FC_INVALID_OBJECT, NULL, -1, -1 },
331 static const FcMatcher*
332 FcObjectToMatcher (FcObject object,
338 case FC_FAMILYLANG_OBJECT:
339 case FC_STYLELANG_OBJECT:
340 case FC_FULLNAMELANG_OBJECT:
341 object = FC_LANG_OBJECT;
345 if (object > FC_MAX_BASE_OBJECT ||
346 !_FcMatchers[object].compare ||
347 _FcMatchers[object].strong == -1 ||
348 _FcMatchers[object].weak == -1)
351 return _FcMatchers + object;
355 FcCompareValueList (FcObject object,
356 const FcMatcher *match,
357 FcValueListPtr v1orig, /* pattern */
358 FcValueListPtr v2orig, /* target */
364 FcValueListPtr v1, v2;
365 double v, best, bestStrong, bestWeak;
371 *bestValue = FcValueCanonicalize(&v2orig->value);
381 for (v1 = v1orig; v1; v1 = FcValueListNext(v1))
383 for (v2 = v2orig, k = 0; v2; v2 = FcValueListNext(v2), k++)
385 v = (match->compare) (&v1->value, &v2->value);
388 *result = FcResultTypeMismatch;
395 *bestValue = FcValueCanonicalize(&v2->value);
399 if (v1->binding == FcValueBindingStrong)
412 if (FcDebug () & FC_DBG_MATCHV)
414 printf (" %s: %g ", FcObjectName (object), best);
415 FcValueListPrint (v1orig);
417 FcValueListPrint (v2orig);
422 int weak = match->weak;
423 int strong = match->strong;
425 value[strong] += best;
428 value[weak] += bestWeak;
429 value[strong] += bestStrong;
439 * Return a value indicating the distance between the two lists of
444 FcCompare (FcPattern *pat,
451 for (i = 0; i < PRI_END; i++)
456 while (i1 < pat->num && i2 < fnt->num)
458 FcPatternElt *elt_i1 = &FcPatternElts(pat)[i1];
459 FcPatternElt *elt_i2 = &FcPatternElts(fnt)[i2];
461 i = FcObjectCompare(elt_i1->object, elt_i2->object);
468 const FcMatcher *match = FcObjectToMatcher (elt_i1->object, FcFalse);
469 if (!FcCompareValueList (elt_i1->object, match,
470 FcPatternEltValues(elt_i1),
471 FcPatternEltValues(elt_i2),
472 NULL, value, NULL, result))
482 FcFontRenderPrepare (FcConfig *config,
488 FcPatternElt *fe, *pe;
492 assert (pat != NULL);
493 assert (font != NULL);
495 new = FcPatternCreate ();
498 for (i = 0; i < font->num; i++)
500 fe = &FcPatternElts(font)[i];
501 if (fe->object == FC_FAMILYLANG_OBJECT ||
502 fe->object == FC_STYLELANG_OBJECT ||
503 fe->object == FC_FULLNAMELANG_OBJECT)
505 /* ignore those objects. we need to deal with them
509 if (fe->object == FC_FAMILY_OBJECT ||
510 fe->object == FC_STYLE_OBJECT ||
511 fe->object == FC_FULLNAME_OBJECT)
513 FcPatternElt *fel, *pel;
515 FC_ASSERT_STATIC ((FC_FAMILY_OBJECT + 1) == FC_FAMILYLANG_OBJECT);
516 FC_ASSERT_STATIC ((FC_STYLE_OBJECT + 1) == FC_STYLELANG_OBJECT);
517 FC_ASSERT_STATIC ((FC_FULLNAME_OBJECT + 1) == FC_FULLNAMELANG_OBJECT);
519 fel = FcPatternObjectFindElt (font, fe->object + 1);
520 pel = FcPatternObjectFindElt (pat, fe->object + 1);
524 /* The font has name languages, and pattern asks for specific language(s).
525 * Match on language and and prefer that result.
526 * Note: Currently the code only give priority to first matching language.
529 FcValueListPtr l1, l2, ln = NULL, ll = NULL;
530 const FcMatcher *match = FcObjectToMatcher (pel->object, FcTrue);
532 if (!FcCompareValueList (pel->object, match,
533 FcPatternEltValues (pel),
534 FcPatternEltValues (fel), NULL, NULL, &n, &result))
536 FcPatternDestroy (new);
540 for (j = 0, l1 = FcPatternEltValues (fe), l2 = FcPatternEltValues (fel);
541 l1 != NULL || l2 != NULL;
542 j++, l1 = l1 ? FcValueListNext (l1) : NULL, l2 = l2 ? FcValueListNext (l2) : NULL)
547 ln = FcValueListPrepend (ln,
548 FcValueCanonicalize (&l1->value),
549 FcValueBindingStrong);
551 ll = FcValueListPrepend (ll,
552 FcValueCanonicalize (&l2->value),
553 FcValueBindingStrong);
558 ln = FcValueListAppend (ln,
559 FcValueCanonicalize (&l1->value),
560 FcValueBindingStrong);
562 ll = FcValueListAppend (ll,
563 FcValueCanonicalize (&l2->value),
564 FcValueBindingStrong);
567 FcPatternObjectListAdd (new, fe->object, ln, FcFalse);
568 FcPatternObjectListAdd (new, fel->object, ll, FcFalse);
574 /* Pattern doesn't ask for specific language. Copy all for name and
576 FcValueListPtr l1, l2;
578 l1 = FcValueListDuplicate (FcPatternEltValues (fe));
579 l2 = FcValueListDuplicate (FcPatternEltValues (fel));
580 FcPatternObjectListAdd (new, fe->object, l1, FcFalse);
581 FcPatternObjectListAdd (new, fel->object, l2, FcFalse);
587 pe = FcPatternObjectFindElt (pat, fe->object);
590 const FcMatcher *match = FcObjectToMatcher (pe->object, FcFalse);
591 if (!FcCompareValueList (pe->object, match,
592 FcPatternEltValues(pe),
593 FcPatternEltValues(fe), &v, NULL, NULL, &result))
595 FcPatternDestroy (new);
598 FcPatternObjectAdd (new, fe->object, v, FcFalse);
602 FcPatternObjectListAdd (new, fe->object,
603 FcValueListDuplicate (FcPatternEltValues (fe)),
607 for (i = 0; i < pat->num; i++)
609 pe = &FcPatternElts(pat)[i];
610 fe = FcPatternObjectFindElt (font, pe->object);
612 pe->object != FC_FAMILYLANG_OBJECT &&
613 pe->object != FC_STYLELANG_OBJECT &&
614 pe->object != FC_FULLNAMELANG_OBJECT)
616 FcPatternObjectListAdd (new, pe->object,
617 FcValueListDuplicate (FcPatternEltValues(pe)),
622 FcConfigSubstituteWithPat (config, new, pat, FcMatchFont);
627 FcFontSetMatchInternal (FcFontSet **sets,
632 double score[PRI_END], bestscore[PRI_END];
639 for (i = 0; i < PRI_END; i++)
642 if (FcDebug () & FC_DBG_MATCH)
647 for (set = 0; set < nsets; set++)
652 for (f = 0; f < s->nfont; f++)
654 if (FcDebug () & FC_DBG_MATCHV)
656 printf ("Font %d ", f);
657 FcPatternPrint (s->fonts[f]);
659 if (!FcCompare (p, s->fonts[f], score, result))
661 if (FcDebug () & FC_DBG_MATCHV)
664 for (i = 0; i < PRI_END; i++)
666 printf (" %g", score[i]);
670 for (i = 0; i < PRI_END; i++)
672 if (best && bestscore[i] < score[i])
674 if (!best || score[i] < bestscore[i])
676 for (i = 0; i < PRI_END; i++)
677 bestscore[i] = score[i];
684 if (FcDebug () & FC_DBG_MATCH)
686 printf ("Best score");
687 for (i = 0; i < PRI_END; i++)
688 printf (" %g", bestscore[i]);
690 FcPatternPrint (best);
692 if (FcDebug () & FC_DBG_MATCH2)
694 char *env = getenv ("FC_DBG_MATCH_FILTER");
695 FcObjectSet *os = NULL;
703 ss = s = strdup (env);
704 os = FcObjectSetCreate ();
710 if (!(p = strchr (s, ',')))
713 len = strlen (s) + 1;
719 x = malloc (sizeof (char) * len);
720 strncpy (x, s, len - 1);
722 if (FcObjectFromName (x) > 0)
723 FcObjectSetAdd (os, x);
729 FcPatternPrint2 (p, best, os);
731 FcObjectSetDestroy (os);
733 /* assuming that 'result' is initialized with FcResultNoMatch
734 * outside this function */
736 *result = FcResultMatch;
742 FcFontSetMatch (FcConfig *config,
750 assert (sets != NULL);
752 assert (result != NULL);
754 *result = FcResultNoMatch;
758 config = FcConfigGetCurrent ();
762 best = FcFontSetMatchInternal (sets, nsets, p, result);
764 return FcFontRenderPrepare (config, p, best);
770 FcFontMatch (FcConfig *config,
779 assert (result != NULL);
781 *result = FcResultNoMatch;
785 config = FcConfigGetCurrent ();
790 if (config->fonts[FcSetSystem])
791 sets[nsets++] = config->fonts[FcSetSystem];
792 if (config->fonts[FcSetApplication])
793 sets[nsets++] = config->fonts[FcSetApplication];
795 best = FcFontSetMatchInternal (sets, nsets, p, result);
797 return FcFontRenderPrepare (config, p, best);
802 typedef struct _FcSortNode {
804 double score[PRI_END];
808 FcSortCompare (const void *aa, const void *ab)
810 FcSortNode *a = *(FcSortNode **) aa;
811 FcSortNode *b = *(FcSortNode **) ab;
812 double *as = &a->score[0];
813 double *bs = &b->score[0];
814 double ad = 0, bd = 0;
818 while (i-- && (ad = *as++) == (bd = *bs++))
820 return ad < bd ? -1 : ad > bd ? 1 : 0;
824 FcSortWalk (FcSortNode **n, int nnode, FcFontSet *fs, FcCharSet **csp, FcBool trim)
826 FcBool ret = FcFalse;
833 cs = FcCharSetCreate ();
838 for (i = 0; i < nnode; i++)
840 FcSortNode *node = *n++;
841 FcBool adds_chars = FcFalse;
844 * Only fetch node charset if we'd need it
850 if (FcPatternGetCharSet (node->pattern, FC_CHARSET, 0, &ncs) !=
854 if (!FcCharSetMerge (cs, ncs, &adds_chars))
859 * If this font isn't a subset of the previous fonts,
862 if (!i || !trim || adds_chars)
864 FcPatternReference (node->pattern);
865 if (FcDebug () & FC_DBG_MATCHV)
868 FcPatternPrint (node->pattern);
870 if (!FcFontSetAdd (fs, node->pattern))
872 FcPatternDestroy (node->pattern);
887 FcCharSetDestroy (cs);
893 FcFontSetSortDestroy (FcFontSet *fs)
895 FcFontSetDestroy (fs);
899 FcFontSetSort (FcConfig *config FC_UNUSED,
910 FcSortNode **nodeps, **nodep;
917 FcBool *patternLangSat;
920 assert (sets != NULL);
922 assert (result != NULL);
924 /* There are some implementation that relying on the result of
925 * "result" to check if the return value of FcFontSetSort
927 * So we should initialize it to the conservative way since
928 * this function doesn't return NULL anymore.
931 *result = FcResultNoMatch;
933 if (FcDebug () & FC_DBG_MATCH)
939 for (set = 0; set < nsets; set++)
947 return FcFontSetCreate ();
949 for (nPatternLang = 0;
950 FcPatternGet (p, FC_LANG, nPatternLang, &patternLang) == FcResultMatch;
955 nodes = malloc (nnodes * sizeof (FcSortNode) +
956 nnodes * sizeof (FcSortNode *) +
957 nPatternLang * sizeof (FcBool));
960 nodeps = (FcSortNode **) (nodes + nnodes);
961 patternLangSat = (FcBool *) (nodeps + nnodes);
965 for (set = 0; set < nsets; set++)
970 for (f = 0; f < s->nfont; f++)
972 if (FcDebug () & FC_DBG_MATCHV)
974 printf ("Font %d ", f);
975 FcPatternPrint (s->fonts[f]);
977 new->pattern = s->fonts[f];
978 if (!FcCompare (p, new->pattern, new->score, result))
980 if (FcDebug () & FC_DBG_MATCHV)
983 for (i = 0; i < PRI_END; i++)
985 printf (" %g", new->score[i]);
995 nnodes = new - nodes;
997 qsort (nodeps, nnodes, sizeof (FcSortNode *),
1000 for (i = 0; i < nPatternLang; i++)
1001 patternLangSat[i] = FcFalse;
1003 for (f = 0; f < nnodes; f++)
1005 FcBool satisfies = FcFalse;
1007 * If this node matches any language, go check
1008 * which ones and satisfy those entries
1010 if (nodeps[f]->score[PRI_LANG] < 2000)
1012 for (i = 0; i < nPatternLang; i++)
1016 if (!patternLangSat[i] &&
1017 FcPatternGet (p, FC_LANG, i, &patternLang) == FcResultMatch &&
1018 FcPatternGet (nodeps[f]->pattern, FC_LANG, 0, &nodeLang) == FcResultMatch)
1020 double compare = FcCompareLang (&patternLang, &nodeLang);
1021 if (compare >= 0 && compare < 2)
1023 if (FcDebug () & FC_DBG_MATCHV)
1028 if (FcPatternGetString (nodeps[f]->pattern, FC_FAMILY, 0, &family) == FcResultMatch &&
1029 FcPatternGetString (nodeps[f]->pattern, FC_STYLE, 0, &style) == FcResultMatch)
1030 printf ("Font %s:%s matches language %d\n", family, style, i);
1032 patternLangSat[i] = FcTrue;
1041 nodeps[f]->score[PRI_LANG] = 10000.0;
1046 * Re-sort once the language issues have been settled
1048 qsort (nodeps, nnodes, sizeof (FcSortNode *),
1051 ret = FcFontSetCreate ();
1055 if (!FcSortWalk (nodeps, nnodes, ret, csp, trim))
1060 if (FcDebug() & FC_DBG_MATCH)
1062 printf ("First font ");
1063 FcPatternPrint (ret->fonts[0]);
1066 *result = FcResultMatch;
1071 FcFontSetDestroy (ret);
1079 FcFontSort (FcConfig *config,
1089 assert (result != NULL);
1091 *result = FcResultNoMatch;
1095 config = FcConfigGetCurrent ();
1100 if (config->fonts[FcSetSystem])
1101 sets[nsets++] = config->fonts[FcSetSystem];
1102 if (config->fonts[FcSetApplication])
1103 sets[nsets++] = config->fonts[FcSetApplication];
1104 return FcFontSetSort (config, sets, nsets, p, trim, csp, result);
1107 #include "fcaliastail.h"