2 * $XFree86: xc/lib/fontconfig/src/fccharset.c,v 1.18 2002/08/22 07:36:44 keithp Exp $
4 * Copyright © 2001 Keith Packard, member of The XFree86 Project, Inc.
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
));
54 return FcCharSetCreate ();
59 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 (fcs
->leaves
[i
]);
73 FcMemFree (FC_MEM_CHARSET
, fcs
->num
* sizeof (FcCharLeaf
*));
78 FcMemFree (FC_MEM_CHARSET
, fcs
->num
* sizeof (FcChar16
));
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
= fcs
->numbers
;
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 fcs
->leaves
[pos
];
128 FcCharSetPutLeaf (FcCharSet
*fcs
,
140 leaves
= malloc (sizeof (FcCharLeaf
*));
142 leaves
= realloc (fcs
->leaves
, (fcs
->num
+ 1) * sizeof (FcCharLeaf
*));
146 FcMemFree (FC_MEM_CHARSET
, fcs
->num
* sizeof (FcCharLeaf
*));
147 FcMemAlloc (FC_MEM_CHARSET
, (fcs
->num
+ 1) * sizeof (FcCharLeaf
*));
148 fcs
->leaves
= leaves
;
150 numbers
= malloc (sizeof (FcChar16
));
152 numbers
= realloc (fcs
->numbers
, (fcs
->num
+ 1) * sizeof (FcChar16
));
156 FcMemFree (FC_MEM_CHARSET
, fcs
->num
* sizeof (FcChar16
));
157 FcMemAlloc (FC_MEM_CHARSET
, (fcs
->num
+ 1) * sizeof (FcChar16
));
158 fcs
->numbers
= numbers
;
160 memmove (fcs
->leaves
+ pos
+ 1, fcs
->leaves
+ pos
,
161 (fcs
->num
- pos
) * sizeof (FcCharLeaf
*));
162 memmove (fcs
->numbers
+ pos
+ 1, fcs
->numbers
+ pos
,
163 (fcs
->num
- pos
) * sizeof (FcChar16
));
164 fcs
->numbers
[pos
] = (FcChar16
) ucs4
;
165 fcs
->leaves
[pos
] = leaf
;
171 * Locate the leaf containing the specified char, creating it
176 FcCharSetFindLeafCreate (FcCharSet
*fcs
, FcChar32 ucs4
)
181 pos
= FcCharSetFindLeafPos (fcs
, ucs4
);
183 return fcs
->leaves
[pos
];
185 leaf
= calloc (1, sizeof (FcCharLeaf
));
190 if (!FcCharSetPutLeaf (fcs
, ucs4
, leaf
, pos
))
195 FcMemAlloc (FC_MEM_CHARLEAF
, sizeof (FcCharLeaf
));
200 FcCharSetInsertLeaf (FcCharSet
*fcs
, FcChar32 ucs4
, FcCharLeaf
*leaf
)
204 pos
= FcCharSetFindLeafPos (fcs
, ucs4
);
207 FcMemFree (FC_MEM_CHARLEAF
, sizeof (FcCharLeaf
));
208 free (fcs
->leaves
[pos
]);
209 fcs
->leaves
[pos
] = leaf
;
213 return FcCharSetPutLeaf (fcs
, ucs4
, leaf
, pos
);
217 FcCharSetAddChar (FcCharSet
*fcs
, FcChar32 ucs4
)
222 if (fcs
->ref
== FC_REF_CONSTANT
)
224 leaf
= FcCharSetFindLeafCreate (fcs
, ucs4
);
227 b
= &leaf
->map
[(ucs4
& 0xff) >> 5];
228 *b
|= (1 << (ucs4
& 0x1f));
233 * An iterator for the leaves of a charset
236 typedef struct _fcCharSetIter
{
243 * Set iter->leaf to the leaf containing iter->ucs4 or higher
247 FcCharSetIterSet (const FcCharSet
*fcs
, FcCharSetIter
*iter
)
249 int pos
= FcCharSetFindLeafPos (fcs
, iter
->ucs4
);
260 iter
->ucs4
= (FcChar32
) fcs
->numbers
[pos
] << 8;
262 iter
->leaf
= fcs
->leaves
[pos
];
265 printf ("set %08x: %08x\n", iter
->ucs4
, (FcChar32
) iter
->leaf
);
270 FcCharSetIterNext (const FcCharSet
*fcs
, FcCharSetIter
*iter
)
272 int pos
= iter
->pos
+ 1;
280 iter
->ucs4
= (FcChar32
) fcs
->numbers
[pos
] << 8;
281 iter
->leaf
= fcs
->leaves
[pos
];
288 FcCharSetDump (const FcCharSet
*fcs
)
292 printf ("fcs %08x:\n", (FcChar32
) fcs
);
293 for (pos
= 0; pos
< fcs
->num
; pos
++)
295 FcCharLeaf
*leaf
= fcs
->leaves
[pos
];
296 FcChar32 ucs4
= (FcChar32
) fcs
->numbers
[pos
] << 8;
298 printf (" %08x: %08x\n", ucs4
, (FcChar32
) leaf
);
304 FcCharSetIterStart (const FcCharSet
*fcs
, FcCharSetIter
*iter
)
310 FcCharSetIterSet (fcs
, iter
);
314 FcCharSetCopy (FcCharSet
*src
)
316 if (src
->ref
!= FC_REF_CONSTANT
)
322 FcCharSetEqual (const FcCharSet
*a
, const FcCharSet
*b
)
324 FcCharSetIter ai
, bi
;
329 for (FcCharSetIterStart (a
, &ai
), FcCharSetIterStart (b
, &bi
);
331 FcCharSetIterNext (a
, &ai
), FcCharSetIterNext (b
, &bi
))
333 if (ai
.ucs4
!= bi
.ucs4
)
335 for (i
= 0; i
< 256/32; i
++)
336 if (ai
.leaf
->map
[i
] != bi
.leaf
->map
[i
])
339 return ai
.leaf
== bi
.leaf
;
343 FcCharSetAddLeaf (FcCharSet
*fcs
,
347 FcCharLeaf
*new = FcCharSetFindLeafCreate (fcs
, ucs4
);
355 FcCharSetOperate (const FcCharSet
*a
,
357 FcBool (*overlap
) (FcCharLeaf
*result
,
358 const FcCharLeaf
*al
,
359 const FcCharLeaf
*bl
),
364 FcCharSetIter ai
, bi
;
366 fcs
= FcCharSetCreate ();
369 FcCharSetIterStart (a
, &ai
);
370 FcCharSetIterStart (b
, &bi
);
371 while ((ai
.leaf
|| (bonly
&& bi
.leaf
)) && (bi
.leaf
|| (aonly
&& ai
.leaf
)))
373 if (ai
.ucs4
< bi
.ucs4
)
377 if (!FcCharSetAddLeaf (fcs
, ai
.ucs4
, ai
.leaf
))
379 FcCharSetIterNext (a
, &ai
);
384 FcCharSetIterSet (a
, &ai
);
387 else if (bi
.ucs4
< ai
.ucs4
)
391 if (!FcCharSetAddLeaf (fcs
, bi
.ucs4
, bi
.leaf
))
393 FcCharSetIterNext (b
, &bi
);
398 FcCharSetIterSet (b
, &bi
);
405 if ((*overlap
) (&leaf
, ai
.leaf
, bi
.leaf
))
407 if (!FcCharSetAddLeaf (fcs
, ai
.ucs4
, &leaf
))
410 FcCharSetIterNext (a
, &ai
);
411 FcCharSetIterNext (b
, &bi
);
416 FcCharSetDestroy (fcs
);
422 FcCharSetIntersectLeaf (FcCharLeaf
*result
,
423 const FcCharLeaf
*al
,
424 const FcCharLeaf
*bl
)
427 FcBool nonempty
= FcFalse
;
429 for (i
= 0; i
< 256/32; i
++)
430 if ((result
->map
[i
] = al
->map
[i
] & bl
->map
[i
]))
436 FcCharSetIntersect (const FcCharSet
*a
, const FcCharSet
*b
)
438 return FcCharSetOperate (a
, b
, FcCharSetIntersectLeaf
, FcFalse
, FcFalse
);
442 FcCharSetUnionLeaf (FcCharLeaf
*result
,
443 const FcCharLeaf
*al
,
444 const FcCharLeaf
*bl
)
448 for (i
= 0; i
< 256/32; i
++)
449 result
->map
[i
] = al
->map
[i
] | bl
->map
[i
];
454 FcCharSetUnion (const FcCharSet
*a
, const FcCharSet
*b
)
456 return FcCharSetOperate (a
, b
, FcCharSetUnionLeaf
, FcTrue
, FcTrue
);
460 FcCharSetSubtractLeaf (FcCharLeaf
*result
,
461 const FcCharLeaf
*al
,
462 const FcCharLeaf
*bl
)
465 FcBool nonempty
= FcFalse
;
467 for (i
= 0; i
< 256/32; i
++)
468 if ((result
->map
[i
] = al
->map
[i
] & ~bl
->map
[i
]))
474 FcCharSetSubtract (const FcCharSet
*a
, const FcCharSet
*b
)
476 return FcCharSetOperate (a
, b
, FcCharSetSubtractLeaf
, FcTrue
, FcFalse
);
480 FcCharSetHasChar (const FcCharSet
*fcs
, FcChar32 ucs4
)
482 FcCharLeaf
*leaf
= FcCharSetFindLeaf (fcs
, ucs4
);
485 return (leaf
->map
[(ucs4
& 0xff) >> 5] & (1 << (ucs4
& 0x1f))) != 0;
489 FcCharSetPopCount (FcChar32 c1
)
492 FcChar32 c2
= (c1
>> 1) & 033333333333;
493 c2
= c1
- c2
- ((c2
>> 1) & 033333333333);
494 return (((c2
+ (c2
>> 3)) & 030707070707) % 077);
498 FcCharSetIntersectCount (const FcCharSet
*a
, const FcCharSet
*b
)
500 FcCharSetIter ai
, bi
;
503 FcCharSetIterStart (a
, &ai
);
504 FcCharSetIterStart (b
, &bi
);
505 while (ai
.leaf
&& bi
.leaf
)
507 if (ai
.ucs4
== bi
.ucs4
)
509 FcChar32
*am
= ai
.leaf
->map
;
510 FcChar32
*bm
= bi
.leaf
->map
;
513 count
+= FcCharSetPopCount (*am
++ & *bm
++);
514 FcCharSetIterNext (a
, &ai
);
516 else if (ai
.ucs4
< bi
.ucs4
)
519 FcCharSetIterSet (a
, &ai
);
521 if (bi
.ucs4
< ai
.ucs4
)
524 FcCharSetIterSet (b
, &bi
);
531 FcCharSetCount (const FcCharSet
*a
)
536 for (FcCharSetIterStart (a
, &ai
); ai
.leaf
; FcCharSetIterNext (a
, &ai
))
539 FcChar32
*am
= ai
.leaf
->map
;
542 count
+= FcCharSetPopCount (*am
++);
548 FcCharSetSubtractCount (const FcCharSet
*a
, const FcCharSet
*b
)
550 FcCharSetIter ai
, bi
;
553 FcCharSetIterStart (a
, &ai
);
554 FcCharSetIterStart (b
, &bi
);
557 if (ai
.ucs4
<= bi
.ucs4
)
559 FcChar32
*am
= ai
.leaf
->map
;
561 if (ai
.ucs4
== bi
.ucs4
)
563 FcChar32
*bm
= bi
.leaf
->map
;;
565 count
+= FcCharSetPopCount (*am
++ & ~*bm
++);
570 count
+= FcCharSetPopCount (*am
++);
572 FcCharSetIterNext (a
, &ai
);
577 FcCharSetIterSet (b
, &bi
);
584 * return FcTrue iff a is a subset of b
587 FcCharSetIsSubset (const FcCharSet
*a
, const FcCharSet
*b
)
592 if (a
== b
) return FcTrue
;
595 while (ai
< a
->num
&& bi
< b
->num
)
601 FcChar32
*am
= a
->leaves
[ai
]->map
;
602 FcChar32
*bm
= b
->leaves
[bi
]->map
;
619 int high
= b
->num
- 1;
623 int mid
= (low
+ high
) >> 1;
624 bn
= b
->numbers
[mid
];
636 while (bi
< b
->num
&& b
->numbers
[bi
] < an
)
644 * These two functions efficiently walk the entire charmap for
645 * other software (like pango) that want their own copy
649 FcCharSetNextPage (const FcCharSet
*a
,
650 FcChar32 map
[FC_CHARSET_MAP_SIZE
],
657 FcCharSetIterSet (a
, &ai
);
659 return FC_CHARSET_DONE
;
662 * Save current information
665 memcpy (map
, ai
.leaf
->map
, sizeof (ai
.leaf
->map
));
669 FcCharSetIterNext (a
, &ai
);
676 FcCharSetFirstPage (const FcCharSet
*a
,
677 FcChar32 map
[FC_CHARSET_MAP_SIZE
],
681 return FcCharSetNextPage (a
, map
, next
);
685 * old coverage API, rather hard to use correctly
688 FcCharSetCoverage (const FcCharSet
*a
, FcChar32 page
, FcChar32
*result
);
691 FcCharSetCoverage (const FcCharSet
*a
, FcChar32 page
, FcChar32
*result
)
696 FcCharSetIterSet (a
, &ai
);
699 memset (result
, '\0', 256 / 8);
704 memcpy (result
, ai
.leaf
->map
, sizeof (ai
.leaf
->map
));
705 FcCharSetIterNext (a
, &ai
);
712 * ASCII representation of charsets.
714 * Each leaf is represented as 9 32-bit values, the code of the first character followed
715 * by 8 32 bit values for the leaf itself. Each value is encoded as 5 ASCII characters,
716 * only 85 different values are used to avoid control characters as well as the other
717 * characters used to encode font names. 85**5 > 2^32 so things work out, but
718 * it's not exactly human readable output. As a special case, 0 is encoded as a space
721 static const unsigned char charToValue
[256] = {
722 /* "" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
723 /* "\b" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
724 /* "\020" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
725 /* "\030" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
726 /* " " */ 0xff, 0x00, 0xff, 0x01, 0x02, 0x03, 0x04, 0xff,
727 /* "(" */ 0x05, 0x06, 0x07, 0x08, 0xff, 0xff, 0x09, 0x0a,
728 /* "0" */ 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12,
729 /* "8" */ 0x13, 0x14, 0xff, 0x15, 0x16, 0xff, 0x17, 0x18,
730 /* "@" */ 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
731 /* "H" */ 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
732 /* "P" */ 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30,
733 /* "X" */ 0x31, 0x32, 0x33, 0x34, 0xff, 0x35, 0x36, 0xff,
734 /* "`" */ 0xff, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d,
735 /* "h" */ 0x3e, 0x3f, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45,
736 /* "p" */ 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d,
737 /* "x" */ 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53, 0x54, 0xff,
738 /* "\200" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
739 /* "\210" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
740 /* "\220" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
741 /* "\230" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
742 /* "\240" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
743 /* "\250" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
744 /* "\260" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
745 /* "\270" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
746 /* "\300" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
747 /* "\310" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
748 /* "\320" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
749 /* "\330" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
750 /* "\340" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
751 /* "\350" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
752 /* "\360" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
753 /* "\370" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
756 static const FcChar8 valueToChar
[0x55] = {
757 /* 0x00 */ '!', '#', '$', '%', '&', '(', ')', '*',
758 /* 0x08 */ '+', '.', '/', '0', '1', '2', '3', '4',
759 /* 0x10 */ '5', '6', '7', '8', '9', ';', '<', '>',
760 /* 0x18 */ '?', '@', 'A', 'B', 'C', 'D', 'E', 'F',
761 /* 0x20 */ 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
762 /* 0x28 */ 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
763 /* 0x30 */ 'W', 'X', 'Y', 'Z', '[', ']', '^', 'a',
764 /* 0x38 */ 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
765 /* 0x40 */ 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q',
766 /* 0x48 */ 'r', 's', 't', 'u', 'v', 'w', 'x', 'y',
767 /* 0x50 */ 'z', '{', '|', '}', '~',
771 FcCharSetParseValue (FcChar8
*string
, FcChar32
*value
)
785 for (i
= 0; i
< 5; i
++)
787 if (!(c
= (FcChar32
) (unsigned char) *string
++))
800 FcCharSetUnparseValue (FcStrBuf
*buf
, FcChar32 value
)
805 return FcStrBufChar (buf
, ' ');
810 FcChar8
*s
= string
+ 5;
812 for (i
= 0; i
< 5; i
++)
814 *--s
= valueToChar
[value
% 85];
817 for (i
= 0; i
< 5; i
++)
818 if (!FcStrBufChar (buf
, *s
++))
824 typedef struct _FcCharLeafEnt FcCharLeafEnt
;
826 struct _FcCharLeafEnt
{
832 #define FC_CHAR_LEAF_BLOCK (4096 / sizeof (FcCharLeafEnt))
834 static FcCharLeafEnt
*
835 FcCharLeafEntCreate (void)
837 static FcCharLeafEnt
*block
;
842 block
= malloc (FC_CHAR_LEAF_BLOCK
* sizeof (FcCharLeafEnt
));
845 FcMemAlloc (FC_MEM_CHARLEAF
, FC_CHAR_LEAF_BLOCK
* sizeof (FcCharLeafEnt
));
846 remain
= FC_CHAR_LEAF_BLOCK
;
852 #define FC_CHAR_LEAF_HASH_SIZE 257
855 FcCharLeafHash (FcCharLeaf
*leaf
)
860 for (i
= 0; i
< 256/32; i
++)
861 hash
= ((hash
<< 1) | (hash
>> 31)) ^ leaf
->map
[i
];
865 static int FcCharLeafTotal
;
866 static int FcCharLeafUsed
;
869 FcCharSetFreezeLeaf (FcCharLeaf
*leaf
)
871 static FcCharLeafEnt
*hashTable
[FC_CHAR_LEAF_HASH_SIZE
];
872 FcChar32 hash
= FcCharLeafHash (leaf
);
873 FcCharLeafEnt
**bucket
= &hashTable
[hash
% FC_CHAR_LEAF_HASH_SIZE
];
877 for (ent
= *bucket
; ent
; ent
= ent
->next
)
879 if (ent
->hash
== hash
&& !memcmp (&ent
->leaf
, leaf
, sizeof (FcCharLeaf
)))
883 ent
= FcCharLeafEntCreate();
894 typedef struct _FcCharSetEnt FcCharSetEnt
;
896 struct _FcCharSetEnt
{
902 #define FC_CHAR_SET_HASH_SIZE 67
905 FcCharSetHash (FcCharSet
*fcs
)
912 p
= (FcChar32
*) fcs
->leaves
;
913 for (i
= 0; i
< fcs
->num
* sizeof (FcCharLeaf
*) / sizeof (FcChar32
); i
++)
914 hash
= ((hash
<< 1) | (hash
>> 31)) ^ *p
++;
915 /* hash in numbers */
916 for (i
= 0; i
< fcs
->num
; i
++)
917 hash
= ((hash
<< 1) | (hash
>> 31)) ^ fcs
->numbers
[i
];
921 static int FcCharSetTotal
;
922 static int FcCharSetUsed
;
923 static int FcCharSetTotalEnts
, FcCharSetUsedEnts
;
926 FcCharSetFreezeBase (FcCharSet
*fcs
)
928 static FcCharSetEnt
*hashTable
[FC_CHAR_SET_HASH_SIZE
];
929 FcChar32 hash
= FcCharSetHash (fcs
);
930 FcCharSetEnt
**bucket
= &hashTable
[hash
% FC_CHAR_SET_HASH_SIZE
];
935 FcCharSetTotalEnts
+= fcs
->num
;
936 for (ent
= *bucket
; ent
; ent
= ent
->next
)
938 if (ent
->hash
== hash
&&
939 ent
->set
.num
== fcs
->num
&&
940 !memcmp (ent
->set
.leaves
, fcs
->leaves
,
941 fcs
->num
* sizeof (FcCharLeaf
*)) &&
942 !memcmp (ent
->set
.numbers
, fcs
->numbers
,
943 fcs
->num
* sizeof (FcChar16
)))
949 size
= (sizeof (FcCharSetEnt
) +
950 fcs
->num
* sizeof (FcCharLeaf
*) +
951 fcs
->num
* sizeof (FcChar16
));
955 FcMemAlloc (FC_MEM_CHARSET
, size
);
957 FcCharSetUsedEnts
+= fcs
->num
;
959 ent
->set
.ref
= FC_REF_CONSTANT
;
960 ent
->set
.num
= fcs
->num
;
963 ent
->set
.leaves
= (FcCharLeaf
**) (ent
+ 1);
964 ent
->set
.numbers
= (FcChar16
*) (ent
->set
.leaves
+ fcs
->num
);
965 memcpy (ent
->set
.leaves
, fcs
->leaves
, fcs
->num
* sizeof (FcCharLeaf
*));
966 memcpy (ent
->set
.numbers
, fcs
->numbers
, fcs
->num
* sizeof (FcChar16
));
971 ent
->set
.numbers
= 0;
981 FcCharSetFreeze (FcCharSet
*fcs
)
988 b
= FcCharSetCreate ();
991 for (i
= 0; i
< fcs
->num
; i
++)
993 l
= FcCharSetFreezeLeaf (fcs
->leaves
[i
]);
996 if (!FcCharSetInsertLeaf (b
, fcs
->numbers
[i
] << 8, l
))
999 n
= FcCharSetFreezeBase (b
);
1003 FcMemFree (FC_MEM_CHARSET
, b
->num
* sizeof (FcCharLeaf
*));
1008 FcMemFree (FC_MEM_CHARSET
, b
->num
* sizeof (FcChar16
));
1011 FcMemFree (FC_MEM_CHARSET
, sizeof (FcCharSet
));
1018 FcNameParseCharSet (FcChar8
*string
)
1020 FcCharSet
*c
, *n
= 0;
1027 c
= FcCharSetCreate ();
1032 string
= FcCharSetParseValue (string
, &ucs4
);
1036 for (i
= 0; i
< 256/32; i
++)
1038 string
= FcCharSetParseValue (string
, &temp
.map
[i
]);
1041 bits
|= temp
.map
[i
];
1045 leaf
= FcCharSetFreezeLeaf (&temp
);
1048 if (!FcCharSetInsertLeaf (c
, ucs4
, leaf
))
1053 printf (" %8s %8s %8s %8s\n", "total", "totalmem", "new", "newmem");
1054 printf ("Leaves: %8d %8d %8d %8d\n",
1055 FcCharLeafTotal
, sizeof (FcCharLeaf
) * FcCharLeafTotal
,
1056 FcCharLeafUsed
, sizeof (FcCharLeaf
) * FcCharLeafUsed
);
1057 printf ("Charsets: %8d %8d %8d %8d\n",
1058 FcCharSetTotal
, sizeof (FcCharSet
) * FcCharSetTotal
,
1059 FcCharSetUsed
, sizeof (FcCharSet
) * FcCharSetUsed
);
1060 printf ("Tables: %8d %8d %8d %8d\n",
1061 FcCharSetTotalEnts
, FcCharSetTotalEnts
* (sizeof (FcCharLeaf
*) + sizeof (FcChar16
)),
1062 FcCharSetUsedEnts
, FcCharSetUsedEnts
* (sizeof (FcCharLeaf
*) + sizeof (FcChar16
)));
1063 printf ("Total: %8s %8d %8s %8d\n",
1065 sizeof (FcCharLeaf
) * FcCharLeafTotal
+
1066 sizeof (FcCharSet
) * FcCharSetTotal
+
1067 FcCharSetTotalEnts
* (sizeof (FcCharLeaf
*) + sizeof (FcChar16
)),
1069 sizeof (FcCharLeaf
) * FcCharLeafUsed
+
1070 sizeof (FcCharSet
) * FcCharSetUsed
+
1071 FcCharSetUsedEnts
* (sizeof (FcCharLeaf
*) + sizeof (FcChar16
)));
1073 n
= FcCharSetFreezeBase (c
);
1077 FcMemFree (FC_MEM_CHARSET
, c
->num
* sizeof (FcCharLeaf
*));
1082 FcMemFree (FC_MEM_CHARSET
, c
->num
* sizeof (FcChar16
));
1085 FcMemFree (FC_MEM_CHARSET
, sizeof (FcCharSet
));
1092 FcNameUnparseCharSet (FcStrBuf
*buf
, const FcCharSet
*c
)
1100 for (FcCharSetIterStart (c
, &ci
);
1102 FcCharSetIterNext (c
, &ci
))
1104 if (!FcCharSetUnparseValue (buf
, ci
.ucs4
))
1106 for (i
= 0; i
< 256/32; i
++)
1107 if (!FcCharSetUnparseValue (buf
, ci
.leaf
->map
[i
]))
1114 FcCharSetIter ci
, checki
;
1116 /* null terminate for parser */
1117 FcStrBufChar (buf
, '\0');
1118 /* step back over null for life after test */
1120 check
= FcNameParseCharSet (buf
->buf
+ len
);
1121 FcCharSetIterStart (c
, &ci
);
1122 FcCharSetIterStart (check
, &checki
);
1123 while (ci
.leaf
|| checki
.leaf
)
1125 if (ci
.ucs4
< checki
.ucs4
)
1127 printf ("Missing leaf node at 0x%x\n", ci
.ucs4
);
1128 FcCharSetIterNext (c
, &ci
);
1130 else if (checki
.ucs4
< ci
.ucs4
)
1132 printf ("Extra leaf node at 0x%x\n", checki
.ucs4
);
1133 FcCharSetIterNext (check
, &checki
);
1138 FcChar32
*cm
= ci
.leaf
->map
;
1139 FcChar32
*checkm
= checki
.leaf
->map
;
1141 for (i
= 0; i
< 256; i
+= 32)
1144 printf ("Mismatching sets at 0x%08x: 0x%08x != 0x%08x\n",
1145 ci
.ucs4
+ i
, *cm
, *checkm
);
1149 FcCharSetIterNext (c
, &ci
);
1150 FcCharSetIterNext (check
, &checki
);
1153 if ((missing
= FcCharSetSubtractCount (c
, check
)))
1154 printf ("%d missing in reparsed result\n", missing
);
1155 if ((missing
= FcCharSetSubtractCount (check
, c
)))
1156 printf ("%d extra in reparsed result\n", missing
);
1157 FcCharSetDestroy (check
);