]> git.donarmstrong.com Git - lilypond.git/blob - lily/font-metric-scheme.cc
Run `make grand-replace'.
[lilypond.git] / lily / font-metric-scheme.cc
1 /*
2   font-metric-scheme.cc -- implement Font_metric scheme bindings
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2005--2008 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "font-metric.hh"
10
11 #include "warn.hh"
12 #include "stencil.hh"
13 #include "modified-font-metric.hh"
14
15 LY_DEFINE (ly_font_get_glyph, "ly:font-get-glyph",
16            2, 0, 0,
17            (SCM font, SCM name),
18            "Return a stencil from @var{font} for the glyph named @var{name}."
19            "  If the glyph is not available, return an empty stencil.\n"
20            "\n"
21            "Note that this command can only be used to access glyphs from"
22            " fonts loaded with @code{ly:system-font-load}; currently, this"
23            " means either the Emmentaler or Aybabtu fonts, corresponding"
24            " to the font encodings @code{fetaMusic} and @code{fetaBraces},"
25            " respectively.")
26 {
27   Font_metric *fm = unsmob_metrics (font);
28   LY_ASSERT_SMOB (Font_metric, font, 1);
29   LY_ASSERT_TYPE (scm_is_string, name, 2);
30
31   Stencil m = fm->find_by_name (ly_scm2string (name));
32
33   /* TODO: make optional argument for default if not found.  */
34   return m.smobbed_copy ();
35 }
36
37 LY_DEFINE (ly_get_glyph, "ly:get-glyph",
38            2, 0, 0,
39            (SCM font, SCM index),
40            "Retrieve a stencil for the glyph numbered @var{index}"
41            " in @var{font}.\n"
42            "\n"
43            "Note that this command can only be used to access glyphs from"
44            " fonts loaded with @code{ly:system-font-load}; currently, this"
45            " means either the Emmentaler or Aybabtu fonts, corresponding"
46            " to the font encodings @code{fetaMusic} and @code{fetaBraces},"
47            " respectively.")
48 {
49   Font_metric *fm = unsmob_metrics (font);
50   LY_ASSERT_SMOB (Font_metric, font, 1);
51   LY_ASSERT_TYPE (scm_is_number, index,2);
52
53   return fm->get_ascii_char_stencil (scm_to_int (index)).smobbed_copy ();
54 }
55
56 LY_DEFINE (ly_font_glyph_name_to_index, "ly:font-glyph-name-to-index",
57            2, 0, 0,
58            (SCM font, SCM name),
59            "Return the index for @var{name} in @var{font}.\n"
60            "\n"
61            "Note that this command can only be used to access glyphs from"
62            " fonts loaded with @code{ly:system-font-load}; currently, this"
63            " means either the Emmentaler or Aybabtu fonts, corresponding"
64            " to the font encodings @code{fetaMusic} and @code{fetaBraces},"
65            " respectively.")
66 {
67   Font_metric *fm = unsmob_metrics (font);
68   LY_ASSERT_SMOB (Font_metric, font, 1);
69   LY_ASSERT_TYPE (scm_is_string, name, 2);
70
71   return scm_from_int (fm->name_to_index (ly_scm2string (name)));
72 }
73
74 LY_DEFINE (ly_font_index_to_charcode, "ly:font-index-to-charcode",
75            2, 0, 0,
76            (SCM font, SCM index),
77            "Return the character code for @var{index} in @var{font}.\n"
78            "\n"
79            "Note that this command can only be used to access glyphs from"
80            " fonts loaded with @code{ly:system-font-load}; currently, this"
81            " means either the Emmentaler or Aybabtu fonts, corresponding"
82            " to the font encodings @code{fetaMusic} and @code{fetaBraces},"
83            " respectively.")
84 {
85   Font_metric *fm = unsmob_metrics (font);
86   LY_ASSERT_SMOB (Font_metric, font, 1);
87   LY_ASSERT_TYPE (scm_is_integer, index, 2);
88
89   return scm_from_unsigned_integer (fm->index_to_charcode (scm_to_int (index)));
90 }
91
92 LY_DEFINE (ly_font_glyph_name_to_charcode, "ly:font-glyph-name-to-charcode",
93            2, 0, 0,
94            (SCM font, SCM name),
95            "Return the character code for glyph @var{name} in @var{font}.\n"
96            "\n"
97            "Note that this command can only be used to access glyphs from"
98            " fonts loaded with @code{ly:system-font-load}; currently, this"
99            " means either the Emmentaler or Aybabtu fonts, corresponding"
100            " to the font encodings @code{fetaMusic} and @code{fetaBraces},"
101            " respectively.")
102 {
103   Font_metric *fm = unsmob_metrics (font);
104   LY_ASSERT_SMOB (Font_metric, font, 1);
105   LY_ASSERT_TYPE (scm_is_string, name, 2);
106
107   return scm_from_unsigned_integer (fm->index_to_charcode (fm->name_to_index (ly_scm2string (name))));
108 }
109
110 LY_DEFINE (ly_text_dimension, "ly:text-dimension",
111            2, 0, 0,
112            (SCM font, SCM text),
113            "Given the font metric in @var{font} and the string @var{text},"
114            " compute the extents of that text in that font.  The return"
115            " value is a pair of number-pairs.")
116 {
117   Box b;
118   Modified_font_metric *fm = dynamic_cast<Modified_font_metric *>
119     (unsmob_metrics (font));
120
121   LY_ASSERT_SMOB (Font_metric, font, 1);
122   LY_ASSERT_TYPE (scm_is_string, text, 2);
123   Stencil stc (fm->text_stencil (ly_scm2string (text)));
124   return scm_cons (ly_interval2scm (stc.extent (X_AXIS)),
125                    ly_interval2scm (stc.extent (Y_AXIS)));
126 }
127
128
129 /*
130   TODO: when are non string retvals allowed?
131  */
132 LY_DEFINE (ly_font_file_name, "ly:font-file-name",
133            1, 0, 0,
134            (SCM font),
135            "Given the font metric @var{font},"
136            " return the corresponding file name.")
137 {
138   LY_ASSERT_SMOB (Font_metric, font, 1);
139
140   Font_metric *fm = unsmob_metrics (font);
141   SCM name = fm->font_file_name ();
142
143   return name;
144 }
145
146 LY_DEFINE (ly_font_name, "ly:font-name",
147            1, 0, 0,
148            (SCM font),
149            "Given the font metric @var{font},"
150            " return the corresponding name.")
151 {
152   LY_ASSERT_SMOB (Font_metric, font, 1);
153   Font_metric *fm = unsmob_metrics (font);
154
155   return ly_string2scm (fm->font_name ());
156 }
157
158 LY_DEFINE (ly_font_magnification, "ly:font-magnification", 1, 0, 0,
159            (SCM font),
160            "Given the font metric @var{font}, return the"
161            " magnification, relative to the current output-scale.")
162 {
163   LY_ASSERT_SMOB (Font_metric, font, 1);
164
165   Font_metric *fm = unsmob_metrics (font);
166   return scm_cdr (fm->description_);
167 }
168
169 LY_DEFINE (ly_font_design_size, "ly:font-design-size", 1, 0, 0,
170            (SCM font),
171            "Given the font metric @var{font}, return the"
172            " design size, 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_from_double (fm->design_size ());
178 }
179