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 l
= otf_langsys
->FeatureIndex
[k
];
1363 if (l
>= gsub_gpos
->FeatureList
.FeatureCount
)
1365 OTF_TAG_SYM (sym
, gsub_gpos
->FeatureList
.Feature
[l
].FeatureTag
);
1366 features
= Fcons (sym
, features
);
1369 OTF_TAG_SYM (sym
, otf_script
->LangSysRecord
[j
].LangSysTag
);
1372 langsyses
= Fcons (Fcons (sym
, features
), langsyses
);
1375 OTF_TAG_SYM (sym
, gsub_gpos
->ScriptList
.Script
[i
].ScriptTag
);
1376 scripts
= Fcons (Fcons (sym
, langsyses
), scripts
);
1384 ftfont_otf_capability (font
)
1387 struct ftfont_info
*ftfont_info
= (struct ftfont_info
*) font
;
1388 OTF
*otf
= ftfont_get_otf (ftfont_info
);
1389 Lisp_Object gsub_gpos
;
1393 gsub_gpos
= Fcons (Qnil
, Qnil
);
1394 if (OTF_get_table (otf
, "GSUB") == 0
1395 && otf
->gsub
->FeatureList
.FeatureCount
> 0)
1396 XSETCAR (gsub_gpos
, ftfont_otf_features (otf
->gsub
));
1397 if (OTF_get_table (otf
, "GPOS") == 0
1398 && otf
->gpos
->FeatureList
.FeatureCount
> 0)
1399 XSETCDR (gsub_gpos
, ftfont_otf_features (otf
->gpos
));
1403 #ifdef HAVE_M17N_FLT
1414 ftfont_get_glyph_id (font
, gstring
, from
, to
)
1416 MFLTGlyphString
*gstring
;
1419 struct MFLTFontFT
*flt_font_ft
= (struct MFLTFontFT
*) font
;
1420 FT_Face ft_face
= flt_font_ft
->ft_face
;
1423 for (g
= gstring
->glyphs
+ from
; from
< to
; g
++, from
++)
1426 FT_UInt code
= FT_Get_Char_Index (ft_face
, g
->code
);
1428 g
->code
= code
> 0 ? code
: FONT_INVALID_CODE
;
1435 ftfont_get_metrics (font
, gstring
, from
, to
)
1437 MFLTGlyphString
*gstring
;
1440 struct MFLTFontFT
*flt_font_ft
= (struct MFLTFontFT
*) font
;
1441 FT_Face ft_face
= flt_font_ft
->ft_face
;
1444 for (g
= gstring
->glyphs
+ from
; from
< to
; g
++, from
++)
1447 if (g
->code
!= FONT_INVALID_CODE
)
1449 FT_Glyph_Metrics
*m
;
1451 if (FT_Load_Glyph (ft_face
, g
->code
, FT_LOAD_DEFAULT
) != 0)
1453 m
= &ft_face
->glyph
->metrics
;
1455 g
->lbearing
= m
->horiBearingX
;
1456 g
->rbearing
= m
->horiBearingX
+ m
->width
;
1457 g
->ascent
= m
->horiBearingY
;
1458 g
->descent
= m
->height
- m
->horiBearingY
;
1459 g
->xadv
= m
->horiAdvance
;
1464 g
->rbearing
= g
->xadv
= flt_font_ft
->font
->space_width
<< 6;
1465 g
->ascent
= flt_font_ft
->font
->ascent
<< 6;
1466 g
->descent
= flt_font_ft
->font
->descent
<< 6;
1475 ftfont_check_otf (MFLTFont
*font
, MFLTOtfSpec
*spec
)
1477 struct MFLTFontFT
*flt_font_ft
= (struct MFLTFontFT
*) font
;
1478 OTF
*otf
= flt_font_ft
->otf
;
1482 for (i
= 0; i
< 2; i
++)
1484 if (! spec
->features
[i
])
1486 for (n
= 0; spec
->features
[i
][n
]; n
++);
1487 tags
= alloca (sizeof (OTF_Tag
) * n
);
1488 for (n
= 0, negative
= 0; spec
->features
[i
][n
]; n
++)
1490 if (spec
->features
[i
][n
] == 0xFFFFFFFF)
1493 tags
[n
- 1] = spec
->features
[i
][n
] | 0x80000000;
1495 tags
[n
] = spec
->features
[i
][n
];
1497 if (n
- negative
> 0
1498 && OTF_check_features (otf
, i
== 0, spec
->script
, spec
->langsys
,
1499 tags
, n
- negative
) != 1)
1505 #define DEVICE_DELTA(table, size) \
1506 (((size) >= (table).StartSize && (size) <= (table).EndSize) \
1507 ? (table).DeltaValue[(size) - (table).StartSize] << 6 \
1511 adjust_anchor (FT_Face ft_face
, OTF_Anchor
*anchor
,
1512 unsigned code
, int x_ppem
, int y_ppem
, int *x
, int *y
)
1514 if (anchor
->AnchorFormat
== 2)
1516 FT_Outline
*outline
;
1517 int ap
= anchor
->f
.f1
.AnchorPoint
;
1519 FT_Load_Glyph (ft_face
, (FT_UInt
) code
, FT_LOAD_MONOCHROME
);
1520 outline
= &ft_face
->glyph
->outline
;
1521 if (ap
< outline
->n_points
)
1523 *x
= outline
->points
[ap
].x
<< 6;
1524 *y
= outline
->points
[ap
].y
<< 6;
1527 else if (anchor
->AnchorFormat
== 3)
1529 if (anchor
->f
.f2
.XDeviceTable
.offset
1530 && anchor
->f
.f2
.XDeviceTable
.DeltaValue
)
1531 *x
+= DEVICE_DELTA (anchor
->f
.f2
.XDeviceTable
, x_ppem
);
1532 if (anchor
->f
.f2
.YDeviceTable
.offset
1533 && anchor
->f
.f2
.YDeviceTable
.DeltaValue
)
1534 *y
+= DEVICE_DELTA (anchor
->f
.f2
.YDeviceTable
, y_ppem
);
1538 static OTF_GlyphString otf_gstring
;
1541 ftfont_drive_otf (font
, spec
, in
, from
, to
, out
, adjustment
)
1544 MFLTGlyphString
*in
;
1546 MFLTGlyphString
*out
;
1547 MFLTGlyphAdjustment
*adjustment
;
1549 struct MFLTFontFT
*flt_font_ft
= (struct MFLTFontFT
*) font
;
1550 FT_Face ft_face
= flt_font_ft
->ft_face
;
1551 OTF
*otf
= flt_font_ft
->otf
;
1552 int len
= to
- from
;
1555 char script
[5], *langsys
= NULL
;
1556 char *gsub_features
= NULL
, *gpos_features
= NULL
;
1560 OTF_tag_name (spec
->script
, script
);
1563 langsys
= alloca (5);
1564 OTF_tag_name (spec
->langsys
, langsys
);
1566 for (i
= 0; i
< 2; i
++)
1570 if (spec
->features
[i
] && spec
->features
[i
][1] != 0xFFFFFFFF)
1572 for (j
= 0; spec
->features
[i
][j
]; j
++);
1574 p
= gsub_features
= alloca (6 * j
);
1576 p
= gpos_features
= alloca (6 * j
);
1577 for (j
= 0; spec
->features
[i
][j
]; j
++)
1579 if (spec
->features
[i
][j
] == 0xFFFFFFFF)
1580 *p
++ = '*', *p
++ = ',';
1583 OTF_tag_name (spec
->features
[i
][j
], p
);
1592 if (otf_gstring
.size
== 0)
1594 otf_gstring
.glyphs
= (OTF_Glyph
*) malloc (sizeof (OTF_Glyph
) * len
);
1595 otf_gstring
.size
= len
;
1597 else if (otf_gstring
.size
< len
)
1599 otf_gstring
.glyphs
= (OTF_Glyph
*) realloc (otf_gstring
.glyphs
,
1600 sizeof (OTF_Glyph
) * len
);
1601 otf_gstring
.size
= len
;
1603 otf_gstring
.used
= len
;
1604 memset (otf_gstring
.glyphs
, 0, sizeof (OTF_Glyph
) * len
);
1605 for (i
= 0; i
< len
; i
++)
1607 otf_gstring
.glyphs
[i
].c
= in
->glyphs
[from
+ i
].c
;
1608 otf_gstring
.glyphs
[i
].glyph_id
= in
->glyphs
[from
+ i
].code
;
1611 OTF_drive_gdef (otf
, &otf_gstring
);
1616 if (OTF_drive_gsub (otf
, &otf_gstring
, script
, langsys
, gsub_features
)
1619 if (out
->allocated
< out
->used
+ otf_gstring
.used
)
1621 for (i
= 0, otfg
= otf_gstring
.glyphs
; i
< otf_gstring
.used
; )
1624 int min_from
, max_to
;
1627 g
= out
->glyphs
+ out
->used
;
1628 *g
= in
->glyphs
[from
+ otfg
->f
.index
.from
];
1629 if (g
->code
!= otfg
->glyph_id
)
1632 g
->code
= otfg
->glyph_id
;
1638 if (otfg
->f
.index
.from
< otfg
->f
.index
.to
)
1640 /* OTFG substitutes multiple glyphs in IN. */
1641 for (j
= from
+ otfg
->f
.index
.from
+ 1;
1642 j
<= from
+ otfg
->f
.index
.to
; j
++)
1644 if (min_from
> in
->glyphs
[j
].from
)
1645 min_from
= in
->glyphs
[j
].from
;
1646 if (max_to
< in
->glyphs
[j
].to
)
1647 max_to
= in
->glyphs
[j
].to
;
1652 for (i
++, otfg
++; (i
< otf_gstring
.used
1653 && otfg
->f
.index
.from
== otfg
[-1].f
.index
.from
);
1656 g
= out
->glyphs
+ out
->used
;
1657 *g
= in
->glyphs
[from
+ otfg
->f
.index
.to
];
1658 if (g
->code
!= otfg
->glyph_id
)
1661 g
->code
= otfg
->glyph_id
;
1670 if (out
->allocated
< out
->used
+ len
)
1672 for (i
= 0; i
< len
; i
++)
1673 out
->glyphs
[out
->used
++] = in
->glyphs
[from
+ i
];
1678 MFLTGlyph
*base
= NULL
, *mark
= NULL
, *g
;
1679 int x_ppem
, y_ppem
, x_scale
, y_scale
;
1681 if (OTF_drive_gpos (otf
, &otf_gstring
, script
, langsys
, gpos_features
)
1685 x_ppem
= ft_face
->size
->metrics
.x_ppem
;
1686 y_ppem
= ft_face
->size
->metrics
.y_ppem
;
1687 x_scale
= ft_face
->size
->metrics
.x_scale
;
1688 y_scale
= ft_face
->size
->metrics
.y_scale
;
1690 for (i
= 0, otfg
= otf_gstring
.glyphs
, g
= out
->glyphs
+ gidx
;
1691 i
< otf_gstring
.used
; i
++, otfg
++, g
++)
1695 if (! otfg
->glyph_id
)
1697 switch (otfg
->positioning_type
)
1701 case 1: /* Single */
1704 int format
= otfg
->f
.f1
.format
;
1706 if (format
& OTF_XPlacement
)
1708 = otfg
->f
.f1
.value
->XPlacement
* x_scale
/ 0x10000;
1709 if (format
& OTF_XPlaDevice
)
1711 += DEVICE_DELTA (otfg
->f
.f1
.value
->XPlaDevice
, x_ppem
);
1712 if (format
& OTF_YPlacement
)
1714 = - (otfg
->f
.f1
.value
->YPlacement
* y_scale
/ 0x10000);
1715 if (format
& OTF_YPlaDevice
)
1717 -= DEVICE_DELTA (otfg
->f
.f1
.value
->YPlaDevice
, y_ppem
);
1718 if (format
& OTF_XAdvance
)
1720 += otfg
->f
.f1
.value
->XAdvance
* x_scale
/ 0x10000;
1721 if (format
& OTF_XAdvDevice
)
1723 += DEVICE_DELTA (otfg
->f
.f1
.value
->XAdvDevice
, x_ppem
);
1724 if (format
& OTF_YAdvance
)
1726 += otfg
->f
.f1
.value
->YAdvance
* y_scale
/ 0x10000;
1727 if (format
& OTF_YAdvDevice
)
1729 += DEVICE_DELTA (otfg
->f
.f1
.value
->YAdvDevice
, y_ppem
);
1730 adjustment
[i
].set
= 1;
1733 case 3: /* Cursive */
1734 /* Not yet supported. */
1736 case 4: /* Mark-to-Base */
1737 case 5: /* Mark-to-Ligature */
1741 goto label_adjust_anchor
;
1742 default: /* i.e. case 6 Mark-to-Mark */
1747 label_adjust_anchor
:
1749 int base_x
, base_y
, mark_x
, mark_y
;
1750 int this_from
, this_to
;
1752 base_x
= otfg
->f
.f4
.base_anchor
->XCoordinate
* x_scale
/ 0x10000;
1753 base_y
= otfg
->f
.f4
.base_anchor
->YCoordinate
* y_scale
/ 0x10000;
1754 mark_x
= otfg
->f
.f4
.mark_anchor
->XCoordinate
* x_scale
/ 0x10000;
1755 mark_y
= otfg
->f
.f4
.mark_anchor
->YCoordinate
* y_scale
/ 0x10000;;
1757 if (otfg
->f
.f4
.base_anchor
->AnchorFormat
!= 1)
1758 adjust_anchor (ft_face
, otfg
->f
.f4
.base_anchor
,
1759 prev
->code
, x_ppem
, y_ppem
, &base_x
, &base_y
);
1760 if (otfg
->f
.f4
.mark_anchor
->AnchorFormat
!= 1)
1761 adjust_anchor (ft_face
, otfg
->f
.f4
.mark_anchor
, g
->code
,
1762 x_ppem
, y_ppem
, &mark_x
, &mark_y
);
1763 adjustment
[i
].xoff
= (base_x
- mark_x
);
1764 adjustment
[i
].yoff
= - (base_y
- mark_y
);
1765 adjustment
[i
].back
= (g
- prev
);
1766 adjustment
[i
].xadv
= 0;
1767 adjustment
[i
].advance_is_absolute
= 1;
1768 adjustment
[i
].set
= 1;
1769 this_from
= g
->from
;
1771 for (j
= 0; prev
+ j
< g
; j
++)
1773 if (this_from
> prev
[j
].from
)
1774 this_from
= prev
[j
].from
;
1775 if (this_to
< prev
[j
].to
)
1776 this_to
= prev
[j
].to
;
1778 for (; prev
<= g
; prev
++)
1780 prev
->from
= this_from
;
1785 if (otfg
->GlyphClass
== OTF_GlyphClass0
)
1787 else if (otfg
->GlyphClass
== OTF_GlyphClassMark
)
1796 if (out
->allocated
< out
->used
+ len
)
1798 font
->get_metrics (font
, in
, from
, to
);
1799 memcpy (out
->glyphs
+ out
->used
, in
->glyphs
+ from
,
1800 sizeof (MFLTGlyph
) * len
);
1805 static MFLTGlyphString gstring
;
1807 static int m17n_flt_initialized
;
1809 extern Lisp_Object QCfamily
;
1812 ftfont_shape_by_flt (lgstring
, font
, ft_face
, otf
)
1813 Lisp_Object lgstring
;
1818 EMACS_UINT len
= LGSTRING_GLYPH_LEN (lgstring
);
1820 struct MFLTFontFT flt_font_ft
;
1823 if (! m17n_flt_initialized
)
1826 m17n_flt_initialized
= 1;
1829 for (i
= 0; i
< len
; i
++)
1830 if (NILP (LGSTRING_GLYPH (lgstring
, i
)))
1834 if (gstring
.allocated
== 0)
1836 gstring
.allocated
= len
* 2;
1837 gstring
.glyph_size
= sizeof (MFLTGlyph
);
1838 gstring
.glyphs
= malloc (sizeof (MFLTGlyph
) * gstring
.allocated
);
1840 else if (gstring
.allocated
< len
* 2)
1842 gstring
.allocated
= len
* 2;
1843 gstring
.glyphs
= realloc (gstring
.glyphs
,
1844 sizeof (MFLTGlyph
) * gstring
.allocated
);
1846 for (i
= 0; i
< len
; i
++)
1847 gstring
.glyphs
[i
].c
= LGLYPH_CHAR (LGSTRING_GLYPH (lgstring
, i
));
1852 Lisp_Object family
= Ffont_get (LGSTRING_FONT (lgstring
), QCfamily
);
1855 flt_font_ft
.flt_font
.family
= Mnil
;
1857 flt_font_ft
.flt_font
.family
1858 = msymbol ((char *) SDATA (Fdowncase (SYMBOL_NAME (family
))));
1860 flt_font_ft
.flt_font
.x_ppem
= ft_face
->size
->metrics
.x_ppem
;
1861 flt_font_ft
.flt_font
.y_ppem
= ft_face
->size
->metrics
.y_ppem
;
1862 flt_font_ft
.flt_font
.get_glyph_id
= ftfont_get_glyph_id
;
1863 flt_font_ft
.flt_font
.get_metrics
= ftfont_get_metrics
;
1864 flt_font_ft
.flt_font
.check_otf
= ftfont_check_otf
;
1865 flt_font_ft
.flt_font
.drive_otf
= ftfont_drive_otf
;
1866 flt_font_ft
.flt_font
.internal
= NULL
;
1867 flt_font_ft
.font
= font
;
1868 flt_font_ft
.ft_face
= ft_face
;
1869 flt_font_ft
.otf
= otf
;
1870 if (ASCII_CHAR_P (gstring
.glyphs
[0].c
))
1871 /* A little bit ad hoc. Perhaps, shaper must get script and
1872 language information, and select a proper flt for them
1874 flt
= mflt_get (msymbol ("combining"));
1875 for (i
= 0; i
< 3; i
++)
1877 int result
= mflt_run (&gstring
, 0, len
, &flt_font_ft
.flt_font
, flt
);
1880 gstring
.allocated
+= gstring
.allocated
;
1881 gstring
.glyphs
= realloc (gstring
.glyphs
,
1882 sizeof (MFLTGlyph
) * gstring
.allocated
);
1884 if (gstring
.used
> LGSTRING_GLYPH_LEN (lgstring
))
1886 for (i
= 0; i
< gstring
.used
; i
++)
1888 MFLTGlyph
*g
= gstring
.glyphs
+ i
;
1890 g
->from
= LGLYPH_FROM (LGSTRING_GLYPH (lgstring
, g
->from
));
1891 g
->to
= LGLYPH_TO (LGSTRING_GLYPH (lgstring
, g
->to
));
1894 for (i
= 0; i
< gstring
.used
; i
++)
1896 Lisp_Object lglyph
= LGSTRING_GLYPH (lgstring
, i
);
1897 MFLTGlyph
*g
= gstring
.glyphs
+ i
;
1901 lglyph
= Fmake_vector (make_number (LGLYPH_SIZE
), Qnil
);
1902 LGSTRING_SET_GLYPH (lgstring
, i
, lglyph
);
1904 LGLYPH_SET_FROM (lglyph
, g
->from
);
1905 LGLYPH_SET_TO (lglyph
, g
->to
);
1906 LGLYPH_SET_CHAR (lglyph
, g
->c
);
1907 LGLYPH_SET_CODE (lglyph
, g
->code
);
1908 LGLYPH_SET_WIDTH (lglyph
, g
->xadv
>> 6);
1909 LGLYPH_SET_LBEARING (lglyph
, g
->lbearing
>> 6);
1910 LGLYPH_SET_RBEARING (lglyph
, g
->rbearing
>> 6);
1911 LGLYPH_SET_ASCENT (lglyph
, g
->ascent
>> 6);
1912 LGLYPH_SET_DESCENT (lglyph
, g
->descent
>> 6);
1917 vec
= Fmake_vector (make_number (3), Qnil
);
1918 ASET (vec
, 0, make_number (g
->xoff
>> 6));
1919 ASET (vec
, 1, make_number (g
->yoff
>> 6));
1920 ASET (vec
, 2, make_number (g
->xadv
>> 6));
1921 LGLYPH_SET_ADJUSTMENT (lglyph
, vec
);
1924 return make_number (i
);
1928 ftfont_shape (lgstring
)
1929 Lisp_Object lgstring
;
1932 struct ftfont_info
*ftfont_info
;
1935 CHECK_FONT_GET_OBJECT (LGSTRING_FONT (lgstring
), font
);
1936 ftfont_info
= (struct ftfont_info
*) font
;
1937 otf
= ftfont_get_otf (ftfont_info
);
1939 return make_number (0);
1940 return ftfont_shape_by_flt (lgstring
, font
, ftfont_info
->ft_size
->face
, otf
);
1943 #endif /* HAVE_M17N_FLT */
1944 #endif /* HAVE_LIBOTF */
1947 ftfont_font_format (FcPattern
*pattern
, Lisp_Object filename
)
1951 #ifdef FC_FONTFORMAT
1954 if (FcPatternGetString (pattern
, FC_FONTFORMAT
, 0, &str
) != FcResultMatch
)
1956 if (strcmp ((char *) str
, "TrueType") == 0)
1957 return intern ("truetype");
1958 if (strcmp ((char *) str
, "Type 1") == 0)
1959 return intern ("type1");
1960 if (strcmp ((char *) str
, "PCF") == 0)
1961 return intern ("pcf");
1962 if (strcmp ((char *) str
, "BDF") == 0)
1963 return intern ("bdf");
1965 #endif /* FC_FONTFORMAT */
1966 if (STRINGP (filename
))
1968 int len
= SBYTES (filename
);
1972 str
= (FcChar8
*) (SDATA (filename
) + len
- 4);
1973 if (xstrcasecmp ((char *) str
, ".ttf") == 0)
1974 return intern ("truetype");
1975 if (xstrcasecmp ((char *) str
, ".pfb") == 0)
1976 return intern ("type1");
1977 if (xstrcasecmp ((char *) str
, ".pcf") == 0)
1978 return intern ("pcf");
1979 if (xstrcasecmp ((char *) str
, ".bdf") == 0)
1980 return intern ("bdf");
1983 return intern ("unknown");
1990 DEFSYM (Qfreetype
, "freetype");
1991 DEFSYM (Qmonospace
, "monospace");
1992 DEFSYM (Qsans_serif
, "sans-serif");
1993 DEFSYM (Qserif
, "serif");
1994 DEFSYM (Qmono
, "mono");
1995 DEFSYM (Qsans
, "sans");
1996 DEFSYM (Qsans__serif
, "sans serif");
1998 staticpro (&freetype_font_cache
);
1999 freetype_font_cache
= Fcons (Qt
, Qnil
);
2001 staticpro (&ftfont_generic_family_list
);
2002 ftfont_generic_family_list
2003 = Fcons (Fcons (Qmonospace
, Qt
),
2004 Fcons (Fcons (Qsans_serif
, Qt
),
2005 Fcons (Fcons (Qsans
, Qt
), Qnil
)));
2007 staticpro (&ft_face_cache
);
2008 ft_face_cache
= Qnil
;
2010 ftfont_driver
.type
= Qfreetype
;
2011 register_font_driver (&ftfont_driver
, NULL
);
2014 /* arch-tag: 7cfa432c-33a6-4988-83d2-a82ed8604aca
2015 (do not change this comment) */