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
)
171 /* convert to dynamic */
174 leaves
= malloc ((fcs
->num
+ 1) * sizeof (FcCharLeaf
*));
177 FcMemAlloc (FC_MEM_CHARSET
, (fcs
->num
+ 1) * sizeof (FcCharLeaf
*));
178 numbers
= malloc ((fcs
->num
+ 1) * sizeof (FcChar16
));
184 FcMemAlloc (FC_MEM_CHARSET
, (fcs
->num
+ 1) * sizeof (FcChar16
));
186 for (i
= 0; i
< fcs
->num
; i
++)
187 leaves
[i
] = FcCharSetGetLeaf(fcs
, i
);
188 memcpy (numbers
, FcCharSetGetNumbers(fcs
),
189 fcs
->num
* sizeof (FcChar16
));
191 fcs
->bank
= FC_BANK_DYNAMIC
;
192 fcs
->u
.dyn
.leaves
= leaves
;
193 fcs
->u
.dyn
.numbers
= numbers
;
197 if (!fcs
->u
.dyn
.leaves
)
198 leaves
= malloc (sizeof (FcCharLeaf
*));
200 leaves
= realloc (fcs
->u
.dyn
.leaves
, (fcs
->num
+ 1) * sizeof (FcCharLeaf
*));
204 FcMemFree (FC_MEM_CHARSET
, fcs
->num
* sizeof (FcCharLeaf
*));
205 FcMemAlloc (FC_MEM_CHARSET
, (fcs
->num
+ 1) * sizeof (FcCharLeaf
*));
206 fcs
->u
.dyn
.leaves
= leaves
;
207 if (!fcs
->u
.dyn
.numbers
)
208 numbers
= malloc (sizeof (FcChar16
));
210 numbers
= realloc (fcs
->u
.dyn
.numbers
, (fcs
->num
+ 1) * sizeof (FcChar16
));
214 FcMemFree (FC_MEM_CHARSET
, fcs
->num
* sizeof (FcChar16
));
215 FcMemAlloc (FC_MEM_CHARSET
, (fcs
->num
+ 1) * sizeof (FcChar16
));
216 fcs
->u
.dyn
.numbers
= numbers
;
219 memmove (fcs
->u
.dyn
.leaves
+ pos
+ 1, fcs
->u
.dyn
.leaves
+ pos
,
220 (fcs
->num
- pos
) * sizeof (FcCharLeaf
*));
221 memmove (fcs
->u
.dyn
.numbers
+ pos
+ 1, fcs
->u
.dyn
.numbers
+ pos
,
222 (fcs
->num
- pos
) * sizeof (FcChar16
));
223 fcs
->u
.dyn
.numbers
[pos
] = (FcChar16
) ucs4
;
224 fcs
->u
.dyn
.leaves
[pos
] = leaf
;
230 * Locate the leaf containing the specified char, creating it
235 FcCharSetFindLeafCreate (FcCharSet
*fcs
, FcChar32 ucs4
)
240 pos
= FcCharSetFindLeafPos (fcs
, ucs4
);
242 return FcCharSetGetLeaf(fcs
, pos
);
244 leaf
= calloc (1, sizeof (FcCharLeaf
));
249 if (!FcCharSetPutLeaf (fcs
, ucs4
, leaf
, pos
))
254 FcMemAlloc (FC_MEM_CHARLEAF
, sizeof (FcCharLeaf
));
259 FcCharSetInsertLeaf (FcCharSet
*fcs
, FcChar32 ucs4
, FcCharLeaf
*leaf
)
263 pos
= FcCharSetFindLeafPos (fcs
, ucs4
);
266 FcMemFree (FC_MEM_CHARLEAF
, sizeof (FcCharLeaf
));
267 if (fcs
->bank
== FC_BANK_DYNAMIC
)
269 free (fcs
->u
.dyn
.leaves
[pos
]);
270 fcs
->u
.dyn
.leaves
[pos
] = leaf
;
274 int bi
= FcCacheBankToIndex(fcs
->bank
);
275 leaves
[bi
][leaf_idx
[fcs
->bank
][fcs
->u
.stat
.leafidx_offset
]+pos
] = *leaf
;
280 return FcCharSetPutLeaf (fcs
, ucs4
, leaf
, pos
);
284 FcCharSetAddChar (FcCharSet
*fcs
, FcChar32 ucs4
)
289 if (fcs
->ref
== FC_REF_CONSTANT
)
291 leaf
= FcCharSetFindLeafCreate (fcs
, ucs4
);
294 b
= &leaf
->map
[(ucs4
& 0xff) >> 5];
295 *b
|= (1 << (ucs4
& 0x1f));
300 * An iterator for the leaves of a charset
303 typedef struct _fcCharSetIter
{
310 * Set iter->leaf to the leaf containing iter->ucs4 or higher
314 FcCharSetIterSet (const FcCharSet
*fcs
, FcCharSetIter
*iter
)
316 int pos
= FcCharSetFindLeafPos (fcs
, iter
->ucs4
);
327 iter
->ucs4
= (FcChar32
) FcCharSetGetNumbers(fcs
)[pos
] << 8;
329 iter
->leaf
= FcCharSetGetLeaf(fcs
, pos
);
332 printf ("set %08x: %08x\n", iter
->ucs4
, (FcChar32
) iter
->leaf
);
337 FcCharSetIterNext (const FcCharSet
*fcs
, FcCharSetIter
*iter
)
339 int pos
= iter
->pos
+ 1;
347 iter
->ucs4
= (FcChar32
) FcCharSetGetNumbers(fcs
)[pos
] << 8;
348 iter
->leaf
= FcCharSetGetLeaf(fcs
, pos
);
355 FcCharSetDump (const FcCharSet
*fcs
)
359 printf ("fcs %08x:\n", (FcChar32
) fcs
);
360 for (pos
= 0; pos
< fcs
->num
; pos
++)
362 FcCharLeaf
*leaf
= fcs
->leaves
[pos
];
363 FcChar32 ucs4
= (FcChar32
) fcs
->numbers
[pos
] << 8;
365 printf (" %08x: %08x\n", ucs4
, (FcChar32
) leaf
);
371 FcCharSetIterStart (const FcCharSet
*fcs
, FcCharSetIter
*iter
)
378 FcCharSetIterSet (fcs
, iter
);
382 FcCharSetCopy (FcCharSet
*src
)
384 if (src
->ref
!= FC_REF_CONSTANT
)
390 FcCharSetEqual (const FcCharSet
*a
, const FcCharSet
*b
)
392 FcCharSetIter ai
, bi
;
397 for (FcCharSetIterStart (a
, &ai
), FcCharSetIterStart (b
, &bi
);
399 FcCharSetIterNext (a
, &ai
), FcCharSetIterNext (b
, &bi
))
401 if (ai
.ucs4
!= bi
.ucs4
)
403 for (i
= 0; i
< 256/32; i
++)
404 if (ai
.leaf
->map
[i
] != bi
.leaf
->map
[i
])
407 return ai
.leaf
== bi
.leaf
;
411 FcCharSetAddLeaf (FcCharSet
*fcs
,
415 FcCharLeaf
*new = FcCharSetFindLeafCreate (fcs
, ucs4
);
423 FcCharSetOperate (const FcCharSet
*a
,
425 FcBool (*overlap
) (FcCharLeaf
*result
,
426 const FcCharLeaf
*al
,
427 const FcCharLeaf
*bl
),
432 FcCharSetIter ai
, bi
;
434 fcs
= FcCharSetCreate ();
437 FcCharSetIterStart (a
, &ai
);
438 FcCharSetIterStart (b
, &bi
);
439 while ((ai
.leaf
|| (bonly
&& bi
.leaf
)) && (bi
.leaf
|| (aonly
&& ai
.leaf
)))
441 if (ai
.ucs4
< bi
.ucs4
)
445 if (!FcCharSetAddLeaf (fcs
, ai
.ucs4
, ai
.leaf
))
447 FcCharSetIterNext (a
, &ai
);
452 FcCharSetIterSet (a
, &ai
);
455 else if (bi
.ucs4
< ai
.ucs4
)
459 if (!FcCharSetAddLeaf (fcs
, bi
.ucs4
, bi
.leaf
))
461 FcCharSetIterNext (b
, &bi
);
466 FcCharSetIterSet (b
, &bi
);
473 if ((*overlap
) (&leaf
, ai
.leaf
, bi
.leaf
))
475 if (!FcCharSetAddLeaf (fcs
, ai
.ucs4
, &leaf
))
478 FcCharSetIterNext (a
, &ai
);
479 FcCharSetIterNext (b
, &bi
);
484 FcCharSetDestroy (fcs
);
490 FcCharSetIntersectLeaf (FcCharLeaf
*result
,
491 const FcCharLeaf
*al
,
492 const FcCharLeaf
*bl
)
495 FcBool nonempty
= FcFalse
;
497 for (i
= 0; i
< 256/32; i
++)
498 if ((result
->map
[i
] = al
->map
[i
] & bl
->map
[i
]))
504 FcCharSetIntersect (const FcCharSet
*a
, const FcCharSet
*b
)
506 return FcCharSetOperate (a
, b
, FcCharSetIntersectLeaf
, FcFalse
, FcFalse
);
510 FcCharSetUnionLeaf (FcCharLeaf
*result
,
511 const FcCharLeaf
*al
,
512 const FcCharLeaf
*bl
)
516 for (i
= 0; i
< 256/32; i
++)
517 result
->map
[i
] = al
->map
[i
] | bl
->map
[i
];
522 FcCharSetUnion (const FcCharSet
*a
, const FcCharSet
*b
)
524 return FcCharSetOperate (a
, b
, FcCharSetUnionLeaf
, FcTrue
, FcTrue
);
528 FcCharSetSubtractLeaf (FcCharLeaf
*result
,
529 const FcCharLeaf
*al
,
530 const FcCharLeaf
*bl
)
533 FcBool nonempty
= FcFalse
;
535 for (i
= 0; i
< 256/32; i
++)
536 if ((result
->map
[i
] = al
->map
[i
] & ~bl
->map
[i
]))
542 FcCharSetSubtract (const FcCharSet
*a
, const FcCharSet
*b
)
544 return FcCharSetOperate (a
, b
, FcCharSetSubtractLeaf
, FcTrue
, FcFalse
);
548 FcCharSetHasChar (const FcCharSet
*fcs
, FcChar32 ucs4
)
550 FcCharLeaf
*leaf
= FcCharSetFindLeaf (fcs
, ucs4
);
553 return (leaf
->map
[(ucs4
& 0xff) >> 5] & (1 << (ucs4
& 0x1f))) != 0;
557 FcCharSetPopCount (FcChar32 c1
)
560 FcChar32 c2
= (c1
>> 1) & 033333333333;
561 c2
= c1
- c2
- ((c2
>> 1) & 033333333333);
562 return (((c2
+ (c2
>> 3)) & 030707070707) % 077);
566 FcCharSetIntersectCount (const FcCharSet
*a
, const FcCharSet
*b
)
568 FcCharSetIter ai
, bi
;
571 FcCharSetIterStart (a
, &ai
);
572 FcCharSetIterStart (b
, &bi
);
573 while (ai
.leaf
&& bi
.leaf
)
575 if (ai
.ucs4
== bi
.ucs4
)
577 FcChar32
*am
= ai
.leaf
->map
;
578 FcChar32
*bm
= bi
.leaf
->map
;
581 count
+= FcCharSetPopCount (*am
++ & *bm
++);
582 FcCharSetIterNext (a
, &ai
);
584 else if (ai
.ucs4
< bi
.ucs4
)
587 FcCharSetIterSet (a
, &ai
);
589 if (bi
.ucs4
< ai
.ucs4
)
592 FcCharSetIterSet (b
, &bi
);
599 FcCharSetCount (const FcCharSet
*a
)
604 for (FcCharSetIterStart (a
, &ai
); ai
.leaf
; FcCharSetIterNext (a
, &ai
))
607 FcChar32
*am
= ai
.leaf
->map
;
610 count
+= FcCharSetPopCount (*am
++);
616 FcCharSetSubtractCount (const FcCharSet
*a
, const FcCharSet
*b
)
618 FcCharSetIter ai
, bi
;
621 FcCharSetIterStart (a
, &ai
);
622 FcCharSetIterStart (b
, &bi
);
625 if (ai
.ucs4
<= bi
.ucs4
)
627 FcChar32
*am
= ai
.leaf
->map
;
629 if (ai
.ucs4
== bi
.ucs4
)
631 FcChar32
*bm
= bi
.leaf
->map
;
633 count
+= FcCharSetPopCount (*am
++ & ~*bm
++);
638 count
+= FcCharSetPopCount (*am
++);
640 FcCharSetIterNext (a
, &ai
);
645 FcCharSetIterSet (b
, &bi
);
652 * return FcTrue iff a is a subset of b
655 FcCharSetIsSubset (const FcCharSet
*a
, const FcCharSet
*b
)
660 if (a
== b
) return FcTrue
;
663 while (ai
< a
->num
&& bi
< b
->num
)
665 an
= FcCharSetGetNumbers(a
)[ai
];
666 bn
= FcCharSetGetNumbers(b
)[bi
];
668 * Check matching pages
672 FcChar32
*am
= FcCharSetGetLeaf(a
, ai
)->map
;
673 FcChar32
*bm
= FcCharSetGetLeaf(b
, bi
)->map
;
679 * Does am have any bits not in bm?
689 * Does a have any pages not in b?
696 int high
= b
->num
- 1;
699 * Search for page 'an' in 'b'
703 int mid
= (low
+ high
) >> 1;
704 bn
= FcCharSetGetNumbers(b
)[mid
];
716 while (bi
< b
->num
&& FcCharSetGetNumbers(b
)[bi
] < an
)
721 * did we look at every page?
727 * These two functions efficiently walk the entire charmap for
728 * other software (like pango) that want their own copy
732 FcCharSetNextPage (const FcCharSet
*a
,
733 FcChar32 map
[FC_CHARSET_MAP_SIZE
],
740 FcCharSetIterSet (a
, &ai
);
742 return FC_CHARSET_DONE
;
745 * Save current information
748 memcpy (map
, ai
.leaf
->map
, sizeof (ai
.leaf
->map
));
752 FcCharSetIterNext (a
, &ai
);
759 FcCharSetFirstPage (const FcCharSet
*a
,
760 FcChar32 map
[FC_CHARSET_MAP_SIZE
],
764 return FcCharSetNextPage (a
, map
, next
);
768 * old coverage API, rather hard to use correctly
771 FcCharSetCoverage (const FcCharSet
*a
, FcChar32 page
, FcChar32
*result
);
774 FcCharSetCoverage (const FcCharSet
*a
, FcChar32 page
, FcChar32
*result
)
779 FcCharSetIterSet (a
, &ai
);
782 memset (result
, '\0', 256 / 8);
787 memcpy (result
, ai
.leaf
->map
, sizeof (ai
.leaf
->map
));
788 FcCharSetIterNext (a
, &ai
);
795 * ASCII representation of charsets.
797 * Each leaf is represented as 9 32-bit values, the code of the first character followed
798 * by 8 32 bit values for the leaf itself. Each value is encoded as 5 ASCII characters,
799 * only 85 different values are used to avoid control characters as well as the other
800 * characters used to encode font names. 85**5 > 2^32 so things work out, but
801 * it's not exactly human readable output. As a special case, 0 is encoded as a space
804 static const unsigned char charToValue
[256] = {
805 /* "" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
806 /* "\b" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
807 /* "\020" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
808 /* "\030" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
809 /* " " */ 0xff, 0x00, 0xff, 0x01, 0x02, 0x03, 0x04, 0xff,
810 /* "(" */ 0x05, 0x06, 0x07, 0x08, 0xff, 0xff, 0x09, 0x0a,
811 /* "0" */ 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12,
812 /* "8" */ 0x13, 0x14, 0xff, 0x15, 0x16, 0xff, 0x17, 0x18,
813 /* "@" */ 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
814 /* "H" */ 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
815 /* "P" */ 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30,
816 /* "X" */ 0x31, 0x32, 0x33, 0x34, 0xff, 0x35, 0x36, 0xff,
817 /* "`" */ 0xff, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d,
818 /* "h" */ 0x3e, 0x3f, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45,
819 /* "p" */ 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d,
820 /* "x" */ 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53, 0x54, 0xff,
821 /* "\200" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
822 /* "\210" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
823 /* "\220" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
824 /* "\230" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
825 /* "\240" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
826 /* "\250" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
827 /* "\260" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
828 /* "\270" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
829 /* "\300" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
830 /* "\310" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
831 /* "\320" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
832 /* "\330" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
833 /* "\340" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
834 /* "\350" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
835 /* "\360" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
836 /* "\370" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
839 static const FcChar8 valueToChar
[0x55] = {
840 /* 0x00 */ '!', '#', '$', '%', '&', '(', ')', '*',
841 /* 0x08 */ '+', '.', '/', '0', '1', '2', '3', '4',
842 /* 0x10 */ '5', '6', '7', '8', '9', ';', '<', '>',
843 /* 0x18 */ '?', '@', 'A', 'B', 'C', 'D', 'E', 'F',
844 /* 0x20 */ 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
845 /* 0x28 */ 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
846 /* 0x30 */ 'W', 'X', 'Y', 'Z', '[', ']', '^', 'a',
847 /* 0x38 */ 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
848 /* 0x40 */ 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q',
849 /* 0x48 */ 'r', 's', 't', 'u', 'v', 'w', 'x', 'y',
850 /* 0x50 */ 'z', '{', '|', '}', '~',
854 FcCharSetParseValue (FcChar8
*string
, FcChar32
*value
)
868 for (i
= 0; i
< 5; i
++)
870 if (!(c
= (FcChar32
) (unsigned char) *string
++))
883 FcCharSetUnparseValue (FcStrBuf
*buf
, FcChar32 value
)
888 return FcStrBufChar (buf
, ' ');
893 FcChar8
*s
= string
+ 5;
895 for (i
= 0; i
< 5; i
++)
897 *--s
= valueToChar
[value
% 85];
900 for (i
= 0; i
< 5; i
++)
901 if (!FcStrBufChar (buf
, *s
++))
907 typedef struct _FcCharLeafEnt FcCharLeafEnt
;
909 struct _FcCharLeafEnt
{
915 #define FC_CHAR_LEAF_BLOCK (4096 / sizeof (FcCharLeafEnt))
916 static FcCharLeafEnt
**FcCharLeafBlocks
;
917 static int FcCharLeafBlockCount
;
919 static FcCharLeafEnt
*
920 FcCharLeafEntCreate (void)
922 static FcCharLeafEnt
*block
;
927 FcCharLeafEnt
**newBlocks
;
929 FcCharLeafBlockCount
++;
930 newBlocks
= realloc (FcCharLeafBlocks
, FcCharLeafBlockCount
* sizeof (FcCharLeafEnt
*));
933 FcCharLeafBlocks
= newBlocks
;
934 block
= FcCharLeafBlocks
[FcCharLeafBlockCount
-1] = malloc (FC_CHAR_LEAF_BLOCK
* sizeof (FcCharLeafEnt
));
937 FcMemAlloc (FC_MEM_CHARLEAF
, FC_CHAR_LEAF_BLOCK
* sizeof (FcCharLeafEnt
));
938 remain
= FC_CHAR_LEAF_BLOCK
;
944 #define FC_CHAR_LEAF_HASH_SIZE 257
947 FcCharLeafHash (FcCharLeaf
*leaf
)
952 for (i
= 0; i
< 256/32; i
++)
953 hash
= ((hash
<< 1) | (hash
>> 31)) ^ leaf
->map
[i
];
957 static int FcCharLeafTotal
;
958 static int FcCharLeafUsed
;
960 static FcCharLeafEnt
*FcCharLeafHashTable
[FC_CHAR_LEAF_HASH_SIZE
];
963 FcCharSetFreezeLeaf (FcCharLeaf
*leaf
)
965 FcChar32 hash
= FcCharLeafHash (leaf
);
966 FcCharLeafEnt
**bucket
= &FcCharLeafHashTable
[hash
% FC_CHAR_LEAF_HASH_SIZE
];
970 for (ent
= *bucket
; ent
; ent
= ent
->next
)
972 if (ent
->hash
== hash
&& !memcmp (&ent
->leaf
, leaf
, sizeof (FcCharLeaf
)))
976 ent
= FcCharLeafEntCreate();
988 FcCharSetThawAllLeaf (void)
992 for (i
= 0; i
< FC_CHAR_LEAF_HASH_SIZE
; i
++)
993 FcCharLeafHashTable
[i
] = 0;
998 for (i
= 0; i
< FcCharLeafBlockCount
; i
++)
999 free (FcCharLeafBlocks
[i
]);
1001 free (FcCharLeafBlocks
);
1002 FcCharLeafBlocks
= 0;
1003 FcCharLeafBlockCount
= 0;
1006 typedef struct _FcCharSetEnt FcCharSetEnt
;
1008 struct _FcCharSetEnt
{
1014 #define FC_CHAR_SET_HASH_SIZE 67
1017 FcCharSetHash (FcCharSet
*fcs
)
1022 /* hash in leaves */
1023 for (i
= 0; i
< fcs
->num
* (int) (sizeof (FcCharLeaf
*) / sizeof (FcChar32
)); i
++)
1024 hash
= ((hash
<< 1) | (hash
>> 31)) ^ (FcChar32
)(FcCharSetGetLeaf(fcs
, i
)->map
);
1025 /* hash in numbers */
1026 for (i
= 0; i
< fcs
->num
; i
++)
1027 hash
= ((hash
<< 1) | (hash
>> 31)) ^ *FcCharSetGetNumbers(fcs
);
1031 static int FcCharSetTotal
;
1032 static int FcCharSetUsed
;
1033 static int FcCharSetTotalEnts
, FcCharSetUsedEnts
;
1035 static FcCharSetEnt
*FcCharSetHashTable
[FC_CHAR_SET_HASH_SIZE
];
1038 FcCharSetFreezeBase (FcCharSet
*fcs
)
1040 FcChar32 hash
= FcCharSetHash (fcs
);
1041 FcCharSetEnt
**bucket
= &FcCharSetHashTable
[hash
% FC_CHAR_SET_HASH_SIZE
];
1046 FcCharSetTotalEnts
+= fcs
->num
;
1047 for (ent
= *bucket
; ent
; ent
= ent
->next
)
1049 if (ent
->hash
== hash
&&
1050 ent
->set
.num
== fcs
->num
&&
1051 !memcmp (FcCharSetGetNumbers(&ent
->set
),
1052 FcCharSetGetNumbers(fcs
),
1053 fcs
->num
* sizeof (FcChar16
)))
1058 for (i
= 0; i
< fcs
->num
; i
++)
1059 if (FcCharSetGetLeaf(&ent
->set
, i
) != FcCharSetGetLeaf(fcs
, i
))
1066 size
= (sizeof (FcCharSetEnt
) +
1067 fcs
->num
* sizeof (FcCharLeaf
*) +
1068 fcs
->num
* sizeof (FcChar16
));
1069 ent
= malloc (size
);
1072 FcMemAlloc (FC_MEM_CHARSET
, size
);
1074 FcCharSetUsedEnts
+= fcs
->num
;
1076 ent
->set
.ref
= FC_REF_CONSTANT
;
1077 ent
->set
.num
= fcs
->num
;
1078 ent
->set
.bank
= fcs
->bank
;
1079 if (fcs
->bank
== FC_BANK_DYNAMIC
)
1083 ent
->set
.u
.dyn
.leaves
= (FcCharLeaf
**) (ent
+ 1);
1084 ent
->set
.u
.dyn
.numbers
= (FcChar16
*) (ent
->set
.u
.dyn
.leaves
+ fcs
->num
);
1085 memcpy (ent
->set
.u
.dyn
.leaves
, fcs
->u
.dyn
.leaves
, fcs
->num
* sizeof (FcCharLeaf
*));
1086 memcpy (ent
->set
.u
.dyn
.numbers
, fcs
->u
.dyn
.numbers
, fcs
->num
* sizeof (FcChar16
));
1090 ent
->set
.u
.dyn
.leaves
= 0;
1091 ent
->set
.u
.dyn
.numbers
= 0;
1096 ent
->set
.u
.stat
.leafidx_offset
= fcs
->u
.stat
.leafidx_offset
;
1097 ent
->set
.u
.stat
.numbers_offset
= fcs
->u
.stat
.numbers_offset
;
1101 ent
->next
= *bucket
;
1107 FcCharSetThawAll (void)
1110 FcCharSetEnt
*ent
, *next
;
1112 for (i
= 0; i
< FC_CHAR_SET_HASH_SIZE
; i
++)
1114 for (ent
= FcCharSetHashTable
[i
]; ent
; ent
= next
)
1119 FcCharSetHashTable
[i
] = 0;
1123 FcCharSetTotalEnts
= 0;
1125 FcCharSetUsedEnts
= 0;
1127 FcCharSetThawAllLeaf ();
1131 FcCharSetFreeze (FcCharSet
*fcs
)
1138 b
= FcCharSetCreate ();
1141 for (i
= 0; i
< fcs
->num
; i
++)
1143 l
= FcCharSetFreezeLeaf (FcCharSetGetLeaf(fcs
, i
));
1146 if (!FcCharSetInsertLeaf (b
, FcCharSetGetNumbers(fcs
)[i
] << 8, l
))
1149 n
= FcCharSetFreezeBase (b
);
1151 if (b
->bank
== FC_BANK_DYNAMIC
)
1153 if (b
->u
.dyn
.leaves
)
1155 FcMemFree (FC_MEM_CHARSET
, b
->num
* sizeof (FcCharLeaf
*));
1156 free (b
->u
.dyn
.leaves
);
1158 if (b
->u
.dyn
.numbers
)
1160 FcMemFree (FC_MEM_CHARSET
, b
->num
* sizeof (FcChar16
));
1161 free (b
->u
.dyn
.numbers
);
1164 FcMemFree (FC_MEM_CHARSET
, sizeof (FcCharSet
));
1171 FcNameParseCharSet (FcChar8
*string
)
1173 FcCharSet
*c
, *n
= 0;
1180 c
= FcCharSetCreate ();
1185 string
= FcCharSetParseValue (string
, &ucs4
);
1189 for (i
= 0; i
< 256/32; i
++)
1191 string
= FcCharSetParseValue (string
, &temp
.map
[i
]);
1194 bits
|= temp
.map
[i
];
1198 leaf
= FcCharSetFreezeLeaf (&temp
);
1201 if (!FcCharSetInsertLeaf (c
, ucs4
, leaf
))
1206 printf (" %8s %8s %8s %8s\n", "total", "totalmem", "new", "newmem");
1207 printf ("Leaves: %8d %8d %8d %8d\n",
1208 FcCharLeafTotal
, sizeof (FcCharLeaf
) * FcCharLeafTotal
,
1209 FcCharLeafUsed
, sizeof (FcCharLeaf
) * FcCharLeafUsed
);
1210 printf ("Charsets: %8d %8d %8d %8d\n",
1211 FcCharSetTotal
, sizeof (FcCharSet
) * FcCharSetTotal
,
1212 FcCharSetUsed
, sizeof (FcCharSet
) * FcCharSetUsed
);
1213 printf ("Tables: %8d %8d %8d %8d\n",
1214 FcCharSetTotalEnts
, FcCharSetTotalEnts
* (sizeof (FcCharLeaf
*) + sizeof (FcChar16
)),
1215 FcCharSetUsedEnts
, FcCharSetUsedEnts
* (sizeof (FcCharLeaf
*) + sizeof (FcChar16
)));
1216 printf ("Total: %8s %8d %8s %8d\n",
1218 sizeof (FcCharLeaf
) * FcCharLeafTotal
+
1219 sizeof (FcCharSet
) * FcCharSetTotal
+
1220 FcCharSetTotalEnts
* (sizeof (FcCharLeaf
*) + sizeof (FcChar16
)),
1222 sizeof (FcCharLeaf
) * FcCharLeafUsed
+
1223 sizeof (FcCharSet
) * FcCharSetUsed
+
1224 FcCharSetUsedEnts
* (sizeof (FcCharLeaf
*) + sizeof (FcChar16
)));
1226 n
= FcCharSetFreezeBase (c
);
1228 if (c
->bank
== FC_BANK_DYNAMIC
)
1230 if (c
->u
.dyn
.leaves
)
1232 FcMemFree (FC_MEM_CHARSET
, c
->num
* sizeof (FcCharLeaf
*));
1233 free (c
->u
.dyn
.leaves
);
1235 if (c
->u
.dyn
.numbers
)
1237 FcMemFree (FC_MEM_CHARSET
, c
->num
* sizeof (FcChar16
));
1238 free (c
->u
.dyn
.numbers
);
1240 FcMemFree (FC_MEM_CHARSET
, sizeof (FcCharSet
));
1248 FcNameUnparseCharSet (FcStrBuf
*buf
, const FcCharSet
*c
)
1256 for (FcCharSetIterStart (c
, &ci
);
1258 FcCharSetIterNext (c
, &ci
))
1260 if (!FcCharSetUnparseValue (buf
, ci
.ucs4
))
1262 for (i
= 0; i
< 256/32; i
++)
1263 if (!FcCharSetUnparseValue (buf
, ci
.leaf
->map
[i
]))
1270 FcCharSetIter ci
, checki
;
1272 /* null terminate for parser */
1273 FcStrBufChar (buf
, '\0');
1274 /* step back over null for life after test */
1276 check
= FcNameParseCharSet (buf
->buf
+ len
);
1277 FcCharSetIterStart (c
, &ci
);
1278 FcCharSetIterStart (check
, &checki
);
1279 while (ci
.leaf
|| checki
.leaf
)
1281 if (ci
.ucs4
< checki
.ucs4
)
1283 printf ("Missing leaf node at 0x%x\n", ci
.ucs4
);
1284 FcCharSetIterNext (c
, &ci
);
1286 else if (checki
.ucs4
< ci
.ucs4
)
1288 printf ("Extra leaf node at 0x%x\n", checki
.ucs4
);
1289 FcCharSetIterNext (check
, &checki
);
1294 FcChar32
*cm
= ci
.leaf
->map
;
1295 FcChar32
*checkm
= checki
.leaf
->map
;
1297 for (i
= 0; i
< 256; i
+= 32)
1300 printf ("Mismatching sets at 0x%08x: 0x%08x != 0x%08x\n",
1301 ci
.ucs4
+ i
, *cm
, *checkm
);
1305 FcCharSetIterNext (c
, &ci
);
1306 FcCharSetIterNext (check
, &checki
);
1309 if ((missing
= FcCharSetSubtractCount (c
, check
)))
1310 printf ("%d missing in reparsed result\n", missing
);
1311 if ((missing
= FcCharSetSubtractCount (check
, c
)))
1312 printf ("%d extra in reparsed result\n", missing
);
1313 FcCharSetDestroy (check
);
1321 FcCharSetNewBank(void)
1324 charset_numbers_count
= 0;
1325 charset_leaf_count
= 0;
1326 charset_leaf_idx_count
= 0;
1330 FcCharSetNeededBytes (const FcCharSet
*c
)
1332 /* yes, there's redundancy */
1334 charset_leaf_idx_count
+= c
->num
;
1335 charset_leaf_count
+= c
->num
;
1336 charset_numbers_count
+= c
->num
;
1337 return sizeof (FcCharSet
) +
1338 sizeof (int) * c
->num
+ /* leaf_idx */
1339 sizeof (FcCharLeaf
) * c
->num
+ /* leaf */
1340 sizeof (FcChar16
) * c
->num
; /* number */
1344 FcCharSetNeededBytesAlign (void)
1346 return fc_alignof (FcCharSet
) + fc_alignof (int) +
1347 fc_alignof (FcCharLeaf
) + fc_alignof (FcChar16
);
1351 FcCharSetEnsureBank (int bi
)
1353 if (!charsets
|| charset_bank_count
<= bi
)
1355 int new_count
= charset_bank_count
+ 2;
1362 cs
= realloc(charsets
, sizeof(FcCharSet
*) * new_count
);
1364 n
= realloc(numbers
, sizeof(FcChar16
*) * new_count
);
1366 lvs
= realloc(leaves
, sizeof(FcCharLeaf
*) * new_count
);
1368 lvi
= realloc(leaf_idx
, sizeof(int*) * new_count
);
1371 charsets
= cs
; numbers
= n
; leaves
= lvs
; leaf_idx
= lvi
;
1372 for (i
= charset_bank_count
; i
< new_count
; i
++)
1379 charset_bank_count
= new_count
;
1385 FcCharSetDistributeBytes (FcCache
* metadata
, void * block_ptr
)
1387 int bi
= FcCacheBankToIndex(metadata
->bank
);
1388 if (!FcCharSetEnsureBank(bi
))
1391 block_ptr
= ALIGN (block_ptr
, FcCharSet
);
1392 charsets
[bi
] = (FcCharSet
*)block_ptr
;
1393 block_ptr
= (void *)((char *)block_ptr
+
1394 (sizeof (FcCharSet
) * charset_count
));
1395 block_ptr
= ALIGN (block_ptr
, FcChar16
);
1396 numbers
[bi
] = (FcChar16
*)block_ptr
;
1397 block_ptr
= (void *)((char *)block_ptr
+
1398 (sizeof(FcChar16
) * charset_numbers_count
));
1399 block_ptr
= ALIGN (block_ptr
, FcCharLeaf
);
1400 leaves
[bi
] = (FcCharLeaf
*)block_ptr
;
1401 block_ptr
= (void *)((char *)block_ptr
+
1402 (sizeof(FcCharLeaf
) * charset_leaf_count
));
1403 block_ptr
= ALIGN (block_ptr
, int);
1404 leaf_idx
[bi
] = (int *)block_ptr
;
1405 block_ptr
= (void *)((char *)block_ptr
+
1406 (sizeof(int) * charset_leaf_idx_count
));
1408 metadata
->charset_count
= charset_count
;
1409 metadata
->charset_numbers_count
= charset_numbers_count
;
1410 metadata
->charset_leaf_count
= charset_leaf_count
;
1411 metadata
->charset_leaf_idx_count
= charset_leaf_idx_count
;
1412 charset_ptr
= 0; charset_leaf_ptr
= 0;
1413 charset_leaf_idx_ptr
= 0; charset_numbers_ptr
= 0;
1418 FcCharSetSerialize(int bank
, FcCharSet
*c
)
1422 int bi
= FcCacheBankToIndex(bank
), cp
= charset_ptr
;
1424 new.ref
= FC_REF_CONSTANT
;
1426 new.u
.stat
.leafidx_offset
= charset_leaf_idx_ptr
;
1427 new.u
.stat
.numbers_offset
= charset_numbers_ptr
;
1430 charsets
[bi
][charset_ptr
++] = new;
1432 for (i
= 0; i
< c
->num
; i
++)
1434 leaf_idx
[bi
][charset_leaf_idx_ptr
++] = charset_leaf_ptr
;
1435 memcpy (&leaves
[bi
][charset_leaf_ptr
++],
1436 c
->u
.dyn
.leaves
[i
], sizeof(FcCharLeaf
));
1437 numbers
[bi
][charset_numbers_ptr
++] = c
->u
.dyn
.numbers
[i
];
1440 return &charsets
[bi
][cp
];
1444 FcCharSetUnserialize (FcCache
*metadata
, void *block_ptr
)
1446 int bi
= FcCacheBankToIndex(metadata
->bank
);
1447 if (!FcCharSetEnsureBank(bi
))
1450 block_ptr
= ALIGN (block_ptr
, FcCharSet
);
1451 charsets
[bi
] = (FcCharSet
*)block_ptr
;
1452 block_ptr
= (void *)((char *)block_ptr
+
1453 (sizeof (FcCharSet
) * metadata
->charset_count
));
1454 block_ptr
= ALIGN (block_ptr
, FcChar16
);
1455 numbers
[bi
] = (FcChar16
*)block_ptr
;
1456 block_ptr
= (void *)((char *)block_ptr
+
1457 (sizeof(FcChar16
) * metadata
->charset_numbers_count
));
1458 block_ptr
= ALIGN (block_ptr
, FcCharLeaf
);
1459 leaves
[bi
] = (FcCharLeaf
*)block_ptr
;
1460 block_ptr
= (void *)((char *)block_ptr
+
1461 (sizeof(FcCharLeaf
) * metadata
->charset_leaf_count
));
1462 block_ptr
= ALIGN (block_ptr
, int);
1463 leaf_idx
[bi
] = (int *)block_ptr
;
1464 block_ptr
= (void *)((char *)block_ptr
+
1465 (sizeof(int) * metadata
->charset_leaf_idx_count
));
1471 FcCharSetGetLeaf(const FcCharSet
*c
, int i
)
1474 if (c
->bank
== FC_BANK_DYNAMIC
)
1475 return c
->u
.dyn
.leaves
[i
];
1476 bi
= FcCacheBankToIndex(c
->bank
);
1478 return &leaves
[bi
][leaf_idx
[bi
][c
->u
.stat
.leafidx_offset
+i
]];
1482 FcCharSetGetNumbers(const FcCharSet
*c
)
1485 if (c
->bank
== FC_BANK_DYNAMIC
)
1486 return c
->u
.dyn
.numbers
;
1487 bi
= FcCacheBankToIndex(c
->bank
);
1489 return &numbers
[bi
][c
->u
.stat
.numbers_offset
];