]>
git.wh0rd.org - fontconfig.git/blob - src/fcmatch.c
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 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 double (*compare
) (FcValue
*value1
, FcValue
*value2
);
182 * Order is significant, it defines the precedence of
183 * each value, earlier values are more significant than
186 static FcMatcher _FcMatchers
[] = {
187 { FC_FOUNDRY_OBJECT
, FcCompareString
, 0, 0 },
188 #define MATCH_FOUNDRY 0
189 #define MATCH_FOUNDRY_INDEX 0
191 { FC_CHARSET_OBJECT
, FcCompareCharSet
, 1, 1 },
192 #define MATCH_CHARSET 1
193 #define MATCH_CHARSET_INDEX 1
195 { FC_FAMILY_OBJECT
, FcCompareFamily
, 2, 4 },
196 #define MATCH_FAMILY 2
197 #define MATCH_FAMILY_STRONG_INDEX 2
198 #define MATCH_FAMILY_WEAK_INDEX 4
200 { FC_LANG_OBJECT
, FcCompareLang
, 3, 3 },
202 #define MATCH_LANG_INDEX 3
204 { FC_SPACING_OBJECT
, FcCompareNumber
, 5, 5 },
205 #define MATCH_SPACING 4
206 #define MATCH_SPACING_INDEX 5
208 { FC_PIXEL_SIZE_OBJECT
, FcCompareSize
, 6, 6 },
209 #define MATCH_PIXEL_SIZE 5
210 #define MATCH_PIXEL_SIZE_INDEX 6
212 { FC_STYLE_OBJECT
, FcCompareString
, 7, 7 },
213 #define MATCH_STYLE 6
214 #define MATCH_STYLE_INDEX 7
216 { FC_SLANT_OBJECT
, FcCompareNumber
, 8, 8 },
217 #define MATCH_SLANT 7
218 #define MATCH_SLANT_INDEX 8
220 { FC_WEIGHT_OBJECT
, FcCompareNumber
, 9, 9 },
221 #define MATCH_WEIGHT 8
222 #define MATCH_WEIGHT_INDEX 9
224 { FC_WIDTH_OBJECT
, FcCompareNumber
, 10, 10 },
225 #define MATCH_WIDTH 9
226 #define MATCH_WIDTH_INDEX 10
228 { FC_DECORATIVE_OBJECT
, FcCompareBool
, 11, 11 },
229 #define MATCH_DECORATIVE 10
230 #define MATCH_DECORATIVE_INDEX 11
232 { FC_ANTIALIAS_OBJECT
, FcCompareBool
, 12, 12 },
233 #define MATCH_ANTIALIAS 11
234 #define MATCH_ANTIALIAS_INDEX 12
236 { FC_RASTERIZER_OBJECT
, FcCompareString
, 13, 13 },
237 #define MATCH_RASTERIZER 12
238 #define MATCH_RASTERIZER_INDEX 13
240 { FC_OUTLINE_OBJECT
, FcCompareBool
, 14, 14 },
241 #define MATCH_OUTLINE 13
242 #define MATCH_OUTLINE_INDEX 14
244 { FC_FONTVERSION_OBJECT
, FcCompareNumber
, 15, 15 },
245 #define MATCH_FONTVERSION 14
246 #define MATCH_FONTVERSION_INDEX 15
249 #define NUM_MATCH_VALUES 16
252 FcObjectToMatcher (FcObject object
)
258 case FC_FOUNDRY_OBJECT
:
259 i
= MATCH_FOUNDRY
; break;
260 case FC_FONTVERSION_OBJECT
:
261 i
= MATCH_FONTVERSION
; break;
262 case FC_FAMILY_OBJECT
:
263 i
= MATCH_FAMILY
; break;
264 case FC_CHARSET_OBJECT
:
265 i
= MATCH_CHARSET
; break;
266 case FC_ANTIALIAS_OBJECT
:
267 i
= MATCH_ANTIALIAS
; break;
269 i
= MATCH_LANG
; break;
270 case FC_SPACING_OBJECT
:
271 i
= MATCH_SPACING
; break;
272 case FC_STYLE_OBJECT
:
273 i
= MATCH_STYLE
; break;
274 case FC_SLANT_OBJECT
:
275 i
= MATCH_SLANT
; break;
276 case FC_PIXEL_SIZE_OBJECT
:
277 i
= MATCH_PIXEL_SIZE
; break;
278 case FC_WIDTH_OBJECT
:
279 i
= MATCH_WIDTH
; break;
280 case FC_WEIGHT_OBJECT
:
281 i
= MATCH_WEIGHT
; break;
282 case FC_RASTERIZER_OBJECT
:
283 i
= MATCH_RASTERIZER
; break;
284 case FC_OUTLINE_OBJECT
:
285 i
= MATCH_OUTLINE
; break;
286 case FC_DECORATIVE_OBJECT
:
287 i
= MATCH_DECORATIVE
; break;
293 return _FcMatchers
+i
;
297 FcCompareValueList (FcObject object
,
298 FcValueListPtr v1orig
, /* pattern */
299 FcValueListPtr v2orig
, /* target */
304 FcValueListPtr v1
, v2
;
305 double v
, best
, bestStrong
, bestWeak
;
307 FcMatcher
*match
= FcObjectToMatcher(object
);
312 *bestValue
= FcValueCanonicalize(&v2orig
->value
);
320 for (v1
= v1orig
; v1
; v1
= FcValueListNext(v1
))
322 for (v2
= v2orig
; v2
; v2
= FcValueListNext(v2
))
324 v
= (match
->compare
) (&v1
->value
, &v2
->value
);
327 *result
= FcResultTypeMismatch
;
334 *bestValue
= FcValueCanonicalize(&v2
->value
);
337 if (v1
->binding
== FcValueBindingStrong
)
350 if (FcDebug () & FC_DBG_MATCHV
)
352 printf (" %s: %g ", FcObjectName (object
), best
);
353 FcValueListPrint (v1orig
);
355 FcValueListPrint (v2orig
);
360 int weak
= match
->weak
;
361 int strong
= match
->strong
;
363 value
[strong
] += best
;
366 value
[weak
] += bestWeak
;
367 value
[strong
] += bestStrong
;
374 * Return a value indicating the distance between the two lists of
379 FcCompare (FcPattern
*pat
,
386 for (i
= 0; i
< NUM_MATCH_VALUES
; i
++)
391 while (i1
< pat
->num
&& i2
< fnt
->num
)
393 FcPatternElt
*elt_i1
= &FcPatternElts(pat
)[i1
];
394 FcPatternElt
*elt_i2
= &FcPatternElts(fnt
)[i2
];
396 i
= FcObjectCompare(elt_i1
->object
, elt_i2
->object
);
403 if (!FcCompareValueList (elt_i1
->object
,
404 FcPatternEltValues(elt_i1
),
405 FcPatternEltValues(elt_i2
),
416 FcFontRenderPrepare (FcConfig
*config
,
422 FcPatternElt
*fe
, *pe
;
426 new = FcPatternCreate ();
429 for (i
= 0; i
< font
->num
; i
++)
431 fe
= &FcPatternElts(font
)[i
];
432 pe
= FcPatternObjectFindElt (pat
, fe
->object
);
435 if (!FcCompareValueList (pe
->object
, FcPatternEltValues(pe
),
436 FcPatternEltValues(fe
), &v
, 0, &result
))
438 FcPatternDestroy (new);
443 v
= FcValueCanonicalize(&FcPatternEltValues (fe
)->value
);
444 FcPatternObjectAdd (new, fe
->object
, v
, FcFalse
);
446 for (i
= 0; i
< pat
->num
; i
++)
448 pe
= &FcPatternElts(pat
)[i
];
449 fe
= FcPatternObjectFindElt (font
, pe
->object
);
452 v
= FcValueCanonicalize(&FcPatternEltValues(pe
)->value
);
453 FcPatternObjectAdd (new, pe
->object
, v
, FcTrue
);
457 FcConfigSubstituteWithPat (config
, new, pat
, FcMatchFont
);
462 FcFontSetMatch (FcConfig
*config
,
468 double score
[NUM_MATCH_VALUES
], bestscore
[NUM_MATCH_VALUES
];
475 for (i
= 0; i
< NUM_MATCH_VALUES
; i
++)
478 if (FcDebug () & FC_DBG_MATCH
)
485 config
= FcConfigGetCurrent ();
489 for (set
= 0; set
< nsets
; set
++)
494 for (f
= 0; f
< s
->nfont
; f
++)
496 if (FcDebug () & FC_DBG_MATCHV
)
498 printf ("Font %d ", f
);
499 FcPatternPrint (s
->fonts
[f
]);
501 if (!FcCompare (p
, s
->fonts
[f
], score
, result
))
503 if (FcDebug () & FC_DBG_MATCHV
)
506 for (i
= 0; i
< NUM_MATCH_VALUES
; i
++)
508 printf (" %g", score
[i
]);
512 for (i
= 0; i
< NUM_MATCH_VALUES
; i
++)
514 if (best
&& bestscore
[i
] < score
[i
])
516 if (!best
|| score
[i
] < bestscore
[i
])
518 for (i
= 0; i
< NUM_MATCH_VALUES
; i
++)
519 bestscore
[i
] = score
[i
];
526 if (FcDebug () & FC_DBG_MATCH
)
528 printf ("Best score");
529 for (i
= 0; i
< NUM_MATCH_VALUES
; i
++)
530 printf (" %g", bestscore
[i
]);
531 FcPatternPrint (best
);
535 *result
= FcResultNoMatch
;
538 return FcFontRenderPrepare (config
, p
, best
);
542 FcFontMatch (FcConfig
*config
,
551 config
= FcConfigGetCurrent ();
556 if (config
->fonts
[FcSetSystem
])
557 sets
[nsets
++] = config
->fonts
[FcSetSystem
];
558 if (config
->fonts
[FcSetApplication
])
559 sets
[nsets
++] = config
->fonts
[FcSetApplication
];
560 return FcFontSetMatch (config
, sets
, nsets
, p
, result
);
563 typedef struct _FcSortNode
{
565 double score
[NUM_MATCH_VALUES
];
569 FcSortCompare (const void *aa
, const void *ab
)
571 FcSortNode
*a
= *(FcSortNode
**) aa
;
572 FcSortNode
*b
= *(FcSortNode
**) ab
;
573 double *as
= &a
->score
[0];
574 double *bs
= &b
->score
[0];
575 double ad
= 0, bd
= 0;
578 i
= NUM_MATCH_VALUES
;
579 while (i
-- && (ad
= *as
++) == (bd
= *bs
++))
581 return ad
< bd
? -1 : ad
> bd
? 1 : 0;
585 FcSortWalk (FcSortNode
**n
, int nnode
, FcFontSet
*fs
, FcCharSet
**cs
, FcBool trim
, FcBool build_cs
)
593 if (FcPatternGetCharSet (node
->pattern
, FC_CHARSET
, 0, &ncs
) ==
597 * If this font isn't a subset of the previous fonts,
600 if (!trim
|| !*cs
|| !FcCharSetIsSubset (ncs
, *cs
))
602 if (trim
|| build_cs
)
606 ncs
= FcCharSetUnion (ncs
, *cs
);
609 FcCharSetDestroy (*cs
);
612 ncs
= FcCharSetCopy (ncs
);
616 FcPatternReference (node
->pattern
);
617 if (FcDebug () & FC_DBG_MATCHV
)
620 FcPatternPrint (node
->pattern
);
622 if (!FcFontSetAdd (fs
, node
->pattern
))
624 FcPatternDestroy (node
->pattern
);
634 FcFontSetSortDestroy (FcFontSet
*fs
)
636 FcFontSetDestroy (fs
);
640 FcFontSetSort (FcConfig
*config
,
651 FcSortNode
**nodeps
, **nodep
;
659 FcBool
*patternLangSat
;
662 if (FcDebug () & FC_DBG_MATCH
)
668 for (set
= 0; set
< nsets
; set
++)
678 for (nPatternLang
= 0;
679 FcPatternGet (p
, FC_LANG
, nPatternLang
, &patternLang
) == FcResultMatch
;
684 nodes
= malloc (nnodes
* sizeof (FcSortNode
) +
685 nnodes
* sizeof (FcSortNode
*) +
686 nPatternLang
* sizeof (FcBool
));
689 nodeps
= (FcSortNode
**) (nodes
+ nnodes
);
690 patternLangSat
= (FcBool
*) (nodeps
+ nnodes
);
694 for (set
= 0; set
< nsets
; set
++)
699 for (f
= 0; f
< s
->nfont
; f
++)
701 if (FcDebug () & FC_DBG_MATCHV
)
703 printf ("Font %d ", f
);
704 FcPatternPrint (s
->fonts
[f
]);
706 new->pattern
= s
->fonts
[f
];
707 if (!FcCompare (p
, new->pattern
, new->score
, result
))
709 if (FcDebug () & FC_DBG_MATCHV
)
712 for (i
= 0; i
< NUM_MATCH_VALUES
; i
++)
714 printf (" %g", new->score
[i
]);
724 nnodes
= new - nodes
;
726 qsort (nodeps
, nnodes
, sizeof (FcSortNode
*),
729 for (i
= 0; i
< nPatternLang
; i
++)
730 patternLangSat
[i
] = FcFalse
;
732 for (f
= 0; f
< nnodes
; f
++)
734 FcBool satisfies
= FcFalse
;
736 * If this node matches any language, go check
737 * which ones and satisfy those entries
739 if (nodeps
[f
]->score
[MATCH_LANG_INDEX
] < 200)
741 for (i
= 0; i
< nPatternLang
; i
++)
745 if (!patternLangSat
[i
] &&
746 FcPatternGet (p
, FC_LANG
, i
, &patternLang
) == FcResultMatch
&&
747 FcPatternGet (nodeps
[f
]->pattern
, FC_LANG
, 0, &nodeLang
) == FcResultMatch
)
749 double compare
= FcCompareLang (&patternLang
, &nodeLang
);
750 if (compare
>= 0 && compare
< 2)
752 if (FcDebug () & FC_DBG_MATCHV
)
757 if (FcPatternGetString (nodeps
[f
]->pattern
, FC_FAMILY
, 0, &family
) == FcResultMatch
&&
758 FcPatternGetString (nodeps
[f
]->pattern
, FC_STYLE
, 0, &style
) == FcResultMatch
)
759 printf ("Font %s:%s matches language %d\n", family
, style
, i
);
761 patternLangSat
[i
] = FcTrue
;
769 nodeps
[f
]->score
[MATCH_LANG_INDEX
] = 1000.0;
773 * Re-sort once the language issues have been settled
775 qsort (nodeps
, nnodes
, sizeof (FcSortNode
*),
778 ret
= FcFontSetCreate ();
784 if (!FcSortWalk (nodeps
, nnodes
, ret
, &cs
, trim
, (csp
!=0)))
792 FcCharSetDestroy (cs
);
797 if (FcDebug() & FC_DBG_MATCH
)
799 printf ("First font ");
800 FcPatternPrint (ret
->fonts
[0]);
806 FcCharSetDestroy (cs
);
807 FcFontSetDestroy (ret
);
815 FcFontSort (FcConfig
*config
,
826 config
= FcConfigGetCurrent ();
831 if (config
->fonts
[FcSetSystem
])
832 sets
[nsets
++] = config
->fonts
[FcSetSystem
];
833 if (config
->fonts
[FcSetApplication
])
834 sets
[nsets
++] = config
->fonts
[FcSetApplication
];
835 return FcFontSetSort (config
, sets
, nsets
, p
, trim
, csp
, result
);
838 #include "fcaliastail.h"