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