* verilog-mode.el (verilog-getopt-file, verilog-set-define):
[emacs.git] / src / ftfont.c
blobed9cb6f19d26da75470f102a3b853102d91920a7
1 /* ftfont.c -- FreeType font driver.
2 Copyright (C) 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
3 Copyright (C) 2006, 2007, 2008, 2009
4 National Institute of Advanced Industrial Science and Technology (AIST)
5 Registration Number H13PRO009
7 This file is part of GNU Emacs.
9 GNU Emacs is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
14 GNU Emacs is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
22 #include <config.h>
23 #include <stdio.h>
24 #include <setjmp.h>
26 #include <fontconfig/fontconfig.h>
27 #include <fontconfig/fcfreetype.h>
29 #include "lisp.h"
30 #include "dispextern.h"
31 #include "frame.h"
32 #include "blockinput.h"
33 #include "character.h"
34 #include "charset.h"
35 #include "coding.h"
36 #include "composite.h"
37 #include "fontset.h"
38 #include "font.h"
39 #include "ftfont.h"
41 /* Symbolic type of this font-driver. */
42 Lisp_Object Qfreetype;
44 /* Fontconfig's generic families and their aliases. */
45 static Lisp_Object Qmonospace, Qsans_serif, Qserif, Qmono, Qsans, Qsans__serif;
47 /* Flag to tell if FcInit is already called or not. */
48 static int fc_initialized;
50 /* Handle to a FreeType library instance. */
51 static FT_Library ft_library;
53 /* Cache for FreeType fonts. */
54 static Lisp_Object freetype_font_cache;
56 /* Cache for FT_Face and FcCharSet. */
57 static Lisp_Object ft_face_cache;
59 /* The actual structure for FreeType font that can be casted to struct
60 font. */
62 struct ftfont_info
64 struct font font;
65 #ifdef HAVE_LIBOTF
66 /* The following four members must be here in this order to be
67 compatible with struct xftfont_info (in xftfont.c). */
68 int maybe_otf; /* Flag to tell if this may be OTF or not. */
69 OTF *otf;
70 #endif /* HAVE_LIBOTF */
71 FT_Size ft_size;
72 int index;
75 enum ftfont_cache_for
77 FTFONT_CACHE_FOR_FACE,
78 FTFONT_CACHE_FOR_CHARSET,
79 FTFONT_CACHE_FOR_ENTITY
82 static Lisp_Object ftfont_pattern_entity P_ ((FcPattern *, Lisp_Object));
84 static Lisp_Object ftfont_resolve_generic_family P_ ((Lisp_Object,
85 FcPattern *));
86 static Lisp_Object ftfont_lookup_cache P_ ((Lisp_Object,
87 enum ftfont_cache_for));
89 Lisp_Object ftfont_font_format P_ ((FcPattern *, Lisp_Object));
91 #define SYMBOL_FcChar8(SYM) (FcChar8 *) SDATA (SYMBOL_NAME (SYM))
93 static struct
95 /* registry name */
96 char *name;
97 /* characters to distinguish the charset from the others */
98 int uniquifier[6];
99 /* additional constraint by language */
100 char *lang;
101 /* set on demand */
102 FcCharSet *fc_charset;
103 } fc_charset_table[] =
104 { { "iso8859-1", { 0x00A0, 0x00A1, 0x00B4, 0x00BC, 0x00D0 } },
105 { "iso8859-2", { 0x00A0, 0x010E }},
106 { "iso8859-3", { 0x00A0, 0x0108 }},
107 { "iso8859-4", { 0x00A0, 0x00AF, 0x0128, 0x0156, 0x02C7 }},
108 { "iso8859-5", { 0x00A0, 0x0401 }},
109 { "iso8859-6", { 0x00A0, 0x060C }},
110 { "iso8859-7", { 0x00A0, 0x0384 }},
111 { "iso8859-8", { 0x00A0, 0x05D0 }},
112 { "iso8859-9", { 0x00A0, 0x00A1, 0x00BC, 0x011E }},
113 { "iso8859-10", { 0x00A0, 0x00D0, 0x0128, 0x2015 }},
114 { "iso8859-11", { 0x00A0, 0x0E01 }},
115 { "iso8859-13", { 0x00A0, 0x201C }},
116 { "iso8859-14", { 0x00A0, 0x0174 }},
117 { "iso8859-15", { 0x00A0, 0x00A1, 0x00D0, 0x0152 }},
118 { "iso8859-16", { 0x00A0, 0x0218}},
119 { "gb2312.1980-0", { 0x4E13 }, "zh-cn"},
120 { "big5-0", { 0xF6B1 }, "zh-tw" },
121 { "jisx0208.1983-0", { 0x4E55 }, "ja"},
122 { "ksc5601.1985-0", { 0xAC00 }, "ko"},
123 { "cns11643.1992-1", { 0xFE32 }, "zh-tw"},
124 { "cns11643.1992-2", { 0x4E33, 0x7934 }},
125 { "cns11643.1992-3", { 0x201A9 }},
126 { "cns11643.1992-4", { 0x20057 }},
127 { "cns11643.1992-5", { 0x20000 }},
128 { "cns11643.1992-6", { 0x20003 }},
129 { "cns11643.1992-7", { 0x20055 }},
130 { "gbk-0", { 0x4E06 }, "zh-cn"},
131 { "jisx0212.1990-0", { 0x4E44 }},
132 { "jisx0213.2000-1", { 0xFA10 }, "ja"},
133 { "jisx0213.2000-2", { 0xFA49 }},
134 { "jisx0213.2004-1", { 0x20B9F }},
135 { "viscii1.1-1", { 0x1EA0, 0x1EAE, 0x1ED2 }, "vi"},
136 { "tis620.2529-1", { 0x0E01 }, "th"},
137 { "windows-1251", { 0x0401, 0x0490 }, "ru"},
138 { "koi8-r", { 0x0401, 0x2219 }, "ru"},
139 { "mulelao-1", { 0x0E81 }, "lo"},
140 { "unicode-sip", { 0x20000 }},
141 { NULL }
144 extern Lisp_Object Qc, Qm, Qp, Qd;
146 /* Dirty hack for handing ADSTYLE property.
148 Fontconfig (actually the underlying FreeType) gives such ADSTYLE
149 font property of PCF/BDF fonts in FC_STYLE. And, "Bold",
150 "Oblique", "Italic", or any non-normal SWIDTH property names
151 (e.g. SemiCondensed) are appended. In addition, if there's no
152 ADSTYLE property nor non-normal WEIGHT/SLANT/SWIDTH properties,
153 "Regular" is used for FC_STYLE (see the function
154 pcf_interpret_style in src/pcf/pcfread.c of FreeType).
156 Unfortunately this behavior is not documented, so the following
157 code may fail if FreeType changes the behavior in the future. */
159 static Lisp_Object
160 get_adstyle_property (FcPattern *p)
162 char *str, *end;
163 Lisp_Object adstyle;
165 if (FcPatternGetString (p, FC_STYLE, 0, (FcChar8 **) &str) != FcResultMatch)
166 return Qnil;
167 for (end = str; *end && *end != ' '; end++);
168 if (*end)
170 char *p = alloca (end - str + 1);
171 memcpy (p, str, end - str);
172 p[end - str] = '\0';
173 end = p + (end - str);
174 str = p;
176 if (xstrcasecmp (str, "Regular") == 0
177 || xstrcasecmp (str, "Bold") == 0
178 || xstrcasecmp (str, "Oblique") == 0
179 || xstrcasecmp (str, "Italic") == 0)
180 return Qnil;
181 adstyle = font_intern_prop (str, end - str, 1);
182 if (font_style_to_value (FONT_WIDTH_INDEX, adstyle, 0) >= 0)
183 return Qnil;
184 return adstyle;
187 static Lisp_Object
188 ftfont_pattern_entity (p, extra)
189 FcPattern *p;
190 Lisp_Object extra;
192 Lisp_Object key, cache, entity;
193 char *file, *str;
194 int index;
195 int numeric;
196 double dbl;
197 FcBool b;
199 if (FcPatternGetString (p, FC_FILE, 0, (FcChar8 **) &file) != FcResultMatch)
200 return Qnil;
201 if (FcPatternGetInteger (p, FC_INDEX, 0, &index) != FcResultMatch)
202 return Qnil;
204 key = Fcons (make_unibyte_string ((char *) file, strlen ((char *) file)),
205 make_number (index));
206 cache = ftfont_lookup_cache (key, FTFONT_CACHE_FOR_ENTITY);
207 entity = XCAR (cache);
208 if (! NILP (entity))
210 Lisp_Object val = font_make_entity ();
211 int i;
213 for (i = 0; i < FONT_OBJLIST_INDEX; i++)
214 ASET (val, i, AREF (entity, i));
215 return val;
217 entity = font_make_entity ();
218 XSETCAR (cache, entity);
220 ASET (entity, FONT_TYPE_INDEX, Qfreetype);
221 ASET (entity, FONT_REGISTRY_INDEX, Qiso10646_1);
223 if (FcPatternGetString (p, FC_FOUNDRY, 0, (FcChar8 **) &str) == FcResultMatch)
224 ASET (entity, FONT_FOUNDRY_INDEX, font_intern_prop (str, strlen (str), 1));
225 if (FcPatternGetString (p, FC_FAMILY, 0, (FcChar8 **) &str) == FcResultMatch)
226 ASET (entity, FONT_FAMILY_INDEX, font_intern_prop (str, strlen (str), 1));
227 if (FcPatternGetInteger (p, FC_WEIGHT, 0, &numeric) == FcResultMatch)
229 if (numeric >= FC_WEIGHT_REGULAR && numeric < FC_WEIGHT_MEDIUM)
230 numeric = FC_WEIGHT_MEDIUM;
231 FONT_SET_STYLE (entity, FONT_WEIGHT_INDEX, make_number (numeric));
233 if (FcPatternGetInteger (p, FC_SLANT, 0, &numeric) == FcResultMatch)
235 numeric += 100;
236 FONT_SET_STYLE (entity, FONT_SLANT_INDEX, make_number (numeric));
238 if (FcPatternGetInteger (p, FC_WIDTH, 0, &numeric) == FcResultMatch)
240 FONT_SET_STYLE (entity, FONT_WIDTH_INDEX, make_number (numeric));
242 if (FcPatternGetDouble (p, FC_PIXEL_SIZE, 0, &dbl) == FcResultMatch)
244 ASET (entity, FONT_SIZE_INDEX, make_number (dbl));
246 else
247 ASET (entity, FONT_SIZE_INDEX, make_number (0));
248 if (FcPatternGetInteger (p, FC_SPACING, 0, &numeric) == FcResultMatch)
249 ASET (entity, FONT_SPACING_INDEX, make_number (numeric));
250 if (FcPatternGetDouble (p, FC_DPI, 0, &dbl) == FcResultMatch)
252 int dpi = dbl;
253 ASET (entity, FONT_DPI_INDEX, make_number (dpi));
255 if (FcPatternGetBool (p, FC_SCALABLE, 0, &b) == FcResultMatch
256 && b == FcTrue)
258 ASET (entity, FONT_SIZE_INDEX, make_number (0));
259 ASET (entity, FONT_AVGWIDTH_INDEX, make_number (0));
261 else
263 /* As this font is not scalable, parhaps this is a BDF or PCF
264 font. */
265 FT_Face ft_face;
267 ASET (entity, FONT_ADSTYLE_INDEX, get_adstyle_property (p));
268 if ((ft_library || FT_Init_FreeType (&ft_library) == 0)
269 && FT_New_Face (ft_library, file, index, &ft_face) == 0)
271 BDF_PropertyRec rec;
273 if (FT_Get_BDF_Property (ft_face, "AVERAGE_WIDTH", &rec) == 0
274 && rec.type == BDF_PROPERTY_TYPE_INTEGER)
275 ASET (entity, FONT_AVGWIDTH_INDEX, make_number (rec.u.integer));
276 FT_Done_Face (ft_face);
280 ASET (entity, FONT_EXTRA_INDEX, Fcopy_sequence (extra));
281 font_put_extra (entity, QCfont_entity, key);
282 return entity;
286 static Lisp_Object ftfont_generic_family_list;
288 static Lisp_Object
289 ftfont_resolve_generic_family (family, pattern)
290 Lisp_Object family;
291 FcPattern *pattern;
293 Lisp_Object slot;
294 FcPattern *match;
295 FcResult result;
296 FcLangSet *langset;
298 family = Fintern (Fdowncase (SYMBOL_NAME (family)), Qnil);
299 if (EQ (family, Qmono))
300 family = Qmonospace;
301 else if (EQ (family, Qsans) || EQ (family, Qsans__serif))
302 family = Qsans_serif;
303 slot = assq_no_quit (family, ftfont_generic_family_list);
304 if (! CONSP (slot))
305 return Qnil;
306 if (! EQ (XCDR (slot), Qt))
307 return XCDR (slot);
308 pattern = FcPatternDuplicate (pattern);
309 if (! pattern)
310 goto err;
311 FcPatternDel (pattern, FC_FOUNDRY);
312 FcPatternDel (pattern, FC_FAMILY);
313 FcPatternAddString (pattern, FC_FAMILY, SYMBOL_FcChar8 (family));
314 if (FcPatternGetLangSet (pattern, FC_LANG, 0, &langset) != FcResultMatch)
316 /* This is to avoid the effect of locale. */
317 langset = FcLangSetCreate ();
318 FcLangSetAdd (langset, "en");
319 FcPatternAddLangSet (pattern, FC_LANG, langset);
320 FcLangSetDestroy (langset);
322 FcConfigSubstitute (NULL, pattern, FcMatchPattern);
323 FcDefaultSubstitute (pattern);
324 match = FcFontMatch (NULL, pattern, &result);
325 if (match)
327 FcChar8 *fam;
329 if (FcPatternGetString (match, FC_FAMILY, 0, &fam) == FcResultMatch)
330 family = intern ((char *) fam);
332 else
333 family = Qnil;
334 XSETCDR (slot, family);
335 if (match) FcPatternDestroy (match);
336 err:
337 if (pattern) FcPatternDestroy (pattern);
338 return family;
341 struct ftfont_cache_data
343 FT_Face ft_face;
344 FcCharSet *fc_charset;
347 static Lisp_Object
348 ftfont_lookup_cache (key, cache_for)
349 Lisp_Object key;
350 enum ftfont_cache_for cache_for;
352 Lisp_Object cache, val, entity;
353 struct ftfont_cache_data *cache_data;
355 if (FONT_ENTITY_P (key))
357 entity = key;
358 val = assq_no_quit (QCfont_entity, AREF (entity, FONT_EXTRA_INDEX));
359 xassert (CONSP (val));
360 key = XCDR (val);
362 else
363 entity = Qnil;
365 if (NILP (ft_face_cache))
366 cache = Qnil;
367 else
368 cache = Fgethash (key, ft_face_cache, Qnil);
369 if (NILP (cache))
371 if (NILP (ft_face_cache))
373 Lisp_Object args[2];
375 args[0] = QCtest;
376 args[1] = Qequal;
377 ft_face_cache = Fmake_hash_table (2, args);
379 cache_data = xmalloc (sizeof (struct ftfont_cache_data));
380 cache_data->ft_face = NULL;
381 cache_data->fc_charset = NULL;
382 val = make_save_value (NULL, 0);
383 XSAVE_VALUE (val)->integer = 0;
384 XSAVE_VALUE (val)->pointer = cache_data;
385 cache = Fcons (Qnil, val);
386 Fputhash (key, cache, ft_face_cache);
388 else
390 val = XCDR (cache);
391 cache_data = XSAVE_VALUE (val)->pointer;
394 if (cache_for == FTFONT_CACHE_FOR_ENTITY)
395 return cache;
397 if (cache_for == FTFONT_CACHE_FOR_FACE
398 ? ! cache_data->ft_face : ! cache_data->fc_charset)
400 char *filename = (char *) SDATA (XCAR (key));
401 int index = XINT (XCDR (key));
403 if (cache_for == FTFONT_CACHE_FOR_FACE)
405 if (! ft_library
406 && FT_Init_FreeType (&ft_library) != 0)
407 return Qnil;
408 if (FT_New_Face (ft_library, filename, index, &cache_data->ft_face)
409 != 0)
410 return Qnil;
412 else
414 FcPattern *pat = NULL;
415 FcFontSet *fontset = NULL;
416 FcObjectSet *objset = NULL;
417 FcCharSet *charset = NULL;
419 pat = FcPatternBuild (0, FC_FILE, FcTypeString, (FcChar8 *) filename,
420 FC_INDEX, FcTypeInteger, index, NULL);
421 if (! pat)
422 goto finish;
423 objset = FcObjectSetBuild (FC_CHARSET, FC_STYLE, NULL);
424 if (! objset)
425 goto finish;
426 fontset = FcFontList (NULL, pat, objset);
427 if (! fontset)
428 goto finish;
429 if (fontset && fontset->nfont > 0
430 && (FcPatternGetCharSet (fontset->fonts[0], FC_CHARSET, 0,
431 &charset)
432 == FcResultMatch))
433 cache_data->fc_charset = FcCharSetCopy (charset);
434 else
435 cache_data->fc_charset = FcCharSetCreate ();
437 finish:
438 if (fontset)
439 FcFontSetDestroy (fontset);
440 if (objset)
441 FcObjectSetDestroy (objset);
442 if (pat)
443 FcPatternDestroy (pat);
446 return cache;
449 FcCharSet *
450 ftfont_get_fc_charset (entity)
451 Lisp_Object entity;
453 Lisp_Object val, cache;
454 struct ftfont_cache_data *cache_data;
456 cache = ftfont_lookup_cache (entity, FTFONT_CACHE_FOR_CHARSET);
457 val = XCDR (cache);
458 cache_data = XSAVE_VALUE (val)->pointer;
459 return cache_data->fc_charset;
462 #ifdef HAVE_LIBOTF
463 static OTF *
464 ftfont_get_otf (ftfont_info)
465 struct ftfont_info *ftfont_info;
467 OTF *otf;
469 if (ftfont_info->otf)
470 return ftfont_info->otf;
471 if (! ftfont_info->maybe_otf)
472 return NULL;
473 otf = OTF_open_ft_face (ftfont_info->ft_size->face);
474 if (! otf || OTF_get_table (otf, "head") < 0)
476 if (otf)
477 OTF_close (otf);
478 ftfont_info->maybe_otf = 0;
479 return NULL;
481 ftfont_info->otf = otf;
482 return otf;
484 #endif /* HAVE_LIBOTF */
486 static Lisp_Object ftfont_get_cache P_ ((FRAME_PTR));
487 static Lisp_Object ftfont_list P_ ((Lisp_Object, Lisp_Object));
488 static Lisp_Object ftfont_match P_ ((Lisp_Object, Lisp_Object));
489 static Lisp_Object ftfont_list_family P_ ((Lisp_Object));
490 static Lisp_Object ftfont_open P_ ((FRAME_PTR, Lisp_Object, int));
491 static void ftfont_close P_ ((FRAME_PTR, struct font *));
492 static int ftfont_has_char P_ ((Lisp_Object, int));
493 static unsigned ftfont_encode_char P_ ((struct font *, int));
494 static int ftfont_text_extents P_ ((struct font *, unsigned *, int,
495 struct font_metrics *));
496 static int ftfont_get_bitmap P_ ((struct font *, unsigned,
497 struct font_bitmap *, int));
498 static int ftfont_anchor_point P_ ((struct font *, unsigned, int,
499 int *, int *));
500 static Lisp_Object ftfont_otf_capability P_ ((struct font *));
501 static Lisp_Object ftfont_shape P_ ((Lisp_Object));
503 #ifdef HAVE_OTF_GET_VARIATION_GLYPHS
504 static int ftfont_variation_glyphs P_ ((struct font *, int c,
505 unsigned variations[256]));
506 #endif /* HAVE_OTF_GET_VARIATION_GLYPHS */
508 struct font_driver ftfont_driver =
510 0, /* Qfreetype */
511 0, /* case insensitive */
512 ftfont_get_cache,
513 ftfont_list,
514 ftfont_match,
515 ftfont_list_family,
516 NULL, /* free_entity */
517 ftfont_open,
518 ftfont_close,
519 /* We can't draw a text without device dependent functions. */
520 NULL, /* prepare_face */
521 NULL, /* done_face */
522 ftfont_has_char,
523 ftfont_encode_char,
524 ftfont_text_extents,
525 /* We can't draw a text without device dependent functions. */
526 NULL, /* draw */
527 ftfont_get_bitmap,
528 NULL, /* get_bitmap */
529 NULL, /* free_bitmap */
530 NULL, /* get_outline */
531 ftfont_anchor_point,
532 #ifdef HAVE_LIBOTF
533 ftfont_otf_capability,
534 #else /* not HAVE_LIBOTF */
535 NULL,
536 #endif /* not HAVE_LIBOTF */
537 NULL, /* otf_drive */
538 NULL, /* start_for_frame */
539 NULL, /* end_for_frame */
540 #if defined (HAVE_M17N_FLT) && defined (HAVE_LIBOTF)
541 ftfont_shape,
542 #else /* not (HAVE_M17N_FLT && HAVE_LIBOTF) */
543 NULL,
544 #endif /* not (HAVE_M17N_FLT && HAVE_LIBOTF) */
545 NULL, /* check */
547 #ifdef HAVE_OTF_GET_VARIATION_GLYPHS
548 ftfont_variation_glyphs
549 #else
550 NULL
551 #endif
554 extern Lisp_Object QCname;
556 static Lisp_Object
557 ftfont_get_cache (f)
558 FRAME_PTR f;
560 return freetype_font_cache;
563 static int
564 ftfont_get_charset (registry)
565 Lisp_Object registry;
567 char *str = (char *) SDATA (SYMBOL_NAME (registry));
568 char *re = alloca (SBYTES (SYMBOL_NAME (registry)) * 2 + 1);
569 Lisp_Object regexp;
570 int i, j;
572 for (i = j = 0; i < SBYTES (SYMBOL_NAME (registry)); i++, j++)
574 if (str[i] == '.')
575 re[j++] = '\\';
576 else if (str[i] == '*')
577 re[j++] = '.';
578 re[j] = str[i];
579 if (re[j] == '?')
580 re[j] = '.';
582 re[j] = '\0';
583 regexp = make_unibyte_string (re, j);
584 for (i = 0; fc_charset_table[i].name; i++)
585 if (fast_c_string_match_ignore_case (regexp, fc_charset_table[i].name) >= 0)
586 break;
587 if (! fc_charset_table[i].name)
588 return -1;
589 if (! fc_charset_table[i].fc_charset)
591 FcCharSet *charset = FcCharSetCreate ();
592 int *uniquifier = fc_charset_table[i].uniquifier;
594 if (! charset)
595 return -1;
596 for (j = 0; uniquifier[j]; j++)
597 if (! FcCharSetAddChar (charset, uniquifier[j]))
599 FcCharSetDestroy (charset);
600 return -1;
602 fc_charset_table[i].fc_charset = charset;
604 return i;
607 struct OpenTypeSpec
609 Lisp_Object script;
610 unsigned int script_tag, langsys_tag;
611 int nfeatures[2];
612 unsigned int *features[2];
615 #define OTF_SYM_TAG(SYM, TAG) \
616 do { \
617 unsigned char *p = SDATA (SYMBOL_NAME (SYM)); \
618 TAG = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]; \
619 } while (0)
621 #define OTF_TAG_STR(TAG, P) \
622 do { \
623 (P)[0] = (char) (TAG >> 24); \
624 (P)[1] = (char) ((TAG >> 16) & 0xFF); \
625 (P)[2] = (char) ((TAG >> 8) & 0xFF); \
626 (P)[3] = (char) (TAG & 0xFF); \
627 (P)[4] = '\0'; \
628 } while (0)
630 #define OTF_TAG_SYM(SYM, TAG) \
631 do { \
632 char str[5]; \
634 OTF_TAG_STR (TAG, str); \
635 (SYM) = font_intern_prop (str, 4, 1); \
636 } while (0)
639 static struct OpenTypeSpec *
640 ftfont_get_open_type_spec (Lisp_Object otf_spec)
642 struct OpenTypeSpec *spec = malloc (sizeof (struct OpenTypeSpec));
643 Lisp_Object val;
644 int i, j, negative;
646 if (! spec)
647 return NULL;
648 spec->script = XCAR (otf_spec);
649 if (! NILP (spec->script))
651 OTF_SYM_TAG (spec->script, spec->script_tag);
652 val = assq_no_quit (spec->script, Votf_script_alist);
653 if (CONSP (val) && SYMBOLP (XCDR (val)))
654 spec->script = XCDR (val);
655 else
656 spec->script = Qnil;
658 else
659 spec->script_tag = 0x44464C54; /* "DFLT" */
660 otf_spec = XCDR (otf_spec);
661 val = XCAR (otf_spec);
662 if (! NILP (val))
663 OTF_SYM_TAG (val, spec->langsys_tag);
664 else
665 spec->langsys_tag = 0;
666 spec->nfeatures[0] = spec->nfeatures[1] = 0;
667 for (i = 0; i < 2; i++)
669 Lisp_Object len;
671 otf_spec = XCDR (otf_spec);
672 if (NILP (otf_spec))
673 break;
674 val = XCAR (otf_spec);
675 if (NILP (val))
676 continue;
677 len = Flength (val);
678 spec->features[i] = malloc (sizeof (int) * XINT (len));
679 if (! spec->features[i])
681 if (i > 0 && spec->features[0])
682 free (spec->features[0]);
683 free (spec);
684 return NULL;
686 for (j = 0, negative = 0; CONSP (val); val = XCDR (val))
688 if (NILP (XCAR (val)))
689 negative = 1;
690 else
692 unsigned int tag;
694 OTF_SYM_TAG (XCAR (val), tag);
695 spec->features[i][j++] = negative ? tag & 0x80000000 : tag;
698 spec->nfeatures[i] = j;
700 return spec;
703 static FcPattern *ftfont_spec_pattern P_ ((Lisp_Object, char *,
704 struct OpenTypeSpec **,
705 char **langname));
707 static FcPattern *
708 ftfont_spec_pattern (spec, otlayout, otspec, langname)
709 Lisp_Object spec;
710 char *otlayout;
711 struct OpenTypeSpec **otspec;
712 char **langname;
714 Lisp_Object tmp, extra;
715 FcPattern *pattern = NULL;
716 FcCharSet *charset = NULL;
717 FcLangSet *langset = NULL;
718 int n;
719 int dpi = -1;
720 int scalable = -1;
721 Lisp_Object script = Qnil;
722 Lisp_Object registry;
723 int fc_charset_idx;
725 if ((n = FONT_SLANT_NUMERIC (spec)) >= 0
726 && n < 100)
727 /* Fontconfig doesn't support reverse-italic/obligue. */
728 return NULL;
730 if (INTEGERP (AREF (spec, FONT_DPI_INDEX)))
731 dpi = XINT (AREF (spec, FONT_DPI_INDEX));
732 if (INTEGERP (AREF (spec, FONT_AVGWIDTH_INDEX))
733 && XINT (AREF (spec, FONT_AVGWIDTH_INDEX)) == 0)
734 scalable = 1;
736 registry = AREF (spec, FONT_REGISTRY_INDEX);
737 if (NILP (registry)
738 || EQ (registry, Qascii_0)
739 || EQ (registry, Qiso10646_1)
740 || EQ (registry, Qunicode_bmp))
741 fc_charset_idx = -1;
742 else
744 FcChar8 *lang;
746 fc_charset_idx = ftfont_get_charset (registry);
747 if (fc_charset_idx < 0)
748 return NULL;
749 charset = fc_charset_table[fc_charset_idx].fc_charset;
750 *langname = fc_charset_table[fc_charset_idx].lang;
751 lang = (FcChar8 *) *langname;
752 if (lang)
754 langset = FcLangSetCreate ();
755 if (! langset)
756 goto err;
757 FcLangSetAdd (langset, lang);
761 otlayout[0] = '\0';
762 for (extra = AREF (spec, FONT_EXTRA_INDEX);
763 CONSP (extra); extra = XCDR (extra))
765 Lisp_Object key, val;
767 key = XCAR (XCAR (extra)), val = XCDR (XCAR (extra));
768 if (EQ (key, QCdpi))
769 dpi = XINT (val);
770 else if (EQ (key, QClang))
772 if (! langset)
773 langset = FcLangSetCreate ();
774 if (! langset)
775 goto err;
776 if (SYMBOLP (val))
778 if (! FcLangSetAdd (langset, SYMBOL_FcChar8 (val)))
779 goto err;
781 else
782 for (; CONSP (val); val = XCDR (val))
783 if (SYMBOLP (XCAR (val))
784 && ! FcLangSetAdd (langset, SYMBOL_FcChar8 (XCAR (val))))
785 goto err;
787 else if (EQ (key, QCotf))
789 *otspec = ftfont_get_open_type_spec (val);
790 if (! *otspec)
791 return NULL;
792 strcat (otlayout, "otlayout:");
793 OTF_TAG_STR ((*otspec)->script_tag, otlayout + 9);
794 script = (*otspec)->script;
796 else if (EQ (key, QCscript))
797 script = val;
798 else if (EQ (key, QCscalable))
799 scalable = ! NILP (val);
802 if (! NILP (script) && ! charset)
804 Lisp_Object chars = assq_no_quit (script, Vscript_representative_chars);
806 if (CONSP (chars) && CONSP (CDR (chars)))
808 charset = FcCharSetCreate ();
809 if (! charset)
810 goto err;
811 for (chars = XCDR (chars); CONSP (chars); chars = XCDR (chars))
812 if (CHARACTERP (XCAR (chars))
813 && ! FcCharSetAddChar (charset, XUINT (XCAR (chars))))
814 goto err;
818 pattern = FcPatternCreate ();
819 if (! pattern)
820 goto err;
821 tmp = AREF (spec, FONT_FOUNDRY_INDEX);
822 if (! NILP (tmp)
823 && ! FcPatternAddString (pattern, FC_FOUNDRY, SYMBOL_FcChar8 (tmp)))
824 goto err;
825 tmp = AREF (spec, FONT_FAMILY_INDEX);
826 if (! NILP (tmp)
827 && ! FcPatternAddString (pattern, FC_FAMILY, SYMBOL_FcChar8 (tmp)))
828 goto err;
829 if (charset
830 && ! FcPatternAddCharSet (pattern, FC_CHARSET, charset))
831 goto err;
832 if (langset
833 && ! FcPatternAddLangSet (pattern, FC_LANG, langset))
834 goto err;
835 if (dpi >= 0
836 && ! FcPatternAddDouble (pattern, FC_DPI, dpi))
837 goto err;
838 if (scalable >= 0
839 && ! FcPatternAddBool (pattern, FC_SCALABLE, scalable ? FcTrue : FcFalse))
840 goto err;
842 goto finish;
844 err:
845 /* We come here because of unexpected error in fontconfig API call
846 (usually insufficient memory). */
847 if (pattern)
849 FcPatternDestroy (pattern);
850 pattern = NULL;
852 if (*otspec)
854 if ((*otspec)->nfeatures[0] > 0)
855 free ((*otspec)->features[0]);
856 if ((*otspec)->nfeatures[1] > 0)
857 free ((*otspec)->features[1]);
858 free (*otspec);
859 *otspec = NULL;
862 finish:
863 if (langset) FcLangSetDestroy (langset);
864 if (charset && fc_charset_idx < 0) FcCharSetDestroy (charset);
865 return pattern;
868 static Lisp_Object
869 ftfont_list (frame, spec)
870 Lisp_Object frame, spec;
872 Lisp_Object val = Qnil, family, adstyle;
873 int i;
874 FcPattern *pattern;
875 FcFontSet *fontset = NULL;
876 FcObjectSet *objset = NULL;
877 FcCharSet *charset;
878 Lisp_Object chars = Qnil;
879 FcResult result;
880 char otlayout[15]; /* For "otlayout:XXXX" */
881 struct OpenTypeSpec *otspec = NULL;
882 int spacing = -1;
883 char *langname = NULL;
885 if (! fc_initialized)
887 FcInit ();
888 fc_initialized = 1;
891 pattern = ftfont_spec_pattern (spec, otlayout, &otspec, &langname);
892 if (! pattern)
893 return Qnil;
894 if (FcPatternGetCharSet (pattern, FC_CHARSET, 0, &charset) != FcResultMatch)
896 val = assq_no_quit (QCscript, AREF (spec, FONT_EXTRA_INDEX));
897 if (! NILP (val))
899 val = assq_no_quit (XCDR (val), Vscript_representative_chars);
900 if (CONSP (val) && VECTORP (XCDR (val)))
901 chars = XCDR (val);
903 val = Qnil;
905 if (INTEGERP (AREF (spec, FONT_SPACING_INDEX)))
906 spacing = XINT (AREF (spec, FONT_SPACING_INDEX));
907 family = AREF (spec, FONT_FAMILY_INDEX);
908 if (! NILP (family))
910 Lisp_Object resolved;
912 resolved = ftfont_resolve_generic_family (family, pattern);
913 if (! NILP (resolved))
915 FcPatternDel (pattern, FC_FAMILY);
916 if (! FcPatternAddString (pattern, FC_FAMILY,
917 SYMBOL_FcChar8 (resolved)))
918 goto err;
921 adstyle = AREF (spec, FONT_ADSTYLE_INDEX);
922 if (! NILP (adstyle) && SBYTES (SYMBOL_NAME (adstyle)) == 0)
923 adstyle = Qnil;
924 objset = FcObjectSetBuild (FC_FOUNDRY, FC_FAMILY, FC_WEIGHT, FC_SLANT,
925 FC_WIDTH, FC_PIXEL_SIZE, FC_SPACING, FC_SCALABLE,
926 FC_STYLE, FC_FILE, FC_INDEX,
927 #ifdef FC_CAPABILITY
928 FC_CAPABILITY,
929 #endif /* FC_CAPABILITY */
930 #ifdef FC_FONTFORMAT
931 FC_FONTFORMAT,
932 #endif
933 NULL);
934 if (! objset)
935 goto err;
936 if (! NILP (chars))
937 FcObjectSetAdd (objset, FC_CHARSET);
939 fontset = FcFontList (NULL, pattern, objset);
940 if (! fontset || fontset->nfont == 0)
941 goto finish;
942 #if 0
943 /* Need fix because this finds any fonts. */
944 if (fontset->nfont == 0 && ! NILP (family))
946 /* Try maching with configuration. For instance, the
947 configuration may specify "Nimbus Mono L" as an alias of
948 "Courier". */
949 FcPattern *pat = FcPatternBuild (0, FC_FAMILY, FcTypeString,
950 SYMBOL_FcChar8 (family), NULL);
951 FcChar8 *fam;
953 if (FcConfigSubstitute (NULL, pat, FcMatchPattern) == FcTrue)
955 for (i = 0;
956 FcPatternGetString (pat, FC_FAMILY, i, &fam) == FcResultMatch;
957 i++)
959 FcPatternDel (pattern, FC_FAMILY);
960 FcPatternAddString (pattern, FC_FAMILY, fam);
961 FcFontSetDestroy (fontset);
962 fontset = FcFontList (NULL, pattern, objset);
963 if (fontset && fontset->nfont > 0)
964 break;
968 #endif
969 for (i = 0; i < fontset->nfont; i++)
971 Lisp_Object entity;
973 if (spacing >= 0)
975 int this;
977 if ((FcPatternGetInteger (fontset->fonts[i], FC_SPACING, 0, &this)
978 == FcResultMatch)
979 && spacing != this)
980 continue;
983 #ifdef FC_CAPABILITY
984 if (otlayout[0])
986 FcChar8 *this;
988 if (FcPatternGetString (fontset->fonts[i], FC_CAPABILITY, 0, &this)
989 != FcResultMatch
990 || ! strstr ((char *) this, otlayout))
991 continue;
993 #endif /* FC_CAPABILITY */
994 #ifdef HAVE_LIBOTF
995 if (otspec)
997 FcChar8 *file;
998 OTF *otf;
1000 if (FcPatternGetString (fontset->fonts[i], FC_FILE, 0, &file)
1001 != FcResultMatch)
1002 continue;
1003 otf = OTF_open ((char *) file);
1004 if (! otf)
1005 continue;
1006 if (OTF_check_features (otf, 1,
1007 otspec->script_tag, otspec->langsys_tag,
1008 otspec->features[0],
1009 otspec->nfeatures[0]) != 1
1010 || OTF_check_features (otf, 0,
1011 otspec->script_tag, otspec->langsys_tag,
1012 otspec->features[1],
1013 otspec->nfeatures[1]) != 1)
1014 continue;
1016 #endif /* HAVE_LIBOTF */
1017 if (VECTORP (chars))
1019 int j;
1021 if (FcPatternGetCharSet (fontset->fonts[i], FC_CHARSET, 0, &charset)
1022 != FcResultMatch)
1023 continue;
1024 for (j = 0; j < ASIZE (chars); j++)
1025 if (NATNUMP (AREF (chars, j))
1026 && FcCharSetHasChar (charset, XFASTINT (AREF (chars, j))))
1027 break;
1028 if (j == ASIZE (chars))
1029 continue;
1031 if (! NILP (adstyle) || langname)
1033 Lisp_Object this_adstyle = get_adstyle_property (fontset->fonts[i]);
1035 if (! NILP (adstyle)
1036 && (NILP (this_adstyle)
1037 || xstrcasecmp (SDATA (SYMBOL_NAME (adstyle)),
1038 SDATA (SYMBOL_NAME (this_adstyle))) != 0))
1039 continue;
1040 if (langname
1041 && ! NILP (this_adstyle)
1042 && xstrcasecmp (langname, SDATA (SYMBOL_NAME (this_adstyle))))
1043 continue;
1045 entity = ftfont_pattern_entity (fontset->fonts[i],
1046 AREF (spec, FONT_EXTRA_INDEX));
1047 if (! NILP (entity))
1048 val = Fcons (entity, val);
1050 val = Fnreverse (val);
1051 goto finish;
1053 err:
1054 /* We come here because of unexpected error in fontconfig API call
1055 (usually insufficient memory). */
1056 val = Qnil;
1058 finish:
1059 FONT_ADD_LOG ("ftfont-list", spec, val);
1060 if (objset) FcObjectSetDestroy (objset);
1061 if (fontset) FcFontSetDestroy (fontset);
1062 if (pattern) FcPatternDestroy (pattern);
1063 return val;
1066 static Lisp_Object
1067 ftfont_match (frame, spec)
1068 Lisp_Object frame, spec;
1070 Lisp_Object entity = Qnil;
1071 FcPattern *pattern, *match = NULL;
1072 FcResult result;
1073 char otlayout[15]; /* For "otlayout:XXXX" */
1074 struct OpenTypeSpec *otspec = NULL;
1075 char *langname = NULL;
1077 if (! fc_initialized)
1079 FcInit ();
1080 fc_initialized = 1;
1083 pattern = ftfont_spec_pattern (spec, otlayout, &otspec, &langname);
1084 if (! pattern)
1085 return Qnil;
1087 if (INTEGERP (AREF (spec, FONT_SIZE_INDEX)))
1089 FcValue value;
1091 value.type = FcTypeDouble;
1092 value.u.d = XINT (AREF (spec, FONT_SIZE_INDEX));
1093 FcPatternAdd (pattern, FC_PIXEL_SIZE, value, FcFalse);
1095 if (FcConfigSubstitute (NULL, pattern, FcMatchPattern) == FcTrue)
1097 FcDefaultSubstitute (pattern);
1098 match = FcFontMatch (NULL, pattern, &result);
1099 if (match)
1101 entity = ftfont_pattern_entity (match, AREF (spec, FONT_EXTRA_INDEX));
1102 FcPatternDestroy (match);
1103 if (! NILP (AREF (spec, FONT_FAMILY_INDEX))
1104 && NILP (assq_no_quit (AREF (spec, FONT_FAMILY_INDEX),
1105 ftfont_generic_family_list))
1106 && NILP (Fstring_equal (AREF (spec, FONT_FAMILY_INDEX),
1107 AREF (entity, FONT_FAMILY_INDEX))))
1108 entity = Qnil;
1111 FcPatternDestroy (pattern);
1113 FONT_ADD_LOG ("ftfont-match", spec, entity);
1114 return entity;
1117 static Lisp_Object
1118 ftfont_list_family (frame)
1119 Lisp_Object frame;
1121 Lisp_Object list = Qnil;
1122 FcPattern *pattern = NULL;
1123 FcFontSet *fontset = NULL;
1124 FcObjectSet *objset = NULL;
1125 int i;
1127 if (! fc_initialized)
1129 FcInit ();
1130 fc_initialized = 1;
1133 pattern = FcPatternCreate ();
1134 if (! pattern)
1135 goto finish;
1136 objset = FcObjectSetBuild (FC_FAMILY, NULL);
1137 if (! objset)
1138 goto finish;
1139 fontset = FcFontList (NULL, pattern, objset);
1140 if (! fontset)
1141 goto finish;
1143 for (i = 0; i < fontset->nfont; i++)
1145 FcPattern *pat = fontset->fonts[i];
1146 FcChar8 *str;
1148 if (FcPatternGetString (pat, FC_FAMILY, 0, &str) == FcResultMatch)
1149 list = Fcons (intern ((char *) str), list);
1152 finish:
1153 if (objset) FcObjectSetDestroy (objset);
1154 if (fontset) FcFontSetDestroy (fontset);
1155 if (pattern) FcPatternDestroy (pattern);
1157 return list;
1161 static Lisp_Object
1162 ftfont_open (f, entity, pixel_size)
1163 FRAME_PTR f;
1164 Lisp_Object entity;
1165 int pixel_size;
1167 struct ftfont_info *ftfont_info;
1168 struct font *font;
1169 struct ftfont_cache_data *cache_data;
1170 FT_Face ft_face;
1171 FT_Size ft_size;
1172 FT_UInt size;
1173 Lisp_Object val, filename, index, cache, font_object;
1174 int scalable;
1175 int spacing;
1176 char name[256];
1177 int i, len;
1178 int upEM;
1180 val = assq_no_quit (QCfont_entity, AREF (entity, FONT_EXTRA_INDEX));
1181 if (! CONSP (val))
1182 return Qnil;
1183 val = XCDR (val);
1184 cache = ftfont_lookup_cache (entity, FTFONT_CACHE_FOR_FACE);
1185 if (NILP (cache))
1186 return Qnil;
1187 filename = XCAR (val);
1188 index = XCDR (val);
1189 val = XCDR (cache);
1190 cache_data = XSAVE_VALUE (XCDR (cache))->pointer;
1191 ft_face = cache_data->ft_face;
1192 if (XSAVE_VALUE (val)->integer > 0)
1194 /* FT_Face in this cache is already used by the different size. */
1195 if (FT_New_Size (ft_face, &ft_size) != 0)
1196 return Qnil;
1197 if (FT_Activate_Size (ft_size) != 0)
1199 FT_Done_Size (ft_size);
1200 return Qnil;
1203 XSAVE_VALUE (val)->integer++;
1204 size = XINT (AREF (entity, FONT_SIZE_INDEX));
1205 if (size == 0)
1206 size = pixel_size;
1207 if (FT_Set_Pixel_Sizes (ft_face, size, size) != 0)
1209 if (XSAVE_VALUE (val)->integer == 0)
1210 FT_Done_Face (ft_face);
1211 return Qnil;
1214 font_object = font_make_object (VECSIZE (struct ftfont_info), entity, size);
1215 ASET (font_object, FONT_TYPE_INDEX, Qfreetype);
1216 len = font_unparse_xlfd (entity, size, name, 256);
1217 if (len > 0)
1218 ASET (font_object, FONT_NAME_INDEX, make_string (name, len));
1219 len = font_unparse_fcname (entity, size, name, 256);
1220 if (len > 0)
1221 ASET (font_object, FONT_FULLNAME_INDEX, make_string (name, len));
1222 else
1223 ASET (font_object, FONT_FULLNAME_INDEX,
1224 AREF (font_object, FONT_NAME_INDEX));
1225 ASET (font_object, FONT_FILE_INDEX, filename);
1226 ASET (font_object, FONT_FORMAT_INDEX, ftfont_font_format (NULL, filename));
1227 font = XFONT_OBJECT (font_object);
1228 ftfont_info = (struct ftfont_info *) font;
1229 ftfont_info->ft_size = ft_face->size;
1230 ftfont_info->index = XINT (index);
1231 #ifdef HAVE_LIBOTF
1232 ftfont_info->maybe_otf = ft_face->face_flags & FT_FACE_FLAG_SFNT;
1233 ftfont_info->otf = NULL;
1234 #endif /* HAVE_LIBOTF */
1235 font->pixel_size = size;
1236 font->driver = &ftfont_driver;
1237 font->encoding_charset = font->repertory_charset = -1;
1239 upEM = ft_face->units_per_EM;
1240 scalable = (INTEGERP (AREF (entity, FONT_AVGWIDTH_INDEX))
1241 && XINT (AREF (entity, FONT_AVGWIDTH_INDEX)) == 0);
1242 if (scalable)
1244 font->ascent = ft_face->ascender * size / upEM;
1245 font->descent = - ft_face->descender * size / upEM;
1246 font->height = ft_face->height * size / upEM;
1248 else
1250 font->ascent = ft_face->size->metrics.ascender >> 6;
1251 font->descent = - ft_face->size->metrics.descender >> 6;
1252 font->height = ft_face->size->metrics.height >> 6;
1254 if (INTEGERP (AREF (entity, FONT_SPACING_INDEX)))
1255 spacing = XINT (AREF (entity, FONT_SPACING_INDEX));
1256 else
1257 spacing = FC_PROPORTIONAL;
1258 if (spacing != FC_PROPORTIONAL)
1259 font->min_width = font->average_width = font->space_width
1260 = (scalable ? ft_face->max_advance_width * size / upEM
1261 : ft_face->size->metrics.max_advance >> 6);
1262 else
1264 int n;
1266 font->min_width = font->average_width = font->space_width = 0;
1267 for (i = 32, n = 0; i < 127; i++)
1268 if (FT_Load_Char (ft_face, i, FT_LOAD_DEFAULT) == 0)
1270 int this_width = ft_face->glyph->metrics.horiAdvance >> 6;
1272 if (this_width > 0
1273 && (! font->min_width || font->min_width > this_width))
1274 font->min_width = this_width;
1275 if (i == 32)
1276 font->space_width = this_width;
1277 font->average_width += this_width;
1278 n++;
1280 if (n > 0)
1281 font->average_width /= n;
1284 font->baseline_offset = 0;
1285 font->relative_compose = 0;
1286 font->default_ascent = 0;
1287 font->vertical_centering = 0;
1288 if (scalable)
1290 font->underline_position = -ft_face->underline_position * size / upEM;
1291 font->underline_thickness = ft_face->underline_thickness * size / upEM;
1293 else
1295 font->underline_position = -1;
1296 font->underline_thickness = 0;
1299 return font_object;
1302 static void
1303 ftfont_close (f, font)
1304 FRAME_PTR f;
1305 struct font *font;
1307 struct ftfont_info *ftfont_info = (struct ftfont_info *) font;
1308 Lisp_Object val, cache;
1310 val = Fcons (font->props[FONT_FILE_INDEX], make_number (ftfont_info->index));
1311 cache = ftfont_lookup_cache (val, FTFONT_CACHE_FOR_FACE);
1312 xassert (CONSP (cache));
1313 val = XCDR (cache);
1314 (XSAVE_VALUE (val)->integer)--;
1315 if (XSAVE_VALUE (val)->integer == 0)
1317 struct ftfont_cache_data *cache_data = XSAVE_VALUE (val)->pointer;
1319 FT_Done_Face (cache_data->ft_face);
1320 #ifdef HAVE_LIBOTF
1321 if (ftfont_info->otf)
1322 OTF_close (ftfont_info->otf);
1323 #endif
1324 cache_data->ft_face = NULL;
1326 else
1327 FT_Done_Size (ftfont_info->ft_size);
1330 static int
1331 ftfont_has_char (font, c)
1332 Lisp_Object font;
1333 int c;
1335 struct charset *cs = NULL;
1337 if (EQ (AREF (font, FONT_ADSTYLE_INDEX), Qja)
1338 && charset_jisx0208 >= 0)
1339 cs = CHARSET_FROM_ID (charset_jisx0208);
1340 else if (EQ (AREF (font, FONT_ADSTYLE_INDEX), Qko)
1341 && charset_ksc5601 >= 0)
1342 cs = CHARSET_FROM_ID (charset_ksc5601);
1343 if (cs)
1344 return (ENCODE_CHAR (cs, c) != CHARSET_INVALID_CODE (cs));
1346 if (FONT_ENTITY_P (font))
1348 FcCharSet *charset = ftfont_get_fc_charset (font);
1350 return (FcCharSetHasChar (charset, c) == FcTrue);
1352 else
1354 struct ftfont_info *ftfont_info;
1356 ftfont_info = (struct ftfont_info *) XFONT_OBJECT (font);
1357 return (FT_Get_Char_Index (ftfont_info->ft_size->face, (FT_ULong) c)
1358 != 0);
1362 static unsigned
1363 ftfont_encode_char (font, c)
1364 struct font *font;
1365 int c;
1367 struct ftfont_info *ftfont_info = (struct ftfont_info *) font;
1368 FT_Face ft_face = ftfont_info->ft_size->face;
1369 FT_ULong charcode = c;
1370 FT_UInt code = FT_Get_Char_Index (ft_face, charcode);
1372 return (code > 0 ? code : FONT_INVALID_CODE);
1375 static int
1376 ftfont_text_extents (font, code, nglyphs, metrics)
1377 struct font *font;
1378 unsigned *code;
1379 int nglyphs;
1380 struct font_metrics *metrics;
1382 struct ftfont_info *ftfont_info = (struct ftfont_info *) font;
1383 FT_Face ft_face = ftfont_info->ft_size->face;
1384 int width = 0;
1385 int i, first;
1387 if (ftfont_info->ft_size != ft_face->size)
1388 FT_Activate_Size (ftfont_info->ft_size);
1389 if (metrics)
1390 bzero (metrics, sizeof (struct font_metrics));
1391 for (i = 0, first = 1; i < nglyphs; i++)
1393 if (FT_Load_Glyph (ft_face, code[i], FT_LOAD_DEFAULT) == 0)
1395 FT_Glyph_Metrics *m = &ft_face->glyph->metrics;
1397 if (first)
1399 if (metrics)
1401 metrics->lbearing = m->horiBearingX >> 6;
1402 metrics->rbearing = (m->horiBearingX + m->width) >> 6;
1403 metrics->ascent = m->horiBearingY >> 6;
1404 metrics->descent = (m->height - m->horiBearingY) >> 6;
1406 first = 0;
1408 if (metrics)
1410 if (metrics->lbearing > width + (m->horiBearingX >> 6))
1411 metrics->lbearing = width + (m->horiBearingX >> 6);
1412 if (metrics->rbearing
1413 < width + ((m->horiBearingX + m->width) >> 6))
1414 metrics->rbearing
1415 = width + ((m->horiBearingX + m->width) >> 6);
1416 if (metrics->ascent < (m->horiBearingY >> 6))
1417 metrics->ascent = m->horiBearingY >> 6;
1418 if (metrics->descent > ((m->height - m->horiBearingY) >> 6))
1419 metrics->descent = (m->height - m->horiBearingY) >> 6;
1421 width += m->horiAdvance >> 6;
1423 else
1425 width += font->space_width;
1428 if (metrics)
1429 metrics->width = width;
1431 return width;
1434 static int
1435 ftfont_get_bitmap (font, code, bitmap, bits_per_pixel)
1436 struct font *font;
1437 unsigned code;
1438 struct font_bitmap *bitmap;
1439 int bits_per_pixel;
1441 struct ftfont_info *ftfont_info = (struct ftfont_info *) font;
1442 FT_Face ft_face = ftfont_info->ft_size->face;
1443 FT_Int32 load_flags = FT_LOAD_RENDER;
1445 if (ftfont_info->ft_size != ft_face->size)
1446 FT_Activate_Size (ftfont_info->ft_size);
1447 if (bits_per_pixel == 1)
1449 #ifdef FT_LOAD_TARGET_MONO
1450 load_flags |= FT_LOAD_TARGET_MONO;
1451 #else
1452 load_flags |= FT_LOAD_MONOCHROME;
1453 #endif
1455 else if (bits_per_pixel != 8)
1456 /* We don't support such a rendering. */
1457 return -1;
1459 if (FT_Load_Glyph (ft_face, code, load_flags) != 0)
1460 return -1;
1461 bitmap->bits_per_pixel
1462 = (ft_face->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_MONO ? 1
1463 : ft_face->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_GRAY ? 8
1464 : ft_face->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_LCD ? 8
1465 : ft_face->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_LCD_V ? 8
1466 : -1);
1467 if (bitmap->bits_per_pixel < 0)
1468 /* We don't suport that kind of pixel mode. */
1469 return -1;
1470 bitmap->rows = ft_face->glyph->bitmap.rows;
1471 bitmap->width = ft_face->glyph->bitmap.width;
1472 bitmap->pitch = ft_face->glyph->bitmap.pitch;
1473 bitmap->buffer = ft_face->glyph->bitmap.buffer;
1474 bitmap->left = ft_face->glyph->bitmap_left;
1475 bitmap->top = ft_face->glyph->bitmap_top;
1476 bitmap->advance = ft_face->glyph->metrics.horiAdvance >> 6;
1477 bitmap->extra = NULL;
1479 return 0;
1482 static int
1483 ftfont_anchor_point (font, code, index, x, y)
1484 struct font *font;
1485 unsigned code;
1486 int index;
1487 int *x, *y;
1489 struct ftfont_info *ftfont_info = (struct ftfont_info *) font;
1490 FT_Face ft_face = ftfont_info->ft_size->face;
1492 if (ftfont_info->ft_size != ft_face->size)
1493 FT_Activate_Size (ftfont_info->ft_size);
1494 if (FT_Load_Glyph (ft_face, code, FT_LOAD_DEFAULT) != 0)
1495 return -1;
1496 if (ft_face->glyph->format != FT_GLYPH_FORMAT_OUTLINE)
1497 return -1;
1498 if (index >= ft_face->glyph->outline.n_points)
1499 return -1;
1500 *x = ft_face->glyph->outline.points[index].x;
1501 *y = ft_face->glyph->outline.points[index].y;
1502 return 0;
1505 #ifdef HAVE_LIBOTF
1507 static Lisp_Object
1508 ftfont_otf_features (gsub_gpos)
1509 OTF_GSUB_GPOS *gsub_gpos;
1511 Lisp_Object scripts, langsyses, features, sym;
1512 int i, j, k, l;
1514 for (scripts = Qnil, i = gsub_gpos->ScriptList.ScriptCount - 1; i >= 0; i--)
1516 OTF_Script *otf_script = gsub_gpos->ScriptList.Script + i;
1518 for (langsyses = Qnil, j = otf_script->LangSysCount - 1; j >= -1; j--)
1520 OTF_LangSys *otf_langsys;
1522 if (j >= 0)
1523 otf_langsys = otf_script->LangSys + j;
1524 else if (otf_script->DefaultLangSysOffset)
1525 otf_langsys = &otf_script->DefaultLangSys;
1526 else
1527 break;
1529 for (features = Qnil, k = otf_langsys->FeatureCount - 1; k >= 0; k--)
1531 l = otf_langsys->FeatureIndex[k];
1532 if (l >= gsub_gpos->FeatureList.FeatureCount)
1533 continue;
1534 OTF_TAG_SYM (sym, gsub_gpos->FeatureList.Feature[l].FeatureTag);
1535 features = Fcons (sym, features);
1537 if (j >= 0)
1538 OTF_TAG_SYM (sym, otf_script->LangSysRecord[j].LangSysTag);
1539 else
1540 sym = Qnil;
1541 langsyses = Fcons (Fcons (sym, features), langsyses);
1544 OTF_TAG_SYM (sym, gsub_gpos->ScriptList.Script[i].ScriptTag);
1545 scripts = Fcons (Fcons (sym, langsyses), scripts);
1547 return scripts;
1552 static Lisp_Object
1553 ftfont_otf_capability (font)
1554 struct font *font;
1556 struct ftfont_info *ftfont_info = (struct ftfont_info *) font;
1557 OTF *otf = ftfont_get_otf (ftfont_info);
1558 Lisp_Object gsub_gpos;
1560 if (! otf)
1561 return Qnil;
1562 gsub_gpos = Fcons (Qnil, Qnil);
1563 if (OTF_get_table (otf, "GSUB") == 0
1564 && otf->gsub->FeatureList.FeatureCount > 0)
1565 XSETCAR (gsub_gpos, ftfont_otf_features (otf->gsub));
1566 if (OTF_get_table (otf, "GPOS") == 0
1567 && otf->gpos->FeatureList.FeatureCount > 0)
1568 XSETCDR (gsub_gpos, ftfont_otf_features (otf->gpos));
1569 return gsub_gpos;
1572 #ifdef HAVE_M17N_FLT
1574 struct MFLTFontFT
1576 MFLTFont flt_font;
1577 struct font *font;
1578 FT_Face ft_face;
1579 OTF *otf;
1582 static int
1583 ftfont_get_glyph_id (font, gstring, from, to)
1584 MFLTFont *font;
1585 MFLTGlyphString *gstring;
1586 int from, to;
1588 struct MFLTFontFT *flt_font_ft = (struct MFLTFontFT *) font;
1589 FT_Face ft_face = flt_font_ft->ft_face;
1590 MFLTGlyph *g;
1592 for (g = gstring->glyphs + from; from < to; g++, from++)
1593 if (! g->encoded)
1595 FT_UInt code = FT_Get_Char_Index (ft_face, g->code);
1597 g->code = code > 0 ? code : FONT_INVALID_CODE;
1598 g->encoded = 1;
1600 return 0;
1603 static int
1604 ftfont_get_metrics (font, gstring, from, to)
1605 MFLTFont *font;
1606 MFLTGlyphString *gstring;
1607 int from, to;
1609 struct MFLTFontFT *flt_font_ft = (struct MFLTFontFT *) font;
1610 FT_Face ft_face = flt_font_ft->ft_face;
1611 MFLTGlyph *g;
1613 for (g = gstring->glyphs + from; from < to; g++, from++)
1614 if (! g->measured)
1616 if (g->code != FONT_INVALID_CODE)
1618 FT_Glyph_Metrics *m;
1620 if (FT_Load_Glyph (ft_face, g->code, FT_LOAD_DEFAULT) != 0)
1621 abort ();
1622 m = &ft_face->glyph->metrics;
1624 g->lbearing = m->horiBearingX;
1625 g->rbearing = m->horiBearingX + m->width;
1626 g->ascent = m->horiBearingY;
1627 g->descent = m->height - m->horiBearingY;
1628 g->xadv = m->horiAdvance;
1630 else
1632 g->lbearing = 0;
1633 g->rbearing = g->xadv = flt_font_ft->font->space_width << 6;
1634 g->ascent = flt_font_ft->font->ascent << 6;
1635 g->descent = flt_font_ft->font->descent << 6;
1637 g->yadv = 0;
1638 g->measured = 1;
1640 return 0;
1643 static int
1644 ftfont_check_otf (MFLTFont *font, MFLTOtfSpec *spec)
1646 struct MFLTFontFT *flt_font_ft = (struct MFLTFontFT *) font;
1647 OTF *otf = flt_font_ft->otf;
1648 OTF_Tag *tags;
1649 int i, n, negative;
1651 for (i = 0; i < 2; i++)
1653 if (! spec->features[i])
1654 continue;
1655 for (n = 0; spec->features[i][n]; n++);
1656 tags = alloca (sizeof (OTF_Tag) * n);
1657 for (n = 0, negative = 0; spec->features[i][n]; n++)
1659 if (spec->features[i][n] == 0xFFFFFFFF)
1660 negative = 1;
1661 else if (negative)
1662 tags[n - 1] = spec->features[i][n] | 0x80000000;
1663 else
1664 tags[n] = spec->features[i][n];
1666 if (n - negative > 0
1667 && OTF_check_features (otf, i == 0, spec->script, spec->langsys,
1668 tags, n - negative) != 1)
1669 return 0;
1671 return 1;
1674 #define DEVICE_DELTA(table, size) \
1675 (((size) >= (table).StartSize && (size) <= (table).EndSize) \
1676 ? (table).DeltaValue[(size) - (table).StartSize] << 6 \
1677 : 0)
1679 static void
1680 adjust_anchor (FT_Face ft_face, OTF_Anchor *anchor,
1681 unsigned code, int x_ppem, int y_ppem, int *x, int *y)
1683 if (anchor->AnchorFormat == 2)
1685 FT_Outline *outline;
1686 int ap = anchor->f.f1.AnchorPoint;
1688 FT_Load_Glyph (ft_face, (FT_UInt) code, FT_LOAD_MONOCHROME);
1689 outline = &ft_face->glyph->outline;
1690 if (ap < outline->n_points)
1692 *x = outline->points[ap].x << 6;
1693 *y = outline->points[ap].y << 6;
1696 else if (anchor->AnchorFormat == 3)
1698 if (anchor->f.f2.XDeviceTable.offset
1699 && anchor->f.f2.XDeviceTable.DeltaValue)
1700 *x += DEVICE_DELTA (anchor->f.f2.XDeviceTable, x_ppem);
1701 if (anchor->f.f2.YDeviceTable.offset
1702 && anchor->f.f2.YDeviceTable.DeltaValue)
1703 *y += DEVICE_DELTA (anchor->f.f2.YDeviceTable, y_ppem);
1707 static OTF_GlyphString otf_gstring;
1709 static void
1710 setup_otf_gstring (int size)
1712 if (otf_gstring.size == 0)
1714 otf_gstring.glyphs = (OTF_Glyph *) xmalloc (sizeof (OTF_Glyph) * size);
1715 otf_gstring.size = size;
1717 else if (otf_gstring.size < size)
1719 otf_gstring.glyphs = xrealloc (otf_gstring.glyphs,
1720 sizeof (OTF_Glyph) * size);
1721 otf_gstring.size = size;
1723 otf_gstring.used = size;
1724 memset (otf_gstring.glyphs, 0, sizeof (OTF_Glyph) * size);
1728 static int
1729 ftfont_drive_otf (font, spec, in, from, to, out, adjustment)
1730 MFLTFont *font;
1731 MFLTOtfSpec *spec;
1732 MFLTGlyphString *in;
1733 int from, to;
1734 MFLTGlyphString *out;
1735 MFLTGlyphAdjustment *adjustment;
1737 struct MFLTFontFT *flt_font_ft = (struct MFLTFontFT *) font;
1738 FT_Face ft_face = flt_font_ft->ft_face;
1739 OTF *otf = flt_font_ft->otf;
1740 int len = to - from;
1741 int i, j, gidx;
1742 OTF_Glyph *otfg;
1743 char script[5], *langsys = NULL;
1744 char *gsub_features = NULL, *gpos_features = NULL;
1746 if (len == 0)
1747 return from;
1748 OTF_tag_name (spec->script, script);
1749 if (spec->langsys)
1751 langsys = alloca (5);
1752 OTF_tag_name (spec->langsys, langsys);
1754 for (i = 0; i < 2; i++)
1756 char *p;
1758 if (spec->features[i] && spec->features[i][1] != 0xFFFFFFFF)
1760 for (j = 0; spec->features[i][j]; j++);
1761 if (i == 0)
1762 p = gsub_features = alloca (6 * j);
1763 else
1764 p = gpos_features = alloca (6 * j);
1765 for (j = 0; spec->features[i][j]; j++)
1767 if (spec->features[i][j] == 0xFFFFFFFF)
1768 *p++ = '*', *p++ = ',';
1769 else
1771 OTF_tag_name (spec->features[i][j], p);
1772 p[4] = ',';
1773 p += 5;
1776 *--p = '\0';
1780 setup_otf_gstring (len);
1781 for (i = 0; i < len; i++)
1783 otf_gstring.glyphs[i].c = in->glyphs[from + i].c;
1784 otf_gstring.glyphs[i].glyph_id = in->glyphs[from + i].code;
1787 OTF_drive_gdef (otf, &otf_gstring);
1788 gidx = out->used;
1790 if (gsub_features)
1792 if (OTF_drive_gsub (otf, &otf_gstring, script, langsys, gsub_features)
1793 < 0)
1794 goto simple_copy;
1795 if (out->allocated < out->used + otf_gstring.used)
1796 return -2;
1797 for (i = 0, otfg = otf_gstring.glyphs; i < otf_gstring.used; )
1799 MFLTGlyph *g;
1800 int min_from, max_to;
1801 int j;
1803 g = out->glyphs + out->used;
1804 *g = in->glyphs[from + otfg->f.index.from];
1805 if (g->code != otfg->glyph_id)
1807 g->c = 0;
1808 g->code = otfg->glyph_id;
1809 g->measured = 0;
1811 out->used++;
1812 min_from = g->from;
1813 max_to = g->to;
1814 if (otfg->f.index.from < otfg->f.index.to)
1816 /* OTFG substitutes multiple glyphs in IN. */
1817 for (j = from + otfg->f.index.from + 1;
1818 j <= from + otfg->f.index.to; j++)
1820 if (min_from > in->glyphs[j].from)
1821 min_from = in->glyphs[j].from;
1822 if (max_to < in->glyphs[j].to)
1823 max_to = in->glyphs[j].to;
1825 g->from = min_from;
1826 g->to = max_to;
1828 for (i++, otfg++; (i < otf_gstring.used
1829 && otfg->f.index.from == otfg[-1].f.index.from);
1830 i++, otfg++)
1832 g = out->glyphs + out->used;
1833 *g = in->glyphs[from + otfg->f.index.to];
1834 if (g->code != otfg->glyph_id)
1836 g->c = 0;
1837 g->code = otfg->glyph_id;
1838 g->measured = 0;
1840 out->used++;
1844 else
1846 if (out->allocated < out->used + len)
1847 return -2;
1848 for (i = 0; i < len; i++)
1849 out->glyphs[out->used++] = in->glyphs[from + i];
1852 if (gpos_features)
1854 MFLTGlyph *base = NULL, *mark = NULL, *g;
1855 int x_ppem, y_ppem, x_scale, y_scale;
1857 if (OTF_drive_gpos (otf, &otf_gstring, script, langsys, gpos_features)
1858 < 0)
1859 return to;
1861 x_ppem = ft_face->size->metrics.x_ppem;
1862 y_ppem = ft_face->size->metrics.y_ppem;
1863 x_scale = ft_face->size->metrics.x_scale;
1864 y_scale = ft_face->size->metrics.y_scale;
1866 for (i = 0, otfg = otf_gstring.glyphs, g = out->glyphs + gidx;
1867 i < otf_gstring.used; i++, otfg++, g++)
1869 MFLTGlyph *prev;
1871 if (! otfg->glyph_id)
1872 continue;
1873 switch (otfg->positioning_type)
1875 case 0:
1876 break;
1877 case 1: /* Single */
1878 case 2: /* Pair */
1880 int format = otfg->f.f1.format;
1882 if (format & OTF_XPlacement)
1883 adjustment[i].xoff
1884 = otfg->f.f1.value->XPlacement * x_scale / 0x10000;
1885 if (format & OTF_XPlaDevice)
1886 adjustment[i].xoff
1887 += DEVICE_DELTA (otfg->f.f1.value->XPlaDevice, x_ppem);
1888 if (format & OTF_YPlacement)
1889 adjustment[i].yoff
1890 = - (otfg->f.f1.value->YPlacement * y_scale / 0x10000);
1891 if (format & OTF_YPlaDevice)
1892 adjustment[i].yoff
1893 -= DEVICE_DELTA (otfg->f.f1.value->YPlaDevice, y_ppem);
1894 if (format & OTF_XAdvance)
1895 adjustment[i].xadv
1896 += otfg->f.f1.value->XAdvance * x_scale / 0x10000;
1897 if (format & OTF_XAdvDevice)
1898 adjustment[i].xadv
1899 += DEVICE_DELTA (otfg->f.f1.value->XAdvDevice, x_ppem);
1900 if (format & OTF_YAdvance)
1901 adjustment[i].yadv
1902 += otfg->f.f1.value->YAdvance * y_scale / 0x10000;
1903 if (format & OTF_YAdvDevice)
1904 adjustment[i].yadv
1905 += DEVICE_DELTA (otfg->f.f1.value->YAdvDevice, y_ppem);
1906 adjustment[i].set = 1;
1908 break;
1909 case 3: /* Cursive */
1910 /* Not yet supported. */
1911 break;
1912 case 4: /* Mark-to-Base */
1913 case 5: /* Mark-to-Ligature */
1914 if (! base)
1915 break;
1916 prev = base;
1917 goto label_adjust_anchor;
1918 default: /* i.e. case 6 Mark-to-Mark */
1919 if (! mark)
1920 break;
1921 prev = mark;
1923 label_adjust_anchor:
1925 int base_x, base_y, mark_x, mark_y;
1926 int this_from, this_to;
1928 base_x = otfg->f.f4.base_anchor->XCoordinate * x_scale / 0x10000;
1929 base_y = otfg->f.f4.base_anchor->YCoordinate * y_scale / 0x10000;
1930 mark_x = otfg->f.f4.mark_anchor->XCoordinate * x_scale / 0x10000;
1931 mark_y = otfg->f.f4.mark_anchor->YCoordinate * y_scale / 0x10000;
1933 if (otfg->f.f4.base_anchor->AnchorFormat != 1)
1934 adjust_anchor (ft_face, otfg->f.f4.base_anchor,
1935 prev->code, x_ppem, y_ppem, &base_x, &base_y);
1936 if (otfg->f.f4.mark_anchor->AnchorFormat != 1)
1937 adjust_anchor (ft_face, otfg->f.f4.mark_anchor, g->code,
1938 x_ppem, y_ppem, &mark_x, &mark_y);
1939 adjustment[i].xoff = (base_x - mark_x);
1940 adjustment[i].yoff = - (base_y - mark_y);
1941 adjustment[i].back = (g - prev);
1942 adjustment[i].xadv = 0;
1943 adjustment[i].advance_is_absolute = 1;
1944 adjustment[i].set = 1;
1945 this_from = g->from;
1946 this_to = g->to;
1947 for (j = 0; prev + j < g; j++)
1949 if (this_from > prev[j].from)
1950 this_from = prev[j].from;
1951 if (this_to < prev[j].to)
1952 this_to = prev[j].to;
1954 for (; prev <= g; prev++)
1956 prev->from = this_from;
1957 prev->to = this_to;
1961 if (otfg->GlyphClass == OTF_GlyphClass0)
1962 base = mark = g;
1963 else if (otfg->GlyphClass == OTF_GlyphClassMark)
1964 mark = g;
1965 else
1966 base = g;
1969 return to;
1971 simple_copy:
1972 if (out->allocated < out->used + len)
1973 return -2;
1974 font->get_metrics (font, in, from, to);
1975 memcpy (out->glyphs + out->used, in->glyphs + from,
1976 sizeof (MFLTGlyph) * len);
1977 out->used += len;
1978 return to;
1981 static MFLTGlyphString gstring;
1983 static int m17n_flt_initialized;
1985 extern Lisp_Object QCfamily;
1987 static Lisp_Object
1988 ftfont_shape_by_flt (lgstring, font, ft_face, otf)
1989 Lisp_Object lgstring;
1990 struct font *font;
1991 FT_Face ft_face;
1992 OTF *otf;
1994 EMACS_UINT len = LGSTRING_GLYPH_LEN (lgstring);
1995 EMACS_UINT i;
1996 struct MFLTFontFT flt_font_ft;
1997 MFLT *flt = NULL;
1998 int with_variation_selector = 0;
2000 if (! m17n_flt_initialized)
2002 M17N_INIT ();
2003 m17n_flt_initialized = 1;
2006 for (i = 0; i < len; i++)
2008 Lisp_Object g = LGSTRING_GLYPH (lgstring, i);
2009 int c;
2011 if (NILP (g))
2012 break;
2013 c = LGLYPH_CHAR (g);
2014 if (CHAR_VARIATION_SELECTOR_P (c))
2015 with_variation_selector++;
2017 len = i;
2018 if (with_variation_selector)
2020 setup_otf_gstring (len);
2021 for (i = 0; i < len; i++)
2023 Lisp_Object g = LGSTRING_GLYPH (lgstring, i);
2025 otf_gstring.glyphs[i].c = LGLYPH_CHAR (g);
2026 otf_gstring.glyphs[i].f.index.from = LGLYPH_FROM (g);
2027 otf_gstring.glyphs[i].f.index.to = LGLYPH_TO (g);
2029 OTF_drive_cmap (otf, &otf_gstring);
2030 for (i = 0; i < otf_gstring.used; i++)
2032 OTF_Glyph *otfg = otf_gstring.glyphs + i;
2033 Lisp_Object g0 = LGSTRING_GLYPH (lgstring, otfg->f.index.from);
2034 Lisp_Object g1 = LGSTRING_GLYPH (lgstring, otfg->f.index.to);
2036 LGLYPH_SET_CODE (g0, otfg->glyph_id);
2037 LGLYPH_SET_TO (g0, LGLYPH_TO (g1));
2038 LGSTRING_SET_GLYPH (lgstring, i, g0);
2040 if (len > otf_gstring.used)
2042 len = otf_gstring.used;
2043 LGSTRING_SET_GLYPH (lgstring, len, Qnil);
2047 if (gstring.allocated == 0)
2049 gstring.allocated = len * 2;
2050 gstring.glyph_size = sizeof (MFLTGlyph);
2051 gstring.glyphs = xmalloc (sizeof (MFLTGlyph) * gstring.allocated);
2053 else if (gstring.allocated < len * 2)
2055 gstring.allocated = len * 2;
2056 gstring.glyphs = xrealloc (gstring.glyphs,
2057 sizeof (MFLTGlyph) * gstring.allocated);
2059 memset (gstring.glyphs, 0, sizeof (MFLTGlyph) * len);
2060 for (i = 0; i < len; i++)
2062 Lisp_Object g = LGSTRING_GLYPH (lgstring, i);
2064 gstring.glyphs[i].c = LGLYPH_CHAR (g);
2065 if (with_variation_selector)
2067 gstring.glyphs[i].code = LGLYPH_CODE (g);
2068 gstring.glyphs[i].encoded = 1;
2072 gstring.used = len;
2073 gstring.r2l = 0;
2076 Lisp_Object family = Ffont_get (LGSTRING_FONT (lgstring), QCfamily);
2078 if (NILP (family))
2079 flt_font_ft.flt_font.family = Mnil;
2080 else
2081 flt_font_ft.flt_font.family
2082 = msymbol ((char *) SDATA (Fdowncase (SYMBOL_NAME (family))));
2084 flt_font_ft.flt_font.x_ppem = ft_face->size->metrics.x_ppem;
2085 flt_font_ft.flt_font.y_ppem = ft_face->size->metrics.y_ppem;
2086 flt_font_ft.flt_font.get_glyph_id = ftfont_get_glyph_id;
2087 flt_font_ft.flt_font.get_metrics = ftfont_get_metrics;
2088 flt_font_ft.flt_font.check_otf = ftfont_check_otf;
2089 flt_font_ft.flt_font.drive_otf = ftfont_drive_otf;
2090 flt_font_ft.flt_font.internal = NULL;
2091 flt_font_ft.font = font;
2092 flt_font_ft.ft_face = ft_face;
2093 flt_font_ft.otf = otf;
2094 if (len > 1
2095 && gstring.glyphs[1].c >= 0x300 && gstring.glyphs[1].c <= 0x36F)
2096 /* A little bit ad hoc. Perhaps, shaper must get script and
2097 language information, and select a proper flt for them
2098 here. */
2099 flt = mflt_get (msymbol ("combining"));
2100 for (i = 0; i < 3; i++)
2102 int result = mflt_run (&gstring, 0, len, &flt_font_ft.flt_font, flt);
2103 if (result != -2)
2104 break;
2105 gstring.allocated += gstring.allocated;
2106 gstring.glyphs = xrealloc (gstring.glyphs,
2107 sizeof (MFLTGlyph) * gstring.allocated);
2109 if (gstring.used > LGSTRING_GLYPH_LEN (lgstring))
2110 return Qnil;
2111 for (i = 0; i < gstring.used; i++)
2113 MFLTGlyph *g = gstring.glyphs + i;
2115 g->from = LGLYPH_FROM (LGSTRING_GLYPH (lgstring, g->from));
2116 g->to = LGLYPH_TO (LGSTRING_GLYPH (lgstring, g->to));
2119 for (i = 0; i < gstring.used; i++)
2121 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
2122 MFLTGlyph *g = gstring.glyphs + i;
2124 if (NILP (lglyph))
2126 lglyph = Fmake_vector (make_number (LGLYPH_SIZE), Qnil);
2127 LGSTRING_SET_GLYPH (lgstring, i, lglyph);
2129 LGLYPH_SET_FROM (lglyph, g->from);
2130 LGLYPH_SET_TO (lglyph, g->to);
2131 LGLYPH_SET_CHAR (lglyph, g->c);
2132 LGLYPH_SET_CODE (lglyph, g->code);
2133 LGLYPH_SET_WIDTH (lglyph, g->xadv >> 6);
2134 LGLYPH_SET_LBEARING (lglyph, g->lbearing >> 6);
2135 LGLYPH_SET_RBEARING (lglyph, g->rbearing >> 6);
2136 LGLYPH_SET_ASCENT (lglyph, g->ascent >> 6);
2137 LGLYPH_SET_DESCENT (lglyph, g->descent >> 6);
2138 if (g->adjusted)
2140 Lisp_Object vec;
2142 vec = Fmake_vector (make_number (3), Qnil);
2143 ASET (vec, 0, make_number (g->xoff >> 6));
2144 ASET (vec, 1, make_number (g->yoff >> 6));
2145 ASET (vec, 2, make_number (g->xadv >> 6));
2146 LGLYPH_SET_ADJUSTMENT (lglyph, vec);
2149 return make_number (i);
2152 Lisp_Object
2153 ftfont_shape (lgstring)
2154 Lisp_Object lgstring;
2156 struct font *font;
2157 struct ftfont_info *ftfont_info;
2158 OTF *otf;
2160 CHECK_FONT_GET_OBJECT (LGSTRING_FONT (lgstring), font);
2161 ftfont_info = (struct ftfont_info *) font;
2162 otf = ftfont_get_otf (ftfont_info);
2163 if (! otf)
2164 return make_number (0);
2165 return ftfont_shape_by_flt (lgstring, font, ftfont_info->ft_size->face, otf);
2168 #endif /* HAVE_M17N_FLT */
2170 #ifdef HAVE_OTF_GET_VARIATION_GLYPHS
2172 static int
2173 ftfont_variation_glyphs (font, c, variations)
2174 struct font *font;
2175 int c;
2176 unsigned variations[256];
2178 struct ftfont_info *ftfont_info = (struct ftfont_info *) font;
2179 OTF *otf = ftfont_get_otf (ftfont_info);
2181 if (! otf)
2182 return 0;
2183 return OTF_get_variation_glyphs (otf, c, variations);
2186 #endif /* HAVE_OTF_GET_VARIATION_GLYPHS */
2187 #endif /* HAVE_LIBOTF */
2189 Lisp_Object
2190 ftfont_font_format (FcPattern *pattern, Lisp_Object filename)
2192 FcChar8 *str;
2194 #ifdef FC_FONTFORMAT
2195 if (pattern)
2197 if (FcPatternGetString (pattern, FC_FONTFORMAT, 0, &str) != FcResultMatch)
2198 return Qnil;
2199 if (strcmp ((char *) str, "TrueType") == 0)
2200 return intern ("truetype");
2201 if (strcmp ((char *) str, "Type 1") == 0)
2202 return intern ("type1");
2203 if (strcmp ((char *) str, "PCF") == 0)
2204 return intern ("pcf");
2205 if (strcmp ((char *) str, "BDF") == 0)
2206 return intern ("bdf");
2208 #endif /* FC_FONTFORMAT */
2209 if (STRINGP (filename))
2211 int len = SBYTES (filename);
2213 if (len >= 4)
2215 str = (FcChar8 *) (SDATA (filename) + len - 4);
2216 if (xstrcasecmp ((char *) str, ".ttf") == 0)
2217 return intern ("truetype");
2218 if (xstrcasecmp ((char *) str, ".pfb") == 0)
2219 return intern ("type1");
2220 if (xstrcasecmp ((char *) str, ".pcf") == 0)
2221 return intern ("pcf");
2222 if (xstrcasecmp ((char *) str, ".bdf") == 0)
2223 return intern ("bdf");
2226 return intern ("unknown");
2230 void
2231 syms_of_ftfont ()
2233 DEFSYM (Qfreetype, "freetype");
2234 DEFSYM (Qmonospace, "monospace");
2235 DEFSYM (Qsans_serif, "sans-serif");
2236 DEFSYM (Qserif, "serif");
2237 DEFSYM (Qmono, "mono");
2238 DEFSYM (Qsans, "sans");
2239 DEFSYM (Qsans__serif, "sans serif");
2241 staticpro (&freetype_font_cache);
2242 freetype_font_cache = Fcons (Qt, Qnil);
2244 staticpro (&ftfont_generic_family_list);
2245 ftfont_generic_family_list
2246 = Fcons (Fcons (Qmonospace, Qt),
2247 Fcons (Fcons (Qsans_serif, Qt),
2248 Fcons (Fcons (Qsans, Qt), Qnil)));
2250 staticpro (&ft_face_cache);
2251 ft_face_cache = Qnil;
2253 ftfont_driver.type = Qfreetype;
2254 register_font_driver (&ftfont_driver, NULL);
2257 /* arch-tag: 7cfa432c-33a6-4988-83d2-a82ed8604aca
2258 (do not change this comment) */