]> git.wh0rd.org - fontconfig.git/blob - src/fcfreetype.c
Move foundry detection data into fcfreetype.c (which is getting rather
[fontconfig.git] / src / fcfreetype.c
1 /*
2 * $RCSId: xc/lib/fontconfig/src/fcfreetype.c,v 1.11 2002/08/31 22:17:32 keithp Exp $
3 *
4 * Copyright © 2001 Keith Packard
5 *
6 * Permission to use, copy, modify, distribute, and sell this software and its
7 * documentation for any purpose is hereby granted without fee, provided that
8 * the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of 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 /*
26 Copyright © 2002-2003 by Juliusz Chroboczek
27
28 Permission is hereby granted, free of charge, to any person obtaining a copy
29 of this software and associated documentation files (the "Software"), to deal
30 in the Software without restriction, including without limitation the rights
31 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
32 copies of the Software, and to permit persons to whom the Software is
33 furnished to do so, subject to the following conditions:
34
35 The above copyright notice and this permission notice shall be included in
36 all copies or substantial portions of the Software.
37
38 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
39 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
40 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
41 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
42 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
43 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
44 THE SOFTWARE.
45 */
46
47 #include <stdlib.h>
48 #include <stdio.h>
49 #include <string.h>
50 #include "fcint.h"
51 #include <freetype/freetype.h>
52 #include <freetype/internal/ftobjs.h>
53 #include <freetype/tttables.h>
54 #include <freetype/ftsnames.h>
55 #include <freetype/ttnameid.h>
56 #include <freetype/t1tables.h>
57
58 /*
59 * Keep Han languages separated by eliminating languages
60 * that the codePageRange bits says aren't supported
61 */
62
63 static const struct {
64 int bit;
65 const FcChar8 *lang;
66 } FcCodePageRange[] = {
67 { 17, (const FcChar8 *) "ja" },
68 { 18, (const FcChar8 *) "zh-cn" },
69 { 19, (const FcChar8 *) "ko" },
70 { 20, (const FcChar8 *) "zh-tw" },
71 };
72
73 #define NUM_CODE_PAGE_RANGE (sizeof FcCodePageRange / sizeof FcCodePageRange[0])
74
75 FcBool
76 FcFreeTypeIsExclusiveLang (const FcChar8 *lang)
77 {
78 int i;
79
80 for (i = 0; i < NUM_CODE_PAGE_RANGE; i++)
81 {
82 if (FcLangCompare (lang, FcCodePageRange[i].lang) != FcLangDifferentLang)
83 return FcTrue;
84 }
85 return FcFalse;
86 }
87
88 #define FC_NAME_PRIO_LANG 0x0f00
89 #define FC_NAME_PRIO_LANG_ENGLISH 0x0200
90 #define FC_NAME_PRIO_LANG_LATIN 0x0100
91 #define FC_NAME_PRIO_LANG_NONE 0x0000
92
93 #define FC_NAME_PRIO_ENC 0x00f0
94 #define FC_NAME_PRIO_ENC_UNICODE 0x0010
95 #define FC_NAME_PRIO_ENC_NONE 0x0000
96
97 #define FC_NAME_PRIO_NAME 0x000f
98 #define FC_NAME_PRIO_NAME_FAMILY 0x0002
99 #define FC_NAME_PRIO_NAME_PS 0x0001
100 #define FC_NAME_PRIO_NAME_NONE 0x0000
101
102 static FcBool
103 FcUcs4IsLatin (FcChar32 ucs4)
104 {
105 FcChar32 page = ucs4 >> 8;
106
107 if (page <= 2)
108 return FcTrue;
109 if (page == 0x1e)
110 return FcTrue;
111 if (0x20 <= page && page <= 0x23)
112 return FcTrue;
113 if (page == 0xfb)
114 return FcTrue;
115 if (page == 0xff)
116 return FcTrue;
117 return FcFalse;
118 }
119
120 static FcBool
121 FcUtf8IsLatin (FcChar8 *str, int len)
122 {
123 while (len)
124 {
125 FcChar32 ucs4;
126 int clen = FcUtf8ToUcs4 (str, &ucs4, len);
127 if (clen <= 0)
128 return FcFalse;
129 if (!FcUcs4IsLatin (ucs4))
130 return FcFalse;
131 len -= clen;
132 str += clen;
133 }
134 return FcTrue;
135 }
136
137 /* Order is significant. For example, some B&H fonts are hinted by
138 URW++, and both strings appear in the notice. */
139
140 static const struct {
141 const FcChar8 *notice;
142 const FcChar8 *foundry;
143 } FcNoticeFoundries[] = {
144 { (const FcChar8*) "Bigelow", (const FcChar8 *) "b&h" },
145 { (const FcChar8*) "Adobe", (const FcChar8 *) "adobe" },
146 { (const FcChar8*) "Bitstream", (const FcChar8 *) "bitsteam" },
147 { (const FcChar8*) "Monotype", (const FcChar8 *) "monotype" },
148 { (const FcChar8*) "Linotype", (const FcChar8 *) "linotype" },
149 { (const FcChar8*) "LINOTYPE-HELL", (const FcChar8 *) "linotype" },
150 { (const FcChar8*) "IBM", (const FcChar8 *) "ibm" },
151 { (const FcChar8*) "URW", (const FcChar8 *) "urw" },
152 { (const FcChar8*) "International Typeface Corporation",
153 (const FcChar8 *) "itc" },
154 { (const FcChar8*) "Tiro Typeworks",(const FcChar8 *) "tiro" },
155 { (const FcChar8*) "XFree86", (const FcChar8 *) "xfree86" },
156 { (const FcChar8*) "Microsoft", (const FcChar8 *) "microsoft" },
157 { (const FcChar8*) "Omega", (const FcChar8 *) "omega" },
158 { (const FcChar8*) "Font21", (const FcChar8 *) "hwan" },
159 { (const FcChar8*) "HanYang System",(const FcChar8 *) "hanyang" }
160 };
161
162 #define NUM_NOTICE_FOUNDRIES (sizeof (FcNoticeFoundries) / sizeof (FcNoticeFoundries[0]))
163
164 static const FcChar8 *
165 FcNoticeFoundry(const char *notice)
166 {
167 int i;
168
169 if (notice)
170 for(i = 0; i < NUM_NOTICE_FOUNDRIES; i++)
171 if (strstr ((const char *) notice, (const char *) FcNoticeFoundries[i].notice))
172 return FcNoticeFoundries[i].foundry;
173 return 0;
174 }
175
176 static FcBool
177 FcVendorMatch(const char *vendor, const char *vendor_string)
178 {
179 /* vendor is not necessarily NUL-terminated. */
180 int i, len;
181
182 len = strlen(vendor_string);
183 if (memcmp(vendor, vendor_string, len) != 0)
184 return FcFalse;
185 for (i = len; i < 4; i++)
186 if (vendor[i] != ' ' && vendor[i] != '\0')
187 return FcFalse;
188 return FcTrue;
189 }
190
191 /* This table is partly taken from ttmkfdir by Joerg Pommnitz. */
192
193 /* It should not contain useless entries (such as UNKN) nor duplicate
194 entries for padding both with spaces and NULs. */
195
196 static const struct {
197 const FcChar8 *vendor;
198 const FcChar8 *foundry;
199 } FcVendorFoundries[] = {
200 { (const FcChar8*) "ADBE", (const FcChar8 *) "adobe"},
201 { (const FcChar8*) "AGFA", (const FcChar8 *) "agfa"},
202 { (const FcChar8*) "ALTS", (const FcChar8 *) "altsys"},
203 { (const FcChar8*) "APPL", (const FcChar8 *) "apple"},
204 { (const FcChar8*) "ARPH", (const FcChar8 *) "arphic"},
205 { (const FcChar8*) "ATEC", (const FcChar8 *) "alltype"},
206 { (const FcChar8*) "B&H", (const FcChar8 *) "b&h"},
207 { (const FcChar8*) "BITS", (const FcChar8 *) "bitstream"},
208 { (const FcChar8*) "CANO", (const FcChar8 *) "cannon"},
209 { (const FcChar8*) "DYNA", (const FcChar8 *) "dynalab"},
210 { (const FcChar8*) "EPSN", (const FcChar8 *) "epson"},
211 { (const FcChar8*) "FJ", (const FcChar8 *) "fujitsu"},
212 { (const FcChar8*) "IBM", (const FcChar8 *) "ibm"},
213 { (const FcChar8*) "ITC", (const FcChar8 *) "itc"},
214 { (const FcChar8*) "IMPR", (const FcChar8 *) "impress"},
215 { (const FcChar8*) "LARA", (const FcChar8 *) "larabiefonts"},
216 { (const FcChar8*) "LEAF", (const FcChar8 *) "interleaf"},
217 { (const FcChar8*) "LETR", (const FcChar8 *) "letraset"},
218 { (const FcChar8*) "LINO", (const FcChar8 *) "linotype"},
219 { (const FcChar8*) "MACR", (const FcChar8 *) "macromedia"},
220 { (const FcChar8*) "MONO", (const FcChar8 *) "monotype"},
221 { (const FcChar8*) "MS", (const FcChar8 *) "microsoft"},
222 { (const FcChar8*) "MT", (const FcChar8 *) "monotype"},
223 { (const FcChar8*) "NEC", (const FcChar8 *) "nec"},
224 { (const FcChar8*) "PARA", (const FcChar8 *) "paratype"},
225 { (const FcChar8*) "QMSI", (const FcChar8 *) "qms"},
226 { (const FcChar8*) "RICO", (const FcChar8 *) "ricoh"},
227 { (const FcChar8*) "URW", (const FcChar8 *) "urw"},
228 { (const FcChar8*) "Y&Y", (const FcChar8 *) "y&y"}
229 };
230
231 #define NUM_VENDOR_FOUNDRIES (sizeof (FcVendorFoundries) / sizeof (FcVendorFoundries[0]))
232
233 static const FcChar8 *
234 FcVendorFoundry(const char *vendor)
235 {
236 int i;
237
238 if (vendor)
239 for(i = 0; i < NUM_VENDOR_FOUNDRIES; i++)
240 if (FcVendorMatch (vendor, FcVendorFoundries[i].vendor))
241 return FcVendorFoundries[i].foundry;
242 return 0;
243 }
244
245
246 FcPattern *
247 FcFreeTypeQuery (const FcChar8 *file,
248 int id,
249 FcBlanks *blanks,
250 int *count)
251 {
252 FT_Face face;
253 FcPattern *pat;
254 int slant;
255 int weight;
256 int width = -1;
257 int i;
258 FcCharSet *cs;
259 FcLangSet *ls;
260 FT_Library ftLibrary;
261 FcChar8 *family = 0;
262 FcChar8 *style = 0;
263 const FcChar8 *foundry = 0;
264 int spacing;
265 TT_OS2 *os2;
266 PS_FontInfoRec psfontinfo;
267 TT_Header *head;
268 const FcChar8 *exclusiveLang = 0;
269 FT_SfntName sname;
270 FT_UInt snamei, snamec;
271 FcBool family_allocated = FcFalse;
272 FcBool style_allocated = FcFalse;
273 int family_prio = 0;
274 int style_prio = 0;
275
276 if (FT_Init_FreeType (&ftLibrary))
277 return 0;
278
279 if (FT_New_Face (ftLibrary, (char *) file, id, &face))
280 goto bail;
281
282 *count = face->num_faces;
283
284 pat = FcPatternCreate ();
285 if (!pat)
286 goto bail0;
287
288 if (!FcPatternAddBool (pat, FC_OUTLINE,
289 (face->face_flags & FT_FACE_FLAG_SCALABLE) != 0))
290 goto bail1;
291
292 if (!FcPatternAddBool (pat, FC_SCALABLE,
293 (face->face_flags & FT_FACE_FLAG_SCALABLE) != 0))
294 goto bail1;
295
296
297 slant = FC_SLANT_ROMAN;
298 if (face->style_flags & FT_STYLE_FLAG_ITALIC)
299 slant = FC_SLANT_ITALIC;
300
301
302 weight = FC_WEIGHT_MEDIUM;
303 if (face->style_flags & FT_STYLE_FLAG_BOLD)
304 weight = FC_WEIGHT_BOLD;
305
306 /*
307 * Get the OS/2 table
308 */
309 os2 = (TT_OS2 *) FT_Get_Sfnt_Table (face, ft_sfnt_os2);
310
311 /*
312 * Look first in the OS/2 table for the foundry, if
313 * not found here, the various notices will be searched for
314 * that information, either from the sfnt name tables or
315 * the Postscript FontInfo dictionary
316 */
317
318 if (os2 && os2->version >= 0x0001 && os2->version != 0xffff)
319 foundry = FcVendorFoundry(os2->achVendID);
320
321 /*
322 * Grub through the name table looking for family
323 * and style names. FreeType makes quite a hash
324 * of them
325 */
326 snamec = FT_Get_Sfnt_Name_Count (face);
327 for (snamei = 0; snamei < snamec; snamei++)
328 {
329 FcChar8 *utf8;
330 int len;
331 int wchar;
332 FcChar8 *src;
333 int src_len;
334 FcChar8 *u8;
335 FcChar32 ucs4;
336 int ilen, olen;
337 int prio = 0;
338
339 const FcCharMap *map;
340 enum {
341 FcNameEncodingUtf16,
342 FcNameEncodingAppleRoman,
343 FcNameEncodingLatin1
344 } encoding;
345
346
347 if (FT_Get_Sfnt_Name (face, snamei, &sname) != 0)
348 break;
349
350 /*
351 * Look for Unicode strings
352 */
353 switch (sname.platform_id) {
354 case TT_PLATFORM_APPLE_UNICODE:
355 /*
356 * All APPLE_UNICODE encodings are Utf16 BE
357 *
358 * Because there's no language id for Unicode,
359 * assume it's English
360 */
361 prio |= FC_NAME_PRIO_LANG_ENGLISH;
362 prio |= FC_NAME_PRIO_ENC_UNICODE;
363 encoding = FcNameEncodingUtf16;
364 break;
365 case TT_PLATFORM_MACINTOSH:
366 switch (sname.encoding_id) {
367 case TT_MAC_ID_ROMAN:
368 encoding = FcNameEncodingAppleRoman;
369 break;
370 default:
371 continue;
372 }
373 switch (sname.language_id) {
374 case TT_MAC_LANGID_ENGLISH:
375 prio |= FC_NAME_PRIO_LANG_ENGLISH;
376 break;
377 default:
378 /*
379 * Sometimes Microsoft language ids
380 * end up in the macintosh table. This
381 * is often accompanied by data in
382 * some mystic encoding. Ignore these names
383 */
384 if (sname.language_id >= 0x100)
385 continue;
386 break;
387 }
388 break;
389 case TT_PLATFORM_MICROSOFT:
390 switch (sname.encoding_id) {
391 case TT_MS_ID_UNICODE_CS:
392 encoding = FcNameEncodingUtf16;
393 prio |= FC_NAME_PRIO_ENC_UNICODE;
394 break;
395 default:
396 continue;
397 }
398 switch (sname.language_id & 0xff) {
399 case 0x09:
400 prio |= FC_NAME_PRIO_LANG_ENGLISH;
401 break;
402 default:
403 break;
404 }
405 break;
406 case TT_PLATFORM_ISO:
407 switch (sname.encoding_id) {
408 case TT_ISO_ID_10646:
409 encoding = FcNameEncodingUtf16;
410 prio |= FC_NAME_PRIO_ENC_UNICODE;
411 break;
412 case TT_ISO_ID_7BIT_ASCII:
413 case TT_ISO_ID_8859_1:
414 encoding = FcNameEncodingLatin1;
415 break;
416 default:
417 continue;
418 }
419 break;
420 default:
421 continue;
422 }
423
424 /*
425 * Look for family and style names
426 */
427 switch (sname.name_id) {
428 case TT_NAME_ID_FONT_FAMILY:
429 prio |= FC_NAME_PRIO_NAME_FAMILY;
430 break;
431 case TT_NAME_ID_PS_NAME:
432 prio |= FC_NAME_PRIO_NAME_PS;
433 break;
434 case TT_NAME_ID_FONT_SUBFAMILY:
435 case TT_NAME_ID_TRADEMARK:
436 case TT_NAME_ID_MANUFACTURER:
437 break;
438 default:
439 continue;
440 }
441
442 src = (FcChar8 *) sname.string;
443 src_len = sname.string_len;
444
445 switch (encoding) {
446 case FcNameEncodingUtf16:
447 /*
448 * Convert Utf16 to Utf8
449 */
450
451 if (!FcUtf16Len (src, FcEndianBig, src_len, &len, &wchar))
452 continue;
453
454 /*
455 * Allocate plenty of space. Freed below
456 */
457 utf8 = malloc (len * FC_UTF8_MAX_LEN + 1);
458 if (!utf8)
459 continue;
460
461 u8 = utf8;
462
463 while ((ilen = FcUtf16ToUcs4 (src, FcEndianBig, &ucs4, src_len)) > 0)
464 {
465 src_len -= ilen;
466 src += ilen;
467 olen = FcUcs4ToUtf8 (ucs4, u8);
468 u8 += olen;
469 }
470 *u8 = '\0';
471 break;
472 case FcNameEncodingLatin1:
473 /*
474 * Convert Latin1 to Utf8. Freed below
475 */
476 utf8 = malloc (src_len * 2 + 1);
477 if (!utf8)
478 continue;
479
480 u8 = utf8;
481 while (src_len > 0)
482 {
483 ucs4 = *src++;
484 src_len--;
485 olen = FcUcs4ToUtf8 (ucs4, u8);
486 u8 += olen;
487 }
488 *u8 = '\0';
489 break;
490 case FcNameEncodingAppleRoman:
491 /*
492 * Convert AppleRoman to Utf8
493 */
494 map = FcFreeTypeGetPrivateMap (ft_encoding_apple_roman);
495 if (!map)
496 continue;
497
498 /* freed below */
499 utf8 = malloc (src_len * 3 + 1);
500 if (!utf8)
501 continue;
502
503 u8 = utf8;
504 while (src_len > 0)
505 {
506 ucs4 = FcFreeTypePrivateToUcs4 (*src++, map);
507 src_len--;
508 olen = FcUcs4ToUtf8 (ucs4, u8);
509 u8 += olen;
510 }
511 *u8 = '\0';
512 break;
513 default:
514 continue;
515 }
516 if ((prio & FC_NAME_PRIO_LANG) == FC_NAME_PRIO_LANG_NONE)
517 if (FcUtf8IsLatin (utf8, strlen ((char *) utf8)))
518 prio |= FC_NAME_PRIO_LANG_LATIN;
519
520 if (FcDebug () & FC_DBG_SCANV)
521 printf ("\nfound name (name %d platform %d encoding %d language 0x%x prio 0x%x) %s\n",
522 sname.name_id, sname.platform_id,
523 sname.encoding_id, sname.language_id,
524 prio, utf8);
525
526 switch (sname.name_id) {
527 case TT_NAME_ID_FONT_FAMILY:
528 case TT_NAME_ID_PS_NAME:
529 if (!family || prio > family_prio)
530 {
531 if (family)
532 free (family);
533 family = utf8;
534 utf8 = 0;
535 family_allocated = FcTrue;
536 family_prio = prio;
537 }
538 break;
539 case TT_NAME_ID_FONT_SUBFAMILY:
540 if (!style || prio > style_prio)
541 {
542 if (style)
543 free (style);
544 style = utf8;
545 utf8 = 0;
546 style_allocated = FcTrue;
547 style_prio = prio;
548 }
549 break;
550 case TT_NAME_ID_TRADEMARK:
551 case TT_NAME_ID_MANUFACTURER:
552 /* If the foundry wasn't found in the OS/2 table, look here */
553 if(!foundry)
554 foundry = FcNoticeFoundry(utf8);
555 break;
556 }
557 if (utf8)
558 free (utf8);
559 }
560
561 if (!family)
562 family = (FcChar8 *) face->family_name;
563
564 if (!style)
565 style = (FcChar8 *) face->style_name;
566
567 if (!family)
568 {
569 FcChar8 *start, *end;
570
571 start = (FcChar8 *) strrchr ((char *) file, '/');
572 if (start)
573 start++;
574 else
575 start = (FcChar8 *) file;
576 end = (FcChar8 *) strrchr ((char *) start, '.');
577 if (!end)
578 end = start + strlen ((char *) start);
579 /* freed below */
580 family = malloc (end - start + 1);
581 strncpy ((char *) family, (char *) start, end - start);
582 family[end - start] = '\0';
583 family_allocated = FcTrue;
584 }
585
586 if (FcDebug() & FC_DBG_SCAN)
587 printf ("\"%s\" \"%s\" ", family, style ? style : (FcChar8 *) "<none>");
588
589 if (!FcPatternAddString (pat, FC_FAMILY, family))
590 {
591 if (family_allocated)
592 free (family);
593 if (style_allocated)
594 free (style);
595 goto bail1;
596 }
597
598 if (family_allocated)
599 free (family);
600
601 if (style)
602 {
603 if (!FcPatternAddString (pat, FC_STYLE, style))
604 {
605 if (style_allocated)
606 free (style);
607 goto bail1;
608 }
609 if (style_allocated)
610 free (style);
611 }
612
613 if (!FcPatternAddString (pat, FC_FILE, file))
614 goto bail1;
615
616 if (!FcPatternAddInteger (pat, FC_INDEX, id))
617 goto bail1;
618
619 if (!FcPatternAddString (pat, FC_SOURCE, (FcChar8 *) "FreeType"))
620 goto bail1;
621
622 #if 0
623 /*
624 * don't even try this -- CJK 'monospace' fonts are really
625 * dual width, and most other fonts don't bother to set
626 * the attribute. Sigh.
627 */
628 if ((face->face_flags & FT_FACE_FLAG_FIXED_WIDTH) != 0)
629 if (!FcPatternAddInteger (pat, FC_SPACING, FC_MONO))
630 goto bail1;
631 #endif
632
633 /*
634 * Find the font revision (if available)
635 */
636 head = (TT_Header *) FT_Get_Sfnt_Table (face, ft_sfnt_head);
637 if (head)
638 {
639 if (!FcPatternAddInteger (pat, FC_FONTVERSION, head->Font_Revision))
640 goto bail1;
641 }
642 else
643 {
644 if (!FcPatternAddInteger (pat, FC_FONTVERSION, 0))
645 goto bail1;
646 }
647
648 if (os2 && os2->version >= 0x0001 && os2->version != 0xffff)
649 {
650 for (i = 0; i < NUM_CODE_PAGE_RANGE; i++)
651 {
652 FT_ULong bits;
653 int bit;
654 if (FcCodePageRange[i].bit < 32)
655 {
656 bits = os2->ulCodePageRange1;
657 bit = FcCodePageRange[i].bit;
658 }
659 else
660 {
661 bits = os2->ulCodePageRange2;
662 bit = FcCodePageRange[i].bit - 32;
663 }
664 if (bits & (1 << bit))
665 {
666 /*
667 * If the font advertises support for multiple
668 * "exclusive" languages, then include support
669 * for any language found to have coverage
670 */
671 if (exclusiveLang)
672 {
673 exclusiveLang = 0;
674 break;
675 }
676 exclusiveLang = FcCodePageRange[i].lang;
677 }
678 }
679 }
680
681 if (os2 && os2->version != 0xffff)
682 {
683 if (os2->usWeightClass == 0)
684 weight = -1;
685 else if (os2->usWeightClass < 150)
686 weight = FC_WEIGHT_THIN;
687 else if (os2->usWeightClass < 250)
688 weight = FC_WEIGHT_EXTRALIGHT;
689 else if (os2->usWeightClass < 350)
690 weight = FC_WEIGHT_LIGHT;
691 else if (os2->usWeightClass < 450)
692 weight = FC_WEIGHT_REGULAR;
693 else if (os2->usWeightClass < 550)
694 weight = FC_WEIGHT_MEDIUM;
695 else if (os2->usWeightClass < 650)
696 weight = FC_WEIGHT_SEMIBOLD;
697 else if (os2->usWeightClass < 750)
698 weight = FC_WEIGHT_BOLD;
699 else if (os2->usWeightClass < 850)
700 weight = FC_WEIGHT_EXTRABOLD;
701 else if (os2->usWeightClass < 950)
702 weight = FC_WEIGHT_BLACK;
703 else
704 weight = FC_WEIGHT_MEDIUM;
705
706 switch (os2->usWidthClass) {
707 case 1: width = FC_WIDTH_ULTRACONDENSED; break;
708 case 2: width = FC_WIDTH_EXTRACONDENSED; break;
709 case 3: width = FC_WIDTH_CONDENSED; break;
710 case 4: width = FC_WIDTH_SEMICONDENSED; break;
711 case 5: width = FC_WIDTH_NORMAL; break;
712 case 6: width = FC_WIDTH_SEMIEXPANDED; break;
713 case 7: width = FC_WIDTH_EXPANDED; break;
714 case 8: width = FC_WIDTH_EXTRAEXPANDED; break;
715 case 9: width = FC_WIDTH_ULTRAEXPANDED; break;
716 }
717 }
718
719 /*
720 * Type 1: Check for FontInfo dictionary information
721 * Code from g2@magestudios.net (Gerard Escalante)
722 */
723
724 if (FT_Get_PS_Font_Info(face, &psfontinfo) == 0)
725 {
726 if (psfontinfo.weight)
727 {
728 static struct {
729 char *name;
730 int value;
731 } ps_weights[] = {
732 { "thin", FC_WEIGHT_THIN },
733 { "extralight", FC_WEIGHT_EXTRALIGHT },
734 { "ultralight", FC_WEIGHT_ULTRALIGHT },
735 { "light", FC_WEIGHT_LIGHT },
736 { "regular", FC_WEIGHT_REGULAR },
737 { "normal", FC_WEIGHT_NORMAL },
738 { "medium", FC_WEIGHT_MEDIUM },
739 { "demibold", FC_WEIGHT_DEMIBOLD },
740 { "semibold", FC_WEIGHT_SEMIBOLD },
741 { "bold", FC_WEIGHT_BOLD },
742 { "extrabold", FC_WEIGHT_EXTRABOLD },
743 { "ultrabold", FC_WEIGHT_ULTRABOLD },
744 { "black", FC_WEIGHT_BLACK },
745 { "heavy", FC_WEIGHT_HEAVY },
746 };
747 #define NUM_PS_WEIGHTS (sizeof (ps_weights) / sizeof (ps_weights[0]))
748 int w;
749 for (w = 0; w < NUM_PS_WEIGHTS; w++)
750 if (!FcStrCmpIgnoreCase ((FcChar8 *) ps_weights[w].name,
751 (FcChar8 *) psfontinfo.weight))
752 {
753 weight = ps_weights[w].value;
754 break;
755 }
756 }
757
758 if (psfontinfo.italic_angle < 0)
759 slant = FC_SLANT_ITALIC;
760 else if (psfontinfo.italic_angle >= 0)
761 slant = FC_SLANT_ROMAN;
762
763 if(!foundry)
764 foundry = FcNoticeFoundry(psfontinfo.notice);
765 }
766
767 if (!FcPatternAddInteger (pat, FC_SLANT, slant))
768 goto bail1;
769
770 if (!FcPatternAddInteger (pat, FC_WEIGHT, weight))
771 goto bail1;
772
773 if (width != -1)
774 if (!FcPatternAddInteger (pat, FC_WIDTH, width))
775 goto bail1;
776
777 if(foundry)
778 {
779 if(!FcPatternAddString (pat, FC_FOUNDRY, foundry))
780 goto bail1;
781 }
782
783 /*
784 * Compute the unicode coverage for the font
785 */
786 cs = FcFreeTypeCharSetAndSpacing (face, blanks, &spacing);
787 if (!cs)
788 goto bail1;
789
790 /*
791 * Skip over PCF fonts that have no encoded characters; they're
792 * usually just Unicode fonts transcoded to some legacy encoding
793 */
794 if (FcCharSetCount (cs) == 0)
795 {
796 if (!strcmp(FT_MODULE_CLASS(&face->driver->root)->module_name, "pcf"))
797 goto bail2;
798 }
799
800 if (!FcPatternAddCharSet (pat, FC_CHARSET, cs))
801 goto bail2;
802
803 ls = FcFreeTypeLangSet (cs, exclusiveLang);
804 if (!ls)
805 goto bail2;
806
807 if (!FcPatternAddLangSet (pat, FC_LANG, ls))
808 goto bail2;
809
810 if (spacing != FC_PROPORTIONAL)
811 if (!FcPatternAddInteger (pat, FC_SPACING, spacing))
812 goto bail2;
813
814 /*
815 * Drop our reference to the charset
816 */
817 FcCharSetDestroy (cs);
818
819 if (!(face->face_flags & FT_FACE_FLAG_SCALABLE))
820 {
821 for (i = 0; i < face->num_fixed_sizes; i++)
822 if (!FcPatternAddDouble (pat, FC_PIXEL_SIZE,
823 (double) face->available_sizes[i].height))
824 goto bail1;
825 if (!FcPatternAddBool (pat, FC_ANTIALIAS, FcFalse))
826 goto bail1;
827 }
828
829 FT_Done_Face (face);
830 FT_Done_FreeType (ftLibrary);
831 return pat;
832
833 bail2:
834 FcCharSetDestroy (cs);
835 bail1:
836 FcPatternDestroy (pat);
837 bail0:
838 FT_Done_Face (face);
839 bail:
840 FT_Done_FreeType (ftLibrary);
841 return 0;
842 }
843
844
845 /*
846 * Figure out whether the available freetype has FT_Get_Next_Char
847 */
848
849 #if FREETYPE_MAJOR > 2
850 # define HAS_NEXT_CHAR
851 #else
852 # if FREETYPE_MAJOR == 2
853 # if FREETYPE_MINOR > 0
854 # define HAS_NEXT_CHAR
855 # else
856 # if FREETYPE_MINOR == 0
857 # if FREETYPE_PATCH >= 9
858 # define HAS_NEXT_CHAR
859 # endif
860 # endif
861 # endif
862 # endif
863 #endif
864
865 /*
866 * For our purposes, this approximation is sufficient
867 */
868 #ifndef HAS_NEXT_CHAR
869 #define FT_Get_First_Char(face, gi) ((*(gi) = 1), 1)
870 #define FT_Get_Next_Char(face, ucs4, gi) ((ucs4) >= 0xffffff ? \
871 (*(gi) = 0), 0 : \
872 (*(gi) = 1), (ucs4) + 1)
873 #warning "No FT_Get_Next_Char"
874 #endif
875
876 typedef struct _FcCharEnt {
877 FcChar16 bmp;
878 unsigned char encode;
879 } FcCharEnt;
880
881 struct _FcCharMap {
882 const FcCharEnt *ent;
883 int nent;
884 };
885
886 typedef struct _FcFontDecode {
887 FT_Encoding encoding;
888 const FcCharMap *map;
889 FcChar32 max;
890 } FcFontDecode;
891
892 static const FcCharEnt AppleRomanEnt[] = {
893 { 0x0020, 0x20 }, /* SPACE */
894 { 0x0021, 0x21 }, /* EXCLAMATION MARK */
895 { 0x0022, 0x22 }, /* QUOTATION MARK */
896 { 0x0023, 0x23 }, /* NUMBER SIGN */
897 { 0x0024, 0x24 }, /* DOLLAR SIGN */
898 { 0x0025, 0x25 }, /* PERCENT SIGN */
899 { 0x0026, 0x26 }, /* AMPERSAND */
900 { 0x0027, 0x27 }, /* APOSTROPHE */
901 { 0x0028, 0x28 }, /* LEFT PARENTHESIS */
902 { 0x0029, 0x29 }, /* RIGHT PARENTHESIS */
903 { 0x002A, 0x2A }, /* ASTERISK */
904 { 0x002B, 0x2B }, /* PLUS SIGN */
905 { 0x002C, 0x2C }, /* COMMA */
906 { 0x002D, 0x2D }, /* HYPHEN-MINUS */
907 { 0x002E, 0x2E }, /* FULL STOP */
908 { 0x002F, 0x2F }, /* SOLIDUS */
909 { 0x0030, 0x30 }, /* DIGIT ZERO */
910 { 0x0031, 0x31 }, /* DIGIT ONE */
911 { 0x0032, 0x32 }, /* DIGIT TWO */
912 { 0x0033, 0x33 }, /* DIGIT THREE */
913 { 0x0034, 0x34 }, /* DIGIT FOUR */
914 { 0x0035, 0x35 }, /* DIGIT FIVE */
915 { 0x0036, 0x36 }, /* DIGIT SIX */
916 { 0x0037, 0x37 }, /* DIGIT SEVEN */
917 { 0x0038, 0x38 }, /* DIGIT EIGHT */
918 { 0x0039, 0x39 }, /* DIGIT NINE */
919 { 0x003A, 0x3A }, /* COLON */
920 { 0x003B, 0x3B }, /* SEMICOLON */
921 { 0x003C, 0x3C }, /* LESS-THAN SIGN */
922 { 0x003D, 0x3D }, /* EQUALS SIGN */
923 { 0x003E, 0x3E }, /* GREATER-THAN SIGN */
924 { 0x003F, 0x3F }, /* QUESTION MARK */
925 { 0x0040, 0x40 }, /* COMMERCIAL AT */
926 { 0x0041, 0x41 }, /* LATIN CAPITAL LETTER A */
927 { 0x0042, 0x42 }, /* LATIN CAPITAL LETTER B */
928 { 0x0043, 0x43 }, /* LATIN CAPITAL LETTER C */
929 { 0x0044, 0x44 }, /* LATIN CAPITAL LETTER D */
930 { 0x0045, 0x45 }, /* LATIN CAPITAL LETTER E */
931 { 0x0046, 0x46 }, /* LATIN CAPITAL LETTER F */
932 { 0x0047, 0x47 }, /* LATIN CAPITAL LETTER G */
933 { 0x0048, 0x48 }, /* LATIN CAPITAL LETTER H */
934 { 0x0049, 0x49 }, /* LATIN CAPITAL LETTER I */
935 { 0x004A, 0x4A }, /* LATIN CAPITAL LETTER J */
936 { 0x004B, 0x4B }, /* LATIN CAPITAL LETTER K */
937 { 0x004C, 0x4C }, /* LATIN CAPITAL LETTER L */
938 { 0x004D, 0x4D }, /* LATIN CAPITAL LETTER M */
939 { 0x004E, 0x4E }, /* LATIN CAPITAL LETTER N */
940 { 0x004F, 0x4F }, /* LATIN CAPITAL LETTER O */
941 { 0x0050, 0x50 }, /* LATIN CAPITAL LETTER P */
942 { 0x0051, 0x51 }, /* LATIN CAPITAL LETTER Q */
943 { 0x0052, 0x52 }, /* LATIN CAPITAL LETTER R */
944 { 0x0053, 0x53 }, /* LATIN CAPITAL LETTER S */
945 { 0x0054, 0x54 }, /* LATIN CAPITAL LETTER T */
946 { 0x0055, 0x55 }, /* LATIN CAPITAL LETTER U */
947 { 0x0056, 0x56 }, /* LATIN CAPITAL LETTER V */
948 { 0x0057, 0x57 }, /* LATIN CAPITAL LETTER W */
949 { 0x0058, 0x58 }, /* LATIN CAPITAL LETTER X */
950 { 0x0059, 0x59 }, /* LATIN CAPITAL LETTER Y */
951 { 0x005A, 0x5A }, /* LATIN CAPITAL LETTER Z */
952 { 0x005B, 0x5B }, /* LEFT SQUARE BRACKET */
953 { 0x005C, 0x5C }, /* REVERSE SOLIDUS */
954 { 0x005D, 0x5D }, /* RIGHT SQUARE BRACKET */
955 { 0x005E, 0x5E }, /* CIRCUMFLEX ACCENT */
956 { 0x005F, 0x5F }, /* LOW LINE */
957 { 0x0060, 0x60 }, /* GRAVE ACCENT */
958 { 0x0061, 0x61 }, /* LATIN SMALL LETTER A */
959 { 0x0062, 0x62 }, /* LATIN SMALL LETTER B */
960 { 0x0063, 0x63 }, /* LATIN SMALL LETTER C */
961 { 0x0064, 0x64 }, /* LATIN SMALL LETTER D */
962 { 0x0065, 0x65 }, /* LATIN SMALL LETTER E */
963 { 0x0066, 0x66 }, /* LATIN SMALL LETTER F */
964 { 0x0067, 0x67 }, /* LATIN SMALL LETTER G */
965 { 0x0068, 0x68 }, /* LATIN SMALL LETTER H */
966 { 0x0069, 0x69 }, /* LATIN SMALL LETTER I */
967 { 0x006A, 0x6A }, /* LATIN SMALL LETTER J */
968 { 0x006B, 0x6B }, /* LATIN SMALL LETTER K */
969 { 0x006C, 0x6C }, /* LATIN SMALL LETTER L */
970 { 0x006D, 0x6D }, /* LATIN SMALL LETTER M */
971 { 0x006E, 0x6E }, /* LATIN SMALL LETTER N */
972 { 0x006F, 0x6F }, /* LATIN SMALL LETTER O */
973 { 0x0070, 0x70 }, /* LATIN SMALL LETTER P */
974 { 0x0071, 0x71 }, /* LATIN SMALL LETTER Q */
975 { 0x0072, 0x72 }, /* LATIN SMALL LETTER R */
976 { 0x0073, 0x73 }, /* LATIN SMALL LETTER S */
977 { 0x0074, 0x74 }, /* LATIN SMALL LETTER T */
978 { 0x0075, 0x75 }, /* LATIN SMALL LETTER U */
979 { 0x0076, 0x76 }, /* LATIN SMALL LETTER V */
980 { 0x0077, 0x77 }, /* LATIN SMALL LETTER W */
981 { 0x0078, 0x78 }, /* LATIN SMALL LETTER X */
982 { 0x0079, 0x79 }, /* LATIN SMALL LETTER Y */
983 { 0x007A, 0x7A }, /* LATIN SMALL LETTER Z */
984 { 0x007B, 0x7B }, /* LEFT CURLY BRACKET */
985 { 0x007C, 0x7C }, /* VERTICAL LINE */
986 { 0x007D, 0x7D }, /* RIGHT CURLY BRACKET */
987 { 0x007E, 0x7E }, /* TILDE */
988 { 0x00A0, 0xCA }, /* NO-BREAK SPACE */
989 { 0x00A1, 0xC1 }, /* INVERTED EXCLAMATION MARK */
990 { 0x00A2, 0xA2 }, /* CENT SIGN */
991 { 0x00A3, 0xA3 }, /* POUND SIGN */
992 { 0x00A5, 0xB4 }, /* YEN SIGN */
993 { 0x00A7, 0xA4 }, /* SECTION SIGN */
994 { 0x00A8, 0xAC }, /* DIAERESIS */
995 { 0x00A9, 0xA9 }, /* COPYRIGHT SIGN */
996 { 0x00AA, 0xBB }, /* FEMININE ORDINAL INDICATOR */
997 { 0x00AB, 0xC7 }, /* LEFT-POINTING DOUBLE ANGLE QUOTATION MARK */
998 { 0x00AC, 0xC2 }, /* NOT SIGN */
999 { 0x00AE, 0xA8 }, /* REGISTERED SIGN */
1000 { 0x00AF, 0xF8 }, /* MACRON */
1001 { 0x00B0, 0xA1 }, /* DEGREE SIGN */
1002 { 0x00B1, 0xB1 }, /* PLUS-MINUS SIGN */
1003 { 0x00B4, 0xAB }, /* ACUTE ACCENT */
1004 { 0x00B5, 0xB5 }, /* MICRO SIGN */
1005 { 0x00B6, 0xA6 }, /* PILCROW SIGN */
1006 { 0x00B7, 0xE1 }, /* MIDDLE DOT */
1007 { 0x00B8, 0xFC }, /* CEDILLA */
1008 { 0x00BA, 0xBC }, /* MASCULINE ORDINAL INDICATOR */
1009 { 0x00BB, 0xC8 }, /* RIGHT-POINTING DOUBLE ANGLE QUOTATION MARK */
1010 { 0x00BF, 0xC0 }, /* INVERTED QUESTION MARK */
1011 { 0x00C0, 0xCB }, /* LATIN CAPITAL LETTER A WITH GRAVE */
1012 { 0x00C1, 0xE7 }, /* LATIN CAPITAL LETTER A WITH ACUTE */
1013 { 0x00C2, 0xE5 }, /* LATIN CAPITAL LETTER A WITH CIRCUMFLEX */
1014 { 0x00C3, 0xCC }, /* LATIN CAPITAL LETTER A WITH TILDE */
1015 { 0x00C4, 0x80 }, /* LATIN CAPITAL LETTER A WITH DIAERESIS */
1016 { 0x00C5, 0x81 }, /* LATIN CAPITAL LETTER A WITH RING ABOVE */
1017 { 0x00C6, 0xAE }, /* LATIN CAPITAL LETTER AE */
1018 { 0x00C7, 0x82 }, /* LATIN CAPITAL LETTER C WITH CEDILLA */
1019 { 0x00C8, 0xE9 }, /* LATIN CAPITAL LETTER E WITH GRAVE */
1020 { 0x00C9, 0x83 }, /* LATIN CAPITAL LETTER E WITH ACUTE */
1021 { 0x00CA, 0xE6 }, /* LATIN CAPITAL LETTER E WITH CIRCUMFLEX */
1022 { 0x00CB, 0xE8 }, /* LATIN CAPITAL LETTER E WITH DIAERESIS */
1023 { 0x00CC, 0xED }, /* LATIN CAPITAL LETTER I WITH GRAVE */
1024 { 0x00CD, 0xEA }, /* LATIN CAPITAL LETTER I WITH ACUTE */
1025 { 0x00CE, 0xEB }, /* LATIN CAPITAL LETTER I WITH CIRCUMFLEX */
1026 { 0x00CF, 0xEC }, /* LATIN CAPITAL LETTER I WITH DIAERESIS */
1027 { 0x00D1, 0x84 }, /* LATIN CAPITAL LETTER N WITH TILDE */
1028 { 0x00D2, 0xF1 }, /* LATIN CAPITAL LETTER O WITH GRAVE */
1029 { 0x00D3, 0xEE }, /* LATIN CAPITAL LETTER O WITH ACUTE */
1030 { 0x00D4, 0xEF }, /* LATIN CAPITAL LETTER O WITH CIRCUMFLEX */
1031 { 0x00D5, 0xCD }, /* LATIN CAPITAL LETTER O WITH TILDE */
1032 { 0x00D6, 0x85 }, /* LATIN CAPITAL LETTER O WITH DIAERESIS */
1033 { 0x00D8, 0xAF }, /* LATIN CAPITAL LETTER O WITH STROKE */
1034 { 0x00D9, 0xF4 }, /* LATIN CAPITAL LETTER U WITH GRAVE */
1035 { 0x00DA, 0xF2 }, /* LATIN CAPITAL LETTER U WITH ACUTE */
1036 { 0x00DB, 0xF3 }, /* LATIN CAPITAL LETTER U WITH CIRCUMFLEX */
1037 { 0x00DC, 0x86 }, /* LATIN CAPITAL LETTER U WITH DIAERESIS */
1038 { 0x00DF, 0xA7 }, /* LATIN SMALL LETTER SHARP S */
1039 { 0x00E0, 0x88 }, /* LATIN SMALL LETTER A WITH GRAVE */
1040 { 0x00E1, 0x87 }, /* LATIN SMALL LETTER A WITH ACUTE */
1041 { 0x00E2, 0x89 }, /* LATIN SMALL LETTER A WITH CIRCUMFLEX */
1042 { 0x00E3, 0x8B }, /* LATIN SMALL LETTER A WITH TILDE */
1043 { 0x00E4, 0x8A }, /* LATIN SMALL LETTER A WITH DIAERESIS */
1044 { 0x00E5, 0x8C }, /* LATIN SMALL LETTER A WITH RING ABOVE */
1045 { 0x00E6, 0xBE }, /* LATIN SMALL LETTER AE */
1046 { 0x00E7, 0x8D }, /* LATIN SMALL LETTER C WITH CEDILLA */
1047 { 0x00E8, 0x8F }, /* LATIN SMALL LETTER E WITH GRAVE */
1048 { 0x00E9, 0x8E }, /* LATIN SMALL LETTER E WITH ACUTE */
1049 { 0x00EA, 0x90 }, /* LATIN SMALL LETTER E WITH CIRCUMFLEX */
1050 { 0x00EB, 0x91 }, /* LATIN SMALL LETTER E WITH DIAERESIS */
1051 { 0x00EC, 0x93 }, /* LATIN SMALL LETTER I WITH GRAVE */
1052 { 0x00ED, 0x92 }, /* LATIN SMALL LETTER I WITH ACUTE */
1053 { 0x00EE, 0x94 }, /* LATIN SMALL LETTER I WITH CIRCUMFLEX */
1054 { 0x00EF, 0x95 }, /* LATIN SMALL LETTER I WITH DIAERESIS */
1055 { 0x00F1, 0x96 }, /* LATIN SMALL LETTER N WITH TILDE */
1056 { 0x00F2, 0x98 }, /* LATIN SMALL LETTER O WITH GRAVE */
1057 { 0x00F3, 0x97 }, /* LATIN SMALL LETTER O WITH ACUTE */
1058 { 0x00F4, 0x99 }, /* LATIN SMALL LETTER O WITH CIRCUMFLEX */
1059 { 0x00F5, 0x9B }, /* LATIN SMALL LETTER O WITH TILDE */
1060 { 0x00F6, 0x9A }, /* LATIN SMALL LETTER O WITH DIAERESIS */
1061 { 0x00F7, 0xD6 }, /* DIVISION SIGN */
1062 { 0x00F8, 0xBF }, /* LATIN SMALL LETTER O WITH STROKE */
1063 { 0x00F9, 0x9D }, /* LATIN SMALL LETTER U WITH GRAVE */
1064 { 0x00FA, 0x9C }, /* LATIN SMALL LETTER U WITH ACUTE */
1065 { 0x00FB, 0x9E }, /* LATIN SMALL LETTER U WITH CIRCUMFLEX */
1066 { 0x00FC, 0x9F }, /* LATIN SMALL LETTER U WITH DIAERESIS */
1067 { 0x00FF, 0xD8 }, /* LATIN SMALL LETTER Y WITH DIAERESIS */
1068 { 0x0131, 0xF5 }, /* LATIN SMALL LETTER DOTLESS I */
1069 { 0x0152, 0xCE }, /* LATIN CAPITAL LIGATURE OE */
1070 { 0x0153, 0xCF }, /* LATIN SMALL LIGATURE OE */
1071 { 0x0178, 0xD9 }, /* LATIN CAPITAL LETTER Y WITH DIAERESIS */
1072 { 0x0192, 0xC4 }, /* LATIN SMALL LETTER F WITH HOOK */
1073 { 0x02C6, 0xF6 }, /* MODIFIER LETTER CIRCUMFLEX ACCENT */
1074 { 0x02C7, 0xFF }, /* CARON */
1075 { 0x02D8, 0xF9 }, /* BREVE */
1076 { 0x02D9, 0xFA }, /* DOT ABOVE */
1077 { 0x02DA, 0xFB }, /* RING ABOVE */
1078 { 0x02DB, 0xFE }, /* OGONEK */
1079 { 0x02DC, 0xF7 }, /* SMALL TILDE */
1080 { 0x02DD, 0xFD }, /* DOUBLE ACUTE ACCENT */
1081 { 0x03A9, 0xBD }, /* GREEK CAPITAL LETTER OMEGA */
1082 { 0x03C0, 0xB9 }, /* GREEK SMALL LETTER PI */
1083 { 0x2013, 0xD0 }, /* EN DASH */
1084 { 0x2014, 0xD1 }, /* EM DASH */
1085 { 0x2018, 0xD4 }, /* LEFT SINGLE QUOTATION MARK */
1086 { 0x2019, 0xD5 }, /* RIGHT SINGLE QUOTATION MARK */
1087 { 0x201A, 0xE2 }, /* SINGLE LOW-9 QUOTATION MARK */
1088 { 0x201C, 0xD2 }, /* LEFT DOUBLE QUOTATION MARK */
1089 { 0x201D, 0xD3 }, /* RIGHT DOUBLE QUOTATION MARK */
1090 { 0x201E, 0xE3 }, /* DOUBLE LOW-9 QUOTATION MARK */
1091 { 0x2020, 0xA0 }, /* DAGGER */
1092 { 0x2021, 0xE0 }, /* DOUBLE DAGGER */
1093 { 0x2022, 0xA5 }, /* BULLET */
1094 { 0x2026, 0xC9 }, /* HORIZONTAL ELLIPSIS */
1095 { 0x2030, 0xE4 }, /* PER MILLE SIGN */
1096 { 0x2039, 0xDC }, /* SINGLE LEFT-POINTING ANGLE QUOTATION MARK */
1097 { 0x203A, 0xDD }, /* SINGLE RIGHT-POINTING ANGLE QUOTATION MARK */
1098 { 0x2044, 0xDA }, /* FRACTION SLASH */
1099 { 0x20AC, 0xDB }, /* EURO SIGN */
1100 { 0x2122, 0xAA }, /* TRADE MARK SIGN */
1101 { 0x2202, 0xB6 }, /* PARTIAL DIFFERENTIAL */
1102 { 0x2206, 0xC6 }, /* INCREMENT */
1103 { 0x220F, 0xB8 }, /* N-ARY PRODUCT */
1104 { 0x2211, 0xB7 }, /* N-ARY SUMMATION */
1105 { 0x221A, 0xC3 }, /* SQUARE ROOT */
1106 { 0x221E, 0xB0 }, /* INFINITY */
1107 { 0x222B, 0xBA }, /* INTEGRAL */
1108 { 0x2248, 0xC5 }, /* ALMOST EQUAL TO */
1109 { 0x2260, 0xAD }, /* NOT EQUAL TO */
1110 { 0x2264, 0xB2 }, /* LESS-THAN OR EQUAL TO */
1111 { 0x2265, 0xB3 }, /* GREATER-THAN OR EQUAL TO */
1112 { 0x25CA, 0xD7 }, /* LOZENGE */
1113 { 0xF8FF, 0xF0 }, /* Apple logo */
1114 { 0xFB01, 0xDE }, /* LATIN SMALL LIGATURE FI */
1115 { 0xFB02, 0xDF }, /* LATIN SMALL LIGATURE FL */
1116 };
1117
1118 static const FcCharMap AppleRoman = {
1119 AppleRomanEnt,
1120 sizeof (AppleRomanEnt) / sizeof (AppleRomanEnt[0])
1121 };
1122
1123 static const FcCharEnt AdobeSymbolEnt[] = {
1124 { 0x0020, 0x20 }, /* SPACE # space */
1125 { 0x0021, 0x21 }, /* EXCLAMATION MARK # exclam */
1126 { 0x0023, 0x23 }, /* NUMBER SIGN # numbersign */
1127 { 0x0025, 0x25 }, /* PERCENT SIGN # percent */
1128 { 0x0026, 0x26 }, /* AMPERSAND # ampersand */
1129 { 0x0028, 0x28 }, /* LEFT PARENTHESIS # parenleft */
1130 { 0x0029, 0x29 }, /* RIGHT PARENTHESIS # parenright */
1131 { 0x002B, 0x2B }, /* PLUS SIGN # plus */
1132 { 0x002C, 0x2C }, /* COMMA # comma */
1133 { 0x002E, 0x2E }, /* FULL STOP # period */
1134 { 0x002F, 0x2F }, /* SOLIDUS # slash */
1135 { 0x0030, 0x30 }, /* DIGIT ZERO # zero */
1136 { 0x0031, 0x31 }, /* DIGIT ONE # one */
1137 { 0x0032, 0x32 }, /* DIGIT TWO # two */
1138 { 0x0033, 0x33 }, /* DIGIT THREE # three */
1139 { 0x0034, 0x34 }, /* DIGIT FOUR # four */
1140 { 0x0035, 0x35 }, /* DIGIT FIVE # five */
1141 { 0x0036, 0x36 }, /* DIGIT SIX # six */
1142 { 0x0037, 0x37 }, /* DIGIT SEVEN # seven */
1143 { 0x0038, 0x38 }, /* DIGIT EIGHT # eight */
1144 { 0x0039, 0x39 }, /* DIGIT NINE # nine */
1145 { 0x003A, 0x3A }, /* COLON # colon */
1146 { 0x003B, 0x3B }, /* SEMICOLON # semicolon */
1147 { 0x003C, 0x3C }, /* LESS-THAN SIGN # less */
1148 { 0x003D, 0x3D }, /* EQUALS SIGN # equal */
1149 { 0x003E, 0x3E }, /* GREATER-THAN SIGN # greater */
1150 { 0x003F, 0x3F }, /* QUESTION MARK # question */
1151 { 0x005B, 0x5B }, /* LEFT SQUARE BRACKET # bracketleft */
1152 { 0x005D, 0x5D }, /* RIGHT SQUARE BRACKET # bracketright */
1153 { 0x005F, 0x5F }, /* LOW LINE # underscore */
1154 { 0x007B, 0x7B }, /* LEFT CURLY BRACKET # braceleft */
1155 { 0x007C, 0x7C }, /* VERTICAL LINE # bar */
1156 { 0x007D, 0x7D }, /* RIGHT CURLY BRACKET # braceright */
1157 { 0x00A0, 0x20 }, /* NO-BREAK SPACE # space */
1158 { 0x00AC, 0xD8 }, /* NOT SIGN # logicalnot */
1159 { 0x00B0, 0xB0 }, /* DEGREE SIGN # degree */
1160 { 0x00B1, 0xB1 }, /* PLUS-MINUS SIGN # plusminus */
1161 { 0x00B5, 0x6D }, /* MICRO SIGN # mu */
1162 { 0x00D7, 0xB4 }, /* MULTIPLICATION SIGN # multiply */
1163 { 0x00F7, 0xB8 }, /* DIVISION SIGN # divide */
1164 { 0x0192, 0xA6 }, /* LATIN SMALL LETTER F WITH HOOK # florin */
1165 { 0x0391, 0x41 }, /* GREEK CAPITAL LETTER ALPHA # Alpha */
1166 { 0x0392, 0x42 }, /* GREEK CAPITAL LETTER BETA # Beta */
1167 { 0x0393, 0x47 }, /* GREEK CAPITAL LETTER GAMMA # Gamma */
1168 { 0x0394, 0x44 }, /* GREEK CAPITAL LETTER DELTA # Delta */
1169 { 0x0395, 0x45 }, /* GREEK CAPITAL LETTER EPSILON # Epsilon */
1170 { 0x0396, 0x5A }, /* GREEK CAPITAL LETTER ZETA # Zeta */
1171 { 0x0397, 0x48 }, /* GREEK CAPITAL LETTER ETA # Eta */
1172 { 0x0398, 0x51 }, /* GREEK CAPITAL LETTER THETA # Theta */
1173 { 0x0399, 0x49 }, /* GREEK CAPITAL LETTER IOTA # Iota */
1174 { 0x039A, 0x4B }, /* GREEK CAPITAL LETTER KAPPA # Kappa */
1175 { 0x039B, 0x4C }, /* GREEK CAPITAL LETTER LAMDA # Lambda */
1176 { 0x039C, 0x4D }, /* GREEK CAPITAL LETTER MU # Mu */
1177 { 0x039D, 0x4E }, /* GREEK CAPITAL LETTER NU # Nu */
1178 { 0x039E, 0x58 }, /* GREEK CAPITAL LETTER XI # Xi */
1179 { 0x039F, 0x4F }, /* GREEK CAPITAL LETTER OMICRON # Omicron */
1180 { 0x03A0, 0x50 }, /* GREEK CAPITAL LETTER PI # Pi */
1181 { 0x03A1, 0x52 }, /* GREEK CAPITAL LETTER RHO # Rho */
1182 { 0x03A3, 0x53 }, /* GREEK CAPITAL LETTER SIGMA # Sigma */
1183 { 0x03A4, 0x54 }, /* GREEK CAPITAL LETTER TAU # Tau */
1184 { 0x03A5, 0x55 }, /* GREEK CAPITAL LETTER UPSILON # Upsilon */
1185 { 0x03A6, 0x46 }, /* GREEK CAPITAL LETTER PHI # Phi */
1186 { 0x03A7, 0x43 }, /* GREEK CAPITAL LETTER CHI # Chi */
1187 { 0x03A8, 0x59 }, /* GREEK CAPITAL LETTER PSI # Psi */
1188 { 0x03A9, 0x57 }, /* GREEK CAPITAL LETTER OMEGA # Omega */
1189 { 0x03B1, 0x61 }, /* GREEK SMALL LETTER ALPHA # alpha */
1190 { 0x03B2, 0x62 }, /* GREEK SMALL LETTER BETA # beta */
1191 { 0x03B3, 0x67 }, /* GREEK SMALL LETTER GAMMA # gamma */
1192 { 0x03B4, 0x64 }, /* GREEK SMALL LETTER DELTA # delta */
1193 { 0x03B5, 0x65 }, /* GREEK SMALL LETTER EPSILON # epsilon */
1194 { 0x03B6, 0x7A }, /* GREEK SMALL LETTER ZETA # zeta */
1195 { 0x03B7, 0x68 }, /* GREEK SMALL LETTER ETA # eta */
1196 { 0x03B8, 0x71 }, /* GREEK SMALL LETTER THETA # theta */
1197 { 0x03B9, 0x69 }, /* GREEK SMALL LETTER IOTA # iota */
1198 { 0x03BA, 0x6B }, /* GREEK SMALL LETTER KAPPA # kappa */
1199 { 0x03BB, 0x6C }, /* GREEK SMALL LETTER LAMDA # lambda */
1200 { 0x03BC, 0x6D }, /* GREEK SMALL LETTER MU # mu */
1201 { 0x03BD, 0x6E }, /* GREEK SMALL LETTER NU # nu */
1202 { 0x03BE, 0x78 }, /* GREEK SMALL LETTER XI # xi */
1203 { 0x03BF, 0x6F }, /* GREEK SMALL LETTER OMICRON # omicron */
1204 { 0x03C0, 0x70 }, /* GREEK SMALL LETTER PI # pi */
1205 { 0x03C1, 0x72 }, /* GREEK SMALL LETTER RHO # rho */
1206 { 0x03C2, 0x56 }, /* GREEK SMALL LETTER FINAL SIGMA # sigma1 */
1207 { 0x03C3, 0x73 }, /* GREEK SMALL LETTER SIGMA # sigma */
1208 { 0x03C4, 0x74 }, /* GREEK SMALL LETTER TAU # tau */
1209 { 0x03C5, 0x75 }, /* GREEK SMALL LETTER UPSILON # upsilon */
1210 { 0x03C6, 0x66 }, /* GREEK SMALL LETTER PHI # phi */
1211 { 0x03C7, 0x63 }, /* GREEK SMALL LETTER CHI # chi */
1212 { 0x03C8, 0x79 }, /* GREEK SMALL LETTER PSI # psi */
1213 { 0x03C9, 0x77 }, /* GREEK SMALL LETTER OMEGA # omega */
1214 { 0x03D1, 0x4A }, /* GREEK THETA SYMBOL # theta1 */
1215 { 0x03D2, 0xA1 }, /* GREEK UPSILON WITH HOOK SYMBOL # Upsilon1 */
1216 { 0x03D5, 0x6A }, /* GREEK PHI SYMBOL # phi1 */
1217 { 0x03D6, 0x76 }, /* GREEK PI SYMBOL # omega1 */
1218 { 0x2022, 0xB7 }, /* BULLET # bullet */
1219 { 0x2026, 0xBC }, /* HORIZONTAL ELLIPSIS # ellipsis */
1220 { 0x2032, 0xA2 }, /* PRIME # minute */
1221 { 0x2033, 0xB2 }, /* DOUBLE PRIME # second */
1222 { 0x2044, 0xA4 }, /* FRACTION SLASH # fraction */
1223 { 0x20AC, 0xA0 }, /* EURO SIGN # Euro */
1224 { 0x2111, 0xC1 }, /* BLACK-LETTER CAPITAL I # Ifraktur */
1225 { 0x2118, 0xC3 }, /* SCRIPT CAPITAL P # weierstrass */
1226 { 0x211C, 0xC2 }, /* BLACK-LETTER CAPITAL R # Rfraktur */
1227 { 0x2126, 0x57 }, /* OHM SIGN # Omega */
1228 { 0x2135, 0xC0 }, /* ALEF SYMBOL # aleph */
1229 { 0x2190, 0xAC }, /* LEFTWARDS ARROW # arrowleft */
1230 { 0x2191, 0xAD }, /* UPWARDS ARROW # arrowup */
1231 { 0x2192, 0xAE }, /* RIGHTWARDS ARROW # arrowright */
1232 { 0x2193, 0xAF }, /* DOWNWARDS ARROW # arrowdown */
1233 { 0x2194, 0xAB }, /* LEFT RIGHT ARROW # arrowboth */
1234 { 0x21B5, 0xBF }, /* DOWNWARDS ARROW WITH CORNER LEFTWARDS # carriagereturn */
1235 { 0x21D0, 0xDC }, /* LEFTWARDS DOUBLE ARROW # arrowdblleft */
1236 { 0x21D1, 0xDD }, /* UPWARDS DOUBLE ARROW # arrowdblup */
1237 { 0x21D2, 0xDE }, /* RIGHTWARDS DOUBLE ARROW # arrowdblright */
1238 { 0x21D3, 0xDF }, /* DOWNWARDS DOUBLE ARROW # arrowdbldown */
1239 { 0x21D4, 0xDB }, /* LEFT RIGHT DOUBLE ARROW # arrowdblboth */
1240 { 0x2200, 0x22 }, /* FOR ALL # universal */
1241 { 0x2202, 0xB6 }, /* PARTIAL DIFFERENTIAL # partialdiff */
1242 { 0x2203, 0x24 }, /* THERE EXISTS # existential */
1243 { 0x2205, 0xC6 }, /* EMPTY SET # emptyset */
1244 { 0x2206, 0x44 }, /* INCREMENT # Delta */
1245 { 0x2207, 0xD1 }, /* NABLA # gradient */
1246 { 0x2208, 0xCE }, /* ELEMENT OF # element */
1247 { 0x2209, 0xCF }, /* NOT AN ELEMENT OF # notelement */
1248 { 0x220B, 0x27 }, /* CONTAINS AS MEMBER # suchthat */
1249 { 0x220F, 0xD5 }, /* N-ARY PRODUCT # product */
1250 { 0x2211, 0xE5 }, /* N-ARY SUMMATION # summation */
1251 { 0x2212, 0x2D }, /* MINUS SIGN # minus */
1252 { 0x2215, 0xA4 }, /* DIVISION SLASH # fraction */
1253 { 0x2217, 0x2A }, /* ASTERISK OPERATOR # asteriskmath */
1254 { 0x221A, 0xD6 }, /* SQUARE ROOT # radical */
1255 { 0x221D, 0xB5 }, /* PROPORTIONAL TO # proportional */
1256 { 0x221E, 0xA5 }, /* INFINITY # infinity */
1257 { 0x2220, 0xD0 }, /* ANGLE # angle */
1258 { 0x2227, 0xD9 }, /* LOGICAL AND # logicaland */
1259 { 0x2228, 0xDA }, /* LOGICAL OR # logicalor */
1260 { 0x2229, 0xC7 }, /* INTERSECTION # intersection */
1261 { 0x222A, 0xC8 }, /* UNION # union */
1262 { 0x222B, 0xF2 }, /* INTEGRAL # integral */
1263 { 0x2234, 0x5C }, /* THEREFORE # therefore */
1264 { 0x223C, 0x7E }, /* TILDE OPERATOR # similar */
1265 { 0x2245, 0x40 }, /* APPROXIMATELY EQUAL TO # congruent */
1266 { 0x2248, 0xBB }, /* ALMOST EQUAL TO # approxequal */
1267 { 0x2260, 0xB9 }, /* NOT EQUAL TO # notequal */
1268 { 0x2261, 0xBA }, /* IDENTICAL TO # equivalence */
1269 { 0x2264, 0xA3 }, /* LESS-THAN OR EQUAL TO # lessequal */
1270 { 0x2265, 0xB3 }, /* GREATER-THAN OR EQUAL TO # greaterequal */
1271 { 0x2282, 0xCC }, /* SUBSET OF # propersubset */
1272 { 0x2283, 0xC9 }, /* SUPERSET OF # propersuperset */
1273 { 0x2284, 0xCB }, /* NOT A SUBSET OF # notsubset */
1274 { 0x2286, 0xCD }, /* SUBSET OF OR EQUAL TO # reflexsubset */
1275 { 0x2287, 0xCA }, /* SUPERSET OF OR EQUAL TO # reflexsuperset */
1276 { 0x2295, 0xC5 }, /* CIRCLED PLUS # circleplus */
1277 { 0x2297, 0xC4 }, /* CIRCLED TIMES # circlemultiply */
1278 { 0x22A5, 0x5E }, /* UP TACK # perpendicular */
1279 { 0x22C5, 0xD7 }, /* DOT OPERATOR # dotmath */
1280 { 0x2320, 0xF3 }, /* TOP HALF INTEGRAL # integraltp */
1281 { 0x2321, 0xF5 }, /* BOTTOM HALF INTEGRAL # integralbt */
1282 { 0x2329, 0xE1 }, /* LEFT-POINTING ANGLE BRACKET # angleleft */
1283 { 0x232A, 0xF1 }, /* RIGHT-POINTING ANGLE BRACKET # angleright */
1284 { 0x25CA, 0xE0 }, /* LOZENGE # lozenge */
1285 { 0x2660, 0xAA }, /* BLACK SPADE SUIT # spade */
1286 { 0x2663, 0xA7 }, /* BLACK CLUB SUIT # club */
1287 { 0x2665, 0xA9 }, /* BLACK HEART SUIT # heart */
1288 { 0x2666, 0xA8 }, /* BLACK DIAMOND SUIT # diamond */
1289 { 0xF6D9, 0xD3 }, /* COPYRIGHT SIGN SERIF # copyrightserif (CUS) */
1290 { 0xF6DA, 0xD2 }, /* REGISTERED SIGN SERIF # registerserif (CUS) */
1291 { 0xF6DB, 0xD4 }, /* TRADE MARK SIGN SERIF # trademarkserif (CUS) */
1292 { 0xF8E5, 0x60 }, /* RADICAL EXTENDER # radicalex (CUS) */
1293 { 0xF8E6, 0xBD }, /* VERTICAL ARROW EXTENDER # arrowvertex (CUS) */
1294 { 0xF8E7, 0xBE }, /* HORIZONTAL ARROW EXTENDER # arrowhorizex (CUS) */
1295 { 0xF8E8, 0xE2 }, /* REGISTERED SIGN SANS SERIF # registersans (CUS) */
1296 { 0xF8E9, 0xE3 }, /* COPYRIGHT SIGN SANS SERIF # copyrightsans (CUS) */
1297 { 0xF8EA, 0xE4 }, /* TRADE MARK SIGN SANS SERIF # trademarksans (CUS) */
1298 { 0xF8EB, 0xE6 }, /* LEFT PAREN TOP # parenlefttp (CUS) */
1299 { 0xF8EC, 0xE7 }, /* LEFT PAREN EXTENDER # parenleftex (CUS) */
1300 { 0xF8ED, 0xE8 }, /* LEFT PAREN BOTTOM # parenleftbt (CUS) */
1301 { 0xF8EE, 0xE9 }, /* LEFT SQUARE BRACKET TOP # bracketlefttp (CUS) */
1302 { 0xF8EF, 0xEA }, /* LEFT SQUARE BRACKET EXTENDER # bracketleftex (CUS) */
1303 { 0xF8F0, 0xEB }, /* LEFT SQUARE BRACKET BOTTOM # bracketleftbt (CUS) */
1304 { 0xF8F1, 0xEC }, /* LEFT CURLY BRACKET TOP # bracelefttp (CUS) */
1305 { 0xF8F2, 0xED }, /* LEFT CURLY BRACKET MID # braceleftmid (CUS) */
1306 { 0xF8F3, 0xEE }, /* LEFT CURLY BRACKET BOTTOM # braceleftbt (CUS) */
1307 { 0xF8F4, 0xEF }, /* CURLY BRACKET EXTENDER # braceex (CUS) */
1308 { 0xF8F5, 0xF4 }, /* INTEGRAL EXTENDER # integralex (CUS) */
1309 { 0xF8F6, 0xF6 }, /* RIGHT PAREN TOP # parenrighttp (CUS) */
1310 { 0xF8F7, 0xF7 }, /* RIGHT PAREN EXTENDER # parenrightex (CUS) */
1311 { 0xF8F8, 0xF8 }, /* RIGHT PAREN BOTTOM # parenrightbt (CUS) */
1312 { 0xF8F9, 0xF9 }, /* RIGHT SQUARE BRACKET TOP # bracketrighttp (CUS) */
1313 { 0xF8FA, 0xFA }, /* RIGHT SQUARE BRACKET EXTENDER # bracketrightex (CUS) */
1314 { 0xF8FB, 0xFB }, /* RIGHT SQUARE BRACKET BOTTOM # bracketrightbt (CUS) */
1315 { 0xF8FC, 0xFC }, /* RIGHT CURLY BRACKET TOP # bracerighttp (CUS) */
1316 { 0xF8FD, 0xFD }, /* RIGHT CURLY BRACKET MID # bracerightmid (CUS) */
1317 { 0xF8FE, 0xFE }, /* RIGHT CURLY BRACKET BOTTOM # bracerightbt (CUS) */
1318 };
1319
1320 static const FcCharMap AdobeSymbol = {
1321 AdobeSymbolEnt,
1322 sizeof (AdobeSymbolEnt) / sizeof (AdobeSymbolEnt[0]),
1323 };
1324
1325 static const FcFontDecode fcFontDecoders[] = {
1326 { ft_encoding_unicode, 0, (1 << 21) - 1 },
1327 { ft_encoding_symbol, &AdobeSymbol, (1 << 16) - 1 },
1328 { ft_encoding_apple_roman, &AppleRoman, (1 << 16) - 1 },
1329 };
1330
1331 #define NUM_DECODE (sizeof (fcFontDecoders) / sizeof (fcFontDecoders[0]))
1332
1333 FcChar32
1334 FcFreeTypeUcs4ToPrivate (FcChar32 ucs4, const FcCharMap *map)
1335 {
1336 int low, high, mid;
1337 FcChar16 bmp;
1338
1339 low = 0;
1340 high = map->nent - 1;
1341 if (ucs4 < map->ent[low].bmp || map->ent[high].bmp < ucs4)
1342 return ~0;
1343 while (low <= high)
1344 {
1345 mid = (high + low) >> 1;
1346 bmp = map->ent[mid].bmp;
1347 if (ucs4 == bmp)
1348 return (FT_ULong) map->ent[mid].encode;
1349 if (ucs4 < bmp)
1350 high = mid - 1;
1351 else
1352 low = mid + 1;
1353 }
1354 return ~0;
1355 }
1356
1357 FcChar32
1358 FcFreeTypePrivateToUcs4 (FcChar32 private, const FcCharMap *map)
1359 {
1360 int i;
1361
1362 for (i = 0; i < map->nent; i++)
1363 if (map->ent[i].encode == private)
1364 return (FcChar32) map->ent[i].bmp;
1365 return ~0;
1366 }
1367
1368 const FcCharMap *
1369 FcFreeTypeGetPrivateMap (FT_Encoding encoding)
1370 {
1371 int i;
1372
1373 for (i = 0; i < NUM_DECODE; i++)
1374 if (fcFontDecoders[i].encoding == encoding)
1375 return fcFontDecoders[i].map;
1376 return 0;
1377 }
1378
1379 /*
1380 * Map a UCS4 glyph to a glyph index. Use all available encoding
1381 * tables to try and find one that works. This information is expected
1382 * to be cached by higher levels, so performance isn't critical
1383 */
1384
1385 FT_UInt
1386 FcFreeTypeCharIndex (FT_Face face, FcChar32 ucs4)
1387 {
1388 int initial, offset, decode;
1389 FT_UInt glyphindex;
1390 FcChar32 charcode;
1391
1392 initial = 0;
1393 /*
1394 * Find the current encoding
1395 */
1396 if (face->charmap)
1397 {
1398 for (; initial < NUM_DECODE; initial++)
1399 if (fcFontDecoders[initial].encoding == face->charmap->encoding)
1400 break;
1401 if (initial == NUM_DECODE)
1402 initial = 0;
1403 }
1404 /*
1405 * Check each encoding for the glyph, starting with the current one
1406 */
1407 for (offset = 0; offset < NUM_DECODE; offset++)
1408 {
1409 decode = (initial + offset) % NUM_DECODE;
1410 if (!face->charmap || face->charmap->encoding != fcFontDecoders[decode].encoding)
1411 if (FT_Select_Charmap (face, fcFontDecoders[decode].encoding) != 0)
1412 continue;
1413 if (fcFontDecoders[decode].map)
1414 {
1415 charcode = FcFreeTypeUcs4ToPrivate (ucs4, fcFontDecoders[decode].map);
1416 if (charcode == ~0)
1417 continue;
1418 }
1419 else
1420 charcode = ucs4;
1421 glyphindex = FT_Get_Char_Index (face, (FT_ULong) charcode);
1422 if (glyphindex)
1423 return glyphindex;
1424 }
1425 return 0;
1426 }
1427
1428 static FcBool
1429 FcFreeTypeCheckGlyph (FT_Face face, FcChar32 ucs4,
1430 FT_UInt glyph, FcBlanks *blanks,
1431 FT_Pos *advance)
1432 {
1433 FT_Int load_flags = FT_LOAD_NO_SCALE | FT_LOAD_NO_HINTING;
1434 FT_GlyphSlot slot;
1435
1436 /*
1437 * When using scalable fonts, only report those glyphs
1438 * which can be scaled; otherwise those fonts will
1439 * only be available at some sizes, and never when
1440 * transformed. Avoid this by simply reporting bitmap-only
1441 * glyphs as missing
1442 */
1443 if (face->face_flags & FT_FACE_FLAG_SCALABLE)
1444 load_flags |= FT_LOAD_NO_BITMAP;
1445
1446 if (FT_Load_Glyph (face, glyph, load_flags))
1447 return FcFalse;
1448
1449 slot = face->glyph;
1450 if (!glyph)
1451 return FcFalse;
1452
1453 *advance = slot->metrics.horiAdvance;
1454
1455 switch (slot->format) {
1456 case ft_glyph_format_bitmap:
1457 /*
1458 * Bitmaps are assumed to be reasonable; if
1459 * this proves to be a rash assumption, this
1460 * code can be easily modified
1461 */
1462 return FcTrue;
1463 case ft_glyph_format_outline:
1464 /*
1465 * Glyphs with contours are always OK
1466 */
1467 if (slot->outline.n_contours != 0)
1468 return FcTrue;
1469 /*
1470 * Glyphs with no contours are only OK if
1471 * they're members of the Blanks set specified
1472 * in the configuration. If blanks isn't set,
1473 * then allow any glyph to be blank
1474 */
1475 if (!blanks || FcBlanksIsMember (blanks, ucs4))
1476 return FcTrue;
1477 /* fall through ... */
1478 default:
1479 break;
1480 }
1481 return FcFalse;
1482 }
1483
1484 FcCharSet *
1485 FcFreeTypeCharSetAndSpacing (FT_Face face, FcBlanks *blanks, int *spacing)
1486 {
1487 FcChar32 page, off, max, ucs4;
1488 #ifdef CHECK
1489 FcChar32 font_max = 0;
1490 #endif
1491 FcCharSet *fcs;
1492 FcCharLeaf *leaf;
1493 const FcCharMap *map;
1494 int o;
1495 int i;
1496 FT_UInt glyph;
1497 FT_Pos advance, all_advance = 0;
1498 FcBool has_advance = FcFalse, fixed_advance = FcTrue;
1499
1500 fcs = FcCharSetCreate ();
1501 if (!fcs)
1502 goto bail0;
1503
1504 for (o = 0; o < NUM_DECODE; o++)
1505 {
1506 if (FT_Select_Charmap (face, fcFontDecoders[o].encoding) != 0)
1507 continue;
1508 map = fcFontDecoders[o].map;
1509 if (map)
1510 {
1511 /*
1512 * Non-Unicode tables are easy; there's a list of all possible
1513 * characters
1514 */
1515 for (i = 0; i < map->nent; i++)
1516 {
1517 ucs4 = map->ent[i].bmp;
1518 glyph = FT_Get_Char_Index (face, map->ent[i].encode);
1519 if (glyph &&
1520 FcFreeTypeCheckGlyph (face, ucs4, glyph, blanks, &advance))
1521 {
1522 if (!has_advance)
1523 {
1524 has_advance = FcTrue;
1525 all_advance = advance;
1526 }
1527 else if (advance != all_advance)
1528 fixed_advance = FcFalse;
1529 leaf = FcCharSetFindLeafCreate (fcs, ucs4);
1530 if (!leaf)
1531 goto bail1;
1532 leaf->map[(ucs4 & 0xff) >> 5] |= (1 << (ucs4 & 0x1f));
1533 #ifdef CHECK
1534 if (ucs4 > font_max)
1535 font_max = ucs4;
1536 #endif
1537 }
1538 }
1539 }
1540 else
1541 {
1542 FT_UInt gindex;
1543
1544 max = fcFontDecoders[o].max;
1545 /*
1546 * Find the first encoded character in the font
1547 */
1548 if (FT_Get_Char_Index (face, 0))
1549 {
1550 ucs4 = 0;
1551 gindex = 1;
1552 }
1553 else
1554 {
1555 ucs4 = FT_Get_Next_Char (face, 0, &gindex);
1556 if (!ucs4)
1557 gindex = 0;
1558 }
1559
1560 while (gindex)
1561 {
1562 page = ucs4 >> 8;
1563 leaf = 0;
1564 while ((ucs4 >> 8) == page)
1565 {
1566 glyph = FT_Get_Char_Index (face, ucs4);
1567 if (glyph && FcFreeTypeCheckGlyph (face, ucs4,
1568 glyph, blanks, &advance))
1569 {
1570 if (!has_advance)
1571 {
1572 has_advance = FcTrue;
1573 all_advance = advance;
1574 }
1575 else if (advance != all_advance)
1576 fixed_advance = FcFalse;
1577 if (!leaf)
1578 {
1579 leaf = FcCharSetFindLeafCreate (fcs, ucs4);
1580 if (!leaf)
1581 goto bail1;
1582 }
1583 off = ucs4 & 0xff;
1584 leaf->map[off >> 5] |= (1 << (off & 0x1f));
1585 #ifdef CHECK
1586 if (ucs4 > font_max)
1587 font_max = ucs4;
1588 #endif
1589 }
1590 ucs4++;
1591 }
1592 ucs4 = FT_Get_Next_Char (face, ucs4 - 1, &gindex);
1593 if (!ucs4)
1594 gindex = 0;
1595 }
1596 #ifdef CHECK
1597 for (ucs4 = 0; ucs4 < 0x10000; ucs4++)
1598 {
1599 FcBool FT_Has, FC_Has;
1600
1601 FT_Has = FT_Get_Char_Index (face, ucs4) != 0;
1602 FC_Has = FcCharSetHasChar (fcs, ucs4);
1603 if (FT_Has != FC_Has)
1604 {
1605 printf ("0x%08x FT says %d FC says %d\n", ucs4, FT_Has, FC_Has);
1606 }
1607 }
1608 #endif
1609 }
1610 }
1611 #ifdef CHECK
1612 printf ("%d glyphs %d encoded\n", (int) face->num_glyphs, FcCharSetCount (fcs));
1613 for (ucs4 = 0; ucs4 <= font_max; ucs4++)
1614 {
1615 FcBool has_char = FcFreeTypeCharIndex (face, ucs4) != 0;
1616 FcBool has_bit = FcCharSetHasChar (fcs, ucs4);
1617
1618 if (has_char && !has_bit)
1619 printf ("Bitmap missing char 0x%x\n", ucs4);
1620 else if (!has_char && has_bit)
1621 printf ("Bitmap extra char 0x%x\n", ucs4);
1622 }
1623 #endif
1624 if (fixed_advance)
1625 *spacing = FC_MONO;
1626 else
1627 *spacing = FC_PROPORTIONAL;
1628 return fcs;
1629 bail1:
1630 FcCharSetDestroy (fcs);
1631 bail0:
1632 return 0;
1633 }
1634
1635 FcCharSet *
1636 FcFreeTypeCharSet (FT_Face face, FcBlanks *blanks)
1637 {
1638 int spacing;
1639
1640 return FcFreeTypeCharSetAndSpacing (face, blanks, &spacing);
1641 }