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 ((int) value1->type) {
38 v1 = (double) value1->u.i;
46 switch ((int) 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 ((int) value1.type) {
91 switch ((int) value2.type) {
93 result = FcLangSetCompare (value1.u.l, value2.u.l);
96 result = FcLangSetHasLang (value1.u.l,
104 switch ((int) 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 ((int) value1->type) {
159 switch ((int) 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 (FcFontSet **sets,
571 double score[NUM_MATCH_VALUES], bestscore[NUM_MATCH_VALUES];
578 for (i = 0; i < NUM_MATCH_VALUES; i++)
581 if (FcDebug () & FC_DBG_MATCH)
586 for (set = 0; set < nsets; set++)
591 for (f = 0; f < s->nfont; f++)
593 if (FcDebug () & FC_DBG_MATCHV)
595 printf ("Font %d ", f);
596 FcPatternPrint (s->fonts[f]);
598 if (!FcCompare (p, s->fonts[f], score, result))
600 if (FcDebug () & FC_DBG_MATCHV)
603 for (i = 0; i < NUM_MATCH_VALUES; i++)
605 printf (" %g", score[i]);
609 for (i = 0; i < NUM_MATCH_VALUES; i++)
611 if (best && bestscore[i] < score[i])
613 if (!best || score[i] < bestscore[i])
615 for (i = 0; i < NUM_MATCH_VALUES; i++)
616 bestscore[i] = score[i];
623 if (FcDebug () & FC_DBG_MATCH)
625 printf ("Best score");
626 for (i = 0; i < NUM_MATCH_VALUES; i++)
627 printf (" %g", bestscore[i]);
629 FcPatternPrint (best);
631 /* assuming that 'result' is initialized with FcResultNoMatch
632 * outside this function */
634 *result = FcResultMatch;
640 FcFontSetMatch (FcConfig *config,
648 assert (sets != NULL);
650 assert (result != NULL);
652 *result = FcResultNoMatch;
656 config = FcConfigGetCurrent ();
660 best = FcFontSetMatchInternal (sets, nsets, p, result);
662 return FcFontRenderPrepare (config, p, best);
668 FcFontMatch (FcConfig *config,
677 assert (result != NULL);
679 *result = FcResultNoMatch;
683 config = FcConfigGetCurrent ();
688 if (config->fonts[FcSetSystem])
689 sets[nsets++] = config->fonts[FcSetSystem];
690 if (config->fonts[FcSetApplication])
691 sets[nsets++] = config->fonts[FcSetApplication];
693 best = FcFontSetMatchInternal (sets, nsets, p, result);
695 return FcFontRenderPrepare (config, p, best);
700 typedef struct _FcSortNode {
702 double score[NUM_MATCH_VALUES];
706 FcSortCompare (const void *aa, const void *ab)
708 FcSortNode *a = *(FcSortNode **) aa;
709 FcSortNode *b = *(FcSortNode **) ab;
710 double *as = &a->score[0];
711 double *bs = &b->score[0];
712 double ad = 0, bd = 0;
715 i = NUM_MATCH_VALUES;
716 while (i-- && (ad = *as++) == (bd = *bs++))
718 return ad < bd ? -1 : ad > bd ? 1 : 0;
722 FcSortWalk (FcSortNode **n, int nnode, FcFontSet *fs, FcCharSet **csp, FcBool trim)
724 FcBool ret = FcFalse;
730 cs = FcCharSetCreate ();
737 FcSortNode *node = *n++;
738 FcBool adds_chars = FcFalse;
741 * Only fetch node charset if we'd need it
747 if (FcPatternGetCharSet (node->pattern, FC_CHARSET, 0, &ncs) !=
751 if (!FcCharSetMerge (cs, ncs, &adds_chars))
756 * If this font isn't a subset of the previous fonts,
759 if (!trim || adds_chars)
761 FcPatternReference (node->pattern);
762 if (FcDebug () & FC_DBG_MATCHV)
765 FcPatternPrint (node->pattern);
767 if (!FcFontSetAdd (fs, node->pattern))
769 FcPatternDestroy (node->pattern);
784 FcCharSetDestroy (cs);
790 FcFontSetSortDestroy (FcFontSet *fs)
792 FcFontSetDestroy (fs);
796 FcFontSetSort (FcConfig *config FC_UNUSED,
807 FcSortNode **nodeps, **nodep;
814 FcBool *patternLangSat;
817 assert (sets != NULL);
819 assert (result != NULL);
821 /* There are some implementation that relying on the result of
822 * "result" to check if the return value of FcFontSetSort
824 * So we should initialize it to the conservative way since
825 * this function doesn't return NULL anymore.
828 *result = FcResultNoMatch;
830 if (FcDebug () & FC_DBG_MATCH)
836 for (set = 0; set < nsets; set++)
844 return FcFontSetCreate ();
846 for (nPatternLang = 0;
847 FcPatternGet (p, FC_LANG, nPatternLang, &patternLang) == FcResultMatch;
852 nodes = malloc (nnodes * sizeof (FcSortNode) +
853 nnodes * sizeof (FcSortNode *) +
854 nPatternLang * sizeof (FcBool));
857 nodeps = (FcSortNode **) (nodes + nnodes);
858 patternLangSat = (FcBool *) (nodeps + nnodes);
862 for (set = 0; set < nsets; set++)
867 for (f = 0; f < s->nfont; f++)
869 if (FcDebug () & FC_DBG_MATCHV)
871 printf ("Font %d ", f);
872 FcPatternPrint (s->fonts[f]);
874 new->pattern = s->fonts[f];
875 if (!FcCompare (p, new->pattern, new->score, result))
877 if (FcDebug () & FC_DBG_MATCHV)
880 for (i = 0; i < NUM_MATCH_VALUES; i++)
882 printf (" %g", new->score[i]);
892 nnodes = new - nodes;
894 qsort (nodeps, nnodes, sizeof (FcSortNode *),
897 for (i = 0; i < nPatternLang; i++)
898 patternLangSat[i] = FcFalse;
900 for (f = 0; f < nnodes; f++)
902 FcBool satisfies = FcFalse;
904 * If this node matches any language, go check
905 * which ones and satisfy those entries
907 if (nodeps[f]->score[MATCH_LANG_INDEX] < 2000)
909 for (i = 0; i < nPatternLang; i++)
913 if (!patternLangSat[i] &&
914 FcPatternGet (p, FC_LANG, i, &patternLang) == FcResultMatch &&
915 FcPatternGet (nodeps[f]->pattern, FC_LANG, 0, &nodeLang) == FcResultMatch)
917 double compare = FcCompareLang (&patternLang, &nodeLang);
918 if (compare >= 0 && compare < 2)
920 if (FcDebug () & FC_DBG_MATCHV)
925 if (FcPatternGetString (nodeps[f]->pattern, FC_FAMILY, 0, &family) == FcResultMatch &&
926 FcPatternGetString (nodeps[f]->pattern, FC_STYLE, 0, &style) == FcResultMatch)
927 printf ("Font %s:%s matches language %d\n", family, style, i);
929 patternLangSat[i] = FcTrue;
937 nodeps[f]->score[MATCH_LANG_INDEX] = 10000.0;
941 * Re-sort once the language issues have been settled
943 qsort (nodeps, nnodes, sizeof (FcSortNode *),
946 ret = FcFontSetCreate ();
950 if (!FcSortWalk (nodeps, nnodes, ret, csp, trim))
955 if (FcDebug() & FC_DBG_MATCH)
957 printf ("First font ");
958 FcPatternPrint (ret->fonts[0]);
961 *result = FcResultMatch;
966 FcFontSetDestroy (ret);
974 FcFontSort (FcConfig *config,
984 assert (result != NULL);
986 *result = FcResultNoMatch;
990 config = FcConfigGetCurrent ();
995 if (config->fonts[FcSetSystem])
996 sets[nsets++] = config->fonts[FcSetSystem];
997 if (config->fonts[FcSetApplication])
998 sets[nsets++] = config->fonts[FcSetApplication];
999 return FcFontSetSort (config, sets, nsets, p, trim, csp, result);
1002 #include "fcaliastail.h"