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 (const FcValue *value1, const FcValue *value2, FcValue *bestValue)
32 switch ((int) value1->type) {
34 v1 = (double) value1->u.i;
42 switch ((int) value2->type) {
44 v2 = (double) value2->u.i;
55 *bestValue = FcValueCanonicalize (value2);
60 FcCompareString (const FcValue *v1, const FcValue *v2, FcValue *bestValue)
62 *bestValue = FcValueCanonicalize (v2);
63 return (double) FcStrCmpIgnoreCase (FcValueString(v1), FcValueString(v2)) != 0;
67 FcCompareFamily (const FcValue *v1, const FcValue *v2, FcValue *bestValue)
69 /* rely on the guarantee in FcPatternObjectAddWithBinding that
70 * families are always FcTypeString. */
71 const FcChar8* v1_string = FcValueString(v1);
72 const FcChar8* v2_string = FcValueString(v2);
74 *bestValue = FcValueCanonicalize (v2);
76 if (FcToLower(*v1_string) != FcToLower(*v2_string) &&
77 *v1_string != ' ' && *v2_string != ' ')
80 return (double) FcStrCmpIgnoreBlanksAndCase (v1_string, v2_string) != 0;
84 FcComparePostScript (const FcValue *v1, const FcValue *v2, FcValue *bestValue)
86 const FcChar8 *v1_string = FcValueString (v1);
87 const FcChar8 *v2_string = FcValueString (v2);
89 size_t len1, len2, mlen;
91 *bestValue = FcValueCanonicalize (v2);
93 if (FcToLower (*v1_string) != FcToLower (*v2_string) &&
94 *v1_string != ' ' && *v2_string != ' ')
97 n = FcStrMatchIgnoreCaseAndDelims (v1_string, v2_string, (const FcChar8 *)" -");
98 len1 = strlen ((const char *)v1_string);
99 len2 = strlen ((const char *)v2_string);
100 mlen = FC_MAX (len1, len2);
102 return (double)(mlen - n) / (double)mlen;
106 FcCompareLang (const FcValue *v1, const FcValue *v2, FcValue *bestValue)
110 switch ((int) v1->type) {
112 switch ((int) v2->type) {
114 result = FcLangSetCompare (FcValueLangSet (v1), FcValueLangSet (v2));
117 result = FcLangSetHasLang (FcValueLangSet (v1), FcValueString (v2));
124 switch ((int) v2->type) {
126 result = FcLangSetHasLang (FcValueLangSet (v2), FcValueString (v1));
129 result = FcLangCompare (FcValueString (v1), FcValueString (v2));
138 *bestValue = FcValueCanonicalize (v2);
142 case FcLangDifferentCountry:
144 case FcLangDifferentLang:
151 FcCompareBool (const FcValue *v1, const FcValue *v2, FcValue *bestValue)
153 if (v2->type != FcTypeBool || v1->type != FcTypeBool)
156 bestValue->type = FcTypeBool;
157 if (v2->u.b != FcDontCare)
158 bestValue->u.b = v2->u.b;
160 bestValue->u.b = v1->u.b;
162 return (double) ((v2->u.b ^ v1->u.b) == 1);
166 FcCompareCharSet (const FcValue *v1, const FcValue *v2, FcValue *bestValue)
168 *bestValue = FcValueCanonicalize (v2); /* TODO Improve. */
169 return (double) FcCharSetSubtractCount (FcValueCharSet(v1), FcValueCharSet(v2));
173 FcCompareRange (const FcValue *v1, const FcValue *v2, FcValue *bestValue)
175 FcValue value1 = FcValueCanonicalize (v1);
176 FcValue value2 = FcValueCanonicalize (v2);
177 double b1, e1, b2, e2, d;
179 switch ((int) value1.type) {
181 b1 = e1 = value1.u.i;
184 b1 = e1 = value1.u.d;
187 b1 = value1.u.r->begin;
188 e1 = value1.u.r->end;
193 switch ((int) value2.type) {
195 b2 = e2 = value2.u.i;
198 b2 = e2 = value2.u.d;
201 b2 = value2.u.r->begin;
202 e2 = value2.u.r->end;
213 d = (FC_MAX (b1, b2) + FC_MIN (e1, e2)) * .5;
215 bestValue->type = FcTypeDouble;
218 /* If the ranges overlap, it's a match, otherwise return closest distance. */
219 if (e1 < b2 || e2 < b1)
220 return FC_MIN (fabs (b2 - e1), fabs (b1 - e2));
226 FcCompareSize (const FcValue *v1, const FcValue *v2, FcValue *bestValue)
228 FcValue value1 = FcValueCanonicalize (v1);
229 FcValue value2 = FcValueCanonicalize (v2);
230 double b1, e1, b2, e2;
232 switch ((int) value1.type) {
234 b1 = e1 = value1.u.i;
237 b1 = e1 = value1.u.d;
240 b1 = value1.u.r->begin;
241 e1 = value1.u.r->end;
246 switch ((int) value2.type) {
248 b2 = e2 = value2.u.i;
251 b2 = e2 = value2.u.d;
254 b2 = value2.u.r->begin;
255 e2 = value2.u.r->end;
261 bestValue->type = FcTypeDouble;
262 bestValue->u.d = (b1 + e1) * .5;
264 /* If the ranges overlap, it's a match, otherwise return closest distance. */
265 if (e1 < b2 || e2 < b1)
266 return FC_MIN (fabs (b2 - e1), fabs (b1 - e2));
267 if (b2 != e2 && b1 == e2) /* Semi-closed interval. */
274 FcCompareFilename (const FcValue *v1, const FcValue *v2, FcValue *bestValue)
276 const FcChar8 *s1 = FcValueString (v1), *s2 = FcValueString (v2);
277 *bestValue = FcValueCanonicalize (v2);
278 if (FcStrCmp (s1, s2) == 0)
280 else if (FcStrCmpIgnoreCase (s1, s2) == 0)
282 else if (FcStrGlobMatch (s1, s2))
289 /* Define priorities to -1 for objects that don't have a compare function. */
291 #define PRI_NULL(n) \
292 PRI_ ## n ## _STRONG = -1, \
293 PRI_ ## n ## _WEAK = -1,
295 #define PRI_FcCompareFamily(n) PRI1(n)
296 #define PRI_FcCompareString(n) PRI1(n)
297 #define PRI_FcCompareNumber(n) PRI1(n)
298 #define PRI_FcCompareBool(n) PRI1(n)
299 #define PRI_FcCompareFilename(n) PRI1(n)
300 #define PRI_FcCompareCharSet(n) PRI1(n)
301 #define PRI_FcCompareLang(n) PRI1(n)
302 #define PRI_FcComparePostScript(n) PRI1(n)
303 #define PRI_FcCompareRange(n) PRI1(n)
304 #define PRI_FcCompareSize(n) PRI1(n)
306 #define FC_OBJECT(NAME, Type, Cmp) PRI_##Cmp(NAME)
308 typedef enum _FcMatcherPriorityDummy {
310 } FcMatcherPriorityDummy;
315 /* Canonical match priority order. */
320 PRI_ ## n ## _STRONG = PRI_ ## n, \
321 PRI_ ## n ## _WEAK = PRI_ ## n
323 typedef enum _FcMatcherPriority {
332 PRI_POSTSCRIPT_NAME_STRONG,
335 PRI_POSTSCRIPT_NAME_WEAK,
356 typedef struct _FcMatcher {
358 double (*compare) (const FcValue *v1, const FcValue *v2, FcValue *bestValue);
363 * Order is significant, it defines the precedence of
364 * each value, earlier values are more significant than
367 #define FC_OBJECT(NAME, Type, Cmp) { FC_##NAME##_OBJECT, Cmp, PRI_##NAME##_STRONG, PRI_##NAME##_WEAK },
368 static const FcMatcher _FcMatchers [] = {
369 { FC_INVALID_OBJECT, NULL, -1, -1 },
374 static const FcMatcher*
375 FcObjectToMatcher (FcObject object,
381 case FC_FAMILYLANG_OBJECT:
382 case FC_STYLELANG_OBJECT:
383 case FC_FULLNAMELANG_OBJECT:
384 object = FC_LANG_OBJECT;
388 if (object > FC_MAX_BASE_OBJECT ||
389 !_FcMatchers[object].compare ||
390 _FcMatchers[object].strong == -1 ||
391 _FcMatchers[object].weak == -1)
394 return _FcMatchers + object;
398 FcCompareValueList (FcObject object,
399 const FcMatcher *match,
400 FcValueListPtr v1orig, /* pattern */
401 FcValueListPtr v2orig, /* target */
407 FcValueListPtr v1, v2;
408 double v, best, bestStrong, bestWeak;
415 *bestValue = FcValueCanonicalize(&v2orig->value);
422 strong = match->strong;
427 for (v1 = v1orig, j = 0; v1; v1 = FcValueListNext(v1), j++)
429 for (v2 = v2orig, k = 0; v2; v2 = FcValueListNext(v2), k++)
432 v = (match->compare) (&v1->value, &v2->value, &matchValue);
435 *result = FcResultTypeMismatch;
438 v = v * 1000 + j * 100 + k * (v2->value.type == FcTypeString ? 1 : 0);
442 *bestValue = matchValue;
448 /* found the best possible match */
452 else if (v1->binding == FcValueBindingStrong)
465 if (FcDebug () & FC_DBG_MATCHV)
467 printf (" %s: %g ", FcObjectName (object), best);
468 FcValueListPrint (v1orig);
470 FcValueListPrint (v2orig);
476 value[strong] += best;
479 value[weak] += bestWeak;
480 value[strong] += bestStrong;
489 /* The bulk of the time in FcFontMatch and FcFontSort goes to
490 * walking long lists of family names. We speed this up with a
501 FcHashTable *family_hash;
505 FcCompareDataClear (FcCompareData *data)
507 FcHashTableDestroy (data->family_hash);
511 FcCompareDataInit (FcPattern *pat,
521 table = FcHashTableCreate ((FcHashFunc)FcStrHashIgnoreBlanksAndCase,
522 (FcCompareFunc)FcStrCmpIgnoreBlanksAndCase,
528 elt = FcPatternObjectFindElt (pat, FC_FAMILY_OBJECT);
531 for (l = FcPatternEltValues(elt), i = 0; l; l = FcValueListNext(l), i++)
533 key = FcValueString (&l->value);
534 if (!FcHashTableFind (table, key, (void **)&e))
536 e = malloc (sizeof (FamilyEntry));
537 e->strong_value = 1e99;
538 e->weak_value = 1e99;
539 FcHashTableAdd (table, (void *)key, e);
541 if (l->binding == FcValueBindingWeak)
543 if (i < e->weak_value)
548 if (i < e->strong_value)
554 data->family_hash = table;
558 FcCompareFamilies (FcPattern *pat,
559 FcValueListPtr v1orig,
561 FcValueListPtr v2orig,
572 assert (table != NULL);
577 for (v2 = v2orig; v2; v2 = FcValueListNext(v2))
579 key = FcValueString (&v2->value);
580 if (FcHashTableFind (table, key, (void **)&e))
582 if (e->strong_value < strong_value)
583 strong_value = e->strong_value;
584 if (e->weak_value < weak_value)
585 weak_value = e->weak_value;
588 if (FcDebug () & FC_DBG_MATCHV)
590 printf ("%s: %g ", FcObjectName (FC_FAMILY_OBJECT), strong_value);
591 FcValueListPrint (v1orig);
593 FcValueListPrint (v2orig);
597 value[PRI_FAMILY_STRONG] = strong_value;
598 value[PRI_FAMILY_WEAK] = weak_value;
604 * Return a value indicating the distance between the two lists of
609 FcCompare (FcPattern *pat,
617 for (i = 0; i < PRI_END; i++)
622 while (i1 < pat->num && i2 < fnt->num)
624 FcPatternElt *elt_i1 = &FcPatternElts(pat)[i1];
625 FcPatternElt *elt_i2 = &FcPatternElts(fnt)[i2];
627 i = FcObjectCompare(elt_i1->object, elt_i2->object);
632 else if (elt_i1->object == FC_FAMILY_OBJECT && data->family_hash)
634 if (!FcCompareFamilies (pat, FcPatternEltValues(elt_i1),
635 fnt, FcPatternEltValues(elt_i2),
644 const FcMatcher *match = FcObjectToMatcher (elt_i1->object, FcFalse);
645 if (!FcCompareValueList (elt_i1->object, match,
646 FcPatternEltValues(elt_i1),
647 FcPatternEltValues(elt_i2),
648 NULL, value, NULL, result))
658 FcFontRenderPrepare (FcConfig *config,
664 FcPatternElt *fe, *pe;
667 FcBool variable = FcFalse;
670 assert (pat != NULL);
671 assert (font != NULL);
673 FcPatternObjectGetBool (font, FC_VARIABLE_OBJECT, 0, &variable);
674 assert (variable != FcDontCare);
676 FcStrBufInit (&variations, NULL, 0);
678 new = FcPatternCreate ();
681 for (i = 0; i < font->num; i++)
683 fe = &FcPatternElts(font)[i];
684 if (fe->object == FC_FAMILYLANG_OBJECT ||
685 fe->object == FC_STYLELANG_OBJECT ||
686 fe->object == FC_FULLNAMELANG_OBJECT)
688 /* ignore those objects. we need to deal with them
692 if (fe->object == FC_FAMILY_OBJECT ||
693 fe->object == FC_STYLE_OBJECT ||
694 fe->object == FC_FULLNAME_OBJECT)
696 FcPatternElt *fel, *pel;
698 FC_ASSERT_STATIC ((FC_FAMILY_OBJECT + 1) == FC_FAMILYLANG_OBJECT);
699 FC_ASSERT_STATIC ((FC_STYLE_OBJECT + 1) == FC_STYLELANG_OBJECT);
700 FC_ASSERT_STATIC ((FC_FULLNAME_OBJECT + 1) == FC_FULLNAMELANG_OBJECT);
702 fel = FcPatternObjectFindElt (font, fe->object + 1);
703 pel = FcPatternObjectFindElt (pat, fe->object + 1);
707 /* The font has name languages, and pattern asks for specific language(s).
708 * Match on language and and prefer that result.
709 * Note: Currently the code only give priority to first matching language.
712 FcValueListPtr l1, l2, ln = NULL, ll = NULL;
713 const FcMatcher *match = FcObjectToMatcher (pel->object, FcTrue);
715 if (!FcCompareValueList (pel->object, match,
716 FcPatternEltValues (pel),
717 FcPatternEltValues (fel), NULL, NULL, &n, &result))
719 FcPatternDestroy (new);
723 for (j = 0, l1 = FcPatternEltValues (fe), l2 = FcPatternEltValues (fel);
724 l1 != NULL || l2 != NULL;
725 j++, l1 = l1 ? FcValueListNext (l1) : NULL, l2 = l2 ? FcValueListNext (l2) : NULL)
727 FcValueListPtr (* func) (FcValueListPtr, FcValue, FcValueBinding);
728 FcValueBinding binding = FcValueBindingEnd;
732 binding = FcValueBindingStrong;
733 func = FcValueListPrepend;
736 func = FcValueListAppend;
740 FcValueCanonicalize (&l1->value),
745 if (binding == FcValueBindingEnd)
746 binding = l2->binding;
748 FcValueCanonicalize (&l2->value),
752 FcPatternObjectListAdd (new, fe->object, ln, FcFalse);
753 FcPatternObjectListAdd (new, fel->object, ll, FcFalse);
759 /* Pattern doesn't ask for specific language. Copy all for name and
761 FcValueListPtr l1, l2;
763 l1 = FcValueListDuplicate (FcPatternEltValues (fe));
764 l2 = FcValueListDuplicate (FcPatternEltValues (fel));
765 FcPatternObjectListAdd (new, fe->object, l1, FcFalse);
766 FcPatternObjectListAdd (new, fel->object, l2, FcFalse);
772 pe = FcPatternObjectFindElt (pat, fe->object);
775 const FcMatcher *match = FcObjectToMatcher (pe->object, FcFalse);
776 if (!FcCompareValueList (pe->object, match,
777 FcPatternEltValues(pe),
778 FcPatternEltValues(fe), &v, NULL, NULL, &result))
780 FcPatternDestroy (new);
783 FcPatternObjectAdd (new, fe->object, v, FcFalse);
785 /* Set font-variations settings for standard axes in variable fonts. */
787 FcPatternEltValues(fe)->value.type == FcTypeRange &&
788 (fe->object == FC_WEIGHT_OBJECT ||
789 fe->object == FC_WIDTH_OBJECT ||
790 fe->object == FC_SIZE_OBJECT))
794 const char *tag = " ";
795 assert (v.type == FcTypeDouble);
798 FcStrBufChar (&variations, ',');
801 case FC_WEIGHT_OBJECT:
803 num = FcWeightToOpenType (num);
806 case FC_WIDTH_OBJECT:
814 sprintf ((char *) temp, "%4s=%g", tag, num);
815 FcStrBufString (&variations, temp);
820 FcPatternObjectListAdd (new, fe->object,
821 FcValueListDuplicate (FcPatternEltValues (fe)),
825 for (i = 0; i < pat->num; i++)
827 pe = &FcPatternElts(pat)[i];
828 fe = FcPatternObjectFindElt (font, pe->object);
830 pe->object != FC_FAMILYLANG_OBJECT &&
831 pe->object != FC_STYLELANG_OBJECT &&
832 pe->object != FC_FULLNAMELANG_OBJECT)
834 FcPatternObjectListAdd (new, pe->object,
835 FcValueListDuplicate (FcPatternEltValues(pe)),
840 if (variable && variations.len)
842 FcChar8 *vars = NULL;
843 if (FcPatternObjectGetString (new, FC_FONT_VARIATIONS_OBJECT, 0, &vars) == FcResultMatch)
845 FcStrBufChar (&variations, ',');
846 FcStrBufString (&variations, vars);
847 FcPatternObjectDel (new, FC_FONT_VARIATIONS_OBJECT);
850 FcPatternObjectAddString (new, FC_FONT_VARIATIONS_OBJECT, FcStrBufDoneStatic (&variations));
851 FcStrBufDestroy (&variations);
854 FcConfigSubstituteWithPat (config, new, pat, FcMatchFont);
859 FcFontSetMatchInternal (FcFontSet **sets,
864 double score[PRI_END], bestscore[PRI_END];
867 FcPattern *best, *pat = NULL;
871 const FcPatternElt *elt;
873 for (i = 0; i < PRI_END; i++)
876 if (FcDebug () & FC_DBG_MATCH)
882 FcCompareDataInit (p, &data);
884 for (set = 0; set < nsets; set++)
889 for (f = 0; f < s->nfont; f++)
891 if (FcDebug () & FC_DBG_MATCHV)
893 printf ("Font %d ", f);
894 FcPatternPrint (s->fonts[f]);
896 if (!FcCompare (p, s->fonts[f], score, result, &data))
898 FcCompareDataClear (&data);
901 if (FcDebug () & FC_DBG_MATCHV)
904 for (i = 0; i < PRI_END; i++)
906 printf (" %g", score[i]);
910 for (i = 0; i < PRI_END; i++)
912 if (best && bestscore[i] < score[i])
914 if (!best || score[i] < bestscore[i])
916 for (i = 0; i < PRI_END; i++)
917 bestscore[i] = score[i];
925 FcCompareDataClear (&data);
927 /* Update the binding according to the score to indicate how exactly values matches on. */
930 pat = FcPatternCreate ();
931 elt = FcPatternElts (best);
932 for (i = 0; i < FcPatternObjectCount (best); i++)
934 const FcMatcher *match = FcObjectToMatcher (elt[i].object, FcFalse);
935 FcValueListPtr l = FcPatternEltValues (&elt[i]);
938 FcPatternObjectListAdd(pat, elt[i].object,
939 FcValueListDuplicate(l), FcTrue);
942 FcValueBinding binding = FcValueBindingWeak;
943 FcValueListPtr new = NULL, ll, t = NULL;
946 /* If the value was matched exactly, update the binding to Strong. */
947 if (bestscore[match->strong] < 1000)
948 binding = FcValueBindingStrong;
950 for (ll = l; ll != NULL; ll = FcValueListNext (ll))
954 t = new = FcValueListCreate ();
958 t->next = FcValueListCreate ();
959 t = FcValueListNext (t);
961 v = FcValueCanonicalize (&ll->value);
962 t->value = FcValueSave (v);
963 t->binding = binding;
966 FcPatternObjectListAdd (pat, elt[i].object, new, FcTrue);
970 if (FcDebug () & FC_DBG_MATCH)
972 printf ("Best score");
973 for (i = 0; i < PRI_END; i++)
974 printf (" %g", bestscore[i]);
976 FcPatternPrint (pat);
978 if (FcDebug () & FC_DBG_MATCH2)
980 char *env = getenv ("FC_DBG_MATCH_FILTER");
981 FcObjectSet *os = NULL;
989 ss = s = strdup (env);
990 os = FcObjectSetCreate ();
996 if (!(p = strchr (s, ',')))
1005 x = malloc (sizeof (char) * (len + 1));
1009 if (FcObjectFromName (x) > 0)
1010 FcObjectSetAdd (os, x);
1017 FcPatternPrint2 (p, pat, os);
1019 FcObjectSetDestroy (os);
1021 /* assuming that 'result' is initialized with FcResultNoMatch
1022 * outside this function */
1024 *result = FcResultMatch;
1030 FcFontSetMatch (FcConfig *config,
1036 FcPattern *best, *ret = NULL;
1038 assert (sets != NULL);
1040 assert (result != NULL);
1042 *result = FcResultNoMatch;
1044 config = FcConfigReference (config);
1047 best = FcFontSetMatchInternal (sets, nsets, p, result);
1050 ret = FcFontRenderPrepare (config, p, best);
1051 FcPatternDestroy (best);
1054 FcConfigDestroy (config);
1060 FcFontMatch (FcConfig *config,
1066 FcPattern *best, *ret = NULL;
1069 assert (result != NULL);
1071 *result = FcResultNoMatch;
1073 config = FcConfigReference (config);
1077 if (config->fonts[FcSetSystem])
1078 sets[nsets++] = config->fonts[FcSetSystem];
1079 if (config->fonts[FcSetApplication])
1080 sets[nsets++] = config->fonts[FcSetApplication];
1082 best = FcFontSetMatchInternal (sets, nsets, p, result);
1085 ret = FcFontRenderPrepare (config, p, best);
1086 FcPatternDestroy (best);
1089 FcConfigDestroy (config);
1094 typedef struct _FcSortNode {
1096 double score[PRI_END];
1100 FcSortCompare (const void *aa, const void *ab)
1102 FcSortNode *a = *(FcSortNode **) aa;
1103 FcSortNode *b = *(FcSortNode **) ab;
1104 double *as = &a->score[0];
1105 double *bs = &b->score[0];
1106 double ad = 0, bd = 0;
1110 while (i-- && (ad = *as++) == (bd = *bs++))
1112 return ad < bd ? -1 : ad > bd ? 1 : 0;
1116 FcSortWalk (FcSortNode **n, int nnode, FcFontSet *fs, FcCharSet **csp, FcBool trim)
1118 FcBool ret = FcFalse;
1125 cs = FcCharSetCreate ();
1130 for (i = 0; i < nnode; i++)
1132 FcSortNode *node = *n++;
1133 FcBool adds_chars = FcFalse;
1136 * Only fetch node charset if we'd need it
1142 if (FcPatternGetCharSet (node->pattern, FC_CHARSET, 0, &ncs) !=
1146 if (!FcCharSetMerge (cs, ncs, &adds_chars))
1151 * If this font isn't a subset of the previous fonts,
1152 * add it to the list
1154 if (!i || !trim || adds_chars)
1156 FcPatternReference (node->pattern);
1157 if (FcDebug () & FC_DBG_MATCHV)
1160 FcPatternPrint (node->pattern);
1162 if (!FcFontSetAdd (fs, node->pattern))
1164 FcPatternDestroy (node->pattern);
1179 FcCharSetDestroy (cs);
1185 FcFontSetSortDestroy (FcFontSet *fs)
1187 FcFontSetDestroy (fs);
1191 FcFontSetSort (FcConfig *config FC_UNUSED,
1202 FcSortNode **nodeps, **nodep;
1209 FcBool *patternLangSat;
1210 FcValue patternLang;
1213 assert (sets != NULL);
1215 assert (result != NULL);
1217 /* There are some implementation that relying on the result of
1218 * "result" to check if the return value of FcFontSetSort
1220 * So we should initialize it to the conservative way since
1221 * this function doesn't return NULL anymore.
1224 *result = FcResultNoMatch;
1226 if (FcDebug () & FC_DBG_MATCH)
1232 for (set = 0; set < nsets; set++)
1240 return FcFontSetCreate ();
1242 for (nPatternLang = 0;
1243 FcPatternGet (p, FC_LANG, nPatternLang, &patternLang) == FcResultMatch;
1248 nodes = malloc (nnodes * sizeof (FcSortNode) +
1249 nnodes * sizeof (FcSortNode *) +
1250 nPatternLang * sizeof (FcBool));
1253 nodeps = (FcSortNode **) (nodes + nnodes);
1254 patternLangSat = (FcBool *) (nodeps + nnodes);
1256 FcCompareDataInit (p, &data);
1260 for (set = 0; set < nsets; set++)
1265 for (f = 0; f < s->nfont; f++)
1267 if (FcDebug () & FC_DBG_MATCHV)
1269 printf ("Font %d ", f);
1270 FcPatternPrint (s->fonts[f]);
1272 new->pattern = s->fonts[f];
1273 if (!FcCompare (p, new->pattern, new->score, result, &data))
1275 if (FcDebug () & FC_DBG_MATCHV)
1278 for (i = 0; i < PRI_END; i++)
1280 printf (" %g", new->score[i]);
1290 FcCompareDataClear (&data);
1292 nnodes = new - nodes;
1294 qsort (nodeps, nnodes, sizeof (FcSortNode *),
1297 for (i = 0; i < nPatternLang; i++)
1298 patternLangSat[i] = FcFalse;
1300 for (f = 0; f < nnodes; f++)
1302 FcBool satisfies = FcFalse;
1304 * If this node matches any language, go check
1305 * which ones and satisfy those entries
1307 if (nodeps[f]->score[PRI_LANG] < 2000)
1309 for (i = 0; i < nPatternLang; i++)
1313 if (!patternLangSat[i] &&
1314 FcPatternGet (p, FC_LANG, i, &patternLang) == FcResultMatch &&
1315 FcPatternGet (nodeps[f]->pattern, FC_LANG, 0, &nodeLang) == FcResultMatch)
1318 double compare = FcCompareLang (&patternLang, &nodeLang, &matchValue);
1319 if (compare >= 0 && compare < 2)
1321 if (FcDebug () & FC_DBG_MATCHV)
1326 if (FcPatternGetString (nodeps[f]->pattern, FC_FAMILY, 0, &family) == FcResultMatch &&
1327 FcPatternGetString (nodeps[f]->pattern, FC_STYLE, 0, &style) == FcResultMatch)
1328 printf ("Font %s:%s matches language %d\n", family, style, i);
1330 patternLangSat[i] = FcTrue;
1339 nodeps[f]->score[PRI_LANG] = 10000.0;
1344 * Re-sort once the language issues have been settled
1346 qsort (nodeps, nnodes, sizeof (FcSortNode *),
1349 ret = FcFontSetCreate ();
1353 if (!FcSortWalk (nodeps, nnodes, ret, csp, trim))
1358 if (FcDebug() & FC_DBG_MATCH)
1360 printf ("First font ");
1361 FcPatternPrint (ret->fonts[0]);
1364 *result = FcResultMatch;
1369 FcFontSetDestroy (ret);
1377 FcFontSort (FcConfig *config,
1383 FcFontSet *sets[2], *ret;
1387 assert (result != NULL);
1389 *result = FcResultNoMatch;
1391 config = FcConfigReference (config);
1395 if (config->fonts[FcSetSystem])
1396 sets[nsets++] = config->fonts[FcSetSystem];
1397 if (config->fonts[FcSetApplication])
1398 sets[nsets++] = config->fonts[FcSetApplication];
1399 ret = FcFontSetSort (config, sets, nsets, p, trim, csp, result);
1400 FcConfigDestroy (config);
1405 #include "fcaliastail.h"