Eliminate compiler warnings
[platform/upstream/fontconfig.git] / src / fccharset.c
1 /*
2  * $XFree86: xc/lib/fontconfig/src/fccharset.c,v 1.3 2002/02/18 22:29:28 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 static int
31 FcCharSetLevels (FcChar32 ucs4)
32 {
33     if (ucs4 <= 0xff)
34         return 1;
35     if (ucs4 <= 0xffff)
36         return 2;
37     if (ucs4 <= 0xffffff)
38         return 3;
39     return 4;
40 }
41
42 static FcBool
43 FcCharSetCheckLevel (FcCharSet *fcs, FcChar32 ucs4)
44 {
45     int level = FcCharSetLevels (ucs4);
46     
47     if (level <= fcs->levels)
48         return FcTrue;
49     while (fcs->levels < level)
50     {
51         if (fcs->levels == 0)
52         {
53             FcCharLeaf      *leaf;
54
55             leaf = (FcCharLeaf *) calloc (1, sizeof (FcCharLeaf));
56             if (!leaf)
57                 return FcFalse;
58             FcMemAlloc (FC_MEM_CHARNODE, sizeof (FcCharLeaf));
59             fcs->node.leaf = leaf;
60         }
61         else
62         {
63             FcCharBranch    *branch;
64     
65             branch = (FcCharBranch *) calloc (1, sizeof (FcCharBranch));
66             if (!branch)
67                 return FcFalse;
68             FcMemAlloc (FC_MEM_CHARNODE, sizeof (FcCharBranch));
69             branch->nodes[0] = fcs->node;
70             fcs->node.branch = branch;
71         }
72         ++fcs->levels;
73     }
74     return FcTrue;
75 }
76
77 FcCharSet *
78 FcCharSetCreate (void)
79 {
80     FcCharSet   *fcs;
81
82     fcs = (FcCharSet *) malloc (sizeof (FcCharSet));
83     if (!fcs)
84         return 0;
85     FcMemAlloc (FC_MEM_CHARSET, sizeof (FcCharSet));
86     fcs->ref = 1;
87     fcs->levels = 0;
88     fcs->node.leaf = 0;
89     fcs->constant = FcFalse;
90     return fcs;
91 }
92
93 FcCharSet *
94 FcCharSetNew (void);
95     
96 FcCharSet *
97 FcCharSetNew (void)
98 {
99     return FcCharSetCreate ();
100 }
101
102 static void
103 FcCharNodeDestroy (FcCharNode node, int level)
104 {
105     int i;
106
107     switch (level) {
108     case 0:
109         break;
110     case 1:
111         FcMemFree (FC_MEM_CHARNODE, sizeof (FcCharLeaf));
112         free (node.leaf);
113         break;
114     default:
115         for (i = 0; i < 256; i++)
116             if (node.branch->nodes[i].branch)
117                 FcCharNodeDestroy (node.branch->nodes[i], level - 1);
118         FcMemFree (FC_MEM_CHARNODE, sizeof (FcCharBranch));
119         free (node.branch);
120     }
121 }
122
123 void
124 FcCharSetDestroy (FcCharSet *fcs)
125 {
126     if (fcs->constant)
127         return;
128     if (--fcs->ref <= 0)
129     {
130         FcCharNodeDestroy (fcs->node, fcs->levels);
131         FcMemFree (FC_MEM_CHARSET, sizeof (FcCharSet));
132         free (fcs);
133     }
134 }
135
136 /*
137  * Locate the leaf containing the specified char, returning
138  * null if it doesn't exist
139  */
140
141 static FcCharLeaf *
142 FcCharSetFindLeaf (const FcCharSet *fcs, FcChar32 ucs4)
143 {
144     int                 l;
145     const FcCharNode    *prev;
146     FcCharNode          node;
147     FcChar32            i;
148
149     prev = &fcs->node;
150     l = fcs->levels;
151     while (--l > 0)
152     {
153         node = *prev;
154         if (!node.branch)
155             return 0;
156         i = (ucs4 >> (l << 3)) & 0xff;
157         prev = &node.branch->nodes[i];
158     }
159     return prev->leaf;
160 }
161
162 /*
163  * Locate the leaf containing the specified char, creating it
164  * if desired
165  */
166
167 static FcCharLeaf *
168 FcCharSetFindLeafCreate (FcCharSet *fcs, FcChar32 ucs4)
169 {
170     int             l;
171     FcCharNode      *prev, node;
172     FcChar32        i;
173
174     if (!FcCharSetCheckLevel (fcs, ucs4))
175         return FcFalse;
176     prev = &fcs->node;
177     l = fcs->levels;
178     while (--l > 0)
179     {
180         node = *prev;
181         if (!node.branch)
182         {
183             node.branch = calloc (1, sizeof (FcCharBranch));
184             if (!node.branch)
185                 return 0;
186             FcMemAlloc (FC_MEM_CHARNODE, sizeof (FcCharBranch));
187             *prev = node;
188         }
189         i = (ucs4 >> (l << 3)) & 0xff;
190         prev = &node.branch->nodes[i];
191     }
192     node = *prev;
193     if (!node.leaf)
194     {
195         node.leaf = calloc (1, sizeof (FcCharLeaf));
196         if (!node.leaf)
197             return 0;
198         FcMemAlloc (FC_MEM_CHARNODE, sizeof (FcCharLeaf));
199         *prev = node;
200     }
201     return node.leaf;
202 }
203
204 FcBool
205 FcCharSetAddChar (FcCharSet *fcs, FcChar32 ucs4)
206 {
207     FcCharLeaf  *leaf;
208     FcChar32    *b;
209     
210     if (fcs->constant)
211         return FcFalse;
212     leaf = FcCharSetFindLeafCreate (fcs, ucs4);
213     if (!leaf)
214         return FcFalse;
215     b = &leaf->map[(ucs4 & 0xff) >> 5];
216     *b |= (1 << (ucs4 & 0x1f));
217     return FcTrue;
218 }
219
220 /*
221  * An iterator for the leaves of a charset
222  */
223
224 typedef struct _fcCharSetIter {
225     FcCharLeaf      *leaf;
226     FcChar32        ucs4;
227 } FcCharSetIter;
228
229 /*
230  * Find the nearest leaf at or beyond *ucs4, return 0 if no leaf
231  * exists
232  */
233 static FcCharLeaf *
234 FcCharSetIterLeaf (FcCharNode node, int level, FcChar32 *ucs4)
235 {
236     if (level <= 1)
237         return node.leaf;
238     else if (!node.branch)
239         return 0;
240     else
241     {
242         int         shift = ((level - 1) << 3);
243         FcChar32    inc = 1 << shift;
244         FcChar32    mask = ~(inc - 1);
245         FcChar32    byte = (*ucs4 >> shift) & 0xff;
246         FcCharLeaf  *leaf;
247
248         for (;;)
249         {
250             leaf = FcCharSetIterLeaf (node.branch->nodes[byte], 
251                                       level - 1, 
252                                       ucs4);
253             if (leaf)
254                 break;
255             /* step to next branch, resetting lower indices */
256             *ucs4 = (*ucs4 & mask) + inc;
257             byte = (byte + 1) & 0xff;
258             if (byte == 0)
259                 break;
260         }
261         return leaf;
262     }
263 }
264
265 static void
266 FcCharSetIterSet (const FcCharSet *fcs, FcCharSetIter *iter)
267 {
268     if (FcCharSetLevels (iter->ucs4) > fcs->levels)
269         iter->leaf = 0;
270     else
271         iter->leaf = FcCharSetIterLeaf (fcs->node, fcs->levels,
272                                         &iter->ucs4);
273     if (!iter->leaf)
274         iter->ucs4 = ~0;
275 }
276
277 static void
278 FcCharSetIterNext (const FcCharSet *fcs, FcCharSetIter *iter)
279 {
280     iter->ucs4 += 0x100;
281     FcCharSetIterSet (fcs, iter);
282 }
283
284 static void
285 FcCharSetIterStart (const FcCharSet *fcs, FcCharSetIter *iter)
286 {
287     iter->ucs4 = 0;
288     FcCharSetIterSet (fcs, iter);
289 }
290
291 FcCharSet *
292 FcCharSetCopy (FcCharSet *src)
293 {
294     src->ref++;
295     return src;
296 }
297
298 FcBool
299 FcCharSetEqual (const FcCharSet *a, const FcCharSet *b)
300 {
301     FcCharSetIter   ai, bi;
302     int             i;
303     
304     if (a == b)
305         return FcTrue;
306     for (FcCharSetIterStart (a, &ai), FcCharSetIterStart (b, &bi);
307          ai.leaf && bi.leaf;
308          FcCharSetIterNext (a, &ai), FcCharSetIterNext (b, &bi))
309     {
310         if (ai.ucs4 != bi.ucs4)
311             return FcFalse;
312         for (i = 0; i < 256/32; i++)
313             if (ai.leaf->map[i] != bi.leaf->map[i])
314                 return FcFalse;
315     }
316     return ai.leaf == bi.leaf;
317 }
318
319 static FcBool
320 FcCharSetAddLeaf (FcCharSet     *fcs,
321                   FcChar32      ucs4,
322                   FcCharLeaf    *leaf)
323 {
324     FcCharLeaf   *new = FcCharSetFindLeafCreate (fcs, ucs4);
325     if (!new)
326         return FcFalse;
327     *new = *leaf;
328     return FcTrue;
329 }
330
331 static FcCharSet *
332 FcCharSetOperate (const FcCharSet   *a,
333                   const FcCharSet   *b,
334                   FcBool            (*overlap) (FcCharLeaf          *result,
335                                                 const FcCharLeaf    *al,
336                                                 const FcCharLeaf    *bl),
337                   FcBool        aonly,
338                   FcBool        bonly)
339 {
340     FcCharSet       *fcs;
341     FcCharSetIter   ai, bi;
342
343     fcs = FcCharSetCreate ();
344     if (!fcs)
345         goto bail0;
346     FcCharSetIterStart (a, &ai);
347     FcCharSetIterStart (b, &bi);
348     while ((ai.leaf || bonly) && (bi.leaf || aonly))
349     {
350         if (ai.ucs4 < bi.ucs4)
351         {
352             if (aonly)
353             {
354                 if (!FcCharSetAddLeaf (fcs, ai.ucs4, ai.leaf))
355                     goto bail1;
356                 FcCharSetIterNext (a, &ai);
357             }
358             else
359             {
360                 ai.ucs4 = bi.ucs4;
361                 FcCharSetIterSet (a, &ai);
362             }
363         }
364         else if (bi.ucs4 < ai.ucs4 )
365         {
366             if (bonly)
367             {
368                 if (!FcCharSetAddLeaf (fcs, bi.ucs4, bi.leaf))
369                     goto bail1;
370                 FcCharSetIterNext (b, &bi);
371             }
372             else
373             {
374                 bi.ucs4 = ai.ucs4;
375                 FcCharSetIterSet (b, &bi);
376             }
377         }
378         else
379         {
380             FcCharLeaf  leaf;
381
382             if ((*overlap) (&leaf, ai.leaf, bi.leaf))
383             {
384                 if (!FcCharSetAddLeaf (fcs, ai.ucs4, &leaf))
385                     goto bail1;
386             }
387             FcCharSetIterNext (a, &ai);
388             FcCharSetIterNext (b, &bi);
389         }
390     }
391     return fcs;
392 bail1:
393     FcCharSetDestroy (fcs);
394 bail0:
395     return 0;
396 }
397
398 static FcBool
399 FcCharSetIntersectLeaf (FcCharLeaf *result,
400                         const FcCharLeaf *al,
401                         const FcCharLeaf *bl)
402 {
403     int     i;
404     FcBool  nonempty = FcFalse;
405
406     for (i = 0; i < 256/32; i++)
407         if ((result->map[i] = al->map[i] & bl->map[i]))
408             nonempty = FcTrue;
409     return nonempty;
410 }
411
412 FcCharSet *
413 FcCharSetIntersect (const FcCharSet *a, const FcCharSet *b)
414 {
415     return FcCharSetOperate (a, b, FcCharSetIntersectLeaf, FcFalse, FcFalse);
416 }
417
418 static FcBool
419 FcCharSetUnionLeaf (FcCharLeaf *result,
420                     const FcCharLeaf *al,
421                     const FcCharLeaf *bl)
422 {
423     int i;
424
425     for (i = 0; i < 256/32; i++)
426         result->map[i] = al->map[i] | bl->map[i];
427     return FcTrue;
428 }
429
430 FcCharSet *
431 FcCharSetUnion (const FcCharSet *a, const FcCharSet *b)
432 {
433     return FcCharSetOperate (a, b, FcCharSetUnionLeaf, FcTrue, FcTrue);
434 }
435
436 static FcBool
437 FcCharSetSubtractLeaf (FcCharLeaf *result,
438                        const FcCharLeaf *al,
439                        const FcCharLeaf *bl)
440 {
441     int     i;
442     FcBool  nonempty = FcFalse;
443
444     for (i = 0; i < 256/32; i++)
445         if ((result->map[i] = al->map[i] & ~bl->map[i]))
446             nonempty = FcTrue;
447     return nonempty;
448 }
449
450 FcCharSet *
451 FcCharSetSubtract (const FcCharSet *a, const FcCharSet *b)
452 {
453     return FcCharSetOperate (a, b, FcCharSetSubtractLeaf, FcTrue, FcFalse);
454 }
455
456 FcBool
457 FcCharSetHasChar (const FcCharSet *fcs, FcChar32 ucs4)
458 {
459     FcCharLeaf  *leaf = FcCharSetFindLeaf (fcs, ucs4);
460     if (!leaf)
461         return FcFalse;
462     return (leaf->map[(ucs4 & 0xff) >> 5] & (1 << (ucs4 & 0x1f))) != 0;
463 }
464
465 static FcChar32
466 FcCharSetPopCount (FcChar32 c1)
467 {
468     /* hackmem 169 */
469     FcChar32    c2 = (c1 >> 1) & 033333333333;
470     c2 = c1 - c2 - ((c2 >> 1) & 033333333333);
471     return (((c2 + (c2 >> 3)) & 030707070707) % 077);
472 }
473
474 FcChar32
475 FcCharSetIntersectCount (const FcCharSet *a, const FcCharSet *b)
476 {
477     FcCharSetIter   ai, bi;
478     FcChar32        count = 0;
479     
480     FcCharSetIterStart (a, &ai);
481     FcCharSetIterStart (b, &bi);
482     while (ai.leaf && bi.leaf)
483     {
484         if (ai.ucs4 == bi.ucs4)
485         {
486             FcChar32    *am = ai.leaf->map;
487             FcChar32    *bm = bi.leaf->map;
488             int         i = 256/32;
489             while (i--)
490                 count += FcCharSetPopCount (*am++ & *bm++);
491             FcCharSetIterNext (a, &ai);
492         } 
493         else if (ai.ucs4 < bi.ucs4)
494         {
495             ai.ucs4 = bi.ucs4;
496             FcCharSetIterSet (a, &ai);
497         }
498         if (bi.ucs4 < ai.ucs4)
499         {
500             bi.ucs4 = ai.ucs4;
501             FcCharSetIterSet (b, &bi);
502         }
503     }
504     return count;
505 }
506
507 FcChar32
508 FcCharSetCount (const FcCharSet *a)
509 {
510     FcCharSetIter   ai;
511     FcChar32        count = 0;
512     
513     for (FcCharSetIterStart (a, &ai); ai.leaf; FcCharSetIterNext (a, &ai))
514     {
515         int                 i = 256/32;
516         FcChar32            *am = ai.leaf->map;
517
518         while (i--)
519             count += FcCharSetPopCount (*am++);
520     }
521     return count;
522 }
523
524 FcChar32
525 FcCharSetSubtractCount (const FcCharSet *a, const FcCharSet *b)
526 {
527     FcCharSetIter   ai, bi;
528     FcChar32        count = 0;
529     
530     FcCharSetIterStart (a, &ai);
531     FcCharSetIterStart (b, &bi);
532     while (ai.leaf)
533     {
534         if (ai.ucs4 <= bi.ucs4)
535         {
536             FcChar32    *am = ai.leaf->map;
537             int         i = 256/32;
538             if (ai.ucs4 == bi.ucs4)
539             {
540                 FcChar32        *bm = bi.leaf->map;;
541                 while (i--)
542                     count += FcCharSetPopCount (*am++ & ~*bm++);
543             }
544             else
545             {
546                 while (i--)
547                     count += FcCharSetPopCount (*am++);
548             }
549             FcCharSetIterNext (a, &ai);
550         }
551         else if (bi.leaf)
552         {
553             bi.ucs4 = ai.ucs4;
554             FcCharSetIterSet (b, &bi);
555         }
556     }
557     return count;
558 }
559
560 FcChar32
561 FcCharSetCoverage (const FcCharSet *a, FcChar32 page, FcChar32 *result)
562 {
563     FcCharSetIter   ai;
564
565     ai.ucs4 = page;
566     FcCharSetIterSet (a, &ai);
567     if (!ai.leaf)
568     {
569         memset (result, '\0', 256 / 8);
570         page = 0;
571     }
572     else
573     {
574         memcpy (result, ai.leaf->map, sizeof (ai.leaf->map));
575         FcCharSetIterNext (a, &ai);
576         page = ai.ucs4;
577     }
578     return page;
579 }
580
581 /*
582  * ASCII representation of charsets.
583  * 
584  * Each leaf is represented as 9 32-bit values, the code of the first character followed
585  * by 8 32 bit values for the leaf itself.  Each value is encoded as 5 ASCII characters,
586  * only 85 different values are used to avoid control characters as well as the other
587  * characters used to encode font names.  85**5 > 2^32 so things work out, but
588  * it's not exactly human readable output.  As a special case, 0 is encoded as a space
589  */
590
591 static unsigned char    charToValue[256] = {
592     /*     "" */ 0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff, 
593     /*   "\b" */ 0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff, 
594     /* "\020" */ 0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff, 
595     /* "\030" */ 0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff, 
596     /*    " " */ 0xff,  0x00,  0xff,  0x01,  0x02,  0x03,  0x04,  0xff, 
597     /*    "(" */ 0x05,  0x06,  0x07,  0x08,  0xff,  0xff,  0x09,  0x0a, 
598     /*    "0" */ 0x0b,  0x0c,  0x0d,  0x0e,  0x0f,  0x10,  0x11,  0x12, 
599     /*    "8" */ 0x13,  0x14,  0xff,  0x15,  0x16,  0xff,  0x17,  0x18, 
600     /*    "@" */ 0x19,  0x1a,  0x1b,  0x1c,  0x1d,  0x1e,  0x1f,  0x20, 
601     /*    "H" */ 0x21,  0x22,  0x23,  0x24,  0x25,  0x26,  0x27,  0x28, 
602     /*    "P" */ 0x29,  0x2a,  0x2b,  0x2c,  0x2d,  0x2e,  0x2f,  0x30, 
603     /*    "X" */ 0x31,  0x32,  0x33,  0x34,  0xff,  0x35,  0x36,  0xff, 
604     /*    "`" */ 0xff,  0x37,  0x38,  0x39,  0x3a,  0x3b,  0x3c,  0x3d, 
605     /*    "h" */ 0x3e,  0x3f,  0x40,  0x41,  0x42,  0x43,  0x44,  0x45, 
606     /*    "p" */ 0x46,  0x47,  0x48,  0x49,  0x4a,  0x4b,  0x4c,  0x4d, 
607     /*    "x" */ 0x4e,  0x4f,  0x50,  0x51,  0x52,  0x53,  0x54,  0xff, 
608     /* "\200" */ 0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff, 
609     /* "\210" */ 0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff, 
610     /* "\220" */ 0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff, 
611     /* "\230" */ 0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff, 
612     /* "\240" */ 0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff, 
613     /* "\250" */ 0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff, 
614     /* "\260" */ 0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff, 
615     /* "\270" */ 0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff, 
616     /* "\300" */ 0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff, 
617     /* "\310" */ 0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff, 
618     /* "\320" */ 0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff, 
619     /* "\330" */ 0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff, 
620     /* "\340" */ 0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff, 
621     /* "\350" */ 0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff, 
622     /* "\360" */ 0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff, 
623     /* "\370" */ 0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff, 
624 };
625
626 static FcChar8 valueToChar[0x55] = {
627     /* 0x00 */ '!', '#', '$', '%', '&', '(', ')', '*',
628     /* 0x08 */ '+', '.', '/', '0', '1', '2', '3', '4',
629     /* 0x10 */ '5', '6', '7', '8', '9', ';', '<', '>',
630     /* 0x18 */ '?', '@', 'A', 'B', 'C', 'D', 'E', 'F',
631     /* 0x20 */ 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
632     /* 0x28 */ 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
633     /* 0x30 */ 'W', 'X', 'Y', 'Z', '[', ']', '^', 'a',
634     /* 0x38 */ 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
635     /* 0x40 */ 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q',
636     /* 0x48 */ 'r', 's', 't', 'u', 'v', 'w', 'x', 'y',
637     /* 0x50 */ 'z', '{', '|', '}', '~',
638 };
639
640 static FcChar8 *
641 FcCharSetParseValue (FcChar8 *string, FcChar32 *value)
642 {
643     int         i;
644     FcChar32    v;
645     FcChar32    c;
646     
647     if (*string == ' ')
648     {
649         v = 0;
650         string++;
651     }
652     else
653     {
654         v = 0;
655         for (i = 0; i < 5; i++)
656         {
657             if (!(c = (FcChar32) (unsigned char) *string++))
658                 return 0;
659             c = charToValue[c];
660             if (c == 0xff)
661                 return 0;
662             v = v * 85 + c;
663         }
664     }
665     *value = v;
666     return string;
667 }
668
669 static FcBool
670 FcCharSetUnparseValue (FcStrBuf *buf, FcChar32 value)
671 {
672     int     i;
673     if (value == 0)
674     {
675         return FcStrBufChar (buf, ' ');
676     }
677     else
678     {
679         FcChar8 string[6];
680         FcChar8 *s = string + 5;
681         string[5] = '\0';
682         for (i = 0; i < 5; i++)
683         {
684             *--s = valueToChar[value % 85];
685             value /= 85;
686         }
687         for (i = 0; i < 5; i++)
688             if (!FcStrBufChar (buf, *s++))
689                 return FcFalse;
690     }
691     return FcTrue;
692 }
693
694 FcCharSet *
695 FcNameParseCharSet (FcChar8 *string)
696 {
697     FcCharSet   *c;
698     FcChar32    ucs4;
699     FcCharLeaf  *leaf;
700     int         i;
701
702     c = FcCharSetCreate ();
703     if (!c)
704         goto bail0;
705     while (*string)
706     {
707         string = FcCharSetParseValue (string, &ucs4);
708         if (!string)
709             goto bail1;
710         leaf = FcCharSetFindLeafCreate (c, ucs4);
711         if (!leaf)
712             goto bail1;
713         for (i = 0; i < 256/32; i++)
714         {
715             string = FcCharSetParseValue (string, &leaf->map[i]);
716             if (!string)
717                 goto bail1;
718         }
719     }
720     return c;
721 bail1:
722     FcCharSetDestroy (c);
723 bail0:
724     return 0;
725 }
726
727 FcBool
728 FcNameUnparseCharSet (FcStrBuf *buf, const FcCharSet *c)
729 {
730     FcCharSetIter   ci;
731     int             i;
732 #ifdef CHECK
733     int             len = buf->len;
734 #endif
735
736     for (FcCharSetIterStart (c, &ci);
737          ci.leaf;
738          FcCharSetIterNext (c, &ci))
739     {
740         if (!FcCharSetUnparseValue (buf, ci.ucs4))
741             return FcFalse;
742         for (i = 0; i < 256/32; i++)
743             if (!FcCharSetUnparseValue (buf, ci.leaf->map[i]))
744                 return FcFalse;
745     }
746 #ifdef CHECK
747     {
748         FcCharSet       *check;
749         FcChar32        missing;
750         FcCharSetIter   ci, checki;
751         
752         /* null terminate for parser */
753         FcStrBufChar (buf, '\0');
754         /* step back over null for life after test */
755         buf->len--;
756         check = FcNameParseCharSet (buf->buf + len);
757         FcCharSetIterStart (c, &ci);
758         FcCharSetIterStart (check, &checki);
759         while (ci.leaf || checki.leaf)
760         {
761             if (ci.ucs4 < checki.ucs4)
762             {
763                 printf ("Missing leaf node at 0x%x\n", ci.ucs4);
764                 FcCharSetIterNext (c, &ci);
765             }
766             else if (checki.ucs4 < ci.ucs4)
767             {
768                 printf ("Extra leaf node at 0x%x\n", checki.ucs4);
769                 FcCharSetIterNext (check, &checki);
770             }
771             else
772             {
773                 int         i = 256/32;
774                 FcChar32    *cm = ci.leaf->map;
775                 FcChar32    *checkm = checki.leaf->map;
776
777                 for (i = 0; i < 256; i += 32)
778                 {
779                     if (*cm != *checkm)
780                         printf ("Mismatching sets at 0x%08x: 0x%08x != 0x%08x\n",
781                                 ci.ucs4 + i, *cm, *checkm);
782                     cm++;
783                     checkm++;
784                 }
785                 FcCharSetIterNext (c, &ci);
786                 FcCharSetIterNext (check, &checki);
787             }
788         }
789         if ((missing = FcCharSetSubtractCount (c, check)))
790             printf ("%d missing in reparsed result\n", missing);
791         if ((missing = FcCharSetSubtractCount (check, c)))
792             printf ("%d extra in reparsed result\n", missing);
793         FcCharSetDestroy (check);
794     }
795 #endif
796     
797     return FcTrue;
798 }
799
800 #include <freetype/freetype.h>
801 #include <fontconfig/fcfreetype.h>
802
803 /*
804  * Figure out whether the available freetype has FT_Get_Next_Char
805  */
806
807 #if FREETYPE_MAJOR > 2
808 # define HAS_NEXT_CHAR
809 #else
810 # if FREETYPE_MAJOR == 2
811 #  if FREETYPE_MINOR > 0
812 #   define HAS_NEXT_CHAR
813 #  else
814 #   if FREETYPE_MINOR == 0
815 #    if FREETYPE_PATCH >= 8
816 #     define HAS_NEXT_CHAR
817 #    endif
818 #   endif
819 #  endif
820 # endif
821 #endif
822
823 /*
824  * For our purposes, this approximation is sufficient
825  */
826 #ifndef HAS_NEXT_CHAR
827 #define FT_Get_Next_Char(face, ucs4) ((ucs4) >= 0xffffff ? 0 : (ucs4) + 1)
828 #warning "No FT_Get_Next_Char"
829 #endif
830
831 typedef struct _FcCharEnt {
832     FcChar16        bmp;
833     unsigned char   encode;
834 } FcCharEnt;
835
836 typedef struct _FcCharMap {
837     const FcCharEnt *ent;
838     int             nent;
839 } FcCharMap;
840
841 typedef struct _FcFontDecode {
842     FT_Encoding     encoding;
843     const FcCharMap *map;
844     FcChar32        max;
845 } FcFontDecode;
846
847 static const FcCharEnt AppleRomanEnt[] = {
848     { 0x0020, 0x20 }, /* SPACE */
849     { 0x0021, 0x21 }, /* EXCLAMATION MARK */
850     { 0x0022, 0x22 }, /* QUOTATION MARK */
851     { 0x0023, 0x23 }, /* NUMBER SIGN */
852     { 0x0024, 0x24 }, /* DOLLAR SIGN */
853     { 0x0025, 0x25 }, /* PERCENT SIGN */
854     { 0x0026, 0x26 }, /* AMPERSAND */
855     { 0x0027, 0x27 }, /* APOSTROPHE */
856     { 0x0028, 0x28 }, /* LEFT PARENTHESIS */
857     { 0x0029, 0x29 }, /* RIGHT PARENTHESIS */
858     { 0x002A, 0x2A }, /* ASTERISK */
859     { 0x002B, 0x2B }, /* PLUS SIGN */
860     { 0x002C, 0x2C }, /* COMMA */
861     { 0x002D, 0x2D }, /* HYPHEN-MINUS */
862     { 0x002E, 0x2E }, /* FULL STOP */
863     { 0x002F, 0x2F }, /* SOLIDUS */
864     { 0x0030, 0x30 }, /* DIGIT ZERO */
865     { 0x0031, 0x31 }, /* DIGIT ONE */
866     { 0x0032, 0x32 }, /* DIGIT TWO */
867     { 0x0033, 0x33 }, /* DIGIT THREE */
868     { 0x0034, 0x34 }, /* DIGIT FOUR */
869     { 0x0035, 0x35 }, /* DIGIT FIVE */
870     { 0x0036, 0x36 }, /* DIGIT SIX */
871     { 0x0037, 0x37 }, /* DIGIT SEVEN */
872     { 0x0038, 0x38 }, /* DIGIT EIGHT */
873     { 0x0039, 0x39 }, /* DIGIT NINE */
874     { 0x003A, 0x3A }, /* COLON */
875     { 0x003B, 0x3B }, /* SEMICOLON */
876     { 0x003C, 0x3C }, /* LESS-THAN SIGN */
877     { 0x003D, 0x3D }, /* EQUALS SIGN */
878     { 0x003E, 0x3E }, /* GREATER-THAN SIGN */
879     { 0x003F, 0x3F }, /* QUESTION MARK */
880     { 0x0040, 0x40 }, /* COMMERCIAL AT */
881     { 0x0041, 0x41 }, /* LATIN CAPITAL LETTER A */
882     { 0x0042, 0x42 }, /* LATIN CAPITAL LETTER B */
883     { 0x0043, 0x43 }, /* LATIN CAPITAL LETTER C */
884     { 0x0044, 0x44 }, /* LATIN CAPITAL LETTER D */
885     { 0x0045, 0x45 }, /* LATIN CAPITAL LETTER E */
886     { 0x0046, 0x46 }, /* LATIN CAPITAL LETTER F */
887     { 0x0047, 0x47 }, /* LATIN CAPITAL LETTER G */
888     { 0x0048, 0x48 }, /* LATIN CAPITAL LETTER H */
889     { 0x0049, 0x49 }, /* LATIN CAPITAL LETTER I */
890     { 0x004A, 0x4A }, /* LATIN CAPITAL LETTER J */
891     { 0x004B, 0x4B }, /* LATIN CAPITAL LETTER K */
892     { 0x004C, 0x4C }, /* LATIN CAPITAL LETTER L */
893     { 0x004D, 0x4D }, /* LATIN CAPITAL LETTER M */
894     { 0x004E, 0x4E }, /* LATIN CAPITAL LETTER N */
895     { 0x004F, 0x4F }, /* LATIN CAPITAL LETTER O */
896     { 0x0050, 0x50 }, /* LATIN CAPITAL LETTER P */
897     { 0x0051, 0x51 }, /* LATIN CAPITAL LETTER Q */
898     { 0x0052, 0x52 }, /* LATIN CAPITAL LETTER R */
899     { 0x0053, 0x53 }, /* LATIN CAPITAL LETTER S */
900     { 0x0054, 0x54 }, /* LATIN CAPITAL LETTER T */
901     { 0x0055, 0x55 }, /* LATIN CAPITAL LETTER U */
902     { 0x0056, 0x56 }, /* LATIN CAPITAL LETTER V */
903     { 0x0057, 0x57 }, /* LATIN CAPITAL LETTER W */
904     { 0x0058, 0x58 }, /* LATIN CAPITAL LETTER X */
905     { 0x0059, 0x59 }, /* LATIN CAPITAL LETTER Y */
906     { 0x005A, 0x5A }, /* LATIN CAPITAL LETTER Z */
907     { 0x005B, 0x5B }, /* LEFT SQUARE BRACKET */
908     { 0x005C, 0x5C }, /* REVERSE SOLIDUS */
909     { 0x005D, 0x5D }, /* RIGHT SQUARE BRACKET */
910     { 0x005E, 0x5E }, /* CIRCUMFLEX ACCENT */
911     { 0x005F, 0x5F }, /* LOW LINE */
912     { 0x0060, 0x60 }, /* GRAVE ACCENT */
913     { 0x0061, 0x61 }, /* LATIN SMALL LETTER A */
914     { 0x0062, 0x62 }, /* LATIN SMALL LETTER B */
915     { 0x0063, 0x63 }, /* LATIN SMALL LETTER C */
916     { 0x0064, 0x64 }, /* LATIN SMALL LETTER D */
917     { 0x0065, 0x65 }, /* LATIN SMALL LETTER E */
918     { 0x0066, 0x66 }, /* LATIN SMALL LETTER F */
919     { 0x0067, 0x67 }, /* LATIN SMALL LETTER G */
920     { 0x0068, 0x68 }, /* LATIN SMALL LETTER H */
921     { 0x0069, 0x69 }, /* LATIN SMALL LETTER I */
922     { 0x006A, 0x6A }, /* LATIN SMALL LETTER J */
923     { 0x006B, 0x6B }, /* LATIN SMALL LETTER K */
924     { 0x006C, 0x6C }, /* LATIN SMALL LETTER L */
925     { 0x006D, 0x6D }, /* LATIN SMALL LETTER M */
926     { 0x006E, 0x6E }, /* LATIN SMALL LETTER N */
927     { 0x006F, 0x6F }, /* LATIN SMALL LETTER O */
928     { 0x0070, 0x70 }, /* LATIN SMALL LETTER P */
929     { 0x0071, 0x71 }, /* LATIN SMALL LETTER Q */
930     { 0x0072, 0x72 }, /* LATIN SMALL LETTER R */
931     { 0x0073, 0x73 }, /* LATIN SMALL LETTER S */
932     { 0x0074, 0x74 }, /* LATIN SMALL LETTER T */
933     { 0x0075, 0x75 }, /* LATIN SMALL LETTER U */
934     { 0x0076, 0x76 }, /* LATIN SMALL LETTER V */
935     { 0x0077, 0x77 }, /* LATIN SMALL LETTER W */
936     { 0x0078, 0x78 }, /* LATIN SMALL LETTER X */
937     { 0x0079, 0x79 }, /* LATIN SMALL LETTER Y */
938     { 0x007A, 0x7A }, /* LATIN SMALL LETTER Z */
939     { 0x007B, 0x7B }, /* LEFT CURLY BRACKET */
940     { 0x007C, 0x7C }, /* VERTICAL LINE */
941     { 0x007D, 0x7D }, /* RIGHT CURLY BRACKET */
942     { 0x007E, 0x7E }, /* TILDE */
943     { 0x00A0, 0xCA }, /* NO-BREAK SPACE */
944     { 0x00A1, 0xC1 }, /* INVERTED EXCLAMATION MARK */
945     { 0x00A2, 0xA2 }, /* CENT SIGN */
946     { 0x00A3, 0xA3 }, /* POUND SIGN */
947     { 0x00A5, 0xB4 }, /* YEN SIGN */
948     { 0x00A7, 0xA4 }, /* SECTION SIGN */
949     { 0x00A8, 0xAC }, /* DIAERESIS */
950     { 0x00A9, 0xA9 }, /* COPYRIGHT SIGN */
951     { 0x00AA, 0xBB }, /* FEMININE ORDINAL INDICATOR */
952     { 0x00AB, 0xC7 }, /* LEFT-POINTING DOUBLE ANGLE QUOTATION MARK */
953     { 0x00AC, 0xC2 }, /* NOT SIGN */
954     { 0x00AE, 0xA8 }, /* REGISTERED SIGN */
955     { 0x00AF, 0xF8 }, /* MACRON */
956     { 0x00B0, 0xA1 }, /* DEGREE SIGN */
957     { 0x00B1, 0xB1 }, /* PLUS-MINUS SIGN */
958     { 0x00B4, 0xAB }, /* ACUTE ACCENT */
959     { 0x00B5, 0xB5 }, /* MICRO SIGN */
960     { 0x00B6, 0xA6 }, /* PILCROW SIGN */
961     { 0x00B7, 0xE1 }, /* MIDDLE DOT */
962     { 0x00B8, 0xFC }, /* CEDILLA */
963     { 0x00BA, 0xBC }, /* MASCULINE ORDINAL INDICATOR */
964     { 0x00BB, 0xC8 }, /* RIGHT-POINTING DOUBLE ANGLE QUOTATION MARK */
965     { 0x00BF, 0xC0 }, /* INVERTED QUESTION MARK */
966     { 0x00C0, 0xCB }, /* LATIN CAPITAL LETTER A WITH GRAVE */
967     { 0x00C1, 0xE7 }, /* LATIN CAPITAL LETTER A WITH ACUTE */
968     { 0x00C2, 0xE5 }, /* LATIN CAPITAL LETTER A WITH CIRCUMFLEX */
969     { 0x00C3, 0xCC }, /* LATIN CAPITAL LETTER A WITH TILDE */
970     { 0x00C4, 0x80 }, /* LATIN CAPITAL LETTER A WITH DIAERESIS */
971     { 0x00C5, 0x81 }, /* LATIN CAPITAL LETTER A WITH RING ABOVE */
972     { 0x00C6, 0xAE }, /* LATIN CAPITAL LETTER AE */
973     { 0x00C7, 0x82 }, /* LATIN CAPITAL LETTER C WITH CEDILLA */
974     { 0x00C8, 0xE9 }, /* LATIN CAPITAL LETTER E WITH GRAVE */
975     { 0x00C9, 0x83 }, /* LATIN CAPITAL LETTER E WITH ACUTE */
976     { 0x00CA, 0xE6 }, /* LATIN CAPITAL LETTER E WITH CIRCUMFLEX */
977     { 0x00CB, 0xE8 }, /* LATIN CAPITAL LETTER E WITH DIAERESIS */
978     { 0x00CC, 0xED }, /* LATIN CAPITAL LETTER I WITH GRAVE */
979     { 0x00CD, 0xEA }, /* LATIN CAPITAL LETTER I WITH ACUTE */
980     { 0x00CE, 0xEB }, /* LATIN CAPITAL LETTER I WITH CIRCUMFLEX */
981     { 0x00CF, 0xEC }, /* LATIN CAPITAL LETTER I WITH DIAERESIS */
982     { 0x00D1, 0x84 }, /* LATIN CAPITAL LETTER N WITH TILDE */
983     { 0x00D2, 0xF1 }, /* LATIN CAPITAL LETTER O WITH GRAVE */
984     { 0x00D3, 0xEE }, /* LATIN CAPITAL LETTER O WITH ACUTE */
985     { 0x00D4, 0xEF }, /* LATIN CAPITAL LETTER O WITH CIRCUMFLEX */
986     { 0x00D5, 0xCD }, /* LATIN CAPITAL LETTER O WITH TILDE */
987     { 0x00D6, 0x85 }, /* LATIN CAPITAL LETTER O WITH DIAERESIS */
988     { 0x00D8, 0xAF }, /* LATIN CAPITAL LETTER O WITH STROKE */
989     { 0x00D9, 0xF4 }, /* LATIN CAPITAL LETTER U WITH GRAVE */
990     { 0x00DA, 0xF2 }, /* LATIN CAPITAL LETTER U WITH ACUTE */
991     { 0x00DB, 0xF3 }, /* LATIN CAPITAL LETTER U WITH CIRCUMFLEX */
992     { 0x00DC, 0x86 }, /* LATIN CAPITAL LETTER U WITH DIAERESIS */
993     { 0x00DF, 0xA7 }, /* LATIN SMALL LETTER SHARP S */
994     { 0x00E0, 0x88 }, /* LATIN SMALL LETTER A WITH GRAVE */
995     { 0x00E1, 0x87 }, /* LATIN SMALL LETTER A WITH ACUTE */
996     { 0x00E2, 0x89 }, /* LATIN SMALL LETTER A WITH CIRCUMFLEX */
997     { 0x00E3, 0x8B }, /* LATIN SMALL LETTER A WITH TILDE */
998     { 0x00E4, 0x8A }, /* LATIN SMALL LETTER A WITH DIAERESIS */
999     { 0x00E5, 0x8C }, /* LATIN SMALL LETTER A WITH RING ABOVE */
1000     { 0x00E6, 0xBE }, /* LATIN SMALL LETTER AE */
1001     { 0x00E7, 0x8D }, /* LATIN SMALL LETTER C WITH CEDILLA */
1002     { 0x00E8, 0x8F }, /* LATIN SMALL LETTER E WITH GRAVE */
1003     { 0x00E9, 0x8E }, /* LATIN SMALL LETTER E WITH ACUTE */
1004     { 0x00EA, 0x90 }, /* LATIN SMALL LETTER E WITH CIRCUMFLEX */
1005     { 0x00EB, 0x91 }, /* LATIN SMALL LETTER E WITH DIAERESIS */
1006     { 0x00EC, 0x93 }, /* LATIN SMALL LETTER I WITH GRAVE */
1007     { 0x00ED, 0x92 }, /* LATIN SMALL LETTER I WITH ACUTE */
1008     { 0x00EE, 0x94 }, /* LATIN SMALL LETTER I WITH CIRCUMFLEX */
1009     { 0x00EF, 0x95 }, /* LATIN SMALL LETTER I WITH DIAERESIS */
1010     { 0x00F1, 0x96 }, /* LATIN SMALL LETTER N WITH TILDE */
1011     { 0x00F2, 0x98 }, /* LATIN SMALL LETTER O WITH GRAVE */
1012     { 0x00F3, 0x97 }, /* LATIN SMALL LETTER O WITH ACUTE */
1013     { 0x00F4, 0x99 }, /* LATIN SMALL LETTER O WITH CIRCUMFLEX */
1014     { 0x00F5, 0x9B }, /* LATIN SMALL LETTER O WITH TILDE */
1015     { 0x00F6, 0x9A }, /* LATIN SMALL LETTER O WITH DIAERESIS */
1016     { 0x00F7, 0xD6 }, /* DIVISION SIGN */
1017     { 0x00F8, 0xBF }, /* LATIN SMALL LETTER O WITH STROKE */
1018     { 0x00F9, 0x9D }, /* LATIN SMALL LETTER U WITH GRAVE */
1019     { 0x00FA, 0x9C }, /* LATIN SMALL LETTER U WITH ACUTE */
1020     { 0x00FB, 0x9E }, /* LATIN SMALL LETTER U WITH CIRCUMFLEX */
1021     { 0x00FC, 0x9F }, /* LATIN SMALL LETTER U WITH DIAERESIS */
1022     { 0x00FF, 0xD8 }, /* LATIN SMALL LETTER Y WITH DIAERESIS */
1023     { 0x0131, 0xF5 }, /* LATIN SMALL LETTER DOTLESS I */
1024     { 0x0152, 0xCE }, /* LATIN CAPITAL LIGATURE OE */
1025     { 0x0153, 0xCF }, /* LATIN SMALL LIGATURE OE */
1026     { 0x0178, 0xD9 }, /* LATIN CAPITAL LETTER Y WITH DIAERESIS */
1027     { 0x0192, 0xC4 }, /* LATIN SMALL LETTER F WITH HOOK */
1028     { 0x02C6, 0xF6 }, /* MODIFIER LETTER CIRCUMFLEX ACCENT */
1029     { 0x02C7, 0xFF }, /* CARON */
1030     { 0x02D8, 0xF9 }, /* BREVE */
1031     { 0x02D9, 0xFA }, /* DOT ABOVE */
1032     { 0x02DA, 0xFB }, /* RING ABOVE */
1033     { 0x02DB, 0xFE }, /* OGONEK */
1034     { 0x02DC, 0xF7 }, /* SMALL TILDE */
1035     { 0x02DD, 0xFD }, /* DOUBLE ACUTE ACCENT */
1036     { 0x03A9, 0xBD }, /* GREEK CAPITAL LETTER OMEGA */
1037     { 0x03C0, 0xB9 }, /* GREEK SMALL LETTER PI */
1038     { 0x2013, 0xD0 }, /* EN DASH */
1039     { 0x2014, 0xD1 }, /* EM DASH */
1040     { 0x2018, 0xD4 }, /* LEFT SINGLE QUOTATION MARK */
1041     { 0x2019, 0xD5 }, /* RIGHT SINGLE QUOTATION MARK */
1042     { 0x201A, 0xE2 }, /* SINGLE LOW-9 QUOTATION MARK */
1043     { 0x201C, 0xD2 }, /* LEFT DOUBLE QUOTATION MARK */
1044     { 0x201D, 0xD3 }, /* RIGHT DOUBLE QUOTATION MARK */
1045     { 0x201E, 0xE3 }, /* DOUBLE LOW-9 QUOTATION MARK */
1046     { 0x2020, 0xA0 }, /* DAGGER */
1047     { 0x2021, 0xE0 }, /* DOUBLE DAGGER */
1048     { 0x2022, 0xA5 }, /* BULLET */
1049     { 0x2026, 0xC9 }, /* HORIZONTAL ELLIPSIS */
1050     { 0x2030, 0xE4 }, /* PER MILLE SIGN */
1051     { 0x2039, 0xDC }, /* SINGLE LEFT-POINTING ANGLE QUOTATION MARK */
1052     { 0x203A, 0xDD }, /* SINGLE RIGHT-POINTING ANGLE QUOTATION MARK */
1053     { 0x2044, 0xDA }, /* FRACTION SLASH */
1054     { 0x20AC, 0xDB }, /* EURO SIGN */
1055     { 0x2122, 0xAA }, /* TRADE MARK SIGN */
1056     { 0x2202, 0xB6 }, /* PARTIAL DIFFERENTIAL */
1057     { 0x2206, 0xC6 }, /* INCREMENT */
1058     { 0x220F, 0xB8 }, /* N-ARY PRODUCT */
1059     { 0x2211, 0xB7 }, /* N-ARY SUMMATION */
1060     { 0x221A, 0xC3 }, /* SQUARE ROOT */
1061     { 0x221E, 0xB0 }, /* INFINITY */
1062     { 0x222B, 0xBA }, /* INTEGRAL */
1063     { 0x2248, 0xC5 }, /* ALMOST EQUAL TO */
1064     { 0x2260, 0xAD }, /* NOT EQUAL TO */
1065     { 0x2264, 0xB2 }, /* LESS-THAN OR EQUAL TO */
1066     { 0x2265, 0xB3 }, /* GREATER-THAN OR EQUAL TO */
1067     { 0x25CA, 0xD7 }, /* LOZENGE */
1068     { 0xF8FF, 0xF0 }, /* Apple logo */
1069     { 0xFB01, 0xDE }, /* LATIN SMALL LIGATURE FI */
1070     { 0xFB02, 0xDF }, /* LATIN SMALL LIGATURE FL */
1071 };
1072
1073 static const FcCharMap AppleRoman = {
1074     AppleRomanEnt,
1075     sizeof (AppleRomanEnt) / sizeof (AppleRomanEnt[0])
1076 };
1077
1078 static const FcCharEnt AdobeSymbolEnt[] = {
1079     { 0x0020, 0x20 }, /* SPACE  # space */
1080     { 0x0021, 0x21 }, /* EXCLAMATION MARK       # exclam */
1081     { 0x0023, 0x23 }, /* NUMBER SIGN    # numbersign */
1082     { 0x0025, 0x25 }, /* PERCENT SIGN   # percent */
1083     { 0x0026, 0x26 }, /* AMPERSAND      # ampersand */
1084     { 0x0028, 0x28 }, /* LEFT PARENTHESIS       # parenleft */
1085     { 0x0029, 0x29 }, /* RIGHT PARENTHESIS      # parenright */
1086     { 0x002B, 0x2B }, /* PLUS SIGN      # plus */
1087     { 0x002C, 0x2C }, /* COMMA  # comma */
1088     { 0x002E, 0x2E }, /* FULL STOP      # period */
1089     { 0x002F, 0x2F }, /* SOLIDUS        # slash */
1090     { 0x0030, 0x30 }, /* DIGIT ZERO     # zero */
1091     { 0x0031, 0x31 }, /* DIGIT ONE      # one */
1092     { 0x0032, 0x32 }, /* DIGIT TWO      # two */
1093     { 0x0033, 0x33 }, /* DIGIT THREE    # three */
1094     { 0x0034, 0x34 }, /* DIGIT FOUR     # four */
1095     { 0x0035, 0x35 }, /* DIGIT FIVE     # five */
1096     { 0x0036, 0x36 }, /* DIGIT SIX      # six */
1097     { 0x0037, 0x37 }, /* DIGIT SEVEN    # seven */
1098     { 0x0038, 0x38 }, /* DIGIT EIGHT    # eight */
1099     { 0x0039, 0x39 }, /* DIGIT NINE     # nine */
1100     { 0x003A, 0x3A }, /* COLON  # colon */
1101     { 0x003B, 0x3B }, /* SEMICOLON      # semicolon */
1102     { 0x003C, 0x3C }, /* LESS-THAN SIGN # less */
1103     { 0x003D, 0x3D }, /* EQUALS SIGN    # equal */
1104     { 0x003E, 0x3E }, /* GREATER-THAN SIGN      # greater */
1105     { 0x003F, 0x3F }, /* QUESTION MARK  # question */
1106     { 0x005B, 0x5B }, /* LEFT SQUARE BRACKET    # bracketleft */
1107     { 0x005D, 0x5D }, /* RIGHT SQUARE BRACKET   # bracketright */
1108     { 0x005F, 0x5F }, /* LOW LINE       # underscore */
1109     { 0x007B, 0x7B }, /* LEFT CURLY BRACKET     # braceleft */
1110     { 0x007C, 0x7C }, /* VERTICAL LINE  # bar */
1111     { 0x007D, 0x7D }, /* RIGHT CURLY BRACKET    # braceright */
1112     { 0x00A0, 0x20 }, /* NO-BREAK SPACE # space */
1113     { 0x00AC, 0xD8 }, /* NOT SIGN       # logicalnot */
1114     { 0x00B0, 0xB0 }, /* DEGREE SIGN    # degree */
1115     { 0x00B1, 0xB1 }, /* PLUS-MINUS SIGN        # plusminus */
1116     { 0x00B5, 0x6D }, /* MICRO SIGN     # mu */
1117     { 0x00D7, 0xB4 }, /* MULTIPLICATION SIGN    # multiply */
1118     { 0x00F7, 0xB8 }, /* DIVISION SIGN  # divide */
1119     { 0x0192, 0xA6 }, /* LATIN SMALL LETTER F WITH HOOK # florin */
1120     { 0x0391, 0x41 }, /* GREEK CAPITAL LETTER ALPHA     # Alpha */
1121     { 0x0392, 0x42 }, /* GREEK CAPITAL LETTER BETA      # Beta */
1122     { 0x0393, 0x47 }, /* GREEK CAPITAL LETTER GAMMA     # Gamma */
1123     { 0x0394, 0x44 }, /* GREEK CAPITAL LETTER DELTA     # Delta */
1124     { 0x0395, 0x45 }, /* GREEK CAPITAL LETTER EPSILON   # Epsilon */
1125     { 0x0396, 0x5A }, /* GREEK CAPITAL LETTER ZETA      # Zeta */
1126     { 0x0397, 0x48 }, /* GREEK CAPITAL LETTER ETA       # Eta */
1127     { 0x0398, 0x51 }, /* GREEK CAPITAL LETTER THETA     # Theta */
1128     { 0x0399, 0x49 }, /* GREEK CAPITAL LETTER IOTA      # Iota */
1129     { 0x039A, 0x4B }, /* GREEK CAPITAL LETTER KAPPA     # Kappa */
1130     { 0x039B, 0x4C }, /* GREEK CAPITAL LETTER LAMDA     # Lambda */
1131     { 0x039C, 0x4D }, /* GREEK CAPITAL LETTER MU        # Mu */
1132     { 0x039D, 0x4E }, /* GREEK CAPITAL LETTER NU        # Nu */
1133     { 0x039E, 0x58 }, /* GREEK CAPITAL LETTER XI        # Xi */
1134     { 0x039F, 0x4F }, /* GREEK CAPITAL LETTER OMICRON   # Omicron */
1135     { 0x03A0, 0x50 }, /* GREEK CAPITAL LETTER PI        # Pi */
1136     { 0x03A1, 0x52 }, /* GREEK CAPITAL LETTER RHO       # Rho */
1137     { 0x03A3, 0x53 }, /* GREEK CAPITAL LETTER SIGMA     # Sigma */
1138     { 0x03A4, 0x54 }, /* GREEK CAPITAL LETTER TAU       # Tau */
1139     { 0x03A5, 0x55 }, /* GREEK CAPITAL LETTER UPSILON   # Upsilon */
1140     { 0x03A6, 0x46 }, /* GREEK CAPITAL LETTER PHI       # Phi */
1141     { 0x03A7, 0x43 }, /* GREEK CAPITAL LETTER CHI       # Chi */
1142     { 0x03A8, 0x59 }, /* GREEK CAPITAL LETTER PSI       # Psi */
1143     { 0x03A9, 0x57 }, /* GREEK CAPITAL LETTER OMEGA     # Omega */
1144     { 0x03B1, 0x61 }, /* GREEK SMALL LETTER ALPHA       # alpha */
1145     { 0x03B2, 0x62 }, /* GREEK SMALL LETTER BETA        # beta */
1146     { 0x03B3, 0x67 }, /* GREEK SMALL LETTER GAMMA       # gamma */
1147     { 0x03B4, 0x64 }, /* GREEK SMALL LETTER DELTA       # delta */
1148     { 0x03B5, 0x65 }, /* GREEK SMALL LETTER EPSILON     # epsilon */
1149     { 0x03B6, 0x7A }, /* GREEK SMALL LETTER ZETA        # zeta */
1150     { 0x03B7, 0x68 }, /* GREEK SMALL LETTER ETA # eta */
1151     { 0x03B8, 0x71 }, /* GREEK SMALL LETTER THETA       # theta */
1152     { 0x03B9, 0x69 }, /* GREEK SMALL LETTER IOTA        # iota */
1153     { 0x03BA, 0x6B }, /* GREEK SMALL LETTER KAPPA       # kappa */
1154     { 0x03BB, 0x6C }, /* GREEK SMALL LETTER LAMDA       # lambda */
1155     { 0x03BC, 0x6D }, /* GREEK SMALL LETTER MU  # mu */
1156     { 0x03BD, 0x6E }, /* GREEK SMALL LETTER NU  # nu */
1157     { 0x03BE, 0x78 }, /* GREEK SMALL LETTER XI  # xi */
1158     { 0x03BF, 0x6F }, /* GREEK SMALL LETTER OMICRON     # omicron */
1159     { 0x03C0, 0x70 }, /* GREEK SMALL LETTER PI  # pi */
1160     { 0x03C1, 0x72 }, /* GREEK SMALL LETTER RHO # rho */
1161     { 0x03C2, 0x56 }, /* GREEK SMALL LETTER FINAL SIGMA # sigma1 */
1162     { 0x03C3, 0x73 }, /* GREEK SMALL LETTER SIGMA       # sigma */
1163     { 0x03C4, 0x74 }, /* GREEK SMALL LETTER TAU # tau */
1164     { 0x03C5, 0x75 }, /* GREEK SMALL LETTER UPSILON     # upsilon */
1165     { 0x03C6, 0x66 }, /* GREEK SMALL LETTER PHI # phi */
1166     { 0x03C7, 0x63 }, /* GREEK SMALL LETTER CHI # chi */
1167     { 0x03C8, 0x79 }, /* GREEK SMALL LETTER PSI # psi */
1168     { 0x03C9, 0x77 }, /* GREEK SMALL LETTER OMEGA       # omega */
1169     { 0x03D1, 0x4A }, /* GREEK THETA SYMBOL     # theta1 */
1170     { 0x03D2, 0xA1 }, /* GREEK UPSILON WITH HOOK SYMBOL # Upsilon1 */
1171     { 0x03D5, 0x6A }, /* GREEK PHI SYMBOL       # phi1 */
1172     { 0x03D6, 0x76 }, /* GREEK PI SYMBOL        # omega1 */
1173     { 0x2022, 0xB7 }, /* BULLET # bullet */
1174     { 0x2026, 0xBC }, /* HORIZONTAL ELLIPSIS    # ellipsis */
1175     { 0x2032, 0xA2 }, /* PRIME  # minute */
1176     { 0x2033, 0xB2 }, /* DOUBLE PRIME   # second */
1177     { 0x2044, 0xA4 }, /* FRACTION SLASH # fraction */
1178     { 0x20AC, 0xA0 }, /* EURO SIGN      # Euro */
1179     { 0x2111, 0xC1 }, /* BLACK-LETTER CAPITAL I # Ifraktur */
1180     { 0x2118, 0xC3 }, /* SCRIPT CAPITAL P       # weierstrass */
1181     { 0x211C, 0xC2 }, /* BLACK-LETTER CAPITAL R # Rfraktur */
1182     { 0x2126, 0x57 }, /* OHM SIGN       # Omega */
1183     { 0x2135, 0xC0 }, /* ALEF SYMBOL    # aleph */
1184     { 0x2190, 0xAC }, /* LEFTWARDS ARROW        # arrowleft */
1185     { 0x2191, 0xAD }, /* UPWARDS ARROW  # arrowup */
1186     { 0x2192, 0xAE }, /* RIGHTWARDS ARROW       # arrowright */
1187     { 0x2193, 0xAF }, /* DOWNWARDS ARROW        # arrowdown */
1188     { 0x2194, 0xAB }, /* LEFT RIGHT ARROW       # arrowboth */
1189     { 0x21B5, 0xBF }, /* DOWNWARDS ARROW WITH CORNER LEFTWARDS  # carriagereturn */
1190     { 0x21D0, 0xDC }, /* LEFTWARDS DOUBLE ARROW # arrowdblleft */
1191     { 0x21D1, 0xDD }, /* UPWARDS DOUBLE ARROW   # arrowdblup */
1192     { 0x21D2, 0xDE }, /* RIGHTWARDS DOUBLE ARROW        # arrowdblright */
1193     { 0x21D3, 0xDF }, /* DOWNWARDS DOUBLE ARROW # arrowdbldown */
1194     { 0x21D4, 0xDB }, /* LEFT RIGHT DOUBLE ARROW        # arrowdblboth */
1195     { 0x2200, 0x22 }, /* FOR ALL        # universal */
1196     { 0x2202, 0xB6 }, /* PARTIAL DIFFERENTIAL   # partialdiff */
1197     { 0x2203, 0x24 }, /* THERE EXISTS   # existential */
1198     { 0x2205, 0xC6 }, /* EMPTY SET      # emptyset */
1199     { 0x2206, 0x44 }, /* INCREMENT      # Delta */
1200     { 0x2207, 0xD1 }, /* NABLA  # gradient */
1201     { 0x2208, 0xCE }, /* ELEMENT OF     # element */
1202     { 0x2209, 0xCF }, /* NOT AN ELEMENT OF      # notelement */
1203     { 0x220B, 0x27 }, /* CONTAINS AS MEMBER     # suchthat */
1204     { 0x220F, 0xD5 }, /* N-ARY PRODUCT  # product */
1205     { 0x2211, 0xE5 }, /* N-ARY SUMMATION        # summation */
1206     { 0x2212, 0x2D }, /* MINUS SIGN     # minus */
1207     { 0x2215, 0xA4 }, /* DIVISION SLASH # fraction */
1208     { 0x2217, 0x2A }, /* ASTERISK OPERATOR      # asteriskmath */
1209     { 0x221A, 0xD6 }, /* SQUARE ROOT    # radical */
1210     { 0x221D, 0xB5 }, /* PROPORTIONAL TO        # proportional */
1211     { 0x221E, 0xA5 }, /* INFINITY       # infinity */
1212     { 0x2220, 0xD0 }, /* ANGLE  # angle */
1213     { 0x2227, 0xD9 }, /* LOGICAL AND    # logicaland */
1214     { 0x2228, 0xDA }, /* LOGICAL OR     # logicalor */
1215     { 0x2229, 0xC7 }, /* INTERSECTION   # intersection */
1216     { 0x222A, 0xC8 }, /* UNION  # union */
1217     { 0x222B, 0xF2 }, /* INTEGRAL       # integral */
1218     { 0x2234, 0x5C }, /* THEREFORE      # therefore */
1219     { 0x223C, 0x7E }, /* TILDE OPERATOR # similar */
1220     { 0x2245, 0x40 }, /* APPROXIMATELY EQUAL TO # congruent */
1221     { 0x2248, 0xBB }, /* ALMOST EQUAL TO        # approxequal */
1222     { 0x2260, 0xB9 }, /* NOT EQUAL TO   # notequal */
1223     { 0x2261, 0xBA }, /* IDENTICAL TO   # equivalence */
1224     { 0x2264, 0xA3 }, /* LESS-THAN OR EQUAL TO  # lessequal */
1225     { 0x2265, 0xB3 }, /* GREATER-THAN OR EQUAL TO       # greaterequal */
1226     { 0x2282, 0xCC }, /* SUBSET OF      # propersubset */
1227     { 0x2283, 0xC9 }, /* SUPERSET OF    # propersuperset */
1228     { 0x2284, 0xCB }, /* NOT A SUBSET OF        # notsubset */
1229     { 0x2286, 0xCD }, /* SUBSET OF OR EQUAL TO  # reflexsubset */
1230     { 0x2287, 0xCA }, /* SUPERSET OF OR EQUAL TO        # reflexsuperset */
1231     { 0x2295, 0xC5 }, /* CIRCLED PLUS   # circleplus */
1232     { 0x2297, 0xC4 }, /* CIRCLED TIMES  # circlemultiply */
1233     { 0x22A5, 0x5E }, /* UP TACK        # perpendicular */
1234     { 0x22C5, 0xD7 }, /* DOT OPERATOR   # dotmath */
1235     { 0x2320, 0xF3 }, /* TOP HALF INTEGRAL      # integraltp */
1236     { 0x2321, 0xF5 }, /* BOTTOM HALF INTEGRAL   # integralbt */
1237     { 0x2329, 0xE1 }, /* LEFT-POINTING ANGLE BRACKET    # angleleft */
1238     { 0x232A, 0xF1 }, /* RIGHT-POINTING ANGLE BRACKET   # angleright */
1239     { 0x25CA, 0xE0 }, /* LOZENGE        # lozenge */
1240     { 0x2660, 0xAA }, /* BLACK SPADE SUIT       # spade */
1241     { 0x2663, 0xA7 }, /* BLACK CLUB SUIT        # club */
1242     { 0x2665, 0xA9 }, /* BLACK HEART SUIT       # heart */
1243     { 0x2666, 0xA8 }, /* BLACK DIAMOND SUIT     # diamond */
1244     { 0xF6D9, 0xD3 }, /* COPYRIGHT SIGN SERIF   # copyrightserif (CUS) */
1245     { 0xF6DA, 0xD2 }, /* REGISTERED SIGN SERIF  # registerserif (CUS) */
1246     { 0xF6DB, 0xD4 }, /* TRADE MARK SIGN SERIF  # trademarkserif (CUS) */
1247     { 0xF8E5, 0x60 }, /* RADICAL EXTENDER       # radicalex (CUS) */
1248     { 0xF8E6, 0xBD }, /* VERTICAL ARROW EXTENDER        # arrowvertex (CUS) */
1249     { 0xF8E7, 0xBE }, /* HORIZONTAL ARROW EXTENDER      # arrowhorizex (CUS) */
1250     { 0xF8E8, 0xE2 }, /* REGISTERED SIGN SANS SERIF     # registersans (CUS) */
1251     { 0xF8E9, 0xE3 }, /* COPYRIGHT SIGN SANS SERIF      # copyrightsans (CUS) */
1252     { 0xF8EA, 0xE4 }, /* TRADE MARK SIGN SANS SERIF     # trademarksans (CUS) */
1253     { 0xF8EB, 0xE6 }, /* LEFT PAREN TOP # parenlefttp (CUS) */
1254     { 0xF8EC, 0xE7 }, /* LEFT PAREN EXTENDER    # parenleftex (CUS) */
1255     { 0xF8ED, 0xE8 }, /* LEFT PAREN BOTTOM      # parenleftbt (CUS) */
1256     { 0xF8EE, 0xE9 }, /* LEFT SQUARE BRACKET TOP        # bracketlefttp (CUS) */
1257     { 0xF8EF, 0xEA }, /* LEFT SQUARE BRACKET EXTENDER   # bracketleftex (CUS) */
1258     { 0xF8F0, 0xEB }, /* LEFT SQUARE BRACKET BOTTOM     # bracketleftbt (CUS) */
1259     { 0xF8F1, 0xEC }, /* LEFT CURLY BRACKET TOP # bracelefttp (CUS) */
1260     { 0xF8F2, 0xED }, /* LEFT CURLY BRACKET MID # braceleftmid (CUS) */
1261     { 0xF8F3, 0xEE }, /* LEFT CURLY BRACKET BOTTOM      # braceleftbt (CUS) */
1262     { 0xF8F4, 0xEF }, /* CURLY BRACKET EXTENDER # braceex (CUS) */
1263     { 0xF8F5, 0xF4 }, /* INTEGRAL EXTENDER      # integralex (CUS) */
1264     { 0xF8F6, 0xF6 }, /* RIGHT PAREN TOP        # parenrighttp (CUS) */
1265     { 0xF8F7, 0xF7 }, /* RIGHT PAREN EXTENDER   # parenrightex (CUS) */
1266     { 0xF8F8, 0xF8 }, /* RIGHT PAREN BOTTOM     # parenrightbt (CUS) */
1267     { 0xF8F9, 0xF9 }, /* RIGHT SQUARE BRACKET TOP       # bracketrighttp (CUS) */
1268     { 0xF8FA, 0xFA }, /* RIGHT SQUARE BRACKET EXTENDER  # bracketrightex (CUS) */
1269     { 0xF8FB, 0xFB }, /* RIGHT SQUARE BRACKET BOTTOM    # bracketrightbt (CUS) */
1270     { 0xF8FC, 0xFC }, /* RIGHT CURLY BRACKET TOP        # bracerighttp (CUS) */
1271     { 0xF8FD, 0xFD }, /* RIGHT CURLY BRACKET MID        # bracerightmid (CUS) */
1272     { 0xF8FE, 0xFE }, /* RIGHT CURLY BRACKET BOTTOM     # bracerightbt (CUS) */
1273 };
1274
1275 static const FcCharMap AdobeSymbol = {
1276     AdobeSymbolEnt,
1277     sizeof (AdobeSymbolEnt) / sizeof (AdobeSymbolEnt[0]),
1278 };
1279     
1280 static const FcFontDecode fcFontDecoders[] = {
1281     { ft_encoding_unicode,      0,              (1 << 21) - 1 },
1282     { ft_encoding_symbol,       &AdobeSymbol,   (1 << 16) - 1 },
1283     { ft_encoding_apple_roman,  &AppleRoman,    (1 << 16) - 1 },
1284 };
1285
1286 #define NUM_DECODE  (sizeof (fcFontDecoders) / sizeof (fcFontDecoders[0]))
1287
1288 static FT_ULong
1289 FcFreeTypeMapChar (FcChar32 ucs4, const FcCharMap *map)
1290 {
1291     int         low, high, mid;
1292     FcChar16    bmp;
1293
1294     low = 0;
1295     high = map->nent - 1;
1296     if (ucs4 < map->ent[low].bmp || map->ent[high].bmp < ucs4)
1297         return ~0;
1298     while (high - low > 1)
1299     {
1300         mid = (high + low) >> 1;
1301         bmp = map->ent[mid].bmp;
1302         if (ucs4 == bmp)
1303             return (FT_ULong) map->ent[mid].encode;
1304         if (ucs4 < bmp)
1305             high = mid;
1306         else
1307             low = mid;
1308     }
1309     for (mid = low; mid <= high; mid++)
1310     {
1311         if (ucs4 == map->ent[mid].bmp)
1312             return (FT_ULong) map->ent[mid].encode;
1313     }
1314     return ~0;
1315 }
1316
1317 /*
1318  * Map a UCS4 glyph to a glyph index.  Use all available encoding
1319  * tables to try and find one that works.  This information is expected
1320  * to be cached by higher levels, so performance isn't critical
1321  */
1322
1323 FT_UInt
1324 FcFreeTypeCharIndex (FT_Face face, FcChar32 ucs4)
1325 {
1326     int             initial, offset, decode;
1327     FT_UInt         glyphindex;
1328     FT_ULong        charcode;
1329
1330     initial = 0;
1331     /*
1332      * Find the current encoding
1333      */
1334     if (face->charmap)
1335     {
1336         for (; initial < NUM_DECODE; initial++)
1337             if (fcFontDecoders[initial].encoding == face->charmap->encoding)
1338                 break;
1339         if (initial == NUM_DECODE)
1340             initial = 0;
1341     }
1342     /*
1343      * Check each encoding for the glyph, starting with the current one
1344      */
1345     for (offset = 0; offset < NUM_DECODE; offset++)
1346     {
1347         decode = (initial + offset) % NUM_DECODE;
1348         if (!face->charmap || face->charmap->encoding != fcFontDecoders[decode].encoding)
1349             if (FT_Select_Charmap (face, fcFontDecoders[decode].encoding) != 0)
1350                 continue;
1351         if (fcFontDecoders[decode].map)
1352         {
1353             charcode = FcFreeTypeMapChar (ucs4, fcFontDecoders[decode].map);
1354             if (charcode == ~0)
1355                 continue;
1356         }
1357         else
1358             charcode = (FT_ULong) ucs4;
1359         glyphindex = FT_Get_Char_Index (face, charcode);
1360         if (glyphindex)
1361             return glyphindex;
1362     }
1363     return 0;
1364 }
1365
1366 static FcBool
1367 FcFreeTypeCheckGlyph (FT_Face face, FcChar32 ucs4, 
1368                       FT_UInt glyph, FcBlanks *blanks)
1369 {
1370     FT_Int          load_flags = FT_LOAD_NO_SCALE | FT_LOAD_NO_HINTING;
1371     FT_GlyphSlot    slot;
1372     
1373     /*
1374      * When using scalable fonts, only report those glyphs
1375      * which can be scaled; otherwise those fonts will
1376      * only be available at some sizes, and never when
1377      * transformed.  Avoid this by simply reporting bitmap-only
1378      * glyphs as missing
1379      */
1380     if (face->face_flags & FT_FACE_FLAG_SCALABLE)
1381         load_flags |= FT_LOAD_NO_BITMAP;
1382     
1383     if (FT_Load_Glyph (face, glyph, load_flags))
1384         return FcFalse;
1385     
1386     slot = face->glyph;
1387     if (!glyph)
1388         return FcFalse;
1389     
1390     switch (slot->format) {
1391     case ft_glyph_format_bitmap:
1392         /*
1393          * Bitmaps are assumed to be reasonable; if
1394          * this proves to be a rash assumption, this
1395          * code can be easily modified
1396          */
1397         return FcTrue;
1398     case ft_glyph_format_outline:
1399         /*
1400          * Glyphs with contours are always OK
1401          */
1402         if (slot->outline.n_contours != 0)
1403             return FcTrue;
1404         /*
1405          * Glyphs with no contours are only OK if
1406          * they're members of the Blanks set specified
1407          * in the configuration.  If blanks isn't set,
1408          * then allow any glyph to be blank
1409          */
1410         if (!blanks || FcBlanksIsMember (blanks, ucs4))
1411             return FcTrue;
1412         /* fall through ... */
1413     default:
1414         break;
1415     }
1416     return FcFalse;
1417 }
1418
1419 FcCharSet *
1420 FcFreeTypeCharSet (FT_Face face, FcBlanks *blanks)
1421 {
1422     FcChar32        page, off, max, ucs4;
1423 #ifdef CHECK
1424     FcChar32        font_max = 0;
1425 #endif
1426     FcCharSet       *fcs;
1427     FcCharLeaf      *leaf;
1428     const FcCharMap *map;
1429     int             o;
1430     int             i;
1431     FT_UInt         glyph;
1432
1433     fcs = FcCharSetCreate ();
1434     if (!fcs)
1435         goto bail0;
1436     
1437     for (o = 0; o < NUM_DECODE; o++)
1438     {
1439         if (FT_Select_Charmap (face, fcFontDecoders[o].encoding) != 0)
1440             continue;
1441         map = fcFontDecoders[o].map;
1442         if (map)
1443         {
1444             /*
1445              * Non-Unicode tables are easy; there's a list of all possible
1446              * characters
1447              */
1448             for (i = 0; i < map->nent; i++)
1449             {
1450                 ucs4 = map->ent[i].bmp;
1451                 glyph = FT_Get_Char_Index (face, map->ent[i].encode);
1452                 if (glyph && FcFreeTypeCheckGlyph (face, ucs4, glyph, blanks))
1453                 {
1454                     leaf = FcCharSetFindLeafCreate (fcs, ucs4);
1455                     if (!leaf)
1456                         goto bail1;
1457                     leaf->map[(ucs4 & 0xff) >> 5] |= (1 << (ucs4 & 0x1f));
1458 #ifdef CHECK
1459                     if (ucs4 > font_max)
1460                         font_max = ucs4;
1461 #endif
1462                 }
1463             }
1464         }
1465         else
1466         {
1467             max = fcFontDecoders[o].max;
1468           
1469             /*
1470              * Find the first encoded character in the font
1471              */
1472             ucs4 = 0;
1473             if (FT_Get_Char_Index (face, 0))
1474                 ucs4 = 0;
1475             else
1476                 ucs4 = FT_Get_Next_Char (face, 0);
1477
1478             for (;;)
1479             {
1480                 page = ucs4 >> 8;
1481                 leaf = 0;
1482                 while ((ucs4 >> 8) == page)
1483                 {
1484                     glyph = FT_Get_Char_Index (face, ucs4);
1485                     if (glyph && FcFreeTypeCheckGlyph (face, ucs4, 
1486                                                        glyph, blanks))
1487                     {
1488                         if (!leaf)
1489                         {
1490                             leaf = FcCharSetFindLeafCreate (fcs, ucs4);
1491                             if (!leaf)
1492                                 goto bail1;
1493                         }
1494                         off = ucs4 & 0xff;
1495                         leaf->map[off >> 5] |= (1 << (off & 0x1f));
1496 #ifdef CHECK
1497                         if (ucs4 > font_max)
1498                             font_max = ucs4;
1499 #endif
1500                     }
1501                     ucs4++;
1502                 }
1503                 ucs4 = FT_Get_Next_Char (face, ucs4 - 1);
1504                 if (!ucs4)
1505                     break;
1506             }
1507 #ifdef CHECK
1508             for (ucs4 = 0; ucs4 < 0x10000; ucs4++)
1509             {
1510                 FcBool      FT_Has, FC_Has;
1511
1512                 FT_Has = FT_Get_Char_Index (face, ucs4) != 0;
1513                 FC_Has = FcCharSetHasChar (fcs, ucs4);
1514                 if (FT_Has != FC_Has)
1515                 {
1516                     printf ("0x%08x FT says %d FC says %d\n", ucs4, FT_Has, FC_Has);
1517                 }
1518             }
1519 #endif
1520         }
1521     }
1522 #ifdef CHECK
1523     printf ("%d glyphs %d encoded\n", (int) face->num_glyphs, FcCharSetCount (fcs));
1524     for (ucs4 = 0; ucs4 <= font_max; ucs4++)
1525     {
1526         FcBool  has_char = FcFreeTypeCharIndex (face, ucs4) != 0;
1527         FcBool  has_bit = FcCharSetHasChar (fcs, ucs4);
1528
1529         if (has_char && !has_bit)
1530             printf ("Bitmap missing char 0x%x\n", ucs4);
1531         else if (!has_char && has_bit)
1532             printf ("Bitmap extra char 0x%x\n", ucs4);
1533     }
1534 #endif
1535     return fcs;
1536 bail1:
1537     FcCharSetDestroy (fcs);
1538 bail0:
1539     return 0;
1540 }
1541