]> git.wh0rd.org - fontconfig.git/blob - src/fcpat.c
Add ref counting to font config patterns so that FcFontSort return values
[fontconfig.git] / src / fcpat.c
1 /*
2 * $XFree86: xc/lib/fontconfig/src/fcpat.c,v 1.7 2002/06/03 08:31:15 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 p->ref = 1;
42 return p;
43 }
44
45 void
46 FcValueDestroy (FcValue v)
47 {
48 switch (v.type) {
49 case FcTypeString:
50 FcStrFree ((FcChar8 *) v.u.s);
51 break;
52 case FcTypeMatrix:
53 FcMatrixFree ((FcMatrix *) v.u.m);
54 break;
55 case FcTypeCharSet:
56 FcCharSetDestroy ((FcCharSet *) v.u.c);
57 break;
58 default:
59 break;
60 }
61 }
62
63 FcValue
64 FcValueSave (FcValue v)
65 {
66 switch (v.type) {
67 case FcTypeString:
68 v.u.s = FcStrCopy (v.u.s);
69 if (!v.u.s)
70 v.type = FcTypeVoid;
71 break;
72 case FcTypeMatrix:
73 v.u.m = FcMatrixCopy (v.u.m);
74 if (!v.u.m)
75 v.type = FcTypeVoid;
76 break;
77 case FcTypeCharSet:
78 v.u.c = FcCharSetCopy ((FcCharSet *) v.u.c);
79 if (!v.u.c)
80 v.type = FcTypeVoid;
81 break;
82 default:
83 break;
84 }
85 return v;
86 }
87
88 void
89 FcValueListDestroy (FcValueList *l)
90 {
91 FcValueList *next;
92 for (; l; l = next)
93 {
94 switch (l->value.type) {
95 case FcTypeString:
96 FcStrFree ((FcChar8 *) l->value.u.s);
97 break;
98 case FcTypeMatrix:
99 FcMatrixFree ((FcMatrix *) l->value.u.m);
100 break;
101 case FcTypeCharSet:
102 FcCharSetDestroy ((FcCharSet *) l->value.u.c);
103 break;
104 default:
105 break;
106 }
107 next = l->next;
108 FcMemFree (FC_MEM_VALLIST, sizeof (FcValueList));
109 free (l);
110 }
111 }
112
113 FcBool
114 FcValueEqual (FcValue va, FcValue vb)
115 {
116 if (va.type != vb.type)
117 {
118 if (va.type == FcTypeInteger)
119 {
120 va.type = FcTypeDouble;
121 va.u.d = va.u.i;
122 }
123 if (vb.type == FcTypeInteger)
124 {
125 vb.type = FcTypeDouble;
126 vb.u.d = vb.u.i;
127 }
128 if (va.type != vb.type)
129 return FcFalse;
130 }
131 switch (va.type) {
132 case FcTypeVoid:
133 return FcTrue;
134 case FcTypeInteger:
135 return va.u.i == vb.u.i;
136 case FcTypeDouble:
137 return va.u.d == vb.u.d;
138 case FcTypeString:
139 return FcStrCmpIgnoreCase (va.u.s, vb.u.s) == 0;
140 case FcTypeBool:
141 return va.u.b == vb.u.b;
142 case FcTypeMatrix:
143 return FcMatrixEqual (va.u.m, vb.u.m);
144 case FcTypeCharSet:
145 return FcCharSetEqual (va.u.c, vb.u.c);
146 case FcTypeFTFace:
147 return va.u.f == vb.u.f;
148 }
149 return FcFalse;
150 }
151
152 static FcChar32
153 FcDoubleHash (double d)
154 {
155 if (d < 0)
156 d = -d;
157 if (d > 0xffffffff)
158 d = 0xffffffff;
159 return (FcChar32) d;
160 }
161
162 static FcChar32
163 FcStringHash (const FcChar8 *s)
164 {
165 FcChar8 c;
166 FcChar32 h = 0;
167
168 if (s)
169 while ((c = *s++))
170 h = ((h << 1) | (h >> 31)) ^ c;
171 return h;
172 }
173
174 static FcChar32
175 FcValueHash (FcValue v)
176 {
177 switch (v.type) {
178 case FcTypeVoid:
179 return 0;
180 case FcTypeInteger:
181 return (FcChar32) v.u.i;
182 case FcTypeDouble:
183 return FcDoubleHash (v.u.d);
184 case FcTypeString:
185 return FcStringHash (v.u.s);
186 case FcTypeBool:
187 return (FcChar32) v.u.b;
188 case FcTypeMatrix:
189 return (FcDoubleHash (v.u.m->xx) ^
190 FcDoubleHash (v.u.m->xy) ^
191 FcDoubleHash (v.u.m->yx) ^
192 FcDoubleHash (v.u.m->yy));
193 case FcTypeCharSet:
194 return (FcChar32) v.u.c->num;
195 case FcTypeFTFace:
196 return FcStringHash ((const FcChar8 *) ((FT_Face) v.u.f)->family_name) ^
197 FcStringHash ((const FcChar8 *) ((FT_Face) v.u.f)->style_name);
198 }
199 return FcFalse;
200 }
201
202 static FcBool
203 FcValueListEqual (FcValueList *la, FcValueList *lb)
204 {
205 while (la && lb)
206 {
207 if (!FcValueEqual (la->value, lb->value))
208 return FcFalse;
209 la = la->next;
210 lb = lb->next;
211 }
212 if (la || lb)
213 return FcFalse;
214 return FcTrue;
215 }
216
217 static FcChar32
218 FcValueListHash (FcValueList *l)
219 {
220 FcChar32 hash = 0;
221
222 while (l)
223 {
224 hash = ((hash << 1) | (hash >> 31)) ^ FcValueHash (l->value);
225 l = l->next;
226 }
227 return hash;
228 }
229
230 void
231 FcPatternDestroy (FcPattern *p)
232 {
233 int i;
234
235 if (--p->ref > 0)
236 return;
237
238 for (i = 0; i < p->num; i++)
239 FcValueListDestroy (p->elts[i].values);
240
241 p->num = 0;
242 if (p->elts)
243 {
244 FcMemFree (FC_MEM_PATELT, p->size * sizeof (FcPatternElt));
245 free (p->elts);
246 p->elts = 0;
247 }
248 p->size = 0;
249 FcMemFree (FC_MEM_PATTERN, sizeof (FcPattern));
250 free (p);
251 }
252
253 static int
254 FcPatternPosition (const FcPattern *p, const char *object)
255 {
256 int low, high, mid, c;
257
258 low = 0;
259 high = p->num - 1;
260 c = 1;
261 mid = 0;
262 while (low <= high)
263 {
264 mid = (low + high) >> 1;
265 c = strcmp (p->elts[mid].object, object);
266 if (c == 0)
267 return mid;
268 if (c < 0)
269 low = mid + 1;
270 else
271 high = mid - 1;
272 }
273 if (c < 0)
274 mid++;
275 return -(mid + 1);
276 }
277
278 FcPatternElt *
279 FcPatternFindElt (const FcPattern *p, const char *object)
280 {
281 int i = FcPatternPosition (p, object);
282 if (i < 0)
283 return 0;
284 return &p->elts[i];
285 }
286
287 FcPatternElt *
288 FcPatternInsertElt (FcPattern *p, const char *object)
289 {
290 int i;
291 FcPatternElt *e;
292
293 i = FcPatternPosition (p, object);
294 if (i < 0)
295 {
296 i = -i - 1;
297
298 /* grow array */
299 if (p->num + 1 >= p->size)
300 {
301 int s = p->size + 16;
302 if (p->elts)
303 e = (FcPatternElt *) realloc (p->elts, s * sizeof (FcPatternElt));
304 else
305 e = (FcPatternElt *) malloc (s * sizeof (FcPatternElt));
306 if (!e)
307 return FcFalse;
308 p->elts = e;
309 if (p->size)
310 FcMemFree (FC_MEM_PATELT, p->size * sizeof (FcPatternElt));
311 FcMemAlloc (FC_MEM_PATELT, s * sizeof (FcPatternElt));
312 while (p->size < s)
313 {
314 p->elts[p->size].object = 0;
315 p->elts[p->size].values = 0;
316 p->size++;
317 }
318 }
319
320 /* move elts up */
321 memmove (p->elts + i + 1,
322 p->elts + i,
323 sizeof (FcPatternElt) *
324 (p->num - i));
325
326 /* bump count */
327 p->num++;
328
329 p->elts[i].object = object;
330 p->elts[i].values = 0;
331 }
332
333 return &p->elts[i];
334 }
335
336 FcBool
337 FcPatternEqual (const FcPattern *pa, const FcPattern *pb)
338 {
339 int i;
340
341 if (pa->num != pb->num)
342 return FcFalse;
343 for (i = 0; i < pa->num; i++)
344 {
345 if (strcmp (pa->elts[i].object, pb->elts[i].object) != 0)
346 return FcFalse;
347 if (!FcValueListEqual (pa->elts[i].values, pb->elts[i].values))
348 return FcFalse;
349 }
350 return FcTrue;
351 }
352
353 FcChar32
354 FcPatternHash (const FcPattern *p)
355 {
356 int i;
357 FcChar32 h = 0;
358
359 for (i = 0; i < p->num; i++)
360 {
361 h = (((h << 1) | (h >> 31)) ^
362 FcStringHash ((const FcChar8 *) p->elts[i].object) ^
363 FcValueListHash (p->elts[i].values));
364 }
365 return h;
366 }
367
368 FcBool
369 FcPatternEqualSubset (const FcPattern *pa, const FcPattern *pb, const FcObjectSet *os)
370 {
371 FcPatternElt *ea, *eb;
372 int i;
373
374 for (i = 0; i < os->nobject; i++)
375 {
376 ea = FcPatternFindElt (pa, os->objects[i]);
377 eb = FcPatternFindElt (pb, os->objects[i]);
378 if (ea)
379 {
380 if (!eb)
381 return FcFalse;
382 if (!FcValueListEqual (ea->values, eb->values))
383 return FcFalse;
384 }
385 else
386 {
387 if (eb)
388 return FcFalse;
389 }
390 }
391 return FcTrue;
392 }
393
394 FcBool
395 FcPatternAdd (FcPattern *p, const char *object, FcValue value, FcBool append)
396 {
397 FcPatternElt *e;
398 FcValueList *new, **prev;
399
400 new = (FcValueList *) malloc (sizeof (FcValueList));
401 if (!new)
402 goto bail0;
403
404 FcMemAlloc (FC_MEM_VALLIST, sizeof (FcValueList));
405 /* dup string */
406 value = FcValueSave (value);
407 if (value.type == FcTypeVoid)
408 goto bail1;
409
410 new->value = value;
411 new->next = 0;
412
413 e = FcPatternInsertElt (p, object);
414 if (!e)
415 goto bail2;
416
417 if (append)
418 {
419 for (prev = &e->values; *prev; prev = &(*prev)->next);
420 *prev = new;
421 }
422 else
423 {
424 new->next = e->values;
425 e->values = new;
426 }
427
428 return FcTrue;
429
430 bail2:
431 switch (value.type) {
432 case FcTypeString:
433 FcStrFree ((FcChar8 *) value.u.s);
434 break;
435 case FcTypeMatrix:
436 FcMatrixFree ((FcMatrix *) value.u.m);
437 break;
438 case FcTypeCharSet:
439 FcCharSetDestroy ((FcCharSet *) value.u.c);
440 break;
441 default:
442 break;
443 }
444 bail1:
445 FcMemFree (FC_MEM_VALLIST, sizeof (FcValueList));
446 free (new);
447 bail0:
448 return FcFalse;
449 }
450
451 FcBool
452 FcPatternDel (FcPattern *p, const char *object)
453 {
454 FcPatternElt *e;
455 int i;
456
457 e = FcPatternFindElt (p, object);
458 if (!e)
459 return FcFalse;
460
461 i = e - p->elts;
462
463 /* destroy value */
464 FcValueListDestroy (e->values);
465
466 /* shuffle existing ones down */
467 memmove (e, e+1, (p->elts + p->num - (e + 1)) * sizeof (FcPatternElt));
468 p->num--;
469 p->elts[p->num].object = 0;
470 p->elts[p->num].values = 0;
471 return FcTrue;
472 }
473
474 FcBool
475 FcPatternAddInteger (FcPattern *p, const char *object, int i)
476 {
477 FcValue v;
478
479 v.type = FcTypeInteger;
480 v.u.i = i;
481 return FcPatternAdd (p, object, v, FcTrue);
482 }
483
484 FcBool
485 FcPatternAddDouble (FcPattern *p, const char *object, double d)
486 {
487 FcValue v;
488
489 v.type = FcTypeDouble;
490 v.u.d = d;
491 return FcPatternAdd (p, object, v, FcTrue);
492 }
493
494
495 FcBool
496 FcPatternAddString (FcPattern *p, const char *object, const FcChar8 *s)
497 {
498 FcValue v;
499
500 v.type = FcTypeString;
501 v.u.s = s;
502 return FcPatternAdd (p, object, v, FcTrue);
503 }
504
505 FcBool
506 FcPatternAddMatrix (FcPattern *p, const char *object, const FcMatrix *s)
507 {
508 FcValue v;
509
510 v.type = FcTypeMatrix;
511 v.u.m = (FcMatrix *) s;
512 return FcPatternAdd (p, object, v, FcTrue);
513 }
514
515
516 FcBool
517 FcPatternAddBool (FcPattern *p, const char *object, FcBool b)
518 {
519 FcValue v;
520
521 v.type = FcTypeBool;
522 v.u.b = b;
523 return FcPatternAdd (p, object, v, FcTrue);
524 }
525
526 FcBool
527 FcPatternAddCharSet (FcPattern *p, const char *object, const FcCharSet *c)
528 {
529 FcValue v;
530
531 v.type = FcTypeCharSet;
532 v.u.c = (FcCharSet *) c;
533 return FcPatternAdd (p, object, v, FcTrue);
534 }
535
536 FcBool
537 FcPatternAddFTFace (FcPattern *p, const char *object, const FT_Face f)
538 {
539 FcValue v;
540
541 v.type = FcTypeFTFace;
542 v.u.f = (void *) f;
543 return FcPatternAdd (p, object, v, FcTrue);
544 }
545
546 FcResult
547 FcPatternGet (FcPattern *p, const char *object, int id, FcValue *v)
548 {
549 FcPatternElt *e;
550 FcValueList *l;
551
552 e = FcPatternFindElt (p, object);
553 if (!e)
554 return FcResultNoMatch;
555 for (l = e->values; l; l = l->next)
556 {
557 if (!id)
558 {
559 *v = l->value;
560 return FcResultMatch;
561 }
562 id--;
563 }
564 return FcResultNoId;
565 }
566
567 FcResult
568 FcPatternGetInteger (FcPattern *p, const char *object, int id, int *i)
569 {
570 FcValue v;
571 FcResult r;
572
573 r = FcPatternGet (p, object, id, &v);
574 if (r != FcResultMatch)
575 return r;
576 switch (v.type) {
577 case FcTypeDouble:
578 *i = (int) v.u.d;
579 break;
580 case FcTypeInteger:
581 *i = v.u.i;
582 break;
583 default:
584 return FcResultTypeMismatch;
585 }
586 return FcResultMatch;
587 }
588
589 FcResult
590 FcPatternGetDouble (FcPattern *p, const char *object, int id, double *d)
591 {
592 FcValue v;
593 FcResult r;
594
595 r = FcPatternGet (p, object, id, &v);
596 if (r != FcResultMatch)
597 return r;
598 switch (v.type) {
599 case FcTypeDouble:
600 *d = v.u.d;
601 break;
602 case FcTypeInteger:
603 *d = (double) v.u.i;
604 break;
605 default:
606 return FcResultTypeMismatch;
607 }
608 return FcResultMatch;
609 }
610
611 FcResult
612 FcPatternGetString (FcPattern *p, const char *object, int id, FcChar8 ** s)
613 {
614 FcValue v;
615 FcResult r;
616
617 r = FcPatternGet (p, object, id, &v);
618 if (r != FcResultMatch)
619 return r;
620 if (v.type != FcTypeString)
621 return FcResultTypeMismatch;
622 *s = (FcChar8 *) v.u.s;
623 return FcResultMatch;
624 }
625
626 FcResult
627 FcPatternGetMatrix (FcPattern *p, const char *object, int id, FcMatrix **m)
628 {
629 FcValue v;
630 FcResult r;
631
632 r = FcPatternGet (p, object, id, &v);
633 if (r != FcResultMatch)
634 return r;
635 if (v.type != FcTypeMatrix)
636 return FcResultTypeMismatch;
637 *m = (FcMatrix *) v.u.m;
638 return FcResultMatch;
639 }
640
641
642 FcResult
643 FcPatternGetBool (FcPattern *p, const char *object, int id, FcBool *b)
644 {
645 FcValue v;
646 FcResult r;
647
648 r = FcPatternGet (p, object, id, &v);
649 if (r != FcResultMatch)
650 return r;
651 if (v.type != FcTypeBool)
652 return FcResultTypeMismatch;
653 *b = v.u.b;
654 return FcResultMatch;
655 }
656
657 FcResult
658 FcPatternGetCharSet (FcPattern *p, const char *object, int id, FcCharSet **c)
659 {
660 FcValue v;
661 FcResult r;
662
663 r = FcPatternGet (p, object, id, &v);
664 if (r != FcResultMatch)
665 return r;
666 if (v.type != FcTypeCharSet)
667 return FcResultTypeMismatch;
668 *c = (FcCharSet *) v.u.c;
669 return FcResultMatch;
670 }
671
672 FcResult
673 FcPatternGetFTFace (FcPattern *p, const char *object, int id, FT_Face *f)
674 {
675 FcValue v;
676 FcResult r;
677
678 r = FcPatternGet (p, object, id, &v);
679 if (r != FcResultMatch)
680 return r;
681 if (v.type != FcTypeFTFace)
682 return FcResultTypeMismatch;
683 *f = (FT_Face) v.u.f;
684 return FcResultMatch;
685 }
686
687 FcPattern *
688 FcPatternDuplicate (FcPattern *orig)
689 {
690 FcPattern *new;
691 int i;
692 FcValueList *l;
693
694 new = FcPatternCreate ();
695 if (!new)
696 goto bail0;
697
698 for (i = 0; i < orig->num; i++)
699 {
700 for (l = orig->elts[i].values; l; l = l->next)
701 if (!FcPatternAdd (new, orig->elts[i].object, l->value, FcTrue))
702 goto bail1;
703 }
704
705 return new;
706
707 bail1:
708 FcPatternDestroy (new);
709 bail0:
710 return 0;
711 }
712
713 void
714 FcPatternReference (FcPattern *p)
715 {
716 p->ref++;
717 }
718
719 FcPattern *
720 FcPatternVaBuild (FcPattern *orig, va_list va)
721 {
722 FcPattern *ret;
723
724 FcPatternVapBuild (ret, orig, va);
725 return ret;
726 }
727
728 FcPattern *
729 FcPatternBuild (FcPattern *orig, ...)
730 {
731 va_list va;
732
733 va_start (va, orig);
734 FcPatternVapBuild (orig, orig, va);
735 va_end (va);
736 return orig;
737 }