]> git.donarmstrong.com Git - lilypond.git/blob - lily/open-type-font-scheme.cc
Merge branch 'master' of /home/jcharles/GIT/Lily/. into translation
[lilypond.git] / lily / open-type-font-scheme.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2004--2015 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
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.
10
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.
15
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/>.
18 */
19
20 #include "international.hh"
21 #include "modified-font-metric.hh"
22 #include "open-type-font.hh"
23 #include "freetype.hh"
24
25 #ifdef FT_FONT_FORMATS_H
26 /* FreeType 2.6+ */
27 #include FT_FONT_FORMATS_H
28 #else
29 /* FreeType 2.5.5 and earlier */
30 #include FT_XFREE86_H
31 #define FT_Get_Font_Format FT_Get_X11_Font_Format
32 #endif
33
34 LY_DEFINE (ly_font_sub_fonts, "ly:font-sub-fonts", 1, 0, 0,
35            (SCM font),
36            "Given the font metric @var{font} of an OpenType font, return the"
37            " names of the subfonts within @var{font}.")
38 {
39   LY_ASSERT_SMOB (Font_metric, font, 1);
40
41   Font_metric *fm = unsmob<Font_metric> (font);
42   return fm->sub_fonts ();
43 }
44
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).")
49 {
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);
55
56   SCM_ASSERT_TYPE (otf, font, SCM_ARG1, __FUNCTION__, "OpenType font");
57   LY_ASSERT_TYPE (scm_is_string, glyph, 2);
58
59   SCM sym = scm_string_to_symbol (glyph);
60   return scm_hashq_ref (otf->get_char_table (), sym, SCM_EOL);
61 }
62
63 LY_DEFINE (ly_otf_font_table_data, "ly:otf-font-table-data", 2, 0, 0,
64            (SCM font, SCM tag),
65            "Extract a table @var{tag} from @var{font}.  Return empty string"
66            " for non-existent @var{tag}.")
67 {
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);
73
74   SCM_ASSERT_TYPE (otf, font, SCM_ARG1, __FUNCTION__, "OpenType font");
75   LY_ASSERT_TYPE (scm_is_string, tag, 2);
76
77   char ctag [5] = "    ";
78
79   string tag_string = ly_scm2string (tag);
80   strncpy (ctag, tag_string.c_str (), tag_string.length ());
81
82   string tab = otf->get_otf_table (string (ctag));
83
84   return scm_from_latin1_stringn ((char const *) tab.data (), tab.length ());
85 }
86
87 LY_DEFINE (ly_otf_font_p, "ly:otf-font?", 1, 0, 0,
88            (SCM font),
89            "Is @var{font} an OpenType font?")
90 {
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);
96
97   return scm_from_bool (otf);
98 }
99
100 LY_DEFINE (ly_otf_glyph_count, "ly:otf-glyph-count", 1, 0, 0,
101            (SCM font),
102            "Return the number of glyphs in @var{font}.")
103 {
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);
109
110   SCM_ASSERT_TYPE (otf, font, SCM_ARG1, __FUNCTION__, "OpenType font");
111
112   return scm_from_int ((int) otf->count ());
113 }
114
115 LY_DEFINE (ly_otf_glyph_list, "ly:otf-glyph-list", 1, 0, 0,
116            (SCM font),
117            "Return a list of glyph names for @var{font}.")
118 {
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);
124
125   SCM_ASSERT_TYPE (otf, font, SCM_ARG1, __FUNCTION__, "OpenType font");
126
127   return otf->glyph_list ();
128 }
129
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.")
138 {
139   LY_ASSERT_TYPE (scm_is_string, font_file_name, 1);
140
141   int i = 0;
142   if (!SCM_UNBNDP (idx))
143     {
144       LY_ASSERT_TYPE (scm_is_integer, idx, 2);
145       i = scm_to_int (idx);
146       if (i < 0)
147         {
148           warning (_ ("font index must be non-negative, using index 0"));
149           i = 0;
150         }
151     }
152
153   string file_name = ly_scm2string (font_file_name);
154
155   FT_Face face;
156   /* check whether font index is valid */
157   if (i > 0)
158     {
159       face = open_ft_face (file_name, -1);
160       if (i >= face->num_faces)
161         {
162           warning (_f ("font index %d too large for font `%s', using index 0",
163                        i, file_name.c_str ()));
164           i = 0;
165         }
166       FT_Done_Face (face);
167     }
168
169   face = open_ft_face (file_name, i);
170   SCM asscm = scm_from_ascii_symbol (FT_Get_Font_Format (face));
171   FT_Done_Face (face);
172
173   return asscm;
174 }