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