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
);
147 /* XXX We can't handle Unicode values in Plane 16 */
151 if (FC_IS_ZERO_OR_POWER_OF_TWO (fcs
->num
))
155 unsigned int alloced
= 8;
156 leaves
= malloc (alloced
* sizeof (*leaves
));
157 numbers
= malloc (alloced
* sizeof (*numbers
));
158 FcMemAlloc (FC_MEM_CHARSET
, alloced
* sizeof (*leaves
));
159 FcMemAlloc (FC_MEM_CHARSET
, alloced
* sizeof (*numbers
));
163 unsigned int alloced
= fcs
->num
;
164 intptr_t *new_leaves
, distance
;
166 FcMemFree (FC_MEM_CHARSET
, alloced
* sizeof (*leaves
));
167 FcMemFree (FC_MEM_CHARSET
, alloced
* sizeof (*numbers
));
170 new_leaves
= realloc (leaves
, alloced
* sizeof (*leaves
));
171 numbers
= realloc (numbers
, alloced
* sizeof (*numbers
));
173 FcMemAlloc (FC_MEM_CHARSET
, alloced
* sizeof (*leaves
));
174 FcMemAlloc (FC_MEM_CHARSET
, alloced
* sizeof (*numbers
));
176 distance
= (intptr_t) new_leaves
- (intptr_t) leaves
;
177 if (new_leaves
&& distance
)
180 for (i
= 0; i
< fcs
->num
; i
++)
181 new_leaves
[i
] -= distance
;
186 if (!leaves
|| !numbers
)
189 fcs
->leaves_offset
= FcPtrToOffset (fcs
, leaves
);
190 fcs
->numbers_offset
= FcPtrToOffset (fcs
, numbers
);
193 memmove (leaves
+ pos
+ 1, leaves
+ pos
,
194 (fcs
->num
- pos
) * sizeof (*leaves
));
195 memmove (numbers
+ pos
+ 1, numbers
+ pos
,
196 (fcs
->num
- pos
) * sizeof (*numbers
));
197 numbers
[pos
] = (FcChar16
) ucs4
;
198 leaves
[pos
] = FcPtrToOffset (leaves
, leaf
);
204 * Locate the leaf containing the specified char, creating it
209 FcCharSetFindLeafCreate (FcCharSet
*fcs
, FcChar32 ucs4
)
214 pos
= FcCharSetFindLeafPos (fcs
, ucs4
);
216 return FcCharSetLeaf(fcs
, pos
);
218 leaf
= calloc (1, sizeof (FcCharLeaf
));
223 if (!FcCharSetPutLeaf (fcs
, ucs4
, leaf
, pos
))
228 FcMemAlloc (FC_MEM_CHARLEAF
, sizeof (FcCharLeaf
));
233 FcCharSetInsertLeaf (FcCharSet
*fcs
, FcChar32 ucs4
, FcCharLeaf
*leaf
)
237 pos
= FcCharSetFindLeafPos (fcs
, ucs4
);
240 FcMemFree (FC_MEM_CHARLEAF
, sizeof (FcCharLeaf
));
241 free (FcCharSetLeaf (fcs
, pos
));
242 FcCharSetLeaves(fcs
)[pos
] = FcPtrToOffset (FcCharSetLeaves(fcs
),
247 return FcCharSetPutLeaf (fcs
, ucs4
, leaf
, pos
);
251 FcCharSetAddChar (FcCharSet
*fcs
, FcChar32 ucs4
)
256 if (fcs
->ref
== FC_REF_CONSTANT
)
258 leaf
= FcCharSetFindLeafCreate (fcs
, ucs4
);
261 b
= &leaf
->map
[(ucs4
& 0xff) >> 5];
262 *b
|= (1 << (ucs4
& 0x1f));
267 * An iterator for the leaves of a charset
270 typedef struct _fcCharSetIter
{
277 * Set iter->leaf to the leaf containing iter->ucs4 or higher
281 FcCharSetIterSet (const FcCharSet
*fcs
, FcCharSetIter
*iter
)
283 int pos
= FcCharSetFindLeafPos (fcs
, iter
->ucs4
);
294 iter
->ucs4
= (FcChar32
) FcCharSetNumbers(fcs
)[pos
] << 8;
296 iter
->leaf
= FcCharSetLeaf(fcs
, pos
);
301 FcCharSetIterNext (const FcCharSet
*fcs
, FcCharSetIter
*iter
)
303 int pos
= iter
->pos
+ 1;
311 iter
->ucs4
= (FcChar32
) FcCharSetNumbers(fcs
)[pos
] << 8;
312 iter
->leaf
= FcCharSetLeaf(fcs
, pos
);
319 FcCharSetIterStart (const FcCharSet
*fcs
, FcCharSetIter
*iter
)
323 FcCharSetIterSet (fcs
, iter
);
327 FcCharSetCopy (FcCharSet
*src
)
329 if (src
->ref
!= FC_REF_CONSTANT
)
332 FcCacheObjectReference (src
);
337 FcCharSetEqual (const FcCharSet
*a
, const FcCharSet
*b
)
339 FcCharSetIter ai
, bi
;
344 for (FcCharSetIterStart (a
, &ai
), FcCharSetIterStart (b
, &bi
);
346 FcCharSetIterNext (a
, &ai
), FcCharSetIterNext (b
, &bi
))
348 if (ai
.ucs4
!= bi
.ucs4
)
350 for (i
= 0; i
< 256/32; i
++)
351 if (ai
.leaf
->map
[i
] != bi
.leaf
->map
[i
])
354 return ai
.leaf
== bi
.leaf
;
358 FcCharSetAddLeaf (FcCharSet
*fcs
,
362 FcCharLeaf
*new = FcCharSetFindLeafCreate (fcs
, ucs4
);
370 FcCharSetOperate (const FcCharSet
*a
,
372 FcBool (*overlap
) (FcCharLeaf
*result
,
373 const FcCharLeaf
*al
,
374 const FcCharLeaf
*bl
),
379 FcCharSetIter ai
, bi
;
381 fcs
= FcCharSetCreate ();
384 FcCharSetIterStart (a
, &ai
);
385 FcCharSetIterStart (b
, &bi
);
386 while ((ai
.leaf
|| (bonly
&& bi
.leaf
)) && (bi
.leaf
|| (aonly
&& ai
.leaf
)))
388 if (ai
.ucs4
< bi
.ucs4
)
392 if (!FcCharSetAddLeaf (fcs
, ai
.ucs4
, ai
.leaf
))
394 FcCharSetIterNext (a
, &ai
);
399 FcCharSetIterSet (a
, &ai
);
402 else if (bi
.ucs4
< ai
.ucs4
)
406 if (!FcCharSetAddLeaf (fcs
, bi
.ucs4
, bi
.leaf
))
408 FcCharSetIterNext (b
, &bi
);
413 FcCharSetIterSet (b
, &bi
);
420 if ((*overlap
) (&leaf
, ai
.leaf
, bi
.leaf
))
422 if (!FcCharSetAddLeaf (fcs
, ai
.ucs4
, &leaf
))
425 FcCharSetIterNext (a
, &ai
);
426 FcCharSetIterNext (b
, &bi
);
431 FcCharSetDestroy (fcs
);
437 FcCharSetIntersectLeaf (FcCharLeaf
*result
,
438 const FcCharLeaf
*al
,
439 const FcCharLeaf
*bl
)
442 FcBool nonempty
= FcFalse
;
444 for (i
= 0; i
< 256/32; i
++)
445 if ((result
->map
[i
] = al
->map
[i
] & bl
->map
[i
]))
451 FcCharSetIntersect (const FcCharSet
*a
, const FcCharSet
*b
)
453 return FcCharSetOperate (a
, b
, FcCharSetIntersectLeaf
, FcFalse
, FcFalse
);
457 FcCharSetUnionLeaf (FcCharLeaf
*result
,
458 const FcCharLeaf
*al
,
459 const FcCharLeaf
*bl
)
463 for (i
= 0; i
< 256/32; i
++)
464 result
->map
[i
] = al
->map
[i
] | bl
->map
[i
];
469 FcCharSetUnion (const FcCharSet
*a
, const FcCharSet
*b
)
471 return FcCharSetOperate (a
, b
, FcCharSetUnionLeaf
, FcTrue
, FcTrue
);
475 FcCharSetMerge (FcCharSet
*a
, const FcCharSet
*b
, FcBool
*changed
)
480 if (a
->ref
== FC_REF_CONSTANT
) {
487 *changed
= !FcCharSetIsSubset(b
, a
);
494 an
= ai
< a
->num
? FcCharSetNumbers(a
)[ai
] : ~0;
495 bn
= FcCharSetNumbers(b
)[bi
];
499 ai
= FcCharSetFindLeafForward (a
, ai
+ 1, bn
);
505 FcCharLeaf
*bl
= FcCharSetLeaf(b
, bi
);
508 if (!FcCharSetAddLeaf (a
, bn
<< 8, bl
))
513 FcCharLeaf
*al
= FcCharSetLeaf(a
, ai
);
514 FcCharSetUnionLeaf (al
, al
, bl
);
526 FcCharSetSubtractLeaf (FcCharLeaf
*result
,
527 const FcCharLeaf
*al
,
528 const FcCharLeaf
*bl
)
531 FcBool nonempty
= FcFalse
;
533 for (i
= 0; i
< 256/32; i
++)
534 if ((result
->map
[i
] = al
->map
[i
] & ~bl
->map
[i
]))
540 FcCharSetSubtract (const FcCharSet
*a
, const FcCharSet
*b
)
542 return FcCharSetOperate (a
, b
, FcCharSetSubtractLeaf
, FcTrue
, FcFalse
);
546 FcCharSetHasChar (const FcCharSet
*fcs
, FcChar32 ucs4
)
548 FcCharLeaf
*leaf
= FcCharSetFindLeaf (fcs
, ucs4
);
551 return (leaf
->map
[(ucs4
& 0xff) >> 5] & (1 << (ucs4
& 0x1f))) != 0;
555 FcCharSetPopCount (FcChar32 c1
)
557 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
558 return __builtin_popcount (c1
);
561 FcChar32 c2
= (c1
>> 1) & 033333333333;
562 c2
= c1
- c2
- ((c2
>> 1) & 033333333333);
563 return (((c2
+ (c2
>> 3)) & 030707070707) % 077);
568 FcCharSetIntersectCount (const FcCharSet
*a
, const FcCharSet
*b
)
570 FcCharSetIter ai
, bi
;
573 FcCharSetIterStart (a
, &ai
);
574 FcCharSetIterStart (b
, &bi
);
575 while (ai
.leaf
&& bi
.leaf
)
577 if (ai
.ucs4
== bi
.ucs4
)
579 FcChar32
*am
= ai
.leaf
->map
;
580 FcChar32
*bm
= bi
.leaf
->map
;
583 count
+= FcCharSetPopCount (*am
++ & *bm
++);
584 FcCharSetIterNext (a
, &ai
);
586 else if (ai
.ucs4
< bi
.ucs4
)
589 FcCharSetIterSet (a
, &ai
);
591 if (bi
.ucs4
< ai
.ucs4
)
594 FcCharSetIterSet (b
, &bi
);
601 FcCharSetCount (const FcCharSet
*a
)
606 for (FcCharSetIterStart (a
, &ai
); ai
.leaf
; FcCharSetIterNext (a
, &ai
))
609 FcChar32
*am
= ai
.leaf
->map
;
612 count
+= FcCharSetPopCount (*am
++);
618 FcCharSetSubtractCount (const FcCharSet
*a
, const FcCharSet
*b
)
620 FcCharSetIter ai
, bi
;
623 FcCharSetIterStart (a
, &ai
);
624 FcCharSetIterStart (b
, &bi
);
627 if (ai
.ucs4
<= bi
.ucs4
)
629 FcChar32
*am
= ai
.leaf
->map
;
631 if (ai
.ucs4
== bi
.ucs4
)
633 FcChar32
*bm
= bi
.leaf
->map
;
635 count
+= FcCharSetPopCount (*am
++ & ~*bm
++);
640 count
+= FcCharSetPopCount (*am
++);
642 FcCharSetIterNext (a
, &ai
);
647 FcCharSetIterSet (b
, &bi
);
654 * return FcTrue iff a is a subset of b
657 FcCharSetIsSubset (const FcCharSet
*a
, const FcCharSet
*b
)
662 if (a
== b
) return FcTrue
;
665 while (ai
< a
->num
&& bi
< b
->num
)
667 an
= FcCharSetNumbers(a
)[ai
];
668 bn
= FcCharSetNumbers(b
)[bi
];
670 * Check matching pages
674 FcChar32
*am
= FcCharSetLeaf(a
, ai
)->map
;
675 FcChar32
*bm
= FcCharSetLeaf(b
, bi
)->map
;
681 * Does am have any bits not in bm?
691 * Does a have any pages not in b?
697 bi
= FcCharSetFindLeafForward (b
, bi
+ 1, an
);
703 * did we look at every page?
709 * These two functions efficiently walk the entire charmap for
710 * other software (like pango) that want their own copy
714 FcCharSetNextPage (const FcCharSet
*a
,
715 FcChar32 map
[FC_CHARSET_MAP_SIZE
],
722 FcCharSetIterSet (a
, &ai
);
724 return FC_CHARSET_DONE
;
727 * Save current information
730 memcpy (map
, ai
.leaf
->map
, sizeof (ai
.leaf
->map
));
734 FcCharSetIterNext (a
, &ai
);
741 FcCharSetFirstPage (const FcCharSet
*a
,
742 FcChar32 map
[FC_CHARSET_MAP_SIZE
],
746 return FcCharSetNextPage (a
, map
, next
);
750 * old coverage API, rather hard to use correctly
754 FcCharSetCoverage (const FcCharSet
*a
, FcChar32 page
, FcChar32
*result
)
759 FcCharSetIterSet (a
, &ai
);
762 memset (result
, '\0', 256 / 8);
767 memcpy (result
, ai
.leaf
->map
, sizeof (ai
.leaf
->map
));
768 FcCharSetIterNext (a
, &ai
);
775 * ASCII representation of charsets.
777 * Each leaf is represented as 9 32-bit values, the code of the first character followed
778 * by 8 32 bit values for the leaf itself. Each value is encoded as 5 ASCII characters,
779 * only 85 different values are used to avoid control characters as well as the other
780 * characters used to encode font names. 85**5 > 2^32 so things work out, but
781 * it's not exactly human readable output. As a special case, 0 is encoded as a space
784 static const unsigned char charToValue
[256] = {
785 /* "" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
786 /* "\b" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
787 /* "\020" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
788 /* "\030" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
789 /* " " */ 0xff, 0x00, 0xff, 0x01, 0x02, 0x03, 0x04, 0xff,
790 /* "(" */ 0x05, 0x06, 0x07, 0x08, 0xff, 0xff, 0x09, 0x0a,
791 /* "0" */ 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12,
792 /* "8" */ 0x13, 0x14, 0xff, 0x15, 0x16, 0xff, 0x17, 0x18,
793 /* "@" */ 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
794 /* "H" */ 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
795 /* "P" */ 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30,
796 /* "X" */ 0x31, 0x32, 0x33, 0x34, 0xff, 0x35, 0x36, 0xff,
797 /* "`" */ 0xff, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d,
798 /* "h" */ 0x3e, 0x3f, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45,
799 /* "p" */ 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d,
800 /* "x" */ 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53, 0x54, 0xff,
801 /* "\200" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
802 /* "\210" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
803 /* "\220" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
804 /* "\230" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
805 /* "\240" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
806 /* "\250" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
807 /* "\260" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
808 /* "\270" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
809 /* "\300" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
810 /* "\310" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
811 /* "\320" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
812 /* "\330" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
813 /* "\340" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
814 /* "\350" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
815 /* "\360" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
816 /* "\370" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
819 static const FcChar8 valueToChar
[0x55] = {
820 /* 0x00 */ '!', '#', '$', '%', '&', '(', ')', '*',
821 /* 0x08 */ '+', '.', '/', '0', '1', '2', '3', '4',
822 /* 0x10 */ '5', '6', '7', '8', '9', ';', '<', '>',
823 /* 0x18 */ '?', '@', 'A', 'B', 'C', 'D', 'E', 'F',
824 /* 0x20 */ 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
825 /* 0x28 */ 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
826 /* 0x30 */ 'W', 'X', 'Y', 'Z', '[', ']', '^', 'a',
827 /* 0x38 */ 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
828 /* 0x40 */ 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q',
829 /* 0x48 */ 'r', 's', 't', 'u', 'v', 'w', 'x', 'y',
830 /* 0x50 */ 'z', '{', '|', '}', '~',
834 FcCharSetParseValue (FcChar8
*string
, FcChar32
*value
)
848 for (i
= 0; i
< 5; i
++)
850 if (!(c
= (FcChar32
) (unsigned char) *string
++))
863 FcCharSetUnparseValue (FcStrBuf
*buf
, FcChar32 value
)
868 return FcStrBufChar (buf
, ' ');
873 FcChar8
*s
= string
+ 5;
875 for (i
= 0; i
< 5; i
++)
877 *--s
= valueToChar
[value
% 85];
880 for (i
= 0; i
< 5; i
++)
881 if (!FcStrBufChar (buf
, *s
++))
888 FcNameParseCharSet (FcChar8
*string
)
897 c
= FcCharSetCreate ();
902 string
= FcCharSetParseValue (string
, &ucs4
);
906 for (i
= 0; i
< 256/32; i
++)
908 string
= FcCharSetParseValue (string
, &temp
.map
[i
]);
915 leaf
= malloc (sizeof (FcCharLeaf
));
919 if (!FcCharSetInsertLeaf (c
, ucs4
, leaf
))
927 FcMemFree (FC_MEM_CHARSET
, c
->num
* sizeof (FcCharLeaf
*));
928 free (FcCharSetLeaves (c
));
932 FcMemFree (FC_MEM_CHARSET
, c
->num
* sizeof (FcChar16
));
933 free (FcCharSetNumbers (c
));
935 FcMemFree (FC_MEM_CHARSET
, sizeof (FcCharSet
));
942 FcNameUnparseCharSet (FcStrBuf
*buf
, const FcCharSet
*c
)
950 for (FcCharSetIterStart (c
, &ci
);
952 FcCharSetIterNext (c
, &ci
))
954 if (!FcCharSetUnparseValue (buf
, ci
.ucs4
))
956 for (i
= 0; i
< 256/32; i
++)
957 if (!FcCharSetUnparseValue (buf
, ci
.leaf
->map
[i
]))
964 FcCharSetIter ci
, checki
;
966 /* null terminate for parser */
967 FcStrBufChar (buf
, '\0');
968 /* step back over null for life after test */
970 check
= FcNameParseCharSet (buf
->buf
+ len
);
971 FcCharSetIterStart (c
, &ci
);
972 FcCharSetIterStart (check
, &checki
);
973 while (ci
.leaf
|| checki
.leaf
)
975 if (ci
.ucs4
< checki
.ucs4
)
977 printf ("Missing leaf node at 0x%x\n", ci
.ucs4
);
978 FcCharSetIterNext (c
, &ci
);
980 else if (checki
.ucs4
< ci
.ucs4
)
982 printf ("Extra leaf node at 0x%x\n", checki
.ucs4
);
983 FcCharSetIterNext (check
, &checki
);
988 FcChar32
*cm
= ci
.leaf
->map
;
989 FcChar32
*checkm
= checki
.leaf
->map
;
991 for (i
= 0; i
< 256; i
+= 32)
994 printf ("Mismatching sets at 0x%08x: 0x%08x != 0x%08x\n",
995 ci
.ucs4
+ i
, *cm
, *checkm
);
999 FcCharSetIterNext (c
, &ci
);
1000 FcCharSetIterNext (check
, &checki
);
1003 if ((missing
= FcCharSetSubtractCount (c
, check
)))
1004 printf ("%d missing in reparsed result\n", missing
);
1005 if ((missing
= FcCharSetSubtractCount (check
, c
)))
1006 printf ("%d extra in reparsed result\n", missing
);
1007 FcCharSetDestroy (check
);
1014 typedef struct _FcCharLeafEnt FcCharLeafEnt
;
1016 struct _FcCharLeafEnt
{
1017 FcCharLeafEnt
*next
;
1022 #define FC_CHAR_LEAF_BLOCK (4096 / sizeof (FcCharLeafEnt))
1023 #define FC_CHAR_LEAF_HASH_SIZE 257
1025 typedef struct _FcCharSetEnt FcCharSetEnt
;
1027 struct _FcCharSetEnt
{
1033 typedef struct _FcCharSetOrigEnt FcCharSetOrigEnt
;
1035 struct _FcCharSetOrigEnt
{
1036 FcCharSetOrigEnt
*next
;
1037 const FcCharSet
*orig
;
1038 const FcCharSet
*frozen
;
1041 #define FC_CHAR_SET_HASH_SIZE 67
1043 struct _FcCharSetFreezer
{
1044 FcCharLeafEnt
*leaf_hash_table
[FC_CHAR_LEAF_HASH_SIZE
];
1045 FcCharLeafEnt
**leaf_blocks
;
1046 int leaf_block_count
;
1047 FcCharSetEnt
*set_hash_table
[FC_CHAR_SET_HASH_SIZE
];
1048 FcCharSetOrigEnt
*orig_hash_table
[FC_CHAR_SET_HASH_SIZE
];
1049 FcCharLeafEnt
*current_block
;
1053 int leaves_allocated
;
1054 int charsets_allocated
;
1057 static FcCharLeafEnt
*
1058 FcCharLeafEntCreate (FcCharSetFreezer
*freezer
)
1060 if (!freezer
->leaf_remain
)
1062 FcCharLeafEnt
**newBlocks
;
1064 freezer
->leaf_block_count
++;
1065 newBlocks
= realloc (freezer
->leaf_blocks
, freezer
->leaf_block_count
* sizeof (FcCharLeafEnt
*));
1068 freezer
->leaf_blocks
= newBlocks
;
1069 freezer
->current_block
= freezer
->leaf_blocks
[freezer
->leaf_block_count
-1] = malloc (FC_CHAR_LEAF_BLOCK
* sizeof (FcCharLeafEnt
));
1070 if (!freezer
->current_block
)
1072 FcMemAlloc (FC_MEM_CHARLEAF
, FC_CHAR_LEAF_BLOCK
* sizeof (FcCharLeafEnt
));
1073 freezer
->leaf_remain
= FC_CHAR_LEAF_BLOCK
;
1075 freezer
->leaf_remain
--;
1076 freezer
->leaves_allocated
++;
1077 return freezer
->current_block
++;
1081 FcCharLeafHash (FcCharLeaf
*leaf
)
1086 for (i
= 0; i
< 256/32; i
++)
1087 hash
= ((hash
<< 1) | (hash
>> 31)) ^ leaf
->map
[i
];
1092 FcCharSetFreezeLeaf (FcCharSetFreezer
*freezer
, FcCharLeaf
*leaf
)
1094 FcChar32 hash
= FcCharLeafHash (leaf
);
1095 FcCharLeafEnt
**bucket
= &freezer
->leaf_hash_table
[hash
% FC_CHAR_LEAF_HASH_SIZE
];
1098 for (ent
= *bucket
; ent
; ent
= ent
->next
)
1100 if (ent
->hash
== hash
&& !memcmp (&ent
->leaf
, leaf
, sizeof (FcCharLeaf
)))
1104 ent
= FcCharLeafEntCreate(freezer
);
1109 ent
->next
= *bucket
;
1115 FcCharSetHash (FcCharSet
*fcs
)
1120 /* hash in leaves */
1121 for (i
= 0; i
< fcs
->num
; i
++)
1122 hash
= ((hash
<< 1) | (hash
>> 31)) ^ FcCharLeafHash (FcCharSetLeaf(fcs
,i
));
1123 /* hash in numbers */
1124 for (i
= 0; i
< fcs
->num
; i
++)
1125 hash
= ((hash
<< 1) | (hash
>> 31)) ^ *FcCharSetNumbers(fcs
);
1130 FcCharSetFreezeOrig (FcCharSetFreezer
*freezer
, const FcCharSet
*orig
, const FcCharSet
*frozen
)
1132 FcCharSetOrigEnt
**bucket
= &freezer
->orig_hash_table
[((uintptr_t) orig
) & FC_CHAR_SET_HASH_SIZE
];
1133 FcCharSetOrigEnt
*ent
;
1135 ent
= malloc (sizeof (FcCharSetOrigEnt
));
1139 ent
->frozen
= frozen
;
1140 ent
->next
= *bucket
;
1146 FcCharSetFreezeBase (FcCharSetFreezer
*freezer
, FcCharSet
*fcs
, const FcCharSet
*orig
)
1148 FcChar32 hash
= FcCharSetHash (fcs
);
1149 FcCharSetEnt
**bucket
= &freezer
->set_hash_table
[hash
% FC_CHAR_SET_HASH_SIZE
];
1154 for (ent
= *bucket
; ent
; ent
= ent
->next
)
1156 if (ent
->hash
== hash
&&
1157 ent
->set
.num
== fcs
->num
&&
1158 !memcmp (FcCharSetNumbers(&ent
->set
),
1159 FcCharSetNumbers(fcs
),
1160 fcs
->num
* sizeof (FcChar16
)))
1165 for (i
= 0; i
< fcs
->num
; i
++)
1166 if (FcCharSetLeaf(&ent
->set
, i
) != FcCharSetLeaf(fcs
, i
))
1173 size
= (sizeof (FcCharSetEnt
) +
1174 fcs
->num
* sizeof (FcCharLeaf
*) +
1175 fcs
->num
* sizeof (FcChar16
));
1176 ent
= malloc (size
);
1179 FcMemAlloc (FC_MEM_CHARSET
, size
);
1181 freezer
->charsets_allocated
++;
1183 ent
->set
.ref
= FC_REF_CONSTANT
;
1184 ent
->set
.num
= fcs
->num
;
1187 intptr_t *ent_leaves
;
1189 ent
->set
.leaves_offset
= sizeof (ent
->set
);
1190 ent
->set
.numbers_offset
= (ent
->set
.leaves_offset
+
1191 fcs
->num
* sizeof (intptr_t));
1193 ent_leaves
= FcCharSetLeaves (&ent
->set
);
1194 for (i
= 0; i
< fcs
->num
; i
++)
1195 ent_leaves
[i
] = FcPtrToOffset (ent_leaves
,
1196 FcCharSetLeaf (fcs
, i
));
1197 memcpy (FcCharSetNumbers (&ent
->set
),
1198 FcCharSetNumbers (fcs
),
1199 fcs
->num
* sizeof (FcChar16
));
1203 ent
->set
.leaves_offset
= 0;
1204 ent
->set
.numbers_offset
= 0;
1208 ent
->next
= *bucket
;
1214 static const FcCharSet
*
1215 FcCharSetFindFrozen (FcCharSetFreezer
*freezer
, const FcCharSet
*orig
)
1217 FcCharSetOrigEnt
**bucket
= &freezer
->orig_hash_table
[((uintptr_t) orig
) & FC_CHAR_SET_HASH_SIZE
];
1218 FcCharSetOrigEnt
*ent
;
1220 for (ent
= *bucket
; ent
; ent
= ent
->next
)
1221 if (ent
->orig
== orig
)
1227 FcCharSetFreeze (FcCharSetFreezer
*freezer
, const FcCharSet
*fcs
)
1230 const FcCharSet
*n
= 0;
1234 b
= FcCharSetCreate ();
1237 for (i
= 0; i
< fcs
->num
; i
++)
1239 l
= FcCharSetFreezeLeaf (freezer
, FcCharSetLeaf(fcs
, i
));
1242 if (!FcCharSetInsertLeaf (b
, FcCharSetNumbers(fcs
)[i
] << 8, l
))
1245 n
= FcCharSetFreezeBase (freezer
, b
, fcs
);
1246 if (!FcCharSetFreezeOrig (freezer
, fcs
, n
))
1251 freezer
->charsets_seen
++;
1252 freezer
->leaves_seen
+= fcs
->num
;
1256 FcMemFree (FC_MEM_CHARSET
, b
->num
* sizeof (FcCharLeaf
*));
1257 free (FcCharSetLeaves (b
));
1261 FcMemFree (FC_MEM_CHARSET
, b
->num
* sizeof (FcChar16
));
1262 free (FcCharSetNumbers (b
));
1264 FcMemFree (FC_MEM_CHARSET
, sizeof (FcCharSet
));
1271 FcCharSetFreezerCreate (void)
1273 FcCharSetFreezer
*freezer
;
1275 freezer
= calloc (1, sizeof (FcCharSetFreezer
));
1280 FcCharSetFreezerDestroy (FcCharSetFreezer
*freezer
)
1284 if (FcDebug() & FC_DBG_CACHE
)
1286 printf ("\ncharsets %d -> %d leaves %d -> %d\n",
1287 freezer
->charsets_seen
, freezer
->charsets_allocated
,
1288 freezer
->leaves_seen
, freezer
->leaves_allocated
);
1290 for (i
= 0; i
< FC_CHAR_SET_HASH_SIZE
; i
++)
1292 FcCharSetEnt
*ent
, *next
;
1293 for (ent
= freezer
->set_hash_table
[i
]; ent
; ent
= next
)
1296 FcMemFree (FC_MEM_CHARSET
, (sizeof (FcCharSetEnt
) +
1297 ent
->set
.num
* sizeof (FcCharLeaf
*) +
1298 ent
->set
.num
* sizeof (FcChar16
)));
1303 for (i
= 0; i
< FC_CHAR_SET_HASH_SIZE
; i
++)
1305 FcCharSetOrigEnt
*ent
, *next
;
1306 for (ent
= freezer
->orig_hash_table
[i
]; ent
; ent
= next
)
1313 for (i
= 0; i
< freezer
->leaf_block_count
; i
++)
1315 free (freezer
->leaf_blocks
[i
]);
1316 FcMemFree (FC_MEM_CHARLEAF
, FC_CHAR_LEAF_BLOCK
* sizeof (FcCharLeafEnt
));
1319 free (freezer
->leaf_blocks
);
1324 FcCharSetSerializeAlloc (FcSerialize
*serialize
, const FcCharSet
*cs
)
1330 if (cs
->ref
!= FC_REF_CONSTANT
)
1332 if (!serialize
->cs_freezer
)
1334 serialize
->cs_freezer
= FcCharSetFreezerCreate ();
1335 if (!serialize
->cs_freezer
)
1338 if (FcCharSetFindFrozen (serialize
->cs_freezer
, cs
))
1341 cs
= FcCharSetFreeze (serialize
->cs_freezer
, cs
);
1344 leaves
= FcCharSetLeaves (cs
);
1345 numbers
= FcCharSetNumbers (cs
);
1347 if (!FcSerializeAlloc (serialize
, cs
, sizeof (FcCharSet
)))
1349 if (!FcSerializeAlloc (serialize
, leaves
, cs
->num
* sizeof (intptr_t)))
1351 if (!FcSerializeAlloc (serialize
, numbers
, cs
->num
* sizeof (FcChar16
)))
1353 for (i
= 0; i
< cs
->num
; i
++)
1354 if (!FcSerializeAlloc (serialize
, FcCharSetLeaf(cs
, i
),
1355 sizeof (FcCharLeaf
)))
1361 FcCharSetSerialize(FcSerialize
*serialize
, const FcCharSet
*cs
)
1363 FcCharSet
*cs_serialized
;
1364 intptr_t *leaves
, *leaves_serialized
;
1365 FcChar16
*numbers
, *numbers_serialized
;
1366 FcCharLeaf
*leaf
, *leaf_serialized
;
1369 if (cs
->ref
!= FC_REF_CONSTANT
&& serialize
->cs_freezer
)
1371 cs
= FcCharSetFindFrozen (serialize
->cs_freezer
, cs
);
1376 cs_serialized
= FcSerializePtr (serialize
, cs
);
1380 cs_serialized
->ref
= FC_REF_CONSTANT
;
1381 cs_serialized
->num
= cs
->num
;
1385 leaves
= FcCharSetLeaves (cs
);
1386 leaves_serialized
= FcSerializePtr (serialize
, leaves
);
1387 if (!leaves_serialized
)
1390 cs_serialized
->leaves_offset
= FcPtrToOffset (cs_serialized
,
1393 numbers
= FcCharSetNumbers (cs
);
1394 numbers_serialized
= FcSerializePtr (serialize
, numbers
);
1398 cs_serialized
->numbers_offset
= FcPtrToOffset (cs_serialized
,
1399 numbers_serialized
);
1401 for (i
= 0; i
< cs
->num
; i
++)
1403 leaf
= FcCharSetLeaf (cs
, i
);
1404 leaf_serialized
= FcSerializePtr (serialize
, leaf
);
1405 if (!leaf_serialized
)
1407 *leaf_serialized
= *leaf
;
1408 leaves_serialized
[i
] = FcPtrToOffset (leaves_serialized
,
1410 numbers_serialized
[i
] = numbers
[i
];
1415 cs_serialized
->leaves_offset
= 0;
1416 cs_serialized
->numbers_offset
= 0;
1419 return cs_serialized
;
1421 #define __fccharset__
1422 #include "fcaliastail.h"
1423 #undef __fccharset__