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.
31 FcCompareNumber (FcValue *value1, FcValue *value2)
35 switch (value1->type) {
37 v1 = (double) value1->u.i;
45 switch (value2->type) {
47 v2 = (double) value2->u.i;
62 FcCompareString (FcValue *v1, FcValue *v2)
64 return (double) FcStrCmpIgnoreCase (FcValueString(v1), FcValueString(v2)) != 0;
68 FcCompareFamily (FcValue *v1, FcValue *v2)
70 /* rely on the guarantee in FcPatternObjectAddWithBinding that
71 * families are always FcTypeString. */
72 const FcChar8* v1_string = FcValueString(v1);
73 const FcChar8* v2_string = FcValueString(v2);
75 if (FcToLower(*v1_string) != FcToLower(*v2_string) &&
76 *v1_string != ' ' && *v2_string != ' ')
79 return (double) FcStrCmpIgnoreBlanksAndCase (v1_string, v2_string) != 0;
83 FcCompareLang (FcValue *v1, FcValue *v2)
86 FcValue value1 = FcValueCanonicalize(v1), value2 = FcValueCanonicalize(v2);
88 switch (value1.type) {
90 switch (value2.type) {
92 result = FcLangSetCompare (value1.u.l, value2.u.l);
95 result = FcLangSetHasLang (value1.u.l,
103 switch (value2.type) {
105 result = FcLangSetHasLang (value2.u.l, value1.u.s);
108 result = FcLangCompare (value1.u.s,
121 case FcLangDifferentCountry:
123 case FcLangDifferentLang:
130 FcCompareBool (FcValue *v1, FcValue *v2)
132 if (v2->type != FcTypeBool || v1->type != FcTypeBool)
134 return (double) v2->u.b != v1->u.b;
138 FcCompareCharSet (FcValue *v1, FcValue *v2)
140 return (double) FcCharSetSubtractCount (FcValueCharSet(v1), FcValueCharSet(v2));
144 FcCompareSize (FcValue *value1, FcValue *value2)
148 switch (value1->type) {
158 switch (value2->type) {
176 typedef struct _FcMatcher {
178 double (*compare) (FcValue *value1, FcValue *value2);
183 * Order is significant, it defines the precedence of
184 * each value, earlier values are more significant than
187 static const FcMatcher _FcMatchers [] = {
188 { FC_FOUNDRY_OBJECT, FcCompareString, 0, 0 },
189 #define MATCH_FOUNDRY 0
190 { FC_CHARSET_OBJECT, FcCompareCharSet, 1, 1 },
191 #define MATCH_CHARSET 1
192 { FC_FAMILY_OBJECT, FcCompareFamily, 2, 4 },
193 #define MATCH_FAMILY 2
194 { FC_LANG_OBJECT, FcCompareLang, 3, 3 },
196 #define MATCH_LANG_INDEX 3
197 { FC_SPACING_OBJECT, FcCompareNumber, 5, 5 },
198 #define MATCH_SPACING 4
199 { FC_PIXEL_SIZE_OBJECT, FcCompareSize, 6, 6 },
200 #define MATCH_PIXEL_SIZE 5
201 { FC_STYLE_OBJECT, FcCompareString, 7, 7 },
202 #define MATCH_STYLE 6
203 { FC_SLANT_OBJECT, FcCompareNumber, 8, 8 },
204 #define MATCH_SLANT 7
205 { FC_WEIGHT_OBJECT, FcCompareNumber, 9, 9 },
206 #define MATCH_WEIGHT 8
207 { FC_WIDTH_OBJECT, FcCompareNumber, 10, 10 },
208 #define MATCH_WIDTH 9
209 { FC_DECORATIVE_OBJECT, FcCompareBool, 11, 11 },
210 #define MATCH_DECORATIVE 10
211 { FC_ANTIALIAS_OBJECT, FcCompareBool, 12, 12 },
212 #define MATCH_ANTIALIAS 11
213 { FC_RASTERIZER_OBJECT, FcCompareString, 13, 13 },
214 #define MATCH_RASTERIZER 12
215 { FC_OUTLINE_OBJECT, FcCompareBool, 14, 14 },
216 #define MATCH_OUTLINE 13
217 { FC_FONTVERSION_OBJECT, FcCompareNumber, 15, 15 },
218 #define MATCH_FONTVERSION 14
221 #define NUM_MATCH_VALUES 16
223 static const FcMatcher*
224 FcObjectToMatcher (FcObject object)
230 case FC_FOUNDRY_OBJECT:
231 i = MATCH_FOUNDRY; break;
232 case FC_FONTVERSION_OBJECT:
233 i = MATCH_FONTVERSION; break;
234 case FC_FAMILY_OBJECT:
235 i = MATCH_FAMILY; break;
236 case FC_CHARSET_OBJECT:
237 i = MATCH_CHARSET; break;
238 case FC_ANTIALIAS_OBJECT:
239 i = MATCH_ANTIALIAS; break;
241 i = MATCH_LANG; break;
242 case FC_SPACING_OBJECT:
243 i = MATCH_SPACING; break;
244 case FC_STYLE_OBJECT:
245 i = MATCH_STYLE; break;
246 case FC_SLANT_OBJECT:
247 i = MATCH_SLANT; break;
248 case FC_PIXEL_SIZE_OBJECT:
249 i = MATCH_PIXEL_SIZE; break;
250 case FC_WIDTH_OBJECT:
251 i = MATCH_WIDTH; break;
252 case FC_WEIGHT_OBJECT:
253 i = MATCH_WEIGHT; break;
254 case FC_RASTERIZER_OBJECT:
255 i = MATCH_RASTERIZER; break;
256 case FC_OUTLINE_OBJECT:
257 i = MATCH_OUTLINE; break;
258 case FC_DECORATIVE_OBJECT:
259 i = MATCH_DECORATIVE; break;
265 return _FcMatchers+i;
269 FcCompareValueList (FcObject object,
270 FcValueListPtr v1orig, /* pattern */
271 FcValueListPtr v2orig, /* target */
276 FcValueListPtr v1, v2;
277 double v, best, bestStrong, bestWeak;
279 const FcMatcher *match = FcObjectToMatcher(object);
284 *bestValue = FcValueCanonicalize(&v2orig->value);
292 for (v1 = v1orig; v1; v1 = FcValueListNext(v1))
294 for (v2 = v2orig; v2; v2 = FcValueListNext(v2))
296 v = (match->compare) (&v1->value, &v2->value);
299 *result = FcResultTypeMismatch;
306 *bestValue = FcValueCanonicalize(&v2->value);
309 if (v1->binding == FcValueBindingStrong)
322 if (FcDebug () & FC_DBG_MATCHV)
324 printf (" %s: %g ", FcObjectName (object), best);
325 FcValueListPrint (v1orig);
327 FcValueListPrint (v2orig);
332 int weak = match->weak;
333 int strong = match->strong;
335 value[strong] += best;
338 value[weak] += bestWeak;
339 value[strong] += bestStrong;
346 * Return a value indicating the distance between the two lists of
351 FcCompare (FcPattern *pat,
358 for (i = 0; i < NUM_MATCH_VALUES; i++)
363 while (i1 < pat->num && i2 < fnt->num)
365 FcPatternElt *elt_i1 = &FcPatternElts(pat)[i1];
366 FcPatternElt *elt_i2 = &FcPatternElts(fnt)[i2];
368 i = FcObjectCompare(elt_i1->object, elt_i2->object);
375 if (!FcCompareValueList (elt_i1->object,
376 FcPatternEltValues(elt_i1),
377 FcPatternEltValues(elt_i2),
388 FcFontRenderPrepare (FcConfig *config,
394 FcPatternElt *fe, *pe;
398 new = FcPatternCreate ();
401 for (i = 0; i < font->num; i++)
403 fe = &FcPatternElts(font)[i];
404 pe = FcPatternObjectFindElt (pat, fe->object);
407 if (!FcCompareValueList (pe->object, FcPatternEltValues(pe),
408 FcPatternEltValues(fe), &v, 0, &result))
410 FcPatternDestroy (new);
415 v = FcValueCanonicalize(&FcPatternEltValues (fe)->value);
416 FcPatternObjectAdd (new, fe->object, v, FcFalse);
418 for (i = 0; i < pat->num; i++)
420 pe = &FcPatternElts(pat)[i];
421 fe = FcPatternObjectFindElt (font, pe->object);
424 v = FcValueCanonicalize(&FcPatternEltValues(pe)->value);
425 FcPatternObjectAdd (new, pe->object, v, FcTrue);
429 FcConfigSubstituteWithPat (config, new, pat, FcMatchFont);
434 FcFontSetMatchInternal (FcConfig *config,
440 double score[NUM_MATCH_VALUES], bestscore[NUM_MATCH_VALUES];
447 for (i = 0; i < NUM_MATCH_VALUES; i++)
450 if (FcDebug () & FC_DBG_MATCH)
455 for (set = 0; set < nsets; set++)
460 for (f = 0; f < s->nfont; f++)
462 if (FcDebug () & FC_DBG_MATCHV)
464 printf ("Font %d ", f);
465 FcPatternPrint (s->fonts[f]);
467 if (!FcCompare (p, s->fonts[f], score, result))
469 if (FcDebug () & FC_DBG_MATCHV)
472 for (i = 0; i < NUM_MATCH_VALUES; i++)
474 printf (" %g", score[i]);
478 for (i = 0; i < NUM_MATCH_VALUES; i++)
480 if (best && bestscore[i] < score[i])
482 if (!best || score[i] < bestscore[i])
484 for (i = 0; i < NUM_MATCH_VALUES; i++)
485 bestscore[i] = score[i];
492 if (FcDebug () & FC_DBG_MATCH)
494 printf ("Best score");
495 for (i = 0; i < NUM_MATCH_VALUES; i++)
496 printf (" %g", bestscore[i]);
498 FcPatternPrint (best);
502 *result = FcResultNoMatch;
509 FcFontSetMatch (FcConfig *config,
519 config = FcConfigGetCurrent ();
523 best = FcFontSetMatchInternal (config, sets, nsets, p, result);
525 return FcFontRenderPrepare (config, p, best);
531 FcFontMatch (FcConfig *config,
541 config = FcConfigGetCurrent ();
546 if (config->fonts[FcSetSystem])
547 sets[nsets++] = config->fonts[FcSetSystem];
548 if (config->fonts[FcSetApplication])
549 sets[nsets++] = config->fonts[FcSetApplication];
551 best = FcFontSetMatchInternal (config, sets, nsets, p, result);
553 return FcFontRenderPrepare (config, p, best);
558 typedef struct _FcSortNode {
560 double score[NUM_MATCH_VALUES];
564 FcSortCompare (const void *aa, const void *ab)
566 FcSortNode *a = *(FcSortNode **) aa;
567 FcSortNode *b = *(FcSortNode **) ab;
568 double *as = &a->score[0];
569 double *bs = &b->score[0];
570 double ad = 0, bd = 0;
573 i = NUM_MATCH_VALUES;
574 while (i-- && (ad = *as++) == (bd = *bs++))
576 return ad < bd ? -1 : ad > bd ? 1 : 0;
580 FcSortWalk (FcSortNode **n, int nnode, FcFontSet *fs, FcCharSet **csp, FcBool trim)
582 FcBool ret = FcFalse;
588 cs = FcCharSetCreate ();
595 FcSortNode *node = *n++;
596 FcBool adds_chars = FcFalse;
599 * Only fetch node charset if we'd need it
605 if (FcPatternGetCharSet (node->pattern, FC_CHARSET, 0, &ncs) !=
609 if (!FcCharSetMerge (cs, ncs, &adds_chars))
614 * If this font isn't a subset of the previous fonts,
617 if (!trim || adds_chars)
619 FcPatternReference (node->pattern);
620 if (FcDebug () & FC_DBG_MATCHV)
623 FcPatternPrint (node->pattern);
625 if (!FcFontSetAdd (fs, node->pattern))
627 FcPatternDestroy (node->pattern);
642 FcCharSetDestroy (cs);
648 FcFontSetSortDestroy (FcFontSet *fs)
650 FcFontSetDestroy (fs);
654 FcFontSetSort (FcConfig *config,
665 FcSortNode **nodeps, **nodep;
672 FcBool *patternLangSat;
675 if (FcDebug () & FC_DBG_MATCH)
681 for (set = 0; set < nsets; set++)
691 for (nPatternLang = 0;
692 FcPatternGet (p, FC_LANG, nPatternLang, &patternLang) == FcResultMatch;
697 nodes = malloc (nnodes * sizeof (FcSortNode) +
698 nnodes * sizeof (FcSortNode *) +
699 nPatternLang * sizeof (FcBool));
702 nodeps = (FcSortNode **) (nodes + nnodes);
703 patternLangSat = (FcBool *) (nodeps + nnodes);
707 for (set = 0; set < nsets; set++)
712 for (f = 0; f < s->nfont; f++)
714 if (FcDebug () & FC_DBG_MATCHV)
716 printf ("Font %d ", f);
717 FcPatternPrint (s->fonts[f]);
719 new->pattern = s->fonts[f];
720 if (!FcCompare (p, new->pattern, new->score, result))
722 if (FcDebug () & FC_DBG_MATCHV)
725 for (i = 0; i < NUM_MATCH_VALUES; i++)
727 printf (" %g", new->score[i]);
737 nnodes = new - nodes;
739 qsort (nodeps, nnodes, sizeof (FcSortNode *),
742 for (i = 0; i < nPatternLang; i++)
743 patternLangSat[i] = FcFalse;
745 for (f = 0; f < nnodes; f++)
747 FcBool satisfies = FcFalse;
749 * If this node matches any language, go check
750 * which ones and satisfy those entries
752 if (nodeps[f]->score[MATCH_LANG_INDEX] < 2000)
754 for (i = 0; i < nPatternLang; i++)
758 if (!patternLangSat[i] &&
759 FcPatternGet (p, FC_LANG, i, &patternLang) == FcResultMatch &&
760 FcPatternGet (nodeps[f]->pattern, FC_LANG, 0, &nodeLang) == FcResultMatch)
762 double compare = FcCompareLang (&patternLang, &nodeLang);
763 if (compare >= 0 && compare < 2)
765 if (FcDebug () & FC_DBG_MATCHV)
770 if (FcPatternGetString (nodeps[f]->pattern, FC_FAMILY, 0, &family) == FcResultMatch &&
771 FcPatternGetString (nodeps[f]->pattern, FC_STYLE, 0, &style) == FcResultMatch)
772 printf ("Font %s:%s matches language %d\n", family, style, i);
774 patternLangSat[i] = FcTrue;
782 nodeps[f]->score[MATCH_LANG_INDEX] = 10000.0;
786 * Re-sort once the language issues have been settled
788 qsort (nodeps, nnodes, sizeof (FcSortNode *),
791 ret = FcFontSetCreate ();
795 if (!FcSortWalk (nodeps, nnodes, ret, csp, trim))
800 if (FcDebug() & FC_DBG_MATCH)
802 printf ("First font ");
803 FcPatternPrint (ret->fonts[0]);
808 FcFontSetDestroy (ret);
816 FcFontSort (FcConfig *config,
827 config = FcConfigGetCurrent ();
832 if (config->fonts[FcSetSystem])
833 sets[nsets++] = config->fonts[FcSetSystem];
834 if (config->fonts[FcSetApplication])
835 sets[nsets++] = config->fonts[FcSetApplication];
836 return FcFontSetSort (config, sets, nsets, p, trim, csp, result);
839 #include "fcaliastail.h"