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"
39 /* Symbolic type of this font-driver. */
40 Lisp_Object Qfreetype
;
42 /* Fontconfig's generic families and their aliases. */
43 static Lisp_Object Qmonospace
, Qsans_serif
, Qserif
, Qmono
, Qsans
, Qsans__serif
;
45 /* Flag to tell if FcInit is already called or not. */
46 static int fc_initialized
;
48 /* Handle to a FreeType library instance. */
49 static FT_Library ft_library
;
51 /* Cache for FreeType fonts. */
52 static Lisp_Object freetype_font_cache
;
54 /* Cache for FT_Face and FcCharSet. */
55 static Lisp_Object ft_face_cache
;
57 /* The actual structure for FreeType font that can be casted to struct
64 /* The following three members must be here in this order to be
65 compatible with struct xftfont_info (in xftfont.c). */
66 int maybe_otf
; /* Flag to tell if this may be OTF or not. */
68 #endif /* HAVE_LIBOTF */
73 static Lisp_Object ftfont_pattern_entity
P_ ((FcPattern
*, Lisp_Object
));
75 static Lisp_Object ftfont_resolve_generic_family
P_ ((Lisp_Object
,
77 static Lisp_Object ftfont_lookup_cache
P_ ((Lisp_Object
, int));
79 Lisp_Object ftfont_font_format
P_ ((FcPattern
*, Lisp_Object
));
81 #define SYMBOL_FcChar8(SYM) (FcChar8 *) SDATA (SYMBOL_NAME (SYM))
87 /* characters to distinguish the charset from the others */
89 /* additional constraint by language */
92 FcCharSet
*fc_charset
;
93 } fc_charset_table
[] =
94 { { "iso8859-1", { 0x00A0, 0x00A1, 0x00B4, 0x00BC, 0x00D0 } },
95 { "iso8859-2", { 0x00A0, 0x010E }},
96 { "iso8859-3", { 0x00A0, 0x0108 }},
97 { "iso8859-4", { 0x00A0, 0x00AF, 0x0128, 0x0156, 0x02C7 }},
98 { "iso8859-5", { 0x00A0, 0x0401 }},
99 { "iso8859-6", { 0x00A0, 0x060C }},
100 { "iso8859-7", { 0x00A0, 0x0384 }},
101 { "iso8859-8", { 0x00A0, 0x05D0 }},
102 { "iso8859-9", { 0x00A0, 0x00A1, 0x00BC, 0x011E }},
103 { "iso8859-10", { 0x00A0, 0x00D0, 0x0128, 0x2015 }},
104 { "iso8859-11", { 0x00A0, 0x0E01 }},
105 { "iso8859-13", { 0x00A0, 0x201C }},
106 { "iso8859-14", { 0x00A0, 0x0174 }},
107 { "iso8859-15", { 0x00A0, 0x00A1, 0x00D0, 0x0152 }},
108 { "iso8859-16", { 0x00A0, 0x0218}},
109 { "gb2312.1980-0", { 0x4E13 }, "zh-cn"},
110 { "big5-0", { 0xF6B1 }, "zh-tw" },
111 { "jisx0208.1983-0", { 0x4E55 }, "ja"},
112 { "ksc5601.1985-0", { 0xAC00 }, "ko"},
113 { "cns11643.1992-1", { 0xFE32 }, "zh-tw"},
114 { "cns11643.1992-2", { 0x4E33, 0x7934 }},
115 { "cns11643.1992-3", { 0x201A9 }},
116 { "cns11643.1992-4", { 0x20057 }},
117 { "cns11643.1992-5", { 0x20000 }},
118 { "cns11643.1992-6", { 0x20003 }},
119 { "cns11643.1992-7", { 0x20055 }},
120 { "gbk-0", { 0x4E06 }, "zh-cn"},
121 { "jisx0212.1990-0", { 0x4E44 }},
122 { "jisx0213.2000-1", { 0xFA10 }, "ja"},
123 { "jisx0213.2000-2", { 0xFA49 }},
124 { "jisx0213.2004-1", { 0x20B9F }},
125 { "viscii1.1-1", { 0x1EA0, 0x1EAE, 0x1ED2 }, "vi"},
126 { "tis620.2529-1", { 0x0E01 }, "th"},
127 { "windows-1251", { 0x0401, 0x0490 }, "ru"},
128 { "koi8-r", { 0x0401, 0x2219 }, "ru"},
129 { "mulelao-1", { 0x0E81 }, "lo"},
133 extern Lisp_Object Qc
, Qm
, Qp
, Qd
;
136 ftfont_pattern_entity (p
, extra
)
147 if (FcPatternGetString (p
, FC_FILE
, 0, (FcChar8
**) &file
) != FcResultMatch
)
149 if (FcPatternGetInteger (p
, FC_INDEX
, 0, &index
) != FcResultMatch
)
152 entity
= font_make_entity ();
154 ASET (entity
, FONT_TYPE_INDEX
, Qfreetype
);
155 ASET (entity
, FONT_REGISTRY_INDEX
, Qiso10646_1
);
157 if (FcPatternGetString (p
, FC_FOUNDRY
, 0, (FcChar8
**) &str
) == FcResultMatch
)
158 ASET (entity
, FONT_FOUNDRY_INDEX
, font_intern_prop (str
, strlen (str
), 1));
159 if (FcPatternGetString (p
, FC_FAMILY
, 0, (FcChar8
**) &str
) == FcResultMatch
)
160 ASET (entity
, FONT_FAMILY_INDEX
, font_intern_prop (str
, strlen (str
), 1));
161 if (FcPatternGetInteger (p
, FC_WEIGHT
, 0, &numeric
) == FcResultMatch
)
163 if (numeric
>= FC_WEIGHT_REGULAR
&& numeric
< FC_WEIGHT_MEDIUM
)
164 numeric
= FC_WEIGHT_MEDIUM
;
165 FONT_SET_STYLE (entity
, FONT_WEIGHT_INDEX
, make_number (numeric
));
167 if (FcPatternGetInteger (p
, FC_SLANT
, 0, &numeric
) == FcResultMatch
)
170 FONT_SET_STYLE (entity
, FONT_SLANT_INDEX
, make_number (numeric
));
172 if (FcPatternGetInteger (p
, FC_WIDTH
, 0, &numeric
) == FcResultMatch
)
174 FONT_SET_STYLE (entity
, FONT_WIDTH_INDEX
, make_number (numeric
));
176 if (FcPatternGetDouble (p
, FC_PIXEL_SIZE
, 0, &dbl
) == FcResultMatch
)
177 ASET (entity
, FONT_SIZE_INDEX
, make_number (dbl
));
179 ASET (entity
, FONT_SIZE_INDEX
, make_number (0));
180 if (FcPatternGetInteger (p
, FC_SPACING
, 0, &numeric
) == FcResultMatch
)
181 ASET (entity
, FONT_SPACING_INDEX
, make_number (numeric
));
182 if (FcPatternGetDouble (p
, FC_DPI
, 0, &dbl
) == FcResultMatch
)
185 ASET (entity
, FONT_DPI_INDEX
, make_number (dpi
));
187 if (FcPatternGetBool (p
, FC_SCALABLE
, 0, &b
) == FcResultMatch
189 ASET (entity
, FONT_AVGWIDTH_INDEX
, make_number (0));
191 ASET (entity
, FONT_EXTRA_INDEX
, Fcopy_sequence (extra
));
192 font_put_extra (entity
, QCfont_entity
,
193 Fcons (make_unibyte_string ((char *) file
,
194 strlen ((char *) file
)),
195 make_number (index
)));
200 static Lisp_Object ftfont_generic_family_list
;
203 ftfont_resolve_generic_family (family
, pattern
)
211 family
= Fintern (Fdowncase (SYMBOL_NAME (family
)), Qnil
);
212 if (EQ (family
, Qmono
))
214 else if (EQ (family
, Qsans
) || EQ (family
, Qsans__serif
))
215 family
= Qsans_serif
;
216 slot
= assq_no_quit (family
, ftfont_generic_family_list
);
219 if (! EQ (XCDR (slot
), Qt
))
221 pattern
= FcPatternDuplicate (pattern
);
224 FcPatternDel (pattern
, FC_FOUNDRY
);
225 FcPatternDel (pattern
, FC_FAMILY
);
226 FcPatternAddString (pattern
, FC_FAMILY
, SYMBOL_FcChar8 (family
));
227 FcConfigSubstitute (NULL
, pattern
, FcMatchPattern
);
228 FcDefaultSubstitute (pattern
);
229 match
= FcFontMatch (NULL
, pattern
, &result
);
234 if (FcPatternGetString (match
, FC_FAMILY
, 0, &fam
) == FcResultMatch
)
235 family
= intern ((char *) fam
);
239 XSETCDR (slot
, family
);
241 if (match
) FcPatternDestroy (match
);
242 if (pattern
) FcPatternDestroy (pattern
);
246 struct ftfont_cache_data
249 FcCharSet
*fc_charset
;
253 ftfont_lookup_cache (key
, for_face
)
257 Lisp_Object cache
, val
;
258 struct ftfont_cache_data
*cache_data
;
260 cache
= assoc_no_quit (key
, ft_face_cache
);
263 cache_data
= xmalloc (sizeof (struct ftfont_cache_data
));
264 cache_data
->ft_face
= NULL
;
265 cache_data
->fc_charset
= NULL
;
266 val
= make_save_value (NULL
, 0);
267 XSAVE_VALUE (val
)->integer
= 0;
268 XSAVE_VALUE (val
)->pointer
= cache_data
;
269 cache
= Fcons (key
, val
);
270 ft_face_cache
= Fcons (cache
, ft_face_cache
);
275 cache_data
= XSAVE_VALUE (val
)->pointer
;
277 if (for_face
? ! cache_data
->ft_face
: ! cache_data
->fc_charset
)
279 char *filename
= (char *) SDATA (XCAR (key
));
280 int index
= XINT (XCDR (key
));
285 && FT_Init_FreeType (&ft_library
) != 0)
287 if (FT_New_Face (ft_library
, filename
, index
, &cache_data
->ft_face
)
298 pat
= FcPatternBuild (0, FC_FILE
, FcTypeString
, (FcChar8
*) filename
,
299 FC_INDEX
, FcTypeInteger
, index
, NULL
);
300 objset
= FcObjectSetBuild (FC_CHARSET
, NULL
);
301 fontset
= FcFontList (NULL
, pat
, objset
);
302 xassert (fontset
&& fontset
->nfont
> 0);
303 if (FcPatternGetCharSet (fontset
->fonts
[0], FC_CHARSET
, 0, &charset
)
305 cache_data
->fc_charset
= FcCharSetCopy (charset
);
307 cache_data
->fc_charset
= FcCharSetCreate ();
308 FcFontSetDestroy (fontset
);
309 FcObjectSetDestroy (objset
);
310 FcPatternDestroy (pat
);
317 ftfont_get_fc_charset (entity
)
320 Lisp_Object val
, cache
;
321 struct ftfont_cache_data
*cache_data
;
323 val
= assq_no_quit (QCfont_entity
, AREF (entity
, FONT_EXTRA_INDEX
));
324 xassert (CONSP (val
));
326 cache
= ftfont_lookup_cache (val
, 0);
328 cache_data
= XSAVE_VALUE (val
)->pointer
;
329 return cache_data
->fc_charset
;
334 ftfont_get_otf (ftfont_info
)
335 struct ftfont_info
*ftfont_info
;
339 if (ftfont_info
->otf
)
340 return ftfont_info
->otf
;
341 if (! ftfont_info
->maybe_otf
)
343 otf
= OTF_open_ft_face (ftfont_info
->ft_size
->face
);
344 if (! otf
|| OTF_get_table (otf
, "head") < 0)
348 ftfont_info
->maybe_otf
= 0;
351 ftfont_info
->otf
= otf
;
354 #endif /* HAVE_LIBOTF */
356 static Lisp_Object ftfont_get_cache
P_ ((FRAME_PTR
));
357 static Lisp_Object ftfont_list
P_ ((Lisp_Object
, Lisp_Object
));
358 static Lisp_Object ftfont_match
P_ ((Lisp_Object
, Lisp_Object
));
359 static Lisp_Object ftfont_list_family
P_ ((Lisp_Object
));
360 static Lisp_Object ftfont_open
P_ ((FRAME_PTR
, Lisp_Object
, int));
361 static void ftfont_close
P_ ((FRAME_PTR
, struct font
*));
362 static int ftfont_has_char
P_ ((Lisp_Object
, int));
363 static unsigned ftfont_encode_char
P_ ((struct font
*, int));
364 static int ftfont_text_extents
P_ ((struct font
*, unsigned *, int,
365 struct font_metrics
*));
366 static int ftfont_get_bitmap
P_ ((struct font
*, unsigned,
367 struct font_bitmap
*, int));
368 static int ftfont_anchor_point
P_ ((struct font
*, unsigned, int,
370 static Lisp_Object ftfont_otf_capability
P_ ((struct font
*));
371 static Lisp_Object ftfont_shape
P_ ((Lisp_Object
));
373 struct font_driver ftfont_driver
=
376 0, /* case insensitive */
384 /* We can't draw a text without device dependent functions. */
390 /* We can't draw a text without device dependent functions. */
398 ftfont_otf_capability
,
399 #else /* not HAVE_LIBOTF */
401 #endif /* not HAVE_LIBOTF */
405 #if defined (HAVE_M17N_FLT) && defined (HAVE_LIBOTF)
407 #else /* not (HAVE_M17N_FLT && HAVE_LIBOTF) */
409 #endif /* not (HAVE_M17N_FLT && HAVE_LIBOTF) */
412 extern Lisp_Object QCname
;
418 return freetype_font_cache
;
422 ftfont_get_charset (registry
)
423 Lisp_Object registry
;
425 char *str
= (char *) SDATA (SYMBOL_NAME (registry
));
426 char *re
= alloca (SBYTES (SYMBOL_NAME (registry
)) * 2 + 1);
430 for (i
= j
= 0; i
< SBYTES (SYMBOL_NAME (registry
)); i
++, j
++)
434 else if (str
[i
] == '*')
441 regexp
= make_unibyte_string (re
, j
);
442 for (i
= 0; fc_charset_table
[i
].name
; i
++)
443 if (fast_c_string_match_ignore_case (regexp
, fc_charset_table
[i
].name
) >= 0)
445 if (! fc_charset_table
[i
].name
)
447 if (! fc_charset_table
[i
].fc_charset
)
449 FcCharSet
*charset
= FcCharSetCreate ();
450 int *uniquifier
= fc_charset_table
[i
].uniquifier
;
454 for (j
= 0; uniquifier
[j
]; j
++)
455 if (! FcCharSetAddChar (charset
, uniquifier
[j
]))
457 FcCharSetDestroy (charset
);
460 fc_charset_table
[i
].fc_charset
= charset
;
468 unsigned int script_tag
, langsys_tag
;
470 unsigned int *features
[2];
473 #define OTF_SYM_TAG(SYM, TAG) \
475 unsigned char *p = SDATA (SYMBOL_NAME (SYM)); \
476 TAG = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]; \
479 #define OTF_TAG_STR(TAG, P) \
481 (P)[0] = (char) (TAG >> 24); \
482 (P)[1] = (char) ((TAG >> 16) & 0xFF); \
483 (P)[2] = (char) ((TAG >> 8) & 0xFF); \
484 (P)[3] = (char) (TAG & 0xFF); \
488 #define OTF_TAG_SYM(SYM, TAG) \
492 OTF_TAG_STR (TAG, str); \
493 (SYM) = font_intern_prop (str, 4, 1); \
497 static struct OpenTypeSpec
*
498 ftfont_get_open_type_spec (Lisp_Object otf_spec
)
500 struct OpenTypeSpec
*spec
= malloc (sizeof (struct OpenTypeSpec
));
506 spec
->script
= XCAR (otf_spec
);
507 if (! NILP (spec
->script
))
509 OTF_SYM_TAG (spec
->script
, spec
->script_tag
);
510 val
= assq_no_quit (spec
->script
, Votf_script_alist
);
511 if (CONSP (val
) && SYMBOLP (XCDR (val
)))
512 spec
->script
= XCDR (val
);
517 spec
->script_tag
= 0x44464C54; /* "DFLT" */
518 otf_spec
= XCDR (otf_spec
);
519 val
= XCAR (otf_spec
);
521 OTF_SYM_TAG (val
, spec
->langsys_tag
);
523 spec
->langsys_tag
= 0;
524 spec
->nfeatures
[0] = spec
->nfeatures
[1] = 0;
525 for (i
= 0; i
< 2; i
++)
529 otf_spec
= XCDR (otf_spec
);
532 val
= XCAR (otf_spec
);
536 spec
->features
[i
] = malloc (sizeof (int) * XINT (len
));
537 if (! spec
->features
[i
])
539 if (i
> 0 && spec
->features
[0])
540 free (spec
->features
[0]);
544 for (j
= 0, negative
= 0; CONSP (val
); val
= XCDR (val
))
546 if (NILP (XCAR (val
)))
552 OTF_SYM_TAG (XCAR (val
), tag
);
553 spec
->features
[i
][j
++] = negative
? tag
& 0x80000000 : tag
;
556 spec
->nfeatures
[i
] = j
;
561 static FcPattern
*ftfont_spec_pattern
P_ ((Lisp_Object
, char *,
562 struct OpenTypeSpec
**));
565 ftfont_spec_pattern (spec
, otlayout
, otspec
)
568 struct OpenTypeSpec
**otspec
;
570 Lisp_Object tmp
, extra
;
571 FcPattern
*pattern
= NULL
;
572 FcCharSet
*charset
= NULL
;
573 FcLangSet
*langset
= NULL
;
577 Lisp_Object script
= Qnil
;
578 Lisp_Object registry
;
581 if (! NILP (AREF (spec
, FONT_ADSTYLE_INDEX
))
582 && SBYTES (SYMBOL_NAME (AREF (spec
, FONT_ADSTYLE_INDEX
))) > 0)
583 /* Fontconfig doesn't support adstyle property. */
585 if ((n
= FONT_SLANT_NUMERIC (spec
)) >= 0
587 /* Fontconfig doesn't support reverse-italic/obligue. */
590 if (INTEGERP (AREF (spec
, FONT_DPI_INDEX
)))
591 dpi
= XINT (AREF (spec
, FONT_DPI_INDEX
));
592 if (INTEGERP (AREF (spec
, FONT_AVGWIDTH_INDEX
))
593 && XINT (AREF (spec
, FONT_AVGWIDTH_INDEX
)) == 0)
596 registry
= AREF (spec
, FONT_REGISTRY_INDEX
);
598 || EQ (registry
, Qascii_0
)
599 || EQ (registry
, Qiso10646_1
)
600 || EQ (registry
, Qunicode_bmp
)
601 || EQ (registry
, Qunicode_sip
))
607 fc_charset_idx
= ftfont_get_charset (registry
);
608 if (fc_charset_idx
< 0)
610 charset
= fc_charset_table
[fc_charset_idx
].fc_charset
;
611 lang
= (FcChar8
*) fc_charset_table
[fc_charset_idx
].lang
;
614 langset
= FcLangSetCreate ();
617 FcLangSetAdd (langset
, lang
);
622 for (extra
= AREF (spec
, FONT_EXTRA_INDEX
);
623 CONSP (extra
); extra
= XCDR (extra
))
625 Lisp_Object key
, val
;
627 key
= XCAR (XCAR (extra
)), val
= XCDR (XCAR (extra
));
630 else if (EQ (key
, QClang
))
633 langset
= FcLangSetCreate ();
638 if (! FcLangSetAdd (langset
, SYMBOL_FcChar8 (val
)))
642 for (; CONSP (val
); val
= XCDR (val
))
643 if (SYMBOLP (XCAR (val
))
644 && ! FcLangSetAdd (langset
, SYMBOL_FcChar8 (XCAR (val
))))
647 else if (EQ (key
, QCotf
))
649 *otspec
= ftfont_get_open_type_spec (val
);
652 strcat (otlayout
, "otlayout:");
653 OTF_TAG_STR ((*otspec
)->script_tag
, otlayout
+ 9);
654 script
= (*otspec
)->script
;
656 else if (EQ (key
, QCscript
))
658 else if (EQ (key
, QCscalable
))
659 scalable
= ! NILP (val
);
662 if (! NILP (script
) && ! charset
)
664 Lisp_Object chars
= assq_no_quit (script
, Vscript_representative_chars
);
668 charset
= FcCharSetCreate ();
671 for (chars
= XCDR (chars
); CONSP (chars
); chars
= XCDR (chars
))
672 if (CHARACTERP (XCAR (chars
))
673 && ! FcCharSetAddChar (charset
, XUINT (XCAR (chars
))))
678 pattern
= FcPatternCreate ();
681 tmp
= AREF (spec
, FONT_FOUNDRY_INDEX
);
683 && ! FcPatternAddString (pattern
, FC_FOUNDRY
, SYMBOL_FcChar8 (tmp
)))
685 tmp
= AREF (spec
, FONT_FAMILY_INDEX
);
687 && ! FcPatternAddString (pattern
, FC_FAMILY
, SYMBOL_FcChar8 (tmp
)))
690 && ! FcPatternAddCharSet (pattern
, FC_CHARSET
, charset
))
693 && ! FcPatternAddLangSet (pattern
, FC_LANG
, langset
))
696 && ! FcPatternAddDouble (pattern
, FC_DPI
, dpi
))
699 && ! FcPatternAddBool (pattern
, FC_SCALABLE
, scalable
? FcTrue
: FcFalse
))
705 /* We come here because of unexpected error in fontconfig API call
706 (usually insufficient memory). */
709 FcPatternDestroy (pattern
);
714 if ((*otspec
)->nfeatures
[0] > 0)
715 free ((*otspec
)->features
[0]);
716 if ((*otspec
)->nfeatures
[1] > 0)
717 free ((*otspec
)->features
[1]);
723 if (langset
) FcLangSetDestroy (langset
);
724 if (charset
&& fc_charset_idx
< 0) FcCharSetDestroy (charset
);
729 ftfont_list (frame
, spec
)
730 Lisp_Object frame
, spec
;
732 Lisp_Object val
= Qnil
, family
;
735 FcFontSet
*fontset
= NULL
;
736 FcObjectSet
*objset
= NULL
;
737 char otlayout
[15]; /* For "otlayout:XXXX" */
738 struct OpenTypeSpec
*otspec
= NULL
;
741 if (! fc_initialized
)
747 pattern
= ftfont_spec_pattern (spec
, otlayout
, &otspec
);
750 if (INTEGERP (AREF (spec
, FONT_SPACING_INDEX
)))
751 spacing
= XINT (AREF (spec
, FONT_SPACING_INDEX
));
752 family
= AREF (spec
, FONT_FAMILY_INDEX
);
755 Lisp_Object resolved
;
757 resolved
= ftfont_resolve_generic_family (family
, pattern
);
758 if (! NILP (resolved
))
760 FcPatternDel (pattern
, FC_FAMILY
);
761 if (! FcPatternAddString (pattern
, FC_FAMILY
,
762 SYMBOL_FcChar8 (resolved
)))
767 objset
= FcObjectSetBuild (FC_FOUNDRY
, FC_FAMILY
, FC_WEIGHT
, FC_SLANT
,
768 FC_WIDTH
, FC_PIXEL_SIZE
, FC_SPACING
, FC_SCALABLE
,
772 #endif /* FC_CAPABILITY */
780 fontset
= FcFontList (NULL
, pattern
, objset
);
784 /* Need fix because this finds any fonts. */
785 if (fontset
->nfont
== 0 && ! NILP (family
))
787 /* Try maching with configuration. For instance, the
788 configuration may specify "Nimbus Mono L" as an alias of
790 FcPattern
*pat
= FcPatternBuild (0, FC_FAMILY
, FcTypeString
,
791 SYMBOL_FcChar8 (family
), NULL
);
794 if (FcConfigSubstitute (NULL
, pat
, FcMatchPattern
) == FcTrue
)
797 FcPatternGetString (pat
, FC_FAMILY
, i
, &fam
) == FcResultMatch
;
800 FcPatternDel (pattern
, FC_FAMILY
);
801 FcPatternAddString (pattern
, FC_FAMILY
, fam
);
802 FcFontSetDestroy (fontset
);
803 fontset
= FcFontList (NULL
, pattern
, objset
);
804 if (fontset
->nfont
> 0)
810 for (i
= 0; i
< fontset
->nfont
; i
++)
818 if ((FcPatternGetInteger (fontset
->fonts
[i
], FC_SPACING
, 0, &this)
829 if (FcPatternGetString (fontset
->fonts
[i
], FC_CAPABILITY
, 0,
830 &this) != FcResultMatch
831 || ! strstr ((char *) this, otlayout
))
834 #endif /* FC_CAPABILITY */
841 if (FcPatternGetString (fontset
->fonts
[i
], FC_FILE
, 0, &file
)
844 otf
= OTF_open ((char *) file
);
847 if (OTF_check_features (otf
, 1,
848 otspec
->script_tag
, otspec
->langsys_tag
,
850 otspec
->nfeatures
[0]) != 1
851 || OTF_check_features (otf
, 0,
852 otspec
->script_tag
, otspec
->langsys_tag
,
854 otspec
->nfeatures
[1]) != 1)
857 #endif /* HAVE_LIBOTF */
858 entity
= ftfont_pattern_entity (fontset
->fonts
[i
],
859 AREF (spec
, FONT_EXTRA_INDEX
));
861 val
= Fcons (entity
, val
);
863 font_add_log ("ftfont-list", spec
, val
);
867 /* We come here because of unexpected error in fontconfig API call
868 (usually insufficient memory). */
872 if (objset
) FcObjectSetDestroy (objset
);
873 if (fontset
) FcFontSetDestroy (fontset
);
874 if (pattern
) FcPatternDestroy (pattern
);
879 ftfont_match (frame
, spec
)
880 Lisp_Object frame
, spec
;
883 FcPattern
*pattern
, *match
= NULL
;
885 char otlayout
[15]; /* For "otlayout:XXXX" */
886 struct OpenTypeSpec
*otspec
= NULL
;
888 if (! fc_initialized
)
894 pattern
= ftfont_spec_pattern (spec
, otlayout
, &otspec
);
898 if (INTEGERP (AREF (spec
, FONT_SIZE_INDEX
)))
902 value
.type
= FcTypeDouble
;
903 value
.u
.d
= XINT (AREF (spec
, FONT_SIZE_INDEX
));
904 FcPatternAdd (pattern
, FC_PIXEL_SIZE
, value
, FcFalse
);
906 if (FcConfigSubstitute (NULL
, pattern
, FcMatchPattern
) == FcTrue
)
908 FcDefaultSubstitute (pattern
);
909 match
= FcFontMatch (NULL
, pattern
, &result
);
912 entity
= ftfont_pattern_entity (match
, AREF (spec
, FONT_EXTRA_INDEX
));
913 FcPatternDestroy (match
);
914 if (! NILP (AREF (spec
, FONT_FAMILY_INDEX
))
915 && NILP (assq_no_quit (AREF (spec
, FONT_FAMILY_INDEX
),
916 ftfont_generic_family_list
))
917 && NILP (Fstring_equal (AREF (spec
, FONT_FAMILY_INDEX
),
918 AREF (entity
, FONT_FAMILY_INDEX
))))
922 FcPatternDestroy (pattern
);
924 font_add_log ("ftfont-match", spec
, entity
);
929 ftfont_list_family (frame
)
933 FcPattern
*pattern
= NULL
;
934 FcFontSet
*fontset
= NULL
;
935 FcObjectSet
*objset
= NULL
;
938 if (! fc_initialized
)
944 pattern
= FcPatternCreate ();
947 objset
= FcObjectSetBuild (FC_FAMILY
, NULL
);
950 fontset
= FcFontList (NULL
, pattern
, objset
);
955 for (i
= 0; i
< fontset
->nfont
; i
++)
957 FcPattern
*pat
= fontset
->fonts
[i
];
960 if (FcPatternGetString (pat
, FC_FAMILY
, 0, &str
) == FcResultMatch
)
961 list
= Fcons (intern ((char *) str
), list
);
965 if (objset
) FcObjectSetDestroy (objset
);
966 if (fontset
) FcFontSetDestroy (fontset
);
967 if (pattern
) FcPatternDestroy (pattern
);
974 ftfont_open (f
, entity
, pixel_size
)
979 struct ftfont_info
*ftfont_info
;
981 struct ftfont_cache_data
*cache_data
;
985 Lisp_Object val
, filename
, index
, cache
, font_object
;
992 val
= assq_no_quit (QCfont_entity
, AREF (entity
, FONT_EXTRA_INDEX
));
996 cache
= ftfont_lookup_cache (val
, 1);
999 filename
= XCAR (val
);
1002 cache_data
= XSAVE_VALUE (XCDR (cache
))->pointer
;
1003 ft_face
= cache_data
->ft_face
;
1004 if (XSAVE_VALUE (val
)->integer
> 0)
1006 /* FT_Face in this cache is already used by the different size. */
1007 if (FT_New_Size (ft_face
, &ft_size
) != 0)
1009 if (FT_Activate_Size (ft_size
) != 0)
1011 FT_Done_Size (ft_size
);
1015 XSAVE_VALUE (val
)->integer
++;
1016 size
= XINT (AREF (entity
, FONT_SIZE_INDEX
));
1019 if (FT_Set_Pixel_Sizes (ft_face
, size
, size
) != 0)
1021 if (XSAVE_VALUE (val
)->integer
== 0)
1022 FT_Done_Face (ft_face
);
1026 font_object
= font_make_object (VECSIZE (struct ftfont_info
), entity
, size
);
1027 ASET (font_object
, FONT_TYPE_INDEX
, Qfreetype
);
1028 len
= font_unparse_xlfd (entity
, size
, name
, 256);
1030 ASET (font_object
, FONT_NAME_INDEX
, make_unibyte_string (name
, len
));
1031 len
= font_unparse_fcname (entity
, size
, name
, 256);
1033 ASET (font_object
, FONT_FULLNAME_INDEX
, make_unibyte_string (name
, len
));
1035 ASET (font_object
, FONT_FULLNAME_INDEX
,
1036 AREF (font_object
, FONT_NAME_INDEX
));
1037 ASET (font_object
, FONT_FILE_INDEX
, filename
);
1038 ASET (font_object
, FONT_FORMAT_INDEX
, ftfont_font_format (NULL
, filename
));
1039 font
= XFONT_OBJECT (font_object
);
1040 ftfont_info
= (struct ftfont_info
*) font
;
1041 ftfont_info
->ft_size
= ft_face
->size
;
1042 ftfont_info
->index
= XINT (index
);
1044 ftfont_info
->maybe_otf
= ft_face
->face_flags
& FT_FACE_FLAG_SFNT
;
1045 ftfont_info
->otf
= NULL
;
1046 #endif /* HAVE_LIBOTF */
1047 font
->pixel_size
= size
;
1048 font
->driver
= &ftfont_driver
;
1049 font
->encoding_charset
= font
->repertory_charset
= -1;
1051 upEM
= ft_face
->units_per_EM
;
1052 scalable
= (INTEGERP (AREF (entity
, FONT_AVGWIDTH_INDEX
))
1053 && XINT (AREF (entity
, FONT_AVGWIDTH_INDEX
)) == 0);
1056 font
->ascent
= ft_face
->ascender
* size
/ upEM
;
1057 font
->descent
= - ft_face
->descender
* size
/ upEM
;
1058 font
->height
= ft_face
->height
* size
/ upEM
;
1062 font
->ascent
= ft_face
->size
->metrics
.ascender
>> 6;
1063 font
->descent
= - ft_face
->size
->metrics
.descender
>> 6;
1064 font
->height
= ft_face
->size
->metrics
.height
>> 6;
1066 if (INTEGERP (AREF (entity
, FONT_SPACING_INDEX
)))
1067 spacing
= XINT (AREF (entity
, FONT_SPACING_INDEX
));
1069 spacing
= FC_PROPORTIONAL
;
1070 if (spacing
!= FC_PROPORTIONAL
)
1071 font
->min_width
= font
->average_width
= font
->space_width
1072 = (scalable
? ft_face
->max_advance_width
* size
/ upEM
1073 : ft_face
->size
->metrics
.max_advance
>> 6);
1078 font
->min_width
= font
->average_width
= font
->space_width
= 0;
1079 for (i
= 32, n
= 0; i
< 127; i
++)
1080 if (FT_Load_Char (ft_face
, i
, FT_LOAD_DEFAULT
) != 0)
1082 int this_width
= ft_face
->glyph
->metrics
.horiAdvance
>> 6;
1085 && (! font
->min_width
|| font
->min_width
> this_width
))
1086 font
->min_width
= this_width
;
1088 font
->space_width
= this_width
;
1089 font
->average_width
+= this_width
;
1093 font
->average_width
/= n
;
1096 font
->baseline_offset
= 0;
1097 font
->relative_compose
= 0;
1098 font
->default_ascent
= 0;
1099 font
->vertical_centering
= 0;
1102 font
->underline_position
= -ft_face
->underline_position
* size
/ upEM
;
1103 font
->underline_thickness
= -ft_face
->underline_thickness
* size
/ upEM
;
1107 font
->underline_position
= -1;
1108 font
->underline_thickness
= 0;
1115 ftfont_close (f
, font
)
1119 struct ftfont_info
*ftfont_info
= (struct ftfont_info
*) font
;
1120 Lisp_Object val
, cache
;
1122 val
= Fcons (font
->props
[FONT_FILE_INDEX
], make_number (ftfont_info
->index
));
1123 cache
= ftfont_lookup_cache (val
, 1);
1124 xassert (CONSP (cache
));
1126 (XSAVE_VALUE (val
)->integer
)--;
1127 if (XSAVE_VALUE (val
)->integer
== 0)
1129 struct ftfont_cache_data
*cache_data
= XSAVE_VALUE (val
)->pointer
;
1131 FT_Done_Face (cache_data
->ft_face
);
1133 if (ftfont_info
->otf
)
1134 OTF_close (ftfont_info
->otf
);
1136 cache_data
->ft_face
= NULL
;
1139 FT_Done_Size (ftfont_info
->ft_size
);
1143 ftfont_has_char (entity
, c
)
1147 FcCharSet
*charset
= ftfont_get_fc_charset (entity
);
1149 return (FcCharSetHasChar (charset
, c
) == FcTrue
);
1153 ftfont_encode_char (font
, c
)
1157 struct ftfont_info
*ftfont_info
= (struct ftfont_info
*) font
;
1158 FT_Face ft_face
= ftfont_info
->ft_size
->face
;
1159 FT_ULong charcode
= c
;
1160 FT_UInt code
= FT_Get_Char_Index (ft_face
, charcode
);
1162 return (code
> 0 ? code
: FONT_INVALID_CODE
);
1166 ftfont_text_extents (font
, code
, nglyphs
, metrics
)
1170 struct font_metrics
*metrics
;
1172 struct ftfont_info
*ftfont_info
= (struct ftfont_info
*) font
;
1173 FT_Face ft_face
= ftfont_info
->ft_size
->face
;
1177 if (ftfont_info
->ft_size
!= ft_face
->size
)
1178 FT_Activate_Size (ftfont_info
->ft_size
);
1180 bzero (metrics
, sizeof (struct font_metrics
));
1181 for (i
= 0, first
= 1; i
< nglyphs
; i
++)
1183 if (FT_Load_Glyph (ft_face
, code
[i
], FT_LOAD_DEFAULT
) == 0)
1185 FT_Glyph_Metrics
*m
= &ft_face
->glyph
->metrics
;
1191 metrics
->lbearing
= m
->horiBearingX
>> 6;
1192 metrics
->rbearing
= (m
->horiBearingX
+ m
->width
) >> 6;
1193 metrics
->ascent
= m
->horiBearingY
>> 6;
1194 metrics
->descent
= (m
->horiBearingY
+ m
->height
) >> 6;
1200 if (metrics
->lbearing
> width
+ (m
->horiBearingX
>> 6))
1201 metrics
->lbearing
= width
+ (m
->horiBearingX
>> 6);
1202 if (metrics
->rbearing
1203 < width
+ ((m
->horiBearingX
+ m
->width
) >> 6))
1205 = width
+ ((m
->horiBearingX
+ m
->width
) >> 6);
1206 if (metrics
->ascent
< (m
->horiBearingY
>> 6))
1207 metrics
->ascent
= m
->horiBearingY
>> 6;
1208 if (metrics
->descent
> ((m
->horiBearingY
+ m
->height
) >> 6))
1209 metrics
->descent
= (m
->horiBearingY
+ m
->height
) >> 6;
1211 width
+= m
->horiAdvance
>> 6;
1215 width
+= font
->space_width
;
1219 metrics
->width
= width
;
1225 ftfont_get_bitmap (font
, code
, bitmap
, bits_per_pixel
)
1228 struct font_bitmap
*bitmap
;
1231 struct ftfont_info
*ftfont_info
= (struct ftfont_info
*) font
;
1232 FT_Face ft_face
= ftfont_info
->ft_size
->face
;
1233 FT_Int32 load_flags
= FT_LOAD_RENDER
;
1235 if (ftfont_info
->ft_size
!= ft_face
->size
)
1236 FT_Activate_Size (ftfont_info
->ft_size
);
1237 if (bits_per_pixel
== 1)
1239 #ifdef FT_LOAD_TARGET_MONO
1240 load_flags
|= FT_LOAD_TARGET_MONO
;
1242 load_flags
|= FT_LOAD_MONOCHROME
;
1245 else if (bits_per_pixel
!= 8)
1246 /* We don't support such a rendering. */
1249 if (FT_Load_Glyph (ft_face
, code
, load_flags
) != 0)
1251 bitmap
->bits_per_pixel
1252 = (ft_face
->glyph
->bitmap
.pixel_mode
== FT_PIXEL_MODE_MONO
? 1
1253 : ft_face
->glyph
->bitmap
.pixel_mode
== FT_PIXEL_MODE_GRAY
? 8
1254 : ft_face
->glyph
->bitmap
.pixel_mode
== FT_PIXEL_MODE_LCD
? 8
1255 : ft_face
->glyph
->bitmap
.pixel_mode
== FT_PIXEL_MODE_LCD_V
? 8
1257 if (bitmap
->bits_per_pixel
< 0)
1258 /* We don't suport that kind of pixel mode. */
1260 bitmap
->rows
= ft_face
->glyph
->bitmap
.rows
;
1261 bitmap
->width
= ft_face
->glyph
->bitmap
.width
;
1262 bitmap
->pitch
= ft_face
->glyph
->bitmap
.pitch
;
1263 bitmap
->buffer
= ft_face
->glyph
->bitmap
.buffer
;
1264 bitmap
->left
= ft_face
->glyph
->bitmap_left
;
1265 bitmap
->top
= ft_face
->glyph
->bitmap_top
;
1266 bitmap
->advance
= ft_face
->glyph
->metrics
.horiAdvance
>> 6;
1267 bitmap
->extra
= NULL
;
1273 ftfont_anchor_point (font
, code
, index
, x
, y
)
1279 struct ftfont_info
*ftfont_info
= (struct ftfont_info
*) font
;
1280 FT_Face ft_face
= ftfont_info
->ft_size
->face
;
1282 if (ftfont_info
->ft_size
!= ft_face
->size
)
1283 FT_Activate_Size (ftfont_info
->ft_size
);
1284 if (FT_Load_Glyph (ft_face
, code
, FT_LOAD_DEFAULT
) != 0)
1286 if (ft_face
->glyph
->format
!= FT_GLYPH_FORMAT_OUTLINE
)
1288 if (index
>= ft_face
->glyph
->outline
.n_points
)
1290 *x
= ft_face
->glyph
->outline
.points
[index
].x
;
1291 *y
= ft_face
->glyph
->outline
.points
[index
].y
;
1298 ftfont_otf_features (gsub_gpos
)
1299 OTF_GSUB_GPOS
*gsub_gpos
;
1301 Lisp_Object scripts
, langsyses
, features
, sym
;
1304 for (scripts
= Qnil
, i
= gsub_gpos
->ScriptList
.ScriptCount
- 1; i
>= 0; i
--)
1306 OTF_Script
*otf_script
= gsub_gpos
->ScriptList
.Script
+ i
;
1308 for (langsyses
= Qnil
, j
= otf_script
->LangSysCount
- 1; j
>= -1; j
--)
1310 OTF_LangSys
*otf_langsys
;
1313 otf_langsys
= otf_script
->LangSys
+ j
;
1314 else if (otf_script
->DefaultLangSysOffset
)
1315 otf_langsys
= &otf_script
->DefaultLangSys
;
1319 for (features
= Qnil
, k
= otf_langsys
->FeatureCount
- 1; k
>= 0; k
--)
1321 OTF_TAG_SYM (sym
, gsub_gpos
->FeatureList
.Feature
[k
].FeatureTag
);
1322 features
= Fcons (sym
, features
);
1325 OTF_TAG_SYM (sym
, otf_script
->LangSysRecord
[j
].LangSysTag
);
1328 langsyses
= Fcons (Fcons (sym
, features
), langsyses
);
1331 OTF_TAG_SYM (sym
, gsub_gpos
->ScriptList
.Script
[i
].ScriptTag
);
1332 scripts
= Fcons (Fcons (sym
, langsyses
), scripts
);
1340 ftfont_otf_capability (font
)
1343 struct ftfont_info
*ftfont_info
= (struct ftfont_info
*) font
;
1344 OTF
*otf
= ftfont_get_otf (ftfont_info
);
1345 Lisp_Object gsub_gpos
;
1349 gsub_gpos
= Fcons (Qnil
, Qnil
);
1350 if (OTF_get_table (otf
, "GSUB") == 0)
1351 XSETCAR (gsub_gpos
, ftfont_otf_features (otf
->gsub
));
1352 if (OTF_get_table (otf
, "GPOS") == 0)
1353 XSETCDR (gsub_gpos
, ftfont_otf_features (otf
->gpos
));
1357 #ifdef HAVE_M17N_FLT
1368 ftfont_get_glyph_id (font
, gstring
, from
, to
)
1370 MFLTGlyphString
*gstring
;
1373 struct MFLTFontFT
*flt_font_ft
= (struct MFLTFontFT
*) font
;
1374 FT_Face ft_face
= flt_font_ft
->ft_face
;
1377 for (g
= gstring
->glyphs
+ from
; from
< to
; g
++, from
++)
1380 FT_UInt code
= FT_Get_Char_Index (ft_face
, g
->code
);
1382 g
->code
= code
> 0 ? code
: FONT_INVALID_CODE
;
1389 ftfont_get_metrics (font
, gstring
, from
, to
)
1391 MFLTGlyphString
*gstring
;
1394 struct MFLTFontFT
*flt_font_ft
= (struct MFLTFontFT
*) font
;
1395 FT_Face ft_face
= flt_font_ft
->ft_face
;
1398 for (g
= gstring
->glyphs
+ from
; from
< to
; g
++, from
++)
1401 if (g
->code
!= FONT_INVALID_CODE
)
1403 FT_Glyph_Metrics
*m
;
1405 if (FT_Load_Glyph (ft_face
, g
->code
, FT_LOAD_DEFAULT
) != 0)
1407 m
= &ft_face
->glyph
->metrics
;
1409 g
->lbearing
= m
->horiBearingX
;
1410 g
->rbearing
= m
->horiBearingX
+ m
->width
;
1411 g
->ascent
= m
->horiBearingY
;
1412 g
->descent
= m
->height
- m
->horiBearingY
;
1413 g
->xadv
= m
->horiAdvance
;
1418 g
->rbearing
= g
->xadv
= flt_font_ft
->font
->space_width
<< 6;
1419 g
->ascent
= flt_font_ft
->font
->ascent
<< 6;
1420 g
->descent
= flt_font_ft
->font
->descent
<< 6;
1429 ftfont_check_otf (MFLTFont
*font
, MFLTOtfSpec
*spec
)
1431 struct MFLTFontFT
*flt_font_ft
= (struct MFLTFontFT
*) font
;
1432 OTF
*otf
= flt_font_ft
->otf
;
1436 for (i
= 0; i
< 2; i
++)
1438 if (! spec
->features
[i
])
1440 for (n
= 0; spec
->features
[i
][n
]; n
++);
1441 tags
= alloca (sizeof (OTF_Tag
) * n
);
1442 for (n
= 0, negative
= 0; spec
->features
[i
][n
]; n
++)
1444 if (spec
->features
[i
][n
] == 0xFFFFFFFF)
1447 tags
[n
- 1] = spec
->features
[i
][n
] | 0x80000000;
1449 tags
[n
] = spec
->features
[i
][n
];
1451 if (n
- negative
> 0
1452 && OTF_check_features (otf
, i
== 0, spec
->script
, spec
->langsys
,
1453 tags
, n
- negative
) != 1)
1459 #define DEVICE_DELTA(table, size) \
1460 (((size) >= (table).StartSize && (size) <= (table).EndSize) \
1461 ? (table).DeltaValue[(size) - (table).StartSize] << 6 \
1465 adjust_anchor (FT_Face ft_face
, OTF_Anchor
*anchor
,
1466 unsigned code
, int x_ppem
, int y_ppem
, int *x
, int *y
)
1468 if (anchor
->AnchorFormat
== 2)
1470 FT_Outline
*outline
;
1471 int ap
= anchor
->f
.f1
.AnchorPoint
;
1473 FT_Load_Glyph (ft_face
, (FT_UInt
) code
, FT_LOAD_MONOCHROME
);
1474 outline
= &ft_face
->glyph
->outline
;
1475 if (ap
< outline
->n_points
)
1477 *x
= outline
->points
[ap
].x
<< 6;
1478 *y
= outline
->points
[ap
].y
<< 6;
1481 else if (anchor
->AnchorFormat
== 3)
1483 if (anchor
->f
.f2
.XDeviceTable
.offset
)
1484 *x
+= DEVICE_DELTA (anchor
->f
.f2
.XDeviceTable
, x_ppem
);
1485 if (anchor
->f
.f2
.YDeviceTable
.offset
)
1486 *y
+= DEVICE_DELTA (anchor
->f
.f2
.YDeviceTable
, y_ppem
);
1490 static OTF_GlyphString otf_gstring
;
1493 ftfont_drive_otf (font
, spec
, in
, from
, to
, out
, adjustment
)
1496 MFLTGlyphString
*in
;
1498 MFLTGlyphString
*out
;
1499 MFLTGlyphAdjustment
*adjustment
;
1501 struct MFLTFontFT
*flt_font_ft
= (struct MFLTFontFT
*) font
;
1502 FT_Face ft_face
= flt_font_ft
->ft_face
;
1503 OTF
*otf
= flt_font_ft
->otf
;
1504 int len
= to
- from
;
1507 char script
[5], *langsys
= NULL
;
1508 char *gsub_features
= NULL
, *gpos_features
= NULL
;
1512 OTF_tag_name (spec
->script
, script
);
1515 langsys
= alloca (5);
1516 OTF_tag_name (spec
->langsys
, langsys
);
1518 for (i
= 0; i
< 2; i
++)
1522 if (spec
->features
[i
] && spec
->features
[i
][1] != 0xFFFFFFFF)
1524 for (j
= 0; spec
->features
[i
][j
]; j
++);
1526 p
= gsub_features
= alloca (6 * j
);
1528 p
= gpos_features
= alloca (6 * j
);
1529 for (j
= 0; spec
->features
[i
][j
]; j
++)
1531 if (spec
->features
[i
][j
] == 0xFFFFFFFF)
1532 *p
++ = '*', *p
++ = ',';
1535 OTF_tag_name (spec
->features
[i
][j
], p
);
1544 if (otf_gstring
.size
== 0)
1546 otf_gstring
.glyphs
= (OTF_Glyph
*) malloc (sizeof (OTF_Glyph
) * len
);
1547 otf_gstring
.size
= len
;
1549 else if (otf_gstring
.size
< len
)
1551 otf_gstring
.glyphs
= (OTF_Glyph
*) realloc (otf_gstring
.glyphs
,
1552 sizeof (OTF_Glyph
) * len
);
1553 otf_gstring
.size
= len
;
1555 otf_gstring
.used
= len
;
1556 memset (otf_gstring
.glyphs
, 0, sizeof (OTF_Glyph
) * len
);
1557 for (i
= 0; i
< len
; i
++)
1559 otf_gstring
.glyphs
[i
].c
= in
->glyphs
[from
+ i
].c
;
1560 otf_gstring
.glyphs
[i
].glyph_id
= in
->glyphs
[from
+ i
].code
;
1563 OTF_drive_gdef (otf
, &otf_gstring
);
1568 if (OTF_drive_gsub (otf
, &otf_gstring
, script
, langsys
, gsub_features
)
1571 if (out
->allocated
< out
->used
+ otf_gstring
.used
)
1573 for (i
= 0, otfg
= otf_gstring
.glyphs
; i
< otf_gstring
.used
; )
1576 int min_from
, max_to
;
1579 g
= out
->glyphs
+ out
->used
;
1580 *g
= in
->glyphs
[from
+ otfg
->f
.index
.from
];
1581 if (g
->code
!= otfg
->glyph_id
)
1584 g
->code
= otfg
->glyph_id
;
1590 if (otfg
->f
.index
.from
< otfg
->f
.index
.to
)
1592 /* OTFG substitutes multiple glyphs in IN. */
1593 for (j
= from
+ otfg
->f
.index
.from
+ 1;
1594 j
<= from
+ otfg
->f
.index
.to
; j
++)
1596 if (min_from
> in
->glyphs
[j
].from
)
1597 min_from
= in
->glyphs
[j
].from
;
1598 if (max_to
< in
->glyphs
[j
].to
)
1599 max_to
= in
->glyphs
[j
].to
;
1604 for (i
++, otfg
++; (i
< otf_gstring
.used
1605 && otfg
->f
.index
.from
== otfg
[-1].f
.index
.from
);
1608 g
= out
->glyphs
+ out
->used
;
1609 *g
= in
->glyphs
[from
+ otfg
->f
.index
.to
];
1610 if (g
->code
!= otfg
->glyph_id
)
1613 g
->code
= otfg
->glyph_id
;
1622 if (out
->allocated
< out
->used
+ len
)
1624 for (i
= 0; i
< len
; i
++)
1625 out
->glyphs
[out
->used
++] = in
->glyphs
[from
+ i
];
1630 MFLTGlyph
*base
= NULL
, *mark
= NULL
, *g
;
1631 int x_ppem
, y_ppem
, x_scale
, y_scale
;
1633 if (OTF_drive_gpos (otf
, &otf_gstring
, script
, langsys
, gpos_features
)
1637 x_ppem
= ft_face
->size
->metrics
.x_ppem
;
1638 y_ppem
= ft_face
->size
->metrics
.y_ppem
;
1639 x_scale
= ft_face
->size
->metrics
.x_scale
;
1640 y_scale
= ft_face
->size
->metrics
.y_scale
;
1642 for (i
= 0, otfg
= otf_gstring
.glyphs
, g
= out
->glyphs
+ gidx
;
1643 i
< otf_gstring
.used
; i
++, otfg
++, g
++)
1647 if (! otfg
->glyph_id
)
1649 switch (otfg
->positioning_type
)
1653 case 1: /* Single */
1656 int format
= otfg
->f
.f1
.format
;
1658 if (format
& OTF_XPlacement
)
1660 = otfg
->f
.f1
.value
->XPlacement
* x_scale
/ 0x10000;
1661 if (format
& OTF_XPlaDevice
)
1663 += DEVICE_DELTA (otfg
->f
.f1
.value
->XPlaDevice
, x_ppem
);
1664 if (format
& OTF_YPlacement
)
1666 = - (otfg
->f
.f1
.value
->YPlacement
* y_scale
/ 0x10000);
1667 if (format
& OTF_YPlaDevice
)
1669 -= DEVICE_DELTA (otfg
->f
.f1
.value
->YPlaDevice
, y_ppem
);
1670 if (format
& OTF_XAdvance
)
1672 += otfg
->f
.f1
.value
->XAdvance
* x_scale
/ 0x10000;
1673 if (format
& OTF_XAdvDevice
)
1675 += DEVICE_DELTA (otfg
->f
.f1
.value
->XAdvDevice
, x_ppem
);
1676 if (format
& OTF_YAdvance
)
1678 += otfg
->f
.f1
.value
->YAdvance
* y_scale
/ 0x10000;
1679 if (format
& OTF_YAdvDevice
)
1681 += DEVICE_DELTA (otfg
->f
.f1
.value
->YAdvDevice
, y_ppem
);
1682 adjustment
[i
].set
= 1;
1685 case 3: /* Cursive */
1686 /* Not yet supported. */
1688 case 4: /* Mark-to-Base */
1689 case 5: /* Mark-to-Ligature */
1693 goto label_adjust_anchor
;
1694 default: /* i.e. case 6 Mark-to-Mark */
1699 label_adjust_anchor
:
1701 int base_x
, base_y
, mark_x
, mark_y
;
1702 int this_from
, this_to
;
1704 base_x
= otfg
->f
.f4
.base_anchor
->XCoordinate
* x_scale
/ 0x10000;
1705 base_y
= otfg
->f
.f4
.base_anchor
->YCoordinate
* y_scale
/ 0x10000;
1706 mark_x
= otfg
->f
.f4
.mark_anchor
->XCoordinate
* x_scale
/ 0x10000;
1707 mark_y
= otfg
->f
.f4
.mark_anchor
->YCoordinate
* y_scale
/ 0x10000;;
1709 if (otfg
->f
.f4
.base_anchor
->AnchorFormat
!= 1)
1710 adjust_anchor (ft_face
, otfg
->f
.f4
.base_anchor
,
1711 prev
->code
, x_ppem
, y_ppem
, &base_x
, &base_y
);
1712 if (otfg
->f
.f4
.mark_anchor
->AnchorFormat
!= 1)
1713 adjust_anchor (ft_face
, otfg
->f
.f4
.mark_anchor
, g
->code
,
1714 x_ppem
, y_ppem
, &mark_x
, &mark_y
);
1715 adjustment
[i
].xoff
= (base_x
- mark_x
);
1716 adjustment
[i
].yoff
= - (base_y
- mark_y
);
1717 adjustment
[i
].back
= (g
- prev
);
1718 adjustment
[i
].xadv
= 0;
1719 adjustment
[i
].advance_is_absolute
= 1;
1720 adjustment
[i
].set
= 1;
1721 this_from
= g
->from
;
1723 for (j
= 0; prev
+ j
< g
; j
++)
1725 if (this_from
> prev
[j
].from
)
1726 this_from
= prev
[j
].from
;
1727 if (this_to
< prev
[j
].to
)
1728 this_to
= prev
[j
].to
;
1730 for (; prev
<= g
; prev
++)
1732 prev
->from
= this_from
;
1737 if (otfg
->GlyphClass
== OTF_GlyphClass0
)
1739 else if (otfg
->GlyphClass
== OTF_GlyphClassMark
)
1748 if (out
->allocated
< out
->used
+ len
)
1750 font
->get_metrics (font
, in
, from
, to
);
1751 memcpy (out
->glyphs
+ out
->used
, in
->glyphs
+ from
,
1752 sizeof (MFLTGlyph
) * len
);
1757 static MFLTGlyphString gstring
;
1759 static int m17n_flt_initialized
;
1761 extern Lisp_Object QCfamily
;
1764 ftfont_shape_by_flt (lgstring
, font
, ft_face
, otf
)
1765 Lisp_Object lgstring
;
1770 EMACS_UINT len
= LGSTRING_LENGTH (lgstring
);
1772 struct MFLTFontFT flt_font_ft
;
1774 if (! m17n_flt_initialized
)
1777 m17n_flt_initialized
= 1;
1780 for (i
= 0; i
< len
; i
++)
1781 if (NILP (LGSTRING_GLYPH (lgstring
, i
)))
1785 if (gstring
.allocated
== 0)
1787 gstring
.allocated
= len
* 2;
1788 gstring
.glyph_size
= sizeof (MFLTGlyph
);
1789 gstring
.glyphs
= malloc (sizeof (MFLTGlyph
) * gstring
.allocated
);
1791 else if (gstring
.allocated
< len
* 2)
1793 gstring
.allocated
= len
* 2;
1794 gstring
.glyphs
= realloc (gstring
.glyphs
,
1795 sizeof (MFLTGlyph
) * gstring
.allocated
);
1797 for (i
= 0; i
< len
; i
++)
1798 gstring
.glyphs
[i
].c
= LGLYPH_CHAR (LGSTRING_GLYPH (lgstring
, i
));
1803 Lisp_Object family
= Ffont_get (LGSTRING_FONT (lgstring
), QCfamily
);
1806 flt_font_ft
.flt_font
.family
= Mnil
;
1808 flt_font_ft
.flt_font
.family
1809 = msymbol ((char *) SDATA (SYMBOL_NAME (family
)));
1811 flt_font_ft
.flt_font
.x_ppem
= ft_face
->size
->metrics
.x_ppem
;
1812 flt_font_ft
.flt_font
.y_ppem
= ft_face
->size
->metrics
.y_ppem
;
1813 flt_font_ft
.flt_font
.get_glyph_id
= ftfont_get_glyph_id
;
1814 flt_font_ft
.flt_font
.get_metrics
= ftfont_get_metrics
;
1815 flt_font_ft
.flt_font
.check_otf
= ftfont_check_otf
;
1816 flt_font_ft
.flt_font
.drive_otf
= ftfont_drive_otf
;
1817 flt_font_ft
.flt_font
.internal
= NULL
;
1818 flt_font_ft
.font
= font
;
1819 flt_font_ft
.ft_face
= ft_face
;
1820 flt_font_ft
.otf
= otf
;
1821 for (i
= 0; i
< 3; i
++)
1823 int result
= mflt_run (&gstring
, 0, len
, &flt_font_ft
.flt_font
, NULL
);
1826 gstring
.allocated
+= gstring
.allocated
;
1827 gstring
.glyphs
= realloc (gstring
.glyphs
,
1828 sizeof (MFLTGlyph
) * gstring
.allocated
);
1830 if (gstring
.used
> LGSTRING_LENGTH (lgstring
))
1832 for (i
= 0; i
< gstring
.used
; i
++)
1834 MFLTGlyph
*g
= gstring
.glyphs
+ i
;
1836 g
->from
= LGLYPH_FROM (LGSTRING_GLYPH (lgstring
, g
->from
));
1837 g
->to
= LGLYPH_TO (LGSTRING_GLYPH (lgstring
, g
->to
));
1840 for (i
= 0; i
< gstring
.used
; i
++)
1842 Lisp_Object lglyph
= LGSTRING_GLYPH (lgstring
, i
);
1843 MFLTGlyph
*g
= gstring
.glyphs
+ i
;
1847 lglyph
= Fmake_vector (make_number (LGLYPH_SIZE
), Qnil
);
1848 LGSTRING_SET_GLYPH (lgstring
, i
, lglyph
);
1850 LGLYPH_SET_FROM (lglyph
, g
->from
);
1851 LGLYPH_SET_TO (lglyph
, g
->to
);
1852 LGLYPH_SET_CHAR (lglyph
, g
->c
);
1853 LGLYPH_SET_CODE (lglyph
, g
->code
);
1854 LGLYPH_SET_WIDTH (lglyph
, g
->xadv
>> 6);
1855 LGLYPH_SET_LBEARING (lglyph
, g
->lbearing
>> 6);
1856 LGLYPH_SET_RBEARING (lglyph
, g
->rbearing
>> 6);
1857 LGLYPH_SET_ASCENT (lglyph
, g
->ascent
>> 6);
1858 LGLYPH_SET_DESCENT (lglyph
, g
->descent
>> 6);
1863 vec
= Fmake_vector (make_number (3), Qnil
);
1864 ASET (vec
, 0, make_number (g
->xoff
>> 6));
1865 ASET (vec
, 1, make_number (g
->yoff
>> 6));
1866 ASET (vec
, 2, make_number (g
->xadv
>> 6));
1867 LGLYPH_SET_ADJUSTMENT (lglyph
, vec
);
1870 return make_number (i
);
1874 ftfont_shape (lgstring
)
1875 Lisp_Object lgstring
;
1878 struct ftfont_info
*ftfont_info
;
1881 CHECK_FONT_GET_OBJECT (LGSTRING_FONT (lgstring
), font
);
1882 ftfont_info
= (struct ftfont_info
*) font
;
1883 otf
= ftfont_get_otf (ftfont_info
);
1885 return make_number (0);
1886 return ftfont_shape_by_flt (lgstring
, font
, ftfont_info
->ft_size
->face
, otf
);
1889 #endif /* HAVE_M17N_FLT */
1890 #endif /* HAVE_LIBOTF */
1893 ftfont_font_format (FcPattern
*pattern
, Lisp_Object filename
)
1897 #ifdef FC_FONTFORMAT
1900 if (FcPatternGetString (pattern
, FC_FONTFORMAT
, 0, &str
) != FcResultMatch
)
1902 if (strcmp ((char *) str
, "TrueType") == 0)
1903 return intern ("truetype");
1904 if (strcmp ((char *) str
, "Type 1") == 0)
1905 return intern ("type1");
1906 if (strcmp ((char *) str
, "PCF") == 0)
1907 return intern ("pcf");
1908 if (strcmp ((char *) str
, "BDF") == 0)
1909 return intern ("bdf");
1911 #endif /* FC_FONTFORMAT */
1912 if (STRINGP (filename
))
1914 int len
= SBYTES (filename
);
1918 str
= (FcChar8
*) (SDATA (filename
) + len
- 4);
1919 if (xstrcasecmp ((char *) str
, ".ttf") == 0)
1920 return intern ("truetype");
1921 if (xstrcasecmp ((char *) str
, ".pfb") == 0)
1922 return intern ("type1");
1923 if (xstrcasecmp ((char *) str
, ".pcf") == 0)
1924 return intern ("pcf");
1925 if (xstrcasecmp ((char *) str
, ".bdf") == 0)
1926 return intern ("bdf");
1929 return intern ("unknown");
1936 DEFSYM (Qfreetype
, "freetype");
1937 DEFSYM (Qmonospace
, "monospace");
1938 DEFSYM (Qsans_serif
, "sans-serif");
1939 DEFSYM (Qserif
, "serif");
1940 DEFSYM (Qmono
, "mono");
1941 DEFSYM (Qsans
, "sans");
1942 DEFSYM (Qsans__serif
, "sans serif");
1944 staticpro (&freetype_font_cache
);
1945 freetype_font_cache
= Fcons (Qt
, Qnil
);
1947 staticpro (&ftfont_generic_family_list
);
1948 ftfont_generic_family_list
1949 = Fcons (Fcons (Qmonospace
, Qt
),
1950 Fcons (Fcons (Qsans_serif
, Qt
),
1951 Fcons (Fcons (Qsans
, Qt
), Qnil
)));
1953 staticpro (&ft_face_cache
);
1954 ft_face_cache
= Qnil
;
1956 ftfont_driver
.type
= Qfreetype
;
1957 register_font_driver (&ftfont_driver
, NULL
);
1960 /* arch-tag: 7cfa432c-33a6-4988-83d2-a82ed8604aca
1961 (do not change this comment) */