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