Imported Upstream version 2.11.93
[platform/upstream/fontconfig.git] / src / fcmatch.c
index 623538b..46d08bc 100644 (file)
  */
 
 #include "fcint.h"
-#include <assert.h>
-#include <string.h>
-#include <ctype.h>
-#include <stdio.h>
 
 static double
 FcCompareNumber (FcValue *value1, FcValue *value2)
 {
     double  v1, v2, v;
 
-    switch (value1->type) {
+    switch ((int) value1->type) {
     case FcTypeInteger:
        v1 = (double) value1->u.i;
        break;
@@ -43,7 +39,7 @@ FcCompareNumber (FcValue *value1, FcValue *value2)
     default:
        return -1.0;
     }
-    switch (value2->type) {
+    switch ((int) value2->type) {
     case FcTypeInteger:
        v2 = (double) value2->u.i;
        break;
@@ -81,14 +77,32 @@ FcCompareFamily (FcValue *v1, FcValue *v2)
 }
 
 static double
+FcComparePostScript (FcValue *v1, FcValue *v2)
+{
+    const FcChar8 *v1_string = FcValueString (v1);
+    const FcChar8 *v2_string = FcValueString (v2);
+    int n;
+    size_t len;
+
+    if (FcToLower (*v1_string) != FcToLower (*v2_string) &&
+       *v1_string != ' ' && *v2_string != ' ')
+       return 1.0;
+
+    n = FcStrMatchIgnoreCaseAndDelims (v1_string, v2_string, (const FcChar8 *)" -");
+    len = strlen ((const char *)v1_string);
+
+    return (double)(len - n) / (double)len;
+}
+
+static double
 FcCompareLang (FcValue *v1, FcValue *v2)
 {
     FcLangResult    result;
     FcValue value1 = FcValueCanonicalize(v1), value2 = FcValueCanonicalize(v2);
 
-    switch (value1.type) {
+    switch ((int) value1.type) {
     case FcTypeLangSet:
-       switch (value2.type) {
+       switch ((int) value2.type) {
        case FcTypeLangSet:
            result = FcLangSetCompare (value1.u.l, value2.u.l);
            break;
@@ -101,7 +115,7 @@ FcCompareLang (FcValue *v1, FcValue *v2)
        }
        break;
     case FcTypeString:
-       switch (value2.type) {
+       switch ((int) value2.type) {
        case FcTypeLangSet:
            result = FcLangSetHasLang (value2.u.l, value1.u.s);
            break;
@@ -146,7 +160,7 @@ FcCompareSize (FcValue *value1, FcValue *value2)
 {
     double  v1, v2, v;
 
-    switch (value1->type) {
+    switch ((int) value1->type) {
     case FcTypeInteger:
        v1 = value1->u.i;
        break;
@@ -156,7 +170,7 @@ FcCompareSize (FcValue *value1, FcValue *value2)
     default:
        return -1;
     }
-    switch (value2->type) {
+    switch ((int) value2->type) {
     case FcTypeInteger:
        v2 = value2->u.i;
        break;
@@ -175,25 +189,130 @@ FcCompareSize (FcValue *value1, FcValue *value2)
 }
 
 static double
+FcCompareSizeRange (FcValue *v1, FcValue *v2)
+{
+    FcValue value1 = FcValueCanonicalize (v1);
+    FcValue value2 = FcValueCanonicalize (v2);
+    FcRange *r1 = NULL, *r2 = NULL;
+    double ret = -1.0;
+
+    switch ((int) value1.type) {
+    case FcTypeDouble:
+       r1 = FcRangeCreateDouble (value1.u.d, value1.u.d);
+       break;
+    case FcTypeRange:
+       r1 = FcRangeCopy (value1.u.r);
+       break;
+    default:
+       goto bail;
+    }
+    switch ((int) value2.type) {
+    case FcTypeDouble:
+       r2 = FcRangeCreateDouble (value2.u.d, value2.u.d);
+       break;
+    case FcTypeRange:
+       r2 = FcRangeCopy (value2.u.r);
+       break;
+    default:
+       goto bail;
+    }
+
+    if (FcRangeIsInRange (r1, r2))
+       ret = 0.0;
+    else
+       ret = FC_MIN (fabs (r1->u.d.end - r2->u.d.begin), fabs (r1->u.d.begin - r2->u.d.end));
+
+bail:
+    if (r1)
+       FcRangeDestroy (r1);
+    if (r2)
+       FcRangeDestroy (r2);
+
+    return ret;
+}
+
+static double
 FcCompareFilename (FcValue *v1, FcValue *v2)
 {
-       const FcChar8 *s1 = FcValueString (v1), *s2 = FcValueString (v2);
-       if (FcStrCmp (s1, s2) == 0)
-           return 0.0;
-       else if (FcStrCmpIgnoreCase (s1, s2) == 0)
-           return 1.0;
-       else if (FcStrRegexCmp (s2, s1))
-           return 2.0;
-       else if (FcStrRegexCmpIgnoreCase (s2, s1))
-           return 3.0;
-       else
-           return 4.0;
+    const FcChar8 *s1 = FcValueString (v1), *s2 = FcValueString (v2);
+    if (FcStrCmp (s1, s2) == 0)
+       return 0.0;
+    else if (FcStrCmpIgnoreCase (s1, s2) == 0)
+       return 1.0;
+    else if (FcStrGlobMatch (s1, s2))
+       return 2.0;
+    else
+       return 3.0;
 }
 
+
+/* Define priorities to -1 for objects that don't have a compare function. */
+
+#define PRI_NULL(n)                            \
+    PRI_ ## n ## _STRONG = -1,                 \
+    PRI_ ## n ## _WEAK = -1,
+#define PRI1(n)
+#define PRI_FcCompareFamily(n)         PRI1(n)
+#define PRI_FcCompareString(n)         PRI1(n)
+#define PRI_FcCompareNumber(n)         PRI1(n)
+#define PRI_FcCompareSize(n)           PRI1(n)
+#define PRI_FcCompareBool(n)           PRI1(n)
+#define PRI_FcCompareFilename(n)       PRI1(n)
+#define PRI_FcCompareCharSet(n)                PRI1(n)
+#define PRI_FcCompareLang(n)           PRI1(n)
+#define PRI_FcComparePostScript(n)     PRI1(n)
+#define PRI_FcCompareSizeRange(n)      PRI1(n)
+
+#define FC_OBJECT(NAME, Type, Cmp)     PRI_##Cmp(NAME)
+
+typedef enum _FcMatcherPriorityDummy {
+#include "fcobjs.h"
+} FcMatcherPriorityDummy;
+
+#undef FC_OBJECT
+
+
+/* Canonical match priority order. */
+
+#undef PRI1
+#define PRI1(n)                                        \
+    PRI_ ## n,                                 \
+    PRI_ ## n ## _STRONG = PRI_ ## n,          \
+    PRI_ ## n ## _WEAK = PRI_ ## n
+
+typedef enum _FcMatcherPriority {
+    PRI1(FILE),
+    PRI1(FONTFORMAT),
+    PRI1(SCALABLE),
+    PRI1(COLOR),
+    PRI1(FOUNDRY),
+    PRI1(CHARSET),
+    PRI_FAMILY_STRONG,
+    PRI_POSTSCRIPT_NAME_STRONG,
+    PRI1(LANG),
+    PRI_FAMILY_WEAK,
+    PRI_POSTSCRIPT_NAME_WEAK,
+    PRI1(SPACING),
+    PRI1(SIZE),
+    PRI1(PIXEL_SIZE),
+    PRI1(STYLE),
+    PRI1(SLANT),
+    PRI1(WEIGHT),
+    PRI1(WIDTH),
+    PRI1(DECORATIVE),
+    PRI1(ANTIALIAS),
+    PRI1(RASTERIZER),
+    PRI1(OUTLINE),
+    PRI1(FONTVERSION),
+    PRI_END
+} FcMatcherPriority;
+
+#undef PRI1
+
 typedef struct _FcMatcher {
-    FcObject       object;
-    double         (*compare) (FcValue *value1, FcValue *value2);
-    int                    strong, weak;
+    FcObject object;
+    double   (*compare) (FcValue *value1, FcValue *value2);
+    int      strong, weak;
 } FcMatcher;
 
 /*
@@ -201,100 +320,34 @@ typedef struct _FcMatcher {
  * each value, earlier values are more significant than
  * later values
  */
+#define FC_OBJECT(NAME, Type, Cmp)     { FC_##NAME##_OBJECT,   Cmp,    PRI_##NAME##_STRONG,    PRI_##NAME##_WEAK },
 static const FcMatcher _FcMatchers [] = {
-    { FC_FILE_OBJECT,          FcCompareFilename,      0, 0 },
-#define MATCH_FILE         0
-    { FC_FOUNDRY_OBJECT,       FcCompareString,        1, 1 },
-#define MATCH_FOUNDRY      1
-    { FC_CHARSET_OBJECT,       FcCompareCharSet,       2, 2 },
-#define MATCH_CHARSET      2
-    { FC_FAMILY_OBJECT,        FcCompareFamily,        3, 5 },
-#define MATCH_FAMILY       3
-    { FC_LANG_OBJECT,          FcCompareLang,          4, 4 },
-#define MATCH_LANG         4
-#define MATCH_LANG_INDEX    4
-    { FC_SPACING_OBJECT,       FcCompareNumber,        6, 6 },
-#define MATCH_SPACING      5
-    { FC_PIXEL_SIZE_OBJECT,    FcCompareSize,          7, 7 },
-#define MATCH_PIXEL_SIZE    6
-    { FC_STYLE_OBJECT,         FcCompareString,        8, 8 },
-#define MATCH_STYLE        7
-    { FC_SLANT_OBJECT,         FcCompareNumber,        9, 9 },
-#define MATCH_SLANT        8
-    { FC_WEIGHT_OBJECT,                FcCompareNumber,        10, 10 },
-#define MATCH_WEIGHT       9
-    { FC_WIDTH_OBJECT,         FcCompareNumber,        11, 11 },
-#define MATCH_WIDTH        10
-    { FC_DECORATIVE_OBJECT,    FcCompareBool,          12, 12 },
-#define MATCH_DECORATIVE       11
-    { FC_ANTIALIAS_OBJECT,     FcCompareBool,          13, 13 },
-#define MATCH_ANTIALIAS                    12
-    { FC_RASTERIZER_OBJECT,    FcCompareString,        14, 14 },
-#define MATCH_RASTERIZER           13
-    { FC_OUTLINE_OBJECT,       FcCompareBool,          15, 15 },
-#define MATCH_OUTLINE              14
-    { FC_FONTVERSION_OBJECT,   FcCompareNumber,        16, 16 },
-#define MATCH_FONTVERSION          15
+    { FC_INVALID_OBJECT, NULL, -1, -1 },
+#include "fcobjs.h"
 };
-
-#define NUM_MATCH_VALUES    17
+#undef FC_OBJECT
 
 static const FcMatcher*
 FcObjectToMatcher (FcObject object,
                   FcBool   include_lang)
 {
-    int        i;
-
-    i = -1;
-    switch (object) {
-    case FC_FILE_OBJECT:
-       i = MATCH_FILE; break;
-    case FC_FOUNDRY_OBJECT:
-       i = MATCH_FOUNDRY; break;
-    case FC_FONTVERSION_OBJECT:
-       i = MATCH_FONTVERSION; break;
-    case FC_FAMILY_OBJECT:
-       i = MATCH_FAMILY; break;
-    case FC_CHARSET_OBJECT:
-       i = MATCH_CHARSET; break;
-    case FC_ANTIALIAS_OBJECT:
-       i = MATCH_ANTIALIAS; break;
-    case FC_LANG_OBJECT:
-       i = MATCH_LANG; break;
-    case FC_SPACING_OBJECT:
-        i = MATCH_SPACING; break;
-    case FC_STYLE_OBJECT:
-        i = MATCH_STYLE; break;
-    case FC_SLANT_OBJECT:
-        i = MATCH_SLANT; break;
-    case FC_PIXEL_SIZE_OBJECT:
-       i = MATCH_PIXEL_SIZE; break;
-    case FC_WIDTH_OBJECT:
-        i = MATCH_WIDTH; break;
-    case FC_WEIGHT_OBJECT:
-        i = MATCH_WEIGHT; break;
-    case FC_RASTERIZER_OBJECT:
-       i = MATCH_RASTERIZER; break;
-    case FC_OUTLINE_OBJECT:
-       i = MATCH_OUTLINE; break;
-    case FC_DECORATIVE_OBJECT:
-       i = MATCH_DECORATIVE; break;
-    default:
-       if (include_lang)
-       {
-           switch (object) {
-           case FC_FAMILYLANG_OBJECT:
-           case FC_STYLELANG_OBJECT:
-           case FC_FULLNAMELANG_OBJECT:
-               i = MATCH_LANG; break;
-           }
+    if (include_lang)
+    {
+       switch (object) {
+       case FC_FAMILYLANG_OBJECT:
+       case FC_STYLELANG_OBJECT:
+       case FC_FULLNAMELANG_OBJECT:
+           object = FC_LANG_OBJECT;
+           break;
        }
     }
-
-    if (i < 0)
+    if (object > FC_MAX_BASE_OBJECT ||
+       !_FcMatchers[object].compare ||
+       _FcMatchers[object].strong == -1 ||
+       _FcMatchers[object].weak == -1)
        return NULL;
 
-    return _FcMatchers+i;
+    return _FcMatchers + object;
 }
 
 static FcBool
@@ -323,7 +376,7 @@ FcCompareValueList (FcObject             object,
     best = 1e99;
     bestStrong = 1e99;
     bestWeak = 1e99;
-    j = 1;
+    j = 0;
     for (v1 = v1orig; v1; v1 = FcValueListNext(v1))
     {
        for (v2 = v2orig, k = 0; v2; v2 = FcValueListNext(v2), k++)
@@ -394,7 +447,7 @@ FcCompare (FcPattern        *pat,
 {
     int                    i, i1, i2;
 
-    for (i = 0; i < NUM_MATCH_VALUES; i++)
+    for (i = 0; i < PRI_END; i++)
        value[i] = 0.0;
 
     i1 = 0;
@@ -431,8 +484,8 @@ FcFontRenderPrepare (FcConfig           *config,
 {
     FcPattern      *new;
     int                    i;
-    FcPatternElt    *fe, *pe, *fel, *pel;
-    FcValue        v, vl;
+    FcPatternElt    *fe, *pe;
+    FcValue        v;
     FcResult       result;
 
     assert (pat != NULL);
@@ -440,7 +493,7 @@ FcFontRenderPrepare (FcConfig           *config,
 
     new = FcPatternCreate ();
     if (!new)
-       return 0;
+       return NULL;
     for (i = 0; i < font->num; i++)
     {
        fe = &FcPatternElts(font)[i];
@@ -456,78 +509,112 @@ FcFontRenderPrepare (FcConfig        *config,
            fe->object == FC_STYLE_OBJECT ||
            fe->object == FC_FULLNAME_OBJECT)
        {
+           FcPatternElt    *fel, *pel;
+
            FC_ASSERT_STATIC ((FC_FAMILY_OBJECT + 1) == FC_FAMILYLANG_OBJECT);
            FC_ASSERT_STATIC ((FC_STYLE_OBJECT + 1) == FC_STYLELANG_OBJECT);
            FC_ASSERT_STATIC ((FC_FULLNAME_OBJECT + 1) == FC_FULLNAMELANG_OBJECT);
 
            fel = FcPatternObjectFindElt (font, fe->object + 1);
            pel = FcPatternObjectFindElt (pat, fe->object + 1);
-       }
-       else
-       {
-           fel = NULL;
-           pel = NULL;
-       }
-       pe = FcPatternObjectFindElt (pat, fe->object);
-       if (pe)
-       {
-           const FcMatcher *match = FcObjectToMatcher (pe->object, FcFalse);
 
-           if (!FcCompareValueList (pe->object, match,
-                                    FcPatternEltValues(pe),
-                                    FcPatternEltValues(fe), &v, NULL, NULL, &result))
-           {
-               FcPatternDestroy (new);
-               return 0;
-           }
            if (fel && pel)
            {
+               /* The font has name languages, and pattern asks for specific language(s).
+                * Match on language and and prefer that result.
+                * Note:  Currently the code only give priority to first matching language.
+                */
                int n = 1, j;
+               FcValueListPtr l1, l2, ln = NULL, ll = NULL;
+               const FcMatcher *match = FcObjectToMatcher (pel->object, FcTrue);
 
-               match = FcObjectToMatcher (pel->object, FcTrue);
                if (!FcCompareValueList (pel->object, match,
                                         FcPatternEltValues (pel),
-                                        FcPatternEltValues (fel), &vl, NULL, &n, &result))
+                                        FcPatternEltValues (fel), NULL, NULL, &n, &result))
                {
                    FcPatternDestroy (new);
                    return NULL;
                }
-               else
-               {
-                   FcValueListPtr l;
 
-                   for (j = 0, l = FcPatternEltValues (fe);
-                        j < n && l != NULL;
-                        j++, l = FcValueListNext (l));
-                   if (l)
-                       v = FcValueCanonicalize (&l->value);
+               for (j = 0, l1 = FcPatternEltValues (fe), l2 = FcPatternEltValues (fel);
+                    l1 != NULL || l2 != NULL;
+                    j++, l1 = l1 ? FcValueListNext (l1) : NULL, l2 = l2 ? FcValueListNext (l2) : NULL)
+               {
+                   if (j == n)
+                   {
+                       if (l1)
+                           ln = FcValueListPrepend (ln,
+                                                    FcValueCanonicalize (&l1->value),
+                                                    FcValueBindingStrong);
+                       if (l2)
+                           ll = FcValueListPrepend (ll,
+                                                    FcValueCanonicalize (&l2->value),
+                                                    FcValueBindingStrong);
+                   }
                    else
-                       v = FcValueCanonicalize (&FcPatternEltValues (fe)->value);
+                   {
+                       if (l1)
+                           ln = FcValueListAppend (ln,
+                                                   FcValueCanonicalize (&l1->value),
+                                                   FcValueBindingStrong);
+                       if (l2)
+                           ll = FcValueListAppend (ll,
+                                                   FcValueCanonicalize (&l2->value),
+                                                   FcValueBindingStrong);
+                   }
                }
+               FcPatternObjectListAdd (new, fe->object, ln, FcFalse);
+               FcPatternObjectListAdd (new, fel->object, ll, FcFalse);
+
+               continue;
            }
            else if (fel)
            {
-               vl = FcValueCanonicalize (&FcPatternEltValues (fel)->value);
+               /* Pattern doesn't ask for specific language.  Copy all for name and
+                * lang. */
+               FcValueListPtr l1, l2;
+
+               l1 = FcValueListDuplicate (FcPatternEltValues (fe));
+               l2 = FcValueListDuplicate (FcPatternEltValues (fel));
+               FcPatternObjectListAdd (new, fe->object, l1, FcFalse);
+               FcPatternObjectListAdd (new, fel->object, l2, FcFalse);
+
+               continue;
+           }
+       }
+
+       pe = FcPatternObjectFindElt (pat, fe->object);
+       if (pe)
+       {
+           const FcMatcher *match = FcObjectToMatcher (pe->object, FcFalse);
+           if (!FcCompareValueList (pe->object, match,
+                                    FcPatternEltValues(pe),
+                                    FcPatternEltValues(fe), &v, NULL, NULL, &result))
+           {
+               FcPatternDestroy (new);
+               return NULL;
            }
+           FcPatternObjectAdd (new, fe->object, v, FcFalse);
        }
        else
        {
-           v = FcValueCanonicalize(&FcPatternEltValues (fe)->value);
-           if (fel)
-               vl = FcValueCanonicalize (&FcPatternEltValues (fel)->value);
+           FcPatternObjectListAdd (new, fe->object,
+                                   FcValueListDuplicate (FcPatternEltValues (fe)),
+                                   FcTrue);
        }
-       FcPatternObjectAdd (new, fe->object, v, FcFalse);
-       if (fel)
-           FcPatternObjectAdd (new, fel->object, vl, FcFalse);
     }
     for (i = 0; i < pat->num; i++)
     {
        pe = &FcPatternElts(pat)[i];
        fe = FcPatternObjectFindElt (font, pe->object);
-       if (!fe)
+       if (!fe &&
+           pe->object != FC_FAMILYLANG_OBJECT &&
+           pe->object != FC_STYLELANG_OBJECT &&
+           pe->object != FC_FULLNAMELANG_OBJECT)
        {
-           v = FcValueCanonicalize(&FcPatternEltValues(pe)->value);
-           FcPatternObjectAdd (new, pe->object, v, FcTrue);
+           FcPatternObjectListAdd (new, pe->object,
+                                   FcValueListDuplicate (FcPatternEltValues(pe)),
+                                   FcFalse);
        }
     }
 
@@ -536,20 +623,19 @@ FcFontRenderPrepare (FcConfig         *config,
 }
 
 static FcPattern *
-FcFontSetMatchInternal (FcConfig    *config,
-                       FcFontSet   **sets,
+FcFontSetMatchInternal (FcFontSet   **sets,
                        int         nsets,
                        FcPattern   *p,
                        FcResult    *result)
 {
-    double         score[NUM_MATCH_VALUES], bestscore[NUM_MATCH_VALUES];
+    double         score[PRI_END], bestscore[PRI_END];
     int                    f;
     FcFontSet      *s;
     FcPattern      *best;
     int                    i;
     int                    set;
 
-    for (i = 0; i < NUM_MATCH_VALUES; i++)
+    for (i = 0; i < PRI_END; i++)
        bestscore[i] = 0;
     best = 0;
     if (FcDebug () & FC_DBG_MATCH)
@@ -574,19 +660,19 @@ FcFontSetMatchInternal (FcConfig    *config,
            if (FcDebug () & FC_DBG_MATCHV)
            {
                printf ("Score");
-               for (i = 0; i < NUM_MATCH_VALUES; i++)
+               for (i = 0; i < PRI_END; i++)
                {
                    printf (" %g", score[i]);
                }
                printf ("\n");
            }
-           for (i = 0; i < NUM_MATCH_VALUES; i++)
+           for (i = 0; i < PRI_END; i++)
            {
                if (best && bestscore[i] < score[i])
                    break;
                if (!best || score[i] < bestscore[i])
                {
-                   for (i = 0; i < NUM_MATCH_VALUES; i++)
+                   for (i = 0; i < PRI_END; i++)
                        bestscore[i] = score[i];
                    best = s->fonts[f];
                    break;
@@ -597,7 +683,7 @@ FcFontSetMatchInternal (FcConfig    *config,
     if (FcDebug () & FC_DBG_MATCH)
     {
        printf ("Best score");
-       for (i = 0; i < NUM_MATCH_VALUES; i++)
+       for (i = 0; i < PRI_END; i++)
            printf (" %g", bestscore[i]);
        printf ("\n");
        FcPatternPrint (best);
@@ -631,7 +717,7 @@ FcFontSetMatch (FcConfig    *config,
        if (!config)
            return 0;
     }
-    best = FcFontSetMatchInternal (config, sets, nsets, p, result);
+    best = FcFontSetMatchInternal (sets, nsets, p, result);
     if (best)
        return FcFontRenderPrepare (config, p, best);
     else
@@ -664,7 +750,7 @@ FcFontMatch (FcConfig       *config,
     if (config->fonts[FcSetApplication])
        sets[nsets++] = config->fonts[FcSetApplication];
 
-    best = FcFontSetMatchInternal (config, sets, nsets, p, result);
+    best = FcFontSetMatchInternal (sets, nsets, p, result);
     if (best)
        return FcFontRenderPrepare (config, p, best);
     else
@@ -673,7 +759,7 @@ FcFontMatch (FcConfig       *config,
 
 typedef struct _FcSortNode {
     FcPattern  *pattern;
-    double     score[NUM_MATCH_VALUES];
+    double     score[PRI_END];
 } FcSortNode;
 
 static int
@@ -686,7 +772,7 @@ FcSortCompare (const void *aa, const void *ab)
     double     ad = 0, bd = 0;
     int         i;
 
-    i = NUM_MATCH_VALUES;
+    i = PRI_END;
     while (i-- && (ad = *as++) == (bd = *bs++))
        ;
     return ad < bd ? -1 : ad > bd ? 1 : 0;
@@ -697,6 +783,7 @@ FcSortWalk (FcSortNode **n, int nnode, FcFontSet *fs, FcCharSet **csp, FcBool tr
 {
     FcBool ret = FcFalse;
     FcCharSet *cs;
+    int i;
 
     cs = 0;
     if (trim || csp)
@@ -706,7 +793,7 @@ FcSortWalk (FcSortNode **n, int nnode, FcFontSet *fs, FcCharSet **csp, FcBool tr
            goto bail;
     }
 
-    while (nnode--)
+    for (i = 0; i < nnode; i++)
     {
        FcSortNode      *node = *n++;
        FcBool          adds_chars = FcFalse;
@@ -730,7 +817,7 @@ FcSortWalk (FcSortNode **n, int nnode, FcFontSet *fs, FcCharSet **csp, FcBool tr
         * If this font isn't a subset of the previous fonts,
         * add it to the list
         */
-       if (!trim || adds_chars)
+       if (!i || !trim || adds_chars)
        {
            FcPatternReference (node->pattern);
            if (FcDebug () & FC_DBG_MATCHV)
@@ -767,7 +854,7 @@ FcFontSetSortDestroy (FcFontSet *fs)
 }
 
 FcFontSet *
-FcFontSetSort (FcConfig            *config,
+FcFontSetSort (FcConfig            *config FC_UNUSED,
               FcFontSet    **sets,
               int          nsets,
               FcPattern    *p,
@@ -851,7 +938,7 @@ FcFontSetSort (FcConfig         *config,
            if (FcDebug () & FC_DBG_MATCHV)
            {
                printf ("Score");
-               for (i = 0; i < NUM_MATCH_VALUES; i++)
+               for (i = 0; i < PRI_END; i++)
                {
                    printf (" %g", new->score[i]);
                }
@@ -878,7 +965,7 @@ FcFontSetSort (FcConfig         *config,
         * If this node matches any language, go check
         * which ones and satisfy those entries
         */
-       if (nodeps[f]->score[MATCH_LANG_INDEX] < 2000)
+       if (nodeps[f]->score[PRI_LANG] < 2000)
        {
            for (i = 0; i < nPatternLang; i++)
            {
@@ -908,7 +995,9 @@ FcFontSetSort (FcConfig         *config,
            }
        }
        if (!satisfies)
-           nodeps[f]->score[MATCH_LANG_INDEX] = 10000.0;
+       {
+           nodeps[f]->score[PRI_LANG] = 10000.0;
+       }
     }
 
     /*