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 the author(s) not be used in
11 * advertising or publicity pertaining to distribution of the software without
12 * specific, written prior permission. The authors make 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 FcCharSetDelChar (FcCharSet
*fcs
, FcChar32 ucs4
)
271 if (fcs
->ref
== FC_REF_CONSTANT
)
273 leaf
= FcCharSetFindLeaf (fcs
, ucs4
);
276 b
= &leaf
->map
[(ucs4
& 0xff) >> 5];
277 *b
&= ~(1 << (ucs4
& 0x1f));
278 /* We don't bother removing the leaf if it's empty */
283 * An iterator for the leaves of a charset
286 typedef struct _fcCharSetIter
{
293 * Set iter->leaf to the leaf containing iter->ucs4 or higher
297 FcCharSetIterSet (const FcCharSet
*fcs
, FcCharSetIter
*iter
)
299 int pos
= FcCharSetFindLeafPos (fcs
, iter
->ucs4
);
310 iter
->ucs4
= (FcChar32
) FcCharSetNumbers(fcs
)[pos
] << 8;
312 iter
->leaf
= FcCharSetLeaf(fcs
, pos
);
317 FcCharSetIterNext (const FcCharSet
*fcs
, FcCharSetIter
*iter
)
319 int pos
= iter
->pos
+ 1;
327 iter
->ucs4
= (FcChar32
) FcCharSetNumbers(fcs
)[pos
] << 8;
328 iter
->leaf
= FcCharSetLeaf(fcs
, pos
);
335 FcCharSetIterStart (const FcCharSet
*fcs
, FcCharSetIter
*iter
)
339 FcCharSetIterSet (fcs
, iter
);
343 FcCharSetCopy (FcCharSet
*src
)
345 if (src
->ref
!= FC_REF_CONSTANT
)
348 FcCacheObjectReference (src
);
353 FcCharSetEqual (const FcCharSet
*a
, const FcCharSet
*b
)
355 FcCharSetIter ai
, bi
;
360 for (FcCharSetIterStart (a
, &ai
), FcCharSetIterStart (b
, &bi
);
362 FcCharSetIterNext (a
, &ai
), FcCharSetIterNext (b
, &bi
))
364 if (ai
.ucs4
!= bi
.ucs4
)
366 for (i
= 0; i
< 256/32; i
++)
367 if (ai
.leaf
->map
[i
] != bi
.leaf
->map
[i
])
370 return ai
.leaf
== bi
.leaf
;
374 FcCharSetAddLeaf (FcCharSet
*fcs
,
378 FcCharLeaf
*new = FcCharSetFindLeafCreate (fcs
, ucs4
);
386 FcCharSetOperate (const FcCharSet
*a
,
388 FcBool (*overlap
) (FcCharLeaf
*result
,
389 const FcCharLeaf
*al
,
390 const FcCharLeaf
*bl
),
395 FcCharSetIter ai
, bi
;
397 fcs
= FcCharSetCreate ();
400 FcCharSetIterStart (a
, &ai
);
401 FcCharSetIterStart (b
, &bi
);
402 while ((ai
.leaf
|| (bonly
&& bi
.leaf
)) && (bi
.leaf
|| (aonly
&& ai
.leaf
)))
404 if (ai
.ucs4
< bi
.ucs4
)
408 if (!FcCharSetAddLeaf (fcs
, ai
.ucs4
, ai
.leaf
))
410 FcCharSetIterNext (a
, &ai
);
415 FcCharSetIterSet (a
, &ai
);
418 else if (bi
.ucs4
< ai
.ucs4
)
422 if (!FcCharSetAddLeaf (fcs
, bi
.ucs4
, bi
.leaf
))
424 FcCharSetIterNext (b
, &bi
);
429 FcCharSetIterSet (b
, &bi
);
436 if ((*overlap
) (&leaf
, ai
.leaf
, bi
.leaf
))
438 if (!FcCharSetAddLeaf (fcs
, ai
.ucs4
, &leaf
))
441 FcCharSetIterNext (a
, &ai
);
442 FcCharSetIterNext (b
, &bi
);
447 FcCharSetDestroy (fcs
);
453 FcCharSetIntersectLeaf (FcCharLeaf
*result
,
454 const FcCharLeaf
*al
,
455 const FcCharLeaf
*bl
)
458 FcBool nonempty
= FcFalse
;
460 for (i
= 0; i
< 256/32; i
++)
461 if ((result
->map
[i
] = al
->map
[i
] & bl
->map
[i
]))
467 FcCharSetIntersect (const FcCharSet
*a
, const FcCharSet
*b
)
469 return FcCharSetOperate (a
, b
, FcCharSetIntersectLeaf
, FcFalse
, FcFalse
);
473 FcCharSetUnionLeaf (FcCharLeaf
*result
,
474 const FcCharLeaf
*al
,
475 const FcCharLeaf
*bl
)
479 for (i
= 0; i
< 256/32; i
++)
480 result
->map
[i
] = al
->map
[i
] | bl
->map
[i
];
485 FcCharSetUnion (const FcCharSet
*a
, const FcCharSet
*b
)
487 return FcCharSetOperate (a
, b
, FcCharSetUnionLeaf
, FcTrue
, FcTrue
);
491 FcCharSetMerge (FcCharSet
*a
, const FcCharSet
*b
, FcBool
*changed
)
496 if (a
->ref
== FC_REF_CONSTANT
) {
503 *changed
= !FcCharSetIsSubset(b
, a
);
510 an
= ai
< a
->num
? FcCharSetNumbers(a
)[ai
] : ~0;
511 bn
= FcCharSetNumbers(b
)[bi
];
515 ai
= FcCharSetFindLeafForward (a
, ai
+ 1, bn
);
521 FcCharLeaf
*bl
= FcCharSetLeaf(b
, bi
);
524 if (!FcCharSetAddLeaf (a
, bn
<< 8, bl
))
529 FcCharLeaf
*al
= FcCharSetLeaf(a
, ai
);
530 FcCharSetUnionLeaf (al
, al
, bl
);
542 FcCharSetSubtractLeaf (FcCharLeaf
*result
,
543 const FcCharLeaf
*al
,
544 const FcCharLeaf
*bl
)
547 FcBool nonempty
= FcFalse
;
549 for (i
= 0; i
< 256/32; i
++)
550 if ((result
->map
[i
] = al
->map
[i
] & ~bl
->map
[i
]))
556 FcCharSetSubtract (const FcCharSet
*a
, const FcCharSet
*b
)
558 return FcCharSetOperate (a
, b
, FcCharSetSubtractLeaf
, FcTrue
, FcFalse
);
562 FcCharSetHasChar (const FcCharSet
*fcs
, FcChar32 ucs4
)
564 FcCharLeaf
*leaf
= FcCharSetFindLeaf (fcs
, ucs4
);
567 return (leaf
->map
[(ucs4
& 0xff) >> 5] & (1 << (ucs4
& 0x1f))) != 0;
571 FcCharSetPopCount (FcChar32 c1
)
573 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
574 return __builtin_popcount (c1
);
577 FcChar32 c2
= (c1
>> 1) & 033333333333;
578 c2
= c1
- c2
- ((c2
>> 1) & 033333333333);
579 return (((c2
+ (c2
>> 3)) & 030707070707) % 077);
584 FcCharSetIntersectCount (const FcCharSet
*a
, const FcCharSet
*b
)
586 FcCharSetIter ai
, bi
;
589 FcCharSetIterStart (a
, &ai
);
590 FcCharSetIterStart (b
, &bi
);
591 while (ai
.leaf
&& bi
.leaf
)
593 if (ai
.ucs4
== bi
.ucs4
)
595 FcChar32
*am
= ai
.leaf
->map
;
596 FcChar32
*bm
= bi
.leaf
->map
;
599 count
+= FcCharSetPopCount (*am
++ & *bm
++);
600 FcCharSetIterNext (a
, &ai
);
602 else if (ai
.ucs4
< bi
.ucs4
)
605 FcCharSetIterSet (a
, &ai
);
607 if (bi
.ucs4
< ai
.ucs4
)
610 FcCharSetIterSet (b
, &bi
);
617 FcCharSetCount (const FcCharSet
*a
)
622 for (FcCharSetIterStart (a
, &ai
); ai
.leaf
; FcCharSetIterNext (a
, &ai
))
625 FcChar32
*am
= ai
.leaf
->map
;
628 count
+= FcCharSetPopCount (*am
++);
634 FcCharSetSubtractCount (const FcCharSet
*a
, const FcCharSet
*b
)
636 FcCharSetIter ai
, bi
;
639 FcCharSetIterStart (a
, &ai
);
640 FcCharSetIterStart (b
, &bi
);
643 if (ai
.ucs4
<= bi
.ucs4
)
645 FcChar32
*am
= ai
.leaf
->map
;
647 if (ai
.ucs4
== bi
.ucs4
)
649 FcChar32
*bm
= bi
.leaf
->map
;
651 count
+= FcCharSetPopCount (*am
++ & ~*bm
++);
656 count
+= FcCharSetPopCount (*am
++);
658 FcCharSetIterNext (a
, &ai
);
663 FcCharSetIterSet (b
, &bi
);
670 * return FcTrue iff a is a subset of b
673 FcCharSetIsSubset (const FcCharSet
*a
, const FcCharSet
*b
)
678 if (a
== b
) return FcTrue
;
681 while (ai
< a
->num
&& bi
< b
->num
)
683 an
= FcCharSetNumbers(a
)[ai
];
684 bn
= FcCharSetNumbers(b
)[bi
];
686 * Check matching pages
690 FcChar32
*am
= FcCharSetLeaf(a
, ai
)->map
;
691 FcChar32
*bm
= FcCharSetLeaf(b
, bi
)->map
;
697 * Does am have any bits not in bm?
707 * Does a have any pages not in b?
713 bi
= FcCharSetFindLeafForward (b
, bi
+ 1, an
);
719 * did we look at every page?
725 * These two functions efficiently walk the entire charmap for
726 * other software (like pango) that want their own copy
730 FcCharSetNextPage (const FcCharSet
*a
,
731 FcChar32 map
[FC_CHARSET_MAP_SIZE
],
738 FcCharSetIterSet (a
, &ai
);
740 return FC_CHARSET_DONE
;
743 * Save current information
746 memcpy (map
, ai
.leaf
->map
, sizeof (ai
.leaf
->map
));
750 FcCharSetIterNext (a
, &ai
);
757 FcCharSetFirstPage (const FcCharSet
*a
,
758 FcChar32 map
[FC_CHARSET_MAP_SIZE
],
762 return FcCharSetNextPage (a
, map
, next
);
766 * old coverage API, rather hard to use correctly
770 FcCharSetCoverage (const FcCharSet
*a
, FcChar32 page
, FcChar32
*result
)
775 FcCharSetIterSet (a
, &ai
);
778 memset (result
, '\0', 256 / 8);
783 memcpy (result
, ai
.leaf
->map
, sizeof (ai
.leaf
->map
));
784 FcCharSetIterNext (a
, &ai
);
791 * ASCII representation of charsets.
793 * Each leaf is represented as 9 32-bit values, the code of the first character followed
794 * by 8 32 bit values for the leaf itself. Each value is encoded as 5 ASCII characters,
795 * only 85 different values are used to avoid control characters as well as the other
796 * characters used to encode font names. 85**5 > 2^32 so things work out, but
797 * it's not exactly human readable output. As a special case, 0 is encoded as a space
800 static const unsigned char charToValue
[256] = {
801 /* "" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
802 /* "\b" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
803 /* "\020" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
804 /* "\030" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
805 /* " " */ 0xff, 0x00, 0xff, 0x01, 0x02, 0x03, 0x04, 0xff,
806 /* "(" */ 0x05, 0x06, 0x07, 0x08, 0xff, 0xff, 0x09, 0x0a,
807 /* "0" */ 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12,
808 /* "8" */ 0x13, 0x14, 0xff, 0x15, 0x16, 0xff, 0x17, 0x18,
809 /* "@" */ 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
810 /* "H" */ 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
811 /* "P" */ 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30,
812 /* "X" */ 0x31, 0x32, 0x33, 0x34, 0xff, 0x35, 0x36, 0xff,
813 /* "`" */ 0xff, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d,
814 /* "h" */ 0x3e, 0x3f, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45,
815 /* "p" */ 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d,
816 /* "x" */ 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53, 0x54, 0xff,
817 /* "\200" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
818 /* "\210" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
819 /* "\220" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
820 /* "\230" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
821 /* "\240" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
822 /* "\250" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
823 /* "\260" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
824 /* "\270" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
825 /* "\300" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
826 /* "\310" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
827 /* "\320" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
828 /* "\330" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
829 /* "\340" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
830 /* "\350" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
831 /* "\360" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
832 /* "\370" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
835 static const FcChar8 valueToChar
[0x55] = {
836 /* 0x00 */ '!', '#', '$', '%', '&', '(', ')', '*',
837 /* 0x08 */ '+', '.', '/', '0', '1', '2', '3', '4',
838 /* 0x10 */ '5', '6', '7', '8', '9', ';', '<', '>',
839 /* 0x18 */ '?', '@', 'A', 'B', 'C', 'D', 'E', 'F',
840 /* 0x20 */ 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
841 /* 0x28 */ 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
842 /* 0x30 */ 'W', 'X', 'Y', 'Z', '[', ']', '^', 'a',
843 /* 0x38 */ 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
844 /* 0x40 */ 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q',
845 /* 0x48 */ 'r', 's', 't', 'u', 'v', 'w', 'x', 'y',
846 /* 0x50 */ 'z', '{', '|', '}', '~',
850 FcCharSetParseValue (FcChar8
*string
, FcChar32
*value
)
864 for (i
= 0; i
< 5; i
++)
866 if (!(c
= (FcChar32
) (unsigned char) *string
++))
879 FcCharSetUnparseValue (FcStrBuf
*buf
, FcChar32 value
)
884 return FcStrBufChar (buf
, ' ');
889 FcChar8
*s
= string
+ 5;
891 for (i
= 0; i
< 5; i
++)
893 *--s
= valueToChar
[value
% 85];
896 for (i
= 0; i
< 5; i
++)
897 if (!FcStrBufChar (buf
, *s
++))
904 FcNameParseCharSet (FcChar8
*string
)
913 c
= FcCharSetCreate ();
918 string
= FcCharSetParseValue (string
, &ucs4
);
922 for (i
= 0; i
< 256/32; i
++)
924 string
= FcCharSetParseValue (string
, &temp
.map
[i
]);
931 leaf
= malloc (sizeof (FcCharLeaf
));
935 if (!FcCharSetInsertLeaf (c
, ucs4
, leaf
))
943 FcMemFree (FC_MEM_CHARSET
, c
->num
* sizeof (FcCharLeaf
*));
944 free (FcCharSetLeaves (c
));
948 FcMemFree (FC_MEM_CHARSET
, c
->num
* sizeof (FcChar16
));
949 free (FcCharSetNumbers (c
));
951 FcMemFree (FC_MEM_CHARSET
, sizeof (FcCharSet
));
958 FcNameUnparseCharSet (FcStrBuf
*buf
, const FcCharSet
*c
)
966 for (FcCharSetIterStart (c
, &ci
);
968 FcCharSetIterNext (c
, &ci
))
970 if (!FcCharSetUnparseValue (buf
, ci
.ucs4
))
972 for (i
= 0; i
< 256/32; i
++)
973 if (!FcCharSetUnparseValue (buf
, ci
.leaf
->map
[i
]))
980 FcCharSetIter ci
, checki
;
982 /* null terminate for parser */
983 FcStrBufChar (buf
, '\0');
984 /* step back over null for life after test */
986 check
= FcNameParseCharSet (buf
->buf
+ len
);
987 FcCharSetIterStart (c
, &ci
);
988 FcCharSetIterStart (check
, &checki
);
989 while (ci
.leaf
|| checki
.leaf
)
991 if (ci
.ucs4
< checki
.ucs4
)
993 printf ("Missing leaf node at 0x%x\n", ci
.ucs4
);
994 FcCharSetIterNext (c
, &ci
);
996 else if (checki
.ucs4
< ci
.ucs4
)
998 printf ("Extra leaf node at 0x%x\n", checki
.ucs4
);
999 FcCharSetIterNext (check
, &checki
);
1004 FcChar32
*cm
= ci
.leaf
->map
;
1005 FcChar32
*checkm
= checki
.leaf
->map
;
1007 for (i
= 0; i
< 256; i
+= 32)
1010 printf ("Mismatching sets at 0x%08x: 0x%08x != 0x%08x\n",
1011 ci
.ucs4
+ i
, *cm
, *checkm
);
1015 FcCharSetIterNext (c
, &ci
);
1016 FcCharSetIterNext (check
, &checki
);
1019 if ((missing
= FcCharSetSubtractCount (c
, check
)))
1020 printf ("%d missing in reparsed result\n", missing
);
1021 if ((missing
= FcCharSetSubtractCount (check
, c
)))
1022 printf ("%d extra in reparsed result\n", missing
);
1023 FcCharSetDestroy (check
);
1030 typedef struct _FcCharLeafEnt FcCharLeafEnt
;
1032 struct _FcCharLeafEnt
{
1033 FcCharLeafEnt
*next
;
1038 #define FC_CHAR_LEAF_BLOCK (4096 / sizeof (FcCharLeafEnt))
1039 #define FC_CHAR_LEAF_HASH_SIZE 257
1041 typedef struct _FcCharSetEnt FcCharSetEnt
;
1043 struct _FcCharSetEnt
{
1049 typedef struct _FcCharSetOrigEnt FcCharSetOrigEnt
;
1051 struct _FcCharSetOrigEnt
{
1052 FcCharSetOrigEnt
*next
;
1053 const FcCharSet
*orig
;
1054 const FcCharSet
*frozen
;
1057 #define FC_CHAR_SET_HASH_SIZE 67
1059 struct _FcCharSetFreezer
{
1060 FcCharLeafEnt
*leaf_hash_table
[FC_CHAR_LEAF_HASH_SIZE
];
1061 FcCharLeafEnt
**leaf_blocks
;
1062 int leaf_block_count
;
1063 FcCharSetEnt
*set_hash_table
[FC_CHAR_SET_HASH_SIZE
];
1064 FcCharSetOrigEnt
*orig_hash_table
[FC_CHAR_SET_HASH_SIZE
];
1065 FcCharLeafEnt
*current_block
;
1069 int leaves_allocated
;
1070 int charsets_allocated
;
1073 static FcCharLeafEnt
*
1074 FcCharLeafEntCreate (FcCharSetFreezer
*freezer
)
1076 if (!freezer
->leaf_remain
)
1078 FcCharLeafEnt
**newBlocks
;
1080 freezer
->leaf_block_count
++;
1081 newBlocks
= realloc (freezer
->leaf_blocks
, freezer
->leaf_block_count
* sizeof (FcCharLeafEnt
*));
1084 freezer
->leaf_blocks
= newBlocks
;
1085 freezer
->current_block
= freezer
->leaf_blocks
[freezer
->leaf_block_count
-1] = malloc (FC_CHAR_LEAF_BLOCK
* sizeof (FcCharLeafEnt
));
1086 if (!freezer
->current_block
)
1088 FcMemAlloc (FC_MEM_CHARLEAF
, FC_CHAR_LEAF_BLOCK
* sizeof (FcCharLeafEnt
));
1089 freezer
->leaf_remain
= FC_CHAR_LEAF_BLOCK
;
1091 freezer
->leaf_remain
--;
1092 freezer
->leaves_allocated
++;
1093 return freezer
->current_block
++;
1097 FcCharLeafHash (FcCharLeaf
*leaf
)
1102 for (i
= 0; i
< 256/32; i
++)
1103 hash
= ((hash
<< 1) | (hash
>> 31)) ^ leaf
->map
[i
];
1108 FcCharSetFreezeLeaf (FcCharSetFreezer
*freezer
, FcCharLeaf
*leaf
)
1110 FcChar32 hash
= FcCharLeafHash (leaf
);
1111 FcCharLeafEnt
**bucket
= &freezer
->leaf_hash_table
[hash
% FC_CHAR_LEAF_HASH_SIZE
];
1114 for (ent
= *bucket
; ent
; ent
= ent
->next
)
1116 if (ent
->hash
== hash
&& !memcmp (&ent
->leaf
, leaf
, sizeof (FcCharLeaf
)))
1120 ent
= FcCharLeafEntCreate(freezer
);
1125 ent
->next
= *bucket
;
1131 FcCharSetHash (FcCharSet
*fcs
)
1136 /* hash in leaves */
1137 for (i
= 0; i
< fcs
->num
; i
++)
1138 hash
= ((hash
<< 1) | (hash
>> 31)) ^ FcCharLeafHash (FcCharSetLeaf(fcs
,i
));
1139 /* hash in numbers */
1140 for (i
= 0; i
< fcs
->num
; i
++)
1141 hash
= ((hash
<< 1) | (hash
>> 31)) ^ *FcCharSetNumbers(fcs
);
1146 FcCharSetFreezeOrig (FcCharSetFreezer
*freezer
, const FcCharSet
*orig
, const FcCharSet
*frozen
)
1148 FcCharSetOrigEnt
**bucket
= &freezer
->orig_hash_table
[((uintptr_t) orig
) & FC_CHAR_SET_HASH_SIZE
];
1149 FcCharSetOrigEnt
*ent
;
1151 ent
= malloc (sizeof (FcCharSetOrigEnt
));
1155 ent
->frozen
= frozen
;
1156 ent
->next
= *bucket
;
1162 FcCharSetFreezeBase (FcCharSetFreezer
*freezer
, FcCharSet
*fcs
, const FcCharSet
*orig
)
1164 FcChar32 hash
= FcCharSetHash (fcs
);
1165 FcCharSetEnt
**bucket
= &freezer
->set_hash_table
[hash
% FC_CHAR_SET_HASH_SIZE
];
1170 for (ent
= *bucket
; ent
; ent
= ent
->next
)
1172 if (ent
->hash
== hash
&&
1173 ent
->set
.num
== fcs
->num
&&
1174 !memcmp (FcCharSetNumbers(&ent
->set
),
1175 FcCharSetNumbers(fcs
),
1176 fcs
->num
* sizeof (FcChar16
)))
1181 for (i
= 0; i
< fcs
->num
; i
++)
1182 if (FcCharSetLeaf(&ent
->set
, i
) != FcCharSetLeaf(fcs
, i
))
1189 size
= (sizeof (FcCharSetEnt
) +
1190 fcs
->num
* sizeof (FcCharLeaf
*) +
1191 fcs
->num
* sizeof (FcChar16
));
1192 ent
= malloc (size
);
1195 FcMemAlloc (FC_MEM_CHARSET
, size
);
1197 freezer
->charsets_allocated
++;
1199 ent
->set
.ref
= FC_REF_CONSTANT
;
1200 ent
->set
.num
= fcs
->num
;
1203 intptr_t *ent_leaves
;
1205 ent
->set
.leaves_offset
= sizeof (ent
->set
);
1206 ent
->set
.numbers_offset
= (ent
->set
.leaves_offset
+
1207 fcs
->num
* sizeof (intptr_t));
1209 ent_leaves
= FcCharSetLeaves (&ent
->set
);
1210 for (i
= 0; i
< fcs
->num
; i
++)
1211 ent_leaves
[i
] = FcPtrToOffset (ent_leaves
,
1212 FcCharSetLeaf (fcs
, i
));
1213 memcpy (FcCharSetNumbers (&ent
->set
),
1214 FcCharSetNumbers (fcs
),
1215 fcs
->num
* sizeof (FcChar16
));
1219 ent
->set
.leaves_offset
= 0;
1220 ent
->set
.numbers_offset
= 0;
1224 ent
->next
= *bucket
;
1230 static const FcCharSet
*
1231 FcCharSetFindFrozen (FcCharSetFreezer
*freezer
, const FcCharSet
*orig
)
1233 FcCharSetOrigEnt
**bucket
= &freezer
->orig_hash_table
[((uintptr_t) orig
) & FC_CHAR_SET_HASH_SIZE
];
1234 FcCharSetOrigEnt
*ent
;
1236 for (ent
= *bucket
; ent
; ent
= ent
->next
)
1237 if (ent
->orig
== orig
)
1243 FcCharSetFreeze (FcCharSetFreezer
*freezer
, const FcCharSet
*fcs
)
1246 const FcCharSet
*n
= 0;
1250 b
= FcCharSetCreate ();
1253 for (i
= 0; i
< fcs
->num
; i
++)
1255 l
= FcCharSetFreezeLeaf (freezer
, FcCharSetLeaf(fcs
, i
));
1258 if (!FcCharSetInsertLeaf (b
, FcCharSetNumbers(fcs
)[i
] << 8, l
))
1261 n
= FcCharSetFreezeBase (freezer
, b
, fcs
);
1262 if (!FcCharSetFreezeOrig (freezer
, fcs
, n
))
1267 freezer
->charsets_seen
++;
1268 freezer
->leaves_seen
+= fcs
->num
;
1272 FcMemFree (FC_MEM_CHARSET
, b
->num
* sizeof (FcCharLeaf
*));
1273 free (FcCharSetLeaves (b
));
1277 FcMemFree (FC_MEM_CHARSET
, b
->num
* sizeof (FcChar16
));
1278 free (FcCharSetNumbers (b
));
1280 FcMemFree (FC_MEM_CHARSET
, sizeof (FcCharSet
));
1287 FcCharSetFreezerCreate (void)
1289 FcCharSetFreezer
*freezer
;
1291 freezer
= calloc (1, sizeof (FcCharSetFreezer
));
1296 FcCharSetFreezerDestroy (FcCharSetFreezer
*freezer
)
1300 if (FcDebug() & FC_DBG_CACHE
)
1302 printf ("\ncharsets %d -> %d leaves %d -> %d\n",
1303 freezer
->charsets_seen
, freezer
->charsets_allocated
,
1304 freezer
->leaves_seen
, freezer
->leaves_allocated
);
1306 for (i
= 0; i
< FC_CHAR_SET_HASH_SIZE
; i
++)
1308 FcCharSetEnt
*ent
, *next
;
1309 for (ent
= freezer
->set_hash_table
[i
]; ent
; ent
= next
)
1312 FcMemFree (FC_MEM_CHARSET
, (sizeof (FcCharSetEnt
) +
1313 ent
->set
.num
* sizeof (FcCharLeaf
*) +
1314 ent
->set
.num
* sizeof (FcChar16
)));
1319 for (i
= 0; i
< FC_CHAR_SET_HASH_SIZE
; i
++)
1321 FcCharSetOrigEnt
*ent
, *next
;
1322 for (ent
= freezer
->orig_hash_table
[i
]; ent
; ent
= next
)
1329 for (i
= 0; i
< freezer
->leaf_block_count
; i
++)
1331 free (freezer
->leaf_blocks
[i
]);
1332 FcMemFree (FC_MEM_CHARLEAF
, FC_CHAR_LEAF_BLOCK
* sizeof (FcCharLeafEnt
));
1335 free (freezer
->leaf_blocks
);
1340 FcCharSetSerializeAlloc (FcSerialize
*serialize
, const FcCharSet
*cs
)
1346 if (cs
->ref
!= FC_REF_CONSTANT
)
1348 if (!serialize
->cs_freezer
)
1350 serialize
->cs_freezer
= FcCharSetFreezerCreate ();
1351 if (!serialize
->cs_freezer
)
1354 if (FcCharSetFindFrozen (serialize
->cs_freezer
, cs
))
1357 cs
= FcCharSetFreeze (serialize
->cs_freezer
, cs
);
1360 leaves
= FcCharSetLeaves (cs
);
1361 numbers
= FcCharSetNumbers (cs
);
1363 if (!FcSerializeAlloc (serialize
, cs
, sizeof (FcCharSet
)))
1365 if (!FcSerializeAlloc (serialize
, leaves
, cs
->num
* sizeof (intptr_t)))
1367 if (!FcSerializeAlloc (serialize
, numbers
, cs
->num
* sizeof (FcChar16
)))
1369 for (i
= 0; i
< cs
->num
; i
++)
1370 if (!FcSerializeAlloc (serialize
, FcCharSetLeaf(cs
, i
),
1371 sizeof (FcCharLeaf
)))
1377 FcCharSetSerialize(FcSerialize
*serialize
, const FcCharSet
*cs
)
1379 FcCharSet
*cs_serialized
;
1380 intptr_t *leaves
, *leaves_serialized
;
1381 FcChar16
*numbers
, *numbers_serialized
;
1382 FcCharLeaf
*leaf
, *leaf_serialized
;
1385 if (cs
->ref
!= FC_REF_CONSTANT
&& serialize
->cs_freezer
)
1387 cs
= FcCharSetFindFrozen (serialize
->cs_freezer
, cs
);
1392 cs_serialized
= FcSerializePtr (serialize
, cs
);
1396 cs_serialized
->ref
= FC_REF_CONSTANT
;
1397 cs_serialized
->num
= cs
->num
;
1401 leaves
= FcCharSetLeaves (cs
);
1402 leaves_serialized
= FcSerializePtr (serialize
, leaves
);
1403 if (!leaves_serialized
)
1406 cs_serialized
->leaves_offset
= FcPtrToOffset (cs_serialized
,
1409 numbers
= FcCharSetNumbers (cs
);
1410 numbers_serialized
= FcSerializePtr (serialize
, numbers
);
1414 cs_serialized
->numbers_offset
= FcPtrToOffset (cs_serialized
,
1415 numbers_serialized
);
1417 for (i
= 0; i
< cs
->num
; i
++)
1419 leaf
= FcCharSetLeaf (cs
, i
);
1420 leaf_serialized
= FcSerializePtr (serialize
, leaf
);
1421 if (!leaf_serialized
)
1423 *leaf_serialized
= *leaf
;
1424 leaves_serialized
[i
] = FcPtrToOffset (leaves_serialized
,
1426 numbers_serialized
[i
] = numbers
[i
];
1431 cs_serialized
->leaves_offset
= 0;
1432 cs_serialized
->numbers_offset
= 0;
1435 return cs_serialized
;
1437 #define __fccharset__
1438 #include "fcaliastail.h"
1439 #undef __fccharset__