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
));
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
)
600 * Check matching pages
604 FcChar32
*am
= a
->leaves
[ai
]->map
;
605 FcChar32
*bm
= b
->leaves
[bi
]->map
;
611 * Does am have any bits not in bm?
621 * Does a have any pages not in b?
628 int high
= b
->num
- 1;
631 * Search for page 'an' in 'b'
635 int mid
= (low
+ high
) >> 1;
636 bn
= b
->numbers
[mid
];
648 while (bi
< b
->num
&& b
->numbers
[bi
] < an
)
653 * did we look at every page?
659 * These two functions efficiently walk the entire charmap for
660 * other software (like pango) that want their own copy
664 FcCharSetNextPage (const FcCharSet
*a
,
665 FcChar32 map
[FC_CHARSET_MAP_SIZE
],
672 FcCharSetIterSet (a
, &ai
);
674 return FC_CHARSET_DONE
;
677 * Save current information
680 memcpy (map
, ai
.leaf
->map
, sizeof (ai
.leaf
->map
));
684 FcCharSetIterNext (a
, &ai
);
691 FcCharSetFirstPage (const FcCharSet
*a
,
692 FcChar32 map
[FC_CHARSET_MAP_SIZE
],
696 return FcCharSetNextPage (a
, map
, next
);
700 * old coverage API, rather hard to use correctly
703 FcCharSetCoverage (const FcCharSet
*a
, FcChar32 page
, FcChar32
*result
);
706 FcCharSetCoverage (const FcCharSet
*a
, FcChar32 page
, FcChar32
*result
)
711 FcCharSetIterSet (a
, &ai
);
714 memset (result
, '\0', 256 / 8);
719 memcpy (result
, ai
.leaf
->map
, sizeof (ai
.leaf
->map
));
720 FcCharSetIterNext (a
, &ai
);
727 * ASCII representation of charsets.
729 * Each leaf is represented as 9 32-bit values, the code of the first character followed
730 * by 8 32 bit values for the leaf itself. Each value is encoded as 5 ASCII characters,
731 * only 85 different values are used to avoid control characters as well as the other
732 * characters used to encode font names. 85**5 > 2^32 so things work out, but
733 * it's not exactly human readable output. As a special case, 0 is encoded as a space
736 static const unsigned char charToValue
[256] = {
737 /* "" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
738 /* "\b" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
739 /* "\020" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
740 /* "\030" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
741 /* " " */ 0xff, 0x00, 0xff, 0x01, 0x02, 0x03, 0x04, 0xff,
742 /* "(" */ 0x05, 0x06, 0x07, 0x08, 0xff, 0xff, 0x09, 0x0a,
743 /* "0" */ 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12,
744 /* "8" */ 0x13, 0x14, 0xff, 0x15, 0x16, 0xff, 0x17, 0x18,
745 /* "@" */ 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
746 /* "H" */ 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
747 /* "P" */ 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30,
748 /* "X" */ 0x31, 0x32, 0x33, 0x34, 0xff, 0x35, 0x36, 0xff,
749 /* "`" */ 0xff, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d,
750 /* "h" */ 0x3e, 0x3f, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45,
751 /* "p" */ 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d,
752 /* "x" */ 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53, 0x54, 0xff,
753 /* "\200" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
754 /* "\210" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
755 /* "\220" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
756 /* "\230" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
757 /* "\240" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
758 /* "\250" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
759 /* "\260" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
760 /* "\270" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
761 /* "\300" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
762 /* "\310" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
763 /* "\320" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
764 /* "\330" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
765 /* "\340" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
766 /* "\350" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
767 /* "\360" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
768 /* "\370" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
771 static const FcChar8 valueToChar
[0x55] = {
772 /* 0x00 */ '!', '#', '$', '%', '&', '(', ')', '*',
773 /* 0x08 */ '+', '.', '/', '0', '1', '2', '3', '4',
774 /* 0x10 */ '5', '6', '7', '8', '9', ';', '<', '>',
775 /* 0x18 */ '?', '@', 'A', 'B', 'C', 'D', 'E', 'F',
776 /* 0x20 */ 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
777 /* 0x28 */ 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
778 /* 0x30 */ 'W', 'X', 'Y', 'Z', '[', ']', '^', 'a',
779 /* 0x38 */ 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
780 /* 0x40 */ 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q',
781 /* 0x48 */ 'r', 's', 't', 'u', 'v', 'w', 'x', 'y',
782 /* 0x50 */ 'z', '{', '|', '}', '~',
786 FcCharSetParseValue (FcChar8
*string
, FcChar32
*value
)
800 for (i
= 0; i
< 5; i
++)
802 if (!(c
= (FcChar32
) (unsigned char) *string
++))
815 FcCharSetUnparseValue (FcStrBuf
*buf
, FcChar32 value
)
820 return FcStrBufChar (buf
, ' ');
825 FcChar8
*s
= string
+ 5;
827 for (i
= 0; i
< 5; i
++)
829 *--s
= valueToChar
[value
% 85];
832 for (i
= 0; i
< 5; i
++)
833 if (!FcStrBufChar (buf
, *s
++))
839 typedef struct _FcCharLeafEnt FcCharLeafEnt
;
841 struct _FcCharLeafEnt
{
847 #define FC_CHAR_LEAF_BLOCK (4096 / sizeof (FcCharLeafEnt))
848 static FcCharLeafEnt
**FcCharLeafBlocks
;
849 static int FcCharLeafBlockCount
;
851 static FcCharLeafEnt
*
852 FcCharLeafEntCreate (void)
854 static FcCharLeafEnt
*block
;
859 FcCharLeafEnt
**newBlocks
;
861 FcCharLeafBlockCount
++;
862 newBlocks
= realloc (FcCharLeafBlocks
, FcCharLeafBlockCount
* sizeof (FcCharLeafEnt
*));
865 FcCharLeafBlocks
= newBlocks
;
866 block
= FcCharLeafBlocks
[FcCharLeafBlockCount
-1] = malloc (FC_CHAR_LEAF_BLOCK
* sizeof (FcCharLeafEnt
));
869 FcMemAlloc (FC_MEM_CHARLEAF
, FC_CHAR_LEAF_BLOCK
* sizeof (FcCharLeafEnt
));
870 remain
= FC_CHAR_LEAF_BLOCK
;
876 #define FC_CHAR_LEAF_HASH_SIZE 257
879 FcCharLeafHash (FcCharLeaf
*leaf
)
884 for (i
= 0; i
< 256/32; i
++)
885 hash
= ((hash
<< 1) | (hash
>> 31)) ^ leaf
->map
[i
];
889 static int FcCharLeafTotal
;
890 static int FcCharLeafUsed
;
892 static FcCharLeafEnt
*FcCharLeafHashTable
[FC_CHAR_LEAF_HASH_SIZE
];
895 FcCharSetFreezeLeaf (FcCharLeaf
*leaf
)
897 FcChar32 hash
= FcCharLeafHash (leaf
);
898 FcCharLeafEnt
**bucket
= &FcCharLeafHashTable
[hash
% FC_CHAR_LEAF_HASH_SIZE
];
902 for (ent
= *bucket
; ent
; ent
= ent
->next
)
904 if (ent
->hash
== hash
&& !memcmp (&ent
->leaf
, leaf
, sizeof (FcCharLeaf
)))
908 ent
= FcCharLeafEntCreate();
920 FcCharSetThawAllLeaf (void)
924 for (i
= 0; i
< FC_CHAR_LEAF_HASH_SIZE
; i
++)
925 FcCharLeafHashTable
[i
] = 0;
930 for (i
= 0; i
< FcCharLeafBlockCount
; i
++)
931 free (FcCharLeafBlocks
[i
]);
933 free (FcCharLeafBlocks
);
934 FcCharLeafBlocks
= 0;
935 FcCharLeafBlockCount
= 0;
938 typedef struct _FcCharSetEnt FcCharSetEnt
;
940 struct _FcCharSetEnt
{
946 #define FC_CHAR_SET_HASH_SIZE 67
949 FcCharSetHash (FcCharSet
*fcs
)
956 p
= (FcChar32
*) fcs
->leaves
;
957 for (i
= 0; i
< fcs
->num
* sizeof (FcCharLeaf
*) / sizeof (FcChar32
); i
++)
958 hash
= ((hash
<< 1) | (hash
>> 31)) ^ *p
++;
959 /* hash in numbers */
960 for (i
= 0; i
< fcs
->num
; i
++)
961 hash
= ((hash
<< 1) | (hash
>> 31)) ^ fcs
->numbers
[i
];
965 static int FcCharSetTotal
;
966 static int FcCharSetUsed
;
967 static int FcCharSetTotalEnts
, FcCharSetUsedEnts
;
969 static FcCharSetEnt
*FcCharSetHashTable
[FC_CHAR_SET_HASH_SIZE
];
972 FcCharSetFreezeBase (FcCharSet
*fcs
)
974 FcChar32 hash
= FcCharSetHash (fcs
);
975 FcCharSetEnt
**bucket
= &FcCharSetHashTable
[hash
% FC_CHAR_SET_HASH_SIZE
];
980 FcCharSetTotalEnts
+= fcs
->num
;
981 for (ent
= *bucket
; ent
; ent
= ent
->next
)
983 if (ent
->hash
== hash
&&
984 ent
->set
.num
== fcs
->num
&&
985 !memcmp (ent
->set
.leaves
, fcs
->leaves
,
986 fcs
->num
* sizeof (FcCharLeaf
*)) &&
987 !memcmp (ent
->set
.numbers
, fcs
->numbers
,
988 fcs
->num
* sizeof (FcChar16
)))
994 size
= (sizeof (FcCharSetEnt
) +
995 fcs
->num
* sizeof (FcCharLeaf
*) +
996 fcs
->num
* sizeof (FcChar16
));
1000 FcMemAlloc (FC_MEM_CHARSET
, size
);
1002 FcCharSetUsedEnts
+= fcs
->num
;
1004 ent
->set
.ref
= FC_REF_CONSTANT
;
1005 ent
->set
.num
= fcs
->num
;
1008 ent
->set
.leaves
= (FcCharLeaf
**) (ent
+ 1);
1009 ent
->set
.numbers
= (FcChar16
*) (ent
->set
.leaves
+ fcs
->num
);
1010 memcpy (ent
->set
.leaves
, fcs
->leaves
, fcs
->num
* sizeof (FcCharLeaf
*));
1011 memcpy (ent
->set
.numbers
, fcs
->numbers
, fcs
->num
* sizeof (FcChar16
));
1015 ent
->set
.leaves
= 0;
1016 ent
->set
.numbers
= 0;
1020 ent
->next
= *bucket
;
1026 FcCharSetThawAll (void)
1029 FcCharSetEnt
*ent
, *next
;
1031 for (i
= 0; i
< FC_CHAR_SET_HASH_SIZE
; i
++)
1033 for (ent
= FcCharSetHashTable
[i
]; ent
; ent
= next
)
1038 FcCharSetHashTable
[i
] = 0;
1042 FcCharSetTotalEnts
= 0;
1044 FcCharSetUsedEnts
= 0;
1046 FcCharSetThawAllLeaf ();
1050 FcCharSetFreeze (FcCharSet
*fcs
)
1057 b
= FcCharSetCreate ();
1060 for (i
= 0; i
< fcs
->num
; i
++)
1062 l
= FcCharSetFreezeLeaf (fcs
->leaves
[i
]);
1065 if (!FcCharSetInsertLeaf (b
, fcs
->numbers
[i
] << 8, l
))
1068 n
= FcCharSetFreezeBase (b
);
1072 FcMemFree (FC_MEM_CHARSET
, b
->num
* sizeof (FcCharLeaf
*));
1077 FcMemFree (FC_MEM_CHARSET
, b
->num
* sizeof (FcChar16
));
1080 FcMemFree (FC_MEM_CHARSET
, sizeof (FcCharSet
));
1087 FcNameParseCharSet (FcChar8
*string
)
1089 FcCharSet
*c
, *n
= 0;
1096 c
= FcCharSetCreate ();
1101 string
= FcCharSetParseValue (string
, &ucs4
);
1105 for (i
= 0; i
< 256/32; i
++)
1107 string
= FcCharSetParseValue (string
, &temp
.map
[i
]);
1110 bits
|= temp
.map
[i
];
1114 leaf
= FcCharSetFreezeLeaf (&temp
);
1117 if (!FcCharSetInsertLeaf (c
, ucs4
, leaf
))
1122 printf (" %8s %8s %8s %8s\n", "total", "totalmem", "new", "newmem");
1123 printf ("Leaves: %8d %8d %8d %8d\n",
1124 FcCharLeafTotal
, sizeof (FcCharLeaf
) * FcCharLeafTotal
,
1125 FcCharLeafUsed
, sizeof (FcCharLeaf
) * FcCharLeafUsed
);
1126 printf ("Charsets: %8d %8d %8d %8d\n",
1127 FcCharSetTotal
, sizeof (FcCharSet
) * FcCharSetTotal
,
1128 FcCharSetUsed
, sizeof (FcCharSet
) * FcCharSetUsed
);
1129 printf ("Tables: %8d %8d %8d %8d\n",
1130 FcCharSetTotalEnts
, FcCharSetTotalEnts
* (sizeof (FcCharLeaf
*) + sizeof (FcChar16
)),
1131 FcCharSetUsedEnts
, FcCharSetUsedEnts
* (sizeof (FcCharLeaf
*) + sizeof (FcChar16
)));
1132 printf ("Total: %8s %8d %8s %8d\n",
1134 sizeof (FcCharLeaf
) * FcCharLeafTotal
+
1135 sizeof (FcCharSet
) * FcCharSetTotal
+
1136 FcCharSetTotalEnts
* (sizeof (FcCharLeaf
*) + sizeof (FcChar16
)),
1138 sizeof (FcCharLeaf
) * FcCharLeafUsed
+
1139 sizeof (FcCharSet
) * FcCharSetUsed
+
1140 FcCharSetUsedEnts
* (sizeof (FcCharLeaf
*) + sizeof (FcChar16
)));
1142 n
= FcCharSetFreezeBase (c
);
1146 FcMemFree (FC_MEM_CHARSET
, c
->num
* sizeof (FcCharLeaf
*));
1151 FcMemFree (FC_MEM_CHARSET
, c
->num
* sizeof (FcChar16
));
1154 FcMemFree (FC_MEM_CHARSET
, sizeof (FcCharSet
));
1161 FcNameUnparseCharSet (FcStrBuf
*buf
, const FcCharSet
*c
)
1169 for (FcCharSetIterStart (c
, &ci
);
1171 FcCharSetIterNext (c
, &ci
))
1173 if (!FcCharSetUnparseValue (buf
, ci
.ucs4
))
1175 for (i
= 0; i
< 256/32; i
++)
1176 if (!FcCharSetUnparseValue (buf
, ci
.leaf
->map
[i
]))
1183 FcCharSetIter ci
, checki
;
1185 /* null terminate for parser */
1186 FcStrBufChar (buf
, '\0');
1187 /* step back over null for life after test */
1189 check
= FcNameParseCharSet (buf
->buf
+ len
);
1190 FcCharSetIterStart (c
, &ci
);
1191 FcCharSetIterStart (check
, &checki
);
1192 while (ci
.leaf
|| checki
.leaf
)
1194 if (ci
.ucs4
< checki
.ucs4
)
1196 printf ("Missing leaf node at 0x%x\n", ci
.ucs4
);
1197 FcCharSetIterNext (c
, &ci
);
1199 else if (checki
.ucs4
< ci
.ucs4
)
1201 printf ("Extra leaf node at 0x%x\n", checki
.ucs4
);
1202 FcCharSetIterNext (check
, &checki
);
1207 FcChar32
*cm
= ci
.leaf
->map
;
1208 FcChar32
*checkm
= checki
.leaf
->map
;
1210 for (i
= 0; i
< 256; i
+= 32)
1213 printf ("Mismatching sets at 0x%08x: 0x%08x != 0x%08x\n",
1214 ci
.ucs4
+ i
, *cm
, *checkm
);
1218 FcCharSetIterNext (c
, &ci
);
1219 FcCharSetIterNext (check
, &checki
);
1222 if ((missing
= FcCharSetSubtractCount (c
, check
)))
1223 printf ("%d missing in reparsed result\n", missing
);
1224 if ((missing
= FcCharSetSubtractCount (check
, c
)))
1225 printf ("%d extra in reparsed result\n", missing
);
1226 FcCharSetDestroy (check
);