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 | ||
2a46904e | 45 | /* Flag to tell if FcInit is already 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 | } | |
2a46904e | 363 | fc_charset_table[i].fc_charset = charset; |
21988a08 KH |
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 | ||
2a46904e | 423 | otf_spec = XCDR (otf_spec); |
cc63eaf9 KH |
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 | 471 | int scalable = -1; |
42984a74 KH |
472 | Lisp_Object name = Qnil; |
473 | Lisp_Object script = Qnil; | |
21988a08 | 474 | Lisp_Object registry; |
c2f5bfd6 | 475 | |
63565713 | 476 | if (! NILP (AREF (spec, FONT_ADSTYLE_INDEX)) |
42984a74 KH |
477 | && SBYTES (SYMBOL_NAME (AREF (spec, FONT_ADSTYLE_INDEX))) > 0) |
478 | /* Fontconfig doesn't support adstyle property. */ | |
479 | return NULL; | |
480 | if ((n = FONT_SLANT_NUMERIC (spec)) >= 0 | |
481 | && n < 100) | |
63565713 | 482 | /* Fontconfig doesn't support reverse-italic/obligue. */ |
42984a74 KH |
483 | return NULL; |
484 | ||
485 | if (INTEGERP (AREF (spec, FONT_DPI_INDEX))) | |
486 | dpi = XINT (AREF (spec, FONT_DPI_INDEX)); | |
42984a74 KH |
487 | if (INTEGERP (AREF (spec, FONT_AVGWIDTH_INDEX)) |
488 | && XINT (AREF (spec, FONT_AVGWIDTH_INDEX)) == 0) | |
489 | scalable = 1; | |
63565713 | 490 | |
21988a08 KH |
491 | registry = AREF (spec, FONT_REGISTRY_INDEX); |
492 | if (NILP (registry) | |
493 | || EQ (registry, Qiso10646_1) | |
494 | || EQ (registry, Qunicode_bmp) | |
495 | || EQ (registry, Qunicode_sip)) | |
496 | *fc_charset_idx = -1; | |
497 | else | |
c2f5bfd6 | 498 | { |
21988a08 KH |
499 | *fc_charset_idx = ftfont_get_charset (registry); |
500 | if (*fc_charset_idx < 0) | |
42984a74 | 501 | return NULL; |
21988a08 | 502 | charset = fc_charset_table[*fc_charset_idx].fc_charset; |
c2f5bfd6 KH |
503 | } |
504 | ||
cc63eaf9 | 505 | otlayout[0] = '\0'; |
8daf5667 KH |
506 | for (extra = AREF (spec, FONT_EXTRA_INDEX); |
507 | CONSP (extra); extra = XCDR (extra)) | |
c2f5bfd6 | 508 | { |
8daf5667 KH |
509 | Lisp_Object key, val; |
510 | ||
42984a74 KH |
511 | key = XCAR (XCAR (extra)), val = XCDR (XCAR (extra)); |
512 | if (EQ (key, QCdpi)) | |
513 | dpi = XINT (val); | |
514 | else if (EQ (key, QCfc_unknown_spec)) | |
515 | name = val; | |
516 | else if (EQ (key, QClang)) | |
c2f5bfd6 KH |
517 | { |
518 | langset = FcLangSetCreate (); | |
519 | if (! langset) | |
520 | goto err; | |
8daf5667 | 521 | if (SYMBOLP (val)) |
c2f5bfd6 | 522 | { |
8daf5667 | 523 | if (! FcLangSetAdd (langset, SYMBOL_FcChar8 (val))) |
c2f5bfd6 KH |
524 | goto err; |
525 | } | |
526 | else | |
8daf5667 KH |
527 | for (; CONSP (val); val = XCDR (val)) |
528 | if (SYMBOLP (XCAR (val)) | |
529 | && ! FcLangSetAdd (langset, SYMBOL_FcChar8 (XCAR (val)))) | |
530 | goto err; | |
c2f5bfd6 | 531 | } |
42984a74 KH |
532 | else if (EQ (key, QCname)) |
533 | name = val; | |
534 | else if (EQ (key, QCotf)) | |
535 | { | |
536 | *otspec = ftfont_get_open_type_spec (val); | |
537 | if (! *otspec) | |
538 | return NULL; | |
539 | strcat (otlayout, "otlayout:"); | |
540 | OTF_TAG_STR ((*otspec)->script_tag, otlayout + 9); | |
541 | script = (*otspec)->script; | |
542 | } | |
8daf5667 KH |
543 | else if (EQ (key, QCscript)) |
544 | script = val; | |
8daf5667 KH |
545 | else if (EQ (key, QCscalable)) |
546 | scalable = ! NILP (val); | |
547 | } | |
c2f5bfd6 | 548 | |
8daf5667 KH |
549 | if (! NILP (script) && ! charset) |
550 | { | |
551 | Lisp_Object chars = assq_no_quit (script, Vscript_representative_chars); | |
552 | ||
553 | if (CONSP (chars)) | |
554 | { | |
555 | charset = FcCharSetCreate (); | |
556 | if (! charset) | |
557 | goto err; | |
558 | for (chars = XCDR (chars); CONSP (chars); chars = XCDR (chars)) | |
559 | if (CHARACTERP (XCAR (chars)) | |
560 | && ! FcCharSetAddChar (charset, XUINT (XCAR (chars)))) | |
561 | goto err; | |
c2f5bfd6 KH |
562 | } |
563 | } | |
564 | ||
42984a74 | 565 | pattern = NILP (name) ? FcPatternCreate () : FcNameParse (SDATA (name)); |
706b6995 KH |
566 | if (! pattern) |
567 | goto err; | |
42984a74 KH |
568 | FcPatternDel (pattern, FC_SIZE); |
569 | FcPatternDel (pattern, FC_PIXEL_SIZE); | |
318548be KH |
570 | FcPatternDel (pattern, FC_FOUNDRY); |
571 | FcPatternDel (pattern, FC_FAMILY); | |
706b6995 | 572 | tmp = AREF (spec, FONT_FOUNDRY_INDEX); |
42984a74 | 573 | if (! NILP (tmp) |
706b6995 KH |
574 | && ! FcPatternAddString (pattern, FC_FOUNDRY, SYMBOL_FcChar8 (tmp))) |
575 | goto err; | |
318548be KH |
576 | tmp = AREF (spec, FONT_FAMILY_INDEX); |
577 | if (! NILP (tmp) | |
578 | && ! FcPatternAddString (pattern, FC_FAMILY, SYMBOL_FcChar8 (tmp))) | |
579 | goto err; | |
c2f5bfd6 KH |
580 | if (charset |
581 | && ! FcPatternAddCharSet (pattern, FC_CHARSET, charset)) | |
582 | goto err; | |
583 | if (langset | |
584 | && ! FcPatternAddLangSet (pattern, FC_LANG, langset)) | |
585 | goto err; | |
bc9a2afe KH |
586 | if (dpi >= 0 |
587 | && ! FcPatternAddDouble (pattern, FC_DPI, dpi)) | |
588 | goto err; | |
bc9a2afe | 589 | if (scalable >= 0 |
25a5d05b | 590 | && ! FcPatternAddBool (pattern, FC_SCALABLE, scalable ? FcTrue : FcFalse)) |
bc9a2afe | 591 | goto err; |
42984a74 | 592 | goto finish; |
c2f5bfd6 | 593 | |
42984a74 KH |
594 | err: |
595 | /* We come here because of unexpected error in fontconfig API call | |
596 | (usually insufficient memory). */ | |
597 | if (pattern) | |
598 | { | |
599 | FcPatternDestroy (pattern); | |
600 | pattern = NULL; | |
601 | } | |
602 | if (*otspec) | |
603 | { | |
604 | if ((*otspec)->nfeatures[0] > 0) | |
605 | free ((*otspec)->features[0]); | |
606 | if ((*otspec)->nfeatures[1] > 0) | |
607 | free ((*otspec)->features[1]); | |
608 | free (*otspec); | |
609 | *otspec = NULL; | |
610 | } | |
611 | ||
612 | finish: | |
42984a74 | 613 | if (langset) FcLangSetDestroy (langset); |
de24e3eb | 614 | if (charset && *fc_charset_idx < 0) FcCharSetDestroy (charset); |
42984a74 KH |
615 | return pattern; |
616 | } | |
617 | ||
618 | static Lisp_Object | |
619 | ftfont_list (frame, spec) | |
620 | Lisp_Object frame, spec; | |
621 | { | |
318548be | 622 | Lisp_Object val = Qnil, registry, family; |
42984a74 KH |
623 | int i; |
624 | FcPattern *pattern; | |
625 | FcFontSet *fontset = NULL; | |
626 | FcObjectSet *objset = NULL; | |
21988a08 | 627 | int fc_charset_idx; |
42984a74 KH |
628 | char otlayout[15]; /* For "otlayout:XXXX" */ |
629 | struct OpenTypeSpec *otspec = NULL; | |
5d376f74 | 630 | int spacing = -1; |
2a46904e | 631 | |
42984a74 KH |
632 | if (! fc_initialized) |
633 | { | |
634 | FcInit (); | |
635 | fc_initialized = 1; | |
636 | } | |
637 | ||
21988a08 | 638 | pattern = ftfont_spec_pattern (spec, &fc_charset_idx, otlayout, &otspec); |
42984a74 KH |
639 | if (! pattern) |
640 | return Qnil; | |
5d376f74 KH |
641 | if (INTEGERP (AREF (spec, FONT_SPACING_INDEX))) |
642 | spacing = XINT (AREF (spec, FONT_SPACING_INDEX)); | |
318548be KH |
643 | registry = AREF (spec, FONT_REGISTRY_INDEX); |
644 | family = AREF (spec, FONT_FAMILY_INDEX); | |
645 | if (! NILP (family)) | |
646 | { | |
647 | Lisp_Object resolved; | |
648 | ||
649 | resolved = ftfont_resolve_generic_family (family); | |
650 | if (! NILP (resolved)) | |
651 | { | |
652 | FcPatternDel (pattern, FC_FAMILY); | |
653 | if (! FcPatternAddString (pattern, FC_FAMILY, | |
654 | SYMBOL_FcChar8 (resolved))) | |
655 | goto err; | |
656 | } | |
657 | } | |
658 | ||
706b6995 | 659 | objset = FcObjectSetBuild (FC_FOUNDRY, FC_FAMILY, FC_WEIGHT, FC_SLANT, |
42984a74 | 660 | FC_WIDTH, FC_PIXEL_SIZE, FC_SPACING, FC_SCALABLE, |
5d376f74 | 661 | FC_FILE, |
42984a74 KH |
662 | #ifdef FC_CAPABILITY |
663 | FC_CAPABILITY, | |
664 | #endif /* FC_CAPABILITY */ | |
e907d979 | 665 | NULL); |
706b6995 KH |
666 | if (! objset) |
667 | goto err; | |
42984a74 | 668 | |
318548be KH |
669 | fontset = FcFontList (NULL, pattern, objset); |
670 | if (! fontset) | |
671 | goto err; | |
672 | for (i = 0; i < fontset->nfont; i++) | |
bc5f6c42 | 673 | { |
318548be | 674 | Lisp_Object entity; |
f63d54dc | 675 | |
5d376f74 KH |
676 | if (spacing >= 0) |
677 | { | |
678 | int this; | |
679 | ||
680 | if ((FcPatternGetInteger (fontset->fonts[i], FC_SPACING, 0, &this) | |
681 | == FcResultMatch) | |
682 | && spacing != this) | |
683 | continue; | |
684 | } | |
685 | ||
bc5f6c42 | 686 | #ifdef FC_CAPABILITY |
318548be KH |
687 | if (otlayout[0]) |
688 | { | |
689 | FcChar8 *this; | |
a85f724a | 690 | |
318548be KH |
691 | if (FcPatternGetString (fontset->fonts[i], FC_CAPABILITY, 0, |
692 | &this) != FcResultMatch | |
693 | || ! strstr ((char *) this, otlayout)) | |
694 | continue; | |
695 | } | |
bc5f6c42 | 696 | #endif /* FC_CAPABILITY */ |
cc63eaf9 | 697 | #ifdef HAVE_LIBOTF |
318548be KH |
698 | if (otspec) |
699 | { | |
700 | FcChar8 *file; | |
701 | OTF *otf; | |
702 | ||
703 | if (FcPatternGetString (fontset->fonts[i], FC_FILE, 0, &file) | |
704 | != FcResultMatch) | |
705 | continue; | |
706 | otf = OTF_open ((char *) file); | |
707 | if (! otf) | |
708 | continue; | |
709 | if (OTF_check_features (otf, 1, | |
710 | otspec->script_tag, otspec->langsys_tag, | |
711 | otspec->features[0], | |
712 | otspec->nfeatures[0]) != 1 | |
713 | || OTF_check_features (otf, 0, | |
714 | otspec->script_tag, otspec->langsys_tag, | |
715 | otspec->features[1], | |
716 | otspec->nfeatures[1]) != 1) | |
717 | continue; | |
c2f5bfd6 | 718 | } |
318548be KH |
719 | #endif /* HAVE_LIBOTF */ |
720 | entity = ftfont_pattern_entity (fontset->fonts[i], registry, | |
721 | fc_charset_idx); | |
722 | if (! NILP (entity)) | |
723 | val = Fcons (entity, val); | |
c2f5bfd6 | 724 | } |
318548be | 725 | font_add_log ("ftfont-list", spec, val); |
c2f5bfd6 KH |
726 | goto finish; |
727 | ||
728 | err: | |
729 | /* We come here because of unexpected error in fontconfig API call | |
706b6995 | 730 | (usually insufficient memory). */ |
c2f5bfd6 KH |
731 | val = Qnil; |
732 | ||
733 | finish: | |
c2f5bfd6 KH |
734 | if (objset) FcObjectSetDestroy (objset); |
735 | if (fontset) FcFontSetDestroy (fontset); | |
c2f5bfd6 | 736 | if (pattern) FcPatternDestroy (pattern); |
c2f5bfd6 KH |
737 | return val; |
738 | } | |
739 | ||
8daf5667 KH |
740 | static Lisp_Object |
741 | ftfont_match (frame, spec) | |
742 | Lisp_Object frame, spec; | |
743 | { | |
21988a08 | 744 | Lisp_Object entity; |
42984a74 | 745 | FcPattern *pattern, *match = NULL; |
8daf5667 | 746 | FcResult result; |
42984a74 KH |
747 | char otlayout[15]; /* For "otlayout:XXXX" */ |
748 | struct OpenTypeSpec *otspec = NULL; | |
21988a08 | 749 | int fc_charset_idx; |
8daf5667 KH |
750 | |
751 | if (! fc_initialized) | |
752 | { | |
753 | FcInit (); | |
754 | fc_initialized = 1; | |
755 | } | |
756 | ||
21988a08 | 757 | pattern = ftfont_spec_pattern (spec, &fc_charset_idx, otlayout, &otspec); |
42984a74 | 758 | if (! pattern) |
8daf5667 KH |
759 | return Qnil; |
760 | ||
42984a74 | 761 | if (INTEGERP (AREF (spec, FONT_SIZE_INDEX))) |
8daf5667 | 762 | { |
42984a74 | 763 | FcValue value; |
55082642 | 764 | |
42984a74 KH |
765 | value.type = FcTypeDouble; |
766 | value.u.d = XINT (AREF (spec, FONT_SIZE_INDEX)); | |
767 | FcPatternAdd (pattern, FC_PIXEL_SIZE, value, FcFalse); | |
768 | } | |
769 | if (FcConfigSubstitute (NULL, pattern, FcMatchPattern) == FcTrue) | |
770 | { | |
771 | FcDefaultSubstitute (pattern); | |
772 | match = FcFontMatch (NULL, pattern, &result); | |
773 | if (match) | |
8daf5667 | 774 | { |
21988a08 | 775 | entity = ftfont_pattern_entity (match, Qunicode_bmp, fc_charset_idx); |
42984a74 KH |
776 | FcPatternDestroy (match); |
777 | if (! NILP (AREF (spec, FONT_FAMILY_INDEX)) | |
778 | && NILP (assq_no_quit (AREF (spec, FONT_FAMILY_INDEX), | |
779 | ftfont_generic_family_list)) | |
780 | && NILP (Fstring_equal (AREF (spec, FONT_FAMILY_INDEX), | |
781 | AREF (entity, FONT_FAMILY_INDEX)))) | |
782 | entity = Qnil; | |
8daf5667 | 783 | } |
8daf5667 | 784 | } |
42984a74 | 785 | FcPatternDestroy (pattern); |
8daf5667 | 786 | |
318548be | 787 | font_add_log ("ftfont-match", spec, entity); |
8daf5667 KH |
788 | return entity; |
789 | } | |
790 | ||
c2f5bfd6 KH |
791 | static Lisp_Object |
792 | ftfont_list_family (frame) | |
793 | Lisp_Object frame; | |
794 | { | |
795 | Lisp_Object list; | |
796 | FcPattern *pattern = NULL; | |
797 | FcFontSet *fontset = NULL; | |
798 | FcObjectSet *objset = NULL; | |
799 | int i; | |
800 | ||
801 | if (! fc_initialized) | |
802 | { | |
803 | FcInit (); | |
804 | fc_initialized = 1; | |
805 | } | |
806 | ||
807 | pattern = FcPatternCreate (); | |
808 | if (! pattern) | |
809 | goto finish; | |
706b6995 | 810 | objset = FcObjectSetBuild (FC_FAMILY, NULL); |
c2f5bfd6 KH |
811 | if (! objset) |
812 | goto finish; | |
813 | fontset = FcFontList (NULL, pattern, objset); | |
814 | if (! fontset) | |
815 | goto finish; | |
816 | ||
817 | list = Qnil; | |
818 | for (i = 0; i < fontset->nfont; i++) | |
819 | { | |
820 | FcPattern *pat = fontset->fonts[i]; | |
821 | FcChar8 *str; | |
822 | ||
823 | if (FcPatternGetString (pat, FC_FAMILY, 0, &str) == FcResultMatch) | |
42984a74 | 824 | list = Fcons (intern ((char *) str), list); |
c2f5bfd6 KH |
825 | } |
826 | ||
827 | finish: | |
828 | if (objset) FcObjectSetDestroy (objset); | |
829 | if (fontset) FcFontSetDestroy (fontset); | |
830 | if (pattern) FcPatternDestroy (pattern); | |
831 | ||
832 | return list; | |
833 | } | |
834 | ||
835 | ||
42984a74 | 836 | static Lisp_Object |
c2f5bfd6 KH |
837 | ftfont_open (f, entity, pixel_size) |
838 | FRAME_PTR f; | |
839 | Lisp_Object entity; | |
840 | int pixel_size; | |
841 | { | |
842 | struct ftfont_info *ftfont_info; | |
843 | struct font *font; | |
844 | FT_Face ft_face; | |
845 | FT_Size ft_size; | |
846 | FT_UInt size; | |
21988a08 KH |
847 | Lisp_Object val, filename, cache, font_object; |
848 | int fc_charset_idx; | |
c2f5bfd6 | 849 | FcPattern *pattern; |
21988a08 | 850 | int scalable; |
c2f5bfd6 | 851 | int spacing; |
42984a74 KH |
852 | char name[256]; |
853 | int i, len; | |
854 | int upEM; | |
c2f5bfd6 | 855 | |
42984a74 KH |
856 | val = assq_no_quit (QCfont_entity, AREF (entity, FONT_EXTRA_INDEX)); |
857 | if (! CONSP (val)) | |
858 | return Qnil; | |
859 | val = XCDR (val); | |
21988a08 KH |
860 | filename = XCAR (val); |
861 | fc_charset_idx = XINT (XCDR (val)); | |
862 | cache = ftfont_lookup_cache (filename); | |
863 | if (NILP (cache)) | |
864 | return Qnil; | |
865 | filename = XCAR (cache); | |
866 | val = XCDR (cache); | |
867 | ft_face = XSAVE_VALUE (val)->pointer; | |
868 | if (XSAVE_VALUE (val)->integer > 0) | |
c2f5bfd6 | 869 | { |
21988a08 | 870 | /* FT_Face in this cache is already used by the different size. */ |
c2f5bfd6 | 871 | if (FT_New_Size (ft_face, &ft_size) != 0) |
42984a74 | 872 | return Qnil; |
c2f5bfd6 KH |
873 | if (FT_Activate_Size (ft_size) != 0) |
874 | { | |
875 | FT_Done_Size (ft_size); | |
42984a74 | 876 | return Qnil; |
c2f5bfd6 | 877 | } |
21988a08 KH |
878 | } |
879 | XSAVE_VALUE (val)->integer++; | |
c2f5bfd6 KH |
880 | size = XINT (AREF (entity, FONT_SIZE_INDEX)); |
881 | if (size == 0) | |
882 | size = pixel_size; | |
883 | if (FT_Set_Pixel_Sizes (ft_face, size, size) != 0) | |
884 | { | |
885 | if (XSAVE_VALUE (val)->integer == 0) | |
886 | FT_Done_Face (ft_face); | |
42984a74 | 887 | return Qnil; |
c2f5bfd6 KH |
888 | } |
889 | ||
42984a74 KH |
890 | font_object = font_make_object (VECSIZE (struct ftfont_info)); |
891 | ASET (font_object, FONT_TYPE_INDEX, Qfreetype); | |
892 | for (i = 1;i < FONT_ENTITY_MAX; i++) | |
893 | ASET (font_object, i, AREF (entity, i)); | |
894 | ASET (font_object, FONT_SIZE_INDEX, make_number (size)); | |
895 | len = font_unparse_xlfd (entity, size, name, 256); | |
896 | if (len > 0) | |
897 | ASET (font_object, FONT_NAME_INDEX, make_unibyte_string (name, len)); | |
898 | len = font_unparse_fcname (entity, size, name, 256); | |
899 | if (len > 0) | |
900 | ASET (font_object, FONT_FULLNAME_INDEX, make_unibyte_string (name, len)); | |
901 | else | |
902 | ASET (font_object, FONT_FULLNAME_INDEX, | |
903 | AREF (font_object, FONT_NAME_INDEX)); | |
21988a08 | 904 | ASET (font_object, FONT_FILE_INDEX, filename); |
42984a74 KH |
905 | ASET (font_object, FONT_FORMAT_INDEX, ftfont_font_format (pattern)); |
906 | font = XFONT_OBJECT (font_object); | |
907 | ftfont_info = (struct ftfont_info *) font; | |
21988a08 KH |
908 | ftfont_info->ft_size = ft_face->size; |
909 | ftfont_info->fc_charset_idx = fc_charset_idx; | |
0d674a05 | 910 | #ifdef HAVE_LIBOTF |
de023c40 KH |
911 | ftfont_info->maybe_otf = ft_face->face_flags & FT_FACE_FLAG_SFNT; |
912 | ftfont_info->otf = NULL; | |
0d674a05 | 913 | #endif /* HAVE_LIBOTF */ |
c2f5bfd6 KH |
914 | font->pixel_size = size; |
915 | font->driver = &ftfont_driver; | |
42984a74 | 916 | font->encoding_charset = font->repertory_charset = -1; |
c9c0c429 | 917 | |
42984a74 | 918 | upEM = ft_face->units_per_EM; |
21988a08 KH |
919 | scalable = (INTEGERP (AREF (entity, FONT_AVGWIDTH_INDEX)) |
920 | && XINT (AREF (entity, FONT_AVGWIDTH_INDEX)) == 0); | |
42984a74 KH |
921 | if (scalable) |
922 | { | |
923 | font->ascent = ft_face->ascender * size / upEM; | |
924 | font->descent = - ft_face->descender * size / upEM; | |
925 | font->height = ft_face->height * size / upEM; | |
926 | } | |
927 | else | |
928 | { | |
929 | font->ascent = ft_face->size->metrics.ascender >> 6; | |
930 | font->descent = - ft_face->size->metrics.descender >> 6; | |
931 | font->height = ft_face->size->metrics.height >> 6; | |
c9c0c429 | 932 | } |
21988a08 KH |
933 | if (INTEGERP (AREF (entity, FONT_SPACING_INDEX))) |
934 | spacing = XINT (AREF (entity, FONT_SPACING_INDEX)); | |
935 | else | |
c9c0c429 KH |
936 | spacing = FC_PROPORTIONAL; |
937 | if (spacing != FC_PROPORTIONAL) | |
42984a74 KH |
938 | font->min_width = font->average_width = font->space_width |
939 | = (scalable ? ft_face->max_advance_width * size / upEM | |
940 | : ft_face->size->metrics.max_advance >> 6); | |
c2f5bfd6 KH |
941 | else |
942 | { | |
42984a74 | 943 | int n; |
c2f5bfd6 | 944 | |
42984a74 KH |
945 | font->min_width = font->average_width = font->space_width = 0; |
946 | for (i = 32, n = 0; i < 127; i++) | |
947 | if (FT_Load_Char (ft_face, i, FT_LOAD_DEFAULT) != 0) | |
948 | { | |
949 | int this_width = ft_face->glyph->metrics.horiAdvance >> 6; | |
950 | ||
951 | if (this_width > 0 | |
952 | && (! font->min_width || font->min_width > this_width)) | |
953 | font->min_width = this_width; | |
954 | if (i == 32) | |
955 | font->space_width = this_width; | |
956 | font->average_width += this_width; | |
957 | n++; | |
958 | } | |
959 | if (n > 0) | |
960 | font->average_width /= n; | |
c2f5bfd6 KH |
961 | } |
962 | ||
42984a74 KH |
963 | font->baseline_offset = 0; |
964 | font->relative_compose = 0; | |
965 | font->default_ascent = 0; | |
966 | font->vertical_centering = 0; | |
967 | if (scalable) | |
968 | { | |
969 | font->underline_position = -ft_face->underline_position * size / upEM; | |
970 | font->underline_thickness = -ft_face->underline_thickness * size / upEM; | |
971 | } | |
972 | else | |
973 | { | |
974 | font->underline_position = -1; | |
975 | font->underline_thickness = 0; | |
976 | } | |
c2f5bfd6 | 977 | |
42984a74 | 978 | return font_object; |
c2f5bfd6 KH |
979 | } |
980 | ||
981 | static void | |
982 | ftfont_close (f, font) | |
983 | FRAME_PTR f; | |
984 | struct font *font; | |
985 | { | |
986 | struct ftfont_info *ftfont_info = (struct ftfont_info *) font; | |
21988a08 | 987 | Lisp_Object val, cache; |
c2f5bfd6 | 988 | |
21988a08 KH |
989 | cache = ftfont_lookup_cache (font->props[FONT_FILE_INDEX]); |
990 | val = XCDR (cache); | |
c2f5bfd6 KH |
991 | (XSAVE_VALUE (val)->integer)--; |
992 | if (XSAVE_VALUE (val)->integer == 0) | |
de023c40 | 993 | { |
21988a08 KH |
994 | FT_Face ft_face = XSAVE_VALUE (val)->pointer; |
995 | ||
996 | FT_Done_Face (ft_face); | |
de023c40 KH |
997 | #ifdef HAVE_LIBOTF |
998 | if (ftfont_info->otf) | |
999 | OTF_close (ftfont_info->otf); | |
1000 | #endif | |
21988a08 | 1001 | XSAVE_VALUE (val)->pointer = NULL; |
de023c40 | 1002 | } |
c2f5bfd6 KH |
1003 | else |
1004 | FT_Done_Size (ftfont_info->ft_size); | |
c2f5bfd6 KH |
1005 | } |
1006 | ||
2a46904e | 1007 | static int |
c2f5bfd6 KH |
1008 | ftfont_has_char (entity, c) |
1009 | Lisp_Object entity; | |
1010 | int c; | |
1011 | { | |
1012 | Lisp_Object val; | |
21988a08 KH |
1013 | int fc_charset_idx; |
1014 | struct charset *charset; | |
c2f5bfd6 | 1015 | |
42984a74 | 1016 | val = assq_no_quit (QCfont_entity, AREF (entity, FONT_EXTRA_INDEX)); |
21988a08 KH |
1017 | fc_charset_idx = XINT (XCDR (XCDR (val))); |
1018 | if (fc_charset_idx < 0) | |
c2801c99 | 1019 | return -1; |
21988a08 KH |
1020 | charset = CHARSET_FROM_ID (fc_charset_table[fc_charset_idx].charset_id); |
1021 | return (ENCODE_CHAR (charset, c) != CHARSET_INVALID_CODE (charset)); | |
c2f5bfd6 KH |
1022 | } |
1023 | ||
1024 | static unsigned | |
1025 | ftfont_encode_char (font, c) | |
1026 | struct font *font; | |
1027 | int c; | |
1028 | { | |
1029 | struct ftfont_info *ftfont_info = (struct ftfont_info *) font; | |
1030 | FT_Face ft_face = ftfont_info->ft_size->face; | |
1031 | FT_ULong charcode = c; | |
1032 | FT_UInt code = FT_Get_Char_Index (ft_face, charcode); | |
1033 | ||
4cec6061 | 1034 | return (code > 0 ? code : FONT_INVALID_CODE); |
c2f5bfd6 KH |
1035 | } |
1036 | ||
1037 | static int | |
1038 | ftfont_text_extents (font, code, nglyphs, metrics) | |
1039 | struct font *font; | |
1040 | unsigned *code; | |
1041 | int nglyphs; | |
1042 | struct font_metrics *metrics; | |
1043 | { | |
1044 | struct ftfont_info *ftfont_info = (struct ftfont_info *) font; | |
1045 | FT_Face ft_face = ftfont_info->ft_size->face; | |
1046 | int width = 0; | |
1047 | int i; | |
1048 | ||
1049 | if (ftfont_info->ft_size != ft_face->size) | |
1050 | FT_Activate_Size (ftfont_info->ft_size); | |
1051 | if (metrics) | |
1052 | bzero (metrics, sizeof (struct font_metrics)); | |
1053 | for (i = 0; i < nglyphs; i++) | |
1054 | { | |
1055 | if (FT_Load_Glyph (ft_face, code[i], FT_LOAD_DEFAULT) == 0) | |
1056 | { | |
1057 | FT_Glyph_Metrics *m = &ft_face->glyph->metrics; | |
1058 | ||
1059 | if (metrics) | |
1060 | { | |
1061 | if (metrics->lbearing > width + (m->horiBearingX >> 6)) | |
1062 | metrics->lbearing = width + (m->horiBearingX >> 6); | |
1063 | if (metrics->rbearing | |
1064 | < width + ((m->horiBearingX + m->width) >> 6)) | |
1065 | metrics->rbearing | |
1066 | = width + ((m->horiBearingX + m->width) >> 6); | |
1067 | if (metrics->ascent < (m->horiBearingY >> 6)) | |
1068 | metrics->ascent = m->horiBearingY >> 6; | |
1069 | if (metrics->descent > ((m->horiBearingY + m->height) >> 6)) | |
1070 | metrics->descent = (m->horiBearingY + m->height) >> 6; | |
1071 | } | |
1072 | width += m->horiAdvance >> 6; | |
1073 | } | |
1074 | else | |
1075 | { | |
42984a74 | 1076 | width += font->space_width; |
c2f5bfd6 KH |
1077 | } |
1078 | } | |
1079 | if (metrics) | |
1080 | metrics->width = width; | |
1081 | ||
1082 | return width; | |
1083 | } | |
1084 | ||
1085 | static int | |
1086 | ftfont_get_bitmap (font, code, bitmap, bits_per_pixel) | |
1087 | struct font *font; | |
1088 | unsigned code; | |
1089 | struct font_bitmap *bitmap; | |
1090 | int bits_per_pixel; | |
1091 | { | |
1092 | struct ftfont_info *ftfont_info = (struct ftfont_info *) font; | |
1093 | FT_Face ft_face = ftfont_info->ft_size->face; | |
1094 | FT_Int32 load_flags = FT_LOAD_RENDER; | |
1095 | ||
1096 | if (ftfont_info->ft_size != ft_face->size) | |
1097 | FT_Activate_Size (ftfont_info->ft_size); | |
1098 | if (bits_per_pixel == 1) | |
1099 | { | |
1100 | #ifdef FT_LOAD_TARGET_MONO | |
1101 | load_flags |= FT_LOAD_TARGET_MONO; | |
1102 | #else | |
1103 | load_flags |= FT_LOAD_MONOCHROME; | |
1104 | #endif | |
1105 | } | |
1106 | else if (bits_per_pixel != 8) | |
1107 | /* We don't support such a rendering. */ | |
1108 | return -1; | |
1109 | ||
1110 | if (FT_Load_Glyph (ft_face, code, load_flags) != 0) | |
1111 | return -1; | |
b51e5112 KH |
1112 | bitmap->bits_per_pixel |
1113 | = (ft_face->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_MONO ? 1 | |
1114 | : ft_face->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_GRAY ? 8 | |
1115 | : ft_face->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_LCD ? 8 | |
1116 | : ft_face->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_LCD_V ? 8 | |
1117 | : -1); | |
1118 | if (bitmap->bits_per_pixel < 0) | |
1119 | /* We don't suport that kind of pixel mode. */ | |
1120 | return -1; | |
c2f5bfd6 KH |
1121 | bitmap->rows = ft_face->glyph->bitmap.rows; |
1122 | bitmap->width = ft_face->glyph->bitmap.width; | |
1123 | bitmap->pitch = ft_face->glyph->bitmap.pitch; | |
1124 | bitmap->buffer = ft_face->glyph->bitmap.buffer; | |
1125 | bitmap->left = ft_face->glyph->bitmap_left; | |
1126 | bitmap->top = ft_face->glyph->bitmap_top; | |
1127 | bitmap->advance = ft_face->glyph->metrics.horiAdvance >> 6; | |
1128 | bitmap->extra = NULL; | |
1129 | ||
1130 | return 0; | |
1131 | } | |
1132 | ||
1133 | static int | |
1134 | ftfont_anchor_point (font, code, index, x, y) | |
1135 | struct font *font; | |
1136 | unsigned code; | |
1137 | int index; | |
1138 | int *x, *y; | |
1139 | { | |
1140 | struct ftfont_info *ftfont_info = (struct ftfont_info *) font; | |
1141 | FT_Face ft_face = ftfont_info->ft_size->face; | |
1142 | ||
1143 | if (ftfont_info->ft_size != ft_face->size) | |
1144 | FT_Activate_Size (ftfont_info->ft_size); | |
1145 | if (FT_Load_Glyph (ft_face, code, FT_LOAD_DEFAULT) != 0) | |
1146 | return -1; | |
1147 | if (ft_face->glyph->format != FT_GLYPH_FORMAT_OUTLINE) | |
1148 | return -1; | |
1149 | if (index >= ft_face->glyph->outline.n_points) | |
1150 | return -1; | |
1151 | *x = ft_face->glyph->outline.points[index].x; | |
1152 | *y = ft_face->glyph->outline.points[index].y; | |
1153 | return 0; | |
1154 | } | |
1155 | ||
de023c40 KH |
1156 | #ifdef HAVE_LIBOTF |
1157 | #ifdef HAVE_M17N_FLT | |
1158 | ||
1159 | struct MFLTFontFT | |
1160 | { | |
1161 | MFLTFont flt_font; | |
1162 | struct font *font; | |
1163 | FT_Face ft_face; | |
1164 | OTF *otf; | |
1165 | }; | |
1166 | ||
1167 | static int | |
1168 | ftfont_get_glyph_id (font, gstring, from, to) | |
1169 | MFLTFont *font; | |
1170 | MFLTGlyphString *gstring; | |
1171 | int from, to; | |
1172 | { | |
1173 | struct MFLTFontFT *flt_font_ft = (struct MFLTFontFT *) font; | |
1174 | FT_Face ft_face = flt_font_ft->ft_face; | |
1175 | MFLTGlyph *g; | |
1176 | ||
1177 | for (g = gstring->glyphs + from; from < to; g++, from++) | |
1178 | if (! g->encoded) | |
1179 | { | |
1180 | FT_UInt code = FT_Get_Char_Index (ft_face, g->code); | |
1181 | ||
1182 | g->code = code > 0 ? code : FONT_INVALID_CODE; | |
1183 | g->encoded = 1; | |
1184 | } | |
1185 | return 0; | |
1186 | } | |
1187 | ||
1188 | static int | |
1189 | ftfont_get_metrics (font, gstring, from, to) | |
1190 | MFLTFont *font; | |
1191 | MFLTGlyphString *gstring; | |
1192 | int from, to; | |
1193 | { | |
1194 | struct MFLTFontFT *flt_font_ft = (struct MFLTFontFT *) font; | |
1195 | FT_Face ft_face = flt_font_ft->ft_face; | |
1196 | MFLTGlyph *g; | |
1197 | ||
1198 | for (g = gstring->glyphs + from; from < to; g++, from++) | |
1199 | if (! g->measured) | |
1200 | { | |
1201 | if (g->code != FONT_INVALID_CODE) | |
1202 | { | |
1203 | FT_Glyph_Metrics *m; | |
1204 | ||
1205 | if (FT_Load_Glyph (ft_face, g->code, FT_LOAD_DEFAULT) != 0) | |
1206 | abort (); | |
1207 | m = &ft_face->glyph->metrics; | |
1208 | ||
1209 | g->lbearing = m->horiBearingX; | |
1210 | g->rbearing = m->horiBearingX + m->width; | |
1211 | g->ascent = m->horiBearingY; | |
1212 | g->descent = m->height - m->horiBearingY; | |
1213 | g->xadv = m->horiAdvance; | |
1214 | } | |
1215 | else | |
1216 | { | |
1217 | g->lbearing = 0; | |
42984a74 | 1218 | g->rbearing = g->xadv = flt_font_ft->font->space_width << 6; |
de023c40 KH |
1219 | g->ascent = flt_font_ft->font->ascent << 6; |
1220 | g->descent = flt_font_ft->font->descent << 6; | |
1221 | } | |
1222 | g->yadv = 0; | |
1223 | g->measured = 1; | |
1224 | } | |
1225 | return 0; | |
1226 | } | |
1227 | ||
2a46904e | 1228 | static int |
de023c40 KH |
1229 | ftfont_check_otf (MFLTFont *font, MFLTOtfSpec *spec) |
1230 | { | |
1231 | struct MFLTFontFT *flt_font_ft = (struct MFLTFontFT *) font; | |
1232 | OTF *otf = flt_font_ft->otf; | |
1233 | OTF_Tag *tags; | |
1234 | int i, n, negative; | |
1235 | ||
1236 | for (i = 0; i < 2; i++) | |
1237 | { | |
1238 | if (! spec->features[i]) | |
1239 | continue; | |
1240 | for (n = 0; spec->features[i][n]; n++); | |
1241 | tags = alloca (sizeof (OTF_Tag) * n); | |
1242 | for (n = 0, negative = 0; spec->features[i][n]; n++) | |
1243 | { | |
1244 | if (spec->features[i][n] == 0xFFFFFFFF) | |
1245 | negative = 1; | |
1246 | else if (negative) | |
1247 | tags[n - 1] = spec->features[i][n] | 0x80000000; | |
1248 | else | |
1249 | tags[n] = spec->features[i][n]; | |
1250 | } | |
1251 | if (n - negative > 0 | |
1252 | && OTF_check_features (otf, i == 0, spec->script, spec->langsys, | |
1253 | tags, n - negative) != 1) | |
1254 | return 0; | |
1255 | } | |
1256 | return 1; | |
1257 | } | |
1258 | ||
1259 | #define DEVICE_DELTA(table, size) \ | |
1260 | (((size) >= (table).StartSize && (size) <= (table).EndSize) \ | |
1261 | ? (table).DeltaValue[(size) - (table).StartSize] << 6 \ | |
1262 | : 0) | |
1263 | ||
1264 | static void | |
1265 | adjust_anchor (FT_Face ft_face, OTF_Anchor *anchor, | |
1266 | unsigned code, int x_ppem, int y_ppem, int *x, int *y) | |
1267 | { | |
1268 | if (anchor->AnchorFormat == 2) | |
1269 | { | |
1270 | FT_Outline *outline; | |
1271 | int ap = anchor->f.f1.AnchorPoint; | |
1272 | ||
1273 | FT_Load_Glyph (ft_face, (FT_UInt) code, FT_LOAD_MONOCHROME); | |
1274 | outline = &ft_face->glyph->outline; | |
1275 | if (ap < outline->n_points) | |
1276 | { | |
1277 | *x = outline->points[ap].x << 6; | |
1278 | *y = outline->points[ap].y << 6; | |
1279 | } | |
1280 | } | |
1281 | else if (anchor->AnchorFormat == 3) | |
1282 | { | |
1283 | if (anchor->f.f2.XDeviceTable.offset) | |
1284 | *x += DEVICE_DELTA (anchor->f.f2.XDeviceTable, x_ppem); | |
1285 | if (anchor->f.f2.YDeviceTable.offset) | |
1286 | *y += DEVICE_DELTA (anchor->f.f2.YDeviceTable, y_ppem); | |
1287 | } | |
1288 | } | |
1289 | ||
1290 | static OTF_GlyphString otf_gstring; | |
1291 | ||
2a46904e | 1292 | static int |
de023c40 KH |
1293 | ftfont_drive_otf (font, spec, in, from, to, out, adjustment) |
1294 | MFLTFont *font; | |
1295 | MFLTOtfSpec *spec; | |
1296 | MFLTGlyphString *in; | |
1297 | int from, to; | |
1298 | MFLTGlyphString *out; | |
1299 | MFLTGlyphAdjustment *adjustment; | |
1300 | { | |
1301 | struct MFLTFontFT *flt_font_ft = (struct MFLTFontFT *) font; | |
1302 | FT_Face ft_face = flt_font_ft->ft_face; | |
1303 | OTF *otf = flt_font_ft->otf; | |
1304 | int len = to - from; | |
1305 | int i, j, gidx; | |
1306 | OTF_Glyph *otfg; | |
1307 | char script[5], *langsys = NULL; | |
1308 | char *gsub_features = NULL, *gpos_features = NULL; | |
1309 | ||
1310 | if (len == 0) | |
1311 | return from; | |
1312 | OTF_tag_name (spec->script, script); | |
1313 | if (spec->langsys) | |
1314 | { | |
1315 | langsys = alloca (5); | |
1316 | OTF_tag_name (spec->langsys, langsys); | |
1317 | } | |
1318 | for (i = 0; i < 2; i++) | |
1319 | { | |
1320 | char *p; | |
1321 | ||
1322 | if (spec->features[i] && spec->features[i][1] != 0xFFFFFFFF) | |
1323 | { | |
1324 | for (j = 0; spec->features[i][j]; j++); | |
1325 | if (i == 0) | |
1326 | p = gsub_features = alloca (6 * j); | |
1327 | else | |
1328 | p = gpos_features = alloca (6 * j); | |
1329 | for (j = 0; spec->features[i][j]; j++) | |
1330 | { | |
1331 | if (spec->features[i][j] == 0xFFFFFFFF) | |
1332 | *p++ = '*', *p++ = ','; | |
1333 | else | |
1334 | { | |
1335 | OTF_tag_name (spec->features[i][j], p); | |
1336 | p[4] = ','; | |
1337 | p += 5; | |
1338 | } | |
1339 | } | |
1340 | *--p = '\0'; | |
1341 | } | |
1342 | } | |
1343 | ||
1344 | if (otf_gstring.size == 0) | |
1345 | { | |
1346 | otf_gstring.glyphs = (OTF_Glyph *) malloc (sizeof (OTF_Glyph) * len); | |
1347 | otf_gstring.size = len; | |
1348 | } | |
1349 | else if (otf_gstring.size < len) | |
1350 | { | |
1351 | otf_gstring.glyphs = (OTF_Glyph *) realloc (otf_gstring.glyphs, | |
1352 | sizeof (OTF_Glyph) * len); | |
1353 | otf_gstring.size = len; | |
1354 | } | |
1355 | otf_gstring.used = len; | |
1356 | memset (otf_gstring.glyphs, 0, sizeof (OTF_Glyph) * len); | |
1357 | for (i = 0; i < len; i++) | |
1358 | { | |
1359 | otf_gstring.glyphs[i].c = in->glyphs[from + i].c; | |
1360 | otf_gstring.glyphs[i].glyph_id = in->glyphs[from + i].code; | |
1361 | } | |
1362 | ||
1363 | OTF_drive_gdef (otf, &otf_gstring); | |
1364 | gidx = out->used; | |
1365 | ||
1366 | if (gsub_features) | |
1367 | { | |
1368 | if (OTF_drive_gsub (otf, &otf_gstring, script, langsys, gsub_features) | |
1369 | < 0) | |
1370 | goto simple_copy; | |
1371 | if (out->allocated < out->used + otf_gstring.used) | |
1372 | return -2; | |
7d2fd545 | 1373 | for (i = 0, otfg = otf_gstring.glyphs; i < otf_gstring.used; ) |
de023c40 | 1374 | { |
7d2fd545 KH |
1375 | MFLTGlyph *g; |
1376 | int min_from, max_to; | |
de023c40 KH |
1377 | int j; |
1378 | ||
7d2fd545 | 1379 | g = out->glyphs + out->used; |
de023c40 | 1380 | *g = in->glyphs[from + otfg->f.index.from]; |
de023c40 KH |
1381 | if (g->code != otfg->glyph_id) |
1382 | { | |
7d2fd545 | 1383 | g->c = 0; |
de023c40 KH |
1384 | g->code = otfg->glyph_id; |
1385 | g->measured = 0; | |
1386 | } | |
1387 | out->used++; | |
7d2fd545 KH |
1388 | min_from = g->from; |
1389 | max_to = g->to; | |
1390 | if (otfg->f.index.from < otfg->f.index.to) | |
1391 | { | |
1392 | /* OTFG substitutes multiple glyphs in IN. */ | |
1393 | for (j = from + otfg->f.index.from + 1; | |
1394 | j <= from + otfg->f.index.to; j++) | |
1395 | { | |
1396 | if (min_from > in->glyphs[j].from) | |
1397 | min_from = in->glyphs[j].from; | |
1398 | if (max_to < in->glyphs[j].to) | |
1399 | max_to = in->glyphs[j].to; | |
1400 | } | |
1401 | g->from = min_from; | |
1402 | g->to = max_to; | |
1403 | } | |
1404 | for (i++, otfg++; (i < otf_gstring.used | |
1405 | && otfg->f.index.from == otfg[-1].f.index.from); | |
1406 | i++, otfg++) | |
1407 | { | |
1408 | g = out->glyphs + out->used; | |
1409 | *g = in->glyphs[from + otfg->f.index.to]; | |
1410 | if (g->code != otfg->glyph_id) | |
1411 | { | |
1412 | g->c = 0; | |
1413 | g->code = otfg->glyph_id; | |
1414 | g->measured = 0; | |
1415 | } | |
1416 | out->used++; | |
1417 | } | |
de023c40 KH |
1418 | } |
1419 | } | |
1420 | else | |
1421 | { | |
1422 | if (out->allocated < out->used + len) | |
1423 | return -2; | |
1424 | for (i = 0; i < len; i++) | |
1425 | out->glyphs[out->used++] = in->glyphs[from + i]; | |
1426 | } | |
1427 | ||
1428 | if (gpos_features) | |
1429 | { | |
1430 | MFLTGlyph *base = NULL, *mark = NULL, *g; | |
1431 | int x_ppem, y_ppem, x_scale, y_scale; | |
1432 | ||
1433 | if (OTF_drive_gpos (otf, &otf_gstring, script, langsys, gpos_features) | |
1434 | < 0) | |
1435 | return to; | |
1436 | ||
1437 | x_ppem = ft_face->size->metrics.x_ppem; | |
1438 | y_ppem = ft_face->size->metrics.y_ppem; | |
1439 | x_scale = ft_face->size->metrics.x_scale; | |
1440 | y_scale = ft_face->size->metrics.y_scale; | |
1441 | ||
1442 | for (i = 0, otfg = otf_gstring.glyphs, g = out->glyphs + gidx; | |
1443 | i < otf_gstring.used; i++, otfg++, g++) | |
1444 | { | |
1445 | MFLTGlyph *prev; | |
1446 | ||
1447 | if (! otfg->glyph_id) | |
1448 | continue; | |
1449 | switch (otfg->positioning_type) | |
1450 | { | |
1451 | case 0: | |
1452 | break; | |
1453 | case 1: /* Single */ | |
1454 | case 2: /* Pair */ | |
1455 | { | |
1456 | int format = otfg->f.f1.format; | |
1457 | ||
1458 | if (format & OTF_XPlacement) | |
1459 | adjustment[i].xoff | |
1460 | = otfg->f.f1.value->XPlacement * x_scale / 0x10000; | |
1461 | if (format & OTF_XPlaDevice) | |
1462 | adjustment[i].xoff | |
1463 | += DEVICE_DELTA (otfg->f.f1.value->XPlaDevice, x_ppem); | |
1464 | if (format & OTF_YPlacement) | |
1465 | adjustment[i].yoff | |
1466 | = - (otfg->f.f1.value->YPlacement * y_scale / 0x10000); | |
1467 | if (format & OTF_YPlaDevice) | |
1468 | adjustment[i].yoff | |
1469 | -= DEVICE_DELTA (otfg->f.f1.value->YPlaDevice, y_ppem); | |
1470 | if (format & OTF_XAdvance) | |
1471 | adjustment[i].xadv | |
1472 | += otfg->f.f1.value->XAdvance * x_scale / 0x10000; | |
1473 | if (format & OTF_XAdvDevice) | |
1474 | adjustment[i].xadv | |
1475 | += DEVICE_DELTA (otfg->f.f1.value->XAdvDevice, x_ppem); | |
1476 | if (format & OTF_YAdvance) | |
1477 | adjustment[i].yadv | |
1478 | += otfg->f.f1.value->YAdvance * y_scale / 0x10000; | |
1479 | if (format & OTF_YAdvDevice) | |
1480 | adjustment[i].yadv | |
1481 | += DEVICE_DELTA (otfg->f.f1.value->YAdvDevice, y_ppem); | |
1482 | adjustment[i].set = 1; | |
1483 | } | |
1484 | break; | |
1485 | case 3: /* Cursive */ | |
1486 | /* Not yet supported. */ | |
1487 | break; | |
1488 | case 4: /* Mark-to-Base */ | |
1489 | case 5: /* Mark-to-Ligature */ | |
1490 | if (! base) | |
1491 | break; | |
1492 | prev = base; | |
1493 | goto label_adjust_anchor; | |
1494 | default: /* i.e. case 6 Mark-to-Mark */ | |
1495 | if (! mark) | |
1496 | break; | |
1497 | prev = mark; | |
1498 | ||
1499 | label_adjust_anchor: | |
1500 | { | |
1501 | int base_x, base_y, mark_x, mark_y; | |
1502 | int this_from, this_to; | |
1503 | ||
1504 | base_x = otfg->f.f4.base_anchor->XCoordinate * x_scale / 0x10000; | |
1505 | base_y = otfg->f.f4.base_anchor->YCoordinate * y_scale / 0x10000; | |
1506 | mark_x = otfg->f.f4.mark_anchor->XCoordinate * x_scale / 0x10000; | |
1507 | mark_y = otfg->f.f4.mark_anchor->YCoordinate * y_scale / 0x10000;; | |
1508 | ||
1509 | if (otfg->f.f4.base_anchor->AnchorFormat != 1) | |
1510 | adjust_anchor (ft_face, otfg->f.f4.base_anchor, | |
1511 | prev->code, x_ppem, y_ppem, &base_x, &base_y); | |
1512 | if (otfg->f.f4.mark_anchor->AnchorFormat != 1) | |
1513 | adjust_anchor (ft_face, otfg->f.f4.mark_anchor, g->code, | |
1514 | x_ppem, y_ppem, &mark_x, &mark_y); | |
1515 | adjustment[i].xoff = (base_x - mark_x); | |
1516 | adjustment[i].yoff = - (base_y - mark_y); | |
1517 | adjustment[i].back = (g - prev); | |
1518 | adjustment[i].xadv = 0; | |
1519 | adjustment[i].advance_is_absolute = 1; | |
1520 | adjustment[i].set = 1; | |
1521 | this_from = g->from; | |
1522 | this_to = g->to; | |
1523 | for (j = 0; prev + j < g; j++) | |
1524 | { | |
1525 | if (this_from > prev[j].from) | |
1526 | this_from = prev[j].from; | |
1527 | if (this_to < prev[j].to) | |
1528 | this_to = prev[j].to; | |
1529 | } | |
1530 | for (; prev <= g; prev++) | |
1531 | { | |
1532 | prev->from = this_from; | |
1533 | prev->to = this_to; | |
1534 | } | |
1535 | } | |
1536 | } | |
1537 | if (otfg->GlyphClass == OTF_GlyphClass0) | |
1538 | base = mark = g; | |
1539 | else if (otfg->GlyphClass == OTF_GlyphClassMark) | |
1540 | mark = g; | |
1541 | else | |
1542 | base = g; | |
1543 | } | |
1544 | } | |
1545 | return to; | |
1546 | ||
1547 | simple_copy: | |
1548 | if (out->allocated < out->used + len) | |
1549 | return -2; | |
1550 | font->get_metrics (font, in, from, to); | |
1551 | memcpy (out->glyphs + out->used, in->glyphs + from, | |
1552 | sizeof (MFLTGlyph) * len); | |
1553 | out->used += len; | |
1554 | return to; | |
1555 | } | |
1556 | ||
1557 | static MFLTGlyphString gstring; | |
1558 | ||
1559 | static int m17n_flt_initialized; | |
1560 | ||
1561 | extern Lisp_Object QCfamily; | |
1562 | ||
1563 | Lisp_Object | |
1564 | ftfont_shape_by_flt (lgstring, font, ft_face, otf) | |
1565 | Lisp_Object lgstring; | |
1566 | struct font *font; | |
1567 | FT_Face ft_face; | |
1568 | OTF *otf; | |
1569 | { | |
1570 | EMACS_UINT len = LGSTRING_LENGTH (lgstring); | |
1571 | EMACS_UINT i; | |
1572 | struct MFLTFontFT flt_font_ft; | |
1573 | ||
1574 | if (! m17n_flt_initialized) | |
1575 | { | |
1576 | M17N_INIT (); | |
1577 | m17n_flt_initialized = 1; | |
1578 | } | |
1579 | ||
1580 | for (i = 0; i < len; i++) | |
1581 | if (NILP (LGSTRING_GLYPH (lgstring, i))) | |
1582 | break; | |
1583 | len = i; | |
1584 | ||
1585 | if (gstring.allocated == 0) | |
1586 | { | |
1587 | gstring.allocated = len * 2; | |
1588 | gstring.glyph_size = sizeof (MFLTGlyph); | |
1589 | gstring.glyphs = malloc (sizeof (MFLTGlyph) * gstring.allocated); | |
1590 | } | |
1591 | else if (gstring.allocated < len * 2) | |
1592 | { | |
1593 | gstring.allocated = len * 2; | |
1594 | gstring.glyphs = realloc (gstring.glyphs, | |
1595 | sizeof (MFLTGlyph) * gstring.allocated); | |
1596 | } | |
1597 | for (i = 0; i < len; i++) | |
1598 | gstring.glyphs[i].c = LGLYPH_CHAR (LGSTRING_GLYPH (lgstring, i)); | |
1599 | gstring.used = len; | |
1600 | gstring.r2l = 0; | |
1601 | ||
1602 | { | |
1603 | Lisp_Object family = Ffont_get (LGSTRING_FONT (lgstring), QCfamily); | |
1604 | ||
1605 | if (NILP (family)) | |
1606 | flt_font_ft.flt_font.family = Mnil; | |
1607 | else | |
21988a08 KH |
1608 | flt_font_ft.flt_font.family |
1609 | = msymbol ((char *) SDATA (SYMBOL_NAME (family))); | |
de023c40 KH |
1610 | } |
1611 | flt_font_ft.flt_font.x_ppem = ft_face->size->metrics.x_ppem; | |
1612 | flt_font_ft.flt_font.y_ppem = ft_face->size->metrics.y_ppem; | |
1613 | flt_font_ft.flt_font.get_glyph_id = ftfont_get_glyph_id; | |
1614 | flt_font_ft.flt_font.get_metrics = ftfont_get_metrics; | |
1615 | flt_font_ft.flt_font.check_otf = ftfont_check_otf; | |
1616 | flt_font_ft.flt_font.drive_otf = ftfont_drive_otf; | |
1617 | flt_font_ft.flt_font.internal = NULL; | |
1618 | flt_font_ft.font = font; | |
1619 | flt_font_ft.ft_face = ft_face; | |
1620 | flt_font_ft.otf = otf; | |
1621 | for (i = 0; i < 3; i++) | |
1622 | { | |
1623 | int result = mflt_run (&gstring, 0, len, &flt_font_ft.flt_font, NULL); | |
1624 | if (result != -2) | |
1625 | break; | |
1626 | gstring.allocated += gstring.allocated; | |
1627 | gstring.glyphs = realloc (gstring.glyphs, | |
1628 | sizeof (MFLTGlyph) * gstring.allocated); | |
1629 | } | |
1630 | if (gstring.used > LGSTRING_LENGTH (lgstring)) | |
1631 | return Qnil; | |
1632 | for (i = 0; i < gstring.used; i++) | |
1633 | { | |
1634 | MFLTGlyph *g = gstring.glyphs + i; | |
1635 | ||
1636 | g->from = LGLYPH_FROM (LGSTRING_GLYPH (lgstring, g->from)); | |
1637 | g->to = LGLYPH_TO (LGSTRING_GLYPH (lgstring, g->to)); | |
1638 | } | |
1639 | ||
1640 | for (i = 0; i < gstring.used; i++) | |
1641 | { | |
1642 | Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i); | |
1643 | MFLTGlyph *g = gstring.glyphs + i; | |
1644 | ||
4cec6061 KH |
1645 | if (NILP (lglyph)) |
1646 | { | |
1647 | lglyph = Fmake_vector (make_number (LGLYPH_SIZE), Qnil); | |
1648 | LGSTRING_SET_GLYPH (lgstring, i, lglyph); | |
1649 | } | |
de023c40 KH |
1650 | LGLYPH_SET_FROM (lglyph, g->from); |
1651 | LGLYPH_SET_TO (lglyph, g->to); | |
1652 | LGLYPH_SET_CHAR (lglyph, g->c); | |
1653 | LGLYPH_SET_CODE (lglyph, g->code); | |
1654 | LGLYPH_SET_WIDTH (lglyph, g->xadv >> 6); | |
1655 | LGLYPH_SET_LBEARING (lglyph, g->lbearing >> 6); | |
1656 | LGLYPH_SET_RBEARING (lglyph, g->rbearing >> 6); | |
1657 | LGLYPH_SET_ASCENT (lglyph, g->ascent >> 6); | |
1658 | LGLYPH_SET_DESCENT (lglyph, g->descent >> 6); | |
1659 | if (g->adjusted) | |
1660 | { | |
1661 | Lisp_Object vec; | |
1662 | ||
1663 | vec = Fmake_vector (make_number (3), Qnil); | |
1664 | ASET (vec, 0, make_number (g->xoff >> 6)); | |
1665 | ASET (vec, 1, make_number (g->yoff >> 6)); | |
1666 | ASET (vec, 2, make_number (g->xadv >> 6)); | |
1667 | LGLYPH_SET_ADJUSTMENT (lglyph, vec); | |
1668 | } | |
1669 | } | |
1670 | return make_number (i); | |
1671 | } | |
1672 | ||
1673 | Lisp_Object | |
1674 | ftfont_shape (lgstring) | |
1675 | Lisp_Object lgstring; | |
1676 | { | |
1677 | struct font *font; | |
1678 | struct ftfont_info *ftfont_info; | |
1679 | ||
1680 | CHECK_FONT_GET_OBJECT (LGSTRING_FONT (lgstring), font); | |
1681 | ftfont_info = (struct ftfont_info *) font; | |
1682 | if (! ftfont_info->maybe_otf) | |
4cc1c806 | 1683 | return make_number (0); |
de023c40 KH |
1684 | if (! ftfont_info->otf) |
1685 | { | |
1686 | OTF *otf = OTF_open_ft_face (ftfont_info->ft_size->face); | |
1687 | ||
1688 | if (! otf || OTF_get_table (otf, "head") < 0) | |
1689 | { | |
1690 | if (otf) | |
1691 | OTF_close (otf); | |
1692 | ftfont_info->maybe_otf = 0; | |
4cc1c806 | 1693 | return make_number (0); |
de023c40 KH |
1694 | } |
1695 | ||
1696 | ftfont_info->otf = otf; | |
1697 | } | |
1698 | ||
1699 | return ftfont_shape_by_flt (lgstring, font, ftfont_info->ft_size->face, | |
1700 | ftfont_info->otf); | |
1701 | } | |
1702 | ||
1703 | #endif /* HAVE_M17N_FLT */ | |
1704 | #endif /* HAVE_LIBOTF */ | |
1705 | ||
0b966021 KH |
1706 | Lisp_Object |
1707 | ftfont_font_format (FcPattern *pattern) | |
1708 | { | |
e907d979 | 1709 | FcChar8 *str; |
719b3d63 | 1710 | int len; |
0b966021 | 1711 | |
e907d979 KH |
1712 | #ifdef FC_FONTFORMAT |
1713 | if (FcPatternGetString (pattern, FC_FONTFORMAT, 0, &str) != FcResultMatch) | |
0b966021 | 1714 | return Qnil; |
e907d979 | 1715 | if (strcmp ((char *) str, "TrueType") == 0) |
0b966021 | 1716 | return intern ("truetype"); |
48875afe | 1717 | if (strcmp ((char *) str, "Type 1") == 0) |
0b966021 | 1718 | return intern ("type1"); |
2a46904e | 1719 | if (strcmp ((char *) str, "PCF") == 0) |
0b966021 | 1720 | return intern ("pcf"); |
2a46904e | 1721 | if (strcmp ((char *) str, "BDF") == 0) |
0b966021 | 1722 | return intern ("bdf"); |
e907d979 | 1723 | #else /* not FC_FONTFORMAT */ |
09a56ce4 | 1724 | if (FcPatternGetString (pattern, FC_FILE, 0, &str) != FcResultMatch) |
e907d979 | 1725 | return Qnil; |
719b3d63 KH |
1726 | len = strlen ((char *) str); |
1727 | if (len >= 4) | |
1728 | { | |
1729 | str += len - 4; | |
1730 | if (xstrcasecmp ((char *) str, ".ttf") == 0) | |
1731 | return intern ("truetype"); | |
1732 | if (xstrcasecmp ((char *) str, "pfb") == 0) | |
1733 | return intern ("type1"); | |
2a46904e | 1734 | if (xstrcasecmp ((char *) str, "pcf") == 0) |
719b3d63 | 1735 | return intern ("pcf"); |
2a46904e | 1736 | if (xstrcasecmp ((char *) str, "bdf") == 0) |
719b3d63 KH |
1737 | return intern ("bdf"); |
1738 | } | |
e907d979 | 1739 | #endif /* not FC_FONTFORMAT */ |
0b966021 KH |
1740 | return intern ("unknown"); |
1741 | } | |
1742 | ||
c2f5bfd6 KH |
1743 | \f |
1744 | void | |
1745 | syms_of_ftfont () | |
1746 | { | |
706b6995 KH |
1747 | DEFSYM (Qfreetype, "freetype"); |
1748 | DEFSYM (Qmonospace, "monospace"); | |
1749 | DEFSYM (Qsans_serif, "sans-serif"); | |
1750 | DEFSYM (Qserif, "serif"); | |
1751 | DEFSYM (Qmono, "mono"); | |
1752 | DEFSYM (Qsans, "sans"); | |
1753 | DEFSYM (Qsans__serif, "sans serif"); | |
1754 | ||
c2f5bfd6 | 1755 | staticpro (&freetype_font_cache); |
c2801c99 | 1756 | freetype_font_cache = Fcons (Qt, Qnil); |
c2f5bfd6 | 1757 | |
706b6995 KH |
1758 | staticpro (&ftfont_generic_family_list); |
1759 | ftfont_generic_family_list | |
1760 | = Fcons (Fcons (Qmonospace, Qt), | |
1761 | Fcons (Fcons (Qsans_serif, Qt), | |
1762 | Fcons (Fcons (Qsans, Qt), Qnil))); | |
c2f5bfd6 | 1763 | |
21988a08 KH |
1764 | staticpro (&ft_face_cache); |
1765 | ft_face_cache = Qnil; | |
1766 | ||
c2f5bfd6 KH |
1767 | ftfont_driver.type = Qfreetype; |
1768 | register_font_driver (&ftfont_driver, NULL); | |
1769 | } | |
885b7d09 MB |
1770 | |
1771 | /* arch-tag: 7cfa432c-33a6-4988-83d2-a82ed8604aca | |
1772 | (do not change this comment) */ |