2 * $XFree86: xc/lib/fontconfig/src/fcpat.c,v 1.3 2002/02/19 07:50:44 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);
150 FcValueListEqual (FcValueList *la, FcValueList *lb)
154 if (!FcValueEqual (la->value, lb->value))
165 FcPatternDestroy (FcPattern *p)
169 for (i = 0; i < p->num; i++)
170 FcValueListDestroy (p->elts[i].values);
175 FcMemFree (FC_MEM_PATELT, p->size * sizeof (FcPatternElt));
180 FcMemFree (FC_MEM_PATTERN, sizeof (FcPattern));
185 FcPatternFind (FcPattern *p, const char *object, FcBool insert)
197 while (low + 1 < high)
199 i = (low + high) >> 1;
200 s = FcStrCmpIgnoreCase ((FcChar8 *) object, (FcChar8 *) p->elts[i].object);
212 s = FcStrCmpIgnoreCase ((FcChar8 *) object, (FcChar8 *) p->elts[i].object);
224 if (p->num + 1 >= p->size)
228 e = (FcPatternElt *) realloc (p->elts, s * sizeof (FcPatternElt));
230 e = (FcPatternElt *) malloc (s * sizeof (FcPatternElt));
235 FcMemFree (FC_MEM_PATELT, p->size * sizeof (FcPatternElt));
236 FcMemAlloc (FC_MEM_PATELT, s * sizeof (FcPatternElt));
239 p->elts[p->size].object = 0;
240 p->elts[p->size].values = 0;
246 memmove (p->elts + i + 1,
248 sizeof (FcPatternElt) *
254 p->elts[i].object = object;
255 p->elts[i].values = 0;
261 FcPatternEqual (FcPattern *pa, FcPattern *pb)
265 if (pa->num != pb->num)
267 for (i = 0; i < pa->num; i++)
269 if (FcStrCmpIgnoreCase ((FcChar8 *) pa->elts[i].object,
270 (FcChar8 *) pb->elts[i].object) != 0)
272 if (!FcValueListEqual (pa->elts[i].values, pb->elts[i].values))
279 FcPatternAdd (FcPattern *p, const char *object, FcValue value, FcBool append)
282 FcValueList *new, **prev;
284 new = (FcValueList *) malloc (sizeof (FcValueList));
288 FcMemAlloc (FC_MEM_VALLIST, sizeof (FcValueList));
290 value = FcValueSave (value);
291 if (value.type == FcTypeVoid)
297 e = FcPatternFind (p, object, FcTrue);
303 for (prev = &e->values; *prev; prev = &(*prev)->next);
308 new->next = e->values;
315 switch (value.type) {
317 FcStrFree ((FcChar8 *) value.u.s);
320 FcMatrixFree ((FcMatrix *) value.u.m);
323 FcCharSetDestroy ((FcCharSet *) value.u.c);
329 FcMemFree (FC_MEM_VALLIST, sizeof (FcValueList));
336 FcPatternDel (FcPattern *p, const char *object)
341 e = FcPatternFind (p, object, FcFalse);
348 FcValueListDestroy (e->values);
350 /* shuffle existing ones down */
351 memmove (e, e+1, (p->elts + p->num - (e + 1)) * sizeof (FcPatternElt));
353 p->elts[p->num].object = 0;
354 p->elts[p->num].values = 0;
359 FcPatternAddInteger (FcPattern *p, const char *object, int i)
363 v.type = FcTypeInteger;
365 return FcPatternAdd (p, object, v, FcTrue);
369 FcPatternAddDouble (FcPattern *p, const char *object, double d)
373 v.type = FcTypeDouble;
375 return FcPatternAdd (p, object, v, FcTrue);
380 FcPatternAddString (FcPattern *p, const char *object, const FcChar8 *s)
384 v.type = FcTypeString;
386 return FcPatternAdd (p, object, v, FcTrue);
390 FcPatternAddMatrix (FcPattern *p, const char *object, const FcMatrix *s)
394 v.type = FcTypeMatrix;
395 v.u.m = (FcMatrix *) s;
396 return FcPatternAdd (p, object, v, FcTrue);
401 FcPatternAddBool (FcPattern *p, const char *object, FcBool b)
407 return FcPatternAdd (p, object, v, FcTrue);
411 FcPatternAddCharSet (FcPattern *p, const char *object, const FcCharSet *c)
415 v.type = FcTypeCharSet;
416 v.u.c = (FcCharSet *) c;
417 return FcPatternAdd (p, object, v, FcTrue);
421 FcPatternGet (FcPattern *p, const char *object, int id, FcValue *v)
426 e = FcPatternFind (p, object, FcFalse);
428 return FcResultNoMatch;
429 for (l = e->values; l; l = l->next)
434 return FcResultMatch;
442 FcPatternGetInteger (FcPattern *p, const char *object, int id, int *i)
447 r = FcPatternGet (p, object, id, &v);
448 if (r != FcResultMatch)
458 return FcResultTypeMismatch;
460 return FcResultMatch;
464 FcPatternGetDouble (FcPattern *p, const char *object, int id, double *d)
469 r = FcPatternGet (p, object, id, &v);
470 if (r != FcResultMatch)
480 return FcResultTypeMismatch;
482 return FcResultMatch;
486 FcPatternGetString (FcPattern *p, const char *object, int id, FcChar8 ** s)
491 r = FcPatternGet (p, object, id, &v);
492 if (r != FcResultMatch)
494 if (v.type != FcTypeString)
495 return FcResultTypeMismatch;
496 *s = (FcChar8 *) v.u.s;
497 return FcResultMatch;
501 FcPatternGetMatrix (FcPattern *p, const char *object, int id, FcMatrix **m)
506 r = FcPatternGet (p, object, id, &v);
507 if (r != FcResultMatch)
509 if (v.type != FcTypeMatrix)
510 return FcResultTypeMismatch;
511 *m = (FcMatrix *) v.u.m;
512 return FcResultMatch;
517 FcPatternGetBool (FcPattern *p, const char *object, int id, FcBool *b)
522 r = FcPatternGet (p, object, id, &v);
523 if (r != FcResultMatch)
525 if (v.type != FcTypeBool)
526 return FcResultTypeMismatch;
528 return FcResultMatch;
532 FcPatternGetCharSet (FcPattern *p, const char *object, int id, FcCharSet **c)
537 r = FcPatternGet (p, object, id, &v);
538 if (r != FcResultMatch)
540 if (v.type != FcTypeCharSet)
541 return FcResultTypeMismatch;
542 *c = (FcCharSet *) v.u.c;
543 return FcResultMatch;
547 FcPatternDuplicate (FcPattern *orig)
553 new = FcPatternCreate ();
557 for (i = 0; i < orig->num; i++)
559 for (l = orig->elts[i].values; l; l = l->next)
560 if (!FcPatternAdd (new, orig->elts[i].object, l->value, FcTrue))
567 FcPatternDestroy (new);
573 FcPatternVaBuild (FcPattern *orig, va_list va)
577 FcPatternVapBuild (ret, orig, va);
582 FcPatternBuild (FcPattern *orig, ...)
587 FcPatternVapBuild (orig, orig, va);