]> git.wh0rd.org - fontconfig.git/blob - src/fccharset.c
Include $(top_srcdir), $(top_srcdir)/src before anything else.
[fontconfig.git] / src / fccharset.c
1 /*
2 * $RCSId: xc/lib/fontconfig/src/fccharset.c,v 1.18 2002/08/22 07:36:44 keithp Exp $
3 *
4 * Copyright © 2001 Keith Packard
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 "fcint.h"
26 #include <stdlib.h>
27
28 /* #define CHECK */
29
30 /* #define CHATTY */
31
32 static FcCharSet ** charsets = 0;
33 static FcChar16 ** numbers = 0;
34 static int charset_bank_count = 0, charset_ptr, charset_count;
35 static int charset_numbers_ptr, charset_numbers_count;
36 static FcCharLeaf ** leaves = 0;
37 static int charset_leaf_ptr, charset_leaf_count;
38 static int ** leaf_idx = 0;
39 static int charset_leaf_idx_ptr, charset_leaf_idx_count;
40
41 extern const FcChar16 langBankNumbers[];
42 extern const FcCharLeaf langBankLeaves[];
43 extern const int langBankLeafIdx[];
44
45 static FcBool
46 FcCharSetEnsureBank (int bi);
47
48 void
49 FcLangCharSetPopulate (void)
50 {
51 int bi = FcCacheBankToIndexMTF (FC_BANK_LANGS);
52 FcCharSetEnsureBank (bi);
53 charsets[bi] = 0;
54 numbers[bi] = (FcChar16 *)langBankNumbers;
55 leaves[bi] = (FcCharLeaf *)langBankLeaves;
56 leaf_idx[bi] = (int *)langBankLeafIdx;
57 }
58
59 FcCharSet *
60 FcCharSetCreate (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;
69 fcs->num = 0;
70 fcs->bank = FC_BANK_DYNAMIC;
71 fcs->u.dyn.leaves = 0;
72 fcs->u.dyn.numbers = 0;
73 return fcs;
74 }
75
76 FcCharSet *
77 FcCharSetNew (void);
78
79 FcCharSet *
80 FcCharSetNew (void)
81 {
82 return FcCharSetCreate ();
83 }
84
85 void
86 FcCharSetDestroy (FcCharSet *fcs)
87 {
88 int i;
89 if (fcs->ref == FC_REF_CONSTANT)
90 return;
91 if (--fcs->ref > 0)
92 return;
93 if (fcs->bank == FC_BANK_DYNAMIC)
94 {
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 }
110 }
111 FcMemFree (FC_MEM_CHARSET, sizeof (FcCharSet));
112 free (fcs);
113 }
114
115 /*
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
119 */
120
121 static int
122 FcCharSetFindLeafPos (const FcCharSet *fcs, FcChar32 ucs4)
123 {
124 FcChar16 *numbers = FcCharSetGetNumbers(fcs);
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 }
143 if (high < 0 || (high < fcs->num && numbers[high] < ucs4))
144 high++;
145 return -(high + 1);
146 }
147
148 static FcCharLeaf *
149 FcCharSetFindLeaf (const FcCharSet *fcs, FcChar32 ucs4)
150 {
151 int pos = FcCharSetFindLeafPos (fcs, ucs4);
152 if (pos >= 0)
153 return FcCharSetGetLeaf(fcs, pos);
154 return 0;
155 }
156
157 static FcBool
158 FcCharSetPutLeaf (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;
169 if (fcs->bank != FC_BANK_DYNAMIC)
170 {
171 /* convert to dynamic */
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)
180 {
181 free (leaves);
182 return FcFalse;
183 }
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));
190
191 fcs->bank = FC_BANK_DYNAMIC;
192 fcs->u.dyn.leaves = leaves;
193 fcs->u.dyn.numbers = numbers;
194 }
195 else
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 }
218
219 memmove (fcs->u.dyn.leaves + pos + 1, fcs->u.dyn.leaves + pos,
220 (fcs->num - pos) * sizeof (FcCharLeaf *));
221 memmove (fcs->u.dyn.numbers + pos + 1, fcs->u.dyn.numbers + pos,
222 (fcs->num - pos) * sizeof (FcChar16));
223 fcs->u.dyn.numbers[pos] = (FcChar16) ucs4;
224 fcs->u.dyn.leaves[pos] = leaf;
225 fcs->num++;
226 return FcTrue;
227 }
228
229 /*
230 * Locate the leaf containing the specified char, creating it
231 * if desired
232 */
233
234 FcCharLeaf *
235 FcCharSetFindLeafCreate (FcCharSet *fcs, FcChar32 ucs4)
236 {
237 int pos;
238 FcCharLeaf *leaf;
239
240 pos = FcCharSetFindLeafPos (fcs, ucs4);
241 if (pos >= 0)
242 return FcCharSetGetLeaf(fcs, pos);
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))
250 {
251 free (leaf);
252 return 0;
253 }
254 FcMemAlloc (FC_MEM_CHARLEAF, sizeof (FcCharLeaf));
255 return leaf;
256 }
257
258 static FcBool
259 FcCharSetInsertLeaf (FcCharSet *fcs, FcChar32 ucs4, FcCharLeaf *leaf)
260 {
261 int pos;
262
263 pos = FcCharSetFindLeafPos (fcs, ucs4);
264 if (pos >= 0)
265 {
266 FcMemFree (FC_MEM_CHARLEAF, sizeof (FcCharLeaf));
267 if (fcs->bank == FC_BANK_DYNAMIC)
268 {
269 free (fcs->u.dyn.leaves[pos]);
270 fcs->u.dyn.leaves[pos] = leaf;
271 }
272 else
273 {
274 int bi = FcCacheBankToIndex(fcs->bank);
275 leaves[bi][leaf_idx[fcs->bank][fcs->u.stat.leafidx_offset]+pos] = *leaf;
276 }
277 return FcTrue;
278 }
279 pos = -pos - 1;
280 return FcCharSetPutLeaf (fcs, ucs4, leaf, pos);
281 }
282
283 FcBool
284 FcCharSetAddChar (FcCharSet *fcs, FcChar32 ucs4)
285 {
286 FcCharLeaf *leaf;
287 FcChar32 *b;
288
289 if (fcs->ref == FC_REF_CONSTANT)
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
303 typedef struct _fcCharSetIter {
304 FcCharLeaf *leaf;
305 FcChar32 ucs4;
306 int pos;
307 } FcCharSetIter;
308
309 /*
310 * Set iter->leaf to the leaf containing iter->ucs4 or higher
311 */
312
313 static void
314 FcCharSetIterSet (const FcCharSet *fcs, FcCharSetIter *iter)
315 {
316 int pos = FcCharSetFindLeafPos (fcs, iter->ucs4);
317
318 if (pos < 0)
319 {
320 pos = -pos - 1;
321 if (pos == fcs->num)
322 {
323 iter->ucs4 = ~0;
324 iter->leaf = 0;
325 return;
326 }
327 iter->ucs4 = (FcChar32) FcCharSetGetNumbers(fcs)[pos] << 8;
328 }
329 iter->leaf = FcCharSetGetLeaf(fcs, pos);
330 iter->pos = pos;
331 #ifdef CHATTY
332 printf ("set %08x: %08x\n", iter->ucs4, (FcChar32) iter->leaf);
333 #endif
334 }
335
336 static void
337 FcCharSetIterNext (const FcCharSet *fcs, FcCharSetIter *iter)
338 {
339 int pos = iter->pos + 1;
340 if (pos >= fcs->num)
341 {
342 iter->ucs4 = ~0;
343 iter->leaf = 0;
344 }
345 else
346 {
347 iter->ucs4 = (FcChar32) FcCharSetGetNumbers(fcs)[pos] << 8;
348 iter->leaf = FcCharSetGetLeaf(fcs, pos);
349 iter->pos = pos;
350 }
351 }
352
353 #ifdef CHATTY
354 static void
355 FcCharSetDump (const FcCharSet *fcs)
356 {
357 int pos;
358
359 printf ("fcs %08x:\n", (FcChar32) fcs);
360 for (pos = 0; pos < fcs->num; pos++)
361 {
362 FcCharLeaf *leaf = fcs->leaves[pos];
363 FcChar32 ucs4 = (FcChar32) fcs->numbers[pos] << 8;
364
365 printf (" %08x: %08x\n", ucs4, (FcChar32) leaf);
366 }
367 }
368 #endif
369
370 static void
371 FcCharSetIterStart (const FcCharSet *fcs, FcCharSetIter *iter)
372 {
373 #ifdef CHATTY
374 FcCharSetDump (fcs);
375 #endif
376 iter->ucs4 = 0;
377 iter->pos = 0;
378 FcCharSetIterSet (fcs, iter);
379 }
380
381 FcCharSet *
382 FcCharSetCopy (FcCharSet *src)
383 {
384 if (src->ref != FC_REF_CONSTANT)
385 src->ref++;
386 return src;
387 }
388
389 FcBool
390 FcCharSetEqual (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
410 static FcBool
411 FcCharSetAddLeaf (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
422 static FcCharSet *
423 FcCharSetOperate (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);
439 while ((ai.leaf || (bonly && bi.leaf)) && (bi.leaf || (aonly && ai.leaf)))
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;
483 bail1:
484 FcCharSetDestroy (fcs);
485 bail0:
486 return 0;
487 }
488
489 static FcBool
490 FcCharSetIntersectLeaf (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
503 FcCharSet *
504 FcCharSetIntersect (const FcCharSet *a, const FcCharSet *b)
505 {
506 return FcCharSetOperate (a, b, FcCharSetIntersectLeaf, FcFalse, FcFalse);
507 }
508
509 static FcBool
510 FcCharSetUnionLeaf (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
521 FcCharSet *
522 FcCharSetUnion (const FcCharSet *a, const FcCharSet *b)
523 {
524 return FcCharSetOperate (a, b, FcCharSetUnionLeaf, FcTrue, FcTrue);
525 }
526
527 static FcBool
528 FcCharSetSubtractLeaf (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
541 FcCharSet *
542 FcCharSetSubtract (const FcCharSet *a, const FcCharSet *b)
543 {
544 return FcCharSetOperate (a, b, FcCharSetSubtractLeaf, FcTrue, FcFalse);
545 }
546
547 FcBool
548 FcCharSetHasChar (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
556 static FcChar32
557 FcCharSetPopCount (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
565 FcChar32
566 FcCharSetIntersectCount (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
598 FcChar32
599 FcCharSetCount (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
615 FcChar32
616 FcCharSetSubtractCount (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 {
631 FcChar32 *bm = bi.leaf->map;
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
651 /*
652 * return FcTrue iff a is a subset of b
653 */
654 FcBool
655 FcCharSetIsSubset (const FcCharSet *a, const FcCharSet *b)
656 {
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)
664 {
665 an = FcCharSetGetNumbers(a)[ai];
666 bn = FcCharSetGetNumbers(b)[bi];
667 /*
668 * Check matching pages
669 */
670 if (an == bn)
671 {
672 FcChar32 *am = FcCharSetGetLeaf(a, ai)->map;
673 FcChar32 *bm = FcCharSetGetLeaf(b, bi)->map;
674
675 if (am != bm)
676 {
677 int i = 256/32;
678 /*
679 * Does am have any bits not in bm?
680 */
681 while (i--)
682 if (*am++ & ~*bm++)
683 return FcFalse;
684 }
685 ai++;
686 bi++;
687 }
688 /*
689 * Does a have any pages not in b?
690 */
691 else if (an < bn)
692 return FcFalse;
693 else
694 {
695 int low = bi + 1;
696 int high = b->num - 1;
697
698 /*
699 * Search for page 'an' in 'b'
700 */
701 while (low <= high)
702 {
703 int mid = (low + high) >> 1;
704 bn = FcCharSetGetNumbers(b)[mid];
705 if (bn == an)
706 {
707 high = mid;
708 break;
709 }
710 if (bn < an)
711 low = mid + 1;
712 else
713 high = mid - 1;
714 }
715 bi = high;
716 while (bi < b->num && FcCharSetGetNumbers(b)[bi] < an)
717 bi++;
718 }
719 }
720 /*
721 * did we look at every page?
722 */
723 return ai >= a->num;
724 }
725
726 /*
727 * These two functions efficiently walk the entire charmap for
728 * other software (like pango) that want their own copy
729 */
730
731 FcChar32
732 FcCharSetNextPage (const FcCharSet *a,
733 FcChar32 map[FC_CHARSET_MAP_SIZE],
734 FcChar32 *next)
735 {
736 FcCharSetIter ai;
737 FcChar32 page;
738
739 ai.ucs4 = *next;
740 FcCharSetIterSet (a, &ai);
741 if (!ai.leaf)
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
755 return page;
756 }
757
758 FcChar32
759 FcCharSetFirstPage (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
767 /*
768 * old coverage API, rather hard to use correctly
769 */
770 FcChar32
771 FcCharSetCoverage (const FcCharSet *a, FcChar32 page, FcChar32 *result);
772
773 FcChar32
774 FcCharSetCoverage (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
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
804 static const unsigned char charToValue[256] = {
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
839 static const FcChar8 valueToChar[0x55] = {
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
853 static FcChar8 *
854 FcCharSetParseValue (FcChar8 *string, FcChar32 *value)
855 {
856 int i;
857 FcChar32 v;
858 FcChar32 c;
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 {
870 if (!(c = (FcChar32) (unsigned char) *string++))
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
882 static FcBool
883 FcCharSetUnparseValue (FcStrBuf *buf, FcChar32 value)
884 {
885 int i;
886 if (value == 0)
887 {
888 return FcStrBufChar (buf, ' ');
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++)
901 if (!FcStrBufChar (buf, *s++))
902 return FcFalse;
903 }
904 return FcTrue;
905 }
906
907 typedef struct _FcCharLeafEnt FcCharLeafEnt;
908
909 struct _FcCharLeafEnt {
910 FcCharLeafEnt *next;
911 FcChar32 hash;
912 FcCharLeaf leaf;
913 };
914
915 #define FC_CHAR_LEAF_BLOCK (4096 / sizeof (FcCharLeafEnt))
916 static FcCharLeafEnt **FcCharLeafBlocks;
917 static int FcCharLeafBlockCount;
918
919 static FcCharLeafEnt *
920 FcCharLeafEntCreate (void)
921 {
922 static FcCharLeafEnt *block;
923 static int remain;
924
925 if (!remain)
926 {
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));
935 if (!block)
936 return 0;
937 FcMemAlloc (FC_MEM_CHARLEAF, FC_CHAR_LEAF_BLOCK * sizeof (FcCharLeafEnt));
938 remain = FC_CHAR_LEAF_BLOCK;
939 }
940 remain--;
941 return block++;
942 }
943
944 #define FC_CHAR_LEAF_HASH_SIZE 257
945
946 static FcChar32
947 FcCharLeafHash (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
957 static int FcCharLeafTotal;
958 static int FcCharLeafUsed;
959
960 static FcCharLeafEnt *FcCharLeafHashTable[FC_CHAR_LEAF_HASH_SIZE];
961
962 static FcCharLeaf *
963 FcCharSetFreezeLeaf (FcCharLeaf *leaf)
964 {
965 FcChar32 hash = FcCharLeafHash (leaf);
966 FcCharLeafEnt **bucket = &FcCharLeafHashTable[hash % FC_CHAR_LEAF_HASH_SIZE];
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
987 static void
988 FcCharSetThawAllLeaf (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
1006 typedef struct _FcCharSetEnt FcCharSetEnt;
1007
1008 struct _FcCharSetEnt {
1009 FcCharSetEnt *next;
1010 FcChar32 hash;
1011 FcCharSet set;
1012 };
1013
1014 #define FC_CHAR_SET_HASH_SIZE 67
1015
1016 static FcChar32
1017 FcCharSetHash (FcCharSet *fcs)
1018 {
1019 FcChar32 hash = 0;
1020 int i;
1021
1022 /* hash in leaves */
1023 for (i = 0; i < fcs->num * (int) (sizeof (FcCharLeaf *) / sizeof (FcChar32)); i++)
1024 hash = ((hash << 1) | (hash >> 31)) ^ (FcChar32)(FcCharSetGetLeaf(fcs, i)->map);
1025 /* hash in numbers */
1026 for (i = 0; i < fcs->num; i++)
1027 hash = ((hash << 1) | (hash >> 31)) ^ *FcCharSetGetNumbers(fcs);
1028 return hash;
1029 }
1030
1031 static int FcCharSetTotal;
1032 static int FcCharSetUsed;
1033 static int FcCharSetTotalEnts, FcCharSetUsedEnts;
1034
1035 static FcCharSetEnt *FcCharSetHashTable[FC_CHAR_SET_HASH_SIZE];
1036
1037 static FcCharSet *
1038 FcCharSetFreezeBase (FcCharSet *fcs)
1039 {
1040 FcChar32 hash = FcCharSetHash (fcs);
1041 FcCharSetEnt **bucket = &FcCharSetHashTable[hash % FC_CHAR_SET_HASH_SIZE];
1042 FcCharSetEnt *ent;
1043 int size;
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 &&
1051 !memcmp (FcCharSetGetNumbers(&ent->set),
1052 FcCharSetGetNumbers(fcs),
1053 fcs->num * sizeof (FcChar16)))
1054 {
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;
1063 }
1064 }
1065
1066 size = (sizeof (FcCharSetEnt) +
1067 fcs->num * sizeof (FcCharLeaf *) +
1068 fcs->num * sizeof (FcChar16));
1069 ent = malloc (size);
1070 if (!ent)
1071 return 0;
1072 FcMemAlloc (FC_MEM_CHARSET, size);
1073 FcCharSetUsed++;
1074 FcCharSetUsedEnts += fcs->num;
1075
1076 ent->set.ref = FC_REF_CONSTANT;
1077 ent->set.num = fcs->num;
1078 ent->set.bank = fcs->bank;
1079 if (fcs->bank == FC_BANK_DYNAMIC)
1080 {
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 }
1093 }
1094 else
1095 {
1096 ent->set.u.stat.leafidx_offset = fcs->u.stat.leafidx_offset;
1097 ent->set.u.stat.numbers_offset = fcs->u.stat.numbers_offset;
1098 }
1099
1100 ent->hash = hash;
1101 ent->next = *bucket;
1102 *bucket = ent;
1103 return &ent->set;
1104 }
1105
1106 void
1107 FcCharSetThawAll (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
1130 FcCharSet *
1131 FcCharSetFreeze (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 {
1143 l = FcCharSetFreezeLeaf (FcCharSetGetLeaf(fcs, i));
1144 if (!l)
1145 goto bail1;
1146 if (!FcCharSetInsertLeaf (b, FcCharSetGetNumbers(fcs)[i] << 8, l))
1147 goto bail1;
1148 }
1149 n = FcCharSetFreezeBase (b);
1150 bail1:
1151 if (b->bank == FC_BANK_DYNAMIC)
1152 {
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 }
1163 }
1164 FcMemFree (FC_MEM_CHARSET, sizeof (FcCharSet));
1165 free (b);
1166 bail0:
1167 return n;
1168 }
1169
1170 FcCharSet *
1171 FcNameParseCharSet (FcChar8 *string)
1172 {
1173 FcCharSet *c, *n = 0;
1174 FcChar32 ucs4;
1175 FcCharLeaf *leaf;
1176 FcCharLeaf temp;
1177 FcChar32 bits;
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;
1188 bits = 0;
1189 for (i = 0; i < 256/32; i++)
1190 {
1191 string = FcCharSetParseValue (string, &temp.map[i]);
1192 if (!string)
1193 goto bail1;
1194 bits |= temp.map[i];
1195 }
1196 if (bits)
1197 {
1198 leaf = FcCharSetFreezeLeaf (&temp);
1199 if (!leaf)
1200 goto bail1;
1201 if (!FcCharSetInsertLeaf (c, ucs4, leaf))
1202 goto bail1;
1203 }
1204 }
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
1226 n = FcCharSetFreezeBase (c);
1227 bail1:
1228 if (c->bank == FC_BANK_DYNAMIC)
1229 {
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));
1241 }
1242 free (c);
1243 bail0:
1244 return n;
1245 }
1246
1247 FcBool
1248 FcNameUnparseCharSet (FcStrBuf *buf, const FcCharSet *c)
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 */
1273 FcStrBufChar (buf, '\0');
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 }
1319
1320 void
1321 FcCharSetNewBank(void)
1322 {
1323 charset_count = 0;
1324 charset_numbers_count = 0;
1325 charset_leaf_count = 0;
1326 charset_leaf_idx_count = 0;
1327 }
1328
1329 int
1330 FcCharSetNeededBytes (const FcCharSet *c)
1331 {
1332 /* yes, there's redundancy */
1333 charset_count++;
1334 charset_leaf_idx_count += c->num;
1335 charset_leaf_count += c->num;
1336 charset_numbers_count += c->num;
1337 return sizeof (FcCharSet) +
1338 sizeof (int) * c->num + /* leaf_idx */
1339 sizeof (FcCharLeaf) * c->num + /* leaf */
1340 sizeof (FcChar16) * c->num; /* number */
1341 }
1342
1343 int
1344 FcCharSetNeededBytesAlign (void)
1345 {
1346 return fc_alignof (FcCharSet) + fc_alignof (int) +
1347 fc_alignof (FcCharLeaf) + fc_alignof (FcChar16);
1348 }
1349
1350 static FcBool
1351 FcCharSetEnsureBank (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 }
1381 return FcTrue;
1382 }
1383
1384 void *
1385 FcCharSetDistributeBytes (FcCache * metadata, void * block_ptr)
1386 {
1387 int bi = FcCacheBankToIndex(metadata->bank);
1388 if (!FcCharSetEnsureBank(bi))
1389 return 0;
1390
1391 block_ptr = ALIGN (block_ptr, FcCharSet);
1392 charsets[bi] = (FcCharSet *)block_ptr;
1393 block_ptr = (void *)((char *)block_ptr +
1394 (sizeof (FcCharSet) * charset_count));
1395 block_ptr = ALIGN (block_ptr, FcChar16);
1396 numbers[bi] = (FcChar16 *)block_ptr;
1397 block_ptr = (void *)((char *)block_ptr +
1398 (sizeof(FcChar16) * charset_numbers_count));
1399 block_ptr = ALIGN (block_ptr, FcCharLeaf);
1400 leaves[bi] = (FcCharLeaf *)block_ptr;
1401 block_ptr = (void *)((char *)block_ptr +
1402 (sizeof(FcCharLeaf) * charset_leaf_count));
1403 block_ptr = ALIGN (block_ptr, int);
1404 leaf_idx[bi] = (int *)block_ptr;
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
1417 FcCharSet *
1418 FcCharSetSerialize(int bank, FcCharSet *c)
1419 {
1420 int i;
1421 FcCharSet new;
1422 int bi = FcCacheBankToIndex(bank), cp = charset_ptr;
1423
1424 new.ref = FC_REF_CONSTANT;
1425 new.bank = bank;
1426 new.u.stat.leafidx_offset = charset_leaf_idx_ptr;
1427 new.u.stat.numbers_offset = charset_numbers_ptr;
1428 new.num = c->num;
1429
1430 charsets[bi][charset_ptr++] = new;
1431
1432 for (i = 0; i < c->num; i++)
1433 {
1434 leaf_idx[bi][charset_leaf_idx_ptr++] = charset_leaf_ptr;
1435 memcpy (&leaves[bi][charset_leaf_ptr++],
1436 c->u.dyn.leaves[i], sizeof(FcCharLeaf));
1437 numbers[bi][charset_numbers_ptr++] = c->u.dyn.numbers[i];
1438 }
1439
1440 return &charsets[bi][cp];
1441 }
1442
1443 void *
1444 FcCharSetUnserialize (FcCache *metadata, void *block_ptr)
1445 {
1446 int bi = FcCacheBankToIndex(metadata->bank);
1447 if (!FcCharSetEnsureBank(bi))
1448 return 0;
1449
1450 block_ptr = ALIGN (block_ptr, FcCharSet);
1451 charsets[bi] = (FcCharSet *)block_ptr;
1452 block_ptr = (void *)((char *)block_ptr +
1453 (sizeof (FcCharSet) * metadata->charset_count));
1454 block_ptr = ALIGN (block_ptr, FcChar16);
1455 numbers[bi] = (FcChar16 *)block_ptr;
1456 block_ptr = (void *)((char *)block_ptr +
1457 (sizeof(FcChar16) * metadata->charset_numbers_count));
1458 block_ptr = ALIGN (block_ptr, FcCharLeaf);
1459 leaves[bi] = (FcCharLeaf *)block_ptr;
1460 block_ptr = (void *)((char *)block_ptr +
1461 (sizeof(FcCharLeaf) * metadata->charset_leaf_count));
1462 block_ptr = ALIGN (block_ptr, int);
1463 leaf_idx[bi] = (int *)block_ptr;
1464 block_ptr = (void *)((char *)block_ptr +
1465 (sizeof(int) * metadata->charset_leaf_idx_count));
1466
1467 return block_ptr;
1468 }
1469
1470 FcCharLeaf *
1471 FcCharSetGetLeaf(const FcCharSet *c, int i)
1472 {
1473 int bi;
1474 if (c->bank == FC_BANK_DYNAMIC)
1475 return c->u.dyn.leaves[i];
1476 bi = FcCacheBankToIndex(c->bank);
1477
1478 return &leaves[bi][leaf_idx[bi][c->u.stat.leafidx_offset+i]];
1479 }
1480
1481 FcChar16 *
1482 FcCharSetGetNumbers(const FcCharSet *c)
1483 {
1484 int bi;
1485 if (c->bank == FC_BANK_DYNAMIC)
1486 return c->u.dyn.numbers;
1487 bi = FcCacheBankToIndex(c->bank);
1488
1489 return &numbers[bi][c->u.stat.numbers_offset];
1490 }
1491