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