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.
31 FcCharSetCreate (void)
35 fcs
= (FcCharSet
*) malloc (sizeof (FcCharSet
));
38 FcMemAlloc (FC_MEM_CHARSET
, sizeof (FcCharSet
));
41 fcs
->leaves_offset
= 0;
42 fcs
->numbers_offset
= 0;
49 return FcCharSetCreate ();
53 FcCharSetDestroy (FcCharSet
*fcs
)
57 if (fcs
->ref
== FC_REF_CONSTANT
)
61 for (i
= 0; i
< fcs
->num
; i
++)
63 FcMemFree (FC_MEM_CHARLEAF
, sizeof (FcCharLeaf
));
64 free (FcCharSetLeaf (fcs
, i
));
68 FcMemFree (FC_MEM_CHARSET
, fcs
->num
* sizeof (intptr_t));
69 free (FcCharSetLeaves (fcs
));
73 FcMemFree (FC_MEM_CHARSET
, fcs
->num
* sizeof (FcChar16
));
74 free (FcCharSetNumbers (fcs
));
76 FcMemFree (FC_MEM_CHARSET
, sizeof (FcCharSet
));
81 * Locate the leaf containing the specified char, return
82 * its index if it exists, otherwise return negative of
83 * the (position + 1) where it should be inserted
87 FcCharSetFindLeafPos (const FcCharSet
*fcs
, FcChar32 ucs4
)
89 FcChar16
*numbers
= FcCharSetNumbers(fcs
);
92 int high
= fcs
->num
- 1;
99 int mid
= (low
+ high
) >> 1;
108 if (high
< 0 || (high
< fcs
->num
&& numbers
[high
] < ucs4
))
114 FcCharSetFindLeaf (const FcCharSet
*fcs
, FcChar32 ucs4
)
116 int pos
= FcCharSetFindLeafPos (fcs
, ucs4
);
118 return FcCharSetLeaf(fcs
, pos
);
123 FcCharSetPutLeaf (FcCharSet
*fcs
,
128 intptr_t *leaves
= FcCharSetLeaves (fcs
);
129 FcChar16
*numbers
= FcCharSetNumbers (fcs
);
135 leaves
= malloc (sizeof (*leaves
));
138 intptr_t *new_leaves
= realloc (leaves
, (fcs
->num
+ 1) *
140 intptr_t distance
= (intptr_t) new_leaves
- (intptr_t) leaves
;
142 if (new_leaves
&& distance
)
146 for (i
= 0; i
< fcs
->num
; i
++)
147 new_leaves
[i
] -= distance
;
155 FcMemFree (FC_MEM_CHARSET
, fcs
->num
* sizeof (intptr_t));
156 FcMemAlloc (FC_MEM_CHARSET
, (fcs
->num
+ 1) * sizeof (intptr_t));
157 fcs
->leaves_offset
= FcPtrToOffset (fcs
, leaves
);
160 numbers
= malloc (sizeof (FcChar16
));
162 numbers
= realloc (numbers
, (fcs
->num
+ 1) * sizeof (FcChar16
));
167 FcMemFree (FC_MEM_CHARSET
, fcs
->num
* sizeof (FcChar16
));
168 FcMemAlloc (FC_MEM_CHARSET
, (fcs
->num
+ 1) * sizeof (FcChar16
));
169 fcs
->numbers_offset
= FcPtrToOffset (fcs
, numbers
);
171 memmove (leaves
+ pos
+ 1, leaves
+ pos
,
172 (fcs
->num
- pos
) * sizeof (*leaves
));
173 memmove (numbers
+ pos
+ 1, numbers
+ pos
,
174 (fcs
->num
- pos
) * sizeof (*numbers
));
175 numbers
[pos
] = (FcChar16
) ucs4
;
176 leaves
[pos
] = FcPtrToOffset (leaves
, leaf
);
182 * Locate the leaf containing the specified char, creating it
187 FcCharSetFindLeafCreate (FcCharSet
*fcs
, FcChar32 ucs4
)
192 pos
= FcCharSetFindLeafPos (fcs
, ucs4
);
194 return FcCharSetLeaf(fcs
, pos
);
196 leaf
= calloc (1, sizeof (FcCharLeaf
));
201 if (!FcCharSetPutLeaf (fcs
, ucs4
, leaf
, pos
))
206 FcMemAlloc (FC_MEM_CHARLEAF
, sizeof (FcCharLeaf
));
211 FcCharSetInsertLeaf (FcCharSet
*fcs
, FcChar32 ucs4
, FcCharLeaf
*leaf
)
215 pos
= FcCharSetFindLeafPos (fcs
, ucs4
);
218 FcMemFree (FC_MEM_CHARLEAF
, sizeof (FcCharLeaf
));
219 free (FcCharSetLeaf (fcs
, pos
));
220 FcCharSetLeaves(fcs
)[pos
] = FcPtrToOffset (FcCharSetLeaves(fcs
),
225 return FcCharSetPutLeaf (fcs
, ucs4
, leaf
, pos
);
229 FcCharSetAddChar (FcCharSet
*fcs
, FcChar32 ucs4
)
234 if (fcs
->ref
== FC_REF_CONSTANT
)
236 leaf
= FcCharSetFindLeafCreate (fcs
, ucs4
);
239 b
= &leaf
->map
[(ucs4
& 0xff) >> 5];
240 *b
|= (1 << (ucs4
& 0x1f));
245 * An iterator for the leaves of a charset
248 typedef struct _fcCharSetIter
{
255 * Set iter->leaf to the leaf containing iter->ucs4 or higher
259 FcCharSetIterSet (const FcCharSet
*fcs
, FcCharSetIter
*iter
)
261 int pos
= FcCharSetFindLeafPos (fcs
, iter
->ucs4
);
272 iter
->ucs4
= (FcChar32
) FcCharSetNumbers(fcs
)[pos
] << 8;
274 iter
->leaf
= FcCharSetLeaf(fcs
, pos
);
279 FcCharSetIterNext (const FcCharSet
*fcs
, FcCharSetIter
*iter
)
281 int pos
= iter
->pos
+ 1;
289 iter
->ucs4
= (FcChar32
) FcCharSetNumbers(fcs
)[pos
] << 8;
290 iter
->leaf
= FcCharSetLeaf(fcs
, pos
);
297 FcCharSetIterStart (const FcCharSet
*fcs
, FcCharSetIter
*iter
)
301 FcCharSetIterSet (fcs
, iter
);
305 FcCharSetCopy (FcCharSet
*src
)
307 if (src
->ref
!= FC_REF_CONSTANT
)
313 FcCharSetEqual (const FcCharSet
*a
, const FcCharSet
*b
)
315 FcCharSetIter ai
, bi
;
320 for (FcCharSetIterStart (a
, &ai
), FcCharSetIterStart (b
, &bi
);
322 FcCharSetIterNext (a
, &ai
), FcCharSetIterNext (b
, &bi
))
324 if (ai
.ucs4
!= bi
.ucs4
)
326 for (i
= 0; i
< 256/32; i
++)
327 if (ai
.leaf
->map
[i
] != bi
.leaf
->map
[i
])
330 return ai
.leaf
== bi
.leaf
;
334 FcCharSetAddLeaf (FcCharSet
*fcs
,
338 FcCharLeaf
*new = FcCharSetFindLeafCreate (fcs
, ucs4
);
346 FcCharSetOperate (const FcCharSet
*a
,
348 FcBool (*overlap
) (FcCharLeaf
*result
,
349 const FcCharLeaf
*al
,
350 const FcCharLeaf
*bl
),
355 FcCharSetIter ai
, bi
;
357 fcs
= FcCharSetCreate ();
360 FcCharSetIterStart (a
, &ai
);
361 FcCharSetIterStart (b
, &bi
);
362 while ((ai
.leaf
|| (bonly
&& bi
.leaf
)) && (bi
.leaf
|| (aonly
&& ai
.leaf
)))
364 if (ai
.ucs4
< bi
.ucs4
)
368 if (!FcCharSetAddLeaf (fcs
, ai
.ucs4
, ai
.leaf
))
370 FcCharSetIterNext (a
, &ai
);
375 FcCharSetIterSet (a
, &ai
);
378 else if (bi
.ucs4
< ai
.ucs4
)
382 if (!FcCharSetAddLeaf (fcs
, bi
.ucs4
, bi
.leaf
))
384 FcCharSetIterNext (b
, &bi
);
389 FcCharSetIterSet (b
, &bi
);
396 if ((*overlap
) (&leaf
, ai
.leaf
, bi
.leaf
))
398 if (!FcCharSetAddLeaf (fcs
, ai
.ucs4
, &leaf
))
401 FcCharSetIterNext (a
, &ai
);
402 FcCharSetIterNext (b
, &bi
);
407 FcCharSetDestroy (fcs
);
413 FcCharSetIntersectLeaf (FcCharLeaf
*result
,
414 const FcCharLeaf
*al
,
415 const FcCharLeaf
*bl
)
418 FcBool nonempty
= FcFalse
;
420 for (i
= 0; i
< 256/32; i
++)
421 if ((result
->map
[i
] = al
->map
[i
] & bl
->map
[i
]))
427 FcCharSetIntersect (const FcCharSet
*a
, const FcCharSet
*b
)
429 return FcCharSetOperate (a
, b
, FcCharSetIntersectLeaf
, FcFalse
, FcFalse
);
433 FcCharSetUnionLeaf (FcCharLeaf
*result
,
434 const FcCharLeaf
*al
,
435 const FcCharLeaf
*bl
)
439 for (i
= 0; i
< 256/32; i
++)
440 result
->map
[i
] = al
->map
[i
] | bl
->map
[i
];
445 FcCharSetUnion (const FcCharSet
*a
, const FcCharSet
*b
)
447 return FcCharSetOperate (a
, b
, FcCharSetUnionLeaf
, FcTrue
, FcTrue
);
451 FcCharSetSubtractLeaf (FcCharLeaf
*result
,
452 const FcCharLeaf
*al
,
453 const FcCharLeaf
*bl
)
456 FcBool nonempty
= FcFalse
;
458 for (i
= 0; i
< 256/32; i
++)
459 if ((result
->map
[i
] = al
->map
[i
] & ~bl
->map
[i
]))
465 FcCharSetSubtract (const FcCharSet
*a
, const FcCharSet
*b
)
467 return FcCharSetOperate (a
, b
, FcCharSetSubtractLeaf
, FcTrue
, FcFalse
);
471 FcCharSetHasChar (const FcCharSet
*fcs
, FcChar32 ucs4
)
473 FcCharLeaf
*leaf
= FcCharSetFindLeaf (fcs
, ucs4
);
476 return (leaf
->map
[(ucs4
& 0xff) >> 5] & (1 << (ucs4
& 0x1f))) != 0;
480 FcCharSetPopCount (FcChar32 c1
)
483 FcChar32 c2
= (c1
>> 1) & 033333333333;
484 c2
= c1
- c2
- ((c2
>> 1) & 033333333333);
485 return (((c2
+ (c2
>> 3)) & 030707070707) % 077);
489 FcCharSetIntersectCount (const FcCharSet
*a
, const FcCharSet
*b
)
491 FcCharSetIter ai
, bi
;
494 FcCharSetIterStart (a
, &ai
);
495 FcCharSetIterStart (b
, &bi
);
496 while (ai
.leaf
&& bi
.leaf
)
498 if (ai
.ucs4
== bi
.ucs4
)
500 FcChar32
*am
= ai
.leaf
->map
;
501 FcChar32
*bm
= bi
.leaf
->map
;
504 count
+= FcCharSetPopCount (*am
++ & *bm
++);
505 FcCharSetIterNext (a
, &ai
);
507 else if (ai
.ucs4
< bi
.ucs4
)
510 FcCharSetIterSet (a
, &ai
);
512 if (bi
.ucs4
< ai
.ucs4
)
515 FcCharSetIterSet (b
, &bi
);
522 FcCharSetCount (const FcCharSet
*a
)
527 for (FcCharSetIterStart (a
, &ai
); ai
.leaf
; FcCharSetIterNext (a
, &ai
))
530 FcChar32
*am
= ai
.leaf
->map
;
533 count
+= FcCharSetPopCount (*am
++);
539 FcCharSetSubtractCount (const FcCharSet
*a
, const FcCharSet
*b
)
541 FcCharSetIter ai
, bi
;
544 FcCharSetIterStart (a
, &ai
);
545 FcCharSetIterStart (b
, &bi
);
548 if (ai
.ucs4
<= bi
.ucs4
)
550 FcChar32
*am
= ai
.leaf
->map
;
552 if (ai
.ucs4
== bi
.ucs4
)
554 FcChar32
*bm
= bi
.leaf
->map
;
556 count
+= FcCharSetPopCount (*am
++ & ~*bm
++);
561 count
+= FcCharSetPopCount (*am
++);
563 FcCharSetIterNext (a
, &ai
);
568 FcCharSetIterSet (b
, &bi
);
575 * return FcTrue iff a is a subset of b
578 FcCharSetIsSubset (const FcCharSet
*a
, const FcCharSet
*b
)
583 if (a
== b
) return FcTrue
;
586 while (ai
< a
->num
&& bi
< b
->num
)
588 an
= FcCharSetNumbers(a
)[ai
];
589 bn
= FcCharSetNumbers(b
)[bi
];
591 * Check matching pages
595 FcChar32
*am
= FcCharSetLeaf(a
, ai
)->map
;
596 FcChar32
*bm
= FcCharSetLeaf(b
, bi
)->map
;
602 * Does am have any bits not in bm?
612 * Does a have any pages not in b?
619 int high
= b
->num
- 1;
622 * Search for page 'an' in 'b'
626 int mid
= (low
+ high
) >> 1;
627 bn
= FcCharSetNumbers(b
)[mid
];
639 while (bi
< b
->num
&& FcCharSetNumbers(b
)[bi
] < an
)
644 * did we look at every page?
650 * These two functions efficiently walk the entire charmap for
651 * other software (like pango) that want their own copy
655 FcCharSetNextPage (const FcCharSet
*a
,
656 FcChar32 map
[FC_CHARSET_MAP_SIZE
],
663 FcCharSetIterSet (a
, &ai
);
665 return FC_CHARSET_DONE
;
668 * Save current information
671 memcpy (map
, ai
.leaf
->map
, sizeof (ai
.leaf
->map
));
675 FcCharSetIterNext (a
, &ai
);
682 FcCharSetFirstPage (const FcCharSet
*a
,
683 FcChar32 map
[FC_CHARSET_MAP_SIZE
],
687 return FcCharSetNextPage (a
, map
, next
);
691 * old coverage API, rather hard to use correctly
695 FcCharSetCoverage (const FcCharSet
*a
, FcChar32 page
, FcChar32
*result
)
700 FcCharSetIterSet (a
, &ai
);
703 memset (result
, '\0', 256 / 8);
708 memcpy (result
, ai
.leaf
->map
, sizeof (ai
.leaf
->map
));
709 FcCharSetIterNext (a
, &ai
);
716 * ASCII representation of charsets.
718 * Each leaf is represented as 9 32-bit values, the code of the first character followed
719 * by 8 32 bit values for the leaf itself. Each value is encoded as 5 ASCII characters,
720 * only 85 different values are used to avoid control characters as well as the other
721 * characters used to encode font names. 85**5 > 2^32 so things work out, but
722 * it's not exactly human readable output. As a special case, 0 is encoded as a space
725 static const unsigned char charToValue
[256] = {
726 /* "" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
727 /* "\b" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
728 /* "\020" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
729 /* "\030" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
730 /* " " */ 0xff, 0x00, 0xff, 0x01, 0x02, 0x03, 0x04, 0xff,
731 /* "(" */ 0x05, 0x06, 0x07, 0x08, 0xff, 0xff, 0x09, 0x0a,
732 /* "0" */ 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12,
733 /* "8" */ 0x13, 0x14, 0xff, 0x15, 0x16, 0xff, 0x17, 0x18,
734 /* "@" */ 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
735 /* "H" */ 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
736 /* "P" */ 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30,
737 /* "X" */ 0x31, 0x32, 0x33, 0x34, 0xff, 0x35, 0x36, 0xff,
738 /* "`" */ 0xff, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d,
739 /* "h" */ 0x3e, 0x3f, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45,
740 /* "p" */ 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d,
741 /* "x" */ 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53, 0x54, 0xff,
742 /* "\200" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
743 /* "\210" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
744 /* "\220" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
745 /* "\230" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
746 /* "\240" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
747 /* "\250" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
748 /* "\260" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
749 /* "\270" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
750 /* "\300" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
751 /* "\310" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
752 /* "\320" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
753 /* "\330" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
754 /* "\340" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
755 /* "\350" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
756 /* "\360" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
757 /* "\370" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
760 static const FcChar8 valueToChar
[0x55] = {
761 /* 0x00 */ '!', '#', '$', '%', '&', '(', ')', '*',
762 /* 0x08 */ '+', '.', '/', '0', '1', '2', '3', '4',
763 /* 0x10 */ '5', '6', '7', '8', '9', ';', '<', '>',
764 /* 0x18 */ '?', '@', 'A', 'B', 'C', 'D', 'E', 'F',
765 /* 0x20 */ 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
766 /* 0x28 */ 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
767 /* 0x30 */ 'W', 'X', 'Y', 'Z', '[', ']', '^', 'a',
768 /* 0x38 */ 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
769 /* 0x40 */ 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q',
770 /* 0x48 */ 'r', 's', 't', 'u', 'v', 'w', 'x', 'y',
771 /* 0x50 */ 'z', '{', '|', '}', '~',
775 FcCharSetParseValue (FcChar8
*string
, FcChar32
*value
)
789 for (i
= 0; i
< 5; i
++)
791 if (!(c
= (FcChar32
) (unsigned char) *string
++))
804 FcCharSetUnparseValue (FcStrBuf
*buf
, FcChar32 value
)
809 return FcStrBufChar (buf
, ' ');
814 FcChar8
*s
= string
+ 5;
816 for (i
= 0; i
< 5; i
++)
818 *--s
= valueToChar
[value
% 85];
821 for (i
= 0; i
< 5; i
++)
822 if (!FcStrBufChar (buf
, *s
++))
829 FcNameParseCharSet (FcChar8
*string
)
838 c
= FcCharSetCreate ();
843 string
= FcCharSetParseValue (string
, &ucs4
);
847 for (i
= 0; i
< 256/32; i
++)
849 string
= FcCharSetParseValue (string
, &temp
.map
[i
]);
856 leaf
= malloc (sizeof (FcCharLeaf
));
860 if (!FcCharSetInsertLeaf (c
, ucs4
, leaf
))
868 FcMemFree (FC_MEM_CHARSET
, c
->num
* sizeof (FcCharLeaf
*));
869 free (FcCharSetLeaves (c
));
873 FcMemFree (FC_MEM_CHARSET
, c
->num
* sizeof (FcChar16
));
874 free (FcCharSetNumbers (c
));
876 FcMemFree (FC_MEM_CHARSET
, sizeof (FcCharSet
));
883 FcNameUnparseCharSet (FcStrBuf
*buf
, const FcCharSet
*c
)
891 for (FcCharSetIterStart (c
, &ci
);
893 FcCharSetIterNext (c
, &ci
))
895 if (!FcCharSetUnparseValue (buf
, ci
.ucs4
))
897 for (i
= 0; i
< 256/32; i
++)
898 if (!FcCharSetUnparseValue (buf
, ci
.leaf
->map
[i
]))
905 FcCharSetIter ci
, checki
;
907 /* null terminate for parser */
908 FcStrBufChar (buf
, '\0');
909 /* step back over null for life after test */
911 check
= FcNameParseCharSet (buf
->buf
+ len
);
912 FcCharSetIterStart (c
, &ci
);
913 FcCharSetIterStart (check
, &checki
);
914 while (ci
.leaf
|| checki
.leaf
)
916 if (ci
.ucs4
< checki
.ucs4
)
918 printf ("Missing leaf node at 0x%x\n", ci
.ucs4
);
919 FcCharSetIterNext (c
, &ci
);
921 else if (checki
.ucs4
< ci
.ucs4
)
923 printf ("Extra leaf node at 0x%x\n", checki
.ucs4
);
924 FcCharSetIterNext (check
, &checki
);
929 FcChar32
*cm
= ci
.leaf
->map
;
930 FcChar32
*checkm
= checki
.leaf
->map
;
932 for (i
= 0; i
< 256; i
+= 32)
935 printf ("Mismatching sets at 0x%08x: 0x%08x != 0x%08x\n",
936 ci
.ucs4
+ i
, *cm
, *checkm
);
940 FcCharSetIterNext (c
, &ci
);
941 FcCharSetIterNext (check
, &checki
);
944 if ((missing
= FcCharSetSubtractCount (c
, check
)))
945 printf ("%d missing in reparsed result\n", missing
);
946 if ((missing
= FcCharSetSubtractCount (check
, c
)))
947 printf ("%d extra in reparsed result\n", missing
);
948 FcCharSetDestroy (check
);
955 typedef struct _FcCharLeafEnt FcCharLeafEnt
;
957 struct _FcCharLeafEnt
{
963 #define FC_CHAR_LEAF_BLOCK (4096 / sizeof (FcCharLeafEnt))
964 #define FC_CHAR_LEAF_HASH_SIZE 257
966 typedef struct _FcCharSetEnt FcCharSetEnt
;
968 struct _FcCharSetEnt
{
974 typedef struct _FcCharSetOrigEnt FcCharSetOrigEnt
;
976 struct _FcCharSetOrigEnt
{
977 FcCharSetOrigEnt
*next
;
978 const FcCharSet
*orig
;
979 const FcCharSet
*frozen
;
982 #define FC_CHAR_SET_HASH_SIZE 67
984 struct _FcCharSetFreezer
{
985 FcCharLeafEnt
*leaf_hash_table
[FC_CHAR_LEAF_HASH_SIZE
];
986 FcCharLeafEnt
**leaf_blocks
;
987 int leaf_block_count
;
988 FcCharSetEnt
*set_hash_table
[FC_CHAR_SET_HASH_SIZE
];
989 FcCharSetOrigEnt
*orig_hash_table
[FC_CHAR_SET_HASH_SIZE
];
990 FcCharLeafEnt
*current_block
;
994 int leaves_allocated
;
995 int charsets_allocated
;
998 static FcCharLeafEnt
*
999 FcCharLeafEntCreate (FcCharSetFreezer
*freezer
)
1001 if (!freezer
->leaf_remain
)
1003 FcCharLeafEnt
**newBlocks
;
1005 freezer
->leaf_block_count
++;
1006 newBlocks
= realloc (freezer
->leaf_blocks
, freezer
->leaf_block_count
* sizeof (FcCharLeafEnt
*));
1009 freezer
->leaf_blocks
= newBlocks
;
1010 freezer
->current_block
= freezer
->leaf_blocks
[freezer
->leaf_block_count
-1] = malloc (FC_CHAR_LEAF_BLOCK
* sizeof (FcCharLeafEnt
));
1011 if (!freezer
->current_block
)
1013 FcMemAlloc (FC_MEM_CHARLEAF
, FC_CHAR_LEAF_BLOCK
* sizeof (FcCharLeafEnt
));
1014 freezer
->leaf_remain
= FC_CHAR_LEAF_BLOCK
;
1016 freezer
->leaf_remain
--;
1017 freezer
->leaves_allocated
++;
1018 return freezer
->current_block
++;
1022 FcCharLeafHash (FcCharLeaf
*leaf
)
1027 for (i
= 0; i
< 256/32; i
++)
1028 hash
= ((hash
<< 1) | (hash
>> 31)) ^ leaf
->map
[i
];
1033 FcCharSetFreezeLeaf (FcCharSetFreezer
*freezer
, FcCharLeaf
*leaf
)
1035 FcChar32 hash
= FcCharLeafHash (leaf
);
1036 FcCharLeafEnt
**bucket
= &freezer
->leaf_hash_table
[hash
% FC_CHAR_LEAF_HASH_SIZE
];
1039 for (ent
= *bucket
; ent
; ent
= ent
->next
)
1041 if (ent
->hash
== hash
&& !memcmp (&ent
->leaf
, leaf
, sizeof (FcCharLeaf
)))
1045 ent
= FcCharLeafEntCreate(freezer
);
1050 ent
->next
= *bucket
;
1056 FcCharSetHash (FcCharSet
*fcs
)
1061 /* hash in leaves */
1062 for (i
= 0; i
< fcs
->num
* (int) (sizeof (FcCharLeaf
*) / sizeof (FcChar32
)); i
++)
1063 hash
= ((hash
<< 1) | (hash
>> 31)) ^ (FcChar32
)(FcCharSetLeaf(fcs
, i
)->map
);
1064 /* hash in numbers */
1065 for (i
= 0; i
< fcs
->num
; i
++)
1066 hash
= ((hash
<< 1) | (hash
>> 31)) ^ *FcCharSetNumbers(fcs
);
1071 FcCharSetFreezeOrig (FcCharSetFreezer
*freezer
, const FcCharSet
*orig
, const FcCharSet
*frozen
)
1073 FcCharSetOrigEnt
**bucket
= &freezer
->orig_hash_table
[((uintptr_t) orig
) & FC_CHAR_SET_HASH_SIZE
];
1074 FcCharSetOrigEnt
*ent
;
1076 ent
= malloc (sizeof (FcCharSetOrigEnt
));
1080 ent
->frozen
= frozen
;
1081 ent
->next
= *bucket
;
1087 FcCharSetFreezeBase (FcCharSetFreezer
*freezer
, FcCharSet
*fcs
, const FcCharSet
*orig
)
1089 FcChar32 hash
= FcCharSetHash (fcs
);
1090 FcCharSetEnt
**bucket
= &freezer
->set_hash_table
[hash
% FC_CHAR_SET_HASH_SIZE
];
1095 for (ent
= *bucket
; ent
; ent
= ent
->next
)
1097 if (ent
->hash
== hash
&&
1098 ent
->set
.num
== fcs
->num
&&
1099 !memcmp (FcCharSetNumbers(&ent
->set
),
1100 FcCharSetNumbers(fcs
),
1101 fcs
->num
* sizeof (FcChar16
)))
1106 for (i
= 0; i
< fcs
->num
; i
++)
1107 if (FcCharSetLeaf(&ent
->set
, i
) != FcCharSetLeaf(fcs
, i
))
1114 size
= (sizeof (FcCharSetEnt
) +
1115 fcs
->num
* sizeof (FcCharLeaf
*) +
1116 fcs
->num
* sizeof (FcChar16
));
1117 ent
= malloc (size
);
1120 FcMemAlloc (FC_MEM_CHARSET
, size
);
1122 freezer
->charsets_allocated
++;
1124 ent
->set
.ref
= FC_REF_CONSTANT
;
1125 ent
->set
.num
= fcs
->num
;
1128 intptr_t *ent_leaves
;
1130 ent
->set
.leaves_offset
= sizeof (ent
->set
);
1131 ent
->set
.numbers_offset
= (ent
->set
.leaves_offset
+
1132 fcs
->num
* sizeof (intptr_t));
1134 ent_leaves
= FcCharSetLeaves (&ent
->set
);
1135 for (i
= 0; i
< fcs
->num
; i
++)
1136 ent_leaves
[i
] = FcPtrToOffset (ent_leaves
,
1137 FcCharSetLeaf (fcs
, i
));
1138 memcpy (FcCharSetNumbers (&ent
->set
),
1139 FcCharSetNumbers (fcs
),
1140 fcs
->num
* sizeof (FcChar16
));
1144 ent
->set
.leaves_offset
= 0;
1145 ent
->set
.numbers_offset
= 0;
1149 ent
->next
= *bucket
;
1155 static const FcCharSet
*
1156 FcCharSetFindFrozen (FcCharSetFreezer
*freezer
, const FcCharSet
*orig
)
1158 FcCharSetOrigEnt
**bucket
= &freezer
->orig_hash_table
[((uintptr_t) orig
) & FC_CHAR_SET_HASH_SIZE
];
1159 FcCharSetOrigEnt
*ent
;
1161 for (ent
= *bucket
; ent
; ent
= ent
->next
)
1162 if (ent
->orig
== orig
)
1168 FcCharSetFreeze (FcCharSetFreezer
*freezer
, const FcCharSet
*fcs
)
1171 const FcCharSet
*n
= 0;
1175 b
= FcCharSetCreate ();
1178 for (i
= 0; i
< fcs
->num
; i
++)
1180 l
= FcCharSetFreezeLeaf (freezer
, FcCharSetLeaf(fcs
, i
));
1183 if (!FcCharSetInsertLeaf (b
, FcCharSetNumbers(fcs
)[i
] << 8, l
))
1186 n
= FcCharSetFreezeBase (freezer
, b
, fcs
);
1187 if (!FcCharSetFreezeOrig (freezer
, fcs
, n
))
1192 freezer
->charsets_seen
++;
1193 freezer
->leaves_seen
+= fcs
->num
;
1197 FcMemFree (FC_MEM_CHARSET
, b
->num
* sizeof (FcCharLeaf
*));
1198 free (FcCharSetLeaves (b
));
1202 FcMemFree (FC_MEM_CHARSET
, b
->num
* sizeof (FcChar16
));
1203 free (FcCharSetNumbers (b
));
1205 FcMemFree (FC_MEM_CHARSET
, sizeof (FcCharSet
));
1212 FcCharSetFreezerCreate (void)
1214 FcCharSetFreezer
*freezer
;
1216 freezer
= calloc (1, sizeof (FcCharSetFreezer
));
1221 FcCharSetFreezerDestroy (FcCharSetFreezer
*freezer
)
1225 if (FcDebug() & FC_DBG_CACHE
)
1227 printf ("\ncharsets %d -> %d leaves %d -> %d\n",
1228 freezer
->charsets_seen
, freezer
->charsets_allocated
,
1229 freezer
->leaves_seen
, freezer
->leaves_allocated
);
1231 for (i
= 0; i
< FC_CHAR_SET_HASH_SIZE
; i
++)
1233 FcCharSetEnt
*ent
, *next
;
1234 for (ent
= freezer
->set_hash_table
[i
]; ent
; ent
= next
)
1241 for (i
= 0; i
< FC_CHAR_SET_HASH_SIZE
; i
++)
1243 FcCharSetOrigEnt
*ent
, *next
;
1244 for (ent
= freezer
->orig_hash_table
[i
]; ent
; ent
= next
)
1251 for (i
= 0; i
< freezer
->leaf_block_count
; i
++)
1252 free (freezer
->leaf_blocks
[i
]);
1254 free (freezer
->leaf_blocks
);
1259 FcCharSetSerializeAlloc (FcSerialize
*serialize
, const FcCharSet
*cs
)
1265 if (cs
->ref
!= FC_REF_CONSTANT
)
1267 if (!serialize
->cs_freezer
)
1269 serialize
->cs_freezer
= FcCharSetFreezerCreate ();
1270 if (!serialize
->cs_freezer
)
1273 if (FcCharSetFindFrozen (serialize
->cs_freezer
, cs
))
1276 cs
= FcCharSetFreeze (serialize
->cs_freezer
, cs
);
1279 leaves
= FcCharSetLeaves (cs
);
1280 numbers
= FcCharSetNumbers (cs
);
1282 if (!FcSerializeAlloc (serialize
, cs
, sizeof (FcCharSet
)))
1284 if (!FcSerializeAlloc (serialize
, leaves
, cs
->num
* sizeof (intptr_t)))
1286 if (!FcSerializeAlloc (serialize
, numbers
, cs
->num
* sizeof (FcChar16
)))
1288 for (i
= 0; i
< cs
->num
; i
++)
1289 if (!FcSerializeAlloc (serialize
, FcCharSetLeaf(cs
, i
),
1290 sizeof (FcCharLeaf
)))
1296 FcCharSetSerialize(FcSerialize
*serialize
, const FcCharSet
*cs
)
1298 FcCharSet
*cs_serialized
;
1299 intptr_t *leaves
, *leaves_serialized
;
1300 FcChar16
*numbers
, *numbers_serialized
;
1301 FcCharLeaf
*leaf
, *leaf_serialized
;
1304 if (cs
->ref
!= FC_REF_CONSTANT
&& serialize
->cs_freezer
)
1306 cs
= FcCharSetFindFrozen (serialize
->cs_freezer
, cs
);
1311 cs_serialized
= FcSerializePtr (serialize
, cs
);
1315 cs_serialized
->ref
= FC_REF_CONSTANT
;
1316 cs_serialized
->num
= cs
->num
;
1320 leaves
= FcCharSetLeaves (cs
);
1321 leaves_serialized
= FcSerializePtr (serialize
, leaves
);
1322 if (!leaves_serialized
)
1325 cs_serialized
->leaves_offset
= FcPtrToOffset (cs_serialized
,
1328 numbers
= FcCharSetNumbers (cs
);
1329 numbers_serialized
= FcSerializePtr (serialize
, numbers
);
1333 cs_serialized
->numbers_offset
= FcPtrToOffset (cs_serialized
,
1334 numbers_serialized
);
1336 for (i
= 0; i
< cs
->num
; i
++)
1338 leaf
= FcCharSetLeaf (cs
, i
);
1339 leaf_serialized
= FcSerializePtr (serialize
, leaf
);
1340 if (!leaf_serialized
)
1342 *leaf_serialized
= *leaf
;
1343 leaves_serialized
[i
] = FcPtrToOffset (leaves_serialized
,
1345 numbers_serialized
[i
] = numbers
[i
];
1350 cs_serialized
->leaves_offset
= 0;
1351 cs_serialized
->numbers_offset
= 0;
1354 return cs_serialized
;