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