2 * $XFree86: xc/lib/fontconfig/src/fcpat.c,v 1.5 2002/05/29 22:07:33 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 FcPatternFind (FcPattern *p, const char *object, FcBool insert)
199 while (low + 1 < high)
201 i = (low + high) >> 1;
202 s = strcmp (object, p->elts[i].object);
214 s = strcmp (object, p->elts[i].object);
226 if (p->num + 1 >= p->size)
230 e = (FcPatternElt *) realloc (p->elts, s * sizeof (FcPatternElt));
232 e = (FcPatternElt *) malloc (s * sizeof (FcPatternElt));
237 FcMemFree (FC_MEM_PATELT, p->size * sizeof (FcPatternElt));
238 FcMemAlloc (FC_MEM_PATELT, s * sizeof (FcPatternElt));
241 p->elts[p->size].object = 0;
242 p->elts[p->size].values = 0;
248 memmove (p->elts + i + 1,
250 sizeof (FcPatternElt) *
256 p->elts[i].object = object;
257 p->elts[i].values = 0;
263 FcPatternEqual (FcPattern *pa, FcPattern *pb)
267 if (pa->num != pb->num)
269 for (i = 0; i < pa->num; i++)
271 if (strcmp (pa->elts[i].object, pb->elts[i].object) != 0)
273 if (!FcValueListEqual (pa->elts[i].values, pb->elts[i].values))
280 FcPatternAdd (FcPattern *p, const char *object, FcValue value, FcBool append)
283 FcValueList *new, **prev;
285 new = (FcValueList *) malloc (sizeof (FcValueList));
289 FcMemAlloc (FC_MEM_VALLIST, sizeof (FcValueList));
291 value = FcValueSave (value);
292 if (value.type == FcTypeVoid)
298 e = FcPatternFind (p, object, FcTrue);
304 for (prev = &e->values; *prev; prev = &(*prev)->next);
309 new->next = e->values;
316 switch (value.type) {
318 FcStrFree ((FcChar8 *) value.u.s);
321 FcMatrixFree ((FcMatrix *) value.u.m);
324 FcCharSetDestroy ((FcCharSet *) value.u.c);
330 FcMemFree (FC_MEM_VALLIST, sizeof (FcValueList));
337 FcPatternDel (FcPattern *p, const char *object)
342 e = FcPatternFind (p, object, FcFalse);
349 FcValueListDestroy (e->values);
351 /* shuffle existing ones down */
352 memmove (e, e+1, (p->elts + p->num - (e + 1)) * sizeof (FcPatternElt));
354 p->elts[p->num].object = 0;
355 p->elts[p->num].values = 0;
360 FcPatternAddInteger (FcPattern *p, const char *object, int i)
364 v.type = FcTypeInteger;
366 return FcPatternAdd (p, object, v, FcTrue);
370 FcPatternAddDouble (FcPattern *p, const char *object, double d)
374 v.type = FcTypeDouble;
376 return FcPatternAdd (p, object, v, FcTrue);
381 FcPatternAddString (FcPattern *p, const char *object, const FcChar8 *s)
385 v.type = FcTypeString;
387 return FcPatternAdd (p, object, v, FcTrue);
391 FcPatternAddMatrix (FcPattern *p, const char *object, const FcMatrix *s)
395 v.type = FcTypeMatrix;
396 v.u.m = (FcMatrix *) s;
397 return FcPatternAdd (p, object, v, FcTrue);
402 FcPatternAddBool (FcPattern *p, const char *object, FcBool b)
408 return FcPatternAdd (p, object, v, FcTrue);
412 FcPatternAddCharSet (FcPattern *p, const char *object, const FcCharSet *c)
416 v.type = FcTypeCharSet;
417 v.u.c = (FcCharSet *) c;
418 return FcPatternAdd (p, object, v, FcTrue);
422 FcPatternAddFTFace (FcPattern *p, const char *object, const FT_Face f)
426 v.type = FcTypeFTFace;
428 return FcPatternAdd (p, object, v, FcTrue);
432 FcPatternGet (FcPattern *p, const char *object, int id, FcValue *v)
437 e = FcPatternFind (p, object, FcFalse);
439 return FcResultNoMatch;
440 for (l = e->values; l; l = l->next)
445 return FcResultMatch;
453 FcPatternGetInteger (FcPattern *p, const char *object, int id, int *i)
458 r = FcPatternGet (p, object, id, &v);
459 if (r != FcResultMatch)
469 return FcResultTypeMismatch;
471 return FcResultMatch;
475 FcPatternGetDouble (FcPattern *p, const char *object, int id, double *d)
480 r = FcPatternGet (p, object, id, &v);
481 if (r != FcResultMatch)
491 return FcResultTypeMismatch;
493 return FcResultMatch;
497 FcPatternGetString (FcPattern *p, const char *object, int id, FcChar8 ** s)
502 r = FcPatternGet (p, object, id, &v);
503 if (r != FcResultMatch)
505 if (v.type != FcTypeString)
506 return FcResultTypeMismatch;
507 *s = (FcChar8 *) v.u.s;
508 return FcResultMatch;
512 FcPatternGetMatrix (FcPattern *p, const char *object, int id, FcMatrix **m)
517 r = FcPatternGet (p, object, id, &v);
518 if (r != FcResultMatch)
520 if (v.type != FcTypeMatrix)
521 return FcResultTypeMismatch;
522 *m = (FcMatrix *) v.u.m;
523 return FcResultMatch;
528 FcPatternGetBool (FcPattern *p, const char *object, int id, FcBool *b)
533 r = FcPatternGet (p, object, id, &v);
534 if (r != FcResultMatch)
536 if (v.type != FcTypeBool)
537 return FcResultTypeMismatch;
539 return FcResultMatch;
543 FcPatternGetCharSet (FcPattern *p, const char *object, int id, FcCharSet **c)
548 r = FcPatternGet (p, object, id, &v);
549 if (r != FcResultMatch)
551 if (v.type != FcTypeCharSet)
552 return FcResultTypeMismatch;
553 *c = (FcCharSet *) v.u.c;
554 return FcResultMatch;
558 FcPatternGetFTFace (FcPattern *p, const char *object, int id, FT_Face *f)
563 r = FcPatternGet (p, object, id, &v);
564 if (r != FcResultMatch)
566 if (v.type != FcTypeFTFace)
567 return FcResultTypeMismatch;
568 *f = (FT_Face) v.u.f;
569 return FcResultMatch;
573 FcPatternDuplicate (FcPattern *orig)
579 new = FcPatternCreate ();
583 for (i = 0; i < orig->num; i++)
585 for (l = orig->elts[i].values; l; l = l->next)
586 if (!FcPatternAdd (new, orig->elts[i].object, l->value, FcTrue))
593 FcPatternDestroy (new);
599 FcPatternVaBuild (FcPattern *orig, va_list va)
603 FcPatternVapBuild (ret, orig, va);
608 FcPatternBuild (FcPattern *orig, ...)
613 FcPatternVapBuild (orig, orig, va);