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
= FcCacheBankToIndex (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 leaves
[fcs
->bank
][leaf_idx
[fcs
->bank
][fcs
->u
.stat
.leafidx_offset
]+pos
] = *leaf
;
271 return FcCharSetPutLeaf (fcs
, ucs4
, leaf
, pos
);
275 FcCharSetAddChar (FcCharSet
*fcs
, FcChar32 ucs4
)
280 if (fcs
->ref
== FC_REF_CONSTANT
)
282 leaf
= FcCharSetFindLeafCreate (fcs
, ucs4
);
285 b
= &leaf
->map
[(ucs4
& 0xff) >> 5];
286 *b
|= (1 << (ucs4
& 0x1f));
291 * An iterator for the leaves of a charset
294 typedef struct _fcCharSetIter
{
301 * Set iter->leaf to the leaf containing iter->ucs4 or higher
305 FcCharSetIterSet (const FcCharSet
*fcs
, FcCharSetIter
*iter
)
307 int pos
= FcCharSetFindLeafPos (fcs
, iter
->ucs4
);
318 iter
->ucs4
= (FcChar32
) FcCharSetGetNumbers(fcs
)[pos
] << 8;
320 iter
->leaf
= FcCharSetGetLeaf(fcs
, pos
);
323 printf ("set %08x: %08x\n", iter
->ucs4
, (FcChar32
) iter
->leaf
);
328 FcCharSetIterNext (const FcCharSet
*fcs
, FcCharSetIter
*iter
)
330 int pos
= iter
->pos
+ 1;
338 iter
->ucs4
= (FcChar32
) FcCharSetGetNumbers(fcs
)[pos
] << 8;
339 iter
->leaf
= FcCharSetGetLeaf(fcs
, pos
);
346 FcCharSetDump (const FcCharSet
*fcs
)
350 printf ("fcs %08x:\n", (FcChar32
) fcs
);
351 for (pos
= 0; pos
< fcs
->num
; pos
++)
353 FcCharLeaf
*leaf
= fcs
->leaves
[pos
];
354 FcChar32 ucs4
= (FcChar32
) fcs
->numbers
[pos
] << 8;
356 printf (" %08x: %08x\n", ucs4
, (FcChar32
) leaf
);
362 FcCharSetIterStart (const FcCharSet
*fcs
, FcCharSetIter
*iter
)
369 FcCharSetIterSet (fcs
, iter
);
373 FcCharSetCopy (FcCharSet
*src
)
375 if (src
->ref
!= FC_REF_CONSTANT
)
381 FcCharSetEqual (const FcCharSet
*a
, const FcCharSet
*b
)
383 FcCharSetIter ai
, bi
;
388 for (FcCharSetIterStart (a
, &ai
), FcCharSetIterStart (b
, &bi
);
390 FcCharSetIterNext (a
, &ai
), FcCharSetIterNext (b
, &bi
))
392 if (ai
.ucs4
!= bi
.ucs4
)
394 for (i
= 0; i
< 256/32; i
++)
395 if (ai
.leaf
->map
[i
] != bi
.leaf
->map
[i
])
398 return ai
.leaf
== bi
.leaf
;
402 FcCharSetAddLeaf (FcCharSet
*fcs
,
406 FcCharLeaf
*new = FcCharSetFindLeafCreate (fcs
, ucs4
);
414 FcCharSetOperate (const FcCharSet
*a
,
416 FcBool (*overlap
) (FcCharLeaf
*result
,
417 const FcCharLeaf
*al
,
418 const FcCharLeaf
*bl
),
423 FcCharSetIter ai
, bi
;
425 fcs
= FcCharSetCreate ();
428 FcCharSetIterStart (a
, &ai
);
429 FcCharSetIterStart (b
, &bi
);
430 while ((ai
.leaf
|| (bonly
&& bi
.leaf
)) && (bi
.leaf
|| (aonly
&& ai
.leaf
)))
432 if (ai
.ucs4
< bi
.ucs4
)
436 if (!FcCharSetAddLeaf (fcs
, ai
.ucs4
, ai
.leaf
))
438 FcCharSetIterNext (a
, &ai
);
443 FcCharSetIterSet (a
, &ai
);
446 else if (bi
.ucs4
< ai
.ucs4
)
450 if (!FcCharSetAddLeaf (fcs
, bi
.ucs4
, bi
.leaf
))
452 FcCharSetIterNext (b
, &bi
);
457 FcCharSetIterSet (b
, &bi
);
464 if ((*overlap
) (&leaf
, ai
.leaf
, bi
.leaf
))
466 if (!FcCharSetAddLeaf (fcs
, ai
.ucs4
, &leaf
))
469 FcCharSetIterNext (a
, &ai
);
470 FcCharSetIterNext (b
, &bi
);
475 FcCharSetDestroy (fcs
);
481 FcCharSetIntersectLeaf (FcCharLeaf
*result
,
482 const FcCharLeaf
*al
,
483 const FcCharLeaf
*bl
)
486 FcBool nonempty
= FcFalse
;
488 for (i
= 0; i
< 256/32; i
++)
489 if ((result
->map
[i
] = al
->map
[i
] & bl
->map
[i
]))
495 FcCharSetIntersect (const FcCharSet
*a
, const FcCharSet
*b
)
497 return FcCharSetOperate (a
, b
, FcCharSetIntersectLeaf
, FcFalse
, FcFalse
);
501 FcCharSetUnionLeaf (FcCharLeaf
*result
,
502 const FcCharLeaf
*al
,
503 const FcCharLeaf
*bl
)
507 for (i
= 0; i
< 256/32; i
++)
508 result
->map
[i
] = al
->map
[i
] | bl
->map
[i
];
513 FcCharSetUnion (const FcCharSet
*a
, const FcCharSet
*b
)
515 return FcCharSetOperate (a
, b
, FcCharSetUnionLeaf
, FcTrue
, FcTrue
);
519 FcCharSetSubtractLeaf (FcCharLeaf
*result
,
520 const FcCharLeaf
*al
,
521 const FcCharLeaf
*bl
)
524 FcBool nonempty
= FcFalse
;
526 for (i
= 0; i
< 256/32; i
++)
527 if ((result
->map
[i
] = al
->map
[i
] & ~bl
->map
[i
]))
533 FcCharSetSubtract (const FcCharSet
*a
, const FcCharSet
*b
)
535 return FcCharSetOperate (a
, b
, FcCharSetSubtractLeaf
, FcTrue
, FcFalse
);
539 FcCharSetHasChar (const FcCharSet
*fcs
, FcChar32 ucs4
)
541 FcCharLeaf
*leaf
= FcCharSetFindLeaf (fcs
, ucs4
);
544 return (leaf
->map
[(ucs4
& 0xff) >> 5] & (1 << (ucs4
& 0x1f))) != 0;
548 FcCharSetPopCount (FcChar32 c1
)
551 FcChar32 c2
= (c1
>> 1) & 033333333333;
552 c2
= c1
- c2
- ((c2
>> 1) & 033333333333);
553 return (((c2
+ (c2
>> 3)) & 030707070707) % 077);
557 FcCharSetIntersectCount (const FcCharSet
*a
, const FcCharSet
*b
)
559 FcCharSetIter ai
, bi
;
562 FcCharSetIterStart (a
, &ai
);
563 FcCharSetIterStart (b
, &bi
);
564 while (ai
.leaf
&& bi
.leaf
)
566 if (ai
.ucs4
== bi
.ucs4
)
568 FcChar32
*am
= ai
.leaf
->map
;
569 FcChar32
*bm
= bi
.leaf
->map
;
572 count
+= FcCharSetPopCount (*am
++ & *bm
++);
573 FcCharSetIterNext (a
, &ai
);
575 else if (ai
.ucs4
< bi
.ucs4
)
578 FcCharSetIterSet (a
, &ai
);
580 if (bi
.ucs4
< ai
.ucs4
)
583 FcCharSetIterSet (b
, &bi
);
590 FcCharSetCount (const FcCharSet
*a
)
595 for (FcCharSetIterStart (a
, &ai
); ai
.leaf
; FcCharSetIterNext (a
, &ai
))
598 FcChar32
*am
= ai
.leaf
->map
;
601 count
+= FcCharSetPopCount (*am
++);
607 FcCharSetSubtractCount (const FcCharSet
*a
, const FcCharSet
*b
)
609 FcCharSetIter ai
, bi
;
612 FcCharSetIterStart (a
, &ai
);
613 FcCharSetIterStart (b
, &bi
);
616 if (ai
.ucs4
<= bi
.ucs4
)
618 FcChar32
*am
= ai
.leaf
->map
;
620 if (ai
.ucs4
== bi
.ucs4
)
622 FcChar32
*bm
= bi
.leaf
->map
;;
624 count
+= FcCharSetPopCount (*am
++ & ~*bm
++);
629 count
+= FcCharSetPopCount (*am
++);
631 FcCharSetIterNext (a
, &ai
);
636 FcCharSetIterSet (b
, &bi
);
643 * return FcTrue iff a is a subset of b
646 FcCharSetIsSubset (const FcCharSet
*a
, const FcCharSet
*b
)
651 if (a
== b
) return FcTrue
;
654 while (ai
< a
->num
&& bi
< b
->num
)
656 an
= FcCharSetGetNumbers(a
)[ai
];
657 bn
= FcCharSetGetNumbers(b
)[bi
];
659 * Check matching pages
663 FcChar32
*am
= FcCharSetGetLeaf(a
, ai
)->map
;
664 FcChar32
*bm
= FcCharSetGetLeaf(b
, bi
)->map
;
670 * Does am have any bits not in bm?
680 * Does a have any pages not in b?
687 int high
= b
->num
- 1;
690 * Search for page 'an' in 'b'
694 int mid
= (low
+ high
) >> 1;
695 bn
= FcCharSetGetNumbers(b
)[mid
];
707 while (bi
< b
->num
&& FcCharSetGetNumbers(b
)[bi
] < an
)
712 * did we look at every page?
718 * These two functions efficiently walk the entire charmap for
719 * other software (like pango) that want their own copy
723 FcCharSetNextPage (const FcCharSet
*a
,
724 FcChar32 map
[FC_CHARSET_MAP_SIZE
],
731 FcCharSetIterSet (a
, &ai
);
733 return FC_CHARSET_DONE
;
736 * Save current information
739 memcpy (map
, ai
.leaf
->map
, sizeof (ai
.leaf
->map
));
743 FcCharSetIterNext (a
, &ai
);
750 FcCharSetFirstPage (const FcCharSet
*a
,
751 FcChar32 map
[FC_CHARSET_MAP_SIZE
],
755 return FcCharSetNextPage (a
, map
, next
);
759 * old coverage API, rather hard to use correctly
762 FcCharSetCoverage (const FcCharSet
*a
, FcChar32 page
, FcChar32
*result
);
765 FcCharSetCoverage (const FcCharSet
*a
, FcChar32 page
, FcChar32
*result
)
770 FcCharSetIterSet (a
, &ai
);
773 memset (result
, '\0', 256 / 8);
778 memcpy (result
, ai
.leaf
->map
, sizeof (ai
.leaf
->map
));
779 FcCharSetIterNext (a
, &ai
);
786 * ASCII representation of charsets.
788 * Each leaf is represented as 9 32-bit values, the code of the first character followed
789 * by 8 32 bit values for the leaf itself. Each value is encoded as 5 ASCII characters,
790 * only 85 different values are used to avoid control characters as well as the other
791 * characters used to encode font names. 85**5 > 2^32 so things work out, but
792 * it's not exactly human readable output. As a special case, 0 is encoded as a space
795 static const unsigned char charToValue
[256] = {
796 /* "" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
797 /* "\b" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
798 /* "\020" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
799 /* "\030" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
800 /* " " */ 0xff, 0x00, 0xff, 0x01, 0x02, 0x03, 0x04, 0xff,
801 /* "(" */ 0x05, 0x06, 0x07, 0x08, 0xff, 0xff, 0x09, 0x0a,
802 /* "0" */ 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12,
803 /* "8" */ 0x13, 0x14, 0xff, 0x15, 0x16, 0xff, 0x17, 0x18,
804 /* "@" */ 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
805 /* "H" */ 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
806 /* "P" */ 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30,
807 /* "X" */ 0x31, 0x32, 0x33, 0x34, 0xff, 0x35, 0x36, 0xff,
808 /* "`" */ 0xff, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d,
809 /* "h" */ 0x3e, 0x3f, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45,
810 /* "p" */ 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d,
811 /* "x" */ 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53, 0x54, 0xff,
812 /* "\200" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
813 /* "\210" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
814 /* "\220" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
815 /* "\230" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
816 /* "\240" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
817 /* "\250" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
818 /* "\260" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
819 /* "\270" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
820 /* "\300" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
821 /* "\310" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
822 /* "\320" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
823 /* "\330" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
824 /* "\340" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
825 /* "\350" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
826 /* "\360" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
827 /* "\370" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
830 static const FcChar8 valueToChar
[0x55] = {
831 /* 0x00 */ '!', '#', '$', '%', '&', '(', ')', '*',
832 /* 0x08 */ '+', '.', '/', '0', '1', '2', '3', '4',
833 /* 0x10 */ '5', '6', '7', '8', '9', ';', '<', '>',
834 /* 0x18 */ '?', '@', 'A', 'B', 'C', 'D', 'E', 'F',
835 /* 0x20 */ 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
836 /* 0x28 */ 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
837 /* 0x30 */ 'W', 'X', 'Y', 'Z', '[', ']', '^', 'a',
838 /* 0x38 */ 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
839 /* 0x40 */ 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q',
840 /* 0x48 */ 'r', 's', 't', 'u', 'v', 'w', 'x', 'y',
841 /* 0x50 */ 'z', '{', '|', '}', '~',
845 FcCharSetParseValue (FcChar8
*string
, FcChar32
*value
)
859 for (i
= 0; i
< 5; i
++)
861 if (!(c
= (FcChar32
) (unsigned char) *string
++))
874 FcCharSetUnparseValue (FcStrBuf
*buf
, FcChar32 value
)
879 return FcStrBufChar (buf
, ' ');
884 FcChar8
*s
= string
+ 5;
886 for (i
= 0; i
< 5; i
++)
888 *--s
= valueToChar
[value
% 85];
891 for (i
= 0; i
< 5; i
++)
892 if (!FcStrBufChar (buf
, *s
++))
898 typedef struct _FcCharLeafEnt FcCharLeafEnt
;
900 struct _FcCharLeafEnt
{
906 #define FC_CHAR_LEAF_BLOCK (4096 / sizeof (FcCharLeafEnt))
907 static FcCharLeafEnt
**FcCharLeafBlocks
;
908 static int FcCharLeafBlockCount
;
910 static FcCharLeafEnt
*
911 FcCharLeafEntCreate (void)
913 static FcCharLeafEnt
*block
;
918 FcCharLeafEnt
**newBlocks
;
920 FcCharLeafBlockCount
++;
921 newBlocks
= realloc (FcCharLeafBlocks
, FcCharLeafBlockCount
* sizeof (FcCharLeafEnt
*));
924 FcCharLeafBlocks
= newBlocks
;
925 block
= FcCharLeafBlocks
[FcCharLeafBlockCount
-1] = malloc (FC_CHAR_LEAF_BLOCK
* sizeof (FcCharLeafEnt
));
928 FcMemAlloc (FC_MEM_CHARLEAF
, FC_CHAR_LEAF_BLOCK
* sizeof (FcCharLeafEnt
));
929 remain
= FC_CHAR_LEAF_BLOCK
;
935 #define FC_CHAR_LEAF_HASH_SIZE 257
938 FcCharLeafHash (FcCharLeaf
*leaf
)
943 for (i
= 0; i
< 256/32; i
++)
944 hash
= ((hash
<< 1) | (hash
>> 31)) ^ leaf
->map
[i
];
948 static int FcCharLeafTotal
;
949 static int FcCharLeafUsed
;
951 static FcCharLeafEnt
*FcCharLeafHashTable
[FC_CHAR_LEAF_HASH_SIZE
];
954 FcCharSetFreezeLeaf (FcCharLeaf
*leaf
)
956 FcChar32 hash
= FcCharLeafHash (leaf
);
957 FcCharLeafEnt
**bucket
= &FcCharLeafHashTable
[hash
% FC_CHAR_LEAF_HASH_SIZE
];
961 for (ent
= *bucket
; ent
; ent
= ent
->next
)
963 if (ent
->hash
== hash
&& !memcmp (&ent
->leaf
, leaf
, sizeof (FcCharLeaf
)))
967 ent
= FcCharLeafEntCreate();
979 FcCharSetThawAllLeaf (void)
983 for (i
= 0; i
< FC_CHAR_LEAF_HASH_SIZE
; i
++)
984 FcCharLeafHashTable
[i
] = 0;
989 for (i
= 0; i
< FcCharLeafBlockCount
; i
++)
990 free (FcCharLeafBlocks
[i
]);
992 free (FcCharLeafBlocks
);
993 FcCharLeafBlocks
= 0;
994 FcCharLeafBlockCount
= 0;
997 typedef struct _FcCharSetEnt FcCharSetEnt
;
999 struct _FcCharSetEnt
{
1005 #define FC_CHAR_SET_HASH_SIZE 67
1008 FcCharSetHash (FcCharSet
*fcs
)
1013 /* hash in leaves */
1014 for (i
= 0; i
< fcs
->num
* (int) (sizeof (FcCharLeaf
*) / sizeof (FcChar32
)); i
++)
1015 hash
= ((hash
<< 1) | (hash
>> 31)) ^ (FcChar32
)(FcCharSetGetLeaf(fcs
, i
)->map
);
1016 /* hash in numbers */
1017 for (i
= 0; i
< fcs
->num
; i
++)
1018 hash
= ((hash
<< 1) | (hash
>> 31)) ^ *FcCharSetGetNumbers(fcs
);
1022 static int FcCharSetTotal
;
1023 static int FcCharSetUsed
;
1024 static int FcCharSetTotalEnts
, FcCharSetUsedEnts
;
1026 static FcCharSetEnt
*FcCharSetHashTable
[FC_CHAR_SET_HASH_SIZE
];
1029 FcCharSetFreezeBase (FcCharSet
*fcs
)
1031 FcChar32 hash
= FcCharSetHash (fcs
);
1032 FcCharSetEnt
**bucket
= &FcCharSetHashTable
[hash
% FC_CHAR_SET_HASH_SIZE
];
1037 FcCharSetTotalEnts
+= fcs
->num
;
1038 for (ent
= *bucket
; ent
; ent
= ent
->next
)
1040 if (ent
->hash
== hash
&&
1041 ent
->set
.num
== fcs
->num
&&
1042 !memcmp (FcCharSetGetNumbers(&ent
->set
),
1043 FcCharSetGetNumbers(fcs
),
1044 fcs
->num
* sizeof (FcChar16
)))
1049 for (i
= 0; i
< fcs
->num
; i
++)
1050 if (FcCharSetGetLeaf(&ent
->set
, i
) != FcCharSetGetLeaf(fcs
, i
))
1057 size
= (sizeof (FcCharSetEnt
) +
1058 fcs
->num
* sizeof (FcCharLeaf
*) +
1059 fcs
->num
* sizeof (FcChar16
));
1060 ent
= malloc (size
);
1063 FcMemAlloc (FC_MEM_CHARSET
, size
);
1065 FcCharSetUsedEnts
+= fcs
->num
;
1067 ent
->set
.ref
= FC_REF_CONSTANT
;
1068 ent
->set
.num
= fcs
->num
;
1069 ent
->set
.bank
= fcs
->bank
;
1070 if (fcs
->bank
== FC_BANK_DYNAMIC
)
1074 ent
->set
.u
.dyn
.leaves
= (FcCharLeaf
**) (ent
+ 1);
1075 ent
->set
.u
.dyn
.numbers
= (FcChar16
*) (ent
->set
.u
.dyn
.leaves
+ fcs
->num
);
1076 memcpy (ent
->set
.u
.dyn
.leaves
, fcs
->u
.dyn
.leaves
, fcs
->num
* sizeof (FcCharLeaf
*));
1077 memcpy (ent
->set
.u
.dyn
.numbers
, fcs
->u
.dyn
.numbers
, fcs
->num
* sizeof (FcChar16
));
1081 ent
->set
.u
.dyn
.leaves
= 0;
1082 ent
->set
.u
.dyn
.numbers
= 0;
1087 ent
->set
.u
.stat
.leafidx_offset
= fcs
->u
.stat
.leafidx_offset
;
1088 ent
->set
.u
.stat
.numbers_offset
= fcs
->u
.stat
.numbers_offset
;
1092 ent
->next
= *bucket
;
1098 FcCharSetThawAll (void)
1101 FcCharSetEnt
*ent
, *next
;
1103 for (i
= 0; i
< FC_CHAR_SET_HASH_SIZE
; i
++)
1105 for (ent
= FcCharSetHashTable
[i
]; ent
; ent
= next
)
1110 FcCharSetHashTable
[i
] = 0;
1114 FcCharSetTotalEnts
= 0;
1116 FcCharSetUsedEnts
= 0;
1118 FcCharSetThawAllLeaf ();
1122 FcCharSetFreeze (FcCharSet
*fcs
)
1129 b
= FcCharSetCreate ();
1132 for (i
= 0; i
< fcs
->num
; i
++)
1134 l
= FcCharSetFreezeLeaf (FcCharSetGetLeaf(fcs
, i
));
1137 if (!FcCharSetInsertLeaf (b
, FcCharSetGetNumbers(fcs
)[i
] << 8, l
))
1140 n
= FcCharSetFreezeBase (b
);
1142 if (b
->bank
== FC_BANK_DYNAMIC
)
1144 if (b
->u
.dyn
.leaves
)
1146 FcMemFree (FC_MEM_CHARSET
, b
->num
* sizeof (FcCharLeaf
*));
1147 free (b
->u
.dyn
.leaves
);
1149 if (b
->u
.dyn
.numbers
)
1151 FcMemFree (FC_MEM_CHARSET
, b
->num
* sizeof (FcChar16
));
1152 free (b
->u
.dyn
.numbers
);
1155 FcMemFree (FC_MEM_CHARSET
, sizeof (FcCharSet
));
1162 FcNameParseCharSet (FcChar8
*string
)
1164 FcCharSet
*c
, *n
= 0;
1171 c
= FcCharSetCreate ();
1176 string
= FcCharSetParseValue (string
, &ucs4
);
1180 for (i
= 0; i
< 256/32; i
++)
1182 string
= FcCharSetParseValue (string
, &temp
.map
[i
]);
1185 bits
|= temp
.map
[i
];
1189 leaf
= FcCharSetFreezeLeaf (&temp
);
1192 if (!FcCharSetInsertLeaf (c
, ucs4
, leaf
))
1197 printf (" %8s %8s %8s %8s\n", "total", "totalmem", "new", "newmem");
1198 printf ("Leaves: %8d %8d %8d %8d\n",
1199 FcCharLeafTotal
, sizeof (FcCharLeaf
) * FcCharLeafTotal
,
1200 FcCharLeafUsed
, sizeof (FcCharLeaf
) * FcCharLeafUsed
);
1201 printf ("Charsets: %8d %8d %8d %8d\n",
1202 FcCharSetTotal
, sizeof (FcCharSet
) * FcCharSetTotal
,
1203 FcCharSetUsed
, sizeof (FcCharSet
) * FcCharSetUsed
);
1204 printf ("Tables: %8d %8d %8d %8d\n",
1205 FcCharSetTotalEnts
, FcCharSetTotalEnts
* (sizeof (FcCharLeaf
*) + sizeof (FcChar16
)),
1206 FcCharSetUsedEnts
, FcCharSetUsedEnts
* (sizeof (FcCharLeaf
*) + sizeof (FcChar16
)));
1207 printf ("Total: %8s %8d %8s %8d\n",
1209 sizeof (FcCharLeaf
) * FcCharLeafTotal
+
1210 sizeof (FcCharSet
) * FcCharSetTotal
+
1211 FcCharSetTotalEnts
* (sizeof (FcCharLeaf
*) + sizeof (FcChar16
)),
1213 sizeof (FcCharLeaf
) * FcCharLeafUsed
+
1214 sizeof (FcCharSet
) * FcCharSetUsed
+
1215 FcCharSetUsedEnts
* (sizeof (FcCharLeaf
*) + sizeof (FcChar16
)));
1217 n
= FcCharSetFreezeBase (c
);
1219 if (c
->bank
== FC_BANK_DYNAMIC
)
1221 if (c
->u
.dyn
.leaves
)
1223 FcMemFree (FC_MEM_CHARSET
, c
->num
* sizeof (FcCharLeaf
*));
1224 free (c
->u
.dyn
.leaves
);
1226 if (c
->u
.dyn
.numbers
)
1228 FcMemFree (FC_MEM_CHARSET
, c
->num
* sizeof (FcChar16
));
1229 free (c
->u
.dyn
.numbers
);
1231 FcMemFree (FC_MEM_CHARSET
, sizeof (FcCharSet
));
1239 FcNameUnparseCharSet (FcStrBuf
*buf
, const FcCharSet
*c
)
1247 for (FcCharSetIterStart (c
, &ci
);
1249 FcCharSetIterNext (c
, &ci
))
1251 if (!FcCharSetUnparseValue (buf
, ci
.ucs4
))
1253 for (i
= 0; i
< 256/32; i
++)
1254 if (!FcCharSetUnparseValue (buf
, ci
.leaf
->map
[i
]))
1261 FcCharSetIter ci
, checki
;
1263 /* null terminate for parser */
1264 FcStrBufChar (buf
, '\0');
1265 /* step back over null for life after test */
1267 check
= FcNameParseCharSet (buf
->buf
+ len
);
1268 FcCharSetIterStart (c
, &ci
);
1269 FcCharSetIterStart (check
, &checki
);
1270 while (ci
.leaf
|| checki
.leaf
)
1272 if (ci
.ucs4
< checki
.ucs4
)
1274 printf ("Missing leaf node at 0x%x\n", ci
.ucs4
);
1275 FcCharSetIterNext (c
, &ci
);
1277 else if (checki
.ucs4
< ci
.ucs4
)
1279 printf ("Extra leaf node at 0x%x\n", checki
.ucs4
);
1280 FcCharSetIterNext (check
, &checki
);
1285 FcChar32
*cm
= ci
.leaf
->map
;
1286 FcChar32
*checkm
= checki
.leaf
->map
;
1288 for (i
= 0; i
< 256; i
+= 32)
1291 printf ("Mismatching sets at 0x%08x: 0x%08x != 0x%08x\n",
1292 ci
.ucs4
+ i
, *cm
, *checkm
);
1296 FcCharSetIterNext (c
, &ci
);
1297 FcCharSetIterNext (check
, &checki
);
1300 if ((missing
= FcCharSetSubtractCount (c
, check
)))
1301 printf ("%d missing in reparsed result\n", missing
);
1302 if ((missing
= FcCharSetSubtractCount (check
, c
)))
1303 printf ("%d extra in reparsed result\n", missing
);
1304 FcCharSetDestroy (check
);
1312 FcCharSetNewBank(void)
1315 charset_numbers_count
= 0;
1316 charset_leaf_count
= 0;
1317 charset_leaf_idx_count
= 0;
1321 FcCharSetNeededBytes (const FcCharSet
*c
)
1323 /* yes, there's redundancy */
1325 charset_leaf_idx_count
++;
1326 charset_leaf_count
+= c
->num
;
1327 charset_numbers_count
+= c
->num
;
1328 return sizeof (FcCharSet
) +
1329 sizeof (int) + /* leaf_idx */
1330 sizeof (FcCharLeaf
) * c
->num
+ /* leaf */
1331 sizeof (FcChar16
) * c
->num
; /* number */
1335 FcCharSetNeededBytesAlign (void)
1337 return __alignof__ (FcCharSet
) + __alignof__ (int) +
1338 __alignof__ (FcCharLeaf
) + __alignof__ (FcChar16
);
1342 FcCharSetEnsureBank (int bi
)
1344 if (!charsets
|| charset_bank_count
<= bi
)
1346 int new_count
= charset_bank_count
+ 2;
1353 cs
= realloc(charsets
, sizeof(FcCharSet
*) * new_count
);
1355 n
= realloc(numbers
, sizeof(FcChar16
*) * new_count
);
1357 lvs
= realloc(leaves
, sizeof(FcCharLeaf
*) * new_count
);
1359 lvi
= realloc(leaf_idx
, sizeof(int*) * new_count
);
1362 charsets
= cs
; numbers
= n
; leaves
= lvs
; leaf_idx
= lvi
;
1363 for (i
= charset_bank_count
; i
< new_count
; i
++)
1370 charset_bank_count
= new_count
;
1376 FcCharSetDistributeBytes (FcCache
* metadata
, void * block_ptr
)
1378 int bi
= FcCacheBankToIndex(metadata
->bank
);
1379 if (!FcCharSetEnsureBank(bi
))
1382 block_ptr
= ALIGN (block_ptr
, FcCharSet
);
1383 charsets
[bi
] = (FcCharSet
*)block_ptr
;
1384 block_ptr
= (void *)((char *)block_ptr
+
1385 (sizeof (FcCharSet
) * charset_count
));
1386 block_ptr
= ALIGN (block_ptr
, FcChar16
);
1387 numbers
[bi
] = (FcChar16
*)block_ptr
;
1388 block_ptr
= (void *)((char *)block_ptr
+
1389 (sizeof(FcChar16
) * charset_numbers_count
));
1390 block_ptr
= ALIGN (block_ptr
, FcCharLeaf
);
1391 leaves
[bi
] = (FcCharLeaf
*)block_ptr
;
1392 block_ptr
= (void *)((char *)block_ptr
+
1393 (sizeof(FcCharLeaf
) * charset_leaf_count
));
1394 block_ptr
= ALIGN (block_ptr
, int);
1395 leaf_idx
[bi
] = (int *)block_ptr
;
1396 block_ptr
= (void *)((char *)block_ptr
+
1397 (sizeof(int) * charset_leaf_idx_count
));
1399 metadata
->charset_count
= charset_count
;
1400 metadata
->charset_numbers_count
= charset_numbers_count
;
1401 metadata
->charset_leaf_count
= charset_leaf_count
;
1402 metadata
->charset_leaf_idx_count
= charset_leaf_idx_count
;
1403 charset_ptr
= 0; charset_leaf_ptr
= 0;
1404 charset_leaf_idx_ptr
= 0; charset_numbers_ptr
= 0;
1409 FcCharSetSerialize(int bank
, FcCharSet
*c
)
1413 int bi
= FcCacheBankToIndex(bank
), cp
= charset_ptr
;
1415 new.ref
= FC_REF_CONSTANT
;
1417 new.u
.stat
.leafidx_offset
= charset_leaf_idx_ptr
;
1418 new.u
.stat
.numbers_offset
= charset_numbers_ptr
;
1421 charsets
[bi
][charset_ptr
++] = new;
1423 leaf_idx
[bi
][charset_leaf_idx_ptr
++] = charset_leaf_ptr
;
1424 for (i
= 0; i
< c
->num
; i
++)
1426 memcpy (&leaves
[bi
][charset_leaf_ptr
++],
1427 c
->u
.dyn
.leaves
[i
], sizeof(FcCharLeaf
));
1428 numbers
[bi
][charset_numbers_ptr
++] = c
->u
.dyn
.numbers
[i
];
1431 return &charsets
[bi
][cp
];
1435 FcCharSetUnserialize (FcCache
*metadata
, void *block_ptr
)
1437 int bi
= FcCacheBankToIndex(metadata
->bank
);
1438 if (!FcCharSetEnsureBank(bi
))
1441 block_ptr
= ALIGN (block_ptr
, FcCharSet
);
1442 charsets
[bi
] = (FcCharSet
*)block_ptr
;
1443 block_ptr
= (void *)((char *)block_ptr
+
1444 (sizeof (FcCharSet
) * metadata
->charset_count
));
1445 block_ptr
= ALIGN (block_ptr
, FcChar16
);
1446 numbers
[bi
] = (FcChar16
*)block_ptr
;
1447 block_ptr
= (void *)((char *)block_ptr
+
1448 (sizeof(FcChar16
) * metadata
->charset_numbers_count
));
1449 block_ptr
= ALIGN (block_ptr
, FcCharLeaf
);
1450 leaves
[bi
] = (FcCharLeaf
*)block_ptr
;
1451 block_ptr
= (void *)((char *)block_ptr
+
1452 (sizeof(FcCharLeaf
) * metadata
->charset_leaf_count
));
1453 block_ptr
= ALIGN (block_ptr
, int);
1454 leaf_idx
[bi
] = (int *)block_ptr
;
1455 block_ptr
= (void *)((char *)block_ptr
+
1456 (sizeof(int) * metadata
->charset_leaf_idx_count
));
1462 FcCharSetGetLeaf(const FcCharSet
*c
, int i
)
1465 if (c
->bank
== FC_BANK_DYNAMIC
)
1466 return c
->u
.dyn
.leaves
[i
];
1467 bi
= FcCacheBankToIndex(c
->bank
);
1469 return &leaves
[bi
][leaf_idx
[bi
][c
->u
.stat
.leafidx_offset
]+i
];
1473 FcCharSetGetNumbers(const FcCharSet
*c
)
1476 if (c
->bank
== FC_BANK_DYNAMIC
)
1477 return c
->u
.dyn
.numbers
;
1478 bi
= FcCacheBankToIndex(c
->bank
);
1480 return &numbers
[bi
][c
->u
.stat
.numbers_offset
];