]> git.wh0rd.org - fontconfig.git/blob - src/fcpat.c
Optimize after profiling. Fix FcStrCmp to return correct sign
[fontconfig.git] / src / fcpat.c
1 /*
2 * $XFree86: xc/lib/fontconfig/src/fcpat.c,v 1.4 2002/05/22 04:12:35 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 <stdlib.h>
26 #include <string.h>
27 #include "fcint.h"
28
29 FcPattern *
30 FcPatternCreate (void)
31 {
32 FcPattern *p;
33
34 p = (FcPattern *) malloc (sizeof (FcPattern));
35 if (!p)
36 return 0;
37 FcMemAlloc (FC_MEM_PATTERN, sizeof (FcPattern));
38 p->num = 0;
39 p->size = 0;
40 p->elts = 0;
41 return p;
42 }
43
44 void
45 FcValueDestroy (FcValue v)
46 {
47 switch (v.type) {
48 case FcTypeString:
49 FcStrFree ((FcChar8 *) v.u.s);
50 break;
51 case FcTypeMatrix:
52 FcMatrixFree ((FcMatrix *) v.u.m);
53 break;
54 case FcTypeCharSet:
55 FcCharSetDestroy ((FcCharSet *) v.u.c);
56 break;
57 default:
58 break;
59 }
60 }
61
62 FcValue
63 FcValueSave (FcValue v)
64 {
65 switch (v.type) {
66 case FcTypeString:
67 v.u.s = FcStrCopy (v.u.s);
68 if (!v.u.s)
69 v.type = FcTypeVoid;
70 break;
71 case FcTypeMatrix:
72 v.u.m = FcMatrixCopy (v.u.m);
73 if (!v.u.m)
74 v.type = FcTypeVoid;
75 break;
76 case FcTypeCharSet:
77 v.u.c = FcCharSetCopy ((FcCharSet *) v.u.c);
78 if (!v.u.c)
79 v.type = FcTypeVoid;
80 break;
81 default:
82 break;
83 }
84 return v;
85 }
86
87 void
88 FcValueListDestroy (FcValueList *l)
89 {
90 FcValueList *next;
91 for (; l; l = next)
92 {
93 switch (l->value.type) {
94 case FcTypeString:
95 FcStrFree ((FcChar8 *) l->value.u.s);
96 break;
97 case FcTypeMatrix:
98 FcMatrixFree ((FcMatrix *) l->value.u.m);
99 break;
100 case FcTypeCharSet:
101 FcCharSetDestroy ((FcCharSet *) l->value.u.c);
102 break;
103 default:
104 break;
105 }
106 next = l->next;
107 FcMemFree (FC_MEM_VALLIST, sizeof (FcValueList));
108 free (l);
109 }
110 }
111
112 FcBool
113 FcValueEqual (FcValue va, FcValue vb)
114 {
115 if (va.type != vb.type)
116 {
117 if (va.type == FcTypeInteger)
118 {
119 va.type = FcTypeDouble;
120 va.u.d = va.u.i;
121 }
122 if (vb.type == FcTypeInteger)
123 {
124 vb.type = FcTypeDouble;
125 vb.u.d = vb.u.i;
126 }
127 if (va.type != vb.type)
128 return FcFalse;
129 }
130 switch (va.type) {
131 case FcTypeVoid:
132 return FcTrue;
133 case FcTypeInteger:
134 return va.u.i == vb.u.i;
135 case FcTypeDouble:
136 return va.u.d == vb.u.d;
137 case FcTypeString:
138 return FcStrCmpIgnoreCase (va.u.s, vb.u.s) == 0;
139 case FcTypeBool:
140 return va.u.b == vb.u.b;
141 case FcTypeMatrix:
142 return FcMatrixEqual (va.u.m, vb.u.m);
143 case FcTypeCharSet:
144 return FcCharSetEqual (va.u.c, vb.u.c);
145 }
146 return FcFalse;
147 }
148
149 static FcBool
150 FcValueListEqual (FcValueList *la, FcValueList *lb)
151 {
152 while (la && lb)
153 {
154 if (!FcValueEqual (la->value, lb->value))
155 return FcFalse;
156 la = la->next;
157 lb = lb->next;
158 }
159 if (la || lb)
160 return FcFalse;
161 return FcTrue;
162 }
163
164 void
165 FcPatternDestroy (FcPattern *p)
166 {
167 int i;
168
169 for (i = 0; i < p->num; i++)
170 FcValueListDestroy (p->elts[i].values);
171
172 p->num = 0;
173 if (p->elts)
174 {
175 FcMemFree (FC_MEM_PATELT, p->size * sizeof (FcPatternElt));
176 free (p->elts);
177 p->elts = 0;
178 }
179 p->size = 0;
180 FcMemFree (FC_MEM_PATTERN, sizeof (FcPattern));
181 free (p);
182 }
183
184 FcPatternElt *
185 FcPatternFind (FcPattern *p, const char *object, FcBool insert)
186 {
187 int i;
188 int s;
189 FcPatternElt *e;
190
191 int low, high;
192
193 /* match existing */
194 low = 0;
195 high = p->num;
196
197 while (low + 1 < high)
198 {
199 i = (low + high) >> 1;
200 s = strcmp (object, p->elts[i].object);
201 if (s == 0)
202 return &p->elts[i];
203 if (s > 0)
204 low = i;
205 else
206 high = i;
207 }
208
209 i = low;
210 while (i < high)
211 {
212 s = strcmp (object, p->elts[i].object);
213 if (s == 0)
214 return &p->elts[i];
215 if (s < 0)
216 break;
217 i++;
218 }
219
220 if (!insert)
221 return 0;
222
223 /* grow array */
224 if (p->num + 1 >= p->size)
225 {
226 s = p->size + 16;
227 if (p->elts)
228 e = (FcPatternElt *) realloc (p->elts, s * sizeof (FcPatternElt));
229 else
230 e = (FcPatternElt *) malloc (s * sizeof (FcPatternElt));
231 if (!e)
232 return FcFalse;
233 p->elts = e;
234 if (p->size)
235 FcMemFree (FC_MEM_PATELT, p->size * sizeof (FcPatternElt));
236 FcMemAlloc (FC_MEM_PATELT, s * sizeof (FcPatternElt));
237 while (p->size < s)
238 {
239 p->elts[p->size].object = 0;
240 p->elts[p->size].values = 0;
241 p->size++;
242 }
243 }
244
245 /* move elts up */
246 memmove (p->elts + i + 1,
247 p->elts + i,
248 sizeof (FcPatternElt) *
249 (p->num - i));
250
251 /* bump count */
252 p->num++;
253
254 p->elts[i].object = object;
255 p->elts[i].values = 0;
256
257 return &p->elts[i];
258 }
259
260 FcBool
261 FcPatternEqual (FcPattern *pa, FcPattern *pb)
262 {
263 int i;
264
265 if (pa->num != pb->num)
266 return FcFalse;
267 for (i = 0; i < pa->num; i++)
268 {
269 if (strcmp (pa->elts[i].object, pb->elts[i].object) != 0)
270 return FcFalse;
271 if (!FcValueListEqual (pa->elts[i].values, pb->elts[i].values))
272 return FcFalse;
273 }
274 return FcTrue;
275 }
276
277 FcBool
278 FcPatternAdd (FcPattern *p, const char *object, FcValue value, FcBool append)
279 {
280 FcPatternElt *e;
281 FcValueList *new, **prev;
282
283 new = (FcValueList *) malloc (sizeof (FcValueList));
284 if (!new)
285 goto bail0;
286
287 FcMemAlloc (FC_MEM_VALLIST, sizeof (FcValueList));
288 /* dup string */
289 value = FcValueSave (value);
290 if (value.type == FcTypeVoid)
291 goto bail1;
292
293 new->value = value;
294 new->next = 0;
295
296 e = FcPatternFind (p, object, FcTrue);
297 if (!e)
298 goto bail2;
299
300 if (append)
301 {
302 for (prev = &e->values; *prev; prev = &(*prev)->next);
303 *prev = new;
304 }
305 else
306 {
307 new->next = e->values;
308 e->values = new;
309 }
310
311 return FcTrue;
312
313 bail2:
314 switch (value.type) {
315 case FcTypeString:
316 FcStrFree ((FcChar8 *) value.u.s);
317 break;
318 case FcTypeMatrix:
319 FcMatrixFree ((FcMatrix *) value.u.m);
320 break;
321 case FcTypeCharSet:
322 FcCharSetDestroy ((FcCharSet *) value.u.c);
323 break;
324 default:
325 break;
326 }
327 bail1:
328 FcMemFree (FC_MEM_VALLIST, sizeof (FcValueList));
329 free (new);
330 bail0:
331 return FcFalse;
332 }
333
334 FcBool
335 FcPatternDel (FcPattern *p, const char *object)
336 {
337 FcPatternElt *e;
338 int i;
339
340 e = FcPatternFind (p, object, FcFalse);
341 if (!e)
342 return FcFalse;
343
344 i = e - p->elts;
345
346 /* destroy value */
347 FcValueListDestroy (e->values);
348
349 /* shuffle existing ones down */
350 memmove (e, e+1, (p->elts + p->num - (e + 1)) * sizeof (FcPatternElt));
351 p->num--;
352 p->elts[p->num].object = 0;
353 p->elts[p->num].values = 0;
354 return FcTrue;
355 }
356
357 FcBool
358 FcPatternAddInteger (FcPattern *p, const char *object, int i)
359 {
360 FcValue v;
361
362 v.type = FcTypeInteger;
363 v.u.i = i;
364 return FcPatternAdd (p, object, v, FcTrue);
365 }
366
367 FcBool
368 FcPatternAddDouble (FcPattern *p, const char *object, double d)
369 {
370 FcValue v;
371
372 v.type = FcTypeDouble;
373 v.u.d = d;
374 return FcPatternAdd (p, object, v, FcTrue);
375 }
376
377
378 FcBool
379 FcPatternAddString (FcPattern *p, const char *object, const FcChar8 *s)
380 {
381 FcValue v;
382
383 v.type = FcTypeString;
384 v.u.s = s;
385 return FcPatternAdd (p, object, v, FcTrue);
386 }
387
388 FcBool
389 FcPatternAddMatrix (FcPattern *p, const char *object, const FcMatrix *s)
390 {
391 FcValue v;
392
393 v.type = FcTypeMatrix;
394 v.u.m = (FcMatrix *) s;
395 return FcPatternAdd (p, object, v, FcTrue);
396 }
397
398
399 FcBool
400 FcPatternAddBool (FcPattern *p, const char *object, FcBool b)
401 {
402 FcValue v;
403
404 v.type = FcTypeBool;
405 v.u.b = b;
406 return FcPatternAdd (p, object, v, FcTrue);
407 }
408
409 FcBool
410 FcPatternAddCharSet (FcPattern *p, const char *object, const FcCharSet *c)
411 {
412 FcValue v;
413
414 v.type = FcTypeCharSet;
415 v.u.c = (FcCharSet *) c;
416 return FcPatternAdd (p, object, v, FcTrue);
417 }
418
419 FcResult
420 FcPatternGet (FcPattern *p, const char *object, int id, FcValue *v)
421 {
422 FcPatternElt *e;
423 FcValueList *l;
424
425 e = FcPatternFind (p, object, FcFalse);
426 if (!e)
427 return FcResultNoMatch;
428 for (l = e->values; l; l = l->next)
429 {
430 if (!id)
431 {
432 *v = l->value;
433 return FcResultMatch;
434 }
435 id--;
436 }
437 return FcResultNoId;
438 }
439
440 FcResult
441 FcPatternGetInteger (FcPattern *p, const char *object, int id, int *i)
442 {
443 FcValue v;
444 FcResult r;
445
446 r = FcPatternGet (p, object, id, &v);
447 if (r != FcResultMatch)
448 return r;
449 switch (v.type) {
450 case FcTypeDouble:
451 *i = (int) v.u.d;
452 break;
453 case FcTypeInteger:
454 *i = v.u.i;
455 break;
456 default:
457 return FcResultTypeMismatch;
458 }
459 return FcResultMatch;
460 }
461
462 FcResult
463 FcPatternGetDouble (FcPattern *p, const char *object, int id, double *d)
464 {
465 FcValue v;
466 FcResult r;
467
468 r = FcPatternGet (p, object, id, &v);
469 if (r != FcResultMatch)
470 return r;
471 switch (v.type) {
472 case FcTypeDouble:
473 *d = v.u.d;
474 break;
475 case FcTypeInteger:
476 *d = (double) v.u.i;
477 break;
478 default:
479 return FcResultTypeMismatch;
480 }
481 return FcResultMatch;
482 }
483
484 FcResult
485 FcPatternGetString (FcPattern *p, const char *object, int id, FcChar8 ** s)
486 {
487 FcValue v;
488 FcResult r;
489
490 r = FcPatternGet (p, object, id, &v);
491 if (r != FcResultMatch)
492 return r;
493 if (v.type != FcTypeString)
494 return FcResultTypeMismatch;
495 *s = (FcChar8 *) v.u.s;
496 return FcResultMatch;
497 }
498
499 FcResult
500 FcPatternGetMatrix (FcPattern *p, const char *object, int id, FcMatrix **m)
501 {
502 FcValue v;
503 FcResult r;
504
505 r = FcPatternGet (p, object, id, &v);
506 if (r != FcResultMatch)
507 return r;
508 if (v.type != FcTypeMatrix)
509 return FcResultTypeMismatch;
510 *m = (FcMatrix *) v.u.m;
511 return FcResultMatch;
512 }
513
514
515 FcResult
516 FcPatternGetBool (FcPattern *p, const char *object, int id, FcBool *b)
517 {
518 FcValue v;
519 FcResult r;
520
521 r = FcPatternGet (p, object, id, &v);
522 if (r != FcResultMatch)
523 return r;
524 if (v.type != FcTypeBool)
525 return FcResultTypeMismatch;
526 *b = v.u.b;
527 return FcResultMatch;
528 }
529
530 FcResult
531 FcPatternGetCharSet (FcPattern *p, const char *object, int id, FcCharSet **c)
532 {
533 FcValue v;
534 FcResult r;
535
536 r = FcPatternGet (p, object, id, &v);
537 if (r != FcResultMatch)
538 return r;
539 if (v.type != FcTypeCharSet)
540 return FcResultTypeMismatch;
541 *c = (FcCharSet *) v.u.c;
542 return FcResultMatch;
543 }
544
545 FcPattern *
546 FcPatternDuplicate (FcPattern *orig)
547 {
548 FcPattern *new;
549 int i;
550 FcValueList *l;
551
552 new = FcPatternCreate ();
553 if (!new)
554 goto bail0;
555
556 for (i = 0; i < orig->num; i++)
557 {
558 for (l = orig->elts[i].values; l; l = l->next)
559 if (!FcPatternAdd (new, orig->elts[i].object, l->value, FcTrue))
560 goto bail1;
561 }
562
563 return new;
564
565 bail1:
566 FcPatternDestroy (new);
567 bail0:
568 return 0;
569 }
570
571 FcPattern *
572 FcPatternVaBuild (FcPattern *orig, va_list va)
573 {
574 FcPattern *ret;
575
576 FcPatternVapBuild (ret, orig, va);
577 return ret;
578 }
579
580 FcPattern *
581 FcPatternBuild (FcPattern *orig, ...)
582 {
583 va_list va;
584
585 va_start (va, orig);
586 FcPatternVapBuild (orig, orig, va);
587 va_end (va);
588 return orig;
589 }