]>
git.wh0rd.org - fontconfig.git/blob - src/fcmatch.c
2 * $XFree86: xc/lib/fontconfig/src/fcmatch.c,v 1.17 2002/08/11 18:10:42 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 if (value2
.type
!= FcTypeString
|| value1
.type
!= FcTypeString
)
66 result
= FcLangCompare (value1
.u
.s
, value2
.u
.s
);
70 case FcLangDifferentCountry
:
72 case FcLangDifferentLang
:
79 FcCompareBool (char *object
, FcValue value1
, FcValue value2
)
81 if (value2
.type
!= FcTypeBool
|| value1
.type
!= FcTypeBool
)
83 return (double) value2
.u
.b
!= value1
.u
.b
;
87 FcCompareCharSet (char *object
, FcValue value1
, FcValue value2
)
89 if (value2
.type
!= FcTypeCharSet
|| value1
.type
!= FcTypeCharSet
)
91 return (double) FcCharSetSubtractCount (value1
.u
.c
, value2
.u
.c
);
95 FcCompareSize (char *object
, FcValue value1
, FcValue value2
)
99 switch (value1
.type
) {
109 switch (value2
.type
) {
127 typedef struct _FcMatcher
{
129 double (*compare
) (char *object
, FcValue value1
, FcValue value2
);
134 * Order is significant, it defines the precedence of
135 * each value, earlier values are more significant than
138 static FcMatcher _FcMatchers
[] = {
139 { FC_FOUNDRY
, FcCompareString
, 0, 0 },
140 #define MATCH_FOUNDRY 0
142 { FC_CHARSET
, FcCompareCharSet
, 1, 1 },
143 #define MATCH_CHARSET 1
145 { FC_FAMILY
, FcCompareFamily
, 2, 4 },
146 #define MATCH_FAMILY 2
148 { FC_LANG
, FcCompareLang
, 3, 3 },
151 { FC_SPACING
, FcCompareInteger
, 5, 5 },
152 #define MATCH_SPACING 4
154 { FC_PIXEL_SIZE
, FcCompareSize
, 6, 6 },
155 #define MATCH_PIXEL_SIZE 5
157 { FC_STYLE
, FcCompareString
, 7, 7 },
158 #define MATCH_STYLE 6
160 { FC_SLANT
, FcCompareInteger
, 8, 8 },
161 #define MATCH_SLANT 7
163 { FC_WEIGHT
, FcCompareInteger
, 9, 9 },
164 #define MATCH_WEIGHT 8
166 { FC_ANTIALIAS
, FcCompareBool
, 10, 10 },
167 #define MATCH_ANTIALIAS 9
169 { FC_RASTERIZER
, FcCompareString
, 11, 11 },
170 #define MATCH_RASTERIZER 10
172 { FC_OUTLINE
, FcCompareBool
, 12, 12 },
173 #define MATCH_OUTLINE 11
176 #define NUM_MATCH_VALUES 13
179 FcCompareValueList (const char *object
,
180 FcValueList
*v1orig
, /* pattern */
181 FcValueList
*v2orig
, /* target */
186 FcValueList
*v1
, *v2
;
187 double v
, best
, bestStrong
, bestWeak
;
192 * Locate the possible matching entry by examining the
193 * first few characters in object
196 switch (FcToLower (object
[0])) {
198 switch (FcToLower (object
[1])) {
200 i
= MATCH_FOUNDRY
; break;
202 i
= MATCH_FAMILY
; break;
206 i
= MATCH_CHARSET
; break;
208 i
= MATCH_ANTIALIAS
; break;
210 i
= MATCH_LANG
; break;
212 switch (FcToLower (object
[1])) {
214 i
= MATCH_SPACING
; break;
216 i
= MATCH_STYLE
; break;
218 i
= MATCH_SLANT
; break;
222 i
= MATCH_PIXEL_SIZE
; break;
224 i
= MATCH_WEIGHT
; break;
226 i
= MATCH_RASTERIZER
; break;
228 i
= MATCH_OUTLINE
; break;
231 FcStrCmpIgnoreCase ((FcChar8
*) _FcMatchers
[i
].object
,
232 (FcChar8
*) object
) != 0)
235 *bestValue
= v2orig
->value
;
239 for (i
= 0; i
< NUM_MATCHER
; i
++)
241 if (!FcStrCmpIgnoreCase ((FcChar8
*) _FcMatchers
[i
].object
,
245 if (i
== NUM_MATCHER
)
248 *bestValue
= v2orig
->value
;
256 for (v1
= v1orig
; v1
; v1
= v1
->next
)
258 for (v2
= v2orig
; v2
; v2
= v2
->next
)
260 v
= (*_FcMatchers
[i
].compare
) (_FcMatchers
[i
].object
,
265 *result
= FcResultTypeMismatch
;
268 if (FcDebug () & FC_DBG_MATCHV
)
269 printf (" v %g j %d ", v
, j
);
274 *bestValue
= v2
->value
;
277 if (v1
->binding
== FcValueBindingStrong
)
290 if (FcDebug () & FC_DBG_MATCHV
)
292 printf (" %s: %g ", object
, best
);
293 FcValueListPrint (v1orig
);
295 FcValueListPrint (v2orig
);
300 int weak
= _FcMatchers
[i
].weak
;
301 int strong
= _FcMatchers
[i
].strong
;
303 value
[strong
] += best
;
306 value
[weak
] += bestWeak
;
307 value
[strong
] += bestStrong
;
314 * Return a value indicating the distance between the two lists of
319 FcCompare (FcPattern
*pat
,
326 for (i
= 0; i
< NUM_MATCH_VALUES
; i
++)
331 while (i1
< pat
->num
&& i2
< fnt
->num
)
333 i
= strcmp (pat
->elts
[i1
].object
, fnt
->elts
[i2
].object
);
340 if (!FcCompareValueList (pat
->elts
[i1
].object
,
341 pat
->elts
[i1
].values
,
342 fnt
->elts
[i2
].values
,
353 for (i1
= 0; i1
< pat
->num
; i1
++)
355 for (i2
= 0; i2
< fnt
->num
; i2
++)
357 if (!strcmp (pat
->elts
[i1
].object
, fnt
->elts
[i2
].object
))
368 FcFontRenderPrepare (FcConfig
*config
,
374 FcPatternElt
*fe
, *pe
;
378 new = FcPatternCreate ();
381 for (i
= 0; i
< font
->num
; i
++)
384 pe
= FcPatternFindElt (pat
, fe
->object
);
387 if (!FcCompareValueList (pe
->object
, pe
->values
,
388 fe
->values
, &v
, 0, &result
))
390 FcPatternDestroy (new);
395 v
= fe
->values
->value
;
396 FcPatternAdd (new, fe
->object
, v
, FcFalse
);
398 for (i
= 0; i
< pat
->num
; i
++)
401 fe
= FcPatternFindElt (font
, pe
->object
);
403 FcPatternAdd (new, pe
->object
, pe
->values
->value
, FcTrue
);
405 FcConfigSubstituteWithPat (config
, new, pat
, FcMatchFont
);
410 FcFontSetMatch (FcConfig
*config
,
416 double score
[NUM_MATCH_VALUES
], bestscore
[NUM_MATCH_VALUES
];
423 for (i
= 0; i
< NUM_MATCH_VALUES
; i
++)
426 if (FcDebug () & FC_DBG_MATCH
)
433 config
= FcConfigGetCurrent ();
437 for (set
= 0; set
< nsets
; set
++)
442 for (f
= 0; f
< s
->nfont
; f
++)
444 if (FcDebug () & FC_DBG_MATCHV
)
446 printf ("Font %d ", f
);
447 FcPatternPrint (s
->fonts
[f
]);
449 if (!FcCompare (p
, s
->fonts
[f
], score
, result
))
451 if (FcDebug () & FC_DBG_MATCHV
)
454 for (i
= 0; i
< NUM_MATCH_VALUES
; i
++)
456 printf (" %g", score
[i
]);
460 for (i
= 0; i
< NUM_MATCH_VALUES
; i
++)
462 if (best
&& bestscore
[i
] < score
[i
])
464 if (!best
|| score
[i
] < bestscore
[i
])
466 for (i
= 0; i
< NUM_MATCH_VALUES
; i
++)
467 bestscore
[i
] = score
[i
];
474 if (FcDebug () & FC_DBG_MATCH
)
476 printf ("Best score");
477 for (i
= 0; i
< NUM_MATCH_VALUES
; i
++)
478 printf (" %g", bestscore
[i
]);
479 FcPatternPrint (best
);
483 *result
= FcResultNoMatch
;
486 return FcFontRenderPrepare (config
, p
, best
);
490 FcFontMatch (FcConfig
*config
,
499 config
= FcConfigGetCurrent ();
504 if (config
->fonts
[FcSetSystem
])
505 sets
[nsets
++] = config
->fonts
[FcSetSystem
];
506 if (config
->fonts
[FcSetApplication
])
507 sets
[nsets
++] = config
->fonts
[FcSetApplication
];
508 return FcFontSetMatch (config
, sets
, nsets
, p
, result
);
511 typedef struct _FcSortNode
{
513 double score
[NUM_MATCH_VALUES
];
517 FcSortCompare (const void *aa
, const void *ab
)
519 FcSortNode
*a
= *(FcSortNode
**) aa
;
520 FcSortNode
*b
= *(FcSortNode
**) ab
;
521 double *as
= &a
->score
[0];
522 double *bs
= &b
->score
[0];
523 double ad
= 0, bd
= 0;
526 i
= NUM_MATCH_VALUES
;
527 while (i
-- && (ad
= *as
++) == (bd
= *bs
++))
529 return ad
< bd
? -1 : ad
> bd
? 1 : 0;
533 FcSortWalk (FcSortNode
**n
, int nnode
, FcFontSet
*fs
, FcCharSet
**cs
, FcBool trim
)
541 if (FcPatternGetCharSet (node
->pattern
, FC_CHARSET
, 0, &ncs
) ==
545 * If this font isn't a subset of the previous fonts,
548 if (!trim
|| !*cs
|| !FcCharSetIsSubset (ncs
, *cs
))
552 ncs
= FcCharSetUnion (ncs
, *cs
);
555 FcCharSetDestroy (*cs
);
558 ncs
= FcCharSetCopy (ncs
);
560 FcPatternReference (node
->pattern
);
561 if (FcDebug () & FC_DBG_MATCH
)
564 FcPatternPrint (node
->pattern
);
566 if (!FcFontSetAdd (fs
, node
->pattern
))
568 FcPatternDestroy (node
->pattern
);
578 FcFontSetSortDestroy (FcFontSet
*fs
)
580 FcFontSetDestroy (fs
);
584 FcFontSetSort (FcConfig
*config
,
595 FcSortNode
**nodeps
, **nodep
;
603 if (FcDebug () & FC_DBG_MATCH
)
609 for (set
= 0; set
< nsets
; set
++)
618 nodes
= malloc (nnodes
* sizeof (FcSortNode
) + nnodes
* sizeof (FcSortNode
*));
621 nodeps
= (FcSortNode
**) (nodes
+ nnodes
);
625 for (set
= 0; set
< nsets
; set
++)
630 for (f
= 0; f
< s
->nfont
; f
++)
632 if (FcDebug () & FC_DBG_MATCHV
)
634 printf ("Font %d ", f
);
635 FcPatternPrint (s
->fonts
[f
]);
637 new->pattern
= s
->fonts
[f
];
638 if (!FcCompare (p
, new->pattern
, new->score
, result
))
640 if (FcDebug () & FC_DBG_MATCHV
)
643 for (i
= 0; i
< NUM_MATCH_VALUES
; i
++)
645 printf (" %g", new->score
[i
]);
655 nnodes
= new - nodes
;
657 qsort (nodeps
, nnodes
, sizeof (FcSortNode
*),
660 ret
= FcFontSetCreate ();
666 if (!FcSortWalk (nodeps
, nnodes
, ret
, &cs
, trim
))
672 FcCharSetDestroy (cs
);
680 FcCharSetDestroy (cs
);
681 FcFontSetDestroy (ret
);
689 FcFontSort (FcConfig
*config
,
700 config
= FcConfigGetCurrent ();
705 if (config
->fonts
[FcSetSystem
])
706 sets
[nsets
++] = config
->fonts
[FcSetSystem
];
707 if (config
->fonts
[FcSetApplication
])
708 sets
[nsets
++] = config
->fonts
[FcSetApplication
];
709 return FcFontSetSort (config
, sets
, nsets
, p
, trim
, csp
, result
);