2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 2004--2015 Han-Wen Nienhuys <hanwen@xs4all.nl>
6 LilyPond is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 LilyPond is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
20 #include "international.hh"
21 #include "modified-font-metric.hh"
22 #include "open-type-font.hh"
23 #include "freetype.hh"
25 #ifdef FT_FONT_FORMATS_H
27 #include FT_FONT_FORMATS_H
29 /* FreeType 2.5.5 and earlier */
31 #define FT_Get_Font_Format FT_Get_X11_Font_Format
34 LY_DEFINE (ly_font_sub_fonts, "ly:font-sub-fonts", 1, 0, 0,
36 "Given the font metric @var{font} of an OpenType font, return the"
37 " names of the subfonts within @var{font}.")
39 LY_ASSERT_SMOB (Font_metric, font, 1);
41 Font_metric *fm = unsmob<Font_metric> (font);
42 return fm->sub_fonts ();
45 LY_DEFINE (ly_otf_font_glyph_info, "ly:otf-font-glyph-info", 2, 0, 0,
46 (SCM font, SCM glyph),
47 "Given the font metric @var{font} of an OpenType font, return the"
48 " information about named glyph @var{glyph} (a string).")
50 Modified_font_metric *fm
51 = unsmob<Modified_font_metric> (font);
52 Open_type_font *otf = fm
53 ? dynamic_cast<Open_type_font *> (fm->original_font ())
54 : unsmob<Open_type_font> (font);
56 SCM_ASSERT_TYPE (otf, font, SCM_ARG1, __FUNCTION__, "OpenType font");
57 LY_ASSERT_TYPE (scm_is_string, glyph, 2);
59 SCM sym = scm_string_to_symbol (glyph);
60 return scm_hashq_ref (otf->get_char_table (), sym, SCM_EOL);
63 LY_DEFINE (ly_otf_font_table_data, "ly:otf-font-table-data", 2, 0, 0,
65 "Extract a table @var{tag} from @var{font}. Return empty string"
66 " for non-existent @var{tag}.")
68 Modified_font_metric *fm
69 = unsmob<Modified_font_metric> (font);
70 Open_type_font *otf = fm
71 ? dynamic_cast<Open_type_font *> (fm->original_font ())
72 : unsmob<Open_type_font> (font);
74 SCM_ASSERT_TYPE (otf, font, SCM_ARG1, __FUNCTION__, "OpenType font");
75 LY_ASSERT_TYPE (scm_is_string, tag, 2);
79 string tag_string = ly_scm2string (tag);
80 strncpy (ctag, tag_string.c_str (), tag_string.length ());
82 string tab = otf->get_otf_table (string (ctag));
84 return scm_from_latin1_stringn ((char const *) tab.data (), tab.length ());
87 LY_DEFINE (ly_otf_font_p, "ly:otf-font?", 1, 0, 0,
89 "Is @var{font} an OpenType font?")
91 Modified_font_metric *fm
92 = unsmob<Modified_font_metric> (font);
93 Open_type_font *otf = fm
94 ? dynamic_cast<Open_type_font *> (fm->original_font ())
95 : unsmob<Open_type_font> (font);
97 return scm_from_bool (otf);
100 LY_DEFINE (ly_otf_glyph_count, "ly:otf-glyph-count", 1, 0, 0,
102 "Return the number of glyphs in @var{font}.")
104 Modified_font_metric *fm
105 = unsmob<Modified_font_metric> (font);
106 Open_type_font *otf = fm
107 ? dynamic_cast<Open_type_font *> (fm->original_font ())
108 : unsmob<Open_type_font> (font);
110 SCM_ASSERT_TYPE (otf, font, SCM_ARG1, __FUNCTION__, "OpenType font");
112 return scm_from_int ((int) otf->count ());
115 LY_DEFINE (ly_otf_glyph_list, "ly:otf-glyph-list", 1, 0, 0,
117 "Return a list of glyph names for @var{font}.")
119 Modified_font_metric *fm
120 = unsmob<Modified_font_metric> (font);
121 Open_type_font *otf = fm
122 ? dynamic_cast<Open_type_font *> (fm->original_font ())
123 : unsmob<Open_type_font> (font);
125 SCM_ASSERT_TYPE (otf, font, SCM_ARG1, __FUNCTION__, "OpenType font");
127 return otf->glyph_list ();
130 LY_DEFINE (ly_get_font_format, "ly:get-font-format",
131 1, 1, 0, (SCM font_file_name, SCM idx),
132 "Get the font format for @var{font_file_name},"
133 " returning it as a symbol. The optional"
134 " @var{idx} argument is useful for TrueType Collections (TTC) and"
135 " OpenType/CFF collections (OTC) only;"
136 " it specifies the font index within the TTC/OTC."
137 " The default value of @var{idx} is@tie{}0.")
139 LY_ASSERT_TYPE (scm_is_string, font_file_name, 1);
142 if (!SCM_UNBNDP (idx))
144 LY_ASSERT_TYPE (scm_is_integer, idx, 2);
145 i = scm_to_int (idx);
148 warning (_ ("font index must be non-negative, using index 0"));
153 string file_name = ly_scm2string (font_file_name);
156 /* check whether font index is valid */
159 face = open_ft_face (file_name, -1);
160 if (i >= face->num_faces)
162 warning (_f ("font index %d too large for font `%s', using index 0",
163 i, file_name.c_str ()));
169 face = open_ft_face (file_name, i);
170 SCM asscm = scm_from_ascii_symbol (FT_Get_Font_Format (face));