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 FcCompareFilename (FcValue *v1, FcValue *v2)
194 const FcChar8 *s1 = FcValueString (v1), *s2 = FcValueString (v2);
195 if (FcStrCmp (s1, s2) == 0)
197 else if (FcStrCmpIgnoreCase (s1, s2) == 0)
199 else if (FcStrGlobMatch (s1, s2))
206 FcCompareHash (FcValue *v1, FcValue *v2)
208 const FcChar8 *s1 = FcValueString (v1), *s2 = FcValueString (v2);
210 /* Do not match an empty string */
211 if (!s1 || !s2 || !s1[0] || !s2[0])
213 return FcCompareString (v1, v2);
216 #define PRI_NULL(n) \
217 PRI_ ## n ## _STRONG = -1, \
218 PRI_ ## n ## _WEAK = -1,
220 #define PRI_FcCompareFamily(n) PRI1(n)
221 #define PRI_FcCompareString(n) PRI1(n)
222 #define PRI_FcCompareNumber(n) PRI1(n)
223 #define PRI_FcCompareSize(n) PRI1(n)
224 #define PRI_FcCompareBool(n) PRI1(n)
225 #define PRI_FcCompareFilename(n) PRI1(n)
226 #define PRI_FcCompareCharSet(n) PRI1(n)
227 #define PRI_FcCompareLang(n) PRI1(n)
228 #define PRI_FcComparePostScript(n) PRI1(n)
229 #define PRI_FcCompareHash(n) PRI1(n)
231 #define FC_OBJECT(NAME, Type, Cmp) PRI_##Cmp(NAME)
233 typedef enum _FcMatcherPriorityDummy {
235 } FcMatcherPriorityDummy;
242 PRI_ ## n ## _STRONG = PRI_ ## n, \
243 PRI_ ## n ## _WEAK = PRI_ ## n
245 typedef enum _FcMatcherPriority {
253 PRI_POSTSCRIPT_NAME_STRONG,
256 PRI_POSTSCRIPT_NAME_WEAK,
273 typedef struct _FcMatcher {
275 double (*compare) (FcValue *value1, FcValue *value2);
280 * Order is significant, it defines the precedence of
281 * each value, earlier values are more significant than
284 #define FC_OBJECT(NAME, Type, Cmp) { FC_##NAME##_OBJECT, Cmp, PRI_##NAME##_STRONG, PRI_##NAME##_WEAK },
285 static const FcMatcher _FcMatchers [] = {
286 { FC_INVALID_OBJECT, NULL, -1, -1 },
291 static const FcMatcher*
292 FcObjectToMatcher (FcObject object,
298 case FC_FAMILYLANG_OBJECT:
299 case FC_STYLELANG_OBJECT:
300 case FC_FULLNAMELANG_OBJECT:
301 object = FC_LANG_OBJECT;
305 if (object > FC_MAX_BASE_OBJECT ||
306 !_FcMatchers[object].compare ||
307 _FcMatchers[object].strong == -1 ||
308 _FcMatchers[object].weak == -1)
311 return _FcMatchers + object;
315 FcCompareValueList (FcObject object,
316 const FcMatcher *match,
317 FcValueListPtr v1orig, /* pattern */
318 FcValueListPtr v2orig, /* target */
324 FcValueListPtr v1, v2;
325 double v, best, bestStrong, bestWeak;
331 *bestValue = FcValueCanonicalize(&v2orig->value);
341 for (v1 = v1orig; v1; v1 = FcValueListNext(v1))
343 for (v2 = v2orig, k = 0; v2; v2 = FcValueListNext(v2), k++)
345 v = (match->compare) (&v1->value, &v2->value);
348 *result = FcResultTypeMismatch;
355 *bestValue = FcValueCanonicalize(&v2->value);
359 if (v1->binding == FcValueBindingStrong)
372 if (FcDebug () & FC_DBG_MATCHV)
374 printf (" %s: %g ", FcObjectName (object), best);
375 FcValueListPrint (v1orig);
377 FcValueListPrint (v2orig);
382 int weak = match->weak;
383 int strong = match->strong;
385 value[strong] += best;
388 value[weak] += bestWeak;
389 value[strong] += bestStrong;
399 * Return a value indicating the distance between the two lists of
404 FcCompare (FcPattern *pat,
411 for (i = 0; i < PRI_END; i++)
416 while (i1 < pat->num && i2 < fnt->num)
418 FcPatternElt *elt_i1 = &FcPatternElts(pat)[i1];
419 FcPatternElt *elt_i2 = &FcPatternElts(fnt)[i2];
421 i = FcObjectCompare(elt_i1->object, elt_i2->object);
428 const FcMatcher *match = FcObjectToMatcher (elt_i1->object, FcFalse);
429 if (!FcCompareValueList (elt_i1->object, match,
430 FcPatternEltValues(elt_i1),
431 FcPatternEltValues(elt_i2),
432 NULL, value, NULL, result))
442 FcFontRenderPrepare (FcConfig *config,
448 FcPatternElt *fe, *pe, *fel, *pel;
452 assert (pat != NULL);
453 assert (font != NULL);
455 new = FcPatternCreate ();
458 for (i = 0; i < font->num; i++)
460 fe = &FcPatternElts(font)[i];
461 if (fe->object == FC_FAMILYLANG_OBJECT ||
462 fe->object == FC_STYLELANG_OBJECT ||
463 fe->object == FC_FULLNAMELANG_OBJECT)
465 /* ignore those objects. we need to deal with them
469 if (fe->object == FC_FAMILY_OBJECT ||
470 fe->object == FC_STYLE_OBJECT ||
471 fe->object == FC_FULLNAME_OBJECT)
473 FC_ASSERT_STATIC ((FC_FAMILY_OBJECT + 1) == FC_FAMILYLANG_OBJECT);
474 FC_ASSERT_STATIC ((FC_STYLE_OBJECT + 1) == FC_STYLELANG_OBJECT);
475 FC_ASSERT_STATIC ((FC_FULLNAME_OBJECT + 1) == FC_FULLNAMELANG_OBJECT);
477 fel = FcPatternObjectFindElt (font, fe->object + 1);
478 pel = FcPatternObjectFindElt (pat, fe->object + 1);
485 pe = FcPatternObjectFindElt (pat, fe->object);
488 const FcMatcher *match = FcObjectToMatcher (pe->object, FcFalse);
490 if (!FcCompareValueList (pe->object, match,
491 FcPatternEltValues(pe),
492 FcPatternEltValues(fe), &v, NULL, NULL, &result))
494 FcPatternDestroy (new);
500 FcValueListPtr l1, l2, ln = NULL, ll = NULL;
502 match = FcObjectToMatcher (pel->object, FcTrue);
503 if (!FcCompareValueList (pel->object, match,
504 FcPatternEltValues (pel),
505 FcPatternEltValues (fel), NULL, NULL, &n, &result))
507 FcPatternDestroy (new);
511 for (j = 0, l1 = FcPatternEltValues (fe), l2 = FcPatternEltValues (fel);
512 l1 != NULL || l2 != NULL;
513 j++, l1 = l1 ? FcValueListNext (l1) : NULL, l2 = l2 ? FcValueListNext (l2) : NULL)
518 ln = FcValueListPrepend (ln,
519 FcValueCanonicalize (&l1->value),
520 FcValueBindingStrong);
522 ll = FcValueListPrepend (ll,
523 FcValueCanonicalize (&l2->value),
524 FcValueBindingStrong);
529 ln = FcValueListAppend (ln,
530 FcValueCanonicalize (&l1->value),
531 FcValueBindingStrong);
533 ll = FcValueListAppend (ll,
534 FcValueCanonicalize (&l2->value),
535 FcValueBindingStrong);
538 FcPatternObjectListAdd (new, fe->object, ln, FcFalse);
539 FcPatternObjectListAdd (new, fel->object, ll, FcFalse);
545 FcValueListPtr l1, l2;
548 l1 = FcValueListDuplicate (FcPatternEltValues (fe));
549 l2 = FcValueListDuplicate (FcPatternEltValues (fel));
550 FcPatternObjectListAdd (new, fe->object, l1, FcFalse);
551 FcPatternObjectListAdd (new, fel->object, l2, FcFalse);
555 FcPatternObjectAdd (new, fe->object, v, FcFalse);
561 FcPatternObjectListAdd (new, fe->object,
562 FcValueListDuplicate (FcPatternEltValues (fe)),
566 for (i = 0; i < pat->num; i++)
568 pe = &FcPatternElts(pat)[i];
569 fe = FcPatternObjectFindElt (font, pe->object);
571 pe->object != FC_FAMILYLANG_OBJECT &&
572 pe->object != FC_STYLELANG_OBJECT &&
573 pe->object != FC_FULLNAMELANG_OBJECT)
575 FcPatternObjectListAdd (new, pe->object,
576 FcValueListDuplicate (FcPatternEltValues(pe)),
581 FcConfigSubstituteWithPat (config, new, pat, FcMatchFont);
586 FcFontSetMatchInternal (FcFontSet **sets,
591 double score[PRI_END], bestscore[PRI_END];
598 for (i = 0; i < PRI_END; i++)
601 if (FcDebug () & FC_DBG_MATCH)
606 for (set = 0; set < nsets; set++)
611 for (f = 0; f < s->nfont; f++)
613 if (FcDebug () & FC_DBG_MATCHV)
615 printf ("Font %d ", f);
616 FcPatternPrint (s->fonts[f]);
618 if (!FcCompare (p, s->fonts[f], score, result))
620 if (FcDebug () & FC_DBG_MATCHV)
623 for (i = 0; i < PRI_END; i++)
625 printf (" %g", score[i]);
629 for (i = 0; i < PRI_END; i++)
631 if (best && bestscore[i] < score[i])
633 if (!best || score[i] < bestscore[i])
635 for (i = 0; i < PRI_END; i++)
636 bestscore[i] = score[i];
643 if (FcDebug () & FC_DBG_MATCH)
645 printf ("Best score");
646 for (i = 0; i < PRI_END; i++)
647 printf (" %g", bestscore[i]);
649 FcPatternPrint (best);
651 /* assuming that 'result' is initialized with FcResultNoMatch
652 * outside this function */
654 *result = FcResultMatch;
660 FcFontSetMatch (FcConfig *config,
668 assert (sets != NULL);
670 assert (result != NULL);
672 *result = FcResultNoMatch;
676 config = FcConfigGetCurrent ();
680 best = FcFontSetMatchInternal (sets, nsets, p, result);
682 return FcFontRenderPrepare (config, p, best);
688 FcFontMatch (FcConfig *config,
697 assert (result != NULL);
699 *result = FcResultNoMatch;
703 config = FcConfigGetCurrent ();
708 if (config->fonts[FcSetSystem])
709 sets[nsets++] = config->fonts[FcSetSystem];
710 if (config->fonts[FcSetApplication])
711 sets[nsets++] = config->fonts[FcSetApplication];
713 best = FcFontSetMatchInternal (sets, nsets, p, result);
715 return FcFontRenderPrepare (config, p, best);
720 typedef struct _FcSortNode {
722 double score[PRI_END];
726 FcSortCompare (const void *aa, const void *ab)
728 FcSortNode *a = *(FcSortNode **) aa;
729 FcSortNode *b = *(FcSortNode **) ab;
730 double *as = &a->score[0];
731 double *bs = &b->score[0];
732 double ad = 0, bd = 0;
736 while (i-- && (ad = *as++) == (bd = *bs++))
738 return ad < bd ? -1 : ad > bd ? 1 : 0;
742 FcSortWalk (FcSortNode **n, int nnode, FcFontSet *fs, FcCharSet **csp, FcBool trim)
744 FcBool ret = FcFalse;
750 cs = FcCharSetCreate ();
757 FcSortNode *node = *n++;
758 FcBool adds_chars = FcFalse;
761 * Only fetch node charset if we'd need it
767 if (FcPatternGetCharSet (node->pattern, FC_CHARSET, 0, &ncs) !=
771 if (!FcCharSetMerge (cs, ncs, &adds_chars))
776 * If this font isn't a subset of the previous fonts,
779 if (!trim || adds_chars)
781 FcPatternReference (node->pattern);
782 if (FcDebug () & FC_DBG_MATCHV)
785 FcPatternPrint (node->pattern);
787 if (!FcFontSetAdd (fs, node->pattern))
789 FcPatternDestroy (node->pattern);
804 FcCharSetDestroy (cs);
810 FcFontSetSortDestroy (FcFontSet *fs)
812 FcFontSetDestroy (fs);
816 FcFontSetSort (FcConfig *config FC_UNUSED,
827 FcSortNode **nodeps, **nodep;
834 FcBool *patternLangSat;
837 assert (sets != NULL);
839 assert (result != NULL);
841 /* There are some implementation that relying on the result of
842 * "result" to check if the return value of FcFontSetSort
844 * So we should initialize it to the conservative way since
845 * this function doesn't return NULL anymore.
848 *result = FcResultNoMatch;
850 if (FcDebug () & FC_DBG_MATCH)
856 for (set = 0; set < nsets; set++)
864 return FcFontSetCreate ();
866 for (nPatternLang = 0;
867 FcPatternGet (p, FC_LANG, nPatternLang, &patternLang) == FcResultMatch;
872 nodes = malloc (nnodes * sizeof (FcSortNode) +
873 nnodes * sizeof (FcSortNode *) +
874 nPatternLang * sizeof (FcBool));
877 nodeps = (FcSortNode **) (nodes + nnodes);
878 patternLangSat = (FcBool *) (nodeps + nnodes);
882 for (set = 0; set < nsets; set++)
887 for (f = 0; f < s->nfont; f++)
889 if (FcDebug () & FC_DBG_MATCHV)
891 printf ("Font %d ", f);
892 FcPatternPrint (s->fonts[f]);
894 new->pattern = s->fonts[f];
895 if (!FcCompare (p, new->pattern, new->score, result))
897 if (FcDebug () & FC_DBG_MATCHV)
900 for (i = 0; i < PRI_END; i++)
902 printf (" %g", new->score[i]);
912 nnodes = new - nodes;
914 qsort (nodeps, nnodes, sizeof (FcSortNode *),
917 for (i = 0; i < nPatternLang; i++)
918 patternLangSat[i] = FcFalse;
920 for (f = 0; f < nnodes; f++)
922 FcBool satisfies = FcFalse;
924 * If this node matches any language, go check
925 * which ones and satisfy those entries
927 if (nodeps[f]->score[PRI_LANG] < 2000)
929 for (i = 0; i < nPatternLang; i++)
933 if (!patternLangSat[i] &&
934 FcPatternGet (p, FC_LANG, i, &patternLang) == FcResultMatch &&
935 FcPatternGet (nodeps[f]->pattern, FC_LANG, 0, &nodeLang) == FcResultMatch)
937 double compare = FcCompareLang (&patternLang, &nodeLang);
938 if (compare >= 0 && compare < 2)
940 if (FcDebug () & FC_DBG_MATCHV)
945 if (FcPatternGetString (nodeps[f]->pattern, FC_FAMILY, 0, &family) == FcResultMatch &&
946 FcPatternGetString (nodeps[f]->pattern, FC_STYLE, 0, &style) == FcResultMatch)
947 printf ("Font %s:%s matches language %d\n", family, style, i);
949 patternLangSat[i] = FcTrue;
958 nodeps[f]->score[PRI_LANG] = 10000.0;
963 * Re-sort once the language issues have been settled
965 qsort (nodeps, nnodes, sizeof (FcSortNode *),
968 ret = FcFontSetCreate ();
972 if (!FcSortWalk (nodeps, nnodes, ret, csp, trim))
977 if (FcDebug() & FC_DBG_MATCH)
979 printf ("First font ");
980 FcPatternPrint (ret->fonts[0]);
983 *result = FcResultMatch;
988 FcFontSetDestroy (ret);
996 FcFontSort (FcConfig *config,
1006 assert (result != NULL);
1008 *result = FcResultNoMatch;
1012 config = FcConfigGetCurrent ();
1017 if (config->fonts[FcSetSystem])
1018 sets[nsets++] = config->fonts[FcSetSystem];
1019 if (config->fonts[FcSetApplication])
1020 sets[nsets++] = config->fonts[FcSetApplication];
1021 return FcFontSetSort (config, sets, nsets, p, trim, csp, result);
1024 #include "fcaliastail.h"