(calendar-cursor-to-nearest-date): Use calendar-column-to-segment,
[emacs.git] / src / ftfont.c
blob92bbec707a20d13c9fdb91026a892de0b6ce9fe1
1 /* ftfont.c -- FreeType font driver.
2 Copyright (C) 2006, 2007, 2008 Free Software Foundation, Inc.
3 Copyright (C) 2006, 2007, 2008
4 National Institute of Advanced Industrial Science and Technology (AIST)
5 Registration Number H13PRO009
7 This file is part of GNU Emacs.
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.
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.
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/>. */
22 #include <config.h>
23 #include <stdio.h>
25 #include <fontconfig/fontconfig.h>
26 #include <fontconfig/fcfreetype.h>
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 "fontset.h"
36 #include "font.h"
37 #include "ftfont.h"
39 /* Symbolic type of this font-driver. */
40 Lisp_Object Qfreetype;
42 /* Fontconfig's generic families and their aliases. */
43 static Lisp_Object Qmonospace, Qsans_serif, Qserif, Qmono, Qsans, Qsans__serif;
45 /* Flag to tell if FcInit is already called or not. */
46 static int fc_initialized;
48 /* Handle to a FreeType library instance. */
49 static FT_Library ft_library;
51 /* Cache for FreeType fonts. */
52 static Lisp_Object freetype_font_cache;
54 /* Cache for FT_Face */
55 static Lisp_Object ft_face_cache;
57 /* The actual structure for FreeType font that can be casted to struct
58 font. */
60 struct ftfont_info
62 struct font font;
63 FT_Size ft_size;
64 int fc_charset_idx;
65 #ifdef HAVE_LIBOTF
66 int maybe_otf; /* Flag to tell if this may be OTF or not. */
67 OTF *otf;
68 #endif /* HAVE_LIBOTF */
71 static Lisp_Object ftfont_pattern_entity P_ ((FcPattern *, Lisp_Object,
72 Lisp_Object, int));
74 static Lisp_Object ftfont_resolve_generic_family P_ ((Lisp_Object));
75 Lisp_Object ftfont_font_format P_ ((FcPattern *));
77 #define SYMBOL_FcChar8(SYM) (FcChar8 *) SDATA (SYMBOL_NAME (SYM))
79 static struct
81 /* charset name */
82 char *name;
83 /* characters to distinguish the charset from the others */
84 int uniquifier[6];
85 /* set in syms_of_ftfont */
86 int charset_id;
87 /* set on demand */
88 FcCharSet *fc_charset;
89 } fc_charset_table[] =
90 { { "iso-8859-1", { 0x00A0, 0x00A1, 0x00B4, 0x00BC, 0x00D0 }, -1 },
91 { "iso-8859-2", { 0x00A0, 0x010E }},
92 { "iso-8859-3", { 0x00A0, 0x0108 }},
93 { "iso-8859-4", { 0x00A0, 0x00AF, 0x0128, 0x0156, 0x02C7 }},
94 { "iso-8859-5", { 0x00A0, 0x0401 }},
95 { "iso-8859-6", { 0x00A0, 0x060C }},
96 { "iso-8859-7", { 0x00A0, 0x0384 }},
97 { "iso-8859-8", { 0x00A0, 0x05D0 }},
98 { "iso-8859-9", { 0x00A0, 0x00A1, 0x00BC, 0x011E }},
99 { "iso-8859-10", { 0x00A0, 0x00D0, 0x0128, 0x2015 }},
100 { "iso-8859-11", { 0x00A0, 0x0E01 }},
101 { "iso-8859-13", { 0x00A0, 0x201C }},
102 { "iso-8859-14", { 0x00A0, 0x0174 }},
103 { "iso-8859-15", { 0x00A0, 0x00A1, 0x00D0, 0x0152 }},
104 { "iso-8859-16", { 0x00A0, 0x0218}},
105 { "chinese-gb2312", { 0x4E13 }},
106 { "big5", { 0xF6B1 }},
107 { "japanese-jisx0208", { 0x4E55 }},
108 { "korean-ksc5601", { 0xAC00 }},
109 { "chinese-cns11643-1", { 0xFE32 }},
110 { "chinese-cns11643-2", { 0x4E33, 0x7934 }},
111 { "chinese-cns11643-3", { 0x201A9 }},
112 { "chinese-cns11643-4", { 0x20057 }},
113 { "chinese-cns11643-5", { 0x20000 }},
114 { "chinese-cns11643-6", { 0x20003 }},
115 { "chinese-cns11643-7", { 0x20055 }},
116 { "chinese-gbk", { 0x4E06 }},
117 { "japanese-jisx0212", { 0x4E44 }},
118 { "japanese-jisx0213-1", { 0xFA10 }},
119 { "japanese-jisx0213-2", { 0xFA49 }},
120 { "japanese-jisx0213.2004-1", { 0x20B9F }},
121 { "viscii", { 0x1EA0, 0x1EAE, 0x1ED2 }},
122 { "tis620", { 0x0E01 }},
123 { "windows-1251", { 0x0401, 0x0490 }},
124 { "koi8-r", { 0x0401, 0x2219 }},
125 { "mule-lao", { 0x0E81 }},
126 { NULL }
129 extern Lisp_Object Qc, Qm, Qp, Qd;
131 static Lisp_Object
132 ftfont_pattern_entity (p, registry, extra, fc_charset_idx)
133 FcPattern *p;
134 Lisp_Object registry, extra;
135 int fc_charset_idx;
137 Lisp_Object entity;
138 char *file, *str;
139 int numeric;
140 double dbl;
141 FcBool b;
143 if (FcPatternGetString (p, FC_FILE, 0, (FcChar8 **) &file) != FcResultMatch)
144 return Qnil;
146 entity = font_make_entity ();
148 ASET (entity, FONT_TYPE_INDEX, Qfreetype);
149 ASET (entity, FONT_REGISTRY_INDEX, registry);
151 if (FcPatternGetString (p, FC_FOUNDRY, 0, (FcChar8 **) &str) == FcResultMatch)
152 ASET (entity, FONT_FOUNDRY_INDEX, font_intern_prop (str, strlen (str), 1));
153 if (FcPatternGetString (p, FC_FAMILY, 0, (FcChar8 **) &str) == FcResultMatch)
154 ASET (entity, FONT_FAMILY_INDEX, font_intern_prop (str, strlen (str), 1));
155 if (FcPatternGetInteger (p, FC_WEIGHT, 0, &numeric) == FcResultMatch)
157 if (numeric >= FC_WEIGHT_REGULAR && numeric < FC_WEIGHT_MEDIUM)
158 numeric = FC_WEIGHT_MEDIUM;
159 FONT_SET_STYLE (entity, FONT_WEIGHT_INDEX, make_number (numeric));
161 if (FcPatternGetInteger (p, FC_SLANT, 0, &numeric) == FcResultMatch)
163 numeric += 100;
164 FONT_SET_STYLE (entity, FONT_SLANT_INDEX, make_number (numeric));
166 if (FcPatternGetInteger (p, FC_WIDTH, 0, &numeric) == FcResultMatch)
168 FONT_SET_STYLE (entity, FONT_WIDTH_INDEX, make_number (numeric));
170 if (FcPatternGetDouble (p, FC_PIXEL_SIZE, 0, &dbl) == FcResultMatch)
171 ASET (entity, FONT_SIZE_INDEX, make_number (dbl));
172 else
173 ASET (entity, FONT_SIZE_INDEX, make_number (0));
174 if (FcPatternGetInteger (p, FC_SPACING, 0, &numeric) == FcResultMatch)
175 ASET (entity, FONT_SPACING_INDEX, make_number (numeric));
176 if (FcPatternGetDouble (p, FC_DPI, 0, &dbl) == FcResultMatch)
178 int dpi = dbl;
179 ASET (entity, FONT_DPI_INDEX, make_number (dpi));
181 if (FcPatternGetBool (p, FC_SCALABLE, 0, &b) == FcResultMatch
182 && b == FcTrue)
183 ASET (entity, FONT_AVGWIDTH_INDEX, make_number (0));
185 ASET (entity, FONT_EXTRA_INDEX, Fcopy_sequence (extra));
186 font_put_extra (entity, QCfont_entity,
187 Fcons (make_unibyte_string ((char *) file,
188 strlen ((char *) file)),
189 make_number (fc_charset_idx)));
190 return entity;
194 static Lisp_Object ftfont_generic_family_list;
196 static Lisp_Object
197 ftfont_resolve_generic_family (family)
198 Lisp_Object family;
200 Lisp_Object slot;
201 FcPattern *pattern = NULL, *match;
202 FcResult result;
204 family = Fintern (Fdowncase (SYMBOL_NAME (family)), Qnil);
205 if (EQ (family, Qmono))
206 family = Qmonospace;
207 else if (EQ (family, Qsans) || EQ (family, Qsans__serif))
208 family = Qsans_serif;
209 slot = assq_no_quit (family, ftfont_generic_family_list);
210 if (! CONSP (slot))
211 return Qnil;
212 if (! EQ (XCDR (slot), Qt))
213 return XCDR (slot);
214 pattern = FcPatternBuild (NULL, FC_FAMILY, FcTypeString,
215 SYMBOL_FcChar8 (family), (char *) 0);
216 if (! pattern)
217 goto err;
218 FcConfigSubstitute (NULL, pattern, FcMatchPattern);
219 FcDefaultSubstitute (pattern);
220 match = FcFontMatch (NULL, pattern, &result);
221 if (match)
223 FcChar8 *fam;
225 if (FcPatternGetString (match, FC_FAMILY, 0, &fam) == FcResultMatch)
226 family = intern ((char *) fam);
228 else
229 family = Qnil;
230 XSETCDR (slot, family);
231 err:
232 if (match) FcPatternDestroy (match);
233 if (pattern) FcPatternDestroy (pattern);
234 return family;
237 Lisp_Object
238 ftfont_lookup_cache (filename)
239 Lisp_Object filename;
241 Lisp_Object cache, val;
243 cache = assoc_no_quit (filename, ft_face_cache);
244 if (NILP (cache))
246 val = make_save_value (NULL, 0);
247 cache = Fcons (filename, val);
248 ft_face_cache = Fcons (cache, ft_face_cache);
250 else
251 val = XCDR (cache);
252 if (! XSAVE_VALUE (val)->pointer)
254 FT_Face ft_face;
256 if (! ft_library
257 && FT_Init_FreeType (&ft_library) != 0)
258 return Qnil;
259 if (FT_New_Face (ft_library, (char *) SDATA (filename), 0, &ft_face) != 0)
260 return Qnil;
261 XSAVE_VALUE (val)->pointer = ft_face;
263 return cache;
266 static Lisp_Object ftfont_get_cache P_ ((FRAME_PTR));
267 static Lisp_Object ftfont_list P_ ((Lisp_Object, Lisp_Object));
268 static Lisp_Object ftfont_match P_ ((Lisp_Object, Lisp_Object));
269 static Lisp_Object ftfont_list_family P_ ((Lisp_Object));
270 static Lisp_Object ftfont_open P_ ((FRAME_PTR, Lisp_Object, int));
271 static void ftfont_close P_ ((FRAME_PTR, struct font *));
272 static int ftfont_has_char P_ ((Lisp_Object, int));
273 static unsigned ftfont_encode_char P_ ((struct font *, int));
274 static int ftfont_text_extents P_ ((struct font *, unsigned *, int,
275 struct font_metrics *));
276 static int ftfont_get_bitmap P_ ((struct font *, unsigned,
277 struct font_bitmap *, int));
278 static int ftfont_anchor_point P_ ((struct font *, unsigned, int,
279 int *, int *));
280 static Lisp_Object ftfont_shape P_ ((Lisp_Object));
282 struct font_driver ftfont_driver =
284 0, /* Qfreetype */
285 0, /* case insensitive */
286 ftfont_get_cache,
287 ftfont_list,
288 ftfont_match,
289 ftfont_list_family,
290 NULL,
291 ftfont_open,
292 ftfont_close,
293 /* We can't draw a text without device dependent functions. */
294 NULL,
295 NULL,
296 ftfont_has_char,
297 ftfont_encode_char,
298 ftfont_text_extents,
299 /* We can't draw a text without device dependent functions. */
300 NULL,
301 ftfont_get_bitmap,
302 NULL,
303 NULL,
304 NULL,
305 ftfont_anchor_point,
306 NULL,
307 NULL,
308 NULL,
309 NULL,
310 #if defined (HAVE_M17N_FLT) && defined (HAVE_LIBOTF)
311 ftfont_shape
312 #else /* not (HAVE_M17N_FLT && HAVE_LIBOTF) */
313 NULL
314 #endif /* not (HAVE_M17N_FLT && HAVE_LIBOTF) */
317 extern Lisp_Object QCname;
319 static Lisp_Object
320 ftfont_get_cache (f)
321 FRAME_PTR f;
323 return freetype_font_cache;
326 static int
327 ftfont_get_charset (registry)
328 Lisp_Object registry;
330 struct charset *encoding;
331 int i, j;
333 if (font_registry_charsets (registry, &encoding, NULL) < 0)
334 return -1;
335 if (fc_charset_table[0].charset_id < 0)
336 /* Setup charset_id field of all elements. */
337 for (i = 0; fc_charset_table[i].name; i++)
339 Lisp_Object sym = intern (fc_charset_table[i].name);
341 if (CHARSETP (sym))
342 fc_charset_table[i].charset_id = XINT (CHARSET_SYMBOL_ID (sym));
343 else
344 fc_charset_table[i].charset_id = -1;
347 for (i = 0; fc_charset_table[i].name; i++)
348 if (encoding->id == fc_charset_table[i].charset_id)
349 break;
350 if (! fc_charset_table[i].name)
351 return -1;
352 if (! fc_charset_table[i].fc_charset)
354 FcCharSet *charset = FcCharSetCreate ();
355 int *uniquifier = fc_charset_table[i].uniquifier;
357 if (! charset)
358 return -1;
359 for (j = 0; uniquifier[j]; j++)
360 if (! FcCharSetAddChar (charset, uniquifier[j]))
362 FcCharSetDestroy (charset);
363 return -1;
365 fc_charset_table[i].fc_charset = charset;
367 return i;
370 struct OpenTypeSpec
372 Lisp_Object script;
373 unsigned int script_tag, langsys_tag;
374 int nfeatures[2];
375 unsigned int *features[2];
378 #define OTF_SYM_TAG(SYM, TAG) \
379 do { \
380 unsigned char *p = SDATA (SYMBOL_NAME (SYM)); \
381 TAG = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]; \
382 } while (0)
384 #define OTF_TAG_STR(TAG, P) \
385 do { \
386 (P)[0] = (char) (TAG >> 24); \
387 (P)[1] = (char) ((TAG >> 16) & 0xFF); \
388 (P)[2] = (char) ((TAG >> 8) & 0xFF); \
389 (P)[3] = (char) (TAG & 0xFF); \
390 (P)[4] = '\0'; \
391 } while (0)
393 static struct OpenTypeSpec *
394 ftfont_get_open_type_spec (Lisp_Object otf_spec)
396 struct OpenTypeSpec *spec = malloc (sizeof (struct OpenTypeSpec));
397 Lisp_Object val;
398 int i, j, negative;
400 if (! spec)
401 return NULL;
402 spec->script = XCAR (otf_spec);
403 if (! NILP (spec->script))
405 OTF_SYM_TAG (spec->script, spec->script_tag);
406 val = assq_no_quit (spec->script, Votf_script_alist);
407 if (CONSP (val) && SYMBOLP (XCDR (val)))
408 spec->script = XCDR (val);
409 else
410 spec->script = Qnil;
412 else
413 spec->script_tag = 0x44464C54; /* "DFLT" */
414 otf_spec = XCDR (otf_spec);
415 val = XCAR (otf_spec);
416 if (! NILP (val))
417 OTF_SYM_TAG (val, spec->langsys_tag);
418 else
419 spec->langsys_tag = 0;
420 spec->nfeatures[0] = spec->nfeatures[1] = 0;
421 for (i = 0; i < 2; i++)
423 Lisp_Object len;
425 otf_spec = XCDR (otf_spec);
426 if (NILP (otf_spec))
427 break;
428 val = XCAR (otf_spec);
429 if (NILP (val))
430 continue;
431 len = Flength (val);
432 spec->features[i] = malloc (sizeof (int) * XINT (len));
433 if (! spec->features[i])
435 if (i > 0 && spec->features[0])
436 free (spec->features[0]);
437 free (spec);
438 return NULL;
440 for (j = 0, negative = 0; CONSP (val); val = XCDR (val))
442 if (NILP (XCAR (val)))
443 negative = 1;
444 else
446 unsigned int tag;
448 OTF_SYM_TAG (XCAR (val), tag);
449 spec->features[i][j++] = negative ? tag & 0x80000000 : tag;
452 spec->nfeatures[i] = j;
454 return spec;
457 static FcPattern *ftfont_spec_pattern P_ ((Lisp_Object, int *, char *,
458 struct OpenTypeSpec **));
460 static FcPattern *
461 ftfont_spec_pattern (spec, fc_charset_idx, otlayout, otspec)
462 Lisp_Object spec;
463 int *fc_charset_idx;
464 char *otlayout;
465 struct OpenTypeSpec **otspec;
467 Lisp_Object tmp, extra;
468 FcPattern *pattern = NULL;
469 FcCharSet *charset = NULL;
470 FcLangSet *langset = NULL;
471 int n;
472 int dpi = -1;
473 int scalable = -1;
474 Lisp_Object script = Qnil;
475 Lisp_Object registry;
477 if (! NILP (AREF (spec, FONT_ADSTYLE_INDEX))
478 && SBYTES (SYMBOL_NAME (AREF (spec, FONT_ADSTYLE_INDEX))) > 0)
479 /* Fontconfig doesn't support adstyle property. */
480 return NULL;
481 if ((n = FONT_SLANT_NUMERIC (spec)) >= 0
482 && n < 100)
483 /* Fontconfig doesn't support reverse-italic/obligue. */
484 return NULL;
486 if (INTEGERP (AREF (spec, FONT_DPI_INDEX)))
487 dpi = XINT (AREF (spec, FONT_DPI_INDEX));
488 if (INTEGERP (AREF (spec, FONT_AVGWIDTH_INDEX))
489 && XINT (AREF (spec, FONT_AVGWIDTH_INDEX)) == 0)
490 scalable = 1;
492 registry = AREF (spec, FONT_REGISTRY_INDEX);
493 if (NILP (registry)
494 || EQ (registry, Qascii_0)
495 || EQ (registry, Qiso10646_1)
496 || EQ (registry, Qunicode_bmp)
497 || EQ (registry, Qunicode_sip))
498 *fc_charset_idx = -1;
499 else
501 *fc_charset_idx = ftfont_get_charset (registry);
502 if (*fc_charset_idx < 0)
503 return NULL;
504 charset = fc_charset_table[*fc_charset_idx].fc_charset;
507 otlayout[0] = '\0';
508 for (extra = AREF (spec, FONT_EXTRA_INDEX);
509 CONSP (extra); extra = XCDR (extra))
511 Lisp_Object key, val;
513 key = XCAR (XCAR (extra)), val = XCDR (XCAR (extra));
514 if (EQ (key, QCdpi))
515 dpi = XINT (val);
516 else if (EQ (key, QClang))
518 langset = FcLangSetCreate ();
519 if (! langset)
520 goto err;
521 if (SYMBOLP (val))
523 if (! FcLangSetAdd (langset, SYMBOL_FcChar8 (val)))
524 goto err;
526 else
527 for (; CONSP (val); val = XCDR (val))
528 if (SYMBOLP (XCAR (val))
529 && ! FcLangSetAdd (langset, SYMBOL_FcChar8 (XCAR (val))))
530 goto err;
532 else if (EQ (key, QCotf))
534 *otspec = ftfont_get_open_type_spec (val);
535 if (! *otspec)
536 return NULL;
537 strcat (otlayout, "otlayout:");
538 OTF_TAG_STR ((*otspec)->script_tag, otlayout + 9);
539 script = (*otspec)->script;
541 else if (EQ (key, QCscript))
542 script = val;
543 else if (EQ (key, QCscalable))
544 scalable = ! NILP (val);
547 if (! NILP (script) && ! charset)
549 Lisp_Object chars = assq_no_quit (script, Vscript_representative_chars);
551 if (CONSP (chars))
553 charset = FcCharSetCreate ();
554 if (! charset)
555 goto err;
556 for (chars = XCDR (chars); CONSP (chars); chars = XCDR (chars))
557 if (CHARACTERP (XCAR (chars))
558 && ! FcCharSetAddChar (charset, XUINT (XCAR (chars))))
559 goto err;
563 pattern = FcPatternCreate ();
564 if (! pattern)
565 goto err;
566 tmp = AREF (spec, FONT_FOUNDRY_INDEX);
567 if (! NILP (tmp)
568 && ! FcPatternAddString (pattern, FC_FOUNDRY, SYMBOL_FcChar8 (tmp)))
569 goto err;
570 tmp = AREF (spec, FONT_FAMILY_INDEX);
571 if (! NILP (tmp)
572 && ! FcPatternAddString (pattern, FC_FAMILY, SYMBOL_FcChar8 (tmp)))
573 goto err;
574 if (charset
575 && ! FcPatternAddCharSet (pattern, FC_CHARSET, charset))
576 goto err;
577 if (langset
578 && ! FcPatternAddLangSet (pattern, FC_LANG, langset))
579 goto err;
580 if (dpi >= 0
581 && ! FcPatternAddDouble (pattern, FC_DPI, dpi))
582 goto err;
583 if (scalable >= 0
584 && ! FcPatternAddBool (pattern, FC_SCALABLE, scalable ? FcTrue : FcFalse))
585 goto err;
586 goto finish;
588 err:
589 /* We come here because of unexpected error in fontconfig API call
590 (usually insufficient memory). */
591 if (pattern)
593 FcPatternDestroy (pattern);
594 pattern = NULL;
596 if (*otspec)
598 if ((*otspec)->nfeatures[0] > 0)
599 free ((*otspec)->features[0]);
600 if ((*otspec)->nfeatures[1] > 0)
601 free ((*otspec)->features[1]);
602 free (*otspec);
603 *otspec = NULL;
606 finish:
607 if (langset) FcLangSetDestroy (langset);
608 if (charset && *fc_charset_idx < 0) FcCharSetDestroy (charset);
609 return pattern;
612 static Lisp_Object
613 ftfont_list (frame, spec)
614 Lisp_Object frame, spec;
616 Lisp_Object val = Qnil, registry, family;
617 int i;
618 FcPattern *pattern;
619 FcFontSet *fontset = NULL;
620 FcObjectSet *objset = NULL;
621 int fc_charset_idx;
622 char otlayout[15]; /* For "otlayout:XXXX" */
623 struct OpenTypeSpec *otspec = NULL;
624 int spacing = -1;
626 if (! fc_initialized)
628 FcInit ();
629 fc_initialized = 1;
632 pattern = ftfont_spec_pattern (spec, &fc_charset_idx, otlayout, &otspec);
633 if (! pattern)
634 return Qnil;
635 if (INTEGERP (AREF (spec, FONT_SPACING_INDEX)))
636 spacing = XINT (AREF (spec, FONT_SPACING_INDEX));
637 registry = AREF (spec, FONT_REGISTRY_INDEX);
638 family = AREF (spec, FONT_FAMILY_INDEX);
639 if (! NILP (family))
641 Lisp_Object resolved;
643 resolved = ftfont_resolve_generic_family (family);
644 if (! NILP (resolved))
646 FcPatternDel (pattern, FC_FAMILY);
647 if (! FcPatternAddString (pattern, FC_FAMILY,
648 SYMBOL_FcChar8 (resolved)))
649 goto err;
653 objset = FcObjectSetBuild (FC_FOUNDRY, FC_FAMILY, FC_WEIGHT, FC_SLANT,
654 FC_WIDTH, FC_PIXEL_SIZE, FC_SPACING, FC_SCALABLE,
655 FC_FILE,
656 #ifdef FC_CAPABILITY
657 FC_CAPABILITY,
658 #endif /* FC_CAPABILITY */
659 #ifdef FC_FONTFORMAT
660 FC_FONTFORMAT,
661 #endif
662 NULL);
663 if (! objset)
664 goto err;
666 fontset = FcFontList (NULL, pattern, objset);
667 if (! fontset)
668 goto err;
669 #if 0
670 /* Need fix because this finds any fonts. */
671 if (fontset->nfont == 0 && ! NILP (family))
673 /* Try maching with configuration. For instance, the
674 configuration may specify "Nimbus Mono L" as an alias of
675 "Courier". */
676 FcPattern *pat = FcPatternBuild (0, FC_FAMILY, FcTypeString,
677 SYMBOL_FcChar8 (family), NULL);
678 FcChar8 *fam;
680 if (FcConfigSubstitute (NULL, pat, FcMatchPattern) == FcTrue)
682 for (i = 0;
683 FcPatternGetString (pat, FC_FAMILY, i, &fam) == FcResultMatch;
684 i++)
686 FcPatternDel (pattern, FC_FAMILY);
687 FcPatternAddString (pattern, FC_FAMILY, fam);
688 FcFontSetDestroy (fontset);
689 fontset = FcFontList (NULL, pattern, objset);
690 if (fontset->nfont > 0)
691 break;
695 #endif
696 for (i = 0; i < fontset->nfont; i++)
698 Lisp_Object entity;
700 if (spacing >= 0)
702 int this;
704 if ((FcPatternGetInteger (fontset->fonts[i], FC_SPACING, 0, &this)
705 == FcResultMatch)
706 && spacing != this)
707 continue;
710 #ifdef FC_CAPABILITY
711 if (otlayout[0])
713 FcChar8 *this;
715 if (FcPatternGetString (fontset->fonts[i], FC_CAPABILITY, 0,
716 &this) != FcResultMatch
717 || ! strstr ((char *) this, otlayout))
718 continue;
720 #endif /* FC_CAPABILITY */
721 #ifdef HAVE_LIBOTF
722 if (otspec)
724 FcChar8 *file;
725 OTF *otf;
727 if (FcPatternGetString (fontset->fonts[i], FC_FILE, 0, &file)
728 != FcResultMatch)
729 continue;
730 otf = OTF_open ((char *) file);
731 if (! otf)
732 continue;
733 if (OTF_check_features (otf, 1,
734 otspec->script_tag, otspec->langsys_tag,
735 otspec->features[0],
736 otspec->nfeatures[0]) != 1
737 || OTF_check_features (otf, 0,
738 otspec->script_tag, otspec->langsys_tag,
739 otspec->features[1],
740 otspec->nfeatures[1]) != 1)
741 continue;
743 #endif /* HAVE_LIBOTF */
744 entity = ftfont_pattern_entity (fontset->fonts[i], registry,
745 AREF (spec, FONT_EXTRA_INDEX),
746 fc_charset_idx);
747 if (! NILP (entity))
748 val = Fcons (entity, val);
750 font_add_log ("ftfont-list", spec, val);
751 goto finish;
753 err:
754 /* We come here because of unexpected error in fontconfig API call
755 (usually insufficient memory). */
756 val = Qnil;
758 finish:
759 if (objset) FcObjectSetDestroy (objset);
760 if (fontset) FcFontSetDestroy (fontset);
761 if (pattern) FcPatternDestroy (pattern);
762 return val;
765 static Lisp_Object
766 ftfont_match (frame, spec)
767 Lisp_Object frame, spec;
769 Lisp_Object entity;
770 FcPattern *pattern, *match = NULL;
771 FcResult result;
772 char otlayout[15]; /* For "otlayout:XXXX" */
773 struct OpenTypeSpec *otspec = NULL;
774 int fc_charset_idx;
776 if (! fc_initialized)
778 FcInit ();
779 fc_initialized = 1;
782 pattern = ftfont_spec_pattern (spec, &fc_charset_idx, otlayout, &otspec);
783 if (! pattern)
784 return Qnil;
786 if (INTEGERP (AREF (spec, FONT_SIZE_INDEX)))
788 FcValue value;
790 value.type = FcTypeDouble;
791 value.u.d = XINT (AREF (spec, FONT_SIZE_INDEX));
792 FcPatternAdd (pattern, FC_PIXEL_SIZE, value, FcFalse);
794 if (FcConfigSubstitute (NULL, pattern, FcMatchPattern) == FcTrue)
796 FcDefaultSubstitute (pattern);
797 match = FcFontMatch (NULL, pattern, &result);
798 if (match)
800 entity = ftfont_pattern_entity (match, Qunicode_bmp,
801 AREF (spec, FONT_EXTRA_INDEX),
802 fc_charset_idx);
803 FcPatternDestroy (match);
804 if (! NILP (AREF (spec, FONT_FAMILY_INDEX))
805 && NILP (assq_no_quit (AREF (spec, FONT_FAMILY_INDEX),
806 ftfont_generic_family_list))
807 && NILP (Fstring_equal (AREF (spec, FONT_FAMILY_INDEX),
808 AREF (entity, FONT_FAMILY_INDEX))))
809 entity = Qnil;
812 FcPatternDestroy (pattern);
814 font_add_log ("ftfont-match", spec, entity);
815 return entity;
818 static Lisp_Object
819 ftfont_list_family (frame)
820 Lisp_Object frame;
822 Lisp_Object list;
823 FcPattern *pattern = NULL;
824 FcFontSet *fontset = NULL;
825 FcObjectSet *objset = NULL;
826 int i;
828 if (! fc_initialized)
830 FcInit ();
831 fc_initialized = 1;
834 pattern = FcPatternCreate ();
835 if (! pattern)
836 goto finish;
837 objset = FcObjectSetBuild (FC_FAMILY, NULL);
838 if (! objset)
839 goto finish;
840 fontset = FcFontList (NULL, pattern, objset);
841 if (! fontset)
842 goto finish;
844 list = Qnil;
845 for (i = 0; i < fontset->nfont; i++)
847 FcPattern *pat = fontset->fonts[i];
848 FcChar8 *str;
850 if (FcPatternGetString (pat, FC_FAMILY, 0, &str) == FcResultMatch)
851 list = Fcons (intern ((char *) str), list);
854 finish:
855 if (objset) FcObjectSetDestroy (objset);
856 if (fontset) FcFontSetDestroy (fontset);
857 if (pattern) FcPatternDestroy (pattern);
859 return list;
863 static Lisp_Object
864 ftfont_open (f, entity, pixel_size)
865 FRAME_PTR f;
866 Lisp_Object entity;
867 int pixel_size;
869 struct ftfont_info *ftfont_info;
870 struct font *font;
871 FT_Face ft_face;
872 FT_Size ft_size;
873 FT_UInt size;
874 Lisp_Object val, filename, cache, font_object;
875 int fc_charset_idx;
876 FcPattern *pattern;
877 int scalable;
878 int spacing;
879 char name[256];
880 int i, len;
881 int upEM;
883 val = assq_no_quit (QCfont_entity, AREF (entity, FONT_EXTRA_INDEX));
884 if (! CONSP (val))
885 return Qnil;
886 val = XCDR (val);
887 filename = XCAR (val);
888 fc_charset_idx = XINT (XCDR (val));
889 cache = ftfont_lookup_cache (filename);
890 if (NILP (cache))
891 return Qnil;
892 filename = XCAR (cache);
893 val = XCDR (cache);
894 ft_face = XSAVE_VALUE (val)->pointer;
895 if (XSAVE_VALUE (val)->integer > 0)
897 /* FT_Face in this cache is already used by the different size. */
898 if (FT_New_Size (ft_face, &ft_size) != 0)
899 return Qnil;
900 if (FT_Activate_Size (ft_size) != 0)
902 FT_Done_Size (ft_size);
903 return Qnil;
906 XSAVE_VALUE (val)->integer++;
907 size = XINT (AREF (entity, FONT_SIZE_INDEX));
908 if (size == 0)
909 size = pixel_size;
910 if (FT_Set_Pixel_Sizes (ft_face, size, size) != 0)
912 if (XSAVE_VALUE (val)->integer == 0)
913 FT_Done_Face (ft_face);
914 return Qnil;
917 font_object = font_make_object (VECSIZE (struct ftfont_info));
918 ASET (font_object, FONT_TYPE_INDEX, Qfreetype);
919 for (i = 1;i < FONT_ENTITY_MAX; i++)
920 ASET (font_object, i, AREF (entity, i));
921 ASET (font_object, FONT_SIZE_INDEX, make_number (size));
922 len = font_unparse_xlfd (entity, size, name, 256);
923 if (len > 0)
924 ASET (font_object, FONT_NAME_INDEX, make_unibyte_string (name, len));
925 len = font_unparse_fcname (entity, size, name, 256);
926 if (len > 0)
927 ASET (font_object, FONT_FULLNAME_INDEX, make_unibyte_string (name, len));
928 else
929 ASET (font_object, FONT_FULLNAME_INDEX,
930 AREF (font_object, FONT_NAME_INDEX));
931 ASET (font_object, FONT_FILE_INDEX, filename);
932 ASET (font_object, FONT_FORMAT_INDEX, ftfont_font_format (pattern));
933 font = XFONT_OBJECT (font_object);
934 ftfont_info = (struct ftfont_info *) font;
935 ftfont_info->ft_size = ft_face->size;
936 ftfont_info->fc_charset_idx = fc_charset_idx;
937 #ifdef HAVE_LIBOTF
938 ftfont_info->maybe_otf = ft_face->face_flags & FT_FACE_FLAG_SFNT;
939 ftfont_info->otf = NULL;
940 #endif /* HAVE_LIBOTF */
941 font->pixel_size = size;
942 font->driver = &ftfont_driver;
943 font->encoding_charset = font->repertory_charset = -1;
945 upEM = ft_face->units_per_EM;
946 scalable = (INTEGERP (AREF (entity, FONT_AVGWIDTH_INDEX))
947 && XINT (AREF (entity, FONT_AVGWIDTH_INDEX)) == 0);
948 if (scalable)
950 font->ascent = ft_face->ascender * size / upEM;
951 font->descent = - ft_face->descender * size / upEM;
952 font->height = ft_face->height * size / upEM;
954 else
956 font->ascent = ft_face->size->metrics.ascender >> 6;
957 font->descent = - ft_face->size->metrics.descender >> 6;
958 font->height = ft_face->size->metrics.height >> 6;
960 if (INTEGERP (AREF (entity, FONT_SPACING_INDEX)))
961 spacing = XINT (AREF (entity, FONT_SPACING_INDEX));
962 else
963 spacing = FC_PROPORTIONAL;
964 if (spacing != FC_PROPORTIONAL)
965 font->min_width = font->average_width = font->space_width
966 = (scalable ? ft_face->max_advance_width * size / upEM
967 : ft_face->size->metrics.max_advance >> 6);
968 else
970 int n;
972 font->min_width = font->average_width = font->space_width = 0;
973 for (i = 32, n = 0; i < 127; i++)
974 if (FT_Load_Char (ft_face, i, FT_LOAD_DEFAULT) != 0)
976 int this_width = ft_face->glyph->metrics.horiAdvance >> 6;
978 if (this_width > 0
979 && (! font->min_width || font->min_width > this_width))
980 font->min_width = this_width;
981 if (i == 32)
982 font->space_width = this_width;
983 font->average_width += this_width;
984 n++;
986 if (n > 0)
987 font->average_width /= n;
990 font->baseline_offset = 0;
991 font->relative_compose = 0;
992 font->default_ascent = 0;
993 font->vertical_centering = 0;
994 if (scalable)
996 font->underline_position = -ft_face->underline_position * size / upEM;
997 font->underline_thickness = -ft_face->underline_thickness * size / upEM;
999 else
1001 font->underline_position = -1;
1002 font->underline_thickness = 0;
1005 return font_object;
1008 static void
1009 ftfont_close (f, font)
1010 FRAME_PTR f;
1011 struct font *font;
1013 struct ftfont_info *ftfont_info = (struct ftfont_info *) font;
1014 Lisp_Object val, cache;
1016 cache = ftfont_lookup_cache (font->props[FONT_FILE_INDEX]);
1017 val = XCDR (cache);
1018 (XSAVE_VALUE (val)->integer)--;
1019 if (XSAVE_VALUE (val)->integer == 0)
1021 FT_Face ft_face = XSAVE_VALUE (val)->pointer;
1023 FT_Done_Face (ft_face);
1024 #ifdef HAVE_LIBOTF
1025 if (ftfont_info->otf)
1026 OTF_close (ftfont_info->otf);
1027 #endif
1028 XSAVE_VALUE (val)->pointer = NULL;
1030 else
1031 FT_Done_Size (ftfont_info->ft_size);
1034 static int
1035 ftfont_has_char (entity, c)
1036 Lisp_Object entity;
1037 int c;
1039 Lisp_Object val;
1040 int fc_charset_idx;
1041 struct charset *charset;
1043 val = assq_no_quit (QCfont_entity, AREF (entity, FONT_EXTRA_INDEX));
1044 fc_charset_idx = XINT (XCDR (XCDR (val)));
1045 if (fc_charset_idx < 0)
1046 return -1;
1047 charset = CHARSET_FROM_ID (fc_charset_table[fc_charset_idx].charset_id);
1048 return (ENCODE_CHAR (charset, c) != CHARSET_INVALID_CODE (charset));
1051 static unsigned
1052 ftfont_encode_char (font, c)
1053 struct font *font;
1054 int c;
1056 struct ftfont_info *ftfont_info = (struct ftfont_info *) font;
1057 FT_Face ft_face = ftfont_info->ft_size->face;
1058 FT_ULong charcode = c;
1059 FT_UInt code = FT_Get_Char_Index (ft_face, charcode);
1061 return (code > 0 ? code : FONT_INVALID_CODE);
1064 static int
1065 ftfont_text_extents (font, code, nglyphs, metrics)
1066 struct font *font;
1067 unsigned *code;
1068 int nglyphs;
1069 struct font_metrics *metrics;
1071 struct ftfont_info *ftfont_info = (struct ftfont_info *) font;
1072 FT_Face ft_face = ftfont_info->ft_size->face;
1073 int width = 0;
1074 int i;
1076 if (ftfont_info->ft_size != ft_face->size)
1077 FT_Activate_Size (ftfont_info->ft_size);
1078 if (metrics)
1079 bzero (metrics, sizeof (struct font_metrics));
1080 for (i = 0; i < nglyphs; i++)
1082 if (FT_Load_Glyph (ft_face, code[i], FT_LOAD_DEFAULT) == 0)
1084 FT_Glyph_Metrics *m = &ft_face->glyph->metrics;
1086 if (metrics)
1088 if (metrics->lbearing > width + (m->horiBearingX >> 6))
1089 metrics->lbearing = width + (m->horiBearingX >> 6);
1090 if (metrics->rbearing
1091 < width + ((m->horiBearingX + m->width) >> 6))
1092 metrics->rbearing
1093 = width + ((m->horiBearingX + m->width) >> 6);
1094 if (metrics->ascent < (m->horiBearingY >> 6))
1095 metrics->ascent = m->horiBearingY >> 6;
1096 if (metrics->descent > ((m->horiBearingY + m->height) >> 6))
1097 metrics->descent = (m->horiBearingY + m->height) >> 6;
1099 width += m->horiAdvance >> 6;
1101 else
1103 width += font->space_width;
1106 if (metrics)
1107 metrics->width = width;
1109 return width;
1112 static int
1113 ftfont_get_bitmap (font, code, bitmap, bits_per_pixel)
1114 struct font *font;
1115 unsigned code;
1116 struct font_bitmap *bitmap;
1117 int bits_per_pixel;
1119 struct ftfont_info *ftfont_info = (struct ftfont_info *) font;
1120 FT_Face ft_face = ftfont_info->ft_size->face;
1121 FT_Int32 load_flags = FT_LOAD_RENDER;
1123 if (ftfont_info->ft_size != ft_face->size)
1124 FT_Activate_Size (ftfont_info->ft_size);
1125 if (bits_per_pixel == 1)
1127 #ifdef FT_LOAD_TARGET_MONO
1128 load_flags |= FT_LOAD_TARGET_MONO;
1129 #else
1130 load_flags |= FT_LOAD_MONOCHROME;
1131 #endif
1133 else if (bits_per_pixel != 8)
1134 /* We don't support such a rendering. */
1135 return -1;
1137 if (FT_Load_Glyph (ft_face, code, load_flags) != 0)
1138 return -1;
1139 bitmap->bits_per_pixel
1140 = (ft_face->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_MONO ? 1
1141 : ft_face->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_GRAY ? 8
1142 : ft_face->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_LCD ? 8
1143 : ft_face->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_LCD_V ? 8
1144 : -1);
1145 if (bitmap->bits_per_pixel < 0)
1146 /* We don't suport that kind of pixel mode. */
1147 return -1;
1148 bitmap->rows = ft_face->glyph->bitmap.rows;
1149 bitmap->width = ft_face->glyph->bitmap.width;
1150 bitmap->pitch = ft_face->glyph->bitmap.pitch;
1151 bitmap->buffer = ft_face->glyph->bitmap.buffer;
1152 bitmap->left = ft_face->glyph->bitmap_left;
1153 bitmap->top = ft_face->glyph->bitmap_top;
1154 bitmap->advance = ft_face->glyph->metrics.horiAdvance >> 6;
1155 bitmap->extra = NULL;
1157 return 0;
1160 static int
1161 ftfont_anchor_point (font, code, index, x, y)
1162 struct font *font;
1163 unsigned code;
1164 int index;
1165 int *x, *y;
1167 struct ftfont_info *ftfont_info = (struct ftfont_info *) font;
1168 FT_Face ft_face = ftfont_info->ft_size->face;
1170 if (ftfont_info->ft_size != ft_face->size)
1171 FT_Activate_Size (ftfont_info->ft_size);
1172 if (FT_Load_Glyph (ft_face, code, FT_LOAD_DEFAULT) != 0)
1173 return -1;
1174 if (ft_face->glyph->format != FT_GLYPH_FORMAT_OUTLINE)
1175 return -1;
1176 if (index >= ft_face->glyph->outline.n_points)
1177 return -1;
1178 *x = ft_face->glyph->outline.points[index].x;
1179 *y = ft_face->glyph->outline.points[index].y;
1180 return 0;
1183 #ifdef HAVE_LIBOTF
1184 #ifdef HAVE_M17N_FLT
1186 struct MFLTFontFT
1188 MFLTFont flt_font;
1189 struct font *font;
1190 FT_Face ft_face;
1191 OTF *otf;
1194 static int
1195 ftfont_get_glyph_id (font, gstring, from, to)
1196 MFLTFont *font;
1197 MFLTGlyphString *gstring;
1198 int from, to;
1200 struct MFLTFontFT *flt_font_ft = (struct MFLTFontFT *) font;
1201 FT_Face ft_face = flt_font_ft->ft_face;
1202 MFLTGlyph *g;
1204 for (g = gstring->glyphs + from; from < to; g++, from++)
1205 if (! g->encoded)
1207 FT_UInt code = FT_Get_Char_Index (ft_face, g->code);
1209 g->code = code > 0 ? code : FONT_INVALID_CODE;
1210 g->encoded = 1;
1212 return 0;
1215 static int
1216 ftfont_get_metrics (font, gstring, from, to)
1217 MFLTFont *font;
1218 MFLTGlyphString *gstring;
1219 int from, to;
1221 struct MFLTFontFT *flt_font_ft = (struct MFLTFontFT *) font;
1222 FT_Face ft_face = flt_font_ft->ft_face;
1223 MFLTGlyph *g;
1225 for (g = gstring->glyphs + from; from < to; g++, from++)
1226 if (! g->measured)
1228 if (g->code != FONT_INVALID_CODE)
1230 FT_Glyph_Metrics *m;
1232 if (FT_Load_Glyph (ft_face, g->code, FT_LOAD_DEFAULT) != 0)
1233 abort ();
1234 m = &ft_face->glyph->metrics;
1236 g->lbearing = m->horiBearingX;
1237 g->rbearing = m->horiBearingX + m->width;
1238 g->ascent = m->horiBearingY;
1239 g->descent = m->height - m->horiBearingY;
1240 g->xadv = m->horiAdvance;
1242 else
1244 g->lbearing = 0;
1245 g->rbearing = g->xadv = flt_font_ft->font->space_width << 6;
1246 g->ascent = flt_font_ft->font->ascent << 6;
1247 g->descent = flt_font_ft->font->descent << 6;
1249 g->yadv = 0;
1250 g->measured = 1;
1252 return 0;
1255 static int
1256 ftfont_check_otf (MFLTFont *font, MFLTOtfSpec *spec)
1258 struct MFLTFontFT *flt_font_ft = (struct MFLTFontFT *) font;
1259 OTF *otf = flt_font_ft->otf;
1260 OTF_Tag *tags;
1261 int i, n, negative;
1263 for (i = 0; i < 2; i++)
1265 if (! spec->features[i])
1266 continue;
1267 for (n = 0; spec->features[i][n]; n++);
1268 tags = alloca (sizeof (OTF_Tag) * n);
1269 for (n = 0, negative = 0; spec->features[i][n]; n++)
1271 if (spec->features[i][n] == 0xFFFFFFFF)
1272 negative = 1;
1273 else if (negative)
1274 tags[n - 1] = spec->features[i][n] | 0x80000000;
1275 else
1276 tags[n] = spec->features[i][n];
1278 if (n - negative > 0
1279 && OTF_check_features (otf, i == 0, spec->script, spec->langsys,
1280 tags, n - negative) != 1)
1281 return 0;
1283 return 1;
1286 #define DEVICE_DELTA(table, size) \
1287 (((size) >= (table).StartSize && (size) <= (table).EndSize) \
1288 ? (table).DeltaValue[(size) - (table).StartSize] << 6 \
1289 : 0)
1291 static void
1292 adjust_anchor (FT_Face ft_face, OTF_Anchor *anchor,
1293 unsigned code, int x_ppem, int y_ppem, int *x, int *y)
1295 if (anchor->AnchorFormat == 2)
1297 FT_Outline *outline;
1298 int ap = anchor->f.f1.AnchorPoint;
1300 FT_Load_Glyph (ft_face, (FT_UInt) code, FT_LOAD_MONOCHROME);
1301 outline = &ft_face->glyph->outline;
1302 if (ap < outline->n_points)
1304 *x = outline->points[ap].x << 6;
1305 *y = outline->points[ap].y << 6;
1308 else if (anchor->AnchorFormat == 3)
1310 if (anchor->f.f2.XDeviceTable.offset)
1311 *x += DEVICE_DELTA (anchor->f.f2.XDeviceTable, x_ppem);
1312 if (anchor->f.f2.YDeviceTable.offset)
1313 *y += DEVICE_DELTA (anchor->f.f2.YDeviceTable, y_ppem);
1317 static OTF_GlyphString otf_gstring;
1319 static int
1320 ftfont_drive_otf (font, spec, in, from, to, out, adjustment)
1321 MFLTFont *font;
1322 MFLTOtfSpec *spec;
1323 MFLTGlyphString *in;
1324 int from, to;
1325 MFLTGlyphString *out;
1326 MFLTGlyphAdjustment *adjustment;
1328 struct MFLTFontFT *flt_font_ft = (struct MFLTFontFT *) font;
1329 FT_Face ft_face = flt_font_ft->ft_face;
1330 OTF *otf = flt_font_ft->otf;
1331 int len = to - from;
1332 int i, j, gidx;
1333 OTF_Glyph *otfg;
1334 char script[5], *langsys = NULL;
1335 char *gsub_features = NULL, *gpos_features = NULL;
1337 if (len == 0)
1338 return from;
1339 OTF_tag_name (spec->script, script);
1340 if (spec->langsys)
1342 langsys = alloca (5);
1343 OTF_tag_name (spec->langsys, langsys);
1345 for (i = 0; i < 2; i++)
1347 char *p;
1349 if (spec->features[i] && spec->features[i][1] != 0xFFFFFFFF)
1351 for (j = 0; spec->features[i][j]; j++);
1352 if (i == 0)
1353 p = gsub_features = alloca (6 * j);
1354 else
1355 p = gpos_features = alloca (6 * j);
1356 for (j = 0; spec->features[i][j]; j++)
1358 if (spec->features[i][j] == 0xFFFFFFFF)
1359 *p++ = '*', *p++ = ',';
1360 else
1362 OTF_tag_name (spec->features[i][j], p);
1363 p[4] = ',';
1364 p += 5;
1367 *--p = '\0';
1371 if (otf_gstring.size == 0)
1373 otf_gstring.glyphs = (OTF_Glyph *) malloc (sizeof (OTF_Glyph) * len);
1374 otf_gstring.size = len;
1376 else if (otf_gstring.size < len)
1378 otf_gstring.glyphs = (OTF_Glyph *) realloc (otf_gstring.glyphs,
1379 sizeof (OTF_Glyph) * len);
1380 otf_gstring.size = len;
1382 otf_gstring.used = len;
1383 memset (otf_gstring.glyphs, 0, sizeof (OTF_Glyph) * len);
1384 for (i = 0; i < len; i++)
1386 otf_gstring.glyphs[i].c = in->glyphs[from + i].c;
1387 otf_gstring.glyphs[i].glyph_id = in->glyphs[from + i].code;
1390 OTF_drive_gdef (otf, &otf_gstring);
1391 gidx = out->used;
1393 if (gsub_features)
1395 if (OTF_drive_gsub (otf, &otf_gstring, script, langsys, gsub_features)
1396 < 0)
1397 goto simple_copy;
1398 if (out->allocated < out->used + otf_gstring.used)
1399 return -2;
1400 for (i = 0, otfg = otf_gstring.glyphs; i < otf_gstring.used; )
1402 MFLTGlyph *g;
1403 int min_from, max_to;
1404 int j;
1406 g = out->glyphs + out->used;
1407 *g = in->glyphs[from + otfg->f.index.from];
1408 if (g->code != otfg->glyph_id)
1410 g->c = 0;
1411 g->code = otfg->glyph_id;
1412 g->measured = 0;
1414 out->used++;
1415 min_from = g->from;
1416 max_to = g->to;
1417 if (otfg->f.index.from < otfg->f.index.to)
1419 /* OTFG substitutes multiple glyphs in IN. */
1420 for (j = from + otfg->f.index.from + 1;
1421 j <= from + otfg->f.index.to; j++)
1423 if (min_from > in->glyphs[j].from)
1424 min_from = in->glyphs[j].from;
1425 if (max_to < in->glyphs[j].to)
1426 max_to = in->glyphs[j].to;
1428 g->from = min_from;
1429 g->to = max_to;
1431 for (i++, otfg++; (i < otf_gstring.used
1432 && otfg->f.index.from == otfg[-1].f.index.from);
1433 i++, otfg++)
1435 g = out->glyphs + out->used;
1436 *g = in->glyphs[from + otfg->f.index.to];
1437 if (g->code != otfg->glyph_id)
1439 g->c = 0;
1440 g->code = otfg->glyph_id;
1441 g->measured = 0;
1443 out->used++;
1447 else
1449 if (out->allocated < out->used + len)
1450 return -2;
1451 for (i = 0; i < len; i++)
1452 out->glyphs[out->used++] = in->glyphs[from + i];
1455 if (gpos_features)
1457 MFLTGlyph *base = NULL, *mark = NULL, *g;
1458 int x_ppem, y_ppem, x_scale, y_scale;
1460 if (OTF_drive_gpos (otf, &otf_gstring, script, langsys, gpos_features)
1461 < 0)
1462 return to;
1464 x_ppem = ft_face->size->metrics.x_ppem;
1465 y_ppem = ft_face->size->metrics.y_ppem;
1466 x_scale = ft_face->size->metrics.x_scale;
1467 y_scale = ft_face->size->metrics.y_scale;
1469 for (i = 0, otfg = otf_gstring.glyphs, g = out->glyphs + gidx;
1470 i < otf_gstring.used; i++, otfg++, g++)
1472 MFLTGlyph *prev;
1474 if (! otfg->glyph_id)
1475 continue;
1476 switch (otfg->positioning_type)
1478 case 0:
1479 break;
1480 case 1: /* Single */
1481 case 2: /* Pair */
1483 int format = otfg->f.f1.format;
1485 if (format & OTF_XPlacement)
1486 adjustment[i].xoff
1487 = otfg->f.f1.value->XPlacement * x_scale / 0x10000;
1488 if (format & OTF_XPlaDevice)
1489 adjustment[i].xoff
1490 += DEVICE_DELTA (otfg->f.f1.value->XPlaDevice, x_ppem);
1491 if (format & OTF_YPlacement)
1492 adjustment[i].yoff
1493 = - (otfg->f.f1.value->YPlacement * y_scale / 0x10000);
1494 if (format & OTF_YPlaDevice)
1495 adjustment[i].yoff
1496 -= DEVICE_DELTA (otfg->f.f1.value->YPlaDevice, y_ppem);
1497 if (format & OTF_XAdvance)
1498 adjustment[i].xadv
1499 += otfg->f.f1.value->XAdvance * x_scale / 0x10000;
1500 if (format & OTF_XAdvDevice)
1501 adjustment[i].xadv
1502 += DEVICE_DELTA (otfg->f.f1.value->XAdvDevice, x_ppem);
1503 if (format & OTF_YAdvance)
1504 adjustment[i].yadv
1505 += otfg->f.f1.value->YAdvance * y_scale / 0x10000;
1506 if (format & OTF_YAdvDevice)
1507 adjustment[i].yadv
1508 += DEVICE_DELTA (otfg->f.f1.value->YAdvDevice, y_ppem);
1509 adjustment[i].set = 1;
1511 break;
1512 case 3: /* Cursive */
1513 /* Not yet supported. */
1514 break;
1515 case 4: /* Mark-to-Base */
1516 case 5: /* Mark-to-Ligature */
1517 if (! base)
1518 break;
1519 prev = base;
1520 goto label_adjust_anchor;
1521 default: /* i.e. case 6 Mark-to-Mark */
1522 if (! mark)
1523 break;
1524 prev = mark;
1526 label_adjust_anchor:
1528 int base_x, base_y, mark_x, mark_y;
1529 int this_from, this_to;
1531 base_x = otfg->f.f4.base_anchor->XCoordinate * x_scale / 0x10000;
1532 base_y = otfg->f.f4.base_anchor->YCoordinate * y_scale / 0x10000;
1533 mark_x = otfg->f.f4.mark_anchor->XCoordinate * x_scale / 0x10000;
1534 mark_y = otfg->f.f4.mark_anchor->YCoordinate * y_scale / 0x10000;;
1536 if (otfg->f.f4.base_anchor->AnchorFormat != 1)
1537 adjust_anchor (ft_face, otfg->f.f4.base_anchor,
1538 prev->code, x_ppem, y_ppem, &base_x, &base_y);
1539 if (otfg->f.f4.mark_anchor->AnchorFormat != 1)
1540 adjust_anchor (ft_face, otfg->f.f4.mark_anchor, g->code,
1541 x_ppem, y_ppem, &mark_x, &mark_y);
1542 adjustment[i].xoff = (base_x - mark_x);
1543 adjustment[i].yoff = - (base_y - mark_y);
1544 adjustment[i].back = (g - prev);
1545 adjustment[i].xadv = 0;
1546 adjustment[i].advance_is_absolute = 1;
1547 adjustment[i].set = 1;
1548 this_from = g->from;
1549 this_to = g->to;
1550 for (j = 0; prev + j < g; j++)
1552 if (this_from > prev[j].from)
1553 this_from = prev[j].from;
1554 if (this_to < prev[j].to)
1555 this_to = prev[j].to;
1557 for (; prev <= g; prev++)
1559 prev->from = this_from;
1560 prev->to = this_to;
1564 if (otfg->GlyphClass == OTF_GlyphClass0)
1565 base = mark = g;
1566 else if (otfg->GlyphClass == OTF_GlyphClassMark)
1567 mark = g;
1568 else
1569 base = g;
1572 return to;
1574 simple_copy:
1575 if (out->allocated < out->used + len)
1576 return -2;
1577 font->get_metrics (font, in, from, to);
1578 memcpy (out->glyphs + out->used, in->glyphs + from,
1579 sizeof (MFLTGlyph) * len);
1580 out->used += len;
1581 return to;
1584 static MFLTGlyphString gstring;
1586 static int m17n_flt_initialized;
1588 extern Lisp_Object QCfamily;
1590 Lisp_Object
1591 ftfont_shape_by_flt (lgstring, font, ft_face, otf)
1592 Lisp_Object lgstring;
1593 struct font *font;
1594 FT_Face ft_face;
1595 OTF *otf;
1597 EMACS_UINT len = LGSTRING_LENGTH (lgstring);
1598 EMACS_UINT i;
1599 struct MFLTFontFT flt_font_ft;
1601 if (! m17n_flt_initialized)
1603 M17N_INIT ();
1604 m17n_flt_initialized = 1;
1607 for (i = 0; i < len; i++)
1608 if (NILP (LGSTRING_GLYPH (lgstring, i)))
1609 break;
1610 len = i;
1612 if (gstring.allocated == 0)
1614 gstring.allocated = len * 2;
1615 gstring.glyph_size = sizeof (MFLTGlyph);
1616 gstring.glyphs = malloc (sizeof (MFLTGlyph) * gstring.allocated);
1618 else if (gstring.allocated < len * 2)
1620 gstring.allocated = len * 2;
1621 gstring.glyphs = realloc (gstring.glyphs,
1622 sizeof (MFLTGlyph) * gstring.allocated);
1624 for (i = 0; i < len; i++)
1625 gstring.glyphs[i].c = LGLYPH_CHAR (LGSTRING_GLYPH (lgstring, i));
1626 gstring.used = len;
1627 gstring.r2l = 0;
1630 Lisp_Object family = Ffont_get (LGSTRING_FONT (lgstring), QCfamily);
1632 if (NILP (family))
1633 flt_font_ft.flt_font.family = Mnil;
1634 else
1635 flt_font_ft.flt_font.family
1636 = msymbol ((char *) SDATA (SYMBOL_NAME (family)));
1638 flt_font_ft.flt_font.x_ppem = ft_face->size->metrics.x_ppem;
1639 flt_font_ft.flt_font.y_ppem = ft_face->size->metrics.y_ppem;
1640 flt_font_ft.flt_font.get_glyph_id = ftfont_get_glyph_id;
1641 flt_font_ft.flt_font.get_metrics = ftfont_get_metrics;
1642 flt_font_ft.flt_font.check_otf = ftfont_check_otf;
1643 flt_font_ft.flt_font.drive_otf = ftfont_drive_otf;
1644 flt_font_ft.flt_font.internal = NULL;
1645 flt_font_ft.font = font;
1646 flt_font_ft.ft_face = ft_face;
1647 flt_font_ft.otf = otf;
1648 for (i = 0; i < 3; i++)
1650 int result = mflt_run (&gstring, 0, len, &flt_font_ft.flt_font, NULL);
1651 if (result != -2)
1652 break;
1653 gstring.allocated += gstring.allocated;
1654 gstring.glyphs = realloc (gstring.glyphs,
1655 sizeof (MFLTGlyph) * gstring.allocated);
1657 if (gstring.used > LGSTRING_LENGTH (lgstring))
1658 return Qnil;
1659 for (i = 0; i < gstring.used; i++)
1661 MFLTGlyph *g = gstring.glyphs + i;
1663 g->from = LGLYPH_FROM (LGSTRING_GLYPH (lgstring, g->from));
1664 g->to = LGLYPH_TO (LGSTRING_GLYPH (lgstring, g->to));
1667 for (i = 0; i < gstring.used; i++)
1669 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
1670 MFLTGlyph *g = gstring.glyphs + i;
1672 if (NILP (lglyph))
1674 lglyph = Fmake_vector (make_number (LGLYPH_SIZE), Qnil);
1675 LGSTRING_SET_GLYPH (lgstring, i, lglyph);
1677 LGLYPH_SET_FROM (lglyph, g->from);
1678 LGLYPH_SET_TO (lglyph, g->to);
1679 LGLYPH_SET_CHAR (lglyph, g->c);
1680 LGLYPH_SET_CODE (lglyph, g->code);
1681 LGLYPH_SET_WIDTH (lglyph, g->xadv >> 6);
1682 LGLYPH_SET_LBEARING (lglyph, g->lbearing >> 6);
1683 LGLYPH_SET_RBEARING (lglyph, g->rbearing >> 6);
1684 LGLYPH_SET_ASCENT (lglyph, g->ascent >> 6);
1685 LGLYPH_SET_DESCENT (lglyph, g->descent >> 6);
1686 if (g->adjusted)
1688 Lisp_Object vec;
1690 vec = Fmake_vector (make_number (3), Qnil);
1691 ASET (vec, 0, make_number (g->xoff >> 6));
1692 ASET (vec, 1, make_number (g->yoff >> 6));
1693 ASET (vec, 2, make_number (g->xadv >> 6));
1694 LGLYPH_SET_ADJUSTMENT (lglyph, vec);
1697 return make_number (i);
1700 Lisp_Object
1701 ftfont_shape (lgstring)
1702 Lisp_Object lgstring;
1704 struct font *font;
1705 struct ftfont_info *ftfont_info;
1707 CHECK_FONT_GET_OBJECT (LGSTRING_FONT (lgstring), font);
1708 ftfont_info = (struct ftfont_info *) font;
1709 if (! ftfont_info->maybe_otf)
1710 return make_number (0);
1711 if (! ftfont_info->otf)
1713 OTF *otf = OTF_open_ft_face (ftfont_info->ft_size->face);
1715 if (! otf || OTF_get_table (otf, "head") < 0)
1717 if (otf)
1718 OTF_close (otf);
1719 ftfont_info->maybe_otf = 0;
1720 return make_number (0);
1723 ftfont_info->otf = otf;
1726 return ftfont_shape_by_flt (lgstring, font, ftfont_info->ft_size->face,
1727 ftfont_info->otf);
1730 #endif /* HAVE_M17N_FLT */
1731 #endif /* HAVE_LIBOTF */
1733 Lisp_Object
1734 ftfont_font_format (FcPattern *pattern)
1736 FcChar8 *str;
1738 #ifdef FC_FONTFORMAT
1739 if (FcPatternGetString (pattern, FC_FONTFORMAT, 0, &str) != FcResultMatch)
1740 return Qnil;
1741 if (strcmp ((char *) str, "TrueType") == 0)
1742 return intern ("truetype");
1743 if (strcmp ((char *) str, "Type 1") == 0)
1744 return intern ("type1");
1745 if (strcmp ((char *) str, "PCF") == 0)
1746 return intern ("pcf");
1747 if (strcmp ((char *) str, "BDF") == 0)
1748 return intern ("bdf");
1749 #else /* not FC_FONTFORMAT */
1750 int len;
1752 if (FcPatternGetString (pattern, FC_FILE, 0, &str) != FcResultMatch)
1753 return Qnil;
1754 len = strlen ((char *) str);
1755 if (len >= 4)
1757 str += len - 4;
1758 if (xstrcasecmp ((char *) str, ".ttf") == 0)
1759 return intern ("truetype");
1760 if (xstrcasecmp ((char *) str, "pfb") == 0)
1761 return intern ("type1");
1762 if (xstrcasecmp ((char *) str, "pcf") == 0)
1763 return intern ("pcf");
1764 if (xstrcasecmp ((char *) str, "bdf") == 0)
1765 return intern ("bdf");
1767 #endif /* not FC_FONTFORMAT */
1768 return intern ("unknown");
1772 void
1773 syms_of_ftfont ()
1775 DEFSYM (Qfreetype, "freetype");
1776 DEFSYM (Qmonospace, "monospace");
1777 DEFSYM (Qsans_serif, "sans-serif");
1778 DEFSYM (Qserif, "serif");
1779 DEFSYM (Qmono, "mono");
1780 DEFSYM (Qsans, "sans");
1781 DEFSYM (Qsans__serif, "sans serif");
1783 staticpro (&freetype_font_cache);
1784 freetype_font_cache = Fcons (Qt, Qnil);
1786 staticpro (&ftfont_generic_family_list);
1787 ftfont_generic_family_list
1788 = Fcons (Fcons (Qmonospace, Qt),
1789 Fcons (Fcons (Qsans_serif, Qt),
1790 Fcons (Fcons (Qsans, Qt), Qnil)));
1792 staticpro (&ft_face_cache);
1793 ft_face_cache = Qnil;
1795 ftfont_driver.type = Qfreetype;
1796 register_font_driver (&ftfont_driver, NULL);
1799 /* arch-tag: 7cfa432c-33a6-4988-83d2-a82ed8604aca
1800 (do not change this comment) */