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.
33 FcCharSetCreate (void)
37 fcs
= (FcCharSet
*) malloc (sizeof (FcCharSet
));
40 FcMemAlloc (FC_MEM_CHARSET
, sizeof (FcCharSet
));
43 fcs
->leaves_offset
= FcPtrToOffset (fcs
, NULL
);
44 fcs
->numbers_offset
= FcPtrToOffset (fcs
, NULL
);
54 return FcCharSetCreate ();
58 FcCharSetDestroy (FcCharSet
*fcs
)
62 if (fcs
->ref
== FC_REF_CONSTANT
)
66 for (i
= 0; i
< fcs
->num
; i
++)
68 FcMemFree (FC_MEM_CHARLEAF
, sizeof (FcCharLeaf
));
69 free (FcCharSetLeaf (fcs
, i
));
71 if (FcCharSetLeaves (fcs
))
73 FcMemFree (FC_MEM_CHARSET
, fcs
->num
* sizeof (intptr_t));
74 free (FcCharSetLeaves (fcs
));
76 if (FcCharSetNumbers (fcs
))
78 FcMemFree (FC_MEM_CHARSET
, fcs
->num
* sizeof (FcChar16
));
79 free (FcCharSetNumbers (fcs
));
81 FcMemFree (FC_MEM_CHARSET
, sizeof (FcCharSet
));
86 * Locate the leaf containing the specified char, return
87 * its index if it exists, otherwise return negative of
88 * the (position + 1) where it should be inserted
92 FcCharSetFindLeafPos (const FcCharSet
*fcs
, FcChar32 ucs4
)
94 FcChar16
*numbers
= FcCharSetNumbers(fcs
);
97 int high
= fcs
->num
- 1;
104 int mid
= (low
+ high
) >> 1;
113 if (high
< 0 || (high
< fcs
->num
&& numbers
[high
] < ucs4
))
119 FcCharSetFindLeaf (const FcCharSet
*fcs
, FcChar32 ucs4
)
121 int pos
= FcCharSetFindLeafPos (fcs
, ucs4
);
123 return FcCharSetLeaf(fcs
, pos
);
128 FcCharSetPutLeaf (FcCharSet
*fcs
,
133 intptr_t *leaves
= FcCharSetLeaves (fcs
);
134 FcChar16
*numbers
= FcCharSetNumbers (fcs
);
140 leaves
= malloc (sizeof (*leaves
));
143 intptr_t *new_leaves
= realloc (leaves
, (fcs
->num
+ 1) *
145 intptr_t distance
= (intptr_t) new_leaves
- (intptr_t) leaves
;
147 if (new_leaves
&& distance
)
151 for (i
= 0; i
< fcs
->num
; i
++)
152 new_leaves
[i
] -= distance
;
160 FcMemFree (FC_MEM_CHARSET
, fcs
->num
* sizeof (intptr_t));
161 FcMemAlloc (FC_MEM_CHARSET
, (fcs
->num
+ 1) * sizeof (intptr_t));
162 fcs
->leaves_offset
= FcPtrToOffset (fcs
, leaves
);
165 numbers
= malloc (sizeof (FcChar16
));
167 numbers
= realloc (numbers
, (fcs
->num
+ 1) * sizeof (FcChar16
));
172 FcMemFree (FC_MEM_CHARSET
, fcs
->num
* sizeof (FcChar16
));
173 FcMemAlloc (FC_MEM_CHARSET
, (fcs
->num
+ 1) * sizeof (FcChar16
));
174 fcs
->numbers_offset
= FcPtrToOffset (fcs
, numbers
);
176 memmove (leaves
+ pos
+ 1, leaves
+ pos
,
177 (fcs
->num
- pos
) * sizeof (*leaves
));
178 memmove (numbers
+ pos
+ 1, numbers
+ pos
,
179 (fcs
->num
- pos
) * sizeof (*numbers
));
180 numbers
[pos
] = (FcChar16
) ucs4
;
181 leaves
[pos
] = FcPtrToOffset (leaves
, leaf
);
187 * Locate the leaf containing the specified char, creating it
192 FcCharSetFindLeafCreate (FcCharSet
*fcs
, FcChar32 ucs4
)
197 pos
= FcCharSetFindLeafPos (fcs
, ucs4
);
199 return FcCharSetLeaf(fcs
, pos
);
201 leaf
= calloc (1, sizeof (FcCharLeaf
));
206 if (!FcCharSetPutLeaf (fcs
, ucs4
, leaf
, pos
))
211 FcMemAlloc (FC_MEM_CHARLEAF
, sizeof (FcCharLeaf
));
216 FcCharSetInsertLeaf (FcCharSet
*fcs
, FcChar32 ucs4
, FcCharLeaf
*leaf
)
220 pos
= FcCharSetFindLeafPos (fcs
, ucs4
);
223 FcMemFree (FC_MEM_CHARLEAF
, sizeof (FcCharLeaf
));
224 free (FcCharSetLeaf (fcs
, pos
));
225 FcCharSetLeaves(fcs
)[pos
] = FcPtrToOffset (FcCharSetLeaves(fcs
),
230 return FcCharSetPutLeaf (fcs
, ucs4
, leaf
, pos
);
234 FcCharSetAddChar (FcCharSet
*fcs
, FcChar32 ucs4
)
239 if (fcs
->ref
== FC_REF_CONSTANT
)
241 leaf
= FcCharSetFindLeafCreate (fcs
, ucs4
);
244 b
= &leaf
->map
[(ucs4
& 0xff) >> 5];
245 *b
|= (1 << (ucs4
& 0x1f));
250 * An iterator for the leaves of a charset
253 typedef struct _fcCharSetIter
{
260 * Set iter->leaf to the leaf containing iter->ucs4 or higher
264 FcCharSetIterSet (const FcCharSet
*fcs
, FcCharSetIter
*iter
)
266 int pos
= FcCharSetFindLeafPos (fcs
, iter
->ucs4
);
277 iter
->ucs4
= (FcChar32
) FcCharSetNumbers(fcs
)[pos
] << 8;
279 iter
->leaf
= FcCharSetLeaf(fcs
, pos
);
282 printf ("set %08x: %08x\n", iter
->ucs4
, (FcChar32
) iter
->leaf
);
287 FcCharSetIterNext (const FcCharSet
*fcs
, FcCharSetIter
*iter
)
289 int pos
= iter
->pos
+ 1;
297 iter
->ucs4
= (FcChar32
) FcCharSetNumbers(fcs
)[pos
] << 8;
298 iter
->leaf
= FcCharSetLeaf(fcs
, pos
);
305 FcCharSetDump (const FcCharSet
*fcs
)
309 printf ("fcs %08x:\n", (FcChar32
) fcs
);
310 for (pos
= 0; pos
< fcs
->num
; pos
++)
312 FcCharLeaf
*leaf
= fcs
->leaves
[pos
];
313 FcChar32 ucs4
= (FcChar32
) fcs
->numbers
[pos
] << 8;
315 printf (" %08x: %08x\n", ucs4
, (FcChar32
) leaf
);
321 FcCharSetIterStart (const FcCharSet
*fcs
, FcCharSetIter
*iter
)
328 FcCharSetIterSet (fcs
, iter
);
332 FcCharSetCopy (FcCharSet
*src
)
334 if (src
->ref
!= FC_REF_CONSTANT
)
340 FcCharSetEqual (const FcCharSet
*a
, const FcCharSet
*b
)
342 FcCharSetIter ai
, bi
;
347 for (FcCharSetIterStart (a
, &ai
), FcCharSetIterStart (b
, &bi
);
349 FcCharSetIterNext (a
, &ai
), FcCharSetIterNext (b
, &bi
))
351 if (ai
.ucs4
!= bi
.ucs4
)
353 for (i
= 0; i
< 256/32; i
++)
354 if (ai
.leaf
->map
[i
] != bi
.leaf
->map
[i
])
357 return ai
.leaf
== bi
.leaf
;
361 FcCharSetAddLeaf (FcCharSet
*fcs
,
365 FcCharLeaf
*new = FcCharSetFindLeafCreate (fcs
, ucs4
);
373 FcCharSetOperate (const FcCharSet
*a
,
375 FcBool (*overlap
) (FcCharLeaf
*result
,
376 const FcCharLeaf
*al
,
377 const FcCharLeaf
*bl
),
382 FcCharSetIter ai
, bi
;
384 fcs
= FcCharSetCreate ();
387 FcCharSetIterStart (a
, &ai
);
388 FcCharSetIterStart (b
, &bi
);
389 while ((ai
.leaf
|| (bonly
&& bi
.leaf
)) && (bi
.leaf
|| (aonly
&& ai
.leaf
)))
391 if (ai
.ucs4
< bi
.ucs4
)
395 if (!FcCharSetAddLeaf (fcs
, ai
.ucs4
, ai
.leaf
))
397 FcCharSetIterNext (a
, &ai
);
402 FcCharSetIterSet (a
, &ai
);
405 else if (bi
.ucs4
< ai
.ucs4
)
409 if (!FcCharSetAddLeaf (fcs
, bi
.ucs4
, bi
.leaf
))
411 FcCharSetIterNext (b
, &bi
);
416 FcCharSetIterSet (b
, &bi
);
423 if ((*overlap
) (&leaf
, ai
.leaf
, bi
.leaf
))
425 if (!FcCharSetAddLeaf (fcs
, ai
.ucs4
, &leaf
))
428 FcCharSetIterNext (a
, &ai
);
429 FcCharSetIterNext (b
, &bi
);
434 FcCharSetDestroy (fcs
);
440 FcCharSetIntersectLeaf (FcCharLeaf
*result
,
441 const FcCharLeaf
*al
,
442 const FcCharLeaf
*bl
)
445 FcBool nonempty
= FcFalse
;
447 for (i
= 0; i
< 256/32; i
++)
448 if ((result
->map
[i
] = al
->map
[i
] & bl
->map
[i
]))
454 FcCharSetIntersect (const FcCharSet
*a
, const FcCharSet
*b
)
456 return FcCharSetOperate (a
, b
, FcCharSetIntersectLeaf
, FcFalse
, FcFalse
);
460 FcCharSetUnionLeaf (FcCharLeaf
*result
,
461 const FcCharLeaf
*al
,
462 const FcCharLeaf
*bl
)
466 for (i
= 0; i
< 256/32; i
++)
467 result
->map
[i
] = al
->map
[i
] | bl
->map
[i
];
472 FcCharSetUnion (const FcCharSet
*a
, const FcCharSet
*b
)
474 return FcCharSetOperate (a
, b
, FcCharSetUnionLeaf
, FcTrue
, FcTrue
);
478 FcCharSetSubtractLeaf (FcCharLeaf
*result
,
479 const FcCharLeaf
*al
,
480 const FcCharLeaf
*bl
)
483 FcBool nonempty
= FcFalse
;
485 for (i
= 0; i
< 256/32; i
++)
486 if ((result
->map
[i
] = al
->map
[i
] & ~bl
->map
[i
]))
492 FcCharSetSubtract (const FcCharSet
*a
, const FcCharSet
*b
)
494 return FcCharSetOperate (a
, b
, FcCharSetSubtractLeaf
, FcTrue
, FcFalse
);
498 FcCharSetHasChar (const FcCharSet
*fcs
, FcChar32 ucs4
)
500 FcCharLeaf
*leaf
= FcCharSetFindLeaf (fcs
, ucs4
);
503 return (leaf
->map
[(ucs4
& 0xff) >> 5] & (1 << (ucs4
& 0x1f))) != 0;
507 FcCharSetPopCount (FcChar32 c1
)
510 FcChar32 c2
= (c1
>> 1) & 033333333333;
511 c2
= c1
- c2
- ((c2
>> 1) & 033333333333);
512 return (((c2
+ (c2
>> 3)) & 030707070707) % 077);
516 FcCharSetIntersectCount (const FcCharSet
*a
, const FcCharSet
*b
)
518 FcCharSetIter ai
, bi
;
521 FcCharSetIterStart (a
, &ai
);
522 FcCharSetIterStart (b
, &bi
);
523 while (ai
.leaf
&& bi
.leaf
)
525 if (ai
.ucs4
== bi
.ucs4
)
527 FcChar32
*am
= ai
.leaf
->map
;
528 FcChar32
*bm
= bi
.leaf
->map
;
531 count
+= FcCharSetPopCount (*am
++ & *bm
++);
532 FcCharSetIterNext (a
, &ai
);
534 else if (ai
.ucs4
< bi
.ucs4
)
537 FcCharSetIterSet (a
, &ai
);
539 if (bi
.ucs4
< ai
.ucs4
)
542 FcCharSetIterSet (b
, &bi
);
549 FcCharSetCount (const FcCharSet
*a
)
554 for (FcCharSetIterStart (a
, &ai
); ai
.leaf
; FcCharSetIterNext (a
, &ai
))
557 FcChar32
*am
= ai
.leaf
->map
;
560 count
+= FcCharSetPopCount (*am
++);
566 FcCharSetSubtractCount (const FcCharSet
*a
, const FcCharSet
*b
)
568 FcCharSetIter ai
, bi
;
571 FcCharSetIterStart (a
, &ai
);
572 FcCharSetIterStart (b
, &bi
);
575 if (ai
.ucs4
<= bi
.ucs4
)
577 FcChar32
*am
= ai
.leaf
->map
;
579 if (ai
.ucs4
== bi
.ucs4
)
581 FcChar32
*bm
= bi
.leaf
->map
;
583 count
+= FcCharSetPopCount (*am
++ & ~*bm
++);
588 count
+= FcCharSetPopCount (*am
++);
590 FcCharSetIterNext (a
, &ai
);
595 FcCharSetIterSet (b
, &bi
);
602 * return FcTrue iff a is a subset of b
605 FcCharSetIsSubset (const FcCharSet
*a
, const FcCharSet
*b
)
610 if (a
== b
) return FcTrue
;
613 while (ai
< a
->num
&& bi
< b
->num
)
615 an
= FcCharSetNumbers(a
)[ai
];
616 bn
= FcCharSetNumbers(b
)[bi
];
618 * Check matching pages
622 FcChar32
*am
= FcCharSetLeaf(a
, ai
)->map
;
623 FcChar32
*bm
= FcCharSetLeaf(b
, bi
)->map
;
629 * Does am have any bits not in bm?
639 * Does a have any pages not in b?
646 int high
= b
->num
- 1;
649 * Search for page 'an' in 'b'
653 int mid
= (low
+ high
) >> 1;
654 bn
= FcCharSetNumbers(b
)[mid
];
666 while (bi
< b
->num
&& FcCharSetNumbers(b
)[bi
] < an
)
671 * did we look at every page?
677 * These two functions efficiently walk the entire charmap for
678 * other software (like pango) that want their own copy
682 FcCharSetNextPage (const FcCharSet
*a
,
683 FcChar32 map
[FC_CHARSET_MAP_SIZE
],
690 FcCharSetIterSet (a
, &ai
);
692 return FC_CHARSET_DONE
;
695 * Save current information
698 memcpy (map
, ai
.leaf
->map
, sizeof (ai
.leaf
->map
));
702 FcCharSetIterNext (a
, &ai
);
709 FcCharSetFirstPage (const FcCharSet
*a
,
710 FcChar32 map
[FC_CHARSET_MAP_SIZE
],
714 return FcCharSetNextPage (a
, map
, next
);
718 * old coverage API, rather hard to use correctly
721 FcCharSetCoverage (const FcCharSet
*a
, FcChar32 page
, FcChar32
*result
);
724 FcCharSetCoverage (const FcCharSet
*a
, FcChar32 page
, FcChar32
*result
)
729 FcCharSetIterSet (a
, &ai
);
732 memset (result
, '\0', 256 / 8);
737 memcpy (result
, ai
.leaf
->map
, sizeof (ai
.leaf
->map
));
738 FcCharSetIterNext (a
, &ai
);
745 * ASCII representation of charsets.
747 * Each leaf is represented as 9 32-bit values, the code of the first character followed
748 * by 8 32 bit values for the leaf itself. Each value is encoded as 5 ASCII characters,
749 * only 85 different values are used to avoid control characters as well as the other
750 * characters used to encode font names. 85**5 > 2^32 so things work out, but
751 * it's not exactly human readable output. As a special case, 0 is encoded as a space
754 static const unsigned char charToValue
[256] = {
755 /* "" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
756 /* "\b" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
757 /* "\020" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
758 /* "\030" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
759 /* " " */ 0xff, 0x00, 0xff, 0x01, 0x02, 0x03, 0x04, 0xff,
760 /* "(" */ 0x05, 0x06, 0x07, 0x08, 0xff, 0xff, 0x09, 0x0a,
761 /* "0" */ 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12,
762 /* "8" */ 0x13, 0x14, 0xff, 0x15, 0x16, 0xff, 0x17, 0x18,
763 /* "@" */ 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
764 /* "H" */ 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
765 /* "P" */ 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30,
766 /* "X" */ 0x31, 0x32, 0x33, 0x34, 0xff, 0x35, 0x36, 0xff,
767 /* "`" */ 0xff, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d,
768 /* "h" */ 0x3e, 0x3f, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45,
769 /* "p" */ 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d,
770 /* "x" */ 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53, 0x54, 0xff,
771 /* "\200" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
772 /* "\210" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
773 /* "\220" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
774 /* "\230" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
775 /* "\240" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
776 /* "\250" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
777 /* "\260" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
778 /* "\270" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
779 /* "\300" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
780 /* "\310" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
781 /* "\320" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
782 /* "\330" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
783 /* "\340" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
784 /* "\350" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
785 /* "\360" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
786 /* "\370" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
789 static const FcChar8 valueToChar
[0x55] = {
790 /* 0x00 */ '!', '#', '$', '%', '&', '(', ')', '*',
791 /* 0x08 */ '+', '.', '/', '0', '1', '2', '3', '4',
792 /* 0x10 */ '5', '6', '7', '8', '9', ';', '<', '>',
793 /* 0x18 */ '?', '@', 'A', 'B', 'C', 'D', 'E', 'F',
794 /* 0x20 */ 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
795 /* 0x28 */ 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
796 /* 0x30 */ 'W', 'X', 'Y', 'Z', '[', ']', '^', 'a',
797 /* 0x38 */ 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
798 /* 0x40 */ 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q',
799 /* 0x48 */ 'r', 's', 't', 'u', 'v', 'w', 'x', 'y',
800 /* 0x50 */ 'z', '{', '|', '}', '~',
804 FcCharSetParseValue (FcChar8
*string
, FcChar32
*value
)
818 for (i
= 0; i
< 5; i
++)
820 if (!(c
= (FcChar32
) (unsigned char) *string
++))
833 FcCharSetUnparseValue (FcStrBuf
*buf
, FcChar32 value
)
838 return FcStrBufChar (buf
, ' ');
843 FcChar8
*s
= string
+ 5;
845 for (i
= 0; i
< 5; i
++)
847 *--s
= valueToChar
[value
% 85];
850 for (i
= 0; i
< 5; i
++)
851 if (!FcStrBufChar (buf
, *s
++))
857 typedef struct _FcCharLeafEnt FcCharLeafEnt
;
859 struct _FcCharLeafEnt
{
865 #define FC_CHAR_LEAF_BLOCK (4096 / sizeof (FcCharLeafEnt))
866 static FcCharLeafEnt
**FcCharLeafBlocks
;
867 static int FcCharLeafBlockCount
;
869 static FcCharLeafEnt
*
870 FcCharLeafEntCreate (void)
872 static FcCharLeafEnt
*block
;
877 FcCharLeafEnt
**newBlocks
;
879 FcCharLeafBlockCount
++;
880 newBlocks
= realloc (FcCharLeafBlocks
, FcCharLeafBlockCount
* sizeof (FcCharLeafEnt
*));
883 FcCharLeafBlocks
= newBlocks
;
884 block
= FcCharLeafBlocks
[FcCharLeafBlockCount
-1] = malloc (FC_CHAR_LEAF_BLOCK
* sizeof (FcCharLeafEnt
));
887 FcMemAlloc (FC_MEM_CHARLEAF
, FC_CHAR_LEAF_BLOCK
* sizeof (FcCharLeafEnt
));
888 remain
= FC_CHAR_LEAF_BLOCK
;
894 #define FC_CHAR_LEAF_HASH_SIZE 257
897 FcCharLeafHash (FcCharLeaf
*leaf
)
902 for (i
= 0; i
< 256/32; i
++)
903 hash
= ((hash
<< 1) | (hash
>> 31)) ^ leaf
->map
[i
];
907 static int FcCharLeafTotal
;
908 static int FcCharLeafUsed
;
910 static FcCharLeafEnt
*FcCharLeafHashTable
[FC_CHAR_LEAF_HASH_SIZE
];
913 FcCharSetFreezeLeaf (FcCharLeaf
*leaf
)
915 FcChar32 hash
= FcCharLeafHash (leaf
);
916 FcCharLeafEnt
**bucket
= &FcCharLeafHashTable
[hash
% FC_CHAR_LEAF_HASH_SIZE
];
920 for (ent
= *bucket
; ent
; ent
= ent
->next
)
922 if (ent
->hash
== hash
&& !memcmp (&ent
->leaf
, leaf
, sizeof (FcCharLeaf
)))
926 ent
= FcCharLeafEntCreate();
938 FcCharSetThawAllLeaf (void)
942 for (i
= 0; i
< FC_CHAR_LEAF_HASH_SIZE
; i
++)
943 FcCharLeafHashTable
[i
] = 0;
948 for (i
= 0; i
< FcCharLeafBlockCount
; i
++)
949 free (FcCharLeafBlocks
[i
]);
951 free (FcCharLeafBlocks
);
952 FcCharLeafBlocks
= 0;
953 FcCharLeafBlockCount
= 0;
956 typedef struct _FcCharSetEnt FcCharSetEnt
;
958 struct _FcCharSetEnt
{
964 #define FC_CHAR_SET_HASH_SIZE 67
967 FcCharSetHash (FcCharSet
*fcs
)
973 for (i
= 0; i
< fcs
->num
* (int) (sizeof (FcCharLeaf
*) / sizeof (FcChar32
)); i
++)
974 hash
= ((hash
<< 1) | (hash
>> 31)) ^ (FcChar32
)(FcCharSetLeaf(fcs
, i
)->map
);
975 /* hash in numbers */
976 for (i
= 0; i
< fcs
->num
; i
++)
977 hash
= ((hash
<< 1) | (hash
>> 31)) ^ *FcCharSetNumbers(fcs
);
981 static int FcCharSetTotal
;
982 static int FcCharSetUsed
;
983 static int FcCharSetTotalEnts
, FcCharSetUsedEnts
;
985 static FcCharSetEnt
*FcCharSetHashTable
[FC_CHAR_SET_HASH_SIZE
];
988 FcCharSetFreezeBase (FcCharSet
*fcs
)
990 FcChar32 hash
= FcCharSetHash (fcs
);
991 FcCharSetEnt
**bucket
= &FcCharSetHashTable
[hash
% FC_CHAR_SET_HASH_SIZE
];
997 FcCharSetTotalEnts
+= fcs
->num
;
998 for (ent
= *bucket
; ent
; ent
= ent
->next
)
1000 if (ent
->hash
== hash
&&
1001 ent
->set
.num
== fcs
->num
&&
1002 !memcmp (FcCharSetNumbers(&ent
->set
),
1003 FcCharSetNumbers(fcs
),
1004 fcs
->num
* sizeof (FcChar16
)))
1009 for (i
= 0; i
< fcs
->num
; i
++)
1010 if (FcCharSetLeaf(&ent
->set
, i
) != FcCharSetLeaf(fcs
, i
))
1017 size
= (sizeof (FcCharSetEnt
) +
1018 fcs
->num
* sizeof (FcCharLeaf
*) +
1019 fcs
->num
* sizeof (FcChar16
));
1020 ent
= malloc (size
);
1023 FcMemAlloc (FC_MEM_CHARSET
, size
);
1025 FcCharSetUsedEnts
+= fcs
->num
;
1027 ent
->set
.ref
= FC_REF_CONSTANT
;
1028 ent
->set
.num
= fcs
->num
;
1031 intptr_t *ent_leaves
;
1033 ent
->set
.leaves_offset
= sizeof (ent
->set
);
1034 ent
->set
.numbers_offset
= (ent
->set
.leaves_offset
+
1035 fcs
->num
* sizeof (intptr_t));
1037 ent_leaves
= FcCharSetLeaves (&ent
->set
);
1038 for (i
= 0; i
< fcs
->num
; i
++)
1039 ent_leaves
[i
] = FcPtrToOffset (ent_leaves
,
1040 FcCharSetLeaf (fcs
, i
));
1041 memcpy (FcCharSetNumbers (&ent
->set
),
1042 FcCharSetNumbers (fcs
),
1043 fcs
->num
* sizeof (FcChar16
));
1047 ent
->set
.leaves_offset
= 0;
1048 ent
->set
.numbers_offset
= 0;
1052 ent
->next
= *bucket
;
1058 FcCharSetThawAll (void)
1061 FcCharSetEnt
*ent
, *next
;
1063 for (i
= 0; i
< FC_CHAR_SET_HASH_SIZE
; i
++)
1065 for (ent
= FcCharSetHashTable
[i
]; ent
; ent
= next
)
1070 FcCharSetHashTable
[i
] = 0;
1074 FcCharSetTotalEnts
= 0;
1076 FcCharSetUsedEnts
= 0;
1078 FcCharSetThawAllLeaf ();
1082 FcCharSetFreeze (FcCharSet
*fcs
)
1089 b
= FcCharSetCreate ();
1092 for (i
= 0; i
< fcs
->num
; i
++)
1094 l
= FcCharSetFreezeLeaf (FcCharSetLeaf(fcs
, i
));
1097 if (!FcCharSetInsertLeaf (b
, FcCharSetNumbers(fcs
)[i
] << 8, l
))
1100 n
= FcCharSetFreezeBase (b
);
1102 if (FcCharSetLeaves (b
))
1104 FcMemFree (FC_MEM_CHARSET
, b
->num
* sizeof (FcCharLeaf
*));
1105 free (FcCharSetLeaves (b
));
1107 if (FcCharSetNumbers (b
))
1109 FcMemFree (FC_MEM_CHARSET
, b
->num
* sizeof (FcChar16
));
1110 free (FcCharSetNumbers (b
));
1112 FcMemFree (FC_MEM_CHARSET
, sizeof (FcCharSet
));
1119 FcNameParseCharSet (FcChar8
*string
)
1121 FcCharSet
*c
, *n
= 0;
1128 c
= FcCharSetCreate ();
1133 string
= FcCharSetParseValue (string
, &ucs4
);
1137 for (i
= 0; i
< 256/32; i
++)
1139 string
= FcCharSetParseValue (string
, &temp
.map
[i
]);
1142 bits
|= temp
.map
[i
];
1146 leaf
= FcCharSetFreezeLeaf (&temp
);
1149 if (!FcCharSetInsertLeaf (c
, ucs4
, leaf
))
1154 printf (" %8s %8s %8s %8s\n", "total", "totalmem", "new", "newmem");
1155 printf ("Leaves: %8d %8d %8d %8d\n",
1156 FcCharLeafTotal
, sizeof (FcCharLeaf
) * FcCharLeafTotal
,
1157 FcCharLeafUsed
, sizeof (FcCharLeaf
) * FcCharLeafUsed
);
1158 printf ("Charsets: %8d %8d %8d %8d\n",
1159 FcCharSetTotal
, sizeof (FcCharSet
) * FcCharSetTotal
,
1160 FcCharSetUsed
, sizeof (FcCharSet
) * FcCharSetUsed
);
1161 printf ("Tables: %8d %8d %8d %8d\n",
1162 FcCharSetTotalEnts
, FcCharSetTotalEnts
* (sizeof (FcCharLeaf
*) + sizeof (FcChar16
)),
1163 FcCharSetUsedEnts
, FcCharSetUsedEnts
* (sizeof (FcCharLeaf
*) + sizeof (FcChar16
)));
1164 printf ("Total: %8s %8d %8s %8d\n",
1166 sizeof (FcCharLeaf
) * FcCharLeafTotal
+
1167 sizeof (FcCharSet
) * FcCharSetTotal
+
1168 FcCharSetTotalEnts
* (sizeof (FcCharLeaf
*) + sizeof (FcChar16
)),
1170 sizeof (FcCharLeaf
) * FcCharLeafUsed
+
1171 sizeof (FcCharSet
) * FcCharSetUsed
+
1172 FcCharSetUsedEnts
* (sizeof (FcCharLeaf
*) + sizeof (FcChar16
)));
1174 n
= FcCharSetFreezeBase (c
);
1176 if (FcCharSetLeaves (c
))
1178 FcMemFree (FC_MEM_CHARSET
, c
->num
* sizeof (FcCharLeaf
*));
1179 free (FcCharSetLeaves (c
));
1181 if (FcCharSetNumbers (c
))
1183 FcMemFree (FC_MEM_CHARSET
, c
->num
* sizeof (FcChar16
));
1184 free (FcCharSetNumbers (c
));
1186 FcMemFree (FC_MEM_CHARSET
, sizeof (FcCharSet
));
1193 FcNameUnparseCharSet (FcStrBuf
*buf
, const FcCharSet
*c
)
1201 for (FcCharSetIterStart (c
, &ci
);
1203 FcCharSetIterNext (c
, &ci
))
1205 if (!FcCharSetUnparseValue (buf
, ci
.ucs4
))
1207 for (i
= 0; i
< 256/32; i
++)
1208 if (!FcCharSetUnparseValue (buf
, ci
.leaf
->map
[i
]))
1215 FcCharSetIter ci
, checki
;
1217 /* null terminate for parser */
1218 FcStrBufChar (buf
, '\0');
1219 /* step back over null for life after test */
1221 check
= FcNameParseCharSet (buf
->buf
+ len
);
1222 FcCharSetIterStart (c
, &ci
);
1223 FcCharSetIterStart (check
, &checki
);
1224 while (ci
.leaf
|| checki
.leaf
)
1226 if (ci
.ucs4
< checki
.ucs4
)
1228 printf ("Missing leaf node at 0x%x\n", ci
.ucs4
);
1229 FcCharSetIterNext (c
, &ci
);
1231 else if (checki
.ucs4
< ci
.ucs4
)
1233 printf ("Extra leaf node at 0x%x\n", checki
.ucs4
);
1234 FcCharSetIterNext (check
, &checki
);
1239 FcChar32
*cm
= ci
.leaf
->map
;
1240 FcChar32
*checkm
= checki
.leaf
->map
;
1242 for (i
= 0; i
< 256; i
+= 32)
1245 printf ("Mismatching sets at 0x%08x: 0x%08x != 0x%08x\n",
1246 ci
.ucs4
+ i
, *cm
, *checkm
);
1250 FcCharSetIterNext (c
, &ci
);
1251 FcCharSetIterNext (check
, &checki
);
1254 if ((missing
= FcCharSetSubtractCount (c
, check
)))
1255 printf ("%d missing in reparsed result\n", missing
);
1256 if ((missing
= FcCharSetSubtractCount (check
, c
)))
1257 printf ("%d extra in reparsed result\n", missing
);
1258 FcCharSetDestroy (check
);
1266 FcCharSetSerializeAlloc (FcSerialize
*serialize
, const FcCharSet
*cs
)
1268 intptr_t *leaves
= FcCharSetLeaves (cs
);
1269 FcChar16
*numbers
= FcCharSetNumbers (cs
);
1272 if (!FcSerializeAlloc (serialize
, cs
, sizeof (FcCharSet
)))
1274 if (!FcSerializeAlloc (serialize
, leaves
, cs
->num
* sizeof (intptr_t)))
1276 if (!FcSerializeAlloc (serialize
, numbers
, cs
->num
* sizeof (FcChar16
)))
1278 for (i
= 0; i
< cs
->num
; i
++)
1279 if (!FcSerializeAlloc (serialize
, FcCharSetLeaf(cs
, i
),
1280 sizeof (FcCharLeaf
)))
1286 FcCharSetSerialize(FcSerialize
*serialize
, const FcCharSet
*cs
)
1288 FcCharSet
*cs_serialized
= FcSerializePtr (serialize
, cs
);
1289 intptr_t *leaves
, *leaves_serialized
;
1290 FcChar16
*numbers
, *numbers_serialized
;
1291 FcCharLeaf
*leaf
, *leaf_serialized
;
1297 cs_serialized
->ref
= FC_REF_CONSTANT
;
1298 cs_serialized
->num
= cs
->num
;
1300 leaves
= FcCharSetLeaves (cs
);
1301 leaves_serialized
= FcSerializePtr (serialize
, leaves
);
1302 if (!leaves_serialized
)
1305 cs_serialized
->leaves_offset
= FcPtrToOffset (cs_serialized
,
1308 numbers
= FcCharSetNumbers (cs
);
1309 numbers_serialized
= FcSerializePtr (serialize
, numbers
);
1313 cs_serialized
->numbers_offset
= FcPtrToOffset (cs_serialized
,
1314 numbers_serialized
);
1316 for (i
= 0; i
< cs
->num
; i
++)
1318 leaf
= FcCharSetLeaf (cs
, i
);
1319 leaf_serialized
= FcSerializePtr (serialize
, leaf
);
1320 if (!leaf_serialized
)
1322 *leaf_serialized
= *leaf
;
1323 leaves_serialized
[i
] = FcPtrToOffset (leaves_serialized
, leaf
);
1324 numbers_serialized
[i
] = numbers
[i
];
1327 return cs_serialized
;