1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3 
4   Copyright (C) 2005--2021 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 "font-metric.hh"
21 
22 #include "warn.hh"
23 #include "stencil.hh"
24 #include "modified-font-metric.hh"
25 
26 LY_DEFINE (ly_font_get_glyph, "ly:font-get-glyph",
27            2, 0, 0,
28            (SCM font, SCM name),
29            "Return a stencil from @var{font} for the glyph named @var{name}."
30            "  If the glyph is not available, return an empty stencil.\n"
31            "\n"
32            "Note that this command can only be used to access glyphs from"
33            " fonts loaded with @code{ly:system-font-load}; currently, this"
34            " means either the Emmentaler or Emmentaler-Brace "
35            " fonts, corresponding"
36            " to the font encodings @code{fetaMusic} and @code{fetaBraces},"
37            " respectively.")
38 {
39   auto *const fm = LY_ASSERT_SMOB (Font_metric, font, 1);
40   LY_ASSERT_TYPE (scm_is_string, name, 2);
41 
42   Stencil m = fm->find_by_name (ly_scm2string (name));
43 
44   /* TODO: make optional argument for default if not found.  */
45   return m.smobbed_copy ();
46 }
47 
48 LY_DEFINE (ly_font_glyph_name_to_index, "ly:font-glyph-name-to-index",
49            2, 0, 0,
50            (SCM font, SCM name),
51            "Return the index for @var{name} in @var{font}.\n"
52            "\n"
53            "Note that this command can only be used to access glyphs from"
54            " fonts loaded with @code{ly:system-font-load}; currently, this"
55            " means either the Emmentaler or Emmentaler-Brace fonts, corresponding"
56            " to the font encodings @code{fetaMusic} and @code{fetaBraces},"
57            " respectively.")
58 {
59   auto *const fm = LY_ASSERT_SMOB (Font_metric, font, 1);
60   LY_ASSERT_TYPE (scm_is_string, name, 2);
61 
62   size_t glyph_index = fm->name_to_index (ly_scm2string (name));
63   if (glyph_index != GLYPH_INDEX_INVALID)
64     return to_scm (glyph_index);
65   else
66     return to_scm (-1);
67 }
68 
69 LY_DEFINE (ly_font_index_to_charcode, "ly:font-index-to-charcode",
70            2, 0, 0,
71            (SCM font, SCM index),
72            "Return the character code for @var{index} in @var{font}.\n"
73            "\n"
74            "Note that this command can only be used to access glyphs from"
75            " fonts loaded with @code{ly:system-font-load}; currently, this"
76            " means either the Emmentaler or Emmentaler-Brace fonts, corresponding"
77            " to the font encodings @code{fetaMusic} and @code{fetaBraces},"
78            " respectively.")
79 {
80   auto *const fm = LY_ASSERT_SMOB (Font_metric, font, 1);
81   LY_ASSERT_TYPE (scm_is_integer, index, 2);
82 
83   int i = scm_to_int (index);
84   size_t glyph_index ((i >= 0) ? i : GLYPH_INDEX_INVALID);
85   size_t charcode = fm->index_to_charcode (glyph_index);
86   return to_scm (charcode);
87 }
88 
89 LY_DEFINE (ly_font_glyph_name_to_charcode, "ly:font-glyph-name-to-charcode",
90            2, 0, 0,
91            (SCM font, SCM name),
92            "Return the character code for glyph @var{name} in @var{font}.\n"
93            "\n"
94            "Note that this command can only be used to access glyphs from"
95            " fonts loaded with @code{ly:system-font-load}; currently, this"
96            " means either the Emmentaler or Emmentaler-Brace fonts, corresponding"
97            " to the font encodings @code{fetaMusic} and @code{fetaBraces},"
98            " respectively.")
99 {
100   auto *const fm = LY_ASSERT_SMOB (Font_metric, font, 1);
101   LY_ASSERT_TYPE (scm_is_string, name, 2);
102 
103   return to_scm (fm->index_to_charcode (fm->name_to_index (ly_scm2string (name))));
104 }
105 
106 /*
107   TODO: when are non string retvals allowed?
108  */
109 LY_DEFINE (ly_font_file_name, "ly:font-file-name",
110            1, 0, 0,
111            (SCM font),
112            "Given the font metric @var{font},"
113            " return the corresponding file name.")
114 {
115   auto *const fm = LY_ASSERT_SMOB (Font_metric, font, 1);
116 
117   SCM name = fm->font_file_name ();
118 
119   return name;
120 }
121 
122 LY_DEFINE (ly_font_name, "ly:font-name",
123            1, 0, 0,
124            (SCM font),
125            "Given the font metric @var{font},"
126            " return the corresponding name.")
127 {
128   auto *const fm = LY_ASSERT_SMOB (Font_metric, font, 1);
129 
130   return ly_string2scm (fm->font_name ());
131 }
132 
133 LY_DEFINE (ly_font_magnification, "ly:font-magnification", 1, 0, 0,
134            (SCM font),
135            "Given the font metric @var{font}, return the"
136            " magnification, relative to the current @code{output-scale}.")
137 {
138   auto *const fm = LY_ASSERT_SMOB (Font_metric, font, 1);
139 
140   return to_scm (fm->magnification ());
141 }
142 
143 LY_DEFINE (ly_font_design_size, "ly:font-design-size", 1, 0, 0,
144            (SCM font),
145            "Given the font metric @var{font}, return the"
146            " design size, relative to the current @code{output-scale}.")
147 {
148   auto *const fm = LY_ASSERT_SMOB (Font_metric, font, 1);
149 
150   return to_scm (fm->design_size ());
151 }
152 
153