Fix stupid bug in FcFontSort()
[platform/upstream/fontconfig.git] / src / fcmatch.c
1 /*
2  * fontconfig/src/fcmatch.c
3  *
4  * Copyright © 2000 Keith Packard
5  *
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.
15  *
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.
23  */
24
25 #include "fcint.h"
26 #include <string.h>
27 #include <ctype.h>
28 #include <stdio.h>
29
30 static double
31 FcCompareNumber (FcValue *value1, FcValue *value2)
32 {
33     double  v1, v2, v;
34
35     switch (value1->type) {
36     case FcTypeInteger:
37         v1 = (double) value1->u.i;
38         break;
39     case FcTypeDouble:
40         v1 = value1->u.d;
41         break;
42     default:
43         return -1.0;
44     }
45     switch (value2->type) {
46     case FcTypeInteger:
47         v2 = (double) value2->u.i;
48         break;
49     case FcTypeDouble:
50         v2 = value2->u.d;
51         break;
52     default:
53         return -1.0;
54     }
55     v = v2 - v1;
56     if (v < 0)
57         v = -v;
58     return v;
59 }
60
61 static double
62 FcCompareString (FcValue *v1, FcValue *v2)
63 {
64     return (double) FcStrCmpIgnoreCase (FcValueString(v1), FcValueString(v2)) != 0;
65 }
66
67 static double
68 FcCompareFamily (FcValue *v1, FcValue *v2)
69 {
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);
74
75     if (FcToLower(*v1_string) != FcToLower(*v2_string) &&
76         *v1_string != ' ' && *v2_string != ' ')
77        return 1.0;
78
79     return (double) FcStrCmpIgnoreBlanksAndCase (v1_string, v2_string) != 0;
80 }
81
82 static double
83 FcCompareLang (FcValue *v1, FcValue *v2)
84 {
85     FcLangResult    result;
86     FcValue value1 = FcValueCanonicalize(v1), value2 = FcValueCanonicalize(v2);
87
88     switch (value1.type) {
89     case FcTypeLangSet:
90         switch (value2.type) {
91         case FcTypeLangSet:
92             result = FcLangSetCompare (value1.u.l, value2.u.l);
93             break;
94         case FcTypeString:
95             result = FcLangSetHasLang (value1.u.l,
96                                        value2.u.s);
97             break;
98         default:
99             return -1.0;
100         }
101         break;
102     case FcTypeString:
103         switch (value2.type) {
104         case FcTypeLangSet:
105             result = FcLangSetHasLang (value2.u.l, value1.u.s);
106             break;
107         case FcTypeString:
108             result = FcLangCompare (value1.u.s,
109                                     value2.u.s);
110             break;
111         default:
112             return -1.0;
113         }
114         break;
115     default:
116         return -1.0;
117     }
118     switch (result) {
119     case FcLangEqual:
120         return 0;
121     case FcLangDifferentCountry:
122         return 1;
123     case FcLangDifferentLang:
124     default:
125         return 2;
126     }
127 }
128
129 static double
130 FcCompareBool (FcValue *v1, FcValue *v2)
131 {
132     if (v2->type != FcTypeBool || v1->type != FcTypeBool)
133         return -1.0;
134     return (double) v2->u.b != v1->u.b;
135 }
136
137 static double
138 FcCompareCharSet (FcValue *v1, FcValue *v2)
139 {
140     return (double) FcCharSetSubtractCount (FcValueCharSet(v1), FcValueCharSet(v2));
141 }
142
143 static double
144 FcCompareSize (FcValue *value1, FcValue *value2)
145 {
146     double  v1, v2, v;
147
148     switch (value1->type) {
149     case FcTypeInteger:
150         v1 = value1->u.i;
151         break;
152     case FcTypeDouble:
153         v1 = value1->u.d;
154         break;
155     default:
156         return -1;
157     }
158     switch (value2->type) {
159     case FcTypeInteger:
160         v2 = value2->u.i;
161         break;
162     case FcTypeDouble:
163         v2 = value2->u.d;
164         break;
165     default:
166         return -1;
167     }
168     if (v2 == 0)
169         return 0;
170     v = v2 - v1;
171     if (v < 0)
172         v = -v;
173     return v;
174 }
175
176 typedef struct _FcMatcher {
177     FcObject        object;
178     double          (*compare) (FcValue *value1, FcValue *value2);
179     int             strong, weak;
180 } FcMatcher;
181
182 /*
183  * Order is significant, it defines the precedence of
184  * each value, earlier values are more significant than
185  * later values
186  */
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 },
195 #define MATCH_LANG          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
219 };
220
221 #define NUM_MATCH_VALUES    16
222
223 static const FcMatcher*
224 FcObjectToMatcher (FcObject object)
225 {
226     int         i;
227
228     i = -1;
229     switch (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;
240     case FC_LANG_OBJECT:
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;
260     }
261
262     if (i < 0)
263         return NULL;
264
265     return _FcMatchers+i;
266 }
267
268 static FcBool
269 FcCompareValueList (FcObject     object,
270                     FcValueListPtr v1orig,      /* pattern */
271                     FcValueListPtr v2orig,      /* target */
272                     FcValue     *bestValue,
273                     double      *value,
274                     FcResult    *result)
275 {
276     FcValueListPtr  v1, v2;
277     double          v, best, bestStrong, bestWeak;
278     int             j;
279     const FcMatcher *match = FcObjectToMatcher(object);
280
281     if (!match)
282     {
283         if (bestValue)
284             *bestValue = FcValueCanonicalize(&v2orig->value);
285         return FcTrue;
286     }
287
288     best = 1e99;
289     bestStrong = 1e99;
290     bestWeak = 1e99;
291     j = 1;
292     for (v1 = v1orig; v1; v1 = FcValueListNext(v1))
293     {
294         for (v2 = v2orig; v2; v2 = FcValueListNext(v2))
295         {
296             v = (match->compare) (&v1->value, &v2->value);
297             if (v < 0)
298             {
299                 *result = FcResultTypeMismatch;
300                 return FcFalse;
301             }
302             v = v * 1000 + j;
303             if (v < best)
304             {
305                 if (bestValue)
306                     *bestValue = FcValueCanonicalize(&v2->value);
307                 best = v;
308             }
309             if (v1->binding == FcValueBindingStrong)
310             {
311                 if (v < bestStrong)
312                     bestStrong = v;
313             }
314             else
315             {
316                 if (v < bestWeak)
317                     bestWeak = v;
318             }
319         }
320         j++;
321     }
322     if (FcDebug () & FC_DBG_MATCHV)
323     {
324         printf (" %s: %g ", FcObjectName (object), best);
325         FcValueListPrint (v1orig);
326         printf (", ");
327         FcValueListPrint (v2orig);
328         printf ("\n");
329     }
330     if (value)
331     {
332         int weak    = match->weak;
333         int strong  = match->strong;
334         if (weak == strong)
335             value[strong] += best;
336         else
337         {
338             value[weak] += bestWeak;
339             value[strong] += bestStrong;
340         }
341     }
342     return FcTrue;
343 }
344
345 /*
346  * Return a value indicating the distance between the two lists of
347  * values
348  */
349
350 static FcBool
351 FcCompare (FcPattern    *pat,
352            FcPattern    *fnt,
353            double       *value,
354            FcResult     *result)
355 {
356     int             i, i1, i2;
357
358     for (i = 0; i < NUM_MATCH_VALUES; i++)
359         value[i] = 0.0;
360
361     i1 = 0;
362     i2 = 0;
363     while (i1 < pat->num && i2 < fnt->num)
364     {
365         FcPatternElt *elt_i1 = &FcPatternElts(pat)[i1];
366         FcPatternElt *elt_i2 = &FcPatternElts(fnt)[i2];
367
368         i = FcObjectCompare(elt_i1->object, elt_i2->object);
369         if (i > 0)
370             i2++;
371         else if (i < 0)
372             i1++;
373         else
374         {
375             if (!FcCompareValueList (elt_i1->object,
376                                      FcPatternEltValues(elt_i1),
377                                      FcPatternEltValues(elt_i2),
378                                      0, value, result))
379                 return FcFalse;
380             i1++;
381             i2++;
382         }
383     }
384     return FcTrue;
385 }
386
387 FcPattern *
388 FcFontRenderPrepare (FcConfig       *config,
389                      FcPattern      *pat,
390                      FcPattern      *font)
391 {
392     FcPattern       *new;
393     int             i;
394     FcPatternElt    *fe, *pe;
395     FcValue         v;
396     FcResult        result;
397
398     new = FcPatternCreate ();
399     if (!new)
400         return 0;
401     for (i = 0; i < font->num; i++)
402     {
403         fe = &FcPatternElts(font)[i];
404         pe = FcPatternObjectFindElt (pat, fe->object);
405         if (pe)
406         {
407             if (!FcCompareValueList (pe->object, FcPatternEltValues(pe),
408                                      FcPatternEltValues(fe), &v, 0, &result))
409             {
410                 FcPatternDestroy (new);
411                 return 0;
412             }
413         }
414         else
415             v = FcValueCanonicalize(&FcPatternEltValues (fe)->value);
416         FcPatternObjectAdd (new, fe->object, v, FcFalse);
417     }
418     for (i = 0; i < pat->num; i++)
419     {
420         pe = &FcPatternElts(pat)[i];
421         fe = FcPatternObjectFindElt (font, pe->object);
422         if (!fe)
423         {
424             v = FcValueCanonicalize(&FcPatternEltValues(pe)->value);
425             FcPatternObjectAdd (new, pe->object, v, FcTrue);
426         }
427     }
428
429     FcConfigSubstituteWithPat (config, new, pat, FcMatchFont);
430     return new;
431 }
432
433 static FcPattern *
434 FcFontSetMatchInternal (FcConfig    *config,
435                         FcFontSet   **sets,
436                         int         nsets,
437                         FcPattern   *p,
438                         FcResult    *result)
439 {
440     double          score[NUM_MATCH_VALUES], bestscore[NUM_MATCH_VALUES];
441     int             f;
442     FcFontSet       *s;
443     FcPattern       *best;
444     int             i;
445     int             set;
446
447     for (i = 0; i < NUM_MATCH_VALUES; i++)
448         bestscore[i] = 0;
449     best = 0;
450     if (FcDebug () & FC_DBG_MATCH)
451     {
452         printf ("Match ");
453         FcPatternPrint (p);
454     }
455     for (set = 0; set < nsets; set++)
456     {
457         s = sets[set];
458         if (!s)
459             continue;
460         for (f = 0; f < s->nfont; f++)
461         {
462             if (FcDebug () & FC_DBG_MATCHV)
463             {
464                 printf ("Font %d ", f);
465                 FcPatternPrint (s->fonts[f]);
466             }
467             if (!FcCompare (p, s->fonts[f], score, result))
468                 return 0;
469             if (FcDebug () & FC_DBG_MATCHV)
470             {
471                 printf ("Score");
472                 for (i = 0; i < NUM_MATCH_VALUES; i++)
473                 {
474                     printf (" %g", score[i]);
475                 }
476                 printf ("\n");
477             }
478             for (i = 0; i < NUM_MATCH_VALUES; i++)
479             {
480                 if (best && bestscore[i] < score[i])
481                     break;
482                 if (!best || score[i] < bestscore[i])
483                 {
484                     for (i = 0; i < NUM_MATCH_VALUES; i++)
485                         bestscore[i] = score[i];
486                     best = s->fonts[f];
487                     break;
488                 }
489             }
490         }
491     }
492     if (FcDebug () & FC_DBG_MATCH)
493     {
494         printf ("Best score");
495         for (i = 0; i < NUM_MATCH_VALUES; i++)
496             printf (" %g", bestscore[i]);
497         printf ("\n");
498         FcPatternPrint (best);
499     }
500     if (!best)
501     {
502         *result = FcResultNoMatch;
503         return 0;
504     }
505     return best;
506 }
507
508 FcPattern *
509 FcFontSetMatch (FcConfig    *config,
510                 FcFontSet   **sets,
511                 int         nsets,
512                 FcPattern   *p,
513                 FcResult    *result)
514 {
515     FcPattern       *best;
516
517     if (!config)
518     {
519         config = FcConfigGetCurrent ();
520         if (!config)
521             return 0;
522     }
523     best = FcFontSetMatchInternal (config, sets, nsets, p, result);
524     if (best)
525         return FcFontRenderPrepare (config, p, best);
526     else
527         return NULL;
528 }
529
530 FcPattern *
531 FcFontMatch (FcConfig   *config,
532              FcPattern  *p,
533              FcResult   *result)
534 {
535     FcFontSet   *sets[2];
536     int         nsets;
537     FcPattern   *best;
538
539     if (!config)
540     {
541         config = FcConfigGetCurrent ();
542         if (!config)
543             return 0;
544     }
545     nsets = 0;
546     if (config->fonts[FcSetSystem])
547         sets[nsets++] = config->fonts[FcSetSystem];
548     if (config->fonts[FcSetApplication])
549         sets[nsets++] = config->fonts[FcSetApplication];
550
551     best = FcFontSetMatchInternal (config, sets, nsets, p, result);
552     if (best)
553         return FcFontRenderPrepare (config, p, best);
554     else
555         return NULL;
556 }
557
558 typedef struct _FcSortNode {
559     FcPattern   *pattern;
560     double      score[NUM_MATCH_VALUES];
561 } FcSortNode;
562
563 static int
564 FcSortCompare (const void *aa, const void *ab)
565 {
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;
571     int         i;
572
573     i = NUM_MATCH_VALUES;
574     while (i-- && (ad = *as++) == (bd = *bs++))
575         ;
576     return ad < bd ? -1 : ad > bd ? 1 : 0;
577 }
578
579 static FcBool
580 FcSortWalk (FcSortNode **n, int nnode, FcFontSet *fs, FcCharSet **csp, FcBool trim)
581 {
582     FcBool ret = FcFalse;
583     FcCharSet *cs;
584
585     cs = 0;
586     if (trim || csp)
587     {
588         cs = FcCharSetCreate ();
589         if (cs == NULL)
590             goto bail;
591     }
592
593     while (nnode--)
594     {
595         FcSortNode      *node = *n++;
596         FcBool          adds_chars = FcFalse;
597
598         /*
599          * Only fetch node charset if we'd need it
600          */
601         if (cs)
602         {
603             FcCharSet   *ncs;
604
605             if (FcPatternGetCharSet (node->pattern, FC_CHARSET, 0, &ncs) !=
606                 FcResultMatch)
607                 continue;
608
609             if (!FcCharSetMerge (cs, ncs, &adds_chars))
610                 goto bail;
611         }
612
613         /*
614          * If this font isn't a subset of the previous fonts,
615          * add it to the list
616          */
617         if (!trim || adds_chars)
618         {
619             FcPatternReference (node->pattern);
620             if (FcDebug () & FC_DBG_MATCHV)
621             {
622                 printf ("Add ");
623                 FcPatternPrint (node->pattern);
624             }
625             if (!FcFontSetAdd (fs, node->pattern))
626             {
627                 FcPatternDestroy (node->pattern);
628                 goto bail;
629             }
630         }
631     }
632     if (csp)
633     {
634         *csp = cs;
635         cs = 0;
636     }
637
638     ret = FcTrue;
639
640 bail:
641     if (cs)
642         FcCharSetDestroy (cs);
643
644     return ret;
645 }
646
647 void
648 FcFontSetSortDestroy (FcFontSet *fs)
649 {
650     FcFontSetDestroy (fs);
651 }
652
653 FcFontSet *
654 FcFontSetSort (FcConfig     *config,
655                FcFontSet    **sets,
656                int          nsets,
657                FcPattern    *p,
658                FcBool       trim,
659                FcCharSet    **csp,
660                FcResult     *result)
661 {
662     FcFontSet       *ret;
663     FcFontSet       *s;
664     FcSortNode      *nodes;
665     FcSortNode      **nodeps, **nodep;
666     int             nnodes;
667     FcSortNode      *new;
668     int             set;
669     int             f;
670     int             i;
671     int             nPatternLang;
672     FcBool          *patternLangSat;
673     FcValue         patternLang;
674
675     if (FcDebug () & FC_DBG_MATCH)
676     {
677         printf ("Sort ");
678         FcPatternPrint (p);
679     }
680     nnodes = 0;
681     for (set = 0; set < nsets; set++)
682     {
683         s = sets[set];
684         if (!s)
685             continue;
686         nnodes += s->nfont;
687     }
688     if (!nnodes)
689         goto bail0;
690
691     for (nPatternLang = 0;
692          FcPatternGet (p, FC_LANG, nPatternLang, &patternLang) == FcResultMatch;
693          nPatternLang++)
694         ;
695         
696     /* freed below */
697     nodes = malloc (nnodes * sizeof (FcSortNode) +
698                     nnodes * sizeof (FcSortNode *) +
699                     nPatternLang * sizeof (FcBool));
700     if (!nodes)
701         goto bail0;
702     nodeps = (FcSortNode **) (nodes + nnodes);
703     patternLangSat = (FcBool *) (nodeps + nnodes);
704
705     new = nodes;
706     nodep = nodeps;
707     for (set = 0; set < nsets; set++)
708     {
709         s = sets[set];
710         if (!s)
711             continue;
712         for (f = 0; f < s->nfont; f++)
713         {
714             if (FcDebug () & FC_DBG_MATCHV)
715             {
716                 printf ("Font %d ", f);
717                 FcPatternPrint (s->fonts[f]);
718             }
719             new->pattern = s->fonts[f];
720             if (!FcCompare (p, new->pattern, new->score, result))
721                 goto bail1;
722             if (FcDebug () & FC_DBG_MATCHV)
723             {
724                 printf ("Score");
725                 for (i = 0; i < NUM_MATCH_VALUES; i++)
726                 {
727                     printf (" %g", new->score[i]);
728                 }
729                 printf ("\n");
730             }
731             *nodep = new;
732             new++;
733             nodep++;
734         }
735     }
736
737     nnodes = new - nodes;
738
739     qsort (nodeps, nnodes, sizeof (FcSortNode *),
740            FcSortCompare);
741
742     for (i = 0; i < nPatternLang; i++)
743         patternLangSat[i] = FcFalse;
744
745     for (f = 0; f < nnodes; f++)
746     {
747         FcBool  satisfies = FcFalse;
748         /*
749          * If this node matches any language, go check
750          * which ones and satisfy those entries
751          */
752         if (nodeps[f]->score[MATCH_LANG_INDEX] < 2000)
753         {
754             for (i = 0; i < nPatternLang; i++)
755             {
756                 FcValue     nodeLang;
757                 
758                 if (!patternLangSat[i] &&
759                     FcPatternGet (p, FC_LANG, i, &patternLang) == FcResultMatch &&
760                     FcPatternGet (nodeps[f]->pattern, FC_LANG, 0, &nodeLang) == FcResultMatch)
761                 {
762                     double  compare = FcCompareLang (&patternLang, &nodeLang);
763                     if (compare >= 0 && compare < 2)
764                     {
765                         if (FcDebug () & FC_DBG_MATCHV)
766                         {
767                             FcChar8 *family;
768                             FcChar8 *style;
769
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);
773                         }
774                         patternLangSat[i] = FcTrue;
775                         satisfies = FcTrue;
776                         break;
777                     }
778                 }
779             }
780         }
781         if (!satisfies)
782             nodeps[f]->score[MATCH_LANG_INDEX] = 10000.0;
783     }
784
785     /*
786      * Re-sort once the language issues have been settled
787      */
788     qsort (nodeps, nnodes, sizeof (FcSortNode *),
789            FcSortCompare);
790
791     ret = FcFontSetCreate ();
792     if (!ret)
793         goto bail1;
794
795     if (!FcSortWalk (nodeps, nnodes, ret, csp, trim))
796         goto bail2;
797
798     free (nodes);
799
800     if (FcDebug() & FC_DBG_MATCH)
801     {
802         printf ("First font ");
803         FcPatternPrint (ret->fonts[0]);
804     }
805     return ret;
806
807 bail2:
808     FcFontSetDestroy (ret);
809 bail1:
810     free (nodes);
811 bail0:
812     return 0;
813 }
814
815 FcFontSet *
816 FcFontSort (FcConfig    *config,
817             FcPattern   *p,
818             FcBool      trim,
819             FcCharSet   **csp,
820             FcResult    *result)
821 {
822     FcFontSet   *sets[2];
823     int         nsets;
824
825     if (!config)
826     {
827         config = FcConfigGetCurrent ();
828         if (!config)
829             return 0;
830     }
831     nsets = 0;
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);
837 }
838 #define __fcmatch__
839 #include "fcaliastail.h"
840 #undef __fcmatch__