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