1 /* ftfont.c -- FreeType font driver.
2 Copyright (C) 2006-2014 Free Software Foundation, Inc.
3 Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011
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/>. */
24 #include <fontconfig/fontconfig.h>
25 #include <fontconfig/fcfreetype.h>
27 #include <c-strcase.h>
30 #include "dispextern.h"
32 #include "blockinput.h"
33 #include "character.h"
36 #include "composite.h"
41 /* Symbolic type of this font-driver. */
42 static 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 bool 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 cast to struct
66 /* The following four members must be here in this order to be
67 compatible with struct xftfont_info (in xftfont.c). */
68 bool maybe_otf
; /* Flag to tell if this may be OTF or not. */
70 #endif /* HAVE_LIBOTF */
78 FTFONT_CACHE_FOR_FACE
,
79 FTFONT_CACHE_FOR_CHARSET
,
80 FTFONT_CACHE_FOR_ENTITY
83 static Lisp_Object
ftfont_pattern_entity (FcPattern
*, Lisp_Object
);
85 static Lisp_Object
ftfont_resolve_generic_family (Lisp_Object
,
87 static Lisp_Object
ftfont_lookup_cache (Lisp_Object
,
88 enum ftfont_cache_for
);
90 static void ftfont_filter_properties (Lisp_Object font
, Lisp_Object alist
);
92 #define SYMBOL_FcChar8(SYM) (FcChar8 *) SDATA (SYMBOL_NAME (SYM))
98 /* characters to distinguish the charset from the others */
100 /* additional constraint by language */
103 FcCharSet
*fc_charset
;
104 } fc_charset_table
[] =
105 { { "iso8859-1", { 0x00A0, 0x00A1, 0x00B4, 0x00BC, 0x00D0 } },
106 { "iso8859-2", { 0x00A0, 0x010E }},
107 { "iso8859-3", { 0x00A0, 0x0108 }},
108 { "iso8859-4", { 0x00A0, 0x00AF, 0x0128, 0x0156, 0x02C7 }},
109 { "iso8859-5", { 0x00A0, 0x0401 }},
110 { "iso8859-6", { 0x00A0, 0x060C }},
111 { "iso8859-7", { 0x00A0, 0x0384 }},
112 { "iso8859-8", { 0x00A0, 0x05D0 }},
113 { "iso8859-9", { 0x00A0, 0x00A1, 0x00BC, 0x011E }},
114 { "iso8859-10", { 0x00A0, 0x00D0, 0x0128, 0x2015 }},
115 { "iso8859-11", { 0x00A0, 0x0E01 }},
116 { "iso8859-13", { 0x00A0, 0x201C }},
117 { "iso8859-14", { 0x00A0, 0x0174 }},
118 { "iso8859-15", { 0x00A0, 0x00A1, 0x00D0, 0x0152 }},
119 { "iso8859-16", { 0x00A0, 0x0218}},
120 { "gb2312.1980-0", { 0x4E13 }, "zh-cn"},
121 { "big5-0", { 0xF6B1 }, "zh-tw" },
122 { "jisx0208.1983-0", { 0x4E55 }, "ja"},
123 { "ksc5601.1985-0", { 0xAC00 }, "ko"},
124 { "cns11643.1992-1", { 0xFE32 }, "zh-tw"},
125 { "cns11643.1992-2", { 0x4E33, 0x7934 }},
126 { "cns11643.1992-3", { 0x201A9 }},
127 { "cns11643.1992-4", { 0x20057 }},
128 { "cns11643.1992-5", { 0x20000 }},
129 { "cns11643.1992-6", { 0x20003 }},
130 { "cns11643.1992-7", { 0x20055 }},
131 { "gbk-0", { 0x4E06 }, "zh-cn"},
132 { "jisx0212.1990-0", { 0x4E44 }},
133 { "jisx0213.2000-1", { 0xFA10 }, "ja"},
134 { "jisx0213.2000-2", { 0xFA49 }},
135 { "jisx0213.2004-1", { 0x20B9F }},
136 { "viscii1.1-1", { 0x1EA0, 0x1EAE, 0x1ED2 }, "vi"},
137 { "tis620.2529-1", { 0x0E01 }, "th"},
138 { "windows-1251", { 0x0401, 0x0490 }, "ru"},
139 { "koi8-r", { 0x0401, 0x2219 }, "ru"},
140 { "mulelao-1", { 0x0E81 }, "lo"},
141 { "unicode-sip", { 0x20000 }},
146 matching_prefix (char const *str
, ptrdiff_t len
, char const *pat
)
148 return len
== strlen (pat
) && c_strncasecmp (str
, pat
, len
) == 0;
151 /* Dirty hack for handing ADSTYLE property.
153 Fontconfig (actually the underlying FreeType) gives such ADSTYLE
154 font property of PCF/BDF fonts in FC_STYLE. And, "Bold",
155 "Oblique", "Italic", or any non-normal SWIDTH property names
156 (e.g. SemiCondensed) are appended. In addition, if there's no
157 ADSTYLE property nor non-normal WEIGHT/SLANT/SWIDTH properties,
158 "Regular" is used for FC_STYLE (see the function
159 pcf_interpret_style in src/pcf/pcfread.c of FreeType).
161 Unfortunately this behavior is not documented, so the following
162 code may fail if FreeType changes the behavior in the future. */
165 get_adstyle_property (FcPattern
*p
)
172 if ((FcPatternGetString (p
, FC_FONTFORMAT
, 0, &fcstr
) == FcResultMatch
)
173 && xstrcasecmp ((char *) fcstr
, "bdf") != 0
174 && xstrcasecmp ((char *) fcstr
, "pcf") != 0)
175 /* Not a BDF nor PCF font. */
178 if (FcPatternGetString (p
, FC_STYLE
, 0, &fcstr
) != FcResultMatch
)
180 str
= (char *) fcstr
;
181 for (end
= str
; *end
&& *end
!= ' '; end
++);
182 if (matching_prefix (str
, end
- str
, "Regular")
183 || matching_prefix (str
, end
- str
, "Bold")
184 || matching_prefix (str
, end
- str
, "Oblique")
185 || matching_prefix (str
, end
- str
, "Italic"))
187 adstyle
= font_intern_prop (str
, end
- str
, 1);
188 if (font_style_to_value (FONT_WIDTH_INDEX
, adstyle
, 0) >= 0)
194 ftfont_pattern_entity (FcPattern
*p
, Lisp_Object extra
)
196 Lisp_Object key
, cache
, entity
;
204 if (FcPatternGetString (p
, FC_FILE
, 0, &str
) != FcResultMatch
)
206 if (FcPatternGetInteger (p
, FC_INDEX
, 0, &idx
) != FcResultMatch
)
210 key
= Fcons (build_unibyte_string (file
), make_number (idx
));
211 cache
= ftfont_lookup_cache (key
, FTFONT_CACHE_FOR_ENTITY
);
212 entity
= XCAR (cache
);
215 Lisp_Object val
= font_make_entity ();
218 for (i
= 0; i
< FONT_OBJLIST_INDEX
; i
++)
219 ASET (val
, i
, AREF (entity
, i
));
221 ASET (val
, FONT_EXTRA_INDEX
, Fcopy_sequence (extra
));
222 font_put_extra (val
, QCfont_entity
, key
);
226 entity
= font_make_entity ();
227 XSETCAR (cache
, entity
);
229 ASET (entity
, FONT_TYPE_INDEX
, Qfreetype
);
230 ASET (entity
, FONT_REGISTRY_INDEX
, Qiso10646_1
);
232 if (FcPatternGetString (p
, FC_FOUNDRY
, 0, &str
) == FcResultMatch
)
234 char *s
= (char *) str
;
235 ASET (entity
, FONT_FOUNDRY_INDEX
, font_intern_prop (s
, strlen (s
), 1));
237 if (FcPatternGetString (p
, FC_FAMILY
, 0, &str
) == FcResultMatch
)
239 char *s
= (char *) str
;
240 ASET (entity
, FONT_FAMILY_INDEX
, font_intern_prop (s
, strlen (s
), 1));
242 if (FcPatternGetInteger (p
, FC_WEIGHT
, 0, &numeric
) == FcResultMatch
)
244 if (numeric
>= FC_WEIGHT_REGULAR
&& numeric
< FC_WEIGHT_MEDIUM
)
245 numeric
= FC_WEIGHT_MEDIUM
;
246 FONT_SET_STYLE (entity
, FONT_WEIGHT_INDEX
, make_number (numeric
));
248 if (FcPatternGetInteger (p
, FC_SLANT
, 0, &numeric
) == FcResultMatch
)
251 FONT_SET_STYLE (entity
, FONT_SLANT_INDEX
, make_number (numeric
));
253 if (FcPatternGetInteger (p
, FC_WIDTH
, 0, &numeric
) == FcResultMatch
)
255 FONT_SET_STYLE (entity
, FONT_WIDTH_INDEX
, make_number (numeric
));
257 if (FcPatternGetDouble (p
, FC_PIXEL_SIZE
, 0, &dbl
) == FcResultMatch
)
259 ASET (entity
, FONT_SIZE_INDEX
, make_number (dbl
));
262 ASET (entity
, FONT_SIZE_INDEX
, make_number (0));
263 if (FcPatternGetInteger (p
, FC_SPACING
, 0, &numeric
) == FcResultMatch
)
264 ASET (entity
, FONT_SPACING_INDEX
, make_number (numeric
));
265 if (FcPatternGetDouble (p
, FC_DPI
, 0, &dbl
) == FcResultMatch
)
268 ASET (entity
, FONT_DPI_INDEX
, make_number (dpi
));
270 if (FcPatternGetBool (p
, FC_SCALABLE
, 0, &b
) == FcResultMatch
273 ASET (entity
, FONT_SIZE_INDEX
, make_number (0));
274 ASET (entity
, FONT_AVGWIDTH_INDEX
, make_number (0));
278 /* As this font is not scalable, perhaps this is a BDF or PCF
282 ASET (entity
, FONT_ADSTYLE_INDEX
, get_adstyle_property (p
));
283 if ((ft_library
|| FT_Init_FreeType (&ft_library
) == 0)
284 && FT_New_Face (ft_library
, file
, idx
, &ft_face
) == 0)
288 if (FT_Get_BDF_Property (ft_face
, "AVERAGE_WIDTH", &rec
) == 0
289 && rec
.type
== BDF_PROPERTY_TYPE_INTEGER
)
290 ASET (entity
, FONT_AVGWIDTH_INDEX
, make_number (rec
.u
.integer
));
291 FT_Done_Face (ft_face
);
295 ASET (entity
, FONT_EXTRA_INDEX
, Fcopy_sequence (extra
));
296 font_put_extra (entity
, QCfont_entity
, key
);
301 static Lisp_Object ftfont_generic_family_list
;
304 ftfont_resolve_generic_family (Lisp_Object family
, FcPattern
*pattern
)
311 family
= Fintern (Fdowncase (SYMBOL_NAME (family
)), Qnil
);
312 if (EQ (family
, Qmono
))
314 else if (EQ (family
, Qsans
) || EQ (family
, Qsans__serif
))
315 family
= Qsans_serif
;
316 slot
= assq_no_quit (family
, ftfont_generic_family_list
);
319 if (! EQ (XCDR (slot
), Qt
))
321 pattern
= FcPatternDuplicate (pattern
);
324 FcPatternDel (pattern
, FC_FOUNDRY
);
325 FcPatternDel (pattern
, FC_FAMILY
);
326 FcPatternAddString (pattern
, FC_FAMILY
, SYMBOL_FcChar8 (family
));
327 if (FcPatternGetLangSet (pattern
, FC_LANG
, 0, &langset
) != FcResultMatch
)
329 /* This is to avoid the effect of locale. */
330 static const FcChar8 lang
[] = "en";
331 langset
= FcLangSetCreate ();
332 FcLangSetAdd (langset
, lang
);
333 FcPatternAddLangSet (pattern
, FC_LANG
, langset
);
334 FcLangSetDestroy (langset
);
336 FcConfigSubstitute (NULL
, pattern
, FcMatchPattern
);
337 FcDefaultSubstitute (pattern
);
338 match
= FcFontMatch (NULL
, pattern
, &result
);
343 if (FcPatternGetString (match
, FC_FAMILY
, 0, &fam
) == FcResultMatch
)
344 family
= intern ((char *) fam
);
348 XSETCDR (slot
, family
);
349 if (match
) FcPatternDestroy (match
);
351 if (pattern
) FcPatternDestroy (pattern
);
355 struct ftfont_cache_data
358 FcCharSet
*fc_charset
;
362 ftfont_lookup_cache (Lisp_Object key
, enum ftfont_cache_for cache_for
)
364 Lisp_Object cache
, val
, entity
;
365 struct ftfont_cache_data
*cache_data
;
367 if (FONT_ENTITY_P (key
))
370 val
= assq_no_quit (QCfont_entity
, AREF (entity
, FONT_EXTRA_INDEX
));
371 eassert (CONSP (val
));
377 if (NILP (ft_face_cache
))
380 cache
= Fgethash (key
, ft_face_cache
, Qnil
);
383 if (NILP (ft_face_cache
))
389 ft_face_cache
= Fmake_hash_table (2, args
);
391 cache_data
= xmalloc (sizeof *cache_data
);
392 cache_data
->ft_face
= NULL
;
393 cache_data
->fc_charset
= NULL
;
394 val
= make_save_ptr_int (cache_data
, 0);
395 cache
= Fcons (Qnil
, val
);
396 Fputhash (key
, cache
, ft_face_cache
);
401 cache_data
= XSAVE_POINTER (val
, 0);
404 if (cache_for
== FTFONT_CACHE_FOR_ENTITY
)
407 if (cache_for
== FTFONT_CACHE_FOR_FACE
408 ? ! cache_data
->ft_face
: ! cache_data
->fc_charset
)
410 char *filename
= SSDATA (XCAR (key
));
411 int idx
= XINT (XCDR (key
));
413 if (cache_for
== FTFONT_CACHE_FOR_FACE
)
416 && FT_Init_FreeType (&ft_library
) != 0)
418 if (FT_New_Face (ft_library
, filename
, idx
, &cache_data
->ft_face
)
424 FcPattern
*pat
= NULL
;
425 FcFontSet
*fontset
= NULL
;
426 FcObjectSet
*objset
= NULL
;
427 FcCharSet
*charset
= NULL
;
429 pat
= FcPatternBuild (0, FC_FILE
, FcTypeString
, (FcChar8
*) filename
,
430 FC_INDEX
, FcTypeInteger
, idx
, NULL
);
433 objset
= FcObjectSetBuild (FC_CHARSET
, FC_STYLE
, NULL
);
436 fontset
= FcFontList (NULL
, pat
, objset
);
439 if (fontset
&& fontset
->nfont
> 0
440 && (FcPatternGetCharSet (fontset
->fonts
[0], FC_CHARSET
, 0,
443 cache_data
->fc_charset
= FcCharSetCopy (charset
);
445 cache_data
->fc_charset
= FcCharSetCreate ();
449 FcFontSetDestroy (fontset
);
451 FcObjectSetDestroy (objset
);
453 FcPatternDestroy (pat
);
460 ftfont_get_fc_charset (Lisp_Object entity
)
462 Lisp_Object val
, cache
;
463 struct ftfont_cache_data
*cache_data
;
465 cache
= ftfont_lookup_cache (entity
, FTFONT_CACHE_FOR_CHARSET
);
467 cache_data
= XSAVE_POINTER (val
, 0);
468 return cache_data
->fc_charset
;
473 ftfont_get_otf (struct ftfont_info
*ftfont_info
)
477 if (ftfont_info
->otf
)
478 return ftfont_info
->otf
;
479 if (! ftfont_info
->maybe_otf
)
481 otf
= OTF_open_ft_face (ftfont_info
->ft_size
->face
);
482 if (! otf
|| OTF_get_table (otf
, "head") < 0)
486 ftfont_info
->maybe_otf
= 0;
489 ftfont_info
->otf
= otf
;
492 #endif /* HAVE_LIBOTF */
494 static Lisp_Object
ftfont_get_cache (struct frame
*);
495 static Lisp_Object
ftfont_list (struct frame
*, Lisp_Object
);
496 static Lisp_Object
ftfont_match (struct frame
*, Lisp_Object
);
497 static Lisp_Object
ftfont_list_family (struct frame
*);
498 static Lisp_Object
ftfont_open (struct frame
*, Lisp_Object
, int);
499 static void ftfont_close (struct font
*);
500 static int ftfont_has_char (Lisp_Object
, int);
501 static unsigned ftfont_encode_char (struct font
*, int);
502 static void ftfont_text_extents (struct font
*, unsigned *, int,
503 struct font_metrics
*);
504 static int ftfont_get_bitmap (struct font
*, unsigned,
505 struct font_bitmap
*, int);
506 static int ftfont_anchor_point (struct font
*, unsigned, int,
509 static Lisp_Object
ftfont_otf_capability (struct font
*);
510 # ifdef HAVE_M17N_FLT
511 static Lisp_Object
ftfont_shape (Lisp_Object
);
515 #ifdef HAVE_OTF_GET_VARIATION_GLYPHS
516 static int ftfont_variation_glyphs (struct font
*, int c
,
517 unsigned variations
[256]);
518 #endif /* HAVE_OTF_GET_VARIATION_GLYPHS */
520 struct font_driver ftfont_driver
=
522 LISP_INITIALLY_ZERO
, /* Qfreetype */
523 0, /* case insensitive */
528 NULL
, /* free_entity */
531 /* We can't draw a text without device dependent functions. */
532 NULL
, /* prepare_face */
533 NULL
, /* done_face */
537 /* We can't draw a text without device dependent functions. */
540 NULL
, /* free_bitmap */
543 ftfont_otf_capability
,
544 #else /* not HAVE_LIBOTF */
546 #endif /* not HAVE_LIBOTF */
547 NULL
, /* otf_drive */
548 NULL
, /* start_for_frame */
549 NULL
, /* end_for_frame */
550 #if defined (HAVE_M17N_FLT) && defined (HAVE_LIBOTF)
552 #else /* not (HAVE_M17N_FLT && HAVE_LIBOTF) */
554 #endif /* not (HAVE_M17N_FLT && HAVE_LIBOTF) */
557 #ifdef HAVE_OTF_GET_VARIATION_GLYPHS
558 ftfont_variation_glyphs
,
563 ftfont_filter_properties
, /* filter_properties */
567 ftfont_get_cache (struct frame
*f
)
569 return freetype_font_cache
;
573 ftfont_get_charset (Lisp_Object registry
)
575 char *str
= SSDATA (SYMBOL_NAME (registry
));
577 char *re
= SAFE_ALLOCA (SBYTES (SYMBOL_NAME (registry
)) * 2 + 1);
581 for (i
= j
= 0; i
< SBYTES (SYMBOL_NAME (registry
)); i
++, j
++)
585 else if (str
[i
] == '*')
592 regexp
= make_unibyte_string (re
, j
);
594 for (i
= 0; fc_charset_table
[i
].name
; i
++)
595 if (fast_c_string_match_ignore_case
596 (regexp
, fc_charset_table
[i
].name
,
597 strlen (fc_charset_table
[i
].name
)) >= 0)
599 if (! fc_charset_table
[i
].name
)
601 if (! fc_charset_table
[i
].fc_charset
)
603 FcCharSet
*charset
= FcCharSetCreate ();
604 int *uniquifier
= fc_charset_table
[i
].uniquifier
;
608 for (j
= 0; uniquifier
[j
]; j
++)
609 if (! FcCharSetAddChar (charset
, uniquifier
[j
]))
611 FcCharSetDestroy (charset
);
614 fc_charset_table
[i
].fc_charset
= charset
;
622 unsigned int script_tag
, langsys_tag
;
624 unsigned int *features
[2];
627 #define OTF_SYM_TAG(SYM, TAG) \
629 unsigned char *p = SDATA (SYMBOL_NAME (SYM)); \
630 TAG = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]; \
633 #define OTF_TAG_STR(TAG, P) \
635 (P)[0] = (char) (TAG >> 24); \
636 (P)[1] = (char) ((TAG >> 16) & 0xFF); \
637 (P)[2] = (char) ((TAG >> 8) & 0xFF); \
638 (P)[3] = (char) (TAG & 0xFF); \
643 #define OTF_TAG_SYM(SYM, TAG) \
647 OTF_TAG_STR (TAG, str); \
648 (SYM) = font_intern_prop (str, 4, 1); \
653 static struct OpenTypeSpec
*
654 ftfont_get_open_type_spec (Lisp_Object otf_spec
)
656 struct OpenTypeSpec
*spec
= malloc (sizeof *spec
);
663 spec
->script
= XCAR (otf_spec
);
664 if (! NILP (spec
->script
))
666 OTF_SYM_TAG (spec
->script
, spec
->script_tag
);
667 val
= assq_no_quit (spec
->script
, Votf_script_alist
);
668 if (CONSP (val
) && SYMBOLP (XCDR (val
)))
669 spec
->script
= XCDR (val
);
674 spec
->script_tag
= 0x44464C54; /* "DFLT" */
675 otf_spec
= XCDR (otf_spec
);
676 spec
->langsys_tag
= 0;
677 if (! NILP (otf_spec
))
679 val
= XCAR (otf_spec
);
681 OTF_SYM_TAG (val
, spec
->langsys_tag
);
682 otf_spec
= XCDR (otf_spec
);
684 spec
->nfeatures
[0] = spec
->nfeatures
[1] = 0;
685 for (i
= 0; i
< 2 && ! NILP (otf_spec
); i
++, otf_spec
= XCDR (otf_spec
))
689 val
= XCAR (otf_spec
);
694 (min (PTRDIFF_MAX
, SIZE_MAX
) / sizeof (int) < XINT (len
)
696 : malloc (XINT (len
) * sizeof *spec
->features
[i
]));
697 if (! spec
->features
[i
])
699 if (i
> 0 && spec
->features
[0])
700 free (spec
->features
[0]);
704 for (j
= 0, negative
= 0; CONSP (val
); val
= XCDR (val
))
706 if (NILP (XCAR (val
)))
712 OTF_SYM_TAG (XCAR (val
), tag
);
713 spec
->features
[i
][j
++] = negative
? tag
& 0x80000000 : tag
;
716 spec
->nfeatures
[i
] = j
;
722 ftfont_spec_pattern (Lisp_Object spec
, char *otlayout
, struct OpenTypeSpec
**otspec
, const char **langname
)
724 Lisp_Object tmp
, extra
;
725 FcPattern
*pattern
= NULL
;
726 FcCharSet
*charset
= NULL
;
727 FcLangSet
*langset
= NULL
;
731 Lisp_Object script
= Qnil
;
732 Lisp_Object registry
;
735 if ((n
= FONT_SLANT_NUMERIC (spec
)) >= 0
737 /* Fontconfig doesn't support reverse-italic/oblique. */
740 if (INTEGERP (AREF (spec
, FONT_DPI_INDEX
)))
741 dpi
= XINT (AREF (spec
, FONT_DPI_INDEX
));
742 if (INTEGERP (AREF (spec
, FONT_AVGWIDTH_INDEX
))
743 && XINT (AREF (spec
, FONT_AVGWIDTH_INDEX
)) == 0)
746 registry
= AREF (spec
, FONT_REGISTRY_INDEX
);
748 || EQ (registry
, Qascii_0
)
749 || EQ (registry
, Qiso10646_1
)
750 || EQ (registry
, Qunicode_bmp
))
756 fc_charset_idx
= ftfont_get_charset (registry
);
757 if (fc_charset_idx
< 0)
759 charset
= fc_charset_table
[fc_charset_idx
].fc_charset
;
760 *langname
= fc_charset_table
[fc_charset_idx
].lang
;
761 lang
= (FcChar8
*) *langname
;
764 langset
= FcLangSetCreate ();
767 FcLangSetAdd (langset
, lang
);
772 for (extra
= AREF (spec
, FONT_EXTRA_INDEX
);
773 CONSP (extra
); extra
= XCDR (extra
))
775 Lisp_Object key
, val
;
777 key
= XCAR (XCAR (extra
)), val
= XCDR (XCAR (extra
));
783 else if (EQ (key
, QClang
))
786 langset
= FcLangSetCreate ();
791 if (! FcLangSetAdd (langset
, SYMBOL_FcChar8 (val
)))
795 for (; CONSP (val
); val
= XCDR (val
))
796 if (SYMBOLP (XCAR (val
))
797 && ! FcLangSetAdd (langset
, SYMBOL_FcChar8 (XCAR (val
))))
800 else if (EQ (key
, QCotf
))
804 *otspec
= ftfont_get_open_type_spec (val
);
807 strcpy (otlayout
, "otlayout:");
808 OTF_TAG_STR ((*otspec
)->script_tag
, otlayout
+ 9);
809 script
= (*otspec
)->script
;
812 else if (EQ (key
, QCscript
))
814 else if (EQ (key
, QCscalable
))
815 scalable
= ! NILP (val
);
818 if (! NILP (script
) && ! charset
)
820 Lisp_Object chars
= assq_no_quit (script
, Vscript_representative_chars
);
822 if (CONSP (chars
) && CONSP (CDR (chars
)))
824 charset
= FcCharSetCreate ();
827 for (chars
= XCDR (chars
); CONSP (chars
); chars
= XCDR (chars
))
828 if (CHARACTERP (XCAR (chars
))
829 && ! FcCharSetAddChar (charset
, XFASTINT (XCAR (chars
))))
834 pattern
= FcPatternCreate ();
837 tmp
= AREF (spec
, FONT_FOUNDRY_INDEX
);
839 && ! FcPatternAddString (pattern
, FC_FOUNDRY
, SYMBOL_FcChar8 (tmp
)))
841 tmp
= AREF (spec
, FONT_FAMILY_INDEX
);
843 && ! FcPatternAddString (pattern
, FC_FAMILY
, SYMBOL_FcChar8 (tmp
)))
846 && ! FcPatternAddCharSet (pattern
, FC_CHARSET
, charset
))
849 && ! FcPatternAddLangSet (pattern
, FC_LANG
, langset
))
852 && ! FcPatternAddDouble (pattern
, FC_DPI
, dpi
))
855 && ! FcPatternAddBool (pattern
, FC_SCALABLE
, scalable
? FcTrue
: FcFalse
))
861 /* We come here because of unexpected error in fontconfig API call
862 (usually insufficient memory). */
865 FcPatternDestroy (pattern
);
870 if ((*otspec
)->nfeatures
[0] > 0)
871 free ((*otspec
)->features
[0]);
872 if ((*otspec
)->nfeatures
[1] > 0)
873 free ((*otspec
)->features
[1]);
879 if (langset
) FcLangSetDestroy (langset
);
880 if (charset
&& fc_charset_idx
< 0) FcCharSetDestroy (charset
);
885 ftfont_list (struct frame
*f
, Lisp_Object spec
)
887 Lisp_Object val
= Qnil
, family
, adstyle
;
890 FcFontSet
*fontset
= NULL
;
891 FcObjectSet
*objset
= NULL
;
893 Lisp_Object chars
= Qnil
;
894 char otlayout
[15]; /* For "otlayout:XXXX" */
895 struct OpenTypeSpec
*otspec
= NULL
;
897 const char *langname
= NULL
;
899 if (! fc_initialized
)
905 pattern
= ftfont_spec_pattern (spec
, otlayout
, &otspec
, &langname
);
908 if (FcPatternGetCharSet (pattern
, FC_CHARSET
, 0, &charset
) != FcResultMatch
)
910 val
= assq_no_quit (QCscript
, AREF (spec
, FONT_EXTRA_INDEX
));
913 val
= assq_no_quit (XCDR (val
), Vscript_representative_chars
);
914 if (CONSP (val
) && VECTORP (XCDR (val
)))
919 if (INTEGERP (AREF (spec
, FONT_SPACING_INDEX
)))
920 spacing
= XINT (AREF (spec
, FONT_SPACING_INDEX
));
921 family
= AREF (spec
, FONT_FAMILY_INDEX
);
924 Lisp_Object resolved
;
926 resolved
= ftfont_resolve_generic_family (family
, pattern
);
927 if (! NILP (resolved
))
929 FcPatternDel (pattern
, FC_FAMILY
);
930 if (! FcPatternAddString (pattern
, FC_FAMILY
,
931 SYMBOL_FcChar8 (resolved
)))
935 adstyle
= AREF (spec
, FONT_ADSTYLE_INDEX
);
936 if (! NILP (adstyle
) && SBYTES (SYMBOL_NAME (adstyle
)) == 0)
938 objset
= FcObjectSetBuild (FC_FOUNDRY
, FC_FAMILY
, FC_WEIGHT
, FC_SLANT
,
939 FC_WIDTH
, FC_PIXEL_SIZE
, FC_SPACING
, FC_SCALABLE
,
940 FC_STYLE
, FC_FILE
, FC_INDEX
,
943 #endif /* FC_CAPABILITY */
951 FcObjectSetAdd (objset
, FC_CHARSET
);
953 fontset
= FcFontList (NULL
, pattern
, objset
);
954 if (! fontset
|| fontset
->nfont
== 0)
957 /* Need fix because this finds any fonts. */
958 if (fontset
->nfont
== 0 && ! NILP (family
))
960 /* Try matching with configuration. For instance, the
961 configuration may specify "Nimbus Mono L" as an alias of
963 FcPattern
*pat
= FcPatternBuild (0, FC_FAMILY
, FcTypeString
,
964 SYMBOL_FcChar8 (family
), NULL
);
967 if (FcConfigSubstitute (NULL
, pat
, FcMatchPattern
) == FcTrue
)
970 FcPatternGetString (pat
, FC_FAMILY
, i
, &fam
) == FcResultMatch
;
973 FcPatternDel (pattern
, FC_FAMILY
);
974 FcPatternAddString (pattern
, FC_FAMILY
, fam
);
975 FcFontSetDestroy (fontset
);
976 fontset
= FcFontList (NULL
, pattern
, objset
);
977 if (fontset
&& fontset
->nfont
> 0)
983 for (i
= 0; i
< fontset
->nfont
; i
++)
991 if ((FcPatternGetInteger (fontset
->fonts
[i
], FC_SPACING
, 0, &this)
1002 if (FcPatternGetString (fontset
->fonts
[i
], FC_CAPABILITY
, 0, &this)
1004 || ! strstr ((char *) this, otlayout
))
1007 #endif /* FC_CAPABILITY */
1015 if (FcPatternGetString (fontset
->fonts
[i
], FC_FILE
, 0, &file
)
1018 otf
= OTF_open ((char *) file
);
1021 passed
= (OTF_check_features (otf
, 1, otspec
->script_tag
,
1022 otspec
->langsys_tag
,
1023 otspec
->features
[0],
1024 otspec
->nfeatures
[0]) == 1
1025 && OTF_check_features (otf
, 0, otspec
->script_tag
,
1026 otspec
->langsys_tag
,
1027 otspec
->features
[1],
1028 otspec
->nfeatures
[1]) == 1);
1033 #endif /* HAVE_LIBOTF */
1034 if (VECTORP (chars
))
1038 if (FcPatternGetCharSet (fontset
->fonts
[i
], FC_CHARSET
, 0, &charset
)
1041 for (j
= 0; j
< ASIZE (chars
); j
++)
1042 if (TYPE_RANGED_INTEGERP (FcChar32
, AREF (chars
, j
))
1043 && FcCharSetHasChar (charset
, XFASTINT (AREF (chars
, j
))))
1045 if (j
== ASIZE (chars
))
1048 if (! NILP (adstyle
) || langname
)
1050 Lisp_Object this_adstyle
= get_adstyle_property (fontset
->fonts
[i
]);
1052 if (! NILP (adstyle
)
1053 && (NILP (this_adstyle
)
1054 || xstrcasecmp (SSDATA (SYMBOL_NAME (adstyle
)),
1055 SSDATA (SYMBOL_NAME (this_adstyle
))) != 0))
1058 && ! NILP (this_adstyle
)
1059 && xstrcasecmp (langname
, SSDATA (SYMBOL_NAME (this_adstyle
))))
1062 entity
= ftfont_pattern_entity (fontset
->fonts
[i
],
1063 AREF (spec
, FONT_EXTRA_INDEX
));
1064 if (! NILP (entity
))
1065 val
= Fcons (entity
, val
);
1067 val
= Fnreverse (val
);
1071 /* We come here because of unexpected error in fontconfig API call
1072 (usually insufficient memory). */
1076 FONT_ADD_LOG ("ftfont-list", spec
, val
);
1077 if (objset
) FcObjectSetDestroy (objset
);
1078 if (fontset
) FcFontSetDestroy (fontset
);
1079 if (pattern
) FcPatternDestroy (pattern
);
1084 ftfont_match (struct frame
*f
, Lisp_Object spec
)
1086 Lisp_Object entity
= Qnil
;
1087 FcPattern
*pattern
, *match
= NULL
;
1089 char otlayout
[15]; /* For "otlayout:XXXX" */
1090 struct OpenTypeSpec
*otspec
= NULL
;
1091 const char *langname
= NULL
;
1093 if (! fc_initialized
)
1099 pattern
= ftfont_spec_pattern (spec
, otlayout
, &otspec
, &langname
);
1103 if (INTEGERP (AREF (spec
, FONT_SIZE_INDEX
)))
1107 value
.type
= FcTypeDouble
;
1108 value
.u
.d
= XINT (AREF (spec
, FONT_SIZE_INDEX
));
1109 FcPatternAdd (pattern
, FC_PIXEL_SIZE
, value
, FcFalse
);
1111 if (FcConfigSubstitute (NULL
, pattern
, FcMatchPattern
) == FcTrue
)
1113 FcDefaultSubstitute (pattern
);
1114 match
= FcFontMatch (NULL
, pattern
, &result
);
1117 entity
= ftfont_pattern_entity (match
, AREF (spec
, FONT_EXTRA_INDEX
));
1118 FcPatternDestroy (match
);
1119 if (! NILP (AREF (spec
, FONT_FAMILY_INDEX
))
1120 && NILP (assq_no_quit (AREF (spec
, FONT_FAMILY_INDEX
),
1121 ftfont_generic_family_list
))
1122 && NILP (Fstring_equal (AREF (spec
, FONT_FAMILY_INDEX
),
1123 AREF (entity
, FONT_FAMILY_INDEX
))))
1127 FcPatternDestroy (pattern
);
1129 FONT_ADD_LOG ("ftfont-match", spec
, entity
);
1134 ftfont_list_family (struct frame
*f
)
1136 Lisp_Object list
= Qnil
;
1137 FcPattern
*pattern
= NULL
;
1138 FcFontSet
*fontset
= NULL
;
1139 FcObjectSet
*objset
= NULL
;
1142 if (! fc_initialized
)
1148 pattern
= FcPatternCreate ();
1151 objset
= FcObjectSetBuild (FC_FAMILY
, NULL
);
1154 fontset
= FcFontList (NULL
, pattern
, objset
);
1158 for (i
= 0; i
< fontset
->nfont
; i
++)
1160 FcPattern
*pat
= fontset
->fonts
[i
];
1163 if (FcPatternGetString (pat
, FC_FAMILY
, 0, &str
) == FcResultMatch
)
1164 list
= Fcons (intern ((char *) str
), list
);
1168 if (objset
) FcObjectSetDestroy (objset
);
1169 if (fontset
) FcFontSetDestroy (fontset
);
1170 if (pattern
) FcPatternDestroy (pattern
);
1177 ftfont_open (struct frame
*f
, Lisp_Object entity
, int pixel_size
)
1179 struct ftfont_info
*ftfont_info
;
1181 struct ftfont_cache_data
*cache_data
;
1185 Lisp_Object val
, filename
, idx
, cache
, font_object
;
1191 val
= assq_no_quit (QCfont_entity
, AREF (entity
, FONT_EXTRA_INDEX
));
1195 cache
= ftfont_lookup_cache (entity
, FTFONT_CACHE_FOR_FACE
);
1198 filename
= XCAR (val
);
1201 cache_data
= XSAVE_POINTER (XCDR (cache
), 0);
1202 ft_face
= cache_data
->ft_face
;
1203 if (XSAVE_INTEGER (val
, 1) > 0)
1205 /* FT_Face in this cache is already used by the different size. */
1206 if (FT_New_Size (ft_face
, &ft_size
) != 0)
1208 if (FT_Activate_Size (ft_size
) != 0)
1210 FT_Done_Size (ft_size
);
1214 set_save_integer (val
, 1, XSAVE_INTEGER (val
, 1) + 1);
1215 size
= XINT (AREF (entity
, FONT_SIZE_INDEX
));
1218 if (FT_Set_Pixel_Sizes (ft_face
, size
, size
) != 0)
1220 if (XSAVE_INTEGER (val
, 1) == 0)
1221 FT_Done_Face (ft_face
);
1225 font_object
= font_build_object (VECSIZE (struct ftfont_info
),
1226 Qfreetype
, entity
, size
);
1227 ASET (font_object
, FONT_FILE_INDEX
, filename
);
1228 font
= XFONT_OBJECT (font_object
);
1229 ftfont_info
= (struct ftfont_info
*) font
;
1230 ftfont_info
->ft_size
= ft_face
->size
;
1231 ftfont_info
->index
= XINT (idx
);
1233 ftfont_info
->maybe_otf
= (ft_face
->face_flags
& FT_FACE_FLAG_SFNT
) != 0;
1234 ftfont_info
->otf
= NULL
;
1235 #endif /* HAVE_LIBOTF */
1236 /* This means that there's no need of transformation. */
1237 ftfont_info
->matrix
.xx
= 0;
1238 font
->pixel_size
= size
;
1239 font
->driver
= &ftfont_driver
;
1240 font
->encoding_charset
= font
->repertory_charset
= -1;
1242 upEM
= ft_face
->units_per_EM
;
1243 scalable
= (INTEGERP (AREF (entity
, FONT_AVGWIDTH_INDEX
))
1244 && XINT (AREF (entity
, FONT_AVGWIDTH_INDEX
)) == 0);
1247 font
->ascent
= ft_face
->ascender
* size
/ upEM
;
1248 font
->descent
= - ft_face
->descender
* size
/ upEM
;
1249 font
->height
= ft_face
->height
* size
/ upEM
;
1253 font
->ascent
= ft_face
->size
->metrics
.ascender
>> 6;
1254 font
->descent
= - ft_face
->size
->metrics
.descender
>> 6;
1255 font
->height
= ft_face
->size
->metrics
.height
>> 6;
1257 if (INTEGERP (AREF (entity
, FONT_SPACING_INDEX
)))
1258 spacing
= XINT (AREF (entity
, FONT_SPACING_INDEX
));
1260 spacing
= FC_PROPORTIONAL
;
1261 if (spacing
!= FC_PROPORTIONAL
1263 && spacing
!= FC_DUAL
1264 #endif /* FC_DUAL */
1266 font
->min_width
= font
->average_width
= font
->space_width
1267 = (scalable
? ft_face
->max_advance_width
* size
/ upEM
1268 : ft_face
->size
->metrics
.max_advance
>> 6);
1273 font
->min_width
= font
->average_width
= font
->space_width
= 0;
1274 for (i
= 32, n
= 0; i
< 127; i
++)
1275 if (FT_Load_Char (ft_face
, i
, FT_LOAD_DEFAULT
) == 0)
1277 int this_width
= ft_face
->glyph
->metrics
.horiAdvance
>> 6;
1280 && (! font
->min_width
|| font
->min_width
> this_width
))
1281 font
->min_width
= this_width
;
1283 font
->space_width
= this_width
;
1284 font
->average_width
+= this_width
;
1288 font
->average_width
/= n
;
1291 font
->baseline_offset
= 0;
1292 font
->relative_compose
= 0;
1293 font
->default_ascent
= 0;
1294 font
->vertical_centering
= 0;
1297 font
->underline_position
= -ft_face
->underline_position
* size
/ upEM
;
1298 font
->underline_thickness
= ft_face
->underline_thickness
* size
/ upEM
;
1302 font
->underline_position
= -1;
1303 font
->underline_thickness
= 0;
1310 ftfont_close (struct font
*font
)
1312 struct ftfont_info
*ftfont_info
= (struct ftfont_info
*) font
;
1313 Lisp_Object val
, cache
;
1315 val
= Fcons (font
->props
[FONT_FILE_INDEX
], make_number (ftfont_info
->index
));
1316 cache
= ftfont_lookup_cache (val
, FTFONT_CACHE_FOR_FACE
);
1317 eassert (CONSP (cache
));
1319 set_save_integer (val
, 1, XSAVE_INTEGER (val
, 1) - 1);
1320 if (XSAVE_INTEGER (val
, 1) == 0)
1322 struct ftfont_cache_data
*cache_data
= XSAVE_POINTER (val
, 0);
1324 FT_Done_Face (cache_data
->ft_face
);
1326 if (ftfont_info
->otf
)
1327 OTF_close (ftfont_info
->otf
);
1329 cache_data
->ft_face
= NULL
;
1332 FT_Done_Size (ftfont_info
->ft_size
);
1336 ftfont_has_char (Lisp_Object font
, int c
)
1338 struct charset
*cs
= NULL
;
1340 if (EQ (AREF (font
, FONT_ADSTYLE_INDEX
), Qja
)
1341 && charset_jisx0208
>= 0)
1342 cs
= CHARSET_FROM_ID (charset_jisx0208
);
1343 else if (EQ (AREF (font
, FONT_ADSTYLE_INDEX
), Qko
)
1344 && charset_ksc5601
>= 0)
1345 cs
= CHARSET_FROM_ID (charset_ksc5601
);
1347 return (ENCODE_CHAR (cs
, c
) != CHARSET_INVALID_CODE (cs
));
1349 if (FONT_ENTITY_P (font
))
1351 FcCharSet
*charset
= ftfont_get_fc_charset (font
);
1353 return (FcCharSetHasChar (charset
, c
) == FcTrue
);
1357 struct ftfont_info
*ftfont_info
;
1359 ftfont_info
= (struct ftfont_info
*) XFONT_OBJECT (font
);
1360 return (FT_Get_Char_Index (ftfont_info
->ft_size
->face
, (FT_ULong
) c
)
1366 ftfont_encode_char (struct font
*font
, int c
)
1368 struct ftfont_info
*ftfont_info
= (struct ftfont_info
*) font
;
1369 FT_Face ft_face
= ftfont_info
->ft_size
->face
;
1370 FT_ULong charcode
= c
;
1371 FT_UInt code
= FT_Get_Char_Index (ft_face
, charcode
);
1373 return (code
> 0 ? code
: FONT_INVALID_CODE
);
1377 ftfont_text_extents (struct font
*font
, unsigned int *code
,
1378 int nglyphs
, struct font_metrics
*metrics
)
1380 struct ftfont_info
*ftfont_info
= (struct ftfont_info
*) font
;
1381 FT_Face ft_face
= ftfont_info
->ft_size
->face
;
1385 if (ftfont_info
->ft_size
!= ft_face
->size
)
1386 FT_Activate_Size (ftfont_info
->ft_size
);
1388 for (i
= 0, first
= 1; i
< nglyphs
; i
++)
1390 if (FT_Load_Glyph (ft_face
, code
[i
], FT_LOAD_DEFAULT
) == 0)
1392 FT_Glyph_Metrics
*m
= &ft_face
->glyph
->metrics
;
1396 metrics
->lbearing
= m
->horiBearingX
>> 6;
1397 metrics
->rbearing
= (m
->horiBearingX
+ m
->width
) >> 6;
1398 metrics
->ascent
= m
->horiBearingY
>> 6;
1399 metrics
->descent
= (m
->height
- m
->horiBearingY
) >> 6;
1402 if (metrics
->lbearing
> width
+ (m
->horiBearingX
>> 6))
1403 metrics
->lbearing
= width
+ (m
->horiBearingX
>> 6);
1404 if (metrics
->rbearing
1405 < width
+ ((m
->horiBearingX
+ m
->width
) >> 6))
1407 = width
+ ((m
->horiBearingX
+ m
->width
) >> 6);
1408 if (metrics
->ascent
< (m
->horiBearingY
>> 6))
1409 metrics
->ascent
= m
->horiBearingY
>> 6;
1410 if (metrics
->descent
> ((m
->height
- m
->horiBearingY
) >> 6))
1411 metrics
->descent
= (m
->height
- m
->horiBearingY
) >> 6;
1412 width
+= m
->horiAdvance
>> 6;
1415 width
+= font
->space_width
;
1417 metrics
->width
= width
;
1421 ftfont_get_bitmap (struct font
*font
, unsigned int code
, struct font_bitmap
*bitmap
, int bits_per_pixel
)
1423 struct ftfont_info
*ftfont_info
= (struct ftfont_info
*) font
;
1424 FT_Face ft_face
= ftfont_info
->ft_size
->face
;
1425 FT_Int32 load_flags
= FT_LOAD_RENDER
;
1427 if (ftfont_info
->ft_size
!= ft_face
->size
)
1428 FT_Activate_Size (ftfont_info
->ft_size
);
1429 if (bits_per_pixel
== 1)
1431 #ifdef FT_LOAD_TARGET_MONO
1432 load_flags
|= FT_LOAD_TARGET_MONO
;
1434 load_flags
|= FT_LOAD_MONOCHROME
;
1437 else if (bits_per_pixel
!= 8)
1438 /* We don't support such a rendering. */
1441 if (FT_Load_Glyph (ft_face
, code
, load_flags
) != 0)
1443 bitmap
->bits_per_pixel
1444 = (ft_face
->glyph
->bitmap
.pixel_mode
== FT_PIXEL_MODE_MONO
? 1
1445 : ft_face
->glyph
->bitmap
.pixel_mode
== FT_PIXEL_MODE_GRAY
? 8
1446 : ft_face
->glyph
->bitmap
.pixel_mode
== FT_PIXEL_MODE_LCD
? 8
1447 : ft_face
->glyph
->bitmap
.pixel_mode
== FT_PIXEL_MODE_LCD_V
? 8
1449 if (bitmap
->bits_per_pixel
< 0)
1450 /* We don't support that kind of pixel mode. */
1452 bitmap
->rows
= ft_face
->glyph
->bitmap
.rows
;
1453 bitmap
->width
= ft_face
->glyph
->bitmap
.width
;
1454 bitmap
->pitch
= ft_face
->glyph
->bitmap
.pitch
;
1455 bitmap
->buffer
= ft_face
->glyph
->bitmap
.buffer
;
1456 bitmap
->left
= ft_face
->glyph
->bitmap_left
;
1457 bitmap
->top
= ft_face
->glyph
->bitmap_top
;
1458 bitmap
->advance
= ft_face
->glyph
->metrics
.horiAdvance
>> 6;
1464 ftfont_anchor_point (struct font
*font
, unsigned int code
, int idx
,
1467 struct ftfont_info
*ftfont_info
= (struct ftfont_info
*) font
;
1468 FT_Face ft_face
= ftfont_info
->ft_size
->face
;
1470 if (ftfont_info
->ft_size
!= ft_face
->size
)
1471 FT_Activate_Size (ftfont_info
->ft_size
);
1472 if (FT_Load_Glyph (ft_face
, code
, FT_LOAD_DEFAULT
) != 0)
1474 if (ft_face
->glyph
->format
!= FT_GLYPH_FORMAT_OUTLINE
)
1476 if (idx
>= ft_face
->glyph
->outline
.n_points
)
1478 *x
= ft_face
->glyph
->outline
.points
[idx
].x
;
1479 *y
= ft_face
->glyph
->outline
.points
[idx
].y
;
1486 ftfont_otf_features (OTF_GSUB_GPOS
*gsub_gpos
)
1488 Lisp_Object scripts
, langsyses
, features
, sym
;
1491 for (scripts
= Qnil
, i
= gsub_gpos
->ScriptList
.ScriptCount
- 1; i
>= 0; i
--)
1493 OTF_Script
*otf_script
= gsub_gpos
->ScriptList
.Script
+ i
;
1495 for (langsyses
= Qnil
, j
= otf_script
->LangSysCount
- 1; j
>= -1; j
--)
1497 OTF_LangSys
*otf_langsys
;
1500 otf_langsys
= otf_script
->LangSys
+ j
;
1501 else if (otf_script
->DefaultLangSysOffset
)
1502 otf_langsys
= &otf_script
->DefaultLangSys
;
1506 for (features
= Qnil
, k
= otf_langsys
->FeatureCount
- 1; k
>= 0; k
--)
1508 l
= otf_langsys
->FeatureIndex
[k
];
1509 if (l
>= gsub_gpos
->FeatureList
.FeatureCount
)
1511 OTF_TAG_SYM (sym
, gsub_gpos
->FeatureList
.Feature
[l
].FeatureTag
);
1512 features
= Fcons (sym
, features
);
1515 OTF_TAG_SYM (sym
, otf_script
->LangSysRecord
[j
].LangSysTag
);
1518 langsyses
= Fcons (Fcons (sym
, features
), langsyses
);
1521 OTF_TAG_SYM (sym
, gsub_gpos
->ScriptList
.Script
[i
].ScriptTag
);
1522 scripts
= Fcons (Fcons (sym
, langsyses
), scripts
);
1530 ftfont_otf_capability (struct font
*font
)
1532 struct ftfont_info
*ftfont_info
= (struct ftfont_info
*) font
;
1533 OTF
*otf
= ftfont_get_otf (ftfont_info
);
1534 Lisp_Object gsub_gpos
;
1538 gsub_gpos
= Fcons (Qnil
, Qnil
);
1539 if (OTF_get_table (otf
, "GSUB") == 0
1540 && otf
->gsub
->FeatureList
.FeatureCount
> 0)
1541 XSETCAR (gsub_gpos
, ftfont_otf_features (otf
->gsub
));
1542 if (OTF_get_table (otf
, "GPOS") == 0
1543 && otf
->gpos
->FeatureList
.FeatureCount
> 0)
1544 XSETCDR (gsub_gpos
, ftfont_otf_features (otf
->gpos
));
1548 #ifdef HAVE_M17N_FLT
1550 #if (((LIBOTF_MAJOR_VERSION > 1) || (LIBOTF_RELEASE_NUMBER >= 10)) \
1551 && ((M17NLIB_MAJOR_VERSION > 1) || (M17NLIB_MINOR_VERSION >= 6)))
1552 /* We can use the new feature of libotf and m17n-flt to handle the
1553 character encoding scheme introduced in Unicode 5.1 and 5.2 for
1554 some Agian scripts. */
1555 #define M17N_FLT_USE_NEW_FEATURE
1568 ftfont_get_glyph_id (MFLTFont
*font
, MFLTGlyphString
*gstring
,
1571 struct MFLTFontFT
*flt_font_ft
= (struct MFLTFontFT
*) font
;
1572 FT_Face ft_face
= flt_font_ft
->ft_face
;
1575 for (g
= gstring
->glyphs
+ from
; from
< to
; g
++, from
++)
1578 FT_UInt code
= FT_Get_Char_Index (ft_face
, g
->code
);
1580 g
->code
= code
> 0 ? code
: FONT_INVALID_CODE
;
1586 /* Operators for 26.6 fixed fractional pixel format */
1588 #define FLOOR(x) ((x) & -64)
1589 #define CEIL(x) (((x)+63) & -64)
1590 #define ROUND(x) (((x)+32) & -64)
1593 ftfont_get_metrics (MFLTFont
*font
, MFLTGlyphString
*gstring
,
1596 struct MFLTFontFT
*flt_font_ft
= (struct MFLTFontFT
*) font
;
1597 FT_Face ft_face
= flt_font_ft
->ft_face
;
1600 for (g
= gstring
->glyphs
+ from
; from
< to
; g
++, from
++)
1603 if (g
->code
!= FONT_INVALID_CODE
)
1605 FT_Glyph_Metrics
*m
;
1607 if (FT_Load_Glyph (ft_face
, g
->code
, FT_LOAD_DEFAULT
) != 0)
1609 m
= &ft_face
->glyph
->metrics
;
1610 if (flt_font_ft
->matrix
)
1615 v
[0].x
= v
[1].x
= m
->horiBearingX
;
1616 v
[2].x
= v
[3].x
= m
->horiBearingX
+ m
->width
;
1617 v
[0].y
= v
[2].y
= m
->horiBearingY
;
1618 v
[1].y
= v
[3].y
= m
->horiBearingY
- m
->height
;
1619 for (i
= 0; i
< 4; i
++)
1620 FT_Vector_Transform (v
+ i
, flt_font_ft
->matrix
);
1621 g
->lbearing
= v
[0].x
< v
[1].x
? FLOOR (v
[0].x
) : FLOOR (v
[1].x
);
1622 g
->rbearing
= v
[2].x
> v
[3].x
? CEIL (v
[2].x
) : CEIL (v
[3].x
);
1623 g
->ascent
= v
[0].y
> v
[2].y
? CEIL (v
[0].y
) : CEIL (v
[2].y
);
1624 g
->descent
= v
[1].y
< v
[3].y
? - FLOOR (v
[1].y
) : - FLOOR (v
[3].y
);
1628 g
->lbearing
= FLOOR (m
->horiBearingX
);
1629 g
->rbearing
= CEIL (m
->horiBearingX
+ m
->width
);
1630 g
->ascent
= CEIL (m
->horiBearingY
);
1631 g
->descent
= - FLOOR (m
->horiBearingY
- m
->height
);
1633 g
->xadv
= ROUND (ft_face
->glyph
->advance
.x
);
1638 g
->rbearing
= g
->xadv
= flt_font_ft
->font
->space_width
<< 6;
1639 g
->ascent
= flt_font_ft
->font
->ascent
<< 6;
1640 g
->descent
= flt_font_ft
->font
->descent
<< 6;
1649 ftfont_check_otf (MFLTFont
*font
, MFLTOtfSpec
*spec
)
1651 #define FEATURE_NONE(IDX) (! spec->features[IDX])
1653 #define FEATURE_ANY(IDX) \
1654 (spec->features[IDX] \
1655 && spec->features[IDX][0] == 0xFFFFFFFF && spec->features[IDX][1] == 0)
1657 struct MFLTFontFT
*flt_font_ft
= (struct MFLTFontFT
*) font
;
1658 OTF
*otf
= flt_font_ft
->otf
;
1663 if (FEATURE_ANY (0) && FEATURE_ANY (1))
1664 /* Return true iff any of GSUB or GPOS support the script (and
1667 && (OTF_check_features (otf
, 0, spec
->script
, spec
->langsys
,
1669 || OTF_check_features (otf
, 1, spec
->script
, spec
->langsys
,
1672 for (i
= 0; i
< 2; i
++)
1673 if (! FEATURE_ANY (i
))
1675 if (FEATURE_NONE (i
))
1678 && OTF_check_features (otf
, i
== 0, spec
->script
, spec
->langsys
,
1683 if (spec
->features
[i
][0] == 0xFFFFFFFF)
1686 || OTF_check_features (otf
, i
== 0, spec
->script
, spec
->langsys
,
1692 for (n
= 1; spec
->features
[i
][n
]; n
++);
1694 SAFE_NALLOCA (tags
, 1, n
);
1695 for (n
= 0, negative
= 0; spec
->features
[i
][n
]; n
++)
1697 if (spec
->features
[i
][n
] == 0xFFFFFFFF)
1700 tags
[n
- 1] = spec
->features
[i
][n
] | 0x80000000;
1702 tags
[n
] = spec
->features
[i
][n
];
1705 #ifndef M17N_FLT_USE_NEW_FEATURE
1706 passed
= n
- negative
> 0;
1709 passed
= (OTF_check_features (otf
, i
== 0, spec
->script
,
1710 spec
->langsys
, tags
, n
- negative
)
1721 #define DEVICE_DELTA(table, size) \
1722 (((size) >= (table).StartSize && (size) <= (table).EndSize) \
1723 ? (table).DeltaValue[(size) - (table).StartSize] << 6 \
1727 adjust_anchor (FT_Face ft_face
, OTF_Anchor
*anchor
,
1728 unsigned code
, int x_ppem
, int y_ppem
, int *x
, int *y
)
1730 if (anchor
->AnchorFormat
== 2)
1732 FT_Outline
*outline
;
1733 int ap
= anchor
->f
.f1
.AnchorPoint
;
1735 FT_Load_Glyph (ft_face
, (FT_UInt
) code
, FT_LOAD_MONOCHROME
);
1736 outline
= &ft_face
->glyph
->outline
;
1737 if (ap
< outline
->n_points
)
1739 *x
= outline
->points
[ap
].x
<< 6;
1740 *y
= outline
->points
[ap
].y
<< 6;
1743 else if (anchor
->AnchorFormat
== 3)
1745 if (anchor
->f
.f2
.XDeviceTable
.offset
1746 && anchor
->f
.f2
.XDeviceTable
.DeltaValue
)
1747 *x
+= DEVICE_DELTA (anchor
->f
.f2
.XDeviceTable
, x_ppem
);
1748 if (anchor
->f
.f2
.YDeviceTable
.offset
1749 && anchor
->f
.f2
.YDeviceTable
.DeltaValue
)
1750 *y
+= DEVICE_DELTA (anchor
->f
.f2
.YDeviceTable
, y_ppem
);
1754 static OTF_GlyphString otf_gstring
;
1757 setup_otf_gstring (int size
)
1759 if (otf_gstring
.size
< size
)
1761 otf_gstring
.glyphs
= xnrealloc (otf_gstring
.glyphs
,
1762 size
, sizeof (OTF_Glyph
));
1763 otf_gstring
.size
= size
;
1765 otf_gstring
.used
= size
;
1766 memset (otf_gstring
.glyphs
, 0, sizeof (OTF_Glyph
) * size
);
1769 #ifdef M17N_FLT_USE_NEW_FEATURE
1771 /* Pack 32-bit OTF tag (0x7F7F7F7F) into 28-bit (0x0FFFFFFF). */
1772 #define PACK_OTF_TAG(TAG) \
1773 ((((TAG) & 0x7F000000) >> 3) \
1774 | (((TAG) & 0x7F0000) >> 2) \
1775 | (((TAG) & 0x7F00) >> 1) \
1778 /* Assuming that FONT is an OpenType font, apply OpenType features
1779 specified in SPEC on glyphs between FROM and TO of IN, and record
1780 the lastly applied feature in each glyph of IN. If OUT is not
1781 NULL, append the resulting glyphs to OUT while storing glyph
1782 position adjustment information in ADJUSTMENT. */
1785 ftfont_drive_otf (MFLTFont
*font
,
1787 MFLTGlyphString
*in
,
1790 MFLTGlyphString
*out
,
1791 MFLTGlyphAdjustment
*adjustment
)
1793 struct MFLTFontFT
*flt_font_ft
= (struct MFLTFontFT
*) font
;
1794 FT_Face ft_face
= flt_font_ft
->ft_face
;
1795 OTF
*otf
= flt_font_ft
->otf
;
1796 int len
= to
- from
;
1799 char script
[5], *langsys
= NULL
;
1800 char *gsub_features
= NULL
, *gpos_features
= NULL
;
1801 OTF_Feature
*features
;
1805 OTF_tag_name (spec
->script
, script
);
1810 langsys
= langsysbuf
;
1811 OTF_tag_name (spec
->langsys
, langsys
);
1815 for (i
= 0; i
< 2; i
++)
1819 if (spec
->features
[i
] && spec
->features
[i
][1] != 0xFFFFFFFF)
1821 for (j
= 0; spec
->features
[i
][j
]; j
++);
1822 SAFE_NALLOCA (p
, 6, j
);
1827 for (j
= 0; spec
->features
[i
][j
]; j
++)
1829 if (spec
->features
[i
][j
] == 0xFFFFFFFF)
1830 *p
++ = '*', *p
++ = ',';
1833 OTF_tag_name (spec
->features
[i
][j
], p
);
1842 setup_otf_gstring (len
);
1843 for (i
= 0; i
< len
; i
++)
1845 otf_gstring
.glyphs
[i
].c
= in
->glyphs
[from
+ i
].c
& 0x11FFFF;
1846 otf_gstring
.glyphs
[i
].glyph_id
= in
->glyphs
[from
+ i
].code
;
1849 OTF_drive_gdef (otf
, &otf_gstring
);
1850 gidx
= out
? out
->used
: from
;
1852 if (gsub_features
&& out
)
1854 if (OTF_drive_gsub_with_log (otf
, &otf_gstring
, script
, langsys
,
1857 if (out
->allocated
< out
->used
+ otf_gstring
.used
)
1862 features
= otf
->gsub
->FeatureList
.Feature
;
1863 for (i
= 0, otfg
= otf_gstring
.glyphs
; i
< otf_gstring
.used
; )
1866 int min_from
, max_to
;
1867 int feature_idx
= otfg
->positioning_type
>> 4;
1869 g
= out
->glyphs
+ out
->used
;
1870 *g
= in
->glyphs
[from
+ otfg
->f
.index
.from
];
1871 if (g
->code
!= otfg
->glyph_id
)
1874 g
->code
= otfg
->glyph_id
;
1880 if (otfg
->f
.index
.from
< otfg
->f
.index
.to
)
1882 /* OTFG substitutes multiple glyphs in IN. */
1883 for (j
= from
+ otfg
->f
.index
.from
+ 1;
1884 j
<= from
+ otfg
->f
.index
.to
; j
++)
1886 if (min_from
> in
->glyphs
[j
].from
)
1887 min_from
= in
->glyphs
[j
].from
;
1888 if (max_to
< in
->glyphs
[j
].to
)
1889 max_to
= in
->glyphs
[j
].to
;
1896 unsigned int tag
= features
[feature_idx
- 1].FeatureTag
;
1897 tag
= PACK_OTF_TAG (tag
);
1898 g
->internal
= (g
->internal
& ~0x1FFFFFFF) | tag
;
1900 for (i
++, otfg
++; (i
< otf_gstring
.used
1901 && otfg
->f
.index
.from
== otfg
[-1].f
.index
.from
);
1904 g
= out
->glyphs
+ out
->used
;
1905 *g
= in
->glyphs
[from
+ otfg
->f
.index
.to
];
1906 if (g
->code
!= otfg
->glyph_id
)
1909 g
->code
= otfg
->glyph_id
;
1912 feature_idx
= otfg
->positioning_type
>> 4;
1915 unsigned int tag
= features
[feature_idx
- 1].FeatureTag
;
1916 tag
= PACK_OTF_TAG (tag
);
1917 g
->internal
= (g
->internal
& ~0x1FFFFFFF) | tag
;
1923 else if (gsub_features
)
1925 /* Just for checking which features will be applied. */
1926 if (OTF_drive_gsub_with_log (otf
, &otf_gstring
, script
, langsys
,
1929 features
= otf
->gsub
->FeatureList
.Feature
;
1930 for (i
= 0, otfg
= otf_gstring
.glyphs
; i
< otf_gstring
.used
; i
++,
1933 int feature_idx
= otfg
->positioning_type
>> 4;
1937 unsigned int tag
= features
[feature_idx
- 1].FeatureTag
;
1938 tag
= PACK_OTF_TAG (tag
);
1939 for (j
= otfg
->f
.index
.from
; j
<= otfg
->f
.index
.to
; j
++)
1941 MFLTGlyph
*g
= in
->glyphs
+ (from
+ j
);
1942 g
->internal
= (g
->internal
& ~0x1FFFFFFF) | tag
;
1949 if (out
->allocated
< out
->used
+ len
)
1954 for (i
= 0; i
< len
; i
++)
1955 out
->glyphs
[out
->used
++] = in
->glyphs
[from
+ i
];
1958 if (gpos_features
&& out
)
1960 MFLTGlyph
*base
= NULL
, *mark
= NULL
, *g
;
1961 int x_ppem
, y_ppem
, x_scale
, y_scale
;
1963 if (OTF_drive_gpos_with_log (otf
, &otf_gstring
, script
, langsys
,
1969 features
= otf
->gpos
->FeatureList
.Feature
;
1970 x_ppem
= ft_face
->size
->metrics
.x_ppem
;
1971 y_ppem
= ft_face
->size
->metrics
.y_ppem
;
1972 x_scale
= ft_face
->size
->metrics
.x_scale
;
1973 y_scale
= ft_face
->size
->metrics
.y_scale
;
1975 for (i
= 0, otfg
= otf_gstring
.glyphs
, g
= out
->glyphs
+ gidx
;
1976 i
< otf_gstring
.used
; i
++, otfg
++, g
++)
1979 int feature_idx
= otfg
->positioning_type
>> 4;
1983 unsigned int tag
= features
[feature_idx
- 1].FeatureTag
;
1984 tag
= PACK_OTF_TAG (tag
);
1985 g
->internal
= (g
->internal
& ~0x1FFFFFFF) | tag
;
1988 if (! otfg
->glyph_id
)
1990 switch (otfg
->positioning_type
& 0xF)
1994 case 1: /* Single */
1997 int format
= otfg
->f
.f1
.format
;
1999 if (format
& OTF_XPlacement
)
2001 = otfg
->f
.f1
.value
->XPlacement
* x_scale
/ 0x10000;
2002 if (format
& OTF_XPlaDevice
)
2004 += DEVICE_DELTA (otfg
->f
.f1
.value
->XPlaDevice
, x_ppem
);
2005 if (format
& OTF_YPlacement
)
2007 = - (otfg
->f
.f1
.value
->YPlacement
* y_scale
/ 0x10000);
2008 if (format
& OTF_YPlaDevice
)
2010 -= DEVICE_DELTA (otfg
->f
.f1
.value
->YPlaDevice
, y_ppem
);
2011 if (format
& OTF_XAdvance
)
2013 += otfg
->f
.f1
.value
->XAdvance
* x_scale
/ 0x10000;
2014 if (format
& OTF_XAdvDevice
)
2016 += DEVICE_DELTA (otfg
->f
.f1
.value
->XAdvDevice
, x_ppem
);
2017 if (format
& OTF_YAdvance
)
2019 += otfg
->f
.f1
.value
->YAdvance
* y_scale
/ 0x10000;
2020 if (format
& OTF_YAdvDevice
)
2022 += DEVICE_DELTA (otfg
->f
.f1
.value
->YAdvDevice
, y_ppem
);
2023 adjustment
[i
].set
= 1;
2026 case 3: /* Cursive */
2027 /* Not yet supported. */
2029 case 4: /* Mark-to-Base */
2030 case 5: /* Mark-to-Ligature */
2034 goto label_adjust_anchor
;
2035 default: /* i.e. case 6 Mark-to-Mark */
2040 label_adjust_anchor
:
2042 int base_x
, base_y
, mark_x
, mark_y
;
2043 int this_from
, this_to
;
2045 base_x
= otfg
->f
.f4
.base_anchor
->XCoordinate
* x_scale
/ 0x10000;
2046 base_y
= otfg
->f
.f4
.base_anchor
->YCoordinate
* y_scale
/ 0x10000;
2047 mark_x
= otfg
->f
.f4
.mark_anchor
->XCoordinate
* x_scale
/ 0x10000;
2048 mark_y
= otfg
->f
.f4
.mark_anchor
->YCoordinate
* y_scale
/ 0x10000;
2050 if (otfg
->f
.f4
.base_anchor
->AnchorFormat
!= 1)
2051 adjust_anchor (ft_face
, otfg
->f
.f4
.base_anchor
,
2052 prev
->code
, x_ppem
, y_ppem
, &base_x
, &base_y
);
2053 if (otfg
->f
.f4
.mark_anchor
->AnchorFormat
!= 1)
2054 adjust_anchor (ft_face
, otfg
->f
.f4
.mark_anchor
, g
->code
,
2055 x_ppem
, y_ppem
, &mark_x
, &mark_y
);
2056 adjustment
[i
].xoff
= (base_x
- mark_x
);
2057 adjustment
[i
].yoff
= - (base_y
- mark_y
);
2058 adjustment
[i
].back
= (g
- prev
);
2059 adjustment
[i
].xadv
= 0;
2060 adjustment
[i
].advance_is_absolute
= 1;
2061 adjustment
[i
].set
= 1;
2062 this_from
= g
->from
;
2064 for (j
= 0; prev
+ j
< g
; j
++)
2066 if (this_from
> prev
[j
].from
)
2067 this_from
= prev
[j
].from
;
2068 if (this_to
< prev
[j
].to
)
2069 this_to
= prev
[j
].to
;
2071 for (; prev
<= g
; prev
++)
2073 prev
->from
= this_from
;
2078 if (otfg
->GlyphClass
== OTF_GlyphClass0
)
2080 else if (otfg
->GlyphClass
== OTF_GlyphClassMark
)
2086 else if (gpos_features
)
2088 if (OTF_drive_gpos_with_log (otf
, &otf_gstring
, script
, langsys
,
2094 features
= otf
->gpos
->FeatureList
.Feature
;
2095 for (i
= 0, otfg
= otf_gstring
.glyphs
; i
< otf_gstring
.used
;
2097 if (otfg
->positioning_type
& 0xF)
2099 int feature_idx
= otfg
->positioning_type
>> 4;
2103 unsigned int tag
= features
[feature_idx
- 1].FeatureTag
;
2104 tag
= PACK_OTF_TAG (tag
);
2105 for (j
= otfg
->f
.index
.from
; j
<= otfg
->f
.index
.to
; j
++)
2107 MFLTGlyph
*g
= in
->glyphs
+ (from
+ j
);
2108 g
->internal
= (g
->internal
& ~0x1FFFFFFF) | tag
;
2120 if (out
->allocated
< out
->used
+ len
)
2122 font
->get_metrics (font
, in
, from
, to
);
2123 memcpy (out
->glyphs
+ out
->used
, in
->glyphs
+ from
,
2124 sizeof (MFLTGlyph
) * len
);
2130 ftfont_try_otf (MFLTFont
*font
, MFLTOtfSpec
*spec
,
2131 MFLTGlyphString
*in
, int from
, int to
)
2133 return ftfont_drive_otf (font
, spec
, in
, from
, to
, NULL
, NULL
);
2136 #else /* not M17N_FLT_USE_NEW_FEATURE */
2139 ftfont_drive_otf (MFLTFont
*font
, MFLTOtfSpec
*spec
, MFLTGlyphString
*in
,
2141 MFLTGlyphString
*out
, MFLTGlyphAdjustment
*adjustment
)
2143 struct MFLTFontFT
*flt_font_ft
= (struct MFLTFontFT
*) font
;
2144 FT_Face ft_face
= flt_font_ft
->ft_face
;
2145 OTF
*otf
= flt_font_ft
->otf
;
2146 int len
= to
- from
;
2149 char script
[5], *langsys
= NULL
;
2150 char *gsub_features
= NULL
, *gpos_features
= NULL
;
2154 OTF_tag_name (spec
->script
, script
);
2159 langsys
= langsysbuf
;
2160 OTF_tag_name (spec
->langsys
, langsys
);
2164 for (i
= 0; i
< 2; i
++)
2168 if (spec
->features
[i
] && spec
->features
[i
][1] != 0xFFFFFFFF)
2170 for (j
= 0; spec
->features
[i
][j
]; j
++);
2171 SAFE_NALLOCA (p
, 6, j
);
2176 for (j
= 0; spec
->features
[i
][j
]; j
++)
2178 if (spec
->features
[i
][j
] == 0xFFFFFFFF)
2179 *p
++ = '*', *p
++ = ',';
2182 OTF_tag_name (spec
->features
[i
][j
], p
);
2191 setup_otf_gstring (len
);
2192 for (i
= 0; i
< len
; i
++)
2194 otf_gstring
.glyphs
[i
].c
= in
->glyphs
[from
+ i
].c
;
2195 otf_gstring
.glyphs
[i
].glyph_id
= in
->glyphs
[from
+ i
].code
;
2198 OTF_drive_gdef (otf
, &otf_gstring
);
2203 if (OTF_drive_gsub (otf
, &otf_gstring
, script
, langsys
, gsub_features
)
2206 if (out
->allocated
< out
->used
+ otf_gstring
.used
)
2211 for (i
= 0, otfg
= otf_gstring
.glyphs
; i
< otf_gstring
.used
; )
2214 int min_from
, max_to
;
2217 g
= out
->glyphs
+ out
->used
;
2218 *g
= in
->glyphs
[from
+ otfg
->f
.index
.from
];
2219 if (g
->code
!= otfg
->glyph_id
)
2222 g
->code
= otfg
->glyph_id
;
2228 if (otfg
->f
.index
.from
< otfg
->f
.index
.to
)
2230 /* OTFG substitutes multiple glyphs in IN. */
2231 for (j
= from
+ otfg
->f
.index
.from
+ 1;
2232 j
<= from
+ otfg
->f
.index
.to
; j
++)
2234 if (min_from
> in
->glyphs
[j
].from
)
2235 min_from
= in
->glyphs
[j
].from
;
2236 if (max_to
< in
->glyphs
[j
].to
)
2237 max_to
= in
->glyphs
[j
].to
;
2242 for (i
++, otfg
++; (i
< otf_gstring
.used
2243 && otfg
->f
.index
.from
== otfg
[-1].f
.index
.from
);
2246 g
= out
->glyphs
+ out
->used
;
2247 *g
= in
->glyphs
[from
+ otfg
->f
.index
.to
];
2248 if (g
->code
!= otfg
->glyph_id
)
2251 g
->code
= otfg
->glyph_id
;
2260 if (out
->allocated
< out
->used
+ len
)
2265 for (i
= 0; i
< len
; i
++)
2266 out
->glyphs
[out
->used
++] = in
->glyphs
[from
+ i
];
2271 MFLTGlyph
*base
= NULL
, *mark
= NULL
, *g
;
2272 int x_ppem
, y_ppem
, x_scale
, y_scale
;
2274 if (OTF_drive_gpos (otf
, &otf_gstring
, script
, langsys
, gpos_features
)
2281 x_ppem
= ft_face
->size
->metrics
.x_ppem
;
2282 y_ppem
= ft_face
->size
->metrics
.y_ppem
;
2283 x_scale
= ft_face
->size
->metrics
.x_scale
;
2284 y_scale
= ft_face
->size
->metrics
.y_scale
;
2286 for (i
= 0, otfg
= otf_gstring
.glyphs
, g
= out
->glyphs
+ gidx
;
2287 i
< otf_gstring
.used
; i
++, otfg
++, g
++)
2291 if (! otfg
->glyph_id
)
2293 switch (otfg
->positioning_type
)
2297 case 1: /* Single */
2300 int format
= otfg
->f
.f1
.format
;
2302 if (format
& OTF_XPlacement
)
2304 = otfg
->f
.f1
.value
->XPlacement
* x_scale
/ 0x10000;
2305 if (format
& OTF_XPlaDevice
)
2307 += DEVICE_DELTA (otfg
->f
.f1
.value
->XPlaDevice
, x_ppem
);
2308 if (format
& OTF_YPlacement
)
2310 = - (otfg
->f
.f1
.value
->YPlacement
* y_scale
/ 0x10000);
2311 if (format
& OTF_YPlaDevice
)
2313 -= DEVICE_DELTA (otfg
->f
.f1
.value
->YPlaDevice
, y_ppem
);
2314 if (format
& OTF_XAdvance
)
2316 += otfg
->f
.f1
.value
->XAdvance
* x_scale
/ 0x10000;
2317 if (format
& OTF_XAdvDevice
)
2319 += DEVICE_DELTA (otfg
->f
.f1
.value
->XAdvDevice
, x_ppem
);
2320 if (format
& OTF_YAdvance
)
2322 += otfg
->f
.f1
.value
->YAdvance
* y_scale
/ 0x10000;
2323 if (format
& OTF_YAdvDevice
)
2325 += DEVICE_DELTA (otfg
->f
.f1
.value
->YAdvDevice
, y_ppem
);
2326 adjustment
[i
].set
= 1;
2329 case 3: /* Cursive */
2330 /* Not yet supported. */
2332 case 4: /* Mark-to-Base */
2333 case 5: /* Mark-to-Ligature */
2337 goto label_adjust_anchor
;
2338 default: /* i.e. case 6 Mark-to-Mark */
2343 label_adjust_anchor
:
2345 int base_x
, base_y
, mark_x
, mark_y
;
2346 int this_from
, this_to
;
2348 base_x
= otfg
->f
.f4
.base_anchor
->XCoordinate
* x_scale
/ 0x10000;
2349 base_y
= otfg
->f
.f4
.base_anchor
->YCoordinate
* y_scale
/ 0x10000;
2350 mark_x
= otfg
->f
.f4
.mark_anchor
->XCoordinate
* x_scale
/ 0x10000;
2351 mark_y
= otfg
->f
.f4
.mark_anchor
->YCoordinate
* y_scale
/ 0x10000;
2353 if (otfg
->f
.f4
.base_anchor
->AnchorFormat
!= 1)
2354 adjust_anchor (ft_face
, otfg
->f
.f4
.base_anchor
,
2355 prev
->code
, x_ppem
, y_ppem
, &base_x
, &base_y
);
2356 if (otfg
->f
.f4
.mark_anchor
->AnchorFormat
!= 1)
2357 adjust_anchor (ft_face
, otfg
->f
.f4
.mark_anchor
, g
->code
,
2358 x_ppem
, y_ppem
, &mark_x
, &mark_y
);
2359 adjustment
[i
].xoff
= (base_x
- mark_x
);
2360 adjustment
[i
].yoff
= - (base_y
- mark_y
);
2361 adjustment
[i
].back
= (g
- prev
);
2362 adjustment
[i
].xadv
= 0;
2363 adjustment
[i
].advance_is_absolute
= 1;
2364 adjustment
[i
].set
= 1;
2365 this_from
= g
->from
;
2367 for (j
= 0; prev
+ j
< g
; j
++)
2369 if (this_from
> prev
[j
].from
)
2370 this_from
= prev
[j
].from
;
2371 if (this_to
< prev
[j
].to
)
2372 this_to
= prev
[j
].to
;
2374 for (; prev
<= g
; prev
++)
2376 prev
->from
= this_from
;
2381 if (otfg
->GlyphClass
== OTF_GlyphClass0
)
2383 else if (otfg
->GlyphClass
== OTF_GlyphClassMark
)
2394 if (out
->allocated
< out
->used
+ len
)
2396 font
->get_metrics (font
, in
, from
, to
);
2397 memcpy (out
->glyphs
+ out
->used
, in
->glyphs
+ from
,
2398 sizeof (MFLTGlyph
) * len
);
2403 #endif /* not M17N_FLT_USE_NEW_FEATURE */
2405 static MFLTGlyphString gstring
;
2407 static bool m17n_flt_initialized
;
2410 ftfont_shape_by_flt (Lisp_Object lgstring
, struct font
*font
,
2411 FT_Face ft_face
, OTF
*otf
, FT_Matrix
*matrix
)
2413 ptrdiff_t len
= LGSTRING_GLYPH_LEN (lgstring
);
2415 struct MFLTFontFT flt_font_ft
;
2417 bool with_variation_selector
= 0;
2419 if (! m17n_flt_initialized
)
2422 #ifdef M17N_FLT_USE_NEW_FEATURE
2423 mflt_enable_new_feature
= 1;
2424 mflt_try_otf
= ftfont_try_otf
;
2425 #endif /* M17N_FLT_USE_NEW_FEATURE */
2426 m17n_flt_initialized
= 1;
2429 for (i
= 0; i
< len
; i
++)
2431 Lisp_Object g
= LGSTRING_GLYPH (lgstring
, i
);
2436 c
= LGLYPH_CHAR (g
);
2437 if (CHAR_VARIATION_SELECTOR_P (c
))
2438 with_variation_selector
= 1;
2443 if (with_variation_selector
)
2445 setup_otf_gstring (len
);
2446 for (i
= 0; i
< len
; i
++)
2448 Lisp_Object g
= LGSTRING_GLYPH (lgstring
, i
);
2450 otf_gstring
.glyphs
[i
].c
= LGLYPH_CHAR (g
);
2451 otf_gstring
.glyphs
[i
].f
.index
.from
= LGLYPH_FROM (g
);
2452 otf_gstring
.glyphs
[i
].f
.index
.to
= LGLYPH_TO (g
);
2454 OTF_drive_cmap (otf
, &otf_gstring
);
2455 for (i
= 0; i
< otf_gstring
.used
; i
++)
2457 OTF_Glyph
*otfg
= otf_gstring
.glyphs
+ i
;
2458 Lisp_Object g0
= LGSTRING_GLYPH (lgstring
, otfg
->f
.index
.from
);
2459 Lisp_Object g1
= LGSTRING_GLYPH (lgstring
, otfg
->f
.index
.to
);
2461 LGLYPH_SET_CODE (g0
, otfg
->glyph_id
);
2462 LGLYPH_SET_TO (g0
, LGLYPH_TO (g1
));
2463 LGSTRING_SET_GLYPH (lgstring
, i
, g0
);
2465 if (len
> otf_gstring
.used
)
2467 len
= otf_gstring
.used
;
2468 LGSTRING_SET_GLYPH (lgstring
, len
, Qnil
);
2472 if (INT_MAX
/ 2 < len
)
2473 memory_full (SIZE_MAX
);
2475 if (gstring
.allocated
== 0)
2477 gstring
.glyph_size
= sizeof (MFLTGlyph
);
2478 gstring
.glyphs
= xnmalloc (len
* 2, sizeof *gstring
.glyphs
);
2479 gstring
.allocated
= len
* 2;
2481 else if (gstring
.allocated
< len
* 2)
2483 gstring
.glyphs
= xnrealloc (gstring
.glyphs
, len
* 2,
2484 sizeof *gstring
.glyphs
);
2485 gstring
.allocated
= len
* 2;
2487 memset (gstring
.glyphs
, 0, len
* sizeof *gstring
.glyphs
);
2488 for (i
= 0; i
< len
; i
++)
2490 Lisp_Object g
= LGSTRING_GLYPH (lgstring
, i
);
2492 gstring
.glyphs
[i
].c
= LGLYPH_CHAR (g
);
2493 if (with_variation_selector
)
2495 gstring
.glyphs
[i
].code
= LGLYPH_CODE (g
);
2496 gstring
.glyphs
[i
].encoded
= 1;
2504 Lisp_Object family
= Ffont_get (LGSTRING_FONT (lgstring
), QCfamily
);
2507 flt_font_ft
.flt_font
.family
= Mnil
;
2509 flt_font_ft
.flt_font
.family
2510 = msymbol (SSDATA (Fdowncase (SYMBOL_NAME (family
))));
2512 flt_font_ft
.flt_font
.x_ppem
= ft_face
->size
->metrics
.x_ppem
;
2513 flt_font_ft
.flt_font
.y_ppem
= ft_face
->size
->metrics
.y_ppem
;
2514 flt_font_ft
.flt_font
.get_glyph_id
= ftfont_get_glyph_id
;
2515 flt_font_ft
.flt_font
.get_metrics
= ftfont_get_metrics
;
2516 flt_font_ft
.flt_font
.check_otf
= ftfont_check_otf
;
2517 flt_font_ft
.flt_font
.drive_otf
= ftfont_drive_otf
;
2518 flt_font_ft
.flt_font
.internal
= NULL
;
2519 flt_font_ft
.font
= font
;
2520 flt_font_ft
.ft_face
= ft_face
;
2521 flt_font_ft
.otf
= otf
;
2522 flt_font_ft
.matrix
= matrix
->xx
!= 0 ? matrix
: 0;
2524 && gstring
.glyphs
[1].c
>= 0x300 && gstring
.glyphs
[1].c
<= 0x36F)
2525 /* A little bit ad hoc. Perhaps, shaper must get script and
2526 language information, and select a proper flt for them
2528 flt
= mflt_get (msymbol ("combining"));
2529 for (i
= 0; i
< 3; i
++)
2531 int result
= mflt_run (&gstring
, 0, len
, &flt_font_ft
.flt_font
, flt
);
2534 if (INT_MAX
/ 2 < gstring
.allocated
)
2535 memory_full (SIZE_MAX
);
2536 gstring
.glyphs
= xnrealloc (gstring
.glyphs
,
2537 gstring
.allocated
, 2 * sizeof (MFLTGlyph
));
2538 gstring
.allocated
*= 2;
2540 if (gstring
.used
> LGSTRING_GLYPH_LEN (lgstring
))
2542 for (i
= 0; i
< gstring
.used
; i
++)
2544 MFLTGlyph
*g
= gstring
.glyphs
+ i
;
2546 g
->from
= LGLYPH_FROM (LGSTRING_GLYPH (lgstring
, g
->from
));
2547 g
->to
= LGLYPH_TO (LGSTRING_GLYPH (lgstring
, g
->to
));
2550 for (i
= 0; i
< gstring
.used
; i
++)
2552 Lisp_Object lglyph
= LGSTRING_GLYPH (lgstring
, i
);
2553 MFLTGlyph
*g
= gstring
.glyphs
+ i
;
2557 lglyph
= LGLYPH_NEW ();
2558 LGSTRING_SET_GLYPH (lgstring
, i
, lglyph
);
2560 LGLYPH_SET_FROM (lglyph
, g
->from
);
2561 LGLYPH_SET_TO (lglyph
, g
->to
);
2562 LGLYPH_SET_CHAR (lglyph
, g
->c
);
2563 LGLYPH_SET_CODE (lglyph
, g
->code
);
2564 LGLYPH_SET_WIDTH (lglyph
, g
->xadv
>> 6);
2565 LGLYPH_SET_LBEARING (lglyph
, g
->lbearing
>> 6);
2566 LGLYPH_SET_RBEARING (lglyph
, g
->rbearing
>> 6);
2567 LGLYPH_SET_ASCENT (lglyph
, g
->ascent
>> 6);
2568 LGLYPH_SET_DESCENT (lglyph
, g
->descent
>> 6);
2571 Lisp_Object vec
= make_uninit_vector (3);
2573 ASET (vec
, 0, make_number (g
->xoff
>> 6));
2574 ASET (vec
, 1, make_number (g
->yoff
>> 6));
2575 ASET (vec
, 2, make_number (g
->xadv
>> 6));
2576 LGLYPH_SET_ADJUSTMENT (lglyph
, vec
);
2579 return make_number (i
);
2583 ftfont_shape (Lisp_Object lgstring
)
2586 struct ftfont_info
*ftfont_info
;
2589 CHECK_FONT_GET_OBJECT (LGSTRING_FONT (lgstring
), font
);
2590 ftfont_info
= (struct ftfont_info
*) font
;
2591 otf
= ftfont_get_otf (ftfont_info
);
2593 return make_number (0);
2594 return ftfont_shape_by_flt (lgstring
, font
, ftfont_info
->ft_size
->face
, otf
,
2595 &ftfont_info
->matrix
);
2598 #endif /* HAVE_M17N_FLT */
2600 #ifdef HAVE_OTF_GET_VARIATION_GLYPHS
2603 ftfont_variation_glyphs (struct font
*font
, int c
, unsigned variations
[256])
2605 struct ftfont_info
*ftfont_info
= (struct ftfont_info
*) font
;
2606 OTF
*otf
= ftfont_get_otf (ftfont_info
);
2610 return OTF_get_variation_glyphs (otf
, c
, variations
);
2613 #endif /* HAVE_OTF_GET_VARIATION_GLYPHS */
2614 #endif /* HAVE_LIBOTF */
2616 static const char *const ftfont_booleans
[] = {
2629 static const char *const ftfont_non_booleans
[] = {
2661 ftfont_filter_properties (Lisp_Object font
, Lisp_Object alist
)
2663 font_filter_properties (font
, alist
, ftfont_booleans
, ftfont_non_booleans
);
2668 syms_of_ftfont (void)
2670 DEFSYM (Qfreetype
, "freetype");
2671 DEFSYM (Qmonospace
, "monospace");
2672 DEFSYM (Qsans_serif
, "sans-serif");
2673 DEFSYM (Qserif
, "serif");
2674 DEFSYM (Qmono
, "mono");
2675 DEFSYM (Qsans
, "sans");
2676 DEFSYM (Qsans__serif
, "sans serif");
2678 staticpro (&freetype_font_cache
);
2679 freetype_font_cache
= list1 (Qt
);
2681 staticpro (&ftfont_generic_family_list
);
2682 ftfont_generic_family_list
= list3 (Fcons (Qmonospace
, Qt
),
2683 Fcons (Qsans_serif
, Qt
),
2686 staticpro (&ft_face_cache
);
2687 ft_face_cache
= Qnil
;
2689 ftfont_driver
.type
= Qfreetype
;
2690 register_font_driver (&ftfont_driver
, NULL
);