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/>. */
25 #include <fontconfig/fontconfig.h>
26 #include <fontconfig/fcfreetype.h>
29 #include "dispextern.h"
31 #include "blockinput.h"
32 #include "character.h"
35 #include "composite.h"
40 /* Symbolic type of this font-driver. */
41 Lisp_Object Qfreetype
;
43 /* Fontconfig's generic families and their aliases. */
44 static Lisp_Object Qmonospace
, Qsans_serif
, Qserif
, Qmono
, Qsans
, Qsans__serif
;
46 /* Flag to tell if FcInit is already called or not. */
47 static int fc_initialized
;
49 /* Handle to a FreeType library instance. */
50 static FT_Library ft_library
;
52 /* Cache for FreeType fonts. */
53 static Lisp_Object freetype_font_cache
;
55 /* Cache for FT_Face and FcCharSet. */
56 static Lisp_Object ft_face_cache
;
58 /* The actual structure for FreeType font that can be casted to struct
65 /* The following three 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. */
69 #endif /* HAVE_LIBOTF */
74 static Lisp_Object ftfont_pattern_entity
P_ ((FcPattern
*, Lisp_Object
));
76 static Lisp_Object ftfont_resolve_generic_family
P_ ((Lisp_Object
,
78 static Lisp_Object ftfont_lookup_cache
P_ ((Lisp_Object
, int));
80 Lisp_Object ftfont_font_format
P_ ((FcPattern
*, Lisp_Object
));
82 #define SYMBOL_FcChar8(SYM) (FcChar8 *) SDATA (SYMBOL_NAME (SYM))
88 /* characters to distinguish the charset from the others */
90 /* additional constraint by language */
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"},
134 extern Lisp_Object Qc
, Qm
, Qp
, Qd
;
137 ftfont_pattern_entity (p
, extra
)
148 if (FcPatternGetString (p
, FC_FILE
, 0, (FcChar8
**) &file
) != FcResultMatch
)
150 if (FcPatternGetInteger (p
, FC_INDEX
, 0, &index
) != FcResultMatch
)
153 entity
= font_make_entity ();
155 ASET (entity
, FONT_TYPE_INDEX
, Qfreetype
);
156 ASET (entity
, FONT_REGISTRY_INDEX
, Qiso10646_1
);
158 if (FcPatternGetString (p
, FC_FOUNDRY
, 0, (FcChar8
**) &str
) == FcResultMatch
)
159 ASET (entity
, FONT_FOUNDRY_INDEX
, font_intern_prop (str
, strlen (str
), 1));
160 if (FcPatternGetString (p
, FC_FAMILY
, 0, (FcChar8
**) &str
) == FcResultMatch
)
161 ASET (entity
, FONT_FAMILY_INDEX
, font_intern_prop (str
, strlen (str
), 1));
162 if (FcPatternGetInteger (p
, FC_WEIGHT
, 0, &numeric
) == FcResultMatch
)
164 if (numeric
>= FC_WEIGHT_REGULAR
&& numeric
< FC_WEIGHT_MEDIUM
)
165 numeric
= FC_WEIGHT_MEDIUM
;
166 FONT_SET_STYLE (entity
, FONT_WEIGHT_INDEX
, make_number (numeric
));
168 if (FcPatternGetInteger (p
, FC_SLANT
, 0, &numeric
) == FcResultMatch
)
171 FONT_SET_STYLE (entity
, FONT_SLANT_INDEX
, make_number (numeric
));
173 if (FcPatternGetInteger (p
, FC_WIDTH
, 0, &numeric
) == FcResultMatch
)
175 FONT_SET_STYLE (entity
, FONT_WIDTH_INDEX
, make_number (numeric
));
177 if (FcPatternGetDouble (p
, FC_PIXEL_SIZE
, 0, &dbl
) == FcResultMatch
)
178 ASET (entity
, FONT_SIZE_INDEX
, make_number (dbl
));
180 ASET (entity
, FONT_SIZE_INDEX
, make_number (0));
181 if (FcPatternGetInteger (p
, FC_SPACING
, 0, &numeric
) == FcResultMatch
)
182 ASET (entity
, FONT_SPACING_INDEX
, make_number (numeric
));
183 if (FcPatternGetDouble (p
, FC_DPI
, 0, &dbl
) == FcResultMatch
)
186 ASET (entity
, FONT_DPI_INDEX
, make_number (dpi
));
188 if (FcPatternGetBool (p
, FC_SCALABLE
, 0, &b
) == FcResultMatch
190 ASET (entity
, FONT_AVGWIDTH_INDEX
, make_number (0));
192 ASET (entity
, FONT_EXTRA_INDEX
, Fcopy_sequence (extra
));
193 font_put_extra (entity
, QCfont_entity
,
194 Fcons (make_unibyte_string ((char *) file
,
195 strlen ((char *) file
)),
196 make_number (index
)));
201 static Lisp_Object ftfont_generic_family_list
;
204 ftfont_resolve_generic_family (family
, pattern
)
213 family
= Fintern (Fdowncase (SYMBOL_NAME (family
)), Qnil
);
214 if (EQ (family
, Qmono
))
216 else if (EQ (family
, Qsans
) || EQ (family
, Qsans__serif
))
217 family
= Qsans_serif
;
218 slot
= assq_no_quit (family
, ftfont_generic_family_list
);
221 if (! EQ (XCDR (slot
), Qt
))
223 pattern
= FcPatternDuplicate (pattern
);
226 FcPatternDel (pattern
, FC_FOUNDRY
);
227 FcPatternDel (pattern
, FC_FAMILY
);
228 FcPatternAddString (pattern
, FC_FAMILY
, SYMBOL_FcChar8 (family
));
229 if (FcPatternGetLangSet (pattern
, FC_LANG
, 0, &langset
) != FcResultMatch
)
231 /* This is to avoid the effect of locale. */
232 langset
= FcLangSetCreate ();
233 FcLangSetAdd (langset
, "en");
234 FcPatternAddLangSet (pattern
, FC_LANG
, langset
);
235 FcLangSetDestroy (langset
);
237 FcConfigSubstitute (NULL
, pattern
, FcMatchPattern
);
238 FcDefaultSubstitute (pattern
);
239 match
= FcFontMatch (NULL
, pattern
, &result
);
244 if (FcPatternGetString (match
, FC_FAMILY
, 0, &fam
) == FcResultMatch
)
245 family
= intern ((char *) fam
);
249 XSETCDR (slot
, family
);
251 if (match
) FcPatternDestroy (match
);
252 if (pattern
) FcPatternDestroy (pattern
);
256 struct ftfont_cache_data
259 FcCharSet
*fc_charset
;
263 ftfont_lookup_cache (key
, for_face
)
267 Lisp_Object cache
, val
;
268 struct ftfont_cache_data
*cache_data
;
270 cache
= assoc_no_quit (key
, ft_face_cache
);
273 cache_data
= xmalloc (sizeof (struct ftfont_cache_data
));
274 cache_data
->ft_face
= NULL
;
275 cache_data
->fc_charset
= NULL
;
276 val
= make_save_value (NULL
, 0);
277 XSAVE_VALUE (val
)->integer
= 0;
278 XSAVE_VALUE (val
)->pointer
= cache_data
;
279 cache
= Fcons (key
, val
);
280 ft_face_cache
= Fcons (cache
, ft_face_cache
);
285 cache_data
= XSAVE_VALUE (val
)->pointer
;
287 if (for_face
? ! cache_data
->ft_face
: ! cache_data
->fc_charset
)
289 char *filename
= (char *) SDATA (XCAR (key
));
290 int index
= XINT (XCDR (key
));
295 && FT_Init_FreeType (&ft_library
) != 0)
297 if (FT_New_Face (ft_library
, filename
, index
, &cache_data
->ft_face
)
308 pat
= FcPatternBuild (0, FC_FILE
, FcTypeString
, (FcChar8
*) filename
,
309 FC_INDEX
, FcTypeInteger
, index
, NULL
);
310 objset
= FcObjectSetBuild (FC_CHARSET
, NULL
);
311 fontset
= FcFontList (NULL
, pat
, objset
);
312 xassert (fontset
&& fontset
->nfont
> 0);
313 if (FcPatternGetCharSet (fontset
->fonts
[0], FC_CHARSET
, 0, &charset
)
315 cache_data
->fc_charset
= FcCharSetCopy (charset
);
317 cache_data
->fc_charset
= FcCharSetCreate ();
318 FcFontSetDestroy (fontset
);
319 FcObjectSetDestroy (objset
);
320 FcPatternDestroy (pat
);
327 ftfont_get_fc_charset (entity
)
330 Lisp_Object val
, cache
;
331 struct ftfont_cache_data
*cache_data
;
333 val
= assq_no_quit (QCfont_entity
, AREF (entity
, FONT_EXTRA_INDEX
));
334 xassert (CONSP (val
));
336 cache
= ftfont_lookup_cache (val
, 0);
338 cache_data
= XSAVE_VALUE (val
)->pointer
;
339 return cache_data
->fc_charset
;
344 ftfont_get_otf (ftfont_info
)
345 struct ftfont_info
*ftfont_info
;
349 if (ftfont_info
->otf
)
350 return ftfont_info
->otf
;
351 if (! ftfont_info
->maybe_otf
)
353 otf
= OTF_open_ft_face (ftfont_info
->ft_size
->face
);
354 if (! otf
|| OTF_get_table (otf
, "head") < 0)
358 ftfont_info
->maybe_otf
= 0;
361 ftfont_info
->otf
= otf
;
364 #endif /* HAVE_LIBOTF */
366 static Lisp_Object ftfont_get_cache
P_ ((FRAME_PTR
));
367 static Lisp_Object ftfont_list
P_ ((Lisp_Object
, Lisp_Object
));
368 static Lisp_Object ftfont_match
P_ ((Lisp_Object
, Lisp_Object
));
369 static Lisp_Object ftfont_list_family
P_ ((Lisp_Object
));
370 static Lisp_Object ftfont_open
P_ ((FRAME_PTR
, Lisp_Object
, int));
371 static void ftfont_close
P_ ((FRAME_PTR
, struct font
*));
372 static int ftfont_has_char
P_ ((Lisp_Object
, int));
373 static unsigned ftfont_encode_char
P_ ((struct font
*, int));
374 static int ftfont_text_extents
P_ ((struct font
*, unsigned *, int,
375 struct font_metrics
*));
376 static int ftfont_get_bitmap
P_ ((struct font
*, unsigned,
377 struct font_bitmap
*, int));
378 static int ftfont_anchor_point
P_ ((struct font
*, unsigned, int,
380 static Lisp_Object ftfont_otf_capability
P_ ((struct font
*));
381 static Lisp_Object ftfont_shape
P_ ((Lisp_Object
));
383 struct font_driver ftfont_driver
=
386 0, /* case insensitive */
394 /* We can't draw a text without device dependent functions. */
400 /* We can't draw a text without device dependent functions. */
408 ftfont_otf_capability
,
409 #else /* not HAVE_LIBOTF */
411 #endif /* not HAVE_LIBOTF */
415 #if defined (HAVE_M17N_FLT) && defined (HAVE_LIBOTF)
417 #else /* not (HAVE_M17N_FLT && HAVE_LIBOTF) */
419 #endif /* not (HAVE_M17N_FLT && HAVE_LIBOTF) */
422 extern Lisp_Object QCname
;
428 return freetype_font_cache
;
432 ftfont_get_charset (registry
)
433 Lisp_Object registry
;
435 char *str
= (char *) SDATA (SYMBOL_NAME (registry
));
436 char *re
= alloca (SBYTES (SYMBOL_NAME (registry
)) * 2 + 1);
440 for (i
= j
= 0; i
< SBYTES (SYMBOL_NAME (registry
)); i
++, j
++)
444 else if (str
[i
] == '*')
451 regexp
= make_unibyte_string (re
, j
);
452 for (i
= 0; fc_charset_table
[i
].name
; i
++)
453 if (fast_c_string_match_ignore_case (regexp
, fc_charset_table
[i
].name
) >= 0)
455 if (! fc_charset_table
[i
].name
)
457 if (! fc_charset_table
[i
].fc_charset
)
459 FcCharSet
*charset
= FcCharSetCreate ();
460 int *uniquifier
= fc_charset_table
[i
].uniquifier
;
464 for (j
= 0; uniquifier
[j
]; j
++)
465 if (! FcCharSetAddChar (charset
, uniquifier
[j
]))
467 FcCharSetDestroy (charset
);
470 fc_charset_table
[i
].fc_charset
= charset
;
478 unsigned int script_tag
, langsys_tag
;
480 unsigned int *features
[2];
483 #define OTF_SYM_TAG(SYM, TAG) \
485 unsigned char *p = SDATA (SYMBOL_NAME (SYM)); \
486 TAG = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]; \
489 #define OTF_TAG_STR(TAG, P) \
491 (P)[0] = (char) (TAG >> 24); \
492 (P)[1] = (char) ((TAG >> 16) & 0xFF); \
493 (P)[2] = (char) ((TAG >> 8) & 0xFF); \
494 (P)[3] = (char) (TAG & 0xFF); \
498 #define OTF_TAG_SYM(SYM, TAG) \
502 OTF_TAG_STR (TAG, str); \
503 (SYM) = font_intern_prop (str, 4, 1); \
507 static struct OpenTypeSpec
*
508 ftfont_get_open_type_spec (Lisp_Object otf_spec
)
510 struct OpenTypeSpec
*spec
= malloc (sizeof (struct OpenTypeSpec
));
516 spec
->script
= XCAR (otf_spec
);
517 if (! NILP (spec
->script
))
519 OTF_SYM_TAG (spec
->script
, spec
->script_tag
);
520 val
= assq_no_quit (spec
->script
, Votf_script_alist
);
521 if (CONSP (val
) && SYMBOLP (XCDR (val
)))
522 spec
->script
= XCDR (val
);
527 spec
->script_tag
= 0x44464C54; /* "DFLT" */
528 otf_spec
= XCDR (otf_spec
);
529 val
= XCAR (otf_spec
);
531 OTF_SYM_TAG (val
, spec
->langsys_tag
);
533 spec
->langsys_tag
= 0;
534 spec
->nfeatures
[0] = spec
->nfeatures
[1] = 0;
535 for (i
= 0; i
< 2; i
++)
539 otf_spec
= XCDR (otf_spec
);
542 val
= XCAR (otf_spec
);
546 spec
->features
[i
] = malloc (sizeof (int) * XINT (len
));
547 if (! spec
->features
[i
])
549 if (i
> 0 && spec
->features
[0])
550 free (spec
->features
[0]);
554 for (j
= 0, negative
= 0; CONSP (val
); val
= XCDR (val
))
556 if (NILP (XCAR (val
)))
562 OTF_SYM_TAG (XCAR (val
), tag
);
563 spec
->features
[i
][j
++] = negative
? tag
& 0x80000000 : tag
;
566 spec
->nfeatures
[i
] = j
;
571 static FcPattern
*ftfont_spec_pattern
P_ ((Lisp_Object
, char *,
572 struct OpenTypeSpec
**));
575 ftfont_spec_pattern (spec
, otlayout
, otspec
)
578 struct OpenTypeSpec
**otspec
;
580 Lisp_Object tmp
, extra
;
581 FcPattern
*pattern
= NULL
;
582 FcCharSet
*charset
= NULL
;
583 FcLangSet
*langset
= NULL
;
587 Lisp_Object script
= Qnil
;
588 Lisp_Object registry
;
591 if (! NILP (AREF (spec
, FONT_ADSTYLE_INDEX
))
592 && SBYTES (SYMBOL_NAME (AREF (spec
, FONT_ADSTYLE_INDEX
))) > 0)
593 /* Fontconfig doesn't support adstyle property. */
595 if ((n
= FONT_SLANT_NUMERIC (spec
)) >= 0
597 /* Fontconfig doesn't support reverse-italic/obligue. */
600 if (INTEGERP (AREF (spec
, FONT_DPI_INDEX
)))
601 dpi
= XINT (AREF (spec
, FONT_DPI_INDEX
));
602 if (INTEGERP (AREF (spec
, FONT_AVGWIDTH_INDEX
))
603 && XINT (AREF (spec
, FONT_AVGWIDTH_INDEX
)) == 0)
606 registry
= AREF (spec
, FONT_REGISTRY_INDEX
);
608 || EQ (registry
, Qascii_0
)
609 || EQ (registry
, Qiso10646_1
)
610 || EQ (registry
, Qunicode_bmp
)
611 || EQ (registry
, Qunicode_sip
))
617 fc_charset_idx
= ftfont_get_charset (registry
);
618 if (fc_charset_idx
< 0)
620 charset
= fc_charset_table
[fc_charset_idx
].fc_charset
;
621 lang
= (FcChar8
*) fc_charset_table
[fc_charset_idx
].lang
;
624 langset
= FcLangSetCreate ();
627 FcLangSetAdd (langset
, lang
);
632 for (extra
= AREF (spec
, FONT_EXTRA_INDEX
);
633 CONSP (extra
); extra
= XCDR (extra
))
635 Lisp_Object key
, val
;
637 key
= XCAR (XCAR (extra
)), val
= XCDR (XCAR (extra
));
640 else if (EQ (key
, QClang
))
643 langset
= FcLangSetCreate ();
648 if (! FcLangSetAdd (langset
, SYMBOL_FcChar8 (val
)))
652 for (; CONSP (val
); val
= XCDR (val
))
653 if (SYMBOLP (XCAR (val
))
654 && ! FcLangSetAdd (langset
, SYMBOL_FcChar8 (XCAR (val
))))
657 else if (EQ (key
, QCotf
))
659 *otspec
= ftfont_get_open_type_spec (val
);
662 strcat (otlayout
, "otlayout:");
663 OTF_TAG_STR ((*otspec
)->script_tag
, otlayout
+ 9);
664 script
= (*otspec
)->script
;
666 else if (EQ (key
, QCscript
))
668 else if (EQ (key
, QCscalable
))
669 scalable
= ! NILP (val
);
672 if (! NILP (script
) && ! charset
)
674 Lisp_Object chars
= assq_no_quit (script
, Vscript_representative_chars
);
676 if (CONSP (chars
) && CONSP (CDR (chars
)))
678 charset
= FcCharSetCreate ();
681 for (chars
= XCDR (chars
); CONSP (chars
); chars
= XCDR (chars
))
682 if (CHARACTERP (XCAR (chars
))
683 && ! FcCharSetAddChar (charset
, XUINT (XCAR (chars
))))
688 pattern
= FcPatternCreate ();
691 tmp
= AREF (spec
, FONT_FOUNDRY_INDEX
);
693 && ! FcPatternAddString (pattern
, FC_FOUNDRY
, SYMBOL_FcChar8 (tmp
)))
695 tmp
= AREF (spec
, FONT_FAMILY_INDEX
);
697 && ! FcPatternAddString (pattern
, FC_FAMILY
, SYMBOL_FcChar8 (tmp
)))
700 && ! FcPatternAddCharSet (pattern
, FC_CHARSET
, charset
))
703 && ! FcPatternAddLangSet (pattern
, FC_LANG
, langset
))
706 && ! FcPatternAddDouble (pattern
, FC_DPI
, dpi
))
709 && ! FcPatternAddBool (pattern
, FC_SCALABLE
, scalable
? FcTrue
: FcFalse
))
715 /* We come here because of unexpected error in fontconfig API call
716 (usually insufficient memory). */
719 FcPatternDestroy (pattern
);
724 if ((*otspec
)->nfeatures
[0] > 0)
725 free ((*otspec
)->features
[0]);
726 if ((*otspec
)->nfeatures
[1] > 0)
727 free ((*otspec
)->features
[1]);
733 if (langset
) FcLangSetDestroy (langset
);
734 if (charset
&& fc_charset_idx
< 0) FcCharSetDestroy (charset
);
739 ftfont_list (frame
, spec
)
740 Lisp_Object frame
, spec
;
742 Lisp_Object val
= Qnil
, family
;
745 FcFontSet
*fontset
= NULL
;
746 FcObjectSet
*objset
= NULL
;
748 Lisp_Object chars
= Qnil
;
750 char otlayout
[15]; /* For "otlayout:XXXX" */
751 struct OpenTypeSpec
*otspec
= NULL
;
754 if (! fc_initialized
)
760 pattern
= ftfont_spec_pattern (spec
, otlayout
, &otspec
);
763 if (FcPatternGetCharSet (pattern
, FC_CHARSET
, 0, &charset
) != FcResultMatch
)
765 val
= assq_no_quit (QCscript
, AREF (spec
, FONT_EXTRA_INDEX
));
768 val
= assq_no_quit (XCDR (val
), Vscript_representative_chars
);
769 if (CONSP (val
) && VECTORP (XCDR (val
)))
774 if (INTEGERP (AREF (spec
, FONT_SPACING_INDEX
)))
775 spacing
= XINT (AREF (spec
, FONT_SPACING_INDEX
));
776 family
= AREF (spec
, FONT_FAMILY_INDEX
);
779 Lisp_Object resolved
;
781 resolved
= ftfont_resolve_generic_family (family
, pattern
);
782 if (! NILP (resolved
))
784 FcPatternDel (pattern
, FC_FAMILY
);
785 if (! FcPatternAddString (pattern
, FC_FAMILY
,
786 SYMBOL_FcChar8 (resolved
)))
791 objset
= FcObjectSetBuild (FC_FOUNDRY
, FC_FAMILY
, FC_WEIGHT
, FC_SLANT
,
792 FC_WIDTH
, FC_PIXEL_SIZE
, FC_SPACING
, FC_SCALABLE
,
796 #endif /* FC_CAPABILITY */
804 FcObjectSetAdd (objset
, FC_CHARSET
);
806 fontset
= FcFontList (NULL
, pattern
, objset
);
807 if (! fontset
|| fontset
->nfont
== 0)
810 /* Need fix because this finds any fonts. */
811 if (fontset
->nfont
== 0 && ! NILP (family
))
813 /* Try maching with configuration. For instance, the
814 configuration may specify "Nimbus Mono L" as an alias of
816 FcPattern
*pat
= FcPatternBuild (0, FC_FAMILY
, FcTypeString
,
817 SYMBOL_FcChar8 (family
), NULL
);
820 if (FcConfigSubstitute (NULL
, pat
, FcMatchPattern
) == FcTrue
)
823 FcPatternGetString (pat
, FC_FAMILY
, i
, &fam
) == FcResultMatch
;
826 FcPatternDel (pattern
, FC_FAMILY
);
827 FcPatternAddString (pattern
, FC_FAMILY
, fam
);
828 FcFontSetDestroy (fontset
);
829 fontset
= FcFontList (NULL
, pattern
, objset
);
830 if (fontset
->nfont
> 0)
836 for (i
= 0; i
< fontset
->nfont
; i
++)
844 if ((FcPatternGetInteger (fontset
->fonts
[i
], FC_SPACING
, 0, &this)
855 if (FcPatternGetString (fontset
->fonts
[i
], FC_CAPABILITY
, 0, &this)
857 || ! strstr ((char *) this, otlayout
))
860 #endif /* FC_CAPABILITY */
867 if (FcPatternGetString (fontset
->fonts
[i
], FC_FILE
, 0, &file
)
870 otf
= OTF_open ((char *) file
);
873 if (OTF_check_features (otf
, 1,
874 otspec
->script_tag
, otspec
->langsys_tag
,
876 otspec
->nfeatures
[0]) != 1
877 || OTF_check_features (otf
, 0,
878 otspec
->script_tag
, otspec
->langsys_tag
,
880 otspec
->nfeatures
[1]) != 1)
883 #endif /* HAVE_LIBOTF */
888 if (FcPatternGetCharSet (fontset
->fonts
[i
], FC_CHARSET
, 0, &charset
)
891 for (j
= 0; j
< ASIZE (chars
); j
++)
892 if (NATNUMP (AREF (chars
, j
))
893 && FcCharSetHasChar (charset
, XFASTINT (AREF (chars
, j
))))
895 if (j
== ASIZE (chars
))
898 entity
= ftfont_pattern_entity (fontset
->fonts
[i
],
899 AREF (spec
, FONT_EXTRA_INDEX
));
901 val
= Fcons (entity
, val
);
903 val
= Fnreverse (val
);
907 /* We come here because of unexpected error in fontconfig API call
908 (usually insufficient memory). */
912 font_add_log ("ftfont-list", spec
, val
);
913 if (objset
) FcObjectSetDestroy (objset
);
914 if (fontset
) FcFontSetDestroy (fontset
);
915 if (pattern
) FcPatternDestroy (pattern
);
920 ftfont_match (frame
, spec
)
921 Lisp_Object frame
, spec
;
924 FcPattern
*pattern
, *match
= NULL
;
926 char otlayout
[15]; /* For "otlayout:XXXX" */
927 struct OpenTypeSpec
*otspec
= NULL
;
929 if (! fc_initialized
)
935 pattern
= ftfont_spec_pattern (spec
, otlayout
, &otspec
);
939 if (INTEGERP (AREF (spec
, FONT_SIZE_INDEX
)))
943 value
.type
= FcTypeDouble
;
944 value
.u
.d
= XINT (AREF (spec
, FONT_SIZE_INDEX
));
945 FcPatternAdd (pattern
, FC_PIXEL_SIZE
, value
, FcFalse
);
947 if (FcConfigSubstitute (NULL
, pattern
, FcMatchPattern
) == FcTrue
)
949 FcDefaultSubstitute (pattern
);
950 match
= FcFontMatch (NULL
, pattern
, &result
);
953 entity
= ftfont_pattern_entity (match
, AREF (spec
, FONT_EXTRA_INDEX
));
954 FcPatternDestroy (match
);
955 if (! NILP (AREF (spec
, FONT_FAMILY_INDEX
))
956 && NILP (assq_no_quit (AREF (spec
, FONT_FAMILY_INDEX
),
957 ftfont_generic_family_list
))
958 && NILP (Fstring_equal (AREF (spec
, FONT_FAMILY_INDEX
),
959 AREF (entity
, FONT_FAMILY_INDEX
))))
963 FcPatternDestroy (pattern
);
965 font_add_log ("ftfont-match", spec
, entity
);
970 ftfont_list_family (frame
)
974 FcPattern
*pattern
= NULL
;
975 FcFontSet
*fontset
= NULL
;
976 FcObjectSet
*objset
= NULL
;
979 if (! fc_initialized
)
985 pattern
= FcPatternCreate ();
988 objset
= FcObjectSetBuild (FC_FAMILY
, NULL
);
991 fontset
= FcFontList (NULL
, pattern
, objset
);
996 for (i
= 0; i
< fontset
->nfont
; i
++)
998 FcPattern
*pat
= fontset
->fonts
[i
];
1001 if (FcPatternGetString (pat
, FC_FAMILY
, 0, &str
) == FcResultMatch
)
1002 list
= Fcons (intern ((char *) str
), list
);
1006 if (objset
) FcObjectSetDestroy (objset
);
1007 if (fontset
) FcFontSetDestroy (fontset
);
1008 if (pattern
) FcPatternDestroy (pattern
);
1015 ftfont_open (f
, entity
, pixel_size
)
1020 struct ftfont_info
*ftfont_info
;
1022 struct ftfont_cache_data
*cache_data
;
1026 Lisp_Object val
, filename
, index
, cache
, font_object
;
1033 val
= assq_no_quit (QCfont_entity
, AREF (entity
, FONT_EXTRA_INDEX
));
1037 cache
= ftfont_lookup_cache (val
, 1);
1040 filename
= XCAR (val
);
1043 cache_data
= XSAVE_VALUE (XCDR (cache
))->pointer
;
1044 ft_face
= cache_data
->ft_face
;
1045 if (XSAVE_VALUE (val
)->integer
> 0)
1047 /* FT_Face in this cache is already used by the different size. */
1048 if (FT_New_Size (ft_face
, &ft_size
) != 0)
1050 if (FT_Activate_Size (ft_size
) != 0)
1052 FT_Done_Size (ft_size
);
1056 XSAVE_VALUE (val
)->integer
++;
1057 size
= XINT (AREF (entity
, FONT_SIZE_INDEX
));
1060 if (FT_Set_Pixel_Sizes (ft_face
, size
, size
) != 0)
1062 if (XSAVE_VALUE (val
)->integer
== 0)
1063 FT_Done_Face (ft_face
);
1067 font_object
= font_make_object (VECSIZE (struct ftfont_info
), entity
, size
);
1068 ASET (font_object
, FONT_TYPE_INDEX
, Qfreetype
);
1069 len
= font_unparse_xlfd (entity
, size
, name
, 256);
1071 ASET (font_object
, FONT_NAME_INDEX
, make_unibyte_string (name
, len
));
1072 len
= font_unparse_fcname (entity
, size
, name
, 256);
1074 ASET (font_object
, FONT_FULLNAME_INDEX
, make_unibyte_string (name
, len
));
1076 ASET (font_object
, FONT_FULLNAME_INDEX
,
1077 AREF (font_object
, FONT_NAME_INDEX
));
1078 ASET (font_object
, FONT_FILE_INDEX
, filename
);
1079 ASET (font_object
, FONT_FORMAT_INDEX
, ftfont_font_format (NULL
, filename
));
1080 font
= XFONT_OBJECT (font_object
);
1081 ftfont_info
= (struct ftfont_info
*) font
;
1082 ftfont_info
->ft_size
= ft_face
->size
;
1083 ftfont_info
->index
= XINT (index
);
1085 ftfont_info
->maybe_otf
= ft_face
->face_flags
& FT_FACE_FLAG_SFNT
;
1086 ftfont_info
->otf
= NULL
;
1087 #endif /* HAVE_LIBOTF */
1088 font
->pixel_size
= size
;
1089 font
->driver
= &ftfont_driver
;
1090 font
->encoding_charset
= font
->repertory_charset
= -1;
1092 upEM
= ft_face
->units_per_EM
;
1093 scalable
= (INTEGERP (AREF (entity
, FONT_AVGWIDTH_INDEX
))
1094 && XINT (AREF (entity
, FONT_AVGWIDTH_INDEX
)) == 0);
1097 font
->ascent
= ft_face
->ascender
* size
/ upEM
;
1098 font
->descent
= - ft_face
->descender
* size
/ upEM
;
1099 font
->height
= ft_face
->height
* size
/ upEM
;
1103 font
->ascent
= ft_face
->size
->metrics
.ascender
>> 6;
1104 font
->descent
= - ft_face
->size
->metrics
.descender
>> 6;
1105 font
->height
= ft_face
->size
->metrics
.height
>> 6;
1107 if (INTEGERP (AREF (entity
, FONT_SPACING_INDEX
)))
1108 spacing
= XINT (AREF (entity
, FONT_SPACING_INDEX
));
1110 spacing
= FC_PROPORTIONAL
;
1111 if (spacing
!= FC_PROPORTIONAL
)
1112 font
->min_width
= font
->average_width
= font
->space_width
1113 = (scalable
? ft_face
->max_advance_width
* size
/ upEM
1114 : ft_face
->size
->metrics
.max_advance
>> 6);
1119 font
->min_width
= font
->average_width
= font
->space_width
= 0;
1120 for (i
= 32, n
= 0; i
< 127; i
++)
1121 if (FT_Load_Char (ft_face
, i
, FT_LOAD_DEFAULT
) != 0)
1123 int this_width
= ft_face
->glyph
->metrics
.horiAdvance
>> 6;
1126 && (! font
->min_width
|| font
->min_width
> this_width
))
1127 font
->min_width
= this_width
;
1129 font
->space_width
= this_width
;
1130 font
->average_width
+= this_width
;
1134 font
->average_width
/= n
;
1137 font
->baseline_offset
= 0;
1138 font
->relative_compose
= 0;
1139 font
->default_ascent
= 0;
1140 font
->vertical_centering
= 0;
1143 font
->underline_position
= -ft_face
->underline_position
* size
/ upEM
;
1144 font
->underline_thickness
= -ft_face
->underline_thickness
* size
/ upEM
;
1148 font
->underline_position
= -1;
1149 font
->underline_thickness
= 0;
1156 ftfont_close (f
, font
)
1160 struct ftfont_info
*ftfont_info
= (struct ftfont_info
*) font
;
1161 Lisp_Object val
, cache
;
1163 val
= Fcons (font
->props
[FONT_FILE_INDEX
], make_number (ftfont_info
->index
));
1164 cache
= ftfont_lookup_cache (val
, 1);
1165 xassert (CONSP (cache
));
1167 (XSAVE_VALUE (val
)->integer
)--;
1168 if (XSAVE_VALUE (val
)->integer
== 0)
1170 struct ftfont_cache_data
*cache_data
= XSAVE_VALUE (val
)->pointer
;
1172 FT_Done_Face (cache_data
->ft_face
);
1174 if (ftfont_info
->otf
)
1175 OTF_close (ftfont_info
->otf
);
1177 cache_data
->ft_face
= NULL
;
1180 FT_Done_Size (ftfont_info
->ft_size
);
1184 ftfont_has_char (entity
, c
)
1188 FcCharSet
*charset
= ftfont_get_fc_charset (entity
);
1190 return (FcCharSetHasChar (charset
, c
) == FcTrue
);
1194 ftfont_encode_char (font
, c
)
1198 struct ftfont_info
*ftfont_info
= (struct ftfont_info
*) font
;
1199 FT_Face ft_face
= ftfont_info
->ft_size
->face
;
1200 FT_ULong charcode
= c
;
1201 FT_UInt code
= FT_Get_Char_Index (ft_face
, charcode
);
1203 return (code
> 0 ? code
: FONT_INVALID_CODE
);
1207 ftfont_text_extents (font
, code
, nglyphs
, metrics
)
1211 struct font_metrics
*metrics
;
1213 struct ftfont_info
*ftfont_info
= (struct ftfont_info
*) font
;
1214 FT_Face ft_face
= ftfont_info
->ft_size
->face
;
1218 if (ftfont_info
->ft_size
!= ft_face
->size
)
1219 FT_Activate_Size (ftfont_info
->ft_size
);
1221 bzero (metrics
, sizeof (struct font_metrics
));
1222 for (i
= 0, first
= 1; i
< nglyphs
; i
++)
1224 if (FT_Load_Glyph (ft_face
, code
[i
], FT_LOAD_DEFAULT
) == 0)
1226 FT_Glyph_Metrics
*m
= &ft_face
->glyph
->metrics
;
1232 metrics
->lbearing
= m
->horiBearingX
>> 6;
1233 metrics
->rbearing
= (m
->horiBearingX
+ m
->width
) >> 6;
1234 metrics
->ascent
= m
->horiBearingY
>> 6;
1235 metrics
->descent
= (m
->horiBearingY
+ m
->height
) >> 6;
1241 if (metrics
->lbearing
> width
+ (m
->horiBearingX
>> 6))
1242 metrics
->lbearing
= width
+ (m
->horiBearingX
>> 6);
1243 if (metrics
->rbearing
1244 < width
+ ((m
->horiBearingX
+ m
->width
) >> 6))
1246 = width
+ ((m
->horiBearingX
+ m
->width
) >> 6);
1247 if (metrics
->ascent
< (m
->horiBearingY
>> 6))
1248 metrics
->ascent
= m
->horiBearingY
>> 6;
1249 if (metrics
->descent
> ((m
->horiBearingY
+ m
->height
) >> 6))
1250 metrics
->descent
= (m
->horiBearingY
+ m
->height
) >> 6;
1252 width
+= m
->horiAdvance
>> 6;
1256 width
+= font
->space_width
;
1260 metrics
->width
= width
;
1266 ftfont_get_bitmap (font
, code
, bitmap
, bits_per_pixel
)
1269 struct font_bitmap
*bitmap
;
1272 struct ftfont_info
*ftfont_info
= (struct ftfont_info
*) font
;
1273 FT_Face ft_face
= ftfont_info
->ft_size
->face
;
1274 FT_Int32 load_flags
= FT_LOAD_RENDER
;
1276 if (ftfont_info
->ft_size
!= ft_face
->size
)
1277 FT_Activate_Size (ftfont_info
->ft_size
);
1278 if (bits_per_pixel
== 1)
1280 #ifdef FT_LOAD_TARGET_MONO
1281 load_flags
|= FT_LOAD_TARGET_MONO
;
1283 load_flags
|= FT_LOAD_MONOCHROME
;
1286 else if (bits_per_pixel
!= 8)
1287 /* We don't support such a rendering. */
1290 if (FT_Load_Glyph (ft_face
, code
, load_flags
) != 0)
1292 bitmap
->bits_per_pixel
1293 = (ft_face
->glyph
->bitmap
.pixel_mode
== FT_PIXEL_MODE_MONO
? 1
1294 : ft_face
->glyph
->bitmap
.pixel_mode
== FT_PIXEL_MODE_GRAY
? 8
1295 : ft_face
->glyph
->bitmap
.pixel_mode
== FT_PIXEL_MODE_LCD
? 8
1296 : ft_face
->glyph
->bitmap
.pixel_mode
== FT_PIXEL_MODE_LCD_V
? 8
1298 if (bitmap
->bits_per_pixel
< 0)
1299 /* We don't suport that kind of pixel mode. */
1301 bitmap
->rows
= ft_face
->glyph
->bitmap
.rows
;
1302 bitmap
->width
= ft_face
->glyph
->bitmap
.width
;
1303 bitmap
->pitch
= ft_face
->glyph
->bitmap
.pitch
;
1304 bitmap
->buffer
= ft_face
->glyph
->bitmap
.buffer
;
1305 bitmap
->left
= ft_face
->glyph
->bitmap_left
;
1306 bitmap
->top
= ft_face
->glyph
->bitmap_top
;
1307 bitmap
->advance
= ft_face
->glyph
->metrics
.horiAdvance
>> 6;
1308 bitmap
->extra
= NULL
;
1314 ftfont_anchor_point (font
, code
, index
, x
, y
)
1320 struct ftfont_info
*ftfont_info
= (struct ftfont_info
*) font
;
1321 FT_Face ft_face
= ftfont_info
->ft_size
->face
;
1323 if (ftfont_info
->ft_size
!= ft_face
->size
)
1324 FT_Activate_Size (ftfont_info
->ft_size
);
1325 if (FT_Load_Glyph (ft_face
, code
, FT_LOAD_DEFAULT
) != 0)
1327 if (ft_face
->glyph
->format
!= FT_GLYPH_FORMAT_OUTLINE
)
1329 if (index
>= ft_face
->glyph
->outline
.n_points
)
1331 *x
= ft_face
->glyph
->outline
.points
[index
].x
;
1332 *y
= ft_face
->glyph
->outline
.points
[index
].y
;
1339 ftfont_otf_features (gsub_gpos
)
1340 OTF_GSUB_GPOS
*gsub_gpos
;
1342 Lisp_Object scripts
, langsyses
, features
, sym
;
1345 for (scripts
= Qnil
, i
= gsub_gpos
->ScriptList
.ScriptCount
- 1; i
>= 0; i
--)
1347 OTF_Script
*otf_script
= gsub_gpos
->ScriptList
.Script
+ i
;
1349 for (langsyses
= Qnil
, j
= otf_script
->LangSysCount
- 1; j
>= -1; j
--)
1351 OTF_LangSys
*otf_langsys
;
1354 otf_langsys
= otf_script
->LangSys
+ j
;
1355 else if (otf_script
->DefaultLangSysOffset
)
1356 otf_langsys
= &otf_script
->DefaultLangSys
;
1360 for (features
= Qnil
, k
= otf_langsys
->FeatureCount
- 1; k
>= 0; k
--)
1362 OTF_TAG_SYM (sym
, gsub_gpos
->FeatureList
.Feature
[k
].FeatureTag
);
1363 features
= Fcons (sym
, features
);
1366 OTF_TAG_SYM (sym
, otf_script
->LangSysRecord
[j
].LangSysTag
);
1369 langsyses
= Fcons (Fcons (sym
, features
), langsyses
);
1372 OTF_TAG_SYM (sym
, gsub_gpos
->ScriptList
.Script
[i
].ScriptTag
);
1373 scripts
= Fcons (Fcons (sym
, langsyses
), scripts
);
1381 ftfont_otf_capability (font
)
1384 struct ftfont_info
*ftfont_info
= (struct ftfont_info
*) font
;
1385 OTF
*otf
= ftfont_get_otf (ftfont_info
);
1386 Lisp_Object gsub_gpos
;
1390 gsub_gpos
= Fcons (Qnil
, Qnil
);
1391 if (OTF_get_table (otf
, "GSUB") == 0)
1392 XSETCAR (gsub_gpos
, ftfont_otf_features (otf
->gsub
));
1393 if (OTF_get_table (otf
, "GPOS") == 0)
1394 XSETCDR (gsub_gpos
, ftfont_otf_features (otf
->gpos
));
1398 #ifdef HAVE_M17N_FLT
1409 ftfont_get_glyph_id (font
, gstring
, from
, to
)
1411 MFLTGlyphString
*gstring
;
1414 struct MFLTFontFT
*flt_font_ft
= (struct MFLTFontFT
*) font
;
1415 FT_Face ft_face
= flt_font_ft
->ft_face
;
1418 for (g
= gstring
->glyphs
+ from
; from
< to
; g
++, from
++)
1421 FT_UInt code
= FT_Get_Char_Index (ft_face
, g
->code
);
1423 g
->code
= code
> 0 ? code
: FONT_INVALID_CODE
;
1430 ftfont_get_metrics (font
, gstring
, from
, to
)
1432 MFLTGlyphString
*gstring
;
1435 struct MFLTFontFT
*flt_font_ft
= (struct MFLTFontFT
*) font
;
1436 FT_Face ft_face
= flt_font_ft
->ft_face
;
1439 for (g
= gstring
->glyphs
+ from
; from
< to
; g
++, from
++)
1442 if (g
->code
!= FONT_INVALID_CODE
)
1444 FT_Glyph_Metrics
*m
;
1446 if (FT_Load_Glyph (ft_face
, g
->code
, FT_LOAD_DEFAULT
) != 0)
1448 m
= &ft_face
->glyph
->metrics
;
1450 g
->lbearing
= m
->horiBearingX
;
1451 g
->rbearing
= m
->horiBearingX
+ m
->width
;
1452 g
->ascent
= m
->horiBearingY
;
1453 g
->descent
= m
->height
- m
->horiBearingY
;
1454 g
->xadv
= m
->horiAdvance
;
1459 g
->rbearing
= g
->xadv
= flt_font_ft
->font
->space_width
<< 6;
1460 g
->ascent
= flt_font_ft
->font
->ascent
<< 6;
1461 g
->descent
= flt_font_ft
->font
->descent
<< 6;
1470 ftfont_check_otf (MFLTFont
*font
, MFLTOtfSpec
*spec
)
1472 struct MFLTFontFT
*flt_font_ft
= (struct MFLTFontFT
*) font
;
1473 OTF
*otf
= flt_font_ft
->otf
;
1477 for (i
= 0; i
< 2; i
++)
1479 if (! spec
->features
[i
])
1481 for (n
= 0; spec
->features
[i
][n
]; n
++);
1482 tags
= alloca (sizeof (OTF_Tag
) * n
);
1483 for (n
= 0, negative
= 0; spec
->features
[i
][n
]; n
++)
1485 if (spec
->features
[i
][n
] == 0xFFFFFFFF)
1488 tags
[n
- 1] = spec
->features
[i
][n
] | 0x80000000;
1490 tags
[n
] = spec
->features
[i
][n
];
1492 if (n
- negative
> 0
1493 && OTF_check_features (otf
, i
== 0, spec
->script
, spec
->langsys
,
1494 tags
, n
- negative
) != 1)
1500 #define DEVICE_DELTA(table, size) \
1501 (((size) >= (table).StartSize && (size) <= (table).EndSize) \
1502 ? (table).DeltaValue[(size) - (table).StartSize] << 6 \
1506 adjust_anchor (FT_Face ft_face
, OTF_Anchor
*anchor
,
1507 unsigned code
, int x_ppem
, int y_ppem
, int *x
, int *y
)
1509 if (anchor
->AnchorFormat
== 2)
1511 FT_Outline
*outline
;
1512 int ap
= anchor
->f
.f1
.AnchorPoint
;
1514 FT_Load_Glyph (ft_face
, (FT_UInt
) code
, FT_LOAD_MONOCHROME
);
1515 outline
= &ft_face
->glyph
->outline
;
1516 if (ap
< outline
->n_points
)
1518 *x
= outline
->points
[ap
].x
<< 6;
1519 *y
= outline
->points
[ap
].y
<< 6;
1522 else if (anchor
->AnchorFormat
== 3)
1524 if (anchor
->f
.f2
.XDeviceTable
.offset
1525 && anchor
->f
.f2
.XDeviceTable
.DeltaValue
)
1526 *x
+= DEVICE_DELTA (anchor
->f
.f2
.XDeviceTable
, x_ppem
);
1527 if (anchor
->f
.f2
.YDeviceTable
.offset
1528 && anchor
->f
.f2
.YDeviceTable
.DeltaValue
)
1529 *y
+= DEVICE_DELTA (anchor
->f
.f2
.YDeviceTable
, y_ppem
);
1533 static OTF_GlyphString otf_gstring
;
1536 ftfont_drive_otf (font
, spec
, in
, from
, to
, out
, adjustment
)
1539 MFLTGlyphString
*in
;
1541 MFLTGlyphString
*out
;
1542 MFLTGlyphAdjustment
*adjustment
;
1544 struct MFLTFontFT
*flt_font_ft
= (struct MFLTFontFT
*) font
;
1545 FT_Face ft_face
= flt_font_ft
->ft_face
;
1546 OTF
*otf
= flt_font_ft
->otf
;
1547 int len
= to
- from
;
1550 char script
[5], *langsys
= NULL
;
1551 char *gsub_features
= NULL
, *gpos_features
= NULL
;
1555 OTF_tag_name (spec
->script
, script
);
1558 langsys
= alloca (5);
1559 OTF_tag_name (spec
->langsys
, langsys
);
1561 for (i
= 0; i
< 2; i
++)
1565 if (spec
->features
[i
] && spec
->features
[i
][1] != 0xFFFFFFFF)
1567 for (j
= 0; spec
->features
[i
][j
]; j
++);
1569 p
= gsub_features
= alloca (6 * j
);
1571 p
= gpos_features
= alloca (6 * j
);
1572 for (j
= 0; spec
->features
[i
][j
]; j
++)
1574 if (spec
->features
[i
][j
] == 0xFFFFFFFF)
1575 *p
++ = '*', *p
++ = ',';
1578 OTF_tag_name (spec
->features
[i
][j
], p
);
1587 if (otf_gstring
.size
== 0)
1589 otf_gstring
.glyphs
= (OTF_Glyph
*) malloc (sizeof (OTF_Glyph
) * len
);
1590 otf_gstring
.size
= len
;
1592 else if (otf_gstring
.size
< len
)
1594 otf_gstring
.glyphs
= (OTF_Glyph
*) realloc (otf_gstring
.glyphs
,
1595 sizeof (OTF_Glyph
) * len
);
1596 otf_gstring
.size
= len
;
1598 otf_gstring
.used
= len
;
1599 memset (otf_gstring
.glyphs
, 0, sizeof (OTF_Glyph
) * len
);
1600 for (i
= 0; i
< len
; i
++)
1602 otf_gstring
.glyphs
[i
].c
= in
->glyphs
[from
+ i
].c
;
1603 otf_gstring
.glyphs
[i
].glyph_id
= in
->glyphs
[from
+ i
].code
;
1606 OTF_drive_gdef (otf
, &otf_gstring
);
1611 if (OTF_drive_gsub (otf
, &otf_gstring
, script
, langsys
, gsub_features
)
1614 if (out
->allocated
< out
->used
+ otf_gstring
.used
)
1616 for (i
= 0, otfg
= otf_gstring
.glyphs
; i
< otf_gstring
.used
; )
1619 int min_from
, max_to
;
1622 g
= out
->glyphs
+ out
->used
;
1623 *g
= in
->glyphs
[from
+ otfg
->f
.index
.from
];
1624 if (g
->code
!= otfg
->glyph_id
)
1627 g
->code
= otfg
->glyph_id
;
1633 if (otfg
->f
.index
.from
< otfg
->f
.index
.to
)
1635 /* OTFG substitutes multiple glyphs in IN. */
1636 for (j
= from
+ otfg
->f
.index
.from
+ 1;
1637 j
<= from
+ otfg
->f
.index
.to
; j
++)
1639 if (min_from
> in
->glyphs
[j
].from
)
1640 min_from
= in
->glyphs
[j
].from
;
1641 if (max_to
< in
->glyphs
[j
].to
)
1642 max_to
= in
->glyphs
[j
].to
;
1647 for (i
++, otfg
++; (i
< otf_gstring
.used
1648 && otfg
->f
.index
.from
== otfg
[-1].f
.index
.from
);
1651 g
= out
->glyphs
+ out
->used
;
1652 *g
= in
->glyphs
[from
+ otfg
->f
.index
.to
];
1653 if (g
->code
!= otfg
->glyph_id
)
1656 g
->code
= otfg
->glyph_id
;
1665 if (out
->allocated
< out
->used
+ len
)
1667 for (i
= 0; i
< len
; i
++)
1668 out
->glyphs
[out
->used
++] = in
->glyphs
[from
+ i
];
1673 MFLTGlyph
*base
= NULL
, *mark
= NULL
, *g
;
1674 int x_ppem
, y_ppem
, x_scale
, y_scale
;
1676 if (OTF_drive_gpos (otf
, &otf_gstring
, script
, langsys
, gpos_features
)
1680 x_ppem
= ft_face
->size
->metrics
.x_ppem
;
1681 y_ppem
= ft_face
->size
->metrics
.y_ppem
;
1682 x_scale
= ft_face
->size
->metrics
.x_scale
;
1683 y_scale
= ft_face
->size
->metrics
.y_scale
;
1685 for (i
= 0, otfg
= otf_gstring
.glyphs
, g
= out
->glyphs
+ gidx
;
1686 i
< otf_gstring
.used
; i
++, otfg
++, g
++)
1690 if (! otfg
->glyph_id
)
1692 switch (otfg
->positioning_type
)
1696 case 1: /* Single */
1699 int format
= otfg
->f
.f1
.format
;
1701 if (format
& OTF_XPlacement
)
1703 = otfg
->f
.f1
.value
->XPlacement
* x_scale
/ 0x10000;
1704 if (format
& OTF_XPlaDevice
)
1706 += DEVICE_DELTA (otfg
->f
.f1
.value
->XPlaDevice
, x_ppem
);
1707 if (format
& OTF_YPlacement
)
1709 = - (otfg
->f
.f1
.value
->YPlacement
* y_scale
/ 0x10000);
1710 if (format
& OTF_YPlaDevice
)
1712 -= DEVICE_DELTA (otfg
->f
.f1
.value
->YPlaDevice
, y_ppem
);
1713 if (format
& OTF_XAdvance
)
1715 += otfg
->f
.f1
.value
->XAdvance
* x_scale
/ 0x10000;
1716 if (format
& OTF_XAdvDevice
)
1718 += DEVICE_DELTA (otfg
->f
.f1
.value
->XAdvDevice
, x_ppem
);
1719 if (format
& OTF_YAdvance
)
1721 += otfg
->f
.f1
.value
->YAdvance
* y_scale
/ 0x10000;
1722 if (format
& OTF_YAdvDevice
)
1724 += DEVICE_DELTA (otfg
->f
.f1
.value
->YAdvDevice
, y_ppem
);
1725 adjustment
[i
].set
= 1;
1728 case 3: /* Cursive */
1729 /* Not yet supported. */
1731 case 4: /* Mark-to-Base */
1732 case 5: /* Mark-to-Ligature */
1736 goto label_adjust_anchor
;
1737 default: /* i.e. case 6 Mark-to-Mark */
1742 label_adjust_anchor
:
1744 int base_x
, base_y
, mark_x
, mark_y
;
1745 int this_from
, this_to
;
1747 base_x
= otfg
->f
.f4
.base_anchor
->XCoordinate
* x_scale
/ 0x10000;
1748 base_y
= otfg
->f
.f4
.base_anchor
->YCoordinate
* y_scale
/ 0x10000;
1749 mark_x
= otfg
->f
.f4
.mark_anchor
->XCoordinate
* x_scale
/ 0x10000;
1750 mark_y
= otfg
->f
.f4
.mark_anchor
->YCoordinate
* y_scale
/ 0x10000;;
1752 if (otfg
->f
.f4
.base_anchor
->AnchorFormat
!= 1)
1753 adjust_anchor (ft_face
, otfg
->f
.f4
.base_anchor
,
1754 prev
->code
, x_ppem
, y_ppem
, &base_x
, &base_y
);
1755 if (otfg
->f
.f4
.mark_anchor
->AnchorFormat
!= 1)
1756 adjust_anchor (ft_face
, otfg
->f
.f4
.mark_anchor
, g
->code
,
1757 x_ppem
, y_ppem
, &mark_x
, &mark_y
);
1758 adjustment
[i
].xoff
= (base_x
- mark_x
);
1759 adjustment
[i
].yoff
= - (base_y
- mark_y
);
1760 adjustment
[i
].back
= (g
- prev
);
1761 adjustment
[i
].xadv
= 0;
1762 adjustment
[i
].advance_is_absolute
= 1;
1763 adjustment
[i
].set
= 1;
1764 this_from
= g
->from
;
1766 for (j
= 0; prev
+ j
< g
; j
++)
1768 if (this_from
> prev
[j
].from
)
1769 this_from
= prev
[j
].from
;
1770 if (this_to
< prev
[j
].to
)
1771 this_to
= prev
[j
].to
;
1773 for (; prev
<= g
; prev
++)
1775 prev
->from
= this_from
;
1780 if (otfg
->GlyphClass
== OTF_GlyphClass0
)
1782 else if (otfg
->GlyphClass
== OTF_GlyphClassMark
)
1791 if (out
->allocated
< out
->used
+ len
)
1793 font
->get_metrics (font
, in
, from
, to
);
1794 memcpy (out
->glyphs
+ out
->used
, in
->glyphs
+ from
,
1795 sizeof (MFLTGlyph
) * len
);
1800 static MFLTGlyphString gstring
;
1802 static int m17n_flt_initialized
;
1804 extern Lisp_Object QCfamily
;
1807 ftfont_shape_by_flt (lgstring
, font
, ft_face
, otf
)
1808 Lisp_Object lgstring
;
1813 EMACS_UINT len
= LGSTRING_GLYPH_LEN (lgstring
);
1815 struct MFLTFontFT flt_font_ft
;
1818 if (! m17n_flt_initialized
)
1821 m17n_flt_initialized
= 1;
1824 for (i
= 0; i
< len
; i
++)
1825 if (NILP (LGSTRING_GLYPH (lgstring
, i
)))
1829 if (gstring
.allocated
== 0)
1831 gstring
.allocated
= len
* 2;
1832 gstring
.glyph_size
= sizeof (MFLTGlyph
);
1833 gstring
.glyphs
= malloc (sizeof (MFLTGlyph
) * gstring
.allocated
);
1835 else if (gstring
.allocated
< len
* 2)
1837 gstring
.allocated
= len
* 2;
1838 gstring
.glyphs
= realloc (gstring
.glyphs
,
1839 sizeof (MFLTGlyph
) * gstring
.allocated
);
1841 for (i
= 0; i
< len
; i
++)
1842 gstring
.glyphs
[i
].c
= LGLYPH_CHAR (LGSTRING_GLYPH (lgstring
, i
));
1847 Lisp_Object family
= Ffont_get (LGSTRING_FONT (lgstring
), QCfamily
);
1850 flt_font_ft
.flt_font
.family
= Mnil
;
1852 flt_font_ft
.flt_font
.family
1853 = msymbol ((char *) SDATA (Fdowncase (SYMBOL_NAME (family
))));
1855 flt_font_ft
.flt_font
.x_ppem
= ft_face
->size
->metrics
.x_ppem
;
1856 flt_font_ft
.flt_font
.y_ppem
= ft_face
->size
->metrics
.y_ppem
;
1857 flt_font_ft
.flt_font
.get_glyph_id
= ftfont_get_glyph_id
;
1858 flt_font_ft
.flt_font
.get_metrics
= ftfont_get_metrics
;
1859 flt_font_ft
.flt_font
.check_otf
= ftfont_check_otf
;
1860 flt_font_ft
.flt_font
.drive_otf
= ftfont_drive_otf
;
1861 flt_font_ft
.flt_font
.internal
= NULL
;
1862 flt_font_ft
.font
= font
;
1863 flt_font_ft
.ft_face
= ft_face
;
1864 flt_font_ft
.otf
= otf
;
1865 if (ASCII_CHAR_P (gstring
.glyphs
[0].c
))
1866 /* A little bit ad hoc. Perhaps, shaper must get script and
1867 language information, and select a proper flt for them
1869 flt
= mflt_get (msymbol ("combining"));
1870 for (i
= 0; i
< 3; i
++)
1872 int result
= mflt_run (&gstring
, 0, len
, &flt_font_ft
.flt_font
, flt
);
1875 gstring
.allocated
+= gstring
.allocated
;
1876 gstring
.glyphs
= realloc (gstring
.glyphs
,
1877 sizeof (MFLTGlyph
) * gstring
.allocated
);
1879 if (gstring
.used
> LGSTRING_GLYPH_LEN (lgstring
))
1881 for (i
= 0; i
< gstring
.used
; i
++)
1883 MFLTGlyph
*g
= gstring
.glyphs
+ i
;
1885 g
->from
= LGLYPH_FROM (LGSTRING_GLYPH (lgstring
, g
->from
));
1886 g
->to
= LGLYPH_TO (LGSTRING_GLYPH (lgstring
, g
->to
));
1889 for (i
= 0; i
< gstring
.used
; i
++)
1891 Lisp_Object lglyph
= LGSTRING_GLYPH (lgstring
, i
);
1892 MFLTGlyph
*g
= gstring
.glyphs
+ i
;
1896 lglyph
= Fmake_vector (make_number (LGLYPH_SIZE
), Qnil
);
1897 LGSTRING_SET_GLYPH (lgstring
, i
, lglyph
);
1899 LGLYPH_SET_FROM (lglyph
, g
->from
);
1900 LGLYPH_SET_TO (lglyph
, g
->to
);
1901 LGLYPH_SET_CHAR (lglyph
, g
->c
);
1902 LGLYPH_SET_CODE (lglyph
, g
->code
);
1903 LGLYPH_SET_WIDTH (lglyph
, g
->xadv
>> 6);
1904 LGLYPH_SET_LBEARING (lglyph
, g
->lbearing
>> 6);
1905 LGLYPH_SET_RBEARING (lglyph
, g
->rbearing
>> 6);
1906 LGLYPH_SET_ASCENT (lglyph
, g
->ascent
>> 6);
1907 LGLYPH_SET_DESCENT (lglyph
, g
->descent
>> 6);
1912 vec
= Fmake_vector (make_number (3), Qnil
);
1913 ASET (vec
, 0, make_number (g
->xoff
>> 6));
1914 ASET (vec
, 1, make_number (g
->yoff
>> 6));
1915 ASET (vec
, 2, make_number (g
->xadv
>> 6));
1916 LGLYPH_SET_ADJUSTMENT (lglyph
, vec
);
1919 return make_number (i
);
1923 ftfont_shape (lgstring
)
1924 Lisp_Object lgstring
;
1927 struct ftfont_info
*ftfont_info
;
1930 CHECK_FONT_GET_OBJECT (LGSTRING_FONT (lgstring
), font
);
1931 ftfont_info
= (struct ftfont_info
*) font
;
1932 otf
= ftfont_get_otf (ftfont_info
);
1934 return make_number (0);
1935 return ftfont_shape_by_flt (lgstring
, font
, ftfont_info
->ft_size
->face
, otf
);
1938 #endif /* HAVE_M17N_FLT */
1939 #endif /* HAVE_LIBOTF */
1942 ftfont_font_format (FcPattern
*pattern
, Lisp_Object filename
)
1946 #ifdef FC_FONTFORMAT
1949 if (FcPatternGetString (pattern
, FC_FONTFORMAT
, 0, &str
) != FcResultMatch
)
1951 if (strcmp ((char *) str
, "TrueType") == 0)
1952 return intern ("truetype");
1953 if (strcmp ((char *) str
, "Type 1") == 0)
1954 return intern ("type1");
1955 if (strcmp ((char *) str
, "PCF") == 0)
1956 return intern ("pcf");
1957 if (strcmp ((char *) str
, "BDF") == 0)
1958 return intern ("bdf");
1960 #endif /* FC_FONTFORMAT */
1961 if (STRINGP (filename
))
1963 int len
= SBYTES (filename
);
1967 str
= (FcChar8
*) (SDATA (filename
) + len
- 4);
1968 if (xstrcasecmp ((char *) str
, ".ttf") == 0)
1969 return intern ("truetype");
1970 if (xstrcasecmp ((char *) str
, ".pfb") == 0)
1971 return intern ("type1");
1972 if (xstrcasecmp ((char *) str
, ".pcf") == 0)
1973 return intern ("pcf");
1974 if (xstrcasecmp ((char *) str
, ".bdf") == 0)
1975 return intern ("bdf");
1978 return intern ("unknown");
1985 DEFSYM (Qfreetype
, "freetype");
1986 DEFSYM (Qmonospace
, "monospace");
1987 DEFSYM (Qsans_serif
, "sans-serif");
1988 DEFSYM (Qserif
, "serif");
1989 DEFSYM (Qmono
, "mono");
1990 DEFSYM (Qsans
, "sans");
1991 DEFSYM (Qsans__serif
, "sans serif");
1993 staticpro (&freetype_font_cache
);
1994 freetype_font_cache
= Fcons (Qt
, Qnil
);
1996 staticpro (&ftfont_generic_family_list
);
1997 ftfont_generic_family_list
1998 = Fcons (Fcons (Qmonospace
, Qt
),
1999 Fcons (Fcons (Qsans_serif
, Qt
),
2000 Fcons (Fcons (Qsans
, Qt
), Qnil
)));
2002 staticpro (&ft_face_cache
);
2003 ft_face_cache
= Qnil
;
2005 ftfont_driver
.type
= Qfreetype
;
2006 register_font_driver (&ftfont_driver
, NULL
);
2009 /* arch-tag: 7cfa432c-33a6-4988-83d2-a82ed8604aca
2010 (do not change this comment) */