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
)
350 FcCharSetIterSet (fcs
, iter
);
354 FcCharSetCopy (FcCharSet
*src
)
356 if (src
->ref
!= FC_REF_CONSTANT
)
362 FcCharSetEqual (const FcCharSet
*a
, const FcCharSet
*b
)
364 FcCharSetIter ai
, bi
;
369 for (FcCharSetIterStart (a
, &ai
), FcCharSetIterStart (b
, &bi
);
371 FcCharSetIterNext (a
, &ai
), FcCharSetIterNext (b
, &bi
))
373 if (ai
.ucs4
!= bi
.ucs4
)
375 for (i
= 0; i
< 256/32; i
++)
376 if (ai
.leaf
->map
[i
] != bi
.leaf
->map
[i
])
379 return ai
.leaf
== bi
.leaf
;
383 FcCharSetAddLeaf (FcCharSet
*fcs
,
387 FcCharLeaf
*new = FcCharSetFindLeafCreate (fcs
, ucs4
);
395 FcCharSetOperate (const FcCharSet
*a
,
397 FcBool (*overlap
) (FcCharLeaf
*result
,
398 const FcCharLeaf
*al
,
399 const FcCharLeaf
*bl
),
404 FcCharSetIter ai
, bi
;
406 fcs
= FcCharSetCreate ();
409 FcCharSetIterStart (a
, &ai
);
410 FcCharSetIterStart (b
, &bi
);
411 while ((ai
.leaf
|| (bonly
&& bi
.leaf
)) && (bi
.leaf
|| (aonly
&& ai
.leaf
)))
413 if (ai
.ucs4
< bi
.ucs4
)
417 if (!FcCharSetAddLeaf (fcs
, ai
.ucs4
, ai
.leaf
))
419 FcCharSetIterNext (a
, &ai
);
424 FcCharSetIterSet (a
, &ai
);
427 else if (bi
.ucs4
< ai
.ucs4
)
431 if (!FcCharSetAddLeaf (fcs
, bi
.ucs4
, bi
.leaf
))
433 FcCharSetIterNext (b
, &bi
);
438 FcCharSetIterSet (b
, &bi
);
445 if ((*overlap
) (&leaf
, ai
.leaf
, bi
.leaf
))
447 if (!FcCharSetAddLeaf (fcs
, ai
.ucs4
, &leaf
))
450 FcCharSetIterNext (a
, &ai
);
451 FcCharSetIterNext (b
, &bi
);
456 FcCharSetDestroy (fcs
);
462 FcCharSetIntersectLeaf (FcCharLeaf
*result
,
463 const FcCharLeaf
*al
,
464 const FcCharLeaf
*bl
)
467 FcBool nonempty
= FcFalse
;
469 for (i
= 0; i
< 256/32; i
++)
470 if ((result
->map
[i
] = al
->map
[i
] & bl
->map
[i
]))
476 FcCharSetIntersect (const FcCharSet
*a
, const FcCharSet
*b
)
478 return FcCharSetOperate (a
, b
, FcCharSetIntersectLeaf
, FcFalse
, FcFalse
);
482 FcCharSetUnionLeaf (FcCharLeaf
*result
,
483 const FcCharLeaf
*al
,
484 const FcCharLeaf
*bl
)
488 for (i
= 0; i
< 256/32; i
++)
489 result
->map
[i
] = al
->map
[i
] | bl
->map
[i
];
494 FcCharSetUnion (const FcCharSet
*a
, const FcCharSet
*b
)
496 return FcCharSetOperate (a
, b
, FcCharSetUnionLeaf
, FcTrue
, FcTrue
);
500 FcCharSetSubtractLeaf (FcCharLeaf
*result
,
501 const FcCharLeaf
*al
,
502 const FcCharLeaf
*bl
)
505 FcBool nonempty
= FcFalse
;
507 for (i
= 0; i
< 256/32; i
++)
508 if ((result
->map
[i
] = al
->map
[i
] & ~bl
->map
[i
]))
514 FcCharSetSubtract (const FcCharSet
*a
, const FcCharSet
*b
)
516 return FcCharSetOperate (a
, b
, FcCharSetSubtractLeaf
, FcTrue
, FcFalse
);
520 FcCharSetHasChar (const FcCharSet
*fcs
, FcChar32 ucs4
)
522 FcCharLeaf
*leaf
= FcCharSetFindLeaf (fcs
, ucs4
);
525 return (leaf
->map
[(ucs4
& 0xff) >> 5] & (1 << (ucs4
& 0x1f))) != 0;
529 FcCharSetPopCount (FcChar32 c1
)
532 FcChar32 c2
= (c1
>> 1) & 033333333333;
533 c2
= c1
- c2
- ((c2
>> 1) & 033333333333);
534 return (((c2
+ (c2
>> 3)) & 030707070707) % 077);
538 FcCharSetIntersectCount (const FcCharSet
*a
, const FcCharSet
*b
)
540 FcCharSetIter ai
, bi
;
543 FcCharSetIterStart (a
, &ai
);
544 FcCharSetIterStart (b
, &bi
);
545 while (ai
.leaf
&& bi
.leaf
)
547 if (ai
.ucs4
== bi
.ucs4
)
549 FcChar32
*am
= ai
.leaf
->map
;
550 FcChar32
*bm
= bi
.leaf
->map
;
553 count
+= FcCharSetPopCount (*am
++ & *bm
++);
554 FcCharSetIterNext (a
, &ai
);
556 else if (ai
.ucs4
< bi
.ucs4
)
559 FcCharSetIterSet (a
, &ai
);
561 if (bi
.ucs4
< ai
.ucs4
)
564 FcCharSetIterSet (b
, &bi
);
571 FcCharSetCount (const FcCharSet
*a
)
576 for (FcCharSetIterStart (a
, &ai
); ai
.leaf
; FcCharSetIterNext (a
, &ai
))
579 FcChar32
*am
= ai
.leaf
->map
;
582 count
+= FcCharSetPopCount (*am
++);
588 FcCharSetSubtractCount (const FcCharSet
*a
, const FcCharSet
*b
)
590 FcCharSetIter ai
, bi
;
593 FcCharSetIterStart (a
, &ai
);
594 FcCharSetIterStart (b
, &bi
);
597 if (ai
.ucs4
<= bi
.ucs4
)
599 FcChar32
*am
= ai
.leaf
->map
;
601 if (ai
.ucs4
== bi
.ucs4
)
603 FcChar32
*bm
= bi
.leaf
->map
;;
605 count
+= FcCharSetPopCount (*am
++ & ~*bm
++);
610 count
+= FcCharSetPopCount (*am
++);
612 FcCharSetIterNext (a
, &ai
);
617 FcCharSetIterSet (b
, &bi
);
624 * return FcTrue iff a is a subset of b
627 FcCharSetIsSubset (const FcCharSet
*a
, const FcCharSet
*b
)
632 if (a
== b
) return FcTrue
;
635 while (ai
< a
->num
&& bi
< b
->num
)
637 an
= FcCharSetGetNumbers(a
)[ai
];
638 bn
= FcCharSetGetNumbers(b
)[bi
];
640 * Check matching pages
644 FcChar32
*am
= FcCharSetGetLeaf(a
, ai
)->map
;
645 FcChar32
*bm
= FcCharSetGetLeaf(b
, bi
)->map
;
651 * Does am have any bits not in bm?
661 * Does a have any pages not in b?
668 int high
= b
->num
- 1;
671 * Search for page 'an' in 'b'
675 int mid
= (low
+ high
) >> 1;
676 bn
= FcCharSetGetNumbers(b
)[mid
];
688 while (bi
< b
->num
&& FcCharSetGetNumbers(b
)[bi
] < an
)
693 * did we look at every page?
699 * These two functions efficiently walk the entire charmap for
700 * other software (like pango) that want their own copy
704 FcCharSetNextPage (const FcCharSet
*a
,
705 FcChar32 map
[FC_CHARSET_MAP_SIZE
],
712 FcCharSetIterSet (a
, &ai
);
714 return FC_CHARSET_DONE
;
717 * Save current information
720 memcpy (map
, ai
.leaf
->map
, sizeof (ai
.leaf
->map
));
724 FcCharSetIterNext (a
, &ai
);
731 FcCharSetFirstPage (const FcCharSet
*a
,
732 FcChar32 map
[FC_CHARSET_MAP_SIZE
],
736 return FcCharSetNextPage (a
, map
, next
);
740 * old coverage API, rather hard to use correctly
743 FcCharSetCoverage (const FcCharSet
*a
, FcChar32 page
, FcChar32
*result
);
746 FcCharSetCoverage (const FcCharSet
*a
, FcChar32 page
, FcChar32
*result
)
751 FcCharSetIterSet (a
, &ai
);
754 memset (result
, '\0', 256 / 8);
759 memcpy (result
, ai
.leaf
->map
, sizeof (ai
.leaf
->map
));
760 FcCharSetIterNext (a
, &ai
);
767 * ASCII representation of charsets.
769 * Each leaf is represented as 9 32-bit values, the code of the first character followed
770 * by 8 32 bit values for the leaf itself. Each value is encoded as 5 ASCII characters,
771 * only 85 different values are used to avoid control characters as well as the other
772 * characters used to encode font names. 85**5 > 2^32 so things work out, but
773 * it's not exactly human readable output. As a special case, 0 is encoded as a space
776 static const unsigned char charToValue
[256] = {
777 /* "" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
778 /* "\b" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
779 /* "\020" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
780 /* "\030" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
781 /* " " */ 0xff, 0x00, 0xff, 0x01, 0x02, 0x03, 0x04, 0xff,
782 /* "(" */ 0x05, 0x06, 0x07, 0x08, 0xff, 0xff, 0x09, 0x0a,
783 /* "0" */ 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12,
784 /* "8" */ 0x13, 0x14, 0xff, 0x15, 0x16, 0xff, 0x17, 0x18,
785 /* "@" */ 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
786 /* "H" */ 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
787 /* "P" */ 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30,
788 /* "X" */ 0x31, 0x32, 0x33, 0x34, 0xff, 0x35, 0x36, 0xff,
789 /* "`" */ 0xff, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d,
790 /* "h" */ 0x3e, 0x3f, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45,
791 /* "p" */ 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d,
792 /* "x" */ 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53, 0x54, 0xff,
793 /* "\200" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
794 /* "\210" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
795 /* "\220" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
796 /* "\230" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
797 /* "\240" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
798 /* "\250" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
799 /* "\260" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
800 /* "\270" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
801 /* "\300" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
802 /* "\310" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
803 /* "\320" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
804 /* "\330" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
805 /* "\340" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
806 /* "\350" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
807 /* "\360" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
808 /* "\370" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
811 static const FcChar8 valueToChar
[0x55] = {
812 /* 0x00 */ '!', '#', '$', '%', '&', '(', ')', '*',
813 /* 0x08 */ '+', '.', '/', '0', '1', '2', '3', '4',
814 /* 0x10 */ '5', '6', '7', '8', '9', ';', '<', '>',
815 /* 0x18 */ '?', '@', 'A', 'B', 'C', 'D', 'E', 'F',
816 /* 0x20 */ 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
817 /* 0x28 */ 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
818 /* 0x30 */ 'W', 'X', 'Y', 'Z', '[', ']', '^', 'a',
819 /* 0x38 */ 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
820 /* 0x40 */ 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q',
821 /* 0x48 */ 'r', 's', 't', 'u', 'v', 'w', 'x', 'y',
822 /* 0x50 */ 'z', '{', '|', '}', '~',
826 FcCharSetParseValue (FcChar8
*string
, FcChar32
*value
)
840 for (i
= 0; i
< 5; i
++)
842 if (!(c
= (FcChar32
) (unsigned char) *string
++))
855 FcCharSetUnparseValue (FcStrBuf
*buf
, FcChar32 value
)
860 return FcStrBufChar (buf
, ' ');
865 FcChar8
*s
= string
+ 5;
867 for (i
= 0; i
< 5; i
++)
869 *--s
= valueToChar
[value
% 85];
872 for (i
= 0; i
< 5; i
++)
873 if (!FcStrBufChar (buf
, *s
++))
879 typedef struct _FcCharLeafEnt FcCharLeafEnt
;
881 struct _FcCharLeafEnt
{
887 #define FC_CHAR_LEAF_BLOCK (4096 / sizeof (FcCharLeafEnt))
888 static FcCharLeafEnt
**FcCharLeafBlocks
;
889 static int FcCharLeafBlockCount
;
891 static FcCharLeafEnt
*
892 FcCharLeafEntCreate (void)
894 static FcCharLeafEnt
*block
;
899 FcCharLeafEnt
**newBlocks
;
901 FcCharLeafBlockCount
++;
902 newBlocks
= realloc (FcCharLeafBlocks
, FcCharLeafBlockCount
* sizeof (FcCharLeafEnt
*));
905 FcCharLeafBlocks
= newBlocks
;
906 block
= FcCharLeafBlocks
[FcCharLeafBlockCount
-1] = malloc (FC_CHAR_LEAF_BLOCK
* sizeof (FcCharLeafEnt
));
909 FcMemAlloc (FC_MEM_CHARLEAF
, FC_CHAR_LEAF_BLOCK
* sizeof (FcCharLeafEnt
));
910 remain
= FC_CHAR_LEAF_BLOCK
;
916 #define FC_CHAR_LEAF_HASH_SIZE 257
919 FcCharLeafHash (FcCharLeaf
*leaf
)
924 for (i
= 0; i
< 256/32; i
++)
925 hash
= ((hash
<< 1) | (hash
>> 31)) ^ leaf
->map
[i
];
929 static int FcCharLeafTotal
;
930 static int FcCharLeafUsed
;
932 static FcCharLeafEnt
*FcCharLeafHashTable
[FC_CHAR_LEAF_HASH_SIZE
];
935 FcCharSetFreezeLeaf (FcCharLeaf
*leaf
)
937 FcChar32 hash
= FcCharLeafHash (leaf
);
938 FcCharLeafEnt
**bucket
= &FcCharLeafHashTable
[hash
% FC_CHAR_LEAF_HASH_SIZE
];
942 for (ent
= *bucket
; ent
; ent
= ent
->next
)
944 if (ent
->hash
== hash
&& !memcmp (&ent
->leaf
, leaf
, sizeof (FcCharLeaf
)))
948 ent
= FcCharLeafEntCreate();
960 FcCharSetThawAllLeaf (void)
964 for (i
= 0; i
< FC_CHAR_LEAF_HASH_SIZE
; i
++)
965 FcCharLeafHashTable
[i
] = 0;
970 for (i
= 0; i
< FcCharLeafBlockCount
; i
++)
971 free (FcCharLeafBlocks
[i
]);
973 free (FcCharLeafBlocks
);
974 FcCharLeafBlocks
= 0;
975 FcCharLeafBlockCount
= 0;
978 typedef struct _FcCharSetEnt FcCharSetEnt
;
980 struct _FcCharSetEnt
{
986 #define FC_CHAR_SET_HASH_SIZE 67
989 FcCharSetHash (FcCharSet
*fcs
)
995 for (i
= 0; i
< fcs
->num
* sizeof (FcCharLeaf
*) / sizeof (FcChar32
); i
++)
996 hash
= ((hash
<< 1) | (hash
>> 31)) ^ (FcChar32
)(FcCharSetGetLeaf(fcs
, i
)->map
);
997 /* hash in numbers */
998 for (i
= 0; i
< fcs
->num
; i
++)
999 hash
= ((hash
<< 1) | (hash
>> 31)) ^ *FcCharSetGetNumbers(fcs
);
1003 static int FcCharSetTotal
;
1004 static int FcCharSetUsed
;
1005 static int FcCharSetTotalEnts
, FcCharSetUsedEnts
;
1007 static FcCharSetEnt
*FcCharSetHashTable
[FC_CHAR_SET_HASH_SIZE
];
1010 FcCharSetFreezeBase (FcCharSet
*fcs
)
1012 FcChar32 hash
= FcCharSetHash (fcs
);
1013 FcCharSetEnt
**bucket
= &FcCharSetHashTable
[hash
% FC_CHAR_SET_HASH_SIZE
];
1018 FcCharSetTotalEnts
+= fcs
->num
;
1019 for (ent
= *bucket
; ent
; ent
= ent
->next
)
1021 if (ent
->hash
== hash
&&
1022 ent
->set
.num
== fcs
->num
&&
1023 !memcmp (FcCharSetGetNumbers(&ent
->set
),
1024 FcCharSetGetNumbers(fcs
),
1025 fcs
->num
* sizeof (FcChar16
)))
1030 for (i
= 0; i
< fcs
->num
; i
++)
1031 if (FcCharSetGetLeaf(&ent
->set
, i
) != FcCharSetGetLeaf(fcs
, i
))
1038 size
= (sizeof (FcCharSetEnt
) +
1039 fcs
->num
* sizeof (FcCharLeaf
*) +
1040 fcs
->num
* sizeof (FcChar16
));
1041 ent
= malloc (size
);
1044 FcMemAlloc (FC_MEM_CHARSET
, size
);
1046 FcCharSetUsedEnts
+= fcs
->num
;
1048 ent
->set
.ref
= FC_REF_CONSTANT
;
1049 ent
->set
.num
= fcs
->num
;
1050 ent
->set
.bank
= fcs
->bank
;
1051 if (fcs
->bank
== FC_BANK_DYNAMIC
)
1055 ent
->set
.u
.dyn
.leaves
= (FcCharLeaf
**) (ent
+ 1);
1056 ent
->set
.u
.dyn
.numbers
= (FcChar16
*) (ent
->set
.u
.dyn
.leaves
+ fcs
->num
);
1057 memcpy (ent
->set
.u
.dyn
.leaves
, fcs
->u
.dyn
.leaves
, fcs
->num
* sizeof (FcCharLeaf
*));
1058 memcpy (ent
->set
.u
.dyn
.numbers
, fcs
->u
.dyn
.numbers
, fcs
->num
* sizeof (FcChar16
));
1062 ent
->set
.u
.dyn
.leaves
= 0;
1063 ent
->set
.u
.dyn
.numbers
= 0;
1068 ent
->set
.u
.stat
.leafidx_offset
= fcs
->u
.stat
.leafidx_offset
;
1069 ent
->set
.u
.stat
.numbers_offset
= fcs
->u
.stat
.numbers_offset
;
1073 ent
->next
= *bucket
;
1079 FcCharSetThawAll (void)
1082 FcCharSetEnt
*ent
, *next
;
1084 for (i
= 0; i
< FC_CHAR_SET_HASH_SIZE
; i
++)
1086 for (ent
= FcCharSetHashTable
[i
]; ent
; ent
= next
)
1091 FcCharSetHashTable
[i
] = 0;
1095 FcCharSetTotalEnts
= 0;
1097 FcCharSetUsedEnts
= 0;
1099 FcCharSetThawAllLeaf ();
1103 FcCharSetFreeze (FcCharSet
*fcs
)
1110 b
= FcCharSetCreate ();
1113 for (i
= 0; i
< fcs
->num
; i
++)
1115 l
= FcCharSetFreezeLeaf (FcCharSetGetLeaf(fcs
, i
));
1118 if (!FcCharSetInsertLeaf (b
, FcCharSetGetNumbers(fcs
)[i
] << 8, l
))
1121 n
= FcCharSetFreezeBase (b
);
1123 if (b
->bank
== FC_BANK_DYNAMIC
)
1125 if (b
->u
.dyn
.leaves
)
1127 FcMemFree (FC_MEM_CHARSET
, b
->num
* sizeof (FcCharLeaf
*));
1128 free (b
->u
.dyn
.leaves
);
1130 if (b
->u
.dyn
.numbers
)
1132 FcMemFree (FC_MEM_CHARSET
, b
->num
* sizeof (FcChar16
));
1133 free (b
->u
.dyn
.numbers
);
1136 FcMemFree (FC_MEM_CHARSET
, sizeof (FcCharSet
));
1143 FcNameParseCharSet (FcChar8
*string
)
1145 FcCharSet
*c
, *n
= 0;
1152 c
= FcCharSetCreate ();
1157 string
= FcCharSetParseValue (string
, &ucs4
);
1161 for (i
= 0; i
< 256/32; i
++)
1163 string
= FcCharSetParseValue (string
, &temp
.map
[i
]);
1166 bits
|= temp
.map
[i
];
1170 leaf
= FcCharSetFreezeLeaf (&temp
);
1173 if (!FcCharSetInsertLeaf (c
, ucs4
, leaf
))
1178 printf (" %8s %8s %8s %8s\n", "total", "totalmem", "new", "newmem");
1179 printf ("Leaves: %8d %8d %8d %8d\n",
1180 FcCharLeafTotal
, sizeof (FcCharLeaf
) * FcCharLeafTotal
,
1181 FcCharLeafUsed
, sizeof (FcCharLeaf
) * FcCharLeafUsed
);
1182 printf ("Charsets: %8d %8d %8d %8d\n",
1183 FcCharSetTotal
, sizeof (FcCharSet
) * FcCharSetTotal
,
1184 FcCharSetUsed
, sizeof (FcCharSet
) * FcCharSetUsed
);
1185 printf ("Tables: %8d %8d %8d %8d\n",
1186 FcCharSetTotalEnts
, FcCharSetTotalEnts
* (sizeof (FcCharLeaf
*) + sizeof (FcChar16
)),
1187 FcCharSetUsedEnts
, FcCharSetUsedEnts
* (sizeof (FcCharLeaf
*) + sizeof (FcChar16
)));
1188 printf ("Total: %8s %8d %8s %8d\n",
1190 sizeof (FcCharLeaf
) * FcCharLeafTotal
+
1191 sizeof (FcCharSet
) * FcCharSetTotal
+
1192 FcCharSetTotalEnts
* (sizeof (FcCharLeaf
*) + sizeof (FcChar16
)),
1194 sizeof (FcCharLeaf
) * FcCharLeafUsed
+
1195 sizeof (FcCharSet
) * FcCharSetUsed
+
1196 FcCharSetUsedEnts
* (sizeof (FcCharLeaf
*) + sizeof (FcChar16
)));
1198 n
= FcCharSetFreezeBase (c
);
1200 if (c
->bank
== FC_BANK_DYNAMIC
)
1202 if (c
->u
.dyn
.leaves
)
1204 FcMemFree (FC_MEM_CHARSET
, c
->num
* sizeof (FcCharLeaf
*));
1205 free (c
->u
.dyn
.leaves
);
1207 if (c
->u
.dyn
.numbers
)
1209 FcMemFree (FC_MEM_CHARSET
, c
->num
* sizeof (FcChar16
));
1210 free (c
->u
.dyn
.numbers
);
1212 FcMemFree (FC_MEM_CHARSET
, sizeof (FcCharSet
));
1220 FcNameUnparseCharSet (FcStrBuf
*buf
, const FcCharSet
*c
)
1228 for (FcCharSetIterStart (c
, &ci
);
1230 FcCharSetIterNext (c
, &ci
))
1232 if (!FcCharSetUnparseValue (buf
, ci
.ucs4
))
1234 for (i
= 0; i
< 256/32; i
++)
1235 if (!FcCharSetUnparseValue (buf
, ci
.leaf
->map
[i
]))
1242 FcCharSetIter ci
, checki
;
1244 /* null terminate for parser */
1245 FcStrBufChar (buf
, '\0');
1246 /* step back over null for life after test */
1248 check
= FcNameParseCharSet (buf
->buf
+ len
);
1249 FcCharSetIterStart (c
, &ci
);
1250 FcCharSetIterStart (check
, &checki
);
1251 while (ci
.leaf
|| checki
.leaf
)
1253 if (ci
.ucs4
< checki
.ucs4
)
1255 printf ("Missing leaf node at 0x%x\n", ci
.ucs4
);
1256 FcCharSetIterNext (c
, &ci
);
1258 else if (checki
.ucs4
< ci
.ucs4
)
1260 printf ("Extra leaf node at 0x%x\n", checki
.ucs4
);
1261 FcCharSetIterNext (check
, &checki
);
1266 FcChar32
*cm
= ci
.leaf
->map
;
1267 FcChar32
*checkm
= checki
.leaf
->map
;
1269 for (i
= 0; i
< 256; i
+= 32)
1272 printf ("Mismatching sets at 0x%08x: 0x%08x != 0x%08x\n",
1273 ci
.ucs4
+ i
, *cm
, *checkm
);
1277 FcCharSetIterNext (c
, &ci
);
1278 FcCharSetIterNext (check
, &checki
);
1281 if ((missing
= FcCharSetSubtractCount (c
, check
)))
1282 printf ("%d missing in reparsed result\n", missing
);
1283 if ((missing
= FcCharSetSubtractCount (check
, c
)))
1284 printf ("%d extra in reparsed result\n", missing
);
1285 FcCharSetDestroy (check
);
1293 FcCharSetNewBank(void)
1296 charset_numbers_count
= 0;
1297 charset_leaf_count
= 0;
1298 charset_leaf_idx_count
= 0;
1302 FcCharSetNeededBytes (const FcCharSet
*c
)
1304 /* yes, there's redundancy */
1306 charset_leaf_idx_count
++;
1307 charset_leaf_count
+= c
->num
;
1308 charset_numbers_count
+= c
->num
;
1309 return sizeof (FcCharSet
) +
1310 sizeof (int) + /* leaf_idx */
1311 sizeof (FcCharLeaf
) * c
->num
+ /* leaf */
1312 sizeof (FcChar16
) * c
->num
; /* number */
1316 FcCharSetEnsureBank (int bi
)
1318 if (!charsets
|| charset_bank_count
<= bi
)
1320 int new_count
= charset_bank_count
+ 2;
1327 cs
= realloc(charsets
, sizeof(FcCharSet
*) * new_count
);
1329 n
= realloc(numbers
, sizeof(FcChar16
*) * new_count
);
1331 lvs
= realloc(leaves
, sizeof(FcCharLeaf
*) * new_count
);
1333 lvi
= realloc(leaf_idx
, sizeof(int*) * new_count
);
1336 charsets
= cs
; numbers
= n
; leaves
= lvs
; leaf_idx
= lvi
;
1337 for (i
= charset_bank_count
; i
< new_count
; i
++)
1344 charset_bank_count
= new_count
;
1350 FcCharSetDistributeBytes (FcCache
* metadata
, void * block_ptr
)
1352 int bi
= FcCacheBankToIndex(metadata
->bank
);
1353 if (!FcCharSetEnsureBank(bi
))
1356 charsets
[bi
] = (FcCharSet
*)block_ptr
;
1357 block_ptr
= (void *)((char *)block_ptr
+
1358 (sizeof (FcCharSet
) * charset_count
));
1359 numbers
[bi
] = (FcChar16
*)block_ptr
;
1360 block_ptr
= (void *)((char *)block_ptr
+
1361 (sizeof(FcChar16
) * charset_numbers_count
));
1362 leaves
[bi
] = (FcCharLeaf
*)block_ptr
;
1363 block_ptr
= (void *)((char *)block_ptr
+
1364 (sizeof(FcCharLeaf
) * charset_leaf_count
));
1365 leaf_idx
[bi
] = (int *)block_ptr
;
1366 block_ptr
= (void *)((char *)block_ptr
+
1367 (sizeof(int) * charset_leaf_idx_count
));
1369 metadata
->charset_count
= charset_count
;
1370 metadata
->charset_numbers_count
= charset_numbers_count
;
1371 metadata
->charset_leaf_count
= charset_leaf_count
;
1372 metadata
->charset_leaf_idx_count
= charset_leaf_idx_count
;
1373 charset_ptr
= 0; charset_leaf_ptr
= 0;
1374 charset_leaf_idx_ptr
= 0; charset_numbers_ptr
= 0;
1379 FcCharSetSerialize(int bank
, FcCharSet
*c
)
1383 int bi
= FcCacheBankToIndex(bank
), cp
= charset_ptr
;
1385 new.ref
= FC_REF_CONSTANT
;
1387 new.u
.stat
.leafidx_offset
= charset_leaf_idx_ptr
;
1388 new.u
.stat
.numbers_offset
= charset_numbers_ptr
;
1391 charsets
[bi
][charset_ptr
++] = new;
1393 leaf_idx
[bi
][charset_leaf_idx_ptr
++] = charset_leaf_ptr
;
1394 for (i
= 0; i
< c
->num
; i
++)
1396 memcpy (&leaves
[bi
][charset_leaf_ptr
++],
1397 c
->u
.dyn
.leaves
[i
], sizeof(FcCharLeaf
));
1398 numbers
[bi
][charset_numbers_ptr
++] = c
->u
.dyn
.numbers
[i
];
1401 return &charsets
[bi
][cp
];
1405 FcCharSetUnserialize (FcCache metadata
, void *block_ptr
)
1407 int bi
= FcCacheBankToIndex(metadata
.bank
);
1408 if (!FcCharSetEnsureBank(bi
))
1411 charsets
[bi
] = (FcCharSet
*)block_ptr
;
1412 block_ptr
= (void *)((char *)block_ptr
+
1413 (sizeof (FcCharSet
) * metadata
.charset_count
));
1414 numbers
[bi
] = (FcChar16
*)block_ptr
;
1415 block_ptr
= (void *)((char *)block_ptr
+
1416 (sizeof(FcChar16
) * metadata
.charset_numbers_count
));
1417 leaves
[bi
] = (FcCharLeaf
*)block_ptr
;
1418 block_ptr
= (void *)((char *)block_ptr
+
1419 (sizeof(FcCharLeaf
) * metadata
.charset_leaf_count
));
1420 leaf_idx
[bi
] = (int *)block_ptr
;
1421 block_ptr
= (void *)((char *)block_ptr
+
1422 (sizeof(int) * metadata
.charset_leaf_idx_count
));
1428 FcCharSetGetLeaf(const FcCharSet
*c
, int i
)
1431 if (c
->bank
== FC_BANK_DYNAMIC
)
1432 return c
->u
.dyn
.leaves
[i
];
1433 bi
= FcCacheBankToIndex(c
->bank
);
1435 return &leaves
[bi
][leaf_idx
[bi
][c
->u
.stat
.leafidx_offset
]+i
];
1439 FcCharSetGetNumbers(const FcCharSet
*c
)
1442 if (c
->bank
== FC_BANK_DYNAMIC
)
1443 return c
->u
.dyn
.numbers
;
1444 bi
= FcCacheBankToIndex(c
->bank
);
1446 return &numbers
[bi
][c
->u
.stat
.numbers_offset
];