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 areadly 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 */
55 static Lisp_Object ft_face_cache
;
57 /* The actual structure for FreeType font that can be casted to struct
66 int maybe_otf
; /* Flag to tell if this may be OTF or not. */
68 #endif /* HAVE_LIBOTF */
71 static Lisp_Object ftfont_pattern_entity
P_ ((FcPattern
*, Lisp_Object
, int));
73 static Lisp_Object ftfont_resolve_generic_family
P_ ((Lisp_Object
));
74 Lisp_Object ftfont_font_format
P_ ((FcPattern
*));
76 #define SYMBOL_FcChar8(SYM) (FcChar8 *) SDATA (SYMBOL_NAME (SYM))
82 /* characters to distinguish the charset from the others */
84 /* set in syms_of_ftfont */
87 FcCharSet
*fc_charset
;
88 } fc_charset_table
[] =
89 { { "iso-8859-1", { 0x00A0, 0x00A1, 0x00B4, 0x00BC, 0x00D0 }, -1 },
90 { "iso-8859-2", { 0x00A0, 0x010E }},
91 { "iso-8859-3", { 0x00A0, 0x0108 }},
92 { "iso-8859-4", { 0x00A0, 0x00AF, 0x0128, 0x0156, 0x02C7 }},
93 { "iso-8859-5", { 0x00A0, 0x0401 }},
94 { "iso-8859-6", { 0x00A0, 0x060C }},
95 { "iso-8859-7", { 0x00A0, 0x0384 }},
96 { "iso-8859-8", { 0x00A0, 0x05D0 }},
97 { "iso-8859-9", { 0x00A0, 0x00A1, 0x00BC, 0x011E }},
98 { "iso-8859-10", { 0x00A0, 0x00D0, 0x0128, 0x2015 }},
99 { "iso-8859-11", { 0x00A0, 0x0E01 }},
100 { "iso-8859-13", { 0x00A0, 0x201C }},
101 { "iso-8859-14", { 0x00A0, 0x0174 }},
102 { "iso-8859-15", { 0x00A0, 0x00A1, 0x00D0, 0x0152 }},
103 { "iso-8859-16", { 0x00A0, 0x0218}},
104 { "chinese-gb2312", { 0x4E13 }},
105 { "big5", { 0xF6B1 }},
106 { "japanese-jisx0208", { 0x4E55 }},
107 { "korean-ksc5601", { 0xAC00 }},
108 { "chinese-cns11643-1", { 0xFE32 }},
109 { "chinese-cns11643-2", { 0x4E33, 0x7934 }},
110 { "chinese-cns11643-3", { 0x201A9 }},
111 { "chinese-cns11643-4", { 0x20057 }},
112 { "chinese-cns11643-5", { 0x20000 }},
113 { "chinese-cns11643-6", { 0x20003 }},
114 { "chinese-cns11643-7", { 0x20055 }},
115 { "chinese-gbk", { 0x4E06 }},
116 { "japanese-jisx0212", { 0x4E44 }},
117 { "japanese-jisx0213-1", { 0xFA10 }},
118 { "japanese-jisx0213-2", { 0xFA49 }},
119 { "japanese-jisx0213.2004-1", { 0x20B9F }},
120 { "viscii", { 0x1EA0, 0x1EAE, 0x1ED2 }},
121 { "tis620", { 0x0E01 }},
122 { "windows-1251", { 0x0401, 0x0490 }},
123 { "koi8-r", { 0x0401, 0x2219 }},
124 { "mule-lao", { 0x0E81 }},
128 extern Lisp_Object Qc
, Qm
, Qp
, Qd
;
131 ftfont_pattern_entity (p
, registry
, fc_charset_idx
)
133 Lisp_Object registry
;
142 if (FcPatternGetString (p
, FC_FILE
, 0, (FcChar8
**) &file
) != FcResultMatch
)
145 entity
= font_make_entity ();
147 ASET (entity
, FONT_TYPE_INDEX
, Qfreetype
);
148 ASET (entity
, FONT_REGISTRY_INDEX
, registry
);
150 if (FcPatternGetString (p
, FC_FOUNDRY
, 0, (FcChar8
**) &str
) == FcResultMatch
)
151 ASET (entity
, FONT_FOUNDRY_INDEX
, font_intern_prop (str
, strlen (str
)));
152 if (FcPatternGetString (p
, FC_FAMILY
, 0, (FcChar8
**) &str
) == FcResultMatch
)
153 ASET (entity
, FONT_FAMILY_INDEX
, font_intern_prop (str
, strlen (str
)));
154 if (FcPatternGetInteger (p
, FC_WEIGHT
, 0, &numeric
) == FcResultMatch
)
156 if (numeric
>= FC_WEIGHT_REGULAR
&& numeric
< FC_WEIGHT_MEDIUM
)
157 numeric
= FC_WEIGHT_MEDIUM
;
158 FONT_SET_STYLE (entity
, FONT_WEIGHT_INDEX
, make_number (numeric
));
160 if (FcPatternGetInteger (p
, FC_SLANT
, 0, &numeric
) == FcResultMatch
)
163 FONT_SET_STYLE (entity
, FONT_SLANT_INDEX
, make_number (numeric
));
165 if (FcPatternGetInteger (p
, FC_WIDTH
, 0, &numeric
) == FcResultMatch
)
167 FONT_SET_STYLE (entity
, FONT_WIDTH_INDEX
, make_number (numeric
));
169 if (FcPatternGetDouble (p
, FC_PIXEL_SIZE
, 0, &dbl
) == FcResultMatch
)
170 ASET (entity
, FONT_SIZE_INDEX
, make_number (dbl
));
172 ASET (entity
, FONT_SIZE_INDEX
, make_number (0));
173 if (FcPatternGetInteger (p
, FC_SPACING
, 0, &numeric
) == FcResultMatch
)
174 ASET (entity
, FONT_SPACING_INDEX
, make_number (numeric
));
175 if (FcPatternGetDouble (p
, FC_DPI
, 0, &dbl
) == FcResultMatch
)
178 ASET (entity
, FONT_DPI_INDEX
, make_number (dpi
));
180 if (FcPatternGetBool (p
, FC_SCALABLE
, 0, &b
) == FcResultMatch
182 ASET (entity
, FONT_AVGWIDTH_INDEX
, make_number (0));
184 font_put_extra (entity
, QCfont_entity
,
185 Fcons (make_unibyte_string ((char *) file
,
186 strlen ((char *) file
)),
187 make_number (fc_charset_idx
)));
192 static Lisp_Object ftfont_generic_family_list
;
195 ftfont_resolve_generic_family (family
)
199 FcPattern
*pattern
= NULL
, *match
;
202 family
= Fintern (Fdowncase (SYMBOL_NAME (family
)), Qnil
);
203 if (EQ (family
, Qmono
))
205 else if (EQ (family
, Qsans
) || EQ (family
, Qsans__serif
))
206 family
= Qsans_serif
;
207 slot
= assq_no_quit (family
, ftfont_generic_family_list
);
210 if (! EQ (XCDR (slot
), Qt
))
212 pattern
= FcPatternBuild (NULL
, FC_FAMILY
, FcTypeString
,
213 SYMBOL_FcChar8 (family
), (char *) 0);
216 FcConfigSubstitute (NULL
, pattern
, FcMatchPattern
);
217 FcDefaultSubstitute (pattern
);
218 match
= FcFontMatch (NULL
, pattern
, &result
);
223 if (FcPatternGetString (match
, FC_FAMILY
, 0, &fam
) == FcResultMatch
)
224 family
= intern ((char *) fam
);
228 XSETCDR (slot
, family
);
230 if (match
) FcPatternDestroy (match
);
231 if (pattern
) FcPatternDestroy (pattern
);
236 ftfont_lookup_cache (filename
)
237 Lisp_Object filename
;
239 Lisp_Object cache
, val
;
241 cache
= assoc_no_quit (filename
, ft_face_cache
);
244 val
= make_save_value (NULL
, 0);
245 cache
= Fcons (filename
, val
);
246 ft_face_cache
= Fcons (cache
, ft_face_cache
);
250 if (! XSAVE_VALUE (val
)->pointer
)
255 && FT_Init_FreeType (&ft_library
) != 0)
257 if (FT_New_Face (ft_library
, (char *) SDATA (filename
), 0, &ft_face
) != 0)
259 XSAVE_VALUE (val
)->pointer
= ft_face
;
264 static Lisp_Object ftfont_get_cache
P_ ((FRAME_PTR
));
265 static Lisp_Object ftfont_list
P_ ((Lisp_Object
, Lisp_Object
));
266 static Lisp_Object ftfont_match
P_ ((Lisp_Object
, Lisp_Object
));
267 static Lisp_Object ftfont_list_family
P_ ((Lisp_Object
));
268 static Lisp_Object ftfont_open
P_ ((FRAME_PTR
, Lisp_Object
, int));
269 static void ftfont_close
P_ ((FRAME_PTR
, struct font
*));
270 static int ftfont_has_char
P_ ((Lisp_Object
, int));
271 static unsigned ftfont_encode_char
P_ ((struct font
*, int));
272 static int ftfont_text_extents
P_ ((struct font
*, unsigned *, int,
273 struct font_metrics
*));
274 static int ftfont_get_bitmap
P_ ((struct font
*, unsigned,
275 struct font_bitmap
*, int));
276 static int ftfont_anchor_point
P_ ((struct font
*, unsigned, int,
278 static Lisp_Object ftfont_shape
P_ ((Lisp_Object
));
280 struct font_driver ftfont_driver
=
283 0, /* case insensitive */
291 /* We can't draw a text without device dependent functions. */
297 /* We can't draw a text without device dependent functions. */
308 #if defined (HAVE_M17N_FLT) && defined (HAVE_LIBOTF)
310 #else /* not (HAVE_M17N_FLT && HAVE_LIBOTF) */
312 #endif /* not (HAVE_M17N_FLT && HAVE_LIBOTF) */
315 extern Lisp_Object QCname
;
321 return freetype_font_cache
;
325 ftfont_get_charset (registry
)
326 Lisp_Object registry
;
328 struct charset
*encoding
;
331 if (font_registry_charsets (registry
, &encoding
, NULL
) < 0)
333 if (fc_charset_table
[0].charset_id
< 0)
334 /* Setup charset_id field of all elements. */
335 for (i
= 0; fc_charset_table
[i
].name
; i
++)
337 Lisp_Object sym
= intern (fc_charset_table
[i
].name
);
340 fc_charset_table
[i
].charset_id
= XINT (CHARSET_SYMBOL_ID (sym
));
342 fc_charset_table
[i
].charset_id
= -1;
345 for (i
= 0; fc_charset_table
[i
].name
; i
++)
346 if (encoding
->id
== fc_charset_table
[i
].charset_id
)
348 if (! fc_charset_table
[i
].name
)
350 if (! fc_charset_table
[i
].fc_charset
)
352 FcCharSet
*charset
= FcCharSetCreate ();
353 int *uniquifier
= fc_charset_table
[i
].uniquifier
;
357 for (j
= 0; uniquifier
[j
]; j
++)
358 if (! FcCharSetAddChar (charset
, uniquifier
[j
]))
360 FcCharSetDestroy (charset
);
363 fc_charset_table
[i
].fc_charset
= charset
;
371 unsigned int script_tag
, langsys_tag
;
373 unsigned int *features
[2];
376 #define OTF_SYM_TAG(SYM, TAG) \
378 unsigned char *p = SDATA (SYMBOL_NAME (SYM)); \
379 TAG = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]; \
382 #define OTF_TAG_STR(TAG, P) \
384 (P)[0] = (char) (TAG >> 24); \
385 (P)[1] = (char) ((TAG >> 16) & 0xFF); \
386 (P)[2] = (char) ((TAG >> 8) & 0xFF); \
387 (P)[3] = (char) (TAG & 0xFF); \
391 static struct OpenTypeSpec
*
392 ftfont_get_open_type_spec (Lisp_Object otf_spec
)
394 struct OpenTypeSpec
*spec
= malloc (sizeof (struct OpenTypeSpec
));
400 spec
->script
= XCAR (otf_spec
);
401 if (! NILP (spec
->script
))
403 OTF_SYM_TAG (spec
->script
, spec
->script_tag
);
404 val
= assq_no_quit (spec
->script
, Votf_script_alist
);
405 if (CONSP (val
) && SYMBOLP (XCDR (val
)))
406 spec
->script
= XCDR (val
);
411 spec
->script_tag
= 0x44464C54; /* "DFLT" */
412 otf_spec
= XCDR (otf_spec
);
413 val
= XCAR (otf_spec
);
415 OTF_SYM_TAG (val
, spec
->langsys_tag
);
417 spec
->langsys_tag
= 0;
418 spec
->nfeatures
[0] = spec
->nfeatures
[1] = 0;
419 for (i
= 0; i
< 2; i
++)
423 otf_spec
= XCDR (otf_spec
);
426 val
= XCAR (otf_spec
);
430 spec
->features
[i
] = malloc (sizeof (int) * XINT (len
));
431 if (! spec
->features
[i
])
433 if (i
> 0 && spec
->features
[0])
434 free (spec
->features
[0]);
438 for (j
= 0, negative
= 0; CONSP (val
); val
= XCDR (val
))
440 if (NILP (XCAR (val
)))
446 OTF_SYM_TAG (XCAR (val
), tag
);
447 spec
->features
[i
][j
++] = negative
? tag
& 0x80000000 : tag
;
450 spec
->nfeatures
[i
] = j
;
455 static FcPattern
*ftfont_spec_pattern
P_ ((Lisp_Object
, int *, char *,
456 struct OpenTypeSpec
**));
459 ftfont_spec_pattern (spec
, fc_charset_idx
, otlayout
, otspec
)
463 struct OpenTypeSpec
**otspec
;
465 Lisp_Object tmp
, extra
;
466 FcPattern
*pattern
= NULL
;
467 FcCharSet
*charset
= NULL
;
468 FcLangSet
*langset
= NULL
;
473 Lisp_Object name
= Qnil
;
474 Lisp_Object script
= Qnil
;
475 Lisp_Object registry
;
477 if (! NILP (AREF (spec
, FONT_ADSTYLE_INDEX
))
478 && SBYTES (SYMBOL_NAME (AREF (spec
, FONT_ADSTYLE_INDEX
))) > 0)
479 /* Fontconfig doesn't support adstyle property. */
481 if ((n
= FONT_SLANT_NUMERIC (spec
)) >= 0
483 /* Fontconfig doesn't support reverse-italic/obligue. */
486 if (INTEGERP (AREF (spec
, FONT_DPI_INDEX
)))
487 dpi
= XINT (AREF (spec
, FONT_DPI_INDEX
));
488 if (INTEGERP (AREF (spec
, FONT_SPACING_INDEX
)))
489 spacing
= XINT (AREF (spec
, FONT_SPACING_INDEX
));
490 if (INTEGERP (AREF (spec
, FONT_AVGWIDTH_INDEX
))
491 && XINT (AREF (spec
, FONT_AVGWIDTH_INDEX
)) == 0)
494 registry
= AREF (spec
, FONT_REGISTRY_INDEX
);
496 || EQ (registry
, Qiso10646_1
)
497 || EQ (registry
, Qunicode_bmp
)
498 || EQ (registry
, Qunicode_sip
))
499 *fc_charset_idx
= -1;
502 *fc_charset_idx
= ftfont_get_charset (registry
);
503 if (*fc_charset_idx
< 0)
505 charset
= fc_charset_table
[*fc_charset_idx
].fc_charset
;
509 for (extra
= AREF (spec
, FONT_EXTRA_INDEX
);
510 CONSP (extra
); extra
= XCDR (extra
))
512 Lisp_Object key
, val
;
514 key
= XCAR (XCAR (extra
)), val
= XCDR (XCAR (extra
));
517 else if (EQ (key
, QCfc_unknown_spec
))
519 else if (EQ (key
, QClang
))
521 langset
= FcLangSetCreate ();
526 if (! FcLangSetAdd (langset
, SYMBOL_FcChar8 (val
)))
530 for (; CONSP (val
); val
= XCDR (val
))
531 if (SYMBOLP (XCAR (val
))
532 && ! FcLangSetAdd (langset
, SYMBOL_FcChar8 (XCAR (val
))))
535 else if (EQ (key
, QCname
))
537 else if (EQ (key
, QCotf
))
539 *otspec
= ftfont_get_open_type_spec (val
);
542 strcat (otlayout
, "otlayout:");
543 OTF_TAG_STR ((*otspec
)->script_tag
, otlayout
+ 9);
544 script
= (*otspec
)->script
;
546 else if (EQ (key
, QCscript
))
548 else if (EQ (key
, QCscalable
))
549 scalable
= ! NILP (val
);
552 if (! NILP (script
) && ! charset
)
554 Lisp_Object chars
= assq_no_quit (script
, Vscript_representative_chars
);
558 charset
= FcCharSetCreate ();
561 for (chars
= XCDR (chars
); CONSP (chars
); chars
= XCDR (chars
))
562 if (CHARACTERP (XCAR (chars
))
563 && ! FcCharSetAddChar (charset
, XUINT (XCAR (chars
))))
568 pattern
= NILP (name
) ? FcPatternCreate () : FcNameParse (SDATA (name
));
571 FcPatternDel (pattern
, FC_SIZE
);
572 FcPatternDel (pattern
, FC_PIXEL_SIZE
);
573 FcPatternDel (pattern
, FC_FOUNDRY
);
574 FcPatternDel (pattern
, FC_FAMILY
);
575 tmp
= AREF (spec
, FONT_FOUNDRY_INDEX
);
577 && ! FcPatternAddString (pattern
, FC_FOUNDRY
, SYMBOL_FcChar8 (tmp
)))
579 tmp
= AREF (spec
, FONT_FAMILY_INDEX
);
581 && ! FcPatternAddString (pattern
, FC_FAMILY
, SYMBOL_FcChar8 (tmp
)))
584 && ! FcPatternAddCharSet (pattern
, FC_CHARSET
, charset
))
587 && ! FcPatternAddLangSet (pattern
, FC_LANG
, langset
))
590 && ! FcPatternAddDouble (pattern
, FC_DPI
, dpi
))
593 && ! FcPatternAddInteger (pattern
, FC_SPACING
, spacing
))
596 && ! FcPatternAddBool (pattern
, FC_SCALABLE
, scalable
? FcTrue
: FcFalse
))
601 /* We come here because of unexpected error in fontconfig API call
602 (usually insufficient memory). */
605 FcPatternDestroy (pattern
);
610 if ((*otspec
)->nfeatures
[0] > 0)
611 free ((*otspec
)->features
[0]);
612 if ((*otspec
)->nfeatures
[1] > 0)
613 free ((*otspec
)->features
[1]);
619 if (langset
) FcLangSetDestroy (langset
);
620 if (charset
&& *fc_charset_idx
< 0) FcCharSetDestroy (charset
);
625 ftfont_list (frame
, spec
)
626 Lisp_Object frame
, spec
;
628 Lisp_Object val
= Qnil
, registry
, family
;
631 FcFontSet
*fontset
= NULL
;
632 FcObjectSet
*objset
= NULL
;
634 char otlayout
[15]; /* For "otlayout:XXXX" */
635 struct OpenTypeSpec
*otspec
= NULL
;
637 if (! fc_initialized
)
643 pattern
= ftfont_spec_pattern (spec
, &fc_charset_idx
, otlayout
, &otspec
);
646 registry
= AREF (spec
, FONT_REGISTRY_INDEX
);
647 family
= AREF (spec
, FONT_FAMILY_INDEX
);
650 Lisp_Object resolved
;
652 resolved
= ftfont_resolve_generic_family (family
);
653 if (! NILP (resolved
))
655 FcPatternDel (pattern
, FC_FAMILY
);
656 if (! FcPatternAddString (pattern
, FC_FAMILY
,
657 SYMBOL_FcChar8 (resolved
)))
662 objset
= FcObjectSetBuild (FC_FOUNDRY
, FC_FAMILY
, FC_WEIGHT
, FC_SLANT
,
663 FC_WIDTH
, FC_PIXEL_SIZE
, FC_SPACING
, FC_SCALABLE
,
667 #endif /* FC_CAPABILITY */
672 fontset
= FcFontList (NULL
, pattern
, objset
);
675 for (i
= 0; i
< fontset
->nfont
; i
++)
684 if (FcPatternGetString (fontset
->fonts
[i
], FC_CAPABILITY
, 0,
685 &this) != FcResultMatch
686 || ! strstr ((char *) this, otlayout
))
689 #endif /* FC_CAPABILITY */
696 if (FcPatternGetString (fontset
->fonts
[i
], FC_FILE
, 0, &file
)
699 otf
= OTF_open ((char *) file
);
702 if (OTF_check_features (otf
, 1,
703 otspec
->script_tag
, otspec
->langsys_tag
,
705 otspec
->nfeatures
[0]) != 1
706 || OTF_check_features (otf
, 0,
707 otspec
->script_tag
, otspec
->langsys_tag
,
709 otspec
->nfeatures
[1]) != 1)
712 #endif /* HAVE_LIBOTF */
713 entity
= ftfont_pattern_entity (fontset
->fonts
[i
], registry
,
716 val
= Fcons (entity
, val
);
718 font_add_log ("ftfont-list", spec
, val
);
722 /* We come here because of unexpected error in fontconfig API call
723 (usually insufficient memory). */
727 if (objset
) FcObjectSetDestroy (objset
);
728 if (fontset
) FcFontSetDestroy (fontset
);
729 if (pattern
) FcPatternDestroy (pattern
);
734 ftfont_match (frame
, spec
)
735 Lisp_Object frame
, spec
;
738 FcPattern
*pattern
, *match
= NULL
;
740 char otlayout
[15]; /* For "otlayout:XXXX" */
741 struct OpenTypeSpec
*otspec
= NULL
;
744 if (! fc_initialized
)
750 pattern
= ftfont_spec_pattern (spec
, &fc_charset_idx
, otlayout
, &otspec
);
754 if (INTEGERP (AREF (spec
, FONT_SIZE_INDEX
)))
758 value
.type
= FcTypeDouble
;
759 value
.u
.d
= XINT (AREF (spec
, FONT_SIZE_INDEX
));
760 FcPatternAdd (pattern
, FC_PIXEL_SIZE
, value
, FcFalse
);
762 if (FcConfigSubstitute (NULL
, pattern
, FcMatchPattern
) == FcTrue
)
764 FcDefaultSubstitute (pattern
);
765 match
= FcFontMatch (NULL
, pattern
, &result
);
768 entity
= ftfont_pattern_entity (match
, Qunicode_bmp
, fc_charset_idx
);
769 FcPatternDestroy (match
);
770 if (! NILP (AREF (spec
, FONT_FAMILY_INDEX
))
771 && NILP (assq_no_quit (AREF (spec
, FONT_FAMILY_INDEX
),
772 ftfont_generic_family_list
))
773 && NILP (Fstring_equal (AREF (spec
, FONT_FAMILY_INDEX
),
774 AREF (entity
, FONT_FAMILY_INDEX
))))
778 FcPatternDestroy (pattern
);
780 font_add_log ("ftfont-match", spec
, entity
);
785 ftfont_list_family (frame
)
789 FcPattern
*pattern
= NULL
;
790 FcFontSet
*fontset
= NULL
;
791 FcObjectSet
*objset
= NULL
;
794 if (! fc_initialized
)
800 pattern
= FcPatternCreate ();
803 objset
= FcObjectSetBuild (FC_FAMILY
, NULL
);
806 fontset
= FcFontList (NULL
, pattern
, objset
);
811 for (i
= 0; i
< fontset
->nfont
; i
++)
813 FcPattern
*pat
= fontset
->fonts
[i
];
816 if (FcPatternGetString (pat
, FC_FAMILY
, 0, &str
) == FcResultMatch
)
817 list
= Fcons (intern ((char *) str
), list
);
821 if (objset
) FcObjectSetDestroy (objset
);
822 if (fontset
) FcFontSetDestroy (fontset
);
823 if (pattern
) FcPatternDestroy (pattern
);
830 ftfont_open (f
, entity
, pixel_size
)
835 struct ftfont_info
*ftfont_info
;
840 Lisp_Object val
, filename
, cache
, font_object
;
849 val
= assq_no_quit (QCfont_entity
, AREF (entity
, FONT_EXTRA_INDEX
));
853 filename
= XCAR (val
);
854 fc_charset_idx
= XINT (XCDR (val
));
855 cache
= ftfont_lookup_cache (filename
);
858 filename
= XCAR (cache
);
860 ft_face
= XSAVE_VALUE (val
)->pointer
;
861 if (XSAVE_VALUE (val
)->integer
> 0)
863 /* FT_Face in this cache is already used by the different size. */
864 if (FT_New_Size (ft_face
, &ft_size
) != 0)
866 if (FT_Activate_Size (ft_size
) != 0)
868 FT_Done_Size (ft_size
);
872 XSAVE_VALUE (val
)->integer
++;
873 size
= XINT (AREF (entity
, FONT_SIZE_INDEX
));
876 if (FT_Set_Pixel_Sizes (ft_face
, size
, size
) != 0)
878 if (XSAVE_VALUE (val
)->integer
== 0)
879 FT_Done_Face (ft_face
);
883 font_object
= font_make_object (VECSIZE (struct ftfont_info
));
884 ASET (font_object
, FONT_TYPE_INDEX
, Qfreetype
);
885 for (i
= 1;i
< FONT_ENTITY_MAX
; i
++)
886 ASET (font_object
, i
, AREF (entity
, i
));
887 ASET (font_object
, FONT_SIZE_INDEX
, make_number (size
));
888 len
= font_unparse_xlfd (entity
, size
, name
, 256);
890 ASET (font_object
, FONT_NAME_INDEX
, make_unibyte_string (name
, len
));
891 len
= font_unparse_fcname (entity
, size
, name
, 256);
893 ASET (font_object
, FONT_FULLNAME_INDEX
, make_unibyte_string (name
, len
));
895 ASET (font_object
, FONT_FULLNAME_INDEX
,
896 AREF (font_object
, FONT_NAME_INDEX
));
897 ASET (font_object
, FONT_FILE_INDEX
, filename
);
898 ASET (font_object
, FONT_FORMAT_INDEX
, ftfont_font_format (pattern
));
899 font
= XFONT_OBJECT (font_object
);
900 ftfont_info
= (struct ftfont_info
*) font
;
901 ftfont_info
->ft_size
= ft_face
->size
;
902 ftfont_info
->fc_charset_idx
= fc_charset_idx
;
904 ftfont_info
->maybe_otf
= ft_face
->face_flags
& FT_FACE_FLAG_SFNT
;
905 ftfont_info
->otf
= NULL
;
906 #endif /* HAVE_LIBOTF */
907 font
->pixel_size
= size
;
908 font
->driver
= &ftfont_driver
;
909 font
->encoding_charset
= font
->repertory_charset
= -1;
911 upEM
= ft_face
->units_per_EM
;
912 scalable
= (INTEGERP (AREF (entity
, FONT_AVGWIDTH_INDEX
))
913 && XINT (AREF (entity
, FONT_AVGWIDTH_INDEX
)) == 0);
916 font
->ascent
= ft_face
->ascender
* size
/ upEM
;
917 font
->descent
= - ft_face
->descender
* size
/ upEM
;
918 font
->height
= ft_face
->height
* size
/ upEM
;
922 font
->ascent
= ft_face
->size
->metrics
.ascender
>> 6;
923 font
->descent
= - ft_face
->size
->metrics
.descender
>> 6;
924 font
->height
= ft_face
->size
->metrics
.height
>> 6;
926 if (INTEGERP (AREF (entity
, FONT_SPACING_INDEX
)))
927 spacing
= XINT (AREF (entity
, FONT_SPACING_INDEX
));
929 spacing
= FC_PROPORTIONAL
;
930 if (spacing
!= FC_PROPORTIONAL
)
931 font
->min_width
= font
->average_width
= font
->space_width
932 = (scalable
? ft_face
->max_advance_width
* size
/ upEM
933 : ft_face
->size
->metrics
.max_advance
>> 6);
938 font
->min_width
= font
->average_width
= font
->space_width
= 0;
939 for (i
= 32, n
= 0; i
< 127; i
++)
940 if (FT_Load_Char (ft_face
, i
, FT_LOAD_DEFAULT
) != 0)
942 int this_width
= ft_face
->glyph
->metrics
.horiAdvance
>> 6;
945 && (! font
->min_width
|| font
->min_width
> this_width
))
946 font
->min_width
= this_width
;
948 font
->space_width
= this_width
;
949 font
->average_width
+= this_width
;
953 font
->average_width
/= n
;
956 font
->baseline_offset
= 0;
957 font
->relative_compose
= 0;
958 font
->default_ascent
= 0;
959 font
->vertical_centering
= 0;
962 font
->underline_position
= -ft_face
->underline_position
* size
/ upEM
;
963 font
->underline_thickness
= -ft_face
->underline_thickness
* size
/ upEM
;
967 font
->underline_position
= -1;
968 font
->underline_thickness
= 0;
975 ftfont_close (f
, font
)
979 struct ftfont_info
*ftfont_info
= (struct ftfont_info
*) font
;
980 Lisp_Object val
, cache
;
982 cache
= ftfont_lookup_cache (font
->props
[FONT_FILE_INDEX
]);
984 (XSAVE_VALUE (val
)->integer
)--;
985 if (XSAVE_VALUE (val
)->integer
== 0)
987 FT_Face ft_face
= XSAVE_VALUE (val
)->pointer
;
989 FT_Done_Face (ft_face
);
991 if (ftfont_info
->otf
)
992 OTF_close (ftfont_info
->otf
);
994 XSAVE_VALUE (val
)->pointer
= NULL
;
997 FT_Done_Size (ftfont_info
->ft_size
);
1001 ftfont_has_char (entity
, c
)
1007 struct charset
*charset
;
1009 val
= assq_no_quit (QCfont_entity
, AREF (entity
, FONT_EXTRA_INDEX
));
1010 fc_charset_idx
= XINT (XCDR (XCDR (val
)));
1011 if (fc_charset_idx
< 0)
1013 charset
= CHARSET_FROM_ID (fc_charset_table
[fc_charset_idx
].charset_id
);
1014 return (ENCODE_CHAR (charset
, c
) != CHARSET_INVALID_CODE (charset
));
1018 ftfont_encode_char (font
, c
)
1022 struct ftfont_info
*ftfont_info
= (struct ftfont_info
*) font
;
1023 FT_Face ft_face
= ftfont_info
->ft_size
->face
;
1024 FT_ULong charcode
= c
;
1025 FT_UInt code
= FT_Get_Char_Index (ft_face
, charcode
);
1027 return (code
> 0 ? code
: FONT_INVALID_CODE
);
1031 ftfont_text_extents (font
, code
, nglyphs
, metrics
)
1035 struct font_metrics
*metrics
;
1037 struct ftfont_info
*ftfont_info
= (struct ftfont_info
*) font
;
1038 FT_Face ft_face
= ftfont_info
->ft_size
->face
;
1042 if (ftfont_info
->ft_size
!= ft_face
->size
)
1043 FT_Activate_Size (ftfont_info
->ft_size
);
1045 bzero (metrics
, sizeof (struct font_metrics
));
1046 for (i
= 0; i
< nglyphs
; i
++)
1048 if (FT_Load_Glyph (ft_face
, code
[i
], FT_LOAD_DEFAULT
) == 0)
1050 FT_Glyph_Metrics
*m
= &ft_face
->glyph
->metrics
;
1054 if (metrics
->lbearing
> width
+ (m
->horiBearingX
>> 6))
1055 metrics
->lbearing
= width
+ (m
->horiBearingX
>> 6);
1056 if (metrics
->rbearing
1057 < width
+ ((m
->horiBearingX
+ m
->width
) >> 6))
1059 = width
+ ((m
->horiBearingX
+ m
->width
) >> 6);
1060 if (metrics
->ascent
< (m
->horiBearingY
>> 6))
1061 metrics
->ascent
= m
->horiBearingY
>> 6;
1062 if (metrics
->descent
> ((m
->horiBearingY
+ m
->height
) >> 6))
1063 metrics
->descent
= (m
->horiBearingY
+ m
->height
) >> 6;
1065 width
+= m
->horiAdvance
>> 6;
1069 width
+= font
->space_width
;
1073 metrics
->width
= width
;
1079 ftfont_get_bitmap (font
, code
, bitmap
, bits_per_pixel
)
1082 struct font_bitmap
*bitmap
;
1085 struct ftfont_info
*ftfont_info
= (struct ftfont_info
*) font
;
1086 FT_Face ft_face
= ftfont_info
->ft_size
->face
;
1087 FT_Int32 load_flags
= FT_LOAD_RENDER
;
1089 if (ftfont_info
->ft_size
!= ft_face
->size
)
1090 FT_Activate_Size (ftfont_info
->ft_size
);
1091 if (bits_per_pixel
== 1)
1093 #ifdef FT_LOAD_TARGET_MONO
1094 load_flags
|= FT_LOAD_TARGET_MONO
;
1096 load_flags
|= FT_LOAD_MONOCHROME
;
1099 else if (bits_per_pixel
!= 8)
1100 /* We don't support such a rendering. */
1103 if (FT_Load_Glyph (ft_face
, code
, load_flags
) != 0)
1105 bitmap
->bits_per_pixel
1106 = (ft_face
->glyph
->bitmap
.pixel_mode
== FT_PIXEL_MODE_MONO
? 1
1107 : ft_face
->glyph
->bitmap
.pixel_mode
== FT_PIXEL_MODE_GRAY
? 8
1108 : ft_face
->glyph
->bitmap
.pixel_mode
== FT_PIXEL_MODE_LCD
? 8
1109 : ft_face
->glyph
->bitmap
.pixel_mode
== FT_PIXEL_MODE_LCD_V
? 8
1111 if (bitmap
->bits_per_pixel
< 0)
1112 /* We don't suport that kind of pixel mode. */
1114 bitmap
->rows
= ft_face
->glyph
->bitmap
.rows
;
1115 bitmap
->width
= ft_face
->glyph
->bitmap
.width
;
1116 bitmap
->pitch
= ft_face
->glyph
->bitmap
.pitch
;
1117 bitmap
->buffer
= ft_face
->glyph
->bitmap
.buffer
;
1118 bitmap
->left
= ft_face
->glyph
->bitmap_left
;
1119 bitmap
->top
= ft_face
->glyph
->bitmap_top
;
1120 bitmap
->advance
= ft_face
->glyph
->metrics
.horiAdvance
>> 6;
1121 bitmap
->extra
= NULL
;
1127 ftfont_anchor_point (font
, code
, index
, x
, y
)
1133 struct ftfont_info
*ftfont_info
= (struct ftfont_info
*) font
;
1134 FT_Face ft_face
= ftfont_info
->ft_size
->face
;
1136 if (ftfont_info
->ft_size
!= ft_face
->size
)
1137 FT_Activate_Size (ftfont_info
->ft_size
);
1138 if (FT_Load_Glyph (ft_face
, code
, FT_LOAD_DEFAULT
) != 0)
1140 if (ft_face
->glyph
->format
!= FT_GLYPH_FORMAT_OUTLINE
)
1142 if (index
>= ft_face
->glyph
->outline
.n_points
)
1144 *x
= ft_face
->glyph
->outline
.points
[index
].x
;
1145 *y
= ft_face
->glyph
->outline
.points
[index
].y
;
1150 #ifdef HAVE_M17N_FLT
1161 ftfont_get_glyph_id (font
, gstring
, from
, to
)
1163 MFLTGlyphString
*gstring
;
1166 struct MFLTFontFT
*flt_font_ft
= (struct MFLTFontFT
*) font
;
1167 FT_Face ft_face
= flt_font_ft
->ft_face
;
1170 for (g
= gstring
->glyphs
+ from
; from
< to
; g
++, from
++)
1173 FT_UInt code
= FT_Get_Char_Index (ft_face
, g
->code
);
1175 g
->code
= code
> 0 ? code
: FONT_INVALID_CODE
;
1182 ftfont_get_metrics (font
, gstring
, from
, to
)
1184 MFLTGlyphString
*gstring
;
1187 struct MFLTFontFT
*flt_font_ft
= (struct MFLTFontFT
*) font
;
1188 FT_Face ft_face
= flt_font_ft
->ft_face
;
1191 for (g
= gstring
->glyphs
+ from
; from
< to
; g
++, from
++)
1194 if (g
->code
!= FONT_INVALID_CODE
)
1196 FT_Glyph_Metrics
*m
;
1198 if (FT_Load_Glyph (ft_face
, g
->code
, FT_LOAD_DEFAULT
) != 0)
1200 m
= &ft_face
->glyph
->metrics
;
1202 g
->lbearing
= m
->horiBearingX
;
1203 g
->rbearing
= m
->horiBearingX
+ m
->width
;
1204 g
->ascent
= m
->horiBearingY
;
1205 g
->descent
= m
->height
- m
->horiBearingY
;
1206 g
->xadv
= m
->horiAdvance
;
1211 g
->rbearing
= g
->xadv
= flt_font_ft
->font
->space_width
<< 6;
1212 g
->ascent
= flt_font_ft
->font
->ascent
<< 6;
1213 g
->descent
= flt_font_ft
->font
->descent
<< 6;
1222 ftfont_check_otf (MFLTFont
*font
, MFLTOtfSpec
*spec
)
1224 struct MFLTFontFT
*flt_font_ft
= (struct MFLTFontFT
*) font
;
1225 OTF
*otf
= flt_font_ft
->otf
;
1229 for (i
= 0; i
< 2; i
++)
1231 if (! spec
->features
[i
])
1233 for (n
= 0; spec
->features
[i
][n
]; n
++);
1234 tags
= alloca (sizeof (OTF_Tag
) * n
);
1235 for (n
= 0, negative
= 0; spec
->features
[i
][n
]; n
++)
1237 if (spec
->features
[i
][n
] == 0xFFFFFFFF)
1240 tags
[n
- 1] = spec
->features
[i
][n
] | 0x80000000;
1242 tags
[n
] = spec
->features
[i
][n
];
1244 if (n
- negative
> 0
1245 && OTF_check_features (otf
, i
== 0, spec
->script
, spec
->langsys
,
1246 tags
, n
- negative
) != 1)
1252 #define DEVICE_DELTA(table, size) \
1253 (((size) >= (table).StartSize && (size) <= (table).EndSize) \
1254 ? (table).DeltaValue[(size) - (table).StartSize] << 6 \
1258 adjust_anchor (FT_Face ft_face
, OTF_Anchor
*anchor
,
1259 unsigned code
, int x_ppem
, int y_ppem
, int *x
, int *y
)
1261 if (anchor
->AnchorFormat
== 2)
1263 FT_Outline
*outline
;
1264 int ap
= anchor
->f
.f1
.AnchorPoint
;
1266 FT_Load_Glyph (ft_face
, (FT_UInt
) code
, FT_LOAD_MONOCHROME
);
1267 outline
= &ft_face
->glyph
->outline
;
1268 if (ap
< outline
->n_points
)
1270 *x
= outline
->points
[ap
].x
<< 6;
1271 *y
= outline
->points
[ap
].y
<< 6;
1274 else if (anchor
->AnchorFormat
== 3)
1276 if (anchor
->f
.f2
.XDeviceTable
.offset
)
1277 *x
+= DEVICE_DELTA (anchor
->f
.f2
.XDeviceTable
, x_ppem
);
1278 if (anchor
->f
.f2
.YDeviceTable
.offset
)
1279 *y
+= DEVICE_DELTA (anchor
->f
.f2
.YDeviceTable
, y_ppem
);
1283 static OTF_GlyphString otf_gstring
;
1286 ftfont_drive_otf (font
, spec
, in
, from
, to
, out
, adjustment
)
1289 MFLTGlyphString
*in
;
1291 MFLTGlyphString
*out
;
1292 MFLTGlyphAdjustment
*adjustment
;
1294 struct MFLTFontFT
*flt_font_ft
= (struct MFLTFontFT
*) font
;
1295 FT_Face ft_face
= flt_font_ft
->ft_face
;
1296 OTF
*otf
= flt_font_ft
->otf
;
1297 int len
= to
- from
;
1300 char script
[5], *langsys
= NULL
;
1301 char *gsub_features
= NULL
, *gpos_features
= NULL
;
1305 OTF_tag_name (spec
->script
, script
);
1308 langsys
= alloca (5);
1309 OTF_tag_name (spec
->langsys
, langsys
);
1311 for (i
= 0; i
< 2; i
++)
1315 if (spec
->features
[i
] && spec
->features
[i
][1] != 0xFFFFFFFF)
1317 for (j
= 0; spec
->features
[i
][j
]; j
++);
1319 p
= gsub_features
= alloca (6 * j
);
1321 p
= gpos_features
= alloca (6 * j
);
1322 for (j
= 0; spec
->features
[i
][j
]; j
++)
1324 if (spec
->features
[i
][j
] == 0xFFFFFFFF)
1325 *p
++ = '*', *p
++ = ',';
1328 OTF_tag_name (spec
->features
[i
][j
], p
);
1337 if (otf_gstring
.size
== 0)
1339 otf_gstring
.glyphs
= (OTF_Glyph
*) malloc (sizeof (OTF_Glyph
) * len
);
1340 otf_gstring
.size
= len
;
1342 else if (otf_gstring
.size
< len
)
1344 otf_gstring
.glyphs
= (OTF_Glyph
*) realloc (otf_gstring
.glyphs
,
1345 sizeof (OTF_Glyph
) * len
);
1346 otf_gstring
.size
= len
;
1348 otf_gstring
.used
= len
;
1349 memset (otf_gstring
.glyphs
, 0, sizeof (OTF_Glyph
) * len
);
1350 for (i
= 0; i
< len
; i
++)
1352 otf_gstring
.glyphs
[i
].c
= in
->glyphs
[from
+ i
].c
;
1353 otf_gstring
.glyphs
[i
].glyph_id
= in
->glyphs
[from
+ i
].code
;
1356 OTF_drive_gdef (otf
, &otf_gstring
);
1361 if (OTF_drive_gsub (otf
, &otf_gstring
, script
, langsys
, gsub_features
)
1364 if (out
->allocated
< out
->used
+ otf_gstring
.used
)
1366 for (i
= 0, otfg
= otf_gstring
.glyphs
; i
< otf_gstring
.used
; )
1369 int min_from
, max_to
;
1372 g
= out
->glyphs
+ out
->used
;
1373 *g
= in
->glyphs
[from
+ otfg
->f
.index
.from
];
1374 if (g
->code
!= otfg
->glyph_id
)
1377 g
->code
= otfg
->glyph_id
;
1383 if (otfg
->f
.index
.from
< otfg
->f
.index
.to
)
1385 /* OTFG substitutes multiple glyphs in IN. */
1386 for (j
= from
+ otfg
->f
.index
.from
+ 1;
1387 j
<= from
+ otfg
->f
.index
.to
; j
++)
1389 if (min_from
> in
->glyphs
[j
].from
)
1390 min_from
= in
->glyphs
[j
].from
;
1391 if (max_to
< in
->glyphs
[j
].to
)
1392 max_to
= in
->glyphs
[j
].to
;
1397 for (i
++, otfg
++; (i
< otf_gstring
.used
1398 && otfg
->f
.index
.from
== otfg
[-1].f
.index
.from
);
1401 g
= out
->glyphs
+ out
->used
;
1402 *g
= in
->glyphs
[from
+ otfg
->f
.index
.to
];
1403 if (g
->code
!= otfg
->glyph_id
)
1406 g
->code
= otfg
->glyph_id
;
1415 if (out
->allocated
< out
->used
+ len
)
1417 for (i
= 0; i
< len
; i
++)
1418 out
->glyphs
[out
->used
++] = in
->glyphs
[from
+ i
];
1423 MFLTGlyph
*base
= NULL
, *mark
= NULL
, *g
;
1424 int x_ppem
, y_ppem
, x_scale
, y_scale
;
1426 if (OTF_drive_gpos (otf
, &otf_gstring
, script
, langsys
, gpos_features
)
1430 x_ppem
= ft_face
->size
->metrics
.x_ppem
;
1431 y_ppem
= ft_face
->size
->metrics
.y_ppem
;
1432 x_scale
= ft_face
->size
->metrics
.x_scale
;
1433 y_scale
= ft_face
->size
->metrics
.y_scale
;
1435 for (i
= 0, otfg
= otf_gstring
.glyphs
, g
= out
->glyphs
+ gidx
;
1436 i
< otf_gstring
.used
; i
++, otfg
++, g
++)
1440 if (! otfg
->glyph_id
)
1442 switch (otfg
->positioning_type
)
1446 case 1: /* Single */
1449 int format
= otfg
->f
.f1
.format
;
1451 if (format
& OTF_XPlacement
)
1453 = otfg
->f
.f1
.value
->XPlacement
* x_scale
/ 0x10000;
1454 if (format
& OTF_XPlaDevice
)
1456 += DEVICE_DELTA (otfg
->f
.f1
.value
->XPlaDevice
, x_ppem
);
1457 if (format
& OTF_YPlacement
)
1459 = - (otfg
->f
.f1
.value
->YPlacement
* y_scale
/ 0x10000);
1460 if (format
& OTF_YPlaDevice
)
1462 -= DEVICE_DELTA (otfg
->f
.f1
.value
->YPlaDevice
, y_ppem
);
1463 if (format
& OTF_XAdvance
)
1465 += otfg
->f
.f1
.value
->XAdvance
* x_scale
/ 0x10000;
1466 if (format
& OTF_XAdvDevice
)
1468 += DEVICE_DELTA (otfg
->f
.f1
.value
->XAdvDevice
, x_ppem
);
1469 if (format
& OTF_YAdvance
)
1471 += otfg
->f
.f1
.value
->YAdvance
* y_scale
/ 0x10000;
1472 if (format
& OTF_YAdvDevice
)
1474 += DEVICE_DELTA (otfg
->f
.f1
.value
->YAdvDevice
, y_ppem
);
1475 adjustment
[i
].set
= 1;
1478 case 3: /* Cursive */
1479 /* Not yet supported. */
1481 case 4: /* Mark-to-Base */
1482 case 5: /* Mark-to-Ligature */
1486 goto label_adjust_anchor
;
1487 default: /* i.e. case 6 Mark-to-Mark */
1492 label_adjust_anchor
:
1494 int base_x
, base_y
, mark_x
, mark_y
;
1495 int this_from
, this_to
;
1497 base_x
= otfg
->f
.f4
.base_anchor
->XCoordinate
* x_scale
/ 0x10000;
1498 base_y
= otfg
->f
.f4
.base_anchor
->YCoordinate
* y_scale
/ 0x10000;
1499 mark_x
= otfg
->f
.f4
.mark_anchor
->XCoordinate
* x_scale
/ 0x10000;
1500 mark_y
= otfg
->f
.f4
.mark_anchor
->YCoordinate
* y_scale
/ 0x10000;;
1502 if (otfg
->f
.f4
.base_anchor
->AnchorFormat
!= 1)
1503 adjust_anchor (ft_face
, otfg
->f
.f4
.base_anchor
,
1504 prev
->code
, x_ppem
, y_ppem
, &base_x
, &base_y
);
1505 if (otfg
->f
.f4
.mark_anchor
->AnchorFormat
!= 1)
1506 adjust_anchor (ft_face
, otfg
->f
.f4
.mark_anchor
, g
->code
,
1507 x_ppem
, y_ppem
, &mark_x
, &mark_y
);
1508 adjustment
[i
].xoff
= (base_x
- mark_x
);
1509 adjustment
[i
].yoff
= - (base_y
- mark_y
);
1510 adjustment
[i
].back
= (g
- prev
);
1511 adjustment
[i
].xadv
= 0;
1512 adjustment
[i
].advance_is_absolute
= 1;
1513 adjustment
[i
].set
= 1;
1514 this_from
= g
->from
;
1516 for (j
= 0; prev
+ j
< g
; j
++)
1518 if (this_from
> prev
[j
].from
)
1519 this_from
= prev
[j
].from
;
1520 if (this_to
< prev
[j
].to
)
1521 this_to
= prev
[j
].to
;
1523 for (; prev
<= g
; prev
++)
1525 prev
->from
= this_from
;
1530 if (otfg
->GlyphClass
== OTF_GlyphClass0
)
1532 else if (otfg
->GlyphClass
== OTF_GlyphClassMark
)
1541 if (out
->allocated
< out
->used
+ len
)
1543 font
->get_metrics (font
, in
, from
, to
);
1544 memcpy (out
->glyphs
+ out
->used
, in
->glyphs
+ from
,
1545 sizeof (MFLTGlyph
) * len
);
1550 static MFLTGlyphString gstring
;
1552 static int m17n_flt_initialized
;
1554 extern Lisp_Object QCfamily
;
1557 ftfont_shape_by_flt (lgstring
, font
, ft_face
, otf
)
1558 Lisp_Object lgstring
;
1563 EMACS_UINT len
= LGSTRING_LENGTH (lgstring
);
1565 struct MFLTFontFT flt_font_ft
;
1567 if (! m17n_flt_initialized
)
1570 m17n_flt_initialized
= 1;
1573 for (i
= 0; i
< len
; i
++)
1574 if (NILP (LGSTRING_GLYPH (lgstring
, i
)))
1578 if (gstring
.allocated
== 0)
1580 gstring
.allocated
= len
* 2;
1581 gstring
.glyph_size
= sizeof (MFLTGlyph
);
1582 gstring
.glyphs
= malloc (sizeof (MFLTGlyph
) * gstring
.allocated
);
1584 else if (gstring
.allocated
< len
* 2)
1586 gstring
.allocated
= len
* 2;
1587 gstring
.glyphs
= realloc (gstring
.glyphs
,
1588 sizeof (MFLTGlyph
) * gstring
.allocated
);
1590 for (i
= 0; i
< len
; i
++)
1591 gstring
.glyphs
[i
].c
= LGLYPH_CHAR (LGSTRING_GLYPH (lgstring
, i
));
1596 Lisp_Object family
= Ffont_get (LGSTRING_FONT (lgstring
), QCfamily
);
1599 flt_font_ft
.flt_font
.family
= Mnil
;
1601 flt_font_ft
.flt_font
.family
1602 = msymbol ((char *) SDATA (SYMBOL_NAME (family
)));
1604 flt_font_ft
.flt_font
.x_ppem
= ft_face
->size
->metrics
.x_ppem
;
1605 flt_font_ft
.flt_font
.y_ppem
= ft_face
->size
->metrics
.y_ppem
;
1606 flt_font_ft
.flt_font
.get_glyph_id
= ftfont_get_glyph_id
;
1607 flt_font_ft
.flt_font
.get_metrics
= ftfont_get_metrics
;
1608 flt_font_ft
.flt_font
.check_otf
= ftfont_check_otf
;
1609 flt_font_ft
.flt_font
.drive_otf
= ftfont_drive_otf
;
1610 flt_font_ft
.flt_font
.internal
= NULL
;
1611 flt_font_ft
.font
= font
;
1612 flt_font_ft
.ft_face
= ft_face
;
1613 flt_font_ft
.otf
= otf
;
1614 for (i
= 0; i
< 3; i
++)
1616 int result
= mflt_run (&gstring
, 0, len
, &flt_font_ft
.flt_font
, NULL
);
1619 gstring
.allocated
+= gstring
.allocated
;
1620 gstring
.glyphs
= realloc (gstring
.glyphs
,
1621 sizeof (MFLTGlyph
) * gstring
.allocated
);
1623 if (gstring
.used
> LGSTRING_LENGTH (lgstring
))
1625 for (i
= 0; i
< gstring
.used
; i
++)
1627 MFLTGlyph
*g
= gstring
.glyphs
+ i
;
1629 g
->from
= LGLYPH_FROM (LGSTRING_GLYPH (lgstring
, g
->from
));
1630 g
->to
= LGLYPH_TO (LGSTRING_GLYPH (lgstring
, g
->to
));
1633 for (i
= 0; i
< gstring
.used
; i
++)
1635 Lisp_Object lglyph
= LGSTRING_GLYPH (lgstring
, i
);
1636 MFLTGlyph
*g
= gstring
.glyphs
+ i
;
1640 lglyph
= Fmake_vector (make_number (LGLYPH_SIZE
), Qnil
);
1641 LGSTRING_SET_GLYPH (lgstring
, i
, lglyph
);
1643 LGLYPH_SET_FROM (lglyph
, g
->from
);
1644 LGLYPH_SET_TO (lglyph
, g
->to
);
1645 LGLYPH_SET_CHAR (lglyph
, g
->c
);
1646 LGLYPH_SET_CODE (lglyph
, g
->code
);
1647 LGLYPH_SET_WIDTH (lglyph
, g
->xadv
>> 6);
1648 LGLYPH_SET_LBEARING (lglyph
, g
->lbearing
>> 6);
1649 LGLYPH_SET_RBEARING (lglyph
, g
->rbearing
>> 6);
1650 LGLYPH_SET_ASCENT (lglyph
, g
->ascent
>> 6);
1651 LGLYPH_SET_DESCENT (lglyph
, g
->descent
>> 6);
1656 vec
= Fmake_vector (make_number (3), Qnil
);
1657 ASET (vec
, 0, make_number (g
->xoff
>> 6));
1658 ASET (vec
, 1, make_number (g
->yoff
>> 6));
1659 ASET (vec
, 2, make_number (g
->xadv
>> 6));
1660 LGLYPH_SET_ADJUSTMENT (lglyph
, vec
);
1663 return make_number (i
);
1667 ftfont_shape (lgstring
)
1668 Lisp_Object lgstring
;
1671 struct ftfont_info
*ftfont_info
;
1673 CHECK_FONT_GET_OBJECT (LGSTRING_FONT (lgstring
), font
);
1674 ftfont_info
= (struct ftfont_info
*) font
;
1675 if (! ftfont_info
->maybe_otf
)
1676 return make_number (0);
1677 if (! ftfont_info
->otf
)
1679 OTF
*otf
= OTF_open_ft_face (ftfont_info
->ft_size
->face
);
1681 if (! otf
|| OTF_get_table (otf
, "head") < 0)
1685 ftfont_info
->maybe_otf
= 0;
1686 return make_number (0);
1689 ftfont_info
->otf
= otf
;
1692 return ftfont_shape_by_flt (lgstring
, font
, ftfont_info
->ft_size
->face
,
1696 #endif /* HAVE_M17N_FLT */
1697 #endif /* HAVE_LIBOTF */
1700 ftfont_font_format (FcPattern
*pattern
)
1704 #ifdef FC_FONTFORMAT
1705 if (FcPatternGetString (pattern
, FC_FONTFORMAT
, 0, &str
) != FcResultMatch
)
1707 if (strcmp ((char *) str
, "TrueType") == 0)
1708 return intern ("truetype");
1709 if (strcmp ((char *) str
, "Type 1") == 0)
1710 return intern ("type1");
1711 if (strcmp ((char *) str
, "PCF") == 0)
1712 return intern ("pcf");
1713 if (strcmp ((char *) str
, "BDF") == 0)
1714 return intern ("bdf");
1715 #else /* not FC_FONTFORMAT */
1716 if (FcPatternGetString (pattern
, FC_FILE
, 0, &str
) != FcResultMatch
)
1718 if (strcasestr ((char *) str
, ".ttf") == 0)
1719 return intern ("truetype");
1720 if (strcasestr ((char *) str
, "pfb") == 0)
1721 return intern ("type1");
1722 if (strcasestr ((char *) str
, "pcf") == 0)
1723 return intern ("pcf");
1724 if (strcasestr ((char *) str
, "bdf") == 0)
1725 return intern ("bdf");
1726 #endif /* not FC_FONTFORMAT */
1727 return intern ("unknown");
1734 DEFSYM (Qfreetype
, "freetype");
1735 DEFSYM (Qmonospace
, "monospace");
1736 DEFSYM (Qsans_serif
, "sans-serif");
1737 DEFSYM (Qserif
, "serif");
1738 DEFSYM (Qmono
, "mono");
1739 DEFSYM (Qsans
, "sans");
1740 DEFSYM (Qsans__serif
, "sans serif");
1742 staticpro (&freetype_font_cache
);
1743 freetype_font_cache
= Fcons (Qt
, Qnil
);
1745 staticpro (&ftfont_generic_family_list
);
1746 ftfont_generic_family_list
1747 = Fcons (Fcons (Qmonospace
, Qt
),
1748 Fcons (Fcons (Qsans_serif
, Qt
),
1749 Fcons (Fcons (Qsans
, Qt
), Qnil
)));
1751 staticpro (&ft_face_cache
);
1752 ft_face_cache
= Qnil
;
1754 ftfont_driver
.type
= Qfreetype
;
1755 register_font_driver (&ftfont_driver
, NULL
);
1758 /* arch-tag: 7cfa432c-33a6-4988-83d2-a82ed8604aca
1759 (do not change this comment) */