2 * $XFree86: xc/lib/fontconfig/src/fcpat.c,v 1.16tsi 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.
31 FcPatternCreate (void)
35 p = (FcPattern *) malloc (sizeof (FcPattern));
38 FcMemAlloc (FC_MEM_PATTERN, sizeof (FcPattern));
47 FcValueDestroy (FcValue v)
51 FcStrFree ((FcChar8 *) v.u.s);
54 FcMatrixFree ((FcMatrix *) v.u.m);
57 FcCharSetDestroy ((FcCharSet *) v.u.c);
60 FcLangSetDestroy ((FcLangSet *) v.u.l);
68 FcValueSave (FcValue v)
72 v.u.s = FcStrCopy (v.u.s);
77 v.u.m = FcMatrixCopy (v.u.m);
82 v.u.c = FcCharSetCopy ((FcCharSet *) v.u.c);
87 v.u.l = FcLangSetCopy (v.u.l);
98 FcValueListDestroy (FcValueList *l)
103 switch (l->value.type) {
105 FcStrFree ((FcChar8 *) l->value.u.s);
108 FcMatrixFree ((FcMatrix *) l->value.u.m);
111 FcCharSetDestroy ((FcCharSet *) l->value.u.c);
114 FcLangSetDestroy ((FcLangSet *) l->value.u.l);
120 FcMemFree (FC_MEM_VALLIST, sizeof (FcValueList));
126 FcValueEqual (FcValue va, FcValue vb)
128 if (va.type != vb.type)
130 if (va.type == FcTypeInteger)
132 va.type = FcTypeDouble;
135 if (vb.type == FcTypeInteger)
137 vb.type = FcTypeDouble;
140 if (va.type != vb.type)
147 return va.u.i == vb.u.i;
149 return va.u.d == vb.u.d;
151 return FcStrCmpIgnoreCase (va.u.s, vb.u.s) == 0;
153 return va.u.b == vb.u.b;
155 return FcMatrixEqual (va.u.m, vb.u.m);
157 return FcCharSetEqual (va.u.c, vb.u.c);
159 return va.u.f == vb.u.f;
161 return FcLangSetEqual (va.u.l, vb.u.l);
167 FcDoubleHash (double d)
177 FcStringHash (const FcChar8 *s)
184 h = ((h << 1) | (h >> 31)) ^ c;
189 FcValueHash (FcValue v)
195 return (FcChar32) v.u.i;
197 return FcDoubleHash (v.u.d);
199 return FcStringHash (v.u.s);
201 return (FcChar32) v.u.b;
203 return (FcDoubleHash (v.u.m->xx) ^
204 FcDoubleHash (v.u.m->xy) ^
205 FcDoubleHash (v.u.m->yx) ^
206 FcDoubleHash (v.u.m->yy));
208 return (FcChar32) v.u.c->num;
210 return FcStringHash ((const FcChar8 *) ((FT_Face) v.u.f)->family_name) ^
211 FcStringHash ((const FcChar8 *) ((FT_Face) v.u.f)->style_name);
213 return FcLangSetHash (v.u.l);
219 FcValueListEqual (FcValueList *la, FcValueList *lb)
226 if (!FcValueEqual (la->value, lb->value))
237 FcValueListHash (FcValueList *l)
243 hash = ((hash << 1) | (hash >> 31)) ^ FcValueHash (l->value);
250 FcPatternDestroy (FcPattern *p)
254 if (p->ref == FC_REF_CONSTANT || --p->ref > 0)
257 for (i = 0; i < p->num; i++)
258 FcValueListDestroy (p->elts[i].values);
263 FcMemFree (FC_MEM_PATELT, p->size * sizeof (FcPatternElt));
268 FcMemFree (FC_MEM_PATTERN, sizeof (FcPattern));
272 #define FC_VALUE_LIST_HASH_SIZE 257
273 #define FC_PATTERN_HASH_SIZE 67
275 typedef struct _FcValueListEnt FcValueListEnt;
277 struct _FcValueListEnt {
278 FcValueListEnt *next;
283 static int FcValueListFrozenCount[FcTypeLangSet + 1];
284 static int FcValueListFrozenBytes[FcTypeLangSet + 1];
285 static char *FcValueListFrozenName[] = {
298 FcValueListReport (void);
301 FcValueListReport (void)
305 printf ("Fc Frozen Values:\n");
306 printf ("\t%8s %9s %9s\n", "Type", "Count", "Bytes");
307 for (t = FcTypeVoid; t <= FcTypeLangSet; t++)
308 printf ("\t%8s %9d %9d\n", FcValueListFrozenName[t],
309 FcValueListFrozenCount[t], FcValueListFrozenBytes[t]);
312 static FcValueListEnt *
313 FcValueListEntCreate (FcValueList *h)
316 FcValueList *l, *new;
323 for (l = h; l; l = l->next)
325 if (l->value.type == FcTypeString)
326 string_size += strlen ((char *) l->value.u.s) + 1;
329 size = sizeof (FcValueListEnt) + n * sizeof (FcValueList) + string_size;
330 FcValueListFrozenCount[h->value.type]++;
331 FcValueListFrozenBytes[h->value.type] += size;
335 FcMemAlloc (FC_MEM_VALLIST, size);
336 e->list = (FcValueList *) (e + 1);
337 strs = (FcChar8 *) (e->list + n);
339 for (l = h; l; l = l->next, new++)
341 if (l->value.type == FcTypeString)
343 new->value.type = FcTypeString;
344 new->value.u.s = strs;
345 strcpy ((char *) strs, (char *) l->value.u.s);
346 strs += strlen ((char *) strs) + 1;
350 new->value = l->value;
351 new->value = FcValueSave (new->value);
353 new->binding = l->binding;
362 static int FcValueListTotal;
363 static int FcValueListUsed;
366 FcValueListFreeze (FcValueList *l)
368 static FcValueListEnt *hashTable[FC_VALUE_LIST_HASH_SIZE];
369 FcChar32 hash = FcValueListHash (l);
370 FcValueListEnt **bucket = &hashTable[hash % FC_VALUE_LIST_HASH_SIZE];
374 for (ent = *bucket; ent; ent = ent->next)
376 if (ent->hash == hash && FcValueListEqual (ent->list, l))
380 ent = FcValueListEntCreate (l);
392 FcPatternBaseHash (FcPattern *b)
394 FcChar32 hash = b->num;
397 for (i = 0; i < b->num; i++)
398 hash = ((hash << 1) | (hash >> 31)) ^ ((FcChar32) b->elts[i].values);
402 typedef struct _FcPatternEnt FcPatternEnt;
404 struct _FcPatternEnt {
410 static int FcPatternTotal;
411 static int FcPatternUsed;
414 FcPatternBaseFreeze (FcPattern *b)
416 static FcPatternEnt *hashTable[FC_VALUE_LIST_HASH_SIZE];
417 FcChar32 hash = FcPatternBaseHash (b);
418 FcPatternEnt **bucket = &hashTable[hash % FC_VALUE_LIST_HASH_SIZE];
426 for (ent = *bucket; ent; ent = ent->next)
428 if (ent->hash == hash && b->num == ent->pattern.num)
430 for (i = 0; i < b->num; i++)
432 if (strcmp (b->elts[i].object, ent->pattern.elts[i].object))
434 if (b->elts[i].values != ent->pattern.elts[i].values)
438 return &ent->pattern;
443 * Compute size of pattern + elts + object names
446 for (i = 0; i < b->num; i++)
447 size_objects += strlen (b->elts[i].object) + 1;
449 size = sizeof (FcPatternEnt) + b->num*sizeof (FcPatternElt) + size_objects;
454 FcMemAlloc (FC_MEM_PATTERN, size);
457 ent->pattern.elts = (FcPatternElt *) (ent + 1);
458 ent->pattern.num = b->num;
459 ent->pattern.size = b->num;
460 ent->pattern.ref = FC_REF_CONSTANT;
462 objects = (char *) (ent->pattern.elts + b->num);
463 for (i = 0; i < b->num; i++)
465 ent->pattern.elts[i].values = b->elts[i].values;
466 strcpy (objects, b->elts[i].object);
467 ent->pattern.elts[i].object = objects;
468 objects += strlen (objects) + 1;
474 return &ent->pattern;
478 FcPatternFreeze (FcPattern *p)
480 FcPattern *b, *n = 0;
484 size = sizeof (FcPattern) + p->num * sizeof (FcPatternElt);
485 b = (FcPattern *) malloc (size);
488 FcMemAlloc (FC_MEM_PATTERN, size);
492 b->elts = (FcPatternElt *) (b + 1);
494 * Freeze object lists
496 for (i = 0; i < p->num; i++)
498 b->elts[i].object = p->elts[i].object;
499 b->elts[i].values = FcValueListFreeze (p->elts[i].values);
500 if (!b->elts[i].values)
506 n = FcPatternBaseFreeze (b);
508 if (FcDebug() & FC_DBG_MEMORY)
510 printf ("ValueLists: total %9d used %9d\n", FcValueListTotal, FcValueListUsed);
511 printf ("Patterns: total %9d used %9d\n", FcPatternTotal, FcPatternUsed);
517 assert (FcPatternEqual (n, p));
523 FcPatternPosition (const FcPattern *p, const char *object)
525 int low, high, mid, c;
533 mid = (low + high) >> 1;
534 c = strcmp (p->elts[mid].object, object);
548 FcPatternFindElt (const FcPattern *p, const char *object)
550 int i = FcPatternPosition (p, object);
557 FcPatternInsertElt (FcPattern *p, const char *object)
562 i = FcPatternPosition (p, object);
568 if (p->num + 1 >= p->size)
570 int s = p->size + 16;
572 e = (FcPatternElt *) realloc (p->elts, s * sizeof (FcPatternElt));
574 e = (FcPatternElt *) malloc (s * sizeof (FcPatternElt));
579 FcMemFree (FC_MEM_PATELT, p->size * sizeof (FcPatternElt));
580 FcMemAlloc (FC_MEM_PATELT, s * sizeof (FcPatternElt));
583 p->elts[p->size].object = 0;
584 p->elts[p->size].values = 0;
590 memmove (p->elts + i + 1,
592 sizeof (FcPatternElt) *
598 p->elts[i].object = object;
599 p->elts[i].values = 0;
606 FcPatternEqual (const FcPattern *pa, const FcPattern *pb)
613 if (pa->num != pb->num)
615 for (i = 0; i < pa->num; i++)
617 if (strcmp (pa->elts[i].object, pb->elts[i].object) != 0)
619 if (!FcValueListEqual (pa->elts[i].values, pb->elts[i].values))
626 FcPatternHash (const FcPattern *p)
631 for (i = 0; i < p->num; i++)
633 h = (((h << 1) | (h >> 31)) ^
634 FcStringHash ((const FcChar8 *) p->elts[i].object) ^
635 FcValueListHash (p->elts[i].values));
641 FcPatternEqualSubset (const FcPattern *pa, const FcPattern *pb, const FcObjectSet *os)
643 FcPatternElt *ea, *eb;
646 for (i = 0; i < os->nobject; i++)
648 ea = FcPatternFindElt (pa, os->objects[i]);
649 eb = FcPatternFindElt (pb, os->objects[i]);
654 if (!FcValueListEqual (ea->values, eb->values))
667 FcPatternAddWithBinding (FcPattern *p,
670 FcValueBinding binding,
674 FcValueList *new, **prev;
676 if (p->ref == FC_REF_CONSTANT)
679 new = (FcValueList *) malloc (sizeof (FcValueList));
683 FcMemAlloc (FC_MEM_VALLIST, sizeof (FcValueList));
685 value = FcValueSave (value);
686 if (value.type == FcTypeVoid)
690 new->binding = binding;
693 e = FcPatternInsertElt (p, object);
699 for (prev = &e->values; *prev; prev = &(*prev)->next);
704 new->next = e->values;
711 switch (value.type) {
713 FcStrFree ((FcChar8 *) value.u.s);
716 FcMatrixFree ((FcMatrix *) value.u.m);
719 FcCharSetDestroy ((FcCharSet *) value.u.c);
722 FcLangSetDestroy ((FcLangSet *) value.u.l);
728 FcMemFree (FC_MEM_VALLIST, sizeof (FcValueList));
735 FcPatternAdd (FcPattern *p, const char *object, FcValue value, FcBool append)
737 return FcPatternAddWithBinding (p, object, value, FcValueBindingStrong, append);
741 FcPatternAddWeak (FcPattern *p, const char *object, FcValue value, FcBool append)
743 return FcPatternAddWithBinding (p, object, value, FcValueBindingWeak, append);
747 FcPatternDel (FcPattern *p, const char *object)
752 e = FcPatternFindElt (p, object);
759 FcValueListDestroy (e->values);
761 /* shuffle existing ones down */
762 memmove (e, e+1, (p->elts + p->num - (e + 1)) * sizeof (FcPatternElt));
764 p->elts[p->num].object = 0;
765 p->elts[p->num].values = 0;
770 FcPatternAddInteger (FcPattern *p, const char *object, int i)
774 v.type = FcTypeInteger;
776 return FcPatternAdd (p, object, v, FcTrue);
780 FcPatternAddDouble (FcPattern *p, const char *object, double d)
784 v.type = FcTypeDouble;
786 return FcPatternAdd (p, object, v, FcTrue);
791 FcPatternAddString (FcPattern *p, const char *object, const FcChar8 *s)
795 v.type = FcTypeString;
797 return FcPatternAdd (p, object, v, FcTrue);
801 FcPatternAddMatrix (FcPattern *p, const char *object, const FcMatrix *s)
805 v.type = FcTypeMatrix;
806 v.u.m = (FcMatrix *) s;
807 return FcPatternAdd (p, object, v, FcTrue);
812 FcPatternAddBool (FcPattern *p, const char *object, FcBool b)
818 return FcPatternAdd (p, object, v, FcTrue);
822 FcPatternAddCharSet (FcPattern *p, const char *object, const FcCharSet *c)
826 v.type = FcTypeCharSet;
827 v.u.c = (FcCharSet *) c;
828 return FcPatternAdd (p, object, v, FcTrue);
832 FcPatternAddFTFace (FcPattern *p, const char *object, const FT_Face f)
836 v.type = FcTypeFTFace;
838 return FcPatternAdd (p, object, v, FcTrue);
842 FcPatternAddLangSet (FcPattern *p, const char *object, const FcLangSet *ls)
846 v.type = FcTypeLangSet;
847 v.u.l = (FcLangSet *) ls;
848 return FcPatternAdd (p, object, v, FcTrue);
852 FcPatternGet (FcPattern *p, const char *object, int id, FcValue *v)
857 e = FcPatternFindElt (p, object);
859 return FcResultNoMatch;
860 for (l = e->values; l; l = l->next)
865 return FcResultMatch;
873 FcPatternGetInteger (FcPattern *p, const char *object, int id, int *i)
878 r = FcPatternGet (p, object, id, &v);
879 if (r != FcResultMatch)
889 return FcResultTypeMismatch;
891 return FcResultMatch;
895 FcPatternGetDouble (FcPattern *p, const char *object, int id, double *d)
900 r = FcPatternGet (p, object, id, &v);
901 if (r != FcResultMatch)
911 return FcResultTypeMismatch;
913 return FcResultMatch;
917 FcPatternGetString (FcPattern *p, const char *object, int id, FcChar8 ** s)
922 r = FcPatternGet (p, object, id, &v);
923 if (r != FcResultMatch)
925 if (v.type != FcTypeString)
926 return FcResultTypeMismatch;
927 *s = (FcChar8 *) v.u.s;
928 return FcResultMatch;
932 FcPatternGetMatrix (FcPattern *p, const char *object, int id, FcMatrix **m)
937 r = FcPatternGet (p, object, id, &v);
938 if (r != FcResultMatch)
940 if (v.type != FcTypeMatrix)
941 return FcResultTypeMismatch;
942 *m = (FcMatrix *) v.u.m;
943 return FcResultMatch;
948 FcPatternGetBool (FcPattern *p, const char *object, int id, FcBool *b)
953 r = FcPatternGet (p, object, id, &v);
954 if (r != FcResultMatch)
956 if (v.type != FcTypeBool)
957 return FcResultTypeMismatch;
959 return FcResultMatch;
963 FcPatternGetCharSet (FcPattern *p, const char *object, int id, FcCharSet **c)
968 r = FcPatternGet (p, object, id, &v);
969 if (r != FcResultMatch)
971 if (v.type != FcTypeCharSet)
972 return FcResultTypeMismatch;
973 *c = (FcCharSet *) v.u.c;
974 return FcResultMatch;
978 FcPatternGetFTFace (FcPattern *p, const char *object, int id, FT_Face *f)
983 r = FcPatternGet (p, object, id, &v);
984 if (r != FcResultMatch)
986 if (v.type != FcTypeFTFace)
987 return FcResultTypeMismatch;
988 *f = (FT_Face) v.u.f;
989 return FcResultMatch;
993 FcPatternGetLangSet (FcPattern *p, const char *object, int id, FcLangSet **ls)
998 r = FcPatternGet (p, object, id, &v);
999 if (r != FcResultMatch)
1001 if (v.type != FcTypeLangSet)
1002 return FcResultTypeMismatch;
1003 *ls = (FcLangSet *) v.u.l;
1004 return FcResultMatch;
1008 FcPatternDuplicate (FcPattern *orig)
1014 new = FcPatternCreate ();
1018 for (i = 0; i < orig->num; i++)
1020 for (l = orig->elts[i].values; l; l = l->next)
1021 if (!FcPatternAdd (new, orig->elts[i].object, l->value, FcTrue))
1028 FcPatternDestroy (new);
1034 FcPatternReference (FcPattern *p)
1036 if (p->ref != FC_REF_CONSTANT)
1041 FcPatternVaBuild (FcPattern *orig, va_list va)
1045 FcPatternVapBuild (ret, orig, va);
1050 FcPatternBuild (FcPattern *orig, ...)
1054 va_start (va, orig);
1055 FcPatternVapBuild (orig, orig, va);