2 * $XFree86: xc/lib/fontconfig/src/fcpat.c,v 1.6 2002/05/31 23:21:25 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));
45 FcValueDestroy (FcValue v)
49 FcStrFree ((FcChar8 *) v.u.s);
52 FcMatrixFree ((FcMatrix *) v.u.m);
55 FcCharSetDestroy ((FcCharSet *) v.u.c);
63 FcValueSave (FcValue v)
67 v.u.s = FcStrCopy (v.u.s);
72 v.u.m = FcMatrixCopy (v.u.m);
77 v.u.c = FcCharSetCopy ((FcCharSet *) v.u.c);
88 FcValueListDestroy (FcValueList *l)
93 switch (l->value.type) {
95 FcStrFree ((FcChar8 *) l->value.u.s);
98 FcMatrixFree ((FcMatrix *) l->value.u.m);
101 FcCharSetDestroy ((FcCharSet *) l->value.u.c);
107 FcMemFree (FC_MEM_VALLIST, sizeof (FcValueList));
113 FcValueEqual (FcValue va, FcValue vb)
115 if (va.type != vb.type)
117 if (va.type == FcTypeInteger)
119 va.type = FcTypeDouble;
122 if (vb.type == FcTypeInteger)
124 vb.type = FcTypeDouble;
127 if (va.type != vb.type)
134 return va.u.i == vb.u.i;
136 return va.u.d == vb.u.d;
138 return FcStrCmpIgnoreCase (va.u.s, vb.u.s) == 0;
140 return va.u.b == vb.u.b;
142 return FcMatrixEqual (va.u.m, vb.u.m);
144 return FcCharSetEqual (va.u.c, vb.u.c);
146 return va.u.f == vb.u.f;
152 FcValueListEqual (FcValueList *la, FcValueList *lb)
156 if (!FcValueEqual (la->value, lb->value))
167 FcPatternDestroy (FcPattern *p)
171 for (i = 0; i < p->num; i++)
172 FcValueListDestroy (p->elts[i].values);
177 FcMemFree (FC_MEM_PATELT, p->size * sizeof (FcPatternElt));
182 FcMemFree (FC_MEM_PATTERN, sizeof (FcPattern));
187 FcPatternPosition (const FcPattern *p, const char *object)
189 int low, high, mid, c;
197 mid = (low + high) >> 1;
198 c = strcmp (p->elts[mid].object, object);
212 FcPatternFindElt (const FcPattern *p, const char *object)
214 int i = FcPatternPosition (p, object);
221 FcPatternInsertElt (FcPattern *p, const char *object)
226 i = FcPatternPosition (p, object);
232 if (p->num + 1 >= p->size)
234 int s = p->size + 16;
236 e = (FcPatternElt *) realloc (p->elts, s * sizeof (FcPatternElt));
238 e = (FcPatternElt *) malloc (s * sizeof (FcPatternElt));
243 FcMemFree (FC_MEM_PATELT, p->size * sizeof (FcPatternElt));
244 FcMemAlloc (FC_MEM_PATELT, s * sizeof (FcPatternElt));
247 p->elts[p->size].object = 0;
248 p->elts[p->size].values = 0;
254 memmove (p->elts + i + 1,
256 sizeof (FcPatternElt) *
262 p->elts[i].object = object;
263 p->elts[i].values = 0;
270 FcPatternEqual (const FcPattern *pa, const FcPattern *pb)
274 if (pa->num != pb->num)
276 for (i = 0; i < pa->num; i++)
278 if (strcmp (pa->elts[i].object, pb->elts[i].object) != 0)
280 if (!FcValueListEqual (pa->elts[i].values, pb->elts[i].values))
287 FcPatternEqualSubset (const FcPattern *pa, const FcPattern *pb, const FcObjectSet *os)
289 FcPatternElt *ea, *eb;
292 for (i = 0; i < os->nobject; i++)
294 ea = FcPatternFindElt (pa, os->objects[i]);
295 eb = FcPatternFindElt (pb, os->objects[i]);
300 if (!FcValueListEqual (ea->values, eb->values))
313 FcPatternAdd (FcPattern *p, const char *object, FcValue value, FcBool append)
316 FcValueList *new, **prev;
318 new = (FcValueList *) malloc (sizeof (FcValueList));
322 FcMemAlloc (FC_MEM_VALLIST, sizeof (FcValueList));
324 value = FcValueSave (value);
325 if (value.type == FcTypeVoid)
331 e = FcPatternInsertElt (p, object);
337 for (prev = &e->values; *prev; prev = &(*prev)->next);
342 new->next = e->values;
349 switch (value.type) {
351 FcStrFree ((FcChar8 *) value.u.s);
354 FcMatrixFree ((FcMatrix *) value.u.m);
357 FcCharSetDestroy ((FcCharSet *) value.u.c);
363 FcMemFree (FC_MEM_VALLIST, sizeof (FcValueList));
370 FcPatternDel (FcPattern *p, const char *object)
375 e = FcPatternFindElt (p, object);
382 FcValueListDestroy (e->values);
384 /* shuffle existing ones down */
385 memmove (e, e+1, (p->elts + p->num - (e + 1)) * sizeof (FcPatternElt));
387 p->elts[p->num].object = 0;
388 p->elts[p->num].values = 0;
393 FcPatternAddInteger (FcPattern *p, const char *object, int i)
397 v.type = FcTypeInteger;
399 return FcPatternAdd (p, object, v, FcTrue);
403 FcPatternAddDouble (FcPattern *p, const char *object, double d)
407 v.type = FcTypeDouble;
409 return FcPatternAdd (p, object, v, FcTrue);
414 FcPatternAddString (FcPattern *p, const char *object, const FcChar8 *s)
418 v.type = FcTypeString;
420 return FcPatternAdd (p, object, v, FcTrue);
424 FcPatternAddMatrix (FcPattern *p, const char *object, const FcMatrix *s)
428 v.type = FcTypeMatrix;
429 v.u.m = (FcMatrix *) s;
430 return FcPatternAdd (p, object, v, FcTrue);
435 FcPatternAddBool (FcPattern *p, const char *object, FcBool b)
441 return FcPatternAdd (p, object, v, FcTrue);
445 FcPatternAddCharSet (FcPattern *p, const char *object, const FcCharSet *c)
449 v.type = FcTypeCharSet;
450 v.u.c = (FcCharSet *) c;
451 return FcPatternAdd (p, object, v, FcTrue);
455 FcPatternAddFTFace (FcPattern *p, const char *object, const FT_Face f)
459 v.type = FcTypeFTFace;
461 return FcPatternAdd (p, object, v, FcTrue);
465 FcPatternGet (FcPattern *p, const char *object, int id, FcValue *v)
470 e = FcPatternFindElt (p, object);
472 return FcResultNoMatch;
473 for (l = e->values; l; l = l->next)
478 return FcResultMatch;
486 FcPatternGetInteger (FcPattern *p, const char *object, int id, int *i)
491 r = FcPatternGet (p, object, id, &v);
492 if (r != FcResultMatch)
502 return FcResultTypeMismatch;
504 return FcResultMatch;
508 FcPatternGetDouble (FcPattern *p, const char *object, int id, double *d)
513 r = FcPatternGet (p, object, id, &v);
514 if (r != FcResultMatch)
524 return FcResultTypeMismatch;
526 return FcResultMatch;
530 FcPatternGetString (FcPattern *p, const char *object, int id, FcChar8 ** s)
535 r = FcPatternGet (p, object, id, &v);
536 if (r != FcResultMatch)
538 if (v.type != FcTypeString)
539 return FcResultTypeMismatch;
540 *s = (FcChar8 *) v.u.s;
541 return FcResultMatch;
545 FcPatternGetMatrix (FcPattern *p, const char *object, int id, FcMatrix **m)
550 r = FcPatternGet (p, object, id, &v);
551 if (r != FcResultMatch)
553 if (v.type != FcTypeMatrix)
554 return FcResultTypeMismatch;
555 *m = (FcMatrix *) v.u.m;
556 return FcResultMatch;
561 FcPatternGetBool (FcPattern *p, const char *object, int id, FcBool *b)
566 r = FcPatternGet (p, object, id, &v);
567 if (r != FcResultMatch)
569 if (v.type != FcTypeBool)
570 return FcResultTypeMismatch;
572 return FcResultMatch;
576 FcPatternGetCharSet (FcPattern *p, const char *object, int id, FcCharSet **c)
581 r = FcPatternGet (p, object, id, &v);
582 if (r != FcResultMatch)
584 if (v.type != FcTypeCharSet)
585 return FcResultTypeMismatch;
586 *c = (FcCharSet *) v.u.c;
587 return FcResultMatch;
591 FcPatternGetFTFace (FcPattern *p, const char *object, int id, FT_Face *f)
596 r = FcPatternGet (p, object, id, &v);
597 if (r != FcResultMatch)
599 if (v.type != FcTypeFTFace)
600 return FcResultTypeMismatch;
601 *f = (FT_Face) v.u.f;
602 return FcResultMatch;
606 FcPatternDuplicate (FcPattern *orig)
612 new = FcPatternCreate ();
616 for (i = 0; i < orig->num; i++)
618 for (l = orig->elts[i].values; l; l = l->next)
619 if (!FcPatternAdd (new, orig->elts[i].object, l->value, FcTrue))
626 FcPatternDestroy (new);
632 FcPatternVaBuild (FcPattern *orig, va_list va)
636 FcPatternVapBuild (ret, orig, va);
641 FcPatternBuild (FcPattern *orig, ...)
646 FcPatternVapBuild (orig, orig, va);