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_FAMILY
);
225 FcPatternAddString (pattern
, FC_FAMILY
, SYMBOL_FcChar8 (family
));
226 FcConfigSubstitute (NULL
, pattern
, FcMatchPattern
);
227 FcDefaultSubstitute (pattern
);
228 match
= FcFontMatch (NULL
, pattern
, &result
);
233 if (FcPatternGetString (match
, FC_FAMILY
, 0, &fam
) == FcResultMatch
)
234 family
= intern ((char *) fam
);
238 XSETCDR (slot
, family
);
240 if (match
) FcPatternDestroy (match
);
241 if (pattern
) FcPatternDestroy (pattern
);
245 struct ftfont_cache_data
248 FcCharSet
*fc_charset
;
252 ftfont_lookup_cache (key
, for_face
)
256 Lisp_Object cache
, val
;
257 struct ftfont_cache_data
*cache_data
;
259 cache
= assoc_no_quit (key
, ft_face_cache
);
262 cache_data
= xmalloc (sizeof (struct ftfont_cache_data
));
263 cache_data
->ft_face
= NULL
;
264 cache_data
->fc_charset
= NULL
;
265 val
= make_save_value (NULL
, 0);
266 XSAVE_VALUE (val
)->integer
= 0;
267 XSAVE_VALUE (val
)->pointer
= cache_data
;
268 cache
= Fcons (key
, val
);
269 ft_face_cache
= Fcons (cache
, ft_face_cache
);
274 cache_data
= XSAVE_VALUE (val
)->pointer
;
276 if (for_face
? ! cache_data
->ft_face
: ! cache_data
->fc_charset
)
278 char *filename
= (char *) SDATA (XCAR (key
));
279 int index
= XINT (XCDR (key
));
284 && FT_Init_FreeType (&ft_library
) != 0)
286 if (FT_New_Face (ft_library
, filename
, index
, &cache_data
->ft_face
)
297 pat
= FcPatternBuild (0, FC_FILE
, FcTypeString
, (FcChar8
*) filename
,
298 FC_INDEX
, FcTypeInteger
, index
, NULL
);
299 objset
= FcObjectSetBuild (FC_CHARSET
, NULL
);
300 fontset
= FcFontList (NULL
, pat
, objset
);
301 xassert (fontset
&& fontset
->nfont
> 0);
302 if (FcPatternGetCharSet (fontset
->fonts
[0], FC_CHARSET
, 0, &charset
)
304 cache_data
->fc_charset
= FcCharSetCopy (charset
);
306 cache_data
->fc_charset
= FcCharSetCreate ();
307 FcFontSetDestroy (fontset
);
308 FcObjectSetDestroy (objset
);
309 FcPatternDestroy (pat
);
316 ftfont_get_fc_charset (entity
)
319 Lisp_Object val
, cache
;
320 struct ftfont_cache_data
*cache_data
;
322 val
= assq_no_quit (QCfont_entity
, AREF (entity
, FONT_EXTRA_INDEX
));
323 xassert (CONSP (val
));
325 cache
= ftfont_lookup_cache (val
, 0);
327 cache_data
= XSAVE_VALUE (val
)->pointer
;
328 return cache_data
->fc_charset
;
333 ftfont_get_otf (ftfont_info
)
334 struct ftfont_info
*ftfont_info
;
338 if (ftfont_info
->otf
)
339 return ftfont_info
->otf
;
340 if (! ftfont_info
->maybe_otf
)
342 otf
= OTF_open_ft_face (ftfont_info
->ft_size
->face
);
343 if (! otf
|| OTF_get_table (otf
, "head") < 0)
347 ftfont_info
->maybe_otf
= 0;
350 ftfont_info
->otf
= otf
;
353 #endif /* HAVE_LIBOTF */
355 static Lisp_Object ftfont_get_cache
P_ ((FRAME_PTR
));
356 static Lisp_Object ftfont_list
P_ ((Lisp_Object
, Lisp_Object
));
357 static Lisp_Object ftfont_match
P_ ((Lisp_Object
, Lisp_Object
));
358 static Lisp_Object ftfont_list_family
P_ ((Lisp_Object
));
359 static Lisp_Object ftfont_open
P_ ((FRAME_PTR
, Lisp_Object
, int));
360 static void ftfont_close
P_ ((FRAME_PTR
, struct font
*));
361 static int ftfont_has_char
P_ ((Lisp_Object
, int));
362 static unsigned ftfont_encode_char
P_ ((struct font
*, int));
363 static int ftfont_text_extents
P_ ((struct font
*, unsigned *, int,
364 struct font_metrics
*));
365 static int ftfont_get_bitmap
P_ ((struct font
*, unsigned,
366 struct font_bitmap
*, int));
367 static int ftfont_anchor_point
P_ ((struct font
*, unsigned, int,
369 static Lisp_Object ftfont_otf_capability
P_ ((struct font
*));
370 static Lisp_Object ftfont_shape
P_ ((Lisp_Object
));
372 struct font_driver ftfont_driver
=
375 0, /* case insensitive */
383 /* We can't draw a text without device dependent functions. */
389 /* We can't draw a text without device dependent functions. */
397 ftfont_otf_capability
,
398 #else /* not HAVE_LIBOTF */
400 #endif /* not HAVE_LIBOTF */
404 #if defined (HAVE_M17N_FLT) && defined (HAVE_LIBOTF)
406 #else /* not (HAVE_M17N_FLT && HAVE_LIBOTF) */
408 #endif /* not (HAVE_M17N_FLT && HAVE_LIBOTF) */
411 extern Lisp_Object QCname
;
417 return freetype_font_cache
;
421 ftfont_get_charset (registry
)
422 Lisp_Object registry
;
424 char *str
= (char *) SDATA (SYMBOL_NAME (registry
));
425 char *re
= alloca (SBYTES (SYMBOL_NAME (registry
)) * 2 + 1);
429 for (i
= j
= 0; i
< SBYTES (SYMBOL_NAME (registry
)); i
++, j
++)
433 else if (str
[i
] == '*')
440 regexp
= make_unibyte_string (re
, j
);
441 for (i
= 0; fc_charset_table
[i
].name
; i
++)
442 if (fast_c_string_match_ignore_case (regexp
, fc_charset_table
[i
].name
) >= 0)
444 if (! fc_charset_table
[i
].name
)
446 if (! fc_charset_table
[i
].fc_charset
)
448 FcCharSet
*charset
= FcCharSetCreate ();
449 int *uniquifier
= fc_charset_table
[i
].uniquifier
;
453 for (j
= 0; uniquifier
[j
]; j
++)
454 if (! FcCharSetAddChar (charset
, uniquifier
[j
]))
456 FcCharSetDestroy (charset
);
459 fc_charset_table
[i
].fc_charset
= charset
;
467 unsigned int script_tag
, langsys_tag
;
469 unsigned int *features
[2];
472 #define OTF_SYM_TAG(SYM, TAG) \
474 unsigned char *p = SDATA (SYMBOL_NAME (SYM)); \
475 TAG = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]; \
478 #define OTF_TAG_STR(TAG, P) \
480 (P)[0] = (char) (TAG >> 24); \
481 (P)[1] = (char) ((TAG >> 16) & 0xFF); \
482 (P)[2] = (char) ((TAG >> 8) & 0xFF); \
483 (P)[3] = (char) (TAG & 0xFF); \
487 #define OTF_TAG_SYM(SYM, TAG) \
491 OTF_TAG_STR (TAG, str); \
492 (SYM) = font_intern_prop (str, 4, 1); \
496 static struct OpenTypeSpec
*
497 ftfont_get_open_type_spec (Lisp_Object otf_spec
)
499 struct OpenTypeSpec
*spec
= malloc (sizeof (struct OpenTypeSpec
));
505 spec
->script
= XCAR (otf_spec
);
506 if (! NILP (spec
->script
))
508 OTF_SYM_TAG (spec
->script
, spec
->script_tag
);
509 val
= assq_no_quit (spec
->script
, Votf_script_alist
);
510 if (CONSP (val
) && SYMBOLP (XCDR (val
)))
511 spec
->script
= XCDR (val
);
516 spec
->script_tag
= 0x44464C54; /* "DFLT" */
517 otf_spec
= XCDR (otf_spec
);
518 val
= XCAR (otf_spec
);
520 OTF_SYM_TAG (val
, spec
->langsys_tag
);
522 spec
->langsys_tag
= 0;
523 spec
->nfeatures
[0] = spec
->nfeatures
[1] = 0;
524 for (i
= 0; i
< 2; i
++)
528 otf_spec
= XCDR (otf_spec
);
531 val
= XCAR (otf_spec
);
535 spec
->features
[i
] = malloc (sizeof (int) * XINT (len
));
536 if (! spec
->features
[i
])
538 if (i
> 0 && spec
->features
[0])
539 free (spec
->features
[0]);
543 for (j
= 0, negative
= 0; CONSP (val
); val
= XCDR (val
))
545 if (NILP (XCAR (val
)))
551 OTF_SYM_TAG (XCAR (val
), tag
);
552 spec
->features
[i
][j
++] = negative
? tag
& 0x80000000 : tag
;
555 spec
->nfeatures
[i
] = j
;
560 static FcPattern
*ftfont_spec_pattern
P_ ((Lisp_Object
, char *,
561 struct OpenTypeSpec
**));
564 ftfont_spec_pattern (spec
, otlayout
, otspec
)
567 struct OpenTypeSpec
**otspec
;
569 Lisp_Object tmp
, extra
;
570 FcPattern
*pattern
= NULL
;
571 FcCharSet
*charset
= NULL
;
572 FcLangSet
*langset
= NULL
;
576 Lisp_Object script
= Qnil
;
577 Lisp_Object registry
;
580 if (! NILP (AREF (spec
, FONT_ADSTYLE_INDEX
))
581 && SBYTES (SYMBOL_NAME (AREF (spec
, FONT_ADSTYLE_INDEX
))) > 0)
582 /* Fontconfig doesn't support adstyle property. */
584 if ((n
= FONT_SLANT_NUMERIC (spec
)) >= 0
586 /* Fontconfig doesn't support reverse-italic/obligue. */
589 if (INTEGERP (AREF (spec
, FONT_DPI_INDEX
)))
590 dpi
= XINT (AREF (spec
, FONT_DPI_INDEX
));
591 if (INTEGERP (AREF (spec
, FONT_AVGWIDTH_INDEX
))
592 && XINT (AREF (spec
, FONT_AVGWIDTH_INDEX
)) == 0)
595 registry
= AREF (spec
, FONT_REGISTRY_INDEX
);
597 || EQ (registry
, Qascii_0
)
598 || EQ (registry
, Qiso10646_1
)
599 || EQ (registry
, Qunicode_bmp
)
600 || EQ (registry
, Qunicode_sip
))
606 fc_charset_idx
= ftfont_get_charset (registry
);
607 if (fc_charset_idx
< 0)
609 charset
= fc_charset_table
[fc_charset_idx
].fc_charset
;
610 lang
= (FcChar8
*) fc_charset_table
[fc_charset_idx
].lang
;
613 langset
= FcLangSetCreate ();
616 FcLangSetAdd (langset
, lang
);
621 for (extra
= AREF (spec
, FONT_EXTRA_INDEX
);
622 CONSP (extra
); extra
= XCDR (extra
))
624 Lisp_Object key
, val
;
626 key
= XCAR (XCAR (extra
)), val
= XCDR (XCAR (extra
));
629 else if (EQ (key
, QClang
))
632 langset
= FcLangSetCreate ();
637 if (! FcLangSetAdd (langset
, SYMBOL_FcChar8 (val
)))
641 for (; CONSP (val
); val
= XCDR (val
))
642 if (SYMBOLP (XCAR (val
))
643 && ! FcLangSetAdd (langset
, SYMBOL_FcChar8 (XCAR (val
))))
646 else if (EQ (key
, QCotf
))
648 *otspec
= ftfont_get_open_type_spec (val
);
651 strcat (otlayout
, "otlayout:");
652 OTF_TAG_STR ((*otspec
)->script_tag
, otlayout
+ 9);
653 script
= (*otspec
)->script
;
655 else if (EQ (key
, QCscript
))
657 else if (EQ (key
, QCscalable
))
658 scalable
= ! NILP (val
);
661 if (! NILP (script
) && ! charset
)
663 Lisp_Object chars
= assq_no_quit (script
, Vscript_representative_chars
);
667 charset
= FcCharSetCreate ();
670 for (chars
= XCDR (chars
); CONSP (chars
); chars
= XCDR (chars
))
671 if (CHARACTERP (XCAR (chars
))
672 && ! FcCharSetAddChar (charset
, XUINT (XCAR (chars
))))
677 pattern
= FcPatternCreate ();
680 tmp
= AREF (spec
, FONT_FOUNDRY_INDEX
);
682 && ! FcPatternAddString (pattern
, FC_FOUNDRY
, SYMBOL_FcChar8 (tmp
)))
684 tmp
= AREF (spec
, FONT_FAMILY_INDEX
);
686 && ! FcPatternAddString (pattern
, FC_FAMILY
, SYMBOL_FcChar8 (tmp
)))
689 && ! FcPatternAddCharSet (pattern
, FC_CHARSET
, charset
))
692 && ! FcPatternAddLangSet (pattern
, FC_LANG
, langset
))
695 && ! FcPatternAddDouble (pattern
, FC_DPI
, dpi
))
698 && ! FcPatternAddBool (pattern
, FC_SCALABLE
, scalable
? FcTrue
: FcFalse
))
704 /* We come here because of unexpected error in fontconfig API call
705 (usually insufficient memory). */
708 FcPatternDestroy (pattern
);
713 if ((*otspec
)->nfeatures
[0] > 0)
714 free ((*otspec
)->features
[0]);
715 if ((*otspec
)->nfeatures
[1] > 0)
716 free ((*otspec
)->features
[1]);
722 if (langset
) FcLangSetDestroy (langset
);
723 if (charset
&& fc_charset_idx
< 0) FcCharSetDestroy (charset
);
728 ftfont_list (frame
, spec
)
729 Lisp_Object frame
, spec
;
731 Lisp_Object val
= Qnil
, family
;
734 FcFontSet
*fontset
= NULL
;
735 FcObjectSet
*objset
= NULL
;
736 char otlayout
[15]; /* For "otlayout:XXXX" */
737 struct OpenTypeSpec
*otspec
= NULL
;
740 if (! fc_initialized
)
746 pattern
= ftfont_spec_pattern (spec
, otlayout
, &otspec
);
749 if (INTEGERP (AREF (spec
, FONT_SPACING_INDEX
)))
750 spacing
= XINT (AREF (spec
, FONT_SPACING_INDEX
));
751 family
= AREF (spec
, FONT_FAMILY_INDEX
);
754 Lisp_Object resolved
;
756 resolved
= ftfont_resolve_generic_family (family
, pattern
);
757 if (! NILP (resolved
))
759 FcPatternDel (pattern
, FC_FAMILY
);
760 if (! FcPatternAddString (pattern
, FC_FAMILY
,
761 SYMBOL_FcChar8 (resolved
)))
766 objset
= FcObjectSetBuild (FC_FOUNDRY
, FC_FAMILY
, FC_WEIGHT
, FC_SLANT
,
767 FC_WIDTH
, FC_PIXEL_SIZE
, FC_SPACING
, FC_SCALABLE
,
771 #endif /* FC_CAPABILITY */
779 fontset
= FcFontList (NULL
, pattern
, objset
);
783 /* Need fix because this finds any fonts. */
784 if (fontset
->nfont
== 0 && ! NILP (family
))
786 /* Try maching with configuration. For instance, the
787 configuration may specify "Nimbus Mono L" as an alias of
789 FcPattern
*pat
= FcPatternBuild (0, FC_FAMILY
, FcTypeString
,
790 SYMBOL_FcChar8 (family
), NULL
);
793 if (FcConfigSubstitute (NULL
, pat
, FcMatchPattern
) == FcTrue
)
796 FcPatternGetString (pat
, FC_FAMILY
, i
, &fam
) == FcResultMatch
;
799 FcPatternDel (pattern
, FC_FAMILY
);
800 FcPatternAddString (pattern
, FC_FAMILY
, fam
);
801 FcFontSetDestroy (fontset
);
802 fontset
= FcFontList (NULL
, pattern
, objset
);
803 if (fontset
->nfont
> 0)
809 for (i
= 0; i
< fontset
->nfont
; i
++)
817 if ((FcPatternGetInteger (fontset
->fonts
[i
], FC_SPACING
, 0, &this)
828 if (FcPatternGetString (fontset
->fonts
[i
], FC_CAPABILITY
, 0,
829 &this) != FcResultMatch
830 || ! strstr ((char *) this, otlayout
))
833 #endif /* FC_CAPABILITY */
840 if (FcPatternGetString (fontset
->fonts
[i
], FC_FILE
, 0, &file
)
843 otf
= OTF_open ((char *) file
);
846 if (OTF_check_features (otf
, 1,
847 otspec
->script_tag
, otspec
->langsys_tag
,
849 otspec
->nfeatures
[0]) != 1
850 || OTF_check_features (otf
, 0,
851 otspec
->script_tag
, otspec
->langsys_tag
,
853 otspec
->nfeatures
[1]) != 1)
856 #endif /* HAVE_LIBOTF */
857 entity
= ftfont_pattern_entity (fontset
->fonts
[i
],
858 AREF (spec
, FONT_EXTRA_INDEX
));
860 val
= Fcons (entity
, val
);
862 font_add_log ("ftfont-list", spec
, val
);
866 /* We come here because of unexpected error in fontconfig API call
867 (usually insufficient memory). */
871 if (objset
) FcObjectSetDestroy (objset
);
872 if (fontset
) FcFontSetDestroy (fontset
);
873 if (pattern
) FcPatternDestroy (pattern
);
878 ftfont_match (frame
, spec
)
879 Lisp_Object frame
, spec
;
882 FcPattern
*pattern
, *match
= NULL
;
884 char otlayout
[15]; /* For "otlayout:XXXX" */
885 struct OpenTypeSpec
*otspec
= NULL
;
887 if (! fc_initialized
)
893 pattern
= ftfont_spec_pattern (spec
, otlayout
, &otspec
);
897 if (INTEGERP (AREF (spec
, FONT_SIZE_INDEX
)))
901 value
.type
= FcTypeDouble
;
902 value
.u
.d
= XINT (AREF (spec
, FONT_SIZE_INDEX
));
903 FcPatternAdd (pattern
, FC_PIXEL_SIZE
, value
, FcFalse
);
905 if (FcConfigSubstitute (NULL
, pattern
, FcMatchPattern
) == FcTrue
)
907 FcDefaultSubstitute (pattern
);
908 match
= FcFontMatch (NULL
, pattern
, &result
);
911 entity
= ftfont_pattern_entity (match
, AREF (spec
, FONT_EXTRA_INDEX
));
912 FcPatternDestroy (match
);
913 if (! NILP (AREF (spec
, FONT_FAMILY_INDEX
))
914 && NILP (assq_no_quit (AREF (spec
, FONT_FAMILY_INDEX
),
915 ftfont_generic_family_list
))
916 && NILP (Fstring_equal (AREF (spec
, FONT_FAMILY_INDEX
),
917 AREF (entity
, FONT_FAMILY_INDEX
))))
921 FcPatternDestroy (pattern
);
923 font_add_log ("ftfont-match", spec
, entity
);
928 ftfont_list_family (frame
)
932 FcPattern
*pattern
= NULL
;
933 FcFontSet
*fontset
= NULL
;
934 FcObjectSet
*objset
= NULL
;
937 if (! fc_initialized
)
943 pattern
= FcPatternCreate ();
946 objset
= FcObjectSetBuild (FC_FAMILY
, NULL
);
949 fontset
= FcFontList (NULL
, pattern
, objset
);
954 for (i
= 0; i
< fontset
->nfont
; i
++)
956 FcPattern
*pat
= fontset
->fonts
[i
];
959 if (FcPatternGetString (pat
, FC_FAMILY
, 0, &str
) == FcResultMatch
)
960 list
= Fcons (intern ((char *) str
), list
);
964 if (objset
) FcObjectSetDestroy (objset
);
965 if (fontset
) FcFontSetDestroy (fontset
);
966 if (pattern
) FcPatternDestroy (pattern
);
973 ftfont_open (f
, entity
, pixel_size
)
978 struct ftfont_info
*ftfont_info
;
980 struct ftfont_cache_data
*cache_data
;
984 Lisp_Object val
, filename
, index
, cache
, font_object
;
991 val
= assq_no_quit (QCfont_entity
, AREF (entity
, FONT_EXTRA_INDEX
));
995 cache
= ftfont_lookup_cache (val
, 1);
998 filename
= XCAR (val
);
1001 cache_data
= XSAVE_VALUE (XCDR (cache
))->pointer
;
1002 ft_face
= cache_data
->ft_face
;
1003 if (XSAVE_VALUE (val
)->integer
> 0)
1005 /* FT_Face in this cache is already used by the different size. */
1006 if (FT_New_Size (ft_face
, &ft_size
) != 0)
1008 if (FT_Activate_Size (ft_size
) != 0)
1010 FT_Done_Size (ft_size
);
1014 XSAVE_VALUE (val
)->integer
++;
1015 size
= XINT (AREF (entity
, FONT_SIZE_INDEX
));
1018 if (FT_Set_Pixel_Sizes (ft_face
, size
, size
) != 0)
1020 if (XSAVE_VALUE (val
)->integer
== 0)
1021 FT_Done_Face (ft_face
);
1025 font_object
= font_make_object (VECSIZE (struct ftfont_info
), entity
, size
);
1026 ASET (font_object
, FONT_TYPE_INDEX
, Qfreetype
);
1027 len
= font_unparse_xlfd (entity
, size
, name
, 256);
1029 ASET (font_object
, FONT_NAME_INDEX
, make_unibyte_string (name
, len
));
1030 len
= font_unparse_fcname (entity
, size
, name
, 256);
1032 ASET (font_object
, FONT_FULLNAME_INDEX
, make_unibyte_string (name
, len
));
1034 ASET (font_object
, FONT_FULLNAME_INDEX
,
1035 AREF (font_object
, FONT_NAME_INDEX
));
1036 ASET (font_object
, FONT_FILE_INDEX
, filename
);
1037 ASET (font_object
, FONT_FORMAT_INDEX
, ftfont_font_format (NULL
, filename
));
1038 font
= XFONT_OBJECT (font_object
);
1039 ftfont_info
= (struct ftfont_info
*) font
;
1040 ftfont_info
->ft_size
= ft_face
->size
;
1041 ftfont_info
->index
= XINT (index
);
1043 ftfont_info
->maybe_otf
= ft_face
->face_flags
& FT_FACE_FLAG_SFNT
;
1044 ftfont_info
->otf
= NULL
;
1045 #endif /* HAVE_LIBOTF */
1046 font
->pixel_size
= size
;
1047 font
->driver
= &ftfont_driver
;
1048 font
->encoding_charset
= font
->repertory_charset
= -1;
1050 upEM
= ft_face
->units_per_EM
;
1051 scalable
= (INTEGERP (AREF (entity
, FONT_AVGWIDTH_INDEX
))
1052 && XINT (AREF (entity
, FONT_AVGWIDTH_INDEX
)) == 0);
1055 font
->ascent
= ft_face
->ascender
* size
/ upEM
;
1056 font
->descent
= - ft_face
->descender
* size
/ upEM
;
1057 font
->height
= ft_face
->height
* size
/ upEM
;
1061 font
->ascent
= ft_face
->size
->metrics
.ascender
>> 6;
1062 font
->descent
= - ft_face
->size
->metrics
.descender
>> 6;
1063 font
->height
= ft_face
->size
->metrics
.height
>> 6;
1065 if (INTEGERP (AREF (entity
, FONT_SPACING_INDEX
)))
1066 spacing
= XINT (AREF (entity
, FONT_SPACING_INDEX
));
1068 spacing
= FC_PROPORTIONAL
;
1069 if (spacing
!= FC_PROPORTIONAL
)
1070 font
->min_width
= font
->average_width
= font
->space_width
1071 = (scalable
? ft_face
->max_advance_width
* size
/ upEM
1072 : ft_face
->size
->metrics
.max_advance
>> 6);
1077 font
->min_width
= font
->average_width
= font
->space_width
= 0;
1078 for (i
= 32, n
= 0; i
< 127; i
++)
1079 if (FT_Load_Char (ft_face
, i
, FT_LOAD_DEFAULT
) != 0)
1081 int this_width
= ft_face
->glyph
->metrics
.horiAdvance
>> 6;
1084 && (! font
->min_width
|| font
->min_width
> this_width
))
1085 font
->min_width
= this_width
;
1087 font
->space_width
= this_width
;
1088 font
->average_width
+= this_width
;
1092 font
->average_width
/= n
;
1095 font
->baseline_offset
= 0;
1096 font
->relative_compose
= 0;
1097 font
->default_ascent
= 0;
1098 font
->vertical_centering
= 0;
1101 font
->underline_position
= -ft_face
->underline_position
* size
/ upEM
;
1102 font
->underline_thickness
= -ft_face
->underline_thickness
* size
/ upEM
;
1106 font
->underline_position
= -1;
1107 font
->underline_thickness
= 0;
1114 ftfont_close (f
, font
)
1118 struct ftfont_info
*ftfont_info
= (struct ftfont_info
*) font
;
1119 Lisp_Object val
, cache
;
1121 val
= Fcons (font
->props
[FONT_FILE_INDEX
], make_number (ftfont_info
->index
));
1122 cache
= ftfont_lookup_cache (val
, 1);
1123 xassert (CONSP (cache
));
1125 (XSAVE_VALUE (val
)->integer
)--;
1126 if (XSAVE_VALUE (val
)->integer
== 0)
1128 struct ftfont_cache_data
*cache_data
= XSAVE_VALUE (val
)->pointer
;
1130 FT_Done_Face (cache_data
->ft_face
);
1132 if (ftfont_info
->otf
)
1133 OTF_close (ftfont_info
->otf
);
1135 cache_data
->ft_face
= NULL
;
1138 FT_Done_Size (ftfont_info
->ft_size
);
1142 ftfont_has_char (entity
, c
)
1146 FcCharSet
*charset
= ftfont_get_fc_charset (entity
);
1148 return (FcCharSetHasChar (charset
, c
) == FcTrue
);
1152 ftfont_encode_char (font
, c
)
1156 struct ftfont_info
*ftfont_info
= (struct ftfont_info
*) font
;
1157 FT_Face ft_face
= ftfont_info
->ft_size
->face
;
1158 FT_ULong charcode
= c
;
1159 FT_UInt code
= FT_Get_Char_Index (ft_face
, charcode
);
1161 return (code
> 0 ? code
: FONT_INVALID_CODE
);
1165 ftfont_text_extents (font
, code
, nglyphs
, metrics
)
1169 struct font_metrics
*metrics
;
1171 struct ftfont_info
*ftfont_info
= (struct ftfont_info
*) font
;
1172 FT_Face ft_face
= ftfont_info
->ft_size
->face
;
1176 if (ftfont_info
->ft_size
!= ft_face
->size
)
1177 FT_Activate_Size (ftfont_info
->ft_size
);
1179 bzero (metrics
, sizeof (struct font_metrics
));
1180 for (i
= 0; i
< nglyphs
; i
++)
1182 if (FT_Load_Glyph (ft_face
, code
[i
], FT_LOAD_DEFAULT
) == 0)
1184 FT_Glyph_Metrics
*m
= &ft_face
->glyph
->metrics
;
1188 if (metrics
->lbearing
> width
+ (m
->horiBearingX
>> 6))
1189 metrics
->lbearing
= width
+ (m
->horiBearingX
>> 6);
1190 if (metrics
->rbearing
1191 < width
+ ((m
->horiBearingX
+ m
->width
) >> 6))
1193 = width
+ ((m
->horiBearingX
+ m
->width
) >> 6);
1194 if (metrics
->ascent
< (m
->horiBearingY
>> 6))
1195 metrics
->ascent
= m
->horiBearingY
>> 6;
1196 if (metrics
->descent
> ((m
->horiBearingY
+ m
->height
) >> 6))
1197 metrics
->descent
= (m
->horiBearingY
+ m
->height
) >> 6;
1199 width
+= m
->horiAdvance
>> 6;
1203 width
+= font
->space_width
;
1207 metrics
->width
= width
;
1213 ftfont_get_bitmap (font
, code
, bitmap
, bits_per_pixel
)
1216 struct font_bitmap
*bitmap
;
1219 struct ftfont_info
*ftfont_info
= (struct ftfont_info
*) font
;
1220 FT_Face ft_face
= ftfont_info
->ft_size
->face
;
1221 FT_Int32 load_flags
= FT_LOAD_RENDER
;
1223 if (ftfont_info
->ft_size
!= ft_face
->size
)
1224 FT_Activate_Size (ftfont_info
->ft_size
);
1225 if (bits_per_pixel
== 1)
1227 #ifdef FT_LOAD_TARGET_MONO
1228 load_flags
|= FT_LOAD_TARGET_MONO
;
1230 load_flags
|= FT_LOAD_MONOCHROME
;
1233 else if (bits_per_pixel
!= 8)
1234 /* We don't support such a rendering. */
1237 if (FT_Load_Glyph (ft_face
, code
, load_flags
) != 0)
1239 bitmap
->bits_per_pixel
1240 = (ft_face
->glyph
->bitmap
.pixel_mode
== FT_PIXEL_MODE_MONO
? 1
1241 : ft_face
->glyph
->bitmap
.pixel_mode
== FT_PIXEL_MODE_GRAY
? 8
1242 : ft_face
->glyph
->bitmap
.pixel_mode
== FT_PIXEL_MODE_LCD
? 8
1243 : ft_face
->glyph
->bitmap
.pixel_mode
== FT_PIXEL_MODE_LCD_V
? 8
1245 if (bitmap
->bits_per_pixel
< 0)
1246 /* We don't suport that kind of pixel mode. */
1248 bitmap
->rows
= ft_face
->glyph
->bitmap
.rows
;
1249 bitmap
->width
= ft_face
->glyph
->bitmap
.width
;
1250 bitmap
->pitch
= ft_face
->glyph
->bitmap
.pitch
;
1251 bitmap
->buffer
= ft_face
->glyph
->bitmap
.buffer
;
1252 bitmap
->left
= ft_face
->glyph
->bitmap_left
;
1253 bitmap
->top
= ft_face
->glyph
->bitmap_top
;
1254 bitmap
->advance
= ft_face
->glyph
->metrics
.horiAdvance
>> 6;
1255 bitmap
->extra
= NULL
;
1261 ftfont_anchor_point (font
, code
, index
, x
, y
)
1267 struct ftfont_info
*ftfont_info
= (struct ftfont_info
*) font
;
1268 FT_Face ft_face
= ftfont_info
->ft_size
->face
;
1270 if (ftfont_info
->ft_size
!= ft_face
->size
)
1271 FT_Activate_Size (ftfont_info
->ft_size
);
1272 if (FT_Load_Glyph (ft_face
, code
, FT_LOAD_DEFAULT
) != 0)
1274 if (ft_face
->glyph
->format
!= FT_GLYPH_FORMAT_OUTLINE
)
1276 if (index
>= ft_face
->glyph
->outline
.n_points
)
1278 *x
= ft_face
->glyph
->outline
.points
[index
].x
;
1279 *y
= ft_face
->glyph
->outline
.points
[index
].y
;
1286 ftfont_otf_features (gsub_gpos
)
1287 OTF_GSUB_GPOS
*gsub_gpos
;
1289 Lisp_Object scripts
, langsyses
, features
, sym
;
1292 for (scripts
= Qnil
, i
= gsub_gpos
->ScriptList
.ScriptCount
- 1; i
>= 0; i
--)
1294 OTF_Script
*otf_script
= gsub_gpos
->ScriptList
.Script
+ i
;
1296 for (langsyses
= Qnil
, j
= otf_script
->LangSysCount
- 1; j
>= -1; j
--)
1298 OTF_LangSys
*otf_langsys
;
1301 otf_langsys
= otf_script
->LangSys
+ j
;
1302 else if (otf_script
->DefaultLangSysOffset
)
1303 otf_langsys
= &otf_script
->DefaultLangSys
;
1307 for (features
= Qnil
, k
= otf_langsys
->FeatureCount
- 1; k
>= 0; k
--)
1309 OTF_TAG_SYM (sym
, gsub_gpos
->FeatureList
.Feature
[k
].FeatureTag
);
1310 features
= Fcons (sym
, features
);
1313 OTF_TAG_SYM (sym
, otf_script
->LangSysRecord
[j
].LangSysTag
);
1316 langsyses
= Fcons (Fcons (sym
, features
), langsyses
);
1319 OTF_TAG_SYM (sym
, gsub_gpos
->ScriptList
.Script
[i
].ScriptTag
);
1320 scripts
= Fcons (Fcons (sym
, langsyses
), scripts
);
1328 ftfont_otf_capability (font
)
1331 struct ftfont_info
*ftfont_info
= (struct ftfont_info
*) font
;
1332 OTF
*otf
= ftfont_get_otf (ftfont_info
);
1333 Lisp_Object gsub_gpos
;
1337 gsub_gpos
= Fcons (Qnil
, Qnil
);
1338 if (OTF_get_table (otf
, "GSUB") == 0)
1339 XSETCAR (gsub_gpos
, ftfont_otf_features (otf
->gsub
));
1340 if (OTF_get_table (otf
, "GPOS") == 0)
1341 XSETCDR (gsub_gpos
, ftfont_otf_features (otf
->gpos
));
1345 #ifdef HAVE_M17N_FLT
1356 ftfont_get_glyph_id (font
, gstring
, from
, to
)
1358 MFLTGlyphString
*gstring
;
1361 struct MFLTFontFT
*flt_font_ft
= (struct MFLTFontFT
*) font
;
1362 FT_Face ft_face
= flt_font_ft
->ft_face
;
1365 for (g
= gstring
->glyphs
+ from
; from
< to
; g
++, from
++)
1368 FT_UInt code
= FT_Get_Char_Index (ft_face
, g
->code
);
1370 g
->code
= code
> 0 ? code
: FONT_INVALID_CODE
;
1377 ftfont_get_metrics (font
, gstring
, from
, to
)
1379 MFLTGlyphString
*gstring
;
1382 struct MFLTFontFT
*flt_font_ft
= (struct MFLTFontFT
*) font
;
1383 FT_Face ft_face
= flt_font_ft
->ft_face
;
1386 for (g
= gstring
->glyphs
+ from
; from
< to
; g
++, from
++)
1389 if (g
->code
!= FONT_INVALID_CODE
)
1391 FT_Glyph_Metrics
*m
;
1393 if (FT_Load_Glyph (ft_face
, g
->code
, FT_LOAD_DEFAULT
) != 0)
1395 m
= &ft_face
->glyph
->metrics
;
1397 g
->lbearing
= m
->horiBearingX
;
1398 g
->rbearing
= m
->horiBearingX
+ m
->width
;
1399 g
->ascent
= m
->horiBearingY
;
1400 g
->descent
= m
->height
- m
->horiBearingY
;
1401 g
->xadv
= m
->horiAdvance
;
1406 g
->rbearing
= g
->xadv
= flt_font_ft
->font
->space_width
<< 6;
1407 g
->ascent
= flt_font_ft
->font
->ascent
<< 6;
1408 g
->descent
= flt_font_ft
->font
->descent
<< 6;
1417 ftfont_check_otf (MFLTFont
*font
, MFLTOtfSpec
*spec
)
1419 struct MFLTFontFT
*flt_font_ft
= (struct MFLTFontFT
*) font
;
1420 OTF
*otf
= flt_font_ft
->otf
;
1424 for (i
= 0; i
< 2; i
++)
1426 if (! spec
->features
[i
])
1428 for (n
= 0; spec
->features
[i
][n
]; n
++);
1429 tags
= alloca (sizeof (OTF_Tag
) * n
);
1430 for (n
= 0, negative
= 0; spec
->features
[i
][n
]; n
++)
1432 if (spec
->features
[i
][n
] == 0xFFFFFFFF)
1435 tags
[n
- 1] = spec
->features
[i
][n
] | 0x80000000;
1437 tags
[n
] = spec
->features
[i
][n
];
1439 if (n
- negative
> 0
1440 && OTF_check_features (otf
, i
== 0, spec
->script
, spec
->langsys
,
1441 tags
, n
- negative
) != 1)
1447 #define DEVICE_DELTA(table, size) \
1448 (((size) >= (table).StartSize && (size) <= (table).EndSize) \
1449 ? (table).DeltaValue[(size) - (table).StartSize] << 6 \
1453 adjust_anchor (FT_Face ft_face
, OTF_Anchor
*anchor
,
1454 unsigned code
, int x_ppem
, int y_ppem
, int *x
, int *y
)
1456 if (anchor
->AnchorFormat
== 2)
1458 FT_Outline
*outline
;
1459 int ap
= anchor
->f
.f1
.AnchorPoint
;
1461 FT_Load_Glyph (ft_face
, (FT_UInt
) code
, FT_LOAD_MONOCHROME
);
1462 outline
= &ft_face
->glyph
->outline
;
1463 if (ap
< outline
->n_points
)
1465 *x
= outline
->points
[ap
].x
<< 6;
1466 *y
= outline
->points
[ap
].y
<< 6;
1469 else if (anchor
->AnchorFormat
== 3)
1471 if (anchor
->f
.f2
.XDeviceTable
.offset
)
1472 *x
+= DEVICE_DELTA (anchor
->f
.f2
.XDeviceTable
, x_ppem
);
1473 if (anchor
->f
.f2
.YDeviceTable
.offset
)
1474 *y
+= DEVICE_DELTA (anchor
->f
.f2
.YDeviceTable
, y_ppem
);
1478 static OTF_GlyphString otf_gstring
;
1481 ftfont_drive_otf (font
, spec
, in
, from
, to
, out
, adjustment
)
1484 MFLTGlyphString
*in
;
1486 MFLTGlyphString
*out
;
1487 MFLTGlyphAdjustment
*adjustment
;
1489 struct MFLTFontFT
*flt_font_ft
= (struct MFLTFontFT
*) font
;
1490 FT_Face ft_face
= flt_font_ft
->ft_face
;
1491 OTF
*otf
= flt_font_ft
->otf
;
1492 int len
= to
- from
;
1495 char script
[5], *langsys
= NULL
;
1496 char *gsub_features
= NULL
, *gpos_features
= NULL
;
1500 OTF_tag_name (spec
->script
, script
);
1503 langsys
= alloca (5);
1504 OTF_tag_name (spec
->langsys
, langsys
);
1506 for (i
= 0; i
< 2; i
++)
1510 if (spec
->features
[i
] && spec
->features
[i
][1] != 0xFFFFFFFF)
1512 for (j
= 0; spec
->features
[i
][j
]; j
++);
1514 p
= gsub_features
= alloca (6 * j
);
1516 p
= gpos_features
= alloca (6 * j
);
1517 for (j
= 0; spec
->features
[i
][j
]; j
++)
1519 if (spec
->features
[i
][j
] == 0xFFFFFFFF)
1520 *p
++ = '*', *p
++ = ',';
1523 OTF_tag_name (spec
->features
[i
][j
], p
);
1532 if (otf_gstring
.size
== 0)
1534 otf_gstring
.glyphs
= (OTF_Glyph
*) malloc (sizeof (OTF_Glyph
) * len
);
1535 otf_gstring
.size
= len
;
1537 else if (otf_gstring
.size
< len
)
1539 otf_gstring
.glyphs
= (OTF_Glyph
*) realloc (otf_gstring
.glyphs
,
1540 sizeof (OTF_Glyph
) * len
);
1541 otf_gstring
.size
= len
;
1543 otf_gstring
.used
= len
;
1544 memset (otf_gstring
.glyphs
, 0, sizeof (OTF_Glyph
) * len
);
1545 for (i
= 0; i
< len
; i
++)
1547 otf_gstring
.glyphs
[i
].c
= in
->glyphs
[from
+ i
].c
;
1548 otf_gstring
.glyphs
[i
].glyph_id
= in
->glyphs
[from
+ i
].code
;
1551 OTF_drive_gdef (otf
, &otf_gstring
);
1556 if (OTF_drive_gsub (otf
, &otf_gstring
, script
, langsys
, gsub_features
)
1559 if (out
->allocated
< out
->used
+ otf_gstring
.used
)
1561 for (i
= 0, otfg
= otf_gstring
.glyphs
; i
< otf_gstring
.used
; )
1564 int min_from
, max_to
;
1567 g
= out
->glyphs
+ out
->used
;
1568 *g
= in
->glyphs
[from
+ otfg
->f
.index
.from
];
1569 if (g
->code
!= otfg
->glyph_id
)
1572 g
->code
= otfg
->glyph_id
;
1578 if (otfg
->f
.index
.from
< otfg
->f
.index
.to
)
1580 /* OTFG substitutes multiple glyphs in IN. */
1581 for (j
= from
+ otfg
->f
.index
.from
+ 1;
1582 j
<= from
+ otfg
->f
.index
.to
; j
++)
1584 if (min_from
> in
->glyphs
[j
].from
)
1585 min_from
= in
->glyphs
[j
].from
;
1586 if (max_to
< in
->glyphs
[j
].to
)
1587 max_to
= in
->glyphs
[j
].to
;
1592 for (i
++, otfg
++; (i
< otf_gstring
.used
1593 && otfg
->f
.index
.from
== otfg
[-1].f
.index
.from
);
1596 g
= out
->glyphs
+ out
->used
;
1597 *g
= in
->glyphs
[from
+ otfg
->f
.index
.to
];
1598 if (g
->code
!= otfg
->glyph_id
)
1601 g
->code
= otfg
->glyph_id
;
1610 if (out
->allocated
< out
->used
+ len
)
1612 for (i
= 0; i
< len
; i
++)
1613 out
->glyphs
[out
->used
++] = in
->glyphs
[from
+ i
];
1618 MFLTGlyph
*base
= NULL
, *mark
= NULL
, *g
;
1619 int x_ppem
, y_ppem
, x_scale
, y_scale
;
1621 if (OTF_drive_gpos (otf
, &otf_gstring
, script
, langsys
, gpos_features
)
1625 x_ppem
= ft_face
->size
->metrics
.x_ppem
;
1626 y_ppem
= ft_face
->size
->metrics
.y_ppem
;
1627 x_scale
= ft_face
->size
->metrics
.x_scale
;
1628 y_scale
= ft_face
->size
->metrics
.y_scale
;
1630 for (i
= 0, otfg
= otf_gstring
.glyphs
, g
= out
->glyphs
+ gidx
;
1631 i
< otf_gstring
.used
; i
++, otfg
++, g
++)
1635 if (! otfg
->glyph_id
)
1637 switch (otfg
->positioning_type
)
1641 case 1: /* Single */
1644 int format
= otfg
->f
.f1
.format
;
1646 if (format
& OTF_XPlacement
)
1648 = otfg
->f
.f1
.value
->XPlacement
* x_scale
/ 0x10000;
1649 if (format
& OTF_XPlaDevice
)
1651 += DEVICE_DELTA (otfg
->f
.f1
.value
->XPlaDevice
, x_ppem
);
1652 if (format
& OTF_YPlacement
)
1654 = - (otfg
->f
.f1
.value
->YPlacement
* y_scale
/ 0x10000);
1655 if (format
& OTF_YPlaDevice
)
1657 -= DEVICE_DELTA (otfg
->f
.f1
.value
->YPlaDevice
, y_ppem
);
1658 if (format
& OTF_XAdvance
)
1660 += otfg
->f
.f1
.value
->XAdvance
* x_scale
/ 0x10000;
1661 if (format
& OTF_XAdvDevice
)
1663 += DEVICE_DELTA (otfg
->f
.f1
.value
->XAdvDevice
, x_ppem
);
1664 if (format
& OTF_YAdvance
)
1666 += otfg
->f
.f1
.value
->YAdvance
* y_scale
/ 0x10000;
1667 if (format
& OTF_YAdvDevice
)
1669 += DEVICE_DELTA (otfg
->f
.f1
.value
->YAdvDevice
, y_ppem
);
1670 adjustment
[i
].set
= 1;
1673 case 3: /* Cursive */
1674 /* Not yet supported. */
1676 case 4: /* Mark-to-Base */
1677 case 5: /* Mark-to-Ligature */
1681 goto label_adjust_anchor
;
1682 default: /* i.e. case 6 Mark-to-Mark */
1687 label_adjust_anchor
:
1689 int base_x
, base_y
, mark_x
, mark_y
;
1690 int this_from
, this_to
;
1692 base_x
= otfg
->f
.f4
.base_anchor
->XCoordinate
* x_scale
/ 0x10000;
1693 base_y
= otfg
->f
.f4
.base_anchor
->YCoordinate
* y_scale
/ 0x10000;
1694 mark_x
= otfg
->f
.f4
.mark_anchor
->XCoordinate
* x_scale
/ 0x10000;
1695 mark_y
= otfg
->f
.f4
.mark_anchor
->YCoordinate
* y_scale
/ 0x10000;;
1697 if (otfg
->f
.f4
.base_anchor
->AnchorFormat
!= 1)
1698 adjust_anchor (ft_face
, otfg
->f
.f4
.base_anchor
,
1699 prev
->code
, x_ppem
, y_ppem
, &base_x
, &base_y
);
1700 if (otfg
->f
.f4
.mark_anchor
->AnchorFormat
!= 1)
1701 adjust_anchor (ft_face
, otfg
->f
.f4
.mark_anchor
, g
->code
,
1702 x_ppem
, y_ppem
, &mark_x
, &mark_y
);
1703 adjustment
[i
].xoff
= (base_x
- mark_x
);
1704 adjustment
[i
].yoff
= - (base_y
- mark_y
);
1705 adjustment
[i
].back
= (g
- prev
);
1706 adjustment
[i
].xadv
= 0;
1707 adjustment
[i
].advance_is_absolute
= 1;
1708 adjustment
[i
].set
= 1;
1709 this_from
= g
->from
;
1711 for (j
= 0; prev
+ j
< g
; j
++)
1713 if (this_from
> prev
[j
].from
)
1714 this_from
= prev
[j
].from
;
1715 if (this_to
< prev
[j
].to
)
1716 this_to
= prev
[j
].to
;
1718 for (; prev
<= g
; prev
++)
1720 prev
->from
= this_from
;
1725 if (otfg
->GlyphClass
== OTF_GlyphClass0
)
1727 else if (otfg
->GlyphClass
== OTF_GlyphClassMark
)
1736 if (out
->allocated
< out
->used
+ len
)
1738 font
->get_metrics (font
, in
, from
, to
);
1739 memcpy (out
->glyphs
+ out
->used
, in
->glyphs
+ from
,
1740 sizeof (MFLTGlyph
) * len
);
1745 static MFLTGlyphString gstring
;
1747 static int m17n_flt_initialized
;
1749 extern Lisp_Object QCfamily
;
1752 ftfont_shape_by_flt (lgstring
, font
, ft_face
, otf
)
1753 Lisp_Object lgstring
;
1758 EMACS_UINT len
= LGSTRING_LENGTH (lgstring
);
1760 struct MFLTFontFT flt_font_ft
;
1762 if (! m17n_flt_initialized
)
1765 m17n_flt_initialized
= 1;
1768 for (i
= 0; i
< len
; i
++)
1769 if (NILP (LGSTRING_GLYPH (lgstring
, i
)))
1773 if (gstring
.allocated
== 0)
1775 gstring
.allocated
= len
* 2;
1776 gstring
.glyph_size
= sizeof (MFLTGlyph
);
1777 gstring
.glyphs
= malloc (sizeof (MFLTGlyph
) * gstring
.allocated
);
1779 else if (gstring
.allocated
< len
* 2)
1781 gstring
.allocated
= len
* 2;
1782 gstring
.glyphs
= realloc (gstring
.glyphs
,
1783 sizeof (MFLTGlyph
) * gstring
.allocated
);
1785 for (i
= 0; i
< len
; i
++)
1786 gstring
.glyphs
[i
].c
= LGLYPH_CHAR (LGSTRING_GLYPH (lgstring
, i
));
1791 Lisp_Object family
= Ffont_get (LGSTRING_FONT (lgstring
), QCfamily
);
1794 flt_font_ft
.flt_font
.family
= Mnil
;
1796 flt_font_ft
.flt_font
.family
1797 = msymbol ((char *) SDATA (SYMBOL_NAME (family
)));
1799 flt_font_ft
.flt_font
.x_ppem
= ft_face
->size
->metrics
.x_ppem
;
1800 flt_font_ft
.flt_font
.y_ppem
= ft_face
->size
->metrics
.y_ppem
;
1801 flt_font_ft
.flt_font
.get_glyph_id
= ftfont_get_glyph_id
;
1802 flt_font_ft
.flt_font
.get_metrics
= ftfont_get_metrics
;
1803 flt_font_ft
.flt_font
.check_otf
= ftfont_check_otf
;
1804 flt_font_ft
.flt_font
.drive_otf
= ftfont_drive_otf
;
1805 flt_font_ft
.flt_font
.internal
= NULL
;
1806 flt_font_ft
.font
= font
;
1807 flt_font_ft
.ft_face
= ft_face
;
1808 flt_font_ft
.otf
= otf
;
1809 for (i
= 0; i
< 3; i
++)
1811 int result
= mflt_run (&gstring
, 0, len
, &flt_font_ft
.flt_font
, NULL
);
1814 gstring
.allocated
+= gstring
.allocated
;
1815 gstring
.glyphs
= realloc (gstring
.glyphs
,
1816 sizeof (MFLTGlyph
) * gstring
.allocated
);
1818 if (gstring
.used
> LGSTRING_LENGTH (lgstring
))
1820 for (i
= 0; i
< gstring
.used
; i
++)
1822 MFLTGlyph
*g
= gstring
.glyphs
+ i
;
1824 g
->from
= LGLYPH_FROM (LGSTRING_GLYPH (lgstring
, g
->from
));
1825 g
->to
= LGLYPH_TO (LGSTRING_GLYPH (lgstring
, g
->to
));
1828 for (i
= 0; i
< gstring
.used
; i
++)
1830 Lisp_Object lglyph
= LGSTRING_GLYPH (lgstring
, i
);
1831 MFLTGlyph
*g
= gstring
.glyphs
+ i
;
1835 lglyph
= Fmake_vector (make_number (LGLYPH_SIZE
), Qnil
);
1836 LGSTRING_SET_GLYPH (lgstring
, i
, lglyph
);
1838 LGLYPH_SET_FROM (lglyph
, g
->from
);
1839 LGLYPH_SET_TO (lglyph
, g
->to
);
1840 LGLYPH_SET_CHAR (lglyph
, g
->c
);
1841 LGLYPH_SET_CODE (lglyph
, g
->code
);
1842 LGLYPH_SET_WIDTH (lglyph
, g
->xadv
>> 6);
1843 LGLYPH_SET_LBEARING (lglyph
, g
->lbearing
>> 6);
1844 LGLYPH_SET_RBEARING (lglyph
, g
->rbearing
>> 6);
1845 LGLYPH_SET_ASCENT (lglyph
, g
->ascent
>> 6);
1846 LGLYPH_SET_DESCENT (lglyph
, g
->descent
>> 6);
1851 vec
= Fmake_vector (make_number (3), Qnil
);
1852 ASET (vec
, 0, make_number (g
->xoff
>> 6));
1853 ASET (vec
, 1, make_number (g
->yoff
>> 6));
1854 ASET (vec
, 2, make_number (g
->xadv
>> 6));
1855 LGLYPH_SET_ADJUSTMENT (lglyph
, vec
);
1858 return make_number (i
);
1862 ftfont_shape (lgstring
)
1863 Lisp_Object lgstring
;
1866 struct ftfont_info
*ftfont_info
;
1869 CHECK_FONT_GET_OBJECT (LGSTRING_FONT (lgstring
), font
);
1870 ftfont_info
= (struct ftfont_info
*) font
;
1871 otf
= ftfont_get_otf (ftfont_info
);
1873 return make_number (0);
1874 return ftfont_shape_by_flt (lgstring
, font
, ftfont_info
->ft_size
->face
, otf
);
1877 #endif /* HAVE_M17N_FLT */
1878 #endif /* HAVE_LIBOTF */
1881 ftfont_font_format (FcPattern
*pattern
, Lisp_Object filename
)
1885 #ifdef FC_FONTFORMAT
1888 if (FcPatternGetString (pattern
, FC_FONTFORMAT
, 0, &str
) != FcResultMatch
)
1890 if (strcmp ((char *) str
, "TrueType") == 0)
1891 return intern ("truetype");
1892 if (strcmp ((char *) str
, "Type 1") == 0)
1893 return intern ("type1");
1894 if (strcmp ((char *) str
, "PCF") == 0)
1895 return intern ("pcf");
1896 if (strcmp ((char *) str
, "BDF") == 0)
1897 return intern ("bdf");
1899 #endif /* FC_FONTFORMAT */
1900 if (STRINGP (filename
))
1902 int len
= SBYTES (filename
);
1906 str
= (FcChar8
*) (SDATA (filename
) + len
- 4);
1907 if (xstrcasecmp ((char *) str
, ".ttf") == 0)
1908 return intern ("truetype");
1909 if (xstrcasecmp ((char *) str
, ".pfb") == 0)
1910 return intern ("type1");
1911 if (xstrcasecmp ((char *) str
, ".pcf") == 0)
1912 return intern ("pcf");
1913 if (xstrcasecmp ((char *) str
, ".bdf") == 0)
1914 return intern ("bdf");
1917 return intern ("unknown");
1924 DEFSYM (Qfreetype
, "freetype");
1925 DEFSYM (Qmonospace
, "monospace");
1926 DEFSYM (Qsans_serif
, "sans-serif");
1927 DEFSYM (Qserif
, "serif");
1928 DEFSYM (Qmono
, "mono");
1929 DEFSYM (Qsans
, "sans");
1930 DEFSYM (Qsans__serif
, "sans serif");
1932 staticpro (&freetype_font_cache
);
1933 freetype_font_cache
= Fcons (Qt
, Qnil
);
1935 staticpro (&ftfont_generic_family_list
);
1936 ftfont_generic_family_list
1937 = Fcons (Fcons (Qmonospace
, Qt
),
1938 Fcons (Fcons (Qsans_serif
, Qt
),
1939 Fcons (Fcons (Qsans
, Qt
), Qnil
)));
1941 staticpro (&ft_face_cache
);
1942 ft_face_cache
= Qnil
;
1944 ftfont_driver
.type
= Qfreetype
;
1945 register_font_driver (&ftfont_driver
, NULL
);
1948 /* arch-tag: 7cfa432c-33a6-4988-83d2-a82ed8604aca
1949 (do not change this comment) */