]> git.wh0rd.org - fontconfig.git/blob - src/fcpat.c
More complete memory tracking. Install always overwrites header files
[fontconfig.git] / src / fcpat.c
1 /*
2 * $XFree86: xc/lib/fontconfig/src/fcpat.c,v 1.15 2002/08/22 07:36:45 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 <assert.h>
28 #include "fcint.h"
29
30 FcPattern *
31 FcPatternCreate (void)
32 {
33 FcPattern *p;
34
35 p = (FcPattern *) malloc (sizeof (FcPattern));
36 if (!p)
37 return 0;
38 FcMemAlloc (FC_MEM_PATTERN, sizeof (FcPattern));
39 p->num = 0;
40 p->size = 0;
41 p->elts = 0;
42 p->ref = 1;
43 return p;
44 }
45
46 void
47 FcValueDestroy (FcValue v)
48 {
49 switch (v.type) {
50 case FcTypeString:
51 FcStrFree ((FcChar8 *) v.u.s);
52 break;
53 case FcTypeMatrix:
54 FcMatrixFree ((FcMatrix *) v.u.m);
55 break;
56 case FcTypeCharSet:
57 FcCharSetDestroy ((FcCharSet *) v.u.c);
58 break;
59 case FcTypeLangSet:
60 FcLangSetDestroy ((FcLangSet *) v.u.l);
61 break;
62 default:
63 break;
64 }
65 }
66
67 FcValue
68 FcValueSave (FcValue v)
69 {
70 switch (v.type) {
71 case FcTypeString:
72 v.u.s = FcStrCopy (v.u.s);
73 if (!v.u.s)
74 v.type = FcTypeVoid;
75 break;
76 case FcTypeMatrix:
77 v.u.m = FcMatrixCopy (v.u.m);
78 if (!v.u.m)
79 v.type = FcTypeVoid;
80 break;
81 case FcTypeCharSet:
82 v.u.c = FcCharSetCopy ((FcCharSet *) v.u.c);
83 if (!v.u.c)
84 v.type = FcTypeVoid;
85 break;
86 case FcTypeLangSet:
87 v.u.l = FcLangSetCopy (v.u.l);
88 if (!v.u.l)
89 v.type = FcTypeVoid;
90 break;
91 default:
92 break;
93 }
94 return v;
95 }
96
97 void
98 FcValueListDestroy (FcValueList *l)
99 {
100 FcValueList *next;
101 for (; l; l = next)
102 {
103 switch (l->value.type) {
104 case FcTypeString:
105 FcStrFree ((FcChar8 *) l->value.u.s);
106 break;
107 case FcTypeMatrix:
108 FcMatrixFree ((FcMatrix *) l->value.u.m);
109 break;
110 case FcTypeCharSet:
111 FcCharSetDestroy ((FcCharSet *) l->value.u.c);
112 break;
113 case FcTypeLangSet:
114 FcLangSetDestroy ((FcLangSet *) l->value.u.l);
115 break;
116 default:
117 break;
118 }
119 next = l->next;
120 FcMemFree (FC_MEM_VALLIST, sizeof (FcValueList));
121 free (l);
122 }
123 }
124
125 FcBool
126 FcValueEqual (FcValue va, FcValue vb)
127 {
128 if (va.type != vb.type)
129 {
130 if (va.type == FcTypeInteger)
131 {
132 va.type = FcTypeDouble;
133 va.u.d = va.u.i;
134 }
135 if (vb.type == FcTypeInteger)
136 {
137 vb.type = FcTypeDouble;
138 vb.u.d = vb.u.i;
139 }
140 if (va.type != vb.type)
141 return FcFalse;
142 }
143 switch (va.type) {
144 case FcTypeVoid:
145 return FcTrue;
146 case FcTypeInteger:
147 return va.u.i == vb.u.i;
148 case FcTypeDouble:
149 return va.u.d == vb.u.d;
150 case FcTypeString:
151 return FcStrCmpIgnoreCase (va.u.s, vb.u.s) == 0;
152 case FcTypeBool:
153 return va.u.b == vb.u.b;
154 case FcTypeMatrix:
155 return FcMatrixEqual (va.u.m, vb.u.m);
156 case FcTypeCharSet:
157 return FcCharSetEqual (va.u.c, vb.u.c);
158 case FcTypeFTFace:
159 return va.u.f == vb.u.f;
160 case FcTypeLangSet:
161 return FcLangSetEqual (va.u.l, vb.u.l);
162 }
163 return FcFalse;
164 }
165
166 static FcChar32
167 FcDoubleHash (double d)
168 {
169 if (d < 0)
170 d = -d;
171 if (d > 0xffffffff)
172 d = 0xffffffff;
173 return (FcChar32) d;
174 }
175
176 static FcChar32
177 FcStringHash (const FcChar8 *s)
178 {
179 FcChar8 c;
180 FcChar32 h = 0;
181
182 if (s)
183 while ((c = *s++))
184 h = ((h << 1) | (h >> 31)) ^ c;
185 return h;
186 }
187
188 static FcChar32
189 FcValueHash (FcValue v)
190 {
191 switch (v.type) {
192 case FcTypeVoid:
193 return 0;
194 case FcTypeInteger:
195 return (FcChar32) v.u.i;
196 case FcTypeDouble:
197 return FcDoubleHash (v.u.d);
198 case FcTypeString:
199 return FcStringHash (v.u.s);
200 case FcTypeBool:
201 return (FcChar32) v.u.b;
202 case FcTypeMatrix:
203 return (FcDoubleHash (v.u.m->xx) ^
204 FcDoubleHash (v.u.m->xy) ^
205 FcDoubleHash (v.u.m->yx) ^
206 FcDoubleHash (v.u.m->yy));
207 case FcTypeCharSet:
208 return (FcChar32) v.u.c->num;
209 case FcTypeFTFace:
210 return FcStringHash ((const FcChar8 *) ((FT_Face) v.u.f)->family_name) ^
211 FcStringHash ((const FcChar8 *) ((FT_Face) v.u.f)->style_name);
212 case FcTypeLangSet:
213 return FcLangSetHash (v.u.l);
214 }
215 return FcFalse;
216 }
217
218 static FcBool
219 FcValueListEqual (FcValueList *la, FcValueList *lb)
220 {
221 if (la == lb)
222 return FcTrue;
223
224 while (la && lb)
225 {
226 if (!FcValueEqual (la->value, lb->value))
227 return FcFalse;
228 la = la->next;
229 lb = lb->next;
230 }
231 if (la || lb)
232 return FcFalse;
233 return FcTrue;
234 }
235
236 static FcChar32
237 FcValueListHash (FcValueList *l)
238 {
239 FcChar32 hash = 0;
240
241 while (l)
242 {
243 hash = ((hash << 1) | (hash >> 31)) ^ FcValueHash (l->value);
244 l = l->next;
245 }
246 return hash;
247 }
248
249 void
250 FcPatternDestroy (FcPattern *p)
251 {
252 int i;
253
254 if (p->ref == FC_REF_CONSTANT || --p->ref > 0)
255 return;
256
257 for (i = 0; i < p->num; i++)
258 FcValueListDestroy (p->elts[i].values);
259
260 p->num = 0;
261 if (p->elts)
262 {
263 FcMemFree (FC_MEM_PATELT, p->size * sizeof (FcPatternElt));
264 free (p->elts);
265 p->elts = 0;
266 }
267 p->size = 0;
268 FcMemFree (FC_MEM_PATTERN, sizeof (FcPattern));
269 free (p);
270 }
271
272 #define FC_VALUE_LIST_HASH_SIZE 257
273 #define FC_PATTERN_HASH_SIZE 67
274
275 typedef struct _FcValueListEnt FcValueListEnt;
276
277 struct _FcValueListEnt {
278 FcValueListEnt *next;
279 FcChar32 hash;
280 FcValueList *list;
281 };
282
283 static int FcValueListFrozenCount[FcTypeLangSet + 1];
284 static int FcValueListFrozenBytes[FcTypeLangSet + 1];
285 static char *FcValueListFrozenName[] = {
286 "Void",
287 "Integer",
288 "Double",
289 "String",
290 "Bool",
291 "Matrix",
292 "CharSet",
293 "FTFace",
294 "LangSet"
295 };
296
297 void
298 FcValueListReport (void);
299
300 void
301 FcValueListReport (void)
302 {
303 FcType t;
304
305 printf ("Fc Frozen Values:\n");
306 printf ("\t%8s %9s %9s\n", "Type", "Count", "Bytes");
307 for (t = FcTypeVoid; t <= FcTypeLangSet; t++)
308 printf ("\t%8s %9d %9d\n", FcValueListFrozenName[t],
309 FcValueListFrozenCount[t], FcValueListFrozenBytes[t]);
310 }
311
312 static FcValueListEnt *
313 FcValueListEntCreate (FcValueList *h)
314 {
315 FcValueListEnt *e;
316 FcValueList *l, *new;
317 int n;
318 int string_size = 0;
319 FcChar8 *strs;
320 int size;
321
322 n = 0;
323 for (l = h; l; l = l->next)
324 {
325 if (l->value.type == FcTypeString)
326 string_size += strlen ((char *) l->value.u.s) + 1;
327 n++;
328 }
329 size = sizeof (FcValueListEnt) + n * sizeof (FcValueList) + string_size;
330 FcValueListFrozenCount[h->value.type]++;
331 FcValueListFrozenBytes[h->value.type] += size;
332 e = malloc (size);
333 if (!e)
334 return 0;
335 FcMemAlloc (FC_MEM_VALLIST, size);
336 e->list = (FcValueList *) (e + 1);
337 strs = (FcChar8 *) (e->list + n);
338 new = e->list;
339 for (l = h; l; l = l->next)
340 {
341 if (l->value.type == FcTypeString)
342 {
343 new->value.type = FcTypeString;
344 new->value.u.s = strs;
345 strcpy ((char *) strs, (char *) l->value.u.s);
346 strs += strlen ((char *) strs) + 1;
347 }
348 else
349 new->value = FcValueSave (l->value);
350 new->binding = l->binding;
351 if (l->next)
352 new->next = new + 1;
353 else
354 new->next = 0;
355 }
356 return e;
357 }
358
359 static int FcValueListTotal;
360 static int FcValueListUsed;
361
362 static FcValueList *
363 FcValueListFreeze (FcValueList *l)
364 {
365 static FcValueListEnt *hashTable[FC_VALUE_LIST_HASH_SIZE];
366 FcChar32 hash = FcValueListHash (l);
367 FcValueListEnt **bucket = &hashTable[hash % FC_VALUE_LIST_HASH_SIZE];
368 FcValueListEnt *ent;
369
370 FcValueListTotal++;
371 for (ent = *bucket; ent; ent = ent->next)
372 {
373 if (ent->hash == hash && FcValueListEqual (ent->list, l))
374 return ent->list;
375 }
376
377 ent = FcValueListEntCreate (l);
378 if (!ent)
379 return 0;
380
381 FcValueListUsed++;
382 ent->hash = hash;
383 ent->next = *bucket;
384 *bucket = ent;
385 return ent->list;
386 }
387
388 static FcChar32
389 FcPatternBaseHash (FcPattern *b)
390 {
391 FcChar32 hash = b->num;
392 int i;
393
394 for (i = 0; i < b->num; i++)
395 hash = ((hash << 1) | (hash >> 31)) ^ ((FcChar32) b->elts[i].values);
396 return hash;
397 }
398
399 typedef struct _FcPatternEnt FcPatternEnt;
400
401 struct _FcPatternEnt {
402 FcPatternEnt *next;
403 FcChar32 hash;
404 FcPattern pattern;
405 };
406
407 static int FcPatternTotal;
408 static int FcPatternUsed;
409
410 static FcPattern *
411 FcPatternBaseFreeze (FcPattern *b)
412 {
413 static FcPatternEnt *hashTable[FC_VALUE_LIST_HASH_SIZE];
414 FcChar32 hash = FcPatternBaseHash (b);
415 FcPatternEnt **bucket = &hashTable[hash % FC_VALUE_LIST_HASH_SIZE];
416 FcPatternEnt *ent;
417 int i;
418 char *objects;
419 int size_objects;
420 int size;
421
422 FcPatternTotal++;
423 for (ent = *bucket; ent; ent = ent->next)
424 {
425 if (ent->hash == hash && b->num == ent->pattern.num)
426 {
427 for (i = 0; i < b->num; i++)
428 {
429 if (strcmp (b->elts[i].object, ent->pattern.elts[i].object))
430 break;
431 if (b->elts[i].values != ent->pattern.elts[i].values)
432 break;
433 }
434 if (i == b->num)
435 return &ent->pattern;
436 }
437 }
438
439 /*
440 * Compute size of pattern + elts + object names
441 */
442 size_objects = 0;
443 for (i = 0; i < b->num; i++)
444 size_objects += strlen (b->elts[i].object) + 1;
445
446 size = sizeof (FcPatternEnt) + b->num*sizeof (FcPatternElt) + size_objects;
447 ent = malloc (size);
448 if (!ent)
449 return 0;
450
451 FcMemAlloc (FC_MEM_PATTERN, size);
452 FcPatternUsed++;
453
454 ent->pattern.elts = (FcPatternElt *) (ent + 1);
455 ent->pattern.num = b->num;
456 ent->pattern.size = b->num;
457 ent->pattern.ref = FC_REF_CONSTANT;
458
459 objects = (char *) (ent->pattern.elts + b->num);
460 for (i = 0; i < b->num; i++)
461 {
462 ent->pattern.elts[i].values = b->elts[i].values;
463 strcpy (objects, b->elts[i].object);
464 ent->pattern.elts[i].object = objects;
465 objects += strlen (objects) + 1;
466 }
467
468 ent->hash = hash;
469 ent->next = *bucket;
470 *bucket = ent;
471 return &ent->pattern;
472 }
473
474 FcPattern *
475 FcPatternFreeze (FcPattern *p)
476 {
477 FcPattern *b, *n = 0;
478 int size;
479 int i;
480
481 size = sizeof (FcPattern) + p->num * sizeof (FcPatternElt);
482 b = (FcPattern *) malloc (size);
483 if (!b)
484 return 0;
485 FcMemAlloc (FC_MEM_PATTERN, size);
486 b->num = p->num;
487 b->size = b->num;
488 b->ref = 1;
489 b->elts = (FcPatternElt *) (b + 1);
490 /*
491 * Freeze object lists
492 */
493 for (i = 0; i < p->num; i++)
494 {
495 b->elts[i].object = p->elts[i].object;
496 b->elts[i].values = FcValueListFreeze (p->elts[i].values);
497 if (!b->elts[i].values)
498 goto bail;
499 }
500 /*
501 * Freeze base
502 */
503 n = FcPatternBaseFreeze (b);
504 #ifdef CHATTY
505 if (FcDebug() & FC_DBG_MEMORY)
506 {
507 printf ("ValueLists: total %9d used %9d\n", FcValueListTotal, FcValueListUsed);
508 printf ("Patterns: total %9d used %9d\n", FcPatternTotal, FcPatternUsed);
509 }
510 #endif
511 bail:
512 free (b);
513 #ifdef DEBUG
514 assert (FcPatternEqual (n, p));
515 #endif
516 return n;
517 }
518
519 static int
520 FcPatternPosition (const FcPattern *p, const char *object)
521 {
522 int low, high, mid, c;
523
524 low = 0;
525 high = p->num - 1;
526 c = 1;
527 mid = 0;
528 while (low <= high)
529 {
530 mid = (low + high) >> 1;
531 c = strcmp (p->elts[mid].object, object);
532 if (c == 0)
533 return mid;
534 if (c < 0)
535 low = mid + 1;
536 else
537 high = mid - 1;
538 }
539 if (c < 0)
540 mid++;
541 return -(mid + 1);
542 }
543
544 FcPatternElt *
545 FcPatternFindElt (const FcPattern *p, const char *object)
546 {
547 int i = FcPatternPosition (p, object);
548 if (i < 0)
549 return 0;
550 return &p->elts[i];
551 }
552
553 FcPatternElt *
554 FcPatternInsertElt (FcPattern *p, const char *object)
555 {
556 int i;
557 FcPatternElt *e;
558
559 i = FcPatternPosition (p, object);
560 if (i < 0)
561 {
562 i = -i - 1;
563
564 /* grow array */
565 if (p->num + 1 >= p->size)
566 {
567 int s = p->size + 16;
568 if (p->elts)
569 e = (FcPatternElt *) realloc (p->elts, s * sizeof (FcPatternElt));
570 else
571 e = (FcPatternElt *) malloc (s * sizeof (FcPatternElt));
572 if (!e)
573 return FcFalse;
574 p->elts = e;
575 if (p->size)
576 FcMemFree (FC_MEM_PATELT, p->size * sizeof (FcPatternElt));
577 FcMemAlloc (FC_MEM_PATELT, s * sizeof (FcPatternElt));
578 while (p->size < s)
579 {
580 p->elts[p->size].object = 0;
581 p->elts[p->size].values = 0;
582 p->size++;
583 }
584 }
585
586 /* move elts up */
587 memmove (p->elts + i + 1,
588 p->elts + i,
589 sizeof (FcPatternElt) *
590 (p->num - i));
591
592 /* bump count */
593 p->num++;
594
595 p->elts[i].object = object;
596 p->elts[i].values = 0;
597 }
598
599 return &p->elts[i];
600 }
601
602 FcBool
603 FcPatternEqual (const FcPattern *pa, const FcPattern *pb)
604 {
605 int i;
606
607 if (pa == pb)
608 return FcTrue;
609
610 if (pa->num != pb->num)
611 return FcFalse;
612 for (i = 0; i < pa->num; i++)
613 {
614 if (strcmp (pa->elts[i].object, pb->elts[i].object) != 0)
615 return FcFalse;
616 if (!FcValueListEqual (pa->elts[i].values, pb->elts[i].values))
617 return FcFalse;
618 }
619 return FcTrue;
620 }
621
622 FcChar32
623 FcPatternHash (const FcPattern *p)
624 {
625 int i;
626 FcChar32 h = 0;
627
628 for (i = 0; i < p->num; i++)
629 {
630 h = (((h << 1) | (h >> 31)) ^
631 FcStringHash ((const FcChar8 *) p->elts[i].object) ^
632 FcValueListHash (p->elts[i].values));
633 }
634 return h;
635 }
636
637 FcBool
638 FcPatternEqualSubset (const FcPattern *pa, const FcPattern *pb, const FcObjectSet *os)
639 {
640 FcPatternElt *ea, *eb;
641 int i;
642
643 for (i = 0; i < os->nobject; i++)
644 {
645 ea = FcPatternFindElt (pa, os->objects[i]);
646 eb = FcPatternFindElt (pb, os->objects[i]);
647 if (ea)
648 {
649 if (!eb)
650 return FcFalse;
651 if (!FcValueListEqual (ea->values, eb->values))
652 return FcFalse;
653 }
654 else
655 {
656 if (eb)
657 return FcFalse;
658 }
659 }
660 return FcTrue;
661 }
662
663 FcBool
664 FcPatternAddWithBinding (FcPattern *p,
665 const char *object,
666 FcValue value,
667 FcValueBinding binding,
668 FcBool append)
669 {
670 FcPatternElt *e;
671 FcValueList *new, **prev;
672
673 if (p->ref == FC_REF_CONSTANT)
674 goto bail0;
675
676 new = (FcValueList *) malloc (sizeof (FcValueList));
677 if (!new)
678 goto bail0;
679
680 FcMemAlloc (FC_MEM_VALLIST, sizeof (FcValueList));
681 /* dup string */
682 value = FcValueSave (value);
683 if (value.type == FcTypeVoid)
684 goto bail1;
685
686 new->value = value;
687 new->binding = binding;
688 new->next = 0;
689
690 e = FcPatternInsertElt (p, object);
691 if (!e)
692 goto bail2;
693
694 if (append)
695 {
696 for (prev = &e->values; *prev; prev = &(*prev)->next);
697 *prev = new;
698 }
699 else
700 {
701 new->next = e->values;
702 e->values = new;
703 }
704
705 return FcTrue;
706
707 bail2:
708 switch (value.type) {
709 case FcTypeString:
710 FcStrFree ((FcChar8 *) value.u.s);
711 break;
712 case FcTypeMatrix:
713 FcMatrixFree ((FcMatrix *) value.u.m);
714 break;
715 case FcTypeCharSet:
716 FcCharSetDestroy ((FcCharSet *) value.u.c);
717 break;
718 case FcTypeLangSet:
719 FcLangSetDestroy ((FcLangSet *) value.u.l);
720 break;
721 default:
722 break;
723 }
724 bail1:
725 FcMemFree (FC_MEM_VALLIST, sizeof (FcValueList));
726 free (new);
727 bail0:
728 return FcFalse;
729 }
730
731 FcBool
732 FcPatternAdd (FcPattern *p, const char *object, FcValue value, FcBool append)
733 {
734 return FcPatternAddWithBinding (p, object, value, FcValueBindingStrong, append);
735 }
736
737 FcBool
738 FcPatternAddWeak (FcPattern *p, const char *object, FcValue value, FcBool append)
739 {
740 return FcPatternAddWithBinding (p, object, value, FcValueBindingWeak, append);
741 }
742
743 FcBool
744 FcPatternDel (FcPattern *p, const char *object)
745 {
746 FcPatternElt *e;
747 int i;
748
749 e = FcPatternFindElt (p, object);
750 if (!e)
751 return FcFalse;
752
753 i = e - p->elts;
754
755 /* destroy value */
756 FcValueListDestroy (e->values);
757
758 /* shuffle existing ones down */
759 memmove (e, e+1, (p->elts + p->num - (e + 1)) * sizeof (FcPatternElt));
760 p->num--;
761 p->elts[p->num].object = 0;
762 p->elts[p->num].values = 0;
763 return FcTrue;
764 }
765
766 FcBool
767 FcPatternAddInteger (FcPattern *p, const char *object, int i)
768 {
769 FcValue v;
770
771 v.type = FcTypeInteger;
772 v.u.i = i;
773 return FcPatternAdd (p, object, v, FcTrue);
774 }
775
776 FcBool
777 FcPatternAddDouble (FcPattern *p, const char *object, double d)
778 {
779 FcValue v;
780
781 v.type = FcTypeDouble;
782 v.u.d = d;
783 return FcPatternAdd (p, object, v, FcTrue);
784 }
785
786
787 FcBool
788 FcPatternAddString (FcPattern *p, const char *object, const FcChar8 *s)
789 {
790 FcValue v;
791
792 v.type = FcTypeString;
793 v.u.s = s;
794 return FcPatternAdd (p, object, v, FcTrue);
795 }
796
797 FcBool
798 FcPatternAddMatrix (FcPattern *p, const char *object, const FcMatrix *s)
799 {
800 FcValue v;
801
802 v.type = FcTypeMatrix;
803 v.u.m = (FcMatrix *) s;
804 return FcPatternAdd (p, object, v, FcTrue);
805 }
806
807
808 FcBool
809 FcPatternAddBool (FcPattern *p, const char *object, FcBool b)
810 {
811 FcValue v;
812
813 v.type = FcTypeBool;
814 v.u.b = b;
815 return FcPatternAdd (p, object, v, FcTrue);
816 }
817
818 FcBool
819 FcPatternAddCharSet (FcPattern *p, const char *object, const FcCharSet *c)
820 {
821 FcValue v;
822
823 v.type = FcTypeCharSet;
824 v.u.c = (FcCharSet *) c;
825 return FcPatternAdd (p, object, v, FcTrue);
826 }
827
828 FcBool
829 FcPatternAddFTFace (FcPattern *p, const char *object, const FT_Face f)
830 {
831 FcValue v;
832
833 v.type = FcTypeFTFace;
834 v.u.f = (void *) f;
835 return FcPatternAdd (p, object, v, FcTrue);
836 }
837
838 FcBool
839 FcPatternAddLangSet (FcPattern *p, const char *object, const FcLangSet *ls)
840 {
841 FcValue v;
842
843 v.type = FcTypeLangSet;
844 v.u.l = (FcLangSet *) ls;
845 return FcPatternAdd (p, object, v, FcTrue);
846 }
847
848 FcResult
849 FcPatternGet (FcPattern *p, const char *object, int id, FcValue *v)
850 {
851 FcPatternElt *e;
852 FcValueList *l;
853
854 e = FcPatternFindElt (p, object);
855 if (!e)
856 return FcResultNoMatch;
857 for (l = e->values; l; l = l->next)
858 {
859 if (!id)
860 {
861 *v = l->value;
862 return FcResultMatch;
863 }
864 id--;
865 }
866 return FcResultNoId;
867 }
868
869 FcResult
870 FcPatternGetInteger (FcPattern *p, const char *object, int id, int *i)
871 {
872 FcValue v;
873 FcResult r;
874
875 r = FcPatternGet (p, object, id, &v);
876 if (r != FcResultMatch)
877 return r;
878 switch (v.type) {
879 case FcTypeDouble:
880 *i = (int) v.u.d;
881 break;
882 case FcTypeInteger:
883 *i = v.u.i;
884 break;
885 default:
886 return FcResultTypeMismatch;
887 }
888 return FcResultMatch;
889 }
890
891 FcResult
892 FcPatternGetDouble (FcPattern *p, const char *object, int id, double *d)
893 {
894 FcValue v;
895 FcResult r;
896
897 r = FcPatternGet (p, object, id, &v);
898 if (r != FcResultMatch)
899 return r;
900 switch (v.type) {
901 case FcTypeDouble:
902 *d = v.u.d;
903 break;
904 case FcTypeInteger:
905 *d = (double) v.u.i;
906 break;
907 default:
908 return FcResultTypeMismatch;
909 }
910 return FcResultMatch;
911 }
912
913 FcResult
914 FcPatternGetString (FcPattern *p, const char *object, int id, FcChar8 ** s)
915 {
916 FcValue v;
917 FcResult r;
918
919 r = FcPatternGet (p, object, id, &v);
920 if (r != FcResultMatch)
921 return r;
922 if (v.type != FcTypeString)
923 return FcResultTypeMismatch;
924 *s = (FcChar8 *) v.u.s;
925 return FcResultMatch;
926 }
927
928 FcResult
929 FcPatternGetMatrix (FcPattern *p, const char *object, int id, FcMatrix **m)
930 {
931 FcValue v;
932 FcResult r;
933
934 r = FcPatternGet (p, object, id, &v);
935 if (r != FcResultMatch)
936 return r;
937 if (v.type != FcTypeMatrix)
938 return FcResultTypeMismatch;
939 *m = (FcMatrix *) v.u.m;
940 return FcResultMatch;
941 }
942
943
944 FcResult
945 FcPatternGetBool (FcPattern *p, const char *object, int id, FcBool *b)
946 {
947 FcValue v;
948 FcResult r;
949
950 r = FcPatternGet (p, object, id, &v);
951 if (r != FcResultMatch)
952 return r;
953 if (v.type != FcTypeBool)
954 return FcResultTypeMismatch;
955 *b = v.u.b;
956 return FcResultMatch;
957 }
958
959 FcResult
960 FcPatternGetCharSet (FcPattern *p, const char *object, int id, FcCharSet **c)
961 {
962 FcValue v;
963 FcResult r;
964
965 r = FcPatternGet (p, object, id, &v);
966 if (r != FcResultMatch)
967 return r;
968 if (v.type != FcTypeCharSet)
969 return FcResultTypeMismatch;
970 *c = (FcCharSet *) v.u.c;
971 return FcResultMatch;
972 }
973
974 FcResult
975 FcPatternGetFTFace (FcPattern *p, const char *object, int id, FT_Face *f)
976 {
977 FcValue v;
978 FcResult r;
979
980 r = FcPatternGet (p, object, id, &v);
981 if (r != FcResultMatch)
982 return r;
983 if (v.type != FcTypeFTFace)
984 return FcResultTypeMismatch;
985 *f = (FT_Face) v.u.f;
986 return FcResultMatch;
987 }
988
989 FcResult
990 FcPatternGetLangSet (FcPattern *p, const char *object, int id, FcLangSet **ls)
991 {
992 FcValue v;
993 FcResult r;
994
995 r = FcPatternGet (p, object, id, &v);
996 if (r != FcResultMatch)
997 return r;
998 if (v.type != FcTypeLangSet)
999 return FcResultTypeMismatch;
1000 *ls = (FcLangSet *) v.u.l;
1001 return FcResultMatch;
1002 }
1003
1004 FcPattern *
1005 FcPatternDuplicate (FcPattern *orig)
1006 {
1007 FcPattern *new;
1008 int i;
1009 FcValueList *l;
1010
1011 new = FcPatternCreate ();
1012 if (!new)
1013 goto bail0;
1014
1015 for (i = 0; i < orig->num; i++)
1016 {
1017 for (l = orig->elts[i].values; l; l = l->next)
1018 if (!FcPatternAdd (new, orig->elts[i].object, l->value, FcTrue))
1019 goto bail1;
1020 }
1021
1022 return new;
1023
1024 bail1:
1025 FcPatternDestroy (new);
1026 bail0:
1027 return 0;
1028 }
1029
1030 void
1031 FcPatternReference (FcPattern *p)
1032 {
1033 if (p->ref != FC_REF_CONSTANT)
1034 p->ref++;
1035 }
1036
1037 FcPattern *
1038 FcPatternVaBuild (FcPattern *orig, va_list va)
1039 {
1040 FcPattern *ret;
1041
1042 FcPatternVapBuild (ret, orig, va);
1043 return ret;
1044 }
1045
1046 FcPattern *
1047 FcPatternBuild (FcPattern *orig, ...)
1048 {
1049 va_list va;
1050
1051 va_start (va, orig);
1052 FcPatternVapBuild (orig, orig, va);
1053 va_end (va);
1054 return orig;
1055 }