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