Add information about C-g to the introduction.
[emacs.git] / src / ftfont.c
blob131e4b8aed16f02713546a240edacd35bc57060a
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 "fontset.h"
36 #include "font.h"
37 #include "ftfont.h"
39 /* Symbolic type of this font-driver. */
40 Lisp_Object Qfreetype;
42 /* Fontconfig's generic families and their aliases. */
43 static Lisp_Object Qmonospace, Qsans_serif, Qserif, Qmono, Qsans, Qsans__serif;
45 /* Flag to tell if FcInit is already called or not. */
46 static int fc_initialized;
48 /* Handle to a FreeType library instance. */
49 static FT_Library ft_library;
51 /* Cache for FreeType fonts. */
52 static Lisp_Object freetype_font_cache;
54 /* Cache for FT_Face and FcCharSet. */
55 static Lisp_Object ft_face_cache;
57 /* The actual structure for FreeType font that can be casted to struct
58 font. */
60 struct ftfont_info
62 struct font font;
63 #ifdef HAVE_LIBOTF
64 /* The following three members must be here in this order to be
65 compatible with struct xftfont_info (in xftfont.c). */
66 int maybe_otf; /* Flag to tell if this may be OTF or not. */
67 OTF *otf;
68 #endif /* HAVE_LIBOTF */
69 FT_Size ft_size;
70 int index;
73 static Lisp_Object ftfont_pattern_entity P_ ((FcPattern *, Lisp_Object));
75 static Lisp_Object ftfont_resolve_generic_family P_ ((Lisp_Object,
76 FcPattern *));
77 static Lisp_Object ftfont_lookup_cache P_ ((Lisp_Object, int));
79 Lisp_Object ftfont_font_format P_ ((FcPattern *, Lisp_Object));
81 #define SYMBOL_FcChar8(SYM) (FcChar8 *) SDATA (SYMBOL_NAME (SYM))
83 static struct
85 /* registry name */
86 char *name;
87 /* characters to distinguish the charset from the others */
88 int uniquifier[6];
89 /* additional constraint by language */
90 char *lang;
91 /* set on demand */
92 FcCharSet *fc_charset;
93 } fc_charset_table[] =
94 { { "iso8859-1", { 0x00A0, 0x00A1, 0x00B4, 0x00BC, 0x00D0 } },
95 { "iso8859-2", { 0x00A0, 0x010E }},
96 { "iso8859-3", { 0x00A0, 0x0108 }},
97 { "iso8859-4", { 0x00A0, 0x00AF, 0x0128, 0x0156, 0x02C7 }},
98 { "iso8859-5", { 0x00A0, 0x0401 }},
99 { "iso8859-6", { 0x00A0, 0x060C }},
100 { "iso8859-7", { 0x00A0, 0x0384 }},
101 { "iso8859-8", { 0x00A0, 0x05D0 }},
102 { "iso8859-9", { 0x00A0, 0x00A1, 0x00BC, 0x011E }},
103 { "iso8859-10", { 0x00A0, 0x00D0, 0x0128, 0x2015 }},
104 { "iso8859-11", { 0x00A0, 0x0E01 }},
105 { "iso8859-13", { 0x00A0, 0x201C }},
106 { "iso8859-14", { 0x00A0, 0x0174 }},
107 { "iso8859-15", { 0x00A0, 0x00A1, 0x00D0, 0x0152 }},
108 { "iso8859-16", { 0x00A0, 0x0218}},
109 { "gb2312.1980-0", { 0x4E13 }, "zh-cn"},
110 { "big5-0", { 0xF6B1 }, "zh-tw" },
111 { "jisx0208.1983-0", { 0x4E55 }, "ja"},
112 { "ksc5601.1985-0", { 0xAC00 }, "ko"},
113 { "cns11643.1992-1", { 0xFE32 }, "zh-tw"},
114 { "cns11643.1992-2", { 0x4E33, 0x7934 }},
115 { "cns11643.1992-3", { 0x201A9 }},
116 { "cns11643.1992-4", { 0x20057 }},
117 { "cns11643.1992-5", { 0x20000 }},
118 { "cns11643.1992-6", { 0x20003 }},
119 { "cns11643.1992-7", { 0x20055 }},
120 { "gbk-0", { 0x4E06 }, "zh-cn"},
121 { "jisx0212.1990-0", { 0x4E44 }},
122 { "jisx0213.2000-1", { 0xFA10 }, "ja"},
123 { "jisx0213.2000-2", { 0xFA49 }},
124 { "jisx0213.2004-1", { 0x20B9F }},
125 { "viscii1.1-1", { 0x1EA0, 0x1EAE, 0x1ED2 }, "vi"},
126 { "tis620.2529-1", { 0x0E01 }, "th"},
127 { "windows-1251", { 0x0401, 0x0490 }, "ru"},
128 { "koi8-r", { 0x0401, 0x2219 }, "ru"},
129 { "mulelao-1", { 0x0E81 }, "lo"},
130 { NULL }
133 extern Lisp_Object Qc, Qm, Qp, Qd;
135 static Lisp_Object
136 ftfont_pattern_entity (p, extra)
137 FcPattern *p;
138 Lisp_Object extra;
140 Lisp_Object entity;
141 char *file, *str;
142 int index;
143 int numeric;
144 double dbl;
145 FcBool b;
147 if (FcPatternGetString (p, FC_FILE, 0, (FcChar8 **) &file) != FcResultMatch)
148 return Qnil;
149 if (FcPatternGetInteger (p, FC_INDEX, 0, &index) != FcResultMatch)
150 return Qnil;
152 entity = font_make_entity ();
154 ASET (entity, FONT_TYPE_INDEX, Qfreetype);
155 ASET (entity, FONT_REGISTRY_INDEX, Qiso10646_1);
157 if (FcPatternGetString (p, FC_FOUNDRY, 0, (FcChar8 **) &str) == FcResultMatch)
158 ASET (entity, FONT_FOUNDRY_INDEX, font_intern_prop (str, strlen (str), 1));
159 if (FcPatternGetString (p, FC_FAMILY, 0, (FcChar8 **) &str) == FcResultMatch)
160 ASET (entity, FONT_FAMILY_INDEX, font_intern_prop (str, strlen (str), 1));
161 if (FcPatternGetInteger (p, FC_WEIGHT, 0, &numeric) == FcResultMatch)
163 if (numeric >= FC_WEIGHT_REGULAR && numeric < FC_WEIGHT_MEDIUM)
164 numeric = FC_WEIGHT_MEDIUM;
165 FONT_SET_STYLE (entity, FONT_WEIGHT_INDEX, make_number (numeric));
167 if (FcPatternGetInteger (p, FC_SLANT, 0, &numeric) == FcResultMatch)
169 numeric += 100;
170 FONT_SET_STYLE (entity, FONT_SLANT_INDEX, make_number (numeric));
172 if (FcPatternGetInteger (p, FC_WIDTH, 0, &numeric) == FcResultMatch)
174 FONT_SET_STYLE (entity, FONT_WIDTH_INDEX, make_number (numeric));
176 if (FcPatternGetDouble (p, FC_PIXEL_SIZE, 0, &dbl) == FcResultMatch)
177 ASET (entity, FONT_SIZE_INDEX, make_number (dbl));
178 else
179 ASET (entity, FONT_SIZE_INDEX, make_number (0));
180 if (FcPatternGetInteger (p, FC_SPACING, 0, &numeric) == FcResultMatch)
181 ASET (entity, FONT_SPACING_INDEX, make_number (numeric));
182 if (FcPatternGetDouble (p, FC_DPI, 0, &dbl) == FcResultMatch)
184 int dpi = dbl;
185 ASET (entity, FONT_DPI_INDEX, make_number (dpi));
187 if (FcPatternGetBool (p, FC_SCALABLE, 0, &b) == FcResultMatch
188 && b == FcTrue)
189 ASET (entity, FONT_AVGWIDTH_INDEX, make_number (0));
191 ASET (entity, FONT_EXTRA_INDEX, Fcopy_sequence (extra));
192 font_put_extra (entity, QCfont_entity,
193 Fcons (make_unibyte_string ((char *) file,
194 strlen ((char *) file)),
195 make_number (index)));
196 return entity;
200 static Lisp_Object ftfont_generic_family_list;
202 static Lisp_Object
203 ftfont_resolve_generic_family (family, pattern)
204 Lisp_Object family;
205 FcPattern *pattern;
207 Lisp_Object slot;
208 FcPattern *match;
209 FcResult result;
211 family = Fintern (Fdowncase (SYMBOL_NAME (family)), Qnil);
212 if (EQ (family, Qmono))
213 family = Qmonospace;
214 else if (EQ (family, Qsans) || EQ (family, Qsans__serif))
215 family = Qsans_serif;
216 slot = assq_no_quit (family, ftfont_generic_family_list);
217 if (! CONSP (slot))
218 return Qnil;
219 if (! EQ (XCDR (slot), Qt))
220 return XCDR (slot);
221 pattern = FcPatternDuplicate (pattern);
222 if (! pattern)
223 goto err;
224 FcPatternDel (pattern, FC_FOUNDRY);
225 FcPatternDel (pattern, FC_FAMILY);
226 FcPatternAddString (pattern, FC_FAMILY, SYMBOL_FcChar8 (family));
227 FcConfigSubstitute (NULL, pattern, FcMatchPattern);
228 FcDefaultSubstitute (pattern);
229 match = FcFontMatch (NULL, pattern, &result);
230 if (match)
232 FcChar8 *fam;
234 if (FcPatternGetString (match, FC_FAMILY, 0, &fam) == FcResultMatch)
235 family = intern ((char *) fam);
237 else
238 family = Qnil;
239 XSETCDR (slot, family);
240 err:
241 if (match) FcPatternDestroy (match);
242 if (pattern) FcPatternDestroy (pattern);
243 return family;
246 struct ftfont_cache_data
248 FT_Face ft_face;
249 FcCharSet *fc_charset;
252 static Lisp_Object
253 ftfont_lookup_cache (key, for_face)
254 Lisp_Object key;
255 int for_face;
257 Lisp_Object cache, val;
258 struct ftfont_cache_data *cache_data;
260 cache = assoc_no_quit (key, ft_face_cache);
261 if (NILP (cache))
263 cache_data = xmalloc (sizeof (struct ftfont_cache_data));
264 cache_data->ft_face = NULL;
265 cache_data->fc_charset = NULL;
266 val = make_save_value (NULL, 0);
267 XSAVE_VALUE (val)->integer = 0;
268 XSAVE_VALUE (val)->pointer = cache_data;
269 cache = Fcons (key, val);
270 ft_face_cache = Fcons (cache, ft_face_cache);
272 else
274 val = XCDR (cache);
275 cache_data = XSAVE_VALUE (val)->pointer;
277 if (for_face ? ! cache_data->ft_face : ! cache_data->fc_charset)
279 char *filename = (char *) SDATA (XCAR (key));
280 int index = XINT (XCDR (key));
282 if (for_face)
284 if (! ft_library
285 && FT_Init_FreeType (&ft_library) != 0)
286 return Qnil;
287 if (FT_New_Face (ft_library, filename, index, &cache_data->ft_face)
288 != 0)
289 return Qnil;
291 else
293 FcPattern *pat;
294 FcFontSet *fontset;
295 FcObjectSet *objset;
296 FcCharSet *charset;
298 pat = FcPatternBuild (0, FC_FILE, FcTypeString, (FcChar8 *) filename,
299 FC_INDEX, FcTypeInteger, index, NULL);
300 objset = FcObjectSetBuild (FC_CHARSET, NULL);
301 fontset = FcFontList (NULL, pat, objset);
302 xassert (fontset && fontset->nfont > 0);
303 if (FcPatternGetCharSet (fontset->fonts[0], FC_CHARSET, 0, &charset)
304 == FcResultMatch)
305 cache_data->fc_charset = FcCharSetCopy (charset);
306 else
307 cache_data->fc_charset = FcCharSetCreate ();
308 FcFontSetDestroy (fontset);
309 FcObjectSetDestroy (objset);
310 FcPatternDestroy (pat);
313 return cache;
316 FcCharSet *
317 ftfont_get_fc_charset (entity)
318 Lisp_Object entity;
320 Lisp_Object val, cache;
321 struct ftfont_cache_data *cache_data;
323 val = assq_no_quit (QCfont_entity, AREF (entity, FONT_EXTRA_INDEX));
324 xassert (CONSP (val));
325 val = XCDR (val);
326 cache = ftfont_lookup_cache (val, 0);
327 val = XCDR (cache);
328 cache_data = XSAVE_VALUE (val)->pointer;
329 return cache_data->fc_charset;
332 #ifdef HAVE_LIBOTF
333 static OTF *
334 ftfont_get_otf (ftfont_info)
335 struct ftfont_info *ftfont_info;
337 OTF *otf;
339 if (ftfont_info->otf)
340 return ftfont_info->otf;
341 if (! ftfont_info->maybe_otf)
342 return NULL;
343 otf = OTF_open_ft_face (ftfont_info->ft_size->face);
344 if (! otf || OTF_get_table (otf, "head") < 0)
346 if (otf)
347 OTF_close (otf);
348 ftfont_info->maybe_otf = 0;
349 return NULL;
351 ftfont_info->otf = otf;
352 return otf;
354 #endif /* HAVE_LIBOTF */
356 static Lisp_Object ftfont_get_cache P_ ((FRAME_PTR));
357 static Lisp_Object ftfont_list P_ ((Lisp_Object, Lisp_Object));
358 static Lisp_Object ftfont_match P_ ((Lisp_Object, Lisp_Object));
359 static Lisp_Object ftfont_list_family P_ ((Lisp_Object));
360 static Lisp_Object ftfont_open P_ ((FRAME_PTR, Lisp_Object, int));
361 static void ftfont_close P_ ((FRAME_PTR, struct font *));
362 static int ftfont_has_char P_ ((Lisp_Object, int));
363 static unsigned ftfont_encode_char P_ ((struct font *, int));
364 static int ftfont_text_extents P_ ((struct font *, unsigned *, int,
365 struct font_metrics *));
366 static int ftfont_get_bitmap P_ ((struct font *, unsigned,
367 struct font_bitmap *, int));
368 static int ftfont_anchor_point P_ ((struct font *, unsigned, int,
369 int *, int *));
370 static Lisp_Object ftfont_otf_capability P_ ((struct font *));
371 static Lisp_Object ftfont_shape P_ ((Lisp_Object));
373 struct font_driver ftfont_driver =
375 0, /* Qfreetype */
376 0, /* case insensitive */
377 ftfont_get_cache,
378 ftfont_list,
379 ftfont_match,
380 ftfont_list_family,
381 NULL,
382 ftfont_open,
383 ftfont_close,
384 /* We can't draw a text without device dependent functions. */
385 NULL,
386 NULL,
387 ftfont_has_char,
388 ftfont_encode_char,
389 ftfont_text_extents,
390 /* We can't draw a text without device dependent functions. */
391 NULL,
392 ftfont_get_bitmap,
393 NULL,
394 NULL,
395 NULL,
396 ftfont_anchor_point,
397 #ifdef HAVE_LIBOTF
398 ftfont_otf_capability,
399 #else /* not HAVE_LIBOTF */
400 NULL,
401 #endif /* not HAVE_LIBOTF */
402 NULL,
403 NULL,
404 NULL,
405 #if defined (HAVE_M17N_FLT) && defined (HAVE_LIBOTF)
406 ftfont_shape
407 #else /* not (HAVE_M17N_FLT && HAVE_LIBOTF) */
408 NULL
409 #endif /* not (HAVE_M17N_FLT && HAVE_LIBOTF) */
412 extern Lisp_Object QCname;
414 static Lisp_Object
415 ftfont_get_cache (f)
416 FRAME_PTR f;
418 return freetype_font_cache;
421 static int
422 ftfont_get_charset (registry)
423 Lisp_Object registry;
425 char *str = (char *) SDATA (SYMBOL_NAME (registry));
426 char *re = alloca (SBYTES (SYMBOL_NAME (registry)) * 2 + 1);
427 Lisp_Object regexp;
428 int i, j;
430 for (i = j = 0; i < SBYTES (SYMBOL_NAME (registry)); i++, j++)
432 if (str[i] == '.')
433 re[j++] = '\\';
434 else if (str[i] == '*')
435 re[j++] = '.';
436 re[j] = str[i];
437 if (re[j] == '?')
438 re[j] = '.';
440 re[j] = '\0';
441 regexp = make_unibyte_string (re, j);
442 for (i = 0; fc_charset_table[i].name; i++)
443 if (fast_c_string_match_ignore_case (regexp, fc_charset_table[i].name) >= 0)
444 break;
445 if (! fc_charset_table[i].name)
446 return -1;
447 if (! fc_charset_table[i].fc_charset)
449 FcCharSet *charset = FcCharSetCreate ();
450 int *uniquifier = fc_charset_table[i].uniquifier;
452 if (! charset)
453 return -1;
454 for (j = 0; uniquifier[j]; j++)
455 if (! FcCharSetAddChar (charset, uniquifier[j]))
457 FcCharSetDestroy (charset);
458 return -1;
460 fc_charset_table[i].fc_charset = charset;
462 return i;
465 struct OpenTypeSpec
467 Lisp_Object script;
468 unsigned int script_tag, langsys_tag;
469 int nfeatures[2];
470 unsigned int *features[2];
473 #define OTF_SYM_TAG(SYM, TAG) \
474 do { \
475 unsigned char *p = SDATA (SYMBOL_NAME (SYM)); \
476 TAG = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]; \
477 } while (0)
479 #define OTF_TAG_STR(TAG, P) \
480 do { \
481 (P)[0] = (char) (TAG >> 24); \
482 (P)[1] = (char) ((TAG >> 16) & 0xFF); \
483 (P)[2] = (char) ((TAG >> 8) & 0xFF); \
484 (P)[3] = (char) (TAG & 0xFF); \
485 (P)[4] = '\0'; \
486 } while (0)
488 #define OTF_TAG_SYM(SYM, TAG) \
489 do { \
490 char str[5]; \
492 OTF_TAG_STR (TAG, str); \
493 (SYM) = font_intern_prop (str, 4, 1); \
494 } while (0)
497 static struct OpenTypeSpec *
498 ftfont_get_open_type_spec (Lisp_Object otf_spec)
500 struct OpenTypeSpec *spec = malloc (sizeof (struct OpenTypeSpec));
501 Lisp_Object val;
502 int i, j, negative;
504 if (! spec)
505 return NULL;
506 spec->script = XCAR (otf_spec);
507 if (! NILP (spec->script))
509 OTF_SYM_TAG (spec->script, spec->script_tag);
510 val = assq_no_quit (spec->script, Votf_script_alist);
511 if (CONSP (val) && SYMBOLP (XCDR (val)))
512 spec->script = XCDR (val);
513 else
514 spec->script = Qnil;
516 else
517 spec->script_tag = 0x44464C54; /* "DFLT" */
518 otf_spec = XCDR (otf_spec);
519 val = XCAR (otf_spec);
520 if (! NILP (val))
521 OTF_SYM_TAG (val, spec->langsys_tag);
522 else
523 spec->langsys_tag = 0;
524 spec->nfeatures[0] = spec->nfeatures[1] = 0;
525 for (i = 0; i < 2; i++)
527 Lisp_Object len;
529 otf_spec = XCDR (otf_spec);
530 if (NILP (otf_spec))
531 break;
532 val = XCAR (otf_spec);
533 if (NILP (val))
534 continue;
535 len = Flength (val);
536 spec->features[i] = malloc (sizeof (int) * XINT (len));
537 if (! spec->features[i])
539 if (i > 0 && spec->features[0])
540 free (spec->features[0]);
541 free (spec);
542 return NULL;
544 for (j = 0, negative = 0; CONSP (val); val = XCDR (val))
546 if (NILP (XCAR (val)))
547 negative = 1;
548 else
550 unsigned int tag;
552 OTF_SYM_TAG (XCAR (val), tag);
553 spec->features[i][j++] = negative ? tag & 0x80000000 : tag;
556 spec->nfeatures[i] = j;
558 return spec;
561 static FcPattern *ftfont_spec_pattern P_ ((Lisp_Object, char *,
562 struct OpenTypeSpec **));
564 static FcPattern *
565 ftfont_spec_pattern (spec, otlayout, otspec)
566 Lisp_Object spec;
567 char *otlayout;
568 struct OpenTypeSpec **otspec;
570 Lisp_Object tmp, extra;
571 FcPattern *pattern = NULL;
572 FcCharSet *charset = NULL;
573 FcLangSet *langset = NULL;
574 int n;
575 int dpi = -1;
576 int scalable = -1;
577 Lisp_Object script = Qnil;
578 Lisp_Object registry;
579 int fc_charset_idx;
581 if (! NILP (AREF (spec, FONT_ADSTYLE_INDEX))
582 && SBYTES (SYMBOL_NAME (AREF (spec, FONT_ADSTYLE_INDEX))) > 0)
583 /* Fontconfig doesn't support adstyle property. */
584 return NULL;
585 if ((n = FONT_SLANT_NUMERIC (spec)) >= 0
586 && n < 100)
587 /* Fontconfig doesn't support reverse-italic/obligue. */
588 return NULL;
590 if (INTEGERP (AREF (spec, FONT_DPI_INDEX)))
591 dpi = XINT (AREF (spec, FONT_DPI_INDEX));
592 if (INTEGERP (AREF (spec, FONT_AVGWIDTH_INDEX))
593 && XINT (AREF (spec, FONT_AVGWIDTH_INDEX)) == 0)
594 scalable = 1;
596 registry = AREF (spec, FONT_REGISTRY_INDEX);
597 if (NILP (registry)
598 || EQ (registry, Qascii_0)
599 || EQ (registry, Qiso10646_1)
600 || EQ (registry, Qunicode_bmp)
601 || EQ (registry, Qunicode_sip))
602 fc_charset_idx = -1;
603 else
605 FcChar8 *lang;
607 fc_charset_idx = ftfont_get_charset (registry);
608 if (fc_charset_idx < 0)
609 return NULL;
610 charset = fc_charset_table[fc_charset_idx].fc_charset;
611 lang = (FcChar8 *) fc_charset_table[fc_charset_idx].lang;
612 if (lang)
614 langset = FcLangSetCreate ();
615 if (! langset)
616 goto err;
617 FcLangSetAdd (langset, lang);
621 otlayout[0] = '\0';
622 for (extra = AREF (spec, FONT_EXTRA_INDEX);
623 CONSP (extra); extra = XCDR (extra))
625 Lisp_Object key, val;
627 key = XCAR (XCAR (extra)), val = XCDR (XCAR (extra));
628 if (EQ (key, QCdpi))
629 dpi = XINT (val);
630 else if (EQ (key, QClang))
632 if (! langset)
633 langset = FcLangSetCreate ();
634 if (! langset)
635 goto err;
636 if (SYMBOLP (val))
638 if (! FcLangSetAdd (langset, SYMBOL_FcChar8 (val)))
639 goto err;
641 else
642 for (; CONSP (val); val = XCDR (val))
643 if (SYMBOLP (XCAR (val))
644 && ! FcLangSetAdd (langset, SYMBOL_FcChar8 (XCAR (val))))
645 goto err;
647 else if (EQ (key, QCotf))
649 *otspec = ftfont_get_open_type_spec (val);
650 if (! *otspec)
651 return NULL;
652 strcat (otlayout, "otlayout:");
653 OTF_TAG_STR ((*otspec)->script_tag, otlayout + 9);
654 script = (*otspec)->script;
656 else if (EQ (key, QCscript))
657 script = val;
658 else if (EQ (key, QCscalable))
659 scalable = ! NILP (val);
662 if (! NILP (script) && ! charset)
664 Lisp_Object chars = assq_no_quit (script, Vscript_representative_chars);
666 if (CONSP (chars))
668 charset = FcCharSetCreate ();
669 if (! charset)
670 goto err;
671 for (chars = XCDR (chars); CONSP (chars); chars = XCDR (chars))
672 if (CHARACTERP (XCAR (chars))
673 && ! FcCharSetAddChar (charset, XUINT (XCAR (chars))))
674 goto err;
678 pattern = FcPatternCreate ();
679 if (! pattern)
680 goto err;
681 tmp = AREF (spec, FONT_FOUNDRY_INDEX);
682 if (! NILP (tmp)
683 && ! FcPatternAddString (pattern, FC_FOUNDRY, SYMBOL_FcChar8 (tmp)))
684 goto err;
685 tmp = AREF (spec, FONT_FAMILY_INDEX);
686 if (! NILP (tmp)
687 && ! FcPatternAddString (pattern, FC_FAMILY, SYMBOL_FcChar8 (tmp)))
688 goto err;
689 if (charset
690 && ! FcPatternAddCharSet (pattern, FC_CHARSET, charset))
691 goto err;
692 if (langset
693 && ! FcPatternAddLangSet (pattern, FC_LANG, langset))
694 goto err;
695 if (dpi >= 0
696 && ! FcPatternAddDouble (pattern, FC_DPI, dpi))
697 goto err;
698 if (scalable >= 0
699 && ! FcPatternAddBool (pattern, FC_SCALABLE, scalable ? FcTrue : FcFalse))
700 goto err;
702 goto finish;
704 err:
705 /* We come here because of unexpected error in fontconfig API call
706 (usually insufficient memory). */
707 if (pattern)
709 FcPatternDestroy (pattern);
710 pattern = NULL;
712 if (*otspec)
714 if ((*otspec)->nfeatures[0] > 0)
715 free ((*otspec)->features[0]);
716 if ((*otspec)->nfeatures[1] > 0)
717 free ((*otspec)->features[1]);
718 free (*otspec);
719 *otspec = NULL;
722 finish:
723 if (langset) FcLangSetDestroy (langset);
724 if (charset && fc_charset_idx < 0) FcCharSetDestroy (charset);
725 return pattern;
728 static Lisp_Object
729 ftfont_list (frame, spec)
730 Lisp_Object frame, spec;
732 Lisp_Object val = Qnil, family;
733 int i;
734 FcPattern *pattern;
735 FcFontSet *fontset = NULL;
736 FcObjectSet *objset = NULL;
737 char otlayout[15]; /* For "otlayout:XXXX" */
738 struct OpenTypeSpec *otspec = NULL;
739 int spacing = -1;
741 if (! fc_initialized)
743 FcInit ();
744 fc_initialized = 1;
747 pattern = ftfont_spec_pattern (spec, otlayout, &otspec);
748 if (! pattern)
749 return Qnil;
750 if (INTEGERP (AREF (spec, FONT_SPACING_INDEX)))
751 spacing = XINT (AREF (spec, FONT_SPACING_INDEX));
752 family = AREF (spec, FONT_FAMILY_INDEX);
753 if (! NILP (family))
755 Lisp_Object resolved;
757 resolved = ftfont_resolve_generic_family (family, pattern);
758 if (! NILP (resolved))
760 FcPatternDel (pattern, FC_FAMILY);
761 if (! FcPatternAddString (pattern, FC_FAMILY,
762 SYMBOL_FcChar8 (resolved)))
763 goto err;
767 objset = FcObjectSetBuild (FC_FOUNDRY, FC_FAMILY, FC_WEIGHT, FC_SLANT,
768 FC_WIDTH, FC_PIXEL_SIZE, FC_SPACING, FC_SCALABLE,
769 FC_FILE, FC_INDEX,
770 #ifdef FC_CAPABILITY
771 FC_CAPABILITY,
772 #endif /* FC_CAPABILITY */
773 #ifdef FC_FONTFORMAT
774 FC_FONTFORMAT,
775 #endif
776 NULL);
777 if (! objset)
778 goto err;
780 fontset = FcFontList (NULL, pattern, objset);
781 if (! fontset)
782 goto err;
783 #if 0
784 /* Need fix because this finds any fonts. */
785 if (fontset->nfont == 0 && ! NILP (family))
787 /* Try maching with configuration. For instance, the
788 configuration may specify "Nimbus Mono L" as an alias of
789 "Courier". */
790 FcPattern *pat = FcPatternBuild (0, FC_FAMILY, FcTypeString,
791 SYMBOL_FcChar8 (family), NULL);
792 FcChar8 *fam;
794 if (FcConfigSubstitute (NULL, pat, FcMatchPattern) == FcTrue)
796 for (i = 0;
797 FcPatternGetString (pat, FC_FAMILY, i, &fam) == FcResultMatch;
798 i++)
800 FcPatternDel (pattern, FC_FAMILY);
801 FcPatternAddString (pattern, FC_FAMILY, fam);
802 FcFontSetDestroy (fontset);
803 fontset = FcFontList (NULL, pattern, objset);
804 if (fontset->nfont > 0)
805 break;
809 #endif
810 for (i = 0; i < fontset->nfont; i++)
812 Lisp_Object entity;
814 if (spacing >= 0)
816 int this;
818 if ((FcPatternGetInteger (fontset->fonts[i], FC_SPACING, 0, &this)
819 == FcResultMatch)
820 && spacing != this)
821 continue;
824 #ifdef FC_CAPABILITY
825 if (otlayout[0])
827 FcChar8 *this;
829 if (FcPatternGetString (fontset->fonts[i], FC_CAPABILITY, 0,
830 &this) != FcResultMatch
831 || ! strstr ((char *) this, otlayout))
832 continue;
834 #endif /* FC_CAPABILITY */
835 #ifdef HAVE_LIBOTF
836 if (otspec)
838 FcChar8 *file;
839 OTF *otf;
841 if (FcPatternGetString (fontset->fonts[i], FC_FILE, 0, &file)
842 != FcResultMatch)
843 continue;
844 otf = OTF_open ((char *) file);
845 if (! otf)
846 continue;
847 if (OTF_check_features (otf, 1,
848 otspec->script_tag, otspec->langsys_tag,
849 otspec->features[0],
850 otspec->nfeatures[0]) != 1
851 || OTF_check_features (otf, 0,
852 otspec->script_tag, otspec->langsys_tag,
853 otspec->features[1],
854 otspec->nfeatures[1]) != 1)
855 continue;
857 #endif /* HAVE_LIBOTF */
858 entity = ftfont_pattern_entity (fontset->fonts[i],
859 AREF (spec, FONT_EXTRA_INDEX));
860 if (! NILP (entity))
861 val = Fcons (entity, val);
863 font_add_log ("ftfont-list", spec, val);
864 goto finish;
866 err:
867 /* We come here because of unexpected error in fontconfig API call
868 (usually insufficient memory). */
869 val = Qnil;
871 finish:
872 if (objset) FcObjectSetDestroy (objset);
873 if (fontset) FcFontSetDestroy (fontset);
874 if (pattern) FcPatternDestroy (pattern);
875 return val;
878 static Lisp_Object
879 ftfont_match (frame, spec)
880 Lisp_Object frame, spec;
882 Lisp_Object entity;
883 FcPattern *pattern, *match = NULL;
884 FcResult result;
885 char otlayout[15]; /* For "otlayout:XXXX" */
886 struct OpenTypeSpec *otspec = NULL;
888 if (! fc_initialized)
890 FcInit ();
891 fc_initialized = 1;
894 pattern = ftfont_spec_pattern (spec, otlayout, &otspec);
895 if (! pattern)
896 return Qnil;
898 if (INTEGERP (AREF (spec, FONT_SIZE_INDEX)))
900 FcValue value;
902 value.type = FcTypeDouble;
903 value.u.d = XINT (AREF (spec, FONT_SIZE_INDEX));
904 FcPatternAdd (pattern, FC_PIXEL_SIZE, value, FcFalse);
906 if (FcConfigSubstitute (NULL, pattern, FcMatchPattern) == FcTrue)
908 FcDefaultSubstitute (pattern);
909 match = FcFontMatch (NULL, pattern, &result);
910 if (match)
912 entity = ftfont_pattern_entity (match, AREF (spec, FONT_EXTRA_INDEX));
913 FcPatternDestroy (match);
914 if (! NILP (AREF (spec, FONT_FAMILY_INDEX))
915 && NILP (assq_no_quit (AREF (spec, FONT_FAMILY_INDEX),
916 ftfont_generic_family_list))
917 && NILP (Fstring_equal (AREF (spec, FONT_FAMILY_INDEX),
918 AREF (entity, FONT_FAMILY_INDEX))))
919 entity = Qnil;
922 FcPatternDestroy (pattern);
924 font_add_log ("ftfont-match", spec, entity);
925 return entity;
928 static Lisp_Object
929 ftfont_list_family (frame)
930 Lisp_Object frame;
932 Lisp_Object list;
933 FcPattern *pattern = NULL;
934 FcFontSet *fontset = NULL;
935 FcObjectSet *objset = NULL;
936 int i;
938 if (! fc_initialized)
940 FcInit ();
941 fc_initialized = 1;
944 pattern = FcPatternCreate ();
945 if (! pattern)
946 goto finish;
947 objset = FcObjectSetBuild (FC_FAMILY, NULL);
948 if (! objset)
949 goto finish;
950 fontset = FcFontList (NULL, pattern, objset);
951 if (! fontset)
952 goto finish;
954 list = Qnil;
955 for (i = 0; i < fontset->nfont; i++)
957 FcPattern *pat = fontset->fonts[i];
958 FcChar8 *str;
960 if (FcPatternGetString (pat, FC_FAMILY, 0, &str) == FcResultMatch)
961 list = Fcons (intern ((char *) str), list);
964 finish:
965 if (objset) FcObjectSetDestroy (objset);
966 if (fontset) FcFontSetDestroy (fontset);
967 if (pattern) FcPatternDestroy (pattern);
969 return list;
973 static Lisp_Object
974 ftfont_open (f, entity, pixel_size)
975 FRAME_PTR f;
976 Lisp_Object entity;
977 int pixel_size;
979 struct ftfont_info *ftfont_info;
980 struct font *font;
981 struct ftfont_cache_data *cache_data;
982 FT_Face ft_face;
983 FT_Size ft_size;
984 FT_UInt size;
985 Lisp_Object val, filename, index, cache, font_object;
986 int scalable;
987 int spacing;
988 char name[256];
989 int i, len;
990 int upEM;
992 val = assq_no_quit (QCfont_entity, AREF (entity, FONT_EXTRA_INDEX));
993 if (! CONSP (val))
994 return Qnil;
995 val = XCDR (val);
996 cache = ftfont_lookup_cache (val, 1);
997 if (NILP (cache))
998 return Qnil;
999 filename = XCAR (val);
1000 index = XCDR (val);
1001 val = XCDR (cache);
1002 cache_data = XSAVE_VALUE (XCDR (cache))->pointer;
1003 ft_face = cache_data->ft_face;
1004 if (XSAVE_VALUE (val)->integer > 0)
1006 /* FT_Face in this cache is already used by the different size. */
1007 if (FT_New_Size (ft_face, &ft_size) != 0)
1008 return Qnil;
1009 if (FT_Activate_Size (ft_size) != 0)
1011 FT_Done_Size (ft_size);
1012 return Qnil;
1015 XSAVE_VALUE (val)->integer++;
1016 size = XINT (AREF (entity, FONT_SIZE_INDEX));
1017 if (size == 0)
1018 size = pixel_size;
1019 if (FT_Set_Pixel_Sizes (ft_face, size, size) != 0)
1021 if (XSAVE_VALUE (val)->integer == 0)
1022 FT_Done_Face (ft_face);
1023 return Qnil;
1026 font_object = font_make_object (VECSIZE (struct ftfont_info), entity, size);
1027 ASET (font_object, FONT_TYPE_INDEX, Qfreetype);
1028 len = font_unparse_xlfd (entity, size, name, 256);
1029 if (len > 0)
1030 ASET (font_object, FONT_NAME_INDEX, make_unibyte_string (name, len));
1031 len = font_unparse_fcname (entity, size, name, 256);
1032 if (len > 0)
1033 ASET (font_object, FONT_FULLNAME_INDEX, make_unibyte_string (name, len));
1034 else
1035 ASET (font_object, FONT_FULLNAME_INDEX,
1036 AREF (font_object, FONT_NAME_INDEX));
1037 ASET (font_object, FONT_FILE_INDEX, filename);
1038 ASET (font_object, FONT_FORMAT_INDEX, ftfont_font_format (NULL, filename));
1039 font = XFONT_OBJECT (font_object);
1040 ftfont_info = (struct ftfont_info *) font;
1041 ftfont_info->ft_size = ft_face->size;
1042 ftfont_info->index = XINT (index);
1043 #ifdef HAVE_LIBOTF
1044 ftfont_info->maybe_otf = ft_face->face_flags & FT_FACE_FLAG_SFNT;
1045 ftfont_info->otf = NULL;
1046 #endif /* HAVE_LIBOTF */
1047 font->pixel_size = size;
1048 font->driver = &ftfont_driver;
1049 font->encoding_charset = font->repertory_charset = -1;
1051 upEM = ft_face->units_per_EM;
1052 scalable = (INTEGERP (AREF (entity, FONT_AVGWIDTH_INDEX))
1053 && XINT (AREF (entity, FONT_AVGWIDTH_INDEX)) == 0);
1054 if (scalable)
1056 font->ascent = ft_face->ascender * size / upEM;
1057 font->descent = - ft_face->descender * size / upEM;
1058 font->height = ft_face->height * size / upEM;
1060 else
1062 font->ascent = ft_face->size->metrics.ascender >> 6;
1063 font->descent = - ft_face->size->metrics.descender >> 6;
1064 font->height = ft_face->size->metrics.height >> 6;
1066 if (INTEGERP (AREF (entity, FONT_SPACING_INDEX)))
1067 spacing = XINT (AREF (entity, FONT_SPACING_INDEX));
1068 else
1069 spacing = FC_PROPORTIONAL;
1070 if (spacing != FC_PROPORTIONAL)
1071 font->min_width = font->average_width = font->space_width
1072 = (scalable ? ft_face->max_advance_width * size / upEM
1073 : ft_face->size->metrics.max_advance >> 6);
1074 else
1076 int n;
1078 font->min_width = font->average_width = font->space_width = 0;
1079 for (i = 32, n = 0; i < 127; i++)
1080 if (FT_Load_Char (ft_face, i, FT_LOAD_DEFAULT) != 0)
1082 int this_width = ft_face->glyph->metrics.horiAdvance >> 6;
1084 if (this_width > 0
1085 && (! font->min_width || font->min_width > this_width))
1086 font->min_width = this_width;
1087 if (i == 32)
1088 font->space_width = this_width;
1089 font->average_width += this_width;
1090 n++;
1092 if (n > 0)
1093 font->average_width /= n;
1096 font->baseline_offset = 0;
1097 font->relative_compose = 0;
1098 font->default_ascent = 0;
1099 font->vertical_centering = 0;
1100 if (scalable)
1102 font->underline_position = -ft_face->underline_position * size / upEM;
1103 font->underline_thickness = -ft_face->underline_thickness * size / upEM;
1105 else
1107 font->underline_position = -1;
1108 font->underline_thickness = 0;
1111 return font_object;
1114 static void
1115 ftfont_close (f, font)
1116 FRAME_PTR f;
1117 struct font *font;
1119 struct ftfont_info *ftfont_info = (struct ftfont_info *) font;
1120 Lisp_Object val, cache;
1122 val = Fcons (font->props[FONT_FILE_INDEX], make_number (ftfont_info->index));
1123 cache = ftfont_lookup_cache (val, 1);
1124 xassert (CONSP (cache));
1125 val = XCDR (cache);
1126 (XSAVE_VALUE (val)->integer)--;
1127 if (XSAVE_VALUE (val)->integer == 0)
1129 struct ftfont_cache_data *cache_data = XSAVE_VALUE (val)->pointer;
1131 FT_Done_Face (cache_data->ft_face);
1132 #ifdef HAVE_LIBOTF
1133 if (ftfont_info->otf)
1134 OTF_close (ftfont_info->otf);
1135 #endif
1136 cache_data->ft_face = NULL;
1138 else
1139 FT_Done_Size (ftfont_info->ft_size);
1142 static int
1143 ftfont_has_char (entity, c)
1144 Lisp_Object entity;
1145 int c;
1147 FcCharSet *charset = ftfont_get_fc_charset (entity);
1149 return (FcCharSetHasChar (charset, c) == FcTrue);
1152 static unsigned
1153 ftfont_encode_char (font, c)
1154 struct font *font;
1155 int c;
1157 struct ftfont_info *ftfont_info = (struct ftfont_info *) font;
1158 FT_Face ft_face = ftfont_info->ft_size->face;
1159 FT_ULong charcode = c;
1160 FT_UInt code = FT_Get_Char_Index (ft_face, charcode);
1162 return (code > 0 ? code : FONT_INVALID_CODE);
1165 static int
1166 ftfont_text_extents (font, code, nglyphs, metrics)
1167 struct font *font;
1168 unsigned *code;
1169 int nglyphs;
1170 struct font_metrics *metrics;
1172 struct ftfont_info *ftfont_info = (struct ftfont_info *) font;
1173 FT_Face ft_face = ftfont_info->ft_size->face;
1174 int width = 0;
1175 int i, first;
1177 if (ftfont_info->ft_size != ft_face->size)
1178 FT_Activate_Size (ftfont_info->ft_size);
1179 if (metrics)
1180 bzero (metrics, sizeof (struct font_metrics));
1181 for (i = 0, first = 1; i < nglyphs; i++)
1183 if (FT_Load_Glyph (ft_face, code[i], FT_LOAD_DEFAULT) == 0)
1185 FT_Glyph_Metrics *m = &ft_face->glyph->metrics;
1187 if (first)
1189 if (metrics)
1191 metrics->lbearing = m->horiBearingX >> 6;
1192 metrics->rbearing = (m->horiBearingX + m->width) >> 6;
1193 metrics->ascent = m->horiBearingY >> 6;
1194 metrics->descent = (m->horiBearingY + m->height) >> 6;
1196 first = 0;
1198 if (metrics)
1200 if (metrics->lbearing > width + (m->horiBearingX >> 6))
1201 metrics->lbearing = width + (m->horiBearingX >> 6);
1202 if (metrics->rbearing
1203 < width + ((m->horiBearingX + m->width) >> 6))
1204 metrics->rbearing
1205 = width + ((m->horiBearingX + m->width) >> 6);
1206 if (metrics->ascent < (m->horiBearingY >> 6))
1207 metrics->ascent = m->horiBearingY >> 6;
1208 if (metrics->descent > ((m->horiBearingY + m->height) >> 6))
1209 metrics->descent = (m->horiBearingY + m->height) >> 6;
1211 width += m->horiAdvance >> 6;
1213 else
1215 width += font->space_width;
1218 if (metrics)
1219 metrics->width = width;
1221 return width;
1224 static int
1225 ftfont_get_bitmap (font, code, bitmap, bits_per_pixel)
1226 struct font *font;
1227 unsigned code;
1228 struct font_bitmap *bitmap;
1229 int bits_per_pixel;
1231 struct ftfont_info *ftfont_info = (struct ftfont_info *) font;
1232 FT_Face ft_face = ftfont_info->ft_size->face;
1233 FT_Int32 load_flags = FT_LOAD_RENDER;
1235 if (ftfont_info->ft_size != ft_face->size)
1236 FT_Activate_Size (ftfont_info->ft_size);
1237 if (bits_per_pixel == 1)
1239 #ifdef FT_LOAD_TARGET_MONO
1240 load_flags |= FT_LOAD_TARGET_MONO;
1241 #else
1242 load_flags |= FT_LOAD_MONOCHROME;
1243 #endif
1245 else if (bits_per_pixel != 8)
1246 /* We don't support such a rendering. */
1247 return -1;
1249 if (FT_Load_Glyph (ft_face, code, load_flags) != 0)
1250 return -1;
1251 bitmap->bits_per_pixel
1252 = (ft_face->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_MONO ? 1
1253 : ft_face->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_GRAY ? 8
1254 : ft_face->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_LCD ? 8
1255 : ft_face->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_LCD_V ? 8
1256 : -1);
1257 if (bitmap->bits_per_pixel < 0)
1258 /* We don't suport that kind of pixel mode. */
1259 return -1;
1260 bitmap->rows = ft_face->glyph->bitmap.rows;
1261 bitmap->width = ft_face->glyph->bitmap.width;
1262 bitmap->pitch = ft_face->glyph->bitmap.pitch;
1263 bitmap->buffer = ft_face->glyph->bitmap.buffer;
1264 bitmap->left = ft_face->glyph->bitmap_left;
1265 bitmap->top = ft_face->glyph->bitmap_top;
1266 bitmap->advance = ft_face->glyph->metrics.horiAdvance >> 6;
1267 bitmap->extra = NULL;
1269 return 0;
1272 static int
1273 ftfont_anchor_point (font, code, index, x, y)
1274 struct font *font;
1275 unsigned code;
1276 int index;
1277 int *x, *y;
1279 struct ftfont_info *ftfont_info = (struct ftfont_info *) font;
1280 FT_Face ft_face = ftfont_info->ft_size->face;
1282 if (ftfont_info->ft_size != ft_face->size)
1283 FT_Activate_Size (ftfont_info->ft_size);
1284 if (FT_Load_Glyph (ft_face, code, FT_LOAD_DEFAULT) != 0)
1285 return -1;
1286 if (ft_face->glyph->format != FT_GLYPH_FORMAT_OUTLINE)
1287 return -1;
1288 if (index >= ft_face->glyph->outline.n_points)
1289 return -1;
1290 *x = ft_face->glyph->outline.points[index].x;
1291 *y = ft_face->glyph->outline.points[index].y;
1292 return 0;
1295 #ifdef HAVE_LIBOTF
1297 static Lisp_Object
1298 ftfont_otf_features (gsub_gpos)
1299 OTF_GSUB_GPOS *gsub_gpos;
1301 Lisp_Object scripts, langsyses, features, sym;
1302 int i, j, k;
1304 for (scripts = Qnil, i = gsub_gpos->ScriptList.ScriptCount - 1; i >= 0; i--)
1306 OTF_Script *otf_script = gsub_gpos->ScriptList.Script + i;
1308 for (langsyses = Qnil, j = otf_script->LangSysCount - 1; j >= -1; j--)
1310 OTF_LangSys *otf_langsys;
1312 if (j >= 0)
1313 otf_langsys = otf_script->LangSys + j;
1314 else if (otf_script->DefaultLangSysOffset)
1315 otf_langsys = &otf_script->DefaultLangSys;
1316 else
1317 break;
1319 for (features = Qnil, k = otf_langsys->FeatureCount - 1; k >= 0; k--)
1321 OTF_TAG_SYM (sym, gsub_gpos->FeatureList.Feature[k].FeatureTag);
1322 features = Fcons (sym, features);
1324 if (j >= 0)
1325 OTF_TAG_SYM (sym, otf_script->LangSysRecord[j].LangSysTag);
1326 else
1327 sym = Qnil;
1328 langsyses = Fcons (Fcons (sym, features), langsyses);
1331 OTF_TAG_SYM (sym, gsub_gpos->ScriptList.Script[i].ScriptTag);
1332 scripts = Fcons (Fcons (sym, langsyses), scripts);
1334 return scripts;
1339 static Lisp_Object
1340 ftfont_otf_capability (font)
1341 struct font *font;
1343 struct ftfont_info *ftfont_info = (struct ftfont_info *) font;
1344 OTF *otf = ftfont_get_otf (ftfont_info);
1345 Lisp_Object gsub_gpos;
1347 if (! otf)
1348 return Qnil;
1349 gsub_gpos = Fcons (Qnil, Qnil);
1350 if (OTF_get_table (otf, "GSUB") == 0)
1351 XSETCAR (gsub_gpos, ftfont_otf_features (otf->gsub));
1352 if (OTF_get_table (otf, "GPOS") == 0)
1353 XSETCDR (gsub_gpos, ftfont_otf_features (otf->gpos));
1354 return gsub_gpos;
1357 #ifdef HAVE_M17N_FLT
1359 struct MFLTFontFT
1361 MFLTFont flt_font;
1362 struct font *font;
1363 FT_Face ft_face;
1364 OTF *otf;
1367 static int
1368 ftfont_get_glyph_id (font, gstring, from, to)
1369 MFLTFont *font;
1370 MFLTGlyphString *gstring;
1371 int from, to;
1373 struct MFLTFontFT *flt_font_ft = (struct MFLTFontFT *) font;
1374 FT_Face ft_face = flt_font_ft->ft_face;
1375 MFLTGlyph *g;
1377 for (g = gstring->glyphs + from; from < to; g++, from++)
1378 if (! g->encoded)
1380 FT_UInt code = FT_Get_Char_Index (ft_face, g->code);
1382 g->code = code > 0 ? code : FONT_INVALID_CODE;
1383 g->encoded = 1;
1385 return 0;
1388 static int
1389 ftfont_get_metrics (font, gstring, from, to)
1390 MFLTFont *font;
1391 MFLTGlyphString *gstring;
1392 int from, to;
1394 struct MFLTFontFT *flt_font_ft = (struct MFLTFontFT *) font;
1395 FT_Face ft_face = flt_font_ft->ft_face;
1396 MFLTGlyph *g;
1398 for (g = gstring->glyphs + from; from < to; g++, from++)
1399 if (! g->measured)
1401 if (g->code != FONT_INVALID_CODE)
1403 FT_Glyph_Metrics *m;
1405 if (FT_Load_Glyph (ft_face, g->code, FT_LOAD_DEFAULT) != 0)
1406 abort ();
1407 m = &ft_face->glyph->metrics;
1409 g->lbearing = m->horiBearingX;
1410 g->rbearing = m->horiBearingX + m->width;
1411 g->ascent = m->horiBearingY;
1412 g->descent = m->height - m->horiBearingY;
1413 g->xadv = m->horiAdvance;
1415 else
1417 g->lbearing = 0;
1418 g->rbearing = g->xadv = flt_font_ft->font->space_width << 6;
1419 g->ascent = flt_font_ft->font->ascent << 6;
1420 g->descent = flt_font_ft->font->descent << 6;
1422 g->yadv = 0;
1423 g->measured = 1;
1425 return 0;
1428 static int
1429 ftfont_check_otf (MFLTFont *font, MFLTOtfSpec *spec)
1431 struct MFLTFontFT *flt_font_ft = (struct MFLTFontFT *) font;
1432 OTF *otf = flt_font_ft->otf;
1433 OTF_Tag *tags;
1434 int i, n, negative;
1436 for (i = 0; i < 2; i++)
1438 if (! spec->features[i])
1439 continue;
1440 for (n = 0; spec->features[i][n]; n++);
1441 tags = alloca (sizeof (OTF_Tag) * n);
1442 for (n = 0, negative = 0; spec->features[i][n]; n++)
1444 if (spec->features[i][n] == 0xFFFFFFFF)
1445 negative = 1;
1446 else if (negative)
1447 tags[n - 1] = spec->features[i][n] | 0x80000000;
1448 else
1449 tags[n] = spec->features[i][n];
1451 if (n - negative > 0
1452 && OTF_check_features (otf, i == 0, spec->script, spec->langsys,
1453 tags, n - negative) != 1)
1454 return 0;
1456 return 1;
1459 #define DEVICE_DELTA(table, size) \
1460 (((size) >= (table).StartSize && (size) <= (table).EndSize) \
1461 ? (table).DeltaValue[(size) - (table).StartSize] << 6 \
1462 : 0)
1464 static void
1465 adjust_anchor (FT_Face ft_face, OTF_Anchor *anchor,
1466 unsigned code, int x_ppem, int y_ppem, int *x, int *y)
1468 if (anchor->AnchorFormat == 2)
1470 FT_Outline *outline;
1471 int ap = anchor->f.f1.AnchorPoint;
1473 FT_Load_Glyph (ft_face, (FT_UInt) code, FT_LOAD_MONOCHROME);
1474 outline = &ft_face->glyph->outline;
1475 if (ap < outline->n_points)
1477 *x = outline->points[ap].x << 6;
1478 *y = outline->points[ap].y << 6;
1481 else if (anchor->AnchorFormat == 3)
1483 if (anchor->f.f2.XDeviceTable.offset)
1484 *x += DEVICE_DELTA (anchor->f.f2.XDeviceTable, x_ppem);
1485 if (anchor->f.f2.YDeviceTable.offset)
1486 *y += DEVICE_DELTA (anchor->f.f2.YDeviceTable, y_ppem);
1490 static OTF_GlyphString otf_gstring;
1492 static int
1493 ftfont_drive_otf (font, spec, in, from, to, out, adjustment)
1494 MFLTFont *font;
1495 MFLTOtfSpec *spec;
1496 MFLTGlyphString *in;
1497 int from, to;
1498 MFLTGlyphString *out;
1499 MFLTGlyphAdjustment *adjustment;
1501 struct MFLTFontFT *flt_font_ft = (struct MFLTFontFT *) font;
1502 FT_Face ft_face = flt_font_ft->ft_face;
1503 OTF *otf = flt_font_ft->otf;
1504 int len = to - from;
1505 int i, j, gidx;
1506 OTF_Glyph *otfg;
1507 char script[5], *langsys = NULL;
1508 char *gsub_features = NULL, *gpos_features = NULL;
1510 if (len == 0)
1511 return from;
1512 OTF_tag_name (spec->script, script);
1513 if (spec->langsys)
1515 langsys = alloca (5);
1516 OTF_tag_name (spec->langsys, langsys);
1518 for (i = 0; i < 2; i++)
1520 char *p;
1522 if (spec->features[i] && spec->features[i][1] != 0xFFFFFFFF)
1524 for (j = 0; spec->features[i][j]; j++);
1525 if (i == 0)
1526 p = gsub_features = alloca (6 * j);
1527 else
1528 p = gpos_features = alloca (6 * j);
1529 for (j = 0; spec->features[i][j]; j++)
1531 if (spec->features[i][j] == 0xFFFFFFFF)
1532 *p++ = '*', *p++ = ',';
1533 else
1535 OTF_tag_name (spec->features[i][j], p);
1536 p[4] = ',';
1537 p += 5;
1540 *--p = '\0';
1544 if (otf_gstring.size == 0)
1546 otf_gstring.glyphs = (OTF_Glyph *) malloc (sizeof (OTF_Glyph) * len);
1547 otf_gstring.size = len;
1549 else if (otf_gstring.size < len)
1551 otf_gstring.glyphs = (OTF_Glyph *) realloc (otf_gstring.glyphs,
1552 sizeof (OTF_Glyph) * len);
1553 otf_gstring.size = len;
1555 otf_gstring.used = len;
1556 memset (otf_gstring.glyphs, 0, sizeof (OTF_Glyph) * len);
1557 for (i = 0; i < len; i++)
1559 otf_gstring.glyphs[i].c = in->glyphs[from + i].c;
1560 otf_gstring.glyphs[i].glyph_id = in->glyphs[from + i].code;
1563 OTF_drive_gdef (otf, &otf_gstring);
1564 gidx = out->used;
1566 if (gsub_features)
1568 if (OTF_drive_gsub (otf, &otf_gstring, script, langsys, gsub_features)
1569 < 0)
1570 goto simple_copy;
1571 if (out->allocated < out->used + otf_gstring.used)
1572 return -2;
1573 for (i = 0, otfg = otf_gstring.glyphs; i < otf_gstring.used; )
1575 MFLTGlyph *g;
1576 int min_from, max_to;
1577 int j;
1579 g = out->glyphs + out->used;
1580 *g = in->glyphs[from + otfg->f.index.from];
1581 if (g->code != otfg->glyph_id)
1583 g->c = 0;
1584 g->code = otfg->glyph_id;
1585 g->measured = 0;
1587 out->used++;
1588 min_from = g->from;
1589 max_to = g->to;
1590 if (otfg->f.index.from < otfg->f.index.to)
1592 /* OTFG substitutes multiple glyphs in IN. */
1593 for (j = from + otfg->f.index.from + 1;
1594 j <= from + otfg->f.index.to; j++)
1596 if (min_from > in->glyphs[j].from)
1597 min_from = in->glyphs[j].from;
1598 if (max_to < in->glyphs[j].to)
1599 max_to = in->glyphs[j].to;
1601 g->from = min_from;
1602 g->to = max_to;
1604 for (i++, otfg++; (i < otf_gstring.used
1605 && otfg->f.index.from == otfg[-1].f.index.from);
1606 i++, otfg++)
1608 g = out->glyphs + out->used;
1609 *g = in->glyphs[from + otfg->f.index.to];
1610 if (g->code != otfg->glyph_id)
1612 g->c = 0;
1613 g->code = otfg->glyph_id;
1614 g->measured = 0;
1616 out->used++;
1620 else
1622 if (out->allocated < out->used + len)
1623 return -2;
1624 for (i = 0; i < len; i++)
1625 out->glyphs[out->used++] = in->glyphs[from + i];
1628 if (gpos_features)
1630 MFLTGlyph *base = NULL, *mark = NULL, *g;
1631 int x_ppem, y_ppem, x_scale, y_scale;
1633 if (OTF_drive_gpos (otf, &otf_gstring, script, langsys, gpos_features)
1634 < 0)
1635 return to;
1637 x_ppem = ft_face->size->metrics.x_ppem;
1638 y_ppem = ft_face->size->metrics.y_ppem;
1639 x_scale = ft_face->size->metrics.x_scale;
1640 y_scale = ft_face->size->metrics.y_scale;
1642 for (i = 0, otfg = otf_gstring.glyphs, g = out->glyphs + gidx;
1643 i < otf_gstring.used; i++, otfg++, g++)
1645 MFLTGlyph *prev;
1647 if (! otfg->glyph_id)
1648 continue;
1649 switch (otfg->positioning_type)
1651 case 0:
1652 break;
1653 case 1: /* Single */
1654 case 2: /* Pair */
1656 int format = otfg->f.f1.format;
1658 if (format & OTF_XPlacement)
1659 adjustment[i].xoff
1660 = otfg->f.f1.value->XPlacement * x_scale / 0x10000;
1661 if (format & OTF_XPlaDevice)
1662 adjustment[i].xoff
1663 += DEVICE_DELTA (otfg->f.f1.value->XPlaDevice, x_ppem);
1664 if (format & OTF_YPlacement)
1665 adjustment[i].yoff
1666 = - (otfg->f.f1.value->YPlacement * y_scale / 0x10000);
1667 if (format & OTF_YPlaDevice)
1668 adjustment[i].yoff
1669 -= DEVICE_DELTA (otfg->f.f1.value->YPlaDevice, y_ppem);
1670 if (format & OTF_XAdvance)
1671 adjustment[i].xadv
1672 += otfg->f.f1.value->XAdvance * x_scale / 0x10000;
1673 if (format & OTF_XAdvDevice)
1674 adjustment[i].xadv
1675 += DEVICE_DELTA (otfg->f.f1.value->XAdvDevice, x_ppem);
1676 if (format & OTF_YAdvance)
1677 adjustment[i].yadv
1678 += otfg->f.f1.value->YAdvance * y_scale / 0x10000;
1679 if (format & OTF_YAdvDevice)
1680 adjustment[i].yadv
1681 += DEVICE_DELTA (otfg->f.f1.value->YAdvDevice, y_ppem);
1682 adjustment[i].set = 1;
1684 break;
1685 case 3: /* Cursive */
1686 /* Not yet supported. */
1687 break;
1688 case 4: /* Mark-to-Base */
1689 case 5: /* Mark-to-Ligature */
1690 if (! base)
1691 break;
1692 prev = base;
1693 goto label_adjust_anchor;
1694 default: /* i.e. case 6 Mark-to-Mark */
1695 if (! mark)
1696 break;
1697 prev = mark;
1699 label_adjust_anchor:
1701 int base_x, base_y, mark_x, mark_y;
1702 int this_from, this_to;
1704 base_x = otfg->f.f4.base_anchor->XCoordinate * x_scale / 0x10000;
1705 base_y = otfg->f.f4.base_anchor->YCoordinate * y_scale / 0x10000;
1706 mark_x = otfg->f.f4.mark_anchor->XCoordinate * x_scale / 0x10000;
1707 mark_y = otfg->f.f4.mark_anchor->YCoordinate * y_scale / 0x10000;;
1709 if (otfg->f.f4.base_anchor->AnchorFormat != 1)
1710 adjust_anchor (ft_face, otfg->f.f4.base_anchor,
1711 prev->code, x_ppem, y_ppem, &base_x, &base_y);
1712 if (otfg->f.f4.mark_anchor->AnchorFormat != 1)
1713 adjust_anchor (ft_face, otfg->f.f4.mark_anchor, g->code,
1714 x_ppem, y_ppem, &mark_x, &mark_y);
1715 adjustment[i].xoff = (base_x - mark_x);
1716 adjustment[i].yoff = - (base_y - mark_y);
1717 adjustment[i].back = (g - prev);
1718 adjustment[i].xadv = 0;
1719 adjustment[i].advance_is_absolute = 1;
1720 adjustment[i].set = 1;
1721 this_from = g->from;
1722 this_to = g->to;
1723 for (j = 0; prev + j < g; j++)
1725 if (this_from > prev[j].from)
1726 this_from = prev[j].from;
1727 if (this_to < prev[j].to)
1728 this_to = prev[j].to;
1730 for (; prev <= g; prev++)
1732 prev->from = this_from;
1733 prev->to = this_to;
1737 if (otfg->GlyphClass == OTF_GlyphClass0)
1738 base = mark = g;
1739 else if (otfg->GlyphClass == OTF_GlyphClassMark)
1740 mark = g;
1741 else
1742 base = g;
1745 return to;
1747 simple_copy:
1748 if (out->allocated < out->used + len)
1749 return -2;
1750 font->get_metrics (font, in, from, to);
1751 memcpy (out->glyphs + out->used, in->glyphs + from,
1752 sizeof (MFLTGlyph) * len);
1753 out->used += len;
1754 return to;
1757 static MFLTGlyphString gstring;
1759 static int m17n_flt_initialized;
1761 extern Lisp_Object QCfamily;
1763 Lisp_Object
1764 ftfont_shape_by_flt (lgstring, font, ft_face, otf)
1765 Lisp_Object lgstring;
1766 struct font *font;
1767 FT_Face ft_face;
1768 OTF *otf;
1770 EMACS_UINT len = LGSTRING_LENGTH (lgstring);
1771 EMACS_UINT i;
1772 struct MFLTFontFT flt_font_ft;
1774 if (! m17n_flt_initialized)
1776 M17N_INIT ();
1777 m17n_flt_initialized = 1;
1780 for (i = 0; i < len; i++)
1781 if (NILP (LGSTRING_GLYPH (lgstring, i)))
1782 break;
1783 len = i;
1785 if (gstring.allocated == 0)
1787 gstring.allocated = len * 2;
1788 gstring.glyph_size = sizeof (MFLTGlyph);
1789 gstring.glyphs = malloc (sizeof (MFLTGlyph) * gstring.allocated);
1791 else if (gstring.allocated < len * 2)
1793 gstring.allocated = len * 2;
1794 gstring.glyphs = realloc (gstring.glyphs,
1795 sizeof (MFLTGlyph) * gstring.allocated);
1797 for (i = 0; i < len; i++)
1798 gstring.glyphs[i].c = LGLYPH_CHAR (LGSTRING_GLYPH (lgstring, i));
1799 gstring.used = len;
1800 gstring.r2l = 0;
1803 Lisp_Object family = Ffont_get (LGSTRING_FONT (lgstring), QCfamily);
1805 if (NILP (family))
1806 flt_font_ft.flt_font.family = Mnil;
1807 else
1808 flt_font_ft.flt_font.family
1809 = msymbol ((char *) SDATA (SYMBOL_NAME (family)));
1811 flt_font_ft.flt_font.x_ppem = ft_face->size->metrics.x_ppem;
1812 flt_font_ft.flt_font.y_ppem = ft_face->size->metrics.y_ppem;
1813 flt_font_ft.flt_font.get_glyph_id = ftfont_get_glyph_id;
1814 flt_font_ft.flt_font.get_metrics = ftfont_get_metrics;
1815 flt_font_ft.flt_font.check_otf = ftfont_check_otf;
1816 flt_font_ft.flt_font.drive_otf = ftfont_drive_otf;
1817 flt_font_ft.flt_font.internal = NULL;
1818 flt_font_ft.font = font;
1819 flt_font_ft.ft_face = ft_face;
1820 flt_font_ft.otf = otf;
1821 for (i = 0; i < 3; i++)
1823 int result = mflt_run (&gstring, 0, len, &flt_font_ft.flt_font, NULL);
1824 if (result != -2)
1825 break;
1826 gstring.allocated += gstring.allocated;
1827 gstring.glyphs = realloc (gstring.glyphs,
1828 sizeof (MFLTGlyph) * gstring.allocated);
1830 if (gstring.used > LGSTRING_LENGTH (lgstring))
1831 return Qnil;
1832 for (i = 0; i < gstring.used; i++)
1834 MFLTGlyph *g = gstring.glyphs + i;
1836 g->from = LGLYPH_FROM (LGSTRING_GLYPH (lgstring, g->from));
1837 g->to = LGLYPH_TO (LGSTRING_GLYPH (lgstring, g->to));
1840 for (i = 0; i < gstring.used; i++)
1842 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
1843 MFLTGlyph *g = gstring.glyphs + i;
1845 if (NILP (lglyph))
1847 lglyph = Fmake_vector (make_number (LGLYPH_SIZE), Qnil);
1848 LGSTRING_SET_GLYPH (lgstring, i, lglyph);
1850 LGLYPH_SET_FROM (lglyph, g->from);
1851 LGLYPH_SET_TO (lglyph, g->to);
1852 LGLYPH_SET_CHAR (lglyph, g->c);
1853 LGLYPH_SET_CODE (lglyph, g->code);
1854 LGLYPH_SET_WIDTH (lglyph, g->xadv >> 6);
1855 LGLYPH_SET_LBEARING (lglyph, g->lbearing >> 6);
1856 LGLYPH_SET_RBEARING (lglyph, g->rbearing >> 6);
1857 LGLYPH_SET_ASCENT (lglyph, g->ascent >> 6);
1858 LGLYPH_SET_DESCENT (lglyph, g->descent >> 6);
1859 if (g->adjusted)
1861 Lisp_Object vec;
1863 vec = Fmake_vector (make_number (3), Qnil);
1864 ASET (vec, 0, make_number (g->xoff >> 6));
1865 ASET (vec, 1, make_number (g->yoff >> 6));
1866 ASET (vec, 2, make_number (g->xadv >> 6));
1867 LGLYPH_SET_ADJUSTMENT (lglyph, vec);
1870 return make_number (i);
1873 Lisp_Object
1874 ftfont_shape (lgstring)
1875 Lisp_Object lgstring;
1877 struct font *font;
1878 struct ftfont_info *ftfont_info;
1879 OTF *otf;
1881 CHECK_FONT_GET_OBJECT (LGSTRING_FONT (lgstring), font);
1882 ftfont_info = (struct ftfont_info *) font;
1883 otf = ftfont_get_otf (ftfont_info);
1884 if (! otf)
1885 return make_number (0);
1886 return ftfont_shape_by_flt (lgstring, font, ftfont_info->ft_size->face, otf);
1889 #endif /* HAVE_M17N_FLT */
1890 #endif /* HAVE_LIBOTF */
1892 Lisp_Object
1893 ftfont_font_format (FcPattern *pattern, Lisp_Object filename)
1895 FcChar8 *str;
1897 #ifdef FC_FONTFORMAT
1898 if (pattern)
1900 if (FcPatternGetString (pattern, FC_FONTFORMAT, 0, &str) != FcResultMatch)
1901 return Qnil;
1902 if (strcmp ((char *) str, "TrueType") == 0)
1903 return intern ("truetype");
1904 if (strcmp ((char *) str, "Type 1") == 0)
1905 return intern ("type1");
1906 if (strcmp ((char *) str, "PCF") == 0)
1907 return intern ("pcf");
1908 if (strcmp ((char *) str, "BDF") == 0)
1909 return intern ("bdf");
1911 #endif /* FC_FONTFORMAT */
1912 if (STRINGP (filename))
1914 int len = SBYTES (filename);
1916 if (len >= 4)
1918 str = (FcChar8 *) (SDATA (filename) + len - 4);
1919 if (xstrcasecmp ((char *) str, ".ttf") == 0)
1920 return intern ("truetype");
1921 if (xstrcasecmp ((char *) str, ".pfb") == 0)
1922 return intern ("type1");
1923 if (xstrcasecmp ((char *) str, ".pcf") == 0)
1924 return intern ("pcf");
1925 if (xstrcasecmp ((char *) str, ".bdf") == 0)
1926 return intern ("bdf");
1929 return intern ("unknown");
1933 void
1934 syms_of_ftfont ()
1936 DEFSYM (Qfreetype, "freetype");
1937 DEFSYM (Qmonospace, "monospace");
1938 DEFSYM (Qsans_serif, "sans-serif");
1939 DEFSYM (Qserif, "serif");
1940 DEFSYM (Qmono, "mono");
1941 DEFSYM (Qsans, "sans");
1942 DEFSYM (Qsans__serif, "sans serif");
1944 staticpro (&freetype_font_cache);
1945 freetype_font_cache = Fcons (Qt, Qnil);
1947 staticpro (&ftfont_generic_family_list);
1948 ftfont_generic_family_list
1949 = Fcons (Fcons (Qmonospace, Qt),
1950 Fcons (Fcons (Qsans_serif, Qt),
1951 Fcons (Fcons (Qsans, Qt), Qnil)));
1953 staticpro (&ft_face_cache);
1954 ft_face_cache = Qnil;
1956 ftfont_driver.type = Qfreetype;
1957 register_font_driver (&ftfont_driver, NULL);
1960 /* arch-tag: 7cfa432c-33a6-4988-83d2-a82ed8604aca
1961 (do not change this comment) */