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 FcCompareLang (FcValue *v1, FcValue *v2)
83 FcValue value1 = FcValueCanonicalize(v1), value2 = FcValueCanonicalize(v2);
85 switch ((int) value1.type) {
87 switch ((int) value2.type) {
89 result = FcLangSetCompare (value1.u.l, value2.u.l);
92 result = FcLangSetHasLang (value1.u.l,
100 switch ((int) value2.type) {
102 result = FcLangSetHasLang (value2.u.l, value1.u.s);
105 result = FcLangCompare (value1.u.s,
118 case FcLangDifferentCountry:
120 case FcLangDifferentLang:
127 FcCompareBool (FcValue *v1, FcValue *v2)
129 if (v2->type != FcTypeBool || v1->type != FcTypeBool)
131 return (double) v2->u.b != v1->u.b;
135 FcCompareCharSet (FcValue *v1, FcValue *v2)
137 return (double) FcCharSetSubtractCount (FcValueCharSet(v1), FcValueCharSet(v2));
141 FcCompareSize (FcValue *value1, FcValue *value2)
145 switch ((int) value1->type) {
155 switch ((int) value2->type) {
174 FcCompareFilename (FcValue *v1, FcValue *v2)
176 const FcChar8 *s1 = FcValueString (v1), *s2 = FcValueString (v2);
177 if (FcStrCmp (s1, s2) == 0)
179 else if (FcStrCmpIgnoreCase (s1, s2) == 0)
181 else if (FcStrRegexCmp (s2, s1))
183 else if (FcStrRegexCmpIgnoreCase (s2, s1))
189 #define PRI_NULL(n) \
190 PRI_ ## n ## _STRONG = -1, \
191 PRI_ ## n ## _WEAK = -1,
193 #define PRI_FcCompareFamily(n) PRI1(n)
194 #define PRI_FcCompareString(n) PRI1(n)
195 #define PRI_FcCompareNumber(n) PRI1(n)
196 #define PRI_FcCompareSize(n) PRI1(n)
197 #define PRI_FcCompareBool(n) PRI1(n)
198 #define PRI_FcCompareFilename(n) PRI1(n)
199 #define PRI_FcCompareCharSet(n) PRI1(n)
200 #define PRI_FcCompareLang(n) PRI1(n)
202 #define FC_OBJECT(NAME, Type, Cmp) PRI_##Cmp(NAME)
204 typedef enum _FcMatcherPriorityDummy {
206 } FcMatcherPriorityDummy;
212 PRI_ ## n ## _STRONG, \
215 typedef enum _FcMatcherPriority {
240 typedef struct _FcMatcher {
242 double (*compare) (FcValue *value1, FcValue *value2);
247 * Order is significant, it defines the precedence of
248 * each value, earlier values are more significant than
251 #define FC_OBJECT(NAME, Type, Cmp) { FC_##NAME##_OBJECT, Cmp, PRI_##NAME##_STRONG, PRI_##NAME##_WEAK },
252 static const FcMatcher _FcMatchers [] = {
253 { FC_INVALID_OBJECT, NULL, -1, -1 },
258 static const FcMatcher*
259 FcObjectToMatcher (FcObject object,
265 case FC_FAMILYLANG_OBJECT:
266 case FC_STYLELANG_OBJECT:
267 case FC_FULLNAMELANG_OBJECT:
268 object = FC_LANG_OBJECT;
272 if (object > FC_MAX_BASE_OBJECT ||
273 !_FcMatchers[object].compare ||
274 _FcMatchers[object].strong == -1 ||
275 _FcMatchers[object].weak == -1)
278 return _FcMatchers + object;
282 FcCompareValueList (FcObject object,
283 const FcMatcher *match,
284 FcValueListPtr v1orig, /* pattern */
285 FcValueListPtr v2orig, /* target */
291 FcValueListPtr v1, v2;
292 double v, best, bestStrong, bestWeak;
298 *bestValue = FcValueCanonicalize(&v2orig->value);
308 for (v1 = v1orig; v1; v1 = FcValueListNext(v1))
310 for (v2 = v2orig, k = 0; v2; v2 = FcValueListNext(v2), k++)
312 v = (match->compare) (&v1->value, &v2->value);
315 *result = FcResultTypeMismatch;
322 *bestValue = FcValueCanonicalize(&v2->value);
326 if (v1->binding == FcValueBindingStrong)
339 if (FcDebug () & FC_DBG_MATCHV)
341 printf (" %s: %g ", FcObjectName (object), best);
342 FcValueListPrint (v1orig);
344 FcValueListPrint (v2orig);
349 int weak = match->weak;
350 int strong = match->strong;
352 value[strong] += best;
355 value[weak] += bestWeak;
356 value[strong] += bestStrong;
366 * Return a value indicating the distance between the two lists of
371 FcCompare (FcPattern *pat,
378 for (i = 0; i < PRI_END; i++)
383 while (i1 < pat->num && i2 < fnt->num)
385 FcPatternElt *elt_i1 = &FcPatternElts(pat)[i1];
386 FcPatternElt *elt_i2 = &FcPatternElts(fnt)[i2];
388 i = FcObjectCompare(elt_i1->object, elt_i2->object);
395 const FcMatcher *match = FcObjectToMatcher (elt_i1->object, FcFalse);
396 if (!FcCompareValueList (elt_i1->object, match,
397 FcPatternEltValues(elt_i1),
398 FcPatternEltValues(elt_i2),
399 NULL, value, NULL, result))
409 FcFontRenderPrepare (FcConfig *config,
415 FcPatternElt *fe, *pe, *fel, *pel;
419 assert (pat != NULL);
420 assert (font != NULL);
422 new = FcPatternCreate ();
425 for (i = 0; i < font->num; i++)
427 fe = &FcPatternElts(font)[i];
428 if (fe->object == FC_FAMILYLANG_OBJECT ||
429 fe->object == FC_STYLELANG_OBJECT ||
430 fe->object == FC_FULLNAMELANG_OBJECT)
432 /* ignore those objects. we need to deal with them
436 if (fe->object == FC_FAMILY_OBJECT ||
437 fe->object == FC_STYLE_OBJECT ||
438 fe->object == FC_FULLNAME_OBJECT)
440 FC_ASSERT_STATIC ((FC_FAMILY_OBJECT + 1) == FC_FAMILYLANG_OBJECT);
441 FC_ASSERT_STATIC ((FC_STYLE_OBJECT + 1) == FC_STYLELANG_OBJECT);
442 FC_ASSERT_STATIC ((FC_FULLNAME_OBJECT + 1) == FC_FULLNAMELANG_OBJECT);
444 fel = FcPatternObjectFindElt (font, fe->object + 1);
445 pel = FcPatternObjectFindElt (pat, fe->object + 1);
452 pe = FcPatternObjectFindElt (pat, fe->object);
455 const FcMatcher *match = FcObjectToMatcher (pe->object, FcFalse);
457 if (!FcCompareValueList (pe->object, match,
458 FcPatternEltValues(pe),
459 FcPatternEltValues(fe), &v, NULL, NULL, &result))
461 FcPatternDestroy (new);
467 FcValueListPtr l1, l2, ln = NULL, ll = NULL;
469 match = FcObjectToMatcher (pel->object, FcTrue);
470 if (!FcCompareValueList (pel->object, match,
471 FcPatternEltValues (pel),
472 FcPatternEltValues (fel), NULL, NULL, &n, &result))
474 FcPatternDestroy (new);
478 for (j = 0, l1 = FcPatternEltValues (fe), l2 = FcPatternEltValues (fel);
479 l1 != NULL || l2 != NULL;
480 j++, l1 = l1 ? FcValueListNext (l1) : NULL, l2 = l2 ? FcValueListNext (l2) : NULL)
485 ln = FcValueListPrepend (ln,
486 FcValueCanonicalize (&l1->value),
487 FcValueBindingStrong);
489 ll = FcValueListPrepend (ll,
490 FcValueCanonicalize (&l2->value),
491 FcValueBindingStrong);
496 ln = FcValueListAppend (ln,
497 FcValueCanonicalize (&l1->value),
498 FcValueBindingStrong);
500 ll = FcValueListAppend (ll,
501 FcValueCanonicalize (&l2->value),
502 FcValueBindingStrong);
505 FcPatternObjectListAdd (new, fe->object, ln, FcFalse);
506 FcPatternObjectListAdd (new, fel->object, ll, FcFalse);
512 FcValueListPtr l1, l2;
515 l1 = FcValueListDuplicate (FcPatternEltValues (fe));
516 l2 = FcValueListDuplicate (FcPatternEltValues (fel));
517 FcPatternObjectListAdd (new, fe->object, l1, FcFalse);
518 FcPatternObjectListAdd (new, fel->object, l2, FcFalse);
527 v = FcValueCanonicalize(&FcPatternEltValues (fe)->value);
529 FcPatternObjectAdd (new, fe->object, v, FcFalse);
531 for (i = 0; i < pat->num; i++)
533 pe = &FcPatternElts(pat)[i];
534 fe = FcPatternObjectFindElt (font, pe->object);
537 FcPatternObjectListAdd (new, pe->object,
538 FcValueListDuplicate (FcPatternEltValues(pe)),
543 FcConfigSubstituteWithPat (config, new, pat, FcMatchFont);
548 FcFontSetMatchInternal (FcFontSet **sets,
553 double score[PRI_END], bestscore[PRI_END];
560 for (i = 0; i < PRI_END; i++)
563 if (FcDebug () & FC_DBG_MATCH)
568 for (set = 0; set < nsets; set++)
573 for (f = 0; f < s->nfont; f++)
575 if (FcDebug () & FC_DBG_MATCHV)
577 printf ("Font %d ", f);
578 FcPatternPrint (s->fonts[f]);
580 if (!FcCompare (p, s->fonts[f], score, result))
582 if (FcDebug () & FC_DBG_MATCHV)
585 for (i = 0; i < PRI_END; i++)
587 printf (" %g", score[i]);
591 for (i = 0; i < PRI_END; i++)
593 if (best && bestscore[i] < score[i])
595 if (!best || score[i] < bestscore[i])
597 for (i = 0; i < PRI_END; i++)
598 bestscore[i] = score[i];
605 if (FcDebug () & FC_DBG_MATCH)
607 printf ("Best score");
608 for (i = 0; i < PRI_END; i++)
609 printf (" %g", bestscore[i]);
611 FcPatternPrint (best);
613 /* assuming that 'result' is initialized with FcResultNoMatch
614 * outside this function */
616 *result = FcResultMatch;
622 FcFontSetMatch (FcConfig *config,
630 assert (sets != NULL);
632 assert (result != NULL);
634 *result = FcResultNoMatch;
638 config = FcConfigGetCurrent ();
642 best = FcFontSetMatchInternal (sets, nsets, p, result);
644 return FcFontRenderPrepare (config, p, best);
650 FcFontMatch (FcConfig *config,
659 assert (result != NULL);
661 *result = FcResultNoMatch;
665 config = FcConfigGetCurrent ();
670 if (config->fonts[FcSetSystem])
671 sets[nsets++] = config->fonts[FcSetSystem];
672 if (config->fonts[FcSetApplication])
673 sets[nsets++] = config->fonts[FcSetApplication];
675 best = FcFontSetMatchInternal (sets, nsets, p, result);
677 return FcFontRenderPrepare (config, p, best);
682 typedef struct _FcSortNode {
684 double score[PRI_END];
688 FcSortCompare (const void *aa, const void *ab)
690 FcSortNode *a = *(FcSortNode **) aa;
691 FcSortNode *b = *(FcSortNode **) ab;
692 double *as = &a->score[0];
693 double *bs = &b->score[0];
694 double ad = 0, bd = 0;
698 while (i-- && (ad = *as++) == (bd = *bs++))
700 return ad < bd ? -1 : ad > bd ? 1 : 0;
704 FcSortWalk (FcSortNode **n, int nnode, FcFontSet *fs, FcCharSet **csp, FcBool trim)
706 FcBool ret = FcFalse;
712 cs = FcCharSetCreate ();
719 FcSortNode *node = *n++;
720 FcBool adds_chars = FcFalse;
723 * Only fetch node charset if we'd need it
729 if (FcPatternGetCharSet (node->pattern, FC_CHARSET, 0, &ncs) !=
733 if (!FcCharSetMerge (cs, ncs, &adds_chars))
738 * If this font isn't a subset of the previous fonts,
741 if (!trim || adds_chars)
743 FcPatternReference (node->pattern);
744 if (FcDebug () & FC_DBG_MATCHV)
747 FcPatternPrint (node->pattern);
749 if (!FcFontSetAdd (fs, node->pattern))
751 FcPatternDestroy (node->pattern);
766 FcCharSetDestroy (cs);
772 FcFontSetSortDestroy (FcFontSet *fs)
774 FcFontSetDestroy (fs);
778 FcFontSetSort (FcConfig *config FC_UNUSED,
789 FcSortNode **nodeps, **nodep;
796 FcBool *patternLangSat;
799 assert (sets != NULL);
801 assert (result != NULL);
803 /* There are some implementation that relying on the result of
804 * "result" to check if the return value of FcFontSetSort
806 * So we should initialize it to the conservative way since
807 * this function doesn't return NULL anymore.
810 *result = FcResultNoMatch;
812 if (FcDebug () & FC_DBG_MATCH)
818 for (set = 0; set < nsets; set++)
826 return FcFontSetCreate ();
828 for (nPatternLang = 0;
829 FcPatternGet (p, FC_LANG, nPatternLang, &patternLang) == FcResultMatch;
834 nodes = malloc (nnodes * sizeof (FcSortNode) +
835 nnodes * sizeof (FcSortNode *) +
836 nPatternLang * sizeof (FcBool));
839 nodeps = (FcSortNode **) (nodes + nnodes);
840 patternLangSat = (FcBool *) (nodeps + nnodes);
844 for (set = 0; set < nsets; set++)
849 for (f = 0; f < s->nfont; f++)
851 if (FcDebug () & FC_DBG_MATCHV)
853 printf ("Font %d ", f);
854 FcPatternPrint (s->fonts[f]);
856 new->pattern = s->fonts[f];
857 if (!FcCompare (p, new->pattern, new->score, result))
859 if (FcDebug () & FC_DBG_MATCHV)
862 for (i = 0; i < PRI_END; i++)
864 printf (" %g", new->score[i]);
874 nnodes = new - nodes;
876 qsort (nodeps, nnodes, sizeof (FcSortNode *),
879 for (i = 0; i < nPatternLang; i++)
880 patternLangSat[i] = FcFalse;
882 for (f = 0; f < nnodes; f++)
884 FcBool satisfies = FcFalse;
886 * If this node matches any language, go check
887 * which ones and satisfy those entries
889 if (nodeps[f]->score[PRI_LANG_STRONG] < 2000 ||
890 nodeps[f]->score[PRI_LANG_WEAK] < 2000)
892 for (i = 0; i < nPatternLang; i++)
896 if (!patternLangSat[i] &&
897 FcPatternGet (p, FC_LANG, i, &patternLang) == FcResultMatch &&
898 FcPatternGet (nodeps[f]->pattern, FC_LANG, 0, &nodeLang) == FcResultMatch)
900 double compare = FcCompareLang (&patternLang, &nodeLang);
901 if (compare >= 0 && compare < 2)
903 if (FcDebug () & FC_DBG_MATCHV)
908 if (FcPatternGetString (nodeps[f]->pattern, FC_FAMILY, 0, &family) == FcResultMatch &&
909 FcPatternGetString (nodeps[f]->pattern, FC_STYLE, 0, &style) == FcResultMatch)
910 printf ("Font %s:%s matches language %d\n", family, style, i);
912 patternLangSat[i] = FcTrue;
921 nodeps[f]->score[PRI_LANG_STRONG] = 10000.0;
922 nodeps[f]->score[PRI_LANG_WEAK] = 10000.0;
927 * Re-sort once the language issues have been settled
929 qsort (nodeps, nnodes, sizeof (FcSortNode *),
932 ret = FcFontSetCreate ();
936 if (!FcSortWalk (nodeps, nnodes, ret, csp, trim))
941 if (FcDebug() & FC_DBG_MATCH)
943 printf ("First font ");
944 FcPatternPrint (ret->fonts[0]);
947 *result = FcResultMatch;
952 FcFontSetDestroy (ret);
960 FcFontSort (FcConfig *config,
970 assert (result != NULL);
972 *result = FcResultNoMatch;
976 config = FcConfigGetCurrent ();
981 if (config->fonts[FcSetSystem])
982 sets[nsets++] = config->fonts[FcSetSystem];
983 if (config->fonts[FcSetApplication])
984 sets[nsets++] = config->fonts[FcSetApplication];
985 return FcFontSetSort (config, sets, nsets, p, trim, csp, result);
988 #include "fcaliastail.h"