]> git.wh0rd.org - fontconfig.git/blob - src/fccharset.c
Build fclang.h before building library This required compiling the charset
[fontconfig.git] / src / fccharset.c
1 /*
2 * $XFree86: xc/lib/fontconfig/src/fccharset.c,v 1.18 2002/08/22 07:36:44 keithp Exp $
3 *
4 * Copyright © 2001 Keith Packard, member of The XFree86 Project, Inc.
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
30 /* #define CHATTY */
31
32 FcCharSet *
33 FcCharSetCreate (void)
34 {
35 FcCharSet *fcs;
36
37 fcs = (FcCharSet *) malloc (sizeof (FcCharSet));
38 if (!fcs)
39 return 0;
40 FcMemAlloc (FC_MEM_CHARSET, sizeof (FcCharSet));
41 fcs->ref = 1;
42 fcs->num = 0;
43 fcs->leaves = 0;
44 fcs->numbers = 0;
45 return fcs;
46 }
47
48 FcCharSet *
49 FcCharSetNew (void);
50
51 FcCharSet *
52 FcCharSetNew (void)
53 {
54 return FcCharSetCreate ();
55 }
56
57
58 void
59 FcCharSetDestroy (FcCharSet *fcs)
60 {
61 int i;
62 if (fcs->ref == FC_REF_CONSTANT)
63 return;
64 if (--fcs->ref > 0)
65 return;
66 for (i = 0; i < fcs->num; i++)
67 {
68 FcMemFree (FC_MEM_CHARLEAF, sizeof (FcCharLeaf));
69 free (fcs->leaves[i]);
70 }
71 if (fcs->leaves)
72 {
73 FcMemFree (FC_MEM_CHARSET, fcs->num * sizeof (FcCharLeaf *));
74 free (fcs->leaves);
75 }
76 if (fcs->numbers)
77 {
78 FcMemFree (FC_MEM_CHARSET, fcs->num * sizeof (FcChar16));
79 free (fcs->numbers);
80 }
81 FcMemFree (FC_MEM_CHARSET, sizeof (FcCharSet));
82 free (fcs);
83 }
84
85 /*
86 * Locate the leaf containing the specified char, return
87 * its index if it exists, otherwise return negative of
88 * the (position + 1) where it should be inserted
89 */
90
91 static int
92 FcCharSetFindLeafPos (const FcCharSet *fcs, FcChar32 ucs4)
93 {
94 FcChar16 *numbers = fcs->numbers;
95 FcChar16 page;
96 int low = 0;
97 int high = fcs->num - 1;
98
99 if (!numbers)
100 return -1;
101 ucs4 >>= 8;
102 while (low <= high)
103 {
104 int mid = (low + high) >> 1;
105 page = numbers[mid];
106 if (page == ucs4)
107 return mid;
108 if (page < ucs4)
109 low = mid + 1;
110 else
111 high = mid - 1;
112 }
113 if (high < 0 || (high < fcs->num && numbers[high] < ucs4))
114 high++;
115 return -(high + 1);
116 }
117
118 static FcCharLeaf *
119 FcCharSetFindLeaf (const FcCharSet *fcs, FcChar32 ucs4)
120 {
121 int pos = FcCharSetFindLeafPos (fcs, ucs4);
122 if (pos >= 0)
123 return fcs->leaves[pos];
124 return 0;
125 }
126
127 static FcBool
128 FcCharSetPutLeaf (FcCharSet *fcs,
129 FcChar32 ucs4,
130 FcCharLeaf *leaf,
131 int pos)
132 {
133 FcCharLeaf **leaves;
134 FcChar16 *numbers;
135
136 ucs4 >>= 8;
137 if (ucs4 >= 0x10000)
138 return FcFalse;
139 if (!fcs->leaves)
140 leaves = malloc (sizeof (FcCharLeaf *));
141 else
142 leaves = realloc (fcs->leaves, (fcs->num + 1) * sizeof (FcCharLeaf *));
143 if (!leaves)
144 return FcFalse;
145 if (fcs->num)
146 FcMemFree (FC_MEM_CHARSET, fcs->num * sizeof (FcCharLeaf *));
147 FcMemAlloc (FC_MEM_CHARSET, (fcs->num + 1) * sizeof (FcCharLeaf *));
148 fcs->leaves = leaves;
149 if (!fcs->numbers)
150 numbers = malloc (sizeof (FcChar16));
151 else
152 numbers = realloc (fcs->numbers, (fcs->num + 1) * sizeof (FcChar16));
153 if (!numbers)
154 return FcFalse;
155 if (fcs->num)
156 FcMemFree (FC_MEM_CHARSET, fcs->num * sizeof (FcChar16));
157 FcMemAlloc (FC_MEM_CHARSET, (fcs->num + 1) * sizeof (FcChar16));
158 fcs->numbers = numbers;
159
160 memmove (fcs->leaves + pos + 1, fcs->leaves + pos,
161 (fcs->num - pos) * sizeof (FcCharLeaf *));
162 memmove (fcs->numbers + pos + 1, fcs->numbers + pos,
163 (fcs->num - pos) * sizeof (FcChar16));
164 fcs->numbers[pos] = (FcChar16) ucs4;
165 fcs->leaves[pos] = leaf;
166 fcs->num++;
167 return FcTrue;
168 }
169
170 /*
171 * Locate the leaf containing the specified char, creating it
172 * if desired
173 */
174
175 FcCharLeaf *
176 FcCharSetFindLeafCreate (FcCharSet *fcs, FcChar32 ucs4)
177 {
178 int pos;
179 FcCharLeaf *leaf;
180
181 pos = FcCharSetFindLeafPos (fcs, ucs4);
182 if (pos >= 0)
183 return fcs->leaves[pos];
184
185 leaf = calloc (1, sizeof (FcCharLeaf));
186 if (!leaf)
187 return 0;
188
189 pos = -pos - 1;
190 if (!FcCharSetPutLeaf (fcs, ucs4, leaf, pos))
191 {
192 free (leaf);
193 return 0;
194 }
195 FcMemAlloc (FC_MEM_CHARLEAF, sizeof (FcCharLeaf));
196 return leaf;
197 }
198
199 static FcBool
200 FcCharSetInsertLeaf (FcCharSet *fcs, FcChar32 ucs4, FcCharLeaf *leaf)
201 {
202 int pos;
203
204 pos = FcCharSetFindLeafPos (fcs, ucs4);
205 if (pos >= 0)
206 {
207 FcMemFree (FC_MEM_CHARLEAF, sizeof (FcCharLeaf));
208 free (fcs->leaves[pos]);
209 fcs->leaves[pos] = leaf;
210 return FcTrue;
211 }
212 pos = -pos - 1;
213 return FcCharSetPutLeaf (fcs, ucs4, leaf, pos);
214 }
215
216 FcBool
217 FcCharSetAddChar (FcCharSet *fcs, FcChar32 ucs4)
218 {
219 FcCharLeaf *leaf;
220 FcChar32 *b;
221
222 if (fcs->ref == FC_REF_CONSTANT)
223 return FcFalse;
224 leaf = FcCharSetFindLeafCreate (fcs, ucs4);
225 if (!leaf)
226 return FcFalse;
227 b = &leaf->map[(ucs4 & 0xff) >> 5];
228 *b |= (1 << (ucs4 & 0x1f));
229 return FcTrue;
230 }
231
232 /*
233 * An iterator for the leaves of a charset
234 */
235
236 typedef struct _fcCharSetIter {
237 FcCharLeaf *leaf;
238 FcChar32 ucs4;
239 int pos;
240 } FcCharSetIter;
241
242 /*
243 * Set iter->leaf to the leaf containing iter->ucs4 or higher
244 */
245
246 static void
247 FcCharSetIterSet (const FcCharSet *fcs, FcCharSetIter *iter)
248 {
249 int pos = FcCharSetFindLeafPos (fcs, iter->ucs4);
250
251 if (pos < 0)
252 {
253 pos = -pos - 1;
254 if (pos == fcs->num)
255 {
256 iter->ucs4 = ~0;
257 iter->leaf = 0;
258 return;
259 }
260 iter->ucs4 = (FcChar32) fcs->numbers[pos] << 8;
261 }
262 iter->leaf = fcs->leaves[pos];
263 iter->pos = pos;
264 #ifdef CHATTY
265 printf ("set %08x: %08x\n", iter->ucs4, (FcChar32) iter->leaf);
266 #endif
267 }
268
269 static void
270 FcCharSetIterNext (const FcCharSet *fcs, FcCharSetIter *iter)
271 {
272 int pos = iter->pos + 1;
273 if (pos >= fcs->num)
274 {
275 iter->ucs4 = ~0;
276 iter->leaf = 0;
277 }
278 else
279 {
280 iter->ucs4 = (FcChar32) fcs->numbers[pos] << 8;
281 iter->leaf = fcs->leaves[pos];
282 iter->pos = pos;
283 }
284 }
285
286 #ifdef CHATTY
287 static void
288 FcCharSetDump (const FcCharSet *fcs)
289 {
290 int pos;
291
292 printf ("fcs %08x:\n", (FcChar32) fcs);
293 for (pos = 0; pos < fcs->num; pos++)
294 {
295 FcCharLeaf *leaf = fcs->leaves[pos];
296 FcChar32 ucs4 = (FcChar32) fcs->numbers[pos] << 8;
297
298 printf (" %08x: %08x\n", ucs4, (FcChar32) leaf);
299 }
300 }
301 #endif
302
303 static void
304 FcCharSetIterStart (const FcCharSet *fcs, FcCharSetIter *iter)
305 {
306 #ifdef CHATTY
307 FcCharSetDump (fcs);
308 #endif
309 iter->ucs4 = 0;
310 FcCharSetIterSet (fcs, iter);
311 }
312
313 FcCharSet *
314 FcCharSetCopy (FcCharSet *src)
315 {
316 if (src->ref != FC_REF_CONSTANT)
317 src->ref++;
318 return src;
319 }
320
321 FcBool
322 FcCharSetEqual (const FcCharSet *a, const FcCharSet *b)
323 {
324 FcCharSetIter ai, bi;
325 int i;
326
327 if (a == b)
328 return FcTrue;
329 for (FcCharSetIterStart (a, &ai), FcCharSetIterStart (b, &bi);
330 ai.leaf && bi.leaf;
331 FcCharSetIterNext (a, &ai), FcCharSetIterNext (b, &bi))
332 {
333 if (ai.ucs4 != bi.ucs4)
334 return FcFalse;
335 for (i = 0; i < 256/32; i++)
336 if (ai.leaf->map[i] != bi.leaf->map[i])
337 return FcFalse;
338 }
339 return ai.leaf == bi.leaf;
340 }
341
342 static FcBool
343 FcCharSetAddLeaf (FcCharSet *fcs,
344 FcChar32 ucs4,
345 FcCharLeaf *leaf)
346 {
347 FcCharLeaf *new = FcCharSetFindLeafCreate (fcs, ucs4);
348 if (!new)
349 return FcFalse;
350 *new = *leaf;
351 return FcTrue;
352 }
353
354 static FcCharSet *
355 FcCharSetOperate (const FcCharSet *a,
356 const FcCharSet *b,
357 FcBool (*overlap) (FcCharLeaf *result,
358 const FcCharLeaf *al,
359 const FcCharLeaf *bl),
360 FcBool aonly,
361 FcBool bonly)
362 {
363 FcCharSet *fcs;
364 FcCharSetIter ai, bi;
365
366 fcs = FcCharSetCreate ();
367 if (!fcs)
368 goto bail0;
369 FcCharSetIterStart (a, &ai);
370 FcCharSetIterStart (b, &bi);
371 while ((ai.leaf || (bonly && bi.leaf)) && (bi.leaf || (aonly && ai.leaf)))
372 {
373 if (ai.ucs4 < bi.ucs4)
374 {
375 if (aonly)
376 {
377 if (!FcCharSetAddLeaf (fcs, ai.ucs4, ai.leaf))
378 goto bail1;
379 FcCharSetIterNext (a, &ai);
380 }
381 else
382 {
383 ai.ucs4 = bi.ucs4;
384 FcCharSetIterSet (a, &ai);
385 }
386 }
387 else if (bi.ucs4 < ai.ucs4 )
388 {
389 if (bonly)
390 {
391 if (!FcCharSetAddLeaf (fcs, bi.ucs4, bi.leaf))
392 goto bail1;
393 FcCharSetIterNext (b, &bi);
394 }
395 else
396 {
397 bi.ucs4 = ai.ucs4;
398 FcCharSetIterSet (b, &bi);
399 }
400 }
401 else
402 {
403 FcCharLeaf leaf;
404
405 if ((*overlap) (&leaf, ai.leaf, bi.leaf))
406 {
407 if (!FcCharSetAddLeaf (fcs, ai.ucs4, &leaf))
408 goto bail1;
409 }
410 FcCharSetIterNext (a, &ai);
411 FcCharSetIterNext (b, &bi);
412 }
413 }
414 return fcs;
415 bail1:
416 FcCharSetDestroy (fcs);
417 bail0:
418 return 0;
419 }
420
421 static FcBool
422 FcCharSetIntersectLeaf (FcCharLeaf *result,
423 const FcCharLeaf *al,
424 const FcCharLeaf *bl)
425 {
426 int i;
427 FcBool nonempty = FcFalse;
428
429 for (i = 0; i < 256/32; i++)
430 if ((result->map[i] = al->map[i] & bl->map[i]))
431 nonempty = FcTrue;
432 return nonempty;
433 }
434
435 FcCharSet *
436 FcCharSetIntersect (const FcCharSet *a, const FcCharSet *b)
437 {
438 return FcCharSetOperate (a, b, FcCharSetIntersectLeaf, FcFalse, FcFalse);
439 }
440
441 static FcBool
442 FcCharSetUnionLeaf (FcCharLeaf *result,
443 const FcCharLeaf *al,
444 const FcCharLeaf *bl)
445 {
446 int i;
447
448 for (i = 0; i < 256/32; i++)
449 result->map[i] = al->map[i] | bl->map[i];
450 return FcTrue;
451 }
452
453 FcCharSet *
454 FcCharSetUnion (const FcCharSet *a, const FcCharSet *b)
455 {
456 return FcCharSetOperate (a, b, FcCharSetUnionLeaf, FcTrue, FcTrue);
457 }
458
459 static FcBool
460 FcCharSetSubtractLeaf (FcCharLeaf *result,
461 const FcCharLeaf *al,
462 const FcCharLeaf *bl)
463 {
464 int i;
465 FcBool nonempty = FcFalse;
466
467 for (i = 0; i < 256/32; i++)
468 if ((result->map[i] = al->map[i] & ~bl->map[i]))
469 nonempty = FcTrue;
470 return nonempty;
471 }
472
473 FcCharSet *
474 FcCharSetSubtract (const FcCharSet *a, const FcCharSet *b)
475 {
476 return FcCharSetOperate (a, b, FcCharSetSubtractLeaf, FcTrue, FcFalse);
477 }
478
479 FcBool
480 FcCharSetHasChar (const FcCharSet *fcs, FcChar32 ucs4)
481 {
482 FcCharLeaf *leaf = FcCharSetFindLeaf (fcs, ucs4);
483 if (!leaf)
484 return FcFalse;
485 return (leaf->map[(ucs4 & 0xff) >> 5] & (1 << (ucs4 & 0x1f))) != 0;
486 }
487
488 static FcChar32
489 FcCharSetPopCount (FcChar32 c1)
490 {
491 /* hackmem 169 */
492 FcChar32 c2 = (c1 >> 1) & 033333333333;
493 c2 = c1 - c2 - ((c2 >> 1) & 033333333333);
494 return (((c2 + (c2 >> 3)) & 030707070707) % 077);
495 }
496
497 FcChar32
498 FcCharSetIntersectCount (const FcCharSet *a, const FcCharSet *b)
499 {
500 FcCharSetIter ai, bi;
501 FcChar32 count = 0;
502
503 FcCharSetIterStart (a, &ai);
504 FcCharSetIterStart (b, &bi);
505 while (ai.leaf && bi.leaf)
506 {
507 if (ai.ucs4 == bi.ucs4)
508 {
509 FcChar32 *am = ai.leaf->map;
510 FcChar32 *bm = bi.leaf->map;
511 int i = 256/32;
512 while (i--)
513 count += FcCharSetPopCount (*am++ & *bm++);
514 FcCharSetIterNext (a, &ai);
515 }
516 else if (ai.ucs4 < bi.ucs4)
517 {
518 ai.ucs4 = bi.ucs4;
519 FcCharSetIterSet (a, &ai);
520 }
521 if (bi.ucs4 < ai.ucs4)
522 {
523 bi.ucs4 = ai.ucs4;
524 FcCharSetIterSet (b, &bi);
525 }
526 }
527 return count;
528 }
529
530 FcChar32
531 FcCharSetCount (const FcCharSet *a)
532 {
533 FcCharSetIter ai;
534 FcChar32 count = 0;
535
536 for (FcCharSetIterStart (a, &ai); ai.leaf; FcCharSetIterNext (a, &ai))
537 {
538 int i = 256/32;
539 FcChar32 *am = ai.leaf->map;
540
541 while (i--)
542 count += FcCharSetPopCount (*am++);
543 }
544 return count;
545 }
546
547 FcChar32
548 FcCharSetSubtractCount (const FcCharSet *a, const FcCharSet *b)
549 {
550 FcCharSetIter ai, bi;
551 FcChar32 count = 0;
552
553 FcCharSetIterStart (a, &ai);
554 FcCharSetIterStart (b, &bi);
555 while (ai.leaf)
556 {
557 if (ai.ucs4 <= bi.ucs4)
558 {
559 FcChar32 *am = ai.leaf->map;
560 int i = 256/32;
561 if (ai.ucs4 == bi.ucs4)
562 {
563 FcChar32 *bm = bi.leaf->map;;
564 while (i--)
565 count += FcCharSetPopCount (*am++ & ~*bm++);
566 }
567 else
568 {
569 while (i--)
570 count += FcCharSetPopCount (*am++);
571 }
572 FcCharSetIterNext (a, &ai);
573 }
574 else if (bi.leaf)
575 {
576 bi.ucs4 = ai.ucs4;
577 FcCharSetIterSet (b, &bi);
578 }
579 }
580 return count;
581 }
582
583 /*
584 * return FcTrue iff a is a subset of b
585 */
586 FcBool
587 FcCharSetIsSubset (const FcCharSet *a, const FcCharSet *b)
588 {
589 int ai, bi;
590 FcChar16 an, bn;
591
592 if (a == b) return FcTrue;
593 bi = 0;
594 ai = 0;
595 while (ai < a->num && bi < b->num)
596 {
597 an = a->numbers[ai];
598 bn = b->numbers[bi];
599 if (an == bn)
600 {
601 FcChar32 *am = a->leaves[ai]->map;
602 FcChar32 *bm = b->leaves[bi]->map;
603
604 if (am != bm)
605 {
606 int i = 256/32;
607 while (i--)
608 if (*am++ & ~*bm++)
609 return FcFalse;
610 }
611 ai++;
612 bi++;
613 }
614 else if (an < bn)
615 return FcFalse;
616 else
617 {
618 int low = bi + 1;
619 int high = b->num - 1;
620
621 while (low <= high)
622 {
623 int mid = (low + high) >> 1;
624 bn = b->numbers[mid];
625 if (bn == an)
626 {
627 high = mid;
628 break;
629 }
630 if (bn < an)
631 low = mid + 1;
632 else
633 high = mid - 1;
634 }
635 bi = high;
636 while (bi < b->num && b->numbers[bi] < an)
637 bi++;
638 }
639 }
640 return FcTrue;
641 }
642
643 /*
644 * These two functions efficiently walk the entire charmap for
645 * other software (like pango) that want their own copy
646 */
647
648 FcChar32
649 FcCharSetNextPage (const FcCharSet *a,
650 FcChar32 map[FC_CHARSET_MAP_SIZE],
651 FcChar32 *next)
652 {
653 FcCharSetIter ai;
654 FcChar32 page;
655
656 ai.ucs4 = *next;
657 FcCharSetIterSet (a, &ai);
658 if (!ai.leaf)
659 return FC_CHARSET_DONE;
660
661 /*
662 * Save current information
663 */
664 page = ai.ucs4;
665 memcpy (map, ai.leaf->map, sizeof (ai.leaf->map));
666 /*
667 * Step to next page
668 */
669 FcCharSetIterNext (a, &ai);
670 *next = ai.ucs4;
671
672 return page;
673 }
674
675 FcChar32
676 FcCharSetFirstPage (const FcCharSet *a,
677 FcChar32 map[FC_CHARSET_MAP_SIZE],
678 FcChar32 *next)
679 {
680 *next = 0;
681 return FcCharSetNextPage (a, map, next);
682 }
683
684 /*
685 * old coverage API, rather hard to use correctly
686 */
687 FcChar32
688 FcCharSetCoverage (const FcCharSet *a, FcChar32 page, FcChar32 *result);
689
690 FcChar32
691 FcCharSetCoverage (const FcCharSet *a, FcChar32 page, FcChar32 *result)
692 {
693 FcCharSetIter ai;
694
695 ai.ucs4 = page;
696 FcCharSetIterSet (a, &ai);
697 if (!ai.leaf)
698 {
699 memset (result, '\0', 256 / 8);
700 page = 0;
701 }
702 else
703 {
704 memcpy (result, ai.leaf->map, sizeof (ai.leaf->map));
705 FcCharSetIterNext (a, &ai);
706 page = ai.ucs4;
707 }
708 return page;
709 }
710
711 /*
712 * ASCII representation of charsets.
713 *
714 * Each leaf is represented as 9 32-bit values, the code of the first character followed
715 * by 8 32 bit values for the leaf itself. Each value is encoded as 5 ASCII characters,
716 * only 85 different values are used to avoid control characters as well as the other
717 * characters used to encode font names. 85**5 > 2^32 so things work out, but
718 * it's not exactly human readable output. As a special case, 0 is encoded as a space
719 */
720
721 static const unsigned char charToValue[256] = {
722 /* "" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
723 /* "\b" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
724 /* "\020" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
725 /* "\030" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
726 /* " " */ 0xff, 0x00, 0xff, 0x01, 0x02, 0x03, 0x04, 0xff,
727 /* "(" */ 0x05, 0x06, 0x07, 0x08, 0xff, 0xff, 0x09, 0x0a,
728 /* "0" */ 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12,
729 /* "8" */ 0x13, 0x14, 0xff, 0x15, 0x16, 0xff, 0x17, 0x18,
730 /* "@" */ 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
731 /* "H" */ 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
732 /* "P" */ 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30,
733 /* "X" */ 0x31, 0x32, 0x33, 0x34, 0xff, 0x35, 0x36, 0xff,
734 /* "`" */ 0xff, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d,
735 /* "h" */ 0x3e, 0x3f, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45,
736 /* "p" */ 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d,
737 /* "x" */ 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53, 0x54, 0xff,
738 /* "\200" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
739 /* "\210" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
740 /* "\220" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
741 /* "\230" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
742 /* "\240" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
743 /* "\250" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
744 /* "\260" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
745 /* "\270" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
746 /* "\300" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
747 /* "\310" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
748 /* "\320" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
749 /* "\330" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
750 /* "\340" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
751 /* "\350" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
752 /* "\360" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
753 /* "\370" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
754 };
755
756 static const FcChar8 valueToChar[0x55] = {
757 /* 0x00 */ '!', '#', '$', '%', '&', '(', ')', '*',
758 /* 0x08 */ '+', '.', '/', '0', '1', '2', '3', '4',
759 /* 0x10 */ '5', '6', '7', '8', '9', ';', '<', '>',
760 /* 0x18 */ '?', '@', 'A', 'B', 'C', 'D', 'E', 'F',
761 /* 0x20 */ 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
762 /* 0x28 */ 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
763 /* 0x30 */ 'W', 'X', 'Y', 'Z', '[', ']', '^', 'a',
764 /* 0x38 */ 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
765 /* 0x40 */ 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q',
766 /* 0x48 */ 'r', 's', 't', 'u', 'v', 'w', 'x', 'y',
767 /* 0x50 */ 'z', '{', '|', '}', '~',
768 };
769
770 static FcChar8 *
771 FcCharSetParseValue (FcChar8 *string, FcChar32 *value)
772 {
773 int i;
774 FcChar32 v;
775 FcChar32 c;
776
777 if (*string == ' ')
778 {
779 v = 0;
780 string++;
781 }
782 else
783 {
784 v = 0;
785 for (i = 0; i < 5; i++)
786 {
787 if (!(c = (FcChar32) (unsigned char) *string++))
788 return 0;
789 c = charToValue[c];
790 if (c == 0xff)
791 return 0;
792 v = v * 85 + c;
793 }
794 }
795 *value = v;
796 return string;
797 }
798
799 static FcBool
800 FcCharSetUnparseValue (FcStrBuf *buf, FcChar32 value)
801 {
802 int i;
803 if (value == 0)
804 {
805 return FcStrBufChar (buf, ' ');
806 }
807 else
808 {
809 FcChar8 string[6];
810 FcChar8 *s = string + 5;
811 string[5] = '\0';
812 for (i = 0; i < 5; i++)
813 {
814 *--s = valueToChar[value % 85];
815 value /= 85;
816 }
817 for (i = 0; i < 5; i++)
818 if (!FcStrBufChar (buf, *s++))
819 return FcFalse;
820 }
821 return FcTrue;
822 }
823
824 typedef struct _FcCharLeafEnt FcCharLeafEnt;
825
826 struct _FcCharLeafEnt {
827 FcCharLeafEnt *next;
828 FcChar32 hash;
829 FcCharLeaf leaf;
830 };
831
832 #define FC_CHAR_LEAF_BLOCK (4096 / sizeof (FcCharLeafEnt))
833
834 static FcCharLeafEnt *
835 FcCharLeafEntCreate (void)
836 {
837 static FcCharLeafEnt *block;
838 static int remain;
839
840 if (!remain)
841 {
842 block = malloc (FC_CHAR_LEAF_BLOCK * sizeof (FcCharLeafEnt));
843 if (!block)
844 return 0;
845 FcMemAlloc (FC_MEM_CHARLEAF, FC_CHAR_LEAF_BLOCK * sizeof (FcCharLeafEnt));
846 remain = FC_CHAR_LEAF_BLOCK;
847 }
848 remain--;
849 return block++;
850 }
851
852 #define FC_CHAR_LEAF_HASH_SIZE 257
853
854 static FcChar32
855 FcCharLeafHash (FcCharLeaf *leaf)
856 {
857 FcChar32 hash = 0;
858 int i;
859
860 for (i = 0; i < 256/32; i++)
861 hash = ((hash << 1) | (hash >> 31)) ^ leaf->map[i];
862 return hash;
863 }
864
865 static int FcCharLeafTotal;
866 static int FcCharLeafUsed;
867
868 static FcCharLeaf *
869 FcCharSetFreezeLeaf (FcCharLeaf *leaf)
870 {
871 static FcCharLeafEnt *hashTable[FC_CHAR_LEAF_HASH_SIZE];
872 FcChar32 hash = FcCharLeafHash (leaf);
873 FcCharLeafEnt **bucket = &hashTable[hash % FC_CHAR_LEAF_HASH_SIZE];
874 FcCharLeafEnt *ent;
875
876 FcCharLeafTotal++;
877 for (ent = *bucket; ent; ent = ent->next)
878 {
879 if (ent->hash == hash && !memcmp (&ent->leaf, leaf, sizeof (FcCharLeaf)))
880 return &ent->leaf;
881 }
882
883 ent = FcCharLeafEntCreate();
884 if (!ent)
885 return 0;
886 FcCharLeafUsed++;
887 ent->leaf = *leaf;
888 ent->hash = hash;
889 ent->next = *bucket;
890 *bucket = ent;
891 return &ent->leaf;
892 }
893
894 typedef struct _FcCharSetEnt FcCharSetEnt;
895
896 struct _FcCharSetEnt {
897 FcCharSetEnt *next;
898 FcChar32 hash;
899 FcCharSet set;
900 };
901
902 #define FC_CHAR_SET_HASH_SIZE 67
903
904 static FcChar32
905 FcCharSetHash (FcCharSet *fcs)
906 {
907 FcChar32 hash = 0;
908 FcChar32 *p;
909 int i;
910
911 /* hash in leaves */
912 p = (FcChar32 *) fcs->leaves;
913 for (i = 0; i < fcs->num * sizeof (FcCharLeaf *) / sizeof (FcChar32); i++)
914 hash = ((hash << 1) | (hash >> 31)) ^ *p++;
915 /* hash in numbers */
916 for (i = 0; i < fcs->num; i++)
917 hash = ((hash << 1) | (hash >> 31)) ^ fcs->numbers[i];
918 return hash;
919 }
920
921 static int FcCharSetTotal;
922 static int FcCharSetUsed;
923 static int FcCharSetTotalEnts, FcCharSetUsedEnts;
924
925 static FcCharSet *
926 FcCharSetFreezeBase (FcCharSet *fcs)
927 {
928 static FcCharSetEnt *hashTable[FC_CHAR_SET_HASH_SIZE];
929 FcChar32 hash = FcCharSetHash (fcs);
930 FcCharSetEnt **bucket = &hashTable[hash % FC_CHAR_SET_HASH_SIZE];
931 FcCharSetEnt *ent;
932 int size;
933
934 FcCharSetTotal++;
935 FcCharSetTotalEnts += fcs->num;
936 for (ent = *bucket; ent; ent = ent->next)
937 {
938 if (ent->hash == hash &&
939 ent->set.num == fcs->num &&
940 !memcmp (ent->set.leaves, fcs->leaves,
941 fcs->num * sizeof (FcCharLeaf *)) &&
942 !memcmp (ent->set.numbers, fcs->numbers,
943 fcs->num * sizeof (FcChar16)))
944 {
945 return &ent->set;
946 }
947 }
948
949 size = (sizeof (FcCharSetEnt) +
950 fcs->num * sizeof (FcCharLeaf *) +
951 fcs->num * sizeof (FcChar16));
952 ent = malloc (size);
953 if (!ent)
954 return 0;
955 FcMemAlloc (FC_MEM_CHARSET, size);
956 FcCharSetUsed++;
957 FcCharSetUsedEnts += fcs->num;
958
959 ent->set.ref = FC_REF_CONSTANT;
960 ent->set.num = fcs->num;
961 if (fcs->num)
962 {
963 ent->set.leaves = (FcCharLeaf **) (ent + 1);
964 ent->set.numbers = (FcChar16 *) (ent->set.leaves + fcs->num);
965 memcpy (ent->set.leaves, fcs->leaves, fcs->num * sizeof (FcCharLeaf *));
966 memcpy (ent->set.numbers, fcs->numbers, fcs->num * sizeof (FcChar16));
967 }
968 else
969 {
970 ent->set.leaves = 0;
971 ent->set.numbers = 0;
972 }
973
974 ent->hash = hash;
975 ent->next = *bucket;
976 *bucket = ent;
977 return &ent->set;
978 }
979
980 FcCharSet *
981 FcCharSetFreeze (FcCharSet *fcs)
982 {
983 FcCharSet *b;
984 FcCharSet *n = 0;
985 FcCharLeaf *l;
986 int i;
987
988 b = FcCharSetCreate ();
989 if (!b)
990 goto bail0;
991 for (i = 0; i < fcs->num; i++)
992 {
993 l = FcCharSetFreezeLeaf (fcs->leaves[i]);
994 if (!l)
995 goto bail1;
996 if (!FcCharSetInsertLeaf (b, fcs->numbers[i] << 8, l))
997 goto bail1;
998 }
999 n = FcCharSetFreezeBase (b);
1000 bail1:
1001 if (b->leaves)
1002 {
1003 FcMemFree (FC_MEM_CHARSET, b->num * sizeof (FcCharLeaf *));
1004 free (b->leaves);
1005 }
1006 if (b->numbers)
1007 {
1008 FcMemFree (FC_MEM_CHARSET, b->num * sizeof (FcChar16));
1009 free (b->numbers);
1010 }
1011 FcMemFree (FC_MEM_CHARSET, sizeof (FcCharSet));
1012 free (b);
1013 bail0:
1014 return n;
1015 }
1016
1017 FcCharSet *
1018 FcNameParseCharSet (FcChar8 *string)
1019 {
1020 FcCharSet *c, *n = 0;
1021 FcChar32 ucs4;
1022 FcCharLeaf *leaf;
1023 FcCharLeaf temp;
1024 FcChar32 bits;
1025 int i;
1026
1027 c = FcCharSetCreate ();
1028 if (!c)
1029 goto bail0;
1030 while (*string)
1031 {
1032 string = FcCharSetParseValue (string, &ucs4);
1033 if (!string)
1034 goto bail1;
1035 bits = 0;
1036 for (i = 0; i < 256/32; i++)
1037 {
1038 string = FcCharSetParseValue (string, &temp.map[i]);
1039 if (!string)
1040 goto bail1;
1041 bits |= temp.map[i];
1042 }
1043 if (bits)
1044 {
1045 leaf = FcCharSetFreezeLeaf (&temp);
1046 if (!leaf)
1047 goto bail1;
1048 if (!FcCharSetInsertLeaf (c, ucs4, leaf))
1049 goto bail1;
1050 }
1051 }
1052 #ifdef CHATTY
1053 printf (" %8s %8s %8s %8s\n", "total", "totalmem", "new", "newmem");
1054 printf ("Leaves: %8d %8d %8d %8d\n",
1055 FcCharLeafTotal, sizeof (FcCharLeaf) * FcCharLeafTotal,
1056 FcCharLeafUsed, sizeof (FcCharLeaf) * FcCharLeafUsed);
1057 printf ("Charsets: %8d %8d %8d %8d\n",
1058 FcCharSetTotal, sizeof (FcCharSet) * FcCharSetTotal,
1059 FcCharSetUsed, sizeof (FcCharSet) * FcCharSetUsed);
1060 printf ("Tables: %8d %8d %8d %8d\n",
1061 FcCharSetTotalEnts, FcCharSetTotalEnts * (sizeof (FcCharLeaf *) + sizeof (FcChar16)),
1062 FcCharSetUsedEnts, FcCharSetUsedEnts * (sizeof (FcCharLeaf *) + sizeof (FcChar16)));
1063 printf ("Total: %8s %8d %8s %8d\n",
1064 "",
1065 sizeof (FcCharLeaf) * FcCharLeafTotal +
1066 sizeof (FcCharSet) * FcCharSetTotal +
1067 FcCharSetTotalEnts * (sizeof (FcCharLeaf *) + sizeof (FcChar16)),
1068 "",
1069 sizeof (FcCharLeaf) * FcCharLeafUsed +
1070 sizeof (FcCharSet) * FcCharSetUsed +
1071 FcCharSetUsedEnts * (sizeof (FcCharLeaf *) + sizeof (FcChar16)));
1072 #endif
1073 n = FcCharSetFreezeBase (c);
1074 bail1:
1075 if (c->leaves)
1076 {
1077 FcMemFree (FC_MEM_CHARSET, c->num * sizeof (FcCharLeaf *));
1078 free (c->leaves);
1079 }
1080 if (c->numbers)
1081 {
1082 FcMemFree (FC_MEM_CHARSET, c->num * sizeof (FcChar16));
1083 free (c->numbers);
1084 }
1085 FcMemFree (FC_MEM_CHARSET, sizeof (FcCharSet));
1086 free (c);
1087 bail0:
1088 return n;
1089 }
1090
1091 FcBool
1092 FcNameUnparseCharSet (FcStrBuf *buf, const FcCharSet *c)
1093 {
1094 FcCharSetIter ci;
1095 int i;
1096 #ifdef CHECK
1097 int len = buf->len;
1098 #endif
1099
1100 for (FcCharSetIterStart (c, &ci);
1101 ci.leaf;
1102 FcCharSetIterNext (c, &ci))
1103 {
1104 if (!FcCharSetUnparseValue (buf, ci.ucs4))
1105 return FcFalse;
1106 for (i = 0; i < 256/32; i++)
1107 if (!FcCharSetUnparseValue (buf, ci.leaf->map[i]))
1108 return FcFalse;
1109 }
1110 #ifdef CHECK
1111 {
1112 FcCharSet *check;
1113 FcChar32 missing;
1114 FcCharSetIter ci, checki;
1115
1116 /* null terminate for parser */
1117 FcStrBufChar (buf, '\0');
1118 /* step back over null for life after test */
1119 buf->len--;
1120 check = FcNameParseCharSet (buf->buf + len);
1121 FcCharSetIterStart (c, &ci);
1122 FcCharSetIterStart (check, &checki);
1123 while (ci.leaf || checki.leaf)
1124 {
1125 if (ci.ucs4 < checki.ucs4)
1126 {
1127 printf ("Missing leaf node at 0x%x\n", ci.ucs4);
1128 FcCharSetIterNext (c, &ci);
1129 }
1130 else if (checki.ucs4 < ci.ucs4)
1131 {
1132 printf ("Extra leaf node at 0x%x\n", checki.ucs4);
1133 FcCharSetIterNext (check, &checki);
1134 }
1135 else
1136 {
1137 int i = 256/32;
1138 FcChar32 *cm = ci.leaf->map;
1139 FcChar32 *checkm = checki.leaf->map;
1140
1141 for (i = 0; i < 256; i += 32)
1142 {
1143 if (*cm != *checkm)
1144 printf ("Mismatching sets at 0x%08x: 0x%08x != 0x%08x\n",
1145 ci.ucs4 + i, *cm, *checkm);
1146 cm++;
1147 checkm++;
1148 }
1149 FcCharSetIterNext (c, &ci);
1150 FcCharSetIterNext (check, &checki);
1151 }
1152 }
1153 if ((missing = FcCharSetSubtractCount (c, check)))
1154 printf ("%d missing in reparsed result\n", missing);
1155 if ((missing = FcCharSetSubtractCount (check, c)))
1156 printf ("%d extra in reparsed result\n", missing);
1157 FcCharSetDestroy (check);
1158 }
1159 #endif
1160
1161 return FcTrue;
1162 }