2 * $XFree86: xc/lib/fontconfig/src/fcmatch.c,v 1.18 2002/08/19 19:32:05 keithp Exp $
4 * Copyright © 2000 Keith Packard, member of The XFree86 Project, Inc.
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 Keith Packard not be used in
11 * advertising or publicity pertaining to distribution of the software without
12 * specific, written prior permission. Keith Packard makes no
13 * representations about the suitability of this software for any purpose. It
14 * is provided "as is" without express or implied warranty.
16 * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18 * EVENT SHALL KEITH PACKARD 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 FcCompareInteger (char *object, FcValue value1, FcValue value2)
35 if (value2.type != FcTypeInteger || value1.type != FcTypeInteger)
37 v = value2.u.i - value1.u.i;
44 FcCompareString (char *object, FcValue value1, FcValue value2)
46 if (value2.type != FcTypeString || value1.type != FcTypeString)
48 return (double) FcStrCmpIgnoreCase (value1.u.s, value2.u.s) != 0;
52 FcCompareFamily (char *object, FcValue value1, FcValue value2)
54 if (value2.type != FcTypeString || value1.type != FcTypeString)
56 return (double) FcStrCmpIgnoreBlanksAndCase (value1.u.s, value2.u.s) != 0;
60 FcCompareLang (char *object, FcValue value1, FcValue value2)
64 switch (value1.type) {
66 switch (value2.type) {
68 result = FcLangSetCompare (value1.u.l, value2.u.l);
71 result = FcLangSetHasLang (value1.u.l, value2.u.s);
78 switch (value2.type) {
80 result = FcLangSetHasLang (value2.u.l, value1.u.s);
83 result = FcLangCompare (value1.u.s, value2.u.s);
95 case FcLangDifferentCountry:
97 case FcLangDifferentLang:
104 FcCompareBool (char *object, FcValue value1, FcValue value2)
106 if (value2.type != FcTypeBool || value1.type != FcTypeBool)
108 return (double) value2.u.b != value1.u.b;
112 FcCompareCharSet (char *object, FcValue value1, FcValue value2)
114 if (value2.type != FcTypeCharSet || value1.type != FcTypeCharSet)
116 return (double) FcCharSetSubtractCount (value1.u.c, value2.u.c);
120 FcCompareSize (char *object, FcValue value1, FcValue value2)
124 switch (value1.type) {
134 switch (value2.type) {
152 typedef struct _FcMatcher {
154 double (*compare) (char *object, FcValue value1, FcValue value2);
159 * Order is significant, it defines the precedence of
160 * each value, earlier values are more significant than
163 static FcMatcher _FcMatchers [] = {
164 { FC_FOUNDRY, FcCompareString, 0, 0 },
165 #define MATCH_FOUNDRY 0
167 { FC_CHARSET, FcCompareCharSet, 1, 1 },
168 #define MATCH_CHARSET 1
170 { FC_FAMILY, FcCompareFamily, 2, 4 },
171 #define MATCH_FAMILY 2
173 { FC_LANG, FcCompareLang, 3, 3 },
176 { FC_SPACING, FcCompareInteger, 5, 5 },
177 #define MATCH_SPACING 4
179 { FC_PIXEL_SIZE, FcCompareSize, 6, 6 },
180 #define MATCH_PIXEL_SIZE 5
182 { FC_STYLE, FcCompareString, 7, 7 },
183 #define MATCH_STYLE 6
185 { FC_SLANT, FcCompareInteger, 8, 8 },
186 #define MATCH_SLANT 7
188 { FC_WEIGHT, FcCompareInteger, 9, 9 },
189 #define MATCH_WEIGHT 8
191 { FC_ANTIALIAS, FcCompareBool, 10, 10 },
192 #define MATCH_ANTIALIAS 9
194 { FC_RASTERIZER, FcCompareString, 11, 11 },
195 #define MATCH_RASTERIZER 10
197 { FC_OUTLINE, FcCompareBool, 12, 12 },
198 #define MATCH_OUTLINE 11
201 #define NUM_MATCH_VALUES 13
204 FcCompareValueList (const char *object,
205 FcValueList *v1orig, /* pattern */
206 FcValueList *v2orig, /* target */
211 FcValueList *v1, *v2;
212 double v, best, bestStrong, bestWeak;
217 * Locate the possible matching entry by examining the
218 * first few characters in object
221 switch (FcToLower (object[0])) {
223 switch (FcToLower (object[1])) {
225 i = MATCH_FOUNDRY; break;
227 i = MATCH_FAMILY; break;
231 i = MATCH_CHARSET; break;
233 i = MATCH_ANTIALIAS; break;
235 i = MATCH_LANG; break;
237 switch (FcToLower (object[1])) {
239 i = MATCH_SPACING; break;
241 i = MATCH_STYLE; break;
243 i = MATCH_SLANT; break;
247 i = MATCH_PIXEL_SIZE; break;
249 i = MATCH_WEIGHT; break;
251 i = MATCH_RASTERIZER; break;
253 i = MATCH_OUTLINE; break;
256 FcStrCmpIgnoreCase ((FcChar8 *) _FcMatchers[i].object,
257 (FcChar8 *) object) != 0)
260 *bestValue = v2orig->value;
264 for (i = 0; i < NUM_MATCHER; i++)
266 if (!FcStrCmpIgnoreCase ((FcChar8 *) _FcMatchers[i].object,
270 if (i == NUM_MATCHER)
273 *bestValue = v2orig->value;
281 for (v1 = v1orig; v1; v1 = v1->next)
283 for (v2 = v2orig; v2; v2 = v2->next)
285 v = (*_FcMatchers[i].compare) (_FcMatchers[i].object,
290 *result = FcResultTypeMismatch;
293 if (FcDebug () & FC_DBG_MATCHV)
294 printf (" v %g j %d ", v, j);
299 *bestValue = v2->value;
302 if (v1->binding == FcValueBindingStrong)
315 if (FcDebug () & FC_DBG_MATCHV)
317 printf (" %s: %g ", object, best);
318 FcValueListPrint (v1orig);
320 FcValueListPrint (v2orig);
325 int weak = _FcMatchers[i].weak;
326 int strong = _FcMatchers[i].strong;
328 value[strong] += best;
331 value[weak] += bestWeak;
332 value[strong] += bestStrong;
339 * Return a value indicating the distance between the two lists of
344 FcCompare (FcPattern *pat,
351 for (i = 0; i < NUM_MATCH_VALUES; i++)
356 while (i1 < pat->num && i2 < fnt->num)
358 i = strcmp (pat->elts[i1].object, fnt->elts[i2].object);
365 if (!FcCompareValueList (pat->elts[i1].object,
366 pat->elts[i1].values,
367 fnt->elts[i2].values,
378 for (i1 = 0; i1 < pat->num; i1++)
380 for (i2 = 0; i2 < fnt->num; i2++)
382 if (!strcmp (pat->elts[i1].object, fnt->elts[i2].object))
393 FcFontRenderPrepare (FcConfig *config,
399 FcPatternElt *fe, *pe;
403 new = FcPatternCreate ();
406 for (i = 0; i < font->num; i++)
409 pe = FcPatternFindElt (pat, fe->object);
412 if (!FcCompareValueList (pe->object, pe->values,
413 fe->values, &v, 0, &result))
415 FcPatternDestroy (new);
420 v = fe->values->value;
421 FcPatternAdd (new, fe->object, v, FcFalse);
423 for (i = 0; i < pat->num; i++)
426 fe = FcPatternFindElt (font, pe->object);
428 FcPatternAdd (new, pe->object, pe->values->value, FcTrue);
430 FcConfigSubstituteWithPat (config, new, pat, FcMatchFont);
435 FcFontSetMatch (FcConfig *config,
441 double score[NUM_MATCH_VALUES], bestscore[NUM_MATCH_VALUES];
448 for (i = 0; i < NUM_MATCH_VALUES; i++)
451 if (FcDebug () & FC_DBG_MATCH)
458 config = FcConfigGetCurrent ();
462 for (set = 0; set < nsets; set++)
467 for (f = 0; f < s->nfont; f++)
469 if (FcDebug () & FC_DBG_MATCHV)
471 printf ("Font %d ", f);
472 FcPatternPrint (s->fonts[f]);
474 if (!FcCompare (p, s->fonts[f], score, result))
476 if (FcDebug () & FC_DBG_MATCHV)
479 for (i = 0; i < NUM_MATCH_VALUES; i++)
481 printf (" %g", score[i]);
485 for (i = 0; i < NUM_MATCH_VALUES; i++)
487 if (best && bestscore[i] < score[i])
489 if (!best || score[i] < bestscore[i])
491 for (i = 0; i < NUM_MATCH_VALUES; i++)
492 bestscore[i] = score[i];
499 if (FcDebug () & FC_DBG_MATCH)
501 printf ("Best score");
502 for (i = 0; i < NUM_MATCH_VALUES; i++)
503 printf (" %g", bestscore[i]);
504 FcPatternPrint (best);
508 *result = FcResultNoMatch;
511 return FcFontRenderPrepare (config, p, best);
515 FcFontMatch (FcConfig *config,
524 config = FcConfigGetCurrent ();
529 if (config->fonts[FcSetSystem])
530 sets[nsets++] = config->fonts[FcSetSystem];
531 if (config->fonts[FcSetApplication])
532 sets[nsets++] = config->fonts[FcSetApplication];
533 return FcFontSetMatch (config, sets, nsets, p, result);
536 typedef struct _FcSortNode {
538 double score[NUM_MATCH_VALUES];
542 FcSortCompare (const void *aa, const void *ab)
544 FcSortNode *a = *(FcSortNode **) aa;
545 FcSortNode *b = *(FcSortNode **) ab;
546 double *as = &a->score[0];
547 double *bs = &b->score[0];
548 double ad = 0, bd = 0;
551 i = NUM_MATCH_VALUES;
552 while (i-- && (ad = *as++) == (bd = *bs++))
554 return ad < bd ? -1 : ad > bd ? 1 : 0;
558 FcSortWalk (FcSortNode **n, int nnode, FcFontSet *fs, FcCharSet **cs, FcBool trim)
566 if (FcPatternGetCharSet (node->pattern, FC_CHARSET, 0, &ncs) ==
570 * If this font isn't a subset of the previous fonts,
573 if (!trim || !*cs || !FcCharSetIsSubset (ncs, *cs))
577 ncs = FcCharSetUnion (ncs, *cs);
580 FcCharSetDestroy (*cs);
583 ncs = FcCharSetCopy (ncs);
585 FcPatternReference (node->pattern);
586 if (FcDebug () & FC_DBG_MATCH)
589 FcPatternPrint (node->pattern);
591 if (!FcFontSetAdd (fs, node->pattern))
593 FcPatternDestroy (node->pattern);
603 FcFontSetSortDestroy (FcFontSet *fs)
605 FcFontSetDestroy (fs);
609 FcFontSetSort (FcConfig *config,
620 FcSortNode **nodeps, **nodep;
628 if (FcDebug () & FC_DBG_MATCH)
634 for (set = 0; set < nsets; set++)
643 nodes = malloc (nnodes * sizeof (FcSortNode) + nnodes * sizeof (FcSortNode *));
646 nodeps = (FcSortNode **) (nodes + nnodes);
650 for (set = 0; set < nsets; set++)
655 for (f = 0; f < s->nfont; f++)
657 if (FcDebug () & FC_DBG_MATCHV)
659 printf ("Font %d ", f);
660 FcPatternPrint (s->fonts[f]);
662 new->pattern = s->fonts[f];
663 if (!FcCompare (p, new->pattern, new->score, result))
665 if (FcDebug () & FC_DBG_MATCHV)
668 for (i = 0; i < NUM_MATCH_VALUES; i++)
670 printf (" %g", new->score[i]);
680 nnodes = new - nodes;
682 qsort (nodeps, nnodes, sizeof (FcSortNode *),
685 ret = FcFontSetCreate ();
691 if (!FcSortWalk (nodeps, nnodes, ret, &cs, trim))
697 FcCharSetDestroy (cs);
705 FcCharSetDestroy (cs);
706 FcFontSetDestroy (ret);
714 FcFontSort (FcConfig *config,
725 config = FcConfigGetCurrent ();
730 if (config->fonts[FcSetSystem])
731 sets[nsets++] = config->fonts[FcSetSystem];
732 if (config->fonts[FcSetApplication])
733 sets[nsets++] = config->fonts[FcSetApplication];
734 return FcFontSetSort (config, sets, nsets, p, trim, csp, result);