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_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
;
42 FcCharSetClearStatic()
46 charset_ptr
= 0; charset_count
= 0;
48 charset_leaf_ptr
= 0; charset_leaf_count
= 0;
50 charset_leaf_idx_ptr
= 0; charset_leaf_idx_count
= 0;
54 FcCharSetCreate (void)
58 fcs
= (FcCharSet
*) malloc (sizeof (FcCharSet
));
61 FcMemAlloc (FC_MEM_CHARSET
, sizeof (FcCharSet
));
64 fcs
->storage
= FcStorageDynamic
;
65 fcs
->u
.dyn
.leaves
= 0;
66 fcs
->u
.dyn
.numbers
= 0;
76 return FcCharSetCreate ();
80 FcCharSetPtrDestroy (FcCharSetPtr fcs
)
82 FcCharSetDestroy (FcCharSetPtrU(fcs
));
83 if (fcs
.storage
== FcStorageDynamic
&&
84 FcCharSetPtrU(fcs
)->ref
!= FC_REF_CONSTANT
)
87 FcMemFree (FC_MEM_CHARSET
, sizeof(FcCharSet
));
92 FcCharSetDestroy (FcCharSet
*fcs
)
95 if (fcs
->ref
== FC_REF_CONSTANT
)
99 if (fcs
->storage
== FcStorageDynamic
)
101 for (i
= 0; i
< fcs
->num
; i
++)
103 FcMemFree (FC_MEM_CHARLEAF
, sizeof (FcCharLeaf
));
104 free (fcs
->u
.dyn
.leaves
[i
]);
106 if (fcs
->u
.dyn
.leaves
)
108 FcMemFree (FC_MEM_CHARSET
, fcs
->num
* sizeof (FcCharLeaf
*));
109 free (fcs
->u
.dyn
.leaves
);
111 if (fcs
->u
.dyn
.numbers
)
113 FcMemFree (FC_MEM_CHARSET
, fcs
->num
* sizeof (FcChar16
));
114 free (fcs
->u
.dyn
.numbers
);
117 FcMemFree (FC_MEM_CHARSET
, sizeof (FcCharSet
));
122 * Locate the leaf containing the specified char, return
123 * its index if it exists, otherwise return negative of
124 * the (position + 1) where it should be inserted
128 FcCharSetFindLeafPos (const FcCharSet
*fcs
, FcChar32 ucs4
)
130 FcChar16
*numbers
= FcCharSetGetNumbers(fcs
);
133 int high
= fcs
->num
- 1;
140 int mid
= (low
+ high
) >> 1;
149 if (high
< 0 || (high
< fcs
->num
&& numbers
[high
] < ucs4
))
155 FcCharSetFindLeaf (const FcCharSet
*fcs
, FcChar32 ucs4
)
157 int pos
= FcCharSetFindLeafPos (fcs
, ucs4
);
159 return FcCharSetGetLeaf(fcs
, pos
);
164 FcCharSetPutLeaf (FcCharSet
*fcs
,
175 if (fcs
->storage
== FcStorageStatic
)
179 leaves
= malloc ((fcs
->num
+ 1) * sizeof (FcCharLeaf
*));
182 FcMemAlloc (FC_MEM_CHARSET
, (fcs
->num
+ 1) * sizeof (FcCharLeaf
*));
183 numbers
= malloc ((fcs
->num
+ 1) * sizeof (FcChar16
));
186 FcMemAlloc (FC_MEM_CHARSET
, (fcs
->num
+ 1) * sizeof (FcChar16
));
188 for (i
= 0; i
< fcs
->num
; i
++)
189 leaves
[i
] = FcCharSetGetLeaf(fcs
, i
);
190 memcpy (numbers
, FcCharSetGetNumbers(fcs
),
191 fcs
->num
* sizeof (FcChar16
));
192 fcs
->storage
= FcStorageDynamic
;
196 if (!fcs
->u
.dyn
.leaves
)
197 leaves
= malloc (sizeof (FcCharLeaf
*));
199 leaves
= realloc (fcs
->u
.dyn
.leaves
, (fcs
->num
+ 1) * sizeof (FcCharLeaf
*));
203 FcMemFree (FC_MEM_CHARSET
, fcs
->num
* sizeof (FcCharLeaf
*));
204 FcMemAlloc (FC_MEM_CHARSET
, (fcs
->num
+ 1) * sizeof (FcCharLeaf
*));
205 fcs
->u
.dyn
.leaves
= leaves
;
206 if (!fcs
->u
.dyn
.numbers
)
207 numbers
= malloc (sizeof (FcChar16
));
209 numbers
= realloc (fcs
->u
.dyn
.numbers
, (fcs
->num
+ 1) * sizeof (FcChar16
));
213 FcMemFree (FC_MEM_CHARSET
, fcs
->num
* sizeof (FcChar16
));
214 FcMemAlloc (FC_MEM_CHARSET
, (fcs
->num
+ 1) * sizeof (FcChar16
));
215 fcs
->u
.dyn
.numbers
= numbers
;
218 memmove (fcs
->u
.dyn
.leaves
+ pos
+ 1, fcs
->u
.dyn
.leaves
+ pos
,
219 (fcs
->num
- pos
) * sizeof (FcCharLeaf
*));
220 memmove (fcs
->u
.dyn
.numbers
+ pos
+ 1, fcs
->u
.dyn
.numbers
+ pos
,
221 (fcs
->num
- pos
) * sizeof (FcChar16
));
222 fcs
->u
.dyn
.numbers
[pos
] = (FcChar16
) ucs4
;
223 fcs
->u
.dyn
.leaves
[pos
] = leaf
;
229 * Locate the leaf containing the specified char, creating it
234 FcCharSetFindLeafCreate (FcCharSet
*fcs
, FcChar32 ucs4
)
239 pos
= FcCharSetFindLeafPos (fcs
, ucs4
);
241 return FcCharSetGetLeaf(fcs
, pos
);
243 leaf
= calloc (1, sizeof (FcCharLeaf
));
248 if (!FcCharSetPutLeaf (fcs
, ucs4
, leaf
, pos
))
253 FcMemAlloc (FC_MEM_CHARLEAF
, sizeof (FcCharLeaf
));
258 FcCharSetInsertLeaf (FcCharSet
*fcs
, FcChar32 ucs4
, FcCharLeaf
*leaf
)
262 pos
= FcCharSetFindLeafPos (fcs
, ucs4
);
265 FcMemFree (FC_MEM_CHARLEAF
, sizeof (FcCharLeaf
));
266 if (fcs
->storage
== FcStorageDynamic
)
268 free (fcs
->u
.dyn
.leaves
[pos
]);
269 fcs
->u
.dyn
.leaves
[pos
] = leaf
;
273 leaves
[leaf_idx
[fcs
->u
.stat
.leafidx_offset
]+pos
] = *leaf
;
278 return FcCharSetPutLeaf (fcs
, ucs4
, leaf
, pos
);
282 FcCharSetAddChar (FcCharSet
*fcs
, FcChar32 ucs4
)
287 if (fcs
->ref
== FC_REF_CONSTANT
)
289 leaf
= FcCharSetFindLeafCreate (fcs
, ucs4
);
292 b
= &leaf
->map
[(ucs4
& 0xff) >> 5];
293 *b
|= (1 << (ucs4
& 0x1f));
298 * An iterator for the leaves of a charset
301 typedef struct _fcCharSetIter
{
308 * Set iter->leaf to the leaf containing iter->ucs4 or higher
312 FcCharSetIterSet (const FcCharSet
*fcs
, FcCharSetIter
*iter
)
314 int pos
= FcCharSetFindLeafPos (fcs
, iter
->ucs4
);
325 iter
->ucs4
= (FcChar32
) FcCharSetGetNumbers(fcs
)[pos
] << 8;
327 iter
->leaf
= FcCharSetGetLeaf(fcs
, pos
);
330 printf ("set %08x: %08x\n", iter
->ucs4
, (FcChar32
) iter
->leaf
);
335 FcCharSetIterNext (const FcCharSet
*fcs
, FcCharSetIter
*iter
)
337 int pos
= iter
->pos
+ 1;
345 iter
->ucs4
= (FcChar32
) FcCharSetGetNumbers(fcs
)[pos
] << 8;
346 iter
->leaf
= FcCharSetGetLeaf(fcs
, pos
);
353 FcCharSetDump (const FcCharSet
*fcs
)
357 printf ("fcs %08x:\n", (FcChar32
) fcs
);
358 for (pos
= 0; pos
< fcs
->num
; pos
++)
360 FcCharLeaf
*leaf
= fcs
->leaves
[pos
];
361 FcChar32 ucs4
= (FcChar32
) fcs
->numbers
[pos
] << 8;
363 printf (" %08x: %08x\n", ucs4
, (FcChar32
) leaf
);
369 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
* 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
.storage
= fcs
->storage
;
1076 if (fcs
->storage
== FcStorageDynamic
)
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
->storage
== FcStorageDynamic
)
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
->storage
== FcStorageDynamic
)
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
);
1319 FcCharSetPtrU (FcCharSetPtr ci
)
1323 case FcStorageDynamic
:
1325 case FcStorageStatic
:
1326 return &charsets
[ci
.u
.stat
];
1333 FcCharSetPtrCreateDynamic(FcCharSet
*c
)
1337 new.storage
= FcStorageDynamic
;
1343 FcCharSetPrepareSerialize(FcCharSet
*c
)
1345 /* note the redundancy */
1347 charset_leaf_idx_count
++;
1348 charset_leaf_count
+= c
->num
;
1349 charset_numbers_count
+= c
->num
;
1354 FcCharSetSerialize(FcCharSet
*c
)
1362 charsets
= malloc(sizeof(FcCharSet
) * charset_count
);
1363 if (!charsets
) goto bail
;
1364 numbers
= malloc(sizeof(FcChar16
) * charset_numbers_count
);
1365 if (!numbers
) goto bail1
;
1366 leaves
= malloc(sizeof(FcCharLeaf
) * charset_leaf_count
);
1367 if (!leaves
) goto bail2
;
1368 leaf_idx
= malloc(sizeof(int)*charset_leaf_idx_count
);
1369 if (!leaf_idx
) goto bail3
;
1373 new.storage
= FcStorageStatic
;
1374 new.u
.stat
.leafidx_offset
= charset_leaf_ptr
;
1375 new.u
.stat
.numbers_offset
= charset_numbers_ptr
;
1377 newp
.storage
= FcStorageStatic
;
1378 newp
.u
.stat
= charset_ptr
;
1379 charsets
[charset_ptr
++] = new;
1381 leaf_idx
[charset_leaf_idx_ptr
++] = charset_leaf_ptr
;
1382 for (i
= 0; i
< c
->num
; i
++)
1384 memcpy (&leaves
[charset_leaf_ptr
++],
1385 c
->u
.dyn
.leaves
[i
], sizeof(FcCharLeaf
));
1386 numbers
[charset_numbers_ptr
++] = c
->u
.dyn
.numbers
[i
];
1398 return FcCharSetPtrCreateDynamic(0);
1402 FcCharSetGetLeaf(const FcCharSet
*c
, int i
)
1406 case FcStorageDynamic
:
1407 return c
->u
.dyn
.leaves
[i
];
1408 case FcStorageStatic
:
1409 return &leaves
[leaf_idx
[c
->u
.stat
.leafidx_offset
]+i
];
1416 FcCharSetGetNumbers(const FcCharSet
*c
)
1420 case FcStorageDynamic
:
1421 return c
->u
.dyn
.numbers
;
1422 case FcStorageStatic
:
1423 return &numbers
[c
->u
.stat
.numbers_offset
];