]>
git.wh0rd.org - fontconfig.git/blob - src/fcmatch.c
2 * $XFree86: xc/lib/fontconfig/src/fcmatch.c,v 1.16 2002/07/06 23:47:44 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 FcPatternAddPattern (new, FC_PATTERN
, pat
);
406 FcConfigSubstitute (config
, new, FcMatchFont
);
411 FcFontSetMatch (FcConfig
*config
,
417 double score
[NUM_MATCH_VALUES
], bestscore
[NUM_MATCH_VALUES
];
424 for (i
= 0; i
< NUM_MATCH_VALUES
; i
++)
427 if (FcDebug () & FC_DBG_MATCH
)
434 config
= FcConfigGetCurrent ();
438 for (set
= 0; set
< nsets
; set
++)
443 for (f
= 0; f
< s
->nfont
; f
++)
445 if (FcDebug () & FC_DBG_MATCHV
)
447 printf ("Font %d ", f
);
448 FcPatternPrint (s
->fonts
[f
]);
450 if (!FcCompare (p
, s
->fonts
[f
], score
, result
))
452 if (FcDebug () & FC_DBG_MATCHV
)
455 for (i
= 0; i
< NUM_MATCH_VALUES
; i
++)
457 printf (" %g", score
[i
]);
461 for (i
= 0; i
< NUM_MATCH_VALUES
; i
++)
463 if (best
&& bestscore
[i
] < score
[i
])
465 if (!best
|| score
[i
] < bestscore
[i
])
467 for (i
= 0; i
< NUM_MATCH_VALUES
; i
++)
468 bestscore
[i
] = score
[i
];
475 if (FcDebug () & FC_DBG_MATCH
)
477 printf ("Best score");
478 for (i
= 0; i
< NUM_MATCH_VALUES
; i
++)
479 printf (" %g", bestscore
[i
]);
480 FcPatternPrint (best
);
484 *result
= FcResultNoMatch
;
487 return FcFontRenderPrepare (config
, p
, best
);
491 FcFontMatch (FcConfig
*config
,
500 config
= FcConfigGetCurrent ();
505 if (config
->fonts
[FcSetSystem
])
506 sets
[nsets
++] = config
->fonts
[FcSetSystem
];
507 if (config
->fonts
[FcSetApplication
])
508 sets
[nsets
++] = config
->fonts
[FcSetApplication
];
509 return FcFontSetMatch (config
, sets
, nsets
, p
, result
);
512 typedef struct _FcSortNode
{
514 double score
[NUM_MATCH_VALUES
];
518 FcSortCompare (const void *aa
, const void *ab
)
520 FcSortNode
*a
= *(FcSortNode
**) aa
;
521 FcSortNode
*b
= *(FcSortNode
**) ab
;
522 double *as
= &a
->score
[0];
523 double *bs
= &b
->score
[0];
524 double ad
= 0, bd
= 0;
527 i
= NUM_MATCH_VALUES
;
528 while (i
-- && (ad
= *as
++) == (bd
= *bs
++))
530 return ad
< bd
? -1 : ad
> bd
? 1 : 0;
534 FcSortWalk (FcSortNode
**n
, int nnode
, FcFontSet
*fs
, FcCharSet
**cs
, FcBool trim
)
542 if (FcPatternGetCharSet (node
->pattern
, FC_CHARSET
, 0, &ncs
) ==
546 * If this font isn't a subset of the previous fonts,
549 if (!trim
|| !*cs
|| !FcCharSetIsSubset (ncs
, *cs
))
553 ncs
= FcCharSetUnion (ncs
, *cs
);
556 FcCharSetDestroy (*cs
);
559 ncs
= FcCharSetCopy (ncs
);
561 FcPatternReference (node
->pattern
);
562 if (FcDebug () & FC_DBG_MATCH
)
565 FcPatternPrint (node
->pattern
);
567 if (!FcFontSetAdd (fs
, node
->pattern
))
569 FcPatternDestroy (node
->pattern
);
579 FcFontSetSortDestroy (FcFontSet
*fs
)
581 FcFontSetDestroy (fs
);
585 FcFontSetSort (FcConfig
*config
,
596 FcSortNode
**nodeps
, **nodep
;
604 if (FcDebug () & FC_DBG_MATCH
)
610 for (set
= 0; set
< nsets
; set
++)
619 nodes
= malloc (nnodes
* sizeof (FcSortNode
) + nnodes
* sizeof (FcSortNode
*));
622 nodeps
= (FcSortNode
**) (nodes
+ nnodes
);
626 for (set
= 0; set
< nsets
; set
++)
631 for (f
= 0; f
< s
->nfont
; f
++)
633 if (FcDebug () & FC_DBG_MATCHV
)
635 printf ("Font %d ", f
);
636 FcPatternPrint (s
->fonts
[f
]);
638 new->pattern
= s
->fonts
[f
];
639 if (!FcCompare (p
, new->pattern
, new->score
, result
))
641 if (FcDebug () & FC_DBG_MATCHV
)
644 for (i
= 0; i
< NUM_MATCH_VALUES
; i
++)
646 printf (" %g", new->score
[i
]);
656 nnodes
= new - nodes
;
658 qsort (nodeps
, nnodes
, sizeof (FcSortNode
*),
661 ret
= FcFontSetCreate ();
667 if (!FcSortWalk (nodeps
, nnodes
, ret
, &cs
, trim
))
673 FcCharSetDestroy (cs
);
681 FcCharSetDestroy (cs
);
682 FcFontSetDestroy (ret
);
690 FcFontSort (FcConfig
*config
,
701 config
= FcConfigGetCurrent ();
706 if (config
->fonts
[FcSetSystem
])
707 sets
[nsets
++] = config
->fonts
[FcSetSystem
];
708 if (config
->fonts
[FcSetApplication
])
709 sets
[nsets
++] = config
->fonts
[FcSetApplication
];
710 return FcFontSetSort (config
, sets
, nsets
, p
, trim
, csp
, result
);