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);
560 v = FcValueCanonicalize(&FcPatternEltValues (fe)->value);
562 FcPatternObjectAdd (new, fe->object, v, FcFalse);
564 for (i = 0; i < pat->num; i++)
566 pe = &FcPatternElts(pat)[i];
567 fe = FcPatternObjectFindElt (font, pe->object);
569 pe->object != FC_FAMILYLANG_OBJECT &&
570 pe->object != FC_STYLELANG_OBJECT &&
571 pe->object != FC_FULLNAMELANG_OBJECT)
573 FcPatternObjectListAdd (new, pe->object,
574 FcValueListDuplicate (FcPatternEltValues(pe)),
579 FcConfigSubstituteWithPat (config, new, pat, FcMatchFont);
584 FcFontSetMatchInternal (FcFontSet **sets,
589 double score[PRI_END], bestscore[PRI_END];
596 for (i = 0; i < PRI_END; i++)
599 if (FcDebug () & FC_DBG_MATCH)
604 for (set = 0; set < nsets; set++)
609 for (f = 0; f < s->nfont; f++)
611 if (FcDebug () & FC_DBG_MATCHV)
613 printf ("Font %d ", f);
614 FcPatternPrint (s->fonts[f]);
616 if (!FcCompare (p, s->fonts[f], score, result))
618 if (FcDebug () & FC_DBG_MATCHV)
621 for (i = 0; i < PRI_END; i++)
623 printf (" %g", score[i]);
627 for (i = 0; i < PRI_END; i++)
629 if (best && bestscore[i] < score[i])
631 if (!best || score[i] < bestscore[i])
633 for (i = 0; i < PRI_END; i++)
634 bestscore[i] = score[i];
641 if (FcDebug () & FC_DBG_MATCH)
643 printf ("Best score");
644 for (i = 0; i < PRI_END; i++)
645 printf (" %g", bestscore[i]);
647 FcPatternPrint (best);
649 /* assuming that 'result' is initialized with FcResultNoMatch
650 * outside this function */
652 *result = FcResultMatch;
658 FcFontSetMatch (FcConfig *config,
666 assert (sets != NULL);
668 assert (result != NULL);
670 *result = FcResultNoMatch;
674 config = FcConfigGetCurrent ();
678 best = FcFontSetMatchInternal (sets, nsets, p, result);
680 return FcFontRenderPrepare (config, p, best);
686 FcFontMatch (FcConfig *config,
695 assert (result != NULL);
697 *result = FcResultNoMatch;
701 config = FcConfigGetCurrent ();
706 if (config->fonts[FcSetSystem])
707 sets[nsets++] = config->fonts[FcSetSystem];
708 if (config->fonts[FcSetApplication])
709 sets[nsets++] = config->fonts[FcSetApplication];
711 best = FcFontSetMatchInternal (sets, nsets, p, result);
713 return FcFontRenderPrepare (config, p, best);
718 typedef struct _FcSortNode {
720 double score[PRI_END];
724 FcSortCompare (const void *aa, const void *ab)
726 FcSortNode *a = *(FcSortNode **) aa;
727 FcSortNode *b = *(FcSortNode **) ab;
728 double *as = &a->score[0];
729 double *bs = &b->score[0];
730 double ad = 0, bd = 0;
734 while (i-- && (ad = *as++) == (bd = *bs++))
736 return ad < bd ? -1 : ad > bd ? 1 : 0;
740 FcSortWalk (FcSortNode **n, int nnode, FcFontSet *fs, FcCharSet **csp, FcBool trim)
742 FcBool ret = FcFalse;
748 cs = FcCharSetCreate ();
755 FcSortNode *node = *n++;
756 FcBool adds_chars = FcFalse;
759 * Only fetch node charset if we'd need it
765 if (FcPatternGetCharSet (node->pattern, FC_CHARSET, 0, &ncs) !=
769 if (!FcCharSetMerge (cs, ncs, &adds_chars))
774 * If this font isn't a subset of the previous fonts,
777 if (!trim || adds_chars)
779 FcPatternReference (node->pattern);
780 if (FcDebug () & FC_DBG_MATCHV)
783 FcPatternPrint (node->pattern);
785 if (!FcFontSetAdd (fs, node->pattern))
787 FcPatternDestroy (node->pattern);
802 FcCharSetDestroy (cs);
808 FcFontSetSortDestroy (FcFontSet *fs)
810 FcFontSetDestroy (fs);
814 FcFontSetSort (FcConfig *config FC_UNUSED,
825 FcSortNode **nodeps, **nodep;
832 FcBool *patternLangSat;
835 assert (sets != NULL);
837 assert (result != NULL);
839 /* There are some implementation that relying on the result of
840 * "result" to check if the return value of FcFontSetSort
842 * So we should initialize it to the conservative way since
843 * this function doesn't return NULL anymore.
846 *result = FcResultNoMatch;
848 if (FcDebug () & FC_DBG_MATCH)
854 for (set = 0; set < nsets; set++)
862 return FcFontSetCreate ();
864 for (nPatternLang = 0;
865 FcPatternGet (p, FC_LANG, nPatternLang, &patternLang) == FcResultMatch;
870 nodes = malloc (nnodes * sizeof (FcSortNode) +
871 nnodes * sizeof (FcSortNode *) +
872 nPatternLang * sizeof (FcBool));
875 nodeps = (FcSortNode **) (nodes + nnodes);
876 patternLangSat = (FcBool *) (nodeps + nnodes);
880 for (set = 0; set < nsets; set++)
885 for (f = 0; f < s->nfont; f++)
887 if (FcDebug () & FC_DBG_MATCHV)
889 printf ("Font %d ", f);
890 FcPatternPrint (s->fonts[f]);
892 new->pattern = s->fonts[f];
893 if (!FcCompare (p, new->pattern, new->score, result))
895 if (FcDebug () & FC_DBG_MATCHV)
898 for (i = 0; i < PRI_END; i++)
900 printf (" %g", new->score[i]);
910 nnodes = new - nodes;
912 qsort (nodeps, nnodes, sizeof (FcSortNode *),
915 for (i = 0; i < nPatternLang; i++)
916 patternLangSat[i] = FcFalse;
918 for (f = 0; f < nnodes; f++)
920 FcBool satisfies = FcFalse;
922 * If this node matches any language, go check
923 * which ones and satisfy those entries
925 if (nodeps[f]->score[PRI_LANG] < 2000)
927 for (i = 0; i < nPatternLang; i++)
931 if (!patternLangSat[i] &&
932 FcPatternGet (p, FC_LANG, i, &patternLang) == FcResultMatch &&
933 FcPatternGet (nodeps[f]->pattern, FC_LANG, 0, &nodeLang) == FcResultMatch)
935 double compare = FcCompareLang (&patternLang, &nodeLang);
936 if (compare >= 0 && compare < 2)
938 if (FcDebug () & FC_DBG_MATCHV)
943 if (FcPatternGetString (nodeps[f]->pattern, FC_FAMILY, 0, &family) == FcResultMatch &&
944 FcPatternGetString (nodeps[f]->pattern, FC_STYLE, 0, &style) == FcResultMatch)
945 printf ("Font %s:%s matches language %d\n", family, style, i);
947 patternLangSat[i] = FcTrue;
956 nodeps[f]->score[PRI_LANG] = 10000.0;
961 * Re-sort once the language issues have been settled
963 qsort (nodeps, nnodes, sizeof (FcSortNode *),
966 ret = FcFontSetCreate ();
970 if (!FcSortWalk (nodeps, nnodes, ret, csp, trim))
975 if (FcDebug() & FC_DBG_MATCH)
977 printf ("First font ");
978 FcPatternPrint (ret->fonts[0]);
981 *result = FcResultMatch;
986 FcFontSetDestroy (ret);
994 FcFontSort (FcConfig *config,
1004 assert (result != NULL);
1006 *result = FcResultNoMatch;
1010 config = FcConfigGetCurrent ();
1015 if (config->fonts[FcSetSystem])
1016 sets[nsets++] = config->fonts[FcSetSystem];
1017 if (config->fonts[FcSetApplication])
1018 sets[nsets++] = config->fonts[FcSetApplication];
1019 return FcFontSetSort (config, sets, nsets, p, trim, csp, result);
1022 #include "fcaliastail.h"