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 * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18 * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
19 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
20 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
21 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
22 * PERFORMANCE OF THIS SOFTWARE.
31 FcCharSetCreate (void)
35 fcs
= (FcCharSet
*) malloc (sizeof (FcCharSet
));
38 FcMemAlloc (FC_MEM_CHARSET
, sizeof (FcCharSet
));
41 fcs
->leaves_offset
= 0;
42 fcs
->numbers_offset
= 0;
49 return FcCharSetCreate ();
53 FcCharSetDestroy (FcCharSet
*fcs
)
57 if (fcs
->ref
== FC_REF_CONSTANT
)
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 * Locate the leaf containing the specified char, return
85 * its index if it exists, otherwise return negative of
86 * the (position + 1) where it should be inserted
90 FcCharSetFindLeafPos (const FcCharSet
*fcs
, FcChar32 ucs4
)
92 FcChar16
*numbers
= FcCharSetNumbers(fcs
);
95 int high
= fcs
->num
- 1;
102 int mid
= (low
+ high
) >> 1;
111 if (high
< 0 || (high
< fcs
->num
&& numbers
[high
] < ucs4
))
117 FcCharSetFindLeaf (const FcCharSet
*fcs
, FcChar32 ucs4
)
119 int pos
= FcCharSetFindLeafPos (fcs
, ucs4
);
121 return FcCharSetLeaf(fcs
, pos
);
126 FcCharSetPutLeaf (FcCharSet
*fcs
,
131 intptr_t *leaves
= FcCharSetLeaves (fcs
);
132 FcChar16
*numbers
= FcCharSetNumbers (fcs
);
138 leaves
= malloc (sizeof (*leaves
));
141 intptr_t *new_leaves
= realloc (leaves
, (fcs
->num
+ 1) *
143 intptr_t distance
= (intptr_t) new_leaves
- (intptr_t) leaves
;
145 if (new_leaves
&& distance
)
149 for (i
= 0; i
< fcs
->num
; i
++)
150 new_leaves
[i
] -= distance
;
158 FcMemFree (FC_MEM_CHARSET
, fcs
->num
* sizeof (intptr_t));
159 FcMemAlloc (FC_MEM_CHARSET
, (fcs
->num
+ 1) * sizeof (intptr_t));
160 fcs
->leaves_offset
= FcPtrToOffset (fcs
, leaves
);
163 numbers
= malloc (sizeof (FcChar16
));
165 numbers
= realloc (numbers
, (fcs
->num
+ 1) * sizeof (FcChar16
));
170 FcMemFree (FC_MEM_CHARSET
, fcs
->num
* sizeof (FcChar16
));
171 FcMemAlloc (FC_MEM_CHARSET
, (fcs
->num
+ 1) * sizeof (FcChar16
));
172 fcs
->numbers_offset
= FcPtrToOffset (fcs
, numbers
);
174 memmove (leaves
+ pos
+ 1, leaves
+ pos
,
175 (fcs
->num
- pos
) * sizeof (*leaves
));
176 memmove (numbers
+ pos
+ 1, numbers
+ pos
,
177 (fcs
->num
- pos
) * sizeof (*numbers
));
178 numbers
[pos
] = (FcChar16
) ucs4
;
179 leaves
[pos
] = FcPtrToOffset (leaves
, leaf
);
185 * Locate the leaf containing the specified char, creating it
190 FcCharSetFindLeafCreate (FcCharSet
*fcs
, FcChar32 ucs4
)
195 pos
= FcCharSetFindLeafPos (fcs
, ucs4
);
197 return FcCharSetLeaf(fcs
, pos
);
199 leaf
= calloc (1, sizeof (FcCharLeaf
));
204 if (!FcCharSetPutLeaf (fcs
, ucs4
, leaf
, pos
))
209 FcMemAlloc (FC_MEM_CHARLEAF
, sizeof (FcCharLeaf
));
214 FcCharSetInsertLeaf (FcCharSet
*fcs
, FcChar32 ucs4
, FcCharLeaf
*leaf
)
218 pos
= FcCharSetFindLeafPos (fcs
, ucs4
);
221 FcMemFree (FC_MEM_CHARLEAF
, sizeof (FcCharLeaf
));
222 free (FcCharSetLeaf (fcs
, pos
));
223 FcCharSetLeaves(fcs
)[pos
] = FcPtrToOffset (FcCharSetLeaves(fcs
),
228 return FcCharSetPutLeaf (fcs
, ucs4
, leaf
, pos
);
232 FcCharSetAddChar (FcCharSet
*fcs
, FcChar32 ucs4
)
237 if (fcs
->ref
== FC_REF_CONSTANT
)
239 leaf
= FcCharSetFindLeafCreate (fcs
, ucs4
);
242 b
= &leaf
->map
[(ucs4
& 0xff) >> 5];
243 *b
|= (1 << (ucs4
& 0x1f));
248 * An iterator for the leaves of a charset
251 typedef struct _fcCharSetIter
{
258 * Set iter->leaf to the leaf containing iter->ucs4 or higher
262 FcCharSetIterSet (const FcCharSet
*fcs
, FcCharSetIter
*iter
)
264 int pos
= FcCharSetFindLeafPos (fcs
, iter
->ucs4
);
275 iter
->ucs4
= (FcChar32
) FcCharSetNumbers(fcs
)[pos
] << 8;
277 iter
->leaf
= FcCharSetLeaf(fcs
, pos
);
282 FcCharSetIterNext (const FcCharSet
*fcs
, FcCharSetIter
*iter
)
284 int pos
= iter
->pos
+ 1;
292 iter
->ucs4
= (FcChar32
) FcCharSetNumbers(fcs
)[pos
] << 8;
293 iter
->leaf
= FcCharSetLeaf(fcs
, pos
);
300 FcCharSetIterStart (const FcCharSet
*fcs
, FcCharSetIter
*iter
)
304 FcCharSetIterSet (fcs
, iter
);
308 FcCharSetCopy (FcCharSet
*src
)
310 if (src
->ref
!= FC_REF_CONSTANT
)
313 FcCacheObjectReference (src
);
318 FcCharSetEqual (const FcCharSet
*a
, const FcCharSet
*b
)
320 FcCharSetIter ai
, bi
;
325 for (FcCharSetIterStart (a
, &ai
), FcCharSetIterStart (b
, &bi
);
327 FcCharSetIterNext (a
, &ai
), FcCharSetIterNext (b
, &bi
))
329 if (ai
.ucs4
!= bi
.ucs4
)
331 for (i
= 0; i
< 256/32; i
++)
332 if (ai
.leaf
->map
[i
] != bi
.leaf
->map
[i
])
335 return ai
.leaf
== bi
.leaf
;
339 FcCharSetAddLeaf (FcCharSet
*fcs
,
343 FcCharLeaf
*new = FcCharSetFindLeafCreate (fcs
, ucs4
);
351 FcCharSetOperate (const FcCharSet
*a
,
353 FcBool (*overlap
) (FcCharLeaf
*result
,
354 const FcCharLeaf
*al
,
355 const FcCharLeaf
*bl
),
360 FcCharSetIter ai
, bi
;
362 fcs
= FcCharSetCreate ();
365 FcCharSetIterStart (a
, &ai
);
366 FcCharSetIterStart (b
, &bi
);
367 while ((ai
.leaf
|| (bonly
&& bi
.leaf
)) && (bi
.leaf
|| (aonly
&& ai
.leaf
)))
369 if (ai
.ucs4
< bi
.ucs4
)
373 if (!FcCharSetAddLeaf (fcs
, ai
.ucs4
, ai
.leaf
))
375 FcCharSetIterNext (a
, &ai
);
380 FcCharSetIterSet (a
, &ai
);
383 else if (bi
.ucs4
< ai
.ucs4
)
387 if (!FcCharSetAddLeaf (fcs
, bi
.ucs4
, bi
.leaf
))
389 FcCharSetIterNext (b
, &bi
);
394 FcCharSetIterSet (b
, &bi
);
401 if ((*overlap
) (&leaf
, ai
.leaf
, bi
.leaf
))
403 if (!FcCharSetAddLeaf (fcs
, ai
.ucs4
, &leaf
))
406 FcCharSetIterNext (a
, &ai
);
407 FcCharSetIterNext (b
, &bi
);
412 FcCharSetDestroy (fcs
);
418 FcCharSetIntersectLeaf (FcCharLeaf
*result
,
419 const FcCharLeaf
*al
,
420 const FcCharLeaf
*bl
)
423 FcBool nonempty
= FcFalse
;
425 for (i
= 0; i
< 256/32; i
++)
426 if ((result
->map
[i
] = al
->map
[i
] & bl
->map
[i
]))
432 FcCharSetIntersect (const FcCharSet
*a
, const FcCharSet
*b
)
434 return FcCharSetOperate (a
, b
, FcCharSetIntersectLeaf
, FcFalse
, FcFalse
);
438 FcCharSetUnionLeaf (FcCharLeaf
*result
,
439 const FcCharLeaf
*al
,
440 const FcCharLeaf
*bl
)
444 for (i
= 0; i
< 256/32; i
++)
445 result
->map
[i
] = al
->map
[i
] | bl
->map
[i
];
450 FcCharSetUnion (const FcCharSet
*a
, const FcCharSet
*b
)
452 return FcCharSetOperate (a
, b
, FcCharSetUnionLeaf
, FcTrue
, FcTrue
);
456 FcCharSetMerge (FcCharSet
*a
, const FcCharSet
*b
)
459 FcCharSetIter ai
, bi
;
462 return FcCharSetCopy ((FcCharSet
*) b
);
463 } else if (a
->ref
== FC_REF_CONSTANT
) {
464 fcs
= FcCharSetCreate ();
470 FcCharSetIterStart (a
, &ai
);
471 FcCharSetIterStart (b
, &bi
);
472 while (ai
.leaf
|| bi
.leaf
)
474 if (ai
.ucs4
< bi
.ucs4
)
476 if (!FcCharSetAddLeaf (fcs
, ai
.ucs4
, ai
.leaf
))
479 FcCharSetIterNext (a
, &ai
);
481 else if (bi
.ucs4
< ai
.ucs4
)
483 if (!FcCharSetAddLeaf (fcs
, bi
.ucs4
, bi
.leaf
))
486 FcCharSetIterNext (b
, &bi
);
492 if (FcCharSetUnionLeaf (&leaf
, ai
.leaf
, bi
.leaf
))
494 if (!FcCharSetAddLeaf (fcs
, ai
.ucs4
, &leaf
))
498 FcCharSetIterNext (a
, &ai
);
499 FcCharSetIterNext (b
, &bi
);
504 FcCharSetDestroy (a
);
509 FcCharSetDestroy (fcs
);
512 FcCharSetDestroy (a
);
518 FcCharSetSubtractLeaf (FcCharLeaf
*result
,
519 const FcCharLeaf
*al
,
520 const FcCharLeaf
*bl
)
523 FcBool nonempty
= FcFalse
;
525 for (i
= 0; i
< 256/32; i
++)
526 if ((result
->map
[i
] = al
->map
[i
] & ~bl
->map
[i
]))
532 FcCharSetSubtract (const FcCharSet
*a
, const FcCharSet
*b
)
534 return FcCharSetOperate (a
, b
, FcCharSetSubtractLeaf
, FcTrue
, FcFalse
);
538 FcCharSetHasChar (const FcCharSet
*fcs
, FcChar32 ucs4
)
540 FcCharLeaf
*leaf
= FcCharSetFindLeaf (fcs
, ucs4
);
543 return (leaf
->map
[(ucs4
& 0xff) >> 5] & (1 << (ucs4
& 0x1f))) != 0;
547 FcCharSetPopCount (FcChar32 c1
)
550 FcChar32 c2
= (c1
>> 1) & 033333333333;
551 c2
= c1
- c2
- ((c2
>> 1) & 033333333333);
552 return (((c2
+ (c2
>> 3)) & 030707070707) % 077);
556 FcCharSetIntersectCount (const FcCharSet
*a
, const FcCharSet
*b
)
558 FcCharSetIter ai
, bi
;
561 FcCharSetIterStart (a
, &ai
);
562 FcCharSetIterStart (b
, &bi
);
563 while (ai
.leaf
&& bi
.leaf
)
565 if (ai
.ucs4
== bi
.ucs4
)
567 FcChar32
*am
= ai
.leaf
->map
;
568 FcChar32
*bm
= bi
.leaf
->map
;
571 count
+= FcCharSetPopCount (*am
++ & *bm
++);
572 FcCharSetIterNext (a
, &ai
);
574 else if (ai
.ucs4
< bi
.ucs4
)
577 FcCharSetIterSet (a
, &ai
);
579 if (bi
.ucs4
< ai
.ucs4
)
582 FcCharSetIterSet (b
, &bi
);
589 FcCharSetCount (const FcCharSet
*a
)
594 for (FcCharSetIterStart (a
, &ai
); ai
.leaf
; FcCharSetIterNext (a
, &ai
))
597 FcChar32
*am
= ai
.leaf
->map
;
600 count
+= FcCharSetPopCount (*am
++);
606 FcCharSetSubtractCount (const FcCharSet
*a
, const FcCharSet
*b
)
608 FcCharSetIter ai
, bi
;
611 FcCharSetIterStart (a
, &ai
);
612 FcCharSetIterStart (b
, &bi
);
615 if (ai
.ucs4
<= bi
.ucs4
)
617 FcChar32
*am
= ai
.leaf
->map
;
619 if (ai
.ucs4
== bi
.ucs4
)
621 FcChar32
*bm
= bi
.leaf
->map
;
623 count
+= FcCharSetPopCount (*am
++ & ~*bm
++);
628 count
+= FcCharSetPopCount (*am
++);
630 FcCharSetIterNext (a
, &ai
);
635 FcCharSetIterSet (b
, &bi
);
642 * return FcTrue iff a is a subset of b
645 FcCharSetIsSubset (const FcCharSet
*a
, const FcCharSet
*b
)
650 if (a
== b
) return FcTrue
;
653 while (ai
< a
->num
&& bi
< b
->num
)
655 an
= FcCharSetNumbers(a
)[ai
];
656 bn
= FcCharSetNumbers(b
)[bi
];
658 * Check matching pages
662 FcChar32
*am
= FcCharSetLeaf(a
, ai
)->map
;
663 FcChar32
*bm
= FcCharSetLeaf(b
, bi
)->map
;
669 * Does am have any bits not in bm?
679 * Does a have any pages not in b?
686 int high
= b
->num
- 1;
689 * Search for page 'an' in 'b'
693 int mid
= (low
+ high
) >> 1;
694 bn
= FcCharSetNumbers(b
)[mid
];
706 while (bi
< b
->num
&& FcCharSetNumbers(b
)[bi
] < an
)
711 * did we look at every page?
717 * These two functions efficiently walk the entire charmap for
718 * other software (like pango) that want their own copy
722 FcCharSetNextPage (const FcCharSet
*a
,
723 FcChar32 map
[FC_CHARSET_MAP_SIZE
],
730 FcCharSetIterSet (a
, &ai
);
732 return FC_CHARSET_DONE
;
735 * Save current information
738 memcpy (map
, ai
.leaf
->map
, sizeof (ai
.leaf
->map
));
742 FcCharSetIterNext (a
, &ai
);
749 FcCharSetFirstPage (const FcCharSet
*a
,
750 FcChar32 map
[FC_CHARSET_MAP_SIZE
],
754 return FcCharSetNextPage (a
, map
, next
);
758 * old coverage API, rather hard to use correctly
762 FcCharSetCoverage (const FcCharSet
*a
, FcChar32 page
, FcChar32
*result
)
767 FcCharSetIterSet (a
, &ai
);
770 memset (result
, '\0', 256 / 8);
775 memcpy (result
, ai
.leaf
->map
, sizeof (ai
.leaf
->map
));
776 FcCharSetIterNext (a
, &ai
);
783 * ASCII representation of charsets.
785 * Each leaf is represented as 9 32-bit values, the code of the first character followed
786 * by 8 32 bit values for the leaf itself. Each value is encoded as 5 ASCII characters,
787 * only 85 different values are used to avoid control characters as well as the other
788 * characters used to encode font names. 85**5 > 2^32 so things work out, but
789 * it's not exactly human readable output. As a special case, 0 is encoded as a space
792 static const unsigned char charToValue
[256] = {
793 /* "" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
794 /* "\b" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
795 /* "\020" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
796 /* "\030" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
797 /* " " */ 0xff, 0x00, 0xff, 0x01, 0x02, 0x03, 0x04, 0xff,
798 /* "(" */ 0x05, 0x06, 0x07, 0x08, 0xff, 0xff, 0x09, 0x0a,
799 /* "0" */ 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12,
800 /* "8" */ 0x13, 0x14, 0xff, 0x15, 0x16, 0xff, 0x17, 0x18,
801 /* "@" */ 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
802 /* "H" */ 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
803 /* "P" */ 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30,
804 /* "X" */ 0x31, 0x32, 0x33, 0x34, 0xff, 0x35, 0x36, 0xff,
805 /* "`" */ 0xff, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d,
806 /* "h" */ 0x3e, 0x3f, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45,
807 /* "p" */ 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d,
808 /* "x" */ 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53, 0x54, 0xff,
809 /* "\200" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
810 /* "\210" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
811 /* "\220" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
812 /* "\230" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
813 /* "\240" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
814 /* "\250" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
815 /* "\260" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
816 /* "\270" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
817 /* "\300" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
818 /* "\310" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
819 /* "\320" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
820 /* "\330" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
821 /* "\340" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
822 /* "\350" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
823 /* "\360" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
824 /* "\370" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
827 static const FcChar8 valueToChar
[0x55] = {
828 /* 0x00 */ '!', '#', '$', '%', '&', '(', ')', '*',
829 /* 0x08 */ '+', '.', '/', '0', '1', '2', '3', '4',
830 /* 0x10 */ '5', '6', '7', '8', '9', ';', '<', '>',
831 /* 0x18 */ '?', '@', 'A', 'B', 'C', 'D', 'E', 'F',
832 /* 0x20 */ 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
833 /* 0x28 */ 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
834 /* 0x30 */ 'W', 'X', 'Y', 'Z', '[', ']', '^', 'a',
835 /* 0x38 */ 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
836 /* 0x40 */ 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q',
837 /* 0x48 */ 'r', 's', 't', 'u', 'v', 'w', 'x', 'y',
838 /* 0x50 */ 'z', '{', '|', '}', '~',
842 FcCharSetParseValue (FcChar8
*string
, FcChar32
*value
)
856 for (i
= 0; i
< 5; i
++)
858 if (!(c
= (FcChar32
) (unsigned char) *string
++))
871 FcCharSetUnparseValue (FcStrBuf
*buf
, FcChar32 value
)
876 return FcStrBufChar (buf
, ' ');
881 FcChar8
*s
= string
+ 5;
883 for (i
= 0; i
< 5; i
++)
885 *--s
= valueToChar
[value
% 85];
888 for (i
= 0; i
< 5; i
++)
889 if (!FcStrBufChar (buf
, *s
++))
896 FcNameParseCharSet (FcChar8
*string
)
905 c
= FcCharSetCreate ();
910 string
= FcCharSetParseValue (string
, &ucs4
);
914 for (i
= 0; i
< 256/32; i
++)
916 string
= FcCharSetParseValue (string
, &temp
.map
[i
]);
923 leaf
= malloc (sizeof (FcCharLeaf
));
927 if (!FcCharSetInsertLeaf (c
, ucs4
, leaf
))
935 FcMemFree (FC_MEM_CHARSET
, c
->num
* sizeof (FcCharLeaf
*));
936 free (FcCharSetLeaves (c
));
940 FcMemFree (FC_MEM_CHARSET
, c
->num
* sizeof (FcChar16
));
941 free (FcCharSetNumbers (c
));
943 FcMemFree (FC_MEM_CHARSET
, sizeof (FcCharSet
));
950 FcNameUnparseCharSet (FcStrBuf
*buf
, const FcCharSet
*c
)
958 for (FcCharSetIterStart (c
, &ci
);
960 FcCharSetIterNext (c
, &ci
))
962 if (!FcCharSetUnparseValue (buf
, ci
.ucs4
))
964 for (i
= 0; i
< 256/32; i
++)
965 if (!FcCharSetUnparseValue (buf
, ci
.leaf
->map
[i
]))
972 FcCharSetIter ci
, checki
;
974 /* null terminate for parser */
975 FcStrBufChar (buf
, '\0');
976 /* step back over null for life after test */
978 check
= FcNameParseCharSet (buf
->buf
+ len
);
979 FcCharSetIterStart (c
, &ci
);
980 FcCharSetIterStart (check
, &checki
);
981 while (ci
.leaf
|| checki
.leaf
)
983 if (ci
.ucs4
< checki
.ucs4
)
985 printf ("Missing leaf node at 0x%x\n", ci
.ucs4
);
986 FcCharSetIterNext (c
, &ci
);
988 else if (checki
.ucs4
< ci
.ucs4
)
990 printf ("Extra leaf node at 0x%x\n", checki
.ucs4
);
991 FcCharSetIterNext (check
, &checki
);
996 FcChar32
*cm
= ci
.leaf
->map
;
997 FcChar32
*checkm
= checki
.leaf
->map
;
999 for (i
= 0; i
< 256; i
+= 32)
1002 printf ("Mismatching sets at 0x%08x: 0x%08x != 0x%08x\n",
1003 ci
.ucs4
+ i
, *cm
, *checkm
);
1007 FcCharSetIterNext (c
, &ci
);
1008 FcCharSetIterNext (check
, &checki
);
1011 if ((missing
= FcCharSetSubtractCount (c
, check
)))
1012 printf ("%d missing in reparsed result\n", missing
);
1013 if ((missing
= FcCharSetSubtractCount (check
, c
)))
1014 printf ("%d extra in reparsed result\n", missing
);
1015 FcCharSetDestroy (check
);
1022 typedef struct _FcCharLeafEnt FcCharLeafEnt
;
1024 struct _FcCharLeafEnt
{
1025 FcCharLeafEnt
*next
;
1030 #define FC_CHAR_LEAF_BLOCK (4096 / sizeof (FcCharLeafEnt))
1031 #define FC_CHAR_LEAF_HASH_SIZE 257
1033 typedef struct _FcCharSetEnt FcCharSetEnt
;
1035 struct _FcCharSetEnt
{
1041 typedef struct _FcCharSetOrigEnt FcCharSetOrigEnt
;
1043 struct _FcCharSetOrigEnt
{
1044 FcCharSetOrigEnt
*next
;
1045 const FcCharSet
*orig
;
1046 const FcCharSet
*frozen
;
1049 #define FC_CHAR_SET_HASH_SIZE 67
1051 struct _FcCharSetFreezer
{
1052 FcCharLeafEnt
*leaf_hash_table
[FC_CHAR_LEAF_HASH_SIZE
];
1053 FcCharLeafEnt
**leaf_blocks
;
1054 int leaf_block_count
;
1055 FcCharSetEnt
*set_hash_table
[FC_CHAR_SET_HASH_SIZE
];
1056 FcCharSetOrigEnt
*orig_hash_table
[FC_CHAR_SET_HASH_SIZE
];
1057 FcCharLeafEnt
*current_block
;
1061 int leaves_allocated
;
1062 int charsets_allocated
;
1065 static FcCharLeafEnt
*
1066 FcCharLeafEntCreate (FcCharSetFreezer
*freezer
)
1068 if (!freezer
->leaf_remain
)
1070 FcCharLeafEnt
**newBlocks
;
1072 freezer
->leaf_block_count
++;
1073 newBlocks
= realloc (freezer
->leaf_blocks
, freezer
->leaf_block_count
* sizeof (FcCharLeafEnt
*));
1076 freezer
->leaf_blocks
= newBlocks
;
1077 freezer
->current_block
= freezer
->leaf_blocks
[freezer
->leaf_block_count
-1] = malloc (FC_CHAR_LEAF_BLOCK
* sizeof (FcCharLeafEnt
));
1078 if (!freezer
->current_block
)
1080 FcMemAlloc (FC_MEM_CHARLEAF
, FC_CHAR_LEAF_BLOCK
* sizeof (FcCharLeafEnt
));
1081 freezer
->leaf_remain
= FC_CHAR_LEAF_BLOCK
;
1083 freezer
->leaf_remain
--;
1084 freezer
->leaves_allocated
++;
1085 return freezer
->current_block
++;
1089 FcCharLeafHash (FcCharLeaf
*leaf
)
1094 for (i
= 0; i
< 256/32; i
++)
1095 hash
= ((hash
<< 1) | (hash
>> 31)) ^ leaf
->map
[i
];
1100 FcCharSetFreezeLeaf (FcCharSetFreezer
*freezer
, FcCharLeaf
*leaf
)
1102 FcChar32 hash
= FcCharLeafHash (leaf
);
1103 FcCharLeafEnt
**bucket
= &freezer
->leaf_hash_table
[hash
% FC_CHAR_LEAF_HASH_SIZE
];
1106 for (ent
= *bucket
; ent
; ent
= ent
->next
)
1108 if (ent
->hash
== hash
&& !memcmp (&ent
->leaf
, leaf
, sizeof (FcCharLeaf
)))
1112 ent
= FcCharLeafEntCreate(freezer
);
1117 ent
->next
= *bucket
;
1123 FcCharSetHash (FcCharSet
*fcs
)
1128 /* hash in leaves */
1129 for (i
= 0; i
< fcs
->num
; i
++)
1130 hash
= ((hash
<< 1) | (hash
>> 31)) ^ FcCharLeafHash (FcCharSetLeaf(fcs
,i
));
1131 /* hash in numbers */
1132 for (i
= 0; i
< fcs
->num
; i
++)
1133 hash
= ((hash
<< 1) | (hash
>> 31)) ^ *FcCharSetNumbers(fcs
);
1138 FcCharSetFreezeOrig (FcCharSetFreezer
*freezer
, const FcCharSet
*orig
, const FcCharSet
*frozen
)
1140 FcCharSetOrigEnt
**bucket
= &freezer
->orig_hash_table
[((uintptr_t) orig
) & FC_CHAR_SET_HASH_SIZE
];
1141 FcCharSetOrigEnt
*ent
;
1143 ent
= malloc (sizeof (FcCharSetOrigEnt
));
1147 ent
->frozen
= frozen
;
1148 ent
->next
= *bucket
;
1154 FcCharSetFreezeBase (FcCharSetFreezer
*freezer
, FcCharSet
*fcs
, const FcCharSet
*orig
)
1156 FcChar32 hash
= FcCharSetHash (fcs
);
1157 FcCharSetEnt
**bucket
= &freezer
->set_hash_table
[hash
% FC_CHAR_SET_HASH_SIZE
];
1162 for (ent
= *bucket
; ent
; ent
= ent
->next
)
1164 if (ent
->hash
== hash
&&
1165 ent
->set
.num
== fcs
->num
&&
1166 !memcmp (FcCharSetNumbers(&ent
->set
),
1167 FcCharSetNumbers(fcs
),
1168 fcs
->num
* sizeof (FcChar16
)))
1173 for (i
= 0; i
< fcs
->num
; i
++)
1174 if (FcCharSetLeaf(&ent
->set
, i
) != FcCharSetLeaf(fcs
, i
))
1181 size
= (sizeof (FcCharSetEnt
) +
1182 fcs
->num
* sizeof (FcCharLeaf
*) +
1183 fcs
->num
* sizeof (FcChar16
));
1184 ent
= malloc (size
);
1187 FcMemAlloc (FC_MEM_CHARSET
, size
);
1189 freezer
->charsets_allocated
++;
1191 ent
->set
.ref
= FC_REF_CONSTANT
;
1192 ent
->set
.num
= fcs
->num
;
1195 intptr_t *ent_leaves
;
1197 ent
->set
.leaves_offset
= sizeof (ent
->set
);
1198 ent
->set
.numbers_offset
= (ent
->set
.leaves_offset
+
1199 fcs
->num
* sizeof (intptr_t));
1201 ent_leaves
= FcCharSetLeaves (&ent
->set
);
1202 for (i
= 0; i
< fcs
->num
; i
++)
1203 ent_leaves
[i
] = FcPtrToOffset (ent_leaves
,
1204 FcCharSetLeaf (fcs
, i
));
1205 memcpy (FcCharSetNumbers (&ent
->set
),
1206 FcCharSetNumbers (fcs
),
1207 fcs
->num
* sizeof (FcChar16
));
1211 ent
->set
.leaves_offset
= 0;
1212 ent
->set
.numbers_offset
= 0;
1216 ent
->next
= *bucket
;
1222 static const FcCharSet
*
1223 FcCharSetFindFrozen (FcCharSetFreezer
*freezer
, const FcCharSet
*orig
)
1225 FcCharSetOrigEnt
**bucket
= &freezer
->orig_hash_table
[((uintptr_t) orig
) & FC_CHAR_SET_HASH_SIZE
];
1226 FcCharSetOrigEnt
*ent
;
1228 for (ent
= *bucket
; ent
; ent
= ent
->next
)
1229 if (ent
->orig
== orig
)
1235 FcCharSetFreeze (FcCharSetFreezer
*freezer
, const FcCharSet
*fcs
)
1238 const FcCharSet
*n
= 0;
1242 b
= FcCharSetCreate ();
1245 for (i
= 0; i
< fcs
->num
; i
++)
1247 l
= FcCharSetFreezeLeaf (freezer
, FcCharSetLeaf(fcs
, i
));
1250 if (!FcCharSetInsertLeaf (b
, FcCharSetNumbers(fcs
)[i
] << 8, l
))
1253 n
= FcCharSetFreezeBase (freezer
, b
, fcs
);
1254 if (!FcCharSetFreezeOrig (freezer
, fcs
, n
))
1259 freezer
->charsets_seen
++;
1260 freezer
->leaves_seen
+= fcs
->num
;
1264 FcMemFree (FC_MEM_CHARSET
, b
->num
* sizeof (FcCharLeaf
*));
1265 free (FcCharSetLeaves (b
));
1269 FcMemFree (FC_MEM_CHARSET
, b
->num
* sizeof (FcChar16
));
1270 free (FcCharSetNumbers (b
));
1272 FcMemFree (FC_MEM_CHARSET
, sizeof (FcCharSet
));
1279 FcCharSetFreezerCreate (void)
1281 FcCharSetFreezer
*freezer
;
1283 freezer
= calloc (1, sizeof (FcCharSetFreezer
));
1288 FcCharSetFreezerDestroy (FcCharSetFreezer
*freezer
)
1292 if (FcDebug() & FC_DBG_CACHE
)
1294 printf ("\ncharsets %d -> %d leaves %d -> %d\n",
1295 freezer
->charsets_seen
, freezer
->charsets_allocated
,
1296 freezer
->leaves_seen
, freezer
->leaves_allocated
);
1298 for (i
= 0; i
< FC_CHAR_SET_HASH_SIZE
; i
++)
1300 FcCharSetEnt
*ent
, *next
;
1301 for (ent
= freezer
->set_hash_table
[i
]; ent
; ent
= next
)
1304 FcMemFree (FC_MEM_CHARSET
, (sizeof (FcCharSetEnt
) +
1305 ent
->set
.num
* sizeof (FcCharLeaf
*) +
1306 ent
->set
.num
* sizeof (FcChar16
)));
1311 for (i
= 0; i
< FC_CHAR_SET_HASH_SIZE
; i
++)
1313 FcCharSetOrigEnt
*ent
, *next
;
1314 for (ent
= freezer
->orig_hash_table
[i
]; ent
; ent
= next
)
1321 for (i
= 0; i
< freezer
->leaf_block_count
; i
++)
1323 free (freezer
->leaf_blocks
[i
]);
1324 FcMemFree (FC_MEM_CHARLEAF
, FC_CHAR_LEAF_BLOCK
* sizeof (FcCharLeafEnt
));
1327 free (freezer
->leaf_blocks
);
1332 FcCharSetSerializeAlloc (FcSerialize
*serialize
, const FcCharSet
*cs
)
1338 if (cs
->ref
!= FC_REF_CONSTANT
)
1340 if (!serialize
->cs_freezer
)
1342 serialize
->cs_freezer
= FcCharSetFreezerCreate ();
1343 if (!serialize
->cs_freezer
)
1346 if (FcCharSetFindFrozen (serialize
->cs_freezer
, cs
))
1349 cs
= FcCharSetFreeze (serialize
->cs_freezer
, cs
);
1352 leaves
= FcCharSetLeaves (cs
);
1353 numbers
= FcCharSetNumbers (cs
);
1355 if (!FcSerializeAlloc (serialize
, cs
, sizeof (FcCharSet
)))
1357 if (!FcSerializeAlloc (serialize
, leaves
, cs
->num
* sizeof (intptr_t)))
1359 if (!FcSerializeAlloc (serialize
, numbers
, cs
->num
* sizeof (FcChar16
)))
1361 for (i
= 0; i
< cs
->num
; i
++)
1362 if (!FcSerializeAlloc (serialize
, FcCharSetLeaf(cs
, i
),
1363 sizeof (FcCharLeaf
)))
1369 FcCharSetSerialize(FcSerialize
*serialize
, const FcCharSet
*cs
)
1371 FcCharSet
*cs_serialized
;
1372 intptr_t *leaves
, *leaves_serialized
;
1373 FcChar16
*numbers
, *numbers_serialized
;
1374 FcCharLeaf
*leaf
, *leaf_serialized
;
1377 if (cs
->ref
!= FC_REF_CONSTANT
&& serialize
->cs_freezer
)
1379 cs
= FcCharSetFindFrozen (serialize
->cs_freezer
, cs
);
1384 cs_serialized
= FcSerializePtr (serialize
, cs
);
1388 cs_serialized
->ref
= FC_REF_CONSTANT
;
1389 cs_serialized
->num
= cs
->num
;
1393 leaves
= FcCharSetLeaves (cs
);
1394 leaves_serialized
= FcSerializePtr (serialize
, leaves
);
1395 if (!leaves_serialized
)
1398 cs_serialized
->leaves_offset
= FcPtrToOffset (cs_serialized
,
1401 numbers
= FcCharSetNumbers (cs
);
1402 numbers_serialized
= FcSerializePtr (serialize
, numbers
);
1406 cs_serialized
->numbers_offset
= FcPtrToOffset (cs_serialized
,
1407 numbers_serialized
);
1409 for (i
= 0; i
< cs
->num
; i
++)
1411 leaf
= FcCharSetLeaf (cs
, i
);
1412 leaf_serialized
= FcSerializePtr (serialize
, leaf
);
1413 if (!leaf_serialized
)
1415 *leaf_serialized
= *leaf
;
1416 leaves_serialized
[i
] = FcPtrToOffset (leaves_serialized
,
1418 numbers_serialized
[i
] = numbers
[i
];
1423 cs_serialized
->leaves_offset
= 0;
1424 cs_serialized
->numbers_offset
= 0;
1427 return cs_serialized
;
1429 #define __fccharset__
1430 #include "fcaliastail.h"
1431 #undef __fccharset__