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 /* the numbers here are estimates */
72 FcMemFree (FC_MEM_CHARSET
, fcs
->num
* sizeof (intptr_t));
73 free (FcCharSetLeaves (fcs
));
74 FcMemFree (FC_MEM_CHARSET
, fcs
->num
* sizeof (FcChar16
));
75 free (FcCharSetNumbers (fcs
));
77 FcMemFree (FC_MEM_CHARSET
, sizeof (FcCharSet
));
82 * Search for the leaf containing with the specified num.
83 * Return its index if it exists, otherwise return negative of
84 * the (position + 1) where it should be inserted
89 FcCharSetFindLeafForward (const FcCharSet
*fcs
, int start
, FcChar16 num
)
91 FcChar16
*numbers
= FcCharSetNumbers(fcs
);
94 int high
= fcs
->num
- 1;
100 int mid
= (low
+ high
) >> 1;
109 if (high
< 0 || (high
< fcs
->num
&& numbers
[high
] < num
))
115 * Locate the leaf containing the specified char, return
116 * its index if it exists, otherwise return negative of
117 * the (position + 1) where it should be inserted
121 FcCharSetFindLeafPos (const FcCharSet
*fcs
, FcChar32 ucs4
)
123 return FcCharSetFindLeafForward (fcs
, 0, ucs4
>> 8);
127 FcCharSetFindLeaf (const FcCharSet
*fcs
, FcChar32 ucs4
)
129 int pos
= FcCharSetFindLeafPos (fcs
, ucs4
);
131 return FcCharSetLeaf(fcs
, pos
);
135 #define FC_IS_ZERO_OR_POWER_OF_TWO(x) (!((x) & ((x)-1)))
138 FcCharSetPutLeaf (FcCharSet
*fcs
,
143 intptr_t *leaves
= FcCharSetLeaves (fcs
);
144 FcChar16
*numbers
= FcCharSetNumbers (fcs
);
150 if (FC_IS_ZERO_OR_POWER_OF_TWO (fcs
->num
))
154 unsigned int alloced
= 8;
155 leaves
= malloc (alloced
* sizeof (*leaves
));
156 numbers
= malloc (alloced
* sizeof (*numbers
));
157 FcMemAlloc (FC_MEM_CHARSET
, alloced
* sizeof (*leaves
));
158 FcMemAlloc (FC_MEM_CHARSET
, alloced
* sizeof (*numbers
));
162 unsigned int alloced
= fcs
->num
;
163 intptr_t *new_leaves
, distance
;
165 FcMemFree (FC_MEM_CHARSET
, alloced
* sizeof (*leaves
));
166 FcMemFree (FC_MEM_CHARSET
, alloced
* sizeof (*numbers
));
169 new_leaves
= realloc (leaves
, alloced
* sizeof (*leaves
));
170 numbers
= realloc (numbers
, alloced
* sizeof (*numbers
));
172 FcMemAlloc (FC_MEM_CHARSET
, alloced
* sizeof (*leaves
));
173 FcMemAlloc (FC_MEM_CHARSET
, alloced
* sizeof (*numbers
));
175 distance
= (intptr_t) new_leaves
- (intptr_t) leaves
;
176 if (new_leaves
&& distance
)
179 for (i
= 0; i
< fcs
->num
; i
++)
180 new_leaves
[i
] -= distance
;
185 if (!leaves
|| !numbers
)
188 fcs
->leaves_offset
= FcPtrToOffset (fcs
, leaves
);
189 fcs
->numbers_offset
= FcPtrToOffset (fcs
, numbers
);
192 memmove (leaves
+ pos
+ 1, leaves
+ pos
,
193 (fcs
->num
- pos
) * sizeof (*leaves
));
194 memmove (numbers
+ pos
+ 1, numbers
+ pos
,
195 (fcs
->num
- pos
) * sizeof (*numbers
));
196 numbers
[pos
] = (FcChar16
) ucs4
;
197 leaves
[pos
] = FcPtrToOffset (leaves
, leaf
);
203 * Locate the leaf containing the specified char, creating it
208 FcCharSetFindLeafCreate (FcCharSet
*fcs
, FcChar32 ucs4
)
213 pos
= FcCharSetFindLeafPos (fcs
, ucs4
);
215 return FcCharSetLeaf(fcs
, pos
);
217 leaf
= calloc (1, sizeof (FcCharLeaf
));
222 if (!FcCharSetPutLeaf (fcs
, ucs4
, leaf
, pos
))
227 FcMemAlloc (FC_MEM_CHARLEAF
, sizeof (FcCharLeaf
));
232 FcCharSetInsertLeaf (FcCharSet
*fcs
, FcChar32 ucs4
, FcCharLeaf
*leaf
)
236 pos
= FcCharSetFindLeafPos (fcs
, ucs4
);
239 FcMemFree (FC_MEM_CHARLEAF
, sizeof (FcCharLeaf
));
240 free (FcCharSetLeaf (fcs
, pos
));
241 FcCharSetLeaves(fcs
)[pos
] = FcPtrToOffset (FcCharSetLeaves(fcs
),
246 return FcCharSetPutLeaf (fcs
, ucs4
, leaf
, pos
);
250 FcCharSetAddChar (FcCharSet
*fcs
, FcChar32 ucs4
)
255 if (fcs
->ref
== FC_REF_CONSTANT
)
257 leaf
= FcCharSetFindLeafCreate (fcs
, ucs4
);
260 b
= &leaf
->map
[(ucs4
& 0xff) >> 5];
261 *b
|= (1 << (ucs4
& 0x1f));
266 * An iterator for the leaves of a charset
269 typedef struct _fcCharSetIter
{
276 * Set iter->leaf to the leaf containing iter->ucs4 or higher
280 FcCharSetIterSet (const FcCharSet
*fcs
, FcCharSetIter
*iter
)
282 int pos
= FcCharSetFindLeafPos (fcs
, iter
->ucs4
);
293 iter
->ucs4
= (FcChar32
) FcCharSetNumbers(fcs
)[pos
] << 8;
295 iter
->leaf
= FcCharSetLeaf(fcs
, pos
);
300 FcCharSetIterNext (const FcCharSet
*fcs
, FcCharSetIter
*iter
)
302 int pos
= iter
->pos
+ 1;
310 iter
->ucs4
= (FcChar32
) FcCharSetNumbers(fcs
)[pos
] << 8;
311 iter
->leaf
= FcCharSetLeaf(fcs
, pos
);
318 FcCharSetIterStart (const FcCharSet
*fcs
, FcCharSetIter
*iter
)
322 FcCharSetIterSet (fcs
, iter
);
326 FcCharSetCopy (FcCharSet
*src
)
328 if (src
->ref
!= FC_REF_CONSTANT
)
331 FcCacheObjectReference (src
);
336 FcCharSetEqual (const FcCharSet
*a
, const FcCharSet
*b
)
338 FcCharSetIter ai
, bi
;
343 for (FcCharSetIterStart (a
, &ai
), FcCharSetIterStart (b
, &bi
);
345 FcCharSetIterNext (a
, &ai
), FcCharSetIterNext (b
, &bi
))
347 if (ai
.ucs4
!= bi
.ucs4
)
349 for (i
= 0; i
< 256/32; i
++)
350 if (ai
.leaf
->map
[i
] != bi
.leaf
->map
[i
])
353 return ai
.leaf
== bi
.leaf
;
357 FcCharSetAddLeaf (FcCharSet
*fcs
,
361 FcCharLeaf
*new = FcCharSetFindLeafCreate (fcs
, ucs4
);
369 FcCharSetOperate (const FcCharSet
*a
,
371 FcBool (*overlap
) (FcCharLeaf
*result
,
372 const FcCharLeaf
*al
,
373 const FcCharLeaf
*bl
),
378 FcCharSetIter ai
, bi
;
380 fcs
= FcCharSetCreate ();
383 FcCharSetIterStart (a
, &ai
);
384 FcCharSetIterStart (b
, &bi
);
385 while ((ai
.leaf
|| (bonly
&& bi
.leaf
)) && (bi
.leaf
|| (aonly
&& ai
.leaf
)))
387 if (ai
.ucs4
< bi
.ucs4
)
391 if (!FcCharSetAddLeaf (fcs
, ai
.ucs4
, ai
.leaf
))
393 FcCharSetIterNext (a
, &ai
);
398 FcCharSetIterSet (a
, &ai
);
401 else if (bi
.ucs4
< ai
.ucs4
)
405 if (!FcCharSetAddLeaf (fcs
, bi
.ucs4
, bi
.leaf
))
407 FcCharSetIterNext (b
, &bi
);
412 FcCharSetIterSet (b
, &bi
);
419 if ((*overlap
) (&leaf
, ai
.leaf
, bi
.leaf
))
421 if (!FcCharSetAddLeaf (fcs
, ai
.ucs4
, &leaf
))
424 FcCharSetIterNext (a
, &ai
);
425 FcCharSetIterNext (b
, &bi
);
430 FcCharSetDestroy (fcs
);
436 FcCharSetIntersectLeaf (FcCharLeaf
*result
,
437 const FcCharLeaf
*al
,
438 const FcCharLeaf
*bl
)
441 FcBool nonempty
= FcFalse
;
443 for (i
= 0; i
< 256/32; i
++)
444 if ((result
->map
[i
] = al
->map
[i
] & bl
->map
[i
]))
450 FcCharSetIntersect (const FcCharSet
*a
, const FcCharSet
*b
)
452 return FcCharSetOperate (a
, b
, FcCharSetIntersectLeaf
, FcFalse
, FcFalse
);
456 FcCharSetUnionLeaf (FcCharLeaf
*result
,
457 const FcCharLeaf
*al
,
458 const FcCharLeaf
*bl
)
462 for (i
= 0; i
< 256/32; i
++)
463 result
->map
[i
] = al
->map
[i
] | bl
->map
[i
];
468 FcCharSetUnion (const FcCharSet
*a
, const FcCharSet
*b
)
470 return FcCharSetOperate (a
, b
, FcCharSetUnionLeaf
, FcTrue
, FcTrue
);
474 FcCharSetMerge (FcCharSet
*a
, const FcCharSet
*b
, FcBool
*changed
)
479 if (a
->ref
== FC_REF_CONSTANT
) {
486 *changed
= !FcCharSetIsSubset(b
, a
);
493 an
= ai
< a
->num
? FcCharSetNumbers(a
)[ai
] : ~0;
494 bn
= FcCharSetNumbers(b
)[bi
];
498 ai
= FcCharSetFindLeafForward (a
, ai
+ 1, bn
);
504 FcCharLeaf
*bl
= FcCharSetLeaf(b
, bi
);
507 if (!FcCharSetAddLeaf (a
, bn
<< 8, bl
))
512 FcCharLeaf
*al
= FcCharSetLeaf(a
, ai
);
513 FcCharSetUnionLeaf (al
, al
, bl
);
525 FcCharSetSubtractLeaf (FcCharLeaf
*result
,
526 const FcCharLeaf
*al
,
527 const FcCharLeaf
*bl
)
530 FcBool nonempty
= FcFalse
;
532 for (i
= 0; i
< 256/32; i
++)
533 if ((result
->map
[i
] = al
->map
[i
] & ~bl
->map
[i
]))
539 FcCharSetSubtract (const FcCharSet
*a
, const FcCharSet
*b
)
541 return FcCharSetOperate (a
, b
, FcCharSetSubtractLeaf
, FcTrue
, FcFalse
);
545 FcCharSetHasChar (const FcCharSet
*fcs
, FcChar32 ucs4
)
547 FcCharLeaf
*leaf
= FcCharSetFindLeaf (fcs
, ucs4
);
550 return (leaf
->map
[(ucs4
& 0xff) >> 5] & (1 << (ucs4
& 0x1f))) != 0;
554 FcCharSetPopCount (FcChar32 c1
)
556 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
557 return __builtin_popcount (c1
);
560 FcChar32 c2
= (c1
>> 1) & 033333333333;
561 c2
= c1
- c2
- ((c2
>> 1) & 033333333333);
562 return (((c2
+ (c2
>> 3)) & 030707070707) % 077);
567 FcCharSetIntersectCount (const FcCharSet
*a
, const FcCharSet
*b
)
569 FcCharSetIter ai
, bi
;
572 FcCharSetIterStart (a
, &ai
);
573 FcCharSetIterStart (b
, &bi
);
574 while (ai
.leaf
&& bi
.leaf
)
576 if (ai
.ucs4
== bi
.ucs4
)
578 FcChar32
*am
= ai
.leaf
->map
;
579 FcChar32
*bm
= bi
.leaf
->map
;
582 count
+= FcCharSetPopCount (*am
++ & *bm
++);
583 FcCharSetIterNext (a
, &ai
);
585 else if (ai
.ucs4
< bi
.ucs4
)
588 FcCharSetIterSet (a
, &ai
);
590 if (bi
.ucs4
< ai
.ucs4
)
593 FcCharSetIterSet (b
, &bi
);
600 FcCharSetCount (const FcCharSet
*a
)
605 for (FcCharSetIterStart (a
, &ai
); ai
.leaf
; FcCharSetIterNext (a
, &ai
))
608 FcChar32
*am
= ai
.leaf
->map
;
611 count
+= FcCharSetPopCount (*am
++);
617 FcCharSetSubtractCount (const FcCharSet
*a
, const FcCharSet
*b
)
619 FcCharSetIter ai
, bi
;
622 FcCharSetIterStart (a
, &ai
);
623 FcCharSetIterStart (b
, &bi
);
626 if (ai
.ucs4
<= bi
.ucs4
)
628 FcChar32
*am
= ai
.leaf
->map
;
630 if (ai
.ucs4
== bi
.ucs4
)
632 FcChar32
*bm
= bi
.leaf
->map
;
634 count
+= FcCharSetPopCount (*am
++ & ~*bm
++);
639 count
+= FcCharSetPopCount (*am
++);
641 FcCharSetIterNext (a
, &ai
);
646 FcCharSetIterSet (b
, &bi
);
653 * return FcTrue iff a is a subset of b
656 FcCharSetIsSubset (const FcCharSet
*a
, const FcCharSet
*b
)
661 if (a
== b
) return FcTrue
;
664 while (ai
< a
->num
&& bi
< b
->num
)
666 an
= FcCharSetNumbers(a
)[ai
];
667 bn
= FcCharSetNumbers(b
)[bi
];
669 * Check matching pages
673 FcChar32
*am
= FcCharSetLeaf(a
, ai
)->map
;
674 FcChar32
*bm
= FcCharSetLeaf(b
, bi
)->map
;
680 * Does am have any bits not in bm?
690 * Does a have any pages not in b?
696 bi
= FcCharSetFindLeafForward (b
, bi
+ 1, an
);
702 * did we look at every page?
708 * These two functions efficiently walk the entire charmap for
709 * other software (like pango) that want their own copy
713 FcCharSetNextPage (const FcCharSet
*a
,
714 FcChar32 map
[FC_CHARSET_MAP_SIZE
],
721 FcCharSetIterSet (a
, &ai
);
723 return FC_CHARSET_DONE
;
726 * Save current information
729 memcpy (map
, ai
.leaf
->map
, sizeof (ai
.leaf
->map
));
733 FcCharSetIterNext (a
, &ai
);
740 FcCharSetFirstPage (const FcCharSet
*a
,
741 FcChar32 map
[FC_CHARSET_MAP_SIZE
],
745 return FcCharSetNextPage (a
, map
, next
);
749 * old coverage API, rather hard to use correctly
753 FcCharSetCoverage (const FcCharSet
*a
, FcChar32 page
, FcChar32
*result
)
758 FcCharSetIterSet (a
, &ai
);
761 memset (result
, '\0', 256 / 8);
766 memcpy (result
, ai
.leaf
->map
, sizeof (ai
.leaf
->map
));
767 FcCharSetIterNext (a
, &ai
);
774 * ASCII representation of charsets.
776 * Each leaf is represented as 9 32-bit values, the code of the first character followed
777 * by 8 32 bit values for the leaf itself. Each value is encoded as 5 ASCII characters,
778 * only 85 different values are used to avoid control characters as well as the other
779 * characters used to encode font names. 85**5 > 2^32 so things work out, but
780 * it's not exactly human readable output. As a special case, 0 is encoded as a space
783 static const unsigned char charToValue
[256] = {
784 /* "" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
785 /* "\b" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
786 /* "\020" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
787 /* "\030" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
788 /* " " */ 0xff, 0x00, 0xff, 0x01, 0x02, 0x03, 0x04, 0xff,
789 /* "(" */ 0x05, 0x06, 0x07, 0x08, 0xff, 0xff, 0x09, 0x0a,
790 /* "0" */ 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12,
791 /* "8" */ 0x13, 0x14, 0xff, 0x15, 0x16, 0xff, 0x17, 0x18,
792 /* "@" */ 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
793 /* "H" */ 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
794 /* "P" */ 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30,
795 /* "X" */ 0x31, 0x32, 0x33, 0x34, 0xff, 0x35, 0x36, 0xff,
796 /* "`" */ 0xff, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d,
797 /* "h" */ 0x3e, 0x3f, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45,
798 /* "p" */ 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d,
799 /* "x" */ 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53, 0x54, 0xff,
800 /* "\200" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
801 /* "\210" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
802 /* "\220" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
803 /* "\230" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
804 /* "\240" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
805 /* "\250" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
806 /* "\260" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
807 /* "\270" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
808 /* "\300" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
809 /* "\310" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
810 /* "\320" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
811 /* "\330" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
812 /* "\340" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
813 /* "\350" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
814 /* "\360" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
815 /* "\370" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
818 static const FcChar8 valueToChar
[0x55] = {
819 /* 0x00 */ '!', '#', '$', '%', '&', '(', ')', '*',
820 /* 0x08 */ '+', '.', '/', '0', '1', '2', '3', '4',
821 /* 0x10 */ '5', '6', '7', '8', '9', ';', '<', '>',
822 /* 0x18 */ '?', '@', 'A', 'B', 'C', 'D', 'E', 'F',
823 /* 0x20 */ 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
824 /* 0x28 */ 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
825 /* 0x30 */ 'W', 'X', 'Y', 'Z', '[', ']', '^', 'a',
826 /* 0x38 */ 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
827 /* 0x40 */ 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q',
828 /* 0x48 */ 'r', 's', 't', 'u', 'v', 'w', 'x', 'y',
829 /* 0x50 */ 'z', '{', '|', '}', '~',
833 FcCharSetParseValue (FcChar8
*string
, FcChar32
*value
)
847 for (i
= 0; i
< 5; i
++)
849 if (!(c
= (FcChar32
) (unsigned char) *string
++))
862 FcCharSetUnparseValue (FcStrBuf
*buf
, FcChar32 value
)
867 return FcStrBufChar (buf
, ' ');
872 FcChar8
*s
= string
+ 5;
874 for (i
= 0; i
< 5; i
++)
876 *--s
= valueToChar
[value
% 85];
879 for (i
= 0; i
< 5; i
++)
880 if (!FcStrBufChar (buf
, *s
++))
887 FcNameParseCharSet (FcChar8
*string
)
896 c
= FcCharSetCreate ();
901 string
= FcCharSetParseValue (string
, &ucs4
);
905 for (i
= 0; i
< 256/32; i
++)
907 string
= FcCharSetParseValue (string
, &temp
.map
[i
]);
914 leaf
= malloc (sizeof (FcCharLeaf
));
918 if (!FcCharSetInsertLeaf (c
, ucs4
, leaf
))
926 FcMemFree (FC_MEM_CHARSET
, c
->num
* sizeof (FcCharLeaf
*));
927 free (FcCharSetLeaves (c
));
931 FcMemFree (FC_MEM_CHARSET
, c
->num
* sizeof (FcChar16
));
932 free (FcCharSetNumbers (c
));
934 FcMemFree (FC_MEM_CHARSET
, sizeof (FcCharSet
));
941 FcNameUnparseCharSet (FcStrBuf
*buf
, const FcCharSet
*c
)
949 for (FcCharSetIterStart (c
, &ci
);
951 FcCharSetIterNext (c
, &ci
))
953 if (!FcCharSetUnparseValue (buf
, ci
.ucs4
))
955 for (i
= 0; i
< 256/32; i
++)
956 if (!FcCharSetUnparseValue (buf
, ci
.leaf
->map
[i
]))
963 FcCharSetIter ci
, checki
;
965 /* null terminate for parser */
966 FcStrBufChar (buf
, '\0');
967 /* step back over null for life after test */
969 check
= FcNameParseCharSet (buf
->buf
+ len
);
970 FcCharSetIterStart (c
, &ci
);
971 FcCharSetIterStart (check
, &checki
);
972 while (ci
.leaf
|| checki
.leaf
)
974 if (ci
.ucs4
< checki
.ucs4
)
976 printf ("Missing leaf node at 0x%x\n", ci
.ucs4
);
977 FcCharSetIterNext (c
, &ci
);
979 else if (checki
.ucs4
< ci
.ucs4
)
981 printf ("Extra leaf node at 0x%x\n", checki
.ucs4
);
982 FcCharSetIterNext (check
, &checki
);
987 FcChar32
*cm
= ci
.leaf
->map
;
988 FcChar32
*checkm
= checki
.leaf
->map
;
990 for (i
= 0; i
< 256; i
+= 32)
993 printf ("Mismatching sets at 0x%08x: 0x%08x != 0x%08x\n",
994 ci
.ucs4
+ i
, *cm
, *checkm
);
998 FcCharSetIterNext (c
, &ci
);
999 FcCharSetIterNext (check
, &checki
);
1002 if ((missing
= FcCharSetSubtractCount (c
, check
)))
1003 printf ("%d missing in reparsed result\n", missing
);
1004 if ((missing
= FcCharSetSubtractCount (check
, c
)))
1005 printf ("%d extra in reparsed result\n", missing
);
1006 FcCharSetDestroy (check
);
1013 typedef struct _FcCharLeafEnt FcCharLeafEnt
;
1015 struct _FcCharLeafEnt
{
1016 FcCharLeafEnt
*next
;
1021 #define FC_CHAR_LEAF_BLOCK (4096 / sizeof (FcCharLeafEnt))
1022 #define FC_CHAR_LEAF_HASH_SIZE 257
1024 typedef struct _FcCharSetEnt FcCharSetEnt
;
1026 struct _FcCharSetEnt
{
1032 typedef struct _FcCharSetOrigEnt FcCharSetOrigEnt
;
1034 struct _FcCharSetOrigEnt
{
1035 FcCharSetOrigEnt
*next
;
1036 const FcCharSet
*orig
;
1037 const FcCharSet
*frozen
;
1040 #define FC_CHAR_SET_HASH_SIZE 67
1042 struct _FcCharSetFreezer
{
1043 FcCharLeafEnt
*leaf_hash_table
[FC_CHAR_LEAF_HASH_SIZE
];
1044 FcCharLeafEnt
**leaf_blocks
;
1045 int leaf_block_count
;
1046 FcCharSetEnt
*set_hash_table
[FC_CHAR_SET_HASH_SIZE
];
1047 FcCharSetOrigEnt
*orig_hash_table
[FC_CHAR_SET_HASH_SIZE
];
1048 FcCharLeafEnt
*current_block
;
1052 int leaves_allocated
;
1053 int charsets_allocated
;
1056 static FcCharLeafEnt
*
1057 FcCharLeafEntCreate (FcCharSetFreezer
*freezer
)
1059 if (!freezer
->leaf_remain
)
1061 FcCharLeafEnt
**newBlocks
;
1063 freezer
->leaf_block_count
++;
1064 newBlocks
= realloc (freezer
->leaf_blocks
, freezer
->leaf_block_count
* sizeof (FcCharLeafEnt
*));
1067 freezer
->leaf_blocks
= newBlocks
;
1068 freezer
->current_block
= freezer
->leaf_blocks
[freezer
->leaf_block_count
-1] = malloc (FC_CHAR_LEAF_BLOCK
* sizeof (FcCharLeafEnt
));
1069 if (!freezer
->current_block
)
1071 FcMemAlloc (FC_MEM_CHARLEAF
, FC_CHAR_LEAF_BLOCK
* sizeof (FcCharLeafEnt
));
1072 freezer
->leaf_remain
= FC_CHAR_LEAF_BLOCK
;
1074 freezer
->leaf_remain
--;
1075 freezer
->leaves_allocated
++;
1076 return freezer
->current_block
++;
1080 FcCharLeafHash (FcCharLeaf
*leaf
)
1085 for (i
= 0; i
< 256/32; i
++)
1086 hash
= ((hash
<< 1) | (hash
>> 31)) ^ leaf
->map
[i
];
1091 FcCharSetFreezeLeaf (FcCharSetFreezer
*freezer
, FcCharLeaf
*leaf
)
1093 FcChar32 hash
= FcCharLeafHash (leaf
);
1094 FcCharLeafEnt
**bucket
= &freezer
->leaf_hash_table
[hash
% FC_CHAR_LEAF_HASH_SIZE
];
1097 for (ent
= *bucket
; ent
; ent
= ent
->next
)
1099 if (ent
->hash
== hash
&& !memcmp (&ent
->leaf
, leaf
, sizeof (FcCharLeaf
)))
1103 ent
= FcCharLeafEntCreate(freezer
);
1108 ent
->next
= *bucket
;
1114 FcCharSetHash (FcCharSet
*fcs
)
1119 /* hash in leaves */
1120 for (i
= 0; i
< fcs
->num
; i
++)
1121 hash
= ((hash
<< 1) | (hash
>> 31)) ^ FcCharLeafHash (FcCharSetLeaf(fcs
,i
));
1122 /* hash in numbers */
1123 for (i
= 0; i
< fcs
->num
; i
++)
1124 hash
= ((hash
<< 1) | (hash
>> 31)) ^ *FcCharSetNumbers(fcs
);
1129 FcCharSetFreezeOrig (FcCharSetFreezer
*freezer
, const FcCharSet
*orig
, const FcCharSet
*frozen
)
1131 FcCharSetOrigEnt
**bucket
= &freezer
->orig_hash_table
[((uintptr_t) orig
) & FC_CHAR_SET_HASH_SIZE
];
1132 FcCharSetOrigEnt
*ent
;
1134 ent
= malloc (sizeof (FcCharSetOrigEnt
));
1138 ent
->frozen
= frozen
;
1139 ent
->next
= *bucket
;
1145 FcCharSetFreezeBase (FcCharSetFreezer
*freezer
, FcCharSet
*fcs
, const FcCharSet
*orig
)
1147 FcChar32 hash
= FcCharSetHash (fcs
);
1148 FcCharSetEnt
**bucket
= &freezer
->set_hash_table
[hash
% FC_CHAR_SET_HASH_SIZE
];
1153 for (ent
= *bucket
; ent
; ent
= ent
->next
)
1155 if (ent
->hash
== hash
&&
1156 ent
->set
.num
== fcs
->num
&&
1157 !memcmp (FcCharSetNumbers(&ent
->set
),
1158 FcCharSetNumbers(fcs
),
1159 fcs
->num
* sizeof (FcChar16
)))
1164 for (i
= 0; i
< fcs
->num
; i
++)
1165 if (FcCharSetLeaf(&ent
->set
, i
) != FcCharSetLeaf(fcs
, i
))
1172 size
= (sizeof (FcCharSetEnt
) +
1173 fcs
->num
* sizeof (FcCharLeaf
*) +
1174 fcs
->num
* sizeof (FcChar16
));
1175 ent
= malloc (size
);
1178 FcMemAlloc (FC_MEM_CHARSET
, size
);
1180 freezer
->charsets_allocated
++;
1182 ent
->set
.ref
= FC_REF_CONSTANT
;
1183 ent
->set
.num
= fcs
->num
;
1186 intptr_t *ent_leaves
;
1188 ent
->set
.leaves_offset
= sizeof (ent
->set
);
1189 ent
->set
.numbers_offset
= (ent
->set
.leaves_offset
+
1190 fcs
->num
* sizeof (intptr_t));
1192 ent_leaves
= FcCharSetLeaves (&ent
->set
);
1193 for (i
= 0; i
< fcs
->num
; i
++)
1194 ent_leaves
[i
] = FcPtrToOffset (ent_leaves
,
1195 FcCharSetLeaf (fcs
, i
));
1196 memcpy (FcCharSetNumbers (&ent
->set
),
1197 FcCharSetNumbers (fcs
),
1198 fcs
->num
* sizeof (FcChar16
));
1202 ent
->set
.leaves_offset
= 0;
1203 ent
->set
.numbers_offset
= 0;
1207 ent
->next
= *bucket
;
1213 static const FcCharSet
*
1214 FcCharSetFindFrozen (FcCharSetFreezer
*freezer
, const FcCharSet
*orig
)
1216 FcCharSetOrigEnt
**bucket
= &freezer
->orig_hash_table
[((uintptr_t) orig
) & FC_CHAR_SET_HASH_SIZE
];
1217 FcCharSetOrigEnt
*ent
;
1219 for (ent
= *bucket
; ent
; ent
= ent
->next
)
1220 if (ent
->orig
== orig
)
1226 FcCharSetFreeze (FcCharSetFreezer
*freezer
, const FcCharSet
*fcs
)
1229 const FcCharSet
*n
= 0;
1233 b
= FcCharSetCreate ();
1236 for (i
= 0; i
< fcs
->num
; i
++)
1238 l
= FcCharSetFreezeLeaf (freezer
, FcCharSetLeaf(fcs
, i
));
1241 if (!FcCharSetInsertLeaf (b
, FcCharSetNumbers(fcs
)[i
] << 8, l
))
1244 n
= FcCharSetFreezeBase (freezer
, b
, fcs
);
1245 if (!FcCharSetFreezeOrig (freezer
, fcs
, n
))
1250 freezer
->charsets_seen
++;
1251 freezer
->leaves_seen
+= fcs
->num
;
1255 FcMemFree (FC_MEM_CHARSET
, b
->num
* sizeof (FcCharLeaf
*));
1256 free (FcCharSetLeaves (b
));
1260 FcMemFree (FC_MEM_CHARSET
, b
->num
* sizeof (FcChar16
));
1261 free (FcCharSetNumbers (b
));
1263 FcMemFree (FC_MEM_CHARSET
, sizeof (FcCharSet
));
1270 FcCharSetFreezerCreate (void)
1272 FcCharSetFreezer
*freezer
;
1274 freezer
= calloc (1, sizeof (FcCharSetFreezer
));
1279 FcCharSetFreezerDestroy (FcCharSetFreezer
*freezer
)
1283 if (FcDebug() & FC_DBG_CACHE
)
1285 printf ("\ncharsets %d -> %d leaves %d -> %d\n",
1286 freezer
->charsets_seen
, freezer
->charsets_allocated
,
1287 freezer
->leaves_seen
, freezer
->leaves_allocated
);
1289 for (i
= 0; i
< FC_CHAR_SET_HASH_SIZE
; i
++)
1291 FcCharSetEnt
*ent
, *next
;
1292 for (ent
= freezer
->set_hash_table
[i
]; ent
; ent
= next
)
1295 FcMemFree (FC_MEM_CHARSET
, (sizeof (FcCharSetEnt
) +
1296 ent
->set
.num
* sizeof (FcCharLeaf
*) +
1297 ent
->set
.num
* sizeof (FcChar16
)));
1302 for (i
= 0; i
< FC_CHAR_SET_HASH_SIZE
; i
++)
1304 FcCharSetOrigEnt
*ent
, *next
;
1305 for (ent
= freezer
->orig_hash_table
[i
]; ent
; ent
= next
)
1312 for (i
= 0; i
< freezer
->leaf_block_count
; i
++)
1314 free (freezer
->leaf_blocks
[i
]);
1315 FcMemFree (FC_MEM_CHARLEAF
, FC_CHAR_LEAF_BLOCK
* sizeof (FcCharLeafEnt
));
1318 free (freezer
->leaf_blocks
);
1323 FcCharSetSerializeAlloc (FcSerialize
*serialize
, const FcCharSet
*cs
)
1329 if (cs
->ref
!= FC_REF_CONSTANT
)
1331 if (!serialize
->cs_freezer
)
1333 serialize
->cs_freezer
= FcCharSetFreezerCreate ();
1334 if (!serialize
->cs_freezer
)
1337 if (FcCharSetFindFrozen (serialize
->cs_freezer
, cs
))
1340 cs
= FcCharSetFreeze (serialize
->cs_freezer
, cs
);
1343 leaves
= FcCharSetLeaves (cs
);
1344 numbers
= FcCharSetNumbers (cs
);
1346 if (!FcSerializeAlloc (serialize
, cs
, sizeof (FcCharSet
)))
1348 if (!FcSerializeAlloc (serialize
, leaves
, cs
->num
* sizeof (intptr_t)))
1350 if (!FcSerializeAlloc (serialize
, numbers
, cs
->num
* sizeof (FcChar16
)))
1352 for (i
= 0; i
< cs
->num
; i
++)
1353 if (!FcSerializeAlloc (serialize
, FcCharSetLeaf(cs
, i
),
1354 sizeof (FcCharLeaf
)))
1360 FcCharSetSerialize(FcSerialize
*serialize
, const FcCharSet
*cs
)
1362 FcCharSet
*cs_serialized
;
1363 intptr_t *leaves
, *leaves_serialized
;
1364 FcChar16
*numbers
, *numbers_serialized
;
1365 FcCharLeaf
*leaf
, *leaf_serialized
;
1368 if (cs
->ref
!= FC_REF_CONSTANT
&& serialize
->cs_freezer
)
1370 cs
= FcCharSetFindFrozen (serialize
->cs_freezer
, cs
);
1375 cs_serialized
= FcSerializePtr (serialize
, cs
);
1379 cs_serialized
->ref
= FC_REF_CONSTANT
;
1380 cs_serialized
->num
= cs
->num
;
1384 leaves
= FcCharSetLeaves (cs
);
1385 leaves_serialized
= FcSerializePtr (serialize
, leaves
);
1386 if (!leaves_serialized
)
1389 cs_serialized
->leaves_offset
= FcPtrToOffset (cs_serialized
,
1392 numbers
= FcCharSetNumbers (cs
);
1393 numbers_serialized
= FcSerializePtr (serialize
, numbers
);
1397 cs_serialized
->numbers_offset
= FcPtrToOffset (cs_serialized
,
1398 numbers_serialized
);
1400 for (i
= 0; i
< cs
->num
; i
++)
1402 leaf
= FcCharSetLeaf (cs
, i
);
1403 leaf_serialized
= FcSerializePtr (serialize
, leaf
);
1404 if (!leaf_serialized
)
1406 *leaf_serialized
= *leaf
;
1407 leaves_serialized
[i
] = FcPtrToOffset (leaves_serialized
,
1409 numbers_serialized
[i
] = numbers
[i
];
1414 cs_serialized
->leaves_offset
= 0;
1415 cs_serialized
->numbers_offset
= 0;
1418 return cs_serialized
;
1420 #define __fccharset__
1421 #include "fcaliastail.h"
1422 #undef __fccharset__