]>
git.wh0rd.org - fontconfig.git/blob - src/fcmatch.c
2 * $XFree86: xc/lib/fontconfig/src/fcmatch.c,v 1.14 2002/06/19 21:32:18 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 FcCompareBool (char *object
, FcValue value1
, FcValue value2
)
54 if (value2
.type
!= FcTypeBool
|| value1
.type
!= FcTypeBool
)
56 return (double) value2
.u
.b
!= value1
.u
.b
;
60 FcCompareCharSet (char *object
, FcValue value1
, FcValue value2
)
62 if (value2
.type
!= FcTypeCharSet
|| value1
.type
!= FcTypeCharSet
)
64 return (double) FcCharSetSubtractCount (value1
.u
.c
, value2
.u
.c
);
68 FcCompareSize (char *object
, FcValue value1
, FcValue value2
)
72 switch (value1
.type
) {
82 switch (value2
.type
) {
100 typedef struct _FcMatcher
{
102 double (*compare
) (char *object
, FcValue value1
, FcValue value2
);
107 * Order is significant, it defines the precedence of
108 * each value, earlier values are more significant than
111 static FcMatcher _FcMatchers
[] = {
112 { FC_FOUNDRY
, FcCompareString
, 0, 0 },
113 #define MATCH_FOUNDRY 0
115 { FC_CHARSET
, FcCompareCharSet
, 1, 1 },
116 #define MATCH_CHARSET 1
118 { FC_FAMILY
, FcCompareString
, 2, 4 },
119 #define MATCH_FAMILY 2
121 { FC_LANG
, FcCompareString
, 3, 3 },
124 { FC_SPACING
, FcCompareInteger
, 5, 5 },
125 #define MATCH_SPACING 4
127 { FC_PIXEL_SIZE
, FcCompareSize
, 6, 6 },
128 #define MATCH_PIXEL_SIZE 5
130 { FC_STYLE
, FcCompareString
, 7, 7 },
131 #define MATCH_STYLE 6
133 { FC_SLANT
, FcCompareInteger
, 8, 8 },
134 #define MATCH_SLANT 7
136 { FC_WEIGHT
, FcCompareInteger
, 9, 9 },
137 #define MATCH_WEIGHT 8
139 { FC_ANTIALIAS
, FcCompareBool
, 10, 10 },
140 #define MATCH_ANTIALIAS 9
142 { FC_RASTERIZER
, FcCompareString
, 11, 11 },
143 #define MATCH_RASTERIZER 10
145 { FC_OUTLINE
, FcCompareBool
, 12, 12 },
146 #define MATCH_OUTLINE 11
149 #define NUM_MATCH_VALUES 13
152 FcCompareValueList (const char *object
,
153 FcValueList
*v1orig
, /* pattern */
154 FcValueList
*v2orig
, /* target */
159 FcValueList
*v1
, *v2
;
160 double v
, best
, bestStrong
, bestWeak
;
165 * Locate the possible matching entry by examining the
166 * first few characters in object
169 switch (FcToLower (object
[0])) {
171 switch (FcToLower (object
[1])) {
173 i
= MATCH_FOUNDRY
; break;
175 i
= MATCH_FAMILY
; break;
179 i
= MATCH_CHARSET
; break;
181 i
= MATCH_ANTIALIAS
; break;
183 i
= MATCH_LANG
; break;
185 switch (FcToLower (object
[1])) {
187 i
= MATCH_SPACING
; break;
189 i
= MATCH_STYLE
; break;
191 i
= MATCH_SLANT
; break;
195 i
= MATCH_PIXEL_SIZE
; break;
197 i
= MATCH_WEIGHT
; break;
199 i
= MATCH_RASTERIZER
; break;
201 i
= MATCH_OUTLINE
; break;
204 FcStrCmpIgnoreCase ((FcChar8
*) _FcMatchers
[i
].object
,
205 (FcChar8
*) object
) != 0)
208 *bestValue
= v2orig
->value
;
212 for (i
= 0; i
< NUM_MATCHER
; i
++)
214 if (!FcStrCmpIgnoreCase ((FcChar8
*) _FcMatchers
[i
].object
,
218 if (i
== NUM_MATCHER
)
221 *bestValue
= v2orig
->value
;
229 for (v1
= v1orig
; v1
; v1
= v1
->next
)
231 for (v2
= v2orig
; v2
; v2
= v2
->next
)
233 v
= (*_FcMatchers
[i
].compare
) (_FcMatchers
[i
].object
,
238 *result
= FcResultTypeMismatch
;
241 if (FcDebug () & FC_DBG_MATCHV
)
242 printf (" v %g j %d ", v
, j
);
247 *bestValue
= v2
->value
;
250 if (v1
->binding
== FcValueBindingStrong
)
263 if (FcDebug () & FC_DBG_MATCHV
)
265 printf (" %s: %g ", object
, best
);
266 FcValueListPrint (v1orig
);
268 FcValueListPrint (v2orig
);
273 int weak
= _FcMatchers
[i
].weak
;
274 int strong
= _FcMatchers
[i
].strong
;
276 value
[strong
] += best
;
279 value
[weak
] += bestWeak
;
280 value
[strong
] += bestStrong
;
287 * Return a value indicating the distance between the two lists of
292 FcCompare (FcPattern
*pat
,
299 for (i
= 0; i
< NUM_MATCH_VALUES
; i
++)
304 while (i1
< pat
->num
&& i2
< fnt
->num
)
306 i
= strcmp (pat
->elts
[i1
].object
, fnt
->elts
[i2
].object
);
313 if (!FcCompareValueList (pat
->elts
[i1
].object
,
314 pat
->elts
[i1
].values
,
315 fnt
->elts
[i2
].values
,
326 for (i1
= 0; i1
< pat
->num
; i1
++)
328 for (i2
= 0; i2
< fnt
->num
; i2
++)
330 if (!strcmp (pat
->elts
[i1
].object
, fnt
->elts
[i2
].object
))
341 FcFontRenderPrepare (FcConfig
*config
,
347 FcPatternElt
*fe
, *pe
;
351 new = FcPatternCreate ();
354 for (i
= 0; i
< font
->num
; i
++)
357 pe
= FcPatternFindElt (pat
, fe
->object
);
361 double score
[NUM_MATCH_VALUES
];
363 for (j
= 0; j
< NUM_MATCH_VALUES
; j
++)
365 if (!FcCompareValueList (pe
->object
, pe
->values
,
366 fe
->values
, &v
, score
, &result
))
368 FcPatternDestroy (new);
371 for (j
= 0; j
< NUM_MATCH_VALUES
; j
++)
372 if (score
[j
] >= 100.0)
376 for (pv
= pe
->values
; pv
; pv
= pv
->next
)
377 FcPatternAdd (new, fe
->object
, pv
->value
, FcTrue
);
382 v
= fe
->values
->value
;
383 FcPatternAdd (new, fe
->object
, v
, FcFalse
);
385 for (i
= 0; i
< pat
->num
; i
++)
388 fe
= FcPatternFindElt (font
, pe
->object
);
390 FcPatternAdd (new, pe
->object
, pe
->values
->value
, FcTrue
);
392 FcConfigSubstitute (config
, new, FcMatchFont
);
397 FcFontSetMatch (FcConfig
*config
,
403 double score
[NUM_MATCH_VALUES
], bestscore
[NUM_MATCH_VALUES
];
410 for (i
= 0; i
< NUM_MATCH_VALUES
; i
++)
413 if (FcDebug () & FC_DBG_MATCH
)
420 config
= FcConfigGetCurrent ();
424 for (set
= 0; set
< nsets
; set
++)
429 for (f
= 0; f
< s
->nfont
; f
++)
431 if (FcDebug () & FC_DBG_MATCHV
)
433 printf ("Font %d ", f
);
434 FcPatternPrint (s
->fonts
[f
]);
436 if (!FcCompare (p
, s
->fonts
[f
], score
, result
))
438 if (FcDebug () & FC_DBG_MATCHV
)
441 for (i
= 0; i
< NUM_MATCH_VALUES
; i
++)
443 printf (" %g", score
[i
]);
447 for (i
= 0; i
< NUM_MATCH_VALUES
; i
++)
449 if (best
&& bestscore
[i
] < score
[i
])
451 if (!best
|| score
[i
] < bestscore
[i
])
453 for (i
= 0; i
< NUM_MATCH_VALUES
; i
++)
454 bestscore
[i
] = score
[i
];
461 if (FcDebug () & FC_DBG_MATCH
)
463 printf ("Best score");
464 for (i
= 0; i
< NUM_MATCH_VALUES
; i
++)
465 printf (" %g", bestscore
[i
]);
466 FcPatternPrint (best
);
470 *result
= FcResultNoMatch
;
473 return FcFontRenderPrepare (config
, p
, best
);
477 FcFontMatch (FcConfig
*config
,
486 config
= FcConfigGetCurrent ();
491 if (config
->fonts
[FcSetSystem
])
492 sets
[nsets
++] = config
->fonts
[FcSetSystem
];
493 if (config
->fonts
[FcSetApplication
])
494 sets
[nsets
++] = config
->fonts
[FcSetApplication
];
495 return FcFontSetMatch (config
, sets
, nsets
, p
, result
);
498 typedef struct _FcSortNode
{
500 double score
[NUM_MATCH_VALUES
];
504 FcSortCompare (const void *aa
, const void *ab
)
506 FcSortNode
*a
= *(FcSortNode
**) aa
;
507 FcSortNode
*b
= *(FcSortNode
**) ab
;
508 double *as
= &a
->score
[0];
509 double *bs
= &b
->score
[0];
510 double ad
= 0, bd
= 0;
513 i
= NUM_MATCH_VALUES
;
514 while (i
-- && (ad
= *as
++) == (bd
= *bs
++))
516 return ad
< bd
? -1 : ad
> bd
? 1 : 0;
520 FcSortWalk (FcSortNode
**n
, int nnode
, FcFontSet
*fs
, FcCharSet
**cs
, FcBool trim
)
528 if (FcPatternGetCharSet (node
->pattern
, FC_CHARSET
, 0, &ncs
) ==
532 * If this font isn't a subset of the previous fonts,
535 if (!trim
|| !*cs
|| !FcCharSetIsSubset (ncs
, *cs
))
539 ncs
= FcCharSetUnion (ncs
, *cs
);
542 FcCharSetDestroy (*cs
);
545 ncs
= FcCharSetCopy (ncs
);
547 FcPatternReference (node
->pattern
);
548 if (!FcFontSetAdd (fs
, node
->pattern
))
550 FcPatternDestroy (node
->pattern
);
560 FcFontSetSortDestroy (FcFontSet
*fs
)
562 FcFontSetDestroy (fs
);
566 FcFontSetSort (FcConfig
*config
,
577 FcSortNode
**nodeps
, **nodep
;
586 for (set
= 0; set
< nsets
; set
++)
595 nodes
= malloc (nnodes
* sizeof (FcSortNode
) + nnodes
* sizeof (FcSortNode
*));
598 nodeps
= (FcSortNode
**) (nodes
+ nnodes
);
602 for (set
= 0; set
< nsets
; set
++)
607 for (f
= 0; f
< s
->nfont
; f
++)
609 if (FcDebug () & FC_DBG_MATCHV
)
611 printf ("Font %d ", f
);
612 FcPatternPrint (s
->fonts
[f
]);
614 new->pattern
= s
->fonts
[f
];
615 if (!FcCompare (p
, new->pattern
, new->score
, result
))
617 if (FcDebug () & FC_DBG_MATCHV
)
620 for (i
= 0; i
< NUM_MATCH_VALUES
; i
++)
622 printf (" %g", new->score
[i
]);
632 nnodes
= new - nodes
;
634 qsort (nodeps
, nnodes
, sizeof (FcSortNode
*),
637 ret
= FcFontSetCreate ();
643 if (!FcSortWalk (nodeps
, nnodes
, ret
, &cs
, trim
))
649 FcCharSetDestroy (cs
);
657 FcCharSetDestroy (cs
);
658 FcFontSetDestroy (ret
);
666 FcFontSort (FcConfig
*config
,
677 config
= FcConfigGetCurrent ();
682 if (config
->fonts
[FcSetSystem
])
683 sets
[nsets
++] = config
->fonts
[FcSetSystem
];
684 if (config
->fonts
[FcSetApplication
])
685 sets
[nsets
++] = config
->fonts
[FcSetApplication
];
686 return FcFontSetSort (config
, sets
, nsets
, p
, trim
, csp
, result
);