Debian Codegraph @

src/fccharset.c

raw
1 /*
2 * fontconfig/src/fccharset.c
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 the author(s) not be used in
11 * advertising or publicity pertaining to distribution of the software without
12 * specific, written prior permission. The authors make 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 * THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18 * EVENT SHALL THE AUTHOR(S) 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
27 #include <stdlib.h>
28
29 /* #define CHECK */
30
31 FcCharSet *
32 FcCharSetCreate (void)
33 {
34 FcCharSet *fcs;
35
36 fcs = (FcCharSet *)malloc (sizeof (FcCharSet));
37 if (!fcs)
38 return 0;
39 FcRefInit (&fcs->ref, 1);
40 fcs->num = 0;
41 fcs->leaves_offset = 0;
42 fcs->numbers_offset = 0;
43 return fcs;
44 }
45
46 FcCharSet *
47 FcCharSetPromote (FcValuePromotionBuffer *vbuf)
48 {
49 FcCharSet *fcs = (FcCharSet *)vbuf;
50
51 FC_ASSERT_STATIC (sizeof (FcCharSet) <= sizeof (FcValuePromotionBuffer));
52
53 FcRefSetConst (&fcs->ref);
54 fcs->num = 0;
55 fcs->leaves_offset = 0;
56 fcs->numbers_offset = 0;
57
58 return fcs;
59 }
60
61 FcCharSet *
62 FcCharSetNew (void)
63 {
64 return FcCharSetCreate();
65 }
66
67 void
68 FcCharSetDestroy (FcCharSet *fcs)
69 {
70 int i;
71
72 if (fcs) {
73 if (FcRefIsConst (&fcs->ref)) {
74 FcCacheObjectDereference (fcs);
75 return;
76 }
77 if (FcRefDec (&fcs->ref) != 1)
78 return;
79 for (i = 0; i < fcs->num; i++)
80 free (FcCharSetLeaf (fcs, i));
81 if (fcs->num) {
82 free (FcCharSetLeaves (fcs));
83 free (FcCharSetNumbers (fcs));
84 }
85 free (fcs);
86 }
87 }
88
89 /*
90 * Search for the leaf containing with the specified num.
91 * Return its index if it exists, otherwise return negative of
92 * the (position + 1) where it should be inserted
93 */
94
95 static int
96 FcCharSetFindLeafForward (const FcCharSet *fcs, int start, FcChar16 num)
97 {
98 FcChar16 *numbers = FcCharSetNumbers (fcs);
99 FcChar16 page;
100 int low = start;
101 int high = fcs->num - 1;
102
103 if (!numbers)
104 return -1;
105 while (low <= high) {
106 int mid = (low + high) >> 1;
107 page = numbers[mid];
108 if (page == num)
109 return mid;
110 if (page < num)
111 low = mid + 1;
112 else
113 high = mid - 1;
114 }
115 if (high < 0 || (high < fcs->num && numbers[high] < num))
116 high++;
117 return -(high + 1);
118 }
119
120 /*
121 * Locate the leaf containing the specified char, return
122 * its index if it exists, otherwise return negative of
123 * the (position + 1) where it should be inserted
124 */
125
126 static int
127 FcCharSetFindLeafPos (const FcCharSet *fcs, FcChar32 ucs4)
128 {
129 return FcCharSetFindLeafForward (fcs, 0, ucs4 >> 8);
130 }
131
132 static FcCharLeaf *
133 FcCharSetFindLeaf (const FcCharSet *fcs, FcChar32 ucs4)
134 {
135 int pos = FcCharSetFindLeafPos (fcs, ucs4);
136 if (pos >= 0)
137 return FcCharSetLeaf (fcs, pos);
138 return 0;
139 }
140
141 #define FC_IS_ZERO_OR_POWER_OF_TWO(x) (!((x) & ((x) - 1)))
142
143 static FcBool
144 FcCharSetPutLeaf (FcCharSet *fcs,
145 FcChar32 ucs4,
146 FcCharLeaf *leaf,
147 int pos)
148 {
149 intptr_t *leaves = FcCharSetLeaves (fcs);
150 FcChar16 *numbers = FcCharSetNumbers (fcs);
151
152 ucs4 >>= 8;
153 if (ucs4 >= 0x10000)
154 return FcFalse;
155
156 if (FC_IS_ZERO_OR_POWER_OF_TWO (fcs->num)) {
157 if (!fcs->num) {
158 unsigned int alloced = 8;
159 leaves = malloc (alloced * sizeof (*leaves));
160 numbers = malloc (alloced * sizeof (*numbers));
161 if (!leaves || !numbers) {
162 if (leaves)
163 free (leaves);
164 if (numbers)
165 free (numbers);
166 return FcFalse;
167 }
168 } else {
169 int i;
170 unsigned int alloced = fcs->num;
171 intptr_t *new_leaves;
172
173 alloced *= 2;
174 numbers = realloc (numbers, alloced * sizeof (*numbers));
175 if (!numbers)
176 return FcFalse;
177 new_leaves = realloc (leaves, alloced * sizeof (*leaves));
178 if (!new_leaves) {
179 /*
180 * Revert the reallocation of numbers. We update numbers_offset
181 * first in case realloc() fails.
182 */
183 fcs->numbers_offset = FcPtrToOffset (fcs, numbers);
184 numbers = realloc (numbers, (alloced / 2) * sizeof (*numbers));
185 /* unlikely to fail though */
186 if (!numbers)
187 return FcFalse;
188 fcs->numbers_offset = FcPtrToOffset (fcs, numbers);
189 return FcFalse;
190 }
191 for (i = 0; i < fcs->num; i++) {
192 // Reconstruct FcCharLeaf* from offset, similar to how FcCharSetLeaf() macro operates
193 FcCharLeaf *leaf = FcOffsetToPtr (leaves, new_leaves[i], FcCharLeaf);
194 new_leaves[i] = FcPtrToOffset (new_leaves, leaf);
195 }
196 leaves = new_leaves;
197 }
198
199 fcs->leaves_offset = FcPtrToOffset (fcs, leaves);
200 fcs->numbers_offset = FcPtrToOffset (fcs, numbers);
201 }
202
203 memmove (leaves + pos + 1, leaves + pos,
204 (fcs->num - pos) * sizeof (*leaves));
205 memmove (numbers + pos + 1, numbers + pos,
206 (fcs->num - pos) * sizeof (*numbers));
207 numbers[pos] = (FcChar16)ucs4;
208 leaves[pos] = FcPtrToOffset (leaves, leaf);
209 fcs->num++;
210 return FcTrue;
211 }
212
213 /*
214 * Locate the leaf containing the specified char, creating it
215 * if desired
216 */
217
218 FcCharLeaf *
219 FcCharSetFindLeafCreate (FcCharSet *fcs, FcChar32 ucs4)
220 {
221 int pos;
222 FcCharLeaf *leaf;
223
224 pos = FcCharSetFindLeafPos (fcs, ucs4);
225 if (pos >= 0)
226 return FcCharSetLeaf (fcs, pos);
227
228 leaf = calloc (1, sizeof (FcCharLeaf));
229 if (!leaf)
230 return 0;
231
232 pos = -pos - 1;
233 if (!FcCharSetPutLeaf (fcs, ucs4, leaf, pos)) {
234 free (leaf);
235 return 0;
236 }
237 return leaf;
238 }
239
240 static FcBool
241 FcCharSetInsertLeaf (FcCharSet *fcs, FcChar32 ucs4, FcCharLeaf *leaf)
242 {
243 int pos;
244
245 pos = FcCharSetFindLeafPos (fcs, ucs4);
246 if (pos >= 0) {
247 free (FcCharSetLeaf (fcs, pos));
248 FcCharSetLeaves (fcs)[pos] = FcPtrToOffset (FcCharSetLeaves (fcs),
249 leaf);
250 return FcTrue;
251 }
252 pos = -pos - 1;
253 return FcCharSetPutLeaf (fcs, ucs4, leaf, pos);
254 }
255
256 FcBool
257 FcCharSetAddChar (FcCharSet *fcs, FcChar32 ucs4)
258 {
259 FcCharLeaf *leaf;
260 FcChar32 *b;
261
262 if (fcs == NULL || FcRefIsConst (&fcs->ref))
263 return FcFalse;
264 leaf = FcCharSetFindLeafCreate (fcs, ucs4);
265 if (!leaf)
266 return FcFalse;
267 b = &leaf->map[(ucs4 & 0xff) >> 5];
268 *b |= (1U << (ucs4 & 0x1f));
269 return FcTrue;
270 }
271
272 FcBool
273 FcCharSetDelChar (FcCharSet *fcs, FcChar32 ucs4)
274 {
275 FcCharLeaf *leaf;
276 FcChar32 *b;
277
278 if (fcs == NULL || FcRefIsConst (&fcs->ref))
279 return FcFalse;
280 leaf = FcCharSetFindLeaf (fcs, ucs4);
281 if (!leaf)
282 return FcTrue;
283 b = &leaf->map[(ucs4 & 0xff) >> 5];
284 *b &= ~(1U << (ucs4 & 0x1f));
285 /* We don't bother removing the leaf if it's empty */
286 return FcTrue;
287 }
288
289 /*
290 * An iterator for the leaves of a charset
291 */
292
293 typedef struct _fcCharSetIter {
294 FcCharLeaf *leaf;
295 FcChar32 ucs4;
296 int pos;
297 } FcCharSetIter;
298
299 /*
300 * Set iter->leaf to the leaf containing iter->ucs4 or higher
301 */
302
303 static void
304 FcCharSetIterSet (const FcCharSet *fcs, FcCharSetIter *iter)
305 {
306 int pos = FcCharSetFindLeafPos (fcs, iter->ucs4);
307
308 if (pos < 0) {
309 pos = -pos - 1;
310 if (pos == fcs->num) {
311 iter->ucs4 = ~0;
312 iter->leaf = 0;
313 return;
314 }
315 iter->ucs4 = (FcChar32)FcCharSetNumbers (fcs)[pos] << 8;
316 }
317 iter->leaf = FcCharSetLeaf (fcs, pos);
318 iter->pos = pos;
319 }
320
321 static void
322 FcCharSetIterNext (const FcCharSet *fcs, FcCharSetIter *iter)
323 {
324 int pos = iter->pos + 1;
325 if (pos >= fcs->num) {
326 iter->ucs4 = ~0;
327 iter->leaf = 0;
328 } else {
329 iter->ucs4 = (FcChar32)FcCharSetNumbers (fcs)[pos] << 8;
330 iter->leaf = FcCharSetLeaf (fcs, pos);
331 iter->pos = pos;
332 }
333 }
334
335 static void
336 FcCharSetIterStart (const FcCharSet *fcs, FcCharSetIter *iter)
337 {
338 iter->ucs4 = 0;
339 iter->pos = 0;
340 FcCharSetIterSet (fcs, iter);
341 }
342
343 FcCharSet *
344 FcCharSetCopy (FcCharSet *src)
345 {
346 if (src) {
347 if (!FcRefIsConst (&src->ref))
348 FcRefInc (&src->ref);
349 else
350 FcCacheObjectReference (src);
351 }
352 return src;
353 }
354
355 FcBool
356 FcCharSetEqual (const FcCharSet *a, const FcCharSet *b)
357 {
358 FcCharSetIter ai, bi;
359 int i;
360
361 if (a == b)
362 return FcTrue;
363 if (!a || !b)
364 return FcFalse;
365 for (FcCharSetIterStart (a, &ai), FcCharSetIterStart (b, &bi);
366 ai.leaf && bi.leaf;
367 FcCharSetIterNext (a, &ai), FcCharSetIterNext (b, &bi)) {
368 if (ai.ucs4 != bi.ucs4)
369 return FcFalse;
370 for (i = 0; i < 256 / 32; i++)
371 if (ai.leaf->map[i] != bi.leaf->map[i])
372 return FcFalse;
373 }
374 return ai.leaf == bi.leaf;
375 }
376
377 static FcBool
378 FcCharSetAddLeaf (FcCharSet *fcs,
379 FcChar32 ucs4,
380 FcCharLeaf *leaf)
381 {
382 FcCharLeaf *newp = FcCharSetFindLeafCreate (fcs, ucs4);
383 if (!newp)
384 return FcFalse;
385 *newp = *leaf;
386 return FcTrue;
387 }
388
389 static FcCharSet *
390 FcCharSetOperate (const FcCharSet *a,
391 const FcCharSet *b,
392 FcBool (*overlap) (FcCharLeaf *result,
393 const FcCharLeaf *al,
394 const FcCharLeaf *bl),
395 FcBool aonly,
396 FcBool bonly)
397 {
398 FcCharSet *fcs;
399 FcCharSetIter ai, bi;
400
401 if (!a || !b)
402 goto bail0;
403 fcs = FcCharSetCreate();
404 if (!fcs)
405 goto bail0;
406 FcCharSetIterStart (a, &ai);
407 FcCharSetIterStart (b, &bi);
408 while ((ai.leaf || (bonly && bi.leaf)) && (bi.leaf || (aonly && ai.leaf))) {
409 if (ai.ucs4 < bi.ucs4) {
410 if (aonly) {
411 if (!FcCharSetAddLeaf (fcs, ai.ucs4, ai.leaf))
412 goto bail1;
413 FcCharSetIterNext (a, &ai);
414 } else {
415 ai.ucs4 = bi.ucs4;
416 FcCharSetIterSet (a, &ai);
417 }
418 } else if (bi.ucs4 < ai.ucs4) {
419 if (bonly) {
420 if (!FcCharSetAddLeaf (fcs, bi.ucs4, bi.leaf))
421 goto bail1;
422 FcCharSetIterNext (b, &bi);
423 } else {
424 bi.ucs4 = ai.ucs4;
425 FcCharSetIterSet (b, &bi);
426 }
427 } else {
428 FcCharLeaf leaf;
429
430 if ((*overlap) (&leaf, ai.leaf, bi.leaf)) {
431 if (!FcCharSetAddLeaf (fcs, ai.ucs4, &leaf))
432 goto bail1;
433 }
434 FcCharSetIterNext (a, &ai);
435 FcCharSetIterNext (b, &bi);
436 }
437 }
438 return fcs;
439 bail1:
440 FcCharSetDestroy (fcs);
441 bail0:
442 return 0;
443 }
444
445 static FcBool
446 FcCharSetIntersectLeaf (FcCharLeaf *result,
447 const FcCharLeaf *al,
448 const FcCharLeaf *bl)
449 {
450 int i;
451 FcBool nonempty = FcFalse;
452
453 for (i = 0; i < 256 / 32; i++)
454 if ((result->map[i] = al->map[i] & bl->map[i]))
455 nonempty = FcTrue;
456 return nonempty;
457 }
458
459 FcCharSet *
460 FcCharSetIntersect (const FcCharSet *a, const FcCharSet *b)
461 {
462 return FcCharSetOperate (a, b, FcCharSetIntersectLeaf, FcFalse, FcFalse);
463 }
464
465 static FcBool
466 FcCharSetUnionLeaf (FcCharLeaf *result,
467 const FcCharLeaf *al,
468 const FcCharLeaf *bl)
469 {
470 int i;
471
472 for (i = 0; i < 256 / 32; i++)
473 result->map[i] = al->map[i] | bl->map[i];
474 return FcTrue;
475 }
476
477 FcCharSet *
478 FcCharSetUnion (const FcCharSet *a, const FcCharSet *b)
479 {
480 return FcCharSetOperate (a, b, FcCharSetUnionLeaf, FcTrue, FcTrue);
481 }
482
483 FcBool
484 FcCharSetMerge (FcCharSet *a, const FcCharSet *b, FcBool *changed)
485 {
486 int ai = 0, bi = 0;
487 FcChar16 an, bn;
488
489 if (!a || !b)
490 return FcFalse;
491
492 if (FcRefIsConst (&a->ref)) {
493 if (changed)
494 *changed = FcFalse;
495 return FcFalse;
496 }
497
498 if (changed) {
499 *changed = !FcCharSetIsSubset (b, a);
500 if (!*changed)
501 return FcTrue;
502 }
503
504 while (bi < b->num) {
505 an = ai < a->num ? FcCharSetNumbers (a)[ai] : ~0;
506 bn = FcCharSetNumbers (b)[bi];
507
508 if (an < bn) {
509 ai = FcCharSetFindLeafForward (a, ai + 1, bn);
510 if (ai < 0)
511 ai = -ai - 1;
512 } else {
513 FcCharLeaf *bl = FcCharSetLeaf (b, bi);
514 if (bn < an) {
515 if (!FcCharSetAddLeaf (a, bn << 8, bl))
516 return FcFalse;
517 } else {
518 FcCharLeaf *al = FcCharSetLeaf (a, ai);
519 FcCharSetUnionLeaf (al, al, bl);
520 }
521
522 ai++;
523 bi++;
524 }
525 }
526
527 return FcTrue;
528 }
529
530 static FcBool
531 FcCharSetSubtractLeaf (FcCharLeaf *result,
532 const FcCharLeaf *al,
533 const FcCharLeaf *bl)
534 {
535 int i;
536 FcBool nonempty = FcFalse;
537
538 for (i = 0; i < 256 / 32; i++)
539 if ((result->map[i] = al->map[i] & ~bl->map[i]))
540 nonempty = FcTrue;
541 return nonempty;
542 }
543
544 FcCharSet *
545 FcCharSetSubtract (const FcCharSet *a, const FcCharSet *b)
546 {
547 return FcCharSetOperate (a, b, FcCharSetSubtractLeaf, FcTrue, FcFalse);
548 }
549
550 FcBool
551 FcCharSetHasChar (const FcCharSet *fcs, FcChar32 ucs4)
552 {
553 FcCharLeaf *leaf;
554
555 if (!fcs)
556 return FcFalse;
557 leaf = FcCharSetFindLeaf (fcs, ucs4);
558 if (!leaf)
559 return FcFalse;
560 return (leaf->map[(ucs4 & 0xff) >> 5] & (1U << (ucs4 & 0x1f))) != 0;
561 }
562
563 static FcChar32
564 FcCharSetPopCount (FcChar32 c1)
565 {
566 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
567 return __builtin_popcount (c1);
568 #else
569 /* hackmem 169 */
570 FcChar32 c2 = (c1 >> 1) & 033333333333;
571 c2 = c1 - c2 - ((c2 >> 1) & 033333333333);
572 return (((c2 + (c2 >> 3)) & 030707070707) % 077);
573 #endif
574 }
575
576 FcChar32
577 FcCharSetIntersectCount (const FcCharSet *a, const FcCharSet *b)
578 {
579 FcCharSetIter ai, bi;
580 FcChar32 count = 0;
581
582 if (a && b) {
583 FcCharSetIterStart (a, &ai);
584 FcCharSetIterStart (b, &bi);
585 while (ai.leaf && bi.leaf) {
586 if (ai.ucs4 == bi.ucs4) {
587 FcChar32 *am = ai.leaf->map;
588 FcChar32 *bm = bi.leaf->map;
589 int i = 256 / 32;
590 while (i--)
591 count += FcCharSetPopCount (*am++ & *bm++);
592 FcCharSetIterNext (a, &ai);
593 } else if (ai.ucs4 < bi.ucs4) {
594 ai.ucs4 = bi.ucs4;
595 FcCharSetIterSet (a, &ai);
596 }
597 if (bi.ucs4 < ai.ucs4) {
598 bi.ucs4 = ai.ucs4;
599 FcCharSetIterSet (b, &bi);
600 }
601 }
602 }
603 return count;
604 }
605
606 FcChar32
607 FcCharSetCount (const FcCharSet *a)
608 {
609 FcCharSetIter ai;
610 FcChar32 count = 0;
611
612 if (a) {
613 for (FcCharSetIterStart (a, &ai); ai.leaf; FcCharSetIterNext (a, &ai)) {
614 int i = 256 / 32;
615 FcChar32 *am = ai.leaf->map;
616
617 while (i--)
618 count += FcCharSetPopCount (*am++);
619 }
620 }
621 return count;
622 }
623
624 FcChar32
625 FcCharSetSubtractCount (const FcCharSet *a, const FcCharSet *b)
626 {
627 FcCharSetIter ai, bi;
628 FcChar32 count = 0;
629
630 if (a && b) {
631 FcCharSetIterStart (a, &ai);
632 FcCharSetIterStart (b, &bi);
633 while (ai.leaf) {
634 if (ai.ucs4 <= bi.ucs4) {
635 FcChar32 *am = ai.leaf->map;
636 int i = 256 / 32;
637 if (ai.ucs4 == bi.ucs4) {
638 FcChar32 *bm = bi.leaf->map;
639 while (i--)
640 count += FcCharSetPopCount (*am++ & ~*bm++);
641 } else {
642 while (i--)
643 count += FcCharSetPopCount (*am++);
644 }
645 FcCharSetIterNext (a, &ai);
646 } else if (bi.leaf) {
647 bi.ucs4 = ai.ucs4;
648 FcCharSetIterSet (b, &bi);
649 }
650 }
651 }
652 return count;
653 }
654
655 /*
656 * return FcTrue iff a is a subset of b
657 */
658 FcBool
659 FcCharSetIsSubset (const FcCharSet *a, const FcCharSet *b)
660 {
661 int ai, bi;
662 FcChar16 an, bn;
663
664 if (a == b)
665 return FcTrue;
666 if (!a || !b)
667 return FcFalse;
668 bi = 0;
669 ai = 0;
670 while (ai < a->num && bi < b->num) {
671 an = FcCharSetNumbers (a)[ai];
672 bn = FcCharSetNumbers (b)[bi];
673 /*
674 * Check matching pages
675 */
676 if (an == bn) {
677 FcChar32 *am = FcCharSetLeaf (a, ai)->map;
678 FcChar32 *bm = FcCharSetLeaf (b, bi)->map;
679
680 if (am != bm) {
681 int i = 256 / 32;
682 /*
683 * Does am have any bits not in bm?
684 */
685 while (i--)
686 if (*am++ & ~*bm++)
687 return FcFalse;
688 }
689 ai++;
690 bi++;
691 }
692 /*
693 * Does a have any pages not in b?
694 */
695 else if (an < bn)
696 return FcFalse;
697 else {
698 bi = FcCharSetFindLeafForward (b, bi + 1, an);
699 if (bi < 0)
700 bi = -bi - 1;
701 }
702 }
703 /*
704 * did we look at every page?
705 */
706 return ai >= a->num;
707 }
708
709 /*
710 * These two functions efficiently walk the entire charmap for
711 * other software (like pango) that want their own copy
712 */
713
714 FcChar32
715 FcCharSetNextPage (const FcCharSet *a,
716 FcChar32 map[FC_CHARSET_MAP_SIZE],
717 FcChar32 *next)
718 {
719 FcCharSetIter ai;
720 FcChar32 page;
721
722 if (!a)
723 return FC_CHARSET_DONE;
724 ai.ucs4 = *next;
725 FcCharSetIterSet (a, &ai);
726 if (!ai.leaf)
727 return FC_CHARSET_DONE;
728
729 /*
730 * Save current information
731 */
732 page = ai.ucs4;
733 memcpy (map, ai.leaf->map, sizeof (ai.leaf->map));
734 /*
735 * Step to next page
736 */
737 FcCharSetIterNext (a, &ai);
738 *next = ai.ucs4;
739
740 return page;
741 }
742
743 FcChar32
744 FcCharSetFirstPage (const FcCharSet *a,
745 FcChar32 map[FC_CHARSET_MAP_SIZE],
746 FcChar32 *next)
747 {
748 *next = 0;
749 return FcCharSetNextPage (a, map, next);
750 }
751
752 /*
753 * old coverage API, rather hard to use correctly
754 */
755
756 FcChar32
757 FcCharSetCoverage (const FcCharSet *a, FcChar32 page, FcChar32 *result)
758 {
759 FcCharSetIter ai;
760
761 ai.ucs4 = page;
762 FcCharSetIterSet (a, &ai);
763 if (!ai.leaf) {
764 memset (result, '\0', 256 / 8);
765 page = 0;
766 } else {
767 memcpy (result, ai.leaf->map, sizeof (ai.leaf->map));
768 FcCharSetIterNext (a, &ai);
769 page = ai.ucs4;
770 }
771 return page;
772 }
773
774 static FcBool
775 FcNameParseRange (FcChar8 **string, FcChar32 *pfirst, FcChar32 *plast)
776 {
777 char *s = (char *)*string;
778 char *t;
779 long first, last;
780
781 while (isspace ((unsigned char)*s))
782 s++;
783 t = s;
784 errno = 0;
785 first = last = strtol (s, &s, 16);
786 if (errno)
787 return FcFalse;
788 while (isspace ((unsigned char)*s))
789 s++;
790 if (*s == '-') {
791 s++;
792 errno = 0;
793 last = strtol (s, &s, 16);
794 if (errno)
795 return FcFalse;
796 }
797
798 if (s == t || first < 0 || last < 0 || last < first || last > 0x10ffff)
799 return FcFalse;
800
801 *string = (FcChar8 *)s;
802 *pfirst = first;
803 *plast = last;
804 return FcTrue;
805 }
806
807 FcCharSet *
808 FcNameParseCharSet (FcChar8 *string)
809 {
810 FcCharSet *c;
811 FcChar32 first, last;
812
813 c = FcCharSetCreate();
814 if (!c)
815 goto bail0;
816 while (*string) {
817 FcChar32 u;
818
819 if (!FcNameParseRange (&string, &first, &last))
820 goto bail1;
821
822 for (u = first; u < last + 1; u++)
823 FcCharSetAddChar (c, u);
824 }
825 return c;
826 bail1:
827 FcCharSetDestroy (c);
828 bail0:
829 return NULL;
830 }
831
832 static void
833 FcNameUnparseUnicode (FcStrBuf *buf, FcChar32 u)
834 {
835 FcChar8 buf_static[64];
836 snprintf ((char *)buf_static, sizeof (buf_static), "%x", u);
837 FcStrBufString (buf, buf_static);
838 }
839
840 FcBool
841 FcNameUnparseCharSet (FcStrBuf *buf, const FcCharSet *c)
842 {
843 FcCharSetIter ci;
844 FcChar32 first, last;
845 int i;
846 #ifdef CHECK
847 int len = buf->len;
848 #endif
849
850 first = last = 0x7FFFFFFF;
851
852 for (FcCharSetIterStart (c, &ci);
853 ci.leaf;
854 FcCharSetIterNext (c, &ci)) {
855 for (i = 0; i < 256 / 32; i++) {
856 FcChar32 bits = ci.leaf->map[i];
857 FcChar32 u = ci.ucs4 + i * 32;
858
859 while (bits) {
860 if (bits & 1) {
861 if (u != last + 1) {
862 if (last != first) {
863 FcStrBufChar (buf, '-');
864 FcNameUnparseUnicode (buf, last);
865 }
866 if (last != 0x7FFFFFFF)
867 FcStrBufChar (buf, ' ');
868 /* Start new range. */
869 first = u;
870 FcNameUnparseUnicode (buf, u);
871 }
872 last = u;
873 }
874 bits >>= 1;
875 u++;
876 }
877 }
878 }
879 if (last != first) {
880 FcStrBufChar (buf, '-');
881 FcNameUnparseUnicode (buf, last);
882 }
883 #ifdef CHECK
884 {
885 FcCharSet *check;
886 FcChar32 missing;
887 FcCharSetIter ci, checki;
888
889 /* null terminate for parser */
890 FcStrBufChar (buf, '\0');
891 /* step back over null for life after test */
892 buf->len--;
893 check = FcNameParseCharSet (buf->buf + len);
894 FcCharSetIterStart (c, &ci);
895 FcCharSetIterStart (check, &checki);
896 while (ci.leaf || checki.leaf) {
897 if (ci.ucs4 < checki.ucs4) {
898 printf ("Missing leaf node at 0x%x\n", ci.ucs4);
899 FcCharSetIterNext (c, &ci);
900 } else if (checki.ucs4 < ci.ucs4) {
901 printf ("Extra leaf node at 0x%x\n", checki.ucs4);
902 FcCharSetIterNext (check, &checki);
903 } else {
904 int i = 256 / 32;
905 FcChar32 *cm = ci.leaf->map;
906 FcChar32 *checkm = checki.leaf->map;
907
908 for (i = 0; i < 256; i += 32) {
909 if (*cm != *checkm)
910 printf ("Mismatching sets at 0x%08x: 0x%08x != 0x%08x\n",
911 ci.ucs4 + i, *cm, *checkm);
912 cm++;
913 checkm++;
914 }
915 FcCharSetIterNext (c, &ci);
916 FcCharSetIterNext (check, &checki);
917 }
918 }
919 if ((missing = FcCharSetSubtractCount (c, check)))
920 printf ("%d missing in reparsed result\n", missing);
921 if ((missing = FcCharSetSubtractCount (check, c)))
922 printf ("%d extra in reparsed result\n", missing);
923 FcCharSetDestroy (check);
924 }
925 #endif
926
927 return FcTrue;
928 }
929
930 typedef struct _FcCharLeafEnt FcCharLeafEnt;
931
932 struct _FcCharLeafEnt {
933 FcCharLeafEnt *next;
934 FcChar32 hash;
935 FcCharLeaf leaf;
936 };
937
938 #define FC_CHAR_LEAF_BLOCK (4096 / sizeof (FcCharLeafEnt))
939 #define FC_CHAR_LEAF_HASH_SIZE 257
940
941 typedef struct _FcCharSetEnt FcCharSetEnt;
942
943 struct _FcCharSetEnt {
944 FcCharSetEnt *next;
945 FcChar32 hash;
946 FcCharSet set;
947 };
948
949 typedef struct _FcCharSetOrigEnt FcCharSetOrigEnt;
950
951 struct _FcCharSetOrigEnt {
952 FcCharSetOrigEnt *next;
953 const FcCharSet *orig;
954 const FcCharSet *frozen;
955 };
956
957 #define FC_CHAR_SET_HASH_SIZE 67
958
959 struct _FcCharSetFreezer {
960 FcCharLeafEnt *leaf_hash_table[FC_CHAR_LEAF_HASH_SIZE];
961 FcCharLeafEnt **leaf_blocks;
962 int leaf_block_count;
963 FcCharSetEnt *set_hash_table[FC_CHAR_SET_HASH_SIZE];
964 FcCharSetOrigEnt *orig_hash_table[FC_CHAR_SET_HASH_SIZE];
965 FcCharLeafEnt *current_block;
966 int leaf_remain;
967 int leaves_seen;
968 int charsets_seen;
969 int leaves_allocated;
970 int charsets_allocated;
971 };
972
973 static FcCharLeafEnt *
974 FcCharLeafEntCreate (FcCharSetFreezer *freezer)
975 {
976 if (!freezer->leaf_remain) {
977 FcCharLeafEnt **newBlocks;
978
979 freezer->leaf_block_count++;
980 newBlocks = realloc (freezer->leaf_blocks, freezer->leaf_block_count * sizeof (FcCharLeafEnt *));
981 if (!newBlocks)
982 return 0;
983 freezer->leaf_blocks = newBlocks;
984 freezer->current_block = freezer->leaf_blocks[freezer->leaf_block_count - 1] = malloc (FC_CHAR_LEAF_BLOCK * sizeof (FcCharLeafEnt));
985 if (!freezer->current_block)
986 return 0;
987 freezer->leaf_remain = FC_CHAR_LEAF_BLOCK;
988 }
989 freezer->leaf_remain--;
990 freezer->leaves_allocated++;
991 return freezer->current_block++;
992 }
993
994 static FcChar32
995 FcCharLeafHash (FcCharLeaf *leaf)
996 {
997 FcChar32 hash = 0;
998 int i;
999
1000 for (i = 0; i < 256 / 32; i++)
1001 hash = ((hash << 1) | (hash >> 31)) ^ leaf->map[i];
1002 return hash;
1003 }
1004
1005 static FcCharLeaf *
1006 FcCharSetFreezeLeaf (FcCharSetFreezer *freezer, FcCharLeaf *leaf)
1007 {
1008 FcChar32 hash = FcCharLeafHash (leaf);
1009 FcCharLeafEnt **bucket = &freezer->leaf_hash_table[hash % FC_CHAR_LEAF_HASH_SIZE];
1010 FcCharLeafEnt *ent;
1011
1012 for (ent = *bucket; ent; ent = ent->next) {
1013 if (ent->hash == hash && !memcmp (&ent->leaf, leaf, sizeof (FcCharLeaf)))
1014 return &ent->leaf;
1015 }
1016
1017 ent = FcCharLeafEntCreate (freezer);
1018 if (!ent)
1019 return 0;
1020 ent->leaf = *leaf;
1021 ent->hash = hash;
1022 ent->next = *bucket;
1023 *bucket = ent;
1024 return &ent->leaf;
1025 }
1026
1027 static FcChar32
1028 FcCharSetHash (FcCharSet *fcs)
1029 {
1030 FcChar32 hash = 0;
1031 int i;
1032
1033 /* hash in leaves */
1034 for (i = 0; i < fcs->num; i++)
1035 hash = ((hash << 1) | (hash >> 31)) ^ FcCharLeafHash (FcCharSetLeaf (fcs, i));
1036 /* hash in numbers */
1037 for (i = 0; i < fcs->num; i++)
1038 hash = ((hash << 1) | (hash >> 31)) ^ FcCharSetNumbers (fcs)[i];
1039 return hash;
1040 }
1041
1042 static FcBool
1043 FcCharSetFreezeOrig (FcCharSetFreezer *freezer, const FcCharSet *orig, const FcCharSet *frozen)
1044 {
1045 FcCharSetOrigEnt **bucket = &freezer->orig_hash_table[((uintptr_t)orig) % FC_CHAR_SET_HASH_SIZE];
1046 FcCharSetOrigEnt *ent;
1047
1048 ent = malloc (sizeof (FcCharSetOrigEnt));
1049 if (!ent)
1050 return FcFalse;
1051 ent->orig = orig;
1052 ent->frozen = frozen;
1053 ent->next = *bucket;
1054 *bucket = ent;
1055 return FcTrue;
1056 }
1057
1058 static FcCharSet *
1059 FcCharSetFreezeBase (FcCharSetFreezer *freezer, FcCharSet *fcs)
1060 {
1061 FcChar32 hash = FcCharSetHash (fcs);
1062 FcCharSetEnt **bucket = &freezer->set_hash_table[hash % FC_CHAR_SET_HASH_SIZE];
1063 FcCharSetEnt *ent;
1064 int size;
1065 int i;
1066
1067 for (ent = *bucket; ent; ent = ent->next) {
1068 if (ent->hash == hash &&
1069 ent->set.num == fcs->num &&
1070 !memcmp (FcCharSetNumbers (&ent->set),
1071 FcCharSetNumbers (fcs),
1072 fcs->num * sizeof (FcChar16))) {
1073 FcBool ok = FcTrue;
1074 int i;
1075
1076 for (i = 0; i < fcs->num; i++)
1077 if (FcCharSetLeaf (&ent->set, i) != FcCharSetLeaf (fcs, i))
1078 ok = FcFalse;
1079 if (ok)
1080 return &ent->set;
1081 }
1082 }
1083
1084 size = (sizeof (FcCharSetEnt) +
1085 fcs->num * sizeof (FcCharLeaf *) +
1086 fcs->num * sizeof (FcChar16));
1087 ent = malloc (size);
1088 if (!ent)
1089 return 0;
1090
1091 freezer->charsets_allocated++;
1092
1093 FcRefSetConst (&ent->set.ref);
1094 ent->set.num = fcs->num;
1095 if (fcs->num) {
1096 intptr_t *ent_leaves;
1097
1098 ent->set.leaves_offset = sizeof (ent->set);
1099 ent->set.numbers_offset = (ent->set.leaves_offset +
1100 fcs->num * sizeof (intptr_t));
1101
1102 ent_leaves = FcCharSetLeaves (&ent->set);
1103 for (i = 0; i < fcs->num; i++)
1104 ent_leaves[i] = FcPtrToOffset (ent_leaves,
1105 FcCharSetLeaf (fcs, i));
1106 memcpy (FcCharSetNumbers (&ent->set),
1107 FcCharSetNumbers (fcs),
1108 fcs->num * sizeof (FcChar16));
1109 } else {
1110 ent->set.leaves_offset = 0;
1111 ent->set.numbers_offset = 0;
1112 }
1113
1114 ent->hash = hash;
1115 ent->next = *bucket;
1116 *bucket = ent;
1117
1118 return &ent->set;
1119 }
1120
1121 static const FcCharSet *
1122 FcCharSetFindFrozen (FcCharSetFreezer *freezer, const FcCharSet *orig)
1123 {
1124 FcCharSetOrigEnt **bucket = &freezer->orig_hash_table[((uintptr_t)orig) % FC_CHAR_SET_HASH_SIZE];
1125 FcCharSetOrigEnt *ent;
1126
1127 for (ent = *bucket; ent; ent = ent->next)
1128 if (ent->orig == orig)
1129 return ent->frozen;
1130 return NULL;
1131 }
1132
1133 const FcCharSet *
1134 FcCharSetFreeze (FcCharSetFreezer *freezer, const FcCharSet *fcs)
1135 {
1136 FcCharSet *b;
1137 const FcCharSet *n = 0;
1138 FcCharLeaf *l;
1139 int i;
1140
1141 b = FcCharSetCreate();
1142 if (!b)
1143 goto bail0;
1144 for (i = 0; i < fcs->num; i++) {
1145 l = FcCharSetFreezeLeaf (freezer, FcCharSetLeaf (fcs, i));
1146 if (!l)
1147 goto bail1;
1148 if (!FcCharSetInsertLeaf (b, FcCharSetNumbers (fcs)[i] << 8, l))
1149 goto bail1;
1150 }
1151 n = FcCharSetFreezeBase (freezer, b);
1152 if (!FcCharSetFreezeOrig (freezer, fcs, n)) {
1153 n = NULL;
1154 goto bail1;
1155 }
1156 freezer->charsets_seen++;
1157 freezer->leaves_seen += fcs->num;
1158 bail1:
1159 if (b->num)
1160 free (FcCharSetLeaves (b));
1161 if (b->num)
1162 free (FcCharSetNumbers (b));
1163 free (b);
1164 bail0:
1165 return n;
1166 }
1167
1168 FcCharSetFreezer *
1169 FcCharSetFreezerCreate (void)
1170 {
1171 FcCharSetFreezer *freezer;
1172
1173 freezer = calloc (1, sizeof (FcCharSetFreezer));
1174 return freezer;
1175 }
1176
1177 void
1178 FcCharSetFreezerDestroy (FcCharSetFreezer *freezer)
1179 {
1180 int i;
1181
1182 if (FcDebug() & FC_DBG_CACHE) {
1183 printf ("\ncharsets %d -> %d leaves %d -> %d\n",
1184 freezer->charsets_seen, freezer->charsets_allocated,
1185 freezer->leaves_seen, freezer->leaves_allocated);
1186 }
1187 for (i = 0; i < FC_CHAR_SET_HASH_SIZE; i++) {
1188 FcCharSetEnt *ent, *next;
1189 for (ent = freezer->set_hash_table[i]; ent; ent = next) {
1190 next = ent->next;
1191 free (ent);
1192 }
1193 }
1194
1195 for (i = 0; i < FC_CHAR_SET_HASH_SIZE; i++) {
1196 FcCharSetOrigEnt *ent, *next;
1197 for (ent = freezer->orig_hash_table[i]; ent; ent = next) {
1198 next = ent->next;
1199 free (ent);
1200 }
1201 }
1202
1203 for (i = 0; i < freezer->leaf_block_count; i++)
1204 free (freezer->leaf_blocks[i]);
1205
1206 free (freezer->leaf_blocks);
1207 free (freezer);
1208 }
1209
1210 FcBool
1211 FcCharSetSerializeAlloc (FcSerialize *serialize, const FcCharSet *cs)
1212 {
1213 intptr_t *leaves;
1214 FcChar16 *numbers;
1215 int i;
1216
1217 if (!FcRefIsConst (&cs->ref)) {
1218 if (!serialize->cs_freezer) {
1219 serialize->cs_freezer = FcCharSetFreezerCreate();
1220 if (!serialize->cs_freezer)
1221 return FcFalse;
1222 }
1223 if (FcCharSetFindFrozen (serialize->cs_freezer, cs))
1224 return FcTrue;
1225
1226 cs = FcCharSetFreeze (serialize->cs_freezer, cs);
1227 }
1228
1229 leaves = FcCharSetLeaves (cs);
1230 numbers = FcCharSetNumbers (cs);
1231
1232 if (!FcSerializeAlloc (serialize, cs, sizeof (FcCharSet)))
1233 return FcFalse;
1234 if (!FcSerializeAlloc (serialize, leaves, cs->num * sizeof (intptr_t)))
1235 return FcFalse;
1236 if (!FcSerializeAlloc (serialize, numbers, cs->num * sizeof (FcChar16)))
1237 return FcFalse;
1238 for (i = 0; i < cs->num; i++)
1239 if (!FcSerializeAlloc (serialize, FcCharSetLeaf (cs, i),
1240 sizeof (FcCharLeaf)))
1241 return FcFalse;
1242 return FcTrue;
1243 }
1244
1245 FcCharSet *
1246 FcCharSetSerialize (FcSerialize *serialize, const FcCharSet *cs)
1247 {
1248 FcCharSet *cs_serialized;
1249 intptr_t *leaves, *leaves_serialized;
1250 FcChar16 *numbers, *numbers_serialized;
1251 FcCharLeaf *leaf, *leaf_serialized;
1252 int i;
1253
1254 if (!FcRefIsConst (&cs->ref) && serialize->cs_freezer) {
1255 cs = FcCharSetFindFrozen (serialize->cs_freezer, cs);
1256 if (!cs)
1257 return NULL;
1258 }
1259
1260 cs_serialized = FcSerializePtr (serialize, cs);
1261 if (!cs_serialized)
1262 return NULL;
1263
1264 FcRefSetConst (&cs_serialized->ref);
1265 cs_serialized->num = cs->num;
1266
1267 if (cs->num) {
1268 leaves = FcCharSetLeaves (cs);
1269 leaves_serialized = FcSerializePtr (serialize, leaves);
1270 if (!leaves_serialized)
1271 return NULL;
1272
1273 cs_serialized->leaves_offset = FcPtrToOffset (cs_serialized,
1274 leaves_serialized);
1275
1276 numbers = FcCharSetNumbers (cs);
1277 numbers_serialized = FcSerializePtr (serialize, numbers);
1278 if (!numbers)
1279 return NULL;
1280
1281 cs_serialized->numbers_offset = FcPtrToOffset (cs_serialized,
1282 numbers_serialized);
1283
1284 for (i = 0; i < cs->num; i++) {
1285 leaf = FcCharSetLeaf (cs, i);
1286 leaf_serialized = FcSerializePtr (serialize, leaf);
1287 if (!leaf_serialized)
1288 return NULL;
1289 *leaf_serialized = *leaf;
1290 leaves_serialized[i] = FcPtrToOffset (leaves_serialized,
1291 leaf_serialized);
1292 numbers_serialized[i] = numbers[i];
1293 }
1294 } else {
1295 cs_serialized->leaves_offset = 0;
1296 cs_serialized->numbers_offset = 0;
1297 }
1298
1299 return cs_serialized;
1300 }
1301 #define __fccharset__
1302 #include "fcaliastail.h"
1303 #undef __fccharset__