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.
32 FcCompareNumber (FcValue *value1, FcValue *value2)
36 switch (value1->type) {
38 v1 = (double) value1->u.i;
46 switch (value2->type) {
48 v2 = (double) value2->u.i;
63 FcCompareString (FcValue *v1, FcValue *v2)
65 return (double) FcStrCmpIgnoreCase (FcValueString(v1), FcValueString(v2)) != 0;
69 FcCompareFamily (FcValue *v1, FcValue *v2)
71 /* rely on the guarantee in FcPatternObjectAddWithBinding that
72 * families are always FcTypeString. */
73 const FcChar8* v1_string = FcValueString(v1);
74 const FcChar8* v2_string = FcValueString(v2);
76 if (FcToLower(*v1_string) != FcToLower(*v2_string) &&
77 *v1_string != ' ' && *v2_string != ' ')
80 return (double) FcStrCmpIgnoreBlanksAndCase (v1_string, v2_string) != 0;
84 FcCompareLang (FcValue *v1, FcValue *v2)
87 FcValue value1 = FcValueCanonicalize(v1), value2 = FcValueCanonicalize(v2);
89 switch (value1.type) {
91 switch (value2.type) {
93 result = FcLangSetCompare (value1.u.l, value2.u.l);
96 result = FcLangSetHasLang (value1.u.l,
104 switch (value2.type) {
106 result = FcLangSetHasLang (value2.u.l, value1.u.s);
109 result = FcLangCompare (value1.u.s,
122 case FcLangDifferentCountry:
124 case FcLangDifferentLang:
131 FcCompareBool (FcValue *v1, FcValue *v2)
133 if (v2->type != FcTypeBool || v1->type != FcTypeBool)
135 return (double) v2->u.b != v1->u.b;
139 FcCompareCharSet (FcValue *v1, FcValue *v2)
141 return (double) FcCharSetSubtractCount (FcValueCharSet(v1), FcValueCharSet(v2));
145 FcCompareSize (FcValue *value1, FcValue *value2)
149 switch (value1->type) {
159 switch (value2->type) {
178 FcCompareFilename (FcValue *v1, FcValue *v2)
180 const FcChar8 *s1 = FcValueString (v1), *s2 = FcValueString (v2);
181 if (FcStrCmp (s1, s2) == 0)
183 else if (FcStrCmpIgnoreCase (s1, s2) == 0)
185 else if (FcStrRegexCmp (s2, s1))
187 else if (FcStrRegexCmpIgnoreCase (s2, s1))
193 typedef struct _FcMatcher {
195 double (*compare) (FcValue *value1, FcValue *value2);
200 * Order is significant, it defines the precedence of
201 * each value, earlier values are more significant than
204 static const FcMatcher _FcMatchers [] = {
205 { FC_FILE_OBJECT, FcCompareFilename, 0, 0 },
207 { FC_FOUNDRY_OBJECT, FcCompareString, 1, 1 },
208 #define MATCH_FOUNDRY 1
209 { FC_CHARSET_OBJECT, FcCompareCharSet, 2, 2 },
210 #define MATCH_CHARSET 2
211 { FC_FAMILY_OBJECT, FcCompareFamily, 3, 5 },
212 #define MATCH_FAMILY 3
213 { FC_LANG_OBJECT, FcCompareLang, 4, 4 },
215 #define MATCH_LANG_INDEX 4
216 { FC_SPACING_OBJECT, FcCompareNumber, 6, 6 },
217 #define MATCH_SPACING 5
218 { FC_PIXEL_SIZE_OBJECT, FcCompareSize, 7, 7 },
219 #define MATCH_PIXEL_SIZE 6
220 { FC_STYLE_OBJECT, FcCompareString, 8, 8 },
221 #define MATCH_STYLE 7
222 { FC_SLANT_OBJECT, FcCompareNumber, 9, 9 },
223 #define MATCH_SLANT 8
224 { FC_WEIGHT_OBJECT, FcCompareNumber, 10, 10 },
225 #define MATCH_WEIGHT 9
226 { FC_WIDTH_OBJECT, FcCompareNumber, 11, 11 },
227 #define MATCH_WIDTH 10
228 { FC_DECORATIVE_OBJECT, FcCompareBool, 12, 12 },
229 #define MATCH_DECORATIVE 11
230 { FC_ANTIALIAS_OBJECT, FcCompareBool, 13, 13 },
231 #define MATCH_ANTIALIAS 12
232 { FC_RASTERIZER_OBJECT, FcCompareString, 14, 14 },
233 #define MATCH_RASTERIZER 13
234 { FC_OUTLINE_OBJECT, FcCompareBool, 15, 15 },
235 #define MATCH_OUTLINE 14
236 { FC_FONTVERSION_OBJECT, FcCompareNumber, 16, 16 },
237 #define MATCH_FONTVERSION 15
240 #define NUM_MATCH_VALUES 17
242 static const FcMatcher*
243 FcObjectToMatcher (FcObject object,
251 i = MATCH_FILE; break;
252 case FC_FOUNDRY_OBJECT:
253 i = MATCH_FOUNDRY; break;
254 case FC_FONTVERSION_OBJECT:
255 i = MATCH_FONTVERSION; break;
256 case FC_FAMILY_OBJECT:
257 i = MATCH_FAMILY; break;
258 case FC_CHARSET_OBJECT:
259 i = MATCH_CHARSET; break;
260 case FC_ANTIALIAS_OBJECT:
261 i = MATCH_ANTIALIAS; break;
263 i = MATCH_LANG; break;
264 case FC_SPACING_OBJECT:
265 i = MATCH_SPACING; break;
266 case FC_STYLE_OBJECT:
267 i = MATCH_STYLE; break;
268 case FC_SLANT_OBJECT:
269 i = MATCH_SLANT; break;
270 case FC_PIXEL_SIZE_OBJECT:
271 i = MATCH_PIXEL_SIZE; break;
272 case FC_WIDTH_OBJECT:
273 i = MATCH_WIDTH; break;
274 case FC_WEIGHT_OBJECT:
275 i = MATCH_WEIGHT; break;
276 case FC_RASTERIZER_OBJECT:
277 i = MATCH_RASTERIZER; break;
278 case FC_OUTLINE_OBJECT:
279 i = MATCH_OUTLINE; break;
280 case FC_DECORATIVE_OBJECT:
281 i = MATCH_DECORATIVE; break;
286 case FC_FAMILYLANG_OBJECT:
287 case FC_STYLELANG_OBJECT:
288 case FC_FULLNAMELANG_OBJECT:
289 i = MATCH_LANG; break;
297 return _FcMatchers+i;
301 FcCompareValueList (FcObject object,
302 const FcMatcher *match,
303 FcValueListPtr v1orig, /* pattern */
304 FcValueListPtr v2orig, /* target */
310 FcValueListPtr v1, v2;
311 double v, best, bestStrong, bestWeak;
317 *bestValue = FcValueCanonicalize(&v2orig->value);
327 for (v1 = v1orig; v1; v1 = FcValueListNext(v1))
329 for (v2 = v2orig, k = 0; v2; v2 = FcValueListNext(v2), k++)
331 v = (match->compare) (&v1->value, &v2->value);
334 *result = FcResultTypeMismatch;
341 *bestValue = FcValueCanonicalize(&v2->value);
345 if (v1->binding == FcValueBindingStrong)
358 if (FcDebug () & FC_DBG_MATCHV)
360 printf (" %s: %g ", FcObjectName (object), best);
361 FcValueListPrint (v1orig);
363 FcValueListPrint (v2orig);
368 int weak = match->weak;
369 int strong = match->strong;
371 value[strong] += best;
374 value[weak] += bestWeak;
375 value[strong] += bestStrong;
385 * Return a value indicating the distance between the two lists of
390 FcCompare (FcPattern *pat,
397 for (i = 0; i < NUM_MATCH_VALUES; i++)
402 while (i1 < pat->num && i2 < fnt->num)
404 FcPatternElt *elt_i1 = &FcPatternElts(pat)[i1];
405 FcPatternElt *elt_i2 = &FcPatternElts(fnt)[i2];
407 i = FcObjectCompare(elt_i1->object, elt_i2->object);
414 const FcMatcher *match = FcObjectToMatcher (elt_i1->object, FcFalse);
415 if (!FcCompareValueList (elt_i1->object, match,
416 FcPatternEltValues(elt_i1),
417 FcPatternEltValues(elt_i2),
418 NULL, value, NULL, result))
428 FcFontRenderPrepare (FcConfig *config,
434 FcPatternElt *fe, *pe, *fel, *pel;
438 assert (pat != NULL);
439 assert (font != NULL);
441 new = FcPatternCreate ();
444 for (i = 0; i < font->num; i++)
446 fe = &FcPatternElts(font)[i];
447 if (fe->object == FC_FAMILYLANG_OBJECT ||
448 fe->object == FC_STYLELANG_OBJECT ||
449 fe->object == FC_FULLNAMELANG_OBJECT)
451 /* ignore those objects. we need to deal with them
455 if (fe->object == FC_FAMILY_OBJECT ||
456 fe->object == FC_STYLE_OBJECT ||
457 fe->object == FC_FULLNAME_OBJECT)
459 FC_ASSERT_STATIC ((FC_FAMILY_OBJECT + 1) == FC_FAMILYLANG_OBJECT);
460 FC_ASSERT_STATIC ((FC_STYLE_OBJECT + 1) == FC_STYLELANG_OBJECT);
461 FC_ASSERT_STATIC ((FC_FULLNAME_OBJECT + 1) == FC_FULLNAMELANG_OBJECT);
463 fel = FcPatternObjectFindElt (font, fe->object + 1);
464 pel = FcPatternObjectFindElt (pat, fe->object + 1);
471 pe = FcPatternObjectFindElt (pat, fe->object);
474 const FcMatcher *match = FcObjectToMatcher (pe->object, FcFalse);
476 if (!FcCompareValueList (pe->object, match,
477 FcPatternEltValues(pe),
478 FcPatternEltValues(fe), &v, NULL, NULL, &result))
480 FcPatternDestroy (new);
486 FcValueListPtr l1, l2, ln = NULL, ll = NULL;
488 match = FcObjectToMatcher (pel->object, FcTrue);
489 if (!FcCompareValueList (pel->object, match,
490 FcPatternEltValues (pel),
491 FcPatternEltValues (fel), NULL, NULL, &n, &result))
493 FcPatternDestroy (new);
497 for (j = 0, l1 = FcPatternEltValues (fe), l2 = FcPatternEltValues (fel);
498 l1 != NULL || l2 != NULL;
499 j++, l1 = l1 ? FcValueListNext (l1) : NULL, l2 = l2 ? FcValueListNext (l2) : NULL)
504 ln = FcValueListPrepend (ln,
505 FcValueCanonicalize (&l1->value),
506 FcValueBindingStrong);
508 ll = FcValueListPrepend (ll,
509 FcValueCanonicalize (&l2->value),
510 FcValueBindingStrong);
515 ln = FcValueListAppend (ln,
516 FcValueCanonicalize (&l1->value),
517 FcValueBindingStrong);
519 ll = FcValueListAppend (ll,
520 FcValueCanonicalize (&l2->value),
521 FcValueBindingStrong);
524 FcPatternObjectListAdd (new, fe->object, ln, FcFalse);
525 FcPatternObjectListAdd (new, fel->object, ll, FcFalse);
531 FcValueListPtr l1, l2;
534 l1 = FcValueListDuplicate (FcPatternEltValues (fe));
535 l2 = FcValueListDuplicate (FcPatternEltValues (fel));
536 FcPatternObjectListAdd (new, fe->object, l1, FcFalse);
537 FcPatternObjectListAdd (new, fel->object, l2, FcFalse);
546 v = FcValueCanonicalize(&FcPatternEltValues (fe)->value);
548 FcPatternObjectAdd (new, fe->object, v, FcFalse);
550 for (i = 0; i < pat->num; i++)
552 pe = &FcPatternElts(pat)[i];
553 fe = FcPatternObjectFindElt (font, pe->object);
556 v = FcValueCanonicalize(&FcPatternEltValues(pe)->value);
557 FcPatternObjectAdd (new, pe->object, v, FcTrue);
561 FcConfigSubstituteWithPat (config, new, pat, FcMatchFont);
566 FcFontSetMatchInternal (FcConfig *config,
572 double score[NUM_MATCH_VALUES], bestscore[NUM_MATCH_VALUES];
579 for (i = 0; i < NUM_MATCH_VALUES; i++)
582 if (FcDebug () & FC_DBG_MATCH)
587 for (set = 0; set < nsets; set++)
592 for (f = 0; f < s->nfont; f++)
594 if (FcDebug () & FC_DBG_MATCHV)
596 printf ("Font %d ", f);
597 FcPatternPrint (s->fonts[f]);
599 if (!FcCompare (p, s->fonts[f], score, result))
601 if (FcDebug () & FC_DBG_MATCHV)
604 for (i = 0; i < NUM_MATCH_VALUES; i++)
606 printf (" %g", score[i]);
610 for (i = 0; i < NUM_MATCH_VALUES; i++)
612 if (best && bestscore[i] < score[i])
614 if (!best || score[i] < bestscore[i])
616 for (i = 0; i < NUM_MATCH_VALUES; i++)
617 bestscore[i] = score[i];
624 if (FcDebug () & FC_DBG_MATCH)
626 printf ("Best score");
627 for (i = 0; i < NUM_MATCH_VALUES; i++)
628 printf (" %g", bestscore[i]);
630 FcPatternPrint (best);
632 /* assuming that 'result' is initialized with FcResultNoMatch
633 * outside this function */
635 *result = FcResultMatch;
641 FcFontSetMatch (FcConfig *config,
649 assert (sets != NULL);
651 assert (result != NULL);
653 *result = FcResultNoMatch;
657 config = FcConfigGetCurrent ();
661 best = FcFontSetMatchInternal (config, sets, nsets, p, result);
663 return FcFontRenderPrepare (config, p, best);
669 FcFontMatch (FcConfig *config,
678 assert (result != NULL);
680 *result = FcResultNoMatch;
684 config = FcConfigGetCurrent ();
689 if (config->fonts[FcSetSystem])
690 sets[nsets++] = config->fonts[FcSetSystem];
691 if (config->fonts[FcSetApplication])
692 sets[nsets++] = config->fonts[FcSetApplication];
694 best = FcFontSetMatchInternal (config, sets, nsets, p, result);
696 return FcFontRenderPrepare (config, p, best);
701 typedef struct _FcSortNode {
703 double score[NUM_MATCH_VALUES];
707 FcSortCompare (const void *aa, const void *ab)
709 FcSortNode *a = *(FcSortNode **) aa;
710 FcSortNode *b = *(FcSortNode **) ab;
711 double *as = &a->score[0];
712 double *bs = &b->score[0];
713 double ad = 0, bd = 0;
716 i = NUM_MATCH_VALUES;
717 while (i-- && (ad = *as++) == (bd = *bs++))
719 return ad < bd ? -1 : ad > bd ? 1 : 0;
723 FcSortWalk (FcSortNode **n, int nnode, FcFontSet *fs, FcCharSet **csp, FcBool trim)
725 FcBool ret = FcFalse;
731 cs = FcCharSetCreate ();
738 FcSortNode *node = *n++;
739 FcBool adds_chars = FcFalse;
742 * Only fetch node charset if we'd need it
748 if (FcPatternGetCharSet (node->pattern, FC_CHARSET, 0, &ncs) !=
752 if (!FcCharSetMerge (cs, ncs, &adds_chars))
757 * If this font isn't a subset of the previous fonts,
760 if (!trim || adds_chars)
762 FcPatternReference (node->pattern);
763 if (FcDebug () & FC_DBG_MATCHV)
766 FcPatternPrint (node->pattern);
768 if (!FcFontSetAdd (fs, node->pattern))
770 FcPatternDestroy (node->pattern);
785 FcCharSetDestroy (cs);
791 FcFontSetSortDestroy (FcFontSet *fs)
793 FcFontSetDestroy (fs);
797 FcFontSetSort (FcConfig *config,
808 FcSortNode **nodeps, **nodep;
815 FcBool *patternLangSat;
818 assert (sets != NULL);
820 assert (result != NULL);
822 /* There are some implementation that relying on the result of
823 * "result" to check if the return value of FcFontSetSort
825 * So we should initialize it to the conservative way since
826 * this function doesn't return NULL anymore.
829 *result = FcResultNoMatch;
831 if (FcDebug () & FC_DBG_MATCH)
837 for (set = 0; set < nsets; set++)
845 return FcFontSetCreate ();
847 for (nPatternLang = 0;
848 FcPatternGet (p, FC_LANG, nPatternLang, &patternLang) == FcResultMatch;
853 nodes = malloc (nnodes * sizeof (FcSortNode) +
854 nnodes * sizeof (FcSortNode *) +
855 nPatternLang * sizeof (FcBool));
858 nodeps = (FcSortNode **) (nodes + nnodes);
859 patternLangSat = (FcBool *) (nodeps + nnodes);
863 for (set = 0; set < nsets; set++)
868 for (f = 0; f < s->nfont; f++)
870 if (FcDebug () & FC_DBG_MATCHV)
872 printf ("Font %d ", f);
873 FcPatternPrint (s->fonts[f]);
875 new->pattern = s->fonts[f];
876 if (!FcCompare (p, new->pattern, new->score, result))
878 if (FcDebug () & FC_DBG_MATCHV)
881 for (i = 0; i < NUM_MATCH_VALUES; i++)
883 printf (" %g", new->score[i]);
893 nnodes = new - nodes;
895 qsort (nodeps, nnodes, sizeof (FcSortNode *),
898 for (i = 0; i < nPatternLang; i++)
899 patternLangSat[i] = FcFalse;
901 for (f = 0; f < nnodes; f++)
903 FcBool satisfies = FcFalse;
905 * If this node matches any language, go check
906 * which ones and satisfy those entries
908 if (nodeps[f]->score[MATCH_LANG_INDEX] < 2000)
910 for (i = 0; i < nPatternLang; i++)
914 if (!patternLangSat[i] &&
915 FcPatternGet (p, FC_LANG, i, &patternLang) == FcResultMatch &&
916 FcPatternGet (nodeps[f]->pattern, FC_LANG, 0, &nodeLang) == FcResultMatch)
918 double compare = FcCompareLang (&patternLang, &nodeLang);
919 if (compare >= 0 && compare < 2)
921 if (FcDebug () & FC_DBG_MATCHV)
926 if (FcPatternGetString (nodeps[f]->pattern, FC_FAMILY, 0, &family) == FcResultMatch &&
927 FcPatternGetString (nodeps[f]->pattern, FC_STYLE, 0, &style) == FcResultMatch)
928 printf ("Font %s:%s matches language %d\n", family, style, i);
930 patternLangSat[i] = FcTrue;
938 nodeps[f]->score[MATCH_LANG_INDEX] = 10000.0;
942 * Re-sort once the language issues have been settled
944 qsort (nodeps, nnodes, sizeof (FcSortNode *),
947 ret = FcFontSetCreate ();
951 if (!FcSortWalk (nodeps, nnodes, ret, csp, trim))
956 if (FcDebug() & FC_DBG_MATCH)
958 printf ("First font ");
959 FcPatternPrint (ret->fonts[0]);
962 *result = FcResultMatch;
967 FcFontSetDestroy (ret);
975 FcFontSort (FcConfig *config,
985 assert (result != NULL);
987 *result = FcResultNoMatch;
991 config = FcConfigGetCurrent ();
996 if (config->fonts[FcSetSystem])
997 sets[nsets++] = config->fonts[FcSetSystem];
998 if (config->fonts[FcSetApplication])
999 sets[nsets++] = config->fonts[FcSetApplication];
1000 return FcFontSetSort (config, sets, nsets, p, trim, csp, result);
1003 #include "fcaliastail.h"