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