2 * $RCSId: xc/lib/fontconfig/src/fccharset.c,v 1.18 2002/08/22 07:36:44 keithp Exp $
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 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.
32 static FcCharSet ** charsets = 0;
33 static FcChar16 ** numbers = 0;
34 static int charset_bank_count = 0, charset_ptr, charset_count;
35 static int charset_numbers_ptr, charset_numbers_count;
36 static FcCharLeaf ** leaves = 0;
37 static int charset_leaf_ptr, charset_leaf_count;
38 static int ** leaf_idx = 0;
39 static int charset_leaf_idx_ptr, charset_leaf_idx_count;
42 FcCharSetCreate (void)
46 fcs = (FcCharSet *) malloc (sizeof (FcCharSet));
49 FcMemAlloc (FC_MEM_CHARSET, sizeof (FcCharSet));
52 fcs->bank = FC_BANK_DYNAMIC;
53 fcs->u.dyn.leaves = 0;
54 fcs->u.dyn.numbers = 0;
64 return FcCharSetCreate ();
68 FcCharSetDestroy (FcCharSet *fcs)
71 if (fcs->ref == FC_REF_CONSTANT)
75 if (fcs->bank == FC_BANK_DYNAMIC)
77 for (i = 0; i < fcs->num; i++)
79 FcMemFree (FC_MEM_CHARLEAF, sizeof (FcCharLeaf));
80 free (fcs->u.dyn.leaves[i]);
82 if (fcs->u.dyn.leaves)
84 FcMemFree (FC_MEM_CHARSET, fcs->num * sizeof (FcCharLeaf *));
85 free (fcs->u.dyn.leaves);
87 if (fcs->u.dyn.numbers)
89 FcMemFree (FC_MEM_CHARSET, fcs->num * sizeof (FcChar16));
90 free (fcs->u.dyn.numbers);
93 FcMemFree (FC_MEM_CHARSET, sizeof (FcCharSet));
98 * Locate the leaf containing the specified char, return
99 * its index if it exists, otherwise return negative of
100 * the (position + 1) where it should be inserted
104 FcCharSetFindLeafPos (const FcCharSet *fcs, FcChar32 ucs4)
106 FcChar16 *numbers = FcCharSetGetNumbers(fcs);
109 int high = fcs->num - 1;
116 int mid = (low + high) >> 1;
125 if (high < 0 || (high < fcs->num && numbers[high] < ucs4))
131 FcCharSetFindLeaf (const FcCharSet *fcs, FcChar32 ucs4)
133 int pos = FcCharSetFindLeafPos (fcs, ucs4);
135 return FcCharSetGetLeaf(fcs, pos);
140 FcCharSetPutLeaf (FcCharSet *fcs,
151 if (fcs->bank != FC_BANK_DYNAMIC)
155 leaves = malloc ((fcs->num + 1) * sizeof (FcCharLeaf *));
158 FcMemAlloc (FC_MEM_CHARSET, (fcs->num + 1) * sizeof (FcCharLeaf *));
159 numbers = malloc ((fcs->num + 1) * sizeof (FcChar16));
162 FcMemAlloc (FC_MEM_CHARSET, (fcs->num + 1) * sizeof (FcChar16));
164 for (i = 0; i < fcs->num; i++)
165 leaves[i] = FcCharSetGetLeaf(fcs, i);
166 memcpy (numbers, FcCharSetGetNumbers(fcs),
167 fcs->num * sizeof (FcChar16));
171 if (!fcs->u.dyn.leaves)
172 leaves = malloc (sizeof (FcCharLeaf *));
174 leaves = realloc (fcs->u.dyn.leaves, (fcs->num + 1) * sizeof (FcCharLeaf *));
178 FcMemFree (FC_MEM_CHARSET, fcs->num * sizeof (FcCharLeaf *));
179 FcMemAlloc (FC_MEM_CHARSET, (fcs->num + 1) * sizeof (FcCharLeaf *));
180 fcs->u.dyn.leaves = leaves;
181 if (!fcs->u.dyn.numbers)
182 numbers = malloc (sizeof (FcChar16));
184 numbers = realloc (fcs->u.dyn.numbers, (fcs->num + 1) * sizeof (FcChar16));
188 FcMemFree (FC_MEM_CHARSET, fcs->num * sizeof (FcChar16));
189 FcMemAlloc (FC_MEM_CHARSET, (fcs->num + 1) * sizeof (FcChar16));
190 fcs->u.dyn.numbers = numbers;
193 memmove (fcs->u.dyn.leaves + pos + 1, fcs->u.dyn.leaves + pos,
194 (fcs->num - pos) * sizeof (FcCharLeaf *));
195 memmove (fcs->u.dyn.numbers + pos + 1, fcs->u.dyn.numbers + pos,
196 (fcs->num - pos) * sizeof (FcChar16));
197 fcs->u.dyn.numbers[pos] = (FcChar16) ucs4;
198 fcs->u.dyn.leaves[pos] = leaf;
204 * Locate the leaf containing the specified char, creating it
209 FcCharSetFindLeafCreate (FcCharSet *fcs, FcChar32 ucs4)
214 pos = FcCharSetFindLeafPos (fcs, ucs4);
216 return FcCharSetGetLeaf(fcs, pos);
218 leaf = calloc (1, sizeof (FcCharLeaf));
223 if (!FcCharSetPutLeaf (fcs, ucs4, leaf, pos))
228 FcMemAlloc (FC_MEM_CHARLEAF, sizeof (FcCharLeaf));
233 FcCharSetInsertLeaf (FcCharSet *fcs, FcChar32 ucs4, FcCharLeaf *leaf)
237 pos = FcCharSetFindLeafPos (fcs, ucs4);
240 FcMemFree (FC_MEM_CHARLEAF, sizeof (FcCharLeaf));
241 if (fcs->bank == FC_BANK_DYNAMIC)
243 free (fcs->u.dyn.leaves[pos]);
244 fcs->u.dyn.leaves[pos] = leaf;
248 leaves[fcs->bank][leaf_idx[fcs->bank][fcs->u.stat.leafidx_offset]+pos] = *leaf;
253 return FcCharSetPutLeaf (fcs, ucs4, leaf, pos);
257 FcCharSetAddChar (FcCharSet *fcs, FcChar32 ucs4)
262 if (fcs->ref == FC_REF_CONSTANT)
264 leaf = FcCharSetFindLeafCreate (fcs, ucs4);
267 b = &leaf->map[(ucs4 & 0xff) >> 5];
268 *b |= (1 << (ucs4 & 0x1f));
273 * An iterator for the leaves of a charset
276 typedef struct _fcCharSetIter {
283 * Set iter->leaf to the leaf containing iter->ucs4 or higher
287 FcCharSetIterSet (const FcCharSet *fcs, FcCharSetIter *iter)
289 int pos = FcCharSetFindLeafPos (fcs, iter->ucs4);
300 iter->ucs4 = (FcChar32) FcCharSetGetNumbers(fcs)[pos] << 8;
302 iter->leaf = FcCharSetGetLeaf(fcs, pos);
305 printf ("set %08x: %08x\n", iter->ucs4, (FcChar32) iter->leaf);
310 FcCharSetIterNext (const FcCharSet *fcs, FcCharSetIter *iter)
312 int pos = iter->pos + 1;
320 iter->ucs4 = (FcChar32) FcCharSetGetNumbers(fcs)[pos] << 8;
321 iter->leaf = FcCharSetGetLeaf(fcs, pos);
328 FcCharSetDump (const FcCharSet *fcs)
332 printf ("fcs %08x:\n", (FcChar32) fcs);
333 for (pos = 0; pos < fcs->num; pos++)
335 FcCharLeaf *leaf = fcs->leaves[pos];
336 FcChar32 ucs4 = (FcChar32) fcs->numbers[pos] << 8;
338 printf (" %08x: %08x\n", ucs4, (FcChar32) leaf);
344 FcCharSetIterStart (const FcCharSet *fcs, FcCharSetIter *iter)
351 FcCharSetIterSet (fcs, iter);
355 FcCharSetCopy (FcCharSet *src)
357 if (src->ref != FC_REF_CONSTANT)
363 FcCharSetEqual (const FcCharSet *a, const FcCharSet *b)
365 FcCharSetIter ai, bi;
370 for (FcCharSetIterStart (a, &ai), FcCharSetIterStart (b, &bi);
372 FcCharSetIterNext (a, &ai), FcCharSetIterNext (b, &bi))
374 if (ai.ucs4 != bi.ucs4)
376 for (i = 0; i < 256/32; i++)
377 if (ai.leaf->map[i] != bi.leaf->map[i])
380 return ai.leaf == bi.leaf;
384 FcCharSetAddLeaf (FcCharSet *fcs,
388 FcCharLeaf *new = FcCharSetFindLeafCreate (fcs, ucs4);
396 FcCharSetOperate (const FcCharSet *a,
398 FcBool (*overlap) (FcCharLeaf *result,
399 const FcCharLeaf *al,
400 const FcCharLeaf *bl),
405 FcCharSetIter ai, bi;
407 fcs = FcCharSetCreate ();
410 FcCharSetIterStart (a, &ai);
411 FcCharSetIterStart (b, &bi);
412 while ((ai.leaf || (bonly && bi.leaf)) && (bi.leaf || (aonly && ai.leaf)))
414 if (ai.ucs4 < bi.ucs4)
418 if (!FcCharSetAddLeaf (fcs, ai.ucs4, ai.leaf))
420 FcCharSetIterNext (a, &ai);
425 FcCharSetIterSet (a, &ai);
428 else if (bi.ucs4 < ai.ucs4 )
432 if (!FcCharSetAddLeaf (fcs, bi.ucs4, bi.leaf))
434 FcCharSetIterNext (b, &bi);
439 FcCharSetIterSet (b, &bi);
446 if ((*overlap) (&leaf, ai.leaf, bi.leaf))
448 if (!FcCharSetAddLeaf (fcs, ai.ucs4, &leaf))
451 FcCharSetIterNext (a, &ai);
452 FcCharSetIterNext (b, &bi);
457 FcCharSetDestroy (fcs);
463 FcCharSetIntersectLeaf (FcCharLeaf *result,
464 const FcCharLeaf *al,
465 const FcCharLeaf *bl)
468 FcBool nonempty = FcFalse;
470 for (i = 0; i < 256/32; i++)
471 if ((result->map[i] = al->map[i] & bl->map[i]))
477 FcCharSetIntersect (const FcCharSet *a, const FcCharSet *b)
479 return FcCharSetOperate (a, b, FcCharSetIntersectLeaf, FcFalse, FcFalse);
483 FcCharSetUnionLeaf (FcCharLeaf *result,
484 const FcCharLeaf *al,
485 const FcCharLeaf *bl)
489 for (i = 0; i < 256/32; i++)
490 result->map[i] = al->map[i] | bl->map[i];
495 FcCharSetUnion (const FcCharSet *a, const FcCharSet *b)
497 return FcCharSetOperate (a, b, FcCharSetUnionLeaf, FcTrue, FcTrue);
501 FcCharSetSubtractLeaf (FcCharLeaf *result,
502 const FcCharLeaf *al,
503 const FcCharLeaf *bl)
506 FcBool nonempty = FcFalse;
508 for (i = 0; i < 256/32; i++)
509 if ((result->map[i] = al->map[i] & ~bl->map[i]))
515 FcCharSetSubtract (const FcCharSet *a, const FcCharSet *b)
517 return FcCharSetOperate (a, b, FcCharSetSubtractLeaf, FcTrue, FcFalse);
521 FcCharSetHasChar (const FcCharSet *fcs, FcChar32 ucs4)
523 FcCharLeaf *leaf = FcCharSetFindLeaf (fcs, ucs4);
526 return (leaf->map[(ucs4 & 0xff) >> 5] & (1 << (ucs4 & 0x1f))) != 0;
530 FcCharSetPopCount (FcChar32 c1)
533 FcChar32 c2 = (c1 >> 1) & 033333333333;
534 c2 = c1 - c2 - ((c2 >> 1) & 033333333333);
535 return (((c2 + (c2 >> 3)) & 030707070707) % 077);
539 FcCharSetIntersectCount (const FcCharSet *a, const FcCharSet *b)
541 FcCharSetIter ai, bi;
544 FcCharSetIterStart (a, &ai);
545 FcCharSetIterStart (b, &bi);
546 while (ai.leaf && bi.leaf)
548 if (ai.ucs4 == bi.ucs4)
550 FcChar32 *am = ai.leaf->map;
551 FcChar32 *bm = bi.leaf->map;
554 count += FcCharSetPopCount (*am++ & *bm++);
555 FcCharSetIterNext (a, &ai);
557 else if (ai.ucs4 < bi.ucs4)
560 FcCharSetIterSet (a, &ai);
562 if (bi.ucs4 < ai.ucs4)
565 FcCharSetIterSet (b, &bi);
572 FcCharSetCount (const FcCharSet *a)
577 for (FcCharSetIterStart (a, &ai); ai.leaf; FcCharSetIterNext (a, &ai))
580 FcChar32 *am = ai.leaf->map;
583 count += FcCharSetPopCount (*am++);
589 FcCharSetSubtractCount (const FcCharSet *a, const FcCharSet *b)
591 FcCharSetIter ai, bi;
594 FcCharSetIterStart (a, &ai);
595 FcCharSetIterStart (b, &bi);
598 if (ai.ucs4 <= bi.ucs4)
600 FcChar32 *am = ai.leaf->map;
602 if (ai.ucs4 == bi.ucs4)
604 FcChar32 *bm = bi.leaf->map;;
606 count += FcCharSetPopCount (*am++ & ~*bm++);
611 count += FcCharSetPopCount (*am++);
613 FcCharSetIterNext (a, &ai);
618 FcCharSetIterSet (b, &bi);
625 * return FcTrue iff a is a subset of b
628 FcCharSetIsSubset (const FcCharSet *a, const FcCharSet *b)
633 if (a == b) return FcTrue;
636 while (ai < a->num && bi < b->num)
638 an = FcCharSetGetNumbers(a)[ai];
639 bn = FcCharSetGetNumbers(b)[bi];
641 * Check matching pages
645 FcChar32 *am = FcCharSetGetLeaf(a, ai)->map;
646 FcChar32 *bm = FcCharSetGetLeaf(b, bi)->map;
652 * Does am have any bits not in bm?
662 * Does a have any pages not in b?
669 int high = b->num - 1;
672 * Search for page 'an' in 'b'
676 int mid = (low + high) >> 1;
677 bn = FcCharSetGetNumbers(b)[mid];
689 while (bi < b->num && FcCharSetGetNumbers(b)[bi] < an)
694 * did we look at every page?
700 * These two functions efficiently walk the entire charmap for
701 * other software (like pango) that want their own copy
705 FcCharSetNextPage (const FcCharSet *a,
706 FcChar32 map[FC_CHARSET_MAP_SIZE],
713 FcCharSetIterSet (a, &ai);
715 return FC_CHARSET_DONE;
718 * Save current information
721 memcpy (map, ai.leaf->map, sizeof (ai.leaf->map));
725 FcCharSetIterNext (a, &ai);
732 FcCharSetFirstPage (const FcCharSet *a,
733 FcChar32 map[FC_CHARSET_MAP_SIZE],
737 return FcCharSetNextPage (a, map, next);
741 * old coverage API, rather hard to use correctly
744 FcCharSetCoverage (const FcCharSet *a, FcChar32 page, FcChar32 *result);
747 FcCharSetCoverage (const FcCharSet *a, FcChar32 page, FcChar32 *result)
752 FcCharSetIterSet (a, &ai);
755 memset (result, '\0', 256 / 8);
760 memcpy (result, ai.leaf->map, sizeof (ai.leaf->map));
761 FcCharSetIterNext (a, &ai);
768 * ASCII representation of charsets.
770 * Each leaf is represented as 9 32-bit values, the code of the first character followed
771 * by 8 32 bit values for the leaf itself. Each value is encoded as 5 ASCII characters,
772 * only 85 different values are used to avoid control characters as well as the other
773 * characters used to encode font names. 85**5 > 2^32 so things work out, but
774 * it's not exactly human readable output. As a special case, 0 is encoded as a space
777 static const unsigned char charToValue[256] = {
778 /* "" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
779 /* "\b" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
780 /* "\020" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
781 /* "\030" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
782 /* " " */ 0xff, 0x00, 0xff, 0x01, 0x02, 0x03, 0x04, 0xff,
783 /* "(" */ 0x05, 0x06, 0x07, 0x08, 0xff, 0xff, 0x09, 0x0a,
784 /* "0" */ 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12,
785 /* "8" */ 0x13, 0x14, 0xff, 0x15, 0x16, 0xff, 0x17, 0x18,
786 /* "@" */ 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
787 /* "H" */ 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
788 /* "P" */ 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30,
789 /* "X" */ 0x31, 0x32, 0x33, 0x34, 0xff, 0x35, 0x36, 0xff,
790 /* "`" */ 0xff, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d,
791 /* "h" */ 0x3e, 0x3f, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45,
792 /* "p" */ 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d,
793 /* "x" */ 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53, 0x54, 0xff,
794 /* "\200" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
795 /* "\210" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
796 /* "\220" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
797 /* "\230" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
798 /* "\240" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
799 /* "\250" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
800 /* "\260" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
801 /* "\270" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
802 /* "\300" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
803 /* "\310" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
804 /* "\320" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
805 /* "\330" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
806 /* "\340" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
807 /* "\350" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
808 /* "\360" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
809 /* "\370" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
812 static const FcChar8 valueToChar[0x55] = {
813 /* 0x00 */ '!', '#', '$', '%', '&', '(', ')', '*',
814 /* 0x08 */ '+', '.', '/', '0', '1', '2', '3', '4',
815 /* 0x10 */ '5', '6', '7', '8', '9', ';', '<', '>',
816 /* 0x18 */ '?', '@', 'A', 'B', 'C', 'D', 'E', 'F',
817 /* 0x20 */ 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
818 /* 0x28 */ 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
819 /* 0x30 */ 'W', 'X', 'Y', 'Z', '[', ']', '^', 'a',
820 /* 0x38 */ 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
821 /* 0x40 */ 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q',
822 /* 0x48 */ 'r', 's', 't', 'u', 'v', 'w', 'x', 'y',
823 /* 0x50 */ 'z', '{', '|', '}', '~',
827 FcCharSetParseValue (FcChar8 *string, FcChar32 *value)
841 for (i = 0; i < 5; i++)
843 if (!(c = (FcChar32) (unsigned char) *string++))
856 FcCharSetUnparseValue (FcStrBuf *buf, FcChar32 value)
861 return FcStrBufChar (buf, ' ');
866 FcChar8 *s = string + 5;
868 for (i = 0; i < 5; i++)
870 *--s = valueToChar[value % 85];
873 for (i = 0; i < 5; i++)
874 if (!FcStrBufChar (buf, *s++))
880 typedef struct _FcCharLeafEnt FcCharLeafEnt;
882 struct _FcCharLeafEnt {
888 #define FC_CHAR_LEAF_BLOCK (4096 / sizeof (FcCharLeafEnt))
889 static FcCharLeafEnt **FcCharLeafBlocks;
890 static int FcCharLeafBlockCount;
892 static FcCharLeafEnt *
893 FcCharLeafEntCreate (void)
895 static FcCharLeafEnt *block;
900 FcCharLeafEnt **newBlocks;
902 FcCharLeafBlockCount++;
903 newBlocks = realloc (FcCharLeafBlocks, FcCharLeafBlockCount * sizeof (FcCharLeafEnt *));
906 FcCharLeafBlocks = newBlocks;
907 block = FcCharLeafBlocks[FcCharLeafBlockCount-1] = malloc (FC_CHAR_LEAF_BLOCK * sizeof (FcCharLeafEnt));
910 FcMemAlloc (FC_MEM_CHARLEAF, FC_CHAR_LEAF_BLOCK * sizeof (FcCharLeafEnt));
911 remain = FC_CHAR_LEAF_BLOCK;
917 #define FC_CHAR_LEAF_HASH_SIZE 257
920 FcCharLeafHash (FcCharLeaf *leaf)
925 for (i = 0; i < 256/32; i++)
926 hash = ((hash << 1) | (hash >> 31)) ^ leaf->map[i];
930 static int FcCharLeafTotal;
931 static int FcCharLeafUsed;
933 static FcCharLeafEnt *FcCharLeafHashTable[FC_CHAR_LEAF_HASH_SIZE];
936 FcCharSetFreezeLeaf (FcCharLeaf *leaf)
938 FcChar32 hash = FcCharLeafHash (leaf);
939 FcCharLeafEnt **bucket = &FcCharLeafHashTable[hash % FC_CHAR_LEAF_HASH_SIZE];
943 for (ent = *bucket; ent; ent = ent->next)
945 if (ent->hash == hash && !memcmp (&ent->leaf, leaf, sizeof (FcCharLeaf)))
949 ent = FcCharLeafEntCreate();
961 FcCharSetThawAllLeaf (void)
965 for (i = 0; i < FC_CHAR_LEAF_HASH_SIZE; i++)
966 FcCharLeafHashTable[i] = 0;
971 for (i = 0; i < FcCharLeafBlockCount; i++)
972 free (FcCharLeafBlocks[i]);
974 free (FcCharLeafBlocks);
975 FcCharLeafBlocks = 0;
976 FcCharLeafBlockCount = 0;
979 typedef struct _FcCharSetEnt FcCharSetEnt;
981 struct _FcCharSetEnt {
987 #define FC_CHAR_SET_HASH_SIZE 67
990 FcCharSetHash (FcCharSet *fcs)
996 for (i = 0; i < fcs->num * (int) (sizeof (FcCharLeaf *) / sizeof (FcChar32)); i++)
997 hash = ((hash << 1) | (hash >> 31)) ^ (FcChar32)(FcCharSetGetLeaf(fcs, i)->map);
998 /* hash in numbers */
999 for (i = 0; i < fcs->num; i++)
1000 hash = ((hash << 1) | (hash >> 31)) ^ *FcCharSetGetNumbers(fcs);
1004 static int FcCharSetTotal;
1005 static int FcCharSetUsed;
1006 static int FcCharSetTotalEnts, FcCharSetUsedEnts;
1008 static FcCharSetEnt *FcCharSetHashTable[FC_CHAR_SET_HASH_SIZE];
1011 FcCharSetFreezeBase (FcCharSet *fcs)
1013 FcChar32 hash = FcCharSetHash (fcs);
1014 FcCharSetEnt **bucket = &FcCharSetHashTable[hash % FC_CHAR_SET_HASH_SIZE];
1019 FcCharSetTotalEnts += fcs->num;
1020 for (ent = *bucket; ent; ent = ent->next)
1022 if (ent->hash == hash &&
1023 ent->set.num == fcs->num &&
1024 !memcmp (FcCharSetGetNumbers(&ent->set),
1025 FcCharSetGetNumbers(fcs),
1026 fcs->num * sizeof (FcChar16)))
1031 for (i = 0; i < fcs->num; i++)
1032 if (FcCharSetGetLeaf(&ent->set, i) != FcCharSetGetLeaf(fcs, i))
1039 size = (sizeof (FcCharSetEnt) +
1040 fcs->num * sizeof (FcCharLeaf *) +
1041 fcs->num * sizeof (FcChar16));
1042 ent = malloc (size);
1045 FcMemAlloc (FC_MEM_CHARSET, size);
1047 FcCharSetUsedEnts += fcs->num;
1049 ent->set.ref = FC_REF_CONSTANT;
1050 ent->set.num = fcs->num;
1051 ent->set.bank = fcs->bank;
1052 if (fcs->bank == FC_BANK_DYNAMIC)
1056 ent->set.u.dyn.leaves = (FcCharLeaf **) (ent + 1);
1057 ent->set.u.dyn.numbers = (FcChar16 *) (ent->set.u.dyn.leaves + fcs->num);
1058 memcpy (ent->set.u.dyn.leaves, fcs->u.dyn.leaves, fcs->num * sizeof (FcCharLeaf *));
1059 memcpy (ent->set.u.dyn.numbers, fcs->u.dyn.numbers, fcs->num * sizeof (FcChar16));
1063 ent->set.u.dyn.leaves = 0;
1064 ent->set.u.dyn.numbers = 0;
1069 ent->set.u.stat.leafidx_offset = fcs->u.stat.leafidx_offset;
1070 ent->set.u.stat.numbers_offset = fcs->u.stat.numbers_offset;
1074 ent->next = *bucket;
1080 FcCharSetThawAll (void)
1083 FcCharSetEnt *ent, *next;
1085 for (i = 0; i < FC_CHAR_SET_HASH_SIZE; i++)
1087 for (ent = FcCharSetHashTable[i]; ent; ent = next)
1092 FcCharSetHashTable[i] = 0;
1096 FcCharSetTotalEnts = 0;
1098 FcCharSetUsedEnts = 0;
1100 FcCharSetThawAllLeaf ();
1104 FcCharSetFreeze (FcCharSet *fcs)
1111 b = FcCharSetCreate ();
1114 for (i = 0; i < fcs->num; i++)
1116 l = FcCharSetFreezeLeaf (FcCharSetGetLeaf(fcs, i));
1119 if (!FcCharSetInsertLeaf (b, FcCharSetGetNumbers(fcs)[i] << 8, l))
1122 n = FcCharSetFreezeBase (b);
1124 if (b->bank == FC_BANK_DYNAMIC)
1126 if (b->u.dyn.leaves)
1128 FcMemFree (FC_MEM_CHARSET, b->num * sizeof (FcCharLeaf *));
1129 free (b->u.dyn.leaves);
1131 if (b->u.dyn.numbers)
1133 FcMemFree (FC_MEM_CHARSET, b->num * sizeof (FcChar16));
1134 free (b->u.dyn.numbers);
1137 FcMemFree (FC_MEM_CHARSET, sizeof (FcCharSet));
1144 FcNameParseCharSet (FcChar8 *string)
1146 FcCharSet *c, *n = 0;
1153 c = FcCharSetCreate ();
1158 string = FcCharSetParseValue (string, &ucs4);
1162 for (i = 0; i < 256/32; i++)
1164 string = FcCharSetParseValue (string, &temp.map[i]);
1167 bits |= temp.map[i];
1171 leaf = FcCharSetFreezeLeaf (&temp);
1174 if (!FcCharSetInsertLeaf (c, ucs4, leaf))
1179 printf (" %8s %8s %8s %8s\n", "total", "totalmem", "new", "newmem");
1180 printf ("Leaves: %8d %8d %8d %8d\n",
1181 FcCharLeafTotal, sizeof (FcCharLeaf) * FcCharLeafTotal,
1182 FcCharLeafUsed, sizeof (FcCharLeaf) * FcCharLeafUsed);
1183 printf ("Charsets: %8d %8d %8d %8d\n",
1184 FcCharSetTotal, sizeof (FcCharSet) * FcCharSetTotal,
1185 FcCharSetUsed, sizeof (FcCharSet) * FcCharSetUsed);
1186 printf ("Tables: %8d %8d %8d %8d\n",
1187 FcCharSetTotalEnts, FcCharSetTotalEnts * (sizeof (FcCharLeaf *) + sizeof (FcChar16)),
1188 FcCharSetUsedEnts, FcCharSetUsedEnts * (sizeof (FcCharLeaf *) + sizeof (FcChar16)));
1189 printf ("Total: %8s %8d %8s %8d\n",
1191 sizeof (FcCharLeaf) * FcCharLeafTotal +
1192 sizeof (FcCharSet) * FcCharSetTotal +
1193 FcCharSetTotalEnts * (sizeof (FcCharLeaf *) + sizeof (FcChar16)),
1195 sizeof (FcCharLeaf) * FcCharLeafUsed +
1196 sizeof (FcCharSet) * FcCharSetUsed +
1197 FcCharSetUsedEnts * (sizeof (FcCharLeaf *) + sizeof (FcChar16)));
1199 n = FcCharSetFreezeBase (c);
1201 if (c->bank == FC_BANK_DYNAMIC)
1203 if (c->u.dyn.leaves)
1205 FcMemFree (FC_MEM_CHARSET, c->num * sizeof (FcCharLeaf *));
1206 free (c->u.dyn.leaves);
1208 if (c->u.dyn.numbers)
1210 FcMemFree (FC_MEM_CHARSET, c->num * sizeof (FcChar16));
1211 free (c->u.dyn.numbers);
1213 FcMemFree (FC_MEM_CHARSET, sizeof (FcCharSet));
1221 FcNameUnparseCharSet (FcStrBuf *buf, const FcCharSet *c)
1229 for (FcCharSetIterStart (c, &ci);
1231 FcCharSetIterNext (c, &ci))
1233 if (!FcCharSetUnparseValue (buf, ci.ucs4))
1235 for (i = 0; i < 256/32; i++)
1236 if (!FcCharSetUnparseValue (buf, ci.leaf->map[i]))
1243 FcCharSetIter ci, checki;
1245 /* null terminate for parser */
1246 FcStrBufChar (buf, '\0');
1247 /* step back over null for life after test */
1249 check = FcNameParseCharSet (buf->buf + len);
1250 FcCharSetIterStart (c, &ci);
1251 FcCharSetIterStart (check, &checki);
1252 while (ci.leaf || checki.leaf)
1254 if (ci.ucs4 < checki.ucs4)
1256 printf ("Missing leaf node at 0x%x\n", ci.ucs4);
1257 FcCharSetIterNext (c, &ci);
1259 else if (checki.ucs4 < ci.ucs4)
1261 printf ("Extra leaf node at 0x%x\n", checki.ucs4);
1262 FcCharSetIterNext (check, &checki);
1267 FcChar32 *cm = ci.leaf->map;
1268 FcChar32 *checkm = checki.leaf->map;
1270 for (i = 0; i < 256; i += 32)
1273 printf ("Mismatching sets at 0x%08x: 0x%08x != 0x%08x\n",
1274 ci.ucs4 + i, *cm, *checkm);
1278 FcCharSetIterNext (c, &ci);
1279 FcCharSetIterNext (check, &checki);
1282 if ((missing = FcCharSetSubtractCount (c, check)))
1283 printf ("%d missing in reparsed result\n", missing);
1284 if ((missing = FcCharSetSubtractCount (check, c)))
1285 printf ("%d extra in reparsed result\n", missing);
1286 FcCharSetDestroy (check);
1294 FcCharSetNewBank(void)
1297 charset_numbers_count = 0;
1298 charset_leaf_count = 0;
1299 charset_leaf_idx_count = 0;
1303 FcCharSetNeededBytes (const FcCharSet *c)
1305 /* yes, there's redundancy */
1307 charset_leaf_idx_count++;
1308 charset_leaf_count += c->num;
1309 charset_numbers_count += c->num;
1310 return sizeof (FcCharSet) +
1311 sizeof (int) + /* leaf_idx */
1312 sizeof (FcCharLeaf) * c->num + /* leaf */
1313 sizeof (FcChar16) * c->num; /* number */
1317 FcCharSetEnsureBank (int bi)
1319 if (!charsets || charset_bank_count <= bi)
1321 int new_count = charset_bank_count + 2;
1328 cs = realloc(charsets, sizeof(FcCharSet*) * new_count);
1330 n = realloc(numbers, sizeof(FcChar16*) * new_count);
1332 lvs = realloc(leaves, sizeof(FcCharLeaf*) * new_count);
1334 lvi = realloc(leaf_idx, sizeof(int*) * new_count);
1337 charsets = cs; numbers = n; leaves = lvs; leaf_idx = lvi;
1338 for (i = charset_bank_count; i < new_count; i++)
1345 charset_bank_count = new_count;
1351 FcCharSetDistributeBytes (FcCache * metadata, void * block_ptr)
1353 int bi = FcCacheBankToIndex(metadata->bank);
1354 if (!FcCharSetEnsureBank(bi))
1357 charsets[bi] = (FcCharSet *)block_ptr;
1358 block_ptr = (void *)((char *)block_ptr +
1359 (sizeof (FcCharSet) * charset_count));
1360 numbers[bi] = (FcChar16 *)block_ptr;
1361 block_ptr = (void *)((char *)block_ptr +
1362 (sizeof(FcChar16) * charset_numbers_count));
1363 leaves[bi] = (FcCharLeaf *)block_ptr;
1364 block_ptr = (void *)((char *)block_ptr +
1365 (sizeof(FcCharLeaf) * charset_leaf_count));
1366 leaf_idx[bi] = (int *)block_ptr;
1367 block_ptr = (void *)((char *)block_ptr +
1368 (sizeof(int) * charset_leaf_idx_count));
1370 metadata->charset_count = charset_count;
1371 metadata->charset_numbers_count = charset_numbers_count;
1372 metadata->charset_leaf_count = charset_leaf_count;
1373 metadata->charset_leaf_idx_count = charset_leaf_idx_count;
1374 charset_ptr = 0; charset_leaf_ptr = 0;
1375 charset_leaf_idx_ptr = 0; charset_numbers_ptr = 0;
1380 FcCharSetSerialize(int bank, FcCharSet *c)
1384 int bi = FcCacheBankToIndex(bank), cp = charset_ptr;
1386 new.ref = FC_REF_CONSTANT;
1388 new.u.stat.leafidx_offset = charset_leaf_idx_ptr;
1389 new.u.stat.numbers_offset = charset_numbers_ptr;
1392 charsets[bi][charset_ptr++] = new;
1394 leaf_idx[bi][charset_leaf_idx_ptr++] = charset_leaf_ptr;
1395 for (i = 0; i < c->num; i++)
1397 memcpy (&leaves[bi][charset_leaf_ptr++],
1398 c->u.dyn.leaves[i], sizeof(FcCharLeaf));
1399 numbers[bi][charset_numbers_ptr++] = c->u.dyn.numbers[i];
1402 return &charsets[bi][cp];
1406 FcCharSetUnserialize (FcCache metadata, void *block_ptr)
1408 int bi = FcCacheBankToIndex(metadata.bank);
1409 if (!FcCharSetEnsureBank(bi))
1412 charsets[bi] = (FcCharSet *)block_ptr;
1413 block_ptr = (void *)((char *)block_ptr +
1414 (sizeof (FcCharSet) * metadata.charset_count));
1415 numbers[bi] = (FcChar16 *)block_ptr;
1416 block_ptr = (void *)((char *)block_ptr +
1417 (sizeof(FcChar16) * metadata.charset_numbers_count));
1418 leaves[bi] = (FcCharLeaf *)block_ptr;
1419 block_ptr = (void *)((char *)block_ptr +
1420 (sizeof(FcCharLeaf) * metadata.charset_leaf_count));
1421 leaf_idx[bi] = (int *)block_ptr;
1422 block_ptr = (void *)((char *)block_ptr +
1423 (sizeof(int) * metadata.charset_leaf_idx_count));
1429 FcCharSetGetLeaf(const FcCharSet *c, int i)
1432 if (c->bank == FC_BANK_DYNAMIC)
1433 return c->u.dyn.leaves[i];
1434 bi = FcCacheBankToIndex(c->bank);
1436 return &leaves[bi][leaf_idx[bi][c->u.stat.leafidx_offset]+i];
1440 FcCharSetGetNumbers(const FcCharSet *c)
1443 if (c->bank == FC_BANK_DYNAMIC)
1444 return c->u.dyn.numbers;
1445 bi = FcCacheBankToIndex(c->bank);
1447 return &numbers[bi][c->u.stat.numbers_offset];