2 * fontconfig/src/fccharset.c
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 * THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18 * EVENT SHALL THE AUTHOR(S) 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
)
59 FcCacheObjectDereference (fcs
);
64 for (i
= 0; i
< fcs
->num
; i
++)
66 FcMemFree (FC_MEM_CHARLEAF
, sizeof (FcCharLeaf
));
67 free (FcCharSetLeaf (fcs
, i
));
71 FcMemFree (FC_MEM_CHARSET
, fcs
->num
* sizeof (intptr_t));
72 free (FcCharSetLeaves (fcs
));
76 FcMemFree (FC_MEM_CHARSET
, fcs
->num
* sizeof (FcChar16
));
77 free (FcCharSetNumbers (fcs
));
79 FcMemFree (FC_MEM_CHARSET
, sizeof (FcCharSet
));
84 * Search for the leaf containing with the specified num.
85 * Return its index if it exists, otherwise return negative of
86 * the (position + 1) where it should be inserted
91 FcCharSetFindLeafForward (const FcCharSet
*fcs
, int start
, FcChar16 num
)
93 FcChar16
*numbers
= FcCharSetNumbers(fcs
);
96 int high
= fcs
->num
- 1;
102 int mid
= (low
+ high
) >> 1;
111 if (high
< 0 || (high
< fcs
->num
&& numbers
[high
] < num
))
117 * Locate the leaf containing the specified char, return
118 * its index if it exists, otherwise return negative of
119 * the (position + 1) where it should be inserted
123 FcCharSetFindLeafPos (const FcCharSet
*fcs
, FcChar32 ucs4
)
125 return FcCharSetFindLeafForward (fcs
, 0, ucs4
>> 8);
129 FcCharSetFindLeaf (const FcCharSet
*fcs
, FcChar32 ucs4
)
131 int pos
= FcCharSetFindLeafPos (fcs
, ucs4
);
133 return FcCharSetLeaf(fcs
, pos
);
138 FcCharSetPutLeaf (FcCharSet
*fcs
,
143 intptr_t *leaves
= FcCharSetLeaves (fcs
);
144 FcChar16
*numbers
= FcCharSetNumbers (fcs
);
146 /* XXX We can't handle Unicode values in Plane 16 */
151 if (fcs
->num
== fcs
->alloced
)
153 intptr_t *new_leaves
= realloc (leaves
, (fcs
->num
+ 1) *
155 intptr_t distance
= (intptr_t) new_leaves
- (intptr_t) leaves
;
157 if (new_leaves
&& distance
)
161 for (i
= 0; i
< fcs
->num
; i
++)
162 new_leaves
[i
] -= distance
;
170 FcMemFree (FC_MEM_CHARSET
, fcs
->num
* sizeof (intptr_t));
171 FcMemAlloc (FC_MEM_CHARSET
, (fcs
->num
+ 1) * sizeof (intptr_t));
172 fcs
->leaves_offset
= FcPtrToOffset (fcs
, leaves
);
175 numbers
= malloc (sizeof (FcChar16
));
177 numbers
= realloc (numbers
, (fcs
->num
+ 1) * sizeof (FcChar16
));
182 FcMemFree (FC_MEM_CHARSET
, fcs
->num
* sizeof (FcChar16
));
183 FcMemAlloc (FC_MEM_CHARSET
, (fcs
->num
+ 1) * sizeof (FcChar16
));
184 fcs
->numbers_offset
= FcPtrToOffset (fcs
, numbers
);
186 memmove (leaves
+ pos
+ 1, leaves
+ pos
,
187 (fcs
->num
- pos
) * sizeof (*leaves
));
188 memmove (numbers
+ pos
+ 1, numbers
+ pos
,
189 (fcs
->num
- pos
) * sizeof (*numbers
));
190 numbers
[pos
] = (FcChar16
) ucs4
;
191 leaves
[pos
] = FcPtrToOffset (leaves
, leaf
);
197 * Locate the leaf containing the specified char, creating it
202 FcCharSetFindLeafCreate (FcCharSet
*fcs
, FcChar32 ucs4
)
207 pos
= FcCharSetFindLeafPos (fcs
, ucs4
);
209 return FcCharSetLeaf(fcs
, pos
);
211 leaf
= calloc (1, sizeof (FcCharLeaf
));
216 if (!FcCharSetPutLeaf (fcs
, ucs4
, leaf
, pos
))
221 FcMemAlloc (FC_MEM_CHARLEAF
, sizeof (FcCharLeaf
));
226 FcCharSetInsertLeaf (FcCharSet
*fcs
, FcChar32 ucs4
, FcCharLeaf
*leaf
)
230 pos
= FcCharSetFindLeafPos (fcs
, ucs4
);
233 FcMemFree (FC_MEM_CHARLEAF
, sizeof (FcCharLeaf
));
234 free (FcCharSetLeaf (fcs
, pos
));
235 FcCharSetLeaves(fcs
)[pos
] = FcPtrToOffset (FcCharSetLeaves(fcs
),
240 return FcCharSetPutLeaf (fcs
, ucs4
, leaf
, pos
);
244 FcCharSetAddChar (FcCharSet
*fcs
, FcChar32 ucs4
)
249 if (fcs
->ref
== FC_REF_CONSTANT
)
251 leaf
= FcCharSetFindLeafCreate (fcs
, ucs4
);
254 b
= &leaf
->map
[(ucs4
& 0xff) >> 5];
255 *b
|= (1 << (ucs4
& 0x1f));
260 * An iterator for the leaves of a charset
263 typedef struct _fcCharSetIter
{
270 * Set iter->leaf to the leaf containing iter->ucs4 or higher
274 FcCharSetIterSet (const FcCharSet
*fcs
, FcCharSetIter
*iter
)
276 int pos
= FcCharSetFindLeafPos (fcs
, iter
->ucs4
);
287 iter
->ucs4
= (FcChar32
) FcCharSetNumbers(fcs
)[pos
] << 8;
289 iter
->leaf
= FcCharSetLeaf(fcs
, pos
);
294 FcCharSetIterNext (const FcCharSet
*fcs
, FcCharSetIter
*iter
)
296 int pos
= iter
->pos
+ 1;
304 iter
->ucs4
= (FcChar32
) FcCharSetNumbers(fcs
)[pos
] << 8;
305 iter
->leaf
= FcCharSetLeaf(fcs
, pos
);
312 FcCharSetIterStart (const FcCharSet
*fcs
, FcCharSetIter
*iter
)
316 FcCharSetIterSet (fcs
, iter
);
320 FcCharSetCopy (FcCharSet
*src
)
322 if (src
->ref
!= FC_REF_CONSTANT
)
325 FcCacheObjectReference (src
);
330 FcCharSetEqual (const FcCharSet
*a
, const FcCharSet
*b
)
332 FcCharSetIter ai
, bi
;
337 for (FcCharSetIterStart (a
, &ai
), FcCharSetIterStart (b
, &bi
);
339 FcCharSetIterNext (a
, &ai
), FcCharSetIterNext (b
, &bi
))
341 if (ai
.ucs4
!= bi
.ucs4
)
343 for (i
= 0; i
< 256/32; i
++)
344 if (ai
.leaf
->map
[i
] != bi
.leaf
->map
[i
])
347 return ai
.leaf
== bi
.leaf
;
351 FcCharSetAddLeaf (FcCharSet
*fcs
,
355 FcCharLeaf
*new = FcCharSetFindLeafCreate (fcs
, ucs4
);
363 FcCharSetOperate (const FcCharSet
*a
,
365 FcBool (*overlap
) (FcCharLeaf
*result
,
366 const FcCharLeaf
*al
,
367 const FcCharLeaf
*bl
),
372 FcCharSetIter ai
, bi
;
374 fcs
= FcCharSetCreate ();
377 FcCharSetIterStart (a
, &ai
);
378 FcCharSetIterStart (b
, &bi
);
379 while ((ai
.leaf
|| (bonly
&& bi
.leaf
)) && (bi
.leaf
|| (aonly
&& ai
.leaf
)))
381 if (ai
.ucs4
< bi
.ucs4
)
385 if (!FcCharSetAddLeaf (fcs
, ai
.ucs4
, ai
.leaf
))
387 FcCharSetIterNext (a
, &ai
);
392 FcCharSetIterSet (a
, &ai
);
395 else if (bi
.ucs4
< ai
.ucs4
)
399 if (!FcCharSetAddLeaf (fcs
, bi
.ucs4
, bi
.leaf
))
401 FcCharSetIterNext (b
, &bi
);
406 FcCharSetIterSet (b
, &bi
);
413 if ((*overlap
) (&leaf
, ai
.leaf
, bi
.leaf
))
415 if (!FcCharSetAddLeaf (fcs
, ai
.ucs4
, &leaf
))
418 FcCharSetIterNext (a
, &ai
);
419 FcCharSetIterNext (b
, &bi
);
424 FcCharSetDestroy (fcs
);
430 FcCharSetIntersectLeaf (FcCharLeaf
*result
,
431 const FcCharLeaf
*al
,
432 const FcCharLeaf
*bl
)
435 FcBool nonempty
= FcFalse
;
437 for (i
= 0; i
< 256/32; i
++)
438 if ((result
->map
[i
] = al
->map
[i
] & bl
->map
[i
]))
444 FcCharSetIntersect (const FcCharSet
*a
, const FcCharSet
*b
)
446 return FcCharSetOperate (a
, b
, FcCharSetIntersectLeaf
, FcFalse
, FcFalse
);
450 FcCharSetUnionLeaf (FcCharLeaf
*result
,
451 const FcCharLeaf
*al
,
452 const FcCharLeaf
*bl
)
456 for (i
= 0; i
< 256/32; i
++)
457 result
->map
[i
] = al
->map
[i
] | bl
->map
[i
];
462 FcCharSetUnion (const FcCharSet
*a
, const FcCharSet
*b
)
464 return FcCharSetOperate (a
, b
, FcCharSetUnionLeaf
, FcTrue
, FcTrue
);
468 FcCharSetMerge (FcCharSet
*a
, const FcCharSet
*b
, FcBool
*changed
)
473 if (a
->ref
== FC_REF_CONSTANT
) {
480 *changed
= !FcCharSetIsSubset(b
, a
);
487 an
= ai
< a
->num
? FcCharSetNumbers(a
)[ai
] : ~0;
488 bn
= FcCharSetNumbers(b
)[bi
];
492 ai
= FcCharSetFindLeafForward (a
, ai
+ 1, bn
);
498 FcCharLeaf
*bl
= FcCharSetLeaf(b
, bi
);
501 if (!FcCharSetAddLeaf (a
, bn
<< 8, bl
))
506 FcCharLeaf
*al
= FcCharSetLeaf(a
, ai
);
507 FcCharSetUnionLeaf (al
, al
, bl
);
519 FcCharSetSubtractLeaf (FcCharLeaf
*result
,
520 const FcCharLeaf
*al
,
521 const FcCharLeaf
*bl
)
524 FcBool nonempty
= FcFalse
;
526 for (i
= 0; i
< 256/32; i
++)
527 if ((result
->map
[i
] = al
->map
[i
] & ~bl
->map
[i
]))
533 FcCharSetSubtract (const FcCharSet
*a
, const FcCharSet
*b
)
535 return FcCharSetOperate (a
, b
, FcCharSetSubtractLeaf
, FcTrue
, FcFalse
);
539 FcCharSetHasChar (const FcCharSet
*fcs
, FcChar32 ucs4
)
541 FcCharLeaf
*leaf
= FcCharSetFindLeaf (fcs
, ucs4
);
544 return (leaf
->map
[(ucs4
& 0xff) >> 5] & (1 << (ucs4
& 0x1f))) != 0;
548 FcCharSetPopCount (FcChar32 c1
)
550 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
551 return __builtin_popcount (c1
);
554 FcChar32 c2
= (c1
>> 1) & 033333333333;
555 c2
= c1
- c2
- ((c2
>> 1) & 033333333333);
556 return (((c2
+ (c2
>> 3)) & 030707070707) % 077);
561 FcCharSetIntersectCount (const FcCharSet
*a
, const FcCharSet
*b
)
563 FcCharSetIter ai
, bi
;
566 FcCharSetIterStart (a
, &ai
);
567 FcCharSetIterStart (b
, &bi
);
568 while (ai
.leaf
&& bi
.leaf
)
570 if (ai
.ucs4
== bi
.ucs4
)
572 FcChar32
*am
= ai
.leaf
->map
;
573 FcChar32
*bm
= bi
.leaf
->map
;
576 count
+= FcCharSetPopCount (*am
++ & *bm
++);
577 FcCharSetIterNext (a
, &ai
);
579 else if (ai
.ucs4
< bi
.ucs4
)
582 FcCharSetIterSet (a
, &ai
);
584 if (bi
.ucs4
< ai
.ucs4
)
587 FcCharSetIterSet (b
, &bi
);
594 FcCharSetCount (const FcCharSet
*a
)
599 for (FcCharSetIterStart (a
, &ai
); ai
.leaf
; FcCharSetIterNext (a
, &ai
))
602 FcChar32
*am
= ai
.leaf
->map
;
605 count
+= FcCharSetPopCount (*am
++);
611 FcCharSetSubtractCount (const FcCharSet
*a
, const FcCharSet
*b
)
613 FcCharSetIter ai
, bi
;
616 FcCharSetIterStart (a
, &ai
);
617 FcCharSetIterStart (b
, &bi
);
620 if (ai
.ucs4
<= bi
.ucs4
)
622 FcChar32
*am
= ai
.leaf
->map
;
624 if (ai
.ucs4
== bi
.ucs4
)
626 FcChar32
*bm
= bi
.leaf
->map
;
628 count
+= FcCharSetPopCount (*am
++ & ~*bm
++);
633 count
+= FcCharSetPopCount (*am
++);
635 FcCharSetIterNext (a
, &ai
);
640 FcCharSetIterSet (b
, &bi
);
647 * return FcTrue iff a is a subset of b
650 FcCharSetIsSubset (const FcCharSet
*a
, const FcCharSet
*b
)
655 if (a
== b
) return FcTrue
;
658 while (ai
< a
->num
&& bi
< b
->num
)
660 an
= FcCharSetNumbers(a
)[ai
];
661 bn
= FcCharSetNumbers(b
)[bi
];
663 * Check matching pages
667 FcChar32
*am
= FcCharSetLeaf(a
, ai
)->map
;
668 FcChar32
*bm
= FcCharSetLeaf(b
, bi
)->map
;
674 * Does am have any bits not in bm?
684 * Does a have any pages not in b?
690 bi
= FcCharSetFindLeafForward (b
, bi
+ 1, an
);
696 * did we look at every page?
702 * These two functions efficiently walk the entire charmap for
703 * other software (like pango) that want their own copy
707 FcCharSetNextPage (const FcCharSet
*a
,
708 FcChar32 map
[FC_CHARSET_MAP_SIZE
],
715 FcCharSetIterSet (a
, &ai
);
717 return FC_CHARSET_DONE
;
720 * Save current information
723 memcpy (map
, ai
.leaf
->map
, sizeof (ai
.leaf
->map
));
727 FcCharSetIterNext (a
, &ai
);
734 FcCharSetFirstPage (const FcCharSet
*a
,
735 FcChar32 map
[FC_CHARSET_MAP_SIZE
],
739 return FcCharSetNextPage (a
, map
, next
);
743 * old coverage API, rather hard to use correctly
747 FcCharSetCoverage (const FcCharSet
*a
, FcChar32 page
, FcChar32
*result
)
752 FcCharSetIterSet (a
, &ai
);
755 memset (result
, '\0', 256 / 8);
760 memcpy (result
, ai
.leaf
->map
, sizeof (ai
.leaf
->map
));
761 FcCharSetIterNext (a
, &ai
);
768 * ASCII representation of charsets.
770 * Each leaf is represented as 9 32-bit values, the code of the first character followed
771 * by 8 32 bit values for the leaf itself. Each value is encoded as 5 ASCII characters,
772 * only 85 different values are used to avoid control characters as well as the other
773 * characters used to encode font names. 85**5 > 2^32 so things work out, but
774 * it's not exactly human readable output. As a special case, 0 is encoded as a space
777 static const unsigned char charToValue
[256] = {
778 /* "" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
779 /* "\b" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
780 /* "\020" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
781 /* "\030" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
782 /* " " */ 0xff, 0x00, 0xff, 0x01, 0x02, 0x03, 0x04, 0xff,
783 /* "(" */ 0x05, 0x06, 0x07, 0x08, 0xff, 0xff, 0x09, 0x0a,
784 /* "0" */ 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12,
785 /* "8" */ 0x13, 0x14, 0xff, 0x15, 0x16, 0xff, 0x17, 0x18,
786 /* "@" */ 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
787 /* "H" */ 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
788 /* "P" */ 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30,
789 /* "X" */ 0x31, 0x32, 0x33, 0x34, 0xff, 0x35, 0x36, 0xff,
790 /* "`" */ 0xff, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d,
791 /* "h" */ 0x3e, 0x3f, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45,
792 /* "p" */ 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d,
793 /* "x" */ 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53, 0x54, 0xff,
794 /* "\200" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
795 /* "\210" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
796 /* "\220" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
797 /* "\230" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
798 /* "\240" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
799 /* "\250" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
800 /* "\260" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
801 /* "\270" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
802 /* "\300" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
803 /* "\310" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
804 /* "\320" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
805 /* "\330" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
806 /* "\340" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
807 /* "\350" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
808 /* "\360" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
809 /* "\370" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
812 static const FcChar8 valueToChar
[0x55] = {
813 /* 0x00 */ '!', '#', '$', '%', '&', '(', ')', '*',
814 /* 0x08 */ '+', '.', '/', '0', '1', '2', '3', '4',
815 /* 0x10 */ '5', '6', '7', '8', '9', ';', '<', '>',
816 /* 0x18 */ '?', '@', 'A', 'B', 'C', 'D', 'E', 'F',
817 /* 0x20 */ 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
818 /* 0x28 */ 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
819 /* 0x30 */ 'W', 'X', 'Y', 'Z', '[', ']', '^', 'a',
820 /* 0x38 */ 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
821 /* 0x40 */ 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q',
822 /* 0x48 */ 'r', 's', 't', 'u', 'v', 'w', 'x', 'y',
823 /* 0x50 */ 'z', '{', '|', '}', '~',
827 FcCharSetParseValue (FcChar8
*string
, FcChar32
*value
)
841 for (i
= 0; i
< 5; i
++)
843 if (!(c
= (FcChar32
) (unsigned char) *string
++))
856 FcCharSetUnparseValue (FcStrBuf
*buf
, FcChar32 value
)
861 return FcStrBufChar (buf
, ' ');
866 FcChar8
*s
= string
+ 5;
868 for (i
= 0; i
< 5; i
++)
870 *--s
= valueToChar
[value
% 85];
873 for (i
= 0; i
< 5; i
++)
874 if (!FcStrBufChar (buf
, *s
++))
881 FcNameParseCharSet (FcChar8
*string
)
890 c
= FcCharSetCreate ();
895 string
= FcCharSetParseValue (string
, &ucs4
);
899 for (i
= 0; i
< 256/32; i
++)
901 string
= FcCharSetParseValue (string
, &temp
.map
[i
]);
908 leaf
= malloc (sizeof (FcCharLeaf
));
912 if (!FcCharSetInsertLeaf (c
, ucs4
, leaf
))
920 FcMemFree (FC_MEM_CHARSET
, c
->num
* sizeof (FcCharLeaf
*));
921 free (FcCharSetLeaves (c
));
925 FcMemFree (FC_MEM_CHARSET
, c
->num
* sizeof (FcChar16
));
926 free (FcCharSetNumbers (c
));
928 FcMemFree (FC_MEM_CHARSET
, sizeof (FcCharSet
));
935 FcNameUnparseCharSet (FcStrBuf
*buf
, const FcCharSet
*c
)
943 for (FcCharSetIterStart (c
, &ci
);
945 FcCharSetIterNext (c
, &ci
))
947 if (!FcCharSetUnparseValue (buf
, ci
.ucs4
))
949 for (i
= 0; i
< 256/32; i
++)
950 if (!FcCharSetUnparseValue (buf
, ci
.leaf
->map
[i
]))
957 FcCharSetIter ci
, checki
;
959 /* null terminate for parser */
960 FcStrBufChar (buf
, '\0');
961 /* step back over null for life after test */
963 check
= FcNameParseCharSet (buf
->buf
+ len
);
964 FcCharSetIterStart (c
, &ci
);
965 FcCharSetIterStart (check
, &checki
);
966 while (ci
.leaf
|| checki
.leaf
)
968 if (ci
.ucs4
< checki
.ucs4
)
970 printf ("Missing leaf node at 0x%x\n", ci
.ucs4
);
971 FcCharSetIterNext (c
, &ci
);
973 else if (checki
.ucs4
< ci
.ucs4
)
975 printf ("Extra leaf node at 0x%x\n", checki
.ucs4
);
976 FcCharSetIterNext (check
, &checki
);
981 FcChar32
*cm
= ci
.leaf
->map
;
982 FcChar32
*checkm
= checki
.leaf
->map
;
984 for (i
= 0; i
< 256; i
+= 32)
987 printf ("Mismatching sets at 0x%08x: 0x%08x != 0x%08x\n",
988 ci
.ucs4
+ i
, *cm
, *checkm
);
992 FcCharSetIterNext (c
, &ci
);
993 FcCharSetIterNext (check
, &checki
);
996 if ((missing
= FcCharSetSubtractCount (c
, check
)))
997 printf ("%d missing in reparsed result\n", missing
);
998 if ((missing
= FcCharSetSubtractCount (check
, c
)))
999 printf ("%d extra in reparsed result\n", missing
);
1000 FcCharSetDestroy (check
);
1007 typedef struct _FcCharLeafEnt FcCharLeafEnt
;
1009 struct _FcCharLeafEnt
{
1010 FcCharLeafEnt
*next
;
1015 #define FC_CHAR_LEAF_BLOCK (4096 / sizeof (FcCharLeafEnt))
1016 #define FC_CHAR_LEAF_HASH_SIZE 257
1018 typedef struct _FcCharSetEnt FcCharSetEnt
;
1020 struct _FcCharSetEnt
{
1026 typedef struct _FcCharSetOrigEnt FcCharSetOrigEnt
;
1028 struct _FcCharSetOrigEnt
{
1029 FcCharSetOrigEnt
*next
;
1030 const FcCharSet
*orig
;
1031 const FcCharSet
*frozen
;
1034 #define FC_CHAR_SET_HASH_SIZE 67
1036 struct _FcCharSetFreezer
{
1037 FcCharLeafEnt
*leaf_hash_table
[FC_CHAR_LEAF_HASH_SIZE
];
1038 FcCharLeafEnt
**leaf_blocks
;
1039 int leaf_block_count
;
1040 FcCharSetEnt
*set_hash_table
[FC_CHAR_SET_HASH_SIZE
];
1041 FcCharSetOrigEnt
*orig_hash_table
[FC_CHAR_SET_HASH_SIZE
];
1042 FcCharLeafEnt
*current_block
;
1046 int leaves_allocated
;
1047 int charsets_allocated
;
1050 static FcCharLeafEnt
*
1051 FcCharLeafEntCreate (FcCharSetFreezer
*freezer
)
1053 if (!freezer
->leaf_remain
)
1055 FcCharLeafEnt
**newBlocks
;
1057 freezer
->leaf_block_count
++;
1058 newBlocks
= realloc (freezer
->leaf_blocks
, freezer
->leaf_block_count
* sizeof (FcCharLeafEnt
*));
1061 freezer
->leaf_blocks
= newBlocks
;
1062 freezer
->current_block
= freezer
->leaf_blocks
[freezer
->leaf_block_count
-1] = malloc (FC_CHAR_LEAF_BLOCK
* sizeof (FcCharLeafEnt
));
1063 if (!freezer
->current_block
)
1065 FcMemAlloc (FC_MEM_CHARLEAF
, FC_CHAR_LEAF_BLOCK
* sizeof (FcCharLeafEnt
));
1066 freezer
->leaf_remain
= FC_CHAR_LEAF_BLOCK
;
1068 freezer
->leaf_remain
--;
1069 freezer
->leaves_allocated
++;
1070 return freezer
->current_block
++;
1074 FcCharLeafHash (FcCharLeaf
*leaf
)
1079 for (i
= 0; i
< 256/32; i
++)
1080 hash
= ((hash
<< 1) | (hash
>> 31)) ^ leaf
->map
[i
];
1085 FcCharSetFreezeLeaf (FcCharSetFreezer
*freezer
, FcCharLeaf
*leaf
)
1087 FcChar32 hash
= FcCharLeafHash (leaf
);
1088 FcCharLeafEnt
**bucket
= &freezer
->leaf_hash_table
[hash
% FC_CHAR_LEAF_HASH_SIZE
];
1091 for (ent
= *bucket
; ent
; ent
= ent
->next
)
1093 if (ent
->hash
== hash
&& !memcmp (&ent
->leaf
, leaf
, sizeof (FcCharLeaf
)))
1097 ent
= FcCharLeafEntCreate(freezer
);
1102 ent
->next
= *bucket
;
1108 FcCharSetHash (FcCharSet
*fcs
)
1113 /* hash in leaves */
1114 for (i
= 0; i
< fcs
->num
; i
++)
1115 hash
= ((hash
<< 1) | (hash
>> 31)) ^ FcCharLeafHash (FcCharSetLeaf(fcs
,i
));
1116 /* hash in numbers */
1117 for (i
= 0; i
< fcs
->num
; i
++)
1118 hash
= ((hash
<< 1) | (hash
>> 31)) ^ *FcCharSetNumbers(fcs
);
1123 FcCharSetFreezeOrig (FcCharSetFreezer
*freezer
, const FcCharSet
*orig
, const FcCharSet
*frozen
)
1125 FcCharSetOrigEnt
**bucket
= &freezer
->orig_hash_table
[((uintptr_t) orig
) & FC_CHAR_SET_HASH_SIZE
];
1126 FcCharSetOrigEnt
*ent
;
1128 ent
= malloc (sizeof (FcCharSetOrigEnt
));
1132 ent
->frozen
= frozen
;
1133 ent
->next
= *bucket
;
1139 FcCharSetFreezeBase (FcCharSetFreezer
*freezer
, FcCharSet
*fcs
, const FcCharSet
*orig
)
1141 FcChar32 hash
= FcCharSetHash (fcs
);
1142 FcCharSetEnt
**bucket
= &freezer
->set_hash_table
[hash
% FC_CHAR_SET_HASH_SIZE
];
1147 for (ent
= *bucket
; ent
; ent
= ent
->next
)
1149 if (ent
->hash
== hash
&&
1150 ent
->set
.num
== fcs
->num
&&
1151 !memcmp (FcCharSetNumbers(&ent
->set
),
1152 FcCharSetNumbers(fcs
),
1153 fcs
->num
* sizeof (FcChar16
)))
1158 for (i
= 0; i
< fcs
->num
; i
++)
1159 if (FcCharSetLeaf(&ent
->set
, i
) != FcCharSetLeaf(fcs
, i
))
1166 size
= (sizeof (FcCharSetEnt
) +
1167 fcs
->num
* sizeof (FcCharLeaf
*) +
1168 fcs
->num
* sizeof (FcChar16
));
1169 ent
= malloc (size
);
1172 FcMemAlloc (FC_MEM_CHARSET
, size
);
1174 freezer
->charsets_allocated
++;
1176 ent
->set
.ref
= FC_REF_CONSTANT
;
1177 ent
->set
.num
= fcs
->num
;
1180 intptr_t *ent_leaves
;
1182 ent
->set
.leaves_offset
= sizeof (ent
->set
);
1183 ent
->set
.numbers_offset
= (ent
->set
.leaves_offset
+
1184 fcs
->num
* sizeof (intptr_t));
1186 ent_leaves
= FcCharSetLeaves (&ent
->set
);
1187 for (i
= 0; i
< fcs
->num
; i
++)
1188 ent_leaves
[i
] = FcPtrToOffset (ent_leaves
,
1189 FcCharSetLeaf (fcs
, i
));
1190 memcpy (FcCharSetNumbers (&ent
->set
),
1191 FcCharSetNumbers (fcs
),
1192 fcs
->num
* sizeof (FcChar16
));
1196 ent
->set
.leaves_offset
= 0;
1197 ent
->set
.numbers_offset
= 0;
1201 ent
->next
= *bucket
;
1207 static const FcCharSet
*
1208 FcCharSetFindFrozen (FcCharSetFreezer
*freezer
, const FcCharSet
*orig
)
1210 FcCharSetOrigEnt
**bucket
= &freezer
->orig_hash_table
[((uintptr_t) orig
) & FC_CHAR_SET_HASH_SIZE
];
1211 FcCharSetOrigEnt
*ent
;
1213 for (ent
= *bucket
; ent
; ent
= ent
->next
)
1214 if (ent
->orig
== orig
)
1220 FcCharSetFreeze (FcCharSetFreezer
*freezer
, const FcCharSet
*fcs
)
1223 const FcCharSet
*n
= 0;
1227 b
= FcCharSetCreate ();
1230 for (i
= 0; i
< fcs
->num
; i
++)
1232 l
= FcCharSetFreezeLeaf (freezer
, FcCharSetLeaf(fcs
, i
));
1235 if (!FcCharSetInsertLeaf (b
, FcCharSetNumbers(fcs
)[i
] << 8, l
))
1238 n
= FcCharSetFreezeBase (freezer
, b
, fcs
);
1239 if (!FcCharSetFreezeOrig (freezer
, fcs
, n
))
1244 freezer
->charsets_seen
++;
1245 freezer
->leaves_seen
+= fcs
->num
;
1249 FcMemFree (FC_MEM_CHARSET
, b
->num
* sizeof (FcCharLeaf
*));
1250 free (FcCharSetLeaves (b
));
1254 FcMemFree (FC_MEM_CHARSET
, b
->num
* sizeof (FcChar16
));
1255 free (FcCharSetNumbers (b
));
1257 FcMemFree (FC_MEM_CHARSET
, sizeof (FcCharSet
));
1264 FcCharSetFreezerCreate (void)
1266 FcCharSetFreezer
*freezer
;
1268 freezer
= calloc (1, sizeof (FcCharSetFreezer
));
1273 FcCharSetFreezerDestroy (FcCharSetFreezer
*freezer
)
1277 if (FcDebug() & FC_DBG_CACHE
)
1279 printf ("\ncharsets %d -> %d leaves %d -> %d\n",
1280 freezer
->charsets_seen
, freezer
->charsets_allocated
,
1281 freezer
->leaves_seen
, freezer
->leaves_allocated
);
1283 for (i
= 0; i
< FC_CHAR_SET_HASH_SIZE
; i
++)
1285 FcCharSetEnt
*ent
, *next
;
1286 for (ent
= freezer
->set_hash_table
[i
]; ent
; ent
= next
)
1289 FcMemFree (FC_MEM_CHARSET
, (sizeof (FcCharSetEnt
) +
1290 ent
->set
.num
* sizeof (FcCharLeaf
*) +
1291 ent
->set
.num
* sizeof (FcChar16
)));
1296 for (i
= 0; i
< FC_CHAR_SET_HASH_SIZE
; i
++)
1298 FcCharSetOrigEnt
*ent
, *next
;
1299 for (ent
= freezer
->orig_hash_table
[i
]; ent
; ent
= next
)
1306 for (i
= 0; i
< freezer
->leaf_block_count
; i
++)
1308 free (freezer
->leaf_blocks
[i
]);
1309 FcMemFree (FC_MEM_CHARLEAF
, FC_CHAR_LEAF_BLOCK
* sizeof (FcCharLeafEnt
));
1312 free (freezer
->leaf_blocks
);
1317 FcCharSetSerializeAlloc (FcSerialize
*serialize
, const FcCharSet
*cs
)
1323 if (cs
->ref
!= FC_REF_CONSTANT
)
1325 if (!serialize
->cs_freezer
)
1327 serialize
->cs_freezer
= FcCharSetFreezerCreate ();
1328 if (!serialize
->cs_freezer
)
1331 if (FcCharSetFindFrozen (serialize
->cs_freezer
, cs
))
1334 cs
= FcCharSetFreeze (serialize
->cs_freezer
, cs
);
1337 leaves
= FcCharSetLeaves (cs
);
1338 numbers
= FcCharSetNumbers (cs
);
1340 if (!FcSerializeAlloc (serialize
, cs
, sizeof (FcCharSet
)))
1342 if (!FcSerializeAlloc (serialize
, leaves
, cs
->num
* sizeof (intptr_t)))
1344 if (!FcSerializeAlloc (serialize
, numbers
, cs
->num
* sizeof (FcChar16
)))
1346 for (i
= 0; i
< cs
->num
; i
++)
1347 if (!FcSerializeAlloc (serialize
, FcCharSetLeaf(cs
, i
),
1348 sizeof (FcCharLeaf
)))
1354 FcCharSetSerialize(FcSerialize
*serialize
, const FcCharSet
*cs
)
1356 FcCharSet
*cs_serialized
;
1357 intptr_t *leaves
, *leaves_serialized
;
1358 FcChar16
*numbers
, *numbers_serialized
;
1359 FcCharLeaf
*leaf
, *leaf_serialized
;
1362 if (cs
->ref
!= FC_REF_CONSTANT
&& serialize
->cs_freezer
)
1364 cs
= FcCharSetFindFrozen (serialize
->cs_freezer
, cs
);
1369 cs_serialized
= FcSerializePtr (serialize
, cs
);
1373 cs_serialized
->ref
= FC_REF_CONSTANT
;
1374 cs_serialized
->num
= cs
->num
;
1378 leaves
= FcCharSetLeaves (cs
);
1379 leaves_serialized
= FcSerializePtr (serialize
, leaves
);
1380 if (!leaves_serialized
)
1383 cs_serialized
->leaves_offset
= FcPtrToOffset (cs_serialized
,
1386 numbers
= FcCharSetNumbers (cs
);
1387 numbers_serialized
= FcSerializePtr (serialize
, numbers
);
1391 cs_serialized
->numbers_offset
= FcPtrToOffset (cs_serialized
,
1392 numbers_serialized
);
1394 for (i
= 0; i
< cs
->num
; i
++)
1396 leaf
= FcCharSetLeaf (cs
, i
);
1397 leaf_serialized
= FcSerializePtr (serialize
, leaf
);
1398 if (!leaf_serialized
)
1400 *leaf_serialized
= *leaf
;
1401 leaves_serialized
[i
] = FcPtrToOffset (leaves_serialized
,
1403 numbers_serialized
[i
] = numbers
[i
];
1408 cs_serialized
->leaves_offset
= 0;
1409 cs_serialized
->numbers_offset
= 0;
1412 return cs_serialized
;
1414 #define __fccharset__
1415 #include "fcaliastail.h"
1416 #undef __fccharset__