]>
git.wh0rd.org - fontconfig.git/blob - src/fcmatch.c
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 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
, FcCompareString
, 0, 0 },
188 #define MATCH_FOUNDRY 0
189 #define MATCH_FOUNDRY_INDEX 0
191 { FC_CHARSET
, FcCompareCharSet
, 1, 1 },
192 #define MATCH_CHARSET 1
193 #define MATCH_CHARSET_INDEX 1
195 { FC_FAMILY
, 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
, FcCompareLang
, 3, 3 },
202 #define MATCH_LANG_INDEX 3
204 { FC_SPACING
, FcCompareNumber
, 5, 5 },
205 #define MATCH_SPACING 4
206 #define MATCH_SPACING_INDEX 5
208 { FC_PIXEL_SIZE
, FcCompareSize
, 6, 6 },
209 #define MATCH_PIXEL_SIZE 5
210 #define MATCH_PIXEL_SIZE_INDEX 6
212 { FC_STYLE
, FcCompareString
, 7, 7 },
213 #define MATCH_STYLE 6
214 #define MATCH_STYLE_INDEX 7
216 { FC_SLANT
, FcCompareNumber
, 8, 8 },
217 #define MATCH_SLANT 7
218 #define MATCH_SLANT_INDEX 8
220 { FC_WEIGHT
, FcCompareNumber
, 9, 9 },
221 #define MATCH_WEIGHT 8
222 #define MATCH_WEIGHT_INDEX 9
224 { FC_WIDTH
, FcCompareNumber
, 10, 10 },
225 #define MATCH_WIDTH 9
226 #define MATCH_WIDTH_INDEX 10
228 { FC_ANTIALIAS
, FcCompareBool
, 11, 11 },
229 #define MATCH_ANTIALIAS 10
230 #define MATCH_ANTIALIAS_INDEX 11
232 { FC_RASTERIZER
, FcCompareString
, 12, 12 },
233 #define MATCH_RASTERIZER 11
234 #define MATCH_RASTERIZER_INDEX 12
236 { FC_OUTLINE
, FcCompareBool
, 13, 13 },
237 #define MATCH_OUTLINE 12
238 #define MATCH_OUTLINE_INDEX 13
240 { FC_FONTVERSION
, FcCompareNumber
, 14, 14 },
241 #define MATCH_FONTVERSION 13
242 #define MATCH_FONTVERSION_INDEX 14
245 #define NUM_MATCH_VALUES 15
248 FcCompareValueList (FcObjectPtr o
,
249 FcValueListPtr v1orig
, /* pattern */
250 FcValueListPtr v2orig
, /* target */
255 FcValueListPtr v1
, v2
;
256 FcValueList
*v1_ptrU
, *v2_ptrU
;
257 double v
, best
, bestStrong
, bestWeak
;
260 const char* object
= FcObjectPtrU(o
);
263 * Locate the possible matching entry by examining the
264 * first few characters in object
273 i
= MATCH_FOUNDRY
; break;
275 i
= MATCH_FONTVERSION
; break;
279 i
= MATCH_FAMILY
; break;
283 i
= MATCH_CHARSET
; break;
285 i
= MATCH_ANTIALIAS
; break;
287 i
= MATCH_LANG
; break;
291 i
= MATCH_SPACING
; break;
293 i
= MATCH_STYLE
; break;
295 i
= MATCH_SLANT
; break;
299 i
= MATCH_PIXEL_SIZE
; break;
303 i
= MATCH_WIDTH
; break;
305 i
= MATCH_WEIGHT
; break;
309 i
= MATCH_RASTERIZER
; break;
311 i
= MATCH_OUTLINE
; break;
314 FcStrCmpIgnoreCase ((FcChar8
*) _FcMatchers
[i
].object
,
315 (FcChar8
*) object
) != 0)
318 *bestValue
= FcValueCanonicalize(&FcValueListPtrU(v2orig
)->value
);
325 for (v1
= v1orig
, v1_ptrU
= FcValueListPtrU(v1
); v1_ptrU
;
326 v1
= FcValueListPtrU(v1
)->next
, v1_ptrU
= FcValueListPtrU(v1
))
328 for (v2
= v2orig
, v2_ptrU
= FcValueListPtrU(v2
); FcValueListPtrU(v2
);
329 v2
= FcValueListPtrU(v2
)->next
)
331 v
= (*_FcMatchers
[i
].compare
) (&v1_ptrU
->value
,
335 *result
= FcResultTypeMismatch
;
342 *bestValue
= FcValueCanonicalize(&v2_ptrU
->value
);
345 if (v1_ptrU
->binding
== FcValueBindingStrong
)
358 if (FcDebug () & FC_DBG_MATCHV
)
360 printf (" %s: %g ", object
, best
);
361 FcValueListPrint (v1orig
);
363 FcValueListPrint (v2orig
);
368 int weak
= _FcMatchers
[i
].weak
;
369 int strong
= _FcMatchers
[i
].strong
;
371 value
[strong
] += best
;
374 value
[weak
] += bestWeak
;
375 value
[strong
] += bestStrong
;
382 * Return a value indicating the distance between the two lists of
387 FcCompare (FcPattern
*pat
,
394 for (i
= 0; i
< NUM_MATCH_VALUES
; i
++)
399 while (i1
< pat
->num
&& i2
< fnt
->num
)
401 FcPatternElt
*elt_i1
= FcPatternEltU(pat
->elts
)+i1
;
402 FcPatternElt
*elt_i2
= FcPatternEltU(fnt
->elts
)+i2
;
404 i
= FcObjectPtrCompare(elt_i1
->object
, elt_i2
->object
);
411 if (!FcCompareValueList (elt_i1
->object
,
412 elt_i1
->values
, elt_i2
->values
,
423 FcFontRenderPrepare (FcConfig
*config
,
429 FcPatternElt
*fe
, *pe
;
433 new = FcPatternCreate ();
436 for (i
= 0; i
< font
->num
; i
++)
438 fe
= FcPatternEltU(font
->elts
)+i
;
439 pe
= FcPatternFindElt (pat
, FcObjectPtrU(fe
->object
));
442 if (!FcCompareValueList (pe
->object
, pe
->values
,
443 fe
->values
, &v
, 0, &result
))
445 FcPatternDestroy (new);
450 v
= FcValueCanonicalize(&FcValueListPtrU(fe
->values
)->value
);
451 FcPatternAdd (new, FcObjectPtrU(fe
->object
), v
, FcFalse
);
453 for (i
= 0; i
< pat
->num
; i
++)
455 pe
= FcPatternEltU(pat
->elts
)+i
;
456 fe
= FcPatternFindElt (font
, FcObjectPtrU(pe
->object
));
458 FcPatternAdd (new, FcObjectPtrU(pe
->object
),
459 FcValueCanonicalize(&FcValueListPtrU(pe
->values
)->value
), FcTrue
);
462 if (FcPatternFindElt (font
, FC_FILE
))
463 FcPatternTransferFullFname (new, font
);
465 FcConfigSubstituteWithPat (config
, new, pat
, FcMatchFont
);
470 FcFontSetMatch (FcConfig
*config
,
476 double score
[NUM_MATCH_VALUES
], bestscore
[NUM_MATCH_VALUES
];
483 for (i
= 0; i
< NUM_MATCH_VALUES
; i
++)
486 if (FcDebug () & FC_DBG_MATCH
)
493 config
= FcConfigGetCurrent ();
496 *result
= FcResultOutOfMemory
;
500 for (set
= 0; set
< nsets
; set
++)
505 for (f
= 0; f
< s
->nfont
; f
++)
507 if (FcDebug () & FC_DBG_MATCHV
)
509 printf ("Font %d ", f
);
510 FcPatternPrint (s
->fonts
[f
]);
512 if (!FcCompare (p
, s
->fonts
[f
], score
, result
))
514 if (FcDebug () & FC_DBG_MATCHV
)
517 for (i
= 0; i
< NUM_MATCH_VALUES
; i
++)
519 printf (" %g", score
[i
]);
523 for (i
= 0; i
< NUM_MATCH_VALUES
; i
++)
525 if (best
&& bestscore
[i
] < score
[i
])
527 if (!best
|| score
[i
] < bestscore
[i
])
529 for (i
= 0; i
< NUM_MATCH_VALUES
; i
++)
530 bestscore
[i
] = score
[i
];
537 if (FcDebug () & FC_DBG_MATCH
)
539 printf ("Best score");
540 for (i
= 0; i
< NUM_MATCH_VALUES
; i
++)
541 printf (" %g", bestscore
[i
]);
542 FcPatternPrint (best
);
546 *result
= FcResultNoMatch
;
549 return FcFontRenderPrepare (config
, p
, best
);
553 FcFontMatch (FcConfig
*config
,
562 config
= FcConfigGetCurrent ();
567 if (config
->fonts
[FcSetSystem
])
568 sets
[nsets
++] = config
->fonts
[FcSetSystem
];
569 if (config
->fonts
[FcSetApplication
])
570 sets
[nsets
++] = config
->fonts
[FcSetApplication
];
571 return FcFontSetMatch (config
, sets
, nsets
, p
, result
);
574 typedef struct _FcSortNode
{
576 double score
[NUM_MATCH_VALUES
];
580 FcSortCompare (const void *aa
, const void *ab
)
582 FcSortNode
*a
= *(FcSortNode
**) aa
;
583 FcSortNode
*b
= *(FcSortNode
**) ab
;
584 double *as
= &a
->score
[0];
585 double *bs
= &b
->score
[0];
586 double ad
= 0, bd
= 0;
589 i
= NUM_MATCH_VALUES
;
590 while (i
-- && (ad
= *as
++) == (bd
= *bs
++))
592 return ad
< bd
? -1 : ad
> bd
? 1 : 0;
596 FcSortWalk (FcSortNode
**n
, int nnode
, FcFontSet
*fs
, FcCharSet
**cs
, FcBool trim
)
604 if (FcPatternGetCharSet (node
->pattern
, FC_CHARSET
, 0, &ncs
) ==
608 * If this font isn't a subset of the previous fonts,
611 if (!trim
|| !*cs
|| !FcCharSetIsSubset (ncs
, *cs
))
615 ncs
= FcCharSetUnion (ncs
, *cs
);
618 FcCharSetDestroy (*cs
);
621 ncs
= FcCharSetCopy (ncs
);
623 FcPatternReference (node
->pattern
);
624 if (FcDebug () & FC_DBG_MATCH
)
627 FcPatternPrint (node
->pattern
);
629 if (!FcFontSetAdd (fs
, node
->pattern
))
631 FcPatternDestroy (node
->pattern
);
641 FcFontSetSortDestroy (FcFontSet
*fs
)
643 FcFontSetDestroy (fs
);
647 FcFontSetSort (FcConfig
*config
,
658 FcSortNode
**nodeps
, **nodep
;
666 FcBool
*patternLangSat
;
669 if (FcDebug () & FC_DBG_MATCH
)
675 for (set
= 0; set
< nsets
; set
++)
685 for (nPatternLang
= 0;
686 FcPatternGet (p
, FC_LANG
, nPatternLang
, &patternLang
) == FcResultMatch
;
691 nodes
= malloc (nnodes
* sizeof (FcSortNode
) +
692 nnodes
* sizeof (FcSortNode
*) +
693 nPatternLang
* sizeof (FcBool
));
696 nodeps
= (FcSortNode
**) (nodes
+ nnodes
);
697 patternLangSat
= (FcBool
*) (nodeps
+ nnodes
);
701 for (set
= 0; set
< nsets
; set
++)
706 for (f
= 0; f
< s
->nfont
; f
++)
708 if (FcDebug () & FC_DBG_MATCHV
)
710 printf ("Font %d ", f
);
711 FcPatternPrint (s
->fonts
[f
]);
713 new->pattern
= s
->fonts
[f
];
714 if (!FcCompare (p
, new->pattern
, new->score
, result
))
716 if (FcDebug () & FC_DBG_MATCHV
)
719 for (i
= 0; i
< NUM_MATCH_VALUES
; i
++)
721 printf (" %g", new->score
[i
]);
731 nnodes
= new - nodes
;
733 qsort (nodeps
, nnodes
, sizeof (FcSortNode
*),
736 for (i
= 0; i
< nPatternLang
; i
++)
737 patternLangSat
[i
] = FcFalse
;
739 for (f
= 0; f
< nnodes
; f
++)
741 FcBool satisfies
= FcFalse
;
743 * If this node matches any language, go check
744 * which ones and satisfy those entries
746 if (nodeps
[f
]->score
[MATCH_LANG_INDEX
] < nPatternLang
)
748 for (i
= 0; i
< nPatternLang
; i
++)
752 if (!patternLangSat
[i
] &&
753 FcPatternGet (p
, FC_LANG
, i
, &patternLang
) == FcResultMatch
&&
754 FcPatternGet (nodeps
[f
]->pattern
, FC_LANG
, 0, &nodeLang
) == FcResultMatch
)
756 double compare
= FcCompareLang (&patternLang
, &nodeLang
);
757 if (compare
>= 0 && compare
< 2)
759 if (FcDebug () & FC_DBG_MATCHV
)
764 if (FcPatternGetString (nodeps
[f
]->pattern
, FC_FAMILY
, 0, &family
) == FcResultMatch
&&
765 FcPatternGetString (nodeps
[f
]->pattern
, FC_STYLE
, 0, &style
) == FcResultMatch
)
766 printf ("Font %s:%s matches language %d\n", family
, style
, i
);
768 patternLangSat
[i
] = FcTrue
;
776 nodeps
[f
]->score
[MATCH_LANG_INDEX
] = 1000.0;
780 * Re-sort once the language issues have been settled
782 qsort (nodeps
, nnodes
, sizeof (FcSortNode
*),
785 ret
= FcFontSetCreate ();
791 if (!FcSortWalk (nodeps
, nnodes
, ret
, &cs
, trim
))
797 FcCharSetDestroy (cs
);
805 FcCharSetDestroy (cs
);
806 FcFontSetDestroy (ret
);
814 FcFontSort (FcConfig
*config
,
825 config
= FcConfigGetCurrent ();
830 if (config
->fonts
[FcSetSystem
])
831 sets
[nsets
++] = config
->fonts
[FcSetSystem
];
832 if (config
->fonts
[FcSetApplication
])
833 sets
[nsets
++] = config
->fonts
[FcSetApplication
];
834 return FcFontSetSort (config
, sets
, nsets
, p
, trim
, csp
, result
);