Commit | Line | Data |
---|---|---|
c2f5bfd6 | 1 | /* ftfont.c -- FreeType font driver. |
77ad4cfe GM |
2 | Copyright (C) 2006, 2007, 2008 Free Software Foundation, Inc. |
3 | Copyright (C) 2006, 2007, 2008 | |
c2f5bfd6 KH |
4 | National Institute of Advanced Industrial Science and Technology (AIST) |
5 | Registration Number H13PRO009 | |
6 | ||
7 | This file is part of GNU Emacs. | |
8 | ||
9ec0b715 | 9 | GNU Emacs is free software: you can redistribute it and/or modify |
c2f5bfd6 | 10 | it under the terms of the GNU General Public License as published by |
9ec0b715 GM |
11 | the Free Software Foundation, either version 3 of the License, or |
12 | (at your option) any later version. | |
c2f5bfd6 KH |
13 | |
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. | |
18 | ||
19 | You should have received a copy of the GNU General Public License | |
9ec0b715 | 20 | along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */ |
c2f5bfd6 KH |
21 | |
22 | #include <config.h> | |
23 | #include <stdio.h> | |
24 | ||
c2f5bfd6 KH |
25 | #include <fontconfig/fontconfig.h> |
26 | #include <fontconfig/fcfreetype.h> | |
27 | ||
28 | #include "lisp.h" | |
29 | #include "dispextern.h" | |
30 | #include "frame.h" | |
31 | #include "blockinput.h" | |
32 | #include "character.h" | |
33 | #include "charset.h" | |
34 | #include "coding.h" | |
89a95b7c | 35 | #include "composite.h" |
c2f5bfd6 KH |
36 | #include "fontset.h" |
37 | #include "font.h" | |
de023c40 | 38 | #include "ftfont.h" |
c2f5bfd6 | 39 | |
c2801c99 | 40 | /* Symbolic type of this font-driver. */ |
c2f5bfd6 KH |
41 | Lisp_Object Qfreetype; |
42 | ||
706b6995 KH |
43 | /* Fontconfig's generic families and their aliases. */ |
44 | static Lisp_Object Qmonospace, Qsans_serif, Qserif, Qmono, Qsans, Qsans__serif; | |
45 | ||
2a46904e | 46 | /* Flag to tell if FcInit is already called or not. */ |
c2f5bfd6 | 47 | static int fc_initialized; |
c2801c99 KH |
48 | |
49 | /* Handle to a FreeType library instance. */ | |
c2f5bfd6 KH |
50 | static FT_Library ft_library; |
51 | ||
c2801c99 | 52 | /* Cache for FreeType fonts. */ |
c2f5bfd6 KH |
53 | static Lisp_Object freetype_font_cache; |
54 | ||
e302a291 | 55 | /* Cache for FT_Face and FcCharSet. */ |
21988a08 | 56 | static Lisp_Object ft_face_cache; |
c2f5bfd6 KH |
57 | |
58 | /* The actual structure for FreeType font that can be casted to struct | |
59 | font. */ | |
60 | ||
61 | struct ftfont_info | |
62 | { | |
63 | struct font font; | |
de023c40 | 64 | #ifdef HAVE_LIBOTF |
e302a291 KH |
65 | /* The following three members must be here in this order to be |
66 | compatible with struct xftfont_info (in xftfont.c). */ | |
de023c40 KH |
67 | int maybe_otf; /* Flag to tell if this may be OTF or not. */ |
68 | OTF *otf; | |
69 | #endif /* HAVE_LIBOTF */ | |
e302a291 KH |
70 | FT_Size ft_size; |
71 | int index; | |
c2f5bfd6 KH |
72 | }; |
73 | ||
e302a291 | 74 | static Lisp_Object ftfont_pattern_entity P_ ((FcPattern *, Lisp_Object)); |
21988a08 | 75 | |
7d7ad10e KH |
76 | static Lisp_Object ftfont_resolve_generic_family P_ ((Lisp_Object, |
77 | FcPattern *)); | |
e302a291 KH |
78 | static Lisp_Object ftfont_lookup_cache P_ ((Lisp_Object, int)); |
79 | ||
7d7ad10e | 80 | Lisp_Object ftfont_font_format P_ ((FcPattern *, Lisp_Object)); |
706b6995 KH |
81 | |
82 | #define SYMBOL_FcChar8(SYM) (FcChar8 *) SDATA (SYMBOL_NAME (SYM)) | |
83 | ||
21988a08 | 84 | static struct |
c2f5bfd6 | 85 | { |
e302a291 | 86 | /* registry name */ |
21988a08 KH |
87 | char *name; |
88 | /* characters to distinguish the charset from the others */ | |
89 | int uniquifier[6]; | |
7d7ad10e KH |
90 | /* additional constraint by language */ |
91 | char *lang; | |
21988a08 KH |
92 | /* set on demand */ |
93 | FcCharSet *fc_charset; | |
94 | } fc_charset_table[] = | |
e302a291 KH |
95 | { { "iso8859-1", { 0x00A0, 0x00A1, 0x00B4, 0x00BC, 0x00D0 } }, |
96 | { "iso8859-2", { 0x00A0, 0x010E }}, | |
97 | { "iso8859-3", { 0x00A0, 0x0108 }}, | |
98 | { "iso8859-4", { 0x00A0, 0x00AF, 0x0128, 0x0156, 0x02C7 }}, | |
99 | { "iso8859-5", { 0x00A0, 0x0401 }}, | |
100 | { "iso8859-6", { 0x00A0, 0x060C }}, | |
101 | { "iso8859-7", { 0x00A0, 0x0384 }}, | |
102 | { "iso8859-8", { 0x00A0, 0x05D0 }}, | |
103 | { "iso8859-9", { 0x00A0, 0x00A1, 0x00BC, 0x011E }}, | |
104 | { "iso8859-10", { 0x00A0, 0x00D0, 0x0128, 0x2015 }}, | |
105 | { "iso8859-11", { 0x00A0, 0x0E01 }}, | |
106 | { "iso8859-13", { 0x00A0, 0x201C }}, | |
107 | { "iso8859-14", { 0x00A0, 0x0174 }}, | |
108 | { "iso8859-15", { 0x00A0, 0x00A1, 0x00D0, 0x0152 }}, | |
109 | { "iso8859-16", { 0x00A0, 0x0218}}, | |
110 | { "gb2312.1980-0", { 0x4E13 }, "zh-cn"}, | |
111 | { "big5-0", { 0xF6B1 }, "zh-tw" }, | |
112 | { "jisx0208.1983-0", { 0x4E55 }, "ja"}, | |
113 | { "ksc5601.1985-0", { 0xAC00 }, "ko"}, | |
114 | { "cns11643.1992-1", { 0xFE32 }, "zh-tw"}, | |
115 | { "cns11643.1992-2", { 0x4E33, 0x7934 }}, | |
116 | { "cns11643.1992-3", { 0x201A9 }}, | |
117 | { "cns11643.1992-4", { 0x20057 }}, | |
118 | { "cns11643.1992-5", { 0x20000 }}, | |
119 | { "cns11643.1992-6", { 0x20003 }}, | |
120 | { "cns11643.1992-7", { 0x20055 }}, | |
121 | { "gbk-0", { 0x4E06 }, "zh-cn"}, | |
122 | { "jisx0212.1990-0", { 0x4E44 }}, | |
123 | { "jisx0213.2000-1", { 0xFA10 }, "ja"}, | |
124 | { "jisx0213.2000-2", { 0xFA49 }}, | |
125 | { "jisx0213.2004-1", { 0x20B9F }}, | |
126 | { "viscii1.1-1", { 0x1EA0, 0x1EAE, 0x1ED2 }, "vi"}, | |
127 | { "tis620.2529-1", { 0x0E01 }, "th"}, | |
7d7ad10e KH |
128 | { "windows-1251", { 0x0401, 0x0490 }, "ru"}, |
129 | { "koi8-r", { 0x0401, 0x2219 }, "ru"}, | |
e302a291 | 130 | { "mulelao-1", { 0x0E81 }, "lo"}, |
7b649478 | 131 | { "unicode-sip", { 0x20000 }}, |
21988a08 KH |
132 | { NULL } |
133 | }; | |
c2f5bfd6 | 134 | |
42984a74 KH |
135 | extern Lisp_Object Qc, Qm, Qp, Qd; |
136 | ||
706b6995 | 137 | static Lisp_Object |
e302a291 | 138 | ftfont_pattern_entity (p, extra) |
c2801c99 | 139 | FcPattern *p; |
e302a291 | 140 | Lisp_Object extra; |
c2801c99 KH |
141 | { |
142 | Lisp_Object entity; | |
21988a08 | 143 | char *file, *str; |
e302a291 | 144 | int index; |
c2801c99 KH |
145 | int numeric; |
146 | double dbl; | |
42984a74 | 147 | FcBool b; |
c2801c99 | 148 | |
21988a08 | 149 | if (FcPatternGetString (p, FC_FILE, 0, (FcChar8 **) &file) != FcResultMatch) |
c2801c99 | 150 | return Qnil; |
e302a291 KH |
151 | if (FcPatternGetInteger (p, FC_INDEX, 0, &index) != FcResultMatch) |
152 | return Qnil; | |
c2801c99 | 153 | |
42984a74 | 154 | entity = font_make_entity (); |
c2801c99 KH |
155 | |
156 | ASET (entity, FONT_TYPE_INDEX, Qfreetype); | |
e302a291 | 157 | ASET (entity, FONT_REGISTRY_INDEX, Qiso10646_1); |
c2801c99 | 158 | |
21988a08 | 159 | if (FcPatternGetString (p, FC_FOUNDRY, 0, (FcChar8 **) &str) == FcResultMatch) |
a1a94102 | 160 | ASET (entity, FONT_FOUNDRY_INDEX, font_intern_prop (str, strlen (str), 1)); |
21988a08 | 161 | if (FcPatternGetString (p, FC_FAMILY, 0, (FcChar8 **) &str) == FcResultMatch) |
a1a94102 | 162 | ASET (entity, FONT_FAMILY_INDEX, font_intern_prop (str, strlen (str), 1)); |
c2801c99 | 163 | if (FcPatternGetInteger (p, FC_WEIGHT, 0, &numeric) == FcResultMatch) |
f63d54dc | 164 | { |
42984a74 KH |
165 | if (numeric >= FC_WEIGHT_REGULAR && numeric < FC_WEIGHT_MEDIUM) |
166 | numeric = FC_WEIGHT_MEDIUM; | |
167 | FONT_SET_STYLE (entity, FONT_WEIGHT_INDEX, make_number (numeric)); | |
f63d54dc | 168 | } |
c2801c99 | 169 | if (FcPatternGetInteger (p, FC_SLANT, 0, &numeric) == FcResultMatch) |
42984a74 KH |
170 | { |
171 | numeric += 100; | |
172 | FONT_SET_STYLE (entity, FONT_SLANT_INDEX, make_number (numeric)); | |
173 | } | |
c2801c99 | 174 | if (FcPatternGetInteger (p, FC_WIDTH, 0, &numeric) == FcResultMatch) |
42984a74 KH |
175 | { |
176 | FONT_SET_STYLE (entity, FONT_WIDTH_INDEX, make_number (numeric)); | |
177 | } | |
c2801c99 KH |
178 | if (FcPatternGetDouble (p, FC_PIXEL_SIZE, 0, &dbl) == FcResultMatch) |
179 | ASET (entity, FONT_SIZE_INDEX, make_number (dbl)); | |
180 | else | |
181 | ASET (entity, FONT_SIZE_INDEX, make_number (0)); | |
42984a74 KH |
182 | if (FcPatternGetInteger (p, FC_SPACING, 0, &numeric) == FcResultMatch) |
183 | ASET (entity, FONT_SPACING_INDEX, make_number (numeric)); | |
184 | if (FcPatternGetDouble (p, FC_DPI, 0, &dbl) == FcResultMatch) | |
185 | { | |
186 | int dpi = dbl; | |
187 | ASET (entity, FONT_DPI_INDEX, make_number (dpi)); | |
188 | } | |
189 | if (FcPatternGetBool (p, FC_SCALABLE, 0, &b) == FcResultMatch | |
190 | && b == FcTrue) | |
191 | ASET (entity, FONT_AVGWIDTH_INDEX, make_number (0)); | |
c2801c99 | 192 | |
4ffe34a4 | 193 | ASET (entity, FONT_EXTRA_INDEX, Fcopy_sequence (extra)); |
21988a08 KH |
194 | font_put_extra (entity, QCfont_entity, |
195 | Fcons (make_unibyte_string ((char *) file, | |
196 | strlen ((char *) file)), | |
e302a291 | 197 | make_number (index))); |
c2801c99 KH |
198 | return entity; |
199 | } | |
200 | ||
42984a74 | 201 | |
706b6995 KH |
202 | static Lisp_Object ftfont_generic_family_list; |
203 | ||
204 | static Lisp_Object | |
7d7ad10e | 205 | ftfont_resolve_generic_family (family, pattern) |
42984a74 | 206 | Lisp_Object family; |
7d7ad10e | 207 | FcPattern *pattern; |
706b6995 | 208 | { |
318548be | 209 | Lisp_Object slot; |
7d7ad10e | 210 | FcPattern *match; |
318548be | 211 | FcResult result; |
89a95b7c | 212 | FcLangSet *langset; |
706b6995 | 213 | |
318548be | 214 | family = Fintern (Fdowncase (SYMBOL_NAME (family)), Qnil); |
706b6995 KH |
215 | if (EQ (family, Qmono)) |
216 | family = Qmonospace; | |
217 | else if (EQ (family, Qsans) || EQ (family, Qsans__serif)) | |
218 | family = Qsans_serif; | |
219 | slot = assq_no_quit (family, ftfont_generic_family_list); | |
220 | if (! CONSP (slot)) | |
42984a74 | 221 | return Qnil; |
318548be KH |
222 | if (! EQ (XCDR (slot), Qt)) |
223 | return XCDR (slot); | |
7d7ad10e | 224 | pattern = FcPatternDuplicate (pattern); |
42984a74 KH |
225 | if (! pattern) |
226 | goto err; | |
bb658864 | 227 | FcPatternDel (pattern, FC_FOUNDRY); |
7d7ad10e KH |
228 | FcPatternDel (pattern, FC_FAMILY); |
229 | FcPatternAddString (pattern, FC_FAMILY, SYMBOL_FcChar8 (family)); | |
89a95b7c KH |
230 | if (FcPatternGetLangSet (pattern, FC_LANG, 0, &langset) != FcResultMatch) |
231 | { | |
232 | /* This is to avoid the effect of locale. */ | |
233 | langset = FcLangSetCreate (); | |
234 | FcLangSetAdd (langset, "en"); | |
235 | FcPatternAddLangSet (pattern, FC_LANG, langset); | |
236 | FcLangSetDestroy (langset); | |
237 | } | |
42984a74 | 238 | FcConfigSubstitute (NULL, pattern, FcMatchPattern); |
318548be KH |
239 | FcDefaultSubstitute (pattern); |
240 | match = FcFontMatch (NULL, pattern, &result); | |
241 | if (match) | |
706b6995 | 242 | { |
318548be KH |
243 | FcChar8 *fam; |
244 | ||
245 | if (FcPatternGetString (match, FC_FAMILY, 0, &fam) == FcResultMatch) | |
246 | family = intern ((char *) fam); | |
706b6995 | 247 | } |
318548be KH |
248 | else |
249 | family = Qnil; | |
250 | XSETCDR (slot, family); | |
318548be | 251 | if (match) FcPatternDestroy (match); |
bf39cdd8 | 252 | err: |
42984a74 | 253 | if (pattern) FcPatternDestroy (pattern); |
318548be | 254 | return family; |
706b6995 KH |
255 | } |
256 | ||
e302a291 KH |
257 | struct ftfont_cache_data |
258 | { | |
259 | FT_Face ft_face; | |
260 | FcCharSet *fc_charset; | |
261 | }; | |
262 | ||
263 | static Lisp_Object | |
264 | ftfont_lookup_cache (key, for_face) | |
265 | Lisp_Object key; | |
266 | int for_face; | |
21988a08 KH |
267 | { |
268 | Lisp_Object cache, val; | |
e302a291 | 269 | struct ftfont_cache_data *cache_data; |
21988a08 | 270 | |
e302a291 | 271 | cache = assoc_no_quit (key, ft_face_cache); |
21988a08 KH |
272 | if (NILP (cache)) |
273 | { | |
e302a291 KH |
274 | cache_data = xmalloc (sizeof (struct ftfont_cache_data)); |
275 | cache_data->ft_face = NULL; | |
276 | cache_data->fc_charset = NULL; | |
21988a08 | 277 | val = make_save_value (NULL, 0); |
e302a291 KH |
278 | XSAVE_VALUE (val)->integer = 0; |
279 | XSAVE_VALUE (val)->pointer = cache_data; | |
280 | cache = Fcons (key, val); | |
21988a08 KH |
281 | ft_face_cache = Fcons (cache, ft_face_cache); |
282 | } | |
283 | else | |
21988a08 | 284 | { |
e302a291 KH |
285 | val = XCDR (cache); |
286 | cache_data = XSAVE_VALUE (val)->pointer; | |
287 | } | |
288 | if (for_face ? ! cache_data->ft_face : ! cache_data->fc_charset) | |
289 | { | |
290 | char *filename = (char *) SDATA (XCAR (key)); | |
291 | int index = XINT (XCDR (key)); | |
21988a08 | 292 | |
e302a291 KH |
293 | if (for_face) |
294 | { | |
295 | if (! ft_library | |
296 | && FT_Init_FreeType (&ft_library) != 0) | |
297 | return Qnil; | |
298 | if (FT_New_Face (ft_library, filename, index, &cache_data->ft_face) | |
299 | != 0) | |
300 | return Qnil; | |
301 | } | |
302 | else | |
303 | { | |
304 | FcPattern *pat; | |
305 | FcFontSet *fontset; | |
306 | FcObjectSet *objset; | |
307 | FcCharSet *charset; | |
308 | ||
309 | pat = FcPatternBuild (0, FC_FILE, FcTypeString, (FcChar8 *) filename, | |
310 | FC_INDEX, FcTypeInteger, index, NULL); | |
311 | objset = FcObjectSetBuild (FC_CHARSET, NULL); | |
312 | fontset = FcFontList (NULL, pat, objset); | |
313 | xassert (fontset && fontset->nfont > 0); | |
314 | if (FcPatternGetCharSet (fontset->fonts[0], FC_CHARSET, 0, &charset) | |
315 | == FcResultMatch) | |
316 | cache_data->fc_charset = FcCharSetCopy (charset); | |
317 | else | |
318 | cache_data->fc_charset = FcCharSetCreate (); | |
319 | FcFontSetDestroy (fontset); | |
320 | FcObjectSetDestroy (objset); | |
321 | FcPatternDestroy (pat); | |
322 | } | |
21988a08 KH |
323 | } |
324 | return cache; | |
325 | } | |
326 | ||
e302a291 KH |
327 | FcCharSet * |
328 | ftfont_get_fc_charset (entity) | |
329 | Lisp_Object entity; | |
330 | { | |
331 | Lisp_Object val, cache; | |
332 | struct ftfont_cache_data *cache_data; | |
333 | ||
334 | val = assq_no_quit (QCfont_entity, AREF (entity, FONT_EXTRA_INDEX)); | |
335 | xassert (CONSP (val)); | |
336 | val = XCDR (val); | |
337 | cache = ftfont_lookup_cache (val, 0); | |
338 | val = XCDR (cache); | |
339 | cache_data = XSAVE_VALUE (val)->pointer; | |
340 | return cache_data->fc_charset; | |
341 | } | |
342 | ||
343 | #ifdef HAVE_LIBOTF | |
344 | static OTF * | |
345 | ftfont_get_otf (ftfont_info) | |
346 | struct ftfont_info *ftfont_info; | |
347 | { | |
348 | OTF *otf; | |
349 | ||
350 | if (ftfont_info->otf) | |
351 | return ftfont_info->otf; | |
352 | if (! ftfont_info->maybe_otf) | |
353 | return NULL; | |
354 | otf = OTF_open_ft_face (ftfont_info->ft_size->face); | |
355 | if (! otf || OTF_get_table (otf, "head") < 0) | |
356 | { | |
357 | if (otf) | |
358 | OTF_close (otf); | |
359 | ftfont_info->maybe_otf = 0; | |
360 | return NULL; | |
361 | } | |
362 | ftfont_info->otf = otf; | |
363 | return otf; | |
364 | } | |
365 | #endif /* HAVE_LIBOTF */ | |
366 | ||
77175228 | 367 | static Lisp_Object ftfont_get_cache P_ ((FRAME_PTR)); |
c2f5bfd6 | 368 | static Lisp_Object ftfont_list P_ ((Lisp_Object, Lisp_Object)); |
8daf5667 | 369 | static Lisp_Object ftfont_match P_ ((Lisp_Object, Lisp_Object)); |
c2f5bfd6 | 370 | static Lisp_Object ftfont_list_family P_ ((Lisp_Object)); |
42984a74 | 371 | static Lisp_Object ftfont_open P_ ((FRAME_PTR, Lisp_Object, int)); |
c2f5bfd6 KH |
372 | static void ftfont_close P_ ((FRAME_PTR, struct font *)); |
373 | static int ftfont_has_char P_ ((Lisp_Object, int)); | |
374 | static unsigned ftfont_encode_char P_ ((struct font *, int)); | |
375 | static int ftfont_text_extents P_ ((struct font *, unsigned *, int, | |
376 | struct font_metrics *)); | |
377 | static int ftfont_get_bitmap P_ ((struct font *, unsigned, | |
378 | struct font_bitmap *, int)); | |
379 | static int ftfont_anchor_point P_ ((struct font *, unsigned, int, | |
380 | int *, int *)); | |
e302a291 | 381 | static Lisp_Object ftfont_otf_capability P_ ((struct font *)); |
de023c40 | 382 | static Lisp_Object ftfont_shape P_ ((Lisp_Object)); |
2b33f1ef KH |
383 | static int ftfont_variation_glyphs P_ ((struct font *, int c, |
384 | unsigned variations[256])); | |
c2f5bfd6 KH |
385 | |
386 | struct font_driver ftfont_driver = | |
387 | { | |
09a56ce4 | 388 | 0, /* Qfreetype */ |
42984a74 | 389 | 0, /* case insensitive */ |
c2f5bfd6 | 390 | ftfont_get_cache, |
c2f5bfd6 | 391 | ftfont_list, |
8daf5667 | 392 | ftfont_match, |
c2f5bfd6 | 393 | ftfont_list_family, |
2b33f1ef | 394 | NULL, /* free_entity */ |
c2f5bfd6 KH |
395 | ftfont_open, |
396 | ftfont_close, | |
397 | /* We can't draw a text without device dependent functions. */ | |
2b33f1ef KH |
398 | NULL, /* prepare_face */ |
399 | NULL, /* done_face */ | |
c2f5bfd6 KH |
400 | ftfont_has_char, |
401 | ftfont_encode_char, | |
402 | ftfont_text_extents, | |
403 | /* We can't draw a text without device dependent functions. */ | |
2b33f1ef | 404 | NULL, /* draw */ |
c2f5bfd6 | 405 | ftfont_get_bitmap, |
2b33f1ef KH |
406 | NULL, /* get_bitmap */ |
407 | NULL, /* free_bitmap */ | |
408 | NULL, /* get_outline */ | |
c2f5bfd6 | 409 | ftfont_anchor_point, |
e302a291 KH |
410 | #ifdef HAVE_LIBOTF |
411 | ftfont_otf_capability, | |
412 | #else /* not HAVE_LIBOTF */ | |
c2f5bfd6 | 413 | NULL, |
e302a291 | 414 | #endif /* not HAVE_LIBOTF */ |
2b33f1ef KH |
415 | NULL, /* otf_drive */ |
416 | NULL, /* start_for_frame */ | |
417 | NULL, /* end_for_frame */ | |
637ac44c | 418 | #if defined (HAVE_M17N_FLT) && defined (HAVE_LIBOTF) |
2b33f1ef | 419 | ftfont_shape, |
637ac44c | 420 | #else /* not (HAVE_M17N_FLT && HAVE_LIBOTF) */ |
2b33f1ef | 421 | NULL, |
637ac44c | 422 | #endif /* not (HAVE_M17N_FLT && HAVE_LIBOTF) */ |
2b33f1ef KH |
423 | NULL, /* check */ |
424 | #ifdef HAVE_OTF_GET_VARIATION_GLYPHS | |
425 | ftfont_variation_glyphs | |
426 | #else | |
427 | NULL | |
428 | #endif | |
c2f5bfd6 KH |
429 | }; |
430 | ||
c2f5bfd6 KH |
431 | extern Lisp_Object QCname; |
432 | ||
433 | static Lisp_Object | |
77175228 KH |
434 | ftfont_get_cache (f) |
435 | FRAME_PTR f; | |
c2f5bfd6 | 436 | { |
c2f5bfd6 KH |
437 | return freetype_font_cache; |
438 | } | |
439 | ||
21988a08 KH |
440 | static int |
441 | ftfont_get_charset (registry) | |
442 | Lisp_Object registry; | |
443 | { | |
e302a291 KH |
444 | char *str = (char *) SDATA (SYMBOL_NAME (registry)); |
445 | char *re = alloca (SBYTES (SYMBOL_NAME (registry)) * 2 + 1); | |
446 | Lisp_Object regexp; | |
21988a08 KH |
447 | int i, j; |
448 | ||
e302a291 KH |
449 | for (i = j = 0; i < SBYTES (SYMBOL_NAME (registry)); i++, j++) |
450 | { | |
451 | if (str[i] == '.') | |
452 | re[j++] = '\\'; | |
453 | else if (str[i] == '*') | |
454 | re[j++] = '.'; | |
455 | re[j] = str[i]; | |
456 | if (re[j] == '?') | |
457 | re[j] = '.'; | |
458 | } | |
459 | re[j] = '\0'; | |
460 | regexp = make_unibyte_string (re, j); | |
21988a08 | 461 | for (i = 0; fc_charset_table[i].name; i++) |
e302a291 | 462 | if (fast_c_string_match_ignore_case (regexp, fc_charset_table[i].name) >= 0) |
21988a08 KH |
463 | break; |
464 | if (! fc_charset_table[i].name) | |
465 | return -1; | |
466 | if (! fc_charset_table[i].fc_charset) | |
467 | { | |
468 | FcCharSet *charset = FcCharSetCreate (); | |
469 | int *uniquifier = fc_charset_table[i].uniquifier; | |
470 | ||
471 | if (! charset) | |
472 | return -1; | |
473 | for (j = 0; uniquifier[j]; j++) | |
474 | if (! FcCharSetAddChar (charset, uniquifier[j])) | |
475 | { | |
476 | FcCharSetDestroy (charset); | |
477 | return -1; | |
478 | } | |
2a46904e | 479 | fc_charset_table[i].fc_charset = charset; |
21988a08 KH |
480 | } |
481 | return i; | |
482 | } | |
483 | ||
cc63eaf9 KH |
484 | struct OpenTypeSpec |
485 | { | |
16963817 KH |
486 | Lisp_Object script; |
487 | unsigned int script_tag, langsys_tag; | |
cc63eaf9 KH |
488 | int nfeatures[2]; |
489 | unsigned int *features[2]; | |
490 | }; | |
491 | ||
7eb5d3d7 | 492 | #define OTF_SYM_TAG(SYM, TAG) \ |
cc63eaf9 | 493 | do { \ |
7eb5d3d7 KH |
494 | unsigned char *p = SDATA (SYMBOL_NAME (SYM)); \ |
495 | TAG = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]; \ | |
cc63eaf9 KH |
496 | } while (0) |
497 | ||
7eb5d3d7 | 498 | #define OTF_TAG_STR(TAG, P) \ |
cc63eaf9 | 499 | do { \ |
7eb5d3d7 KH |
500 | (P)[0] = (char) (TAG >> 24); \ |
501 | (P)[1] = (char) ((TAG >> 16) & 0xFF); \ | |
502 | (P)[2] = (char) ((TAG >> 8) & 0xFF); \ | |
503 | (P)[3] = (char) (TAG & 0xFF); \ | |
16963817 | 504 | (P)[4] = '\0'; \ |
cc63eaf9 KH |
505 | } while (0) |
506 | ||
e302a291 KH |
507 | #define OTF_TAG_SYM(SYM, TAG) \ |
508 | do { \ | |
509 | char str[5]; \ | |
510 | \ | |
511 | OTF_TAG_STR (TAG, str); \ | |
512 | (SYM) = font_intern_prop (str, 4, 1); \ | |
513 | } while (0) | |
514 | ||
515 | ||
cc63eaf9 KH |
516 | static struct OpenTypeSpec * |
517 | ftfont_get_open_type_spec (Lisp_Object otf_spec) | |
518 | { | |
519 | struct OpenTypeSpec *spec = malloc (sizeof (struct OpenTypeSpec)); | |
520 | Lisp_Object val; | |
521 | int i, j, negative; | |
522 | ||
523 | if (! spec) | |
524 | return NULL; | |
16963817 | 525 | spec->script = XCAR (otf_spec); |
43f4f91c | 526 | if (! NILP (spec->script)) |
16963817 KH |
527 | { |
528 | OTF_SYM_TAG (spec->script, spec->script_tag); | |
529 | val = assq_no_quit (spec->script, Votf_script_alist); | |
530 | if (CONSP (val) && SYMBOLP (XCDR (val))) | |
531 | spec->script = XCDR (val); | |
532 | else | |
533 | spec->script = Qnil; | |
534 | } | |
cc63eaf9 | 535 | else |
16963817 | 536 | spec->script_tag = 0x44464C54; /* "DFLT" */ |
cc63eaf9 KH |
537 | otf_spec = XCDR (otf_spec); |
538 | val = XCAR (otf_spec); | |
539 | if (! NILP (val)) | |
16963817 | 540 | OTF_SYM_TAG (val, spec->langsys_tag); |
cc63eaf9 | 541 | else |
16963817 | 542 | spec->langsys_tag = 0; |
cc63eaf9 KH |
543 | spec->nfeatures[0] = spec->nfeatures[1] = 0; |
544 | for (i = 0; i < 2; i++) | |
545 | { | |
546 | Lisp_Object len; | |
547 | ||
2a46904e | 548 | otf_spec = XCDR (otf_spec); |
cc63eaf9 KH |
549 | if (NILP (otf_spec)) |
550 | break; | |
551 | val = XCAR (otf_spec); | |
552 | if (NILP (val)) | |
553 | continue; | |
554 | len = Flength (val); | |
555 | spec->features[i] = malloc (sizeof (int) * XINT (len)); | |
556 | if (! spec->features[i]) | |
557 | { | |
558 | if (i > 0 && spec->features[0]) | |
559 | free (spec->features[0]); | |
560 | free (spec); | |
561 | return NULL; | |
562 | } | |
563 | for (j = 0, negative = 0; CONSP (val); val = XCDR (val)) | |
564 | { | |
565 | if (NILP (XCAR (val))) | |
566 | negative = 1; | |
567 | else | |
568 | { | |
569 | unsigned int tag; | |
570 | ||
571 | OTF_SYM_TAG (XCAR (val), tag); | |
572 | spec->features[i][j++] = negative ? tag & 0x80000000 : tag; | |
573 | } | |
574 | } | |
575 | spec->nfeatures[i] = j; | |
576 | } | |
577 | return spec; | |
578 | } | |
579 | ||
e302a291 | 580 | static FcPattern *ftfont_spec_pattern P_ ((Lisp_Object, char *, |
21988a08 KH |
581 | struct OpenTypeSpec **)); |
582 | ||
42984a74 | 583 | static FcPattern * |
e302a291 | 584 | ftfont_spec_pattern (spec, otlayout, otspec) |
42984a74 KH |
585 | Lisp_Object spec; |
586 | char *otlayout; | |
587 | struct OpenTypeSpec **otspec; | |
c2f5bfd6 | 588 | { |
21988a08 | 589 | Lisp_Object tmp, extra; |
c2f5bfd6 KH |
590 | FcPattern *pattern = NULL; |
591 | FcCharSet *charset = NULL; | |
592 | FcLangSet *langset = NULL; | |
42984a74 KH |
593 | int n; |
594 | int dpi = -1; | |
bc9a2afe | 595 | int scalable = -1; |
42984a74 | 596 | Lisp_Object script = Qnil; |
21988a08 | 597 | Lisp_Object registry; |
e302a291 | 598 | int fc_charset_idx; |
c2f5bfd6 | 599 | |
63565713 | 600 | if (! NILP (AREF (spec, FONT_ADSTYLE_INDEX)) |
42984a74 KH |
601 | && SBYTES (SYMBOL_NAME (AREF (spec, FONT_ADSTYLE_INDEX))) > 0) |
602 | /* Fontconfig doesn't support adstyle property. */ | |
603 | return NULL; | |
604 | if ((n = FONT_SLANT_NUMERIC (spec)) >= 0 | |
605 | && n < 100) | |
63565713 | 606 | /* Fontconfig doesn't support reverse-italic/obligue. */ |
42984a74 KH |
607 | return NULL; |
608 | ||
609 | if (INTEGERP (AREF (spec, FONT_DPI_INDEX))) | |
610 | dpi = XINT (AREF (spec, FONT_DPI_INDEX)); | |
42984a74 KH |
611 | if (INTEGERP (AREF (spec, FONT_AVGWIDTH_INDEX)) |
612 | && XINT (AREF (spec, FONT_AVGWIDTH_INDEX)) == 0) | |
613 | scalable = 1; | |
63565713 | 614 | |
21988a08 KH |
615 | registry = AREF (spec, FONT_REGISTRY_INDEX); |
616 | if (NILP (registry) | |
770835fd | 617 | || EQ (registry, Qascii_0) |
21988a08 | 618 | || EQ (registry, Qiso10646_1) |
7b649478 | 619 | || EQ (registry, Qunicode_bmp)) |
e302a291 | 620 | fc_charset_idx = -1; |
21988a08 | 621 | else |
c2f5bfd6 | 622 | { |
7d7ad10e KH |
623 | FcChar8 *lang; |
624 | ||
e302a291 KH |
625 | fc_charset_idx = ftfont_get_charset (registry); |
626 | if (fc_charset_idx < 0) | |
42984a74 | 627 | return NULL; |
e302a291 KH |
628 | charset = fc_charset_table[fc_charset_idx].fc_charset; |
629 | lang = (FcChar8 *) fc_charset_table[fc_charset_idx].lang; | |
7d7ad10e KH |
630 | if (lang) |
631 | { | |
632 | langset = FcLangSetCreate (); | |
633 | if (! langset) | |
634 | goto err; | |
635 | FcLangSetAdd (langset, lang); | |
636 | } | |
c2f5bfd6 KH |
637 | } |
638 | ||
cc63eaf9 | 639 | otlayout[0] = '\0'; |
8daf5667 KH |
640 | for (extra = AREF (spec, FONT_EXTRA_INDEX); |
641 | CONSP (extra); extra = XCDR (extra)) | |
c2f5bfd6 | 642 | { |
8daf5667 KH |
643 | Lisp_Object key, val; |
644 | ||
42984a74 KH |
645 | key = XCAR (XCAR (extra)), val = XCDR (XCAR (extra)); |
646 | if (EQ (key, QCdpi)) | |
647 | dpi = XINT (val); | |
42984a74 | 648 | else if (EQ (key, QClang)) |
c2f5bfd6 | 649 | { |
7d7ad10e KH |
650 | if (! langset) |
651 | langset = FcLangSetCreate (); | |
c2f5bfd6 KH |
652 | if (! langset) |
653 | goto err; | |
8daf5667 | 654 | if (SYMBOLP (val)) |
c2f5bfd6 | 655 | { |
8daf5667 | 656 | if (! FcLangSetAdd (langset, SYMBOL_FcChar8 (val))) |
c2f5bfd6 KH |
657 | goto err; |
658 | } | |
659 | else | |
8daf5667 KH |
660 | for (; CONSP (val); val = XCDR (val)) |
661 | if (SYMBOLP (XCAR (val)) | |
662 | && ! FcLangSetAdd (langset, SYMBOL_FcChar8 (XCAR (val)))) | |
663 | goto err; | |
c2f5bfd6 | 664 | } |
42984a74 KH |
665 | else if (EQ (key, QCotf)) |
666 | { | |
667 | *otspec = ftfont_get_open_type_spec (val); | |
668 | if (! *otspec) | |
669 | return NULL; | |
670 | strcat (otlayout, "otlayout:"); | |
671 | OTF_TAG_STR ((*otspec)->script_tag, otlayout + 9); | |
672 | script = (*otspec)->script; | |
673 | } | |
8daf5667 KH |
674 | else if (EQ (key, QCscript)) |
675 | script = val; | |
8daf5667 KH |
676 | else if (EQ (key, QCscalable)) |
677 | scalable = ! NILP (val); | |
678 | } | |
c2f5bfd6 | 679 | |
8daf5667 KH |
680 | if (! NILP (script) && ! charset) |
681 | { | |
682 | Lisp_Object chars = assq_no_quit (script, Vscript_representative_chars); | |
683 | ||
7c4bd58d | 684 | if (CONSP (chars) && CONSP (CDR (chars))) |
8daf5667 KH |
685 | { |
686 | charset = FcCharSetCreate (); | |
687 | if (! charset) | |
688 | goto err; | |
689 | for (chars = XCDR (chars); CONSP (chars); chars = XCDR (chars)) | |
690 | if (CHARACTERP (XCAR (chars)) | |
691 | && ! FcCharSetAddChar (charset, XUINT (XCAR (chars)))) | |
692 | goto err; | |
c2f5bfd6 KH |
693 | } |
694 | } | |
695 | ||
4ffe34a4 | 696 | pattern = FcPatternCreate (); |
706b6995 KH |
697 | if (! pattern) |
698 | goto err; | |
706b6995 | 699 | tmp = AREF (spec, FONT_FOUNDRY_INDEX); |
42984a74 | 700 | if (! NILP (tmp) |
706b6995 KH |
701 | && ! FcPatternAddString (pattern, FC_FOUNDRY, SYMBOL_FcChar8 (tmp))) |
702 | goto err; | |
318548be KH |
703 | tmp = AREF (spec, FONT_FAMILY_INDEX); |
704 | if (! NILP (tmp) | |
705 | && ! FcPatternAddString (pattern, FC_FAMILY, SYMBOL_FcChar8 (tmp))) | |
706 | goto err; | |
c2f5bfd6 KH |
707 | if (charset |
708 | && ! FcPatternAddCharSet (pattern, FC_CHARSET, charset)) | |
709 | goto err; | |
710 | if (langset | |
711 | && ! FcPatternAddLangSet (pattern, FC_LANG, langset)) | |
712 | goto err; | |
bc9a2afe KH |
713 | if (dpi >= 0 |
714 | && ! FcPatternAddDouble (pattern, FC_DPI, dpi)) | |
715 | goto err; | |
bc9a2afe | 716 | if (scalable >= 0 |
25a5d05b | 717 | && ! FcPatternAddBool (pattern, FC_SCALABLE, scalable ? FcTrue : FcFalse)) |
bc9a2afe | 718 | goto err; |
7d7ad10e | 719 | |
42984a74 | 720 | goto finish; |
c2f5bfd6 | 721 | |
42984a74 KH |
722 | err: |
723 | /* We come here because of unexpected error in fontconfig API call | |
724 | (usually insufficient memory). */ | |
725 | if (pattern) | |
726 | { | |
727 | FcPatternDestroy (pattern); | |
728 | pattern = NULL; | |
729 | } | |
730 | if (*otspec) | |
731 | { | |
732 | if ((*otspec)->nfeatures[0] > 0) | |
733 | free ((*otspec)->features[0]); | |
734 | if ((*otspec)->nfeatures[1] > 0) | |
735 | free ((*otspec)->features[1]); | |
736 | free (*otspec); | |
737 | *otspec = NULL; | |
738 | } | |
739 | ||
740 | finish: | |
42984a74 | 741 | if (langset) FcLangSetDestroy (langset); |
e302a291 | 742 | if (charset && fc_charset_idx < 0) FcCharSetDestroy (charset); |
42984a74 KH |
743 | return pattern; |
744 | } | |
745 | ||
746 | static Lisp_Object | |
747 | ftfont_list (frame, spec) | |
748 | Lisp_Object frame, spec; | |
749 | { | |
e302a291 | 750 | Lisp_Object val = Qnil, family; |
42984a74 KH |
751 | int i; |
752 | FcPattern *pattern; | |
753 | FcFontSet *fontset = NULL; | |
754 | FcObjectSet *objset = NULL; | |
7c4bd58d KH |
755 | FcCharSet *charset; |
756 | Lisp_Object chars = Qnil; | |
757 | FcResult result; | |
42984a74 KH |
758 | char otlayout[15]; /* For "otlayout:XXXX" */ |
759 | struct OpenTypeSpec *otspec = NULL; | |
5d376f74 | 760 | int spacing = -1; |
2a46904e | 761 | |
42984a74 KH |
762 | if (! fc_initialized) |
763 | { | |
764 | FcInit (); | |
765 | fc_initialized = 1; | |
766 | } | |
767 | ||
e302a291 | 768 | pattern = ftfont_spec_pattern (spec, otlayout, &otspec); |
42984a74 KH |
769 | if (! pattern) |
770 | return Qnil; | |
7c4bd58d KH |
771 | if (FcPatternGetCharSet (pattern, FC_CHARSET, 0, &charset) != FcResultMatch) |
772 | { | |
773 | val = assq_no_quit (QCscript, AREF (spec, FONT_EXTRA_INDEX)); | |
774 | if (! NILP (val)) | |
775 | { | |
776 | val = assq_no_quit (XCDR (val), Vscript_representative_chars); | |
777 | if (CONSP (val) && VECTORP (XCDR (val))) | |
778 | chars = XCDR (val); | |
779 | } | |
780 | val = Qnil; | |
781 | } | |
5d376f74 KH |
782 | if (INTEGERP (AREF (spec, FONT_SPACING_INDEX))) |
783 | spacing = XINT (AREF (spec, FONT_SPACING_INDEX)); | |
318548be KH |
784 | family = AREF (spec, FONT_FAMILY_INDEX); |
785 | if (! NILP (family)) | |
786 | { | |
787 | Lisp_Object resolved; | |
788 | ||
7d7ad10e | 789 | resolved = ftfont_resolve_generic_family (family, pattern); |
318548be KH |
790 | if (! NILP (resolved)) |
791 | { | |
792 | FcPatternDel (pattern, FC_FAMILY); | |
793 | if (! FcPatternAddString (pattern, FC_FAMILY, | |
794 | SYMBOL_FcChar8 (resolved))) | |
795 | goto err; | |
796 | } | |
797 | } | |
798 | ||
706b6995 | 799 | objset = FcObjectSetBuild (FC_FOUNDRY, FC_FAMILY, FC_WEIGHT, FC_SLANT, |
42984a74 | 800 | FC_WIDTH, FC_PIXEL_SIZE, FC_SPACING, FC_SCALABLE, |
e302a291 | 801 | FC_FILE, FC_INDEX, |
42984a74 KH |
802 | #ifdef FC_CAPABILITY |
803 | FC_CAPABILITY, | |
804 | #endif /* FC_CAPABILITY */ | |
4ffe34a4 KH |
805 | #ifdef FC_FONTFORMAT |
806 | FC_FONTFORMAT, | |
807 | #endif | |
e907d979 | 808 | NULL); |
706b6995 KH |
809 | if (! objset) |
810 | goto err; | |
7c4bd58d KH |
811 | if (! NILP (chars)) |
812 | FcObjectSetAdd (objset, FC_CHARSET); | |
42984a74 | 813 | |
318548be | 814 | fontset = FcFontList (NULL, pattern, objset); |
7c4bd58d KH |
815 | if (! fontset || fontset->nfont == 0) |
816 | goto finish; | |
770835fd KH |
817 | #if 0 |
818 | /* Need fix because this finds any fonts. */ | |
819 | if (fontset->nfont == 0 && ! NILP (family)) | |
820 | { | |
821 | /* Try maching with configuration. For instance, the | |
822 | configuration may specify "Nimbus Mono L" as an alias of | |
823 | "Courier". */ | |
824 | FcPattern *pat = FcPatternBuild (0, FC_FAMILY, FcTypeString, | |
825 | SYMBOL_FcChar8 (family), NULL); | |
826 | FcChar8 *fam; | |
827 | ||
828 | if (FcConfigSubstitute (NULL, pat, FcMatchPattern) == FcTrue) | |
829 | { | |
830 | for (i = 0; | |
831 | FcPatternGetString (pat, FC_FAMILY, i, &fam) == FcResultMatch; | |
832 | i++) | |
833 | { | |
834 | FcPatternDel (pattern, FC_FAMILY); | |
835 | FcPatternAddString (pattern, FC_FAMILY, fam); | |
836 | FcFontSetDestroy (fontset); | |
837 | fontset = FcFontList (NULL, pattern, objset); | |
838 | if (fontset->nfont > 0) | |
839 | break; | |
840 | } | |
841 | } | |
842 | } | |
843 | #endif | |
318548be | 844 | for (i = 0; i < fontset->nfont; i++) |
bc5f6c42 | 845 | { |
318548be | 846 | Lisp_Object entity; |
f63d54dc | 847 | |
5d376f74 KH |
848 | if (spacing >= 0) |
849 | { | |
850 | int this; | |
851 | ||
852 | if ((FcPatternGetInteger (fontset->fonts[i], FC_SPACING, 0, &this) | |
853 | == FcResultMatch) | |
854 | && spacing != this) | |
855 | continue; | |
856 | } | |
857 | ||
bc5f6c42 | 858 | #ifdef FC_CAPABILITY |
318548be KH |
859 | if (otlayout[0]) |
860 | { | |
861 | FcChar8 *this; | |
a85f724a | 862 | |
7c4bd58d KH |
863 | if (FcPatternGetString (fontset->fonts[i], FC_CAPABILITY, 0, &this) |
864 | != FcResultMatch | |
318548be KH |
865 | || ! strstr ((char *) this, otlayout)) |
866 | continue; | |
867 | } | |
bc5f6c42 | 868 | #endif /* FC_CAPABILITY */ |
cc63eaf9 | 869 | #ifdef HAVE_LIBOTF |
318548be KH |
870 | if (otspec) |
871 | { | |
872 | FcChar8 *file; | |
873 | OTF *otf; | |
874 | ||
875 | if (FcPatternGetString (fontset->fonts[i], FC_FILE, 0, &file) | |
876 | != FcResultMatch) | |
877 | continue; | |
878 | otf = OTF_open ((char *) file); | |
879 | if (! otf) | |
880 | continue; | |
881 | if (OTF_check_features (otf, 1, | |
882 | otspec->script_tag, otspec->langsys_tag, | |
883 | otspec->features[0], | |
884 | otspec->nfeatures[0]) != 1 | |
885 | || OTF_check_features (otf, 0, | |
886 | otspec->script_tag, otspec->langsys_tag, | |
887 | otspec->features[1], | |
888 | otspec->nfeatures[1]) != 1) | |
889 | continue; | |
c2f5bfd6 | 890 | } |
318548be | 891 | #endif /* HAVE_LIBOTF */ |
7c4bd58d KH |
892 | if (VECTORP (chars)) |
893 | { | |
894 | int j; | |
895 | ||
896 | if (FcPatternGetCharSet (fontset->fonts[i], FC_CHARSET, 0, &charset) | |
897 | != FcResultMatch) | |
898 | continue; | |
899 | for (j = 0; j < ASIZE (chars); j++) | |
900 | if (NATNUMP (AREF (chars, j)) | |
901 | && FcCharSetHasChar (charset, XFASTINT (AREF (chars, j)))) | |
902 | break; | |
903 | if (j == ASIZE (chars)) | |
904 | continue; | |
905 | } | |
e302a291 KH |
906 | entity = ftfont_pattern_entity (fontset->fonts[i], |
907 | AREF (spec, FONT_EXTRA_INDEX)); | |
318548be KH |
908 | if (! NILP (entity)) |
909 | val = Fcons (entity, val); | |
c2f5bfd6 | 910 | } |
7c4bd58d | 911 | val = Fnreverse (val); |
c2f5bfd6 KH |
912 | goto finish; |
913 | ||
914 | err: | |
915 | /* We come here because of unexpected error in fontconfig API call | |
706b6995 | 916 | (usually insufficient memory). */ |
c2f5bfd6 KH |
917 | val = Qnil; |
918 | ||
919 | finish: | |
7c4bd58d | 920 | font_add_log ("ftfont-list", spec, val); |
c2f5bfd6 KH |
921 | if (objset) FcObjectSetDestroy (objset); |
922 | if (fontset) FcFontSetDestroy (fontset); | |
c2f5bfd6 | 923 | if (pattern) FcPatternDestroy (pattern); |
c2f5bfd6 KH |
924 | return val; |
925 | } | |
926 | ||
8daf5667 KH |
927 | static Lisp_Object |
928 | ftfont_match (frame, spec) | |
929 | Lisp_Object frame, spec; | |
930 | { | |
a3c15670 | 931 | Lisp_Object entity = Qnil; |
42984a74 | 932 | FcPattern *pattern, *match = NULL; |
8daf5667 | 933 | FcResult result; |
42984a74 KH |
934 | char otlayout[15]; /* For "otlayout:XXXX" */ |
935 | struct OpenTypeSpec *otspec = NULL; | |
8daf5667 KH |
936 | |
937 | if (! fc_initialized) | |
938 | { | |
939 | FcInit (); | |
940 | fc_initialized = 1; | |
941 | } | |
942 | ||
e302a291 | 943 | pattern = ftfont_spec_pattern (spec, otlayout, &otspec); |
42984a74 | 944 | if (! pattern) |
8daf5667 KH |
945 | return Qnil; |
946 | ||
42984a74 | 947 | if (INTEGERP (AREF (spec, FONT_SIZE_INDEX))) |
8daf5667 | 948 | { |
42984a74 | 949 | FcValue value; |
55082642 | 950 | |
42984a74 KH |
951 | value.type = FcTypeDouble; |
952 | value.u.d = XINT (AREF (spec, FONT_SIZE_INDEX)); | |
953 | FcPatternAdd (pattern, FC_PIXEL_SIZE, value, FcFalse); | |
954 | } | |
955 | if (FcConfigSubstitute (NULL, pattern, FcMatchPattern) == FcTrue) | |
956 | { | |
957 | FcDefaultSubstitute (pattern); | |
958 | match = FcFontMatch (NULL, pattern, &result); | |
959 | if (match) | |
8daf5667 | 960 | { |
e302a291 | 961 | entity = ftfont_pattern_entity (match, AREF (spec, FONT_EXTRA_INDEX)); |
42984a74 KH |
962 | FcPatternDestroy (match); |
963 | if (! NILP (AREF (spec, FONT_FAMILY_INDEX)) | |
964 | && NILP (assq_no_quit (AREF (spec, FONT_FAMILY_INDEX), | |
965 | ftfont_generic_family_list)) | |
966 | && NILP (Fstring_equal (AREF (spec, FONT_FAMILY_INDEX), | |
967 | AREF (entity, FONT_FAMILY_INDEX)))) | |
968 | entity = Qnil; | |
8daf5667 | 969 | } |
8daf5667 | 970 | } |
42984a74 | 971 | FcPatternDestroy (pattern); |
8daf5667 | 972 | |
318548be | 973 | font_add_log ("ftfont-match", spec, entity); |
8daf5667 KH |
974 | return entity; |
975 | } | |
976 | ||
c2f5bfd6 KH |
977 | static Lisp_Object |
978 | ftfont_list_family (frame) | |
979 | Lisp_Object frame; | |
980 | { | |
76d126ec | 981 | Lisp_Object list = Qnil; |
c2f5bfd6 KH |
982 | FcPattern *pattern = NULL; |
983 | FcFontSet *fontset = NULL; | |
984 | FcObjectSet *objset = NULL; | |
985 | int i; | |
986 | ||
987 | if (! fc_initialized) | |
988 | { | |
989 | FcInit (); | |
990 | fc_initialized = 1; | |
991 | } | |
992 | ||
993 | pattern = FcPatternCreate (); | |
994 | if (! pattern) | |
995 | goto finish; | |
706b6995 | 996 | objset = FcObjectSetBuild (FC_FAMILY, NULL); |
c2f5bfd6 KH |
997 | if (! objset) |
998 | goto finish; | |
999 | fontset = FcFontList (NULL, pattern, objset); | |
1000 | if (! fontset) | |
1001 | goto finish; | |
1002 | ||
c2f5bfd6 KH |
1003 | for (i = 0; i < fontset->nfont; i++) |
1004 | { | |
1005 | FcPattern *pat = fontset->fonts[i]; | |
1006 | FcChar8 *str; | |
1007 | ||
1008 | if (FcPatternGetString (pat, FC_FAMILY, 0, &str) == FcResultMatch) | |
42984a74 | 1009 | list = Fcons (intern ((char *) str), list); |
c2f5bfd6 KH |
1010 | } |
1011 | ||
1012 | finish: | |
1013 | if (objset) FcObjectSetDestroy (objset); | |
1014 | if (fontset) FcFontSetDestroy (fontset); | |
1015 | if (pattern) FcPatternDestroy (pattern); | |
1016 | ||
1017 | return list; | |
1018 | } | |
1019 | ||
1020 | ||
42984a74 | 1021 | static Lisp_Object |
c2f5bfd6 KH |
1022 | ftfont_open (f, entity, pixel_size) |
1023 | FRAME_PTR f; | |
1024 | Lisp_Object entity; | |
1025 | int pixel_size; | |
1026 | { | |
1027 | struct ftfont_info *ftfont_info; | |
1028 | struct font *font; | |
e302a291 | 1029 | struct ftfont_cache_data *cache_data; |
c2f5bfd6 KH |
1030 | FT_Face ft_face; |
1031 | FT_Size ft_size; | |
1032 | FT_UInt size; | |
e302a291 | 1033 | Lisp_Object val, filename, index, cache, font_object; |
21988a08 | 1034 | int scalable; |
c2f5bfd6 | 1035 | int spacing; |
42984a74 KH |
1036 | char name[256]; |
1037 | int i, len; | |
1038 | int upEM; | |
c2f5bfd6 | 1039 | |
42984a74 KH |
1040 | val = assq_no_quit (QCfont_entity, AREF (entity, FONT_EXTRA_INDEX)); |
1041 | if (! CONSP (val)) | |
1042 | return Qnil; | |
1043 | val = XCDR (val); | |
e302a291 | 1044 | cache = ftfont_lookup_cache (val, 1); |
21988a08 KH |
1045 | if (NILP (cache)) |
1046 | return Qnil; | |
e302a291 KH |
1047 | filename = XCAR (val); |
1048 | index = XCDR (val); | |
21988a08 | 1049 | val = XCDR (cache); |
e302a291 KH |
1050 | cache_data = XSAVE_VALUE (XCDR (cache))->pointer; |
1051 | ft_face = cache_data->ft_face; | |
21988a08 | 1052 | if (XSAVE_VALUE (val)->integer > 0) |
c2f5bfd6 | 1053 | { |
21988a08 | 1054 | /* FT_Face in this cache is already used by the different size. */ |
c2f5bfd6 | 1055 | if (FT_New_Size (ft_face, &ft_size) != 0) |
42984a74 | 1056 | return Qnil; |
c2f5bfd6 KH |
1057 | if (FT_Activate_Size (ft_size) != 0) |
1058 | { | |
1059 | FT_Done_Size (ft_size); | |
42984a74 | 1060 | return Qnil; |
c2f5bfd6 | 1061 | } |
21988a08 KH |
1062 | } |
1063 | XSAVE_VALUE (val)->integer++; | |
c2f5bfd6 KH |
1064 | size = XINT (AREF (entity, FONT_SIZE_INDEX)); |
1065 | if (size == 0) | |
1066 | size = pixel_size; | |
1067 | if (FT_Set_Pixel_Sizes (ft_face, size, size) != 0) | |
1068 | { | |
1069 | if (XSAVE_VALUE (val)->integer == 0) | |
1070 | FT_Done_Face (ft_face); | |
42984a74 | 1071 | return Qnil; |
c2f5bfd6 KH |
1072 | } |
1073 | ||
e302a291 | 1074 | font_object = font_make_object (VECSIZE (struct ftfont_info), entity, size); |
42984a74 | 1075 | ASET (font_object, FONT_TYPE_INDEX, Qfreetype); |
42984a74 KH |
1076 | len = font_unparse_xlfd (entity, size, name, 256); |
1077 | if (len > 0) | |
3299c552 | 1078 | ASET (font_object, FONT_NAME_INDEX, make_string (name, len)); |
42984a74 KH |
1079 | len = font_unparse_fcname (entity, size, name, 256); |
1080 | if (len > 0) | |
3299c552 | 1081 | ASET (font_object, FONT_FULLNAME_INDEX, make_string (name, len)); |
42984a74 KH |
1082 | else |
1083 | ASET (font_object, FONT_FULLNAME_INDEX, | |
1084 | AREF (font_object, FONT_NAME_INDEX)); | |
21988a08 | 1085 | ASET (font_object, FONT_FILE_INDEX, filename); |
7d7ad10e | 1086 | ASET (font_object, FONT_FORMAT_INDEX, ftfont_font_format (NULL, filename)); |
42984a74 KH |
1087 | font = XFONT_OBJECT (font_object); |
1088 | ftfont_info = (struct ftfont_info *) font; | |
21988a08 | 1089 | ftfont_info->ft_size = ft_face->size; |
e302a291 | 1090 | ftfont_info->index = XINT (index); |
0d674a05 | 1091 | #ifdef HAVE_LIBOTF |
de023c40 KH |
1092 | ftfont_info->maybe_otf = ft_face->face_flags & FT_FACE_FLAG_SFNT; |
1093 | ftfont_info->otf = NULL; | |
0d674a05 | 1094 | #endif /* HAVE_LIBOTF */ |
c2f5bfd6 KH |
1095 | font->pixel_size = size; |
1096 | font->driver = &ftfont_driver; | |
42984a74 | 1097 | font->encoding_charset = font->repertory_charset = -1; |
c9c0c429 | 1098 | |
42984a74 | 1099 | upEM = ft_face->units_per_EM; |
21988a08 KH |
1100 | scalable = (INTEGERP (AREF (entity, FONT_AVGWIDTH_INDEX)) |
1101 | && XINT (AREF (entity, FONT_AVGWIDTH_INDEX)) == 0); | |
42984a74 KH |
1102 | if (scalable) |
1103 | { | |
1104 | font->ascent = ft_face->ascender * size / upEM; | |
1105 | font->descent = - ft_face->descender * size / upEM; | |
1106 | font->height = ft_face->height * size / upEM; | |
1107 | } | |
1108 | else | |
1109 | { | |
1110 | font->ascent = ft_face->size->metrics.ascender >> 6; | |
1111 | font->descent = - ft_face->size->metrics.descender >> 6; | |
1112 | font->height = ft_face->size->metrics.height >> 6; | |
c9c0c429 | 1113 | } |
21988a08 KH |
1114 | if (INTEGERP (AREF (entity, FONT_SPACING_INDEX))) |
1115 | spacing = XINT (AREF (entity, FONT_SPACING_INDEX)); | |
1116 | else | |
c9c0c429 KH |
1117 | spacing = FC_PROPORTIONAL; |
1118 | if (spacing != FC_PROPORTIONAL) | |
42984a74 KH |
1119 | font->min_width = font->average_width = font->space_width |
1120 | = (scalable ? ft_face->max_advance_width * size / upEM | |
1121 | : ft_face->size->metrics.max_advance >> 6); | |
c2f5bfd6 KH |
1122 | else |
1123 | { | |
42984a74 | 1124 | int n; |
c2f5bfd6 | 1125 | |
42984a74 KH |
1126 | font->min_width = font->average_width = font->space_width = 0; |
1127 | for (i = 32, n = 0; i < 127; i++) | |
1128 | if (FT_Load_Char (ft_face, i, FT_LOAD_DEFAULT) != 0) | |
1129 | { | |
1130 | int this_width = ft_face->glyph->metrics.horiAdvance >> 6; | |
1131 | ||
1132 | if (this_width > 0 | |
1133 | && (! font->min_width || font->min_width > this_width)) | |
1134 | font->min_width = this_width; | |
1135 | if (i == 32) | |
1136 | font->space_width = this_width; | |
1137 | font->average_width += this_width; | |
1138 | n++; | |
1139 | } | |
1140 | if (n > 0) | |
1141 | font->average_width /= n; | |
c2f5bfd6 KH |
1142 | } |
1143 | ||
42984a74 KH |
1144 | font->baseline_offset = 0; |
1145 | font->relative_compose = 0; | |
1146 | font->default_ascent = 0; | |
1147 | font->vertical_centering = 0; | |
1148 | if (scalable) | |
1149 | { | |
1150 | font->underline_position = -ft_face->underline_position * size / upEM; | |
1151 | font->underline_thickness = -ft_face->underline_thickness * size / upEM; | |
1152 | } | |
1153 | else | |
1154 | { | |
1155 | font->underline_position = -1; | |
1156 | font->underline_thickness = 0; | |
1157 | } | |
c2f5bfd6 | 1158 | |
42984a74 | 1159 | return font_object; |
c2f5bfd6 KH |
1160 | } |
1161 | ||
1162 | static void | |
1163 | ftfont_close (f, font) | |
1164 | FRAME_PTR f; | |
1165 | struct font *font; | |
1166 | { | |
1167 | struct ftfont_info *ftfont_info = (struct ftfont_info *) font; | |
21988a08 | 1168 | Lisp_Object val, cache; |
c2f5bfd6 | 1169 | |
e302a291 KH |
1170 | val = Fcons (font->props[FONT_FILE_INDEX], make_number (ftfont_info->index)); |
1171 | cache = ftfont_lookup_cache (val, 1); | |
1172 | xassert (CONSP (cache)); | |
21988a08 | 1173 | val = XCDR (cache); |
c2f5bfd6 KH |
1174 | (XSAVE_VALUE (val)->integer)--; |
1175 | if (XSAVE_VALUE (val)->integer == 0) | |
de023c40 | 1176 | { |
e302a291 | 1177 | struct ftfont_cache_data *cache_data = XSAVE_VALUE (val)->pointer; |
21988a08 | 1178 | |
e302a291 | 1179 | FT_Done_Face (cache_data->ft_face); |
de023c40 KH |
1180 | #ifdef HAVE_LIBOTF |
1181 | if (ftfont_info->otf) | |
1182 | OTF_close (ftfont_info->otf); | |
1183 | #endif | |
e302a291 | 1184 | cache_data->ft_face = NULL; |
de023c40 | 1185 | } |
c2f5bfd6 KH |
1186 | else |
1187 | FT_Done_Size (ftfont_info->ft_size); | |
c2f5bfd6 KH |
1188 | } |
1189 | ||
2a46904e | 1190 | static int |
c2f5bfd6 KH |
1191 | ftfont_has_char (entity, c) |
1192 | Lisp_Object entity; | |
1193 | int c; | |
1194 | { | |
e302a291 | 1195 | FcCharSet *charset = ftfont_get_fc_charset (entity); |
c2f5bfd6 | 1196 | |
e302a291 | 1197 | return (FcCharSetHasChar (charset, c) == FcTrue); |
c2f5bfd6 KH |
1198 | } |
1199 | ||
1200 | static unsigned | |
1201 | ftfont_encode_char (font, c) | |
1202 | struct font *font; | |
1203 | int c; | |
1204 | { | |
1205 | struct ftfont_info *ftfont_info = (struct ftfont_info *) font; | |
1206 | FT_Face ft_face = ftfont_info->ft_size->face; | |
1207 | FT_ULong charcode = c; | |
1208 | FT_UInt code = FT_Get_Char_Index (ft_face, charcode); | |
1209 | ||
4cec6061 | 1210 | return (code > 0 ? code : FONT_INVALID_CODE); |
c2f5bfd6 KH |
1211 | } |
1212 | ||
1213 | static int | |
1214 | ftfont_text_extents (font, code, nglyphs, metrics) | |
1215 | struct font *font; | |
1216 | unsigned *code; | |
1217 | int nglyphs; | |
1218 | struct font_metrics *metrics; | |
1219 | { | |
1220 | struct ftfont_info *ftfont_info = (struct ftfont_info *) font; | |
1221 | FT_Face ft_face = ftfont_info->ft_size->face; | |
1222 | int width = 0; | |
e5d05978 | 1223 | int i, first; |
c2f5bfd6 KH |
1224 | |
1225 | if (ftfont_info->ft_size != ft_face->size) | |
1226 | FT_Activate_Size (ftfont_info->ft_size); | |
1227 | if (metrics) | |
1228 | bzero (metrics, sizeof (struct font_metrics)); | |
e5d05978 | 1229 | for (i = 0, first = 1; i < nglyphs; i++) |
c2f5bfd6 KH |
1230 | { |
1231 | if (FT_Load_Glyph (ft_face, code[i], FT_LOAD_DEFAULT) == 0) | |
1232 | { | |
1233 | FT_Glyph_Metrics *m = &ft_face->glyph->metrics; | |
1234 | ||
e5d05978 KH |
1235 | if (first) |
1236 | { | |
1237 | if (metrics) | |
1238 | { | |
1239 | metrics->lbearing = m->horiBearingX >> 6; | |
1240 | metrics->rbearing = (m->horiBearingX + m->width) >> 6; | |
1241 | metrics->ascent = m->horiBearingY >> 6; | |
1242 | metrics->descent = (m->horiBearingY + m->height) >> 6; | |
1243 | } | |
1244 | first = 0; | |
1245 | } | |
c2f5bfd6 KH |
1246 | if (metrics) |
1247 | { | |
1248 | if (metrics->lbearing > width + (m->horiBearingX >> 6)) | |
1249 | metrics->lbearing = width + (m->horiBearingX >> 6); | |
1250 | if (metrics->rbearing | |
1251 | < width + ((m->horiBearingX + m->width) >> 6)) | |
1252 | metrics->rbearing | |
1253 | = width + ((m->horiBearingX + m->width) >> 6); | |
1254 | if (metrics->ascent < (m->horiBearingY >> 6)) | |
1255 | metrics->ascent = m->horiBearingY >> 6; | |
1256 | if (metrics->descent > ((m->horiBearingY + m->height) >> 6)) | |
1257 | metrics->descent = (m->horiBearingY + m->height) >> 6; | |
1258 | } | |
1259 | width += m->horiAdvance >> 6; | |
1260 | } | |
1261 | else | |
1262 | { | |
42984a74 | 1263 | width += font->space_width; |
c2f5bfd6 KH |
1264 | } |
1265 | } | |
1266 | if (metrics) | |
1267 | metrics->width = width; | |
1268 | ||
1269 | return width; | |
1270 | } | |
1271 | ||
1272 | static int | |
1273 | ftfont_get_bitmap (font, code, bitmap, bits_per_pixel) | |
1274 | struct font *font; | |
1275 | unsigned code; | |
1276 | struct font_bitmap *bitmap; | |
1277 | int bits_per_pixel; | |
1278 | { | |
1279 | struct ftfont_info *ftfont_info = (struct ftfont_info *) font; | |
1280 | FT_Face ft_face = ftfont_info->ft_size->face; | |
1281 | FT_Int32 load_flags = FT_LOAD_RENDER; | |
1282 | ||
1283 | if (ftfont_info->ft_size != ft_face->size) | |
1284 | FT_Activate_Size (ftfont_info->ft_size); | |
1285 | if (bits_per_pixel == 1) | |
1286 | { | |
1287 | #ifdef FT_LOAD_TARGET_MONO | |
1288 | load_flags |= FT_LOAD_TARGET_MONO; | |
1289 | #else | |
1290 | load_flags |= FT_LOAD_MONOCHROME; | |
1291 | #endif | |
1292 | } | |
1293 | else if (bits_per_pixel != 8) | |
1294 | /* We don't support such a rendering. */ | |
1295 | return -1; | |
1296 | ||
1297 | if (FT_Load_Glyph (ft_face, code, load_flags) != 0) | |
1298 | return -1; | |
b51e5112 KH |
1299 | bitmap->bits_per_pixel |
1300 | = (ft_face->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_MONO ? 1 | |
1301 | : ft_face->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_GRAY ? 8 | |
1302 | : ft_face->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_LCD ? 8 | |
1303 | : ft_face->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_LCD_V ? 8 | |
1304 | : -1); | |
1305 | if (bitmap->bits_per_pixel < 0) | |
1306 | /* We don't suport that kind of pixel mode. */ | |
1307 | return -1; | |
c2f5bfd6 KH |
1308 | bitmap->rows = ft_face->glyph->bitmap.rows; |
1309 | bitmap->width = ft_face->glyph->bitmap.width; | |
1310 | bitmap->pitch = ft_face->glyph->bitmap.pitch; | |
1311 | bitmap->buffer = ft_face->glyph->bitmap.buffer; | |
1312 | bitmap->left = ft_face->glyph->bitmap_left; | |
1313 | bitmap->top = ft_face->glyph->bitmap_top; | |
1314 | bitmap->advance = ft_face->glyph->metrics.horiAdvance >> 6; | |
1315 | bitmap->extra = NULL; | |
1316 | ||
1317 | return 0; | |
1318 | } | |
1319 | ||
1320 | static int | |
1321 | ftfont_anchor_point (font, code, index, x, y) | |
1322 | struct font *font; | |
1323 | unsigned code; | |
1324 | int index; | |
1325 | int *x, *y; | |
1326 | { | |
1327 | struct ftfont_info *ftfont_info = (struct ftfont_info *) font; | |
1328 | FT_Face ft_face = ftfont_info->ft_size->face; | |
1329 | ||
1330 | if (ftfont_info->ft_size != ft_face->size) | |
1331 | FT_Activate_Size (ftfont_info->ft_size); | |
1332 | if (FT_Load_Glyph (ft_face, code, FT_LOAD_DEFAULT) != 0) | |
1333 | return -1; | |
1334 | if (ft_face->glyph->format != FT_GLYPH_FORMAT_OUTLINE) | |
1335 | return -1; | |
1336 | if (index >= ft_face->glyph->outline.n_points) | |
1337 | return -1; | |
1338 | *x = ft_face->glyph->outline.points[index].x; | |
1339 | *y = ft_face->glyph->outline.points[index].y; | |
1340 | return 0; | |
1341 | } | |
1342 | ||
de023c40 | 1343 | #ifdef HAVE_LIBOTF |
e302a291 KH |
1344 | |
1345 | static Lisp_Object | |
1346 | ftfont_otf_features (gsub_gpos) | |
1347 | OTF_GSUB_GPOS *gsub_gpos; | |
1348 | { | |
1349 | Lisp_Object scripts, langsyses, features, sym; | |
39356621 | 1350 | int i, j, k, l; |
e302a291 KH |
1351 | |
1352 | for (scripts = Qnil, i = gsub_gpos->ScriptList.ScriptCount - 1; i >= 0; i--) | |
1353 | { | |
1354 | OTF_Script *otf_script = gsub_gpos->ScriptList.Script + i; | |
1355 | ||
1356 | for (langsyses = Qnil, j = otf_script->LangSysCount - 1; j >= -1; j--) | |
1357 | { | |
1358 | OTF_LangSys *otf_langsys; | |
1359 | ||
1360 | if (j >= 0) | |
1361 | otf_langsys = otf_script->LangSys + j; | |
1362 | else if (otf_script->DefaultLangSysOffset) | |
1363 | otf_langsys = &otf_script->DefaultLangSys; | |
1364 | else | |
1365 | break; | |
1366 | ||
1367 | for (features = Qnil, k = otf_langsys->FeatureCount - 1; k >= 0; k--) | |
1368 | { | |
39356621 | 1369 | l = otf_langsys->FeatureIndex[k]; |
064766f2 | 1370 | if (l >= gsub_gpos->FeatureList.FeatureCount) |
39356621 KH |
1371 | continue; |
1372 | OTF_TAG_SYM (sym, gsub_gpos->FeatureList.Feature[l].FeatureTag); | |
e302a291 KH |
1373 | features = Fcons (sym, features); |
1374 | } | |
1375 | if (j >= 0) | |
1376 | OTF_TAG_SYM (sym, otf_script->LangSysRecord[j].LangSysTag); | |
1377 | else | |
1378 | sym = Qnil; | |
1379 | langsyses = Fcons (Fcons (sym, features), langsyses); | |
1380 | } | |
1381 | ||
1382 | OTF_TAG_SYM (sym, gsub_gpos->ScriptList.Script[i].ScriptTag); | |
1383 | scripts = Fcons (Fcons (sym, langsyses), scripts); | |
1384 | } | |
1385 | return scripts; | |
1386 | ||
1387 | } | |
1388 | ||
1389 | ||
1390 | static Lisp_Object | |
1391 | ftfont_otf_capability (font) | |
1392 | struct font *font; | |
1393 | { | |
1394 | struct ftfont_info *ftfont_info = (struct ftfont_info *) font; | |
1395 | OTF *otf = ftfont_get_otf (ftfont_info); | |
1396 | Lisp_Object gsub_gpos; | |
1397 | ||
1398 | if (! otf) | |
1399 | return Qnil; | |
1400 | gsub_gpos = Fcons (Qnil, Qnil); | |
064766f2 KH |
1401 | if (OTF_get_table (otf, "GSUB") == 0 |
1402 | && otf->gsub->FeatureList.FeatureCount > 0) | |
e302a291 | 1403 | XSETCAR (gsub_gpos, ftfont_otf_features (otf->gsub)); |
064766f2 KH |
1404 | if (OTF_get_table (otf, "GPOS") == 0 |
1405 | && otf->gpos->FeatureList.FeatureCount > 0) | |
e302a291 KH |
1406 | XSETCDR (gsub_gpos, ftfont_otf_features (otf->gpos)); |
1407 | return gsub_gpos; | |
1408 | } | |
1409 | ||
de023c40 KH |
1410 | #ifdef HAVE_M17N_FLT |
1411 | ||
1412 | struct MFLTFontFT | |
1413 | { | |
1414 | MFLTFont flt_font; | |
1415 | struct font *font; | |
1416 | FT_Face ft_face; | |
1417 | OTF *otf; | |
1418 | }; | |
1419 | ||
1420 | static int | |
1421 | ftfont_get_glyph_id (font, gstring, from, to) | |
1422 | MFLTFont *font; | |
1423 | MFLTGlyphString *gstring; | |
1424 | int from, to; | |
1425 | { | |
1426 | struct MFLTFontFT *flt_font_ft = (struct MFLTFontFT *) font; | |
1427 | FT_Face ft_face = flt_font_ft->ft_face; | |
1428 | MFLTGlyph *g; | |
1429 | ||
1430 | for (g = gstring->glyphs + from; from < to; g++, from++) | |
1431 | if (! g->encoded) | |
1432 | { | |
1433 | FT_UInt code = FT_Get_Char_Index (ft_face, g->code); | |
1434 | ||
1435 | g->code = code > 0 ? code : FONT_INVALID_CODE; | |
1436 | g->encoded = 1; | |
1437 | } | |
1438 | return 0; | |
1439 | } | |
1440 | ||
1441 | static int | |
1442 | ftfont_get_metrics (font, gstring, from, to) | |
1443 | MFLTFont *font; | |
1444 | MFLTGlyphString *gstring; | |
1445 | int from, to; | |
1446 | { | |
1447 | struct MFLTFontFT *flt_font_ft = (struct MFLTFontFT *) font; | |
1448 | FT_Face ft_face = flt_font_ft->ft_face; | |
1449 | MFLTGlyph *g; | |
1450 | ||
1451 | for (g = gstring->glyphs + from; from < to; g++, from++) | |
1452 | if (! g->measured) | |
1453 | { | |
1454 | if (g->code != FONT_INVALID_CODE) | |
1455 | { | |
1456 | FT_Glyph_Metrics *m; | |
1457 | ||
1458 | if (FT_Load_Glyph (ft_face, g->code, FT_LOAD_DEFAULT) != 0) | |
1459 | abort (); | |
1460 | m = &ft_face->glyph->metrics; | |
1461 | ||
1462 | g->lbearing = m->horiBearingX; | |
1463 | g->rbearing = m->horiBearingX + m->width; | |
1464 | g->ascent = m->horiBearingY; | |
1465 | g->descent = m->height - m->horiBearingY; | |
1466 | g->xadv = m->horiAdvance; | |
1467 | } | |
1468 | else | |
1469 | { | |
1470 | g->lbearing = 0; | |
42984a74 | 1471 | g->rbearing = g->xadv = flt_font_ft->font->space_width << 6; |
de023c40 KH |
1472 | g->ascent = flt_font_ft->font->ascent << 6; |
1473 | g->descent = flt_font_ft->font->descent << 6; | |
1474 | } | |
1475 | g->yadv = 0; | |
1476 | g->measured = 1; | |
1477 | } | |
1478 | return 0; | |
1479 | } | |
1480 | ||
2a46904e | 1481 | static int |
de023c40 KH |
1482 | ftfont_check_otf (MFLTFont *font, MFLTOtfSpec *spec) |
1483 | { | |
1484 | struct MFLTFontFT *flt_font_ft = (struct MFLTFontFT *) font; | |
1485 | OTF *otf = flt_font_ft->otf; | |
1486 | OTF_Tag *tags; | |
1487 | int i, n, negative; | |
1488 | ||
1489 | for (i = 0; i < 2; i++) | |
1490 | { | |
1491 | if (! spec->features[i]) | |
1492 | continue; | |
1493 | for (n = 0; spec->features[i][n]; n++); | |
1494 | tags = alloca (sizeof (OTF_Tag) * n); | |
1495 | for (n = 0, negative = 0; spec->features[i][n]; n++) | |
1496 | { | |
1497 | if (spec->features[i][n] == 0xFFFFFFFF) | |
1498 | negative = 1; | |
1499 | else if (negative) | |
1500 | tags[n - 1] = spec->features[i][n] | 0x80000000; | |
1501 | else | |
1502 | tags[n] = spec->features[i][n]; | |
1503 | } | |
1504 | if (n - negative > 0 | |
1505 | && OTF_check_features (otf, i == 0, spec->script, spec->langsys, | |
1506 | tags, n - negative) != 1) | |
1507 | return 0; | |
1508 | } | |
1509 | return 1; | |
1510 | } | |
1511 | ||
1512 | #define DEVICE_DELTA(table, size) \ | |
1513 | (((size) >= (table).StartSize && (size) <= (table).EndSize) \ | |
1514 | ? (table).DeltaValue[(size) - (table).StartSize] << 6 \ | |
1515 | : 0) | |
1516 | ||
1517 | static void | |
1518 | adjust_anchor (FT_Face ft_face, OTF_Anchor *anchor, | |
1519 | unsigned code, int x_ppem, int y_ppem, int *x, int *y) | |
1520 | { | |
1521 | if (anchor->AnchorFormat == 2) | |
1522 | { | |
1523 | FT_Outline *outline; | |
1524 | int ap = anchor->f.f1.AnchorPoint; | |
1525 | ||
1526 | FT_Load_Glyph (ft_face, (FT_UInt) code, FT_LOAD_MONOCHROME); | |
1527 | outline = &ft_face->glyph->outline; | |
1528 | if (ap < outline->n_points) | |
1529 | { | |
1530 | *x = outline->points[ap].x << 6; | |
1531 | *y = outline->points[ap].y << 6; | |
1532 | } | |
1533 | } | |
1534 | else if (anchor->AnchorFormat == 3) | |
1535 | { | |
d90bfd1c KH |
1536 | if (anchor->f.f2.XDeviceTable.offset |
1537 | && anchor->f.f2.XDeviceTable.DeltaValue) | |
de023c40 | 1538 | *x += DEVICE_DELTA (anchor->f.f2.XDeviceTable, x_ppem); |
d90bfd1c KH |
1539 | if (anchor->f.f2.YDeviceTable.offset |
1540 | && anchor->f.f2.YDeviceTable.DeltaValue) | |
de023c40 KH |
1541 | *y += DEVICE_DELTA (anchor->f.f2.YDeviceTable, y_ppem); |
1542 | } | |
1543 | } | |
1544 | ||
1545 | static OTF_GlyphString otf_gstring; | |
1546 | ||
2b33f1ef KH |
1547 | static void |
1548 | setup_otf_gstring (int size) | |
1549 | { | |
1550 | if (otf_gstring.size == 0) | |
1551 | { | |
1552 | otf_gstring.glyphs = (OTF_Glyph *) malloc (sizeof (OTF_Glyph) * size); | |
1553 | otf_gstring.size = size; | |
1554 | } | |
1555 | else if (otf_gstring.size < size) | |
1556 | { | |
1557 | otf_gstring.glyphs = (OTF_Glyph *) realloc (otf_gstring.glyphs, | |
1558 | sizeof (OTF_Glyph) * size); | |
1559 | otf_gstring.size = size; | |
1560 | } | |
1561 | otf_gstring.used = size; | |
1562 | memset (otf_gstring.glyphs, 0, sizeof (OTF_Glyph) * size); | |
1563 | } | |
1564 | ||
1565 | ||
2a46904e | 1566 | static int |
de023c40 KH |
1567 | ftfont_drive_otf (font, spec, in, from, to, out, adjustment) |
1568 | MFLTFont *font; | |
1569 | MFLTOtfSpec *spec; | |
1570 | MFLTGlyphString *in; | |
1571 | int from, to; | |
1572 | MFLTGlyphString *out; | |
1573 | MFLTGlyphAdjustment *adjustment; | |
1574 | { | |
1575 | struct MFLTFontFT *flt_font_ft = (struct MFLTFontFT *) font; | |
1576 | FT_Face ft_face = flt_font_ft->ft_face; | |
1577 | OTF *otf = flt_font_ft->otf; | |
1578 | int len = to - from; | |
1579 | int i, j, gidx; | |
1580 | OTF_Glyph *otfg; | |
1581 | char script[5], *langsys = NULL; | |
1582 | char *gsub_features = NULL, *gpos_features = NULL; | |
1583 | ||
1584 | if (len == 0) | |
1585 | return from; | |
1586 | OTF_tag_name (spec->script, script); | |
1587 | if (spec->langsys) | |
1588 | { | |
1589 | langsys = alloca (5); | |
1590 | OTF_tag_name (spec->langsys, langsys); | |
1591 | } | |
1592 | for (i = 0; i < 2; i++) | |
1593 | { | |
1594 | char *p; | |
1595 | ||
1596 | if (spec->features[i] && spec->features[i][1] != 0xFFFFFFFF) | |
1597 | { | |
1598 | for (j = 0; spec->features[i][j]; j++); | |
1599 | if (i == 0) | |
1600 | p = gsub_features = alloca (6 * j); | |
1601 | else | |
1602 | p = gpos_features = alloca (6 * j); | |
1603 | for (j = 0; spec->features[i][j]; j++) | |
1604 | { | |
1605 | if (spec->features[i][j] == 0xFFFFFFFF) | |
1606 | *p++ = '*', *p++ = ','; | |
1607 | else | |
1608 | { | |
1609 | OTF_tag_name (spec->features[i][j], p); | |
1610 | p[4] = ','; | |
1611 | p += 5; | |
1612 | } | |
1613 | } | |
1614 | *--p = '\0'; | |
1615 | } | |
1616 | } | |
1617 | ||
2b33f1ef | 1618 | setup_otf_gstring (len); |
de023c40 KH |
1619 | for (i = 0; i < len; i++) |
1620 | { | |
1621 | otf_gstring.glyphs[i].c = in->glyphs[from + i].c; | |
1622 | otf_gstring.glyphs[i].glyph_id = in->glyphs[from + i].code; | |
1623 | } | |
1624 | ||
1625 | OTF_drive_gdef (otf, &otf_gstring); | |
1626 | gidx = out->used; | |
1627 | ||
1628 | if (gsub_features) | |
1629 | { | |
1630 | if (OTF_drive_gsub (otf, &otf_gstring, script, langsys, gsub_features) | |
1631 | < 0) | |
1632 | goto simple_copy; | |
1633 | if (out->allocated < out->used + otf_gstring.used) | |
1634 | return -2; | |
7d2fd545 | 1635 | for (i = 0, otfg = otf_gstring.glyphs; i < otf_gstring.used; ) |
de023c40 | 1636 | { |
7d2fd545 KH |
1637 | MFLTGlyph *g; |
1638 | int min_from, max_to; | |
de023c40 KH |
1639 | int j; |
1640 | ||
7d2fd545 | 1641 | g = out->glyphs + out->used; |
de023c40 | 1642 | *g = in->glyphs[from + otfg->f.index.from]; |
de023c40 KH |
1643 | if (g->code != otfg->glyph_id) |
1644 | { | |
7d2fd545 | 1645 | g->c = 0; |
de023c40 KH |
1646 | g->code = otfg->glyph_id; |
1647 | g->measured = 0; | |
1648 | } | |
1649 | out->used++; | |
7d2fd545 KH |
1650 | min_from = g->from; |
1651 | max_to = g->to; | |
1652 | if (otfg->f.index.from < otfg->f.index.to) | |
1653 | { | |
1654 | /* OTFG substitutes multiple glyphs in IN. */ | |
1655 | for (j = from + otfg->f.index.from + 1; | |
1656 | j <= from + otfg->f.index.to; j++) | |
1657 | { | |
1658 | if (min_from > in->glyphs[j].from) | |
1659 | min_from = in->glyphs[j].from; | |
1660 | if (max_to < in->glyphs[j].to) | |
1661 | max_to = in->glyphs[j].to; | |
1662 | } | |
1663 | g->from = min_from; | |
1664 | g->to = max_to; | |
1665 | } | |
1666 | for (i++, otfg++; (i < otf_gstring.used | |
1667 | && otfg->f.index.from == otfg[-1].f.index.from); | |
1668 | i++, otfg++) | |
1669 | { | |
1670 | g = out->glyphs + out->used; | |
1671 | *g = in->glyphs[from + otfg->f.index.to]; | |
1672 | if (g->code != otfg->glyph_id) | |
1673 | { | |
1674 | g->c = 0; | |
1675 | g->code = otfg->glyph_id; | |
1676 | g->measured = 0; | |
1677 | } | |
1678 | out->used++; | |
1679 | } | |
de023c40 KH |
1680 | } |
1681 | } | |
1682 | else | |
1683 | { | |
1684 | if (out->allocated < out->used + len) | |
1685 | return -2; | |
1686 | for (i = 0; i < len; i++) | |
1687 | out->glyphs[out->used++] = in->glyphs[from + i]; | |
1688 | } | |
1689 | ||
1690 | if (gpos_features) | |
1691 | { | |
1692 | MFLTGlyph *base = NULL, *mark = NULL, *g; | |
1693 | int x_ppem, y_ppem, x_scale, y_scale; | |
1694 | ||
1695 | if (OTF_drive_gpos (otf, &otf_gstring, script, langsys, gpos_features) | |
1696 | < 0) | |
1697 | return to; | |
1698 | ||
1699 | x_ppem = ft_face->size->metrics.x_ppem; | |
1700 | y_ppem = ft_face->size->metrics.y_ppem; | |
1701 | x_scale = ft_face->size->metrics.x_scale; | |
1702 | y_scale = ft_face->size->metrics.y_scale; | |
1703 | ||
1704 | for (i = 0, otfg = otf_gstring.glyphs, g = out->glyphs + gidx; | |
1705 | i < otf_gstring.used; i++, otfg++, g++) | |
1706 | { | |
1707 | MFLTGlyph *prev; | |
1708 | ||
1709 | if (! otfg->glyph_id) | |
1710 | continue; | |
1711 | switch (otfg->positioning_type) | |
1712 | { | |
1713 | case 0: | |
1714 | break; | |
1715 | case 1: /* Single */ | |
1716 | case 2: /* Pair */ | |
1717 | { | |
1718 | int format = otfg->f.f1.format; | |
1719 | ||
1720 | if (format & OTF_XPlacement) | |
1721 | adjustment[i].xoff | |
1722 | = otfg->f.f1.value->XPlacement * x_scale / 0x10000; | |
1723 | if (format & OTF_XPlaDevice) | |
1724 | adjustment[i].xoff | |
1725 | += DEVICE_DELTA (otfg->f.f1.value->XPlaDevice, x_ppem); | |
1726 | if (format & OTF_YPlacement) | |
1727 | adjustment[i].yoff | |
1728 | = - (otfg->f.f1.value->YPlacement * y_scale / 0x10000); | |
1729 | if (format & OTF_YPlaDevice) | |
1730 | adjustment[i].yoff | |
1731 | -= DEVICE_DELTA (otfg->f.f1.value->YPlaDevice, y_ppem); | |
1732 | if (format & OTF_XAdvance) | |
1733 | adjustment[i].xadv | |
1734 | += otfg->f.f1.value->XAdvance * x_scale / 0x10000; | |
1735 | if (format & OTF_XAdvDevice) | |
1736 | adjustment[i].xadv | |
1737 | += DEVICE_DELTA (otfg->f.f1.value->XAdvDevice, x_ppem); | |
1738 | if (format & OTF_YAdvance) | |
1739 | adjustment[i].yadv | |
1740 | += otfg->f.f1.value->YAdvance * y_scale / 0x10000; | |
1741 | if (format & OTF_YAdvDevice) | |
1742 | adjustment[i].yadv | |
1743 | += DEVICE_DELTA (otfg->f.f1.value->YAdvDevice, y_ppem); | |
1744 | adjustment[i].set = 1; | |
1745 | } | |
1746 | break; | |
1747 | case 3: /* Cursive */ | |
1748 | /* Not yet supported. */ | |
1749 | break; | |
1750 | case 4: /* Mark-to-Base */ | |
1751 | case 5: /* Mark-to-Ligature */ | |
1752 | if (! base) | |
1753 | break; | |
1754 | prev = base; | |
1755 | goto label_adjust_anchor; | |
1756 | default: /* i.e. case 6 Mark-to-Mark */ | |
1757 | if (! mark) | |
1758 | break; | |
1759 | prev = mark; | |
1760 | ||
1761 | label_adjust_anchor: | |
1762 | { | |
1763 | int base_x, base_y, mark_x, mark_y; | |
1764 | int this_from, this_to; | |
1765 | ||
1766 | base_x = otfg->f.f4.base_anchor->XCoordinate * x_scale / 0x10000; | |
1767 | base_y = otfg->f.f4.base_anchor->YCoordinate * y_scale / 0x10000; | |
1768 | mark_x = otfg->f.f4.mark_anchor->XCoordinate * x_scale / 0x10000; | |
1769 | mark_y = otfg->f.f4.mark_anchor->YCoordinate * y_scale / 0x10000;; | |
1770 | ||
1771 | if (otfg->f.f4.base_anchor->AnchorFormat != 1) | |
1772 | adjust_anchor (ft_face, otfg->f.f4.base_anchor, | |
1773 | prev->code, x_ppem, y_ppem, &base_x, &base_y); | |
1774 | if (otfg->f.f4.mark_anchor->AnchorFormat != 1) | |
1775 | adjust_anchor (ft_face, otfg->f.f4.mark_anchor, g->code, | |
1776 | x_ppem, y_ppem, &mark_x, &mark_y); | |
1777 | adjustment[i].xoff = (base_x - mark_x); | |
1778 | adjustment[i].yoff = - (base_y - mark_y); | |
1779 | adjustment[i].back = (g - prev); | |
1780 | adjustment[i].xadv = 0; | |
1781 | adjustment[i].advance_is_absolute = 1; | |
1782 | adjustment[i].set = 1; | |
1783 | this_from = g->from; | |
1784 | this_to = g->to; | |
1785 | for (j = 0; prev + j < g; j++) | |
1786 | { | |
1787 | if (this_from > prev[j].from) | |
1788 | this_from = prev[j].from; | |
1789 | if (this_to < prev[j].to) | |
1790 | this_to = prev[j].to; | |
1791 | } | |
1792 | for (; prev <= g; prev++) | |
1793 | { | |
1794 | prev->from = this_from; | |
1795 | prev->to = this_to; | |
1796 | } | |
1797 | } | |
1798 | } | |
1799 | if (otfg->GlyphClass == OTF_GlyphClass0) | |
1800 | base = mark = g; | |
1801 | else if (otfg->GlyphClass == OTF_GlyphClassMark) | |
1802 | mark = g; | |
1803 | else | |
1804 | base = g; | |
1805 | } | |
1806 | } | |
1807 | return to; | |
1808 | ||
1809 | simple_copy: | |
1810 | if (out->allocated < out->used + len) | |
1811 | return -2; | |
1812 | font->get_metrics (font, in, from, to); | |
1813 | memcpy (out->glyphs + out->used, in->glyphs + from, | |
1814 | sizeof (MFLTGlyph) * len); | |
1815 | out->used += len; | |
1816 | return to; | |
1817 | } | |
1818 | ||
1819 | static MFLTGlyphString gstring; | |
1820 | ||
1821 | static int m17n_flt_initialized; | |
1822 | ||
1823 | extern Lisp_Object QCfamily; | |
1824 | ||
990a73f0 | 1825 | static Lisp_Object |
de023c40 KH |
1826 | ftfont_shape_by_flt (lgstring, font, ft_face, otf) |
1827 | Lisp_Object lgstring; | |
1828 | struct font *font; | |
1829 | FT_Face ft_face; | |
1830 | OTF *otf; | |
1831 | { | |
89a95b7c | 1832 | EMACS_UINT len = LGSTRING_GLYPH_LEN (lgstring); |
de023c40 KH |
1833 | EMACS_UINT i; |
1834 | struct MFLTFontFT flt_font_ft; | |
da2cf488 | 1835 | MFLT *flt = NULL; |
2b33f1ef | 1836 | int with_variation_selector = 0; |
de023c40 KH |
1837 | |
1838 | if (! m17n_flt_initialized) | |
1839 | { | |
1840 | M17N_INIT (); | |
1841 | m17n_flt_initialized = 1; | |
1842 | } | |
1843 | ||
1844 | for (i = 0; i < len; i++) | |
2b33f1ef KH |
1845 | { |
1846 | Lisp_Object g = LGSTRING_GLYPH (lgstring, i); | |
1847 | int c; | |
1848 | ||
1849 | if (NILP (g)) | |
1850 | break; | |
1851 | c = LGLYPH_CHAR (g); | |
1852 | if (CHAR_VARIATION_SELECTOR_P (c)) | |
1853 | with_variation_selector++; | |
1854 | } | |
de023c40 | 1855 | len = i; |
2b33f1ef KH |
1856 | if (with_variation_selector) |
1857 | { | |
1858 | setup_otf_gstring (len); | |
1859 | for (i = 0; i < len; i++) | |
1860 | { | |
1861 | Lisp_Object g = LGSTRING_GLYPH (lgstring, i); | |
1862 | ||
1863 | otf_gstring.glyphs[i].c = LGLYPH_CHAR (g); | |
1864 | otf_gstring.glyphs[i].f.index.from = LGLYPH_FROM (g); | |
1865 | otf_gstring.glyphs[i].f.index.to = LGLYPH_TO (g); | |
1866 | } | |
1867 | OTF_drive_cmap (otf, &otf_gstring); | |
1868 | for (i = 0; i < otf_gstring.used; i++) | |
1869 | { | |
1870 | OTF_Glyph *otfg = otf_gstring.glyphs + i; | |
1871 | Lisp_Object g0 = LGSTRING_GLYPH (lgstring, otfg->f.index.from); | |
1872 | Lisp_Object g1 = LGSTRING_GLYPH (lgstring, otfg->f.index.to); | |
1873 | ||
1874 | LGLYPH_SET_CODE (g0, otfg->glyph_id); | |
1875 | LGLYPH_SET_TO (g0, LGLYPH_TO (g1)); | |
1876 | LGSTRING_SET_GLYPH (lgstring, i, g0); | |
1877 | } | |
1878 | if (len > otf_gstring.used) | |
1879 | { | |
1880 | len = otf_gstring.used; | |
1881 | LGSTRING_SET_GLYPH (lgstring, len, Qnil); | |
1882 | } | |
1883 | } | |
de023c40 KH |
1884 | |
1885 | if (gstring.allocated == 0) | |
1886 | { | |
1887 | gstring.allocated = len * 2; | |
1888 | gstring.glyph_size = sizeof (MFLTGlyph); | |
1889 | gstring.glyphs = malloc (sizeof (MFLTGlyph) * gstring.allocated); | |
1890 | } | |
1891 | else if (gstring.allocated < len * 2) | |
1892 | { | |
1893 | gstring.allocated = len * 2; | |
1894 | gstring.glyphs = realloc (gstring.glyphs, | |
1895 | sizeof (MFLTGlyph) * gstring.allocated); | |
1896 | } | |
2b33f1ef | 1897 | memset (gstring.glyphs, 0, sizeof (MFLTGlyph) * len); |
de023c40 | 1898 | for (i = 0; i < len; i++) |
2b33f1ef KH |
1899 | { |
1900 | Lisp_Object g = LGSTRING_GLYPH (lgstring, i); | |
1901 | ||
1902 | gstring.glyphs[i].c = LGLYPH_CHAR (g); | |
1903 | if (with_variation_selector) | |
1904 | { | |
1905 | gstring.glyphs[i].code = LGLYPH_CODE (g); | |
1906 | gstring.glyphs[i].encoded = 1; | |
1907 | } | |
1908 | } | |
1909 | ||
de023c40 KH |
1910 | gstring.used = len; |
1911 | gstring.r2l = 0; | |
1912 | ||
1913 | { | |
1914 | Lisp_Object family = Ffont_get (LGSTRING_FONT (lgstring), QCfamily); | |
1915 | ||
1916 | if (NILP (family)) | |
1917 | flt_font_ft.flt_font.family = Mnil; | |
1918 | else | |
21988a08 | 1919 | flt_font_ft.flt_font.family |
fdf90679 | 1920 | = msymbol ((char *) SDATA (Fdowncase (SYMBOL_NAME (family)))); |
de023c40 KH |
1921 | } |
1922 | flt_font_ft.flt_font.x_ppem = ft_face->size->metrics.x_ppem; | |
1923 | flt_font_ft.flt_font.y_ppem = ft_face->size->metrics.y_ppem; | |
1924 | flt_font_ft.flt_font.get_glyph_id = ftfont_get_glyph_id; | |
1925 | flt_font_ft.flt_font.get_metrics = ftfont_get_metrics; | |
1926 | flt_font_ft.flt_font.check_otf = ftfont_check_otf; | |
1927 | flt_font_ft.flt_font.drive_otf = ftfont_drive_otf; | |
1928 | flt_font_ft.flt_font.internal = NULL; | |
1929 | flt_font_ft.font = font; | |
1930 | flt_font_ft.ft_face = ft_face; | |
1931 | flt_font_ft.otf = otf; | |
7bf1bb21 KH |
1932 | if (len > 1 |
1933 | && gstring.glyphs[1].c >= 0x300 && gstring.glyphs[1].c <= 0x36F) | |
da2cf488 KH |
1934 | /* A little bit ad hoc. Perhaps, shaper must get script and |
1935 | language information, and select a proper flt for them | |
1936 | here. */ | |
1937 | flt = mflt_get (msymbol ("combining")); | |
de023c40 KH |
1938 | for (i = 0; i < 3; i++) |
1939 | { | |
da2cf488 | 1940 | int result = mflt_run (&gstring, 0, len, &flt_font_ft.flt_font, flt); |
de023c40 KH |
1941 | if (result != -2) |
1942 | break; | |
1943 | gstring.allocated += gstring.allocated; | |
1944 | gstring.glyphs = realloc (gstring.glyphs, | |
1945 | sizeof (MFLTGlyph) * gstring.allocated); | |
1946 | } | |
89a95b7c | 1947 | if (gstring.used > LGSTRING_GLYPH_LEN (lgstring)) |
de023c40 KH |
1948 | return Qnil; |
1949 | for (i = 0; i < gstring.used; i++) | |
1950 | { | |
1951 | MFLTGlyph *g = gstring.glyphs + i; | |
1952 | ||
1953 | g->from = LGLYPH_FROM (LGSTRING_GLYPH (lgstring, g->from)); | |
1954 | g->to = LGLYPH_TO (LGSTRING_GLYPH (lgstring, g->to)); | |
1955 | } | |
1956 | ||
1957 | for (i = 0; i < gstring.used; i++) | |
1958 | { | |
1959 | Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i); | |
1960 | MFLTGlyph *g = gstring.glyphs + i; | |
1961 | ||
4cec6061 KH |
1962 | if (NILP (lglyph)) |
1963 | { | |
1964 | lglyph = Fmake_vector (make_number (LGLYPH_SIZE), Qnil); | |
1965 | LGSTRING_SET_GLYPH (lgstring, i, lglyph); | |
1966 | } | |
de023c40 KH |
1967 | LGLYPH_SET_FROM (lglyph, g->from); |
1968 | LGLYPH_SET_TO (lglyph, g->to); | |
1969 | LGLYPH_SET_CHAR (lglyph, g->c); | |
1970 | LGLYPH_SET_CODE (lglyph, g->code); | |
1971 | LGLYPH_SET_WIDTH (lglyph, g->xadv >> 6); | |
1972 | LGLYPH_SET_LBEARING (lglyph, g->lbearing >> 6); | |
1973 | LGLYPH_SET_RBEARING (lglyph, g->rbearing >> 6); | |
1974 | LGLYPH_SET_ASCENT (lglyph, g->ascent >> 6); | |
1975 | LGLYPH_SET_DESCENT (lglyph, g->descent >> 6); | |
1976 | if (g->adjusted) | |
1977 | { | |
1978 | Lisp_Object vec; | |
1979 | ||
1980 | vec = Fmake_vector (make_number (3), Qnil); | |
1981 | ASET (vec, 0, make_number (g->xoff >> 6)); | |
1982 | ASET (vec, 1, make_number (g->yoff >> 6)); | |
1983 | ASET (vec, 2, make_number (g->xadv >> 6)); | |
1984 | LGLYPH_SET_ADJUSTMENT (lglyph, vec); | |
1985 | } | |
1986 | } | |
1987 | return make_number (i); | |
1988 | } | |
1989 | ||
1990 | Lisp_Object | |
1991 | ftfont_shape (lgstring) | |
1992 | Lisp_Object lgstring; | |
1993 | { | |
1994 | struct font *font; | |
1995 | struct ftfont_info *ftfont_info; | |
e302a291 | 1996 | OTF *otf; |
de023c40 KH |
1997 | |
1998 | CHECK_FONT_GET_OBJECT (LGSTRING_FONT (lgstring), font); | |
1999 | ftfont_info = (struct ftfont_info *) font; | |
e302a291 KH |
2000 | otf = ftfont_get_otf (ftfont_info); |
2001 | if (! otf) | |
4cc1c806 | 2002 | return make_number (0); |
e302a291 | 2003 | return ftfont_shape_by_flt (lgstring, font, ftfont_info->ft_size->face, otf); |
de023c40 KH |
2004 | } |
2005 | ||
2b33f1ef KH |
2006 | #ifdef HAVE_OTF_GET_VARIATION_GLYPHS |
2007 | ||
2008 | static int | |
2009 | ftfont_variation_glyphs (font, c, variations) | |
2010 | struct font *font; | |
2011 | int c; | |
2012 | unsigned variations[256]; | |
2013 | { | |
2014 | struct ftfont_info *ftfont_info = (struct ftfont_info *) font; | |
2015 | OTF *otf = ftfont_get_otf (ftfont_info); | |
2016 | ||
2017 | if (! otf) | |
2018 | return 0; | |
2019 | return OTF_get_variation_glyphs (otf, c, variations); | |
2020 | } | |
2021 | ||
2022 | #endif /* HAVE_OTF_GET_VARIATION_GLYPHS */ | |
de023c40 KH |
2023 | #endif /* HAVE_M17N_FLT */ |
2024 | #endif /* HAVE_LIBOTF */ | |
2025 | ||
0b966021 | 2026 | Lisp_Object |
7d7ad10e | 2027 | ftfont_font_format (FcPattern *pattern, Lisp_Object filename) |
0b966021 | 2028 | { |
e907d979 | 2029 | FcChar8 *str; |
0b966021 | 2030 | |
e907d979 | 2031 | #ifdef FC_FONTFORMAT |
7d7ad10e | 2032 | if (pattern) |
719b3d63 | 2033 | { |
7d7ad10e KH |
2034 | if (FcPatternGetString (pattern, FC_FONTFORMAT, 0, &str) != FcResultMatch) |
2035 | return Qnil; | |
2036 | if (strcmp ((char *) str, "TrueType") == 0) | |
719b3d63 | 2037 | return intern ("truetype"); |
7d7ad10e | 2038 | if (strcmp ((char *) str, "Type 1") == 0) |
719b3d63 | 2039 | return intern ("type1"); |
7d7ad10e | 2040 | if (strcmp ((char *) str, "PCF") == 0) |
719b3d63 | 2041 | return intern ("pcf"); |
7d7ad10e | 2042 | if (strcmp ((char *) str, "BDF") == 0) |
719b3d63 KH |
2043 | return intern ("bdf"); |
2044 | } | |
7d7ad10e KH |
2045 | #endif /* FC_FONTFORMAT */ |
2046 | if (STRINGP (filename)) | |
2047 | { | |
2048 | int len = SBYTES (filename); | |
2049 | ||
2050 | if (len >= 4) | |
2051 | { | |
2052 | str = (FcChar8 *) (SDATA (filename) + len - 4); | |
2053 | if (xstrcasecmp ((char *) str, ".ttf") == 0) | |
2054 | return intern ("truetype"); | |
2055 | if (xstrcasecmp ((char *) str, ".pfb") == 0) | |
2056 | return intern ("type1"); | |
2057 | if (xstrcasecmp ((char *) str, ".pcf") == 0) | |
2058 | return intern ("pcf"); | |
2059 | if (xstrcasecmp ((char *) str, ".bdf") == 0) | |
2060 | return intern ("bdf"); | |
2061 | } | |
2062 | } | |
0b966021 KH |
2063 | return intern ("unknown"); |
2064 | } | |
2065 | ||
c2f5bfd6 KH |
2066 | \f |
2067 | void | |
2068 | syms_of_ftfont () | |
2069 | { | |
706b6995 KH |
2070 | DEFSYM (Qfreetype, "freetype"); |
2071 | DEFSYM (Qmonospace, "monospace"); | |
2072 | DEFSYM (Qsans_serif, "sans-serif"); | |
2073 | DEFSYM (Qserif, "serif"); | |
2074 | DEFSYM (Qmono, "mono"); | |
2075 | DEFSYM (Qsans, "sans"); | |
2076 | DEFSYM (Qsans__serif, "sans serif"); | |
2077 | ||
c2f5bfd6 | 2078 | staticpro (&freetype_font_cache); |
c2801c99 | 2079 | freetype_font_cache = Fcons (Qt, Qnil); |
c2f5bfd6 | 2080 | |
706b6995 KH |
2081 | staticpro (&ftfont_generic_family_list); |
2082 | ftfont_generic_family_list | |
2083 | = Fcons (Fcons (Qmonospace, Qt), | |
2084 | Fcons (Fcons (Qsans_serif, Qt), | |
2085 | Fcons (Fcons (Qsans, Qt), Qnil))); | |
c2f5bfd6 | 2086 | |
21988a08 KH |
2087 | staticpro (&ft_face_cache); |
2088 | ft_face_cache = Qnil; | |
2089 | ||
c2f5bfd6 KH |
2090 | ftfont_driver.type = Qfreetype; |
2091 | register_font_driver (&ftfont_driver, NULL); | |
2092 | } | |
885b7d09 MB |
2093 | |
2094 | /* arch-tag: 7cfa432c-33a6-4988-83d2-a82ed8604aca | |
2095 | (do not change this comment) */ |