(fill_gstring_header): Fix copy/paste typo.
[emacs.git] / src / ftfont.c
blob6d1033555d31cb602a054fc6f7ab2dc57d24709e
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, l;
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 l = otf_langsys->FeatureIndex[k];
1363 if (l >= gsub_gpos->FeatureList.FeatureCount)
1364 continue;
1365 OTF_TAG_SYM (sym, gsub_gpos->FeatureList.Feature[l].FeatureTag);
1366 features = Fcons (sym, features);
1368 if (j >= 0)
1369 OTF_TAG_SYM (sym, otf_script->LangSysRecord[j].LangSysTag);
1370 else
1371 sym = Qnil;
1372 langsyses = Fcons (Fcons (sym, features), langsyses);
1375 OTF_TAG_SYM (sym, gsub_gpos->ScriptList.Script[i].ScriptTag);
1376 scripts = Fcons (Fcons (sym, langsyses), scripts);
1378 return scripts;
1383 static Lisp_Object
1384 ftfont_otf_capability (font)
1385 struct font *font;
1387 struct ftfont_info *ftfont_info = (struct ftfont_info *) font;
1388 OTF *otf = ftfont_get_otf (ftfont_info);
1389 Lisp_Object gsub_gpos;
1391 if (! otf)
1392 return Qnil;
1393 gsub_gpos = Fcons (Qnil, Qnil);
1394 if (OTF_get_table (otf, "GSUB") == 0
1395 && otf->gsub->FeatureList.FeatureCount > 0)
1396 XSETCAR (gsub_gpos, ftfont_otf_features (otf->gsub));
1397 if (OTF_get_table (otf, "GPOS") == 0
1398 && otf->gpos->FeatureList.FeatureCount > 0)
1399 XSETCDR (gsub_gpos, ftfont_otf_features (otf->gpos));
1400 return gsub_gpos;
1403 #ifdef HAVE_M17N_FLT
1405 struct MFLTFontFT
1407 MFLTFont flt_font;
1408 struct font *font;
1409 FT_Face ft_face;
1410 OTF *otf;
1413 static int
1414 ftfont_get_glyph_id (font, gstring, from, to)
1415 MFLTFont *font;
1416 MFLTGlyphString *gstring;
1417 int from, to;
1419 struct MFLTFontFT *flt_font_ft = (struct MFLTFontFT *) font;
1420 FT_Face ft_face = flt_font_ft->ft_face;
1421 MFLTGlyph *g;
1423 for (g = gstring->glyphs + from; from < to; g++, from++)
1424 if (! g->encoded)
1426 FT_UInt code = FT_Get_Char_Index (ft_face, g->code);
1428 g->code = code > 0 ? code : FONT_INVALID_CODE;
1429 g->encoded = 1;
1431 return 0;
1434 static int
1435 ftfont_get_metrics (font, gstring, from, to)
1436 MFLTFont *font;
1437 MFLTGlyphString *gstring;
1438 int from, to;
1440 struct MFLTFontFT *flt_font_ft = (struct MFLTFontFT *) font;
1441 FT_Face ft_face = flt_font_ft->ft_face;
1442 MFLTGlyph *g;
1444 for (g = gstring->glyphs + from; from < to; g++, from++)
1445 if (! g->measured)
1447 if (g->code != FONT_INVALID_CODE)
1449 FT_Glyph_Metrics *m;
1451 if (FT_Load_Glyph (ft_face, g->code, FT_LOAD_DEFAULT) != 0)
1452 abort ();
1453 m = &ft_face->glyph->metrics;
1455 g->lbearing = m->horiBearingX;
1456 g->rbearing = m->horiBearingX + m->width;
1457 g->ascent = m->horiBearingY;
1458 g->descent = m->height - m->horiBearingY;
1459 g->xadv = m->horiAdvance;
1461 else
1463 g->lbearing = 0;
1464 g->rbearing = g->xadv = flt_font_ft->font->space_width << 6;
1465 g->ascent = flt_font_ft->font->ascent << 6;
1466 g->descent = flt_font_ft->font->descent << 6;
1468 g->yadv = 0;
1469 g->measured = 1;
1471 return 0;
1474 static int
1475 ftfont_check_otf (MFLTFont *font, MFLTOtfSpec *spec)
1477 struct MFLTFontFT *flt_font_ft = (struct MFLTFontFT *) font;
1478 OTF *otf = flt_font_ft->otf;
1479 OTF_Tag *tags;
1480 int i, n, negative;
1482 for (i = 0; i < 2; i++)
1484 if (! spec->features[i])
1485 continue;
1486 for (n = 0; spec->features[i][n]; n++);
1487 tags = alloca (sizeof (OTF_Tag) * n);
1488 for (n = 0, negative = 0; spec->features[i][n]; n++)
1490 if (spec->features[i][n] == 0xFFFFFFFF)
1491 negative = 1;
1492 else if (negative)
1493 tags[n - 1] = spec->features[i][n] | 0x80000000;
1494 else
1495 tags[n] = spec->features[i][n];
1497 if (n - negative > 0
1498 && OTF_check_features (otf, i == 0, spec->script, spec->langsys,
1499 tags, n - negative) != 1)
1500 return 0;
1502 return 1;
1505 #define DEVICE_DELTA(table, size) \
1506 (((size) >= (table).StartSize && (size) <= (table).EndSize) \
1507 ? (table).DeltaValue[(size) - (table).StartSize] << 6 \
1508 : 0)
1510 static void
1511 adjust_anchor (FT_Face ft_face, OTF_Anchor *anchor,
1512 unsigned code, int x_ppem, int y_ppem, int *x, int *y)
1514 if (anchor->AnchorFormat == 2)
1516 FT_Outline *outline;
1517 int ap = anchor->f.f1.AnchorPoint;
1519 FT_Load_Glyph (ft_face, (FT_UInt) code, FT_LOAD_MONOCHROME);
1520 outline = &ft_face->glyph->outline;
1521 if (ap < outline->n_points)
1523 *x = outline->points[ap].x << 6;
1524 *y = outline->points[ap].y << 6;
1527 else if (anchor->AnchorFormat == 3)
1529 if (anchor->f.f2.XDeviceTable.offset
1530 && anchor->f.f2.XDeviceTable.DeltaValue)
1531 *x += DEVICE_DELTA (anchor->f.f2.XDeviceTable, x_ppem);
1532 if (anchor->f.f2.YDeviceTable.offset
1533 && anchor->f.f2.YDeviceTable.DeltaValue)
1534 *y += DEVICE_DELTA (anchor->f.f2.YDeviceTable, y_ppem);
1538 static OTF_GlyphString otf_gstring;
1540 static int
1541 ftfont_drive_otf (font, spec, in, from, to, out, adjustment)
1542 MFLTFont *font;
1543 MFLTOtfSpec *spec;
1544 MFLTGlyphString *in;
1545 int from, to;
1546 MFLTGlyphString *out;
1547 MFLTGlyphAdjustment *adjustment;
1549 struct MFLTFontFT *flt_font_ft = (struct MFLTFontFT *) font;
1550 FT_Face ft_face = flt_font_ft->ft_face;
1551 OTF *otf = flt_font_ft->otf;
1552 int len = to - from;
1553 int i, j, gidx;
1554 OTF_Glyph *otfg;
1555 char script[5], *langsys = NULL;
1556 char *gsub_features = NULL, *gpos_features = NULL;
1558 if (len == 0)
1559 return from;
1560 OTF_tag_name (spec->script, script);
1561 if (spec->langsys)
1563 langsys = alloca (5);
1564 OTF_tag_name (spec->langsys, langsys);
1566 for (i = 0; i < 2; i++)
1568 char *p;
1570 if (spec->features[i] && spec->features[i][1] != 0xFFFFFFFF)
1572 for (j = 0; spec->features[i][j]; j++);
1573 if (i == 0)
1574 p = gsub_features = alloca (6 * j);
1575 else
1576 p = gpos_features = alloca (6 * j);
1577 for (j = 0; spec->features[i][j]; j++)
1579 if (spec->features[i][j] == 0xFFFFFFFF)
1580 *p++ = '*', *p++ = ',';
1581 else
1583 OTF_tag_name (spec->features[i][j], p);
1584 p[4] = ',';
1585 p += 5;
1588 *--p = '\0';
1592 if (otf_gstring.size == 0)
1594 otf_gstring.glyphs = (OTF_Glyph *) malloc (sizeof (OTF_Glyph) * len);
1595 otf_gstring.size = len;
1597 else if (otf_gstring.size < len)
1599 otf_gstring.glyphs = (OTF_Glyph *) realloc (otf_gstring.glyphs,
1600 sizeof (OTF_Glyph) * len);
1601 otf_gstring.size = len;
1603 otf_gstring.used = len;
1604 memset (otf_gstring.glyphs, 0, sizeof (OTF_Glyph) * len);
1605 for (i = 0; i < len; i++)
1607 otf_gstring.glyphs[i].c = in->glyphs[from + i].c;
1608 otf_gstring.glyphs[i].glyph_id = in->glyphs[from + i].code;
1611 OTF_drive_gdef (otf, &otf_gstring);
1612 gidx = out->used;
1614 if (gsub_features)
1616 if (OTF_drive_gsub (otf, &otf_gstring, script, langsys, gsub_features)
1617 < 0)
1618 goto simple_copy;
1619 if (out->allocated < out->used + otf_gstring.used)
1620 return -2;
1621 for (i = 0, otfg = otf_gstring.glyphs; i < otf_gstring.used; )
1623 MFLTGlyph *g;
1624 int min_from, max_to;
1625 int j;
1627 g = out->glyphs + out->used;
1628 *g = in->glyphs[from + otfg->f.index.from];
1629 if (g->code != otfg->glyph_id)
1631 g->c = 0;
1632 g->code = otfg->glyph_id;
1633 g->measured = 0;
1635 out->used++;
1636 min_from = g->from;
1637 max_to = g->to;
1638 if (otfg->f.index.from < otfg->f.index.to)
1640 /* OTFG substitutes multiple glyphs in IN. */
1641 for (j = from + otfg->f.index.from + 1;
1642 j <= from + otfg->f.index.to; j++)
1644 if (min_from > in->glyphs[j].from)
1645 min_from = in->glyphs[j].from;
1646 if (max_to < in->glyphs[j].to)
1647 max_to = in->glyphs[j].to;
1649 g->from = min_from;
1650 g->to = max_to;
1652 for (i++, otfg++; (i < otf_gstring.used
1653 && otfg->f.index.from == otfg[-1].f.index.from);
1654 i++, otfg++)
1656 g = out->glyphs + out->used;
1657 *g = in->glyphs[from + otfg->f.index.to];
1658 if (g->code != otfg->glyph_id)
1660 g->c = 0;
1661 g->code = otfg->glyph_id;
1662 g->measured = 0;
1664 out->used++;
1668 else
1670 if (out->allocated < out->used + len)
1671 return -2;
1672 for (i = 0; i < len; i++)
1673 out->glyphs[out->used++] = in->glyphs[from + i];
1676 if (gpos_features)
1678 MFLTGlyph *base = NULL, *mark = NULL, *g;
1679 int x_ppem, y_ppem, x_scale, y_scale;
1681 if (OTF_drive_gpos (otf, &otf_gstring, script, langsys, gpos_features)
1682 < 0)
1683 return to;
1685 x_ppem = ft_face->size->metrics.x_ppem;
1686 y_ppem = ft_face->size->metrics.y_ppem;
1687 x_scale = ft_face->size->metrics.x_scale;
1688 y_scale = ft_face->size->metrics.y_scale;
1690 for (i = 0, otfg = otf_gstring.glyphs, g = out->glyphs + gidx;
1691 i < otf_gstring.used; i++, otfg++, g++)
1693 MFLTGlyph *prev;
1695 if (! otfg->glyph_id)
1696 continue;
1697 switch (otfg->positioning_type)
1699 case 0:
1700 break;
1701 case 1: /* Single */
1702 case 2: /* Pair */
1704 int format = otfg->f.f1.format;
1706 if (format & OTF_XPlacement)
1707 adjustment[i].xoff
1708 = otfg->f.f1.value->XPlacement * x_scale / 0x10000;
1709 if (format & OTF_XPlaDevice)
1710 adjustment[i].xoff
1711 += DEVICE_DELTA (otfg->f.f1.value->XPlaDevice, x_ppem);
1712 if (format & OTF_YPlacement)
1713 adjustment[i].yoff
1714 = - (otfg->f.f1.value->YPlacement * y_scale / 0x10000);
1715 if (format & OTF_YPlaDevice)
1716 adjustment[i].yoff
1717 -= DEVICE_DELTA (otfg->f.f1.value->YPlaDevice, y_ppem);
1718 if (format & OTF_XAdvance)
1719 adjustment[i].xadv
1720 += otfg->f.f1.value->XAdvance * x_scale / 0x10000;
1721 if (format & OTF_XAdvDevice)
1722 adjustment[i].xadv
1723 += DEVICE_DELTA (otfg->f.f1.value->XAdvDevice, x_ppem);
1724 if (format & OTF_YAdvance)
1725 adjustment[i].yadv
1726 += otfg->f.f1.value->YAdvance * y_scale / 0x10000;
1727 if (format & OTF_YAdvDevice)
1728 adjustment[i].yadv
1729 += DEVICE_DELTA (otfg->f.f1.value->YAdvDevice, y_ppem);
1730 adjustment[i].set = 1;
1732 break;
1733 case 3: /* Cursive */
1734 /* Not yet supported. */
1735 break;
1736 case 4: /* Mark-to-Base */
1737 case 5: /* Mark-to-Ligature */
1738 if (! base)
1739 break;
1740 prev = base;
1741 goto label_adjust_anchor;
1742 default: /* i.e. case 6 Mark-to-Mark */
1743 if (! mark)
1744 break;
1745 prev = mark;
1747 label_adjust_anchor:
1749 int base_x, base_y, mark_x, mark_y;
1750 int this_from, this_to;
1752 base_x = otfg->f.f4.base_anchor->XCoordinate * x_scale / 0x10000;
1753 base_y = otfg->f.f4.base_anchor->YCoordinate * y_scale / 0x10000;
1754 mark_x = otfg->f.f4.mark_anchor->XCoordinate * x_scale / 0x10000;
1755 mark_y = otfg->f.f4.mark_anchor->YCoordinate * y_scale / 0x10000;;
1757 if (otfg->f.f4.base_anchor->AnchorFormat != 1)
1758 adjust_anchor (ft_face, otfg->f.f4.base_anchor,
1759 prev->code, x_ppem, y_ppem, &base_x, &base_y);
1760 if (otfg->f.f4.mark_anchor->AnchorFormat != 1)
1761 adjust_anchor (ft_face, otfg->f.f4.mark_anchor, g->code,
1762 x_ppem, y_ppem, &mark_x, &mark_y);
1763 adjustment[i].xoff = (base_x - mark_x);
1764 adjustment[i].yoff = - (base_y - mark_y);
1765 adjustment[i].back = (g - prev);
1766 adjustment[i].xadv = 0;
1767 adjustment[i].advance_is_absolute = 1;
1768 adjustment[i].set = 1;
1769 this_from = g->from;
1770 this_to = g->to;
1771 for (j = 0; prev + j < g; j++)
1773 if (this_from > prev[j].from)
1774 this_from = prev[j].from;
1775 if (this_to < prev[j].to)
1776 this_to = prev[j].to;
1778 for (; prev <= g; prev++)
1780 prev->from = this_from;
1781 prev->to = this_to;
1785 if (otfg->GlyphClass == OTF_GlyphClass0)
1786 base = mark = g;
1787 else if (otfg->GlyphClass == OTF_GlyphClassMark)
1788 mark = g;
1789 else
1790 base = g;
1793 return to;
1795 simple_copy:
1796 if (out->allocated < out->used + len)
1797 return -2;
1798 font->get_metrics (font, in, from, to);
1799 memcpy (out->glyphs + out->used, in->glyphs + from,
1800 sizeof (MFLTGlyph) * len);
1801 out->used += len;
1802 return to;
1805 static MFLTGlyphString gstring;
1807 static int m17n_flt_initialized;
1809 extern Lisp_Object QCfamily;
1811 static Lisp_Object
1812 ftfont_shape_by_flt (lgstring, font, ft_face, otf)
1813 Lisp_Object lgstring;
1814 struct font *font;
1815 FT_Face ft_face;
1816 OTF *otf;
1818 EMACS_UINT len = LGSTRING_GLYPH_LEN (lgstring);
1819 EMACS_UINT i;
1820 struct MFLTFontFT flt_font_ft;
1821 MFLT *flt = NULL;
1823 if (! m17n_flt_initialized)
1825 M17N_INIT ();
1826 m17n_flt_initialized = 1;
1829 for (i = 0; i < len; i++)
1830 if (NILP (LGSTRING_GLYPH (lgstring, i)))
1831 break;
1832 len = i;
1834 if (gstring.allocated == 0)
1836 gstring.allocated = len * 2;
1837 gstring.glyph_size = sizeof (MFLTGlyph);
1838 gstring.glyphs = malloc (sizeof (MFLTGlyph) * gstring.allocated);
1840 else if (gstring.allocated < len * 2)
1842 gstring.allocated = len * 2;
1843 gstring.glyphs = realloc (gstring.glyphs,
1844 sizeof (MFLTGlyph) * gstring.allocated);
1846 for (i = 0; i < len; i++)
1847 gstring.glyphs[i].c = LGLYPH_CHAR (LGSTRING_GLYPH (lgstring, i));
1848 gstring.used = len;
1849 gstring.r2l = 0;
1852 Lisp_Object family = Ffont_get (LGSTRING_FONT (lgstring), QCfamily);
1854 if (NILP (family))
1855 flt_font_ft.flt_font.family = Mnil;
1856 else
1857 flt_font_ft.flt_font.family
1858 = msymbol ((char *) SDATA (Fdowncase (SYMBOL_NAME (family))));
1860 flt_font_ft.flt_font.x_ppem = ft_face->size->metrics.x_ppem;
1861 flt_font_ft.flt_font.y_ppem = ft_face->size->metrics.y_ppem;
1862 flt_font_ft.flt_font.get_glyph_id = ftfont_get_glyph_id;
1863 flt_font_ft.flt_font.get_metrics = ftfont_get_metrics;
1864 flt_font_ft.flt_font.check_otf = ftfont_check_otf;
1865 flt_font_ft.flt_font.drive_otf = ftfont_drive_otf;
1866 flt_font_ft.flt_font.internal = NULL;
1867 flt_font_ft.font = font;
1868 flt_font_ft.ft_face = ft_face;
1869 flt_font_ft.otf = otf;
1870 if (ASCII_CHAR_P (gstring.glyphs[0].c))
1871 /* A little bit ad hoc. Perhaps, shaper must get script and
1872 language information, and select a proper flt for them
1873 here. */
1874 flt = mflt_get (msymbol ("combining"));
1875 for (i = 0; i < 3; i++)
1877 int result = mflt_run (&gstring, 0, len, &flt_font_ft.flt_font, flt);
1878 if (result != -2)
1879 break;
1880 gstring.allocated += gstring.allocated;
1881 gstring.glyphs = realloc (gstring.glyphs,
1882 sizeof (MFLTGlyph) * gstring.allocated);
1884 if (gstring.used > LGSTRING_GLYPH_LEN (lgstring))
1885 return Qnil;
1886 for (i = 0; i < gstring.used; i++)
1888 MFLTGlyph *g = gstring.glyphs + i;
1890 g->from = LGLYPH_FROM (LGSTRING_GLYPH (lgstring, g->from));
1891 g->to = LGLYPH_TO (LGSTRING_GLYPH (lgstring, g->to));
1894 for (i = 0; i < gstring.used; i++)
1896 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
1897 MFLTGlyph *g = gstring.glyphs + i;
1899 if (NILP (lglyph))
1901 lglyph = Fmake_vector (make_number (LGLYPH_SIZE), Qnil);
1902 LGSTRING_SET_GLYPH (lgstring, i, lglyph);
1904 LGLYPH_SET_FROM (lglyph, g->from);
1905 LGLYPH_SET_TO (lglyph, g->to);
1906 LGLYPH_SET_CHAR (lglyph, g->c);
1907 LGLYPH_SET_CODE (lglyph, g->code);
1908 LGLYPH_SET_WIDTH (lglyph, g->xadv >> 6);
1909 LGLYPH_SET_LBEARING (lglyph, g->lbearing >> 6);
1910 LGLYPH_SET_RBEARING (lglyph, g->rbearing >> 6);
1911 LGLYPH_SET_ASCENT (lglyph, g->ascent >> 6);
1912 LGLYPH_SET_DESCENT (lglyph, g->descent >> 6);
1913 if (g->adjusted)
1915 Lisp_Object vec;
1917 vec = Fmake_vector (make_number (3), Qnil);
1918 ASET (vec, 0, make_number (g->xoff >> 6));
1919 ASET (vec, 1, make_number (g->yoff >> 6));
1920 ASET (vec, 2, make_number (g->xadv >> 6));
1921 LGLYPH_SET_ADJUSTMENT (lglyph, vec);
1924 return make_number (i);
1927 Lisp_Object
1928 ftfont_shape (lgstring)
1929 Lisp_Object lgstring;
1931 struct font *font;
1932 struct ftfont_info *ftfont_info;
1933 OTF *otf;
1935 CHECK_FONT_GET_OBJECT (LGSTRING_FONT (lgstring), font);
1936 ftfont_info = (struct ftfont_info *) font;
1937 otf = ftfont_get_otf (ftfont_info);
1938 if (! otf)
1939 return make_number (0);
1940 return ftfont_shape_by_flt (lgstring, font, ftfont_info->ft_size->face, otf);
1943 #endif /* HAVE_M17N_FLT */
1944 #endif /* HAVE_LIBOTF */
1946 Lisp_Object
1947 ftfont_font_format (FcPattern *pattern, Lisp_Object filename)
1949 FcChar8 *str;
1951 #ifdef FC_FONTFORMAT
1952 if (pattern)
1954 if (FcPatternGetString (pattern, FC_FONTFORMAT, 0, &str) != FcResultMatch)
1955 return Qnil;
1956 if (strcmp ((char *) str, "TrueType") == 0)
1957 return intern ("truetype");
1958 if (strcmp ((char *) str, "Type 1") == 0)
1959 return intern ("type1");
1960 if (strcmp ((char *) str, "PCF") == 0)
1961 return intern ("pcf");
1962 if (strcmp ((char *) str, "BDF") == 0)
1963 return intern ("bdf");
1965 #endif /* FC_FONTFORMAT */
1966 if (STRINGP (filename))
1968 int len = SBYTES (filename);
1970 if (len >= 4)
1972 str = (FcChar8 *) (SDATA (filename) + len - 4);
1973 if (xstrcasecmp ((char *) str, ".ttf") == 0)
1974 return intern ("truetype");
1975 if (xstrcasecmp ((char *) str, ".pfb") == 0)
1976 return intern ("type1");
1977 if (xstrcasecmp ((char *) str, ".pcf") == 0)
1978 return intern ("pcf");
1979 if (xstrcasecmp ((char *) str, ".bdf") == 0)
1980 return intern ("bdf");
1983 return intern ("unknown");
1987 void
1988 syms_of_ftfont ()
1990 DEFSYM (Qfreetype, "freetype");
1991 DEFSYM (Qmonospace, "monospace");
1992 DEFSYM (Qsans_serif, "sans-serif");
1993 DEFSYM (Qserif, "serif");
1994 DEFSYM (Qmono, "mono");
1995 DEFSYM (Qsans, "sans");
1996 DEFSYM (Qsans__serif, "sans serif");
1998 staticpro (&freetype_font_cache);
1999 freetype_font_cache = Fcons (Qt, Qnil);
2001 staticpro (&ftfont_generic_family_list);
2002 ftfont_generic_family_list
2003 = Fcons (Fcons (Qmonospace, Qt),
2004 Fcons (Fcons (Qsans_serif, Qt),
2005 Fcons (Fcons (Qsans, Qt), Qnil)));
2007 staticpro (&ft_face_cache);
2008 ft_face_cache = Qnil;
2010 ftfont_driver.type = Qfreetype;
2011 register_font_driver (&ftfont_driver, NULL);
2014 /* arch-tag: 7cfa432c-33a6-4988-83d2-a82ed8604aca
2015 (do not change this comment) */