2 * $XFree86: xc/lib/fontconfig/src/fcpat.c,v 1.13 2002/08/11 18:10:42 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)
344 if (pa->num != pb->num)
346 for (i = 0; i < pa->num; i++)
348 if (strcmp (pa->elts[i].object, pb->elts[i].object) != 0)
350 if (!FcValueListEqual (pa->elts[i].values, pb->elts[i].values))
357 FcPatternHash (const FcPattern *p)
362 for (i = 0; i < p->num; i++)
364 h = (((h << 1) | (h >> 31)) ^
365 FcStringHash ((const FcChar8 *) p->elts[i].object) ^
366 FcValueListHash (p->elts[i].values));
372 FcPatternEqualSubset (const FcPattern *pa, const FcPattern *pb, const FcObjectSet *os)
374 FcPatternElt *ea, *eb;
377 for (i = 0; i < os->nobject; i++)
379 ea = FcPatternFindElt (pa, os->objects[i]);
380 eb = FcPatternFindElt (pb, os->objects[i]);
385 if (!FcValueListEqual (ea->values, eb->values))
398 FcPatternAddWithBinding (FcPattern *p,
401 FcValueBinding binding,
405 FcValueList *new, **prev;
407 new = (FcValueList *) malloc (sizeof (FcValueList));
411 FcMemAlloc (FC_MEM_VALLIST, sizeof (FcValueList));
413 value = FcValueSave (value);
414 if (value.type == FcTypeVoid)
418 new->binding = binding;
421 e = FcPatternInsertElt (p, object);
427 for (prev = &e->values; *prev; prev = &(*prev)->next);
432 new->next = e->values;
439 switch (value.type) {
441 FcStrFree ((FcChar8 *) value.u.s);
444 FcMatrixFree ((FcMatrix *) value.u.m);
447 FcCharSetDestroy ((FcCharSet *) value.u.c);
453 FcMemFree (FC_MEM_VALLIST, sizeof (FcValueList));
460 FcPatternAdd (FcPattern *p, const char *object, FcValue value, FcBool append)
462 return FcPatternAddWithBinding (p, object, value, FcValueBindingStrong, append);
466 FcPatternAddWeak (FcPattern *p, const char *object, FcValue value, FcBool append)
468 return FcPatternAddWithBinding (p, object, value, FcValueBindingWeak, append);
472 FcPatternDel (FcPattern *p, const char *object)
477 e = FcPatternFindElt (p, object);
484 FcValueListDestroy (e->values);
486 /* shuffle existing ones down */
487 memmove (e, e+1, (p->elts + p->num - (e + 1)) * sizeof (FcPatternElt));
489 p->elts[p->num].object = 0;
490 p->elts[p->num].values = 0;
495 FcPatternAddInteger (FcPattern *p, const char *object, int i)
499 v.type = FcTypeInteger;
501 return FcPatternAdd (p, object, v, FcTrue);
505 FcPatternAddDouble (FcPattern *p, const char *object, double d)
509 v.type = FcTypeDouble;
511 return FcPatternAdd (p, object, v, FcTrue);
516 FcPatternAddString (FcPattern *p, const char *object, const FcChar8 *s)
520 v.type = FcTypeString;
522 return FcPatternAdd (p, object, v, FcTrue);
526 FcPatternAddMatrix (FcPattern *p, const char *object, const FcMatrix *s)
530 v.type = FcTypeMatrix;
531 v.u.m = (FcMatrix *) s;
532 return FcPatternAdd (p, object, v, FcTrue);
537 FcPatternAddBool (FcPattern *p, const char *object, FcBool b)
543 return FcPatternAdd (p, object, v, FcTrue);
547 FcPatternAddCharSet (FcPattern *p, const char *object, const FcCharSet *c)
551 v.type = FcTypeCharSet;
552 v.u.c = (FcCharSet *) c;
553 return FcPatternAdd (p, object, v, FcTrue);
557 FcPatternAddFTFace (FcPattern *p, const char *object, const FT_Face f)
561 v.type = FcTypeFTFace;
563 return FcPatternAdd (p, object, v, FcTrue);
567 FcPatternGet (FcPattern *p, const char *object, int id, FcValue *v)
572 e = FcPatternFindElt (p, object);
574 return FcResultNoMatch;
575 for (l = e->values; l; l = l->next)
580 return FcResultMatch;
588 FcPatternGetInteger (FcPattern *p, const char *object, int id, int *i)
593 r = FcPatternGet (p, object, id, &v);
594 if (r != FcResultMatch)
604 return FcResultTypeMismatch;
606 return FcResultMatch;
610 FcPatternGetDouble (FcPattern *p, const char *object, int id, double *d)
615 r = FcPatternGet (p, object, id, &v);
616 if (r != FcResultMatch)
626 return FcResultTypeMismatch;
628 return FcResultMatch;
632 FcPatternGetString (FcPattern *p, const char *object, int id, FcChar8 ** s)
637 r = FcPatternGet (p, object, id, &v);
638 if (r != FcResultMatch)
640 if (v.type != FcTypeString)
641 return FcResultTypeMismatch;
642 *s = (FcChar8 *) v.u.s;
643 return FcResultMatch;
647 FcPatternGetMatrix (FcPattern *p, const char *object, int id, FcMatrix **m)
652 r = FcPatternGet (p, object, id, &v);
653 if (r != FcResultMatch)
655 if (v.type != FcTypeMatrix)
656 return FcResultTypeMismatch;
657 *m = (FcMatrix *) v.u.m;
658 return FcResultMatch;
663 FcPatternGetBool (FcPattern *p, const char *object, int id, FcBool *b)
668 r = FcPatternGet (p, object, id, &v);
669 if (r != FcResultMatch)
671 if (v.type != FcTypeBool)
672 return FcResultTypeMismatch;
674 return FcResultMatch;
678 FcPatternGetCharSet (FcPattern *p, const char *object, int id, FcCharSet **c)
683 r = FcPatternGet (p, object, id, &v);
684 if (r != FcResultMatch)
686 if (v.type != FcTypeCharSet)
687 return FcResultTypeMismatch;
688 *c = (FcCharSet *) v.u.c;
689 return FcResultMatch;
693 FcPatternGetFTFace (FcPattern *p, const char *object, int id, FT_Face *f)
698 r = FcPatternGet (p, object, id, &v);
699 if (r != FcResultMatch)
701 if (v.type != FcTypeFTFace)
702 return FcResultTypeMismatch;
703 *f = (FT_Face) v.u.f;
704 return FcResultMatch;
708 FcPatternDuplicate (FcPattern *orig)
714 new = FcPatternCreate ();
718 for (i = 0; i < orig->num; i++)
720 for (l = orig->elts[i].values; l; l = l->next)
721 if (!FcPatternAdd (new, orig->elts[i].object, l->value, FcTrue))
728 FcPatternDestroy (new);
734 FcPatternReference (FcPattern *p)
740 FcPatternVaBuild (FcPattern *orig, va_list va)
744 FcPatternVapBuild (ret, orig, va);
749 FcPatternBuild (FcPattern *orig, ...)
754 FcPatternVapBuild (orig, orig, va);