2 * $RCSId: xc/lib/fontconfig/src/fccharset.c,v 1.18 2002/08/22 07:36:44 keithp Exp $
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.
32 static FcCharSet
** charsets
= 0;
33 static FcChar16
** numbers
= 0;
34 static int charset_bank_count
= 0, charset_ptr
, charset_count
;
35 static int charset_numbers_ptr
, charset_numbers_count
;
36 static FcCharLeaf
** leaves
= 0;
37 static int charset_leaf_ptr
, charset_leaf_count
;
38 static int ** leaf_idx
= 0;
39 static int charset_leaf_idx_ptr
, charset_leaf_idx_count
;
41 extern const FcChar16 langBankNumbers
[];
42 extern const FcCharLeaf langBankLeaves
[];
43 extern const int langBankLeafIdx
[];
46 FcCharSetEnsureBank (int bi
);
49 FcLangCharSetPopulate (void)
51 int bi
= FcCacheBankToIndexMTF (FC_BANK_LANGS
);
52 FcCharSetEnsureBank (bi
);
54 numbers
[bi
] = (FcChar16
*)langBankNumbers
;
55 leaves
[bi
] = (FcCharLeaf
*)langBankLeaves
;
56 leaf_idx
[bi
] = (int *)langBankLeafIdx
;
60 FcCharSetCreate (void)
64 fcs
= (FcCharSet
*) malloc (sizeof (FcCharSet
));
67 FcMemAlloc (FC_MEM_CHARSET
, sizeof (FcCharSet
));
70 fcs
->bank
= FC_BANK_DYNAMIC
;
71 fcs
->u
.dyn
.leaves
= 0;
72 fcs
->u
.dyn
.numbers
= 0;
82 return FcCharSetCreate ();
86 FcCharSetDestroy (FcCharSet
*fcs
)
89 if (fcs
->ref
== FC_REF_CONSTANT
)
93 if (fcs
->bank
== FC_BANK_DYNAMIC
)
95 for (i
= 0; i
< fcs
->num
; i
++)
97 FcMemFree (FC_MEM_CHARLEAF
, sizeof (FcCharLeaf
));
98 free (fcs
->u
.dyn
.leaves
[i
]);
100 if (fcs
->u
.dyn
.leaves
)
102 FcMemFree (FC_MEM_CHARSET
, fcs
->num
* sizeof (FcCharLeaf
*));
103 free (fcs
->u
.dyn
.leaves
);
105 if (fcs
->u
.dyn
.numbers
)
107 FcMemFree (FC_MEM_CHARSET
, fcs
->num
* sizeof (FcChar16
));
108 free (fcs
->u
.dyn
.numbers
);
111 FcMemFree (FC_MEM_CHARSET
, sizeof (FcCharSet
));
116 * Locate the leaf containing the specified char, return
117 * its index if it exists, otherwise return negative of
118 * the (position + 1) where it should be inserted
122 FcCharSetFindLeafPos (const FcCharSet
*fcs
, FcChar32 ucs4
)
124 FcChar16
*numbers
= FcCharSetGetNumbers(fcs
);
127 int high
= fcs
->num
- 1;
134 int mid
= (low
+ high
) >> 1;
143 if (high
< 0 || (high
< fcs
->num
&& numbers
[high
] < ucs4
))
149 FcCharSetFindLeaf (const FcCharSet
*fcs
, FcChar32 ucs4
)
151 int pos
= FcCharSetFindLeafPos (fcs
, ucs4
);
153 return FcCharSetGetLeaf(fcs
, pos
);
158 FcCharSetPutLeaf (FcCharSet
*fcs
,
169 if (fcs
->bank
!= FC_BANK_DYNAMIC
)
171 /* convert to dynamic */
174 leaves
= malloc ((fcs
->num
+ 1) * sizeof (FcCharLeaf
*));
177 FcMemAlloc (FC_MEM_CHARSET
, (fcs
->num
+ 1) * sizeof (FcCharLeaf
*));
178 numbers
= malloc ((fcs
->num
+ 1) * sizeof (FcChar16
));
181 FcMemAlloc (FC_MEM_CHARSET
, (fcs
->num
+ 1) * sizeof (FcChar16
));
183 for (i
= 0; i
< fcs
->num
; i
++)
184 leaves
[i
] = FcCharSetGetLeaf(fcs
, i
);
185 memcpy (numbers
, FcCharSetGetNumbers(fcs
),
186 fcs
->num
* sizeof (FcChar16
));
188 fcs
->bank
= FC_BANK_DYNAMIC
;
189 fcs
->u
.dyn
.leaves
= leaves
;
190 fcs
->u
.dyn
.numbers
= numbers
;
194 if (!fcs
->u
.dyn
.leaves
)
195 leaves
= malloc (sizeof (FcCharLeaf
*));
197 leaves
= realloc (fcs
->u
.dyn
.leaves
, (fcs
->num
+ 1) * sizeof (FcCharLeaf
*));
201 FcMemFree (FC_MEM_CHARSET
, fcs
->num
* sizeof (FcCharLeaf
*));
202 FcMemAlloc (FC_MEM_CHARSET
, (fcs
->num
+ 1) * sizeof (FcCharLeaf
*));
203 fcs
->u
.dyn
.leaves
= leaves
;
204 if (!fcs
->u
.dyn
.numbers
)
205 numbers
= malloc (sizeof (FcChar16
));
207 numbers
= realloc (fcs
->u
.dyn
.numbers
, (fcs
->num
+ 1) * sizeof (FcChar16
));
211 FcMemFree (FC_MEM_CHARSET
, fcs
->num
* sizeof (FcChar16
));
212 FcMemAlloc (FC_MEM_CHARSET
, (fcs
->num
+ 1) * sizeof (FcChar16
));
213 fcs
->u
.dyn
.numbers
= numbers
;
216 memmove (fcs
->u
.dyn
.leaves
+ pos
+ 1, fcs
->u
.dyn
.leaves
+ pos
,
217 (fcs
->num
- pos
) * sizeof (FcCharLeaf
*));
218 memmove (fcs
->u
.dyn
.numbers
+ pos
+ 1, fcs
->u
.dyn
.numbers
+ pos
,
219 (fcs
->num
- pos
) * sizeof (FcChar16
));
220 fcs
->u
.dyn
.numbers
[pos
] = (FcChar16
) ucs4
;
221 fcs
->u
.dyn
.leaves
[pos
] = leaf
;
227 * Locate the leaf containing the specified char, creating it
232 FcCharSetFindLeafCreate (FcCharSet
*fcs
, FcChar32 ucs4
)
237 pos
= FcCharSetFindLeafPos (fcs
, ucs4
);
239 return FcCharSetGetLeaf(fcs
, pos
);
241 leaf
= calloc (1, sizeof (FcCharLeaf
));
246 if (!FcCharSetPutLeaf (fcs
, ucs4
, leaf
, pos
))
251 FcMemAlloc (FC_MEM_CHARLEAF
, sizeof (FcCharLeaf
));
256 FcCharSetInsertLeaf (FcCharSet
*fcs
, FcChar32 ucs4
, FcCharLeaf
*leaf
)
260 pos
= FcCharSetFindLeafPos (fcs
, ucs4
);
263 FcMemFree (FC_MEM_CHARLEAF
, sizeof (FcCharLeaf
));
264 if (fcs
->bank
== FC_BANK_DYNAMIC
)
266 free (fcs
->u
.dyn
.leaves
[pos
]);
267 fcs
->u
.dyn
.leaves
[pos
] = leaf
;
271 int bi
= FcCacheBankToIndex(fcs
->bank
);
272 leaves
[bi
][leaf_idx
[fcs
->bank
][fcs
->u
.stat
.leafidx_offset
]+pos
] = *leaf
;
277 return FcCharSetPutLeaf (fcs
, ucs4
, leaf
, pos
);
281 FcCharSetAddChar (FcCharSet
*fcs
, FcChar32 ucs4
)
286 if (fcs
->ref
== FC_REF_CONSTANT
)
288 leaf
= FcCharSetFindLeafCreate (fcs
, ucs4
);
291 b
= &leaf
->map
[(ucs4
& 0xff) >> 5];
292 *b
|= (1 << (ucs4
& 0x1f));
297 * An iterator for the leaves of a charset
300 typedef struct _fcCharSetIter
{
307 * Set iter->leaf to the leaf containing iter->ucs4 or higher
311 FcCharSetIterSet (const FcCharSet
*fcs
, FcCharSetIter
*iter
)
313 int pos
= FcCharSetFindLeafPos (fcs
, iter
->ucs4
);
324 iter
->ucs4
= (FcChar32
) FcCharSetGetNumbers(fcs
)[pos
] << 8;
326 iter
->leaf
= FcCharSetGetLeaf(fcs
, pos
);
329 printf ("set %08x: %08x\n", iter
->ucs4
, (FcChar32
) iter
->leaf
);
334 FcCharSetIterNext (const FcCharSet
*fcs
, FcCharSetIter
*iter
)
336 int pos
= iter
->pos
+ 1;
344 iter
->ucs4
= (FcChar32
) FcCharSetGetNumbers(fcs
)[pos
] << 8;
345 iter
->leaf
= FcCharSetGetLeaf(fcs
, pos
);
352 FcCharSetDump (const FcCharSet
*fcs
)
356 printf ("fcs %08x:\n", (FcChar32
) fcs
);
357 for (pos
= 0; pos
< fcs
->num
; pos
++)
359 FcCharLeaf
*leaf
= fcs
->leaves
[pos
];
360 FcChar32 ucs4
= (FcChar32
) fcs
->numbers
[pos
] << 8;
362 printf (" %08x: %08x\n", ucs4
, (FcChar32
) leaf
);
368 FcCharSetIterStart (const FcCharSet
*fcs
, FcCharSetIter
*iter
)
375 FcCharSetIterSet (fcs
, iter
);
379 FcCharSetCopy (FcCharSet
*src
)
381 if (src
->ref
!= FC_REF_CONSTANT
)
387 FcCharSetEqual (const FcCharSet
*a
, const FcCharSet
*b
)
389 FcCharSetIter ai
, bi
;
394 for (FcCharSetIterStart (a
, &ai
), FcCharSetIterStart (b
, &bi
);
396 FcCharSetIterNext (a
, &ai
), FcCharSetIterNext (b
, &bi
))
398 if (ai
.ucs4
!= bi
.ucs4
)
400 for (i
= 0; i
< 256/32; i
++)
401 if (ai
.leaf
->map
[i
] != bi
.leaf
->map
[i
])
404 return ai
.leaf
== bi
.leaf
;
408 FcCharSetAddLeaf (FcCharSet
*fcs
,
412 FcCharLeaf
*new = FcCharSetFindLeafCreate (fcs
, ucs4
);
420 FcCharSetOperate (const FcCharSet
*a
,
422 FcBool (*overlap
) (FcCharLeaf
*result
,
423 const FcCharLeaf
*al
,
424 const FcCharLeaf
*bl
),
429 FcCharSetIter ai
, bi
;
431 fcs
= FcCharSetCreate ();
434 FcCharSetIterStart (a
, &ai
);
435 FcCharSetIterStart (b
, &bi
);
436 while ((ai
.leaf
|| (bonly
&& bi
.leaf
)) && (bi
.leaf
|| (aonly
&& ai
.leaf
)))
438 if (ai
.ucs4
< bi
.ucs4
)
442 if (!FcCharSetAddLeaf (fcs
, ai
.ucs4
, ai
.leaf
))
444 FcCharSetIterNext (a
, &ai
);
449 FcCharSetIterSet (a
, &ai
);
452 else if (bi
.ucs4
< ai
.ucs4
)
456 if (!FcCharSetAddLeaf (fcs
, bi
.ucs4
, bi
.leaf
))
458 FcCharSetIterNext (b
, &bi
);
463 FcCharSetIterSet (b
, &bi
);
470 if ((*overlap
) (&leaf
, ai
.leaf
, bi
.leaf
))
472 if (!FcCharSetAddLeaf (fcs
, ai
.ucs4
, &leaf
))
475 FcCharSetIterNext (a
, &ai
);
476 FcCharSetIterNext (b
, &bi
);
481 FcCharSetDestroy (fcs
);
487 FcCharSetIntersectLeaf (FcCharLeaf
*result
,
488 const FcCharLeaf
*al
,
489 const FcCharLeaf
*bl
)
492 FcBool nonempty
= FcFalse
;
494 for (i
= 0; i
< 256/32; i
++)
495 if ((result
->map
[i
] = al
->map
[i
] & bl
->map
[i
]))
501 FcCharSetIntersect (const FcCharSet
*a
, const FcCharSet
*b
)
503 return FcCharSetOperate (a
, b
, FcCharSetIntersectLeaf
, FcFalse
, FcFalse
);
507 FcCharSetUnionLeaf (FcCharLeaf
*result
,
508 const FcCharLeaf
*al
,
509 const FcCharLeaf
*bl
)
513 for (i
= 0; i
< 256/32; i
++)
514 result
->map
[i
] = al
->map
[i
] | bl
->map
[i
];
519 FcCharSetUnion (const FcCharSet
*a
, const FcCharSet
*b
)
521 return FcCharSetOperate (a
, b
, FcCharSetUnionLeaf
, FcTrue
, FcTrue
);
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
)
557 FcChar32 c2
= (c1
>> 1) & 033333333333;
558 c2
= c1
- c2
- ((c2
>> 1) & 033333333333);
559 return (((c2
+ (c2
>> 3)) & 030707070707) % 077);
563 FcCharSetIntersectCount (const FcCharSet
*a
, const FcCharSet
*b
)
565 FcCharSetIter ai
, bi
;
568 FcCharSetIterStart (a
, &ai
);
569 FcCharSetIterStart (b
, &bi
);
570 while (ai
.leaf
&& bi
.leaf
)
572 if (ai
.ucs4
== bi
.ucs4
)
574 FcChar32
*am
= ai
.leaf
->map
;
575 FcChar32
*bm
= bi
.leaf
->map
;
578 count
+= FcCharSetPopCount (*am
++ & *bm
++);
579 FcCharSetIterNext (a
, &ai
);
581 else if (ai
.ucs4
< bi
.ucs4
)
584 FcCharSetIterSet (a
, &ai
);
586 if (bi
.ucs4
< ai
.ucs4
)
589 FcCharSetIterSet (b
, &bi
);
596 FcCharSetCount (const FcCharSet
*a
)
601 for (FcCharSetIterStart (a
, &ai
); ai
.leaf
; FcCharSetIterNext (a
, &ai
))
604 FcChar32
*am
= ai
.leaf
->map
;
607 count
+= FcCharSetPopCount (*am
++);
613 FcCharSetSubtractCount (const FcCharSet
*a
, const FcCharSet
*b
)
615 FcCharSetIter ai
, bi
;
618 FcCharSetIterStart (a
, &ai
);
619 FcCharSetIterStart (b
, &bi
);
622 if (ai
.ucs4
<= bi
.ucs4
)
624 FcChar32
*am
= ai
.leaf
->map
;
626 if (ai
.ucs4
== bi
.ucs4
)
628 FcChar32
*bm
= bi
.leaf
->map
;;
630 count
+= FcCharSetPopCount (*am
++ & ~*bm
++);
635 count
+= FcCharSetPopCount (*am
++);
637 FcCharSetIterNext (a
, &ai
);
642 FcCharSetIterSet (b
, &bi
);
649 * return FcTrue iff a is a subset of b
652 FcCharSetIsSubset (const FcCharSet
*a
, const FcCharSet
*b
)
657 if (a
== b
) return FcTrue
;
660 while (ai
< a
->num
&& bi
< b
->num
)
662 an
= FcCharSetGetNumbers(a
)[ai
];
663 bn
= FcCharSetGetNumbers(b
)[bi
];
665 * Check matching pages
669 FcChar32
*am
= FcCharSetGetLeaf(a
, ai
)->map
;
670 FcChar32
*bm
= FcCharSetGetLeaf(b
, bi
)->map
;
676 * Does am have any bits not in bm?
686 * Does a have any pages not in b?
693 int high
= b
->num
- 1;
696 * Search for page 'an' in 'b'
700 int mid
= (low
+ high
) >> 1;
701 bn
= FcCharSetGetNumbers(b
)[mid
];
713 while (bi
< b
->num
&& FcCharSetGetNumbers(b
)[bi
] < an
)
718 * did we look at every page?
724 * These two functions efficiently walk the entire charmap for
725 * other software (like pango) that want their own copy
729 FcCharSetNextPage (const FcCharSet
*a
,
730 FcChar32 map
[FC_CHARSET_MAP_SIZE
],
737 FcCharSetIterSet (a
, &ai
);
739 return FC_CHARSET_DONE
;
742 * Save current information
745 memcpy (map
, ai
.leaf
->map
, sizeof (ai
.leaf
->map
));
749 FcCharSetIterNext (a
, &ai
);
756 FcCharSetFirstPage (const FcCharSet
*a
,
757 FcChar32 map
[FC_CHARSET_MAP_SIZE
],
761 return FcCharSetNextPage (a
, map
, next
);
765 * old coverage API, rather hard to use correctly
768 FcCharSetCoverage (const FcCharSet
*a
, FcChar32 page
, FcChar32
*result
);
771 FcCharSetCoverage (const FcCharSet
*a
, FcChar32 page
, FcChar32
*result
)
776 FcCharSetIterSet (a
, &ai
);
779 memset (result
, '\0', 256 / 8);
784 memcpy (result
, ai
.leaf
->map
, sizeof (ai
.leaf
->map
));
785 FcCharSetIterNext (a
, &ai
);
792 * ASCII representation of charsets.
794 * Each leaf is represented as 9 32-bit values, the code of the first character followed
795 * by 8 32 bit values for the leaf itself. Each value is encoded as 5 ASCII characters,
796 * only 85 different values are used to avoid control characters as well as the other
797 * characters used to encode font names. 85**5 > 2^32 so things work out, but
798 * it's not exactly human readable output. As a special case, 0 is encoded as a space
801 static const unsigned char charToValue
[256] = {
802 /* "" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
803 /* "\b" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
804 /* "\020" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
805 /* "\030" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
806 /* " " */ 0xff, 0x00, 0xff, 0x01, 0x02, 0x03, 0x04, 0xff,
807 /* "(" */ 0x05, 0x06, 0x07, 0x08, 0xff, 0xff, 0x09, 0x0a,
808 /* "0" */ 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12,
809 /* "8" */ 0x13, 0x14, 0xff, 0x15, 0x16, 0xff, 0x17, 0x18,
810 /* "@" */ 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
811 /* "H" */ 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
812 /* "P" */ 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30,
813 /* "X" */ 0x31, 0x32, 0x33, 0x34, 0xff, 0x35, 0x36, 0xff,
814 /* "`" */ 0xff, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d,
815 /* "h" */ 0x3e, 0x3f, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45,
816 /* "p" */ 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d,
817 /* "x" */ 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53, 0x54, 0xff,
818 /* "\200" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
819 /* "\210" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
820 /* "\220" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
821 /* "\230" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
822 /* "\240" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
823 /* "\250" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
824 /* "\260" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
825 /* "\270" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
826 /* "\300" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
827 /* "\310" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
828 /* "\320" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
829 /* "\330" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
830 /* "\340" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
831 /* "\350" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
832 /* "\360" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
833 /* "\370" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
836 static const FcChar8 valueToChar
[0x55] = {
837 /* 0x00 */ '!', '#', '$', '%', '&', '(', ')', '*',
838 /* 0x08 */ '+', '.', '/', '0', '1', '2', '3', '4',
839 /* 0x10 */ '5', '6', '7', '8', '9', ';', '<', '>',
840 /* 0x18 */ '?', '@', 'A', 'B', 'C', 'D', 'E', 'F',
841 /* 0x20 */ 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
842 /* 0x28 */ 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
843 /* 0x30 */ 'W', 'X', 'Y', 'Z', '[', ']', '^', 'a',
844 /* 0x38 */ 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
845 /* 0x40 */ 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q',
846 /* 0x48 */ 'r', 's', 't', 'u', 'v', 'w', 'x', 'y',
847 /* 0x50 */ 'z', '{', '|', '}', '~',
851 FcCharSetParseValue (FcChar8
*string
, FcChar32
*value
)
865 for (i
= 0; i
< 5; i
++)
867 if (!(c
= (FcChar32
) (unsigned char) *string
++))
880 FcCharSetUnparseValue (FcStrBuf
*buf
, FcChar32 value
)
885 return FcStrBufChar (buf
, ' ');
890 FcChar8
*s
= string
+ 5;
892 for (i
= 0; i
< 5; i
++)
894 *--s
= valueToChar
[value
% 85];
897 for (i
= 0; i
< 5; i
++)
898 if (!FcStrBufChar (buf
, *s
++))
904 typedef struct _FcCharLeafEnt FcCharLeafEnt
;
906 struct _FcCharLeafEnt
{
912 #define FC_CHAR_LEAF_BLOCK (4096 / sizeof (FcCharLeafEnt))
913 static FcCharLeafEnt
**FcCharLeafBlocks
;
914 static int FcCharLeafBlockCount
;
916 static FcCharLeafEnt
*
917 FcCharLeafEntCreate (void)
919 static FcCharLeafEnt
*block
;
924 FcCharLeafEnt
**newBlocks
;
926 FcCharLeafBlockCount
++;
927 newBlocks
= realloc (FcCharLeafBlocks
, FcCharLeafBlockCount
* sizeof (FcCharLeafEnt
*));
930 FcCharLeafBlocks
= newBlocks
;
931 block
= FcCharLeafBlocks
[FcCharLeafBlockCount
-1] = malloc (FC_CHAR_LEAF_BLOCK
* sizeof (FcCharLeafEnt
));
934 FcMemAlloc (FC_MEM_CHARLEAF
, FC_CHAR_LEAF_BLOCK
* sizeof (FcCharLeafEnt
));
935 remain
= FC_CHAR_LEAF_BLOCK
;
941 #define FC_CHAR_LEAF_HASH_SIZE 257
944 FcCharLeafHash (FcCharLeaf
*leaf
)
949 for (i
= 0; i
< 256/32; i
++)
950 hash
= ((hash
<< 1) | (hash
>> 31)) ^ leaf
->map
[i
];
954 static int FcCharLeafTotal
;
955 static int FcCharLeafUsed
;
957 static FcCharLeafEnt
*FcCharLeafHashTable
[FC_CHAR_LEAF_HASH_SIZE
];
960 FcCharSetFreezeLeaf (FcCharLeaf
*leaf
)
962 FcChar32 hash
= FcCharLeafHash (leaf
);
963 FcCharLeafEnt
**bucket
= &FcCharLeafHashTable
[hash
% FC_CHAR_LEAF_HASH_SIZE
];
967 for (ent
= *bucket
; ent
; ent
= ent
->next
)
969 if (ent
->hash
== hash
&& !memcmp (&ent
->leaf
, leaf
, sizeof (FcCharLeaf
)))
973 ent
= FcCharLeafEntCreate();
985 FcCharSetThawAllLeaf (void)
989 for (i
= 0; i
< FC_CHAR_LEAF_HASH_SIZE
; i
++)
990 FcCharLeafHashTable
[i
] = 0;
995 for (i
= 0; i
< FcCharLeafBlockCount
; i
++)
996 free (FcCharLeafBlocks
[i
]);
998 free (FcCharLeafBlocks
);
999 FcCharLeafBlocks
= 0;
1000 FcCharLeafBlockCount
= 0;
1003 typedef struct _FcCharSetEnt FcCharSetEnt
;
1005 struct _FcCharSetEnt
{
1011 #define FC_CHAR_SET_HASH_SIZE 67
1014 FcCharSetHash (FcCharSet
*fcs
)
1019 /* hash in leaves */
1020 for (i
= 0; i
< fcs
->num
* (int) (sizeof (FcCharLeaf
*) / sizeof (FcChar32
)); i
++)
1021 hash
= ((hash
<< 1) | (hash
>> 31)) ^ (FcChar32
)(FcCharSetGetLeaf(fcs
, i
)->map
);
1022 /* hash in numbers */
1023 for (i
= 0; i
< fcs
->num
; i
++)
1024 hash
= ((hash
<< 1) | (hash
>> 31)) ^ *FcCharSetGetNumbers(fcs
);
1028 static int FcCharSetTotal
;
1029 static int FcCharSetUsed
;
1030 static int FcCharSetTotalEnts
, FcCharSetUsedEnts
;
1032 static FcCharSetEnt
*FcCharSetHashTable
[FC_CHAR_SET_HASH_SIZE
];
1035 FcCharSetFreezeBase (FcCharSet
*fcs
)
1037 FcChar32 hash
= FcCharSetHash (fcs
);
1038 FcCharSetEnt
**bucket
= &FcCharSetHashTable
[hash
% FC_CHAR_SET_HASH_SIZE
];
1043 FcCharSetTotalEnts
+= fcs
->num
;
1044 for (ent
= *bucket
; ent
; ent
= ent
->next
)
1046 if (ent
->hash
== hash
&&
1047 ent
->set
.num
== fcs
->num
&&
1048 !memcmp (FcCharSetGetNumbers(&ent
->set
),
1049 FcCharSetGetNumbers(fcs
),
1050 fcs
->num
* sizeof (FcChar16
)))
1055 for (i
= 0; i
< fcs
->num
; i
++)
1056 if (FcCharSetGetLeaf(&ent
->set
, i
) != FcCharSetGetLeaf(fcs
, i
))
1063 size
= (sizeof (FcCharSetEnt
) +
1064 fcs
->num
* sizeof (FcCharLeaf
*) +
1065 fcs
->num
* sizeof (FcChar16
));
1066 ent
= malloc (size
);
1069 FcMemAlloc (FC_MEM_CHARSET
, size
);
1071 FcCharSetUsedEnts
+= fcs
->num
;
1073 ent
->set
.ref
= FC_REF_CONSTANT
;
1074 ent
->set
.num
= fcs
->num
;
1075 ent
->set
.bank
= fcs
->bank
;
1076 if (fcs
->bank
== FC_BANK_DYNAMIC
)
1080 ent
->set
.u
.dyn
.leaves
= (FcCharLeaf
**) (ent
+ 1);
1081 ent
->set
.u
.dyn
.numbers
= (FcChar16
*) (ent
->set
.u
.dyn
.leaves
+ fcs
->num
);
1082 memcpy (ent
->set
.u
.dyn
.leaves
, fcs
->u
.dyn
.leaves
, fcs
->num
* sizeof (FcCharLeaf
*));
1083 memcpy (ent
->set
.u
.dyn
.numbers
, fcs
->u
.dyn
.numbers
, fcs
->num
* sizeof (FcChar16
));
1087 ent
->set
.u
.dyn
.leaves
= 0;
1088 ent
->set
.u
.dyn
.numbers
= 0;
1093 ent
->set
.u
.stat
.leafidx_offset
= fcs
->u
.stat
.leafidx_offset
;
1094 ent
->set
.u
.stat
.numbers_offset
= fcs
->u
.stat
.numbers_offset
;
1098 ent
->next
= *bucket
;
1104 FcCharSetThawAll (void)
1107 FcCharSetEnt
*ent
, *next
;
1109 for (i
= 0; i
< FC_CHAR_SET_HASH_SIZE
; i
++)
1111 for (ent
= FcCharSetHashTable
[i
]; ent
; ent
= next
)
1116 FcCharSetHashTable
[i
] = 0;
1120 FcCharSetTotalEnts
= 0;
1122 FcCharSetUsedEnts
= 0;
1124 FcCharSetThawAllLeaf ();
1128 FcCharSetFreeze (FcCharSet
*fcs
)
1135 b
= FcCharSetCreate ();
1138 for (i
= 0; i
< fcs
->num
; i
++)
1140 l
= FcCharSetFreezeLeaf (FcCharSetGetLeaf(fcs
, i
));
1143 if (!FcCharSetInsertLeaf (b
, FcCharSetGetNumbers(fcs
)[i
] << 8, l
))
1146 n
= FcCharSetFreezeBase (b
);
1148 if (b
->bank
== FC_BANK_DYNAMIC
)
1150 if (b
->u
.dyn
.leaves
)
1152 FcMemFree (FC_MEM_CHARSET
, b
->num
* sizeof (FcCharLeaf
*));
1153 free (b
->u
.dyn
.leaves
);
1155 if (b
->u
.dyn
.numbers
)
1157 FcMemFree (FC_MEM_CHARSET
, b
->num
* sizeof (FcChar16
));
1158 free (b
->u
.dyn
.numbers
);
1161 FcMemFree (FC_MEM_CHARSET
, sizeof (FcCharSet
));
1168 FcNameParseCharSet (FcChar8
*string
)
1170 FcCharSet
*c
, *n
= 0;
1177 c
= FcCharSetCreate ();
1182 string
= FcCharSetParseValue (string
, &ucs4
);
1186 for (i
= 0; i
< 256/32; i
++)
1188 string
= FcCharSetParseValue (string
, &temp
.map
[i
]);
1191 bits
|= temp
.map
[i
];
1195 leaf
= FcCharSetFreezeLeaf (&temp
);
1198 if (!FcCharSetInsertLeaf (c
, ucs4
, leaf
))
1203 printf (" %8s %8s %8s %8s\n", "total", "totalmem", "new", "newmem");
1204 printf ("Leaves: %8d %8d %8d %8d\n",
1205 FcCharLeafTotal
, sizeof (FcCharLeaf
) * FcCharLeafTotal
,
1206 FcCharLeafUsed
, sizeof (FcCharLeaf
) * FcCharLeafUsed
);
1207 printf ("Charsets: %8d %8d %8d %8d\n",
1208 FcCharSetTotal
, sizeof (FcCharSet
) * FcCharSetTotal
,
1209 FcCharSetUsed
, sizeof (FcCharSet
) * FcCharSetUsed
);
1210 printf ("Tables: %8d %8d %8d %8d\n",
1211 FcCharSetTotalEnts
, FcCharSetTotalEnts
* (sizeof (FcCharLeaf
*) + sizeof (FcChar16
)),
1212 FcCharSetUsedEnts
, FcCharSetUsedEnts
* (sizeof (FcCharLeaf
*) + sizeof (FcChar16
)));
1213 printf ("Total: %8s %8d %8s %8d\n",
1215 sizeof (FcCharLeaf
) * FcCharLeafTotal
+
1216 sizeof (FcCharSet
) * FcCharSetTotal
+
1217 FcCharSetTotalEnts
* (sizeof (FcCharLeaf
*) + sizeof (FcChar16
)),
1219 sizeof (FcCharLeaf
) * FcCharLeafUsed
+
1220 sizeof (FcCharSet
) * FcCharSetUsed
+
1221 FcCharSetUsedEnts
* (sizeof (FcCharLeaf
*) + sizeof (FcChar16
)));
1223 n
= FcCharSetFreezeBase (c
);
1225 if (c
->bank
== FC_BANK_DYNAMIC
)
1227 if (c
->u
.dyn
.leaves
)
1229 FcMemFree (FC_MEM_CHARSET
, c
->num
* sizeof (FcCharLeaf
*));
1230 free (c
->u
.dyn
.leaves
);
1232 if (c
->u
.dyn
.numbers
)
1234 FcMemFree (FC_MEM_CHARSET
, c
->num
* sizeof (FcChar16
));
1235 free (c
->u
.dyn
.numbers
);
1237 FcMemFree (FC_MEM_CHARSET
, sizeof (FcCharSet
));
1245 FcNameUnparseCharSet (FcStrBuf
*buf
, const FcCharSet
*c
)
1253 for (FcCharSetIterStart (c
, &ci
);
1255 FcCharSetIterNext (c
, &ci
))
1257 if (!FcCharSetUnparseValue (buf
, ci
.ucs4
))
1259 for (i
= 0; i
< 256/32; i
++)
1260 if (!FcCharSetUnparseValue (buf
, ci
.leaf
->map
[i
]))
1267 FcCharSetIter ci
, checki
;
1269 /* null terminate for parser */
1270 FcStrBufChar (buf
, '\0');
1271 /* step back over null for life after test */
1273 check
= FcNameParseCharSet (buf
->buf
+ len
);
1274 FcCharSetIterStart (c
, &ci
);
1275 FcCharSetIterStart (check
, &checki
);
1276 while (ci
.leaf
|| checki
.leaf
)
1278 if (ci
.ucs4
< checki
.ucs4
)
1280 printf ("Missing leaf node at 0x%x\n", ci
.ucs4
);
1281 FcCharSetIterNext (c
, &ci
);
1283 else if (checki
.ucs4
< ci
.ucs4
)
1285 printf ("Extra leaf node at 0x%x\n", checki
.ucs4
);
1286 FcCharSetIterNext (check
, &checki
);
1291 FcChar32
*cm
= ci
.leaf
->map
;
1292 FcChar32
*checkm
= checki
.leaf
->map
;
1294 for (i
= 0; i
< 256; i
+= 32)
1297 printf ("Mismatching sets at 0x%08x: 0x%08x != 0x%08x\n",
1298 ci
.ucs4
+ i
, *cm
, *checkm
);
1302 FcCharSetIterNext (c
, &ci
);
1303 FcCharSetIterNext (check
, &checki
);
1306 if ((missing
= FcCharSetSubtractCount (c
, check
)))
1307 printf ("%d missing in reparsed result\n", missing
);
1308 if ((missing
= FcCharSetSubtractCount (check
, c
)))
1309 printf ("%d extra in reparsed result\n", missing
);
1310 FcCharSetDestroy (check
);
1318 FcCharSetNewBank(void)
1321 charset_numbers_count
= 0;
1322 charset_leaf_count
= 0;
1323 charset_leaf_idx_count
= 0;
1327 FcCharSetNeededBytes (const FcCharSet
*c
)
1329 /* yes, there's redundancy */
1331 charset_leaf_idx_count
+= c
->num
;
1332 charset_leaf_count
+= c
->num
;
1333 charset_numbers_count
+= c
->num
;
1334 return sizeof (FcCharSet
) +
1335 sizeof (int) * c
->num
+ /* leaf_idx */
1336 sizeof (FcCharLeaf
) * c
->num
+ /* leaf */
1337 sizeof (FcChar16
) * c
->num
; /* number */
1341 FcCharSetNeededBytesAlign (void)
1343 return fc_alignof (FcCharSet
) + fc_alignof (int) +
1344 fc_alignof (FcCharLeaf
) + fc_alignof (FcChar16
);
1348 FcCharSetEnsureBank (int bi
)
1350 if (!charsets
|| charset_bank_count
<= bi
)
1352 int new_count
= charset_bank_count
+ 2;
1359 cs
= realloc(charsets
, sizeof(FcCharSet
*) * new_count
);
1361 n
= realloc(numbers
, sizeof(FcChar16
*) * new_count
);
1363 lvs
= realloc(leaves
, sizeof(FcCharLeaf
*) * new_count
);
1365 lvi
= realloc(leaf_idx
, sizeof(int*) * new_count
);
1368 charsets
= cs
; numbers
= n
; leaves
= lvs
; leaf_idx
= lvi
;
1369 for (i
= charset_bank_count
; i
< new_count
; i
++)
1376 charset_bank_count
= new_count
;
1382 FcCharSetDistributeBytes (FcCache
* metadata
, void * block_ptr
)
1384 int bi
= FcCacheBankToIndex(metadata
->bank
);
1385 if (!FcCharSetEnsureBank(bi
))
1388 block_ptr
= ALIGN (block_ptr
, FcCharSet
);
1389 charsets
[bi
] = (FcCharSet
*)block_ptr
;
1390 block_ptr
= (void *)((char *)block_ptr
+
1391 (sizeof (FcCharSet
) * charset_count
));
1392 block_ptr
= ALIGN (block_ptr
, FcChar16
);
1393 numbers
[bi
] = (FcChar16
*)block_ptr
;
1394 block_ptr
= (void *)((char *)block_ptr
+
1395 (sizeof(FcChar16
) * charset_numbers_count
));
1396 block_ptr
= ALIGN (block_ptr
, FcCharLeaf
);
1397 leaves
[bi
] = (FcCharLeaf
*)block_ptr
;
1398 block_ptr
= (void *)((char *)block_ptr
+
1399 (sizeof(FcCharLeaf
) * charset_leaf_count
));
1400 block_ptr
= ALIGN (block_ptr
, int);
1401 leaf_idx
[bi
] = (int *)block_ptr
;
1402 block_ptr
= (void *)((char *)block_ptr
+
1403 (sizeof(int) * charset_leaf_idx_count
));
1405 metadata
->charset_count
= charset_count
;
1406 metadata
->charset_numbers_count
= charset_numbers_count
;
1407 metadata
->charset_leaf_count
= charset_leaf_count
;
1408 metadata
->charset_leaf_idx_count
= charset_leaf_idx_count
;
1409 charset_ptr
= 0; charset_leaf_ptr
= 0;
1410 charset_leaf_idx_ptr
= 0; charset_numbers_ptr
= 0;
1415 FcCharSetSerialize(int bank
, FcCharSet
*c
)
1419 int bi
= FcCacheBankToIndex(bank
), cp
= charset_ptr
;
1421 new.ref
= FC_REF_CONSTANT
;
1423 new.u
.stat
.leafidx_offset
= charset_leaf_idx_ptr
;
1424 new.u
.stat
.numbers_offset
= charset_numbers_ptr
;
1427 charsets
[bi
][charset_ptr
++] = new;
1429 for (i
= 0; i
< c
->num
; i
++)
1431 leaf_idx
[bi
][charset_leaf_idx_ptr
++] = charset_leaf_ptr
;
1432 memcpy (&leaves
[bi
][charset_leaf_ptr
++],
1433 c
->u
.dyn
.leaves
[i
], sizeof(FcCharLeaf
));
1434 numbers
[bi
][charset_numbers_ptr
++] = c
->u
.dyn
.numbers
[i
];
1437 return &charsets
[bi
][cp
];
1441 FcCharSetUnserialize (FcCache
*metadata
, void *block_ptr
)
1443 int bi
= FcCacheBankToIndex(metadata
->bank
);
1444 if (!FcCharSetEnsureBank(bi
))
1447 block_ptr
= ALIGN (block_ptr
, FcCharSet
);
1448 charsets
[bi
] = (FcCharSet
*)block_ptr
;
1449 block_ptr
= (void *)((char *)block_ptr
+
1450 (sizeof (FcCharSet
) * metadata
->charset_count
));
1451 block_ptr
= ALIGN (block_ptr
, FcChar16
);
1452 numbers
[bi
] = (FcChar16
*)block_ptr
;
1453 block_ptr
= (void *)((char *)block_ptr
+
1454 (sizeof(FcChar16
) * metadata
->charset_numbers_count
));
1455 block_ptr
= ALIGN (block_ptr
, FcCharLeaf
);
1456 leaves
[bi
] = (FcCharLeaf
*)block_ptr
;
1457 block_ptr
= (void *)((char *)block_ptr
+
1458 (sizeof(FcCharLeaf
) * metadata
->charset_leaf_count
));
1459 block_ptr
= ALIGN (block_ptr
, int);
1460 leaf_idx
[bi
] = (int *)block_ptr
;
1461 block_ptr
= (void *)((char *)block_ptr
+
1462 (sizeof(int) * metadata
->charset_leaf_idx_count
));
1468 FcCharSetGetLeaf(const FcCharSet
*c
, int i
)
1471 if (c
->bank
== FC_BANK_DYNAMIC
)
1472 return c
->u
.dyn
.leaves
[i
];
1473 bi
= FcCacheBankToIndex(c
->bank
);
1475 return &leaves
[bi
][leaf_idx
[bi
][c
->u
.stat
.leafidx_offset
+i
]];
1479 FcCharSetGetNumbers(const FcCharSet
*c
)
1482 if (c
->bank
== FC_BANK_DYNAMIC
)
1483 return c
->u
.dyn
.numbers
;
1484 bi
= FcCacheBankToIndex(c
->bank
);
1486 return &numbers
[bi
][c
->u
.stat
.numbers_offset
];