2 * fontconfig/src/fccharset.c
4 * Copyright © 2001 Keith Packard
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 the author(s) not be used in
11 * advertising or publicity pertaining to distribution of the software without
12 * specific, written prior permission. The authors make no
13 * representations about the suitability of this software for any purpose. It
14 * is provided "as is" without express or implied warranty.
16 * THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18 * EVENT SHALL THE AUTHOR(S) 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 FcCharSetCreate (void)
35 fcs = (FcCharSet *) malloc (sizeof (FcCharSet));
38 FcRefInit (&fcs->ref, 1);
40 fcs->leaves_offset = 0;
41 fcs->numbers_offset = 0;
46 FcCharSetPromote (FcValuePromotionBuffer *vbuf)
48 FcCharSet *fcs = (FcCharSet *) vbuf;
50 FC_ASSERT_STATIC (sizeof (FcCharSet) <= sizeof (FcValuePromotionBuffer));
52 FcRefSetConst (&fcs->ref);
54 fcs->leaves_offset = 0;
55 fcs->numbers_offset = 0;
63 return FcCharSetCreate ();
67 FcCharSetDestroy (FcCharSet *fcs)
73 if (FcRefIsConst (&fcs->ref))
75 FcCacheObjectDereference (fcs);
78 if (FcRefDec (&fcs->ref) != 1)
80 for (i = 0; i < fcs->num; i++)
81 free (FcCharSetLeaf (fcs, i));
84 free (FcCharSetLeaves (fcs));
85 free (FcCharSetNumbers (fcs));
92 * Search for the leaf containing with the specified num.
93 * Return its index if it exists, otherwise return negative of
94 * the (position + 1) where it should be inserted
99 FcCharSetFindLeafForward (const FcCharSet *fcs, int start, FcChar16 num)
101 FcChar16 *numbers = FcCharSetNumbers(fcs);
104 int high = fcs->num - 1;
110 int mid = (low + high) >> 1;
119 if (high < 0 || (high < fcs->num && numbers[high] < num))
125 * Locate the leaf containing the specified char, return
126 * its index if it exists, otherwise return negative of
127 * the (position + 1) where it should be inserted
131 FcCharSetFindLeafPos (const FcCharSet *fcs, FcChar32 ucs4)
133 return FcCharSetFindLeafForward (fcs, 0, ucs4 >> 8);
137 FcCharSetFindLeaf (const FcCharSet *fcs, FcChar32 ucs4)
139 int pos = FcCharSetFindLeafPos (fcs, ucs4);
141 return FcCharSetLeaf(fcs, pos);
145 #define FC_IS_ZERO_OR_POWER_OF_TWO(x) (!((x) & ((x)-1)))
148 FcCharSetPutLeaf (FcCharSet *fcs,
153 intptr_t *leaves = FcCharSetLeaves (fcs);
154 FcChar16 *numbers = FcCharSetNumbers (fcs);
160 if (FC_IS_ZERO_OR_POWER_OF_TWO (fcs->num))
164 unsigned int alloced = 8;
165 leaves = malloc (alloced * sizeof (*leaves));
166 numbers = malloc (alloced * sizeof (*numbers));
170 unsigned int alloced = fcs->num;
171 intptr_t *new_leaves, distance;
174 new_leaves = realloc (leaves, alloced * sizeof (*leaves));
175 numbers = realloc (numbers, alloced * sizeof (*numbers));
177 distance = (intptr_t) new_leaves - (intptr_t) leaves;
178 if (new_leaves && distance)
181 for (i = 0; i < fcs->num; i++)
182 new_leaves[i] -= distance;
187 if (!leaves || !numbers)
190 fcs->leaves_offset = FcPtrToOffset (fcs, leaves);
191 fcs->numbers_offset = FcPtrToOffset (fcs, numbers);
194 memmove (leaves + pos + 1, leaves + pos,
195 (fcs->num - pos) * sizeof (*leaves));
196 memmove (numbers + pos + 1, numbers + pos,
197 (fcs->num - pos) * sizeof (*numbers));
198 numbers[pos] = (FcChar16) ucs4;
199 leaves[pos] = FcPtrToOffset (leaves, leaf);
205 * Locate the leaf containing the specified char, creating it
210 FcCharSetFindLeafCreate (FcCharSet *fcs, FcChar32 ucs4)
215 pos = FcCharSetFindLeafPos (fcs, ucs4);
217 return FcCharSetLeaf(fcs, pos);
219 leaf = calloc (1, sizeof (FcCharLeaf));
224 if (!FcCharSetPutLeaf (fcs, ucs4, leaf, pos))
233 FcCharSetInsertLeaf (FcCharSet *fcs, FcChar32 ucs4, FcCharLeaf *leaf)
237 pos = FcCharSetFindLeafPos (fcs, ucs4);
240 free (FcCharSetLeaf (fcs, pos));
241 FcCharSetLeaves(fcs)[pos] = FcPtrToOffset (FcCharSetLeaves(fcs),
246 return FcCharSetPutLeaf (fcs, ucs4, leaf, pos);
250 FcCharSetAddChar (FcCharSet *fcs, FcChar32 ucs4)
255 if (fcs == NULL || FcRefIsConst (&fcs->ref))
257 leaf = FcCharSetFindLeafCreate (fcs, ucs4);
260 b = &leaf->map[(ucs4 & 0xff) >> 5];
261 *b |= (1 << (ucs4 & 0x1f));
266 FcCharSetDelChar (FcCharSet *fcs, FcChar32 ucs4)
271 if (fcs == NULL || FcRefIsConst (&fcs->ref))
273 leaf = FcCharSetFindLeaf (fcs, ucs4);
276 b = &leaf->map[(ucs4 & 0xff) >> 5];
277 *b &= ~(1 << (ucs4 & 0x1f));
278 /* We don't bother removing the leaf if it's empty */
283 * An iterator for the leaves of a charset
286 typedef struct _fcCharSetIter {
293 * Set iter->leaf to the leaf containing iter->ucs4 or higher
297 FcCharSetIterSet (const FcCharSet *fcs, FcCharSetIter *iter)
299 int pos = FcCharSetFindLeafPos (fcs, iter->ucs4);
310 iter->ucs4 = (FcChar32) FcCharSetNumbers(fcs)[pos] << 8;
312 iter->leaf = FcCharSetLeaf(fcs, pos);
317 FcCharSetIterNext (const FcCharSet *fcs, FcCharSetIter *iter)
319 int pos = iter->pos + 1;
327 iter->ucs4 = (FcChar32) FcCharSetNumbers(fcs)[pos] << 8;
328 iter->leaf = FcCharSetLeaf(fcs, pos);
335 FcCharSetIterStart (const FcCharSet *fcs, FcCharSetIter *iter)
339 FcCharSetIterSet (fcs, iter);
343 FcCharSetCopy (FcCharSet *src)
347 if (!FcRefIsConst (&src->ref))
348 FcRefInc (&src->ref);
350 FcCacheObjectReference (src);
356 FcCharSetEqual (const FcCharSet *a, const FcCharSet *b)
358 FcCharSetIter ai, bi;
365 for (FcCharSetIterStart (a, &ai), FcCharSetIterStart (b, &bi);
367 FcCharSetIterNext (a, &ai), FcCharSetIterNext (b, &bi))
369 if (ai.ucs4 != bi.ucs4)
371 for (i = 0; i < 256/32; i++)
372 if (ai.leaf->map[i] != bi.leaf->map[i])
375 return ai.leaf == bi.leaf;
379 FcCharSetAddLeaf (FcCharSet *fcs,
383 FcCharLeaf *new = FcCharSetFindLeafCreate (fcs, ucs4);
391 FcCharSetOperate (const FcCharSet *a,
393 FcBool (*overlap) (FcCharLeaf *result,
394 const FcCharLeaf *al,
395 const FcCharLeaf *bl),
400 FcCharSetIter ai, bi;
404 fcs = FcCharSetCreate ();
407 FcCharSetIterStart (a, &ai);
408 FcCharSetIterStart (b, &bi);
409 while ((ai.leaf || (bonly && bi.leaf)) && (bi.leaf || (aonly && ai.leaf)))
411 if (ai.ucs4 < bi.ucs4)
415 if (!FcCharSetAddLeaf (fcs, ai.ucs4, ai.leaf))
417 FcCharSetIterNext (a, &ai);
422 FcCharSetIterSet (a, &ai);
425 else if (bi.ucs4 < ai.ucs4 )
429 if (!FcCharSetAddLeaf (fcs, bi.ucs4, bi.leaf))
431 FcCharSetIterNext (b, &bi);
436 FcCharSetIterSet (b, &bi);
443 if ((*overlap) (&leaf, ai.leaf, bi.leaf))
445 if (!FcCharSetAddLeaf (fcs, ai.ucs4, &leaf))
448 FcCharSetIterNext (a, &ai);
449 FcCharSetIterNext (b, &bi);
454 FcCharSetDestroy (fcs);
460 FcCharSetIntersectLeaf (FcCharLeaf *result,
461 const FcCharLeaf *al,
462 const FcCharLeaf *bl)
465 FcBool nonempty = FcFalse;
467 for (i = 0; i < 256/32; i++)
468 if ((result->map[i] = al->map[i] & bl->map[i]))
474 FcCharSetIntersect (const FcCharSet *a, const FcCharSet *b)
476 return FcCharSetOperate (a, b, FcCharSetIntersectLeaf, FcFalse, FcFalse);
480 FcCharSetUnionLeaf (FcCharLeaf *result,
481 const FcCharLeaf *al,
482 const FcCharLeaf *bl)
486 for (i = 0; i < 256/32; i++)
487 result->map[i] = al->map[i] | bl->map[i];
492 FcCharSetUnion (const FcCharSet *a, const FcCharSet *b)
494 return FcCharSetOperate (a, b, FcCharSetUnionLeaf, FcTrue, FcTrue);
498 FcCharSetMerge (FcCharSet *a, const FcCharSet *b, FcBool *changed)
506 if (FcRefIsConst (&a->ref)) {
513 *changed = !FcCharSetIsSubset(b, a);
520 an = ai < a->num ? FcCharSetNumbers(a)[ai] : ~0;
521 bn = FcCharSetNumbers(b)[bi];
525 ai = FcCharSetFindLeafForward (a, ai + 1, bn);
531 FcCharLeaf *bl = FcCharSetLeaf(b, bi);
534 if (!FcCharSetAddLeaf (a, bn << 8, bl))
539 FcCharLeaf *al = FcCharSetLeaf(a, ai);
540 FcCharSetUnionLeaf (al, al, bl);
552 FcCharSetSubtractLeaf (FcCharLeaf *result,
553 const FcCharLeaf *al,
554 const FcCharLeaf *bl)
557 FcBool nonempty = FcFalse;
559 for (i = 0; i < 256/32; i++)
560 if ((result->map[i] = al->map[i] & ~bl->map[i]))
566 FcCharSetSubtract (const FcCharSet *a, const FcCharSet *b)
568 return FcCharSetOperate (a, b, FcCharSetSubtractLeaf, FcTrue, FcFalse);
572 FcCharSetHasChar (const FcCharSet *fcs, FcChar32 ucs4)
578 leaf = FcCharSetFindLeaf (fcs, ucs4);
581 return (leaf->map[(ucs4 & 0xff) >> 5] & (1 << (ucs4 & 0x1f))) != 0;
585 FcCharSetPopCount (FcChar32 c1)
587 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
588 return __builtin_popcount (c1);
591 FcChar32 c2 = (c1 >> 1) & 033333333333;
592 c2 = c1 - c2 - ((c2 >> 1) & 033333333333);
593 return (((c2 + (c2 >> 3)) & 030707070707) % 077);
598 FcCharSetIntersectCount (const FcCharSet *a, const FcCharSet *b)
600 FcCharSetIter ai, bi;
605 FcCharSetIterStart (a, &ai);
606 FcCharSetIterStart (b, &bi);
607 while (ai.leaf && bi.leaf)
609 if (ai.ucs4 == bi.ucs4)
611 FcChar32 *am = ai.leaf->map;
612 FcChar32 *bm = bi.leaf->map;
615 count += FcCharSetPopCount (*am++ & *bm++);
616 FcCharSetIterNext (a, &ai);
618 else if (ai.ucs4 < bi.ucs4)
621 FcCharSetIterSet (a, &ai);
623 if (bi.ucs4 < ai.ucs4)
626 FcCharSetIterSet (b, &bi);
634 FcCharSetCount (const FcCharSet *a)
641 for (FcCharSetIterStart (a, &ai); ai.leaf; FcCharSetIterNext (a, &ai))
644 FcChar32 *am = ai.leaf->map;
647 count += FcCharSetPopCount (*am++);
654 FcCharSetSubtractCount (const FcCharSet *a, const FcCharSet *b)
656 FcCharSetIter ai, bi;
661 FcCharSetIterStart (a, &ai);
662 FcCharSetIterStart (b, &bi);
665 if (ai.ucs4 <= bi.ucs4)
667 FcChar32 *am = ai.leaf->map;
669 if (ai.ucs4 == bi.ucs4)
671 FcChar32 *bm = bi.leaf->map;
673 count += FcCharSetPopCount (*am++ & ~*bm++);
678 count += FcCharSetPopCount (*am++);
680 FcCharSetIterNext (a, &ai);
685 FcCharSetIterSet (b, &bi);
693 * return FcTrue iff a is a subset of b
696 FcCharSetIsSubset (const FcCharSet *a, const FcCharSet *b)
707 while (ai < a->num && bi < b->num)
709 an = FcCharSetNumbers(a)[ai];
710 bn = FcCharSetNumbers(b)[bi];
712 * Check matching pages
716 FcChar32 *am = FcCharSetLeaf(a, ai)->map;
717 FcChar32 *bm = FcCharSetLeaf(b, bi)->map;
723 * Does am have any bits not in bm?
733 * Does a have any pages not in b?
739 bi = FcCharSetFindLeafForward (b, bi + 1, an);
745 * did we look at every page?
751 * These two functions efficiently walk the entire charmap for
752 * other software (like pango) that want their own copy
756 FcCharSetNextPage (const FcCharSet *a,
757 FcChar32 map[FC_CHARSET_MAP_SIZE],
764 return FC_CHARSET_DONE;
766 FcCharSetIterSet (a, &ai);
768 return FC_CHARSET_DONE;
771 * Save current information
774 memcpy (map, ai.leaf->map, sizeof (ai.leaf->map));
778 FcCharSetIterNext (a, &ai);
785 FcCharSetFirstPage (const FcCharSet *a,
786 FcChar32 map[FC_CHARSET_MAP_SIZE],
790 return FcCharSetNextPage (a, map, next);
794 * old coverage API, rather hard to use correctly
798 FcCharSetCoverage (const FcCharSet *a, FcChar32 page, FcChar32 *result)
803 FcCharSetIterSet (a, &ai);
806 memset (result, '\0', 256 / 8);
811 memcpy (result, ai.leaf->map, sizeof (ai.leaf->map));
812 FcCharSetIterNext (a, &ai);
819 FcNameParseRange (FcChar8 **string, FcChar32 *pfirst, FcChar32 *plast)
821 char *s = (char *) *string;
829 first = last = strtol (s, &s, 16);
838 last = strtol (s, &s, 16);
843 if (s == t || first < 0 || last < 0 || last < first || last > 0x10ffff)
846 *string = (FcChar8 *) s;
853 FcNameParseCharSet (FcChar8 *string)
856 FcChar32 first, last;
858 c = FcCharSetCreate ();
865 if (!FcNameParseRange (&string, &first, &last))
868 for (u = first; u < last + 1; u++)
869 FcCharSetAddChar (c, u);
873 FcCharSetDestroy (c);
879 FcNameUnparseUnicode (FcStrBuf *buf, FcChar32 u)
881 FcChar8 buf_static[64];
882 snprintf ((char *) buf_static, sizeof (buf_static), "%x", u);
883 FcStrBufString (buf, buf_static);
887 FcNameUnparseCharSet (FcStrBuf *buf, const FcCharSet *c)
890 FcChar32 first, last;
896 first = last = 0x7FFFFFFF;
898 for (FcCharSetIterStart (c, &ci);
900 FcCharSetIterNext (c, &ci))
902 for (i = 0; i < 256/32; i++)
904 FcChar32 bits = ci.leaf->map[i];
905 FcChar32 u = ci.ucs4 + i * 32;
915 FcStrBufChar (buf, '-');
916 FcNameUnparseUnicode (buf, last);
918 if (last != 0x7FFFFFFF)
919 FcStrBufChar (buf, ' ');
920 /* Start new range. */
922 FcNameUnparseUnicode (buf, u);
933 FcStrBufChar (buf, '-');
934 FcNameUnparseUnicode (buf, last);
940 FcCharSetIter ci, checki;
942 /* null terminate for parser */
943 FcStrBufChar (buf, '\0');
944 /* step back over null for life after test */
946 check = FcNameParseCharSet (buf->buf + len);
947 FcCharSetIterStart (c, &ci);
948 FcCharSetIterStart (check, &checki);
949 while (ci.leaf || checki.leaf)
951 if (ci.ucs4 < checki.ucs4)
953 printf ("Missing leaf node at 0x%x\n", ci.ucs4);
954 FcCharSetIterNext (c, &ci);
956 else if (checki.ucs4 < ci.ucs4)
958 printf ("Extra leaf node at 0x%x\n", checki.ucs4);
959 FcCharSetIterNext (check, &checki);
964 FcChar32 *cm = ci.leaf->map;
965 FcChar32 *checkm = checki.leaf->map;
967 for (i = 0; i < 256; i += 32)
970 printf ("Mismatching sets at 0x%08x: 0x%08x != 0x%08x\n",
971 ci.ucs4 + i, *cm, *checkm);
975 FcCharSetIterNext (c, &ci);
976 FcCharSetIterNext (check, &checki);
979 if ((missing = FcCharSetSubtractCount (c, check)))
980 printf ("%d missing in reparsed result\n", missing);
981 if ((missing = FcCharSetSubtractCount (check, c)))
982 printf ("%d extra in reparsed result\n", missing);
983 FcCharSetDestroy (check);
990 typedef struct _FcCharLeafEnt FcCharLeafEnt;
992 struct _FcCharLeafEnt {
998 #define FC_CHAR_LEAF_BLOCK (4096 / sizeof (FcCharLeafEnt))
999 #define FC_CHAR_LEAF_HASH_SIZE 257
1001 typedef struct _FcCharSetEnt FcCharSetEnt;
1003 struct _FcCharSetEnt {
1009 typedef struct _FcCharSetOrigEnt FcCharSetOrigEnt;
1011 struct _FcCharSetOrigEnt {
1012 FcCharSetOrigEnt *next;
1013 const FcCharSet *orig;
1014 const FcCharSet *frozen;
1017 #define FC_CHAR_SET_HASH_SIZE 67
1019 struct _FcCharSetFreezer {
1020 FcCharLeafEnt *leaf_hash_table[FC_CHAR_LEAF_HASH_SIZE];
1021 FcCharLeafEnt **leaf_blocks;
1022 int leaf_block_count;
1023 FcCharSetEnt *set_hash_table[FC_CHAR_SET_HASH_SIZE];
1024 FcCharSetOrigEnt *orig_hash_table[FC_CHAR_SET_HASH_SIZE];
1025 FcCharLeafEnt *current_block;
1029 int leaves_allocated;
1030 int charsets_allocated;
1033 static FcCharLeafEnt *
1034 FcCharLeafEntCreate (FcCharSetFreezer *freezer)
1036 if (!freezer->leaf_remain)
1038 FcCharLeafEnt **newBlocks;
1040 freezer->leaf_block_count++;
1041 newBlocks = realloc (freezer->leaf_blocks, freezer->leaf_block_count * sizeof (FcCharLeafEnt *));
1044 freezer->leaf_blocks = newBlocks;
1045 freezer->current_block = freezer->leaf_blocks[freezer->leaf_block_count-1] = malloc (FC_CHAR_LEAF_BLOCK * sizeof (FcCharLeafEnt));
1046 if (!freezer->current_block)
1048 freezer->leaf_remain = FC_CHAR_LEAF_BLOCK;
1050 freezer->leaf_remain--;
1051 freezer->leaves_allocated++;
1052 return freezer->current_block++;
1056 FcCharLeafHash (FcCharLeaf *leaf)
1061 for (i = 0; i < 256/32; i++)
1062 hash = ((hash << 1) | (hash >> 31)) ^ leaf->map[i];
1067 FcCharSetFreezeLeaf (FcCharSetFreezer *freezer, FcCharLeaf *leaf)
1069 FcChar32 hash = FcCharLeafHash (leaf);
1070 FcCharLeafEnt **bucket = &freezer->leaf_hash_table[hash % FC_CHAR_LEAF_HASH_SIZE];
1073 for (ent = *bucket; ent; ent = ent->next)
1075 if (ent->hash == hash && !memcmp (&ent->leaf, leaf, sizeof (FcCharLeaf)))
1079 ent = FcCharLeafEntCreate(freezer);
1084 ent->next = *bucket;
1090 FcCharSetHash (FcCharSet *fcs)
1095 /* hash in leaves */
1096 for (i = 0; i < fcs->num; i++)
1097 hash = ((hash << 1) | (hash >> 31)) ^ FcCharLeafHash (FcCharSetLeaf(fcs,i));
1098 /* hash in numbers */
1099 for (i = 0; i < fcs->num; i++)
1100 hash = ((hash << 1) | (hash >> 31)) ^ *FcCharSetNumbers(fcs);
1105 FcCharSetFreezeOrig (FcCharSetFreezer *freezer, const FcCharSet *orig, const FcCharSet *frozen)
1107 FcCharSetOrigEnt **bucket = &freezer->orig_hash_table[((uintptr_t) orig) & FC_CHAR_SET_HASH_SIZE];
1108 FcCharSetOrigEnt *ent;
1110 ent = malloc (sizeof (FcCharSetOrigEnt));
1114 ent->frozen = frozen;
1115 ent->next = *bucket;
1121 FcCharSetFreezeBase (FcCharSetFreezer *freezer, FcCharSet *fcs)
1123 FcChar32 hash = FcCharSetHash (fcs);
1124 FcCharSetEnt **bucket = &freezer->set_hash_table[hash % FC_CHAR_SET_HASH_SIZE];
1129 for (ent = *bucket; ent; ent = ent->next)
1131 if (ent->hash == hash &&
1132 ent->set.num == fcs->num &&
1133 !memcmp (FcCharSetNumbers(&ent->set),
1134 FcCharSetNumbers(fcs),
1135 fcs->num * sizeof (FcChar16)))
1140 for (i = 0; i < fcs->num; i++)
1141 if (FcCharSetLeaf(&ent->set, i) != FcCharSetLeaf(fcs, i))
1148 size = (sizeof (FcCharSetEnt) +
1149 fcs->num * sizeof (FcCharLeaf *) +
1150 fcs->num * sizeof (FcChar16));
1151 ent = malloc (size);
1155 freezer->charsets_allocated++;
1157 FcRefSetConst (&ent->set.ref);
1158 ent->set.num = fcs->num;
1161 intptr_t *ent_leaves;
1163 ent->set.leaves_offset = sizeof (ent->set);
1164 ent->set.numbers_offset = (ent->set.leaves_offset +
1165 fcs->num * sizeof (intptr_t));
1167 ent_leaves = FcCharSetLeaves (&ent->set);
1168 for (i = 0; i < fcs->num; i++)
1169 ent_leaves[i] = FcPtrToOffset (ent_leaves,
1170 FcCharSetLeaf (fcs, i));
1171 memcpy (FcCharSetNumbers (&ent->set),
1172 FcCharSetNumbers (fcs),
1173 fcs->num * sizeof (FcChar16));
1177 ent->set.leaves_offset = 0;
1178 ent->set.numbers_offset = 0;
1182 ent->next = *bucket;
1188 static const FcCharSet *
1189 FcCharSetFindFrozen (FcCharSetFreezer *freezer, const FcCharSet *orig)
1191 FcCharSetOrigEnt **bucket = &freezer->orig_hash_table[((uintptr_t) orig) & FC_CHAR_SET_HASH_SIZE];
1192 FcCharSetOrigEnt *ent;
1194 for (ent = *bucket; ent; ent = ent->next)
1195 if (ent->orig == orig)
1201 FcCharSetFreeze (FcCharSetFreezer *freezer, const FcCharSet *fcs)
1204 const FcCharSet *n = 0;
1208 b = FcCharSetCreate ();
1211 for (i = 0; i < fcs->num; i++)
1213 l = FcCharSetFreezeLeaf (freezer, FcCharSetLeaf(fcs, i));
1216 if (!FcCharSetInsertLeaf (b, FcCharSetNumbers(fcs)[i] << 8, l))
1219 n = FcCharSetFreezeBase (freezer, b);
1220 if (!FcCharSetFreezeOrig (freezer, fcs, n))
1225 freezer->charsets_seen++;
1226 freezer->leaves_seen += fcs->num;
1229 free (FcCharSetLeaves (b));
1231 free (FcCharSetNumbers (b));
1238 FcCharSetFreezerCreate (void)
1240 FcCharSetFreezer *freezer;
1242 freezer = calloc (1, sizeof (FcCharSetFreezer));
1247 FcCharSetFreezerDestroy (FcCharSetFreezer *freezer)
1251 if (FcDebug() & FC_DBG_CACHE)
1253 printf ("\ncharsets %d -> %d leaves %d -> %d\n",
1254 freezer->charsets_seen, freezer->charsets_allocated,
1255 freezer->leaves_seen, freezer->leaves_allocated);
1257 for (i = 0; i < FC_CHAR_SET_HASH_SIZE; i++)
1259 FcCharSetEnt *ent, *next;
1260 for (ent = freezer->set_hash_table[i]; ent; ent = next)
1267 for (i = 0; i < FC_CHAR_SET_HASH_SIZE; i++)
1269 FcCharSetOrigEnt *ent, *next;
1270 for (ent = freezer->orig_hash_table[i]; ent; ent = next)
1277 for (i = 0; i < freezer->leaf_block_count; i++)
1278 free (freezer->leaf_blocks[i]);
1280 free (freezer->leaf_blocks);
1285 FcCharSetSerializeAlloc (FcSerialize *serialize, const FcCharSet *cs)
1291 if (!FcRefIsConst (&cs->ref))
1293 if (!serialize->cs_freezer)
1295 serialize->cs_freezer = FcCharSetFreezerCreate ();
1296 if (!serialize->cs_freezer)
1299 if (FcCharSetFindFrozen (serialize->cs_freezer, cs))
1302 cs = FcCharSetFreeze (serialize->cs_freezer, cs);
1305 leaves = FcCharSetLeaves (cs);
1306 numbers = FcCharSetNumbers (cs);
1308 if (!FcSerializeAlloc (serialize, cs, sizeof (FcCharSet)))
1310 if (!FcSerializeAlloc (serialize, leaves, cs->num * sizeof (intptr_t)))
1312 if (!FcSerializeAlloc (serialize, numbers, cs->num * sizeof (FcChar16)))
1314 for (i = 0; i < cs->num; i++)
1315 if (!FcSerializeAlloc (serialize, FcCharSetLeaf(cs, i),
1316 sizeof (FcCharLeaf)))
1322 FcCharSetSerialize(FcSerialize *serialize, const FcCharSet *cs)
1324 FcCharSet *cs_serialized;
1325 intptr_t *leaves, *leaves_serialized;
1326 FcChar16 *numbers, *numbers_serialized;
1327 FcCharLeaf *leaf, *leaf_serialized;
1330 if (!FcRefIsConst (&cs->ref) && serialize->cs_freezer)
1332 cs = FcCharSetFindFrozen (serialize->cs_freezer, cs);
1337 cs_serialized = FcSerializePtr (serialize, cs);
1341 FcRefSetConst (&cs_serialized->ref);
1342 cs_serialized->num = cs->num;
1346 leaves = FcCharSetLeaves (cs);
1347 leaves_serialized = FcSerializePtr (serialize, leaves);
1348 if (!leaves_serialized)
1351 cs_serialized->leaves_offset = FcPtrToOffset (cs_serialized,
1354 numbers = FcCharSetNumbers (cs);
1355 numbers_serialized = FcSerializePtr (serialize, numbers);
1359 cs_serialized->numbers_offset = FcPtrToOffset (cs_serialized,
1360 numbers_serialized);
1362 for (i = 0; i < cs->num; i++)
1364 leaf = FcCharSetLeaf (cs, i);
1365 leaf_serialized = FcSerializePtr (serialize, leaf);
1366 if (!leaf_serialized)
1368 *leaf_serialized = *leaf;
1369 leaves_serialized[i] = FcPtrToOffset (leaves_serialized,
1371 numbers_serialized[i] = numbers[i];
1376 cs_serialized->leaves_offset = 0;
1377 cs_serialized->numbers_offset = 0;
1380 return cs_serialized;
1382 #define __fccharset__
1383 #include "fcaliastail.h"
1384 #undef __fccharset__