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