]> git.wh0rd.org - fontconfig.git/blob - src/fcpat.c
Add strong/weak pattern value binding, add known charsets for automatic
[fontconfig.git] / src / fcpat.c
1 /*
2 * $XFree86: xc/lib/fontconfig/src/fcpat.c,v 1.9 2002/06/19 20:08:22 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->binding = FcValueBindingStrong;
412 new->next = 0;
413
414 e = FcPatternInsertElt (p, object);
415 if (!e)
416 goto bail2;
417
418 if (append)
419 {
420 for (prev = &e->values; *prev; prev = &(*prev)->next);
421 *prev = new;
422 }
423 else
424 {
425 new->next = e->values;
426 e->values = new;
427 }
428
429 return FcTrue;
430
431 bail2:
432 switch (value.type) {
433 case FcTypeString:
434 FcStrFree ((FcChar8 *) value.u.s);
435 break;
436 case FcTypeMatrix:
437 FcMatrixFree ((FcMatrix *) value.u.m);
438 break;
439 case FcTypeCharSet:
440 FcCharSetDestroy ((FcCharSet *) value.u.c);
441 break;
442 default:
443 break;
444 }
445 bail1:
446 FcMemFree (FC_MEM_VALLIST, sizeof (FcValueList));
447 free (new);
448 bail0:
449 return FcFalse;
450 }
451
452 FcBool
453 FcPatternDel (FcPattern *p, const char *object)
454 {
455 FcPatternElt *e;
456 int i;
457
458 e = FcPatternFindElt (p, object);
459 if (!e)
460 return FcFalse;
461
462 i = e - p->elts;
463
464 /* destroy value */
465 FcValueListDestroy (e->values);
466
467 /* shuffle existing ones down */
468 memmove (e, e+1, (p->elts + p->num - (e + 1)) * sizeof (FcPatternElt));
469 p->num--;
470 p->elts[p->num].object = 0;
471 p->elts[p->num].values = 0;
472 return FcTrue;
473 }
474
475 FcBool
476 FcPatternAddInteger (FcPattern *p, const char *object, int i)
477 {
478 FcValue v;
479
480 v.type = FcTypeInteger;
481 v.u.i = i;
482 return FcPatternAdd (p, object, v, FcTrue);
483 }
484
485 FcBool
486 FcPatternAddDouble (FcPattern *p, const char *object, double d)
487 {
488 FcValue v;
489
490 v.type = FcTypeDouble;
491 v.u.d = d;
492 return FcPatternAdd (p, object, v, FcTrue);
493 }
494
495
496 FcBool
497 FcPatternAddString (FcPattern *p, const char *object, const FcChar8 *s)
498 {
499 FcValue v;
500
501 v.type = FcTypeString;
502 v.u.s = s;
503 return FcPatternAdd (p, object, v, FcTrue);
504 }
505
506 FcBool
507 FcPatternAddMatrix (FcPattern *p, const char *object, const FcMatrix *s)
508 {
509 FcValue v;
510
511 v.type = FcTypeMatrix;
512 v.u.m = (FcMatrix *) s;
513 return FcPatternAdd (p, object, v, FcTrue);
514 }
515
516
517 FcBool
518 FcPatternAddBool (FcPattern *p, const char *object, FcBool b)
519 {
520 FcValue v;
521
522 v.type = FcTypeBool;
523 v.u.b = b;
524 return FcPatternAdd (p, object, v, FcTrue);
525 }
526
527 FcBool
528 FcPatternAddCharSet (FcPattern *p, const char *object, const FcCharSet *c)
529 {
530 FcValue v;
531
532 v.type = FcTypeCharSet;
533 v.u.c = (FcCharSet *) c;
534 return FcPatternAdd (p, object, v, FcTrue);
535 }
536
537 FcBool
538 FcPatternAddFTFace (FcPattern *p, const char *object, const FT_Face f)
539 {
540 FcValue v;
541
542 v.type = FcTypeFTFace;
543 v.u.f = (void *) f;
544 return FcPatternAdd (p, object, v, FcTrue);
545 }
546
547 FcResult
548 FcPatternGet (FcPattern *p, const char *object, int id, FcValue *v)
549 {
550 FcPatternElt *e;
551 FcValueList *l;
552
553 e = FcPatternFindElt (p, object);
554 if (!e)
555 return FcResultNoMatch;
556 for (l = e->values; l; l = l->next)
557 {
558 if (!id)
559 {
560 *v = l->value;
561 return FcResultMatch;
562 }
563 id--;
564 }
565 return FcResultNoId;
566 }
567
568 FcResult
569 FcPatternGetInteger (FcPattern *p, const char *object, int id, int *i)
570 {
571 FcValue v;
572 FcResult r;
573
574 r = FcPatternGet (p, object, id, &v);
575 if (r != FcResultMatch)
576 return r;
577 switch (v.type) {
578 case FcTypeDouble:
579 *i = (int) v.u.d;
580 break;
581 case FcTypeInteger:
582 *i = v.u.i;
583 break;
584 default:
585 return FcResultTypeMismatch;
586 }
587 return FcResultMatch;
588 }
589
590 FcResult
591 FcPatternGetDouble (FcPattern *p, const char *object, int id, double *d)
592 {
593 FcValue v;
594 FcResult r;
595
596 r = FcPatternGet (p, object, id, &v);
597 if (r != FcResultMatch)
598 return r;
599 switch (v.type) {
600 case FcTypeDouble:
601 *d = v.u.d;
602 break;
603 case FcTypeInteger:
604 *d = (double) v.u.i;
605 break;
606 default:
607 return FcResultTypeMismatch;
608 }
609 return FcResultMatch;
610 }
611
612 FcResult
613 FcPatternGetString (FcPattern *p, const char *object, int id, FcChar8 ** s)
614 {
615 FcValue v;
616 FcResult r;
617
618 r = FcPatternGet (p, object, id, &v);
619 if (r != FcResultMatch)
620 return r;
621 if (v.type != FcTypeString)
622 return FcResultTypeMismatch;
623 *s = (FcChar8 *) v.u.s;
624 return FcResultMatch;
625 }
626
627 FcResult
628 FcPatternGetMatrix (FcPattern *p, const char *object, int id, FcMatrix **m)
629 {
630 FcValue v;
631 FcResult r;
632
633 r = FcPatternGet (p, object, id, &v);
634 if (r != FcResultMatch)
635 return r;
636 if (v.type != FcTypeMatrix)
637 return FcResultTypeMismatch;
638 *m = (FcMatrix *) v.u.m;
639 return FcResultMatch;
640 }
641
642
643 FcResult
644 FcPatternGetBool (FcPattern *p, const char *object, int id, FcBool *b)
645 {
646 FcValue v;
647 FcResult r;
648
649 r = FcPatternGet (p, object, id, &v);
650 if (r != FcResultMatch)
651 return r;
652 if (v.type != FcTypeBool)
653 return FcResultTypeMismatch;
654 *b = v.u.b;
655 return FcResultMatch;
656 }
657
658 FcResult
659 FcPatternGetCharSet (FcPattern *p, const char *object, int id, FcCharSet **c)
660 {
661 FcValue v;
662 FcResult r;
663
664 r = FcPatternGet (p, object, id, &v);
665 if (r != FcResultMatch)
666 return r;
667 if (v.type != FcTypeCharSet)
668 return FcResultTypeMismatch;
669 *c = (FcCharSet *) v.u.c;
670 return FcResultMatch;
671 }
672
673 FcResult
674 FcPatternGetFTFace (FcPattern *p, const char *object, int id, FT_Face *f)
675 {
676 FcValue v;
677 FcResult r;
678
679 r = FcPatternGet (p, object, id, &v);
680 if (r != FcResultMatch)
681 return r;
682 if (v.type != FcTypeFTFace)
683 return FcResultTypeMismatch;
684 *f = (FT_Face) v.u.f;
685 return FcResultMatch;
686 }
687
688 FcPattern *
689 FcPatternDuplicate (FcPattern *orig)
690 {
691 FcPattern *new;
692 int i;
693 FcValueList *l;
694
695 new = FcPatternCreate ();
696 if (!new)
697 goto bail0;
698
699 for (i = 0; i < orig->num; i++)
700 {
701 for (l = orig->elts[i].values; l; l = l->next)
702 if (!FcPatternAdd (new, orig->elts[i].object, l->value, FcTrue))
703 goto bail1;
704 }
705
706 return new;
707
708 bail1:
709 FcPatternDestroy (new);
710 bail0:
711 return 0;
712 }
713
714 void
715 FcPatternReference (FcPattern *p)
716 {
717 p->ref++;
718 }
719
720 FcPattern *
721 FcPatternVaBuild (FcPattern *orig, va_list va)
722 {
723 FcPattern *ret;
724
725 FcPatternVapBuild (ret, orig, va);
726 return ret;
727 }
728
729 FcPattern *
730 FcPatternBuild (FcPattern *orig, ...)
731 {
732 va_list va;
733
734 va_start (va, orig);
735 FcPatternVapBuild (orig, orig, va);
736 va_end (va);
737 return orig;
738 }