]> git.wh0rd.org - fontconfig.git/blame - src/fccharset.c
Fix warning.
[fontconfig.git] / src / fccharset.c
CommitLineData
24330d27 1/*
4bd4418a 2 * $RCSId: xc/lib/fontconfig/src/fccharset.c,v 1.18 2002/08/22 07:36:44 keithp Exp $
24330d27 3 *
46b51147 4 * Copyright © 2001 Keith Packard
24330d27
KP
5 *
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.
15 *
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.
23 */
24
25#include <stdlib.h>
26#include "fcint.h"
27
28/* #define CHECK */
29
20ac65ab 30/* #define CHATTY */
24330d27 31
4262e0b3
PL
32static FcCharSet ** charsets = 0;
33static FcChar16 ** numbers = 0;
34static int charset_bank_count = 0, charset_ptr, charset_count;
cd2ec1a9 35static int charset_numbers_ptr, charset_numbers_count;
4262e0b3 36static FcCharLeaf ** leaves = 0;
cd2ec1a9 37static int charset_leaf_ptr, charset_leaf_count;
4262e0b3 38static int ** leaf_idx = 0;
cd2ec1a9
PL
39static int charset_leaf_idx_ptr, charset_leaf_idx_count;
40
a81f23c0
PL
41extern const FcChar16 langBankNumbers[];
42extern const FcCharLeaf langBankLeaves[];
43extern const int langBankLeafIdx[];
82f35f8b
PL
44
45static FcBool
46FcCharSetEnsureBank (int bi);
47
48void
49FcLangCharSetPopulate (void)
50{
19ea60bc 51 int bi = FcCacheBankToIndexMTF (FC_BANK_LANGS);
82f35f8b
PL
52 FcCharSetEnsureBank (bi);
53 charsets[bi] = 0;
6cc02fe6
PL
54 numbers[bi] = (FcChar16 *)langBankNumbers;
55 leaves[bi] = (FcCharLeaf *)langBankLeaves;
56 leaf_idx[bi] = (int *)langBankLeafIdx;
82f35f8b
PL
57}
58
24330d27
KP
59FcCharSet *
60FcCharSetCreate (void)
61{
62 FcCharSet *fcs;
63
64 fcs = (FcCharSet *) malloc (sizeof (FcCharSet));
65 if (!fcs)
66 return 0;
67 FcMemAlloc (FC_MEM_CHARSET, sizeof (FcCharSet));
68 fcs->ref = 1;
20ac65ab 69 fcs->num = 0;
4262e0b3 70 fcs->bank = FC_BANK_DYNAMIC;
cd2ec1a9
PL
71 fcs->u.dyn.leaves = 0;
72 fcs->u.dyn.numbers = 0;
24330d27
KP
73 return fcs;
74}
75
76FcCharSet *
77FcCharSetNew (void);
78
79FcCharSet *
80FcCharSetNew (void)
81{
82 return FcCharSetCreate ();
83}
84
24330d27
KP
85void
86FcCharSetDestroy (FcCharSet *fcs)
87{
d8d73958
KP
88 int i;
89 if (fcs->ref == FC_REF_CONSTANT)
90 return;
91 if (--fcs->ref > 0)
92 return;
4262e0b3 93 if (fcs->bank == FC_BANK_DYNAMIC)
d8d73958 94 {
cd2ec1a9
PL
95 for (i = 0; i < fcs->num; i++)
96 {
97 FcMemFree (FC_MEM_CHARLEAF, sizeof (FcCharLeaf));
98 free (fcs->u.dyn.leaves[i]);
99 }
100 if (fcs->u.dyn.leaves)
101 {
102 FcMemFree (FC_MEM_CHARSET, fcs->num * sizeof (FcCharLeaf *));
103 free (fcs->u.dyn.leaves);
104 }
105 if (fcs->u.dyn.numbers)
106 {
107 FcMemFree (FC_MEM_CHARSET, fcs->num * sizeof (FcChar16));
108 free (fcs->u.dyn.numbers);
109 }
d8d73958
KP
110 }
111 FcMemFree (FC_MEM_CHARSET, sizeof (FcCharSet));
112 free (fcs);
24330d27
KP
113}
114
115/*
20ac65ab
KP
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
24330d27
KP
119 */
120
20ac65ab
KP
121static int
122FcCharSetFindLeafPos (const FcCharSet *fcs, FcChar32 ucs4)
123{
cd2ec1a9 124 FcChar16 *numbers = FcCharSetGetNumbers(fcs);
20ac65ab
KP
125 FcChar16 page;
126 int low = 0;
127 int high = fcs->num - 1;
128
129 if (!numbers)
130 return -1;
131 ucs4 >>= 8;
132 while (low <= high)
133 {
134 int mid = (low + high) >> 1;
135 page = numbers[mid];
136 if (page == ucs4)
137 return mid;
138 if (page < ucs4)
139 low = mid + 1;
140 else
141 high = mid - 1;
142 }
2fcac349 143 if (high < 0 || (high < fcs->num && numbers[high] < ucs4))
20ac65ab
KP
144 high++;
145 return -(high + 1);
146}
147
24330d27
KP
148static FcCharLeaf *
149FcCharSetFindLeaf (const FcCharSet *fcs, FcChar32 ucs4)
150{
20ac65ab
KP
151 int pos = FcCharSetFindLeafPos (fcs, ucs4);
152 if (pos >= 0)
cd2ec1a9 153 return FcCharSetGetLeaf(fcs, pos);
20ac65ab
KP
154 return 0;
155}
24330d27 156
20ac65ab
KP
157static FcBool
158FcCharSetPutLeaf (FcCharSet *fcs,
159 FcChar32 ucs4,
160 FcCharLeaf *leaf,
161 int pos)
162{
163 FcCharLeaf **leaves;
164 FcChar16 *numbers;
165
166 ucs4 >>= 8;
167 if (ucs4 >= 0x10000)
168 return FcFalse;
4262e0b3 169 if (fcs->bank != FC_BANK_DYNAMIC)
cd2ec1a9
PL
170 {
171 int i;
172
173 leaves = malloc ((fcs->num + 1) * sizeof (FcCharLeaf *));
174 if (!leaves)
175 return FcFalse;
176 FcMemAlloc (FC_MEM_CHARSET, (fcs->num + 1) * sizeof (FcCharLeaf *));
177 numbers = malloc ((fcs->num + 1) * sizeof (FcChar16));
178 if (!numbers)
179 return FcFalse;
180 FcMemAlloc (FC_MEM_CHARSET, (fcs->num + 1) * sizeof (FcChar16));
181
182 for (i = 0; i < fcs->num; i++)
183 leaves[i] = FcCharSetGetLeaf(fcs, i);
184 memcpy (numbers, FcCharSetGetNumbers(fcs),
185 fcs->num * sizeof (FcChar16));
cd2ec1a9 186 }
20ac65ab 187 else
cd2ec1a9
PL
188 {
189 if (!fcs->u.dyn.leaves)
190 leaves = malloc (sizeof (FcCharLeaf *));
191 else
192 leaves = realloc (fcs->u.dyn.leaves, (fcs->num + 1) * sizeof (FcCharLeaf *));
193 if (!leaves)
194 return FcFalse;
195 if (fcs->num)
196 FcMemFree (FC_MEM_CHARSET, fcs->num * sizeof (FcCharLeaf *));
197 FcMemAlloc (FC_MEM_CHARSET, (fcs->num + 1) * sizeof (FcCharLeaf *));
198 fcs->u.dyn.leaves = leaves;
199 if (!fcs->u.dyn.numbers)
200 numbers = malloc (sizeof (FcChar16));
201 else
202 numbers = realloc (fcs->u.dyn.numbers, (fcs->num + 1) * sizeof (FcChar16));
203 if (!numbers)
204 return FcFalse;
205 if (fcs->num)
206 FcMemFree (FC_MEM_CHARSET, fcs->num * sizeof (FcChar16));
207 FcMemAlloc (FC_MEM_CHARSET, (fcs->num + 1) * sizeof (FcChar16));
208 fcs->u.dyn.numbers = numbers;
209 }
20ac65ab 210
cd2ec1a9 211 memmove (fcs->u.dyn.leaves + pos + 1, fcs->u.dyn.leaves + pos,
20ac65ab 212 (fcs->num - pos) * sizeof (FcCharLeaf *));
cd2ec1a9 213 memmove (fcs->u.dyn.numbers + pos + 1, fcs->u.dyn.numbers + pos,
20ac65ab 214 (fcs->num - pos) * sizeof (FcChar16));
cd2ec1a9
PL
215 fcs->u.dyn.numbers[pos] = (FcChar16) ucs4;
216 fcs->u.dyn.leaves[pos] = leaf;
20ac65ab
KP
217 fcs->num++;
218 return FcTrue;
24330d27
KP
219}
220
221/*
222 * Locate the leaf containing the specified char, creating it
223 * if desired
224 */
225
c647f6f1 226FcCharLeaf *
24330d27
KP
227FcCharSetFindLeafCreate (FcCharSet *fcs, FcChar32 ucs4)
228{
20ac65ab
KP
229 int pos;
230 FcCharLeaf *leaf;
24330d27 231
20ac65ab
KP
232 pos = FcCharSetFindLeafPos (fcs, ucs4);
233 if (pos >= 0)
cd2ec1a9 234 return FcCharSetGetLeaf(fcs, pos);
20ac65ab
KP
235
236 leaf = calloc (1, sizeof (FcCharLeaf));
237 if (!leaf)
238 return 0;
239
240 pos = -pos - 1;
241 if (!FcCharSetPutLeaf (fcs, ucs4, leaf, pos))
24330d27 242 {
20ac65ab
KP
243 free (leaf);
244 return 0;
24330d27 245 }
d8d73958 246 FcMemAlloc (FC_MEM_CHARLEAF, sizeof (FcCharLeaf));
20ac65ab
KP
247 return leaf;
248}
249
250static FcBool
251FcCharSetInsertLeaf (FcCharSet *fcs, FcChar32 ucs4, FcCharLeaf *leaf)
252{
253 int pos;
254
255 pos = FcCharSetFindLeafPos (fcs, ucs4);
256 if (pos >= 0)
24330d27 257 {
d8d73958 258 FcMemFree (FC_MEM_CHARLEAF, sizeof (FcCharLeaf));
4262e0b3 259 if (fcs->bank == FC_BANK_DYNAMIC)
cd2ec1a9
PL
260 {
261 free (fcs->u.dyn.leaves[pos]);
262 fcs->u.dyn.leaves[pos] = leaf;
263 }
264 else
265 {
a81f23c0
PL
266 int bi = FcCacheBankToIndex(fcs->bank);
267 leaves[bi][leaf_idx[fcs->bank][fcs->u.stat.leafidx_offset]+pos] = *leaf;
cd2ec1a9 268 }
20ac65ab 269 return FcTrue;
24330d27 270 }
20ac65ab
KP
271 pos = -pos - 1;
272 return FcCharSetPutLeaf (fcs, ucs4, leaf, pos);
24330d27
KP
273}
274
275FcBool
276FcCharSetAddChar (FcCharSet *fcs, FcChar32 ucs4)
277{
278 FcCharLeaf *leaf;
279 FcChar32 *b;
280
d8d73958 281 if (fcs->ref == FC_REF_CONSTANT)
24330d27
KP
282 return FcFalse;
283 leaf = FcCharSetFindLeafCreate (fcs, ucs4);
284 if (!leaf)
285 return FcFalse;
286 b = &leaf->map[(ucs4 & 0xff) >> 5];
287 *b |= (1 << (ucs4 & 0x1f));
288 return FcTrue;
289}
290
291/*
292 * An iterator for the leaves of a charset
293 */
294
295typedef struct _fcCharSetIter {
296 FcCharLeaf *leaf;
297 FcChar32 ucs4;
20ac65ab 298 int pos;
24330d27
KP
299} FcCharSetIter;
300
301/*
20ac65ab 302 * Set iter->leaf to the leaf containing iter->ucs4 or higher
24330d27 303 */
24330d27
KP
304
305static void
306FcCharSetIterSet (const FcCharSet *fcs, FcCharSetIter *iter)
307{
20ac65ab 308 int pos = FcCharSetFindLeafPos (fcs, iter->ucs4);
1412a699 309
20ac65ab 310 if (pos < 0)
1412a699 311 {
20ac65ab
KP
312 pos = -pos - 1;
313 if (pos == fcs->num)
1412a699 314 {
20ac65ab
KP
315 iter->ucs4 = ~0;
316 iter->leaf = 0;
317 return;
1412a699 318 }
cd2ec1a9 319 iter->ucs4 = (FcChar32) FcCharSetGetNumbers(fcs)[pos] << 8;
1412a699 320 }
cd2ec1a9 321 iter->leaf = FcCharSetGetLeaf(fcs, pos);
20ac65ab
KP
322 iter->pos = pos;
323#ifdef CHATTY
324 printf ("set %08x: %08x\n", iter->ucs4, (FcChar32) iter->leaf);
1412a699 325#endif
24330d27
KP
326}
327
328static void
329FcCharSetIterNext (const FcCharSet *fcs, FcCharSetIter *iter)
330{
20ac65ab
KP
331 int pos = iter->pos + 1;
332 if (pos >= fcs->num)
1412a699
KP
333 {
334 iter->ucs4 = ~0;
335 iter->leaf = 0;
1412a699 336 }
20ac65ab 337 else
1412a699 338 {
cd2ec1a9
PL
339 iter->ucs4 = (FcChar32) FcCharSetGetNumbers(fcs)[pos] << 8;
340 iter->leaf = FcCharSetGetLeaf(fcs, pos);
20ac65ab 341 iter->pos = pos;
1412a699 342 }
1412a699
KP
343}
344
20ac65ab 345#ifdef CHATTY
1412a699 346static void
20ac65ab 347FcCharSetDump (const FcCharSet *fcs)
1412a699 348{
20ac65ab
KP
349 int pos;
350
351 printf ("fcs %08x:\n", (FcChar32) fcs);
352 for (pos = 0; pos < fcs->num; pos++)
1412a699 353 {
20ac65ab
KP
354 FcCharLeaf *leaf = fcs->leaves[pos];
355 FcChar32 ucs4 = (FcChar32) fcs->numbers[pos] << 8;
356
357 printf (" %08x: %08x\n", ucs4, (FcChar32) leaf);
1412a699 358 }
24330d27 359}
1412a699 360#endif
24330d27
KP
361
362static void
363FcCharSetIterStart (const FcCharSet *fcs, FcCharSetIter *iter)
364{
20ac65ab
KP
365#ifdef CHATTY
366 FcCharSetDump (fcs);
1412a699 367#endif
24330d27 368 iter->ucs4 = 0;
67accef4 369 iter->pos = 0;
24330d27
KP
370 FcCharSetIterSet (fcs, iter);
371}
372
373FcCharSet *
374FcCharSetCopy (FcCharSet *src)
375{
d8d73958
KP
376 if (src->ref != FC_REF_CONSTANT)
377 src->ref++;
24330d27
KP
378 return src;
379}
380
381FcBool
382FcCharSetEqual (const FcCharSet *a, const FcCharSet *b)
383{
384 FcCharSetIter ai, bi;
385 int i;
386
387 if (a == b)
388 return FcTrue;
389 for (FcCharSetIterStart (a, &ai), FcCharSetIterStart (b, &bi);
390 ai.leaf && bi.leaf;
391 FcCharSetIterNext (a, &ai), FcCharSetIterNext (b, &bi))
392 {
393 if (ai.ucs4 != bi.ucs4)
394 return FcFalse;
395 for (i = 0; i < 256/32; i++)
396 if (ai.leaf->map[i] != bi.leaf->map[i])
397 return FcFalse;
398 }
399 return ai.leaf == bi.leaf;
400}
401
402static FcBool
403FcCharSetAddLeaf (FcCharSet *fcs,
404 FcChar32 ucs4,
405 FcCharLeaf *leaf)
406{
407 FcCharLeaf *new = FcCharSetFindLeafCreate (fcs, ucs4);
408 if (!new)
409 return FcFalse;
410 *new = *leaf;
411 return FcTrue;
412}
413
414static FcCharSet *
415FcCharSetOperate (const FcCharSet *a,
416 const FcCharSet *b,
417 FcBool (*overlap) (FcCharLeaf *result,
418 const FcCharLeaf *al,
419 const FcCharLeaf *bl),
420 FcBool aonly,
421 FcBool bonly)
422{
423 FcCharSet *fcs;
424 FcCharSetIter ai, bi;
425
426 fcs = FcCharSetCreate ();
427 if (!fcs)
428 goto bail0;
429 FcCharSetIterStart (a, &ai);
430 FcCharSetIterStart (b, &bi);
1412a699 431 while ((ai.leaf || (bonly && bi.leaf)) && (bi.leaf || (aonly && ai.leaf)))
24330d27
KP
432 {
433 if (ai.ucs4 < bi.ucs4)
434 {
435 if (aonly)
436 {
437 if (!FcCharSetAddLeaf (fcs, ai.ucs4, ai.leaf))
438 goto bail1;
439 FcCharSetIterNext (a, &ai);
440 }
441 else
442 {
443 ai.ucs4 = bi.ucs4;
444 FcCharSetIterSet (a, &ai);
445 }
446 }
447 else if (bi.ucs4 < ai.ucs4 )
448 {
449 if (bonly)
450 {
451 if (!FcCharSetAddLeaf (fcs, bi.ucs4, bi.leaf))
452 goto bail1;
453 FcCharSetIterNext (b, &bi);
454 }
455 else
456 {
457 bi.ucs4 = ai.ucs4;
458 FcCharSetIterSet (b, &bi);
459 }
460 }
461 else
462 {
463 FcCharLeaf leaf;
464
465 if ((*overlap) (&leaf, ai.leaf, bi.leaf))
466 {
467 if (!FcCharSetAddLeaf (fcs, ai.ucs4, &leaf))
468 goto bail1;
469 }
470 FcCharSetIterNext (a, &ai);
471 FcCharSetIterNext (b, &bi);
472 }
473 }
474 return fcs;
475bail1:
476 FcCharSetDestroy (fcs);
477bail0:
478 return 0;
479}
480
481static FcBool
482FcCharSetIntersectLeaf (FcCharLeaf *result,
483 const FcCharLeaf *al,
484 const FcCharLeaf *bl)
485{
486 int i;
487 FcBool nonempty = FcFalse;
488
489 for (i = 0; i < 256/32; i++)
490 if ((result->map[i] = al->map[i] & bl->map[i]))
491 nonempty = FcTrue;
492 return nonempty;
493}
494
495FcCharSet *
496FcCharSetIntersect (const FcCharSet *a, const FcCharSet *b)
497{
498 return FcCharSetOperate (a, b, FcCharSetIntersectLeaf, FcFalse, FcFalse);
499}
500
501static FcBool
502FcCharSetUnionLeaf (FcCharLeaf *result,
503 const FcCharLeaf *al,
504 const FcCharLeaf *bl)
505{
506 int i;
507
508 for (i = 0; i < 256/32; i++)
509 result->map[i] = al->map[i] | bl->map[i];
510 return FcTrue;
511}
512
513FcCharSet *
514FcCharSetUnion (const FcCharSet *a, const FcCharSet *b)
515{
516 return FcCharSetOperate (a, b, FcCharSetUnionLeaf, FcTrue, FcTrue);
517}
518
519static FcBool
520FcCharSetSubtractLeaf (FcCharLeaf *result,
521 const FcCharLeaf *al,
522 const FcCharLeaf *bl)
523{
524 int i;
525 FcBool nonempty = FcFalse;
526
527 for (i = 0; i < 256/32; i++)
528 if ((result->map[i] = al->map[i] & ~bl->map[i]))
529 nonempty = FcTrue;
530 return nonempty;
531}
532
533FcCharSet *
534FcCharSetSubtract (const FcCharSet *a, const FcCharSet *b)
535{
536 return FcCharSetOperate (a, b, FcCharSetSubtractLeaf, FcTrue, FcFalse);
537}
538
539FcBool
540FcCharSetHasChar (const FcCharSet *fcs, FcChar32 ucs4)
541{
542 FcCharLeaf *leaf = FcCharSetFindLeaf (fcs, ucs4);
543 if (!leaf)
544 return FcFalse;
545 return (leaf->map[(ucs4 & 0xff) >> 5] & (1 << (ucs4 & 0x1f))) != 0;
546}
547
548static FcChar32
549FcCharSetPopCount (FcChar32 c1)
550{
551 /* hackmem 169 */
552 FcChar32 c2 = (c1 >> 1) & 033333333333;
553 c2 = c1 - c2 - ((c2 >> 1) & 033333333333);
554 return (((c2 + (c2 >> 3)) & 030707070707) % 077);
555}
556
557FcChar32
558FcCharSetIntersectCount (const FcCharSet *a, const FcCharSet *b)
559{
560 FcCharSetIter ai, bi;
561 FcChar32 count = 0;
562
563 FcCharSetIterStart (a, &ai);
564 FcCharSetIterStart (b, &bi);
565 while (ai.leaf && bi.leaf)
566 {
567 if (ai.ucs4 == bi.ucs4)
568 {
569 FcChar32 *am = ai.leaf->map;
570 FcChar32 *bm = bi.leaf->map;
571 int i = 256/32;
572 while (i--)
573 count += FcCharSetPopCount (*am++ & *bm++);
574 FcCharSetIterNext (a, &ai);
575 }
576 else if (ai.ucs4 < bi.ucs4)
577 {
578 ai.ucs4 = bi.ucs4;
579 FcCharSetIterSet (a, &ai);
580 }
581 if (bi.ucs4 < ai.ucs4)
582 {
583 bi.ucs4 = ai.ucs4;
584 FcCharSetIterSet (b, &bi);
585 }
586 }
587 return count;
588}
589
590FcChar32
591FcCharSetCount (const FcCharSet *a)
592{
593 FcCharSetIter ai;
594 FcChar32 count = 0;
595
596 for (FcCharSetIterStart (a, &ai); ai.leaf; FcCharSetIterNext (a, &ai))
597 {
598 int i = 256/32;
599 FcChar32 *am = ai.leaf->map;
600
601 while (i--)
602 count += FcCharSetPopCount (*am++);
603 }
604 return count;
605}
606
607FcChar32
608FcCharSetSubtractCount (const FcCharSet *a, const FcCharSet *b)
609{
610 FcCharSetIter ai, bi;
611 FcChar32 count = 0;
612
613 FcCharSetIterStart (a, &ai);
614 FcCharSetIterStart (b, &bi);
615 while (ai.leaf)
616 {
617 if (ai.ucs4 <= bi.ucs4)
618 {
619 FcChar32 *am = ai.leaf->map;
620 int i = 256/32;
621 if (ai.ucs4 == bi.ucs4)
622 {
623 FcChar32 *bm = bi.leaf->map;;
624 while (i--)
625 count += FcCharSetPopCount (*am++ & ~*bm++);
626 }
627 else
628 {
629 while (i--)
630 count += FcCharSetPopCount (*am++);
631 }
632 FcCharSetIterNext (a, &ai);
633 }
634 else if (bi.leaf)
635 {
636 bi.ucs4 = ai.ucs4;
637 FcCharSetIterSet (b, &bi);
638 }
639 }
640 return count;
641}
642
1412a699
KP
643/*
644 * return FcTrue iff a is a subset of b
645 */
646FcBool
647FcCharSetIsSubset (const FcCharSet *a, const FcCharSet *b)
648{
20ac65ab
KP
649 int ai, bi;
650 FcChar16 an, bn;
651
652 if (a == b) return FcTrue;
653 bi = 0;
654 ai = 0;
655 while (ai < a->num && bi < b->num)
bc9469ba 656 {
cd2ec1a9
PL
657 an = FcCharSetGetNumbers(a)[ai];
658 bn = FcCharSetGetNumbers(b)[bi];
f45d39b1
KP
659 /*
660 * Check matching pages
661 */
20ac65ab 662 if (an == bn)
1412a699 663 {
cd2ec1a9
PL
664 FcChar32 *am = FcCharSetGetLeaf(a, ai)->map;
665 FcChar32 *bm = FcCharSetGetLeaf(b, bi)->map;
20ac65ab
KP
666
667 if (am != bm)
bc9469ba 668 {
20ac65ab 669 int i = 256/32;
f45d39b1
KP
670 /*
671 * Does am have any bits not in bm?
672 */
20ac65ab
KP
673 while (i--)
674 if (*am++ & ~*bm++)
675 return FcFalse;
bc9469ba 676 }
20ac65ab
KP
677 ai++;
678 bi++;
679 }
f45d39b1
KP
680 /*
681 * Does a have any pages not in b?
682 */
20ac65ab
KP
683 else if (an < bn)
684 return FcFalse;
685 else
686 {
687 int low = bi + 1;
688 int high = b->num - 1;
689
f45d39b1
KP
690 /*
691 * Search for page 'an' in 'b'
692 */
20ac65ab 693 while (low <= high)
bc9469ba 694 {
20ac65ab 695 int mid = (low + high) >> 1;
cd2ec1a9 696 bn = FcCharSetGetNumbers(b)[mid];
20ac65ab
KP
697 if (bn == an)
698 {
699 high = mid;
bc9469ba 700 break;
20ac65ab
KP
701 }
702 if (bn < an)
703 low = mid + 1;
704 else
705 high = mid - 1;
bc9469ba 706 }
20ac65ab 707 bi = high;
cd2ec1a9 708 while (bi < b->num && FcCharSetGetNumbers(b)[bi] < an)
20ac65ab 709 bi++;
1412a699 710 }
1412a699 711 }
f45d39b1
KP
712 /*
713 * did we look at every page?
714 */
1b16ef20 715 return ai >= a->num;
1412a699
KP
716}
717
36732012
KP
718/*
719 * These two functions efficiently walk the entire charmap for
720 * other software (like pango) that want their own copy
721 */
722
aae6f7d4 723FcChar32
36732012
KP
724FcCharSetNextPage (const FcCharSet *a,
725 FcChar32 map[FC_CHARSET_MAP_SIZE],
726 FcChar32 *next)
aae6f7d4
KP
727{
728 FcCharSetIter ai;
36732012 729 FcChar32 page;
aae6f7d4 730
36732012 731 ai.ucs4 = *next;
aae6f7d4
KP
732 FcCharSetIterSet (a, &ai);
733 if (!ai.leaf)
36732012
KP
734 return FC_CHARSET_DONE;
735
736 /*
737 * Save current information
738 */
739 page = ai.ucs4;
740 memcpy (map, ai.leaf->map, sizeof (ai.leaf->map));
741 /*
742 * Step to next page
743 */
744 FcCharSetIterNext (a, &ai);
745 *next = ai.ucs4;
746
aae6f7d4
KP
747 return page;
748}
749
36732012
KP
750FcChar32
751FcCharSetFirstPage (const FcCharSet *a,
752 FcChar32 map[FC_CHARSET_MAP_SIZE],
753 FcChar32 *next)
754{
755 *next = 0;
756 return FcCharSetNextPage (a, map, next);
757}
758
20ac65ab
KP
759/*
760 * old coverage API, rather hard to use correctly
761 */
762FcChar32
763FcCharSetCoverage (const FcCharSet *a, FcChar32 page, FcChar32 *result);
764
765FcChar32
766FcCharSetCoverage (const FcCharSet *a, FcChar32 page, FcChar32 *result)
767{
768 FcCharSetIter ai;
769
770 ai.ucs4 = page;
771 FcCharSetIterSet (a, &ai);
772 if (!ai.leaf)
773 {
774 memset (result, '\0', 256 / 8);
775 page = 0;
776 }
777 else
778 {
779 memcpy (result, ai.leaf->map, sizeof (ai.leaf->map));
780 FcCharSetIterNext (a, &ai);
781 page = ai.ucs4;
782 }
783 return page;
784}
785
24330d27
KP
786/*
787 * ASCII representation of charsets.
788 *
789 * Each leaf is represented as 9 32-bit values, the code of the first character followed
790 * by 8 32 bit values for the leaf itself. Each value is encoded as 5 ASCII characters,
791 * only 85 different values are used to avoid control characters as well as the other
792 * characters used to encode font names. 85**5 > 2^32 so things work out, but
793 * it's not exactly human readable output. As a special case, 0 is encoded as a space
794 */
795
82f4243f 796static const unsigned char charToValue[256] = {
24330d27
KP
797 /* "" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
798 /* "\b" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
799 /* "\020" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
800 /* "\030" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
801 /* " " */ 0xff, 0x00, 0xff, 0x01, 0x02, 0x03, 0x04, 0xff,
802 /* "(" */ 0x05, 0x06, 0x07, 0x08, 0xff, 0xff, 0x09, 0x0a,
803 /* "0" */ 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12,
804 /* "8" */ 0x13, 0x14, 0xff, 0x15, 0x16, 0xff, 0x17, 0x18,
805 /* "@" */ 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
806 /* "H" */ 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
807 /* "P" */ 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30,
808 /* "X" */ 0x31, 0x32, 0x33, 0x34, 0xff, 0x35, 0x36, 0xff,
809 /* "`" */ 0xff, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d,
810 /* "h" */ 0x3e, 0x3f, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45,
811 /* "p" */ 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d,
812 /* "x" */ 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53, 0x54, 0xff,
813 /* "\200" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
814 /* "\210" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
815 /* "\220" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
816 /* "\230" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
817 /* "\240" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
818 /* "\250" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
819 /* "\260" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
820 /* "\270" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
821 /* "\300" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
822 /* "\310" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
823 /* "\320" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
824 /* "\330" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
825 /* "\340" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
826 /* "\350" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
827 /* "\360" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
828 /* "\370" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
829};
830
82f4243f 831static const FcChar8 valueToChar[0x55] = {
24330d27
KP
832 /* 0x00 */ '!', '#', '$', '%', '&', '(', ')', '*',
833 /* 0x08 */ '+', '.', '/', '0', '1', '2', '3', '4',
834 /* 0x10 */ '5', '6', '7', '8', '9', ';', '<', '>',
835 /* 0x18 */ '?', '@', 'A', 'B', 'C', 'D', 'E', 'F',
836 /* 0x20 */ 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
837 /* 0x28 */ 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
838 /* 0x30 */ 'W', 'X', 'Y', 'Z', '[', ']', '^', 'a',
839 /* 0x38 */ 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
840 /* 0x40 */ 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q',
841 /* 0x48 */ 'r', 's', 't', 'u', 'v', 'w', 'x', 'y',
842 /* 0x50 */ 'z', '{', '|', '}', '~',
843};
844
845static FcChar8 *
846FcCharSetParseValue (FcChar8 *string, FcChar32 *value)
847{
848 int i;
849 FcChar32 v;
ccb3e93b 850 FcChar32 c;
24330d27
KP
851
852 if (*string == ' ')
853 {
854 v = 0;
855 string++;
856 }
857 else
858 {
859 v = 0;
860 for (i = 0; i < 5; i++)
861 {
ccb3e93b 862 if (!(c = (FcChar32) (unsigned char) *string++))
24330d27
KP
863 return 0;
864 c = charToValue[c];
865 if (c == 0xff)
866 return 0;
867 v = v * 85 + c;
868 }
869 }
870 *value = v;
871 return string;
872}
873
874static FcBool
c2e7c611 875FcCharSetUnparseValue (FcStrBuf *buf, FcChar32 value)
24330d27
KP
876{
877 int i;
878 if (value == 0)
879 {
c2e7c611 880 return FcStrBufChar (buf, ' ');
24330d27
KP
881 }
882 else
883 {
884 FcChar8 string[6];
885 FcChar8 *s = string + 5;
886 string[5] = '\0';
887 for (i = 0; i < 5; i++)
888 {
889 *--s = valueToChar[value % 85];
890 value /= 85;
891 }
892 for (i = 0; i < 5; i++)
c2e7c611 893 if (!FcStrBufChar (buf, *s++))
24330d27
KP
894 return FcFalse;
895 }
896 return FcTrue;
897}
898
20ac65ab
KP
899typedef struct _FcCharLeafEnt FcCharLeafEnt;
900
901struct _FcCharLeafEnt {
902 FcCharLeafEnt *next;
903 FcChar32 hash;
904 FcCharLeaf leaf;
905};
906
907#define FC_CHAR_LEAF_BLOCK (4096 / sizeof (FcCharLeafEnt))
34cd0514
CW
908static FcCharLeafEnt **FcCharLeafBlocks;
909static int FcCharLeafBlockCount;
20ac65ab
KP
910
911static FcCharLeafEnt *
912FcCharLeafEntCreate (void)
913{
914 static FcCharLeafEnt *block;
915 static int remain;
916
917 if (!remain)
918 {
34cd0514
CW
919 FcCharLeafEnt **newBlocks;
920
921 FcCharLeafBlockCount++;
922 newBlocks = realloc (FcCharLeafBlocks, FcCharLeafBlockCount * sizeof (FcCharLeafEnt *));
923 if (!newBlocks)
924 return 0;
925 FcCharLeafBlocks = newBlocks;
926 block = FcCharLeafBlocks[FcCharLeafBlockCount-1] = malloc (FC_CHAR_LEAF_BLOCK * sizeof (FcCharLeafEnt));
20ac65ab
KP
927 if (!block)
928 return 0;
d8d73958 929 FcMemAlloc (FC_MEM_CHARLEAF, FC_CHAR_LEAF_BLOCK * sizeof (FcCharLeafEnt));
20ac65ab
KP
930 remain = FC_CHAR_LEAF_BLOCK;
931 }
932 remain--;
933 return block++;
934}
935
936#define FC_CHAR_LEAF_HASH_SIZE 257
937
938static FcChar32
939FcCharLeafHash (FcCharLeaf *leaf)
940{
941 FcChar32 hash = 0;
942 int i;
943
944 for (i = 0; i < 256/32; i++)
945 hash = ((hash << 1) | (hash >> 31)) ^ leaf->map[i];
946 return hash;
947}
948
949static int FcCharLeafTotal;
950static int FcCharLeafUsed;
951
34cd0514
CW
952static FcCharLeafEnt *FcCharLeafHashTable[FC_CHAR_LEAF_HASH_SIZE];
953
20ac65ab 954static FcCharLeaf *
4c003605 955FcCharSetFreezeLeaf (FcCharLeaf *leaf)
20ac65ab 956{
20ac65ab 957 FcChar32 hash = FcCharLeafHash (leaf);
34cd0514 958 FcCharLeafEnt **bucket = &FcCharLeafHashTable[hash % FC_CHAR_LEAF_HASH_SIZE];
20ac65ab
KP
959 FcCharLeafEnt *ent;
960
961 FcCharLeafTotal++;
962 for (ent = *bucket; ent; ent = ent->next)
963 {
964 if (ent->hash == hash && !memcmp (&ent->leaf, leaf, sizeof (FcCharLeaf)))
965 return &ent->leaf;
966 }
967
968 ent = FcCharLeafEntCreate();
969 if (!ent)
970 return 0;
971 FcCharLeafUsed++;
972 ent->leaf = *leaf;
973 ent->hash = hash;
974 ent->next = *bucket;
975 *bucket = ent;
976 return &ent->leaf;
977}
978
34cd0514
CW
979static void
980FcCharSetThawAllLeaf (void)
981{
982 int i;
983
984 for (i = 0; i < FC_CHAR_LEAF_HASH_SIZE; i++)
985 FcCharLeafHashTable[i] = 0;
986
987 FcCharLeafTotal = 0;
988 FcCharLeafUsed = 0;
989
990 for (i = 0; i < FcCharLeafBlockCount; i++)
991 free (FcCharLeafBlocks[i]);
992
993 free (FcCharLeafBlocks);
994 FcCharLeafBlocks = 0;
995 FcCharLeafBlockCount = 0;
996}
997
20ac65ab
KP
998typedef struct _FcCharSetEnt FcCharSetEnt;
999
1000struct _FcCharSetEnt {
1001 FcCharSetEnt *next;
1002 FcChar32 hash;
1003 FcCharSet set;
1004};
1005
1006#define FC_CHAR_SET_HASH_SIZE 67
1007
1008static FcChar32
1009FcCharSetHash (FcCharSet *fcs)
1010{
1011 FcChar32 hash = 0;
20ac65ab
KP
1012 int i;
1013
1014 /* hash in leaves */
67accef4 1015 for (i = 0; i < fcs->num * (int) (sizeof (FcCharLeaf *) / sizeof (FcChar32)); i++)
cd2ec1a9 1016 hash = ((hash << 1) | (hash >> 31)) ^ (FcChar32)(FcCharSetGetLeaf(fcs, i)->map);
20ac65ab
KP
1017 /* hash in numbers */
1018 for (i = 0; i < fcs->num; i++)
cd2ec1a9 1019 hash = ((hash << 1) | (hash >> 31)) ^ *FcCharSetGetNumbers(fcs);
20ac65ab
KP
1020 return hash;
1021}
1022
1023static int FcCharSetTotal;
1024static int FcCharSetUsed;
1025static int FcCharSetTotalEnts, FcCharSetUsedEnts;
1026
34cd0514
CW
1027static FcCharSetEnt *FcCharSetHashTable[FC_CHAR_SET_HASH_SIZE];
1028
20ac65ab 1029static FcCharSet *
4c003605 1030FcCharSetFreezeBase (FcCharSet *fcs)
20ac65ab 1031{
20ac65ab 1032 FcChar32 hash = FcCharSetHash (fcs);
34cd0514 1033 FcCharSetEnt **bucket = &FcCharSetHashTable[hash % FC_CHAR_SET_HASH_SIZE];
20ac65ab 1034 FcCharSetEnt *ent;
d8d73958 1035 int size;
20ac65ab
KP
1036
1037 FcCharSetTotal++;
1038 FcCharSetTotalEnts += fcs->num;
1039 for (ent = *bucket; ent; ent = ent->next)
1040 {
1041 if (ent->hash == hash &&
1042 ent->set.num == fcs->num &&
cd2ec1a9
PL
1043 !memcmp (FcCharSetGetNumbers(&ent->set),
1044 FcCharSetGetNumbers(fcs),
20ac65ab
KP
1045 fcs->num * sizeof (FcChar16)))
1046 {
cd2ec1a9
PL
1047 FcBool ok = FcTrue;
1048 int i;
1049
1050 for (i = 0; i < fcs->num; i++)
1051 if (FcCharSetGetLeaf(&ent->set, i) != FcCharSetGetLeaf(fcs, i))
1052 ok = FcFalse;
1053 if (ok)
1054 return &ent->set;
20ac65ab
KP
1055 }
1056 }
1057
d8d73958
KP
1058 size = (sizeof (FcCharSetEnt) +
1059 fcs->num * sizeof (FcCharLeaf *) +
1060 fcs->num * sizeof (FcChar16));
1061 ent = malloc (size);
20ac65ab
KP
1062 if (!ent)
1063 return 0;
d8d73958 1064 FcMemAlloc (FC_MEM_CHARSET, size);
20ac65ab
KP
1065 FcCharSetUsed++;
1066 FcCharSetUsedEnts += fcs->num;
1067
d8d73958 1068 ent->set.ref = FC_REF_CONSTANT;
20ac65ab 1069 ent->set.num = fcs->num;
4262e0b3
PL
1070 ent->set.bank = fcs->bank;
1071 if (fcs->bank == FC_BANK_DYNAMIC)
5c7fb827 1072 {
cd2ec1a9
PL
1073 if (fcs->num)
1074 {
1075 ent->set.u.dyn.leaves = (FcCharLeaf **) (ent + 1);
1076 ent->set.u.dyn.numbers = (FcChar16 *) (ent->set.u.dyn.leaves + fcs->num);
1077 memcpy (ent->set.u.dyn.leaves, fcs->u.dyn.leaves, fcs->num * sizeof (FcCharLeaf *));
1078 memcpy (ent->set.u.dyn.numbers, fcs->u.dyn.numbers, fcs->num * sizeof (FcChar16));
1079 }
1080 else
1081 {
1082 ent->set.u.dyn.leaves = 0;
1083 ent->set.u.dyn.numbers = 0;
1084 }
5c7fb827
KP
1085 }
1086 else
1087 {
cd2ec1a9
PL
1088 ent->set.u.stat.leafidx_offset = fcs->u.stat.leafidx_offset;
1089 ent->set.u.stat.numbers_offset = fcs->u.stat.numbers_offset;
5c7fb827 1090 }
20ac65ab
KP
1091
1092 ent->hash = hash;
1093 ent->next = *bucket;
1094 *bucket = ent;
1095 return &ent->set;
1096}
1097
34cd0514
CW
1098void
1099FcCharSetThawAll (void)
1100{
1101 int i;
1102 FcCharSetEnt *ent, *next;
1103
1104 for (i = 0; i < FC_CHAR_SET_HASH_SIZE; i++)
1105 {
1106 for (ent = FcCharSetHashTable[i]; ent; ent = next)
1107 {
1108 next = ent->next;
1109 free (ent);
1110 }
1111 FcCharSetHashTable[i] = 0;
1112 }
1113
1114 FcCharSetTotal = 0;
1115 FcCharSetTotalEnts = 0;
1116 FcCharSetUsed = 0;
1117 FcCharSetUsedEnts = 0;
1118
1119 FcCharSetThawAllLeaf ();
1120}
1121
4c003605
KP
1122FcCharSet *
1123FcCharSetFreeze (FcCharSet *fcs)
1124{
1125 FcCharSet *b;
1126 FcCharSet *n = 0;
1127 FcCharLeaf *l;
1128 int i;
1129
1130 b = FcCharSetCreate ();
1131 if (!b)
1132 goto bail0;
1133 for (i = 0; i < fcs->num; i++)
1134 {
cd2ec1a9 1135 l = FcCharSetFreezeLeaf (FcCharSetGetLeaf(fcs, i));
4c003605
KP
1136 if (!l)
1137 goto bail1;
cd2ec1a9 1138 if (!FcCharSetInsertLeaf (b, FcCharSetGetNumbers(fcs)[i] << 8, l))
4c003605
KP
1139 goto bail1;
1140 }
1141 n = FcCharSetFreezeBase (b);
1142bail1:
4262e0b3 1143 if (b->bank == FC_BANK_DYNAMIC)
d8d73958 1144 {
cd2ec1a9
PL
1145 if (b->u.dyn.leaves)
1146 {
1147 FcMemFree (FC_MEM_CHARSET, b->num * sizeof (FcCharLeaf *));
1148 free (b->u.dyn.leaves);
1149 }
1150 if (b->u.dyn.numbers)
1151 {
1152 FcMemFree (FC_MEM_CHARSET, b->num * sizeof (FcChar16));
1153 free (b->u.dyn.numbers);
1154 }
d8d73958
KP
1155 }
1156 FcMemFree (FC_MEM_CHARSET, sizeof (FcCharSet));
4c003605
KP
1157 free (b);
1158bail0:
1159 return n;
1160}
1161
24330d27
KP
1162FcCharSet *
1163FcNameParseCharSet (FcChar8 *string)
1164{
20ac65ab 1165 FcCharSet *c, *n = 0;
24330d27
KP
1166 FcChar32 ucs4;
1167 FcCharLeaf *leaf;
20ac65ab
KP
1168 FcCharLeaf temp;
1169 FcChar32 bits;
24330d27
KP
1170 int i;
1171
1172 c = FcCharSetCreate ();
1173 if (!c)
1174 goto bail0;
1175 while (*string)
1176 {
1177 string = FcCharSetParseValue (string, &ucs4);
1178 if (!string)
1179 goto bail1;
20ac65ab 1180 bits = 0;
24330d27
KP
1181 for (i = 0; i < 256/32; i++)
1182 {
20ac65ab 1183 string = FcCharSetParseValue (string, &temp.map[i]);
24330d27
KP
1184 if (!string)
1185 goto bail1;
20ac65ab
KP
1186 bits |= temp.map[i];
1187 }
1188 if (bits)
1189 {
4c003605 1190 leaf = FcCharSetFreezeLeaf (&temp);
20ac65ab
KP
1191 if (!leaf)
1192 goto bail1;
1193 if (!FcCharSetInsertLeaf (c, ucs4, leaf))
1194 goto bail1;
24330d27
KP
1195 }
1196 }
20ac65ab
KP
1197#ifdef CHATTY
1198 printf (" %8s %8s %8s %8s\n", "total", "totalmem", "new", "newmem");
1199 printf ("Leaves: %8d %8d %8d %8d\n",
1200 FcCharLeafTotal, sizeof (FcCharLeaf) * FcCharLeafTotal,
1201 FcCharLeafUsed, sizeof (FcCharLeaf) * FcCharLeafUsed);
1202 printf ("Charsets: %8d %8d %8d %8d\n",
1203 FcCharSetTotal, sizeof (FcCharSet) * FcCharSetTotal,
1204 FcCharSetUsed, sizeof (FcCharSet) * FcCharSetUsed);
1205 printf ("Tables: %8d %8d %8d %8d\n",
1206 FcCharSetTotalEnts, FcCharSetTotalEnts * (sizeof (FcCharLeaf *) + sizeof (FcChar16)),
1207 FcCharSetUsedEnts, FcCharSetUsedEnts * (sizeof (FcCharLeaf *) + sizeof (FcChar16)));
1208 printf ("Total: %8s %8d %8s %8d\n",
1209 "",
1210 sizeof (FcCharLeaf) * FcCharLeafTotal +
1211 sizeof (FcCharSet) * FcCharSetTotal +
1212 FcCharSetTotalEnts * (sizeof (FcCharLeaf *) + sizeof (FcChar16)),
1213 "",
1214 sizeof (FcCharLeaf) * FcCharLeafUsed +
1215 sizeof (FcCharSet) * FcCharSetUsed +
1216 FcCharSetUsedEnts * (sizeof (FcCharLeaf *) + sizeof (FcChar16)));
1217#endif
4c003605 1218 n = FcCharSetFreezeBase (c);
24330d27 1219bail1:
4262e0b3 1220 if (c->bank == FC_BANK_DYNAMIC)
d8d73958 1221 {
cd2ec1a9
PL
1222 if (c->u.dyn.leaves)
1223 {
1224 FcMemFree (FC_MEM_CHARSET, c->num * sizeof (FcCharLeaf *));
1225 free (c->u.dyn.leaves);
1226 }
1227 if (c->u.dyn.numbers)
1228 {
1229 FcMemFree (FC_MEM_CHARSET, c->num * sizeof (FcChar16));
1230 free (c->u.dyn.numbers);
1231 }
1232 FcMemFree (FC_MEM_CHARSET, sizeof (FcCharSet));
d8d73958 1233 }
20ac65ab 1234 free (c);
24330d27 1235bail0:
20ac65ab 1236 return n;
24330d27
KP
1237}
1238
1239FcBool
c2e7c611 1240FcNameUnparseCharSet (FcStrBuf *buf, const FcCharSet *c)
24330d27
KP
1241{
1242 FcCharSetIter ci;
1243 int i;
1244#ifdef CHECK
1245 int len = buf->len;
1246#endif
1247
1248 for (FcCharSetIterStart (c, &ci);
1249 ci.leaf;
1250 FcCharSetIterNext (c, &ci))
1251 {
1252 if (!FcCharSetUnparseValue (buf, ci.ucs4))
1253 return FcFalse;
1254 for (i = 0; i < 256/32; i++)
1255 if (!FcCharSetUnparseValue (buf, ci.leaf->map[i]))
1256 return FcFalse;
1257 }
1258#ifdef CHECK
1259 {
1260 FcCharSet *check;
1261 FcChar32 missing;
1262 FcCharSetIter ci, checki;
1263
1264 /* null terminate for parser */
c2e7c611 1265 FcStrBufChar (buf, '\0');
24330d27
KP
1266 /* step back over null for life after test */
1267 buf->len--;
1268 check = FcNameParseCharSet (buf->buf + len);
1269 FcCharSetIterStart (c, &ci);
1270 FcCharSetIterStart (check, &checki);
1271 while (ci.leaf || checki.leaf)
1272 {
1273 if (ci.ucs4 < checki.ucs4)
1274 {
1275 printf ("Missing leaf node at 0x%x\n", ci.ucs4);
1276 FcCharSetIterNext (c, &ci);
1277 }
1278 else if (checki.ucs4 < ci.ucs4)
1279 {
1280 printf ("Extra leaf node at 0x%x\n", checki.ucs4);
1281 FcCharSetIterNext (check, &checki);
1282 }
1283 else
1284 {
1285 int i = 256/32;
1286 FcChar32 *cm = ci.leaf->map;
1287 FcChar32 *checkm = checki.leaf->map;
1288
1289 for (i = 0; i < 256; i += 32)
1290 {
1291 if (*cm != *checkm)
1292 printf ("Mismatching sets at 0x%08x: 0x%08x != 0x%08x\n",
1293 ci.ucs4 + i, *cm, *checkm);
1294 cm++;
1295 checkm++;
1296 }
1297 FcCharSetIterNext (c, &ci);
1298 FcCharSetIterNext (check, &checki);
1299 }
1300 }
1301 if ((missing = FcCharSetSubtractCount (c, check)))
1302 printf ("%d missing in reparsed result\n", missing);
1303 if ((missing = FcCharSetSubtractCount (check, c)))
1304 printf ("%d extra in reparsed result\n", missing);
1305 FcCharSetDestroy (check);
1306 }
1307#endif
1308
1309 return FcTrue;
1310}
cd2ec1a9 1311
4262e0b3
PL
1312void
1313FcCharSetNewBank(void)
cd2ec1a9 1314{
4262e0b3
PL
1315 charset_count = 0;
1316 charset_numbers_count = 0;
1317 charset_leaf_count = 0;
1318 charset_leaf_idx_count = 0;
cd2ec1a9
PL
1319}
1320
4262e0b3
PL
1321int
1322FcCharSetNeededBytes (const FcCharSet *c)
cd2ec1a9 1323{
4262e0b3 1324 /* yes, there's redundancy */
cd2ec1a9
PL
1325 charset_count++;
1326 charset_leaf_idx_count++;
1327 charset_leaf_count += c->num;
1328 charset_numbers_count += c->num;
4262e0b3
PL
1329 return sizeof (FcCharSet) +
1330 sizeof (int) + /* leaf_idx */
1331 sizeof (FcCharLeaf) * c->num + /* leaf */
1332 sizeof (FcChar16) * c->num; /* number */
1333}
1334
7fd7221e
PL
1335int
1336FcCharSetNeededBytesAlign (void)
1337{
1338 return __alignof__ (FcCharSet) + __alignof__ (int) +
1339 __alignof__ (FcCharLeaf) + __alignof__ (FcChar16);
1340}
1341
4262e0b3
PL
1342static FcBool
1343FcCharSetEnsureBank (int bi)
1344{
1345 if (!charsets || charset_bank_count <= bi)
1346 {
1347 int new_count = charset_bank_count + 2;
1348 FcCharSet ** cs;
1349 FcChar16 ** n;
1350 FcCharLeaf ** lvs;
1351 int ** lvi;
1352 int i;
1353
1354 cs = realloc(charsets, sizeof(FcCharSet*) * new_count);
1355 if (!cs) return 0;
1356 n = realloc(numbers, sizeof(FcChar16*) * new_count);
1357 if (!n) return 0;
1358 lvs = realloc(leaves, sizeof(FcCharLeaf*) * new_count);
1359 if (!lvs) return 0;
1360 lvi = realloc(leaf_idx, sizeof(int*) * new_count);
1361 if (!lvi) return 0;
1362
1363 charsets = cs; numbers = n; leaves = lvs; leaf_idx = lvi;
1364 for (i = charset_bank_count; i < new_count; i++)
1365 {
1366 charsets[i] = 0;
1367 numbers[i] = 0;
1368 leaves[i] = 0;
1369 leaf_idx[i] = 0;
1370 }
1371 charset_bank_count = new_count;
1372 }
cd2ec1a9
PL
1373 return FcTrue;
1374}
1375
4262e0b3
PL
1376void *
1377FcCharSetDistributeBytes (FcCache * metadata, void * block_ptr)
1378{
1379 int bi = FcCacheBankToIndex(metadata->bank);
1380 if (!FcCharSetEnsureBank(bi))
1381 return 0;
1382
7fd7221e 1383 block_ptr = ALIGN (block_ptr, FcCharSet);
1c5b6345 1384 charsets[bi] = (FcCharSet *)block_ptr;
4262e0b3
PL
1385 block_ptr = (void *)((char *)block_ptr +
1386 (sizeof (FcCharSet) * charset_count));
7fd7221e 1387 block_ptr = ALIGN (block_ptr, FcChar16);
1c5b6345 1388 numbers[bi] = (FcChar16 *)block_ptr;
4262e0b3
PL
1389 block_ptr = (void *)((char *)block_ptr +
1390 (sizeof(FcChar16) * charset_numbers_count));
7fd7221e 1391 block_ptr = ALIGN (block_ptr, FcCharLeaf);
1c5b6345 1392 leaves[bi] = (FcCharLeaf *)block_ptr;
4262e0b3
PL
1393 block_ptr = (void *)((char *)block_ptr +
1394 (sizeof(FcCharLeaf) * charset_leaf_count));
7fd7221e 1395 block_ptr = ALIGN (block_ptr, int);
1c5b6345 1396 leaf_idx[bi] = (int *)block_ptr;
4262e0b3
PL
1397 block_ptr = (void *)((char *)block_ptr +
1398 (sizeof(int) * charset_leaf_idx_count));
1399
1400 metadata->charset_count = charset_count;
1401 metadata->charset_numbers_count = charset_numbers_count;
1402 metadata->charset_leaf_count = charset_leaf_count;
1403 metadata->charset_leaf_idx_count = charset_leaf_idx_count;
1404 charset_ptr = 0; charset_leaf_ptr = 0;
1405 charset_leaf_idx_ptr = 0; charset_numbers_ptr = 0;
1406 return block_ptr;
1407}
1408
1409FcCharSet *
1410FcCharSetSerialize(int bank, FcCharSet *c)
cd2ec1a9
PL
1411{
1412 int i;
cd2ec1a9 1413 FcCharSet new;
4262e0b3 1414 int bi = FcCacheBankToIndex(bank), cp = charset_ptr;
cd2ec1a9 1415
212c9f43 1416 new.ref = FC_REF_CONSTANT;
4262e0b3 1417 new.bank = bank;
212c9f43 1418 new.u.stat.leafidx_offset = charset_leaf_idx_ptr;
cd2ec1a9 1419 new.u.stat.numbers_offset = charset_numbers_ptr;
212c9f43 1420 new.num = c->num;
cd2ec1a9 1421
4262e0b3 1422 charsets[bi][charset_ptr++] = new;
cd2ec1a9 1423
4262e0b3 1424 leaf_idx[bi][charset_leaf_idx_ptr++] = charset_leaf_ptr;
cd2ec1a9
PL
1425 for (i = 0; i < c->num; i++)
1426 {
4262e0b3 1427 memcpy (&leaves[bi][charset_leaf_ptr++],
cd2ec1a9 1428 c->u.dyn.leaves[i], sizeof(FcCharLeaf));
4262e0b3 1429 numbers[bi][charset_numbers_ptr++] = c->u.dyn.numbers[i];
cd2ec1a9
PL
1430 }
1431
4262e0b3 1432 return &charsets[bi][cp];
212c9f43
PL
1433}
1434
4262e0b3 1435void *
61571f3f 1436FcCharSetUnserialize (FcCache *metadata, void *block_ptr)
212c9f43 1437{
61571f3f 1438 int bi = FcCacheBankToIndex(metadata->bank);
4262e0b3
PL
1439 if (!FcCharSetEnsureBank(bi))
1440 return 0;
212c9f43 1441
1c5b6345 1442 block_ptr = ALIGN (block_ptr, FcCharSet);
4262e0b3
PL
1443 charsets[bi] = (FcCharSet *)block_ptr;
1444 block_ptr = (void *)((char *)block_ptr +
61571f3f 1445 (sizeof (FcCharSet) * metadata->charset_count));
1c5b6345 1446 block_ptr = ALIGN (block_ptr, FcChar16);
4262e0b3
PL
1447 numbers[bi] = (FcChar16 *)block_ptr;
1448 block_ptr = (void *)((char *)block_ptr +
61571f3f 1449 (sizeof(FcChar16) * metadata->charset_numbers_count));
1c5b6345 1450 block_ptr = ALIGN (block_ptr, FcCharLeaf);
4262e0b3
PL
1451 leaves[bi] = (FcCharLeaf *)block_ptr;
1452 block_ptr = (void *)((char *)block_ptr +
61571f3f 1453 (sizeof(FcCharLeaf) * metadata->charset_leaf_count));
1c5b6345 1454 block_ptr = ALIGN (block_ptr, int);
4262e0b3
PL
1455 leaf_idx[bi] = (int *)block_ptr;
1456 block_ptr = (void *)((char *)block_ptr +
61571f3f 1457 (sizeof(int) * metadata->charset_leaf_idx_count));
4262e0b3
PL
1458
1459 return block_ptr;
212c9f43
PL
1460}
1461
cd2ec1a9
PL
1462FcCharLeaf *
1463FcCharSetGetLeaf(const FcCharSet *c, int i)
1464{
4262e0b3
PL
1465 int bi;
1466 if (c->bank == FC_BANK_DYNAMIC)
cd2ec1a9 1467 return c->u.dyn.leaves[i];
4262e0b3
PL
1468 bi = FcCacheBankToIndex(c->bank);
1469
1470 return &leaves[bi][leaf_idx[bi][c->u.stat.leafidx_offset]+i];
cd2ec1a9
PL
1471}
1472
1473FcChar16 *
1474FcCharSetGetNumbers(const FcCharSet *c)
1475{
4262e0b3
PL
1476 int bi;
1477 if (c->bank == FC_BANK_DYNAMIC)
cd2ec1a9 1478 return c->u.dyn.numbers;
4262e0b3
PL
1479 bi = FcCacheBankToIndex(c->bank);
1480
1481 return &numbers[bi][c->u.stat.numbers_offset];
cd2ec1a9
PL
1482}
1483