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;
41 extern const FcChar16 langBankNumbers[];
42 extern const FcCharLeaf langBankLeaves[];
43 extern const int langBankLeafIdx[];
46 FcCharSetEnsureBank (int bi);
49 FcLangCharSetPopulate (void)
51 int bi = FcCacheBankToIndexMTF (FC_BANK_LANGS);
52 FcCharSetEnsureBank (bi);
54 numbers[bi] = (FcChar16 *)&langBankNumbers;
55 leaves[bi] = (FcCharLeaf *)&langBankLeaves;
56 leaf_idx[bi] = (int *)&langBankLeafIdx;
60 FcCharSetCreate (void)
64 fcs = (FcCharSet *) malloc (sizeof (FcCharSet));
67 FcMemAlloc (FC_MEM_CHARSET, sizeof (FcCharSet));
70 fcs->bank = FC_BANK_DYNAMIC;
71 fcs->u.dyn.leaves = 0;
72 fcs->u.dyn.numbers = 0;
82 return FcCharSetCreate ();
86 FcCharSetDestroy (FcCharSet *fcs)
89 if (fcs->ref == FC_REF_CONSTANT)
93 if (fcs->bank == FC_BANK_DYNAMIC)
95 for (i = 0; i < fcs->num; i++)
97 FcMemFree (FC_MEM_CHARLEAF, sizeof (FcCharLeaf));
98 free (fcs->u.dyn.leaves[i]);
100 if (fcs->u.dyn.leaves)
102 FcMemFree (FC_MEM_CHARSET, fcs->num * sizeof (FcCharLeaf *));
103 free (fcs->u.dyn.leaves);
105 if (fcs->u.dyn.numbers)
107 FcMemFree (FC_MEM_CHARSET, fcs->num * sizeof (FcChar16));
108 free (fcs->u.dyn.numbers);
111 FcMemFree (FC_MEM_CHARSET, sizeof (FcCharSet));
116 * Locate the leaf containing the specified char, return
117 * its index if it exists, otherwise return negative of
118 * the (position + 1) where it should be inserted
122 FcCharSetFindLeafPos (const FcCharSet *fcs, FcChar32 ucs4)
124 FcChar16 *numbers = FcCharSetGetNumbers(fcs);
127 int high = fcs->num - 1;
134 int mid = (low + high) >> 1;
143 if (high < 0 || (high < fcs->num && numbers[high] < ucs4))
149 FcCharSetFindLeaf (const FcCharSet *fcs, FcChar32 ucs4)
151 int pos = FcCharSetFindLeafPos (fcs, ucs4);
153 return FcCharSetGetLeaf(fcs, pos);
158 FcCharSetPutLeaf (FcCharSet *fcs,
169 if (fcs->bank != FC_BANK_DYNAMIC)
173 leaves = malloc ((fcs->num + 1) * sizeof (FcCharLeaf *));
176 FcMemAlloc (FC_MEM_CHARSET, (fcs->num + 1) * sizeof (FcCharLeaf *));
177 numbers = malloc ((fcs->num + 1) * sizeof (FcChar16));
180 FcMemAlloc (FC_MEM_CHARSET, (fcs->num + 1) * sizeof (FcChar16));
182 for (i = 0; i < fcs->num; i++)
183 leaves[i] = FcCharSetGetLeaf(fcs, i);
184 memcpy (numbers, FcCharSetGetNumbers(fcs),
185 fcs->num * sizeof (FcChar16));
189 if (!fcs->u.dyn.leaves)
190 leaves = malloc (sizeof (FcCharLeaf *));
192 leaves = realloc (fcs->u.dyn.leaves, (fcs->num + 1) * sizeof (FcCharLeaf *));
196 FcMemFree (FC_MEM_CHARSET, fcs->num * sizeof (FcCharLeaf *));
197 FcMemAlloc (FC_MEM_CHARSET, (fcs->num + 1) * sizeof (FcCharLeaf *));
198 fcs->u.dyn.leaves = leaves;
199 if (!fcs->u.dyn.numbers)
200 numbers = malloc (sizeof (FcChar16));
202 numbers = realloc (fcs->u.dyn.numbers, (fcs->num + 1) * sizeof (FcChar16));
206 FcMemFree (FC_MEM_CHARSET, fcs->num * sizeof (FcChar16));
207 FcMemAlloc (FC_MEM_CHARSET, (fcs->num + 1) * sizeof (FcChar16));
208 fcs->u.dyn.numbers = numbers;
211 memmove (fcs->u.dyn.leaves + pos + 1, fcs->u.dyn.leaves + pos,
212 (fcs->num - pos) * sizeof (FcCharLeaf *));
213 memmove (fcs->u.dyn.numbers + pos + 1, fcs->u.dyn.numbers + pos,
214 (fcs->num - pos) * sizeof (FcChar16));
215 fcs->u.dyn.numbers[pos] = (FcChar16) ucs4;
216 fcs->u.dyn.leaves[pos] = leaf;
222 * Locate the leaf containing the specified char, creating it
227 FcCharSetFindLeafCreate (FcCharSet *fcs, FcChar32 ucs4)
232 pos = FcCharSetFindLeafPos (fcs, ucs4);
234 return FcCharSetGetLeaf(fcs, pos);
236 leaf = calloc (1, sizeof (FcCharLeaf));
241 if (!FcCharSetPutLeaf (fcs, ucs4, leaf, pos))
246 FcMemAlloc (FC_MEM_CHARLEAF, sizeof (FcCharLeaf));
251 FcCharSetInsertLeaf (FcCharSet *fcs, FcChar32 ucs4, FcCharLeaf *leaf)
255 pos = FcCharSetFindLeafPos (fcs, ucs4);
258 FcMemFree (FC_MEM_CHARLEAF, sizeof (FcCharLeaf));
259 if (fcs->bank == FC_BANK_DYNAMIC)
261 free (fcs->u.dyn.leaves[pos]);
262 fcs->u.dyn.leaves[pos] = leaf;
266 int bi = FcCacheBankToIndex(fcs->bank);
267 leaves[bi][leaf_idx[fcs->bank][fcs->u.stat.leafidx_offset]+pos] = *leaf;
272 return FcCharSetPutLeaf (fcs, ucs4, leaf, pos);
276 FcCharSetAddChar (FcCharSet *fcs, FcChar32 ucs4)
281 if (fcs->ref == FC_REF_CONSTANT)
283 leaf = FcCharSetFindLeafCreate (fcs, ucs4);
286 b = &leaf->map[(ucs4 & 0xff) >> 5];
287 *b |= (1 << (ucs4 & 0x1f));
292 * An iterator for the leaves of a charset
295 typedef struct _fcCharSetIter {
302 * Set iter->leaf to the leaf containing iter->ucs4 or higher
306 FcCharSetIterSet (const FcCharSet *fcs, FcCharSetIter *iter)
308 int pos = FcCharSetFindLeafPos (fcs, iter->ucs4);
319 iter->ucs4 = (FcChar32) FcCharSetGetNumbers(fcs)[pos] << 8;
321 iter->leaf = FcCharSetGetLeaf(fcs, pos);
324 printf ("set %08x: %08x\n", iter->ucs4, (FcChar32) iter->leaf);
329 FcCharSetIterNext (const FcCharSet *fcs, FcCharSetIter *iter)
331 int pos = iter->pos + 1;
339 iter->ucs4 = (FcChar32) FcCharSetGetNumbers(fcs)[pos] << 8;
340 iter->leaf = FcCharSetGetLeaf(fcs, pos);
347 FcCharSetDump (const FcCharSet *fcs)
351 printf ("fcs %08x:\n", (FcChar32) fcs);
352 for (pos = 0; pos < fcs->num; pos++)
354 FcCharLeaf *leaf = fcs->leaves[pos];
355 FcChar32 ucs4 = (FcChar32) fcs->numbers[pos] << 8;
357 printf (" %08x: %08x\n", ucs4, (FcChar32) leaf);
363 FcCharSetIterStart (const FcCharSet *fcs, FcCharSetIter *iter)
370 FcCharSetIterSet (fcs, iter);
374 FcCharSetCopy (FcCharSet *src)
376 if (src->ref != FC_REF_CONSTANT)
382 FcCharSetEqual (const FcCharSet *a, const FcCharSet *b)
384 FcCharSetIter ai, bi;
389 for (FcCharSetIterStart (a, &ai), FcCharSetIterStart (b, &bi);
391 FcCharSetIterNext (a, &ai), FcCharSetIterNext (b, &bi))
393 if (ai.ucs4 != bi.ucs4)
395 for (i = 0; i < 256/32; i++)
396 if (ai.leaf->map[i] != bi.leaf->map[i])
399 return ai.leaf == bi.leaf;
403 FcCharSetAddLeaf (FcCharSet *fcs,
407 FcCharLeaf *new = FcCharSetFindLeafCreate (fcs, ucs4);
415 FcCharSetOperate (const FcCharSet *a,
417 FcBool (*overlap) (FcCharLeaf *result,
418 const FcCharLeaf *al,
419 const FcCharLeaf *bl),
424 FcCharSetIter ai, bi;
426 fcs = FcCharSetCreate ();
429 FcCharSetIterStart (a, &ai);
430 FcCharSetIterStart (b, &bi);
431 while ((ai.leaf || (bonly && bi.leaf)) && (bi.leaf || (aonly && ai.leaf)))
433 if (ai.ucs4 < bi.ucs4)
437 if (!FcCharSetAddLeaf (fcs, ai.ucs4, ai.leaf))
439 FcCharSetIterNext (a, &ai);
444 FcCharSetIterSet (a, &ai);
447 else if (bi.ucs4 < ai.ucs4 )
451 if (!FcCharSetAddLeaf (fcs, bi.ucs4, bi.leaf))
453 FcCharSetIterNext (b, &bi);
458 FcCharSetIterSet (b, &bi);
465 if ((*overlap) (&leaf, ai.leaf, bi.leaf))
467 if (!FcCharSetAddLeaf (fcs, ai.ucs4, &leaf))
470 FcCharSetIterNext (a, &ai);
471 FcCharSetIterNext (b, &bi);
476 FcCharSetDestroy (fcs);
482 FcCharSetIntersectLeaf (FcCharLeaf *result,
483 const FcCharLeaf *al,
484 const FcCharLeaf *bl)
487 FcBool nonempty = FcFalse;
489 for (i = 0; i < 256/32; i++)
490 if ((result->map[i] = al->map[i] & bl->map[i]))
496 FcCharSetIntersect (const FcCharSet *a, const FcCharSet *b)
498 return FcCharSetOperate (a, b, FcCharSetIntersectLeaf, FcFalse, FcFalse);
502 FcCharSetUnionLeaf (FcCharLeaf *result,
503 const FcCharLeaf *al,
504 const FcCharLeaf *bl)
508 for (i = 0; i < 256/32; i++)
509 result->map[i] = al->map[i] | bl->map[i];
514 FcCharSetUnion (const FcCharSet *a, const FcCharSet *b)
516 return FcCharSetOperate (a, b, FcCharSetUnionLeaf, FcTrue, FcTrue);
520 FcCharSetSubtractLeaf (FcCharLeaf *result,
521 const FcCharLeaf *al,
522 const FcCharLeaf *bl)
525 FcBool nonempty = FcFalse;
527 for (i = 0; i < 256/32; i++)
528 if ((result->map[i] = al->map[i] & ~bl->map[i]))
534 FcCharSetSubtract (const FcCharSet *a, const FcCharSet *b)
536 return FcCharSetOperate (a, b, FcCharSetSubtractLeaf, FcTrue, FcFalse);
540 FcCharSetHasChar (const FcCharSet *fcs, FcChar32 ucs4)
542 FcCharLeaf *leaf = FcCharSetFindLeaf (fcs, ucs4);
545 return (leaf->map[(ucs4 & 0xff) >> 5] & (1 << (ucs4 & 0x1f))) != 0;
549 FcCharSetPopCount (FcChar32 c1)
552 FcChar32 c2 = (c1 >> 1) & 033333333333;
553 c2 = c1 - c2 - ((c2 >> 1) & 033333333333);
554 return (((c2 + (c2 >> 3)) & 030707070707) % 077);
558 FcCharSetIntersectCount (const FcCharSet *a, const FcCharSet *b)
560 FcCharSetIter ai, bi;
563 FcCharSetIterStart (a, &ai);
564 FcCharSetIterStart (b, &bi);
565 while (ai.leaf && bi.leaf)
567 if (ai.ucs4 == bi.ucs4)
569 FcChar32 *am = ai.leaf->map;
570 FcChar32 *bm = bi.leaf->map;
573 count += FcCharSetPopCount (*am++ & *bm++);
574 FcCharSetIterNext (a, &ai);
576 else if (ai.ucs4 < bi.ucs4)
579 FcCharSetIterSet (a, &ai);
581 if (bi.ucs4 < ai.ucs4)
584 FcCharSetIterSet (b, &bi);
591 FcCharSetCount (const FcCharSet *a)
596 for (FcCharSetIterStart (a, &ai); ai.leaf; FcCharSetIterNext (a, &ai))
599 FcChar32 *am = ai.leaf->map;
602 count += FcCharSetPopCount (*am++);
608 FcCharSetSubtractCount (const FcCharSet *a, const FcCharSet *b)
610 FcCharSetIter ai, bi;
613 FcCharSetIterStart (a, &ai);
614 FcCharSetIterStart (b, &bi);
617 if (ai.ucs4 <= bi.ucs4)
619 FcChar32 *am = ai.leaf->map;
621 if (ai.ucs4 == bi.ucs4)
623 FcChar32 *bm = bi.leaf->map;;
625 count += FcCharSetPopCount (*am++ & ~*bm++);
630 count += FcCharSetPopCount (*am++);
632 FcCharSetIterNext (a, &ai);
637 FcCharSetIterSet (b, &bi);
644 * return FcTrue iff a is a subset of b
647 FcCharSetIsSubset (const FcCharSet *a, const FcCharSet *b)
652 if (a == b) return FcTrue;
655 while (ai < a->num && bi < b->num)
657 an = FcCharSetGetNumbers(a)[ai];
658 bn = FcCharSetGetNumbers(b)[bi];
660 * Check matching pages
664 FcChar32 *am = FcCharSetGetLeaf(a, ai)->map;
665 FcChar32 *bm = FcCharSetGetLeaf(b, bi)->map;
671 * Does am have any bits not in bm?
681 * Does a have any pages not in b?
688 int high = b->num - 1;
691 * Search for page 'an' in 'b'
695 int mid = (low + high) >> 1;
696 bn = FcCharSetGetNumbers(b)[mid];
708 while (bi < b->num && FcCharSetGetNumbers(b)[bi] < an)
713 * did we look at every page?
719 * These two functions efficiently walk the entire charmap for
720 * other software (like pango) that want their own copy
724 FcCharSetNextPage (const FcCharSet *a,
725 FcChar32 map[FC_CHARSET_MAP_SIZE],
732 FcCharSetIterSet (a, &ai);
734 return FC_CHARSET_DONE;
737 * Save current information
740 memcpy (map, ai.leaf->map, sizeof (ai.leaf->map));
744 FcCharSetIterNext (a, &ai);
751 FcCharSetFirstPage (const FcCharSet *a,
752 FcChar32 map[FC_CHARSET_MAP_SIZE],
756 return FcCharSetNextPage (a, map, next);
760 * old coverage API, rather hard to use correctly
763 FcCharSetCoverage (const FcCharSet *a, FcChar32 page, FcChar32 *result);
766 FcCharSetCoverage (const FcCharSet *a, FcChar32 page, FcChar32 *result)
771 FcCharSetIterSet (a, &ai);
774 memset (result, '\0', 256 / 8);
779 memcpy (result, ai.leaf->map, sizeof (ai.leaf->map));
780 FcCharSetIterNext (a, &ai);
787 * ASCII representation of charsets.
789 * Each leaf is represented as 9 32-bit values, the code of the first character followed
790 * by 8 32 bit values for the leaf itself. Each value is encoded as 5 ASCII characters,
791 * only 85 different values are used to avoid control characters as well as the other
792 * characters used to encode font names. 85**5 > 2^32 so things work out, but
793 * it's not exactly human readable output. As a special case, 0 is encoded as a space
796 static const unsigned char charToValue[256] = {
797 /* "" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
798 /* "\b" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
799 /* "\020" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
800 /* "\030" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
801 /* " " */ 0xff, 0x00, 0xff, 0x01, 0x02, 0x03, 0x04, 0xff,
802 /* "(" */ 0x05, 0x06, 0x07, 0x08, 0xff, 0xff, 0x09, 0x0a,
803 /* "0" */ 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12,
804 /* "8" */ 0x13, 0x14, 0xff, 0x15, 0x16, 0xff, 0x17, 0x18,
805 /* "@" */ 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
806 /* "H" */ 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
807 /* "P" */ 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30,
808 /* "X" */ 0x31, 0x32, 0x33, 0x34, 0xff, 0x35, 0x36, 0xff,
809 /* "`" */ 0xff, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d,
810 /* "h" */ 0x3e, 0x3f, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45,
811 /* "p" */ 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d,
812 /* "x" */ 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53, 0x54, 0xff,
813 /* "\200" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
814 /* "\210" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
815 /* "\220" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
816 /* "\230" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
817 /* "\240" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
818 /* "\250" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
819 /* "\260" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
820 /* "\270" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
821 /* "\300" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
822 /* "\310" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
823 /* "\320" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
824 /* "\330" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
825 /* "\340" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
826 /* "\350" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
827 /* "\360" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
828 /* "\370" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
831 static const FcChar8 valueToChar[0x55] = {
832 /* 0x00 */ '!', '#', '$', '%', '&', '(', ')', '*',
833 /* 0x08 */ '+', '.', '/', '0', '1', '2', '3', '4',
834 /* 0x10 */ '5', '6', '7', '8', '9', ';', '<', '>',
835 /* 0x18 */ '?', '@', 'A', 'B', 'C', 'D', 'E', 'F',
836 /* 0x20 */ 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
837 /* 0x28 */ 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
838 /* 0x30 */ 'W', 'X', 'Y', 'Z', '[', ']', '^', 'a',
839 /* 0x38 */ 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
840 /* 0x40 */ 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q',
841 /* 0x48 */ 'r', 's', 't', 'u', 'v', 'w', 'x', 'y',
842 /* 0x50 */ 'z', '{', '|', '}', '~',
846 FcCharSetParseValue (FcChar8 *string, FcChar32 *value)
860 for (i = 0; i < 5; i++)
862 if (!(c = (FcChar32) (unsigned char) *string++))
875 FcCharSetUnparseValue (FcStrBuf *buf, FcChar32 value)
880 return FcStrBufChar (buf, ' ');
885 FcChar8 *s = string + 5;
887 for (i = 0; i < 5; i++)
889 *--s = valueToChar[value % 85];
892 for (i = 0; i < 5; i++)
893 if (!FcStrBufChar (buf, *s++))
899 typedef struct _FcCharLeafEnt FcCharLeafEnt;
901 struct _FcCharLeafEnt {
907 #define FC_CHAR_LEAF_BLOCK (4096 / sizeof (FcCharLeafEnt))
908 static FcCharLeafEnt **FcCharLeafBlocks;
909 static int FcCharLeafBlockCount;
911 static FcCharLeafEnt *
912 FcCharLeafEntCreate (void)
914 static FcCharLeafEnt *block;
919 FcCharLeafEnt **newBlocks;
921 FcCharLeafBlockCount++;
922 newBlocks = realloc (FcCharLeafBlocks, FcCharLeafBlockCount * sizeof (FcCharLeafEnt *));
925 FcCharLeafBlocks = newBlocks;
926 block = FcCharLeafBlocks[FcCharLeafBlockCount-1] = malloc (FC_CHAR_LEAF_BLOCK * sizeof (FcCharLeafEnt));
929 FcMemAlloc (FC_MEM_CHARLEAF, FC_CHAR_LEAF_BLOCK * sizeof (FcCharLeafEnt));
930 remain = FC_CHAR_LEAF_BLOCK;
936 #define FC_CHAR_LEAF_HASH_SIZE 257
939 FcCharLeafHash (FcCharLeaf *leaf)
944 for (i = 0; i < 256/32; i++)
945 hash = ((hash << 1) | (hash >> 31)) ^ leaf->map[i];
949 static int FcCharLeafTotal;
950 static int FcCharLeafUsed;
952 static FcCharLeafEnt *FcCharLeafHashTable[FC_CHAR_LEAF_HASH_SIZE];
955 FcCharSetFreezeLeaf (FcCharLeaf *leaf)
957 FcChar32 hash = FcCharLeafHash (leaf);
958 FcCharLeafEnt **bucket = &FcCharLeafHashTable[hash % FC_CHAR_LEAF_HASH_SIZE];
962 for (ent = *bucket; ent; ent = ent->next)
964 if (ent->hash == hash && !memcmp (&ent->leaf, leaf, sizeof (FcCharLeaf)))
968 ent = FcCharLeafEntCreate();
980 FcCharSetThawAllLeaf (void)
984 for (i = 0; i < FC_CHAR_LEAF_HASH_SIZE; i++)
985 FcCharLeafHashTable[i] = 0;
990 for (i = 0; i < FcCharLeafBlockCount; i++)
991 free (FcCharLeafBlocks[i]);
993 free (FcCharLeafBlocks);
994 FcCharLeafBlocks = 0;
995 FcCharLeafBlockCount = 0;
998 typedef struct _FcCharSetEnt FcCharSetEnt;
1000 struct _FcCharSetEnt {
1006 #define FC_CHAR_SET_HASH_SIZE 67
1009 FcCharSetHash (FcCharSet *fcs)
1014 /* hash in leaves */
1015 for (i = 0; i < fcs->num * (int) (sizeof (FcCharLeaf *) / sizeof (FcChar32)); i++)
1016 hash = ((hash << 1) | (hash >> 31)) ^ (FcChar32)(FcCharSetGetLeaf(fcs, i)->map);
1017 /* hash in numbers */
1018 for (i = 0; i < fcs->num; i++)
1019 hash = ((hash << 1) | (hash >> 31)) ^ *FcCharSetGetNumbers(fcs);
1023 static int FcCharSetTotal;
1024 static int FcCharSetUsed;
1025 static int FcCharSetTotalEnts, FcCharSetUsedEnts;
1027 static FcCharSetEnt *FcCharSetHashTable[FC_CHAR_SET_HASH_SIZE];
1030 FcCharSetFreezeBase (FcCharSet *fcs)
1032 FcChar32 hash = FcCharSetHash (fcs);
1033 FcCharSetEnt **bucket = &FcCharSetHashTable[hash % FC_CHAR_SET_HASH_SIZE];
1038 FcCharSetTotalEnts += fcs->num;
1039 for (ent = *bucket; ent; ent = ent->next)
1041 if (ent->hash == hash &&
1042 ent->set.num == fcs->num &&
1043 !memcmp (FcCharSetGetNumbers(&ent->set),
1044 FcCharSetGetNumbers(fcs),
1045 fcs->num * sizeof (FcChar16)))
1050 for (i = 0; i < fcs->num; i++)
1051 if (FcCharSetGetLeaf(&ent->set, i) != FcCharSetGetLeaf(fcs, i))
1058 size = (sizeof (FcCharSetEnt) +
1059 fcs->num * sizeof (FcCharLeaf *) +
1060 fcs->num * sizeof (FcChar16));
1061 ent = malloc (size);
1064 FcMemAlloc (FC_MEM_CHARSET, size);
1066 FcCharSetUsedEnts += fcs->num;
1068 ent->set.ref = FC_REF_CONSTANT;
1069 ent->set.num = fcs->num;
1070 ent->set.bank = fcs->bank;
1071 if (fcs->bank == FC_BANK_DYNAMIC)
1075 ent->set.u.dyn.leaves = (FcCharLeaf **) (ent + 1);
1076 ent->set.u.dyn.numbers = (FcChar16 *) (ent->set.u.dyn.leaves + fcs->num);
1077 memcpy (ent->set.u.dyn.leaves, fcs->u.dyn.leaves, fcs->num * sizeof (FcCharLeaf *));
1078 memcpy (ent->set.u.dyn.numbers, fcs->u.dyn.numbers, fcs->num * sizeof (FcChar16));
1082 ent->set.u.dyn.leaves = 0;
1083 ent->set.u.dyn.numbers = 0;
1088 ent->set.u.stat.leafidx_offset = fcs->u.stat.leafidx_offset;
1089 ent->set.u.stat.numbers_offset = fcs->u.stat.numbers_offset;
1093 ent->next = *bucket;
1099 FcCharSetThawAll (void)
1102 FcCharSetEnt *ent, *next;
1104 for (i = 0; i < FC_CHAR_SET_HASH_SIZE; i++)
1106 for (ent = FcCharSetHashTable[i]; ent; ent = next)
1111 FcCharSetHashTable[i] = 0;
1115 FcCharSetTotalEnts = 0;
1117 FcCharSetUsedEnts = 0;
1119 FcCharSetThawAllLeaf ();
1123 FcCharSetFreeze (FcCharSet *fcs)
1130 b = FcCharSetCreate ();
1133 for (i = 0; i < fcs->num; i++)
1135 l = FcCharSetFreezeLeaf (FcCharSetGetLeaf(fcs, i));
1138 if (!FcCharSetInsertLeaf (b, FcCharSetGetNumbers(fcs)[i] << 8, l))
1141 n = FcCharSetFreezeBase (b);
1143 if (b->bank == FC_BANK_DYNAMIC)
1145 if (b->u.dyn.leaves)
1147 FcMemFree (FC_MEM_CHARSET, b->num * sizeof (FcCharLeaf *));
1148 free (b->u.dyn.leaves);
1150 if (b->u.dyn.numbers)
1152 FcMemFree (FC_MEM_CHARSET, b->num * sizeof (FcChar16));
1153 free (b->u.dyn.numbers);
1156 FcMemFree (FC_MEM_CHARSET, sizeof (FcCharSet));
1163 FcNameParseCharSet (FcChar8 *string)
1165 FcCharSet *c, *n = 0;
1172 c = FcCharSetCreate ();
1177 string = FcCharSetParseValue (string, &ucs4);
1181 for (i = 0; i < 256/32; i++)
1183 string = FcCharSetParseValue (string, &temp.map[i]);
1186 bits |= temp.map[i];
1190 leaf = FcCharSetFreezeLeaf (&temp);
1193 if (!FcCharSetInsertLeaf (c, ucs4, leaf))
1198 printf (" %8s %8s %8s %8s\n", "total", "totalmem", "new", "newmem");
1199 printf ("Leaves: %8d %8d %8d %8d\n",
1200 FcCharLeafTotal, sizeof (FcCharLeaf) * FcCharLeafTotal,
1201 FcCharLeafUsed, sizeof (FcCharLeaf) * FcCharLeafUsed);
1202 printf ("Charsets: %8d %8d %8d %8d\n",
1203 FcCharSetTotal, sizeof (FcCharSet) * FcCharSetTotal,
1204 FcCharSetUsed, sizeof (FcCharSet) * FcCharSetUsed);
1205 printf ("Tables: %8d %8d %8d %8d\n",
1206 FcCharSetTotalEnts, FcCharSetTotalEnts * (sizeof (FcCharLeaf *) + sizeof (FcChar16)),
1207 FcCharSetUsedEnts, FcCharSetUsedEnts * (sizeof (FcCharLeaf *) + sizeof (FcChar16)));
1208 printf ("Total: %8s %8d %8s %8d\n",
1210 sizeof (FcCharLeaf) * FcCharLeafTotal +
1211 sizeof (FcCharSet) * FcCharSetTotal +
1212 FcCharSetTotalEnts * (sizeof (FcCharLeaf *) + sizeof (FcChar16)),
1214 sizeof (FcCharLeaf) * FcCharLeafUsed +
1215 sizeof (FcCharSet) * FcCharSetUsed +
1216 FcCharSetUsedEnts * (sizeof (FcCharLeaf *) + sizeof (FcChar16)));
1218 n = FcCharSetFreezeBase (c);
1220 if (c->bank == FC_BANK_DYNAMIC)
1222 if (c->u.dyn.leaves)
1224 FcMemFree (FC_MEM_CHARSET, c->num * sizeof (FcCharLeaf *));
1225 free (c->u.dyn.leaves);
1227 if (c->u.dyn.numbers)
1229 FcMemFree (FC_MEM_CHARSET, c->num * sizeof (FcChar16));
1230 free (c->u.dyn.numbers);
1232 FcMemFree (FC_MEM_CHARSET, sizeof (FcCharSet));
1240 FcNameUnparseCharSet (FcStrBuf *buf, const FcCharSet *c)
1248 for (FcCharSetIterStart (c, &ci);
1250 FcCharSetIterNext (c, &ci))
1252 if (!FcCharSetUnparseValue (buf, ci.ucs4))
1254 for (i = 0; i < 256/32; i++)
1255 if (!FcCharSetUnparseValue (buf, ci.leaf->map[i]))
1262 FcCharSetIter ci, checki;
1264 /* null terminate for parser */
1265 FcStrBufChar (buf, '\0');
1266 /* step back over null for life after test */
1268 check = FcNameParseCharSet (buf->buf + len);
1269 FcCharSetIterStart (c, &ci);
1270 FcCharSetIterStart (check, &checki);
1271 while (ci.leaf || checki.leaf)
1273 if (ci.ucs4 < checki.ucs4)
1275 printf ("Missing leaf node at 0x%x\n", ci.ucs4);
1276 FcCharSetIterNext (c, &ci);
1278 else if (checki.ucs4 < ci.ucs4)
1280 printf ("Extra leaf node at 0x%x\n", checki.ucs4);
1281 FcCharSetIterNext (check, &checki);
1286 FcChar32 *cm = ci.leaf->map;
1287 FcChar32 *checkm = checki.leaf->map;
1289 for (i = 0; i < 256; i += 32)
1292 printf ("Mismatching sets at 0x%08x: 0x%08x != 0x%08x\n",
1293 ci.ucs4 + i, *cm, *checkm);
1297 FcCharSetIterNext (c, &ci);
1298 FcCharSetIterNext (check, &checki);
1301 if ((missing = FcCharSetSubtractCount (c, check)))
1302 printf ("%d missing in reparsed result\n", missing);
1303 if ((missing = FcCharSetSubtractCount (check, c)))
1304 printf ("%d extra in reparsed result\n", missing);
1305 FcCharSetDestroy (check);
1313 FcCharSetNewBank(void)
1316 charset_numbers_count = 0;
1317 charset_leaf_count = 0;
1318 charset_leaf_idx_count = 0;
1322 FcCharSetNeededBytes (const FcCharSet *c)
1324 /* yes, there's redundancy */
1326 charset_leaf_idx_count++;
1327 charset_leaf_count += c->num;
1328 charset_numbers_count += c->num;
1329 return sizeof (FcCharSet) +
1330 sizeof (int) + /* leaf_idx */
1331 sizeof (FcCharLeaf) * c->num + /* leaf */
1332 sizeof (FcChar16) * c->num; /* number */
1336 FcCharSetNeededBytesAlign (void)
1338 return __alignof__ (FcCharSet) + __alignof__ (int) +
1339 __alignof__ (FcCharLeaf) + __alignof__ (FcChar16);
1343 FcCharSetEnsureBank (int bi)
1345 if (!charsets || charset_bank_count <= bi)
1347 int new_count = charset_bank_count + 2;
1354 cs = realloc(charsets, sizeof(FcCharSet*) * new_count);
1356 n = realloc(numbers, sizeof(FcChar16*) * new_count);
1358 lvs = realloc(leaves, sizeof(FcCharLeaf*) * new_count);
1360 lvi = realloc(leaf_idx, sizeof(int*) * new_count);
1363 charsets = cs; numbers = n; leaves = lvs; leaf_idx = lvi;
1364 for (i = charset_bank_count; i < new_count; i++)
1371 charset_bank_count = new_count;
1377 FcCharSetDistributeBytes (FcCache * metadata, void * block_ptr)
1379 int bi = FcCacheBankToIndex(metadata->bank);
1380 if (!FcCharSetEnsureBank(bi))
1383 block_ptr = ALIGN (block_ptr, FcCharSet);
1384 charsets[bi] = (FcCharSet *)block_ptr;
1385 block_ptr = (void *)((char *)block_ptr +
1386 (sizeof (FcCharSet) * charset_count));
1387 block_ptr = ALIGN (block_ptr, FcChar16);
1388 numbers[bi] = (FcChar16 *)block_ptr;
1389 block_ptr = (void *)((char *)block_ptr +
1390 (sizeof(FcChar16) * charset_numbers_count));
1391 block_ptr = ALIGN (block_ptr, FcCharLeaf);
1392 leaves[bi] = (FcCharLeaf *)block_ptr;
1393 block_ptr = (void *)((char *)block_ptr +
1394 (sizeof(FcCharLeaf) * charset_leaf_count));
1395 block_ptr = ALIGN (block_ptr, int);
1396 leaf_idx[bi] = (int *)block_ptr;
1397 block_ptr = (void *)((char *)block_ptr +
1398 (sizeof(int) * charset_leaf_idx_count));
1400 metadata->charset_count = charset_count;
1401 metadata->charset_numbers_count = charset_numbers_count;
1402 metadata->charset_leaf_count = charset_leaf_count;
1403 metadata->charset_leaf_idx_count = charset_leaf_idx_count;
1404 charset_ptr = 0; charset_leaf_ptr = 0;
1405 charset_leaf_idx_ptr = 0; charset_numbers_ptr = 0;
1410 FcCharSetSerialize(int bank, FcCharSet *c)
1414 int bi = FcCacheBankToIndex(bank), cp = charset_ptr;
1416 new.ref = FC_REF_CONSTANT;
1418 new.u.stat.leafidx_offset = charset_leaf_idx_ptr;
1419 new.u.stat.numbers_offset = charset_numbers_ptr;
1422 charsets[bi][charset_ptr++] = new;
1424 leaf_idx[bi][charset_leaf_idx_ptr++] = charset_leaf_ptr;
1425 for (i = 0; i < c->num; i++)
1427 memcpy (&leaves[bi][charset_leaf_ptr++],
1428 c->u.dyn.leaves[i], sizeof(FcCharLeaf));
1429 numbers[bi][charset_numbers_ptr++] = c->u.dyn.numbers[i];
1432 return &charsets[bi][cp];
1436 FcCharSetUnserialize (FcCache *metadata, void *block_ptr)
1438 int bi = FcCacheBankToIndex(metadata->bank);
1439 if (!FcCharSetEnsureBank(bi))
1442 block_ptr = ALIGN (block_ptr, FcCharSet);
1443 charsets[bi] = (FcCharSet *)block_ptr;
1444 block_ptr = (void *)((char *)block_ptr +
1445 (sizeof (FcCharSet) * metadata->charset_count));
1446 block_ptr = ALIGN (block_ptr, FcChar16);
1447 numbers[bi] = (FcChar16 *)block_ptr;
1448 block_ptr = (void *)((char *)block_ptr +
1449 (sizeof(FcChar16) * metadata->charset_numbers_count));
1450 block_ptr = ALIGN (block_ptr, FcCharLeaf);
1451 leaves[bi] = (FcCharLeaf *)block_ptr;
1452 block_ptr = (void *)((char *)block_ptr +
1453 (sizeof(FcCharLeaf) * metadata->charset_leaf_count));
1454 block_ptr = ALIGN (block_ptr, int);
1455 leaf_idx[bi] = (int *)block_ptr;
1456 block_ptr = (void *)((char *)block_ptr +
1457 (sizeof(int) * metadata->charset_leaf_idx_count));
1463 FcCharSetGetLeaf(const FcCharSet *c, int i)
1466 if (c->bank == FC_BANK_DYNAMIC)
1467 return c->u.dyn.leaves[i];
1468 bi = FcCacheBankToIndex(c->bank);
1470 return &leaves[bi][leaf_idx[bi][c->u.stat.leafidx_offset]+i];
1474 FcCharSetGetNumbers(const FcCharSet *c)
1477 if (c->bank == FC_BANK_DYNAMIC)
1478 return c->u.dyn.numbers;
1479 bi = FcCacheBankToIndex(c->bank);
1481 return &numbers[bi][c->u.stat.numbers_offset];