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