2 * $RCSId: xc/lib/fontconfig/src/fcmatch.c,v 1.20 2002/08/31 22:17:32 keithp Exp $
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 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 FcCompareNumber (FcValue
*value1
, FcValue
*value2
)
35 switch (value1
->type
) {
37 v1
= (double) value1
->u
.i
;
45 switch (value2
->type
) {
47 v2
= (double) value2
->u
.i
;
62 FcCompareString (FcValue
*v1
, FcValue
*v2
)
64 return (double) FcStrCmpIgnoreCase (fc_value_string(v1
), fc_value_string(v2
)) != 0;
68 FcCompareFamily (FcValue
*v1
, FcValue
*v2
)
70 /* rely on the guarantee in FcPatternAddWithBinding that
71 * families are always FcTypeString. */
72 const FcChar8
* v1_string
= fc_value_string(v1
);
73 const FcChar8
* v2_string
= fc_value_string(v2
);
75 if (FcToLower(*v1_string
) != FcToLower(*v2_string
))
78 return (double) FcStrCmpIgnoreBlanksAndCase (v1_string
, v2_string
) != 0;
82 FcCompareLang (FcValue
*v1
, FcValue
*v2
)
85 FcValue value1
= FcValueCanonicalize(v1
), value2
= FcValueCanonicalize(v2
);
87 switch (value1
.type
) {
89 switch (value2
.type
) {
91 result
= FcLangSetCompare (value1
.u
.l
, value2
.u
.l
);
94 result
= FcLangSetHasLang (value1
.u
.l
,
102 switch (value2
.type
) {
104 result
= FcLangSetHasLang (value2
.u
.l
, value1
.u
.s
);
107 result
= FcLangCompare (value1
.u
.s
,
120 case FcLangDifferentCountry
:
122 case FcLangDifferentLang
:
129 FcCompareBool (FcValue
*v1
, FcValue
*v2
)
131 if (fc_storage_type(v2
) != FcTypeBool
|| fc_storage_type(v1
) != FcTypeBool
)
133 return (double) v2
->u
.b
!= v1
->u
.b
;
137 FcCompareCharSet (FcValue
*v1
, FcValue
*v2
)
139 return (double) FcCharSetSubtractCount (fc_value_charset(v1
), fc_value_charset(v2
));
143 FcCompareSize (FcValue
*value1
, FcValue
*value2
)
147 switch (value1
->type
) {
157 switch (value2
->type
) {
175 typedef struct _FcMatcher
{
177 FcObjectPtr objectPtr
;
178 double (*compare
) (FcValue
*value1
, FcValue
*value2
);
183 * Order is significant, it defines the precedence of
184 * each value, earlier values are more significant than
187 static FcMatcher _FcMatchers
[] = {
188 { FC_FOUNDRY
, 0, FcCompareString
, 0, 0 },
189 #define MATCH_FOUNDRY 0
190 #define MATCH_FOUNDRY_INDEX 0
192 { FC_CHARSET
, 0, FcCompareCharSet
, 1, 1 },
193 #define MATCH_CHARSET 1
194 #define MATCH_CHARSET_INDEX 1
196 { FC_FAMILY
, 0, FcCompareFamily
, 2, 4 },
197 #define MATCH_FAMILY 2
198 #define MATCH_FAMILY_STRONG_INDEX 2
199 #define MATCH_FAMILY_WEAK_INDEX 4
201 { FC_LANG
, 0, FcCompareLang
, 3, 3 },
203 #define MATCH_LANG_INDEX 3
205 { FC_SPACING
, 0, FcCompareNumber
, 5, 5 },
206 #define MATCH_SPACING 4
207 #define MATCH_SPACING_INDEX 5
209 { FC_PIXEL_SIZE
, 0, FcCompareSize
, 6, 6 },
210 #define MATCH_PIXEL_SIZE 5
211 #define MATCH_PIXEL_SIZE_INDEX 6
213 { FC_STYLE
, 0, FcCompareString
, 7, 7 },
214 #define MATCH_STYLE 6
215 #define MATCH_STYLE_INDEX 7
217 { FC_SLANT
, 0, FcCompareNumber
, 8, 8 },
218 #define MATCH_SLANT 7
219 #define MATCH_SLANT_INDEX 8
221 { FC_WEIGHT
, 0, FcCompareNumber
, 9, 9 },
222 #define MATCH_WEIGHT 8
223 #define MATCH_WEIGHT_INDEX 9
225 { FC_WIDTH
, 0, FcCompareNumber
, 10, 10 },
226 #define MATCH_WIDTH 9
227 #define MATCH_WIDTH_INDEX 10
229 { FC_ANTIALIAS
, 0, FcCompareBool
, 11, 11 },
230 #define MATCH_ANTIALIAS 10
231 #define MATCH_ANTIALIAS_INDEX 11
233 { FC_RASTERIZER
, 0, FcCompareString
, 12, 12 },
234 #define MATCH_RASTERIZER 11
235 #define MATCH_RASTERIZER_INDEX 12
237 { FC_OUTLINE
, 0, FcCompareBool
, 13, 13 },
238 #define MATCH_OUTLINE 12
239 #define MATCH_OUTLINE_INDEX 13
241 { FC_FONTVERSION
, 0, FcCompareNumber
, 14, 14 },
242 #define MATCH_FONTVERSION 13
243 #define MATCH_FONTVERSION_INDEX 14
246 #define NUM_MATCH_VALUES 15
248 static FcBool matchObjectPtrsInit
= FcFalse
;
251 FcMatchObjectPtrsInit (void)
253 _FcMatchers
[MATCH_FOUNDRY
].objectPtr
= FcObjectToPtr(FC_FOUNDRY
);
254 _FcMatchers
[MATCH_CHARSET
].objectPtr
= FcObjectToPtr(FC_CHARSET
);
255 _FcMatchers
[MATCH_FAMILY
].objectPtr
= FcObjectToPtr(FC_FAMILY
);
256 _FcMatchers
[MATCH_LANG
].objectPtr
= FcObjectToPtr(FC_LANG
);
257 _FcMatchers
[MATCH_SPACING
].objectPtr
= FcObjectToPtr(FC_SPACING
);
258 _FcMatchers
[MATCH_PIXEL_SIZE
].objectPtr
= FcObjectToPtr(FC_PIXEL_SIZE
);
259 _FcMatchers
[MATCH_STYLE
].objectPtr
= FcObjectToPtr(FC_STYLE
);
260 _FcMatchers
[MATCH_SLANT
].objectPtr
= FcObjectToPtr(FC_SLANT
);
261 _FcMatchers
[MATCH_WEIGHT
].objectPtr
= FcObjectToPtr(FC_WEIGHT
);
262 _FcMatchers
[MATCH_WIDTH
].objectPtr
= FcObjectToPtr(FC_WIDTH
);
263 _FcMatchers
[MATCH_ANTIALIAS
].objectPtr
= FcObjectToPtr(FC_ANTIALIAS
);
264 _FcMatchers
[MATCH_RASTERIZER
].objectPtr
= FcObjectToPtr(FC_RASTERIZER
);
265 _FcMatchers
[MATCH_OUTLINE
].objectPtr
= FcObjectToPtr(FC_OUTLINE
);
266 _FcMatchers
[MATCH_FONTVERSION
].objectPtr
= FcObjectToPtr(FC_FONTVERSION
);
267 matchObjectPtrsInit
= FcTrue
;
271 FcCompareValueList (FcObjectPtr o
,
272 FcValueListPtr v1orig
, /* pattern */
273 FcValueListPtr v2orig
, /* target */
278 FcValueListPtr v1
, v2
;
279 FcValueList
*v1_ptrU
, *v2_ptrU
;
280 double v
, best
, bestStrong
, bestWeak
;
283 const char *object
= FcObjectPtrU(o
);
286 * Locate the possible matching entry by examining the
287 * first few characters in object
296 i
= MATCH_FOUNDRY
; break;
298 i
= MATCH_FONTVERSION
; break;
302 i
= MATCH_FAMILY
; break;
306 i
= MATCH_CHARSET
; break;
308 i
= MATCH_ANTIALIAS
; break;
310 i
= MATCH_LANG
; break;
314 i
= MATCH_SPACING
; break;
316 i
= MATCH_STYLE
; break;
318 i
= MATCH_SLANT
; break;
322 i
= MATCH_PIXEL_SIZE
; break;
326 i
= MATCH_WIDTH
; break;
328 i
= MATCH_WEIGHT
; break;
332 i
= MATCH_RASTERIZER
; break;
334 i
= MATCH_OUTLINE
; break;
337 if (!matchObjectPtrsInit
)
338 FcMatchObjectPtrsInit();
339 if (_FcMatchers
[i
].objectPtr
!= o
) i
= -1;
342 FcStrCmpIgnoreCase ((FcChar8
*) _FcMatchers
[i
].object
,
343 (FcChar8
*) object
) != 0)
346 *bestValue
= FcValueCanonicalize(&FcValueListPtrU(v2orig
)->value
);
353 for (v1
= v1orig
, v1_ptrU
= FcValueListPtrU(v1
); v1_ptrU
;
354 v1
= FcValueListPtrU(v1
)->next
, v1_ptrU
= FcValueListPtrU(v1
))
356 for (v2
= v2orig
, v2_ptrU
= FcValueListPtrU(v2
); FcValueListPtrU(v2
);
357 v2
= FcValueListPtrU(v2
)->next
)
359 v
= (*_FcMatchers
[i
].compare
) (&v1_ptrU
->value
,
363 *result
= FcResultTypeMismatch
;
370 *bestValue
= FcValueCanonicalize(&v2_ptrU
->value
);
373 if (v1_ptrU
->binding
== FcValueBindingStrong
)
386 if (FcDebug () & FC_DBG_MATCHV
)
388 printf (" %s: %g ", object
, best
);
389 FcValueListPrint (v1orig
);
391 FcValueListPrint (v2orig
);
396 int weak
= _FcMatchers
[i
].weak
;
397 int strong
= _FcMatchers
[i
].strong
;
399 value
[strong
] += best
;
402 value
[weak
] += bestWeak
;
403 value
[strong
] += bestStrong
;
410 * Return a value indicating the distance between the two lists of
415 FcCompare (FcPattern
*pat
,
422 for (i
= 0; i
< NUM_MATCH_VALUES
; i
++)
427 while (i1
< pat
->num
&& i2
< fnt
->num
)
429 FcPatternElt
*elt_i1
= FcPatternEltU(pat
->elts
)+i1
;
430 FcPatternElt
*elt_i2
= FcPatternEltU(fnt
->elts
)+i2
;
432 i
= FcObjectPtrCompare(elt_i1
->object
, elt_i2
->object
);
439 if (!FcCompareValueList (elt_i1
->object
,
440 elt_i1
->values
, elt_i2
->values
,
451 FcFontRenderPrepare (FcConfig
*config
,
457 FcPatternElt
*fe
, *pe
;
461 new = FcPatternCreate ();
464 for (i
= 0; i
< font
->num
; i
++)
466 fe
= FcPatternEltU(font
->elts
)+i
;
467 pe
= FcPatternFindElt (pat
, FcObjectPtrU(fe
->object
));
470 if (!FcCompareValueList (pe
->object
, pe
->values
,
471 fe
->values
, &v
, 0, &result
))
473 FcPatternDestroy (new);
478 v
= FcValueCanonicalize(&FcValueListPtrU(fe
->values
)->value
);
479 FcPatternAdd (new, FcObjectPtrU(fe
->object
), v
, FcFalse
);
481 for (i
= 0; i
< pat
->num
; i
++)
483 pe
= FcPatternEltU(pat
->elts
)+i
;
484 fe
= FcPatternFindElt (font
, FcObjectPtrU(pe
->object
));
486 FcPatternAdd (new, FcObjectPtrU(pe
->object
),
487 FcValueCanonicalize(&FcValueListPtrU(pe
->values
)->value
), FcTrue
);
490 if (FcPatternFindElt (font
, FC_FILE
))
491 FcPatternTransferFullFname (new, font
);
493 FcConfigSubstituteWithPat (config
, new, pat
, FcMatchFont
);
498 FcFontSetMatch (FcConfig
*config
,
504 double score
[NUM_MATCH_VALUES
], bestscore
[NUM_MATCH_VALUES
];
511 for (i
= 0; i
< NUM_MATCH_VALUES
; i
++)
514 if (FcDebug () & FC_DBG_MATCH
)
521 config
= FcConfigGetCurrent ();
524 *result
= FcResultOutOfMemory
;
528 for (set
= 0; set
< nsets
; set
++)
533 for (f
= 0; f
< s
->nfont
; f
++)
535 if (FcDebug () & FC_DBG_MATCHV
)
537 printf ("Font %d ", f
);
538 FcPatternPrint (s
->fonts
[f
]);
540 if (!FcCompare (p
, s
->fonts
[f
], score
, result
))
542 if (FcDebug () & FC_DBG_MATCHV
)
545 for (i
= 0; i
< NUM_MATCH_VALUES
; i
++)
547 printf (" %g", score
[i
]);
551 for (i
= 0; i
< NUM_MATCH_VALUES
; i
++)
553 if (best
&& bestscore
[i
] < score
[i
])
555 if (!best
|| score
[i
] < bestscore
[i
])
557 for (i
= 0; i
< NUM_MATCH_VALUES
; i
++)
558 bestscore
[i
] = score
[i
];
565 if (FcDebug () & FC_DBG_MATCH
)
567 printf ("Best score");
568 for (i
= 0; i
< NUM_MATCH_VALUES
; i
++)
569 printf (" %g", bestscore
[i
]);
570 FcPatternPrint (best
);
574 *result
= FcResultNoMatch
;
577 return FcFontRenderPrepare (config
, p
, best
);
581 FcFontMatch (FcConfig
*config
,
590 config
= FcConfigGetCurrent ();
595 if (config
->fonts
[FcSetSystem
])
596 sets
[nsets
++] = config
->fonts
[FcSetSystem
];
597 if (config
->fonts
[FcSetApplication
])
598 sets
[nsets
++] = config
->fonts
[FcSetApplication
];
599 return FcFontSetMatch (config
, sets
, nsets
, p
, result
);
602 typedef struct _FcSortNode
{
604 double score
[NUM_MATCH_VALUES
];
608 FcSortCompare (const void *aa
, const void *ab
)
610 FcSortNode
*a
= *(FcSortNode
**) aa
;
611 FcSortNode
*b
= *(FcSortNode
**) ab
;
612 double *as
= &a
->score
[0];
613 double *bs
= &b
->score
[0];
614 double ad
= 0, bd
= 0;
617 i
= NUM_MATCH_VALUES
;
618 while (i
-- && (ad
= *as
++) == (bd
= *bs
++))
620 return ad
< bd
? -1 : ad
> bd
? 1 : 0;
624 FcSortWalk (FcSortNode
**n
, int nnode
, FcFontSet
*fs
, FcCharSet
**cs
, FcBool trim
)
632 if (FcPatternGetCharSet (node
->pattern
, FC_CHARSET
, 0, &ncs
) ==
636 * If this font isn't a subset of the previous fonts,
639 if (!trim
|| !*cs
|| !FcCharSetIsSubset (ncs
, *cs
))
643 ncs
= FcCharSetUnion (ncs
, *cs
);
646 FcCharSetDestroy (*cs
);
649 ncs
= FcCharSetCopy (ncs
);
651 FcPatternReference (node
->pattern
);
652 if (FcDebug () & FC_DBG_MATCH
)
655 FcPatternPrint (node
->pattern
);
657 if (!FcFontSetAdd (fs
, node
->pattern
))
659 FcPatternDestroy (node
->pattern
);
669 FcFontSetSortDestroy (FcFontSet
*fs
)
671 FcFontSetDestroy (fs
);
675 FcFontSetSort (FcConfig
*config
,
686 FcSortNode
**nodeps
, **nodep
;
694 FcBool
*patternLangSat
;
697 if (FcDebug () & FC_DBG_MATCH
)
703 for (set
= 0; set
< nsets
; set
++)
713 for (nPatternLang
= 0;
714 FcPatternGet (p
, FC_LANG
, nPatternLang
, &patternLang
) == FcResultMatch
;
719 nodes
= malloc (nnodes
* sizeof (FcSortNode
) +
720 nnodes
* sizeof (FcSortNode
*) +
721 nPatternLang
* sizeof (FcBool
));
724 nodeps
= (FcSortNode
**) (nodes
+ nnodes
);
725 patternLangSat
= (FcBool
*) (nodeps
+ nnodes
);
729 for (set
= 0; set
< nsets
; set
++)
734 for (f
= 0; f
< s
->nfont
; f
++)
736 if (FcDebug () & FC_DBG_MATCHV
)
738 printf ("Font %d ", f
);
739 FcPatternPrint (s
->fonts
[f
]);
741 new->pattern
= s
->fonts
[f
];
742 if (!FcCompare (p
, new->pattern
, new->score
, result
))
744 if (FcDebug () & FC_DBG_MATCHV
)
747 for (i
= 0; i
< NUM_MATCH_VALUES
; i
++)
749 printf (" %g", new->score
[i
]);
759 nnodes
= new - nodes
;
761 qsort (nodeps
, nnodes
, sizeof (FcSortNode
*),
764 for (i
= 0; i
< nPatternLang
; i
++)
765 patternLangSat
[i
] = FcFalse
;
767 for (f
= 0; f
< nnodes
; f
++)
769 FcBool satisfies
= FcFalse
;
771 * If this node matches any language, go check
772 * which ones and satisfy those entries
774 if (nodeps
[f
]->score
[MATCH_LANG_INDEX
] < nPatternLang
)
776 for (i
= 0; i
< nPatternLang
; i
++)
780 if (!patternLangSat
[i
] &&
781 FcPatternGet (p
, FC_LANG
, i
, &patternLang
) == FcResultMatch
&&
782 FcPatternGet (nodeps
[f
]->pattern
, FC_LANG
, 0, &nodeLang
) == FcResultMatch
)
784 double compare
= FcCompareLang (&patternLang
, &nodeLang
);
785 if (compare
>= 0 && compare
< 2)
787 if (FcDebug () & FC_DBG_MATCHV
)
792 if (FcPatternGetString (nodeps
[f
]->pattern
, FC_FAMILY
, 0, &family
) == FcResultMatch
&&
793 FcPatternGetString (nodeps
[f
]->pattern
, FC_STYLE
, 0, &style
) == FcResultMatch
)
794 printf ("Font %s:%s matches language %d\n", family
, style
, i
);
796 patternLangSat
[i
] = FcTrue
;
804 nodeps
[f
]->score
[MATCH_LANG_INDEX
] = 1000.0;
808 * Re-sort once the language issues have been settled
810 qsort (nodeps
, nnodes
, sizeof (FcSortNode
*),
813 ret
= FcFontSetCreate ();
819 if (!FcSortWalk (nodeps
, nnodes
, ret
, &cs
, trim
))
825 FcCharSetDestroy (cs
);
833 FcCharSetDestroy (cs
);
834 FcFontSetDestroy (ret
);
842 FcFontSort (FcConfig
*config
,
853 config
= FcConfigGetCurrent ();
858 if (config
->fonts
[FcSetSystem
])
859 sets
[nsets
++] = config
->fonts
[FcSetSystem
];
860 if (config
->fonts
[FcSetApplication
])
861 sets
[nsets
++] = config
->fonts
[FcSetApplication
];
862 return FcFontSetSort (config
, sets
, nsets
, p
, trim
, csp
, result
);