1 /* ftfont.c -- FreeType font driver.
2 Copyright (C) 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
3 Copyright (C) 2006, 2007, 2008, 2009
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"},
131 { "unicode-sip", { 0x20000 }},
135 extern Lisp_Object Qc
, Qm
, Qp
, Qd
;
138 ftfont_pattern_entity (p
, extra
)
149 if (FcPatternGetString (p
, FC_FILE
, 0, (FcChar8
**) &file
) != FcResultMatch
)
151 if (FcPatternGetInteger (p
, FC_INDEX
, 0, &index
) != FcResultMatch
)
154 entity
= font_make_entity ();
156 ASET (entity
, FONT_TYPE_INDEX
, Qfreetype
);
157 ASET (entity
, FONT_REGISTRY_INDEX
, Qiso10646_1
);
159 if (FcPatternGetString (p
, FC_FOUNDRY
, 0, (FcChar8
**) &str
) == FcResultMatch
)
160 ASET (entity
, FONT_FOUNDRY_INDEX
, font_intern_prop (str
, strlen (str
), 1));
161 if (FcPatternGetString (p
, FC_FAMILY
, 0, (FcChar8
**) &str
) == FcResultMatch
)
162 ASET (entity
, FONT_FAMILY_INDEX
, font_intern_prop (str
, strlen (str
), 1));
163 if (FcPatternGetInteger (p
, FC_WEIGHT
, 0, &numeric
) == FcResultMatch
)
165 if (numeric
>= FC_WEIGHT_REGULAR
&& numeric
< FC_WEIGHT_MEDIUM
)
166 numeric
= FC_WEIGHT_MEDIUM
;
167 FONT_SET_STYLE (entity
, FONT_WEIGHT_INDEX
, make_number (numeric
));
169 if (FcPatternGetInteger (p
, FC_SLANT
, 0, &numeric
) == FcResultMatch
)
172 FONT_SET_STYLE (entity
, FONT_SLANT_INDEX
, make_number (numeric
));
174 if (FcPatternGetInteger (p
, FC_WIDTH
, 0, &numeric
) == FcResultMatch
)
176 FONT_SET_STYLE (entity
, FONT_WIDTH_INDEX
, make_number (numeric
));
178 if (FcPatternGetDouble (p
, FC_PIXEL_SIZE
, 0, &dbl
) == FcResultMatch
)
179 ASET (entity
, FONT_SIZE_INDEX
, make_number (dbl
));
181 ASET (entity
, FONT_SIZE_INDEX
, make_number (0));
182 if (FcPatternGetInteger (p
, FC_SPACING
, 0, &numeric
) == FcResultMatch
)
183 ASET (entity
, FONT_SPACING_INDEX
, make_number (numeric
));
184 if (FcPatternGetDouble (p
, FC_DPI
, 0, &dbl
) == FcResultMatch
)
187 ASET (entity
, FONT_DPI_INDEX
, make_number (dpi
));
189 if (FcPatternGetBool (p
, FC_SCALABLE
, 0, &b
) == FcResultMatch
191 ASET (entity
, FONT_AVGWIDTH_INDEX
, make_number (0));
193 ASET (entity
, FONT_EXTRA_INDEX
, Fcopy_sequence (extra
));
194 font_put_extra (entity
, QCfont_entity
,
195 Fcons (make_unibyte_string ((char *) file
,
196 strlen ((char *) file
)),
197 make_number (index
)));
202 static Lisp_Object ftfont_generic_family_list
;
205 ftfont_resolve_generic_family (family
, pattern
)
214 family
= Fintern (Fdowncase (SYMBOL_NAME (family
)), Qnil
);
215 if (EQ (family
, Qmono
))
217 else if (EQ (family
, Qsans
) || EQ (family
, Qsans__serif
))
218 family
= Qsans_serif
;
219 slot
= assq_no_quit (family
, ftfont_generic_family_list
);
222 if (! EQ (XCDR (slot
), Qt
))
224 pattern
= FcPatternDuplicate (pattern
);
227 FcPatternDel (pattern
, FC_FOUNDRY
);
228 FcPatternDel (pattern
, FC_FAMILY
);
229 FcPatternAddString (pattern
, FC_FAMILY
, SYMBOL_FcChar8 (family
));
230 if (FcPatternGetLangSet (pattern
, FC_LANG
, 0, &langset
) != FcResultMatch
)
232 /* This is to avoid the effect of locale. */
233 langset
= FcLangSetCreate ();
234 FcLangSetAdd (langset
, "en");
235 FcPatternAddLangSet (pattern
, FC_LANG
, langset
);
236 FcLangSetDestroy (langset
);
238 FcConfigSubstitute (NULL
, pattern
, FcMatchPattern
);
239 FcDefaultSubstitute (pattern
);
240 match
= FcFontMatch (NULL
, pattern
, &result
);
245 if (FcPatternGetString (match
, FC_FAMILY
, 0, &fam
) == FcResultMatch
)
246 family
= intern ((char *) fam
);
250 XSETCDR (slot
, family
);
251 if (match
) FcPatternDestroy (match
);
253 if (pattern
) FcPatternDestroy (pattern
);
257 struct ftfont_cache_data
260 FcCharSet
*fc_charset
;
264 ftfont_lookup_cache (key
, for_face
)
268 Lisp_Object cache
, val
;
269 struct ftfont_cache_data
*cache_data
;
271 cache
= assoc_no_quit (key
, ft_face_cache
);
274 cache_data
= xmalloc (sizeof (struct ftfont_cache_data
));
275 cache_data
->ft_face
= NULL
;
276 cache_data
->fc_charset
= NULL
;
277 val
= make_save_value (NULL
, 0);
278 XSAVE_VALUE (val
)->integer
= 0;
279 XSAVE_VALUE (val
)->pointer
= cache_data
;
280 cache
= Fcons (key
, val
);
281 ft_face_cache
= Fcons (cache
, ft_face_cache
);
286 cache_data
= XSAVE_VALUE (val
)->pointer
;
288 if (for_face
? ! cache_data
->ft_face
: ! cache_data
->fc_charset
)
290 char *filename
= (char *) SDATA (XCAR (key
));
291 int index
= XINT (XCDR (key
));
296 && FT_Init_FreeType (&ft_library
) != 0)
298 if (FT_New_Face (ft_library
, filename
, index
, &cache_data
->ft_face
)
309 pat
= FcPatternBuild (0, FC_FILE
, FcTypeString
, (FcChar8
*) filename
,
310 FC_INDEX
, FcTypeInteger
, index
, NULL
);
311 objset
= FcObjectSetBuild (FC_CHARSET
, NULL
);
312 fontset
= FcFontList (NULL
, pat
, objset
);
313 xassert (fontset
&& fontset
->nfont
> 0);
314 if (FcPatternGetCharSet (fontset
->fonts
[0], FC_CHARSET
, 0, &charset
)
316 cache_data
->fc_charset
= FcCharSetCopy (charset
);
318 cache_data
->fc_charset
= FcCharSetCreate ();
319 FcFontSetDestroy (fontset
);
320 FcObjectSetDestroy (objset
);
321 FcPatternDestroy (pat
);
328 ftfont_get_fc_charset (entity
)
331 Lisp_Object val
, cache
;
332 struct ftfont_cache_data
*cache_data
;
334 val
= assq_no_quit (QCfont_entity
, AREF (entity
, FONT_EXTRA_INDEX
));
335 xassert (CONSP (val
));
337 cache
= ftfont_lookup_cache (val
, 0);
339 cache_data
= XSAVE_VALUE (val
)->pointer
;
340 return cache_data
->fc_charset
;
345 ftfont_get_otf (ftfont_info
)
346 struct ftfont_info
*ftfont_info
;
350 if (ftfont_info
->otf
)
351 return ftfont_info
->otf
;
352 if (! ftfont_info
->maybe_otf
)
354 otf
= OTF_open_ft_face (ftfont_info
->ft_size
->face
);
355 if (! otf
|| OTF_get_table (otf
, "head") < 0)
359 ftfont_info
->maybe_otf
= 0;
362 ftfont_info
->otf
= otf
;
365 #endif /* HAVE_LIBOTF */
367 static Lisp_Object ftfont_get_cache
P_ ((FRAME_PTR
));
368 static Lisp_Object ftfont_list
P_ ((Lisp_Object
, Lisp_Object
));
369 static Lisp_Object ftfont_match
P_ ((Lisp_Object
, Lisp_Object
));
370 static Lisp_Object ftfont_list_family
P_ ((Lisp_Object
));
371 static Lisp_Object ftfont_open
P_ ((FRAME_PTR
, Lisp_Object
, int));
372 static void ftfont_close
P_ ((FRAME_PTR
, struct font
*));
373 static int ftfont_has_char
P_ ((Lisp_Object
, int));
374 static unsigned ftfont_encode_char
P_ ((struct font
*, int));
375 static int ftfont_text_extents
P_ ((struct font
*, unsigned *, int,
376 struct font_metrics
*));
377 static int ftfont_get_bitmap
P_ ((struct font
*, unsigned,
378 struct font_bitmap
*, int));
379 static int ftfont_anchor_point
P_ ((struct font
*, unsigned, int,
381 static Lisp_Object ftfont_otf_capability
P_ ((struct font
*));
382 static Lisp_Object ftfont_shape
P_ ((Lisp_Object
));
383 static int ftfont_variation_glyphs
P_ ((struct font
*, int c
,
384 unsigned variations
[256]));
386 struct font_driver ftfont_driver
=
389 0, /* case insensitive */
394 NULL
, /* free_entity */
397 /* We can't draw a text without device dependent functions. */
398 NULL
, /* prepare_face */
399 NULL
, /* done_face */
403 /* We can't draw a text without device dependent functions. */
406 NULL
, /* get_bitmap */
407 NULL
, /* free_bitmap */
408 NULL
, /* get_outline */
411 ftfont_otf_capability
,
412 #else /* not HAVE_LIBOTF */
414 #endif /* not HAVE_LIBOTF */
415 NULL
, /* otf_drive */
416 NULL
, /* start_for_frame */
417 NULL
, /* end_for_frame */
418 #if defined (HAVE_M17N_FLT) && defined (HAVE_LIBOTF)
420 #else /* not (HAVE_M17N_FLT && HAVE_LIBOTF) */
422 #endif /* not (HAVE_M17N_FLT && HAVE_LIBOTF) */
424 #ifdef HAVE_OTF_GET_VARIATION_GLYPHS
425 ftfont_variation_glyphs
431 extern Lisp_Object QCname
;
437 return freetype_font_cache
;
441 ftfont_get_charset (registry
)
442 Lisp_Object registry
;
444 char *str
= (char *) SDATA (SYMBOL_NAME (registry
));
445 char *re
= alloca (SBYTES (SYMBOL_NAME (registry
)) * 2 + 1);
449 for (i
= j
= 0; i
< SBYTES (SYMBOL_NAME (registry
)); i
++, j
++)
453 else if (str
[i
] == '*')
460 regexp
= make_unibyte_string (re
, j
);
461 for (i
= 0; fc_charset_table
[i
].name
; i
++)
462 if (fast_c_string_match_ignore_case (regexp
, fc_charset_table
[i
].name
) >= 0)
464 if (! fc_charset_table
[i
].name
)
466 if (! fc_charset_table
[i
].fc_charset
)
468 FcCharSet
*charset
= FcCharSetCreate ();
469 int *uniquifier
= fc_charset_table
[i
].uniquifier
;
473 for (j
= 0; uniquifier
[j
]; j
++)
474 if (! FcCharSetAddChar (charset
, uniquifier
[j
]))
476 FcCharSetDestroy (charset
);
479 fc_charset_table
[i
].fc_charset
= charset
;
487 unsigned int script_tag
, langsys_tag
;
489 unsigned int *features
[2];
492 #define OTF_SYM_TAG(SYM, TAG) \
494 unsigned char *p = SDATA (SYMBOL_NAME (SYM)); \
495 TAG = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]; \
498 #define OTF_TAG_STR(TAG, P) \
500 (P)[0] = (char) (TAG >> 24); \
501 (P)[1] = (char) ((TAG >> 16) & 0xFF); \
502 (P)[2] = (char) ((TAG >> 8) & 0xFF); \
503 (P)[3] = (char) (TAG & 0xFF); \
507 #define OTF_TAG_SYM(SYM, TAG) \
511 OTF_TAG_STR (TAG, str); \
512 (SYM) = font_intern_prop (str, 4, 1); \
516 static struct OpenTypeSpec
*
517 ftfont_get_open_type_spec (Lisp_Object otf_spec
)
519 struct OpenTypeSpec
*spec
= malloc (sizeof (struct OpenTypeSpec
));
525 spec
->script
= XCAR (otf_spec
);
526 if (! NILP (spec
->script
))
528 OTF_SYM_TAG (spec
->script
, spec
->script_tag
);
529 val
= assq_no_quit (spec
->script
, Votf_script_alist
);
530 if (CONSP (val
) && SYMBOLP (XCDR (val
)))
531 spec
->script
= XCDR (val
);
536 spec
->script_tag
= 0x44464C54; /* "DFLT" */
537 otf_spec
= XCDR (otf_spec
);
538 val
= XCAR (otf_spec
);
540 OTF_SYM_TAG (val
, spec
->langsys_tag
);
542 spec
->langsys_tag
= 0;
543 spec
->nfeatures
[0] = spec
->nfeatures
[1] = 0;
544 for (i
= 0; i
< 2; i
++)
548 otf_spec
= XCDR (otf_spec
);
551 val
= XCAR (otf_spec
);
555 spec
->features
[i
] = malloc (sizeof (int) * XINT (len
));
556 if (! spec
->features
[i
])
558 if (i
> 0 && spec
->features
[0])
559 free (spec
->features
[0]);
563 for (j
= 0, negative
= 0; CONSP (val
); val
= XCDR (val
))
565 if (NILP (XCAR (val
)))
571 OTF_SYM_TAG (XCAR (val
), tag
);
572 spec
->features
[i
][j
++] = negative
? tag
& 0x80000000 : tag
;
575 spec
->nfeatures
[i
] = j
;
580 static FcPattern
*ftfont_spec_pattern
P_ ((Lisp_Object
, char *,
581 struct OpenTypeSpec
**));
584 ftfont_spec_pattern (spec
, otlayout
, otspec
)
587 struct OpenTypeSpec
**otspec
;
589 Lisp_Object tmp
, extra
;
590 FcPattern
*pattern
= NULL
;
591 FcCharSet
*charset
= NULL
;
592 FcLangSet
*langset
= NULL
;
596 Lisp_Object script
= Qnil
;
597 Lisp_Object registry
;
600 if (! NILP (AREF (spec
, FONT_ADSTYLE_INDEX
))
601 && SBYTES (SYMBOL_NAME (AREF (spec
, FONT_ADSTYLE_INDEX
))) > 0)
602 /* Fontconfig doesn't support adstyle property. */
604 if ((n
= FONT_SLANT_NUMERIC (spec
)) >= 0
606 /* Fontconfig doesn't support reverse-italic/obligue. */
609 if (INTEGERP (AREF (spec
, FONT_DPI_INDEX
)))
610 dpi
= XINT (AREF (spec
, FONT_DPI_INDEX
));
611 if (INTEGERP (AREF (spec
, FONT_AVGWIDTH_INDEX
))
612 && XINT (AREF (spec
, FONT_AVGWIDTH_INDEX
)) == 0)
615 registry
= AREF (spec
, FONT_REGISTRY_INDEX
);
617 || EQ (registry
, Qascii_0
)
618 || EQ (registry
, Qiso10646_1
)
619 || EQ (registry
, Qunicode_bmp
))
625 fc_charset_idx
= ftfont_get_charset (registry
);
626 if (fc_charset_idx
< 0)
628 charset
= fc_charset_table
[fc_charset_idx
].fc_charset
;
629 lang
= (FcChar8
*) fc_charset_table
[fc_charset_idx
].lang
;
632 langset
= FcLangSetCreate ();
635 FcLangSetAdd (langset
, lang
);
640 for (extra
= AREF (spec
, FONT_EXTRA_INDEX
);
641 CONSP (extra
); extra
= XCDR (extra
))
643 Lisp_Object key
, val
;
645 key
= XCAR (XCAR (extra
)), val
= XCDR (XCAR (extra
));
648 else if (EQ (key
, QClang
))
651 langset
= FcLangSetCreate ();
656 if (! FcLangSetAdd (langset
, SYMBOL_FcChar8 (val
)))
660 for (; CONSP (val
); val
= XCDR (val
))
661 if (SYMBOLP (XCAR (val
))
662 && ! FcLangSetAdd (langset
, SYMBOL_FcChar8 (XCAR (val
))))
665 else if (EQ (key
, QCotf
))
667 *otspec
= ftfont_get_open_type_spec (val
);
670 strcat (otlayout
, "otlayout:");
671 OTF_TAG_STR ((*otspec
)->script_tag
, otlayout
+ 9);
672 script
= (*otspec
)->script
;
674 else if (EQ (key
, QCscript
))
676 else if (EQ (key
, QCscalable
))
677 scalable
= ! NILP (val
);
680 if (! NILP (script
) && ! charset
)
682 Lisp_Object chars
= assq_no_quit (script
, Vscript_representative_chars
);
684 if (CONSP (chars
) && CONSP (CDR (chars
)))
686 charset
= FcCharSetCreate ();
689 for (chars
= XCDR (chars
); CONSP (chars
); chars
= XCDR (chars
))
690 if (CHARACTERP (XCAR (chars
))
691 && ! FcCharSetAddChar (charset
, XUINT (XCAR (chars
))))
696 pattern
= FcPatternCreate ();
699 tmp
= AREF (spec
, FONT_FOUNDRY_INDEX
);
701 && ! FcPatternAddString (pattern
, FC_FOUNDRY
, SYMBOL_FcChar8 (tmp
)))
703 tmp
= AREF (spec
, FONT_FAMILY_INDEX
);
705 && ! FcPatternAddString (pattern
, FC_FAMILY
, SYMBOL_FcChar8 (tmp
)))
708 && ! FcPatternAddCharSet (pattern
, FC_CHARSET
, charset
))
711 && ! FcPatternAddLangSet (pattern
, FC_LANG
, langset
))
714 && ! FcPatternAddDouble (pattern
, FC_DPI
, dpi
))
717 && ! FcPatternAddBool (pattern
, FC_SCALABLE
, scalable
? FcTrue
: FcFalse
))
723 /* We come here because of unexpected error in fontconfig API call
724 (usually insufficient memory). */
727 FcPatternDestroy (pattern
);
732 if ((*otspec
)->nfeatures
[0] > 0)
733 free ((*otspec
)->features
[0]);
734 if ((*otspec
)->nfeatures
[1] > 0)
735 free ((*otspec
)->features
[1]);
741 if (langset
) FcLangSetDestroy (langset
);
742 if (charset
&& fc_charset_idx
< 0) FcCharSetDestroy (charset
);
747 ftfont_list (frame
, spec
)
748 Lisp_Object frame
, spec
;
750 Lisp_Object val
= Qnil
, family
;
753 FcFontSet
*fontset
= NULL
;
754 FcObjectSet
*objset
= NULL
;
756 Lisp_Object chars
= Qnil
;
758 char otlayout
[15]; /* For "otlayout:XXXX" */
759 struct OpenTypeSpec
*otspec
= NULL
;
762 if (! fc_initialized
)
768 pattern
= ftfont_spec_pattern (spec
, otlayout
, &otspec
);
771 if (FcPatternGetCharSet (pattern
, FC_CHARSET
, 0, &charset
) != FcResultMatch
)
773 val
= assq_no_quit (QCscript
, AREF (spec
, FONT_EXTRA_INDEX
));
776 val
= assq_no_quit (XCDR (val
), Vscript_representative_chars
);
777 if (CONSP (val
) && VECTORP (XCDR (val
)))
782 if (INTEGERP (AREF (spec
, FONT_SPACING_INDEX
)))
783 spacing
= XINT (AREF (spec
, FONT_SPACING_INDEX
));
784 family
= AREF (spec
, FONT_FAMILY_INDEX
);
787 Lisp_Object resolved
;
789 resolved
= ftfont_resolve_generic_family (family
, pattern
);
790 if (! NILP (resolved
))
792 FcPatternDel (pattern
, FC_FAMILY
);
793 if (! FcPatternAddString (pattern
, FC_FAMILY
,
794 SYMBOL_FcChar8 (resolved
)))
799 objset
= FcObjectSetBuild (FC_FOUNDRY
, FC_FAMILY
, FC_WEIGHT
, FC_SLANT
,
800 FC_WIDTH
, FC_PIXEL_SIZE
, FC_SPACING
, FC_SCALABLE
,
804 #endif /* FC_CAPABILITY */
812 FcObjectSetAdd (objset
, FC_CHARSET
);
814 fontset
= FcFontList (NULL
, pattern
, objset
);
815 if (! fontset
|| fontset
->nfont
== 0)
818 /* Need fix because this finds any fonts. */
819 if (fontset
->nfont
== 0 && ! NILP (family
))
821 /* Try maching with configuration. For instance, the
822 configuration may specify "Nimbus Mono L" as an alias of
824 FcPattern
*pat
= FcPatternBuild (0, FC_FAMILY
, FcTypeString
,
825 SYMBOL_FcChar8 (family
), NULL
);
828 if (FcConfigSubstitute (NULL
, pat
, FcMatchPattern
) == FcTrue
)
831 FcPatternGetString (pat
, FC_FAMILY
, i
, &fam
) == FcResultMatch
;
834 FcPatternDel (pattern
, FC_FAMILY
);
835 FcPatternAddString (pattern
, FC_FAMILY
, fam
);
836 FcFontSetDestroy (fontset
);
837 fontset
= FcFontList (NULL
, pattern
, objset
);
838 if (fontset
->nfont
> 0)
844 for (i
= 0; i
< fontset
->nfont
; i
++)
852 if ((FcPatternGetInteger (fontset
->fonts
[i
], FC_SPACING
, 0, &this)
863 if (FcPatternGetString (fontset
->fonts
[i
], FC_CAPABILITY
, 0, &this)
865 || ! strstr ((char *) this, otlayout
))
868 #endif /* FC_CAPABILITY */
875 if (FcPatternGetString (fontset
->fonts
[i
], FC_FILE
, 0, &file
)
878 otf
= OTF_open ((char *) file
);
881 if (OTF_check_features (otf
, 1,
882 otspec
->script_tag
, otspec
->langsys_tag
,
884 otspec
->nfeatures
[0]) != 1
885 || OTF_check_features (otf
, 0,
886 otspec
->script_tag
, otspec
->langsys_tag
,
888 otspec
->nfeatures
[1]) != 1)
891 #endif /* HAVE_LIBOTF */
896 if (FcPatternGetCharSet (fontset
->fonts
[i
], FC_CHARSET
, 0, &charset
)
899 for (j
= 0; j
< ASIZE (chars
); j
++)
900 if (NATNUMP (AREF (chars
, j
))
901 && FcCharSetHasChar (charset
, XFASTINT (AREF (chars
, j
))))
903 if (j
== ASIZE (chars
))
906 entity
= ftfont_pattern_entity (fontset
->fonts
[i
],
907 AREF (spec
, FONT_EXTRA_INDEX
));
909 val
= Fcons (entity
, val
);
911 val
= Fnreverse (val
);
915 /* We come here because of unexpected error in fontconfig API call
916 (usually insufficient memory). */
920 font_add_log ("ftfont-list", spec
, val
);
921 if (objset
) FcObjectSetDestroy (objset
);
922 if (fontset
) FcFontSetDestroy (fontset
);
923 if (pattern
) FcPatternDestroy (pattern
);
928 ftfont_match (frame
, spec
)
929 Lisp_Object frame
, spec
;
931 Lisp_Object entity
= Qnil
;
932 FcPattern
*pattern
, *match
= NULL
;
934 char otlayout
[15]; /* For "otlayout:XXXX" */
935 struct OpenTypeSpec
*otspec
= NULL
;
937 if (! fc_initialized
)
943 pattern
= ftfont_spec_pattern (spec
, otlayout
, &otspec
);
947 if (INTEGERP (AREF (spec
, FONT_SIZE_INDEX
)))
951 value
.type
= FcTypeDouble
;
952 value
.u
.d
= XINT (AREF (spec
, FONT_SIZE_INDEX
));
953 FcPatternAdd (pattern
, FC_PIXEL_SIZE
, value
, FcFalse
);
955 if (FcConfigSubstitute (NULL
, pattern
, FcMatchPattern
) == FcTrue
)
957 FcDefaultSubstitute (pattern
);
958 match
= FcFontMatch (NULL
, pattern
, &result
);
961 entity
= ftfont_pattern_entity (match
, AREF (spec
, FONT_EXTRA_INDEX
));
962 FcPatternDestroy (match
);
963 if (! NILP (AREF (spec
, FONT_FAMILY_INDEX
))
964 && NILP (assq_no_quit (AREF (spec
, FONT_FAMILY_INDEX
),
965 ftfont_generic_family_list
))
966 && NILP (Fstring_equal (AREF (spec
, FONT_FAMILY_INDEX
),
967 AREF (entity
, FONT_FAMILY_INDEX
))))
971 FcPatternDestroy (pattern
);
973 font_add_log ("ftfont-match", spec
, entity
);
978 ftfont_list_family (frame
)
981 Lisp_Object list
= Qnil
;
982 FcPattern
*pattern
= NULL
;
983 FcFontSet
*fontset
= NULL
;
984 FcObjectSet
*objset
= NULL
;
987 if (! fc_initialized
)
993 pattern
= FcPatternCreate ();
996 objset
= FcObjectSetBuild (FC_FAMILY
, NULL
);
999 fontset
= FcFontList (NULL
, pattern
, objset
);
1003 for (i
= 0; i
< fontset
->nfont
; i
++)
1005 FcPattern
*pat
= fontset
->fonts
[i
];
1008 if (FcPatternGetString (pat
, FC_FAMILY
, 0, &str
) == FcResultMatch
)
1009 list
= Fcons (intern ((char *) str
), list
);
1013 if (objset
) FcObjectSetDestroy (objset
);
1014 if (fontset
) FcFontSetDestroy (fontset
);
1015 if (pattern
) FcPatternDestroy (pattern
);
1022 ftfont_open (f
, entity
, pixel_size
)
1027 struct ftfont_info
*ftfont_info
;
1029 struct ftfont_cache_data
*cache_data
;
1033 Lisp_Object val
, filename
, index
, cache
, font_object
;
1040 val
= assq_no_quit (QCfont_entity
, AREF (entity
, FONT_EXTRA_INDEX
));
1044 cache
= ftfont_lookup_cache (val
, 1);
1047 filename
= XCAR (val
);
1050 cache_data
= XSAVE_VALUE (XCDR (cache
))->pointer
;
1051 ft_face
= cache_data
->ft_face
;
1052 if (XSAVE_VALUE (val
)->integer
> 0)
1054 /* FT_Face in this cache is already used by the different size. */
1055 if (FT_New_Size (ft_face
, &ft_size
) != 0)
1057 if (FT_Activate_Size (ft_size
) != 0)
1059 FT_Done_Size (ft_size
);
1063 XSAVE_VALUE (val
)->integer
++;
1064 size
= XINT (AREF (entity
, FONT_SIZE_INDEX
));
1067 if (FT_Set_Pixel_Sizes (ft_face
, size
, size
) != 0)
1069 if (XSAVE_VALUE (val
)->integer
== 0)
1070 FT_Done_Face (ft_face
);
1074 font_object
= font_make_object (VECSIZE (struct ftfont_info
), entity
, size
);
1075 ASET (font_object
, FONT_TYPE_INDEX
, Qfreetype
);
1076 len
= font_unparse_xlfd (entity
, size
, name
, 256);
1078 ASET (font_object
, FONT_NAME_INDEX
, make_string (name
, len
));
1079 len
= font_unparse_fcname (entity
, size
, name
, 256);
1081 ASET (font_object
, FONT_FULLNAME_INDEX
, make_string (name
, len
));
1083 ASET (font_object
, FONT_FULLNAME_INDEX
,
1084 AREF (font_object
, FONT_NAME_INDEX
));
1085 ASET (font_object
, FONT_FILE_INDEX
, filename
);
1086 ASET (font_object
, FONT_FORMAT_INDEX
, ftfont_font_format (NULL
, filename
));
1087 font
= XFONT_OBJECT (font_object
);
1088 ftfont_info
= (struct ftfont_info
*) font
;
1089 ftfont_info
->ft_size
= ft_face
->size
;
1090 ftfont_info
->index
= XINT (index
);
1092 ftfont_info
->maybe_otf
= ft_face
->face_flags
& FT_FACE_FLAG_SFNT
;
1093 ftfont_info
->otf
= NULL
;
1094 #endif /* HAVE_LIBOTF */
1095 font
->pixel_size
= size
;
1096 font
->driver
= &ftfont_driver
;
1097 font
->encoding_charset
= font
->repertory_charset
= -1;
1099 upEM
= ft_face
->units_per_EM
;
1100 scalable
= (INTEGERP (AREF (entity
, FONT_AVGWIDTH_INDEX
))
1101 && XINT (AREF (entity
, FONT_AVGWIDTH_INDEX
)) == 0);
1104 font
->ascent
= ft_face
->ascender
* size
/ upEM
;
1105 font
->descent
= - ft_face
->descender
* size
/ upEM
;
1106 font
->height
= ft_face
->height
* size
/ upEM
;
1110 font
->ascent
= ft_face
->size
->metrics
.ascender
>> 6;
1111 font
->descent
= - ft_face
->size
->metrics
.descender
>> 6;
1112 font
->height
= ft_face
->size
->metrics
.height
>> 6;
1114 if (INTEGERP (AREF (entity
, FONT_SPACING_INDEX
)))
1115 spacing
= XINT (AREF (entity
, FONT_SPACING_INDEX
));
1117 spacing
= FC_PROPORTIONAL
;
1118 if (spacing
!= FC_PROPORTIONAL
)
1119 font
->min_width
= font
->average_width
= font
->space_width
1120 = (scalable
? ft_face
->max_advance_width
* size
/ upEM
1121 : ft_face
->size
->metrics
.max_advance
>> 6);
1126 font
->min_width
= font
->average_width
= font
->space_width
= 0;
1127 for (i
= 32, n
= 0; i
< 127; i
++)
1128 if (FT_Load_Char (ft_face
, i
, FT_LOAD_DEFAULT
) != 0)
1130 int this_width
= ft_face
->glyph
->metrics
.horiAdvance
>> 6;
1133 && (! font
->min_width
|| font
->min_width
> this_width
))
1134 font
->min_width
= this_width
;
1136 font
->space_width
= this_width
;
1137 font
->average_width
+= this_width
;
1141 font
->average_width
/= n
;
1144 font
->baseline_offset
= 0;
1145 font
->relative_compose
= 0;
1146 font
->default_ascent
= 0;
1147 font
->vertical_centering
= 0;
1150 font
->underline_position
= -ft_face
->underline_position
* size
/ upEM
;
1151 font
->underline_thickness
= -ft_face
->underline_thickness
* size
/ upEM
;
1155 font
->underline_position
= -1;
1156 font
->underline_thickness
= 0;
1163 ftfont_close (f
, font
)
1167 struct ftfont_info
*ftfont_info
= (struct ftfont_info
*) font
;
1168 Lisp_Object val
, cache
;
1170 val
= Fcons (font
->props
[FONT_FILE_INDEX
], make_number (ftfont_info
->index
));
1171 cache
= ftfont_lookup_cache (val
, 1);
1172 xassert (CONSP (cache
));
1174 (XSAVE_VALUE (val
)->integer
)--;
1175 if (XSAVE_VALUE (val
)->integer
== 0)
1177 struct ftfont_cache_data
*cache_data
= XSAVE_VALUE (val
)->pointer
;
1179 FT_Done_Face (cache_data
->ft_face
);
1181 if (ftfont_info
->otf
)
1182 OTF_close (ftfont_info
->otf
);
1184 cache_data
->ft_face
= NULL
;
1187 FT_Done_Size (ftfont_info
->ft_size
);
1191 ftfont_has_char (entity
, c
)
1195 FcCharSet
*charset
= ftfont_get_fc_charset (entity
);
1197 return (FcCharSetHasChar (charset
, c
) == FcTrue
);
1201 ftfont_encode_char (font
, c
)
1205 struct ftfont_info
*ftfont_info
= (struct ftfont_info
*) font
;
1206 FT_Face ft_face
= ftfont_info
->ft_size
->face
;
1207 FT_ULong charcode
= c
;
1208 FT_UInt code
= FT_Get_Char_Index (ft_face
, charcode
);
1210 return (code
> 0 ? code
: FONT_INVALID_CODE
);
1214 ftfont_text_extents (font
, code
, nglyphs
, metrics
)
1218 struct font_metrics
*metrics
;
1220 struct ftfont_info
*ftfont_info
= (struct ftfont_info
*) font
;
1221 FT_Face ft_face
= ftfont_info
->ft_size
->face
;
1225 if (ftfont_info
->ft_size
!= ft_face
->size
)
1226 FT_Activate_Size (ftfont_info
->ft_size
);
1228 bzero (metrics
, sizeof (struct font_metrics
));
1229 for (i
= 0, first
= 1; i
< nglyphs
; i
++)
1231 if (FT_Load_Glyph (ft_face
, code
[i
], FT_LOAD_DEFAULT
) == 0)
1233 FT_Glyph_Metrics
*m
= &ft_face
->glyph
->metrics
;
1239 metrics
->lbearing
= m
->horiBearingX
>> 6;
1240 metrics
->rbearing
= (m
->horiBearingX
+ m
->width
) >> 6;
1241 metrics
->ascent
= m
->horiBearingY
>> 6;
1242 metrics
->descent
= (m
->horiBearingY
+ m
->height
) >> 6;
1248 if (metrics
->lbearing
> width
+ (m
->horiBearingX
>> 6))
1249 metrics
->lbearing
= width
+ (m
->horiBearingX
>> 6);
1250 if (metrics
->rbearing
1251 < width
+ ((m
->horiBearingX
+ m
->width
) >> 6))
1253 = width
+ ((m
->horiBearingX
+ m
->width
) >> 6);
1254 if (metrics
->ascent
< (m
->horiBearingY
>> 6))
1255 metrics
->ascent
= m
->horiBearingY
>> 6;
1256 if (metrics
->descent
> ((m
->horiBearingY
+ m
->height
) >> 6))
1257 metrics
->descent
= (m
->horiBearingY
+ m
->height
) >> 6;
1259 width
+= m
->horiAdvance
>> 6;
1263 width
+= font
->space_width
;
1267 metrics
->width
= width
;
1273 ftfont_get_bitmap (font
, code
, bitmap
, bits_per_pixel
)
1276 struct font_bitmap
*bitmap
;
1279 struct ftfont_info
*ftfont_info
= (struct ftfont_info
*) font
;
1280 FT_Face ft_face
= ftfont_info
->ft_size
->face
;
1281 FT_Int32 load_flags
= FT_LOAD_RENDER
;
1283 if (ftfont_info
->ft_size
!= ft_face
->size
)
1284 FT_Activate_Size (ftfont_info
->ft_size
);
1285 if (bits_per_pixel
== 1)
1287 #ifdef FT_LOAD_TARGET_MONO
1288 load_flags
|= FT_LOAD_TARGET_MONO
;
1290 load_flags
|= FT_LOAD_MONOCHROME
;
1293 else if (bits_per_pixel
!= 8)
1294 /* We don't support such a rendering. */
1297 if (FT_Load_Glyph (ft_face
, code
, load_flags
) != 0)
1299 bitmap
->bits_per_pixel
1300 = (ft_face
->glyph
->bitmap
.pixel_mode
== FT_PIXEL_MODE_MONO
? 1
1301 : ft_face
->glyph
->bitmap
.pixel_mode
== FT_PIXEL_MODE_GRAY
? 8
1302 : ft_face
->glyph
->bitmap
.pixel_mode
== FT_PIXEL_MODE_LCD
? 8
1303 : ft_face
->glyph
->bitmap
.pixel_mode
== FT_PIXEL_MODE_LCD_V
? 8
1305 if (bitmap
->bits_per_pixel
< 0)
1306 /* We don't suport that kind of pixel mode. */
1308 bitmap
->rows
= ft_face
->glyph
->bitmap
.rows
;
1309 bitmap
->width
= ft_face
->glyph
->bitmap
.width
;
1310 bitmap
->pitch
= ft_face
->glyph
->bitmap
.pitch
;
1311 bitmap
->buffer
= ft_face
->glyph
->bitmap
.buffer
;
1312 bitmap
->left
= ft_face
->glyph
->bitmap_left
;
1313 bitmap
->top
= ft_face
->glyph
->bitmap_top
;
1314 bitmap
->advance
= ft_face
->glyph
->metrics
.horiAdvance
>> 6;
1315 bitmap
->extra
= NULL
;
1321 ftfont_anchor_point (font
, code
, index
, x
, y
)
1327 struct ftfont_info
*ftfont_info
= (struct ftfont_info
*) font
;
1328 FT_Face ft_face
= ftfont_info
->ft_size
->face
;
1330 if (ftfont_info
->ft_size
!= ft_face
->size
)
1331 FT_Activate_Size (ftfont_info
->ft_size
);
1332 if (FT_Load_Glyph (ft_face
, code
, FT_LOAD_DEFAULT
) != 0)
1334 if (ft_face
->glyph
->format
!= FT_GLYPH_FORMAT_OUTLINE
)
1336 if (index
>= ft_face
->glyph
->outline
.n_points
)
1338 *x
= ft_face
->glyph
->outline
.points
[index
].x
;
1339 *y
= ft_face
->glyph
->outline
.points
[index
].y
;
1346 ftfont_otf_features (gsub_gpos
)
1347 OTF_GSUB_GPOS
*gsub_gpos
;
1349 Lisp_Object scripts
, langsyses
, features
, sym
;
1352 for (scripts
= Qnil
, i
= gsub_gpos
->ScriptList
.ScriptCount
- 1; i
>= 0; i
--)
1354 OTF_Script
*otf_script
= gsub_gpos
->ScriptList
.Script
+ i
;
1356 for (langsyses
= Qnil
, j
= otf_script
->LangSysCount
- 1; j
>= -1; j
--)
1358 OTF_LangSys
*otf_langsys
;
1361 otf_langsys
= otf_script
->LangSys
+ j
;
1362 else if (otf_script
->DefaultLangSysOffset
)
1363 otf_langsys
= &otf_script
->DefaultLangSys
;
1367 for (features
= Qnil
, k
= otf_langsys
->FeatureCount
- 1; k
>= 0; k
--)
1369 l
= otf_langsys
->FeatureIndex
[k
];
1370 if (l
>= gsub_gpos
->FeatureList
.FeatureCount
)
1372 OTF_TAG_SYM (sym
, gsub_gpos
->FeatureList
.Feature
[l
].FeatureTag
);
1373 features
= Fcons (sym
, features
);
1376 OTF_TAG_SYM (sym
, otf_script
->LangSysRecord
[j
].LangSysTag
);
1379 langsyses
= Fcons (Fcons (sym
, features
), langsyses
);
1382 OTF_TAG_SYM (sym
, gsub_gpos
->ScriptList
.Script
[i
].ScriptTag
);
1383 scripts
= Fcons (Fcons (sym
, langsyses
), scripts
);
1391 ftfont_otf_capability (font
)
1394 struct ftfont_info
*ftfont_info
= (struct ftfont_info
*) font
;
1395 OTF
*otf
= ftfont_get_otf (ftfont_info
);
1396 Lisp_Object gsub_gpos
;
1400 gsub_gpos
= Fcons (Qnil
, Qnil
);
1401 if (OTF_get_table (otf
, "GSUB") == 0
1402 && otf
->gsub
->FeatureList
.FeatureCount
> 0)
1403 XSETCAR (gsub_gpos
, ftfont_otf_features (otf
->gsub
));
1404 if (OTF_get_table (otf
, "GPOS") == 0
1405 && otf
->gpos
->FeatureList
.FeatureCount
> 0)
1406 XSETCDR (gsub_gpos
, ftfont_otf_features (otf
->gpos
));
1410 #ifdef HAVE_M17N_FLT
1421 ftfont_get_glyph_id (font
, gstring
, from
, to
)
1423 MFLTGlyphString
*gstring
;
1426 struct MFLTFontFT
*flt_font_ft
= (struct MFLTFontFT
*) font
;
1427 FT_Face ft_face
= flt_font_ft
->ft_face
;
1430 for (g
= gstring
->glyphs
+ from
; from
< to
; g
++, from
++)
1433 FT_UInt code
= FT_Get_Char_Index (ft_face
, g
->code
);
1435 g
->code
= code
> 0 ? code
: FONT_INVALID_CODE
;
1442 ftfont_get_metrics (font
, gstring
, from
, to
)
1444 MFLTGlyphString
*gstring
;
1447 struct MFLTFontFT
*flt_font_ft
= (struct MFLTFontFT
*) font
;
1448 FT_Face ft_face
= flt_font_ft
->ft_face
;
1451 for (g
= gstring
->glyphs
+ from
; from
< to
; g
++, from
++)
1454 if (g
->code
!= FONT_INVALID_CODE
)
1456 FT_Glyph_Metrics
*m
;
1458 if (FT_Load_Glyph (ft_face
, g
->code
, FT_LOAD_DEFAULT
) != 0)
1460 m
= &ft_face
->glyph
->metrics
;
1462 g
->lbearing
= m
->horiBearingX
;
1463 g
->rbearing
= m
->horiBearingX
+ m
->width
;
1464 g
->ascent
= m
->horiBearingY
;
1465 g
->descent
= m
->height
- m
->horiBearingY
;
1466 g
->xadv
= m
->horiAdvance
;
1471 g
->rbearing
= g
->xadv
= flt_font_ft
->font
->space_width
<< 6;
1472 g
->ascent
= flt_font_ft
->font
->ascent
<< 6;
1473 g
->descent
= flt_font_ft
->font
->descent
<< 6;
1482 ftfont_check_otf (MFLTFont
*font
, MFLTOtfSpec
*spec
)
1484 struct MFLTFontFT
*flt_font_ft
= (struct MFLTFontFT
*) font
;
1485 OTF
*otf
= flt_font_ft
->otf
;
1489 for (i
= 0; i
< 2; i
++)
1491 if (! spec
->features
[i
])
1493 for (n
= 0; spec
->features
[i
][n
]; n
++);
1494 tags
= alloca (sizeof (OTF_Tag
) * n
);
1495 for (n
= 0, negative
= 0; spec
->features
[i
][n
]; n
++)
1497 if (spec
->features
[i
][n
] == 0xFFFFFFFF)
1500 tags
[n
- 1] = spec
->features
[i
][n
] | 0x80000000;
1502 tags
[n
] = spec
->features
[i
][n
];
1504 if (n
- negative
> 0
1505 && OTF_check_features (otf
, i
== 0, spec
->script
, spec
->langsys
,
1506 tags
, n
- negative
) != 1)
1512 #define DEVICE_DELTA(table, size) \
1513 (((size) >= (table).StartSize && (size) <= (table).EndSize) \
1514 ? (table).DeltaValue[(size) - (table).StartSize] << 6 \
1518 adjust_anchor (FT_Face ft_face
, OTF_Anchor
*anchor
,
1519 unsigned code
, int x_ppem
, int y_ppem
, int *x
, int *y
)
1521 if (anchor
->AnchorFormat
== 2)
1523 FT_Outline
*outline
;
1524 int ap
= anchor
->f
.f1
.AnchorPoint
;
1526 FT_Load_Glyph (ft_face
, (FT_UInt
) code
, FT_LOAD_MONOCHROME
);
1527 outline
= &ft_face
->glyph
->outline
;
1528 if (ap
< outline
->n_points
)
1530 *x
= outline
->points
[ap
].x
<< 6;
1531 *y
= outline
->points
[ap
].y
<< 6;
1534 else if (anchor
->AnchorFormat
== 3)
1536 if (anchor
->f
.f2
.XDeviceTable
.offset
1537 && anchor
->f
.f2
.XDeviceTable
.DeltaValue
)
1538 *x
+= DEVICE_DELTA (anchor
->f
.f2
.XDeviceTable
, x_ppem
);
1539 if (anchor
->f
.f2
.YDeviceTable
.offset
1540 && anchor
->f
.f2
.YDeviceTable
.DeltaValue
)
1541 *y
+= DEVICE_DELTA (anchor
->f
.f2
.YDeviceTable
, y_ppem
);
1545 static OTF_GlyphString otf_gstring
;
1548 setup_otf_gstring (int size
)
1550 if (otf_gstring
.size
== 0)
1552 otf_gstring
.glyphs
= (OTF_Glyph
*) malloc (sizeof (OTF_Glyph
) * size
);
1553 otf_gstring
.size
= size
;
1555 else if (otf_gstring
.size
< size
)
1557 otf_gstring
.glyphs
= (OTF_Glyph
*) realloc (otf_gstring
.glyphs
,
1558 sizeof (OTF_Glyph
) * size
);
1559 otf_gstring
.size
= size
;
1561 otf_gstring
.used
= size
;
1562 memset (otf_gstring
.glyphs
, 0, sizeof (OTF_Glyph
) * size
);
1567 ftfont_drive_otf (font
, spec
, in
, from
, to
, out
, adjustment
)
1570 MFLTGlyphString
*in
;
1572 MFLTGlyphString
*out
;
1573 MFLTGlyphAdjustment
*adjustment
;
1575 struct MFLTFontFT
*flt_font_ft
= (struct MFLTFontFT
*) font
;
1576 FT_Face ft_face
= flt_font_ft
->ft_face
;
1577 OTF
*otf
= flt_font_ft
->otf
;
1578 int len
= to
- from
;
1581 char script
[5], *langsys
= NULL
;
1582 char *gsub_features
= NULL
, *gpos_features
= NULL
;
1586 OTF_tag_name (spec
->script
, script
);
1589 langsys
= alloca (5);
1590 OTF_tag_name (spec
->langsys
, langsys
);
1592 for (i
= 0; i
< 2; i
++)
1596 if (spec
->features
[i
] && spec
->features
[i
][1] != 0xFFFFFFFF)
1598 for (j
= 0; spec
->features
[i
][j
]; j
++);
1600 p
= gsub_features
= alloca (6 * j
);
1602 p
= gpos_features
= alloca (6 * j
);
1603 for (j
= 0; spec
->features
[i
][j
]; j
++)
1605 if (spec
->features
[i
][j
] == 0xFFFFFFFF)
1606 *p
++ = '*', *p
++ = ',';
1609 OTF_tag_name (spec
->features
[i
][j
], p
);
1618 setup_otf_gstring (len
);
1619 for (i
= 0; i
< len
; i
++)
1621 otf_gstring
.glyphs
[i
].c
= in
->glyphs
[from
+ i
].c
;
1622 otf_gstring
.glyphs
[i
].glyph_id
= in
->glyphs
[from
+ i
].code
;
1625 OTF_drive_gdef (otf
, &otf_gstring
);
1630 if (OTF_drive_gsub (otf
, &otf_gstring
, script
, langsys
, gsub_features
)
1633 if (out
->allocated
< out
->used
+ otf_gstring
.used
)
1635 for (i
= 0, otfg
= otf_gstring
.glyphs
; i
< otf_gstring
.used
; )
1638 int min_from
, max_to
;
1641 g
= out
->glyphs
+ out
->used
;
1642 *g
= in
->glyphs
[from
+ otfg
->f
.index
.from
];
1643 if (g
->code
!= otfg
->glyph_id
)
1646 g
->code
= otfg
->glyph_id
;
1652 if (otfg
->f
.index
.from
< otfg
->f
.index
.to
)
1654 /* OTFG substitutes multiple glyphs in IN. */
1655 for (j
= from
+ otfg
->f
.index
.from
+ 1;
1656 j
<= from
+ otfg
->f
.index
.to
; j
++)
1658 if (min_from
> in
->glyphs
[j
].from
)
1659 min_from
= in
->glyphs
[j
].from
;
1660 if (max_to
< in
->glyphs
[j
].to
)
1661 max_to
= in
->glyphs
[j
].to
;
1666 for (i
++, otfg
++; (i
< otf_gstring
.used
1667 && otfg
->f
.index
.from
== otfg
[-1].f
.index
.from
);
1670 g
= out
->glyphs
+ out
->used
;
1671 *g
= in
->glyphs
[from
+ otfg
->f
.index
.to
];
1672 if (g
->code
!= otfg
->glyph_id
)
1675 g
->code
= otfg
->glyph_id
;
1684 if (out
->allocated
< out
->used
+ len
)
1686 for (i
= 0; i
< len
; i
++)
1687 out
->glyphs
[out
->used
++] = in
->glyphs
[from
+ i
];
1692 MFLTGlyph
*base
= NULL
, *mark
= NULL
, *g
;
1693 int x_ppem
, y_ppem
, x_scale
, y_scale
;
1695 if (OTF_drive_gpos (otf
, &otf_gstring
, script
, langsys
, gpos_features
)
1699 x_ppem
= ft_face
->size
->metrics
.x_ppem
;
1700 y_ppem
= ft_face
->size
->metrics
.y_ppem
;
1701 x_scale
= ft_face
->size
->metrics
.x_scale
;
1702 y_scale
= ft_face
->size
->metrics
.y_scale
;
1704 for (i
= 0, otfg
= otf_gstring
.glyphs
, g
= out
->glyphs
+ gidx
;
1705 i
< otf_gstring
.used
; i
++, otfg
++, g
++)
1709 if (! otfg
->glyph_id
)
1711 switch (otfg
->positioning_type
)
1715 case 1: /* Single */
1718 int format
= otfg
->f
.f1
.format
;
1720 if (format
& OTF_XPlacement
)
1722 = otfg
->f
.f1
.value
->XPlacement
* x_scale
/ 0x10000;
1723 if (format
& OTF_XPlaDevice
)
1725 += DEVICE_DELTA (otfg
->f
.f1
.value
->XPlaDevice
, x_ppem
);
1726 if (format
& OTF_YPlacement
)
1728 = - (otfg
->f
.f1
.value
->YPlacement
* y_scale
/ 0x10000);
1729 if (format
& OTF_YPlaDevice
)
1731 -= DEVICE_DELTA (otfg
->f
.f1
.value
->YPlaDevice
, y_ppem
);
1732 if (format
& OTF_XAdvance
)
1734 += otfg
->f
.f1
.value
->XAdvance
* x_scale
/ 0x10000;
1735 if (format
& OTF_XAdvDevice
)
1737 += DEVICE_DELTA (otfg
->f
.f1
.value
->XAdvDevice
, x_ppem
);
1738 if (format
& OTF_YAdvance
)
1740 += otfg
->f
.f1
.value
->YAdvance
* y_scale
/ 0x10000;
1741 if (format
& OTF_YAdvDevice
)
1743 += DEVICE_DELTA (otfg
->f
.f1
.value
->YAdvDevice
, y_ppem
);
1744 adjustment
[i
].set
= 1;
1747 case 3: /* Cursive */
1748 /* Not yet supported. */
1750 case 4: /* Mark-to-Base */
1751 case 5: /* Mark-to-Ligature */
1755 goto label_adjust_anchor
;
1756 default: /* i.e. case 6 Mark-to-Mark */
1761 label_adjust_anchor
:
1763 int base_x
, base_y
, mark_x
, mark_y
;
1764 int this_from
, this_to
;
1766 base_x
= otfg
->f
.f4
.base_anchor
->XCoordinate
* x_scale
/ 0x10000;
1767 base_y
= otfg
->f
.f4
.base_anchor
->YCoordinate
* y_scale
/ 0x10000;
1768 mark_x
= otfg
->f
.f4
.mark_anchor
->XCoordinate
* x_scale
/ 0x10000;
1769 mark_y
= otfg
->f
.f4
.mark_anchor
->YCoordinate
* y_scale
/ 0x10000;;
1771 if (otfg
->f
.f4
.base_anchor
->AnchorFormat
!= 1)
1772 adjust_anchor (ft_face
, otfg
->f
.f4
.base_anchor
,
1773 prev
->code
, x_ppem
, y_ppem
, &base_x
, &base_y
);
1774 if (otfg
->f
.f4
.mark_anchor
->AnchorFormat
!= 1)
1775 adjust_anchor (ft_face
, otfg
->f
.f4
.mark_anchor
, g
->code
,
1776 x_ppem
, y_ppem
, &mark_x
, &mark_y
);
1777 adjustment
[i
].xoff
= (base_x
- mark_x
);
1778 adjustment
[i
].yoff
= - (base_y
- mark_y
);
1779 adjustment
[i
].back
= (g
- prev
);
1780 adjustment
[i
].xadv
= 0;
1781 adjustment
[i
].advance_is_absolute
= 1;
1782 adjustment
[i
].set
= 1;
1783 this_from
= g
->from
;
1785 for (j
= 0; prev
+ j
< g
; j
++)
1787 if (this_from
> prev
[j
].from
)
1788 this_from
= prev
[j
].from
;
1789 if (this_to
< prev
[j
].to
)
1790 this_to
= prev
[j
].to
;
1792 for (; prev
<= g
; prev
++)
1794 prev
->from
= this_from
;
1799 if (otfg
->GlyphClass
== OTF_GlyphClass0
)
1801 else if (otfg
->GlyphClass
== OTF_GlyphClassMark
)
1810 if (out
->allocated
< out
->used
+ len
)
1812 font
->get_metrics (font
, in
, from
, to
);
1813 memcpy (out
->glyphs
+ out
->used
, in
->glyphs
+ from
,
1814 sizeof (MFLTGlyph
) * len
);
1819 static MFLTGlyphString gstring
;
1821 static int m17n_flt_initialized
;
1823 extern Lisp_Object QCfamily
;
1826 ftfont_shape_by_flt (lgstring
, font
, ft_face
, otf
)
1827 Lisp_Object lgstring
;
1832 EMACS_UINT len
= LGSTRING_GLYPH_LEN (lgstring
);
1834 struct MFLTFontFT flt_font_ft
;
1836 int with_variation_selector
= 0;
1838 if (! m17n_flt_initialized
)
1841 m17n_flt_initialized
= 1;
1844 for (i
= 0; i
< len
; i
++)
1846 Lisp_Object g
= LGSTRING_GLYPH (lgstring
, i
);
1851 c
= LGLYPH_CHAR (g
);
1852 if (CHAR_VARIATION_SELECTOR_P (c
))
1853 with_variation_selector
++;
1856 if (with_variation_selector
)
1858 setup_otf_gstring (len
);
1859 for (i
= 0; i
< len
; i
++)
1861 Lisp_Object g
= LGSTRING_GLYPH (lgstring
, i
);
1863 otf_gstring
.glyphs
[i
].c
= LGLYPH_CHAR (g
);
1864 otf_gstring
.glyphs
[i
].f
.index
.from
= LGLYPH_FROM (g
);
1865 otf_gstring
.glyphs
[i
].f
.index
.to
= LGLYPH_TO (g
);
1867 OTF_drive_cmap (otf
, &otf_gstring
);
1868 for (i
= 0; i
< otf_gstring
.used
; i
++)
1870 OTF_Glyph
*otfg
= otf_gstring
.glyphs
+ i
;
1871 Lisp_Object g0
= LGSTRING_GLYPH (lgstring
, otfg
->f
.index
.from
);
1872 Lisp_Object g1
= LGSTRING_GLYPH (lgstring
, otfg
->f
.index
.to
);
1874 LGLYPH_SET_CODE (g0
, otfg
->glyph_id
);
1875 LGLYPH_SET_TO (g0
, LGLYPH_TO (g1
));
1876 LGSTRING_SET_GLYPH (lgstring
, i
, g0
);
1878 if (len
> otf_gstring
.used
)
1880 len
= otf_gstring
.used
;
1881 LGSTRING_SET_GLYPH (lgstring
, len
, Qnil
);
1885 if (gstring
.allocated
== 0)
1887 gstring
.allocated
= len
* 2;
1888 gstring
.glyph_size
= sizeof (MFLTGlyph
);
1889 gstring
.glyphs
= malloc (sizeof (MFLTGlyph
) * gstring
.allocated
);
1891 else if (gstring
.allocated
< len
* 2)
1893 gstring
.allocated
= len
* 2;
1894 gstring
.glyphs
= realloc (gstring
.glyphs
,
1895 sizeof (MFLTGlyph
) * gstring
.allocated
);
1897 memset (gstring
.glyphs
, 0, sizeof (MFLTGlyph
) * len
);
1898 for (i
= 0; i
< len
; i
++)
1900 Lisp_Object g
= LGSTRING_GLYPH (lgstring
, i
);
1902 gstring
.glyphs
[i
].c
= LGLYPH_CHAR (g
);
1903 if (with_variation_selector
)
1905 gstring
.glyphs
[i
].code
= LGLYPH_CODE (g
);
1906 gstring
.glyphs
[i
].encoded
= 1;
1914 Lisp_Object family
= Ffont_get (LGSTRING_FONT (lgstring
), QCfamily
);
1917 flt_font_ft
.flt_font
.family
= Mnil
;
1919 flt_font_ft
.flt_font
.family
1920 = msymbol ((char *) SDATA (Fdowncase (SYMBOL_NAME (family
))));
1922 flt_font_ft
.flt_font
.x_ppem
= ft_face
->size
->metrics
.x_ppem
;
1923 flt_font_ft
.flt_font
.y_ppem
= ft_face
->size
->metrics
.y_ppem
;
1924 flt_font_ft
.flt_font
.get_glyph_id
= ftfont_get_glyph_id
;
1925 flt_font_ft
.flt_font
.get_metrics
= ftfont_get_metrics
;
1926 flt_font_ft
.flt_font
.check_otf
= ftfont_check_otf
;
1927 flt_font_ft
.flt_font
.drive_otf
= ftfont_drive_otf
;
1928 flt_font_ft
.flt_font
.internal
= NULL
;
1929 flt_font_ft
.font
= font
;
1930 flt_font_ft
.ft_face
= ft_face
;
1931 flt_font_ft
.otf
= otf
;
1933 && gstring
.glyphs
[1].c
>= 0x300 && gstring
.glyphs
[1].c
<= 0x36F)
1934 /* A little bit ad hoc. Perhaps, shaper must get script and
1935 language information, and select a proper flt for them
1937 flt
= mflt_get (msymbol ("combining"));
1938 for (i
= 0; i
< 3; i
++)
1940 int result
= mflt_run (&gstring
, 0, len
, &flt_font_ft
.flt_font
, flt
);
1943 gstring
.allocated
+= gstring
.allocated
;
1944 gstring
.glyphs
= realloc (gstring
.glyphs
,
1945 sizeof (MFLTGlyph
) * gstring
.allocated
);
1947 if (gstring
.used
> LGSTRING_GLYPH_LEN (lgstring
))
1949 for (i
= 0; i
< gstring
.used
; i
++)
1951 MFLTGlyph
*g
= gstring
.glyphs
+ i
;
1953 g
->from
= LGLYPH_FROM (LGSTRING_GLYPH (lgstring
, g
->from
));
1954 g
->to
= LGLYPH_TO (LGSTRING_GLYPH (lgstring
, g
->to
));
1957 for (i
= 0; i
< gstring
.used
; i
++)
1959 Lisp_Object lglyph
= LGSTRING_GLYPH (lgstring
, i
);
1960 MFLTGlyph
*g
= gstring
.glyphs
+ i
;
1964 lglyph
= Fmake_vector (make_number (LGLYPH_SIZE
), Qnil
);
1965 LGSTRING_SET_GLYPH (lgstring
, i
, lglyph
);
1967 LGLYPH_SET_FROM (lglyph
, g
->from
);
1968 LGLYPH_SET_TO (lglyph
, g
->to
);
1969 LGLYPH_SET_CHAR (lglyph
, g
->c
);
1970 LGLYPH_SET_CODE (lglyph
, g
->code
);
1971 LGLYPH_SET_WIDTH (lglyph
, g
->xadv
>> 6);
1972 LGLYPH_SET_LBEARING (lglyph
, g
->lbearing
>> 6);
1973 LGLYPH_SET_RBEARING (lglyph
, g
->rbearing
>> 6);
1974 LGLYPH_SET_ASCENT (lglyph
, g
->ascent
>> 6);
1975 LGLYPH_SET_DESCENT (lglyph
, g
->descent
>> 6);
1980 vec
= Fmake_vector (make_number (3), Qnil
);
1981 ASET (vec
, 0, make_number (g
->xoff
>> 6));
1982 ASET (vec
, 1, make_number (g
->yoff
>> 6));
1983 ASET (vec
, 2, make_number (g
->xadv
>> 6));
1984 LGLYPH_SET_ADJUSTMENT (lglyph
, vec
);
1987 return make_number (i
);
1991 ftfont_shape (lgstring
)
1992 Lisp_Object lgstring
;
1995 struct ftfont_info
*ftfont_info
;
1998 CHECK_FONT_GET_OBJECT (LGSTRING_FONT (lgstring
), font
);
1999 ftfont_info
= (struct ftfont_info
*) font
;
2000 otf
= ftfont_get_otf (ftfont_info
);
2002 return make_number (0);
2003 return ftfont_shape_by_flt (lgstring
, font
, ftfont_info
->ft_size
->face
, otf
);
2006 #ifdef HAVE_OTF_GET_VARIATION_GLYPHS
2009 ftfont_variation_glyphs (font
, c
, variations
)
2012 unsigned variations
[256];
2014 struct ftfont_info
*ftfont_info
= (struct ftfont_info
*) font
;
2015 OTF
*otf
= ftfont_get_otf (ftfont_info
);
2019 return OTF_get_variation_glyphs (otf
, c
, variations
);
2022 #endif /* HAVE_OTF_GET_VARIATION_GLYPHS */
2023 #endif /* HAVE_M17N_FLT */
2024 #endif /* HAVE_LIBOTF */
2027 ftfont_font_format (FcPattern
*pattern
, Lisp_Object filename
)
2031 #ifdef FC_FONTFORMAT
2034 if (FcPatternGetString (pattern
, FC_FONTFORMAT
, 0, &str
) != FcResultMatch
)
2036 if (strcmp ((char *) str
, "TrueType") == 0)
2037 return intern ("truetype");
2038 if (strcmp ((char *) str
, "Type 1") == 0)
2039 return intern ("type1");
2040 if (strcmp ((char *) str
, "PCF") == 0)
2041 return intern ("pcf");
2042 if (strcmp ((char *) str
, "BDF") == 0)
2043 return intern ("bdf");
2045 #endif /* FC_FONTFORMAT */
2046 if (STRINGP (filename
))
2048 int len
= SBYTES (filename
);
2052 str
= (FcChar8
*) (SDATA (filename
) + len
- 4);
2053 if (xstrcasecmp ((char *) str
, ".ttf") == 0)
2054 return intern ("truetype");
2055 if (xstrcasecmp ((char *) str
, ".pfb") == 0)
2056 return intern ("type1");
2057 if (xstrcasecmp ((char *) str
, ".pcf") == 0)
2058 return intern ("pcf");
2059 if (xstrcasecmp ((char *) str
, ".bdf") == 0)
2060 return intern ("bdf");
2063 return intern ("unknown");
2070 DEFSYM (Qfreetype
, "freetype");
2071 DEFSYM (Qmonospace
, "monospace");
2072 DEFSYM (Qsans_serif
, "sans-serif");
2073 DEFSYM (Qserif
, "serif");
2074 DEFSYM (Qmono
, "mono");
2075 DEFSYM (Qsans
, "sans");
2076 DEFSYM (Qsans__serif
, "sans serif");
2078 staticpro (&freetype_font_cache
);
2079 freetype_font_cache
= Fcons (Qt
, Qnil
);
2081 staticpro (&ftfont_generic_family_list
);
2082 ftfont_generic_family_list
2083 = Fcons (Fcons (Qmonospace
, Qt
),
2084 Fcons (Fcons (Qsans_serif
, Qt
),
2085 Fcons (Fcons (Qsans
, Qt
), Qnil
)));
2087 staticpro (&ft_face_cache
);
2088 ft_face_cache
= Qnil
;
2090 ftfont_driver
.type
= Qfreetype
;
2091 register_font_driver (&ftfont_driver
, NULL
);
2094 /* arch-tag: 7cfa432c-33a6-4988-83d2-a82ed8604aca
2095 (do not change this comment) */