Remove experimental code inadvertently introduced when reverting the
[emacs.git] / src / ftfont.c
blobeb8893965be53608dedf3013f7f58b20ca0321cf
1 /* ftfont.c -- FreeType font driver.
2 Copyright (C) 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
3 Copyright (C) 2006, 2007, 2008, 2009
4 National Institute of Advanced Industrial Science and Technology (AIST)
5 Registration Number H13PRO009
7 This file is part of GNU Emacs.
9 GNU Emacs is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
14 GNU Emacs is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
22 #include <config.h>
23 #include <stdio.h>
25 #include <fontconfig/fontconfig.h>
26 #include <fontconfig/fcfreetype.h>
28 #include "lisp.h"
29 #include "dispextern.h"
30 #include "frame.h"
31 #include "blockinput.h"
32 #include "character.h"
33 #include "charset.h"
34 #include "coding.h"
35 #include "composite.h"
36 #include "fontset.h"
37 #include "font.h"
38 #include "ftfont.h"
40 /* Symbolic type of this font-driver. */
41 Lisp_Object Qfreetype;
43 /* Fontconfig's generic families and their aliases. */
44 static Lisp_Object Qmonospace, Qsans_serif, Qserif, Qmono, Qsans, Qsans__serif;
46 /* Flag to tell if FcInit is already called or not. */
47 static int fc_initialized;
49 /* Handle to a FreeType library instance. */
50 static FT_Library ft_library;
52 /* Cache for FreeType fonts. */
53 static Lisp_Object freetype_font_cache;
55 /* Cache for FT_Face and FcCharSet. */
56 static Lisp_Object ft_face_cache;
58 /* The actual structure for FreeType font that can be casted to struct
59 font. */
61 struct ftfont_info
63 struct font font;
64 #ifdef HAVE_LIBOTF
65 /* The following three members must be here in this order to be
66 compatible with struct xftfont_info (in xftfont.c). */
67 int maybe_otf; /* Flag to tell if this may be OTF or not. */
68 OTF *otf;
69 #endif /* HAVE_LIBOTF */
70 FT_Size ft_size;
71 int index;
74 static Lisp_Object ftfont_pattern_entity P_ ((FcPattern *, Lisp_Object));
76 static Lisp_Object ftfont_resolve_generic_family P_ ((Lisp_Object,
77 FcPattern *));
78 static Lisp_Object ftfont_lookup_cache P_ ((Lisp_Object, int));
80 Lisp_Object ftfont_font_format P_ ((FcPattern *, Lisp_Object));
82 #define SYMBOL_FcChar8(SYM) (FcChar8 *) SDATA (SYMBOL_NAME (SYM))
84 static struct
86 /* registry name */
87 char *name;
88 /* characters to distinguish the charset from the others */
89 int uniquifier[6];
90 /* additional constraint by language */
91 char *lang;
92 /* set on demand */
93 FcCharSet *fc_charset;
94 } fc_charset_table[] =
95 { { "iso8859-1", { 0x00A0, 0x00A1, 0x00B4, 0x00BC, 0x00D0 } },
96 { "iso8859-2", { 0x00A0, 0x010E }},
97 { "iso8859-3", { 0x00A0, 0x0108 }},
98 { "iso8859-4", { 0x00A0, 0x00AF, 0x0128, 0x0156, 0x02C7 }},
99 { "iso8859-5", { 0x00A0, 0x0401 }},
100 { "iso8859-6", { 0x00A0, 0x060C }},
101 { "iso8859-7", { 0x00A0, 0x0384 }},
102 { "iso8859-8", { 0x00A0, 0x05D0 }},
103 { "iso8859-9", { 0x00A0, 0x00A1, 0x00BC, 0x011E }},
104 { "iso8859-10", { 0x00A0, 0x00D0, 0x0128, 0x2015 }},
105 { "iso8859-11", { 0x00A0, 0x0E01 }},
106 { "iso8859-13", { 0x00A0, 0x201C }},
107 { "iso8859-14", { 0x00A0, 0x0174 }},
108 { "iso8859-15", { 0x00A0, 0x00A1, 0x00D0, 0x0152 }},
109 { "iso8859-16", { 0x00A0, 0x0218}},
110 { "gb2312.1980-0", { 0x4E13 }, "zh-cn"},
111 { "big5-0", { 0xF6B1 }, "zh-tw" },
112 { "jisx0208.1983-0", { 0x4E55 }, "ja"},
113 { "ksc5601.1985-0", { 0xAC00 }, "ko"},
114 { "cns11643.1992-1", { 0xFE32 }, "zh-tw"},
115 { "cns11643.1992-2", { 0x4E33, 0x7934 }},
116 { "cns11643.1992-3", { 0x201A9 }},
117 { "cns11643.1992-4", { 0x20057 }},
118 { "cns11643.1992-5", { 0x20000 }},
119 { "cns11643.1992-6", { 0x20003 }},
120 { "cns11643.1992-7", { 0x20055 }},
121 { "gbk-0", { 0x4E06 }, "zh-cn"},
122 { "jisx0212.1990-0", { 0x4E44 }},
123 { "jisx0213.2000-1", { 0xFA10 }, "ja"},
124 { "jisx0213.2000-2", { 0xFA49 }},
125 { "jisx0213.2004-1", { 0x20B9F }},
126 { "viscii1.1-1", { 0x1EA0, 0x1EAE, 0x1ED2 }, "vi"},
127 { "tis620.2529-1", { 0x0E01 }, "th"},
128 { "windows-1251", { 0x0401, 0x0490 }, "ru"},
129 { "koi8-r", { 0x0401, 0x2219 }, "ru"},
130 { "mulelao-1", { 0x0E81 }, "lo"},
131 { "unicode-sip", { 0x20000 }},
132 { NULL }
135 extern Lisp_Object Qc, Qm, Qp, Qd;
137 static Lisp_Object
138 ftfont_pattern_entity (p, extra)
139 FcPattern *p;
140 Lisp_Object extra;
142 Lisp_Object entity;
143 char *file, *str;
144 int index;
145 int numeric;
146 double dbl;
147 FcBool b;
149 if (FcPatternGetString (p, FC_FILE, 0, (FcChar8 **) &file) != FcResultMatch)
150 return Qnil;
151 if (FcPatternGetInteger (p, FC_INDEX, 0, &index) != FcResultMatch)
152 return Qnil;
154 entity = font_make_entity ();
156 ASET (entity, FONT_TYPE_INDEX, Qfreetype);
157 ASET (entity, FONT_REGISTRY_INDEX, Qiso10646_1);
159 if (FcPatternGetString (p, FC_FOUNDRY, 0, (FcChar8 **) &str) == FcResultMatch)
160 ASET (entity, FONT_FOUNDRY_INDEX, font_intern_prop (str, strlen (str), 1));
161 if (FcPatternGetString (p, FC_FAMILY, 0, (FcChar8 **) &str) == FcResultMatch)
162 ASET (entity, FONT_FAMILY_INDEX, font_intern_prop (str, strlen (str), 1));
163 if (FcPatternGetInteger (p, FC_WEIGHT, 0, &numeric) == FcResultMatch)
165 if (numeric >= FC_WEIGHT_REGULAR && numeric < FC_WEIGHT_MEDIUM)
166 numeric = FC_WEIGHT_MEDIUM;
167 FONT_SET_STYLE (entity, FONT_WEIGHT_INDEX, make_number (numeric));
169 if (FcPatternGetInteger (p, FC_SLANT, 0, &numeric) == FcResultMatch)
171 numeric += 100;
172 FONT_SET_STYLE (entity, FONT_SLANT_INDEX, make_number (numeric));
174 if (FcPatternGetInteger (p, FC_WIDTH, 0, &numeric) == FcResultMatch)
176 FONT_SET_STYLE (entity, FONT_WIDTH_INDEX, make_number (numeric));
178 if (FcPatternGetDouble (p, FC_PIXEL_SIZE, 0, &dbl) == FcResultMatch)
179 ASET (entity, FONT_SIZE_INDEX, make_number (dbl));
180 else
181 ASET (entity, FONT_SIZE_INDEX, make_number (0));
182 if (FcPatternGetInteger (p, FC_SPACING, 0, &numeric) == FcResultMatch)
183 ASET (entity, FONT_SPACING_INDEX, make_number (numeric));
184 if (FcPatternGetDouble (p, FC_DPI, 0, &dbl) == FcResultMatch)
186 int dpi = dbl;
187 ASET (entity, FONT_DPI_INDEX, make_number (dpi));
189 if (FcPatternGetBool (p, FC_SCALABLE, 0, &b) == FcResultMatch
190 && b == FcTrue)
191 ASET (entity, FONT_AVGWIDTH_INDEX, make_number (0));
193 ASET (entity, FONT_EXTRA_INDEX, Fcopy_sequence (extra));
194 font_put_extra (entity, QCfont_entity,
195 Fcons (make_unibyte_string ((char *) file,
196 strlen ((char *) file)),
197 make_number (index)));
198 return entity;
202 static Lisp_Object ftfont_generic_family_list;
204 static Lisp_Object
205 ftfont_resolve_generic_family (family, pattern)
206 Lisp_Object family;
207 FcPattern *pattern;
209 Lisp_Object slot;
210 FcPattern *match;
211 FcResult result;
212 FcLangSet *langset;
214 family = Fintern (Fdowncase (SYMBOL_NAME (family)), Qnil);
215 if (EQ (family, Qmono))
216 family = Qmonospace;
217 else if (EQ (family, Qsans) || EQ (family, Qsans__serif))
218 family = Qsans_serif;
219 slot = assq_no_quit (family, ftfont_generic_family_list);
220 if (! CONSP (slot))
221 return Qnil;
222 if (! EQ (XCDR (slot), Qt))
223 return XCDR (slot);
224 pattern = FcPatternDuplicate (pattern);
225 if (! pattern)
226 goto err;
227 FcPatternDel (pattern, FC_FOUNDRY);
228 FcPatternDel (pattern, FC_FAMILY);
229 FcPatternAddString (pattern, FC_FAMILY, SYMBOL_FcChar8 (family));
230 if (FcPatternGetLangSet (pattern, FC_LANG, 0, &langset) != FcResultMatch)
232 /* This is to avoid the effect of locale. */
233 langset = FcLangSetCreate ();
234 FcLangSetAdd (langset, "en");
235 FcPatternAddLangSet (pattern, FC_LANG, langset);
236 FcLangSetDestroy (langset);
238 FcConfigSubstitute (NULL, pattern, FcMatchPattern);
239 FcDefaultSubstitute (pattern);
240 match = FcFontMatch (NULL, pattern, &result);
241 if (match)
243 FcChar8 *fam;
245 if (FcPatternGetString (match, FC_FAMILY, 0, &fam) == FcResultMatch)
246 family = intern ((char *) fam);
248 else
249 family = Qnil;
250 XSETCDR (slot, family);
251 if (match) FcPatternDestroy (match);
252 err:
253 if (pattern) FcPatternDestroy (pattern);
254 return family;
257 struct ftfont_cache_data
259 FT_Face ft_face;
260 FcCharSet *fc_charset;
263 static Lisp_Object
264 ftfont_lookup_cache (key, for_face)
265 Lisp_Object key;
266 int for_face;
268 Lisp_Object cache, val;
269 struct ftfont_cache_data *cache_data;
271 cache = assoc_no_quit (key, ft_face_cache);
272 if (NILP (cache))
274 cache_data = xmalloc (sizeof (struct ftfont_cache_data));
275 cache_data->ft_face = NULL;
276 cache_data->fc_charset = NULL;
277 val = make_save_value (NULL, 0);
278 XSAVE_VALUE (val)->integer = 0;
279 XSAVE_VALUE (val)->pointer = cache_data;
280 cache = Fcons (key, val);
281 ft_face_cache = Fcons (cache, ft_face_cache);
283 else
285 val = XCDR (cache);
286 cache_data = XSAVE_VALUE (val)->pointer;
288 if (for_face ? ! cache_data->ft_face : ! cache_data->fc_charset)
290 char *filename = (char *) SDATA (XCAR (key));
291 int index = XINT (XCDR (key));
293 if (for_face)
295 if (! ft_library
296 && FT_Init_FreeType (&ft_library) != 0)
297 return Qnil;
298 if (FT_New_Face (ft_library, filename, index, &cache_data->ft_face)
299 != 0)
300 return Qnil;
302 else
304 FcPattern *pat;
305 FcFontSet *fontset;
306 FcObjectSet *objset;
307 FcCharSet *charset;
309 pat = FcPatternBuild (0, FC_FILE, FcTypeString, (FcChar8 *) filename,
310 FC_INDEX, FcTypeInteger, index, NULL);
311 objset = FcObjectSetBuild (FC_CHARSET, NULL);
312 fontset = FcFontList (NULL, pat, objset);
313 xassert (fontset && fontset->nfont > 0);
314 if (FcPatternGetCharSet (fontset->fonts[0], FC_CHARSET, 0, &charset)
315 == FcResultMatch)
316 cache_data->fc_charset = FcCharSetCopy (charset);
317 else
318 cache_data->fc_charset = FcCharSetCreate ();
319 FcFontSetDestroy (fontset);
320 FcObjectSetDestroy (objset);
321 FcPatternDestroy (pat);
324 return cache;
327 FcCharSet *
328 ftfont_get_fc_charset (entity)
329 Lisp_Object entity;
331 Lisp_Object val, cache;
332 struct ftfont_cache_data *cache_data;
334 val = assq_no_quit (QCfont_entity, AREF (entity, FONT_EXTRA_INDEX));
335 xassert (CONSP (val));
336 val = XCDR (val);
337 cache = ftfont_lookup_cache (val, 0);
338 val = XCDR (cache);
339 cache_data = XSAVE_VALUE (val)->pointer;
340 return cache_data->fc_charset;
343 #ifdef HAVE_LIBOTF
344 static OTF *
345 ftfont_get_otf (ftfont_info)
346 struct ftfont_info *ftfont_info;
348 OTF *otf;
350 if (ftfont_info->otf)
351 return ftfont_info->otf;
352 if (! ftfont_info->maybe_otf)
353 return NULL;
354 otf = OTF_open_ft_face (ftfont_info->ft_size->face);
355 if (! otf || OTF_get_table (otf, "head") < 0)
357 if (otf)
358 OTF_close (otf);
359 ftfont_info->maybe_otf = 0;
360 return NULL;
362 ftfont_info->otf = otf;
363 return otf;
365 #endif /* HAVE_LIBOTF */
367 static Lisp_Object ftfont_get_cache P_ ((FRAME_PTR));
368 static Lisp_Object ftfont_list P_ ((Lisp_Object, Lisp_Object));
369 static Lisp_Object ftfont_match P_ ((Lisp_Object, Lisp_Object));
370 static Lisp_Object ftfont_list_family P_ ((Lisp_Object));
371 static Lisp_Object ftfont_open P_ ((FRAME_PTR, Lisp_Object, int));
372 static void ftfont_close P_ ((FRAME_PTR, struct font *));
373 static int ftfont_has_char P_ ((Lisp_Object, int));
374 static unsigned ftfont_encode_char P_ ((struct font *, int));
375 static int ftfont_text_extents P_ ((struct font *, unsigned *, int,
376 struct font_metrics *));
377 static int ftfont_get_bitmap P_ ((struct font *, unsigned,
378 struct font_bitmap *, int));
379 static int ftfont_anchor_point P_ ((struct font *, unsigned, int,
380 int *, int *));
381 static Lisp_Object ftfont_otf_capability P_ ((struct font *));
382 static Lisp_Object ftfont_shape P_ ((Lisp_Object));
383 static int ftfont_variation_glyphs P_ ((struct font *, int c,
384 unsigned variations[256]));
386 struct font_driver ftfont_driver =
388 0, /* Qfreetype */
389 0, /* case insensitive */
390 ftfont_get_cache,
391 ftfont_list,
392 ftfont_match,
393 ftfont_list_family,
394 NULL, /* free_entity */
395 ftfont_open,
396 ftfont_close,
397 /* We can't draw a text without device dependent functions. */
398 NULL, /* prepare_face */
399 NULL, /* done_face */
400 ftfont_has_char,
401 ftfont_encode_char,
402 ftfont_text_extents,
403 /* We can't draw a text without device dependent functions. */
404 NULL, /* draw */
405 ftfont_get_bitmap,
406 NULL, /* get_bitmap */
407 NULL, /* free_bitmap */
408 NULL, /* get_outline */
409 ftfont_anchor_point,
410 #ifdef HAVE_LIBOTF
411 ftfont_otf_capability,
412 #else /* not HAVE_LIBOTF */
413 NULL,
414 #endif /* not HAVE_LIBOTF */
415 NULL, /* otf_drive */
416 NULL, /* start_for_frame */
417 NULL, /* end_for_frame */
418 #if defined (HAVE_M17N_FLT) && defined (HAVE_LIBOTF)
419 ftfont_shape,
420 #else /* not (HAVE_M17N_FLT && HAVE_LIBOTF) */
421 NULL,
422 #endif /* not (HAVE_M17N_FLT && HAVE_LIBOTF) */
423 NULL, /* check */
424 #ifdef HAVE_OTF_GET_VARIATION_GLYPHS
425 ftfont_variation_glyphs
426 #else
427 NULL
428 #endif
431 extern Lisp_Object QCname;
433 static Lisp_Object
434 ftfont_get_cache (f)
435 FRAME_PTR f;
437 return freetype_font_cache;
440 static int
441 ftfont_get_charset (registry)
442 Lisp_Object registry;
444 char *str = (char *) SDATA (SYMBOL_NAME (registry));
445 char *re = alloca (SBYTES (SYMBOL_NAME (registry)) * 2 + 1);
446 Lisp_Object regexp;
447 int i, j;
449 for (i = j = 0; i < SBYTES (SYMBOL_NAME (registry)); i++, j++)
451 if (str[i] == '.')
452 re[j++] = '\\';
453 else if (str[i] == '*')
454 re[j++] = '.';
455 re[j] = str[i];
456 if (re[j] == '?')
457 re[j] = '.';
459 re[j] = '\0';
460 regexp = make_unibyte_string (re, j);
461 for (i = 0; fc_charset_table[i].name; i++)
462 if (fast_c_string_match_ignore_case (regexp, fc_charset_table[i].name) >= 0)
463 break;
464 if (! fc_charset_table[i].name)
465 return -1;
466 if (! fc_charset_table[i].fc_charset)
468 FcCharSet *charset = FcCharSetCreate ();
469 int *uniquifier = fc_charset_table[i].uniquifier;
471 if (! charset)
472 return -1;
473 for (j = 0; uniquifier[j]; j++)
474 if (! FcCharSetAddChar (charset, uniquifier[j]))
476 FcCharSetDestroy (charset);
477 return -1;
479 fc_charset_table[i].fc_charset = charset;
481 return i;
484 struct OpenTypeSpec
486 Lisp_Object script;
487 unsigned int script_tag, langsys_tag;
488 int nfeatures[2];
489 unsigned int *features[2];
492 #define OTF_SYM_TAG(SYM, TAG) \
493 do { \
494 unsigned char *p = SDATA (SYMBOL_NAME (SYM)); \
495 TAG = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]; \
496 } while (0)
498 #define OTF_TAG_STR(TAG, P) \
499 do { \
500 (P)[0] = (char) (TAG >> 24); \
501 (P)[1] = (char) ((TAG >> 16) & 0xFF); \
502 (P)[2] = (char) ((TAG >> 8) & 0xFF); \
503 (P)[3] = (char) (TAG & 0xFF); \
504 (P)[4] = '\0'; \
505 } while (0)
507 #define OTF_TAG_SYM(SYM, TAG) \
508 do { \
509 char str[5]; \
511 OTF_TAG_STR (TAG, str); \
512 (SYM) = font_intern_prop (str, 4, 1); \
513 } while (0)
516 static struct OpenTypeSpec *
517 ftfont_get_open_type_spec (Lisp_Object otf_spec)
519 struct OpenTypeSpec *spec = malloc (sizeof (struct OpenTypeSpec));
520 Lisp_Object val;
521 int i, j, negative;
523 if (! spec)
524 return NULL;
525 spec->script = XCAR (otf_spec);
526 if (! NILP (spec->script))
528 OTF_SYM_TAG (spec->script, spec->script_tag);
529 val = assq_no_quit (spec->script, Votf_script_alist);
530 if (CONSP (val) && SYMBOLP (XCDR (val)))
531 spec->script = XCDR (val);
532 else
533 spec->script = Qnil;
535 else
536 spec->script_tag = 0x44464C54; /* "DFLT" */
537 otf_spec = XCDR (otf_spec);
538 val = XCAR (otf_spec);
539 if (! NILP (val))
540 OTF_SYM_TAG (val, spec->langsys_tag);
541 else
542 spec->langsys_tag = 0;
543 spec->nfeatures[0] = spec->nfeatures[1] = 0;
544 for (i = 0; i < 2; i++)
546 Lisp_Object len;
548 otf_spec = XCDR (otf_spec);
549 if (NILP (otf_spec))
550 break;
551 val = XCAR (otf_spec);
552 if (NILP (val))
553 continue;
554 len = Flength (val);
555 spec->features[i] = malloc (sizeof (int) * XINT (len));
556 if (! spec->features[i])
558 if (i > 0 && spec->features[0])
559 free (spec->features[0]);
560 free (spec);
561 return NULL;
563 for (j = 0, negative = 0; CONSP (val); val = XCDR (val))
565 if (NILP (XCAR (val)))
566 negative = 1;
567 else
569 unsigned int tag;
571 OTF_SYM_TAG (XCAR (val), tag);
572 spec->features[i][j++] = negative ? tag & 0x80000000 : tag;
575 spec->nfeatures[i] = j;
577 return spec;
580 static FcPattern *ftfont_spec_pattern P_ ((Lisp_Object, char *,
581 struct OpenTypeSpec **));
583 static FcPattern *
584 ftfont_spec_pattern (spec, otlayout, otspec)
585 Lisp_Object spec;
586 char *otlayout;
587 struct OpenTypeSpec **otspec;
589 Lisp_Object tmp, extra;
590 FcPattern *pattern = NULL;
591 FcCharSet *charset = NULL;
592 FcLangSet *langset = NULL;
593 int n;
594 int dpi = -1;
595 int scalable = -1;
596 Lisp_Object script = Qnil;
597 Lisp_Object registry;
598 int fc_charset_idx;
600 if (! NILP (AREF (spec, FONT_ADSTYLE_INDEX))
601 && SBYTES (SYMBOL_NAME (AREF (spec, FONT_ADSTYLE_INDEX))) > 0)
602 /* Fontconfig doesn't support adstyle property. */
603 return NULL;
604 if ((n = FONT_SLANT_NUMERIC (spec)) >= 0
605 && n < 100)
606 /* Fontconfig doesn't support reverse-italic/obligue. */
607 return NULL;
609 if (INTEGERP (AREF (spec, FONT_DPI_INDEX)))
610 dpi = XINT (AREF (spec, FONT_DPI_INDEX));
611 if (INTEGERP (AREF (spec, FONT_AVGWIDTH_INDEX))
612 && XINT (AREF (spec, FONT_AVGWIDTH_INDEX)) == 0)
613 scalable = 1;
615 registry = AREF (spec, FONT_REGISTRY_INDEX);
616 if (NILP (registry)
617 || EQ (registry, Qascii_0)
618 || EQ (registry, Qiso10646_1)
619 || EQ (registry, Qunicode_bmp))
620 fc_charset_idx = -1;
621 else
623 FcChar8 *lang;
625 fc_charset_idx = ftfont_get_charset (registry);
626 if (fc_charset_idx < 0)
627 return NULL;
628 charset = fc_charset_table[fc_charset_idx].fc_charset;
629 lang = (FcChar8 *) fc_charset_table[fc_charset_idx].lang;
630 if (lang)
632 langset = FcLangSetCreate ();
633 if (! langset)
634 goto err;
635 FcLangSetAdd (langset, lang);
639 otlayout[0] = '\0';
640 for (extra = AREF (spec, FONT_EXTRA_INDEX);
641 CONSP (extra); extra = XCDR (extra))
643 Lisp_Object key, val;
645 key = XCAR (XCAR (extra)), val = XCDR (XCAR (extra));
646 if (EQ (key, QCdpi))
647 dpi = XINT (val);
648 else if (EQ (key, QClang))
650 if (! langset)
651 langset = FcLangSetCreate ();
652 if (! langset)
653 goto err;
654 if (SYMBOLP (val))
656 if (! FcLangSetAdd (langset, SYMBOL_FcChar8 (val)))
657 goto err;
659 else
660 for (; CONSP (val); val = XCDR (val))
661 if (SYMBOLP (XCAR (val))
662 && ! FcLangSetAdd (langset, SYMBOL_FcChar8 (XCAR (val))))
663 goto err;
665 else if (EQ (key, QCotf))
667 *otspec = ftfont_get_open_type_spec (val);
668 if (! *otspec)
669 return NULL;
670 strcat (otlayout, "otlayout:");
671 OTF_TAG_STR ((*otspec)->script_tag, otlayout + 9);
672 script = (*otspec)->script;
674 else if (EQ (key, QCscript))
675 script = val;
676 else if (EQ (key, QCscalable))
677 scalable = ! NILP (val);
680 if (! NILP (script) && ! charset)
682 Lisp_Object chars = assq_no_quit (script, Vscript_representative_chars);
684 if (CONSP (chars) && CONSP (CDR (chars)))
686 charset = FcCharSetCreate ();
687 if (! charset)
688 goto err;
689 for (chars = XCDR (chars); CONSP (chars); chars = XCDR (chars))
690 if (CHARACTERP (XCAR (chars))
691 && ! FcCharSetAddChar (charset, XUINT (XCAR (chars))))
692 goto err;
696 pattern = FcPatternCreate ();
697 if (! pattern)
698 goto err;
699 tmp = AREF (spec, FONT_FOUNDRY_INDEX);
700 if (! NILP (tmp)
701 && ! FcPatternAddString (pattern, FC_FOUNDRY, SYMBOL_FcChar8 (tmp)))
702 goto err;
703 tmp = AREF (spec, FONT_FAMILY_INDEX);
704 if (! NILP (tmp)
705 && ! FcPatternAddString (pattern, FC_FAMILY, SYMBOL_FcChar8 (tmp)))
706 goto err;
707 if (charset
708 && ! FcPatternAddCharSet (pattern, FC_CHARSET, charset))
709 goto err;
710 if (langset
711 && ! FcPatternAddLangSet (pattern, FC_LANG, langset))
712 goto err;
713 if (dpi >= 0
714 && ! FcPatternAddDouble (pattern, FC_DPI, dpi))
715 goto err;
716 if (scalable >= 0
717 && ! FcPatternAddBool (pattern, FC_SCALABLE, scalable ? FcTrue : FcFalse))
718 goto err;
720 goto finish;
722 err:
723 /* We come here because of unexpected error in fontconfig API call
724 (usually insufficient memory). */
725 if (pattern)
727 FcPatternDestroy (pattern);
728 pattern = NULL;
730 if (*otspec)
732 if ((*otspec)->nfeatures[0] > 0)
733 free ((*otspec)->features[0]);
734 if ((*otspec)->nfeatures[1] > 0)
735 free ((*otspec)->features[1]);
736 free (*otspec);
737 *otspec = NULL;
740 finish:
741 if (langset) FcLangSetDestroy (langset);
742 if (charset && fc_charset_idx < 0) FcCharSetDestroy (charset);
743 return pattern;
746 static Lisp_Object
747 ftfont_list (frame, spec)
748 Lisp_Object frame, spec;
750 Lisp_Object val = Qnil, family;
751 int i;
752 FcPattern *pattern;
753 FcFontSet *fontset = NULL;
754 FcObjectSet *objset = NULL;
755 FcCharSet *charset;
756 Lisp_Object chars = Qnil;
757 FcResult result;
758 char otlayout[15]; /* For "otlayout:XXXX" */
759 struct OpenTypeSpec *otspec = NULL;
760 int spacing = -1;
762 if (! fc_initialized)
764 FcInit ();
765 fc_initialized = 1;
768 pattern = ftfont_spec_pattern (spec, otlayout, &otspec);
769 if (! pattern)
770 return Qnil;
771 if (FcPatternGetCharSet (pattern, FC_CHARSET, 0, &charset) != FcResultMatch)
773 val = assq_no_quit (QCscript, AREF (spec, FONT_EXTRA_INDEX));
774 if (! NILP (val))
776 val = assq_no_quit (XCDR (val), Vscript_representative_chars);
777 if (CONSP (val) && VECTORP (XCDR (val)))
778 chars = XCDR (val);
780 val = Qnil;
782 if (INTEGERP (AREF (spec, FONT_SPACING_INDEX)))
783 spacing = XINT (AREF (spec, FONT_SPACING_INDEX));
784 family = AREF (spec, FONT_FAMILY_INDEX);
785 if (! NILP (family))
787 Lisp_Object resolved;
789 resolved = ftfont_resolve_generic_family (family, pattern);
790 if (! NILP (resolved))
792 FcPatternDel (pattern, FC_FAMILY);
793 if (! FcPatternAddString (pattern, FC_FAMILY,
794 SYMBOL_FcChar8 (resolved)))
795 goto err;
799 objset = FcObjectSetBuild (FC_FOUNDRY, FC_FAMILY, FC_WEIGHT, FC_SLANT,
800 FC_WIDTH, FC_PIXEL_SIZE, FC_SPACING, FC_SCALABLE,
801 FC_FILE, FC_INDEX,
802 #ifdef FC_CAPABILITY
803 FC_CAPABILITY,
804 #endif /* FC_CAPABILITY */
805 #ifdef FC_FONTFORMAT
806 FC_FONTFORMAT,
807 #endif
808 NULL);
809 if (! objset)
810 goto err;
811 if (! NILP (chars))
812 FcObjectSetAdd (objset, FC_CHARSET);
814 fontset = FcFontList (NULL, pattern, objset);
815 if (! fontset || fontset->nfont == 0)
816 goto finish;
817 #if 0
818 /* Need fix because this finds any fonts. */
819 if (fontset->nfont == 0 && ! NILP (family))
821 /* Try maching with configuration. For instance, the
822 configuration may specify "Nimbus Mono L" as an alias of
823 "Courier". */
824 FcPattern *pat = FcPatternBuild (0, FC_FAMILY, FcTypeString,
825 SYMBOL_FcChar8 (family), NULL);
826 FcChar8 *fam;
828 if (FcConfigSubstitute (NULL, pat, FcMatchPattern) == FcTrue)
830 for (i = 0;
831 FcPatternGetString (pat, FC_FAMILY, i, &fam) == FcResultMatch;
832 i++)
834 FcPatternDel (pattern, FC_FAMILY);
835 FcPatternAddString (pattern, FC_FAMILY, fam);
836 FcFontSetDestroy (fontset);
837 fontset = FcFontList (NULL, pattern, objset);
838 if (fontset->nfont > 0)
839 break;
843 #endif
844 for (i = 0; i < fontset->nfont; i++)
846 Lisp_Object entity;
848 if (spacing >= 0)
850 int this;
852 if ((FcPatternGetInteger (fontset->fonts[i], FC_SPACING, 0, &this)
853 == FcResultMatch)
854 && spacing != this)
855 continue;
858 #ifdef FC_CAPABILITY
859 if (otlayout[0])
861 FcChar8 *this;
863 if (FcPatternGetString (fontset->fonts[i], FC_CAPABILITY, 0, &this)
864 != FcResultMatch
865 || ! strstr ((char *) this, otlayout))
866 continue;
868 #endif /* FC_CAPABILITY */
869 #ifdef HAVE_LIBOTF
870 if (otspec)
872 FcChar8 *file;
873 OTF *otf;
875 if (FcPatternGetString (fontset->fonts[i], FC_FILE, 0, &file)
876 != FcResultMatch)
877 continue;
878 otf = OTF_open ((char *) file);
879 if (! otf)
880 continue;
881 if (OTF_check_features (otf, 1,
882 otspec->script_tag, otspec->langsys_tag,
883 otspec->features[0],
884 otspec->nfeatures[0]) != 1
885 || OTF_check_features (otf, 0,
886 otspec->script_tag, otspec->langsys_tag,
887 otspec->features[1],
888 otspec->nfeatures[1]) != 1)
889 continue;
891 #endif /* HAVE_LIBOTF */
892 if (VECTORP (chars))
894 int j;
896 if (FcPatternGetCharSet (fontset->fonts[i], FC_CHARSET, 0, &charset)
897 != FcResultMatch)
898 continue;
899 for (j = 0; j < ASIZE (chars); j++)
900 if (NATNUMP (AREF (chars, j))
901 && FcCharSetHasChar (charset, XFASTINT (AREF (chars, j))))
902 break;
903 if (j == ASIZE (chars))
904 continue;
906 entity = ftfont_pattern_entity (fontset->fonts[i],
907 AREF (spec, FONT_EXTRA_INDEX));
908 if (! NILP (entity))
909 val = Fcons (entity, val);
911 val = Fnreverse (val);
912 goto finish;
914 err:
915 /* We come here because of unexpected error in fontconfig API call
916 (usually insufficient memory). */
917 val = Qnil;
919 finish:
920 font_add_log ("ftfont-list", spec, val);
921 if (objset) FcObjectSetDestroy (objset);
922 if (fontset) FcFontSetDestroy (fontset);
923 if (pattern) FcPatternDestroy (pattern);
924 return val;
927 static Lisp_Object
928 ftfont_match (frame, spec)
929 Lisp_Object frame, spec;
931 Lisp_Object entity = Qnil;
932 FcPattern *pattern, *match = NULL;
933 FcResult result;
934 char otlayout[15]; /* For "otlayout:XXXX" */
935 struct OpenTypeSpec *otspec = NULL;
937 if (! fc_initialized)
939 FcInit ();
940 fc_initialized = 1;
943 pattern = ftfont_spec_pattern (spec, otlayout, &otspec);
944 if (! pattern)
945 return Qnil;
947 if (INTEGERP (AREF (spec, FONT_SIZE_INDEX)))
949 FcValue value;
951 value.type = FcTypeDouble;
952 value.u.d = XINT (AREF (spec, FONT_SIZE_INDEX));
953 FcPatternAdd (pattern, FC_PIXEL_SIZE, value, FcFalse);
955 if (FcConfigSubstitute (NULL, pattern, FcMatchPattern) == FcTrue)
957 FcDefaultSubstitute (pattern);
958 match = FcFontMatch (NULL, pattern, &result);
959 if (match)
961 entity = ftfont_pattern_entity (match, AREF (spec, FONT_EXTRA_INDEX));
962 FcPatternDestroy (match);
963 if (! NILP (AREF (spec, FONT_FAMILY_INDEX))
964 && NILP (assq_no_quit (AREF (spec, FONT_FAMILY_INDEX),
965 ftfont_generic_family_list))
966 && NILP (Fstring_equal (AREF (spec, FONT_FAMILY_INDEX),
967 AREF (entity, FONT_FAMILY_INDEX))))
968 entity = Qnil;
971 FcPatternDestroy (pattern);
973 font_add_log ("ftfont-match", spec, entity);
974 return entity;
977 static Lisp_Object
978 ftfont_list_family (frame)
979 Lisp_Object frame;
981 Lisp_Object list = Qnil;
982 FcPattern *pattern = NULL;
983 FcFontSet *fontset = NULL;
984 FcObjectSet *objset = NULL;
985 int i;
987 if (! fc_initialized)
989 FcInit ();
990 fc_initialized = 1;
993 pattern = FcPatternCreate ();
994 if (! pattern)
995 goto finish;
996 objset = FcObjectSetBuild (FC_FAMILY, NULL);
997 if (! objset)
998 goto finish;
999 fontset = FcFontList (NULL, pattern, objset);
1000 if (! fontset)
1001 goto finish;
1003 for (i = 0; i < fontset->nfont; i++)
1005 FcPattern *pat = fontset->fonts[i];
1006 FcChar8 *str;
1008 if (FcPatternGetString (pat, FC_FAMILY, 0, &str) == FcResultMatch)
1009 list = Fcons (intern ((char *) str), list);
1012 finish:
1013 if (objset) FcObjectSetDestroy (objset);
1014 if (fontset) FcFontSetDestroy (fontset);
1015 if (pattern) FcPatternDestroy (pattern);
1017 return list;
1021 static Lisp_Object
1022 ftfont_open (f, entity, pixel_size)
1023 FRAME_PTR f;
1024 Lisp_Object entity;
1025 int pixel_size;
1027 struct ftfont_info *ftfont_info;
1028 struct font *font;
1029 struct ftfont_cache_data *cache_data;
1030 FT_Face ft_face;
1031 FT_Size ft_size;
1032 FT_UInt size;
1033 Lisp_Object val, filename, index, cache, font_object;
1034 int scalable;
1035 int spacing;
1036 char name[256];
1037 int i, len;
1038 int upEM;
1040 val = assq_no_quit (QCfont_entity, AREF (entity, FONT_EXTRA_INDEX));
1041 if (! CONSP (val))
1042 return Qnil;
1043 val = XCDR (val);
1044 cache = ftfont_lookup_cache (val, 1);
1045 if (NILP (cache))
1046 return Qnil;
1047 filename = XCAR (val);
1048 index = XCDR (val);
1049 val = XCDR (cache);
1050 cache_data = XSAVE_VALUE (XCDR (cache))->pointer;
1051 ft_face = cache_data->ft_face;
1052 if (XSAVE_VALUE (val)->integer > 0)
1054 /* FT_Face in this cache is already used by the different size. */
1055 if (FT_New_Size (ft_face, &ft_size) != 0)
1056 return Qnil;
1057 if (FT_Activate_Size (ft_size) != 0)
1059 FT_Done_Size (ft_size);
1060 return Qnil;
1063 XSAVE_VALUE (val)->integer++;
1064 size = XINT (AREF (entity, FONT_SIZE_INDEX));
1065 if (size == 0)
1066 size = pixel_size;
1067 if (FT_Set_Pixel_Sizes (ft_face, size, size) != 0)
1069 if (XSAVE_VALUE (val)->integer == 0)
1070 FT_Done_Face (ft_face);
1071 return Qnil;
1074 font_object = font_make_object (VECSIZE (struct ftfont_info), entity, size);
1075 ASET (font_object, FONT_TYPE_INDEX, Qfreetype);
1076 len = font_unparse_xlfd (entity, size, name, 256);
1077 if (len > 0)
1078 ASET (font_object, FONT_NAME_INDEX, make_string (name, len));
1079 len = font_unparse_fcname (entity, size, name, 256);
1080 if (len > 0)
1081 ASET (font_object, FONT_FULLNAME_INDEX, make_string (name, len));
1082 else
1083 ASET (font_object, FONT_FULLNAME_INDEX,
1084 AREF (font_object, FONT_NAME_INDEX));
1085 ASET (font_object, FONT_FILE_INDEX, filename);
1086 ASET (font_object, FONT_FORMAT_INDEX, ftfont_font_format (NULL, filename));
1087 font = XFONT_OBJECT (font_object);
1088 ftfont_info = (struct ftfont_info *) font;
1089 ftfont_info->ft_size = ft_face->size;
1090 ftfont_info->index = XINT (index);
1091 #ifdef HAVE_LIBOTF
1092 ftfont_info->maybe_otf = ft_face->face_flags & FT_FACE_FLAG_SFNT;
1093 ftfont_info->otf = NULL;
1094 #endif /* HAVE_LIBOTF */
1095 font->pixel_size = size;
1096 font->driver = &ftfont_driver;
1097 font->encoding_charset = font->repertory_charset = -1;
1099 upEM = ft_face->units_per_EM;
1100 scalable = (INTEGERP (AREF (entity, FONT_AVGWIDTH_INDEX))
1101 && XINT (AREF (entity, FONT_AVGWIDTH_INDEX)) == 0);
1102 if (scalable)
1104 font->ascent = ft_face->ascender * size / upEM;
1105 font->descent = - ft_face->descender * size / upEM;
1106 font->height = ft_face->height * size / upEM;
1108 else
1110 font->ascent = ft_face->size->metrics.ascender >> 6;
1111 font->descent = - ft_face->size->metrics.descender >> 6;
1112 font->height = ft_face->size->metrics.height >> 6;
1114 if (INTEGERP (AREF (entity, FONT_SPACING_INDEX)))
1115 spacing = XINT (AREF (entity, FONT_SPACING_INDEX));
1116 else
1117 spacing = FC_PROPORTIONAL;
1118 if (spacing != FC_PROPORTIONAL)
1119 font->min_width = font->average_width = font->space_width
1120 = (scalable ? ft_face->max_advance_width * size / upEM
1121 : ft_face->size->metrics.max_advance >> 6);
1122 else
1124 int n;
1126 font->min_width = font->average_width = font->space_width = 0;
1127 for (i = 32, n = 0; i < 127; i++)
1128 if (FT_Load_Char (ft_face, i, FT_LOAD_DEFAULT) != 0)
1130 int this_width = ft_face->glyph->metrics.horiAdvance >> 6;
1132 if (this_width > 0
1133 && (! font->min_width || font->min_width > this_width))
1134 font->min_width = this_width;
1135 if (i == 32)
1136 font->space_width = this_width;
1137 font->average_width += this_width;
1138 n++;
1140 if (n > 0)
1141 font->average_width /= n;
1144 font->baseline_offset = 0;
1145 font->relative_compose = 0;
1146 font->default_ascent = 0;
1147 font->vertical_centering = 0;
1148 if (scalable)
1150 font->underline_position = -ft_face->underline_position * size / upEM;
1151 font->underline_thickness = -ft_face->underline_thickness * size / upEM;
1153 else
1155 font->underline_position = -1;
1156 font->underline_thickness = 0;
1159 return font_object;
1162 static void
1163 ftfont_close (f, font)
1164 FRAME_PTR f;
1165 struct font *font;
1167 struct ftfont_info *ftfont_info = (struct ftfont_info *) font;
1168 Lisp_Object val, cache;
1170 val = Fcons (font->props[FONT_FILE_INDEX], make_number (ftfont_info->index));
1171 cache = ftfont_lookup_cache (val, 1);
1172 xassert (CONSP (cache));
1173 val = XCDR (cache);
1174 (XSAVE_VALUE (val)->integer)--;
1175 if (XSAVE_VALUE (val)->integer == 0)
1177 struct ftfont_cache_data *cache_data = XSAVE_VALUE (val)->pointer;
1179 FT_Done_Face (cache_data->ft_face);
1180 #ifdef HAVE_LIBOTF
1181 if (ftfont_info->otf)
1182 OTF_close (ftfont_info->otf);
1183 #endif
1184 cache_data->ft_face = NULL;
1186 else
1187 FT_Done_Size (ftfont_info->ft_size);
1190 static int
1191 ftfont_has_char (entity, c)
1192 Lisp_Object entity;
1193 int c;
1195 FcCharSet *charset = ftfont_get_fc_charset (entity);
1197 return (FcCharSetHasChar (charset, c) == FcTrue);
1200 static unsigned
1201 ftfont_encode_char (font, c)
1202 struct font *font;
1203 int c;
1205 struct ftfont_info *ftfont_info = (struct ftfont_info *) font;
1206 FT_Face ft_face = ftfont_info->ft_size->face;
1207 FT_ULong charcode = c;
1208 FT_UInt code = FT_Get_Char_Index (ft_face, charcode);
1210 return (code > 0 ? code : FONT_INVALID_CODE);
1213 static int
1214 ftfont_text_extents (font, code, nglyphs, metrics)
1215 struct font *font;
1216 unsigned *code;
1217 int nglyphs;
1218 struct font_metrics *metrics;
1220 struct ftfont_info *ftfont_info = (struct ftfont_info *) font;
1221 FT_Face ft_face = ftfont_info->ft_size->face;
1222 int width = 0;
1223 int i, first;
1225 if (ftfont_info->ft_size != ft_face->size)
1226 FT_Activate_Size (ftfont_info->ft_size);
1227 if (metrics)
1228 bzero (metrics, sizeof (struct font_metrics));
1229 for (i = 0, first = 1; i < nglyphs; i++)
1231 if (FT_Load_Glyph (ft_face, code[i], FT_LOAD_DEFAULT) == 0)
1233 FT_Glyph_Metrics *m = &ft_face->glyph->metrics;
1235 if (first)
1237 if (metrics)
1239 metrics->lbearing = m->horiBearingX >> 6;
1240 metrics->rbearing = (m->horiBearingX + m->width) >> 6;
1241 metrics->ascent = m->horiBearingY >> 6;
1242 metrics->descent = (m->horiBearingY + m->height) >> 6;
1244 first = 0;
1246 if (metrics)
1248 if (metrics->lbearing > width + (m->horiBearingX >> 6))
1249 metrics->lbearing = width + (m->horiBearingX >> 6);
1250 if (metrics->rbearing
1251 < width + ((m->horiBearingX + m->width) >> 6))
1252 metrics->rbearing
1253 = width + ((m->horiBearingX + m->width) >> 6);
1254 if (metrics->ascent < (m->horiBearingY >> 6))
1255 metrics->ascent = m->horiBearingY >> 6;
1256 if (metrics->descent > ((m->horiBearingY + m->height) >> 6))
1257 metrics->descent = (m->horiBearingY + m->height) >> 6;
1259 width += m->horiAdvance >> 6;
1261 else
1263 width += font->space_width;
1266 if (metrics)
1267 metrics->width = width;
1269 return width;
1272 static int
1273 ftfont_get_bitmap (font, code, bitmap, bits_per_pixel)
1274 struct font *font;
1275 unsigned code;
1276 struct font_bitmap *bitmap;
1277 int bits_per_pixel;
1279 struct ftfont_info *ftfont_info = (struct ftfont_info *) font;
1280 FT_Face ft_face = ftfont_info->ft_size->face;
1281 FT_Int32 load_flags = FT_LOAD_RENDER;
1283 if (ftfont_info->ft_size != ft_face->size)
1284 FT_Activate_Size (ftfont_info->ft_size);
1285 if (bits_per_pixel == 1)
1287 #ifdef FT_LOAD_TARGET_MONO
1288 load_flags |= FT_LOAD_TARGET_MONO;
1289 #else
1290 load_flags |= FT_LOAD_MONOCHROME;
1291 #endif
1293 else if (bits_per_pixel != 8)
1294 /* We don't support such a rendering. */
1295 return -1;
1297 if (FT_Load_Glyph (ft_face, code, load_flags) != 0)
1298 return -1;
1299 bitmap->bits_per_pixel
1300 = (ft_face->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_MONO ? 1
1301 : ft_face->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_GRAY ? 8
1302 : ft_face->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_LCD ? 8
1303 : ft_face->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_LCD_V ? 8
1304 : -1);
1305 if (bitmap->bits_per_pixel < 0)
1306 /* We don't suport that kind of pixel mode. */
1307 return -1;
1308 bitmap->rows = ft_face->glyph->bitmap.rows;
1309 bitmap->width = ft_face->glyph->bitmap.width;
1310 bitmap->pitch = ft_face->glyph->bitmap.pitch;
1311 bitmap->buffer = ft_face->glyph->bitmap.buffer;
1312 bitmap->left = ft_face->glyph->bitmap_left;
1313 bitmap->top = ft_face->glyph->bitmap_top;
1314 bitmap->advance = ft_face->glyph->metrics.horiAdvance >> 6;
1315 bitmap->extra = NULL;
1317 return 0;
1320 static int
1321 ftfont_anchor_point (font, code, index, x, y)
1322 struct font *font;
1323 unsigned code;
1324 int index;
1325 int *x, *y;
1327 struct ftfont_info *ftfont_info = (struct ftfont_info *) font;
1328 FT_Face ft_face = ftfont_info->ft_size->face;
1330 if (ftfont_info->ft_size != ft_face->size)
1331 FT_Activate_Size (ftfont_info->ft_size);
1332 if (FT_Load_Glyph (ft_face, code, FT_LOAD_DEFAULT) != 0)
1333 return -1;
1334 if (ft_face->glyph->format != FT_GLYPH_FORMAT_OUTLINE)
1335 return -1;
1336 if (index >= ft_face->glyph->outline.n_points)
1337 return -1;
1338 *x = ft_face->glyph->outline.points[index].x;
1339 *y = ft_face->glyph->outline.points[index].y;
1340 return 0;
1343 #ifdef HAVE_LIBOTF
1345 static Lisp_Object
1346 ftfont_otf_features (gsub_gpos)
1347 OTF_GSUB_GPOS *gsub_gpos;
1349 Lisp_Object scripts, langsyses, features, sym;
1350 int i, j, k, l;
1352 for (scripts = Qnil, i = gsub_gpos->ScriptList.ScriptCount - 1; i >= 0; i--)
1354 OTF_Script *otf_script = gsub_gpos->ScriptList.Script + i;
1356 for (langsyses = Qnil, j = otf_script->LangSysCount - 1; j >= -1; j--)
1358 OTF_LangSys *otf_langsys;
1360 if (j >= 0)
1361 otf_langsys = otf_script->LangSys + j;
1362 else if (otf_script->DefaultLangSysOffset)
1363 otf_langsys = &otf_script->DefaultLangSys;
1364 else
1365 break;
1367 for (features = Qnil, k = otf_langsys->FeatureCount - 1; k >= 0; k--)
1369 l = otf_langsys->FeatureIndex[k];
1370 if (l >= gsub_gpos->FeatureList.FeatureCount)
1371 continue;
1372 OTF_TAG_SYM (sym, gsub_gpos->FeatureList.Feature[l].FeatureTag);
1373 features = Fcons (sym, features);
1375 if (j >= 0)
1376 OTF_TAG_SYM (sym, otf_script->LangSysRecord[j].LangSysTag);
1377 else
1378 sym = Qnil;
1379 langsyses = Fcons (Fcons (sym, features), langsyses);
1382 OTF_TAG_SYM (sym, gsub_gpos->ScriptList.Script[i].ScriptTag);
1383 scripts = Fcons (Fcons (sym, langsyses), scripts);
1385 return scripts;
1390 static Lisp_Object
1391 ftfont_otf_capability (font)
1392 struct font *font;
1394 struct ftfont_info *ftfont_info = (struct ftfont_info *) font;
1395 OTF *otf = ftfont_get_otf (ftfont_info);
1396 Lisp_Object gsub_gpos;
1398 if (! otf)
1399 return Qnil;
1400 gsub_gpos = Fcons (Qnil, Qnil);
1401 if (OTF_get_table (otf, "GSUB") == 0
1402 && otf->gsub->FeatureList.FeatureCount > 0)
1403 XSETCAR (gsub_gpos, ftfont_otf_features (otf->gsub));
1404 if (OTF_get_table (otf, "GPOS") == 0
1405 && otf->gpos->FeatureList.FeatureCount > 0)
1406 XSETCDR (gsub_gpos, ftfont_otf_features (otf->gpos));
1407 return gsub_gpos;
1410 #ifdef HAVE_M17N_FLT
1412 struct MFLTFontFT
1414 MFLTFont flt_font;
1415 struct font *font;
1416 FT_Face ft_face;
1417 OTF *otf;
1420 static int
1421 ftfont_get_glyph_id (font, gstring, from, to)
1422 MFLTFont *font;
1423 MFLTGlyphString *gstring;
1424 int from, to;
1426 struct MFLTFontFT *flt_font_ft = (struct MFLTFontFT *) font;
1427 FT_Face ft_face = flt_font_ft->ft_face;
1428 MFLTGlyph *g;
1430 for (g = gstring->glyphs + from; from < to; g++, from++)
1431 if (! g->encoded)
1433 FT_UInt code = FT_Get_Char_Index (ft_face, g->code);
1435 g->code = code > 0 ? code : FONT_INVALID_CODE;
1436 g->encoded = 1;
1438 return 0;
1441 static int
1442 ftfont_get_metrics (font, gstring, from, to)
1443 MFLTFont *font;
1444 MFLTGlyphString *gstring;
1445 int from, to;
1447 struct MFLTFontFT *flt_font_ft = (struct MFLTFontFT *) font;
1448 FT_Face ft_face = flt_font_ft->ft_face;
1449 MFLTGlyph *g;
1451 for (g = gstring->glyphs + from; from < to; g++, from++)
1452 if (! g->measured)
1454 if (g->code != FONT_INVALID_CODE)
1456 FT_Glyph_Metrics *m;
1458 if (FT_Load_Glyph (ft_face, g->code, FT_LOAD_DEFAULT) != 0)
1459 abort ();
1460 m = &ft_face->glyph->metrics;
1462 g->lbearing = m->horiBearingX;
1463 g->rbearing = m->horiBearingX + m->width;
1464 g->ascent = m->horiBearingY;
1465 g->descent = m->height - m->horiBearingY;
1466 g->xadv = m->horiAdvance;
1468 else
1470 g->lbearing = 0;
1471 g->rbearing = g->xadv = flt_font_ft->font->space_width << 6;
1472 g->ascent = flt_font_ft->font->ascent << 6;
1473 g->descent = flt_font_ft->font->descent << 6;
1475 g->yadv = 0;
1476 g->measured = 1;
1478 return 0;
1481 static int
1482 ftfont_check_otf (MFLTFont *font, MFLTOtfSpec *spec)
1484 struct MFLTFontFT *flt_font_ft = (struct MFLTFontFT *) font;
1485 OTF *otf = flt_font_ft->otf;
1486 OTF_Tag *tags;
1487 int i, n, negative;
1489 for (i = 0; i < 2; i++)
1491 if (! spec->features[i])
1492 continue;
1493 for (n = 0; spec->features[i][n]; n++);
1494 tags = alloca (sizeof (OTF_Tag) * n);
1495 for (n = 0, negative = 0; spec->features[i][n]; n++)
1497 if (spec->features[i][n] == 0xFFFFFFFF)
1498 negative = 1;
1499 else if (negative)
1500 tags[n - 1] = spec->features[i][n] | 0x80000000;
1501 else
1502 tags[n] = spec->features[i][n];
1504 if (n - negative > 0
1505 && OTF_check_features (otf, i == 0, spec->script, spec->langsys,
1506 tags, n - negative) != 1)
1507 return 0;
1509 return 1;
1512 #define DEVICE_DELTA(table, size) \
1513 (((size) >= (table).StartSize && (size) <= (table).EndSize) \
1514 ? (table).DeltaValue[(size) - (table).StartSize] << 6 \
1515 : 0)
1517 static void
1518 adjust_anchor (FT_Face ft_face, OTF_Anchor *anchor,
1519 unsigned code, int x_ppem, int y_ppem, int *x, int *y)
1521 if (anchor->AnchorFormat == 2)
1523 FT_Outline *outline;
1524 int ap = anchor->f.f1.AnchorPoint;
1526 FT_Load_Glyph (ft_face, (FT_UInt) code, FT_LOAD_MONOCHROME);
1527 outline = &ft_face->glyph->outline;
1528 if (ap < outline->n_points)
1530 *x = outline->points[ap].x << 6;
1531 *y = outline->points[ap].y << 6;
1534 else if (anchor->AnchorFormat == 3)
1536 if (anchor->f.f2.XDeviceTable.offset
1537 && anchor->f.f2.XDeviceTable.DeltaValue)
1538 *x += DEVICE_DELTA (anchor->f.f2.XDeviceTable, x_ppem);
1539 if (anchor->f.f2.YDeviceTable.offset
1540 && anchor->f.f2.YDeviceTable.DeltaValue)
1541 *y += DEVICE_DELTA (anchor->f.f2.YDeviceTable, y_ppem);
1545 static OTF_GlyphString otf_gstring;
1547 static void
1548 setup_otf_gstring (int size)
1550 if (otf_gstring.size == 0)
1552 otf_gstring.glyphs = (OTF_Glyph *) malloc (sizeof (OTF_Glyph) * size);
1553 otf_gstring.size = size;
1555 else if (otf_gstring.size < size)
1557 otf_gstring.glyphs = (OTF_Glyph *) realloc (otf_gstring.glyphs,
1558 sizeof (OTF_Glyph) * size);
1559 otf_gstring.size = size;
1561 otf_gstring.used = size;
1562 memset (otf_gstring.glyphs, 0, sizeof (OTF_Glyph) * size);
1566 static int
1567 ftfont_drive_otf (font, spec, in, from, to, out, adjustment)
1568 MFLTFont *font;
1569 MFLTOtfSpec *spec;
1570 MFLTGlyphString *in;
1571 int from, to;
1572 MFLTGlyphString *out;
1573 MFLTGlyphAdjustment *adjustment;
1575 struct MFLTFontFT *flt_font_ft = (struct MFLTFontFT *) font;
1576 FT_Face ft_face = flt_font_ft->ft_face;
1577 OTF *otf = flt_font_ft->otf;
1578 int len = to - from;
1579 int i, j, gidx;
1580 OTF_Glyph *otfg;
1581 char script[5], *langsys = NULL;
1582 char *gsub_features = NULL, *gpos_features = NULL;
1584 if (len == 0)
1585 return from;
1586 OTF_tag_name (spec->script, script);
1587 if (spec->langsys)
1589 langsys = alloca (5);
1590 OTF_tag_name (spec->langsys, langsys);
1592 for (i = 0; i < 2; i++)
1594 char *p;
1596 if (spec->features[i] && spec->features[i][1] != 0xFFFFFFFF)
1598 for (j = 0; spec->features[i][j]; j++);
1599 if (i == 0)
1600 p = gsub_features = alloca (6 * j);
1601 else
1602 p = gpos_features = alloca (6 * j);
1603 for (j = 0; spec->features[i][j]; j++)
1605 if (spec->features[i][j] == 0xFFFFFFFF)
1606 *p++ = '*', *p++ = ',';
1607 else
1609 OTF_tag_name (spec->features[i][j], p);
1610 p[4] = ',';
1611 p += 5;
1614 *--p = '\0';
1618 setup_otf_gstring (len);
1619 for (i = 0; i < len; i++)
1621 otf_gstring.glyphs[i].c = in->glyphs[from + i].c;
1622 otf_gstring.glyphs[i].glyph_id = in->glyphs[from + i].code;
1625 OTF_drive_gdef (otf, &otf_gstring);
1626 gidx = out->used;
1628 if (gsub_features)
1630 if (OTF_drive_gsub (otf, &otf_gstring, script, langsys, gsub_features)
1631 < 0)
1632 goto simple_copy;
1633 if (out->allocated < out->used + otf_gstring.used)
1634 return -2;
1635 for (i = 0, otfg = otf_gstring.glyphs; i < otf_gstring.used; )
1637 MFLTGlyph *g;
1638 int min_from, max_to;
1639 int j;
1641 g = out->glyphs + out->used;
1642 *g = in->glyphs[from + otfg->f.index.from];
1643 if (g->code != otfg->glyph_id)
1645 g->c = 0;
1646 g->code = otfg->glyph_id;
1647 g->measured = 0;
1649 out->used++;
1650 min_from = g->from;
1651 max_to = g->to;
1652 if (otfg->f.index.from < otfg->f.index.to)
1654 /* OTFG substitutes multiple glyphs in IN. */
1655 for (j = from + otfg->f.index.from + 1;
1656 j <= from + otfg->f.index.to; j++)
1658 if (min_from > in->glyphs[j].from)
1659 min_from = in->glyphs[j].from;
1660 if (max_to < in->glyphs[j].to)
1661 max_to = in->glyphs[j].to;
1663 g->from = min_from;
1664 g->to = max_to;
1666 for (i++, otfg++; (i < otf_gstring.used
1667 && otfg->f.index.from == otfg[-1].f.index.from);
1668 i++, otfg++)
1670 g = out->glyphs + out->used;
1671 *g = in->glyphs[from + otfg->f.index.to];
1672 if (g->code != otfg->glyph_id)
1674 g->c = 0;
1675 g->code = otfg->glyph_id;
1676 g->measured = 0;
1678 out->used++;
1682 else
1684 if (out->allocated < out->used + len)
1685 return -2;
1686 for (i = 0; i < len; i++)
1687 out->glyphs[out->used++] = in->glyphs[from + i];
1690 if (gpos_features)
1692 MFLTGlyph *base = NULL, *mark = NULL, *g;
1693 int x_ppem, y_ppem, x_scale, y_scale;
1695 if (OTF_drive_gpos (otf, &otf_gstring, script, langsys, gpos_features)
1696 < 0)
1697 return to;
1699 x_ppem = ft_face->size->metrics.x_ppem;
1700 y_ppem = ft_face->size->metrics.y_ppem;
1701 x_scale = ft_face->size->metrics.x_scale;
1702 y_scale = ft_face->size->metrics.y_scale;
1704 for (i = 0, otfg = otf_gstring.glyphs, g = out->glyphs + gidx;
1705 i < otf_gstring.used; i++, otfg++, g++)
1707 MFLTGlyph *prev;
1709 if (! otfg->glyph_id)
1710 continue;
1711 switch (otfg->positioning_type)
1713 case 0:
1714 break;
1715 case 1: /* Single */
1716 case 2: /* Pair */
1718 int format = otfg->f.f1.format;
1720 if (format & OTF_XPlacement)
1721 adjustment[i].xoff
1722 = otfg->f.f1.value->XPlacement * x_scale / 0x10000;
1723 if (format & OTF_XPlaDevice)
1724 adjustment[i].xoff
1725 += DEVICE_DELTA (otfg->f.f1.value->XPlaDevice, x_ppem);
1726 if (format & OTF_YPlacement)
1727 adjustment[i].yoff
1728 = - (otfg->f.f1.value->YPlacement * y_scale / 0x10000);
1729 if (format & OTF_YPlaDevice)
1730 adjustment[i].yoff
1731 -= DEVICE_DELTA (otfg->f.f1.value->YPlaDevice, y_ppem);
1732 if (format & OTF_XAdvance)
1733 adjustment[i].xadv
1734 += otfg->f.f1.value->XAdvance * x_scale / 0x10000;
1735 if (format & OTF_XAdvDevice)
1736 adjustment[i].xadv
1737 += DEVICE_DELTA (otfg->f.f1.value->XAdvDevice, x_ppem);
1738 if (format & OTF_YAdvance)
1739 adjustment[i].yadv
1740 += otfg->f.f1.value->YAdvance * y_scale / 0x10000;
1741 if (format & OTF_YAdvDevice)
1742 adjustment[i].yadv
1743 += DEVICE_DELTA (otfg->f.f1.value->YAdvDevice, y_ppem);
1744 adjustment[i].set = 1;
1746 break;
1747 case 3: /* Cursive */
1748 /* Not yet supported. */
1749 break;
1750 case 4: /* Mark-to-Base */
1751 case 5: /* Mark-to-Ligature */
1752 if (! base)
1753 break;
1754 prev = base;
1755 goto label_adjust_anchor;
1756 default: /* i.e. case 6 Mark-to-Mark */
1757 if (! mark)
1758 break;
1759 prev = mark;
1761 label_adjust_anchor:
1763 int base_x, base_y, mark_x, mark_y;
1764 int this_from, this_to;
1766 base_x = otfg->f.f4.base_anchor->XCoordinate * x_scale / 0x10000;
1767 base_y = otfg->f.f4.base_anchor->YCoordinate * y_scale / 0x10000;
1768 mark_x = otfg->f.f4.mark_anchor->XCoordinate * x_scale / 0x10000;
1769 mark_y = otfg->f.f4.mark_anchor->YCoordinate * y_scale / 0x10000;;
1771 if (otfg->f.f4.base_anchor->AnchorFormat != 1)
1772 adjust_anchor (ft_face, otfg->f.f4.base_anchor,
1773 prev->code, x_ppem, y_ppem, &base_x, &base_y);
1774 if (otfg->f.f4.mark_anchor->AnchorFormat != 1)
1775 adjust_anchor (ft_face, otfg->f.f4.mark_anchor, g->code,
1776 x_ppem, y_ppem, &mark_x, &mark_y);
1777 adjustment[i].xoff = (base_x - mark_x);
1778 adjustment[i].yoff = - (base_y - mark_y);
1779 adjustment[i].back = (g - prev);
1780 adjustment[i].xadv = 0;
1781 adjustment[i].advance_is_absolute = 1;
1782 adjustment[i].set = 1;
1783 this_from = g->from;
1784 this_to = g->to;
1785 for (j = 0; prev + j < g; j++)
1787 if (this_from > prev[j].from)
1788 this_from = prev[j].from;
1789 if (this_to < prev[j].to)
1790 this_to = prev[j].to;
1792 for (; prev <= g; prev++)
1794 prev->from = this_from;
1795 prev->to = this_to;
1799 if (otfg->GlyphClass == OTF_GlyphClass0)
1800 base = mark = g;
1801 else if (otfg->GlyphClass == OTF_GlyphClassMark)
1802 mark = g;
1803 else
1804 base = g;
1807 return to;
1809 simple_copy:
1810 if (out->allocated < out->used + len)
1811 return -2;
1812 font->get_metrics (font, in, from, to);
1813 memcpy (out->glyphs + out->used, in->glyphs + from,
1814 sizeof (MFLTGlyph) * len);
1815 out->used += len;
1816 return to;
1819 static MFLTGlyphString gstring;
1821 static int m17n_flt_initialized;
1823 extern Lisp_Object QCfamily;
1825 static Lisp_Object
1826 ftfont_shape_by_flt (lgstring, font, ft_face, otf)
1827 Lisp_Object lgstring;
1828 struct font *font;
1829 FT_Face ft_face;
1830 OTF *otf;
1832 EMACS_UINT len = LGSTRING_GLYPH_LEN (lgstring);
1833 EMACS_UINT i;
1834 struct MFLTFontFT flt_font_ft;
1835 MFLT *flt = NULL;
1836 int with_variation_selector = 0;
1838 if (! m17n_flt_initialized)
1840 M17N_INIT ();
1841 m17n_flt_initialized = 1;
1844 for (i = 0; i < len; i++)
1846 Lisp_Object g = LGSTRING_GLYPH (lgstring, i);
1847 int c;
1849 if (NILP (g))
1850 break;
1851 c = LGLYPH_CHAR (g);
1852 if (CHAR_VARIATION_SELECTOR_P (c))
1853 with_variation_selector++;
1855 len = i;
1856 if (with_variation_selector)
1858 setup_otf_gstring (len);
1859 for (i = 0; i < len; i++)
1861 Lisp_Object g = LGSTRING_GLYPH (lgstring, i);
1863 otf_gstring.glyphs[i].c = LGLYPH_CHAR (g);
1864 otf_gstring.glyphs[i].f.index.from = LGLYPH_FROM (g);
1865 otf_gstring.glyphs[i].f.index.to = LGLYPH_TO (g);
1867 OTF_drive_cmap (otf, &otf_gstring);
1868 for (i = 0; i < otf_gstring.used; i++)
1870 OTF_Glyph *otfg = otf_gstring.glyphs + i;
1871 Lisp_Object g0 = LGSTRING_GLYPH (lgstring, otfg->f.index.from);
1872 Lisp_Object g1 = LGSTRING_GLYPH (lgstring, otfg->f.index.to);
1874 LGLYPH_SET_CODE (g0, otfg->glyph_id);
1875 LGLYPH_SET_TO (g0, LGLYPH_TO (g1));
1876 LGSTRING_SET_GLYPH (lgstring, i, g0);
1878 if (len > otf_gstring.used)
1880 len = otf_gstring.used;
1881 LGSTRING_SET_GLYPH (lgstring, len, Qnil);
1885 if (gstring.allocated == 0)
1887 gstring.allocated = len * 2;
1888 gstring.glyph_size = sizeof (MFLTGlyph);
1889 gstring.glyphs = malloc (sizeof (MFLTGlyph) * gstring.allocated);
1891 else if (gstring.allocated < len * 2)
1893 gstring.allocated = len * 2;
1894 gstring.glyphs = realloc (gstring.glyphs,
1895 sizeof (MFLTGlyph) * gstring.allocated);
1897 memset (gstring.glyphs, 0, sizeof (MFLTGlyph) * len);
1898 for (i = 0; i < len; i++)
1900 Lisp_Object g = LGSTRING_GLYPH (lgstring, i);
1902 gstring.glyphs[i].c = LGLYPH_CHAR (g);
1903 if (with_variation_selector)
1905 gstring.glyphs[i].code = LGLYPH_CODE (g);
1906 gstring.glyphs[i].encoded = 1;
1910 gstring.used = len;
1911 gstring.r2l = 0;
1914 Lisp_Object family = Ffont_get (LGSTRING_FONT (lgstring), QCfamily);
1916 if (NILP (family))
1917 flt_font_ft.flt_font.family = Mnil;
1918 else
1919 flt_font_ft.flt_font.family
1920 = msymbol ((char *) SDATA (Fdowncase (SYMBOL_NAME (family))));
1922 flt_font_ft.flt_font.x_ppem = ft_face->size->metrics.x_ppem;
1923 flt_font_ft.flt_font.y_ppem = ft_face->size->metrics.y_ppem;
1924 flt_font_ft.flt_font.get_glyph_id = ftfont_get_glyph_id;
1925 flt_font_ft.flt_font.get_metrics = ftfont_get_metrics;
1926 flt_font_ft.flt_font.check_otf = ftfont_check_otf;
1927 flt_font_ft.flt_font.drive_otf = ftfont_drive_otf;
1928 flt_font_ft.flt_font.internal = NULL;
1929 flt_font_ft.font = font;
1930 flt_font_ft.ft_face = ft_face;
1931 flt_font_ft.otf = otf;
1932 if (len > 1
1933 && gstring.glyphs[1].c >= 0x300 && gstring.glyphs[1].c <= 0x36F)
1934 /* A little bit ad hoc. Perhaps, shaper must get script and
1935 language information, and select a proper flt for them
1936 here. */
1937 flt = mflt_get (msymbol ("combining"));
1938 for (i = 0; i < 3; i++)
1940 int result = mflt_run (&gstring, 0, len, &flt_font_ft.flt_font, flt);
1941 if (result != -2)
1942 break;
1943 gstring.allocated += gstring.allocated;
1944 gstring.glyphs = realloc (gstring.glyphs,
1945 sizeof (MFLTGlyph) * gstring.allocated);
1947 if (gstring.used > LGSTRING_GLYPH_LEN (lgstring))
1948 return Qnil;
1949 for (i = 0; i < gstring.used; i++)
1951 MFLTGlyph *g = gstring.glyphs + i;
1953 g->from = LGLYPH_FROM (LGSTRING_GLYPH (lgstring, g->from));
1954 g->to = LGLYPH_TO (LGSTRING_GLYPH (lgstring, g->to));
1957 for (i = 0; i < gstring.used; i++)
1959 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
1960 MFLTGlyph *g = gstring.glyphs + i;
1962 if (NILP (lglyph))
1964 lglyph = Fmake_vector (make_number (LGLYPH_SIZE), Qnil);
1965 LGSTRING_SET_GLYPH (lgstring, i, lglyph);
1967 LGLYPH_SET_FROM (lglyph, g->from);
1968 LGLYPH_SET_TO (lglyph, g->to);
1969 LGLYPH_SET_CHAR (lglyph, g->c);
1970 LGLYPH_SET_CODE (lglyph, g->code);
1971 LGLYPH_SET_WIDTH (lglyph, g->xadv >> 6);
1972 LGLYPH_SET_LBEARING (lglyph, g->lbearing >> 6);
1973 LGLYPH_SET_RBEARING (lglyph, g->rbearing >> 6);
1974 LGLYPH_SET_ASCENT (lglyph, g->ascent >> 6);
1975 LGLYPH_SET_DESCENT (lglyph, g->descent >> 6);
1976 if (g->adjusted)
1978 Lisp_Object vec;
1980 vec = Fmake_vector (make_number (3), Qnil);
1981 ASET (vec, 0, make_number (g->xoff >> 6));
1982 ASET (vec, 1, make_number (g->yoff >> 6));
1983 ASET (vec, 2, make_number (g->xadv >> 6));
1984 LGLYPH_SET_ADJUSTMENT (lglyph, vec);
1987 return make_number (i);
1990 Lisp_Object
1991 ftfont_shape (lgstring)
1992 Lisp_Object lgstring;
1994 struct font *font;
1995 struct ftfont_info *ftfont_info;
1996 OTF *otf;
1998 CHECK_FONT_GET_OBJECT (LGSTRING_FONT (lgstring), font);
1999 ftfont_info = (struct ftfont_info *) font;
2000 otf = ftfont_get_otf (ftfont_info);
2001 if (! otf)
2002 return make_number (0);
2003 return ftfont_shape_by_flt (lgstring, font, ftfont_info->ft_size->face, otf);
2006 #ifdef HAVE_OTF_GET_VARIATION_GLYPHS
2008 static int
2009 ftfont_variation_glyphs (font, c, variations)
2010 struct font *font;
2011 int c;
2012 unsigned variations[256];
2014 struct ftfont_info *ftfont_info = (struct ftfont_info *) font;
2015 OTF *otf = ftfont_get_otf (ftfont_info);
2017 if (! otf)
2018 return 0;
2019 return OTF_get_variation_glyphs (otf, c, variations);
2022 #endif /* HAVE_OTF_GET_VARIATION_GLYPHS */
2023 #endif /* HAVE_M17N_FLT */
2024 #endif /* HAVE_LIBOTF */
2026 Lisp_Object
2027 ftfont_font_format (FcPattern *pattern, Lisp_Object filename)
2029 FcChar8 *str;
2031 #ifdef FC_FONTFORMAT
2032 if (pattern)
2034 if (FcPatternGetString (pattern, FC_FONTFORMAT, 0, &str) != FcResultMatch)
2035 return Qnil;
2036 if (strcmp ((char *) str, "TrueType") == 0)
2037 return intern ("truetype");
2038 if (strcmp ((char *) str, "Type 1") == 0)
2039 return intern ("type1");
2040 if (strcmp ((char *) str, "PCF") == 0)
2041 return intern ("pcf");
2042 if (strcmp ((char *) str, "BDF") == 0)
2043 return intern ("bdf");
2045 #endif /* FC_FONTFORMAT */
2046 if (STRINGP (filename))
2048 int len = SBYTES (filename);
2050 if (len >= 4)
2052 str = (FcChar8 *) (SDATA (filename) + len - 4);
2053 if (xstrcasecmp ((char *) str, ".ttf") == 0)
2054 return intern ("truetype");
2055 if (xstrcasecmp ((char *) str, ".pfb") == 0)
2056 return intern ("type1");
2057 if (xstrcasecmp ((char *) str, ".pcf") == 0)
2058 return intern ("pcf");
2059 if (xstrcasecmp ((char *) str, ".bdf") == 0)
2060 return intern ("bdf");
2063 return intern ("unknown");
2067 void
2068 syms_of_ftfont ()
2070 DEFSYM (Qfreetype, "freetype");
2071 DEFSYM (Qmonospace, "monospace");
2072 DEFSYM (Qsans_serif, "sans-serif");
2073 DEFSYM (Qserif, "serif");
2074 DEFSYM (Qmono, "mono");
2075 DEFSYM (Qsans, "sans");
2076 DEFSYM (Qsans__serif, "sans serif");
2078 staticpro (&freetype_font_cache);
2079 freetype_font_cache = Fcons (Qt, Qnil);
2081 staticpro (&ftfont_generic_family_list);
2082 ftfont_generic_family_list
2083 = Fcons (Fcons (Qmonospace, Qt),
2084 Fcons (Fcons (Qsans_serif, Qt),
2085 Fcons (Fcons (Qsans, Qt), Qnil)));
2087 staticpro (&ft_face_cache);
2088 ft_face_cache = Qnil;
2090 ftfont_driver.type = Qfreetype;
2091 register_font_driver (&ftfont_driver, NULL);
2094 /* arch-tag: 7cfa432c-33a6-4988-83d2-a82ed8604aca
2095 (do not change this comment) */