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;
48 return FcCharSetCreate ();
52 FcCharSetDestroy (FcCharSet *fcs)
58 if (FcRefIsConst (&fcs->ref))
60 FcCacheObjectDereference (fcs);
63 if (FcRefDec (&fcs->ref) != 1)
65 for (i = 0; i < fcs->num; i++)
66 free (FcCharSetLeaf (fcs, i));
69 free (FcCharSetLeaves (fcs));
70 free (FcCharSetNumbers (fcs));
77 * Search for the leaf containing with the specified num.
78 * Return its index if it exists, otherwise return negative of
79 * the (position + 1) where it should be inserted
84 FcCharSetFindLeafForward (const FcCharSet *fcs, int start, FcChar16 num)
86 FcChar16 *numbers = FcCharSetNumbers(fcs);
89 int high = fcs->num - 1;
95 int mid = (low + high) >> 1;
104 if (high < 0 || (high < fcs->num && numbers[high] < num))
110 * Locate the leaf containing the specified char, return
111 * its index if it exists, otherwise return negative of
112 * the (position + 1) where it should be inserted
116 FcCharSetFindLeafPos (const FcCharSet *fcs, FcChar32 ucs4)
118 return FcCharSetFindLeafForward (fcs, 0, ucs4 >> 8);
122 FcCharSetFindLeaf (const FcCharSet *fcs, FcChar32 ucs4)
124 int pos = FcCharSetFindLeafPos (fcs, ucs4);
126 return FcCharSetLeaf(fcs, pos);
130 #define FC_IS_ZERO_OR_POWER_OF_TWO(x) (!((x) & ((x)-1)))
133 FcCharSetPutLeaf (FcCharSet *fcs,
138 intptr_t *leaves = FcCharSetLeaves (fcs);
139 FcChar16 *numbers = FcCharSetNumbers (fcs);
145 if (FC_IS_ZERO_OR_POWER_OF_TWO (fcs->num))
149 unsigned int alloced = 8;
150 leaves = malloc (alloced * sizeof (*leaves));
151 numbers = malloc (alloced * sizeof (*numbers));
155 unsigned int alloced = fcs->num;
156 intptr_t *new_leaves, distance;
159 new_leaves = realloc (leaves, alloced * sizeof (*leaves));
160 numbers = realloc (numbers, alloced * sizeof (*numbers));
162 distance = (intptr_t) new_leaves - (intptr_t) leaves;
163 if (new_leaves && distance)
166 for (i = 0; i < fcs->num; i++)
167 new_leaves[i] -= distance;
172 if (!leaves || !numbers)
175 fcs->leaves_offset = FcPtrToOffset (fcs, leaves);
176 fcs->numbers_offset = FcPtrToOffset (fcs, numbers);
179 memmove (leaves + pos + 1, leaves + pos,
180 (fcs->num - pos) * sizeof (*leaves));
181 memmove (numbers + pos + 1, numbers + pos,
182 (fcs->num - pos) * sizeof (*numbers));
183 numbers[pos] = (FcChar16) ucs4;
184 leaves[pos] = FcPtrToOffset (leaves, leaf);
190 * Locate the leaf containing the specified char, creating it
195 FcCharSetFindLeafCreate (FcCharSet *fcs, FcChar32 ucs4)
200 pos = FcCharSetFindLeafPos (fcs, ucs4);
202 return FcCharSetLeaf(fcs, pos);
204 leaf = calloc (1, sizeof (FcCharLeaf));
209 if (!FcCharSetPutLeaf (fcs, ucs4, leaf, pos))
218 FcCharSetInsertLeaf (FcCharSet *fcs, FcChar32 ucs4, FcCharLeaf *leaf)
222 pos = FcCharSetFindLeafPos (fcs, ucs4);
225 free (FcCharSetLeaf (fcs, pos));
226 FcCharSetLeaves(fcs)[pos] = FcPtrToOffset (FcCharSetLeaves(fcs),
231 return FcCharSetPutLeaf (fcs, ucs4, leaf, pos);
235 FcCharSetAddChar (FcCharSet *fcs, FcChar32 ucs4)
240 if (fcs == NULL || FcRefIsConst (&fcs->ref))
242 leaf = FcCharSetFindLeafCreate (fcs, ucs4);
245 b = &leaf->map[(ucs4 & 0xff) >> 5];
246 *b |= (1 << (ucs4 & 0x1f));
251 FcCharSetDelChar (FcCharSet *fcs, FcChar32 ucs4)
256 if (fcs == NULL || FcRefIsConst (&fcs->ref))
258 leaf = FcCharSetFindLeaf (fcs, ucs4);
261 b = &leaf->map[(ucs4 & 0xff) >> 5];
262 *b &= ~(1 << (ucs4 & 0x1f));
263 /* We don't bother removing the leaf if it's empty */
268 * An iterator for the leaves of a charset
271 typedef struct _fcCharSetIter {
278 * Set iter->leaf to the leaf containing iter->ucs4 or higher
282 FcCharSetIterSet (const FcCharSet *fcs, FcCharSetIter *iter)
284 int pos = FcCharSetFindLeafPos (fcs, iter->ucs4);
295 iter->ucs4 = (FcChar32) FcCharSetNumbers(fcs)[pos] << 8;
297 iter->leaf = FcCharSetLeaf(fcs, pos);
302 FcCharSetIterNext (const FcCharSet *fcs, FcCharSetIter *iter)
304 int pos = iter->pos + 1;
312 iter->ucs4 = (FcChar32) FcCharSetNumbers(fcs)[pos] << 8;
313 iter->leaf = FcCharSetLeaf(fcs, pos);
320 FcCharSetIterStart (const FcCharSet *fcs, FcCharSetIter *iter)
324 FcCharSetIterSet (fcs, iter);
328 FcCharSetCopy (FcCharSet *src)
332 if (!FcRefIsConst (&src->ref))
333 FcRefInc (&src->ref);
335 FcCacheObjectReference (src);
341 FcCharSetEqual (const FcCharSet *a, const FcCharSet *b)
343 FcCharSetIter ai, bi;
350 for (FcCharSetIterStart (a, &ai), FcCharSetIterStart (b, &bi);
352 FcCharSetIterNext (a, &ai), FcCharSetIterNext (b, &bi))
354 if (ai.ucs4 != bi.ucs4)
356 for (i = 0; i < 256/32; i++)
357 if (ai.leaf->map[i] != bi.leaf->map[i])
360 return ai.leaf == bi.leaf;
364 FcCharSetAddLeaf (FcCharSet *fcs,
368 FcCharLeaf *new = FcCharSetFindLeafCreate (fcs, ucs4);
376 FcCharSetOperate (const FcCharSet *a,
378 FcBool (*overlap) (FcCharLeaf *result,
379 const FcCharLeaf *al,
380 const FcCharLeaf *bl),
385 FcCharSetIter ai, bi;
389 fcs = FcCharSetCreate ();
392 FcCharSetIterStart (a, &ai);
393 FcCharSetIterStart (b, &bi);
394 while ((ai.leaf || (bonly && bi.leaf)) && (bi.leaf || (aonly && ai.leaf)))
396 if (ai.ucs4 < bi.ucs4)
400 if (!FcCharSetAddLeaf (fcs, ai.ucs4, ai.leaf))
402 FcCharSetIterNext (a, &ai);
407 FcCharSetIterSet (a, &ai);
410 else if (bi.ucs4 < ai.ucs4 )
414 if (!FcCharSetAddLeaf (fcs, bi.ucs4, bi.leaf))
416 FcCharSetIterNext (b, &bi);
421 FcCharSetIterSet (b, &bi);
428 if ((*overlap) (&leaf, ai.leaf, bi.leaf))
430 if (!FcCharSetAddLeaf (fcs, ai.ucs4, &leaf))
433 FcCharSetIterNext (a, &ai);
434 FcCharSetIterNext (b, &bi);
439 FcCharSetDestroy (fcs);
445 FcCharSetIntersectLeaf (FcCharLeaf *result,
446 const FcCharLeaf *al,
447 const FcCharLeaf *bl)
450 FcBool nonempty = FcFalse;
452 for (i = 0; i < 256/32; i++)
453 if ((result->map[i] = al->map[i] & bl->map[i]))
459 FcCharSetIntersect (const FcCharSet *a, const FcCharSet *b)
461 return FcCharSetOperate (a, b, FcCharSetIntersectLeaf, FcFalse, FcFalse);
465 FcCharSetUnionLeaf (FcCharLeaf *result,
466 const FcCharLeaf *al,
467 const FcCharLeaf *bl)
471 for (i = 0; i < 256/32; i++)
472 result->map[i] = al->map[i] | bl->map[i];
477 FcCharSetUnion (const FcCharSet *a, const FcCharSet *b)
479 return FcCharSetOperate (a, b, FcCharSetUnionLeaf, FcTrue, FcTrue);
483 FcCharSetMerge (FcCharSet *a, const FcCharSet *b, FcBool *changed)
491 if (FcRefIsConst (&a->ref)) {
498 *changed = !FcCharSetIsSubset(b, a);
505 an = ai < a->num ? FcCharSetNumbers(a)[ai] : ~0;
506 bn = FcCharSetNumbers(b)[bi];
510 ai = FcCharSetFindLeafForward (a, ai + 1, bn);
516 FcCharLeaf *bl = FcCharSetLeaf(b, bi);
519 if (!FcCharSetAddLeaf (a, bn << 8, bl))
524 FcCharLeaf *al = FcCharSetLeaf(a, ai);
525 FcCharSetUnionLeaf (al, al, bl);
537 FcCharSetSubtractLeaf (FcCharLeaf *result,
538 const FcCharLeaf *al,
539 const FcCharLeaf *bl)
542 FcBool nonempty = FcFalse;
544 for (i = 0; i < 256/32; i++)
545 if ((result->map[i] = al->map[i] & ~bl->map[i]))
551 FcCharSetSubtract (const FcCharSet *a, const FcCharSet *b)
553 return FcCharSetOperate (a, b, FcCharSetSubtractLeaf, FcTrue, FcFalse);
557 FcCharSetHasChar (const FcCharSet *fcs, FcChar32 ucs4)
563 leaf = FcCharSetFindLeaf (fcs, ucs4);
566 return (leaf->map[(ucs4 & 0xff) >> 5] & (1 << (ucs4 & 0x1f))) != 0;
570 FcCharSetPopCount (FcChar32 c1)
572 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
573 return __builtin_popcount (c1);
576 FcChar32 c2 = (c1 >> 1) & 033333333333;
577 c2 = c1 - c2 - ((c2 >> 1) & 033333333333);
578 return (((c2 + (c2 >> 3)) & 030707070707) % 077);
583 FcCharSetIntersectCount (const FcCharSet *a, const FcCharSet *b)
585 FcCharSetIter ai, bi;
590 FcCharSetIterStart (a, &ai);
591 FcCharSetIterStart (b, &bi);
592 while (ai.leaf && bi.leaf)
594 if (ai.ucs4 == bi.ucs4)
596 FcChar32 *am = ai.leaf->map;
597 FcChar32 *bm = bi.leaf->map;
600 count += FcCharSetPopCount (*am++ & *bm++);
601 FcCharSetIterNext (a, &ai);
603 else if (ai.ucs4 < bi.ucs4)
606 FcCharSetIterSet (a, &ai);
608 if (bi.ucs4 < ai.ucs4)
611 FcCharSetIterSet (b, &bi);
619 FcCharSetCount (const FcCharSet *a)
626 for (FcCharSetIterStart (a, &ai); ai.leaf; FcCharSetIterNext (a, &ai))
629 FcChar32 *am = ai.leaf->map;
632 count += FcCharSetPopCount (*am++);
639 FcCharSetSubtractCount (const FcCharSet *a, const FcCharSet *b)
641 FcCharSetIter ai, bi;
646 FcCharSetIterStart (a, &ai);
647 FcCharSetIterStart (b, &bi);
650 if (ai.ucs4 <= bi.ucs4)
652 FcChar32 *am = ai.leaf->map;
654 if (ai.ucs4 == bi.ucs4)
656 FcChar32 *bm = bi.leaf->map;
658 count += FcCharSetPopCount (*am++ & ~*bm++);
663 count += FcCharSetPopCount (*am++);
665 FcCharSetIterNext (a, &ai);
670 FcCharSetIterSet (b, &bi);
678 * return FcTrue iff a is a subset of b
681 FcCharSetIsSubset (const FcCharSet *a, const FcCharSet *b)
692 while (ai < a->num && bi < b->num)
694 an = FcCharSetNumbers(a)[ai];
695 bn = FcCharSetNumbers(b)[bi];
697 * Check matching pages
701 FcChar32 *am = FcCharSetLeaf(a, ai)->map;
702 FcChar32 *bm = FcCharSetLeaf(b, bi)->map;
708 * Does am have any bits not in bm?
718 * Does a have any pages not in b?
724 bi = FcCharSetFindLeafForward (b, bi + 1, an);
730 * did we look at every page?
736 * These two functions efficiently walk the entire charmap for
737 * other software (like pango) that want their own copy
741 FcCharSetNextPage (const FcCharSet *a,
742 FcChar32 map[FC_CHARSET_MAP_SIZE],
749 return FC_CHARSET_DONE;
751 FcCharSetIterSet (a, &ai);
753 return FC_CHARSET_DONE;
756 * Save current information
759 memcpy (map, ai.leaf->map, sizeof (ai.leaf->map));
763 FcCharSetIterNext (a, &ai);
770 FcCharSetFirstPage (const FcCharSet *a,
771 FcChar32 map[FC_CHARSET_MAP_SIZE],
775 return FcCharSetNextPage (a, map, next);
779 * old coverage API, rather hard to use correctly
783 FcCharSetCoverage (const FcCharSet *a, FcChar32 page, FcChar32 *result)
788 FcCharSetIterSet (a, &ai);
791 memset (result, '\0', 256 / 8);
796 memcpy (result, ai.leaf->map, sizeof (ai.leaf->map));
797 FcCharSetIterNext (a, &ai);
804 * ASCII representation of charsets.
806 * Each leaf is represented as 9 32-bit values, the code of the first character followed
807 * by 8 32 bit values for the leaf itself. Each value is encoded as 5 ASCII characters,
808 * only 85 different values are used to avoid control characters as well as the other
809 * characters used to encode font names. 85**5 > 2^32 so things work out, but
810 * it's not exactly human readable output. As a special case, 0 is encoded as a space
813 static const unsigned char charToValue[256] = {
814 /* "" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
815 /* "\b" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
816 /* "\020" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
817 /* "\030" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
818 /* " " */ 0xff, 0x00, 0xff, 0x01, 0x02, 0x03, 0x04, 0xff,
819 /* "(" */ 0x05, 0x06, 0x07, 0x08, 0xff, 0xff, 0x09, 0x0a,
820 /* "0" */ 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12,
821 /* "8" */ 0x13, 0x14, 0xff, 0x15, 0x16, 0xff, 0x17, 0x18,
822 /* "@" */ 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
823 /* "H" */ 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
824 /* "P" */ 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30,
825 /* "X" */ 0x31, 0x32, 0x33, 0x34, 0xff, 0x35, 0x36, 0xff,
826 /* "`" */ 0xff, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d,
827 /* "h" */ 0x3e, 0x3f, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45,
828 /* "p" */ 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d,
829 /* "x" */ 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53, 0x54, 0xff,
830 /* "\200" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
831 /* "\210" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
832 /* "\220" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
833 /* "\230" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
834 /* "\240" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
835 /* "\250" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
836 /* "\260" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
837 /* "\270" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
838 /* "\300" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
839 /* "\310" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
840 /* "\320" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
841 /* "\330" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
842 /* "\340" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
843 /* "\350" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
844 /* "\360" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
845 /* "\370" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
848 static const FcChar8 valueToChar[0x55] = {
849 /* 0x00 */ '!', '#', '$', '%', '&', '(', ')', '*',
850 /* 0x08 */ '+', '.', '/', '0', '1', '2', '3', '4',
851 /* 0x10 */ '5', '6', '7', '8', '9', ';', '<', '>',
852 /* 0x18 */ '?', '@', 'A', 'B', 'C', 'D', 'E', 'F',
853 /* 0x20 */ 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
854 /* 0x28 */ 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
855 /* 0x30 */ 'W', 'X', 'Y', 'Z', '[', ']', '^', 'a',
856 /* 0x38 */ 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
857 /* 0x40 */ 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q',
858 /* 0x48 */ 'r', 's', 't', 'u', 'v', 'w', 'x', 'y',
859 /* 0x50 */ 'z', '{', '|', '}', '~',
863 FcCharSetParseValue (FcChar8 *string, FcChar32 *value)
877 for (i = 0; i < 5; i++)
879 if (!(c = (FcChar32) (unsigned char) *string++))
892 FcCharSetUnparseValue (FcStrBuf *buf, FcChar32 value)
897 return FcStrBufChar (buf, ' ');
902 FcChar8 *s = string + 5;
904 for (i = 0; i < 5; i++)
906 *--s = valueToChar[value % 85];
909 for (i = 0; i < 5; i++)
910 if (!FcStrBufChar (buf, *s++))
917 FcNameParseCharSet (FcChar8 *string)
926 c = FcCharSetCreate ();
931 string = FcCharSetParseValue (string, &ucs4);
935 for (i = 0; i < 256/32; i++)
937 string = FcCharSetParseValue (string, &temp.map[i]);
944 leaf = malloc (sizeof (FcCharLeaf));
948 if (!FcCharSetInsertLeaf (c, ucs4, leaf))
956 free (FcCharSetLeaves (c));
960 free (FcCharSetNumbers (c));
968 FcNameUnparseCharSet (FcStrBuf *buf, const FcCharSet *c)
976 for (FcCharSetIterStart (c, &ci);
978 FcCharSetIterNext (c, &ci))
980 if (!FcCharSetUnparseValue (buf, ci.ucs4))
982 for (i = 0; i < 256/32; i++)
983 if (!FcCharSetUnparseValue (buf, ci.leaf->map[i]))
990 FcCharSetIter ci, checki;
992 /* null terminate for parser */
993 FcStrBufChar (buf, '\0');
994 /* step back over null for life after test */
996 check = FcNameParseCharSet (buf->buf + len);
997 FcCharSetIterStart (c, &ci);
998 FcCharSetIterStart (check, &checki);
999 while (ci.leaf || checki.leaf)
1001 if (ci.ucs4 < checki.ucs4)
1003 printf ("Missing leaf node at 0x%x\n", ci.ucs4);
1004 FcCharSetIterNext (c, &ci);
1006 else if (checki.ucs4 < ci.ucs4)
1008 printf ("Extra leaf node at 0x%x\n", checki.ucs4);
1009 FcCharSetIterNext (check, &checki);
1014 FcChar32 *cm = ci.leaf->map;
1015 FcChar32 *checkm = checki.leaf->map;
1017 for (i = 0; i < 256; i += 32)
1020 printf ("Mismatching sets at 0x%08x: 0x%08x != 0x%08x\n",
1021 ci.ucs4 + i, *cm, *checkm);
1025 FcCharSetIterNext (c, &ci);
1026 FcCharSetIterNext (check, &checki);
1029 if ((missing = FcCharSetSubtractCount (c, check)))
1030 printf ("%d missing in reparsed result\n", missing);
1031 if ((missing = FcCharSetSubtractCount (check, c)))
1032 printf ("%d extra in reparsed result\n", missing);
1033 FcCharSetDestroy (check);
1040 typedef struct _FcCharLeafEnt FcCharLeafEnt;
1042 struct _FcCharLeafEnt {
1043 FcCharLeafEnt *next;
1048 #define FC_CHAR_LEAF_BLOCK (4096 / sizeof (FcCharLeafEnt))
1049 #define FC_CHAR_LEAF_HASH_SIZE 257
1051 typedef struct _FcCharSetEnt FcCharSetEnt;
1053 struct _FcCharSetEnt {
1059 typedef struct _FcCharSetOrigEnt FcCharSetOrigEnt;
1061 struct _FcCharSetOrigEnt {
1062 FcCharSetOrigEnt *next;
1063 const FcCharSet *orig;
1064 const FcCharSet *frozen;
1067 #define FC_CHAR_SET_HASH_SIZE 67
1069 struct _FcCharSetFreezer {
1070 FcCharLeafEnt *leaf_hash_table[FC_CHAR_LEAF_HASH_SIZE];
1071 FcCharLeafEnt **leaf_blocks;
1072 int leaf_block_count;
1073 FcCharSetEnt *set_hash_table[FC_CHAR_SET_HASH_SIZE];
1074 FcCharSetOrigEnt *orig_hash_table[FC_CHAR_SET_HASH_SIZE];
1075 FcCharLeafEnt *current_block;
1079 int leaves_allocated;
1080 int charsets_allocated;
1083 static FcCharLeafEnt *
1084 FcCharLeafEntCreate (FcCharSetFreezer *freezer)
1086 if (!freezer->leaf_remain)
1088 FcCharLeafEnt **newBlocks;
1090 freezer->leaf_block_count++;
1091 newBlocks = realloc (freezer->leaf_blocks, freezer->leaf_block_count * sizeof (FcCharLeafEnt *));
1094 freezer->leaf_blocks = newBlocks;
1095 freezer->current_block = freezer->leaf_blocks[freezer->leaf_block_count-1] = malloc (FC_CHAR_LEAF_BLOCK * sizeof (FcCharLeafEnt));
1096 if (!freezer->current_block)
1098 freezer->leaf_remain = FC_CHAR_LEAF_BLOCK;
1100 freezer->leaf_remain--;
1101 freezer->leaves_allocated++;
1102 return freezer->current_block++;
1106 FcCharLeafHash (FcCharLeaf *leaf)
1111 for (i = 0; i < 256/32; i++)
1112 hash = ((hash << 1) | (hash >> 31)) ^ leaf->map[i];
1117 FcCharSetFreezeLeaf (FcCharSetFreezer *freezer, FcCharLeaf *leaf)
1119 FcChar32 hash = FcCharLeafHash (leaf);
1120 FcCharLeafEnt **bucket = &freezer->leaf_hash_table[hash % FC_CHAR_LEAF_HASH_SIZE];
1123 for (ent = *bucket; ent; ent = ent->next)
1125 if (ent->hash == hash && !memcmp (&ent->leaf, leaf, sizeof (FcCharLeaf)))
1129 ent = FcCharLeafEntCreate(freezer);
1134 ent->next = *bucket;
1140 FcCharSetHash (FcCharSet *fcs)
1145 /* hash in leaves */
1146 for (i = 0; i < fcs->num; i++)
1147 hash = ((hash << 1) | (hash >> 31)) ^ FcCharLeafHash (FcCharSetLeaf(fcs,i));
1148 /* hash in numbers */
1149 for (i = 0; i < fcs->num; i++)
1150 hash = ((hash << 1) | (hash >> 31)) ^ *FcCharSetNumbers(fcs);
1155 FcCharSetFreezeOrig (FcCharSetFreezer *freezer, const FcCharSet *orig, const FcCharSet *frozen)
1157 FcCharSetOrigEnt **bucket = &freezer->orig_hash_table[((uintptr_t) orig) & FC_CHAR_SET_HASH_SIZE];
1158 FcCharSetOrigEnt *ent;
1160 ent = malloc (sizeof (FcCharSetOrigEnt));
1164 ent->frozen = frozen;
1165 ent->next = *bucket;
1171 FcCharSetFreezeBase (FcCharSetFreezer *freezer, FcCharSet *fcs)
1173 FcChar32 hash = FcCharSetHash (fcs);
1174 FcCharSetEnt **bucket = &freezer->set_hash_table[hash % FC_CHAR_SET_HASH_SIZE];
1179 for (ent = *bucket; ent; ent = ent->next)
1181 if (ent->hash == hash &&
1182 ent->set.num == fcs->num &&
1183 !memcmp (FcCharSetNumbers(&ent->set),
1184 FcCharSetNumbers(fcs),
1185 fcs->num * sizeof (FcChar16)))
1190 for (i = 0; i < fcs->num; i++)
1191 if (FcCharSetLeaf(&ent->set, i) != FcCharSetLeaf(fcs, i))
1198 size = (sizeof (FcCharSetEnt) +
1199 fcs->num * sizeof (FcCharLeaf *) +
1200 fcs->num * sizeof (FcChar16));
1201 ent = malloc (size);
1205 freezer->charsets_allocated++;
1207 FcRefSetConst (&ent->set.ref);
1208 ent->set.num = fcs->num;
1211 intptr_t *ent_leaves;
1213 ent->set.leaves_offset = sizeof (ent->set);
1214 ent->set.numbers_offset = (ent->set.leaves_offset +
1215 fcs->num * sizeof (intptr_t));
1217 ent_leaves = FcCharSetLeaves (&ent->set);
1218 for (i = 0; i < fcs->num; i++)
1219 ent_leaves[i] = FcPtrToOffset (ent_leaves,
1220 FcCharSetLeaf (fcs, i));
1221 memcpy (FcCharSetNumbers (&ent->set),
1222 FcCharSetNumbers (fcs),
1223 fcs->num * sizeof (FcChar16));
1227 ent->set.leaves_offset = 0;
1228 ent->set.numbers_offset = 0;
1232 ent->next = *bucket;
1238 static const FcCharSet *
1239 FcCharSetFindFrozen (FcCharSetFreezer *freezer, const FcCharSet *orig)
1241 FcCharSetOrigEnt **bucket = &freezer->orig_hash_table[((uintptr_t) orig) & FC_CHAR_SET_HASH_SIZE];
1242 FcCharSetOrigEnt *ent;
1244 for (ent = *bucket; ent; ent = ent->next)
1245 if (ent->orig == orig)
1251 FcCharSetFreeze (FcCharSetFreezer *freezer, const FcCharSet *fcs)
1254 const FcCharSet *n = 0;
1258 b = FcCharSetCreate ();
1261 for (i = 0; i < fcs->num; i++)
1263 l = FcCharSetFreezeLeaf (freezer, FcCharSetLeaf(fcs, i));
1266 if (!FcCharSetInsertLeaf (b, FcCharSetNumbers(fcs)[i] << 8, l))
1269 n = FcCharSetFreezeBase (freezer, b);
1270 if (!FcCharSetFreezeOrig (freezer, fcs, n))
1275 freezer->charsets_seen++;
1276 freezer->leaves_seen += fcs->num;
1279 free (FcCharSetLeaves (b));
1281 free (FcCharSetNumbers (b));
1288 FcCharSetFreezerCreate (void)
1290 FcCharSetFreezer *freezer;
1292 freezer = calloc (1, sizeof (FcCharSetFreezer));
1297 FcCharSetFreezerDestroy (FcCharSetFreezer *freezer)
1301 if (FcDebug() & FC_DBG_CACHE)
1303 printf ("\ncharsets %d -> %d leaves %d -> %d\n",
1304 freezer->charsets_seen, freezer->charsets_allocated,
1305 freezer->leaves_seen, freezer->leaves_allocated);
1307 for (i = 0; i < FC_CHAR_SET_HASH_SIZE; i++)
1309 FcCharSetEnt *ent, *next;
1310 for (ent = freezer->set_hash_table[i]; ent; ent = next)
1317 for (i = 0; i < FC_CHAR_SET_HASH_SIZE; i++)
1319 FcCharSetOrigEnt *ent, *next;
1320 for (ent = freezer->orig_hash_table[i]; ent; ent = next)
1327 for (i = 0; i < freezer->leaf_block_count; i++)
1328 free (freezer->leaf_blocks[i]);
1330 free (freezer->leaf_blocks);
1335 FcCharSetSerializeAlloc (FcSerialize *serialize, const FcCharSet *cs)
1341 if (!FcRefIsConst (&cs->ref))
1343 if (!serialize->cs_freezer)
1345 serialize->cs_freezer = FcCharSetFreezerCreate ();
1346 if (!serialize->cs_freezer)
1349 if (FcCharSetFindFrozen (serialize->cs_freezer, cs))
1352 cs = FcCharSetFreeze (serialize->cs_freezer, cs);
1355 leaves = FcCharSetLeaves (cs);
1356 numbers = FcCharSetNumbers (cs);
1358 if (!FcSerializeAlloc (serialize, cs, sizeof (FcCharSet)))
1360 if (!FcSerializeAlloc (serialize, leaves, cs->num * sizeof (intptr_t)))
1362 if (!FcSerializeAlloc (serialize, numbers, cs->num * sizeof (FcChar16)))
1364 for (i = 0; i < cs->num; i++)
1365 if (!FcSerializeAlloc (serialize, FcCharSetLeaf(cs, i),
1366 sizeof (FcCharLeaf)))
1372 FcCharSetSerialize(FcSerialize *serialize, const FcCharSet *cs)
1374 FcCharSet *cs_serialized;
1375 intptr_t *leaves, *leaves_serialized;
1376 FcChar16 *numbers, *numbers_serialized;
1377 FcCharLeaf *leaf, *leaf_serialized;
1380 if (!FcRefIsConst (&cs->ref) && serialize->cs_freezer)
1382 cs = FcCharSetFindFrozen (serialize->cs_freezer, cs);
1387 cs_serialized = FcSerializePtr (serialize, cs);
1391 FcRefSetConst (&cs_serialized->ref);
1392 cs_serialized->num = cs->num;
1396 leaves = FcCharSetLeaves (cs);
1397 leaves_serialized = FcSerializePtr (serialize, leaves);
1398 if (!leaves_serialized)
1401 cs_serialized->leaves_offset = FcPtrToOffset (cs_serialized,
1404 numbers = FcCharSetNumbers (cs);
1405 numbers_serialized = FcSerializePtr (serialize, numbers);
1409 cs_serialized->numbers_offset = FcPtrToOffset (cs_serialized,
1410 numbers_serialized);
1412 for (i = 0; i < cs->num; i++)
1414 leaf = FcCharSetLeaf (cs, i);
1415 leaf_serialized = FcSerializePtr (serialize, leaf);
1416 if (!leaf_serialized)
1418 *leaf_serialized = *leaf;
1419 leaves_serialized[i] = FcPtrToOffset (leaves_serialized,
1421 numbers_serialized[i] = numbers[i];
1426 cs_serialized->leaves_offset = 0;
1427 cs_serialized->numbers_offset = 0;
1430 return cs_serialized;
1432 #define __fccharset__
1433 #include "fcaliastail.h"
1434 #undef __fccharset__