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)
250 i = MATCH_FILE; break;
251 case FC_FOUNDRY_OBJECT:
252 i = MATCH_FOUNDRY; break;
253 case FC_FONTVERSION_OBJECT:
254 i = MATCH_FONTVERSION; break;
255 case FC_FAMILY_OBJECT:
256 i = MATCH_FAMILY; break;
257 case FC_CHARSET_OBJECT:
258 i = MATCH_CHARSET; break;
259 case FC_ANTIALIAS_OBJECT:
260 i = MATCH_ANTIALIAS; break;
262 i = MATCH_LANG; break;
263 case FC_SPACING_OBJECT:
264 i = MATCH_SPACING; break;
265 case FC_STYLE_OBJECT:
266 i = MATCH_STYLE; break;
267 case FC_SLANT_OBJECT:
268 i = MATCH_SLANT; break;
269 case FC_PIXEL_SIZE_OBJECT:
270 i = MATCH_PIXEL_SIZE; break;
271 case FC_WIDTH_OBJECT:
272 i = MATCH_WIDTH; break;
273 case FC_WEIGHT_OBJECT:
274 i = MATCH_WEIGHT; break;
275 case FC_RASTERIZER_OBJECT:
276 i = MATCH_RASTERIZER; break;
277 case FC_OUTLINE_OBJECT:
278 i = MATCH_OUTLINE; break;
279 case FC_DECORATIVE_OBJECT:
280 i = MATCH_DECORATIVE; break;
286 return _FcMatchers+i;
290 FcCompareValueList (FcObject object,
291 FcValueListPtr v1orig, /* pattern */
292 FcValueListPtr v2orig, /* target */
297 FcValueListPtr v1, v2;
298 double v, best, bestStrong, bestWeak;
300 const FcMatcher *match = FcObjectToMatcher(object);
305 *bestValue = FcValueCanonicalize(&v2orig->value);
313 for (v1 = v1orig; v1; v1 = FcValueListNext(v1))
315 for (v2 = v2orig; v2; v2 = FcValueListNext(v2))
317 v = (match->compare) (&v1->value, &v2->value);
320 *result = FcResultTypeMismatch;
327 *bestValue = FcValueCanonicalize(&v2->value);
330 if (v1->binding == FcValueBindingStrong)
343 if (FcDebug () & FC_DBG_MATCHV)
345 printf (" %s: %g ", FcObjectName (object), best);
346 FcValueListPrint (v1orig);
348 FcValueListPrint (v2orig);
353 int weak = match->weak;
354 int strong = match->strong;
356 value[strong] += best;
359 value[weak] += bestWeak;
360 value[strong] += bestStrong;
367 * Return a value indicating the distance between the two lists of
372 FcCompare (FcPattern *pat,
379 for (i = 0; i < NUM_MATCH_VALUES; i++)
384 while (i1 < pat->num && i2 < fnt->num)
386 FcPatternElt *elt_i1 = &FcPatternElts(pat)[i1];
387 FcPatternElt *elt_i2 = &FcPatternElts(fnt)[i2];
389 i = FcObjectCompare(elt_i1->object, elt_i2->object);
396 if (!FcCompareValueList (elt_i1->object,
397 FcPatternEltValues(elt_i1),
398 FcPatternEltValues(elt_i2),
409 FcFontRenderPrepare (FcConfig *config,
415 FcPatternElt *fe, *pe;
419 assert (pat != NULL);
420 assert (font != NULL);
422 new = FcPatternCreate ();
425 for (i = 0; i < font->num; i++)
427 fe = &FcPatternElts(font)[i];
428 pe = FcPatternObjectFindElt (pat, fe->object);
431 if (!FcCompareValueList (pe->object, FcPatternEltValues(pe),
432 FcPatternEltValues(fe), &v, 0, &result))
434 FcPatternDestroy (new);
439 v = FcValueCanonicalize(&FcPatternEltValues (fe)->value);
440 FcPatternObjectAdd (new, fe->object, v, FcFalse);
442 for (i = 0; i < pat->num; i++)
444 pe = &FcPatternElts(pat)[i];
445 fe = FcPatternObjectFindElt (font, pe->object);
448 v = FcValueCanonicalize(&FcPatternEltValues(pe)->value);
449 FcPatternObjectAdd (new, pe->object, v, FcTrue);
453 FcConfigSubstituteWithPat (config, new, pat, FcMatchFont);
458 FcFontSetMatchInternal (FcConfig *config,
464 double score[NUM_MATCH_VALUES], bestscore[NUM_MATCH_VALUES];
471 for (i = 0; i < NUM_MATCH_VALUES; i++)
474 if (FcDebug () & FC_DBG_MATCH)
479 for (set = 0; set < nsets; set++)
484 for (f = 0; f < s->nfont; f++)
486 if (FcDebug () & FC_DBG_MATCHV)
488 printf ("Font %d ", f);
489 FcPatternPrint (s->fonts[f]);
491 if (!FcCompare (p, s->fonts[f], score, result))
493 if (FcDebug () & FC_DBG_MATCHV)
496 for (i = 0; i < NUM_MATCH_VALUES; i++)
498 printf (" %g", score[i]);
502 for (i = 0; i < NUM_MATCH_VALUES; i++)
504 if (best && bestscore[i] < score[i])
506 if (!best || score[i] < bestscore[i])
508 for (i = 0; i < NUM_MATCH_VALUES; i++)
509 bestscore[i] = score[i];
516 if (FcDebug () & FC_DBG_MATCH)
518 printf ("Best score");
519 for (i = 0; i < NUM_MATCH_VALUES; i++)
520 printf (" %g", bestscore[i]);
522 FcPatternPrint (best);
524 /* assuming that 'result' is initialized with FcResultNoMatch
525 * outside this function */
527 *result = FcResultMatch;
533 FcFontSetMatch (FcConfig *config,
541 assert (sets != NULL);
543 assert (result != NULL);
545 *result = FcResultNoMatch;
549 config = FcConfigGetCurrent ();
553 best = FcFontSetMatchInternal (config, sets, nsets, p, result);
555 return FcFontRenderPrepare (config, p, best);
561 FcFontMatch (FcConfig *config,
570 assert (result != NULL);
572 *result = FcResultNoMatch;
576 config = FcConfigGetCurrent ();
581 if (config->fonts[FcSetSystem])
582 sets[nsets++] = config->fonts[FcSetSystem];
583 if (config->fonts[FcSetApplication])
584 sets[nsets++] = config->fonts[FcSetApplication];
586 best = FcFontSetMatchInternal (config, sets, nsets, p, result);
588 return FcFontRenderPrepare (config, p, best);
593 typedef struct _FcSortNode {
595 double score[NUM_MATCH_VALUES];
599 FcSortCompare (const void *aa, const void *ab)
601 FcSortNode *a = *(FcSortNode **) aa;
602 FcSortNode *b = *(FcSortNode **) ab;
603 double *as = &a->score[0];
604 double *bs = &b->score[0];
605 double ad = 0, bd = 0;
608 i = NUM_MATCH_VALUES;
609 while (i-- && (ad = *as++) == (bd = *bs++))
611 return ad < bd ? -1 : ad > bd ? 1 : 0;
615 FcSortWalk (FcSortNode **n, int nnode, FcFontSet *fs, FcCharSet **csp, FcBool trim)
617 FcBool ret = FcFalse;
623 cs = FcCharSetCreate ();
630 FcSortNode *node = *n++;
631 FcBool adds_chars = FcFalse;
634 * Only fetch node charset if we'd need it
640 if (FcPatternGetCharSet (node->pattern, FC_CHARSET, 0, &ncs) !=
644 if (!FcCharSetMerge (cs, ncs, &adds_chars))
649 * If this font isn't a subset of the previous fonts,
652 if (!trim || adds_chars)
654 FcPatternReference (node->pattern);
655 if (FcDebug () & FC_DBG_MATCHV)
658 FcPatternPrint (node->pattern);
660 if (!FcFontSetAdd (fs, node->pattern))
662 FcPatternDestroy (node->pattern);
677 FcCharSetDestroy (cs);
683 FcFontSetSortDestroy (FcFontSet *fs)
685 FcFontSetDestroy (fs);
689 FcFontSetSort (FcConfig *config,
700 FcSortNode **nodeps, **nodep;
707 FcBool *patternLangSat;
710 assert (sets != NULL);
712 assert (result != NULL);
714 /* There are some implementation that relying on the result of
715 * "result" to check if the return value of FcFontSetSort
717 * So we should initialize it to the conservative way since
718 * this function doesn't return NULL anymore.
721 *result = FcResultNoMatch;
723 if (FcDebug () & FC_DBG_MATCH)
729 for (set = 0; set < nsets; set++)
737 return FcFontSetCreate ();
739 for (nPatternLang = 0;
740 FcPatternGet (p, FC_LANG, nPatternLang, &patternLang) == FcResultMatch;
745 nodes = malloc (nnodes * sizeof (FcSortNode) +
746 nnodes * sizeof (FcSortNode *) +
747 nPatternLang * sizeof (FcBool));
750 nodeps = (FcSortNode **) (nodes + nnodes);
751 patternLangSat = (FcBool *) (nodeps + nnodes);
755 for (set = 0; set < nsets; set++)
760 for (f = 0; f < s->nfont; f++)
762 if (FcDebug () & FC_DBG_MATCHV)
764 printf ("Font %d ", f);
765 FcPatternPrint (s->fonts[f]);
767 new->pattern = s->fonts[f];
768 if (!FcCompare (p, new->pattern, new->score, result))
770 if (FcDebug () & FC_DBG_MATCHV)
773 for (i = 0; i < NUM_MATCH_VALUES; i++)
775 printf (" %g", new->score[i]);
785 nnodes = new - nodes;
787 qsort (nodeps, nnodes, sizeof (FcSortNode *),
790 for (i = 0; i < nPatternLang; i++)
791 patternLangSat[i] = FcFalse;
793 for (f = 0; f < nnodes; f++)
795 FcBool satisfies = FcFalse;
797 * If this node matches any language, go check
798 * which ones and satisfy those entries
800 if (nodeps[f]->score[MATCH_LANG_INDEX] < 2000)
802 for (i = 0; i < nPatternLang; i++)
806 if (!patternLangSat[i] &&
807 FcPatternGet (p, FC_LANG, i, &patternLang) == FcResultMatch &&
808 FcPatternGet (nodeps[f]->pattern, FC_LANG, 0, &nodeLang) == FcResultMatch)
810 double compare = FcCompareLang (&patternLang, &nodeLang);
811 if (compare >= 0 && compare < 2)
813 if (FcDebug () & FC_DBG_MATCHV)
818 if (FcPatternGetString (nodeps[f]->pattern, FC_FAMILY, 0, &family) == FcResultMatch &&
819 FcPatternGetString (nodeps[f]->pattern, FC_STYLE, 0, &style) == FcResultMatch)
820 printf ("Font %s:%s matches language %d\n", family, style, i);
822 patternLangSat[i] = FcTrue;
830 nodeps[f]->score[MATCH_LANG_INDEX] = 10000.0;
834 * Re-sort once the language issues have been settled
836 qsort (nodeps, nnodes, sizeof (FcSortNode *),
839 ret = FcFontSetCreate ();
843 if (!FcSortWalk (nodeps, nnodes, ret, csp, trim))
848 if (FcDebug() & FC_DBG_MATCH)
850 printf ("First font ");
851 FcPatternPrint (ret->fonts[0]);
854 *result = FcResultMatch;
859 FcFontSetDestroy (ret);
867 FcFontSort (FcConfig *config,
877 assert (result != NULL);
879 *result = FcResultNoMatch;
883 config = FcConfigGetCurrent ();
888 if (config->fonts[FcSetSystem])
889 sets[nsets++] = config->fonts[FcSetSystem];
890 if (config->fonts[FcSetApplication])
891 sets[nsets++] = config->fonts[FcSetApplication];
892 return FcFontSetSort (config, sets, nsets, p, trim, csp, result);
895 #include "fcaliastail.h"