Fix copyright years and license notices.
[emacs.git] / src / ftfont.c
bloba6637fe1287e7cf5a140bbe16b5a1c68433b1a01
1 /* ftfont.c -- FreeType font driver.
2 Copyright (C) 2006, 2007, 2008 Free Software Foundation, Inc.
3 Copyright (C) 2006, 2007, 2008
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 { NULL }
134 extern Lisp_Object Qc, Qm, Qp, Qd;
136 static Lisp_Object
137 ftfont_pattern_entity (p, extra)
138 FcPattern *p;
139 Lisp_Object extra;
141 Lisp_Object entity;
142 char *file, *str;
143 int index;
144 int numeric;
145 double dbl;
146 FcBool b;
148 if (FcPatternGetString (p, FC_FILE, 0, (FcChar8 **) &file) != FcResultMatch)
149 return Qnil;
150 if (FcPatternGetInteger (p, FC_INDEX, 0, &index) != FcResultMatch)
151 return Qnil;
153 entity = font_make_entity ();
155 ASET (entity, FONT_TYPE_INDEX, Qfreetype);
156 ASET (entity, FONT_REGISTRY_INDEX, Qiso10646_1);
158 if (FcPatternGetString (p, FC_FOUNDRY, 0, (FcChar8 **) &str) == FcResultMatch)
159 ASET (entity, FONT_FOUNDRY_INDEX, font_intern_prop (str, strlen (str), 1));
160 if (FcPatternGetString (p, FC_FAMILY, 0, (FcChar8 **) &str) == FcResultMatch)
161 ASET (entity, FONT_FAMILY_INDEX, font_intern_prop (str, strlen (str), 1));
162 if (FcPatternGetInteger (p, FC_WEIGHT, 0, &numeric) == FcResultMatch)
164 if (numeric >= FC_WEIGHT_REGULAR && numeric < FC_WEIGHT_MEDIUM)
165 numeric = FC_WEIGHT_MEDIUM;
166 FONT_SET_STYLE (entity, FONT_WEIGHT_INDEX, make_number (numeric));
168 if (FcPatternGetInteger (p, FC_SLANT, 0, &numeric) == FcResultMatch)
170 numeric += 100;
171 FONT_SET_STYLE (entity, FONT_SLANT_INDEX, make_number (numeric));
173 if (FcPatternGetInteger (p, FC_WIDTH, 0, &numeric) == FcResultMatch)
175 FONT_SET_STYLE (entity, FONT_WIDTH_INDEX, make_number (numeric));
177 if (FcPatternGetDouble (p, FC_PIXEL_SIZE, 0, &dbl) == FcResultMatch)
178 ASET (entity, FONT_SIZE_INDEX, make_number (dbl));
179 else
180 ASET (entity, FONT_SIZE_INDEX, make_number (0));
181 if (FcPatternGetInteger (p, FC_SPACING, 0, &numeric) == FcResultMatch)
182 ASET (entity, FONT_SPACING_INDEX, make_number (numeric));
183 if (FcPatternGetDouble (p, FC_DPI, 0, &dbl) == FcResultMatch)
185 int dpi = dbl;
186 ASET (entity, FONT_DPI_INDEX, make_number (dpi));
188 if (FcPatternGetBool (p, FC_SCALABLE, 0, &b) == FcResultMatch
189 && b == FcTrue)
190 ASET (entity, FONT_AVGWIDTH_INDEX, make_number (0));
192 ASET (entity, FONT_EXTRA_INDEX, Fcopy_sequence (extra));
193 font_put_extra (entity, QCfont_entity,
194 Fcons (make_unibyte_string ((char *) file,
195 strlen ((char *) file)),
196 make_number (index)));
197 return entity;
201 static Lisp_Object ftfont_generic_family_list;
203 static Lisp_Object
204 ftfont_resolve_generic_family (family, pattern)
205 Lisp_Object family;
206 FcPattern *pattern;
208 Lisp_Object slot;
209 FcPattern *match;
210 FcResult result;
211 FcLangSet *langset;
213 family = Fintern (Fdowncase (SYMBOL_NAME (family)), Qnil);
214 if (EQ (family, Qmono))
215 family = Qmonospace;
216 else if (EQ (family, Qsans) || EQ (family, Qsans__serif))
217 family = Qsans_serif;
218 slot = assq_no_quit (family, ftfont_generic_family_list);
219 if (! CONSP (slot))
220 return Qnil;
221 if (! EQ (XCDR (slot), Qt))
222 return XCDR (slot);
223 pattern = FcPatternDuplicate (pattern);
224 if (! pattern)
225 goto err;
226 FcPatternDel (pattern, FC_FOUNDRY);
227 FcPatternDel (pattern, FC_FAMILY);
228 FcPatternAddString (pattern, FC_FAMILY, SYMBOL_FcChar8 (family));
229 if (FcPatternGetLangSet (pattern, FC_LANG, 0, &langset) != FcResultMatch)
231 /* This is to avoid the effect of locale. */
232 langset = FcLangSetCreate ();
233 FcLangSetAdd (langset, "en");
234 FcPatternAddLangSet (pattern, FC_LANG, langset);
235 FcLangSetDestroy (langset);
237 FcConfigSubstitute (NULL, pattern, FcMatchPattern);
238 FcDefaultSubstitute (pattern);
239 match = FcFontMatch (NULL, pattern, &result);
240 if (match)
242 FcChar8 *fam;
244 if (FcPatternGetString (match, FC_FAMILY, 0, &fam) == FcResultMatch)
245 family = intern ((char *) fam);
247 else
248 family = Qnil;
249 XSETCDR (slot, family);
250 err:
251 if (match) FcPatternDestroy (match);
252 if (pattern) FcPatternDestroy (pattern);
253 return family;
256 struct ftfont_cache_data
258 FT_Face ft_face;
259 FcCharSet *fc_charset;
262 static Lisp_Object
263 ftfont_lookup_cache (key, for_face)
264 Lisp_Object key;
265 int for_face;
267 Lisp_Object cache, val;
268 struct ftfont_cache_data *cache_data;
270 cache = assoc_no_quit (key, ft_face_cache);
271 if (NILP (cache))
273 cache_data = xmalloc (sizeof (struct ftfont_cache_data));
274 cache_data->ft_face = NULL;
275 cache_data->fc_charset = NULL;
276 val = make_save_value (NULL, 0);
277 XSAVE_VALUE (val)->integer = 0;
278 XSAVE_VALUE (val)->pointer = cache_data;
279 cache = Fcons (key, val);
280 ft_face_cache = Fcons (cache, ft_face_cache);
282 else
284 val = XCDR (cache);
285 cache_data = XSAVE_VALUE (val)->pointer;
287 if (for_face ? ! cache_data->ft_face : ! cache_data->fc_charset)
289 char *filename = (char *) SDATA (XCAR (key));
290 int index = XINT (XCDR (key));
292 if (for_face)
294 if (! ft_library
295 && FT_Init_FreeType (&ft_library) != 0)
296 return Qnil;
297 if (FT_New_Face (ft_library, filename, index, &cache_data->ft_face)
298 != 0)
299 return Qnil;
301 else
303 FcPattern *pat;
304 FcFontSet *fontset;
305 FcObjectSet *objset;
306 FcCharSet *charset;
308 pat = FcPatternBuild (0, FC_FILE, FcTypeString, (FcChar8 *) filename,
309 FC_INDEX, FcTypeInteger, index, NULL);
310 objset = FcObjectSetBuild (FC_CHARSET, NULL);
311 fontset = FcFontList (NULL, pat, objset);
312 xassert (fontset && fontset->nfont > 0);
313 if (FcPatternGetCharSet (fontset->fonts[0], FC_CHARSET, 0, &charset)
314 == FcResultMatch)
315 cache_data->fc_charset = FcCharSetCopy (charset);
316 else
317 cache_data->fc_charset = FcCharSetCreate ();
318 FcFontSetDestroy (fontset);
319 FcObjectSetDestroy (objset);
320 FcPatternDestroy (pat);
323 return cache;
326 FcCharSet *
327 ftfont_get_fc_charset (entity)
328 Lisp_Object entity;
330 Lisp_Object val, cache;
331 struct ftfont_cache_data *cache_data;
333 val = assq_no_quit (QCfont_entity, AREF (entity, FONT_EXTRA_INDEX));
334 xassert (CONSP (val));
335 val = XCDR (val);
336 cache = ftfont_lookup_cache (val, 0);
337 val = XCDR (cache);
338 cache_data = XSAVE_VALUE (val)->pointer;
339 return cache_data->fc_charset;
342 #ifdef HAVE_LIBOTF
343 static OTF *
344 ftfont_get_otf (ftfont_info)
345 struct ftfont_info *ftfont_info;
347 OTF *otf;
349 if (ftfont_info->otf)
350 return ftfont_info->otf;
351 if (! ftfont_info->maybe_otf)
352 return NULL;
353 otf = OTF_open_ft_face (ftfont_info->ft_size->face);
354 if (! otf || OTF_get_table (otf, "head") < 0)
356 if (otf)
357 OTF_close (otf);
358 ftfont_info->maybe_otf = 0;
359 return NULL;
361 ftfont_info->otf = otf;
362 return otf;
364 #endif /* HAVE_LIBOTF */
366 static Lisp_Object ftfont_get_cache P_ ((FRAME_PTR));
367 static Lisp_Object ftfont_list P_ ((Lisp_Object, Lisp_Object));
368 static Lisp_Object ftfont_match P_ ((Lisp_Object, Lisp_Object));
369 static Lisp_Object ftfont_list_family P_ ((Lisp_Object));
370 static Lisp_Object ftfont_open P_ ((FRAME_PTR, Lisp_Object, int));
371 static void ftfont_close P_ ((FRAME_PTR, struct font *));
372 static int ftfont_has_char P_ ((Lisp_Object, int));
373 static unsigned ftfont_encode_char P_ ((struct font *, int));
374 static int ftfont_text_extents P_ ((struct font *, unsigned *, int,
375 struct font_metrics *));
376 static int ftfont_get_bitmap P_ ((struct font *, unsigned,
377 struct font_bitmap *, int));
378 static int ftfont_anchor_point P_ ((struct font *, unsigned, int,
379 int *, int *));
380 static Lisp_Object ftfont_otf_capability P_ ((struct font *));
381 static Lisp_Object ftfont_shape P_ ((Lisp_Object));
383 struct font_driver ftfont_driver =
385 0, /* Qfreetype */
386 0, /* case insensitive */
387 ftfont_get_cache,
388 ftfont_list,
389 ftfont_match,
390 ftfont_list_family,
391 NULL,
392 ftfont_open,
393 ftfont_close,
394 /* We can't draw a text without device dependent functions. */
395 NULL,
396 NULL,
397 ftfont_has_char,
398 ftfont_encode_char,
399 ftfont_text_extents,
400 /* We can't draw a text without device dependent functions. */
401 NULL,
402 ftfont_get_bitmap,
403 NULL,
404 NULL,
405 NULL,
406 ftfont_anchor_point,
407 #ifdef HAVE_LIBOTF
408 ftfont_otf_capability,
409 #else /* not HAVE_LIBOTF */
410 NULL,
411 #endif /* not HAVE_LIBOTF */
412 NULL,
413 NULL,
414 NULL,
415 #if defined (HAVE_M17N_FLT) && defined (HAVE_LIBOTF)
416 ftfont_shape
417 #else /* not (HAVE_M17N_FLT && HAVE_LIBOTF) */
418 NULL
419 #endif /* not (HAVE_M17N_FLT && HAVE_LIBOTF) */
422 extern Lisp_Object QCname;
424 static Lisp_Object
425 ftfont_get_cache (f)
426 FRAME_PTR f;
428 return freetype_font_cache;
431 static int
432 ftfont_get_charset (registry)
433 Lisp_Object registry;
435 char *str = (char *) SDATA (SYMBOL_NAME (registry));
436 char *re = alloca (SBYTES (SYMBOL_NAME (registry)) * 2 + 1);
437 Lisp_Object regexp;
438 int i, j;
440 for (i = j = 0; i < SBYTES (SYMBOL_NAME (registry)); i++, j++)
442 if (str[i] == '.')
443 re[j++] = '\\';
444 else if (str[i] == '*')
445 re[j++] = '.';
446 re[j] = str[i];
447 if (re[j] == '?')
448 re[j] = '.';
450 re[j] = '\0';
451 regexp = make_unibyte_string (re, j);
452 for (i = 0; fc_charset_table[i].name; i++)
453 if (fast_c_string_match_ignore_case (regexp, fc_charset_table[i].name) >= 0)
454 break;
455 if (! fc_charset_table[i].name)
456 return -1;
457 if (! fc_charset_table[i].fc_charset)
459 FcCharSet *charset = FcCharSetCreate ();
460 int *uniquifier = fc_charset_table[i].uniquifier;
462 if (! charset)
463 return -1;
464 for (j = 0; uniquifier[j]; j++)
465 if (! FcCharSetAddChar (charset, uniquifier[j]))
467 FcCharSetDestroy (charset);
468 return -1;
470 fc_charset_table[i].fc_charset = charset;
472 return i;
475 struct OpenTypeSpec
477 Lisp_Object script;
478 unsigned int script_tag, langsys_tag;
479 int nfeatures[2];
480 unsigned int *features[2];
483 #define OTF_SYM_TAG(SYM, TAG) \
484 do { \
485 unsigned char *p = SDATA (SYMBOL_NAME (SYM)); \
486 TAG = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]; \
487 } while (0)
489 #define OTF_TAG_STR(TAG, P) \
490 do { \
491 (P)[0] = (char) (TAG >> 24); \
492 (P)[1] = (char) ((TAG >> 16) & 0xFF); \
493 (P)[2] = (char) ((TAG >> 8) & 0xFF); \
494 (P)[3] = (char) (TAG & 0xFF); \
495 (P)[4] = '\0'; \
496 } while (0)
498 #define OTF_TAG_SYM(SYM, TAG) \
499 do { \
500 char str[5]; \
502 OTF_TAG_STR (TAG, str); \
503 (SYM) = font_intern_prop (str, 4, 1); \
504 } while (0)
507 static struct OpenTypeSpec *
508 ftfont_get_open_type_spec (Lisp_Object otf_spec)
510 struct OpenTypeSpec *spec = malloc (sizeof (struct OpenTypeSpec));
511 Lisp_Object val;
512 int i, j, negative;
514 if (! spec)
515 return NULL;
516 spec->script = XCAR (otf_spec);
517 if (! NILP (spec->script))
519 OTF_SYM_TAG (spec->script, spec->script_tag);
520 val = assq_no_quit (spec->script, Votf_script_alist);
521 if (CONSP (val) && SYMBOLP (XCDR (val)))
522 spec->script = XCDR (val);
523 else
524 spec->script = Qnil;
526 else
527 spec->script_tag = 0x44464C54; /* "DFLT" */
528 otf_spec = XCDR (otf_spec);
529 val = XCAR (otf_spec);
530 if (! NILP (val))
531 OTF_SYM_TAG (val, spec->langsys_tag);
532 else
533 spec->langsys_tag = 0;
534 spec->nfeatures[0] = spec->nfeatures[1] = 0;
535 for (i = 0; i < 2; i++)
537 Lisp_Object len;
539 otf_spec = XCDR (otf_spec);
540 if (NILP (otf_spec))
541 break;
542 val = XCAR (otf_spec);
543 if (NILP (val))
544 continue;
545 len = Flength (val);
546 spec->features[i] = malloc (sizeof (int) * XINT (len));
547 if (! spec->features[i])
549 if (i > 0 && spec->features[0])
550 free (spec->features[0]);
551 free (spec);
552 return NULL;
554 for (j = 0, negative = 0; CONSP (val); val = XCDR (val))
556 if (NILP (XCAR (val)))
557 negative = 1;
558 else
560 unsigned int tag;
562 OTF_SYM_TAG (XCAR (val), tag);
563 spec->features[i][j++] = negative ? tag & 0x80000000 : tag;
566 spec->nfeatures[i] = j;
568 return spec;
571 static FcPattern *ftfont_spec_pattern P_ ((Lisp_Object, char *,
572 struct OpenTypeSpec **));
574 static FcPattern *
575 ftfont_spec_pattern (spec, otlayout, otspec)
576 Lisp_Object spec;
577 char *otlayout;
578 struct OpenTypeSpec **otspec;
580 Lisp_Object tmp, extra;
581 FcPattern *pattern = NULL;
582 FcCharSet *charset = NULL;
583 FcLangSet *langset = NULL;
584 int n;
585 int dpi = -1;
586 int scalable = -1;
587 Lisp_Object script = Qnil;
588 Lisp_Object registry;
589 int fc_charset_idx;
591 if (! NILP (AREF (spec, FONT_ADSTYLE_INDEX))
592 && SBYTES (SYMBOL_NAME (AREF (spec, FONT_ADSTYLE_INDEX))) > 0)
593 /* Fontconfig doesn't support adstyle property. */
594 return NULL;
595 if ((n = FONT_SLANT_NUMERIC (spec)) >= 0
596 && n < 100)
597 /* Fontconfig doesn't support reverse-italic/obligue. */
598 return NULL;
600 if (INTEGERP (AREF (spec, FONT_DPI_INDEX)))
601 dpi = XINT (AREF (spec, FONT_DPI_INDEX));
602 if (INTEGERP (AREF (spec, FONT_AVGWIDTH_INDEX))
603 && XINT (AREF (spec, FONT_AVGWIDTH_INDEX)) == 0)
604 scalable = 1;
606 registry = AREF (spec, FONT_REGISTRY_INDEX);
607 if (NILP (registry)
608 || EQ (registry, Qascii_0)
609 || EQ (registry, Qiso10646_1)
610 || EQ (registry, Qunicode_bmp)
611 || EQ (registry, Qunicode_sip))
612 fc_charset_idx = -1;
613 else
615 FcChar8 *lang;
617 fc_charset_idx = ftfont_get_charset (registry);
618 if (fc_charset_idx < 0)
619 return NULL;
620 charset = fc_charset_table[fc_charset_idx].fc_charset;
621 lang = (FcChar8 *) fc_charset_table[fc_charset_idx].lang;
622 if (lang)
624 langset = FcLangSetCreate ();
625 if (! langset)
626 goto err;
627 FcLangSetAdd (langset, lang);
631 otlayout[0] = '\0';
632 for (extra = AREF (spec, FONT_EXTRA_INDEX);
633 CONSP (extra); extra = XCDR (extra))
635 Lisp_Object key, val;
637 key = XCAR (XCAR (extra)), val = XCDR (XCAR (extra));
638 if (EQ (key, QCdpi))
639 dpi = XINT (val);
640 else if (EQ (key, QClang))
642 if (! langset)
643 langset = FcLangSetCreate ();
644 if (! langset)
645 goto err;
646 if (SYMBOLP (val))
648 if (! FcLangSetAdd (langset, SYMBOL_FcChar8 (val)))
649 goto err;
651 else
652 for (; CONSP (val); val = XCDR (val))
653 if (SYMBOLP (XCAR (val))
654 && ! FcLangSetAdd (langset, SYMBOL_FcChar8 (XCAR (val))))
655 goto err;
657 else if (EQ (key, QCotf))
659 *otspec = ftfont_get_open_type_spec (val);
660 if (! *otspec)
661 return NULL;
662 strcat (otlayout, "otlayout:");
663 OTF_TAG_STR ((*otspec)->script_tag, otlayout + 9);
664 script = (*otspec)->script;
666 else if (EQ (key, QCscript))
667 script = val;
668 else if (EQ (key, QCscalable))
669 scalable = ! NILP (val);
672 if (! NILP (script) && ! charset)
674 Lisp_Object chars = assq_no_quit (script, Vscript_representative_chars);
676 if (CONSP (chars) && CONSP (CDR (chars)))
678 charset = FcCharSetCreate ();
679 if (! charset)
680 goto err;
681 for (chars = XCDR (chars); CONSP (chars); chars = XCDR (chars))
682 if (CHARACTERP (XCAR (chars))
683 && ! FcCharSetAddChar (charset, XUINT (XCAR (chars))))
684 goto err;
688 pattern = FcPatternCreate ();
689 if (! pattern)
690 goto err;
691 tmp = AREF (spec, FONT_FOUNDRY_INDEX);
692 if (! NILP (tmp)
693 && ! FcPatternAddString (pattern, FC_FOUNDRY, SYMBOL_FcChar8 (tmp)))
694 goto err;
695 tmp = AREF (spec, FONT_FAMILY_INDEX);
696 if (! NILP (tmp)
697 && ! FcPatternAddString (pattern, FC_FAMILY, SYMBOL_FcChar8 (tmp)))
698 goto err;
699 if (charset
700 && ! FcPatternAddCharSet (pattern, FC_CHARSET, charset))
701 goto err;
702 if (langset
703 && ! FcPatternAddLangSet (pattern, FC_LANG, langset))
704 goto err;
705 if (dpi >= 0
706 && ! FcPatternAddDouble (pattern, FC_DPI, dpi))
707 goto err;
708 if (scalable >= 0
709 && ! FcPatternAddBool (pattern, FC_SCALABLE, scalable ? FcTrue : FcFalse))
710 goto err;
712 goto finish;
714 err:
715 /* We come here because of unexpected error in fontconfig API call
716 (usually insufficient memory). */
717 if (pattern)
719 FcPatternDestroy (pattern);
720 pattern = NULL;
722 if (*otspec)
724 if ((*otspec)->nfeatures[0] > 0)
725 free ((*otspec)->features[0]);
726 if ((*otspec)->nfeatures[1] > 0)
727 free ((*otspec)->features[1]);
728 free (*otspec);
729 *otspec = NULL;
732 finish:
733 if (langset) FcLangSetDestroy (langset);
734 if (charset && fc_charset_idx < 0) FcCharSetDestroy (charset);
735 return pattern;
738 static Lisp_Object
739 ftfont_list (frame, spec)
740 Lisp_Object frame, spec;
742 Lisp_Object val = Qnil, family;
743 int i;
744 FcPattern *pattern;
745 FcFontSet *fontset = NULL;
746 FcObjectSet *objset = NULL;
747 FcCharSet *charset;
748 Lisp_Object chars = Qnil;
749 FcResult result;
750 char otlayout[15]; /* For "otlayout:XXXX" */
751 struct OpenTypeSpec *otspec = NULL;
752 int spacing = -1;
754 if (! fc_initialized)
756 FcInit ();
757 fc_initialized = 1;
760 pattern = ftfont_spec_pattern (spec, otlayout, &otspec);
761 if (! pattern)
762 return Qnil;
763 if (FcPatternGetCharSet (pattern, FC_CHARSET, 0, &charset) != FcResultMatch)
765 val = assq_no_quit (QCscript, AREF (spec, FONT_EXTRA_INDEX));
766 if (! NILP (val))
768 val = assq_no_quit (XCDR (val), Vscript_representative_chars);
769 if (CONSP (val) && VECTORP (XCDR (val)))
770 chars = XCDR (val);
772 val = Qnil;
774 if (INTEGERP (AREF (spec, FONT_SPACING_INDEX)))
775 spacing = XINT (AREF (spec, FONT_SPACING_INDEX));
776 family = AREF (spec, FONT_FAMILY_INDEX);
777 if (! NILP (family))
779 Lisp_Object resolved;
781 resolved = ftfont_resolve_generic_family (family, pattern);
782 if (! NILP (resolved))
784 FcPatternDel (pattern, FC_FAMILY);
785 if (! FcPatternAddString (pattern, FC_FAMILY,
786 SYMBOL_FcChar8 (resolved)))
787 goto err;
791 objset = FcObjectSetBuild (FC_FOUNDRY, FC_FAMILY, FC_WEIGHT, FC_SLANT,
792 FC_WIDTH, FC_PIXEL_SIZE, FC_SPACING, FC_SCALABLE,
793 FC_FILE, FC_INDEX,
794 #ifdef FC_CAPABILITY
795 FC_CAPABILITY,
796 #endif /* FC_CAPABILITY */
797 #ifdef FC_FONTFORMAT
798 FC_FONTFORMAT,
799 #endif
800 NULL);
801 if (! objset)
802 goto err;
803 if (! NILP (chars))
804 FcObjectSetAdd (objset, FC_CHARSET);
806 fontset = FcFontList (NULL, pattern, objset);
807 if (! fontset || fontset->nfont == 0)
808 goto finish;
809 #if 0
810 /* Need fix because this finds any fonts. */
811 if (fontset->nfont == 0 && ! NILP (family))
813 /* Try maching with configuration. For instance, the
814 configuration may specify "Nimbus Mono L" as an alias of
815 "Courier". */
816 FcPattern *pat = FcPatternBuild (0, FC_FAMILY, FcTypeString,
817 SYMBOL_FcChar8 (family), NULL);
818 FcChar8 *fam;
820 if (FcConfigSubstitute (NULL, pat, FcMatchPattern) == FcTrue)
822 for (i = 0;
823 FcPatternGetString (pat, FC_FAMILY, i, &fam) == FcResultMatch;
824 i++)
826 FcPatternDel (pattern, FC_FAMILY);
827 FcPatternAddString (pattern, FC_FAMILY, fam);
828 FcFontSetDestroy (fontset);
829 fontset = FcFontList (NULL, pattern, objset);
830 if (fontset->nfont > 0)
831 break;
835 #endif
836 for (i = 0; i < fontset->nfont; i++)
838 Lisp_Object entity;
840 if (spacing >= 0)
842 int this;
844 if ((FcPatternGetInteger (fontset->fonts[i], FC_SPACING, 0, &this)
845 == FcResultMatch)
846 && spacing != this)
847 continue;
850 #ifdef FC_CAPABILITY
851 if (otlayout[0])
853 FcChar8 *this;
855 if (FcPatternGetString (fontset->fonts[i], FC_CAPABILITY, 0, &this)
856 != FcResultMatch
857 || ! strstr ((char *) this, otlayout))
858 continue;
860 #endif /* FC_CAPABILITY */
861 #ifdef HAVE_LIBOTF
862 if (otspec)
864 FcChar8 *file;
865 OTF *otf;
867 if (FcPatternGetString (fontset->fonts[i], FC_FILE, 0, &file)
868 != FcResultMatch)
869 continue;
870 otf = OTF_open ((char *) file);
871 if (! otf)
872 continue;
873 if (OTF_check_features (otf, 1,
874 otspec->script_tag, otspec->langsys_tag,
875 otspec->features[0],
876 otspec->nfeatures[0]) != 1
877 || OTF_check_features (otf, 0,
878 otspec->script_tag, otspec->langsys_tag,
879 otspec->features[1],
880 otspec->nfeatures[1]) != 1)
881 continue;
883 #endif /* HAVE_LIBOTF */
884 if (VECTORP (chars))
886 int j;
888 if (FcPatternGetCharSet (fontset->fonts[i], FC_CHARSET, 0, &charset)
889 != FcResultMatch)
890 continue;
891 for (j = 0; j < ASIZE (chars); j++)
892 if (NATNUMP (AREF (chars, j))
893 && FcCharSetHasChar (charset, XFASTINT (AREF (chars, j))))
894 break;
895 if (j == ASIZE (chars))
896 continue;
898 entity = ftfont_pattern_entity (fontset->fonts[i],
899 AREF (spec, FONT_EXTRA_INDEX));
900 if (! NILP (entity))
901 val = Fcons (entity, val);
903 val = Fnreverse (val);
904 goto finish;
906 err:
907 /* We come here because of unexpected error in fontconfig API call
908 (usually insufficient memory). */
909 val = Qnil;
911 finish:
912 font_add_log ("ftfont-list", spec, val);
913 if (objset) FcObjectSetDestroy (objset);
914 if (fontset) FcFontSetDestroy (fontset);
915 if (pattern) FcPatternDestroy (pattern);
916 return val;
919 static Lisp_Object
920 ftfont_match (frame, spec)
921 Lisp_Object frame, spec;
923 Lisp_Object entity;
924 FcPattern *pattern, *match = NULL;
925 FcResult result;
926 char otlayout[15]; /* For "otlayout:XXXX" */
927 struct OpenTypeSpec *otspec = NULL;
929 if (! fc_initialized)
931 FcInit ();
932 fc_initialized = 1;
935 pattern = ftfont_spec_pattern (spec, otlayout, &otspec);
936 if (! pattern)
937 return Qnil;
939 if (INTEGERP (AREF (spec, FONT_SIZE_INDEX)))
941 FcValue value;
943 value.type = FcTypeDouble;
944 value.u.d = XINT (AREF (spec, FONT_SIZE_INDEX));
945 FcPatternAdd (pattern, FC_PIXEL_SIZE, value, FcFalse);
947 if (FcConfigSubstitute (NULL, pattern, FcMatchPattern) == FcTrue)
949 FcDefaultSubstitute (pattern);
950 match = FcFontMatch (NULL, pattern, &result);
951 if (match)
953 entity = ftfont_pattern_entity (match, AREF (spec, FONT_EXTRA_INDEX));
954 FcPatternDestroy (match);
955 if (! NILP (AREF (spec, FONT_FAMILY_INDEX))
956 && NILP (assq_no_quit (AREF (spec, FONT_FAMILY_INDEX),
957 ftfont_generic_family_list))
958 && NILP (Fstring_equal (AREF (spec, FONT_FAMILY_INDEX),
959 AREF (entity, FONT_FAMILY_INDEX))))
960 entity = Qnil;
963 FcPatternDestroy (pattern);
965 font_add_log ("ftfont-match", spec, entity);
966 return entity;
969 static Lisp_Object
970 ftfont_list_family (frame)
971 Lisp_Object frame;
973 Lisp_Object list;
974 FcPattern *pattern = NULL;
975 FcFontSet *fontset = NULL;
976 FcObjectSet *objset = NULL;
977 int i;
979 if (! fc_initialized)
981 FcInit ();
982 fc_initialized = 1;
985 pattern = FcPatternCreate ();
986 if (! pattern)
987 goto finish;
988 objset = FcObjectSetBuild (FC_FAMILY, NULL);
989 if (! objset)
990 goto finish;
991 fontset = FcFontList (NULL, pattern, objset);
992 if (! fontset)
993 goto finish;
995 list = Qnil;
996 for (i = 0; i < fontset->nfont; i++)
998 FcPattern *pat = fontset->fonts[i];
999 FcChar8 *str;
1001 if (FcPatternGetString (pat, FC_FAMILY, 0, &str) == FcResultMatch)
1002 list = Fcons (intern ((char *) str), list);
1005 finish:
1006 if (objset) FcObjectSetDestroy (objset);
1007 if (fontset) FcFontSetDestroy (fontset);
1008 if (pattern) FcPatternDestroy (pattern);
1010 return list;
1014 static Lisp_Object
1015 ftfont_open (f, entity, pixel_size)
1016 FRAME_PTR f;
1017 Lisp_Object entity;
1018 int pixel_size;
1020 struct ftfont_info *ftfont_info;
1021 struct font *font;
1022 struct ftfont_cache_data *cache_data;
1023 FT_Face ft_face;
1024 FT_Size ft_size;
1025 FT_UInt size;
1026 Lisp_Object val, filename, index, cache, font_object;
1027 int scalable;
1028 int spacing;
1029 char name[256];
1030 int i, len;
1031 int upEM;
1033 val = assq_no_quit (QCfont_entity, AREF (entity, FONT_EXTRA_INDEX));
1034 if (! CONSP (val))
1035 return Qnil;
1036 val = XCDR (val);
1037 cache = ftfont_lookup_cache (val, 1);
1038 if (NILP (cache))
1039 return Qnil;
1040 filename = XCAR (val);
1041 index = XCDR (val);
1042 val = XCDR (cache);
1043 cache_data = XSAVE_VALUE (XCDR (cache))->pointer;
1044 ft_face = cache_data->ft_face;
1045 if (XSAVE_VALUE (val)->integer > 0)
1047 /* FT_Face in this cache is already used by the different size. */
1048 if (FT_New_Size (ft_face, &ft_size) != 0)
1049 return Qnil;
1050 if (FT_Activate_Size (ft_size) != 0)
1052 FT_Done_Size (ft_size);
1053 return Qnil;
1056 XSAVE_VALUE (val)->integer++;
1057 size = XINT (AREF (entity, FONT_SIZE_INDEX));
1058 if (size == 0)
1059 size = pixel_size;
1060 if (FT_Set_Pixel_Sizes (ft_face, size, size) != 0)
1062 if (XSAVE_VALUE (val)->integer == 0)
1063 FT_Done_Face (ft_face);
1064 return Qnil;
1067 font_object = font_make_object (VECSIZE (struct ftfont_info), entity, size);
1068 ASET (font_object, FONT_TYPE_INDEX, Qfreetype);
1069 len = font_unparse_xlfd (entity, size, name, 256);
1070 if (len > 0)
1071 ASET (font_object, FONT_NAME_INDEX, make_unibyte_string (name, len));
1072 len = font_unparse_fcname (entity, size, name, 256);
1073 if (len > 0)
1074 ASET (font_object, FONT_FULLNAME_INDEX, make_unibyte_string (name, len));
1075 else
1076 ASET (font_object, FONT_FULLNAME_INDEX,
1077 AREF (font_object, FONT_NAME_INDEX));
1078 ASET (font_object, FONT_FILE_INDEX, filename);
1079 ASET (font_object, FONT_FORMAT_INDEX, ftfont_font_format (NULL, filename));
1080 font = XFONT_OBJECT (font_object);
1081 ftfont_info = (struct ftfont_info *) font;
1082 ftfont_info->ft_size = ft_face->size;
1083 ftfont_info->index = XINT (index);
1084 #ifdef HAVE_LIBOTF
1085 ftfont_info->maybe_otf = ft_face->face_flags & FT_FACE_FLAG_SFNT;
1086 ftfont_info->otf = NULL;
1087 #endif /* HAVE_LIBOTF */
1088 font->pixel_size = size;
1089 font->driver = &ftfont_driver;
1090 font->encoding_charset = font->repertory_charset = -1;
1092 upEM = ft_face->units_per_EM;
1093 scalable = (INTEGERP (AREF (entity, FONT_AVGWIDTH_INDEX))
1094 && XINT (AREF (entity, FONT_AVGWIDTH_INDEX)) == 0);
1095 if (scalable)
1097 font->ascent = ft_face->ascender * size / upEM;
1098 font->descent = - ft_face->descender * size / upEM;
1099 font->height = ft_face->height * size / upEM;
1101 else
1103 font->ascent = ft_face->size->metrics.ascender >> 6;
1104 font->descent = - ft_face->size->metrics.descender >> 6;
1105 font->height = ft_face->size->metrics.height >> 6;
1107 if (INTEGERP (AREF (entity, FONT_SPACING_INDEX)))
1108 spacing = XINT (AREF (entity, FONT_SPACING_INDEX));
1109 else
1110 spacing = FC_PROPORTIONAL;
1111 if (spacing != FC_PROPORTIONAL)
1112 font->min_width = font->average_width = font->space_width
1113 = (scalable ? ft_face->max_advance_width * size / upEM
1114 : ft_face->size->metrics.max_advance >> 6);
1115 else
1117 int n;
1119 font->min_width = font->average_width = font->space_width = 0;
1120 for (i = 32, n = 0; i < 127; i++)
1121 if (FT_Load_Char (ft_face, i, FT_LOAD_DEFAULT) != 0)
1123 int this_width = ft_face->glyph->metrics.horiAdvance >> 6;
1125 if (this_width > 0
1126 && (! font->min_width || font->min_width > this_width))
1127 font->min_width = this_width;
1128 if (i == 32)
1129 font->space_width = this_width;
1130 font->average_width += this_width;
1131 n++;
1133 if (n > 0)
1134 font->average_width /= n;
1137 font->baseline_offset = 0;
1138 font->relative_compose = 0;
1139 font->default_ascent = 0;
1140 font->vertical_centering = 0;
1141 if (scalable)
1143 font->underline_position = -ft_face->underline_position * size / upEM;
1144 font->underline_thickness = -ft_face->underline_thickness * size / upEM;
1146 else
1148 font->underline_position = -1;
1149 font->underline_thickness = 0;
1152 return font_object;
1155 static void
1156 ftfont_close (f, font)
1157 FRAME_PTR f;
1158 struct font *font;
1160 struct ftfont_info *ftfont_info = (struct ftfont_info *) font;
1161 Lisp_Object val, cache;
1163 val = Fcons (font->props[FONT_FILE_INDEX], make_number (ftfont_info->index));
1164 cache = ftfont_lookup_cache (val, 1);
1165 xassert (CONSP (cache));
1166 val = XCDR (cache);
1167 (XSAVE_VALUE (val)->integer)--;
1168 if (XSAVE_VALUE (val)->integer == 0)
1170 struct ftfont_cache_data *cache_data = XSAVE_VALUE (val)->pointer;
1172 FT_Done_Face (cache_data->ft_face);
1173 #ifdef HAVE_LIBOTF
1174 if (ftfont_info->otf)
1175 OTF_close (ftfont_info->otf);
1176 #endif
1177 cache_data->ft_face = NULL;
1179 else
1180 FT_Done_Size (ftfont_info->ft_size);
1183 static int
1184 ftfont_has_char (entity, c)
1185 Lisp_Object entity;
1186 int c;
1188 FcCharSet *charset = ftfont_get_fc_charset (entity);
1190 return (FcCharSetHasChar (charset, c) == FcTrue);
1193 static unsigned
1194 ftfont_encode_char (font, c)
1195 struct font *font;
1196 int c;
1198 struct ftfont_info *ftfont_info = (struct ftfont_info *) font;
1199 FT_Face ft_face = ftfont_info->ft_size->face;
1200 FT_ULong charcode = c;
1201 FT_UInt code = FT_Get_Char_Index (ft_face, charcode);
1203 return (code > 0 ? code : FONT_INVALID_CODE);
1206 static int
1207 ftfont_text_extents (font, code, nglyphs, metrics)
1208 struct font *font;
1209 unsigned *code;
1210 int nglyphs;
1211 struct font_metrics *metrics;
1213 struct ftfont_info *ftfont_info = (struct ftfont_info *) font;
1214 FT_Face ft_face = ftfont_info->ft_size->face;
1215 int width = 0;
1216 int i, first;
1218 if (ftfont_info->ft_size != ft_face->size)
1219 FT_Activate_Size (ftfont_info->ft_size);
1220 if (metrics)
1221 bzero (metrics, sizeof (struct font_metrics));
1222 for (i = 0, first = 1; i < nglyphs; i++)
1224 if (FT_Load_Glyph (ft_face, code[i], FT_LOAD_DEFAULT) == 0)
1226 FT_Glyph_Metrics *m = &ft_face->glyph->metrics;
1228 if (first)
1230 if (metrics)
1232 metrics->lbearing = m->horiBearingX >> 6;
1233 metrics->rbearing = (m->horiBearingX + m->width) >> 6;
1234 metrics->ascent = m->horiBearingY >> 6;
1235 metrics->descent = (m->horiBearingY + m->height) >> 6;
1237 first = 0;
1239 if (metrics)
1241 if (metrics->lbearing > width + (m->horiBearingX >> 6))
1242 metrics->lbearing = width + (m->horiBearingX >> 6);
1243 if (metrics->rbearing
1244 < width + ((m->horiBearingX + m->width) >> 6))
1245 metrics->rbearing
1246 = width + ((m->horiBearingX + m->width) >> 6);
1247 if (metrics->ascent < (m->horiBearingY >> 6))
1248 metrics->ascent = m->horiBearingY >> 6;
1249 if (metrics->descent > ((m->horiBearingY + m->height) >> 6))
1250 metrics->descent = (m->horiBearingY + m->height) >> 6;
1252 width += m->horiAdvance >> 6;
1254 else
1256 width += font->space_width;
1259 if (metrics)
1260 metrics->width = width;
1262 return width;
1265 static int
1266 ftfont_get_bitmap (font, code, bitmap, bits_per_pixel)
1267 struct font *font;
1268 unsigned code;
1269 struct font_bitmap *bitmap;
1270 int bits_per_pixel;
1272 struct ftfont_info *ftfont_info = (struct ftfont_info *) font;
1273 FT_Face ft_face = ftfont_info->ft_size->face;
1274 FT_Int32 load_flags = FT_LOAD_RENDER;
1276 if (ftfont_info->ft_size != ft_face->size)
1277 FT_Activate_Size (ftfont_info->ft_size);
1278 if (bits_per_pixel == 1)
1280 #ifdef FT_LOAD_TARGET_MONO
1281 load_flags |= FT_LOAD_TARGET_MONO;
1282 #else
1283 load_flags |= FT_LOAD_MONOCHROME;
1284 #endif
1286 else if (bits_per_pixel != 8)
1287 /* We don't support such a rendering. */
1288 return -1;
1290 if (FT_Load_Glyph (ft_face, code, load_flags) != 0)
1291 return -1;
1292 bitmap->bits_per_pixel
1293 = (ft_face->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_MONO ? 1
1294 : ft_face->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_GRAY ? 8
1295 : ft_face->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_LCD ? 8
1296 : ft_face->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_LCD_V ? 8
1297 : -1);
1298 if (bitmap->bits_per_pixel < 0)
1299 /* We don't suport that kind of pixel mode. */
1300 return -1;
1301 bitmap->rows = ft_face->glyph->bitmap.rows;
1302 bitmap->width = ft_face->glyph->bitmap.width;
1303 bitmap->pitch = ft_face->glyph->bitmap.pitch;
1304 bitmap->buffer = ft_face->glyph->bitmap.buffer;
1305 bitmap->left = ft_face->glyph->bitmap_left;
1306 bitmap->top = ft_face->glyph->bitmap_top;
1307 bitmap->advance = ft_face->glyph->metrics.horiAdvance >> 6;
1308 bitmap->extra = NULL;
1310 return 0;
1313 static int
1314 ftfont_anchor_point (font, code, index, x, y)
1315 struct font *font;
1316 unsigned code;
1317 int index;
1318 int *x, *y;
1320 struct ftfont_info *ftfont_info = (struct ftfont_info *) font;
1321 FT_Face ft_face = ftfont_info->ft_size->face;
1323 if (ftfont_info->ft_size != ft_face->size)
1324 FT_Activate_Size (ftfont_info->ft_size);
1325 if (FT_Load_Glyph (ft_face, code, FT_LOAD_DEFAULT) != 0)
1326 return -1;
1327 if (ft_face->glyph->format != FT_GLYPH_FORMAT_OUTLINE)
1328 return -1;
1329 if (index >= ft_face->glyph->outline.n_points)
1330 return -1;
1331 *x = ft_face->glyph->outline.points[index].x;
1332 *y = ft_face->glyph->outline.points[index].y;
1333 return 0;
1336 #ifdef HAVE_LIBOTF
1338 static Lisp_Object
1339 ftfont_otf_features (gsub_gpos)
1340 OTF_GSUB_GPOS *gsub_gpos;
1342 Lisp_Object scripts, langsyses, features, sym;
1343 int i, j, k;
1345 for (scripts = Qnil, i = gsub_gpos->ScriptList.ScriptCount - 1; i >= 0; i--)
1347 OTF_Script *otf_script = gsub_gpos->ScriptList.Script + i;
1349 for (langsyses = Qnil, j = otf_script->LangSysCount - 1; j >= -1; j--)
1351 OTF_LangSys *otf_langsys;
1353 if (j >= 0)
1354 otf_langsys = otf_script->LangSys + j;
1355 else if (otf_script->DefaultLangSysOffset)
1356 otf_langsys = &otf_script->DefaultLangSys;
1357 else
1358 break;
1360 for (features = Qnil, k = otf_langsys->FeatureCount - 1; k >= 0; k--)
1362 OTF_TAG_SYM (sym, gsub_gpos->FeatureList.Feature[k].FeatureTag);
1363 features = Fcons (sym, features);
1365 if (j >= 0)
1366 OTF_TAG_SYM (sym, otf_script->LangSysRecord[j].LangSysTag);
1367 else
1368 sym = Qnil;
1369 langsyses = Fcons (Fcons (sym, features), langsyses);
1372 OTF_TAG_SYM (sym, gsub_gpos->ScriptList.Script[i].ScriptTag);
1373 scripts = Fcons (Fcons (sym, langsyses), scripts);
1375 return scripts;
1380 static Lisp_Object
1381 ftfont_otf_capability (font)
1382 struct font *font;
1384 struct ftfont_info *ftfont_info = (struct ftfont_info *) font;
1385 OTF *otf = ftfont_get_otf (ftfont_info);
1386 Lisp_Object gsub_gpos;
1388 if (! otf)
1389 return Qnil;
1390 gsub_gpos = Fcons (Qnil, Qnil);
1391 if (OTF_get_table (otf, "GSUB") == 0)
1392 XSETCAR (gsub_gpos, ftfont_otf_features (otf->gsub));
1393 if (OTF_get_table (otf, "GPOS") == 0)
1394 XSETCDR (gsub_gpos, ftfont_otf_features (otf->gpos));
1395 return gsub_gpos;
1398 #ifdef HAVE_M17N_FLT
1400 struct MFLTFontFT
1402 MFLTFont flt_font;
1403 struct font *font;
1404 FT_Face ft_face;
1405 OTF *otf;
1408 static int
1409 ftfont_get_glyph_id (font, gstring, from, to)
1410 MFLTFont *font;
1411 MFLTGlyphString *gstring;
1412 int from, to;
1414 struct MFLTFontFT *flt_font_ft = (struct MFLTFontFT *) font;
1415 FT_Face ft_face = flt_font_ft->ft_face;
1416 MFLTGlyph *g;
1418 for (g = gstring->glyphs + from; from < to; g++, from++)
1419 if (! g->encoded)
1421 FT_UInt code = FT_Get_Char_Index (ft_face, g->code);
1423 g->code = code > 0 ? code : FONT_INVALID_CODE;
1424 g->encoded = 1;
1426 return 0;
1429 static int
1430 ftfont_get_metrics (font, gstring, from, to)
1431 MFLTFont *font;
1432 MFLTGlyphString *gstring;
1433 int from, to;
1435 struct MFLTFontFT *flt_font_ft = (struct MFLTFontFT *) font;
1436 FT_Face ft_face = flt_font_ft->ft_face;
1437 MFLTGlyph *g;
1439 for (g = gstring->glyphs + from; from < to; g++, from++)
1440 if (! g->measured)
1442 if (g->code != FONT_INVALID_CODE)
1444 FT_Glyph_Metrics *m;
1446 if (FT_Load_Glyph (ft_face, g->code, FT_LOAD_DEFAULT) != 0)
1447 abort ();
1448 m = &ft_face->glyph->metrics;
1450 g->lbearing = m->horiBearingX;
1451 g->rbearing = m->horiBearingX + m->width;
1452 g->ascent = m->horiBearingY;
1453 g->descent = m->height - m->horiBearingY;
1454 g->xadv = m->horiAdvance;
1456 else
1458 g->lbearing = 0;
1459 g->rbearing = g->xadv = flt_font_ft->font->space_width << 6;
1460 g->ascent = flt_font_ft->font->ascent << 6;
1461 g->descent = flt_font_ft->font->descent << 6;
1463 g->yadv = 0;
1464 g->measured = 1;
1466 return 0;
1469 static int
1470 ftfont_check_otf (MFLTFont *font, MFLTOtfSpec *spec)
1472 struct MFLTFontFT *flt_font_ft = (struct MFLTFontFT *) font;
1473 OTF *otf = flt_font_ft->otf;
1474 OTF_Tag *tags;
1475 int i, n, negative;
1477 for (i = 0; i < 2; i++)
1479 if (! spec->features[i])
1480 continue;
1481 for (n = 0; spec->features[i][n]; n++);
1482 tags = alloca (sizeof (OTF_Tag) * n);
1483 for (n = 0, negative = 0; spec->features[i][n]; n++)
1485 if (spec->features[i][n] == 0xFFFFFFFF)
1486 negative = 1;
1487 else if (negative)
1488 tags[n - 1] = spec->features[i][n] | 0x80000000;
1489 else
1490 tags[n] = spec->features[i][n];
1492 if (n - negative > 0
1493 && OTF_check_features (otf, i == 0, spec->script, spec->langsys,
1494 tags, n - negative) != 1)
1495 return 0;
1497 return 1;
1500 #define DEVICE_DELTA(table, size) \
1501 (((size) >= (table).StartSize && (size) <= (table).EndSize) \
1502 ? (table).DeltaValue[(size) - (table).StartSize] << 6 \
1503 : 0)
1505 static void
1506 adjust_anchor (FT_Face ft_face, OTF_Anchor *anchor,
1507 unsigned code, int x_ppem, int y_ppem, int *x, int *y)
1509 if (anchor->AnchorFormat == 2)
1511 FT_Outline *outline;
1512 int ap = anchor->f.f1.AnchorPoint;
1514 FT_Load_Glyph (ft_face, (FT_UInt) code, FT_LOAD_MONOCHROME);
1515 outline = &ft_face->glyph->outline;
1516 if (ap < outline->n_points)
1518 *x = outline->points[ap].x << 6;
1519 *y = outline->points[ap].y << 6;
1522 else if (anchor->AnchorFormat == 3)
1524 if (anchor->f.f2.XDeviceTable.offset
1525 && anchor->f.f2.XDeviceTable.DeltaValue)
1526 *x += DEVICE_DELTA (anchor->f.f2.XDeviceTable, x_ppem);
1527 if (anchor->f.f2.YDeviceTable.offset
1528 && anchor->f.f2.YDeviceTable.DeltaValue)
1529 *y += DEVICE_DELTA (anchor->f.f2.YDeviceTable, y_ppem);
1533 static OTF_GlyphString otf_gstring;
1535 static int
1536 ftfont_drive_otf (font, spec, in, from, to, out, adjustment)
1537 MFLTFont *font;
1538 MFLTOtfSpec *spec;
1539 MFLTGlyphString *in;
1540 int from, to;
1541 MFLTGlyphString *out;
1542 MFLTGlyphAdjustment *adjustment;
1544 struct MFLTFontFT *flt_font_ft = (struct MFLTFontFT *) font;
1545 FT_Face ft_face = flt_font_ft->ft_face;
1546 OTF *otf = flt_font_ft->otf;
1547 int len = to - from;
1548 int i, j, gidx;
1549 OTF_Glyph *otfg;
1550 char script[5], *langsys = NULL;
1551 char *gsub_features = NULL, *gpos_features = NULL;
1553 if (len == 0)
1554 return from;
1555 OTF_tag_name (spec->script, script);
1556 if (spec->langsys)
1558 langsys = alloca (5);
1559 OTF_tag_name (spec->langsys, langsys);
1561 for (i = 0; i < 2; i++)
1563 char *p;
1565 if (spec->features[i] && spec->features[i][1] != 0xFFFFFFFF)
1567 for (j = 0; spec->features[i][j]; j++);
1568 if (i == 0)
1569 p = gsub_features = alloca (6 * j);
1570 else
1571 p = gpos_features = alloca (6 * j);
1572 for (j = 0; spec->features[i][j]; j++)
1574 if (spec->features[i][j] == 0xFFFFFFFF)
1575 *p++ = '*', *p++ = ',';
1576 else
1578 OTF_tag_name (spec->features[i][j], p);
1579 p[4] = ',';
1580 p += 5;
1583 *--p = '\0';
1587 if (otf_gstring.size == 0)
1589 otf_gstring.glyphs = (OTF_Glyph *) malloc (sizeof (OTF_Glyph) * len);
1590 otf_gstring.size = len;
1592 else if (otf_gstring.size < len)
1594 otf_gstring.glyphs = (OTF_Glyph *) realloc (otf_gstring.glyphs,
1595 sizeof (OTF_Glyph) * len);
1596 otf_gstring.size = len;
1598 otf_gstring.used = len;
1599 memset (otf_gstring.glyphs, 0, sizeof (OTF_Glyph) * len);
1600 for (i = 0; i < len; i++)
1602 otf_gstring.glyphs[i].c = in->glyphs[from + i].c;
1603 otf_gstring.glyphs[i].glyph_id = in->glyphs[from + i].code;
1606 OTF_drive_gdef (otf, &otf_gstring);
1607 gidx = out->used;
1609 if (gsub_features)
1611 if (OTF_drive_gsub (otf, &otf_gstring, script, langsys, gsub_features)
1612 < 0)
1613 goto simple_copy;
1614 if (out->allocated < out->used + otf_gstring.used)
1615 return -2;
1616 for (i = 0, otfg = otf_gstring.glyphs; i < otf_gstring.used; )
1618 MFLTGlyph *g;
1619 int min_from, max_to;
1620 int j;
1622 g = out->glyphs + out->used;
1623 *g = in->glyphs[from + otfg->f.index.from];
1624 if (g->code != otfg->glyph_id)
1626 g->c = 0;
1627 g->code = otfg->glyph_id;
1628 g->measured = 0;
1630 out->used++;
1631 min_from = g->from;
1632 max_to = g->to;
1633 if (otfg->f.index.from < otfg->f.index.to)
1635 /* OTFG substitutes multiple glyphs in IN. */
1636 for (j = from + otfg->f.index.from + 1;
1637 j <= from + otfg->f.index.to; j++)
1639 if (min_from > in->glyphs[j].from)
1640 min_from = in->glyphs[j].from;
1641 if (max_to < in->glyphs[j].to)
1642 max_to = in->glyphs[j].to;
1644 g->from = min_from;
1645 g->to = max_to;
1647 for (i++, otfg++; (i < otf_gstring.used
1648 && otfg->f.index.from == otfg[-1].f.index.from);
1649 i++, otfg++)
1651 g = out->glyphs + out->used;
1652 *g = in->glyphs[from + otfg->f.index.to];
1653 if (g->code != otfg->glyph_id)
1655 g->c = 0;
1656 g->code = otfg->glyph_id;
1657 g->measured = 0;
1659 out->used++;
1663 else
1665 if (out->allocated < out->used + len)
1666 return -2;
1667 for (i = 0; i < len; i++)
1668 out->glyphs[out->used++] = in->glyphs[from + i];
1671 if (gpos_features)
1673 MFLTGlyph *base = NULL, *mark = NULL, *g;
1674 int x_ppem, y_ppem, x_scale, y_scale;
1676 if (OTF_drive_gpos (otf, &otf_gstring, script, langsys, gpos_features)
1677 < 0)
1678 return to;
1680 x_ppem = ft_face->size->metrics.x_ppem;
1681 y_ppem = ft_face->size->metrics.y_ppem;
1682 x_scale = ft_face->size->metrics.x_scale;
1683 y_scale = ft_face->size->metrics.y_scale;
1685 for (i = 0, otfg = otf_gstring.glyphs, g = out->glyphs + gidx;
1686 i < otf_gstring.used; i++, otfg++, g++)
1688 MFLTGlyph *prev;
1690 if (! otfg->glyph_id)
1691 continue;
1692 switch (otfg->positioning_type)
1694 case 0:
1695 break;
1696 case 1: /* Single */
1697 case 2: /* Pair */
1699 int format = otfg->f.f1.format;
1701 if (format & OTF_XPlacement)
1702 adjustment[i].xoff
1703 = otfg->f.f1.value->XPlacement * x_scale / 0x10000;
1704 if (format & OTF_XPlaDevice)
1705 adjustment[i].xoff
1706 += DEVICE_DELTA (otfg->f.f1.value->XPlaDevice, x_ppem);
1707 if (format & OTF_YPlacement)
1708 adjustment[i].yoff
1709 = - (otfg->f.f1.value->YPlacement * y_scale / 0x10000);
1710 if (format & OTF_YPlaDevice)
1711 adjustment[i].yoff
1712 -= DEVICE_DELTA (otfg->f.f1.value->YPlaDevice, y_ppem);
1713 if (format & OTF_XAdvance)
1714 adjustment[i].xadv
1715 += otfg->f.f1.value->XAdvance * x_scale / 0x10000;
1716 if (format & OTF_XAdvDevice)
1717 adjustment[i].xadv
1718 += DEVICE_DELTA (otfg->f.f1.value->XAdvDevice, x_ppem);
1719 if (format & OTF_YAdvance)
1720 adjustment[i].yadv
1721 += otfg->f.f1.value->YAdvance * y_scale / 0x10000;
1722 if (format & OTF_YAdvDevice)
1723 adjustment[i].yadv
1724 += DEVICE_DELTA (otfg->f.f1.value->YAdvDevice, y_ppem);
1725 adjustment[i].set = 1;
1727 break;
1728 case 3: /* Cursive */
1729 /* Not yet supported. */
1730 break;
1731 case 4: /* Mark-to-Base */
1732 case 5: /* Mark-to-Ligature */
1733 if (! base)
1734 break;
1735 prev = base;
1736 goto label_adjust_anchor;
1737 default: /* i.e. case 6 Mark-to-Mark */
1738 if (! mark)
1739 break;
1740 prev = mark;
1742 label_adjust_anchor:
1744 int base_x, base_y, mark_x, mark_y;
1745 int this_from, this_to;
1747 base_x = otfg->f.f4.base_anchor->XCoordinate * x_scale / 0x10000;
1748 base_y = otfg->f.f4.base_anchor->YCoordinate * y_scale / 0x10000;
1749 mark_x = otfg->f.f4.mark_anchor->XCoordinate * x_scale / 0x10000;
1750 mark_y = otfg->f.f4.mark_anchor->YCoordinate * y_scale / 0x10000;;
1752 if (otfg->f.f4.base_anchor->AnchorFormat != 1)
1753 adjust_anchor (ft_face, otfg->f.f4.base_anchor,
1754 prev->code, x_ppem, y_ppem, &base_x, &base_y);
1755 if (otfg->f.f4.mark_anchor->AnchorFormat != 1)
1756 adjust_anchor (ft_face, otfg->f.f4.mark_anchor, g->code,
1757 x_ppem, y_ppem, &mark_x, &mark_y);
1758 adjustment[i].xoff = (base_x - mark_x);
1759 adjustment[i].yoff = - (base_y - mark_y);
1760 adjustment[i].back = (g - prev);
1761 adjustment[i].xadv = 0;
1762 adjustment[i].advance_is_absolute = 1;
1763 adjustment[i].set = 1;
1764 this_from = g->from;
1765 this_to = g->to;
1766 for (j = 0; prev + j < g; j++)
1768 if (this_from > prev[j].from)
1769 this_from = prev[j].from;
1770 if (this_to < prev[j].to)
1771 this_to = prev[j].to;
1773 for (; prev <= g; prev++)
1775 prev->from = this_from;
1776 prev->to = this_to;
1780 if (otfg->GlyphClass == OTF_GlyphClass0)
1781 base = mark = g;
1782 else if (otfg->GlyphClass == OTF_GlyphClassMark)
1783 mark = g;
1784 else
1785 base = g;
1788 return to;
1790 simple_copy:
1791 if (out->allocated < out->used + len)
1792 return -2;
1793 font->get_metrics (font, in, from, to);
1794 memcpy (out->glyphs + out->used, in->glyphs + from,
1795 sizeof (MFLTGlyph) * len);
1796 out->used += len;
1797 return to;
1800 static MFLTGlyphString gstring;
1802 static int m17n_flt_initialized;
1804 extern Lisp_Object QCfamily;
1806 Lisp_Object
1807 ftfont_shape_by_flt (lgstring, font, ft_face, otf)
1808 Lisp_Object lgstring;
1809 struct font *font;
1810 FT_Face ft_face;
1811 OTF *otf;
1813 EMACS_UINT len = LGSTRING_GLYPH_LEN (lgstring);
1814 EMACS_UINT i;
1815 struct MFLTFontFT flt_font_ft;
1817 if (! m17n_flt_initialized)
1819 M17N_INIT ();
1820 m17n_flt_initialized = 1;
1823 for (i = 0; i < len; i++)
1824 if (NILP (LGSTRING_GLYPH (lgstring, i)))
1825 break;
1826 len = i;
1828 if (gstring.allocated == 0)
1830 gstring.allocated = len * 2;
1831 gstring.glyph_size = sizeof (MFLTGlyph);
1832 gstring.glyphs = malloc (sizeof (MFLTGlyph) * gstring.allocated);
1834 else if (gstring.allocated < len * 2)
1836 gstring.allocated = len * 2;
1837 gstring.glyphs = realloc (gstring.glyphs,
1838 sizeof (MFLTGlyph) * gstring.allocated);
1840 for (i = 0; i < len; i++)
1841 gstring.glyphs[i].c = LGLYPH_CHAR (LGSTRING_GLYPH (lgstring, i));
1842 gstring.used = len;
1843 gstring.r2l = 0;
1846 Lisp_Object family = Ffont_get (LGSTRING_FONT (lgstring), QCfamily);
1848 if (NILP (family))
1849 flt_font_ft.flt_font.family = Mnil;
1850 else
1851 flt_font_ft.flt_font.family
1852 = msymbol ((char *) SDATA (SYMBOL_NAME (family)));
1854 flt_font_ft.flt_font.x_ppem = ft_face->size->metrics.x_ppem;
1855 flt_font_ft.flt_font.y_ppem = ft_face->size->metrics.y_ppem;
1856 flt_font_ft.flt_font.get_glyph_id = ftfont_get_glyph_id;
1857 flt_font_ft.flt_font.get_metrics = ftfont_get_metrics;
1858 flt_font_ft.flt_font.check_otf = ftfont_check_otf;
1859 flt_font_ft.flt_font.drive_otf = ftfont_drive_otf;
1860 flt_font_ft.flt_font.internal = NULL;
1861 flt_font_ft.font = font;
1862 flt_font_ft.ft_face = ft_face;
1863 flt_font_ft.otf = otf;
1864 for (i = 0; i < 3; i++)
1866 int result = mflt_run (&gstring, 0, len, &flt_font_ft.flt_font, NULL);
1867 if (result != -2)
1868 break;
1869 gstring.allocated += gstring.allocated;
1870 gstring.glyphs = realloc (gstring.glyphs,
1871 sizeof (MFLTGlyph) * gstring.allocated);
1873 if (gstring.used > LGSTRING_GLYPH_LEN (lgstring))
1874 return Qnil;
1875 for (i = 0; i < gstring.used; i++)
1877 MFLTGlyph *g = gstring.glyphs + i;
1879 g->from = LGLYPH_FROM (LGSTRING_GLYPH (lgstring, g->from));
1880 g->to = LGLYPH_TO (LGSTRING_GLYPH (lgstring, g->to));
1883 for (i = 0; i < gstring.used; i++)
1885 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
1886 MFLTGlyph *g = gstring.glyphs + i;
1888 if (NILP (lglyph))
1890 lglyph = Fmake_vector (make_number (LGLYPH_SIZE), Qnil);
1891 LGSTRING_SET_GLYPH (lgstring, i, lglyph);
1893 LGLYPH_SET_FROM (lglyph, g->from);
1894 LGLYPH_SET_TO (lglyph, g->to);
1895 LGLYPH_SET_CHAR (lglyph, g->c);
1896 LGLYPH_SET_CODE (lglyph, g->code);
1897 LGLYPH_SET_WIDTH (lglyph, g->xadv >> 6);
1898 LGLYPH_SET_LBEARING (lglyph, g->lbearing >> 6);
1899 LGLYPH_SET_RBEARING (lglyph, g->rbearing >> 6);
1900 LGLYPH_SET_ASCENT (lglyph, g->ascent >> 6);
1901 LGLYPH_SET_DESCENT (lglyph, g->descent >> 6);
1902 if (g->adjusted)
1904 Lisp_Object vec;
1906 vec = Fmake_vector (make_number (3), Qnil);
1907 ASET (vec, 0, make_number (g->xoff >> 6));
1908 ASET (vec, 1, make_number (g->yoff >> 6));
1909 ASET (vec, 2, make_number (g->xadv >> 6));
1910 LGLYPH_SET_ADJUSTMENT (lglyph, vec);
1913 return make_number (i);
1916 Lisp_Object
1917 ftfont_shape (lgstring)
1918 Lisp_Object lgstring;
1920 struct font *font;
1921 struct ftfont_info *ftfont_info;
1922 OTF *otf;
1924 CHECK_FONT_GET_OBJECT (LGSTRING_FONT (lgstring), font);
1925 ftfont_info = (struct ftfont_info *) font;
1926 otf = ftfont_get_otf (ftfont_info);
1927 if (! otf)
1928 return make_number (0);
1929 return ftfont_shape_by_flt (lgstring, font, ftfont_info->ft_size->face, otf);
1932 #endif /* HAVE_M17N_FLT */
1933 #endif /* HAVE_LIBOTF */
1935 Lisp_Object
1936 ftfont_font_format (FcPattern *pattern, Lisp_Object filename)
1938 FcChar8 *str;
1940 #ifdef FC_FONTFORMAT
1941 if (pattern)
1943 if (FcPatternGetString (pattern, FC_FONTFORMAT, 0, &str) != FcResultMatch)
1944 return Qnil;
1945 if (strcmp ((char *) str, "TrueType") == 0)
1946 return intern ("truetype");
1947 if (strcmp ((char *) str, "Type 1") == 0)
1948 return intern ("type1");
1949 if (strcmp ((char *) str, "PCF") == 0)
1950 return intern ("pcf");
1951 if (strcmp ((char *) str, "BDF") == 0)
1952 return intern ("bdf");
1954 #endif /* FC_FONTFORMAT */
1955 if (STRINGP (filename))
1957 int len = SBYTES (filename);
1959 if (len >= 4)
1961 str = (FcChar8 *) (SDATA (filename) + len - 4);
1962 if (xstrcasecmp ((char *) str, ".ttf") == 0)
1963 return intern ("truetype");
1964 if (xstrcasecmp ((char *) str, ".pfb") == 0)
1965 return intern ("type1");
1966 if (xstrcasecmp ((char *) str, ".pcf") == 0)
1967 return intern ("pcf");
1968 if (xstrcasecmp ((char *) str, ".bdf") == 0)
1969 return intern ("bdf");
1972 return intern ("unknown");
1976 void
1977 syms_of_ftfont ()
1979 DEFSYM (Qfreetype, "freetype");
1980 DEFSYM (Qmonospace, "monospace");
1981 DEFSYM (Qsans_serif, "sans-serif");
1982 DEFSYM (Qserif, "serif");
1983 DEFSYM (Qmono, "mono");
1984 DEFSYM (Qsans, "sans");
1985 DEFSYM (Qsans__serif, "sans serif");
1987 staticpro (&freetype_font_cache);
1988 freetype_font_cache = Fcons (Qt, Qnil);
1990 staticpro (&ftfont_generic_family_list);
1991 ftfont_generic_family_list
1992 = Fcons (Fcons (Qmonospace, Qt),
1993 Fcons (Fcons (Qsans_serif, Qt),
1994 Fcons (Fcons (Qsans, Qt), Qnil)));
1996 staticpro (&ft_face_cache);
1997 ft_face_cache = Qnil;
1999 ftfont_driver.type = Qfreetype;
2000 register_font_driver (&ftfont_driver, NULL);
2003 /* arch-tag: 7cfa432c-33a6-4988-83d2-a82ed8604aca
2004 (do not change this comment) */