]> git.donarmstrong.com Git - lilypond.git/blob - lily/font-metric-scheme.cc
Run grand-replace for 2010.
[lilypond.git] / lily / font-metric-scheme.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2005--2010 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 Aybabtu fonts, corresponding"
35            " to the font encodings @code{fetaMusic} and @code{fetaBraces},"
36            " respectively.")
37 {
38   Font_metric *fm = unsmob_metrics (font);
39   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_get_glyph, "ly:get-glyph",
49            2, 0, 0,
50            (SCM font, SCM index),
51            "Retrieve a stencil for the glyph numbered @var{index}"
52            " in @var{font}.\n"
53            "\n"
54            "Note that this command can only be used to access glyphs from"
55            " fonts loaded with @code{ly:system-font-load}; currently, this"
56            " means either the Emmentaler or Aybabtu fonts, corresponding"
57            " to the font encodings @code{fetaMusic} and @code{fetaBraces},"
58            " respectively.")
59 {
60   Font_metric *fm = unsmob_metrics (font);
61   LY_ASSERT_SMOB (Font_metric, font, 1);
62   LY_ASSERT_TYPE (scm_is_number, index,2);
63
64   return fm->get_ascii_char_stencil (scm_to_int (index)).smobbed_copy ();
65 }
66
67 LY_DEFINE (ly_font_glyph_name_to_index, "ly:font-glyph-name-to-index",
68            2, 0, 0,
69            (SCM font, SCM name),
70            "Return the index for @var{name} in @var{font}.\n"
71            "\n"
72            "Note that this command can only be used to access glyphs from"
73            " fonts loaded with @code{ly:system-font-load}; currently, this"
74            " means either the Emmentaler or Aybabtu fonts, corresponding"
75            " to the font encodings @code{fetaMusic} and @code{fetaBraces},"
76            " respectively.")
77 {
78   Font_metric *fm = unsmob_metrics (font);
79   LY_ASSERT_SMOB (Font_metric, font, 1);
80   LY_ASSERT_TYPE (scm_is_string, name, 2);
81
82   return scm_from_int (fm->name_to_index (ly_scm2string (name)));
83 }
84
85 LY_DEFINE (ly_font_index_to_charcode, "ly:font-index-to-charcode",
86            2, 0, 0,
87            (SCM font, SCM index),
88            "Return the character code for @var{index} in @var{font}.\n"
89            "\n"
90            "Note that this command can only be used to access glyphs from"
91            " fonts loaded with @code{ly:system-font-load}; currently, this"
92            " means either the Emmentaler or Aybabtu fonts, corresponding"
93            " to the font encodings @code{fetaMusic} and @code{fetaBraces},"
94            " respectively.")
95 {
96   Font_metric *fm = unsmob_metrics (font);
97   LY_ASSERT_SMOB (Font_metric, font, 1);
98   LY_ASSERT_TYPE (scm_is_integer, index, 2);
99
100   return scm_from_unsigned_integer (fm->index_to_charcode (scm_to_int (index)));
101 }
102
103 LY_DEFINE (ly_font_glyph_name_to_charcode, "ly:font-glyph-name-to-charcode",
104            2, 0, 0,
105            (SCM font, SCM name),
106            "Return the character code for glyph @var{name} in @var{font}.\n"
107            "\n"
108            "Note that this command can only be used to access glyphs from"
109            " fonts loaded with @code{ly:system-font-load}; currently, this"
110            " means either the Emmentaler or Aybabtu fonts, corresponding"
111            " to the font encodings @code{fetaMusic} and @code{fetaBraces},"
112            " respectively.")
113 {
114   Font_metric *fm = unsmob_metrics (font);
115   LY_ASSERT_SMOB (Font_metric, font, 1);
116   LY_ASSERT_TYPE (scm_is_string, name, 2);
117
118   return scm_from_unsigned_integer (fm->index_to_charcode (fm->name_to_index (ly_scm2string (name))));
119 }
120
121 LY_DEFINE (ly_text_dimension, "ly:text-dimension",
122            2, 0, 0,
123            (SCM font, SCM text),
124            "Given the font metric in @var{font} and the string @var{text},"
125            " compute the extents of that text in that font.  The return"
126            " value is a pair of number-pairs.")
127 {
128   Box b;
129   Modified_font_metric *fm = dynamic_cast<Modified_font_metric *>
130     (unsmob_metrics (font));
131
132   LY_ASSERT_SMOB (Font_metric, font, 1);
133   LY_ASSERT_TYPE (scm_is_string, text, 2);
134   Stencil stc (fm->text_stencil (ly_scm2string (text), false));
135   return scm_cons (ly_interval2scm (stc.extent (X_AXIS)),
136                    ly_interval2scm (stc.extent (Y_AXIS)));
137 }
138
139
140 /*
141   TODO: when are non string retvals allowed?
142  */
143 LY_DEFINE (ly_font_file_name, "ly:font-file-name",
144            1, 0, 0,
145            (SCM font),
146            "Given the font metric @var{font},"
147            " return the corresponding file name.")
148 {
149   LY_ASSERT_SMOB (Font_metric, font, 1);
150
151   Font_metric *fm = unsmob_metrics (font);
152   SCM name = fm->font_file_name ();
153
154   return name;
155 }
156
157 LY_DEFINE (ly_font_name, "ly:font-name",
158            1, 0, 0,
159            (SCM font),
160            "Given the font metric @var{font},"
161            " return the corresponding name.")
162 {
163   LY_ASSERT_SMOB (Font_metric, font, 1);
164   Font_metric *fm = unsmob_metrics (font);
165
166   return ly_string2scm (fm->font_name ());
167 }
168
169 LY_DEFINE (ly_font_magnification, "ly:font-magnification", 1, 0, 0,
170            (SCM font),
171            "Given the font metric @var{font}, return the"
172            " magnification, relative to the current output-scale.")
173 {
174   LY_ASSERT_SMOB (Font_metric, font, 1);
175
176   Font_metric *fm = unsmob_metrics (font);
177   return scm_cdr (fm->description_);
178 }
179
180 LY_DEFINE (ly_font_design_size, "ly:font-design-size", 1, 0, 0,
181            (SCM font),
182            "Given the font metric @var{font}, return the"
183            " design size, relative to the current output-scale.")
184 {
185   LY_ASSERT_SMOB (Font_metric, font, 1);
186
187   Font_metric *fm = unsmob_metrics (font);
188   return scm_from_double (fm->design_size ());
189 }
190