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