]> git.wh0rd.org - fontconfig.git/blob - src/fccharset.c
Reimplement FC_LANG as FcTypeLang, freeze patterns, other cleanup
[fontconfig.git] / src / fccharset.c
1 /*
2 * $XFree86: xc/lib/fontconfig/src/fccharset.c,v 1.17 2002/07/13 05:43:25 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 FcMemAlloc (FC_MEM_CHARSET, sizeof (FcCharLeaf *));
146 fcs->leaves = leaves;
147 if (!fcs->numbers)
148 numbers = malloc (sizeof (FcChar16));
149 else
150 numbers = realloc (fcs->numbers, (fcs->num + 1) * sizeof (FcChar16));
151 if (!numbers)
152 return FcFalse;
153 FcMemAlloc (FC_MEM_CHARSET, sizeof (FcChar16));
154 fcs->numbers = numbers;
155
156 memmove (fcs->leaves + pos + 1, fcs->leaves + pos,
157 (fcs->num - pos) * sizeof (FcCharLeaf *));
158 memmove (fcs->numbers + pos + 1, fcs->numbers + pos,
159 (fcs->num - pos) * sizeof (FcChar16));
160 fcs->numbers[pos] = (FcChar16) ucs4;
161 fcs->leaves[pos] = leaf;
162 fcs->num++;
163 return FcTrue;
164 }
165
166 /*
167 * Locate the leaf containing the specified char, creating it
168 * if desired
169 */
170
171 static FcCharLeaf *
172 FcCharSetFindLeafCreate (FcCharSet *fcs, FcChar32 ucs4)
173 {
174 int pos;
175 FcCharLeaf *leaf;
176
177 pos = FcCharSetFindLeafPos (fcs, ucs4);
178 if (pos >= 0)
179 return fcs->leaves[pos];
180
181 leaf = calloc (1, sizeof (FcCharLeaf));
182 if (!leaf)
183 return 0;
184
185 pos = -pos - 1;
186 if (!FcCharSetPutLeaf (fcs, ucs4, leaf, pos))
187 {
188 free (leaf);
189 return 0;
190 }
191 FcMemAlloc (FC_MEM_CHARLEAF, sizeof (FcCharLeaf));
192 return leaf;
193 }
194
195 static FcBool
196 FcCharSetInsertLeaf (FcCharSet *fcs, FcChar32 ucs4, FcCharLeaf *leaf)
197 {
198 int pos;
199
200 pos = FcCharSetFindLeafPos (fcs, ucs4);
201 if (pos >= 0)
202 {
203 FcMemFree (FC_MEM_CHARLEAF, sizeof (FcCharLeaf));
204 free (fcs->leaves[pos]);
205 fcs->leaves[pos] = leaf;
206 return FcTrue;
207 }
208 pos = -pos - 1;
209 return FcCharSetPutLeaf (fcs, ucs4, leaf, pos);
210 }
211
212 FcBool
213 FcCharSetAddChar (FcCharSet *fcs, FcChar32 ucs4)
214 {
215 FcCharLeaf *leaf;
216 FcChar32 *b;
217
218 if (fcs->ref == FC_REF_CONSTANT)
219 return FcFalse;
220 leaf = FcCharSetFindLeafCreate (fcs, ucs4);
221 if (!leaf)
222 return FcFalse;
223 b = &leaf->map[(ucs4 & 0xff) >> 5];
224 *b |= (1 << (ucs4 & 0x1f));
225 return FcTrue;
226 }
227
228 /*
229 * An iterator for the leaves of a charset
230 */
231
232 typedef struct _fcCharSetIter {
233 FcCharLeaf *leaf;
234 FcChar32 ucs4;
235 int pos;
236 } FcCharSetIter;
237
238 /*
239 * Set iter->leaf to the leaf containing iter->ucs4 or higher
240 */
241
242 static void
243 FcCharSetIterSet (const FcCharSet *fcs, FcCharSetIter *iter)
244 {
245 int pos = FcCharSetFindLeafPos (fcs, iter->ucs4);
246
247 if (pos < 0)
248 {
249 pos = -pos - 1;
250 if (pos == fcs->num)
251 {
252 iter->ucs4 = ~0;
253 iter->leaf = 0;
254 return;
255 }
256 iter->ucs4 = (FcChar32) fcs->numbers[pos] << 8;
257 }
258 iter->leaf = fcs->leaves[pos];
259 iter->pos = pos;
260 #ifdef CHATTY
261 printf ("set %08x: %08x\n", iter->ucs4, (FcChar32) iter->leaf);
262 #endif
263 }
264
265 static void
266 FcCharSetIterNext (const FcCharSet *fcs, FcCharSetIter *iter)
267 {
268 int pos = iter->pos + 1;
269 if (pos >= fcs->num)
270 {
271 iter->ucs4 = ~0;
272 iter->leaf = 0;
273 }
274 else
275 {
276 iter->ucs4 = (FcChar32) fcs->numbers[pos] << 8;
277 iter->leaf = fcs->leaves[pos];
278 iter->pos = pos;
279 }
280 }
281
282 #ifdef CHATTY
283 static void
284 FcCharSetDump (const FcCharSet *fcs)
285 {
286 int pos;
287
288 printf ("fcs %08x:\n", (FcChar32) fcs);
289 for (pos = 0; pos < fcs->num; pos++)
290 {
291 FcCharLeaf *leaf = fcs->leaves[pos];
292 FcChar32 ucs4 = (FcChar32) fcs->numbers[pos] << 8;
293
294 printf (" %08x: %08x\n", ucs4, (FcChar32) leaf);
295 }
296 }
297 #endif
298
299 static void
300 FcCharSetIterStart (const FcCharSet *fcs, FcCharSetIter *iter)
301 {
302 #ifdef CHATTY
303 FcCharSetDump (fcs);
304 #endif
305 iter->ucs4 = 0;
306 FcCharSetIterSet (fcs, iter);
307 }
308
309 FcCharSet *
310 FcCharSetCopy (FcCharSet *src)
311 {
312 if (src->ref != FC_REF_CONSTANT)
313 src->ref++;
314 return src;
315 }
316
317 FcBool
318 FcCharSetEqual (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
338 static FcBool
339 FcCharSetAddLeaf (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
350 static FcCharSet *
351 FcCharSetOperate (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);
367 while ((ai.leaf || (bonly && bi.leaf)) && (bi.leaf || (aonly && ai.leaf)))
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;
411 bail1:
412 FcCharSetDestroy (fcs);
413 bail0:
414 return 0;
415 }
416
417 static FcBool
418 FcCharSetIntersectLeaf (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
431 FcCharSet *
432 FcCharSetIntersect (const FcCharSet *a, const FcCharSet *b)
433 {
434 return FcCharSetOperate (a, b, FcCharSetIntersectLeaf, FcFalse, FcFalse);
435 }
436
437 static FcBool
438 FcCharSetUnionLeaf (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
449 FcCharSet *
450 FcCharSetUnion (const FcCharSet *a, const FcCharSet *b)
451 {
452 return FcCharSetOperate (a, b, FcCharSetUnionLeaf, FcTrue, FcTrue);
453 }
454
455 static FcBool
456 FcCharSetSubtractLeaf (FcCharLeaf *result,
457 const FcCharLeaf *al,
458 const FcCharLeaf *bl)
459 {
460 int i;
461 FcBool nonempty = FcFalse;
462
463 for (i = 0; i < 256/32; i++)
464 if ((result->map[i] = al->map[i] & ~bl->map[i]))
465 nonempty = FcTrue;
466 return nonempty;
467 }
468
469 FcCharSet *
470 FcCharSetSubtract (const FcCharSet *a, const FcCharSet *b)
471 {
472 return FcCharSetOperate (a, b, FcCharSetSubtractLeaf, FcTrue, FcFalse);
473 }
474
475 FcBool
476 FcCharSetHasChar (const FcCharSet *fcs, FcChar32 ucs4)
477 {
478 FcCharLeaf *leaf = FcCharSetFindLeaf (fcs, ucs4);
479 if (!leaf)
480 return FcFalse;
481 return (leaf->map[(ucs4 & 0xff) >> 5] & (1 << (ucs4 & 0x1f))) != 0;
482 }
483
484 static FcChar32
485 FcCharSetPopCount (FcChar32 c1)
486 {
487 /* hackmem 169 */
488 FcChar32 c2 = (c1 >> 1) & 033333333333;
489 c2 = c1 - c2 - ((c2 >> 1) & 033333333333);
490 return (((c2 + (c2 >> 3)) & 030707070707) % 077);
491 }
492
493 FcChar32
494 FcCharSetIntersectCount (const FcCharSet *a, const FcCharSet *b)
495 {
496 FcCharSetIter ai, bi;
497 FcChar32 count = 0;
498
499 FcCharSetIterStart (a, &ai);
500 FcCharSetIterStart (b, &bi);
501 while (ai.leaf && bi.leaf)
502 {
503 if (ai.ucs4 == bi.ucs4)
504 {
505 FcChar32 *am = ai.leaf->map;
506 FcChar32 *bm = bi.leaf->map;
507 int i = 256/32;
508 while (i--)
509 count += FcCharSetPopCount (*am++ & *bm++);
510 FcCharSetIterNext (a, &ai);
511 }
512 else if (ai.ucs4 < bi.ucs4)
513 {
514 ai.ucs4 = bi.ucs4;
515 FcCharSetIterSet (a, &ai);
516 }
517 if (bi.ucs4 < ai.ucs4)
518 {
519 bi.ucs4 = ai.ucs4;
520 FcCharSetIterSet (b, &bi);
521 }
522 }
523 return count;
524 }
525
526 FcChar32
527 FcCharSetCount (const FcCharSet *a)
528 {
529 FcCharSetIter ai;
530 FcChar32 count = 0;
531
532 for (FcCharSetIterStart (a, &ai); ai.leaf; FcCharSetIterNext (a, &ai))
533 {
534 int i = 256/32;
535 FcChar32 *am = ai.leaf->map;
536
537 while (i--)
538 count += FcCharSetPopCount (*am++);
539 }
540 return count;
541 }
542
543 FcChar32
544 FcCharSetSubtractCount (const FcCharSet *a, const FcCharSet *b)
545 {
546 FcCharSetIter ai, bi;
547 FcChar32 count = 0;
548
549 FcCharSetIterStart (a, &ai);
550 FcCharSetIterStart (b, &bi);
551 while (ai.leaf)
552 {
553 if (ai.ucs4 <= bi.ucs4)
554 {
555 FcChar32 *am = ai.leaf->map;
556 int i = 256/32;
557 if (ai.ucs4 == bi.ucs4)
558 {
559 FcChar32 *bm = bi.leaf->map;;
560 while (i--)
561 count += FcCharSetPopCount (*am++ & ~*bm++);
562 }
563 else
564 {
565 while (i--)
566 count += FcCharSetPopCount (*am++);
567 }
568 FcCharSetIterNext (a, &ai);
569 }
570 else if (bi.leaf)
571 {
572 bi.ucs4 = ai.ucs4;
573 FcCharSetIterSet (b, &bi);
574 }
575 }
576 return count;
577 }
578
579 /*
580 * return FcTrue iff a is a subset of b
581 */
582 FcBool
583 FcCharSetIsSubset (const FcCharSet *a, const FcCharSet *b)
584 {
585 int ai, bi;
586 FcChar16 an, bn;
587
588 if (a == b) return FcTrue;
589 bi = 0;
590 ai = 0;
591 while (ai < a->num && bi < b->num)
592 {
593 an = a->numbers[ai];
594 bn = b->numbers[bi];
595 if (an == bn)
596 {
597 FcChar32 *am = a->leaves[ai]->map;
598 FcChar32 *bm = b->leaves[bi]->map;
599
600 if (am != bm)
601 {
602 int i = 256/32;
603 while (i--)
604 if (*am++ & ~*bm++)
605 return FcFalse;
606 }
607 ai++;
608 bi++;
609 }
610 else if (an < bn)
611 return FcFalse;
612 else
613 {
614 int low = bi + 1;
615 int high = b->num - 1;
616
617 while (low <= high)
618 {
619 int mid = (low + high) >> 1;
620 bn = b->numbers[mid];
621 if (bn == an)
622 {
623 high = mid;
624 break;
625 }
626 if (bn < an)
627 low = mid + 1;
628 else
629 high = mid - 1;
630 }
631 bi = high;
632 while (bi < b->num && b->numbers[bi] < an)
633 bi++;
634 }
635 }
636 return FcTrue;
637 }
638
639 /*
640 * These two functions efficiently walk the entire charmap for
641 * other software (like pango) that want their own copy
642 */
643
644 FcChar32
645 FcCharSetNextPage (const FcCharSet *a,
646 FcChar32 map[FC_CHARSET_MAP_SIZE],
647 FcChar32 *next)
648 {
649 FcCharSetIter ai;
650 FcChar32 page;
651
652 ai.ucs4 = *next;
653 FcCharSetIterSet (a, &ai);
654 if (!ai.leaf)
655 return FC_CHARSET_DONE;
656
657 /*
658 * Save current information
659 */
660 page = ai.ucs4;
661 memcpy (map, ai.leaf->map, sizeof (ai.leaf->map));
662 /*
663 * Step to next page
664 */
665 FcCharSetIterNext (a, &ai);
666 *next = ai.ucs4;
667
668 return page;
669 }
670
671 FcChar32
672 FcCharSetFirstPage (const FcCharSet *a,
673 FcChar32 map[FC_CHARSET_MAP_SIZE],
674 FcChar32 *next)
675 {
676 *next = 0;
677 return FcCharSetNextPage (a, map, next);
678 }
679
680 /*
681 * old coverage API, rather hard to use correctly
682 */
683 FcChar32
684 FcCharSetCoverage (const FcCharSet *a, FcChar32 page, FcChar32 *result);
685
686 FcChar32
687 FcCharSetCoverage (const FcCharSet *a, FcChar32 page, FcChar32 *result)
688 {
689 FcCharSetIter ai;
690
691 ai.ucs4 = page;
692 FcCharSetIterSet (a, &ai);
693 if (!ai.leaf)
694 {
695 memset (result, '\0', 256 / 8);
696 page = 0;
697 }
698 else
699 {
700 memcpy (result, ai.leaf->map, sizeof (ai.leaf->map));
701 FcCharSetIterNext (a, &ai);
702 page = ai.ucs4;
703 }
704 return page;
705 }
706
707 /*
708 * ASCII representation of charsets.
709 *
710 * Each leaf is represented as 9 32-bit values, the code of the first character followed
711 * by 8 32 bit values for the leaf itself. Each value is encoded as 5 ASCII characters,
712 * only 85 different values are used to avoid control characters as well as the other
713 * characters used to encode font names. 85**5 > 2^32 so things work out, but
714 * it's not exactly human readable output. As a special case, 0 is encoded as a space
715 */
716
717 static const unsigned char charToValue[256] = {
718 /* "" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
719 /* "\b" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
720 /* "\020" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
721 /* "\030" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
722 /* " " */ 0xff, 0x00, 0xff, 0x01, 0x02, 0x03, 0x04, 0xff,
723 /* "(" */ 0x05, 0x06, 0x07, 0x08, 0xff, 0xff, 0x09, 0x0a,
724 /* "0" */ 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12,
725 /* "8" */ 0x13, 0x14, 0xff, 0x15, 0x16, 0xff, 0x17, 0x18,
726 /* "@" */ 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
727 /* "H" */ 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
728 /* "P" */ 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30,
729 /* "X" */ 0x31, 0x32, 0x33, 0x34, 0xff, 0x35, 0x36, 0xff,
730 /* "`" */ 0xff, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d,
731 /* "h" */ 0x3e, 0x3f, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45,
732 /* "p" */ 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d,
733 /* "x" */ 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53, 0x54, 0xff,
734 /* "\200" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
735 /* "\210" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
736 /* "\220" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
737 /* "\230" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
738 /* "\240" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
739 /* "\250" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
740 /* "\260" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
741 /* "\270" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
742 /* "\300" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
743 /* "\310" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
744 /* "\320" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
745 /* "\330" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
746 /* "\340" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
747 /* "\350" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
748 /* "\360" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
749 /* "\370" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
750 };
751
752 static const FcChar8 valueToChar[0x55] = {
753 /* 0x00 */ '!', '#', '$', '%', '&', '(', ')', '*',
754 /* 0x08 */ '+', '.', '/', '0', '1', '2', '3', '4',
755 /* 0x10 */ '5', '6', '7', '8', '9', ';', '<', '>',
756 /* 0x18 */ '?', '@', 'A', 'B', 'C', 'D', 'E', 'F',
757 /* 0x20 */ 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
758 /* 0x28 */ 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
759 /* 0x30 */ 'W', 'X', 'Y', 'Z', '[', ']', '^', 'a',
760 /* 0x38 */ 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
761 /* 0x40 */ 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q',
762 /* 0x48 */ 'r', 's', 't', 'u', 'v', 'w', 'x', 'y',
763 /* 0x50 */ 'z', '{', '|', '}', '~',
764 };
765
766 static FcChar8 *
767 FcCharSetParseValue (FcChar8 *string, FcChar32 *value)
768 {
769 int i;
770 FcChar32 v;
771 FcChar32 c;
772
773 if (*string == ' ')
774 {
775 v = 0;
776 string++;
777 }
778 else
779 {
780 v = 0;
781 for (i = 0; i < 5; i++)
782 {
783 if (!(c = (FcChar32) (unsigned char) *string++))
784 return 0;
785 c = charToValue[c];
786 if (c == 0xff)
787 return 0;
788 v = v * 85 + c;
789 }
790 }
791 *value = v;
792 return string;
793 }
794
795 static FcBool
796 FcCharSetUnparseValue (FcStrBuf *buf, FcChar32 value)
797 {
798 int i;
799 if (value == 0)
800 {
801 return FcStrBufChar (buf, ' ');
802 }
803 else
804 {
805 FcChar8 string[6];
806 FcChar8 *s = string + 5;
807 string[5] = '\0';
808 for (i = 0; i < 5; i++)
809 {
810 *--s = valueToChar[value % 85];
811 value /= 85;
812 }
813 for (i = 0; i < 5; i++)
814 if (!FcStrBufChar (buf, *s++))
815 return FcFalse;
816 }
817 return FcTrue;
818 }
819
820 typedef struct _FcCharLeafEnt FcCharLeafEnt;
821
822 struct _FcCharLeafEnt {
823 FcCharLeafEnt *next;
824 FcChar32 hash;
825 FcCharLeaf leaf;
826 };
827
828 #define FC_CHAR_LEAF_BLOCK (4096 / sizeof (FcCharLeafEnt))
829
830 static FcCharLeafEnt *
831 FcCharLeafEntCreate (void)
832 {
833 static FcCharLeafEnt *block;
834 static int remain;
835
836 if (!remain)
837 {
838 block = malloc (FC_CHAR_LEAF_BLOCK * sizeof (FcCharLeafEnt));
839 if (!block)
840 return 0;
841 FcMemAlloc (FC_MEM_CHARLEAF, FC_CHAR_LEAF_BLOCK * sizeof (FcCharLeafEnt));
842 remain = FC_CHAR_LEAF_BLOCK;
843 }
844 remain--;
845 return block++;
846 }
847
848 #define FC_CHAR_LEAF_HASH_SIZE 257
849
850 static FcChar32
851 FcCharLeafHash (FcCharLeaf *leaf)
852 {
853 FcChar32 hash = 0;
854 int i;
855
856 for (i = 0; i < 256/32; i++)
857 hash = ((hash << 1) | (hash >> 31)) ^ leaf->map[i];
858 return hash;
859 }
860
861 static int FcCharLeafTotal;
862 static int FcCharLeafUsed;
863
864 static FcCharLeaf *
865 FcCharSetFreezeLeaf (FcCharLeaf *leaf)
866 {
867 static FcCharLeafEnt *hashTable[FC_CHAR_LEAF_HASH_SIZE];
868 FcChar32 hash = FcCharLeafHash (leaf);
869 FcCharLeafEnt **bucket = &hashTable[hash % FC_CHAR_LEAF_HASH_SIZE];
870 FcCharLeafEnt *ent;
871
872 FcCharLeafTotal++;
873 for (ent = *bucket; ent; ent = ent->next)
874 {
875 if (ent->hash == hash && !memcmp (&ent->leaf, leaf, sizeof (FcCharLeaf)))
876 return &ent->leaf;
877 }
878
879 ent = FcCharLeafEntCreate();
880 if (!ent)
881 return 0;
882 FcCharLeafUsed++;
883 ent->leaf = *leaf;
884 ent->hash = hash;
885 ent->next = *bucket;
886 *bucket = ent;
887 return &ent->leaf;
888 }
889
890 typedef struct _FcCharSetEnt FcCharSetEnt;
891
892 struct _FcCharSetEnt {
893 FcCharSetEnt *next;
894 FcChar32 hash;
895 FcCharSet set;
896 };
897
898 #define FC_CHAR_SET_HASH_SIZE 67
899
900 static FcChar32
901 FcCharSetHash (FcCharSet *fcs)
902 {
903 FcChar32 hash = 0;
904 FcChar32 *p;
905 int i;
906
907 /* hash in leaves */
908 p = (FcChar32 *) fcs->leaves;
909 for (i = 0; i < fcs->num * sizeof (FcCharLeaf *) / sizeof (FcChar32); i++)
910 hash = ((hash << 1) | (hash >> 31)) ^ *p++;
911 /* hash in numbers */
912 for (i = 0; i < fcs->num; i++)
913 hash = ((hash << 1) | (hash >> 31)) ^ fcs->numbers[i];
914 return hash;
915 }
916
917 static int FcCharSetTotal;
918 static int FcCharSetUsed;
919 static int FcCharSetTotalEnts, FcCharSetUsedEnts;
920
921 static FcCharSet *
922 FcCharSetFreezeBase (FcCharSet *fcs)
923 {
924 static FcCharSetEnt *hashTable[FC_CHAR_SET_HASH_SIZE];
925 FcChar32 hash = FcCharSetHash (fcs);
926 FcCharSetEnt **bucket = &hashTable[hash % FC_CHAR_SET_HASH_SIZE];
927 FcCharSetEnt *ent;
928 int size;
929
930 FcCharSetTotal++;
931 FcCharSetTotalEnts += fcs->num;
932 for (ent = *bucket; ent; ent = ent->next)
933 {
934 if (ent->hash == hash &&
935 ent->set.num == fcs->num &&
936 !memcmp (ent->set.leaves, fcs->leaves,
937 fcs->num * sizeof (FcCharLeaf *)) &&
938 !memcmp (ent->set.numbers, fcs->numbers,
939 fcs->num * sizeof (FcChar16)))
940 {
941 return &ent->set;
942 }
943 }
944
945 size = (sizeof (FcCharSetEnt) +
946 fcs->num * sizeof (FcCharLeaf *) +
947 fcs->num * sizeof (FcChar16));
948 ent = malloc (size);
949 if (!ent)
950 return 0;
951 FcMemAlloc (FC_MEM_CHARSET, size);
952 FcCharSetUsed++;
953 FcCharSetUsedEnts += fcs->num;
954
955 ent->set.ref = FC_REF_CONSTANT;
956 ent->set.num = fcs->num;
957 if (fcs->num)
958 {
959 ent->set.leaves = (FcCharLeaf **) (ent + 1);
960 ent->set.numbers = (FcChar16 *) (ent->set.leaves + fcs->num);
961 memcpy (ent->set.leaves, fcs->leaves, fcs->num * sizeof (FcCharLeaf *));
962 memcpy (ent->set.numbers, fcs->numbers, fcs->num * sizeof (FcChar16));
963 }
964 else
965 {
966 ent->set.leaves = 0;
967 ent->set.numbers = 0;
968 }
969
970 ent->hash = hash;
971 ent->next = *bucket;
972 *bucket = ent;
973 return &ent->set;
974 }
975
976 FcCharSet *
977 FcCharSetFreeze (FcCharSet *fcs)
978 {
979 FcCharSet *b;
980 FcCharSet *n = 0;
981 FcCharLeaf *l;
982 int i;
983
984 b = FcCharSetCreate ();
985 if (!b)
986 goto bail0;
987 for (i = 0; i < fcs->num; i++)
988 {
989 l = FcCharSetFreezeLeaf (fcs->leaves[i]);
990 if (!l)
991 goto bail1;
992 if (!FcCharSetInsertLeaf (b, fcs->numbers[i] << 8, l))
993 goto bail1;
994 }
995 n = FcCharSetFreezeBase (b);
996 bail1:
997 if (b->leaves)
998 {
999 FcMemFree (FC_MEM_CHARSET, b->num * sizeof (FcCharLeaf *));
1000 free (b->leaves);
1001 }
1002 if (b->numbers)
1003 {
1004 FcMemFree (FC_MEM_CHARSET, b->num * sizeof (FcChar16));
1005 free (b->numbers);
1006 }
1007 FcMemFree (FC_MEM_CHARSET, sizeof (FcCharSet));
1008 free (b);
1009 bail0:
1010 return n;
1011 }
1012
1013 FcCharSet *
1014 FcNameParseCharSet (FcChar8 *string)
1015 {
1016 FcCharSet *c, *n = 0;
1017 FcChar32 ucs4;
1018 FcCharLeaf *leaf;
1019 FcCharLeaf temp;
1020 FcChar32 bits;
1021 int i;
1022
1023 c = FcCharSetCreate ();
1024 if (!c)
1025 goto bail0;
1026 while (*string)
1027 {
1028 string = FcCharSetParseValue (string, &ucs4);
1029 if (!string)
1030 goto bail1;
1031 bits = 0;
1032 for (i = 0; i < 256/32; i++)
1033 {
1034 string = FcCharSetParseValue (string, &temp.map[i]);
1035 if (!string)
1036 goto bail1;
1037 bits |= temp.map[i];
1038 }
1039 if (bits)
1040 {
1041 leaf = FcCharSetFreezeLeaf (&temp);
1042 if (!leaf)
1043 goto bail1;
1044 if (!FcCharSetInsertLeaf (c, ucs4, leaf))
1045 goto bail1;
1046 }
1047 }
1048 #ifdef CHATTY
1049 printf (" %8s %8s %8s %8s\n", "total", "totalmem", "new", "newmem");
1050 printf ("Leaves: %8d %8d %8d %8d\n",
1051 FcCharLeafTotal, sizeof (FcCharLeaf) * FcCharLeafTotal,
1052 FcCharLeafUsed, sizeof (FcCharLeaf) * FcCharLeafUsed);
1053 printf ("Charsets: %8d %8d %8d %8d\n",
1054 FcCharSetTotal, sizeof (FcCharSet) * FcCharSetTotal,
1055 FcCharSetUsed, sizeof (FcCharSet) * FcCharSetUsed);
1056 printf ("Tables: %8d %8d %8d %8d\n",
1057 FcCharSetTotalEnts, FcCharSetTotalEnts * (sizeof (FcCharLeaf *) + sizeof (FcChar16)),
1058 FcCharSetUsedEnts, FcCharSetUsedEnts * (sizeof (FcCharLeaf *) + sizeof (FcChar16)));
1059 printf ("Total: %8s %8d %8s %8d\n",
1060 "",
1061 sizeof (FcCharLeaf) * FcCharLeafTotal +
1062 sizeof (FcCharSet) * FcCharSetTotal +
1063 FcCharSetTotalEnts * (sizeof (FcCharLeaf *) + sizeof (FcChar16)),
1064 "",
1065 sizeof (FcCharLeaf) * FcCharLeafUsed +
1066 sizeof (FcCharSet) * FcCharSetUsed +
1067 FcCharSetUsedEnts * (sizeof (FcCharLeaf *) + sizeof (FcChar16)));
1068 #endif
1069 n = FcCharSetFreezeBase (c);
1070 bail1:
1071 if (c->leaves)
1072 {
1073 FcMemFree (FC_MEM_CHARSET, c->num * sizeof (FcCharLeaf *));
1074 free (c->leaves);
1075 }
1076 if (c->numbers)
1077 {
1078 FcMemFree (FC_MEM_CHARSET, c->num * sizeof (FcChar16));
1079 free (c->numbers);
1080 }
1081 FcMemFree (FC_MEM_CHARSET, sizeof (FcCharSet));
1082 free (c);
1083 bail0:
1084 return n;
1085 }
1086
1087 FcBool
1088 FcNameUnparseCharSet (FcStrBuf *buf, const FcCharSet *c)
1089 {
1090 FcCharSetIter ci;
1091 int i;
1092 #ifdef CHECK
1093 int len = buf->len;
1094 #endif
1095
1096 for (FcCharSetIterStart (c, &ci);
1097 ci.leaf;
1098 FcCharSetIterNext (c, &ci))
1099 {
1100 if (!FcCharSetUnparseValue (buf, ci.ucs4))
1101 return FcFalse;
1102 for (i = 0; i < 256/32; i++)
1103 if (!FcCharSetUnparseValue (buf, ci.leaf->map[i]))
1104 return FcFalse;
1105 }
1106 #ifdef CHECK
1107 {
1108 FcCharSet *check;
1109 FcChar32 missing;
1110 FcCharSetIter ci, checki;
1111
1112 /* null terminate for parser */
1113 FcStrBufChar (buf, '\0');
1114 /* step back over null for life after test */
1115 buf->len--;
1116 check = FcNameParseCharSet (buf->buf + len);
1117 FcCharSetIterStart (c, &ci);
1118 FcCharSetIterStart (check, &checki);
1119 while (ci.leaf || checki.leaf)
1120 {
1121 if (ci.ucs4 < checki.ucs4)
1122 {
1123 printf ("Missing leaf node at 0x%x\n", ci.ucs4);
1124 FcCharSetIterNext (c, &ci);
1125 }
1126 else if (checki.ucs4 < ci.ucs4)
1127 {
1128 printf ("Extra leaf node at 0x%x\n", checki.ucs4);
1129 FcCharSetIterNext (check, &checki);
1130 }
1131 else
1132 {
1133 int i = 256/32;
1134 FcChar32 *cm = ci.leaf->map;
1135 FcChar32 *checkm = checki.leaf->map;
1136
1137 for (i = 0; i < 256; i += 32)
1138 {
1139 if (*cm != *checkm)
1140 printf ("Mismatching sets at 0x%08x: 0x%08x != 0x%08x\n",
1141 ci.ucs4 + i, *cm, *checkm);
1142 cm++;
1143 checkm++;
1144 }
1145 FcCharSetIterNext (c, &ci);
1146 FcCharSetIterNext (check, &checki);
1147 }
1148 }
1149 if ((missing = FcCharSetSubtractCount (c, check)))
1150 printf ("%d missing in reparsed result\n", missing);
1151 if ((missing = FcCharSetSubtractCount (check, c)))
1152 printf ("%d extra in reparsed result\n", missing);
1153 FcCharSetDestroy (check);
1154 }
1155 #endif
1156
1157 return FcTrue;
1158 }
1159
1160 /*
1161 * Figure out whether the available freetype has FT_Get_Next_Char
1162 */
1163
1164 #if FREETYPE_MAJOR > 2
1165 # define HAS_NEXT_CHAR
1166 #else
1167 # if FREETYPE_MAJOR == 2
1168 # if FREETYPE_MINOR > 0
1169 # define HAS_NEXT_CHAR
1170 # else
1171 # if FREETYPE_MINOR == 0
1172 # if FREETYPE_PATCH >= 9
1173 # define HAS_NEXT_CHAR
1174 # endif
1175 # endif
1176 # endif
1177 # endif
1178 #endif
1179
1180 /*
1181 * For our purposes, this approximation is sufficient
1182 */
1183 #ifndef HAS_NEXT_CHAR
1184 #define FT_Get_First_Char(face, gi) ((*(gi) = 1), 1)
1185 #define FT_Get_Next_Char(face, ucs4, gi) ((ucs4) >= 0xffffff ? \
1186 (*(gi) = 0), 0 : \
1187 (*(gi) = 1), (ucs4) + 1)
1188 #warning "No FT_Get_Next_Char"
1189 #endif
1190
1191 typedef struct _FcCharEnt {
1192 FcChar16 bmp;
1193 unsigned char encode;
1194 } FcCharEnt;
1195
1196 struct _FcCharMap {
1197 const FcCharEnt *ent;
1198 int nent;
1199 };
1200
1201 typedef struct _FcFontDecode {
1202 FT_Encoding encoding;
1203 const FcCharMap *map;
1204 FcChar32 max;
1205 } FcFontDecode;
1206
1207 static const FcCharEnt AppleRomanEnt[] = {
1208 { 0x0020, 0x20 }, /* SPACE */
1209 { 0x0021, 0x21 }, /* EXCLAMATION MARK */
1210 { 0x0022, 0x22 }, /* QUOTATION MARK */
1211 { 0x0023, 0x23 }, /* NUMBER SIGN */
1212 { 0x0024, 0x24 }, /* DOLLAR SIGN */
1213 { 0x0025, 0x25 }, /* PERCENT SIGN */
1214 { 0x0026, 0x26 }, /* AMPERSAND */
1215 { 0x0027, 0x27 }, /* APOSTROPHE */
1216 { 0x0028, 0x28 }, /* LEFT PARENTHESIS */
1217 { 0x0029, 0x29 }, /* RIGHT PARENTHESIS */
1218 { 0x002A, 0x2A }, /* ASTERISK */
1219 { 0x002B, 0x2B }, /* PLUS SIGN */
1220 { 0x002C, 0x2C }, /* COMMA */
1221 { 0x002D, 0x2D }, /* HYPHEN-MINUS */
1222 { 0x002E, 0x2E }, /* FULL STOP */
1223 { 0x002F, 0x2F }, /* SOLIDUS */
1224 { 0x0030, 0x30 }, /* DIGIT ZERO */
1225 { 0x0031, 0x31 }, /* DIGIT ONE */
1226 { 0x0032, 0x32 }, /* DIGIT TWO */
1227 { 0x0033, 0x33 }, /* DIGIT THREE */
1228 { 0x0034, 0x34 }, /* DIGIT FOUR */
1229 { 0x0035, 0x35 }, /* DIGIT FIVE */
1230 { 0x0036, 0x36 }, /* DIGIT SIX */
1231 { 0x0037, 0x37 }, /* DIGIT SEVEN */
1232 { 0x0038, 0x38 }, /* DIGIT EIGHT */
1233 { 0x0039, 0x39 }, /* DIGIT NINE */
1234 { 0x003A, 0x3A }, /* COLON */
1235 { 0x003B, 0x3B }, /* SEMICOLON */
1236 { 0x003C, 0x3C }, /* LESS-THAN SIGN */
1237 { 0x003D, 0x3D }, /* EQUALS SIGN */
1238 { 0x003E, 0x3E }, /* GREATER-THAN SIGN */
1239 { 0x003F, 0x3F }, /* QUESTION MARK */
1240 { 0x0040, 0x40 }, /* COMMERCIAL AT */
1241 { 0x0041, 0x41 }, /* LATIN CAPITAL LETTER A */
1242 { 0x0042, 0x42 }, /* LATIN CAPITAL LETTER B */
1243 { 0x0043, 0x43 }, /* LATIN CAPITAL LETTER C */
1244 { 0x0044, 0x44 }, /* LATIN CAPITAL LETTER D */
1245 { 0x0045, 0x45 }, /* LATIN CAPITAL LETTER E */
1246 { 0x0046, 0x46 }, /* LATIN CAPITAL LETTER F */
1247 { 0x0047, 0x47 }, /* LATIN CAPITAL LETTER G */
1248 { 0x0048, 0x48 }, /* LATIN CAPITAL LETTER H */
1249 { 0x0049, 0x49 }, /* LATIN CAPITAL LETTER I */
1250 { 0x004A, 0x4A }, /* LATIN CAPITAL LETTER J */
1251 { 0x004B, 0x4B }, /* LATIN CAPITAL LETTER K */
1252 { 0x004C, 0x4C }, /* LATIN CAPITAL LETTER L */
1253 { 0x004D, 0x4D }, /* LATIN CAPITAL LETTER M */
1254 { 0x004E, 0x4E }, /* LATIN CAPITAL LETTER N */
1255 { 0x004F, 0x4F }, /* LATIN CAPITAL LETTER O */
1256 { 0x0050, 0x50 }, /* LATIN CAPITAL LETTER P */
1257 { 0x0051, 0x51 }, /* LATIN CAPITAL LETTER Q */
1258 { 0x0052, 0x52 }, /* LATIN CAPITAL LETTER R */
1259 { 0x0053, 0x53 }, /* LATIN CAPITAL LETTER S */
1260 { 0x0054, 0x54 }, /* LATIN CAPITAL LETTER T */
1261 { 0x0055, 0x55 }, /* LATIN CAPITAL LETTER U */
1262 { 0x0056, 0x56 }, /* LATIN CAPITAL LETTER V */
1263 { 0x0057, 0x57 }, /* LATIN CAPITAL LETTER W */
1264 { 0x0058, 0x58 }, /* LATIN CAPITAL LETTER X */
1265 { 0x0059, 0x59 }, /* LATIN CAPITAL LETTER Y */
1266 { 0x005A, 0x5A }, /* LATIN CAPITAL LETTER Z */
1267 { 0x005B, 0x5B }, /* LEFT SQUARE BRACKET */
1268 { 0x005C, 0x5C }, /* REVERSE SOLIDUS */
1269 { 0x005D, 0x5D }, /* RIGHT SQUARE BRACKET */
1270 { 0x005E, 0x5E }, /* CIRCUMFLEX ACCENT */
1271 { 0x005F, 0x5F }, /* LOW LINE */
1272 { 0x0060, 0x60 }, /* GRAVE ACCENT */
1273 { 0x0061, 0x61 }, /* LATIN SMALL LETTER A */
1274 { 0x0062, 0x62 }, /* LATIN SMALL LETTER B */
1275 { 0x0063, 0x63 }, /* LATIN SMALL LETTER C */
1276 { 0x0064, 0x64 }, /* LATIN SMALL LETTER D */
1277 { 0x0065, 0x65 }, /* LATIN SMALL LETTER E */
1278 { 0x0066, 0x66 }, /* LATIN SMALL LETTER F */
1279 { 0x0067, 0x67 }, /* LATIN SMALL LETTER G */
1280 { 0x0068, 0x68 }, /* LATIN SMALL LETTER H */
1281 { 0x0069, 0x69 }, /* LATIN SMALL LETTER I */
1282 { 0x006A, 0x6A }, /* LATIN SMALL LETTER J */
1283 { 0x006B, 0x6B }, /* LATIN SMALL LETTER K */
1284 { 0x006C, 0x6C }, /* LATIN SMALL LETTER L */
1285 { 0x006D, 0x6D }, /* LATIN SMALL LETTER M */
1286 { 0x006E, 0x6E }, /* LATIN SMALL LETTER N */
1287 { 0x006F, 0x6F }, /* LATIN SMALL LETTER O */
1288 { 0x0070, 0x70 }, /* LATIN SMALL LETTER P */
1289 { 0x0071, 0x71 }, /* LATIN SMALL LETTER Q */
1290 { 0x0072, 0x72 }, /* LATIN SMALL LETTER R */
1291 { 0x0073, 0x73 }, /* LATIN SMALL LETTER S */
1292 { 0x0074, 0x74 }, /* LATIN SMALL LETTER T */
1293 { 0x0075, 0x75 }, /* LATIN SMALL LETTER U */
1294 { 0x0076, 0x76 }, /* LATIN SMALL LETTER V */
1295 { 0x0077, 0x77 }, /* LATIN SMALL LETTER W */
1296 { 0x0078, 0x78 }, /* LATIN SMALL LETTER X */
1297 { 0x0079, 0x79 }, /* LATIN SMALL LETTER Y */
1298 { 0x007A, 0x7A }, /* LATIN SMALL LETTER Z */
1299 { 0x007B, 0x7B }, /* LEFT CURLY BRACKET */
1300 { 0x007C, 0x7C }, /* VERTICAL LINE */
1301 { 0x007D, 0x7D }, /* RIGHT CURLY BRACKET */
1302 { 0x007E, 0x7E }, /* TILDE */
1303 { 0x00A0, 0xCA }, /* NO-BREAK SPACE */
1304 { 0x00A1, 0xC1 }, /* INVERTED EXCLAMATION MARK */
1305 { 0x00A2, 0xA2 }, /* CENT SIGN */
1306 { 0x00A3, 0xA3 }, /* POUND SIGN */
1307 { 0x00A5, 0xB4 }, /* YEN SIGN */
1308 { 0x00A7, 0xA4 }, /* SECTION SIGN */
1309 { 0x00A8, 0xAC }, /* DIAERESIS */
1310 { 0x00A9, 0xA9 }, /* COPYRIGHT SIGN */
1311 { 0x00AA, 0xBB }, /* FEMININE ORDINAL INDICATOR */
1312 { 0x00AB, 0xC7 }, /* LEFT-POINTING DOUBLE ANGLE QUOTATION MARK */
1313 { 0x00AC, 0xC2 }, /* NOT SIGN */
1314 { 0x00AE, 0xA8 }, /* REGISTERED SIGN */
1315 { 0x00AF, 0xF8 }, /* MACRON */
1316 { 0x00B0, 0xA1 }, /* DEGREE SIGN */
1317 { 0x00B1, 0xB1 }, /* PLUS-MINUS SIGN */
1318 { 0x00B4, 0xAB }, /* ACUTE ACCENT */
1319 { 0x00B5, 0xB5 }, /* MICRO SIGN */
1320 { 0x00B6, 0xA6 }, /* PILCROW SIGN */
1321 { 0x00B7, 0xE1 }, /* MIDDLE DOT */
1322 { 0x00B8, 0xFC }, /* CEDILLA */
1323 { 0x00BA, 0xBC }, /* MASCULINE ORDINAL INDICATOR */
1324 { 0x00BB, 0xC8 }, /* RIGHT-POINTING DOUBLE ANGLE QUOTATION MARK */
1325 { 0x00BF, 0xC0 }, /* INVERTED QUESTION MARK */
1326 { 0x00C0, 0xCB }, /* LATIN CAPITAL LETTER A WITH GRAVE */
1327 { 0x00C1, 0xE7 }, /* LATIN CAPITAL LETTER A WITH ACUTE */
1328 { 0x00C2, 0xE5 }, /* LATIN CAPITAL LETTER A WITH CIRCUMFLEX */
1329 { 0x00C3, 0xCC }, /* LATIN CAPITAL LETTER A WITH TILDE */
1330 { 0x00C4, 0x80 }, /* LATIN CAPITAL LETTER A WITH DIAERESIS */
1331 { 0x00C5, 0x81 }, /* LATIN CAPITAL LETTER A WITH RING ABOVE */
1332 { 0x00C6, 0xAE }, /* LATIN CAPITAL LETTER AE */
1333 { 0x00C7, 0x82 }, /* LATIN CAPITAL LETTER C WITH CEDILLA */
1334 { 0x00C8, 0xE9 }, /* LATIN CAPITAL LETTER E WITH GRAVE */
1335 { 0x00C9, 0x83 }, /* LATIN CAPITAL LETTER E WITH ACUTE */
1336 { 0x00CA, 0xE6 }, /* LATIN CAPITAL LETTER E WITH CIRCUMFLEX */
1337 { 0x00CB, 0xE8 }, /* LATIN CAPITAL LETTER E WITH DIAERESIS */
1338 { 0x00CC, 0xED }, /* LATIN CAPITAL LETTER I WITH GRAVE */
1339 { 0x00CD, 0xEA }, /* LATIN CAPITAL LETTER I WITH ACUTE */
1340 { 0x00CE, 0xEB }, /* LATIN CAPITAL LETTER I WITH CIRCUMFLEX */
1341 { 0x00CF, 0xEC }, /* LATIN CAPITAL LETTER I WITH DIAERESIS */
1342 { 0x00D1, 0x84 }, /* LATIN CAPITAL LETTER N WITH TILDE */
1343 { 0x00D2, 0xF1 }, /* LATIN CAPITAL LETTER O WITH GRAVE */
1344 { 0x00D3, 0xEE }, /* LATIN CAPITAL LETTER O WITH ACUTE */
1345 { 0x00D4, 0xEF }, /* LATIN CAPITAL LETTER O WITH CIRCUMFLEX */
1346 { 0x00D5, 0xCD }, /* LATIN CAPITAL LETTER O WITH TILDE */
1347 { 0x00D6, 0x85 }, /* LATIN CAPITAL LETTER O WITH DIAERESIS */
1348 { 0x00D8, 0xAF }, /* LATIN CAPITAL LETTER O WITH STROKE */
1349 { 0x00D9, 0xF4 }, /* LATIN CAPITAL LETTER U WITH GRAVE */
1350 { 0x00DA, 0xF2 }, /* LATIN CAPITAL LETTER U WITH ACUTE */
1351 { 0x00DB, 0xF3 }, /* LATIN CAPITAL LETTER U WITH CIRCUMFLEX */
1352 { 0x00DC, 0x86 }, /* LATIN CAPITAL LETTER U WITH DIAERESIS */
1353 { 0x00DF, 0xA7 }, /* LATIN SMALL LETTER SHARP S */
1354 { 0x00E0, 0x88 }, /* LATIN SMALL LETTER A WITH GRAVE */
1355 { 0x00E1, 0x87 }, /* LATIN SMALL LETTER A WITH ACUTE */
1356 { 0x00E2, 0x89 }, /* LATIN SMALL LETTER A WITH CIRCUMFLEX */
1357 { 0x00E3, 0x8B }, /* LATIN SMALL LETTER A WITH TILDE */
1358 { 0x00E4, 0x8A }, /* LATIN SMALL LETTER A WITH DIAERESIS */
1359 { 0x00E5, 0x8C }, /* LATIN SMALL LETTER A WITH RING ABOVE */
1360 { 0x00E6, 0xBE }, /* LATIN SMALL LETTER AE */
1361 { 0x00E7, 0x8D }, /* LATIN SMALL LETTER C WITH CEDILLA */
1362 { 0x00E8, 0x8F }, /* LATIN SMALL LETTER E WITH GRAVE */
1363 { 0x00E9, 0x8E }, /* LATIN SMALL LETTER E WITH ACUTE */
1364 { 0x00EA, 0x90 }, /* LATIN SMALL LETTER E WITH CIRCUMFLEX */
1365 { 0x00EB, 0x91 }, /* LATIN SMALL LETTER E WITH DIAERESIS */
1366 { 0x00EC, 0x93 }, /* LATIN SMALL LETTER I WITH GRAVE */
1367 { 0x00ED, 0x92 }, /* LATIN SMALL LETTER I WITH ACUTE */
1368 { 0x00EE, 0x94 }, /* LATIN SMALL LETTER I WITH CIRCUMFLEX */
1369 { 0x00EF, 0x95 }, /* LATIN SMALL LETTER I WITH DIAERESIS */
1370 { 0x00F1, 0x96 }, /* LATIN SMALL LETTER N WITH TILDE */
1371 { 0x00F2, 0x98 }, /* LATIN SMALL LETTER O WITH GRAVE */
1372 { 0x00F3, 0x97 }, /* LATIN SMALL LETTER O WITH ACUTE */
1373 { 0x00F4, 0x99 }, /* LATIN SMALL LETTER O WITH CIRCUMFLEX */
1374 { 0x00F5, 0x9B }, /* LATIN SMALL LETTER O WITH TILDE */
1375 { 0x00F6, 0x9A }, /* LATIN SMALL LETTER O WITH DIAERESIS */
1376 { 0x00F7, 0xD6 }, /* DIVISION SIGN */
1377 { 0x00F8, 0xBF }, /* LATIN SMALL LETTER O WITH STROKE */
1378 { 0x00F9, 0x9D }, /* LATIN SMALL LETTER U WITH GRAVE */
1379 { 0x00FA, 0x9C }, /* LATIN SMALL LETTER U WITH ACUTE */
1380 { 0x00FB, 0x9E }, /* LATIN SMALL LETTER U WITH CIRCUMFLEX */
1381 { 0x00FC, 0x9F }, /* LATIN SMALL LETTER U WITH DIAERESIS */
1382 { 0x00FF, 0xD8 }, /* LATIN SMALL LETTER Y WITH DIAERESIS */
1383 { 0x0131, 0xF5 }, /* LATIN SMALL LETTER DOTLESS I */
1384 { 0x0152, 0xCE }, /* LATIN CAPITAL LIGATURE OE */
1385 { 0x0153, 0xCF }, /* LATIN SMALL LIGATURE OE */
1386 { 0x0178, 0xD9 }, /* LATIN CAPITAL LETTER Y WITH DIAERESIS */
1387 { 0x0192, 0xC4 }, /* LATIN SMALL LETTER F WITH HOOK */
1388 { 0x02C6, 0xF6 }, /* MODIFIER LETTER CIRCUMFLEX ACCENT */
1389 { 0x02C7, 0xFF }, /* CARON */
1390 { 0x02D8, 0xF9 }, /* BREVE */
1391 { 0x02D9, 0xFA }, /* DOT ABOVE */
1392 { 0x02DA, 0xFB }, /* RING ABOVE */
1393 { 0x02DB, 0xFE }, /* OGONEK */
1394 { 0x02DC, 0xF7 }, /* SMALL TILDE */
1395 { 0x02DD, 0xFD }, /* DOUBLE ACUTE ACCENT */
1396 { 0x03A9, 0xBD }, /* GREEK CAPITAL LETTER OMEGA */
1397 { 0x03C0, 0xB9 }, /* GREEK SMALL LETTER PI */
1398 { 0x2013, 0xD0 }, /* EN DASH */
1399 { 0x2014, 0xD1 }, /* EM DASH */
1400 { 0x2018, 0xD4 }, /* LEFT SINGLE QUOTATION MARK */
1401 { 0x2019, 0xD5 }, /* RIGHT SINGLE QUOTATION MARK */
1402 { 0x201A, 0xE2 }, /* SINGLE LOW-9 QUOTATION MARK */
1403 { 0x201C, 0xD2 }, /* LEFT DOUBLE QUOTATION MARK */
1404 { 0x201D, 0xD3 }, /* RIGHT DOUBLE QUOTATION MARK */
1405 { 0x201E, 0xE3 }, /* DOUBLE LOW-9 QUOTATION MARK */
1406 { 0x2020, 0xA0 }, /* DAGGER */
1407 { 0x2021, 0xE0 }, /* DOUBLE DAGGER */
1408 { 0x2022, 0xA5 }, /* BULLET */
1409 { 0x2026, 0xC9 }, /* HORIZONTAL ELLIPSIS */
1410 { 0x2030, 0xE4 }, /* PER MILLE SIGN */
1411 { 0x2039, 0xDC }, /* SINGLE LEFT-POINTING ANGLE QUOTATION MARK */
1412 { 0x203A, 0xDD }, /* SINGLE RIGHT-POINTING ANGLE QUOTATION MARK */
1413 { 0x2044, 0xDA }, /* FRACTION SLASH */
1414 { 0x20AC, 0xDB }, /* EURO SIGN */
1415 { 0x2122, 0xAA }, /* TRADE MARK SIGN */
1416 { 0x2202, 0xB6 }, /* PARTIAL DIFFERENTIAL */
1417 { 0x2206, 0xC6 }, /* INCREMENT */
1418 { 0x220F, 0xB8 }, /* N-ARY PRODUCT */
1419 { 0x2211, 0xB7 }, /* N-ARY SUMMATION */
1420 { 0x221A, 0xC3 }, /* SQUARE ROOT */
1421 { 0x221E, 0xB0 }, /* INFINITY */
1422 { 0x222B, 0xBA }, /* INTEGRAL */
1423 { 0x2248, 0xC5 }, /* ALMOST EQUAL TO */
1424 { 0x2260, 0xAD }, /* NOT EQUAL TO */
1425 { 0x2264, 0xB2 }, /* LESS-THAN OR EQUAL TO */
1426 { 0x2265, 0xB3 }, /* GREATER-THAN OR EQUAL TO */
1427 { 0x25CA, 0xD7 }, /* LOZENGE */
1428 { 0xF8FF, 0xF0 }, /* Apple logo */
1429 { 0xFB01, 0xDE }, /* LATIN SMALL LIGATURE FI */
1430 { 0xFB02, 0xDF }, /* LATIN SMALL LIGATURE FL */
1431 };
1432
1433 static const FcCharMap AppleRoman = {
1434 AppleRomanEnt,
1435 sizeof (AppleRomanEnt) / sizeof (AppleRomanEnt[0])
1436 };
1437
1438 static const FcCharEnt AdobeSymbolEnt[] = {
1439 { 0x0020, 0x20 }, /* SPACE # space */
1440 { 0x0021, 0x21 }, /* EXCLAMATION MARK # exclam */
1441 { 0x0023, 0x23 }, /* NUMBER SIGN # numbersign */
1442 { 0x0025, 0x25 }, /* PERCENT SIGN # percent */
1443 { 0x0026, 0x26 }, /* AMPERSAND # ampersand */
1444 { 0x0028, 0x28 }, /* LEFT PARENTHESIS # parenleft */
1445 { 0x0029, 0x29 }, /* RIGHT PARENTHESIS # parenright */
1446 { 0x002B, 0x2B }, /* PLUS SIGN # plus */
1447 { 0x002C, 0x2C }, /* COMMA # comma */
1448 { 0x002E, 0x2E }, /* FULL STOP # period */
1449 { 0x002F, 0x2F }, /* SOLIDUS # slash */
1450 { 0x0030, 0x30 }, /* DIGIT ZERO # zero */
1451 { 0x0031, 0x31 }, /* DIGIT ONE # one */
1452 { 0x0032, 0x32 }, /* DIGIT TWO # two */
1453 { 0x0033, 0x33 }, /* DIGIT THREE # three */
1454 { 0x0034, 0x34 }, /* DIGIT FOUR # four */
1455 { 0x0035, 0x35 }, /* DIGIT FIVE # five */
1456 { 0x0036, 0x36 }, /* DIGIT SIX # six */
1457 { 0x0037, 0x37 }, /* DIGIT SEVEN # seven */
1458 { 0x0038, 0x38 }, /* DIGIT EIGHT # eight */
1459 { 0x0039, 0x39 }, /* DIGIT NINE # nine */
1460 { 0x003A, 0x3A }, /* COLON # colon */
1461 { 0x003B, 0x3B }, /* SEMICOLON # semicolon */
1462 { 0x003C, 0x3C }, /* LESS-THAN SIGN # less */
1463 { 0x003D, 0x3D }, /* EQUALS SIGN # equal */
1464 { 0x003E, 0x3E }, /* GREATER-THAN SIGN # greater */
1465 { 0x003F, 0x3F }, /* QUESTION MARK # question */
1466 { 0x005B, 0x5B }, /* LEFT SQUARE BRACKET # bracketleft */
1467 { 0x005D, 0x5D }, /* RIGHT SQUARE BRACKET # bracketright */
1468 { 0x005F, 0x5F }, /* LOW LINE # underscore */
1469 { 0x007B, 0x7B }, /* LEFT CURLY BRACKET # braceleft */
1470 { 0x007C, 0x7C }, /* VERTICAL LINE # bar */
1471 { 0x007D, 0x7D }, /* RIGHT CURLY BRACKET # braceright */
1472 { 0x00A0, 0x20 }, /* NO-BREAK SPACE # space */
1473 { 0x00AC, 0xD8 }, /* NOT SIGN # logicalnot */
1474 { 0x00B0, 0xB0 }, /* DEGREE SIGN # degree */
1475 { 0x00B1, 0xB1 }, /* PLUS-MINUS SIGN # plusminus */
1476 { 0x00B5, 0x6D }, /* MICRO SIGN # mu */
1477 { 0x00D7, 0xB4 }, /* MULTIPLICATION SIGN # multiply */
1478 { 0x00F7, 0xB8 }, /* DIVISION SIGN # divide */
1479 { 0x0192, 0xA6 }, /* LATIN SMALL LETTER F WITH HOOK # florin */
1480 { 0x0391, 0x41 }, /* GREEK CAPITAL LETTER ALPHA # Alpha */
1481 { 0x0392, 0x42 }, /* GREEK CAPITAL LETTER BETA # Beta */
1482 { 0x0393, 0x47 }, /* GREEK CAPITAL LETTER GAMMA # Gamma */
1483 { 0x0394, 0x44 }, /* GREEK CAPITAL LETTER DELTA # Delta */
1484 { 0x0395, 0x45 }, /* GREEK CAPITAL LETTER EPSILON # Epsilon */
1485 { 0x0396, 0x5A }, /* GREEK CAPITAL LETTER ZETA # Zeta */
1486 { 0x0397, 0x48 }, /* GREEK CAPITAL LETTER ETA # Eta */
1487 { 0x0398, 0x51 }, /* GREEK CAPITAL LETTER THETA # Theta */
1488 { 0x0399, 0x49 }, /* GREEK CAPITAL LETTER IOTA # Iota */
1489 { 0x039A, 0x4B }, /* GREEK CAPITAL LETTER KAPPA # Kappa */
1490 { 0x039B, 0x4C }, /* GREEK CAPITAL LETTER LAMDA # Lambda */
1491 { 0x039C, 0x4D }, /* GREEK CAPITAL LETTER MU # Mu */
1492 { 0x039D, 0x4E }, /* GREEK CAPITAL LETTER NU # Nu */
1493 { 0x039E, 0x58 }, /* GREEK CAPITAL LETTER XI # Xi */
1494 { 0x039F, 0x4F }, /* GREEK CAPITAL LETTER OMICRON # Omicron */
1495 { 0x03A0, 0x50 }, /* GREEK CAPITAL LETTER PI # Pi */
1496 { 0x03A1, 0x52 }, /* GREEK CAPITAL LETTER RHO # Rho */
1497 { 0x03A3, 0x53 }, /* GREEK CAPITAL LETTER SIGMA # Sigma */
1498 { 0x03A4, 0x54 }, /* GREEK CAPITAL LETTER TAU # Tau */
1499 { 0x03A5, 0x55 }, /* GREEK CAPITAL LETTER UPSILON # Upsilon */
1500 { 0x03A6, 0x46 }, /* GREEK CAPITAL LETTER PHI # Phi */
1501 { 0x03A7, 0x43 }, /* GREEK CAPITAL LETTER CHI # Chi */
1502 { 0x03A8, 0x59 }, /* GREEK CAPITAL LETTER PSI # Psi */
1503 { 0x03A9, 0x57 }, /* GREEK CAPITAL LETTER OMEGA # Omega */
1504 { 0x03B1, 0x61 }, /* GREEK SMALL LETTER ALPHA # alpha */
1505 { 0x03B2, 0x62 }, /* GREEK SMALL LETTER BETA # beta */
1506 { 0x03B3, 0x67 }, /* GREEK SMALL LETTER GAMMA # gamma */
1507 { 0x03B4, 0x64 }, /* GREEK SMALL LETTER DELTA # delta */
1508 { 0x03B5, 0x65 }, /* GREEK SMALL LETTER EPSILON # epsilon */
1509 { 0x03B6, 0x7A }, /* GREEK SMALL LETTER ZETA # zeta */
1510 { 0x03B7, 0x68 }, /* GREEK SMALL LETTER ETA # eta */
1511 { 0x03B8, 0x71 }, /* GREEK SMALL LETTER THETA # theta */
1512 { 0x03B9, 0x69 }, /* GREEK SMALL LETTER IOTA # iota */
1513 { 0x03BA, 0x6B }, /* GREEK SMALL LETTER KAPPA # kappa */
1514 { 0x03BB, 0x6C }, /* GREEK SMALL LETTER LAMDA # lambda */
1515 { 0x03BC, 0x6D }, /* GREEK SMALL LETTER MU # mu */
1516 { 0x03BD, 0x6E }, /* GREEK SMALL LETTER NU # nu */
1517 { 0x03BE, 0x78 }, /* GREEK SMALL LETTER XI # xi */
1518 { 0x03BF, 0x6F }, /* GREEK SMALL LETTER OMICRON # omicron */
1519 { 0x03C0, 0x70 }, /* GREEK SMALL LETTER PI # pi */
1520 { 0x03C1, 0x72 }, /* GREEK SMALL LETTER RHO # rho */
1521 { 0x03C2, 0x56 }, /* GREEK SMALL LETTER FINAL SIGMA # sigma1 */
1522 { 0x03C3, 0x73 }, /* GREEK SMALL LETTER SIGMA # sigma */
1523 { 0x03C4, 0x74 }, /* GREEK SMALL LETTER TAU # tau */
1524 { 0x03C5, 0x75 }, /* GREEK SMALL LETTER UPSILON # upsilon */
1525 { 0x03C6, 0x66 }, /* GREEK SMALL LETTER PHI # phi */
1526 { 0x03C7, 0x63 }, /* GREEK SMALL LETTER CHI # chi */
1527 { 0x03C8, 0x79 }, /* GREEK SMALL LETTER PSI # psi */
1528 { 0x03C9, 0x77 }, /* GREEK SMALL LETTER OMEGA # omega */
1529 { 0x03D1, 0x4A }, /* GREEK THETA SYMBOL # theta1 */
1530 { 0x03D2, 0xA1 }, /* GREEK UPSILON WITH HOOK SYMBOL # Upsilon1 */
1531 { 0x03D5, 0x6A }, /* GREEK PHI SYMBOL # phi1 */
1532 { 0x03D6, 0x76 }, /* GREEK PI SYMBOL # omega1 */
1533 { 0x2022, 0xB7 }, /* BULLET # bullet */
1534 { 0x2026, 0xBC }, /* HORIZONTAL ELLIPSIS # ellipsis */
1535 { 0x2032, 0xA2 }, /* PRIME # minute */
1536 { 0x2033, 0xB2 }, /* DOUBLE PRIME # second */
1537 { 0x2044, 0xA4 }, /* FRACTION SLASH # fraction */
1538 { 0x20AC, 0xA0 }, /* EURO SIGN # Euro */
1539 { 0x2111, 0xC1 }, /* BLACK-LETTER CAPITAL I # Ifraktur */
1540 { 0x2118, 0xC3 }, /* SCRIPT CAPITAL P # weierstrass */
1541 { 0x211C, 0xC2 }, /* BLACK-LETTER CAPITAL R # Rfraktur */
1542 { 0x2126, 0x57 }, /* OHM SIGN # Omega */
1543 { 0x2135, 0xC0 }, /* ALEF SYMBOL # aleph */
1544 { 0x2190, 0xAC }, /* LEFTWARDS ARROW # arrowleft */
1545 { 0x2191, 0xAD }, /* UPWARDS ARROW # arrowup */
1546 { 0x2192, 0xAE }, /* RIGHTWARDS ARROW # arrowright */
1547 { 0x2193, 0xAF }, /* DOWNWARDS ARROW # arrowdown */
1548 { 0x2194, 0xAB }, /* LEFT RIGHT ARROW # arrowboth */
1549 { 0x21B5, 0xBF }, /* DOWNWARDS ARROW WITH CORNER LEFTWARDS # carriagereturn */
1550 { 0x21D0, 0xDC }, /* LEFTWARDS DOUBLE ARROW # arrowdblleft */
1551 { 0x21D1, 0xDD }, /* UPWARDS DOUBLE ARROW # arrowdblup */
1552 { 0x21D2, 0xDE }, /* RIGHTWARDS DOUBLE ARROW # arrowdblright */
1553 { 0x21D3, 0xDF }, /* DOWNWARDS DOUBLE ARROW # arrowdbldown */
1554 { 0x21D4, 0xDB }, /* LEFT RIGHT DOUBLE ARROW # arrowdblboth */
1555 { 0x2200, 0x22 }, /* FOR ALL # universal */
1556 { 0x2202, 0xB6 }, /* PARTIAL DIFFERENTIAL # partialdiff */
1557 { 0x2203, 0x24 }, /* THERE EXISTS # existential */
1558 { 0x2205, 0xC6 }, /* EMPTY SET # emptyset */
1559 { 0x2206, 0x44 }, /* INCREMENT # Delta */
1560 { 0x2207, 0xD1 }, /* NABLA # gradient */
1561 { 0x2208, 0xCE }, /* ELEMENT OF # element */
1562 { 0x2209, 0xCF }, /* NOT AN ELEMENT OF # notelement */
1563 { 0x220B, 0x27 }, /* CONTAINS AS MEMBER # suchthat */
1564 { 0x220F, 0xD5 }, /* N-ARY PRODUCT # product */
1565 { 0x2211, 0xE5 }, /* N-ARY SUMMATION # summation */
1566 { 0x2212, 0x2D }, /* MINUS SIGN # minus */
1567 { 0x2215, 0xA4 }, /* DIVISION SLASH # fraction */
1568 { 0x2217, 0x2A }, /* ASTERISK OPERATOR # asteriskmath */
1569 { 0x221A, 0xD6 }, /* SQUARE ROOT # radical */
1570 { 0x221D, 0xB5 }, /* PROPORTIONAL TO # proportional */
1571 { 0x221E, 0xA5 }, /* INFINITY # infinity */
1572 { 0x2220, 0xD0 }, /* ANGLE # angle */
1573 { 0x2227, 0xD9 }, /* LOGICAL AND # logicaland */
1574 { 0x2228, 0xDA }, /* LOGICAL OR # logicalor */
1575 { 0x2229, 0xC7 }, /* INTERSECTION # intersection */
1576 { 0x222A, 0xC8 }, /* UNION # union */
1577 { 0x222B, 0xF2 }, /* INTEGRAL # integral */
1578 { 0x2234, 0x5C }, /* THEREFORE # therefore */
1579 { 0x223C, 0x7E }, /* TILDE OPERATOR # similar */
1580 { 0x2245, 0x40 }, /* APPROXIMATELY EQUAL TO # congruent */
1581 { 0x2248, 0xBB }, /* ALMOST EQUAL TO # approxequal */
1582 { 0x2260, 0xB9 }, /* NOT EQUAL TO # notequal */
1583 { 0x2261, 0xBA }, /* IDENTICAL TO # equivalence */
1584 { 0x2264, 0xA3 }, /* LESS-THAN OR EQUAL TO # lessequal */
1585 { 0x2265, 0xB3 }, /* GREATER-THAN OR EQUAL TO # greaterequal */
1586 { 0x2282, 0xCC }, /* SUBSET OF # propersubset */
1587 { 0x2283, 0xC9 }, /* SUPERSET OF # propersuperset */
1588 { 0x2284, 0xCB }, /* NOT A SUBSET OF # notsubset */
1589 { 0x2286, 0xCD }, /* SUBSET OF OR EQUAL TO # reflexsubset */
1590 { 0x2287, 0xCA }, /* SUPERSET OF OR EQUAL TO # reflexsuperset */
1591 { 0x2295, 0xC5 }, /* CIRCLED PLUS # circleplus */
1592 { 0x2297, 0xC4 }, /* CIRCLED TIMES # circlemultiply */
1593 { 0x22A5, 0x5E }, /* UP TACK # perpendicular */
1594 { 0x22C5, 0xD7 }, /* DOT OPERATOR # dotmath */
1595 { 0x2320, 0xF3 }, /* TOP HALF INTEGRAL # integraltp */
1596 { 0x2321, 0xF5 }, /* BOTTOM HALF INTEGRAL # integralbt */
1597 { 0x2329, 0xE1 }, /* LEFT-POINTING ANGLE BRACKET # angleleft */
1598 { 0x232A, 0xF1 }, /* RIGHT-POINTING ANGLE BRACKET # angleright */
1599 { 0x25CA, 0xE0 }, /* LOZENGE # lozenge */
1600 { 0x2660, 0xAA }, /* BLACK SPADE SUIT # spade */
1601 { 0x2663, 0xA7 }, /* BLACK CLUB SUIT # club */
1602 { 0x2665, 0xA9 }, /* BLACK HEART SUIT # heart */
1603 { 0x2666, 0xA8 }, /* BLACK DIAMOND SUIT # diamond */
1604 { 0xF6D9, 0xD3 }, /* COPYRIGHT SIGN SERIF # copyrightserif (CUS) */
1605 { 0xF6DA, 0xD2 }, /* REGISTERED SIGN SERIF # registerserif (CUS) */
1606 { 0xF6DB, 0xD4 }, /* TRADE MARK SIGN SERIF # trademarkserif (CUS) */
1607 { 0xF8E5, 0x60 }, /* RADICAL EXTENDER # radicalex (CUS) */
1608 { 0xF8E6, 0xBD }, /* VERTICAL ARROW EXTENDER # arrowvertex (CUS) */
1609 { 0xF8E7, 0xBE }, /* HORIZONTAL ARROW EXTENDER # arrowhorizex (CUS) */
1610 { 0xF8E8, 0xE2 }, /* REGISTERED SIGN SANS SERIF # registersans (CUS) */
1611 { 0xF8E9, 0xE3 }, /* COPYRIGHT SIGN SANS SERIF # copyrightsans (CUS) */
1612 { 0xF8EA, 0xE4 }, /* TRADE MARK SIGN SANS SERIF # trademarksans (CUS) */
1613 { 0xF8EB, 0xE6 }, /* LEFT PAREN TOP # parenlefttp (CUS) */
1614 { 0xF8EC, 0xE7 }, /* LEFT PAREN EXTENDER # parenleftex (CUS) */
1615 { 0xF8ED, 0xE8 }, /* LEFT PAREN BOTTOM # parenleftbt (CUS) */
1616 { 0xF8EE, 0xE9 }, /* LEFT SQUARE BRACKET TOP # bracketlefttp (CUS) */
1617 { 0xF8EF, 0xEA }, /* LEFT SQUARE BRACKET EXTENDER # bracketleftex (CUS) */
1618 { 0xF8F0, 0xEB }, /* LEFT SQUARE BRACKET BOTTOM # bracketleftbt (CUS) */
1619 { 0xF8F1, 0xEC }, /* LEFT CURLY BRACKET TOP # bracelefttp (CUS) */
1620 { 0xF8F2, 0xED }, /* LEFT CURLY BRACKET MID # braceleftmid (CUS) */
1621 { 0xF8F3, 0xEE }, /* LEFT CURLY BRACKET BOTTOM # braceleftbt (CUS) */
1622 { 0xF8F4, 0xEF }, /* CURLY BRACKET EXTENDER # braceex (CUS) */
1623 { 0xF8F5, 0xF4 }, /* INTEGRAL EXTENDER # integralex (CUS) */
1624 { 0xF8F6, 0xF6 }, /* RIGHT PAREN TOP # parenrighttp (CUS) */
1625 { 0xF8F7, 0xF7 }, /* RIGHT PAREN EXTENDER # parenrightex (CUS) */
1626 { 0xF8F8, 0xF8 }, /* RIGHT PAREN BOTTOM # parenrightbt (CUS) */
1627 { 0xF8F9, 0xF9 }, /* RIGHT SQUARE BRACKET TOP # bracketrighttp (CUS) */
1628 { 0xF8FA, 0xFA }, /* RIGHT SQUARE BRACKET EXTENDER # bracketrightex (CUS) */
1629 { 0xF8FB, 0xFB }, /* RIGHT SQUARE BRACKET BOTTOM # bracketrightbt (CUS) */
1630 { 0xF8FC, 0xFC }, /* RIGHT CURLY BRACKET TOP # bracerighttp (CUS) */
1631 { 0xF8FD, 0xFD }, /* RIGHT CURLY BRACKET MID # bracerightmid (CUS) */
1632 { 0xF8FE, 0xFE }, /* RIGHT CURLY BRACKET BOTTOM # bracerightbt (CUS) */
1633 };
1634
1635 static const FcCharMap AdobeSymbol = {
1636 AdobeSymbolEnt,
1637 sizeof (AdobeSymbolEnt) / sizeof (AdobeSymbolEnt[0]),
1638 };
1639
1640 static const FcFontDecode fcFontDecoders[] = {
1641 { ft_encoding_unicode, 0, (1 << 21) - 1 },
1642 { ft_encoding_symbol, &AdobeSymbol, (1 << 16) - 1 },
1643 { ft_encoding_apple_roman, &AppleRoman, (1 << 16) - 1 },
1644 };
1645
1646 #define NUM_DECODE (sizeof (fcFontDecoders) / sizeof (fcFontDecoders[0]))
1647
1648 FcChar32
1649 FcFreeTypeUcs4ToPrivate (FcChar32 ucs4, const FcCharMap *map)
1650 {
1651 int low, high, mid;
1652 FcChar16 bmp;
1653
1654 low = 0;
1655 high = map->nent - 1;
1656 if (ucs4 < map->ent[low].bmp || map->ent[high].bmp < ucs4)
1657 return ~0;
1658 while (low <= high)
1659 {
1660 mid = (high + low) >> 1;
1661 bmp = map->ent[mid].bmp;
1662 if (ucs4 == bmp)
1663 return (FT_ULong) map->ent[mid].encode;
1664 if (ucs4 < bmp)
1665 high = mid - 1;
1666 else
1667 low = mid + 1;
1668 }
1669 return ~0;
1670 }
1671
1672 FcChar32
1673 FcFreeTypePrivateToUcs4 (FcChar32 private, const FcCharMap *map)
1674 {
1675 int i;
1676
1677 for (i = 0; i < map->nent; i++)
1678 if (map->ent[i].encode == private)
1679 return (FcChar32) map->ent[i].bmp;
1680 return ~0;
1681 }
1682
1683 const FcCharMap *
1684 FcFreeTypeGetPrivateMap (FT_Encoding encoding)
1685 {
1686 int i;
1687
1688 for (i = 0; i < NUM_DECODE; i++)
1689 if (fcFontDecoders[i].encoding == encoding)
1690 return fcFontDecoders[i].map;
1691 return 0;
1692 }
1693
1694 /*
1695 * Map a UCS4 glyph to a glyph index. Use all available encoding
1696 * tables to try and find one that works. This information is expected
1697 * to be cached by higher levels, so performance isn't critical
1698 */
1699
1700 FT_UInt
1701 FcFreeTypeCharIndex (FT_Face face, FcChar32 ucs4)
1702 {
1703 int initial, offset, decode;
1704 FT_UInt glyphindex;
1705 FcChar32 charcode;
1706
1707 initial = 0;
1708 /*
1709 * Find the current encoding
1710 */
1711 if (face->charmap)
1712 {
1713 for (; initial < NUM_DECODE; initial++)
1714 if (fcFontDecoders[initial].encoding == face->charmap->encoding)
1715 break;
1716 if (initial == NUM_DECODE)
1717 initial = 0;
1718 }
1719 /*
1720 * Check each encoding for the glyph, starting with the current one
1721 */
1722 for (offset = 0; offset < NUM_DECODE; offset++)
1723 {
1724 decode = (initial + offset) % NUM_DECODE;
1725 if (!face->charmap || face->charmap->encoding != fcFontDecoders[decode].encoding)
1726 if (FT_Select_Charmap (face, fcFontDecoders[decode].encoding) != 0)
1727 continue;
1728 if (fcFontDecoders[decode].map)
1729 {
1730 charcode = FcFreeTypeUcs4ToPrivate (ucs4, fcFontDecoders[decode].map);
1731 if (charcode == ~0)
1732 continue;
1733 }
1734 else
1735 charcode = ucs4;
1736 glyphindex = FT_Get_Char_Index (face, (FT_ULong) charcode);
1737 if (glyphindex)
1738 return glyphindex;
1739 }
1740 return 0;
1741 }
1742
1743 static FcBool
1744 FcFreeTypeCheckGlyph (FT_Face face, FcChar32 ucs4,
1745 FT_UInt glyph, FcBlanks *blanks)
1746 {
1747 FT_Int load_flags = FT_LOAD_NO_SCALE | FT_LOAD_NO_HINTING;
1748 FT_GlyphSlot slot;
1749
1750 /*
1751 * When using scalable fonts, only report those glyphs
1752 * which can be scaled; otherwise those fonts will
1753 * only be available at some sizes, and never when
1754 * transformed. Avoid this by simply reporting bitmap-only
1755 * glyphs as missing
1756 */
1757 if (face->face_flags & FT_FACE_FLAG_SCALABLE)
1758 load_flags |= FT_LOAD_NO_BITMAP;
1759
1760 if (FT_Load_Glyph (face, glyph, load_flags))
1761 return FcFalse;
1762
1763 slot = face->glyph;
1764 if (!glyph)
1765 return FcFalse;
1766
1767 switch (slot->format) {
1768 case ft_glyph_format_bitmap:
1769 /*
1770 * Bitmaps are assumed to be reasonable; if
1771 * this proves to be a rash assumption, this
1772 * code can be easily modified
1773 */
1774 return FcTrue;
1775 case ft_glyph_format_outline:
1776 /*
1777 * Glyphs with contours are always OK
1778 */
1779 if (slot->outline.n_contours != 0)
1780 return FcTrue;
1781 /*
1782 * Glyphs with no contours are only OK if
1783 * they're members of the Blanks set specified
1784 * in the configuration. If blanks isn't set,
1785 * then allow any glyph to be blank
1786 */
1787 if (!blanks || FcBlanksIsMember (blanks, ucs4))
1788 return FcTrue;
1789 /* fall through ... */
1790 default:
1791 break;
1792 }
1793 return FcFalse;
1794 }
1795
1796 FcCharSet *
1797 FcFreeTypeCharSet (FT_Face face, FcBlanks *blanks)
1798 {
1799 FcChar32 page, off, max, ucs4;
1800 #ifdef CHECK
1801 FcChar32 font_max = 0;
1802 #endif
1803 FcCharSet *fcs;
1804 FcCharLeaf *leaf;
1805 const FcCharMap *map;
1806 int o;
1807 int i;
1808 FT_UInt glyph;
1809
1810 fcs = FcCharSetCreate ();
1811 if (!fcs)
1812 goto bail0;
1813
1814 for (o = 0; o < NUM_DECODE; o++)
1815 {
1816 if (FT_Select_Charmap (face, fcFontDecoders[o].encoding) != 0)
1817 continue;
1818 map = fcFontDecoders[o].map;
1819 if (map)
1820 {
1821 /*
1822 * Non-Unicode tables are easy; there's a list of all possible
1823 * characters
1824 */
1825 for (i = 0; i < map->nent; i++)
1826 {
1827 ucs4 = map->ent[i].bmp;
1828 glyph = FT_Get_Char_Index (face, map->ent[i].encode);
1829 if (glyph && FcFreeTypeCheckGlyph (face, ucs4, glyph, blanks))
1830 {
1831 leaf = FcCharSetFindLeafCreate (fcs, ucs4);
1832 if (!leaf)
1833 goto bail1;
1834 leaf->map[(ucs4 & 0xff) >> 5] |= (1 << (ucs4 & 0x1f));
1835 #ifdef CHECK
1836 if (ucs4 > font_max)
1837 font_max = ucs4;
1838 #endif
1839 }
1840 }
1841 }
1842 else
1843 {
1844 FT_UInt gindex;
1845
1846 max = fcFontDecoders[o].max;
1847 /*
1848 * Find the first encoded character in the font
1849 */
1850 if (FT_Get_Char_Index (face, 0))
1851 {
1852 ucs4 = 0;
1853 gindex = 1;
1854 }
1855 else
1856 {
1857 ucs4 = FT_Get_Next_Char (face, 0, &gindex);
1858 if (!ucs4)
1859 gindex = 0;
1860 }
1861
1862 while (gindex)
1863 {
1864 page = ucs4 >> 8;
1865 leaf = 0;
1866 while ((ucs4 >> 8) == page)
1867 {
1868 glyph = FT_Get_Char_Index (face, ucs4);
1869 if (glyph && FcFreeTypeCheckGlyph (face, ucs4,
1870 glyph, blanks))
1871 {
1872 if (!leaf)
1873 {
1874 leaf = FcCharSetFindLeafCreate (fcs, ucs4);
1875 if (!leaf)
1876 goto bail1;
1877 }
1878 off = ucs4 & 0xff;
1879 leaf->map[off >> 5] |= (1 << (off & 0x1f));
1880 #ifdef CHECK
1881 if (ucs4 > font_max)
1882 font_max = ucs4;
1883 #endif
1884 }
1885 ucs4++;
1886 }
1887 ucs4 = FT_Get_Next_Char (face, ucs4 - 1, &gindex);
1888 if (!ucs4)
1889 gindex = 0;
1890 }
1891 #ifdef CHECK
1892 for (ucs4 = 0; ucs4 < 0x10000; ucs4++)
1893 {
1894 FcBool FT_Has, FC_Has;
1895
1896 FT_Has = FT_Get_Char_Index (face, ucs4) != 0;
1897 FC_Has = FcCharSetHasChar (fcs, ucs4);
1898 if (FT_Has != FC_Has)
1899 {
1900 printf ("0x%08x FT says %d FC says %d\n", ucs4, FT_Has, FC_Has);
1901 }
1902 }
1903 #endif
1904 }
1905 }
1906 #ifdef CHECK
1907 printf ("%d glyphs %d encoded\n", (int) face->num_glyphs, FcCharSetCount (fcs));
1908 for (ucs4 = 0; ucs4 <= font_max; ucs4++)
1909 {
1910 FcBool has_char = FcFreeTypeCharIndex (face, ucs4) != 0;
1911 FcBool has_bit = FcCharSetHasChar (fcs, ucs4);
1912
1913 if (has_char && !has_bit)
1914 printf ("Bitmap missing char 0x%x\n", ucs4);
1915 else if (!has_char && has_bit)
1916 printf ("Bitmap extra char 0x%x\n", ucs4);
1917 }
1918 #endif
1919 return fcs;
1920 bail1:
1921 FcCharSetDestroy (fcs);
1922 bail0:
1923 return 0;
1924 }
1925