]> git.wh0rd.org - fontconfig.git/blob - src/fcmatch.c
Add better error reporting when loading config file
[fontconfig.git] / src / fcmatch.c
1 /*
2 * $XFree86: xc/lib/fontconfig/src/fcmatch.c,v 1.2 2002/02/15 06:01:28 keithp Exp $
3 *
4 * Copyright © 2000 Keith Packard, member of The XFree86 Project, Inc.
5 *
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.
15 *
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.
23 */
24
25 #include <string.h>
26 #include <ctype.h>
27 #include "fcint.h"
28 #include <stdio.h>
29
30 static double
31 FcCompareInteger (char *object, FcValue value1, FcValue value2)
32 {
33 int v;
34
35 if (value2.type != FcTypeInteger || value1.type != FcTypeInteger)
36 return -1.0;
37 v = value2.u.i - value1.u.i;
38 if (v < 0)
39 v = -v;
40 return (double) v;
41 }
42
43 static double
44 FcCompareString (char *object, FcValue value1, FcValue value2)
45 {
46 if (value2.type != FcTypeString || value1.type != FcTypeString)
47 return -1.0;
48 return (double) FcStrCmpIgnoreCase (value1.u.s, value2.u.s) != 0;
49 }
50
51 static double
52 FcCompareBool (char *object, FcValue value1, FcValue value2)
53 {
54 if (value2.type != FcTypeBool || value1.type != FcTypeBool)
55 return -1.0;
56 return (double) value2.u.b != value1.u.b;
57 }
58
59 static double
60 FcCompareCharSet (char *object, FcValue value1, FcValue value2)
61 {
62 if (value2.type != FcTypeCharSet || value1.type != FcTypeCharSet)
63 return -1.0;
64 return (double) FcCharSetSubtractCount (value1.u.c, value2.u.c);
65 }
66
67 static double
68 FcCompareSize (char *object, FcValue value1, FcValue value2)
69 {
70 double v1, v2, v;
71
72 switch (value1.type) {
73 case FcTypeInteger:
74 v1 = value1.u.i;
75 break;
76 case FcTypeDouble:
77 v1 = value1.u.d;
78 break;
79 default:
80 return -1;
81 }
82 switch (value2.type) {
83 case FcTypeInteger:
84 v2 = value2.u.i;
85 break;
86 case FcTypeDouble:
87 v2 = value2.u.d;
88 break;
89 default:
90 return -1;
91 }
92 if (v2 == 0)
93 return 0;
94 v = v2 - v1;
95 if (v < 0)
96 v = -v;
97 return v;
98 }
99
100 /*
101 * Order is significant, it defines the precedence of
102 * each value, earlier values are more significant than
103 * later values
104 */
105 static FcMatcher _FcMatchers [] = {
106 { FC_FOUNDRY, FcCompareString, },
107 { FC_CHARSET, FcCompareCharSet },
108 { FC_ANTIALIAS, FcCompareBool, },
109 { FC_LANG, FcCompareString },
110 { FC_FAMILY, FcCompareString, },
111 { FC_SPACING, FcCompareInteger, },
112 { FC_PIXEL_SIZE, FcCompareSize, },
113 { FC_STYLE, FcCompareString, },
114 { FC_SLANT, FcCompareInteger, },
115 { FC_WEIGHT, FcCompareInteger, },
116 { FC_RASTERIZER, FcCompareString, },
117 { FC_OUTLINE, FcCompareBool, },
118 };
119
120 #define NUM_MATCHER (sizeof _FcMatchers / sizeof _FcMatchers[0])
121
122 static FcBool
123 FcCompareValueList (const char *object,
124 FcValueList *v1orig, /* pattern */
125 FcValueList *v2orig, /* target */
126 FcValue *bestValue,
127 double *value,
128 FcResult *result)
129 {
130 FcValueList *v1, *v2;
131 double v, best;
132 int j;
133 int i;
134
135 for (i = 0; i < NUM_MATCHER; i++)
136 {
137 if (!FcStrCmpIgnoreCase ((FcChar8 *) _FcMatchers[i].object,
138 (FcChar8 *) object))
139 break;
140 }
141 if (i == NUM_MATCHER)
142 {
143 if (bestValue)
144 *bestValue = v2orig->value;
145 return FcTrue;
146 }
147
148 best = 1e99;
149 j = 0;
150 for (v1 = v1orig; v1; v1 = v1->next)
151 {
152 for (v2 = v2orig; v2; v2 = v2->next)
153 {
154 v = (*_FcMatchers[i].compare) (_FcMatchers[i].object,
155 v1->value,
156 v2->value);
157 if (v < 0)
158 {
159 *result = FcResultTypeMismatch;
160 return FcFalse;
161 }
162 if (FcDebug () & FC_DBG_MATCHV)
163 printf (" v %g j %d ", v, j);
164 v = v * 100 + j;
165 if (v < best)
166 {
167 if (bestValue)
168 *bestValue = v2->value;
169 best = v;
170 }
171 }
172 j++;
173 }
174 if (FcDebug () & FC_DBG_MATCHV)
175 {
176 printf (" %s: %g ", object, best);
177 FcValueListPrint (v1orig);
178 printf (", ");
179 FcValueListPrint (v2orig);
180 printf ("\n");
181 }
182 value[i] += best;
183 return FcTrue;
184 }
185
186 /*
187 * Return a value indicating the distance between the two lists of
188 * values
189 */
190
191 static FcBool
192 FcCompare (FcPattern *pat,
193 FcPattern *fnt,
194 double *value,
195 FcResult *result)
196 {
197 int i, i1, i2;
198
199 for (i = 0; i < NUM_MATCHER; i++)
200 value[i] = 0.0;
201
202 for (i1 = 0; i1 < pat->num; i1++)
203 {
204 for (i2 = 0; i2 < fnt->num; i2++)
205 {
206 if (!FcStrCmpIgnoreCase ((FcChar8 *) pat->elts[i1].object,
207 (FcChar8 *) fnt->elts[i2].object))
208 {
209 if (!FcCompareValueList (pat->elts[i1].object,
210 pat->elts[i1].values,
211 fnt->elts[i2].values,
212 0,
213 value,
214 result))
215 return FcFalse;
216 break;
217 }
218 }
219 #if 0
220 /*
221 * Overspecified patterns are slightly penalized in
222 * case some other font includes the requested field
223 */
224 if (i2 == fnt->num)
225 {
226 for (i2 = 0; i2 < NUM_MATCHER; i2++)
227 {
228 if (!FcStrCmpIgnoreCase (_FcMatchers[i2].object,
229 pat->elts[i1].object))
230 {
231 value[i2] = 1.0;
232 break;
233 }
234 }
235 }
236 #endif
237 }
238 return FcTrue;
239 }
240
241 FcPattern *
242 FcFontSetMatch (FcConfig *config,
243 FcFontSet **sets,
244 int nsets,
245 FcPattern *p,
246 FcResult *result)
247 {
248 double score[NUM_MATCHER], bestscore[NUM_MATCHER];
249 int f;
250 FcFontSet *s;
251 FcPattern *best;
252 FcPattern *new;
253 FcPatternElt *fe, *pe;
254 FcValue v;
255 int i;
256 FcSetName set;
257
258 for (i = 0; i < NUM_MATCHER; i++)
259 bestscore[i] = 0;
260 best = 0;
261 if (FcDebug () & FC_DBG_MATCH)
262 {
263 printf ("Match ");
264 FcPatternPrint (p);
265 }
266 if (!config)
267 {
268 config = FcConfigGetCurrent ();
269 if (!config)
270 return 0;
271 }
272 for (set = 0; set < nsets; set++)
273 {
274 s = sets[set];
275 if (!s)
276 continue;
277 for (f = 0; f < s->nfont; f++)
278 {
279 if (FcDebug () & FC_DBG_MATCHV)
280 {
281 printf ("Font %d ", f);
282 FcPatternPrint (s->fonts[f]);
283 }
284 if (!FcCompare (p, s->fonts[f], score, result))
285 return 0;
286 if (FcDebug () & FC_DBG_MATCHV)
287 {
288 printf ("Score");
289 for (i = 0; i < NUM_MATCHER; i++)
290 {
291 printf (" %g", score[i]);
292 }
293 printf ("\n");
294 }
295 for (i = 0; i < NUM_MATCHER; i++)
296 {
297 if (best && bestscore[i] < score[i])
298 break;
299 if (!best || score[i] < bestscore[i])
300 {
301 for (i = 0; i < NUM_MATCHER; i++)
302 bestscore[i] = score[i];
303 best = s->fonts[f];
304 break;
305 }
306 }
307 }
308 }
309 if (FcDebug () & FC_DBG_MATCH)
310 {
311 printf ("Best score");
312 for (i = 0; i < NUM_MATCHER; i++)
313 printf (" %g", bestscore[i]);
314 FcPatternPrint (best);
315 }
316 if (!best)
317 {
318 *result = FcResultNoMatch;
319 return 0;
320 }
321 new = FcPatternCreate ();
322 if (!new)
323 return 0;
324 for (i = 0; i < best->num; i++)
325 {
326 fe = &best->elts[i];
327 pe = FcPatternFind (p, fe->object, FcFalse);
328 if (pe)
329 {
330 if (!FcCompareValueList (pe->object, pe->values,
331 fe->values, &v, score, result))
332 {
333 FcPatternDestroy (new);
334 return 0;
335 }
336 }
337 else
338 v = fe->values->value;
339 FcPatternAdd (new, fe->object, v, FcTrue);
340 }
341 for (i = 0; i < p->num; i++)
342 {
343 pe = &p->elts[i];
344 fe = FcPatternFind (best, pe->object, FcFalse);
345 if (!fe)
346 FcPatternAdd (new, pe->object, pe->values->value, FcTrue);
347 }
348 FcConfigSubstitute (config, new, FcMatchFont);
349 return new;
350 }
351
352 FcPattern *
353 FcFontMatch (FcConfig *config,
354 FcPattern *p,
355 FcResult *result)
356 {
357 FcFontSet *sets[2];
358 int nsets;
359
360 if (!config)
361 {
362 config = FcConfigGetCurrent ();
363 if (!config)
364 return 0;
365 }
366 nsets = 0;
367 if (config->fonts[FcSetSystem])
368 sets[nsets++] = config->fonts[FcSetSystem];
369 if (config->fonts[FcSetApplication])
370 sets[nsets++] = config->fonts[FcSetApplication];
371 return FcFontSetMatch (config, sets, nsets, p, result);
372 }