]> git.wh0rd.org - fontconfig.git/blob - src/fcpat.c
9cd01a02a9303c10ed0d9c578ed850b5d7df8dda
[fontconfig.git] / src / fcpat.c
1 /*
2 * Copyright © 2000 Keith Packard
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name of Keith Packard not be used in
9 * advertising or publicity pertaining to distribution of the software without
10 * specific, written prior permission. Keith Packard makes no
11 * representations about the suitability of this software for any purpose. It
12 * is provided "as is" without express or implied warranty.
13 *
14 * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
20 * PERFORMANCE OF THIS SOFTWARE.
21 */
22
23 #include "fcint.h"
24 #include <stdlib.h>
25 #include <string.h>
26 #include <assert.h>
27
28 static FcBool
29 FcStrHashed (const FcChar8 *name);
30
31 FcPattern *
32 FcPatternCreate (void)
33 {
34 FcPattern *p;
35
36 p = (FcPattern *) malloc (sizeof (FcPattern));
37 if (!p)
38 return 0;
39 FcMemAlloc (FC_MEM_PATTERN, sizeof (FcPattern));
40 p->num = 0;
41 p->size = 0;
42 p->elts_offset = FcPtrToOffset (p, NULL);
43 p->ref = 1;
44 return p;
45 }
46
47 void
48 FcValueDestroy (FcValue v)
49 {
50 switch (v.type) {
51 case FcTypeString:
52 if (!FcStrHashed (v.u.s))
53 FcStrFree ((FcChar8 *) v.u.s);
54 break;
55 case FcTypeMatrix:
56 FcMatrixFree ((FcMatrix *) v.u.m);
57 break;
58 case FcTypeCharSet:
59 FcCharSetDestroy ((FcCharSet *) v.u.c);
60 break;
61 case FcTypeLangSet:
62 FcLangSetDestroy ((FcLangSet *) v.u.l);
63 break;
64 default:
65 break;
66 }
67 }
68
69 FcValue
70 FcValueCanonicalize (const FcValue *v)
71 {
72 FcValue new;
73
74 switch (v->type)
75 {
76 case FcTypeString:
77 new.u.s = fc_value_string(v);
78 new.type = FcTypeString;
79 break;
80 case FcTypeCharSet:
81 new.u.c = fc_value_charset(v);
82 new.type = FcTypeCharSet;
83 break;
84 case FcTypeLangSet:
85 new.u.l = fc_value_langset(v);
86 new.type = FcTypeLangSet;
87 break;
88 default:
89 new = *v;
90 break;
91 }
92 return new;
93 }
94
95 FcValue
96 FcValueSave (FcValue v)
97 {
98 switch (v.type) {
99 case FcTypeString:
100 v.u.s = FcStrCopy (v.u.s);
101 if (!v.u.s)
102 v.type = FcTypeVoid;
103 break;
104 case FcTypeMatrix:
105 v.u.m = FcMatrixCopy (v.u.m);
106 if (!v.u.m)
107 v.type = FcTypeVoid;
108 break;
109 case FcTypeCharSet:
110 v.u.c = FcCharSetCopy ((FcCharSet *) v.u.c);
111 if (!v.u.c)
112 v.type = FcTypeVoid;
113 break;
114 case FcTypeLangSet:
115 v.u.l = FcLangSetCopy (v.u.l);
116 if (!v.u.l)
117 v.type = FcTypeVoid;
118 break;
119 default:
120 break;
121 }
122 return v;
123 }
124
125 void
126 FcValueListDestroy (FcValueListPtr l)
127 {
128 FcValueListPtr next;
129 for (; l; l = next)
130 {
131 switch (l->value.type) {
132 case FcTypeString:
133 if (!FcStrHashed ((FcChar8 *)l->value.u.s))
134 FcStrFree ((FcChar8 *)l->value.u.s);
135 break;
136 case FcTypeMatrix:
137 FcMatrixFree ((FcMatrix *)l->value.u.m);
138 break;
139 case FcTypeCharSet:
140 FcCharSetDestroy
141 ((FcCharSet *) (l->value.u.c));
142 break;
143 case FcTypeLangSet:
144 FcLangSetDestroy
145 ((FcLangSet *) (l->value.u.l));
146 break;
147 default:
148 break;
149 }
150 next = FcValueListNext(l);
151 FcMemFree (FC_MEM_VALLIST, sizeof (FcValueList));
152 free(l);
153 }
154 }
155
156 FcBool
157 FcValueEqual (FcValue va, FcValue vb)
158 {
159 if (va.type != vb.type)
160 {
161 if (va.type == FcTypeInteger)
162 {
163 va.type = FcTypeDouble;
164 va.u.d = va.u.i;
165 }
166 if (vb.type == FcTypeInteger)
167 {
168 vb.type = FcTypeDouble;
169 vb.u.d = vb.u.i;
170 }
171 if (va.type != vb.type)
172 return FcFalse;
173 }
174 switch (va.type) {
175 case FcTypeVoid:
176 return FcTrue;
177 case FcTypeInteger:
178 return va.u.i == vb.u.i;
179 case FcTypeDouble:
180 return va.u.d == vb.u.d;
181 case FcTypeString:
182 return FcStrCmpIgnoreCase (va.u.s, vb.u.s) == 0;
183 case FcTypeBool:
184 return va.u.b == vb.u.b;
185 case FcTypeMatrix:
186 return FcMatrixEqual (va.u.m, vb.u.m);
187 case FcTypeCharSet:
188 return FcCharSetEqual (va.u.c, vb.u.c);
189 case FcTypeFTFace:
190 return va.u.f == vb.u.f;
191 case FcTypeLangSet:
192 return FcLangSetEqual (va.u.l, vb.u.l);
193 }
194 return FcFalse;
195 }
196
197 static FcChar32
198 FcDoubleHash (double d)
199 {
200 if (d < 0)
201 d = -d;
202 if (d > 0xffffffff)
203 d = 0xffffffff;
204 return (FcChar32) d;
205 }
206
207 FcChar32
208 FcStringHash (const FcChar8 *s)
209 {
210 FcChar8 c;
211 FcChar32 h = 0;
212
213 if (s)
214 while ((c = *s++))
215 h = ((h << 1) | (h >> 31)) ^ c;
216 return h;
217 }
218
219 static FcChar32
220 FcValueHash (const FcValue *v)
221 {
222 switch (fc_storage_type(v)) {
223 case FcTypeVoid:
224 return 0;
225 case FcTypeInteger:
226 return (FcChar32) v->u.i;
227 case FcTypeDouble:
228 return FcDoubleHash (v->u.d);
229 case FcTypeString:
230 return FcStringHash (fc_value_string(v));
231 case FcTypeBool:
232 return (FcChar32) v->u.b;
233 case FcTypeMatrix:
234 return (FcDoubleHash (v->u.m->xx) ^
235 FcDoubleHash (v->u.m->xy) ^
236 FcDoubleHash (v->u.m->yx) ^
237 FcDoubleHash (v->u.m->yy));
238 case FcTypeCharSet:
239 return (FcChar32) fc_value_charset(v)->num;
240 case FcTypeFTFace:
241 return FcStringHash ((const FcChar8 *) ((FT_Face) v->u.f)->family_name) ^
242 FcStringHash ((const FcChar8 *) ((FT_Face) v->u.f)->style_name);
243 case FcTypeLangSet:
244 return FcLangSetHash (fc_value_langset(v));
245 }
246 return FcFalse;
247 }
248
249 static FcBool
250 FcValueListEqual (FcValueListPtr la, FcValueListPtr lb)
251 {
252 if (la == lb)
253 return FcTrue;
254
255 while (la && lb)
256 {
257 if (!FcValueEqual (la->value, lb->value))
258 return FcFalse;
259 la = FcValueListNext(la);
260 lb = FcValueListNext(lb);
261 }
262 if (la || lb)
263 return FcFalse;
264 return FcTrue;
265 }
266
267 static FcChar32
268 FcValueListHash (FcValueListPtr l)
269 {
270 FcChar32 hash = 0;
271
272 for (; l; l = FcValueListNext(l))
273 {
274 hash = ((hash << 1) | (hash >> 31)) ^ FcValueHash (&l->value);
275 }
276 return hash;
277 }
278
279 void
280 FcPatternDestroy (FcPattern *p)
281 {
282 int i;
283 FcPatternElt *elts;
284
285 if (p->ref == FC_REF_CONSTANT || --p->ref > 0)
286 return;
287
288 elts = FcPatternElts (p);
289 for (i = 0; i < p->num; i++)
290 FcValueListDestroy (FcPatternEltValues(&elts[i]));
291
292 FcMemFree (FC_MEM_PATELT, p->size * sizeof (FcPatternElt));
293 free (elts);
294 FcMemFree (FC_MEM_PATTERN, sizeof (FcPattern));
295 free (p);
296 }
297
298 static int
299 FcPatternObjectPosition (const FcPattern *p, FcObject object)
300 {
301 int low, high, mid, c;
302 FcPatternElt *elts = FcPatternElts(p);
303
304 low = 0;
305 high = p->num - 1;
306 c = 1;
307 mid = 0;
308 while (low <= high)
309 {
310 mid = (low + high) >> 1;
311 c = elts[mid].object - object;
312 if (c == 0)
313 return mid;
314 if (c < 0)
315 low = mid + 1;
316 else
317 high = mid - 1;
318 }
319 if (c < 0)
320 mid++;
321 return -(mid + 1);
322 }
323
324 FcPatternElt *
325 FcPatternObjectFindElt (const FcPattern *p, FcObject object)
326 {
327 int i = FcPatternObjectPosition (p, object);
328 if (i < 0)
329 return 0;
330 return &FcPatternElts(p)[i];
331 }
332
333 FcPatternElt *
334 FcPatternObjectInsertElt (FcPattern *p, FcObject object)
335 {
336 int i;
337 FcPatternElt *e;
338
339 i = FcPatternObjectPosition (p, object);
340 if (i < 0)
341 {
342 i = -i - 1;
343
344 /* reallocate array */
345 if (p->num + 1 >= p->size)
346 {
347 int s = p->size + 16;
348 if (p->size)
349 {
350 FcPatternElt *e0 = FcPatternElts(p);
351 e = (FcPatternElt *) realloc (e0, s * sizeof (FcPatternElt));
352 if (!e) /* maybe it was mmapped */
353 {
354 e = malloc(s * sizeof (FcPatternElt));
355 if (e)
356 memcpy(e, e0, p->num * sizeof (FcPatternElt));
357 }
358 }
359 else
360 e = (FcPatternElt *) malloc (s * sizeof (FcPatternElt));
361 if (!e)
362 return FcFalse;
363 p->elts_offset = FcPtrToOffset (p, e);
364 if (p->size)
365 FcMemFree (FC_MEM_PATELT, p->size * sizeof (FcPatternElt));
366 FcMemAlloc (FC_MEM_PATELT, s * sizeof (FcPatternElt));
367 while (p->size < s)
368 {
369 e[p->size].object = 0;
370 e[p->size].values = NULL;
371 p->size++;
372 }
373 }
374
375 e = FcPatternElts(p);
376 /* move elts up */
377 memmove (e + i + 1,
378 e + i,
379 sizeof (FcPatternElt) *
380 (p->num - i));
381
382 /* bump count */
383 p->num++;
384
385 e[i].object = object;
386 e[i].values = NULL;
387 }
388
389 return FcPatternElts(p) + i;
390 }
391
392 FcBool
393 FcPatternEqual (const FcPattern *pa, const FcPattern *pb)
394 {
395 int i;
396 FcPatternElt *pae, *pbe;
397
398 if (pa == pb)
399 return FcTrue;
400
401 if (pa->num != pb->num)
402 return FcFalse;
403 pae = FcPatternElts(pa);
404 pbe = FcPatternElts(pb);
405 for (i = 0; i < pa->num; i++)
406 {
407 if (pae[i].object != pbe[i].object)
408 return FcFalse;
409 if (!FcValueListEqual (FcPatternEltValues(&pae[i]),
410 FcPatternEltValues(&pbe[i])))
411 return FcFalse;
412 }
413 return FcTrue;
414 }
415
416 FcChar32
417 FcPatternHash (const FcPattern *p)
418 {
419 int i;
420 FcChar32 h = 0;
421 FcPatternElt *pe = FcPatternElts(p);
422
423 for (i = 0; i < p->num; i++)
424 {
425 h = (((h << 1) | (h >> 31)) ^
426 pe[i].object ^
427 FcValueListHash (FcPatternEltValues(&pe[i])));
428 }
429 return h;
430 }
431
432 FcBool
433 FcPatternEqualSubset (const FcPattern *pai, const FcPattern *pbi, const FcObjectSet *os)
434 {
435 FcPatternElt *ea, *eb;
436 int i;
437
438 for (i = 0; i < os->nobject; i++)
439 {
440 FcObject object = FcObjectFromName (os->objects[i]);
441 ea = FcPatternObjectFindElt (pai, object);
442 eb = FcPatternObjectFindElt (pbi, object);
443 if (ea)
444 {
445 if (!eb)
446 return FcFalse;
447 if (!FcValueListEqual (FcPatternEltValues(ea), FcPatternEltValues(eb)))
448 return FcFalse;
449 }
450 else
451 {
452 if (eb)
453 return FcFalse;
454 }
455 }
456 return FcTrue;
457 }
458
459 FcBool
460 FcPatternObjectAddWithBinding (FcPattern *p,
461 FcObject object,
462 FcValue value,
463 FcValueBinding binding,
464 FcBool append)
465 {
466 FcPatternElt *e;
467 FcValueListPtr new, *prev;
468
469 if (p->ref == FC_REF_CONSTANT)
470 goto bail0;
471
472 new = malloc (sizeof (FcValueList));
473 if (!new)
474 goto bail0;
475
476 memset(new, 0, sizeof (FcValueList));
477 FcMemAlloc (FC_MEM_VALLIST, sizeof (FcValueList));
478 /* dup string */
479 if (value.type == FcTypeString)
480 {
481 value.u.s = FcStrStaticName (value.u.s);
482 if (!value.u.s)
483 value.type = FcTypeVoid;
484 }
485 else
486 value = FcValueSave (value);
487 if (value.type == FcTypeVoid)
488 goto bail1;
489
490 /*
491 * Make sure the stored type is valid for built-in objects
492 */
493 if (!FcObjectValidType (object, value.type))
494 {
495 if (FcDebug() & FC_DBG_OBJTYPES)
496 {
497 printf ("FcPattern object %s does not accept value ",
498 FcObjectName (object));
499 FcValuePrint (value);
500 }
501 goto bail1;
502 }
503
504 new->value = value;
505 new->binding = binding;
506 new->next = NULL;
507
508 e = FcPatternObjectInsertElt (p, object);
509 if (!e)
510 goto bail2;
511
512 if (append)
513 {
514 for (prev = &e->values; *prev; prev = &(*prev)->next)
515 ;
516 *prev = new;
517 }
518 else
519 {
520 new->next = e->values;
521 e->values = new;
522 }
523
524 return FcTrue;
525
526 bail2:
527 FcValueDestroy (value);
528 bail1:
529 FcMemFree (FC_MEM_VALLIST, sizeof (FcValueList));
530 free (new);
531 bail0:
532 return FcFalse;
533 }
534
535 FcBool
536 FcPatternObjectAdd (FcPattern *p, FcObject object, FcValue value, FcBool append)
537 {
538 return FcPatternObjectAddWithBinding (p, object,
539 value, FcValueBindingStrong, append);
540 }
541
542 FcBool
543 FcPatternAdd (FcPattern *p, const char *object, FcValue value, FcBool append)
544 {
545 return FcPatternObjectAddWithBinding (p, FcObjectFromName (object),
546 value, FcValueBindingStrong, append);
547 }
548
549 FcBool
550 FcPatternAddWeak (FcPattern *p, const char *object, FcValue value, FcBool append)
551 {
552 return FcPatternObjectAddWithBinding (p, FcObjectFromName (object),
553 value, FcValueBindingWeak, append);
554 }
555
556 FcBool
557 FcPatternObjectDel (FcPattern *p, FcObject object)
558 {
559 FcPatternElt *e;
560
561 e = FcPatternObjectFindElt (p, object);
562 if (!e)
563 return FcFalse;
564
565 /* destroy value */
566 FcValueListDestroy (e->values);
567
568 /* shuffle existing ones down */
569 memmove (e, e+1,
570 (FcPatternElts(p) + p->num - (e + 1)) *
571 sizeof (FcPatternElt));
572 p->num--;
573 e = FcPatternElts(p) + p->num;
574 e->object = 0;
575 e->values = NULL;
576 return FcTrue;
577 }
578
579 FcBool
580 FcPatternDel (FcPattern *p, const char *object)
581 {
582 return FcPatternObjectDel (p, FcObjectFromName (object));
583 }
584
585 FcBool
586 FcPatternRemove (FcPattern *p, const char *object, int id)
587 {
588 FcPatternElt *e;
589 FcValueListPtr *prev, l;
590
591 e = FcPatternObjectFindElt (p, FcObjectFromName (object));
592 if (!e)
593 return FcFalse;
594 for (prev = &e->values; (l = *prev); prev = &l->next)
595 {
596 if (!id)
597 {
598 *prev = l->next;
599 l->next = NULL;
600 FcValueListDestroy (l);
601 if (!e->values)
602 FcPatternDel (p, object);
603 return FcTrue;
604 }
605 id--;
606 }
607 return FcFalse;
608 }
609
610 FcBool
611 FcPatternObjectAddInteger (FcPattern *p, FcObject object, int i)
612 {
613 FcValue v;
614
615 v.type = FcTypeInteger;
616 v.u.i = i;
617 return FcPatternObjectAdd (p, object, v, FcTrue);
618 }
619
620 FcBool
621 FcPatternAddInteger (FcPattern *p, const char *object, int i)
622 {
623 return FcPatternObjectAddInteger (p, FcObjectFromName (object), i);
624 }
625
626 FcBool
627 FcPatternObjectAddDouble (FcPattern *p, FcObject object, double d)
628 {
629 FcValue v;
630
631 v.type = FcTypeDouble;
632 v.u.d = d;
633 return FcPatternObjectAdd (p, object, v, FcTrue);
634 }
635
636
637 FcBool
638 FcPatternAddDouble (FcPattern *p, const char *object, double d)
639 {
640 return FcPatternObjectAddDouble (p, FcObjectFromName (object), d);
641 }
642
643 FcBool
644 FcPatternObjectAddString (FcPattern *p, FcObject object, const FcChar8 *s)
645 {
646 FcValue v;
647
648 if (!s)
649 {
650 v.type = FcTypeVoid;
651 v.u.s = 0;
652 return FcPatternObjectAdd (p, object, v, FcTrue);
653 }
654
655 v.type = FcTypeString;
656 v.u.s = FcStrStaticName(s);
657 return FcPatternObjectAdd (p, object, v, FcTrue);
658 }
659
660 FcBool
661 FcPatternAddString (FcPattern *p, const char *object, const FcChar8 *s)
662 {
663 return FcPatternObjectAddString (p, FcObjectFromName (object), s);
664 }
665
666 FcBool
667 FcPatternAddMatrix (FcPattern *p, const char *object, const FcMatrix *s)
668 {
669 FcValue v;
670
671 v.type = FcTypeMatrix;
672 v.u.m = s;
673 return FcPatternAdd (p, object, v, FcTrue);
674 }
675
676
677 FcBool
678 FcPatternObjectAddBool (FcPattern *p, FcObject object, FcBool b)
679 {
680 FcValue v;
681
682 v.type = FcTypeBool;
683 v.u.b = b;
684 return FcPatternObjectAdd (p, object, v, FcTrue);
685 }
686
687 FcBool
688 FcPatternAddBool (FcPattern *p, const char *object, FcBool b)
689 {
690 return FcPatternObjectAddBool (p, FcObjectFromName (object), b);
691 }
692
693 FcBool
694 FcPatternAddCharSet (FcPattern *p, const char *object, const FcCharSet *c)
695 {
696 FcValue v;
697
698 v.type = FcTypeCharSet;
699 v.u.c = (FcCharSet *)c;
700 return FcPatternAdd (p, object, v, FcTrue);
701 }
702
703 FcBool
704 FcPatternAddFTFace (FcPattern *p, const char *object, const FT_Face f)
705 {
706 FcValue v;
707
708 v.type = FcTypeFTFace;
709 v.u.f = (void *) f;
710 return FcPatternAdd (p, object, v, FcTrue);
711 }
712
713 FcBool
714 FcPatternAddLangSet (FcPattern *p, const char *object, const FcLangSet *ls)
715 {
716 FcValue v;
717
718 v.type = FcTypeLangSet;
719 v.u.l = (FcLangSet *)ls;
720 return FcPatternAdd (p, object, v, FcTrue);
721 }
722
723 FcResult
724 FcPatternObjectGet (const FcPattern *p, FcObject object, int id, FcValue *v)
725 {
726 FcPatternElt *e;
727 FcValueListPtr l;
728
729 e = FcPatternObjectFindElt (p, object);
730 if (!e)
731 return FcResultNoMatch;
732 for (l = FcPatternEltValues(e); l; l = FcValueListNext(l))
733 {
734 if (!id)
735 {
736 *v = FcValueCanonicalize(&l->value);
737 return FcResultMatch;
738 }
739 id--;
740 }
741 return FcResultNoId;
742 }
743
744 FcResult
745 FcPatternGet (const FcPattern *p, const char *object, int id, FcValue *v)
746 {
747 return FcPatternObjectGet (p, FcObjectFromName (object), id, v);
748 }
749
750 FcResult
751 FcPatternObjectGetInteger (const FcPattern *p, FcObject object, int id, int *i)
752 {
753 FcValue v;
754 FcResult r;
755
756 r = FcPatternObjectGet (p, object, id, &v);
757 if (r != FcResultMatch)
758 return r;
759 switch (v.type) {
760 case FcTypeDouble:
761 *i = (int) v.u.d;
762 break;
763 case FcTypeInteger:
764 *i = v.u.i;
765 break;
766 default:
767 return FcResultTypeMismatch;
768 }
769 return FcResultMatch;
770 }
771
772 FcResult
773 FcPatternGetInteger (const FcPattern *p, const char *object, int id, int *i)
774 {
775 return FcPatternObjectGetInteger (p, FcObjectFromName (object), id, i);
776 }
777
778
779 FcResult
780 FcPatternObjectGetDouble (const FcPattern *p, FcObject object, int id, double *d)
781 {
782 FcValue v;
783 FcResult r;
784
785 r = FcPatternObjectGet (p, object, id, &v);
786 if (r != FcResultMatch)
787 return r;
788 switch (v.type) {
789 case FcTypeDouble:
790 *d = v.u.d;
791 break;
792 case FcTypeInteger:
793 *d = (double) v.u.i;
794 break;
795 default:
796 return FcResultTypeMismatch;
797 }
798 return FcResultMatch;
799 }
800
801 FcResult
802 FcPatternGetDouble (const FcPattern *p, const char *object, int id, double *d)
803 {
804 return FcPatternObjectGetDouble (p, FcObjectFromName (object), id, d);
805 }
806
807 FcResult
808 FcPatternObjectGetString (const FcPattern *p, FcObject object, int id, FcChar8 ** s)
809 {
810 FcValue v;
811 FcResult r;
812
813 r = FcPatternObjectGet (p, object, id, &v);
814 if (r != FcResultMatch)
815 return r;
816 if (v.type != FcTypeString)
817 return FcResultTypeMismatch;
818
819 *s = (FcChar8 *) v.u.s;
820 return FcResultMatch;
821 }
822
823 FcResult
824 FcPatternGetString (const FcPattern *p, const char *object, int id, FcChar8 ** s)
825 {
826 return FcPatternObjectGetString (p, FcObjectFromName (object), id, s);
827 }
828
829 FcResult
830 FcPatternGetMatrix(const FcPattern *p, const char *object, int id, FcMatrix **m)
831 {
832 FcValue v;
833 FcResult r;
834
835 r = FcPatternGet (p, object, id, &v);
836 if (r != FcResultMatch)
837 return r;
838 if (v.type != FcTypeMatrix)
839 return FcResultTypeMismatch;
840 *m = (FcMatrix *)v.u.m;
841 return FcResultMatch;
842 }
843
844
845 FcResult
846 FcPatternGetBool(const FcPattern *p, const char *object, int id, FcBool *b)
847 {
848 FcValue v;
849 FcResult r;
850
851 r = FcPatternGet (p, object, id, &v);
852 if (r != FcResultMatch)
853 return r;
854 if (v.type != FcTypeBool)
855 return FcResultTypeMismatch;
856 *b = v.u.b;
857 return FcResultMatch;
858 }
859
860 FcResult
861 FcPatternGetCharSet(const FcPattern *p, const char *object, int id, FcCharSet **c)
862 {
863 FcValue v;
864 FcResult r;
865
866 r = FcPatternGet (p, object, id, &v);
867 if (r != FcResultMatch)
868 return r;
869 if (v.type != FcTypeCharSet)
870 return FcResultTypeMismatch;
871 *c = (FcCharSet *)v.u.c;
872 return FcResultMatch;
873 }
874
875 FcResult
876 FcPatternGetFTFace(const FcPattern *p, const char *object, int id, FT_Face *f)
877 {
878 FcValue v;
879 FcResult r;
880
881 r = FcPatternGet (p, object, id, &v);
882 if (r != FcResultMatch)
883 return r;
884 if (v.type != FcTypeFTFace)
885 return FcResultTypeMismatch;
886 *f = (FT_Face) v.u.f;
887 return FcResultMatch;
888 }
889
890 FcResult
891 FcPatternGetLangSet(const FcPattern *p, const char *object, int id, FcLangSet **ls)
892 {
893 FcValue v;
894 FcResult r;
895
896 r = FcPatternGet (p, object, id, &v);
897 if (r != FcResultMatch)
898 return r;
899 if (v.type != FcTypeLangSet)
900 return FcResultTypeMismatch;
901 *ls = (FcLangSet *)v.u.l;
902 return FcResultMatch;
903 }
904
905 FcPattern *
906 FcPatternDuplicate (const FcPattern *orig)
907 {
908 FcPattern *new;
909 FcPatternElt *e;
910 int i;
911 FcValueListPtr l;
912
913 new = FcPatternCreate ();
914 if (!new)
915 goto bail0;
916
917 e = FcPatternElts(orig);
918
919 for (i = 0; i < orig->num; i++)
920 {
921 for (l = FcPatternEltValues(e + i); l; l = FcValueListNext(l))
922 if (!FcPatternObjectAdd (new, e[i].object,
923 FcValueCanonicalize(&l->value),
924 FcTrue))
925 goto bail1;
926 }
927
928 return new;
929
930 bail1:
931 FcPatternDestroy (new);
932 bail0:
933 return 0;
934 }
935
936 void
937 FcPatternReference (FcPattern *p)
938 {
939 if (p->ref != FC_REF_CONSTANT)
940 p->ref++;
941 }
942
943 FcPattern *
944 FcPatternVaBuild (FcPattern *orig, va_list va)
945 {
946 FcPattern *ret;
947
948 FcPatternVapBuild (ret, orig, va);
949 return ret;
950 }
951
952 FcPattern *
953 FcPatternBuild (FcPattern *orig, ...)
954 {
955 va_list va;
956
957 va_start (va, orig);
958 FcPatternVapBuild (orig, orig, va);
959 va_end (va);
960 return orig;
961 }
962
963 /*
964 * Add all of the elements in 's' to 'p'
965 */
966 FcBool
967 FcPatternAppend (FcPattern *p, FcPattern *s)
968 {
969 int i;
970 FcPatternElt *e;
971 FcValueListPtr v;
972
973 for (i = 0; i < s->num; i++)
974 {
975 e = FcPatternElts(s)+i;
976 for (v = FcPatternEltValues(e); v; v = FcValueListNext(v))
977 {
978 if (!FcPatternObjectAddWithBinding (p, e->object,
979 FcValueCanonicalize(&v->value),
980 v->binding, FcTrue))
981 return FcFalse;
982 }
983 }
984 return FcTrue;
985 }
986
987 #define OBJECT_HASH_SIZE 31
988 static struct objectBucket {
989 struct objectBucket *next;
990 FcChar32 hash;
991 } *FcObjectBuckets[OBJECT_HASH_SIZE];
992
993 static FcBool
994 FcStrHashed (const FcChar8 *name)
995 {
996 FcChar32 hash = FcStringHash (name);
997 struct objectBucket **p;
998 struct objectBucket *b;
999
1000 for (p = &FcObjectBuckets[hash % OBJECT_HASH_SIZE]; (b = *p); p = &(b->next))
1001 if (b->hash == hash && !strcmp ((char *)name, (char *) (b + 1)))
1002 return FcTrue;
1003 return FcFalse;
1004 }
1005
1006 const FcChar8 *
1007 FcStrStaticName (const FcChar8 *name)
1008 {
1009 FcChar32 hash = FcStringHash (name);
1010 struct objectBucket **p;
1011 struct objectBucket *b;
1012 int size;
1013
1014 for (p = &FcObjectBuckets[hash % OBJECT_HASH_SIZE]; (b = *p); p = &(b->next))
1015 if (b->hash == hash && !strcmp ((char *)name, (char *) (b + 1)))
1016 return (FcChar8 *) (b + 1);
1017 size = sizeof (struct objectBucket) + strlen ((char *)name) + 1;
1018 b = malloc (size + sizeof (int));
1019 /* workaround glibc bug which reads strlen in groups of 4 */
1020 FcMemAlloc (FC_MEM_STATICSTR, size + sizeof (int));
1021 if (!b)
1022 return NULL;
1023 b->next = 0;
1024 b->hash = hash;
1025 strcpy ((char *) (b + 1), (char *)name);
1026 *p = b;
1027 return (FcChar8 *) (b + 1);
1028 }
1029
1030 static void
1031 FcStrStaticNameFini (void)
1032 {
1033 int i, size;
1034 struct objectBucket *b, *next;
1035 char *name;
1036
1037 for (i = 0; i < OBJECT_HASH_SIZE; i++)
1038 {
1039 for (b = FcObjectBuckets[i]; b; b = next)
1040 {
1041 next = b->next;
1042 name = (char *) (b + 1);
1043 size = sizeof (struct objectBucket) + strlen (name) + 1;
1044 FcMemFree (FC_MEM_STATICSTR, size);
1045 free (b);
1046 }
1047 FcObjectBuckets[i] = 0;
1048 }
1049 }
1050
1051 void
1052 FcPatternFini (void)
1053 {
1054 FcStrStaticNameFini ();
1055 FcObjectFini ();
1056 }
1057
1058 FcBool
1059 FcPatternSerializeAlloc (FcSerialize *serialize, const FcPattern *pat)
1060 {
1061 int i;
1062 FcPatternElt *elts = FcPatternElts(pat);
1063
1064 if (!FcSerializeAlloc (serialize, pat, sizeof (FcPattern)))
1065 return FcFalse;
1066 if (!FcSerializeAlloc (serialize, elts, pat->num * sizeof (FcPatternElt)))
1067 return FcFalse;
1068 for (i = 0; i < pat->num; i++)
1069 if (!FcValueListSerializeAlloc (serialize, FcPatternEltValues(elts+i)))
1070 return FcFalse;
1071 return FcTrue;
1072 }
1073
1074 FcPattern *
1075 FcPatternSerialize (FcSerialize *serialize, const FcPattern *pat)
1076 {
1077 FcPattern *pat_serialized;
1078 FcPatternElt *elts = FcPatternElts (pat);
1079 FcPatternElt *elts_serialized;
1080 FcValueList *values_serialized;
1081 int i;
1082
1083 pat_serialized = FcSerializePtr (serialize, pat);
1084 if (!pat_serialized)
1085 return NULL;
1086 *pat_serialized = *pat;
1087 pat_serialized->size = pat->num;
1088 pat_serialized->ref = FC_REF_CONSTANT;
1089
1090 elts_serialized = FcSerializePtr (serialize, elts);
1091 if (!elts_serialized)
1092 return NULL;
1093
1094 pat_serialized->elts_offset = FcPtrToOffset (pat_serialized,
1095 elts_serialized);
1096
1097 for (i = 0; i < pat->num; i++)
1098 {
1099 values_serialized = FcValueListSerialize (serialize, FcPatternEltValues (elts+i));
1100 if (!values_serialized)
1101 return NULL;
1102 elts_serialized[i].object = elts[i].object;
1103 elts_serialized[i].values = FcPtrToEncodedOffset (&elts_serialized[i],
1104 values_serialized,
1105 FcValueList);
1106 }
1107 if (FcDebug() & FC_DBG_CACHEV) {
1108 printf ("Raw pattern:\n");
1109 FcPatternPrint (pat);
1110 printf ("Serialized pattern:\n");
1111 FcPatternPrint (pat_serialized);
1112 printf ("\n");
1113 }
1114 return pat_serialized;
1115 }
1116
1117 FcBool
1118 FcValueListSerializeAlloc (FcSerialize *serialize, const FcValueList *vl)
1119 {
1120 while (vl)
1121 {
1122 if (!FcSerializeAlloc (serialize, vl, sizeof (FcValueList)))
1123 return FcFalse;
1124 switch (vl->value.type) {
1125 case FcTypeString:
1126 if (!FcStrSerializeAlloc (serialize, vl->value.u.s))
1127 return FcFalse;
1128 break;
1129 case FcTypeCharSet:
1130 if (!FcCharSetSerializeAlloc (serialize, vl->value.u.c))
1131 return FcFalse;
1132 break;
1133 case FcTypeLangSet:
1134 if (!FcLangSetSerializeAlloc (serialize, vl->value.u.l))
1135 return FcFalse;
1136 break;
1137 default:
1138 break;
1139 }
1140 vl = vl->next;
1141 }
1142 return FcTrue;
1143 }
1144
1145 FcValueList *
1146 FcValueListSerialize (FcSerialize *serialize, const FcValueList *vl)
1147 {
1148 FcValueList *vl_serialized;
1149 FcChar8 *s_serialized;
1150 FcCharSet *c_serialized;
1151 FcLangSet *l_serialized;
1152 FcValueList *head_serialized = NULL;
1153 FcValueList *prev_serialized = NULL;
1154
1155 while (vl)
1156 {
1157 vl_serialized = FcSerializePtr (serialize, vl);
1158 if (!vl_serialized)
1159 return NULL;
1160
1161 if (prev_serialized)
1162 prev_serialized->next = FcPtrToEncodedOffset (prev_serialized,
1163 vl_serialized,
1164 FcValueList);
1165 else
1166 head_serialized = vl_serialized;
1167
1168 vl_serialized->next = NULL;
1169 vl_serialized->value = vl->value;
1170 switch (vl->value.type) {
1171 case FcTypeString:
1172 s_serialized = FcStrSerialize (serialize, vl->value.u.s);
1173 if (!s_serialized)
1174 return NULL;
1175 vl_serialized->value.u.s = FcPtrToEncodedOffset (&vl_serialized->value,
1176 s_serialized,
1177 FcChar8);
1178 break;
1179 case FcTypeCharSet:
1180 c_serialized = FcCharSetSerialize (serialize, vl->value.u.c);
1181 if (!c_serialized)
1182 return NULL;
1183 vl_serialized->value.u.c = FcPtrToEncodedOffset (&vl_serialized->value,
1184 c_serialized,
1185 FcCharSet);
1186 break;
1187 case FcTypeLangSet:
1188 l_serialized = FcLangSetSerialize (serialize, vl->value.u.l);
1189 if (!l_serialized)
1190 return NULL;
1191 vl_serialized->value.u.l = FcPtrToEncodedOffset (&vl_serialized->value,
1192 l_serialized,
1193 FcLangSet);
1194 break;
1195 default:
1196 break;
1197 }
1198 prev_serialized = vl_serialized;
1199 vl = vl->next;
1200 }
1201 return head_serialized;
1202 }