2 * $XFree86: xc/lib/fontconfig/src/fcpat.c,v 1.9 2002/06/19 20:08:22 keithp Exp $
4 * Copyright © 2000 Keith Packard, member of The XFree86 Project, Inc.
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.
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.
30 FcPatternCreate (void)
34 p = (FcPattern *) malloc (sizeof (FcPattern));
37 FcMemAlloc (FC_MEM_PATTERN, sizeof (FcPattern));
46 FcValueDestroy (FcValue v)
50 FcStrFree ((FcChar8 *) v.u.s);
53 FcMatrixFree ((FcMatrix *) v.u.m);
56 FcCharSetDestroy ((FcCharSet *) v.u.c);
64 FcValueSave (FcValue v)
68 v.u.s = FcStrCopy (v.u.s);
73 v.u.m = FcMatrixCopy (v.u.m);
78 v.u.c = FcCharSetCopy ((FcCharSet *) v.u.c);
89 FcValueListDestroy (FcValueList *l)
94 switch (l->value.type) {
96 FcStrFree ((FcChar8 *) l->value.u.s);
99 FcMatrixFree ((FcMatrix *) l->value.u.m);
102 FcCharSetDestroy ((FcCharSet *) l->value.u.c);
108 FcMemFree (FC_MEM_VALLIST, sizeof (FcValueList));
114 FcValueEqual (FcValue va, FcValue vb)
116 if (va.type != vb.type)
118 if (va.type == FcTypeInteger)
120 va.type = FcTypeDouble;
123 if (vb.type == FcTypeInteger)
125 vb.type = FcTypeDouble;
128 if (va.type != vb.type)
135 return va.u.i == vb.u.i;
137 return va.u.d == vb.u.d;
139 return FcStrCmpIgnoreCase (va.u.s, vb.u.s) == 0;
141 return va.u.b == vb.u.b;
143 return FcMatrixEqual (va.u.m, vb.u.m);
145 return FcCharSetEqual (va.u.c, vb.u.c);
147 return va.u.f == vb.u.f;
153 FcDoubleHash (double d)
163 FcStringHash (const FcChar8 *s)
170 h = ((h << 1) | (h >> 31)) ^ c;
175 FcValueHash (FcValue v)
181 return (FcChar32) v.u.i;
183 return FcDoubleHash (v.u.d);
185 return FcStringHash (v.u.s);
187 return (FcChar32) v.u.b;
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));
194 return (FcChar32) v.u.c->num;
196 return FcStringHash ((const FcChar8 *) ((FT_Face) v.u.f)->family_name) ^
197 FcStringHash ((const FcChar8 *) ((FT_Face) v.u.f)->style_name);
203 FcValueListEqual (FcValueList *la, FcValueList *lb)
207 if (!FcValueEqual (la->value, lb->value))
218 FcValueListHash (FcValueList *l)
224 hash = ((hash << 1) | (hash >> 31)) ^ FcValueHash (l->value);
231 FcPatternDestroy (FcPattern *p)
238 for (i = 0; i < p->num; i++)
239 FcValueListDestroy (p->elts[i].values);
244 FcMemFree (FC_MEM_PATELT, p->size * sizeof (FcPatternElt));
249 FcMemFree (FC_MEM_PATTERN, sizeof (FcPattern));
254 FcPatternPosition (const FcPattern *p, const char *object)
256 int low, high, mid, c;
264 mid = (low + high) >> 1;
265 c = strcmp (p->elts[mid].object, object);
279 FcPatternFindElt (const FcPattern *p, const char *object)
281 int i = FcPatternPosition (p, object);
288 FcPatternInsertElt (FcPattern *p, const char *object)
293 i = FcPatternPosition (p, object);
299 if (p->num + 1 >= p->size)
301 int s = p->size + 16;
303 e = (FcPatternElt *) realloc (p->elts, s * sizeof (FcPatternElt));
305 e = (FcPatternElt *) malloc (s * sizeof (FcPatternElt));
310 FcMemFree (FC_MEM_PATELT, p->size * sizeof (FcPatternElt));
311 FcMemAlloc (FC_MEM_PATELT, s * sizeof (FcPatternElt));
314 p->elts[p->size].object = 0;
315 p->elts[p->size].values = 0;
321 memmove (p->elts + i + 1,
323 sizeof (FcPatternElt) *
329 p->elts[i].object = object;
330 p->elts[i].values = 0;
337 FcPatternEqual (const FcPattern *pa, const FcPattern *pb)
341 if (pa->num != pb->num)
343 for (i = 0; i < pa->num; i++)
345 if (strcmp (pa->elts[i].object, pb->elts[i].object) != 0)
347 if (!FcValueListEqual (pa->elts[i].values, pb->elts[i].values))
354 FcPatternHash (const FcPattern *p)
359 for (i = 0; i < p->num; i++)
361 h = (((h << 1) | (h >> 31)) ^
362 FcStringHash ((const FcChar8 *) p->elts[i].object) ^
363 FcValueListHash (p->elts[i].values));
369 FcPatternEqualSubset (const FcPattern *pa, const FcPattern *pb, const FcObjectSet *os)
371 FcPatternElt *ea, *eb;
374 for (i = 0; i < os->nobject; i++)
376 ea = FcPatternFindElt (pa, os->objects[i]);
377 eb = FcPatternFindElt (pb, os->objects[i]);
382 if (!FcValueListEqual (ea->values, eb->values))
395 FcPatternAdd (FcPattern *p, const char *object, FcValue value, FcBool append)
398 FcValueList *new, **prev;
400 new = (FcValueList *) malloc (sizeof (FcValueList));
404 FcMemAlloc (FC_MEM_VALLIST, sizeof (FcValueList));
406 value = FcValueSave (value);
407 if (value.type == FcTypeVoid)
411 new->binding = FcValueBindingStrong;
414 e = FcPatternInsertElt (p, object);
420 for (prev = &e->values; *prev; prev = &(*prev)->next);
425 new->next = e->values;
432 switch (value.type) {
434 FcStrFree ((FcChar8 *) value.u.s);
437 FcMatrixFree ((FcMatrix *) value.u.m);
440 FcCharSetDestroy ((FcCharSet *) value.u.c);
446 FcMemFree (FC_MEM_VALLIST, sizeof (FcValueList));
453 FcPatternDel (FcPattern *p, const char *object)
458 e = FcPatternFindElt (p, object);
465 FcValueListDestroy (e->values);
467 /* shuffle existing ones down */
468 memmove (e, e+1, (p->elts + p->num - (e + 1)) * sizeof (FcPatternElt));
470 p->elts[p->num].object = 0;
471 p->elts[p->num].values = 0;
476 FcPatternAddInteger (FcPattern *p, const char *object, int i)
480 v.type = FcTypeInteger;
482 return FcPatternAdd (p, object, v, FcTrue);
486 FcPatternAddDouble (FcPattern *p, const char *object, double d)
490 v.type = FcTypeDouble;
492 return FcPatternAdd (p, object, v, FcTrue);
497 FcPatternAddString (FcPattern *p, const char *object, const FcChar8 *s)
501 v.type = FcTypeString;
503 return FcPatternAdd (p, object, v, FcTrue);
507 FcPatternAddMatrix (FcPattern *p, const char *object, const FcMatrix *s)
511 v.type = FcTypeMatrix;
512 v.u.m = (FcMatrix *) s;
513 return FcPatternAdd (p, object, v, FcTrue);
518 FcPatternAddBool (FcPattern *p, const char *object, FcBool b)
524 return FcPatternAdd (p, object, v, FcTrue);
528 FcPatternAddCharSet (FcPattern *p, const char *object, const FcCharSet *c)
532 v.type = FcTypeCharSet;
533 v.u.c = (FcCharSet *) c;
534 return FcPatternAdd (p, object, v, FcTrue);
538 FcPatternAddFTFace (FcPattern *p, const char *object, const FT_Face f)
542 v.type = FcTypeFTFace;
544 return FcPatternAdd (p, object, v, FcTrue);
548 FcPatternGet (FcPattern *p, const char *object, int id, FcValue *v)
553 e = FcPatternFindElt (p, object);
555 return FcResultNoMatch;
556 for (l = e->values; l; l = l->next)
561 return FcResultMatch;
569 FcPatternGetInteger (FcPattern *p, const char *object, int id, int *i)
574 r = FcPatternGet (p, object, id, &v);
575 if (r != FcResultMatch)
585 return FcResultTypeMismatch;
587 return FcResultMatch;
591 FcPatternGetDouble (FcPattern *p, const char *object, int id, double *d)
596 r = FcPatternGet (p, object, id, &v);
597 if (r != FcResultMatch)
607 return FcResultTypeMismatch;
609 return FcResultMatch;
613 FcPatternGetString (FcPattern *p, const char *object, int id, FcChar8 ** s)
618 r = FcPatternGet (p, object, id, &v);
619 if (r != FcResultMatch)
621 if (v.type != FcTypeString)
622 return FcResultTypeMismatch;
623 *s = (FcChar8 *) v.u.s;
624 return FcResultMatch;
628 FcPatternGetMatrix (FcPattern *p, const char *object, int id, FcMatrix **m)
633 r = FcPatternGet (p, object, id, &v);
634 if (r != FcResultMatch)
636 if (v.type != FcTypeMatrix)
637 return FcResultTypeMismatch;
638 *m = (FcMatrix *) v.u.m;
639 return FcResultMatch;
644 FcPatternGetBool (FcPattern *p, const char *object, int id, FcBool *b)
649 r = FcPatternGet (p, object, id, &v);
650 if (r != FcResultMatch)
652 if (v.type != FcTypeBool)
653 return FcResultTypeMismatch;
655 return FcResultMatch;
659 FcPatternGetCharSet (FcPattern *p, const char *object, int id, FcCharSet **c)
664 r = FcPatternGet (p, object, id, &v);
665 if (r != FcResultMatch)
667 if (v.type != FcTypeCharSet)
668 return FcResultTypeMismatch;
669 *c = (FcCharSet *) v.u.c;
670 return FcResultMatch;
674 FcPatternGetFTFace (FcPattern *p, const char *object, int id, FT_Face *f)
679 r = FcPatternGet (p, object, id, &v);
680 if (r != FcResultMatch)
682 if (v.type != FcTypeFTFace)
683 return FcResultTypeMismatch;
684 *f = (FT_Face) v.u.f;
685 return FcResultMatch;
689 FcPatternDuplicate (FcPattern *orig)
695 new = FcPatternCreate ();
699 for (i = 0; i < orig->num; i++)
701 for (l = orig->elts[i].values; l; l = l->next)
702 if (!FcPatternAdd (new, orig->elts[i].object, l->value, FcTrue))
709 FcPatternDestroy (new);
715 FcPatternReference (FcPattern *p)
721 FcPatternVaBuild (FcPattern *orig, va_list va)
725 FcPatternVapBuild (ret, orig, va);
730 FcPatternBuild (FcPattern *orig, ...)
735 FcPatternVapBuild (orig, orig, va);