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