Use scalar instead of embedded_scm for context mod overrides.
[lilypond/mpolesky.git] / lily / font-metric-scheme.cc
blobcf351b2628b013627e35810f4668abc28b7665a5
1 /*
2 font-metric-scheme.cc -- implement Font_metric scheme bindings
4 source file of the GNU LilyPond music typesetter
6 (c) 2005--2009 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
9 #include "font-metric.hh"
11 #include "warn.hh"
12 #include "stencil.hh"
13 #include "modified-font-metric.hh"
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.")
27 Font_metric *fm = unsmob_metrics (font);
28 LY_ASSERT_SMOB (Font_metric, font, 1);
29 LY_ASSERT_TYPE (scm_is_string, name, 2);
31 Stencil m = fm->find_by_name (ly_scm2string (name));
33 /* TODO: make optional argument for default if not found. */
34 return m.smobbed_copy ();
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.")
49 Font_metric *fm = unsmob_metrics (font);
50 LY_ASSERT_SMOB (Font_metric, font, 1);
51 LY_ASSERT_TYPE (scm_is_number, index,2);
53 return fm->get_ascii_char_stencil (scm_to_int (index)).smobbed_copy ();
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.")
67 Font_metric *fm = unsmob_metrics (font);
68 LY_ASSERT_SMOB (Font_metric, font, 1);
69 LY_ASSERT_TYPE (scm_is_string, name, 2);
71 return scm_from_int (fm->name_to_index (ly_scm2string (name)));
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.")
85 Font_metric *fm = unsmob_metrics (font);
86 LY_ASSERT_SMOB (Font_metric, font, 1);
87 LY_ASSERT_TYPE (scm_is_integer, index, 2);
89 return scm_from_unsigned_integer (fm->index_to_charcode (scm_to_int (index)));
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.")
103 Font_metric *fm = unsmob_metrics (font);
104 LY_ASSERT_SMOB (Font_metric, font, 1);
105 LY_ASSERT_TYPE (scm_is_string, name, 2);
107 return scm_from_unsigned_integer (fm->index_to_charcode (fm->name_to_index (ly_scm2string (name))));
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.")
117 Box b;
118 Modified_font_metric *fm = dynamic_cast<Modified_font_metric *>
119 (unsmob_metrics (font));
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), false));
124 return scm_cons (ly_interval2scm (stc.extent (X_AXIS)),
125 ly_interval2scm (stc.extent (Y_AXIS)));
130 TODO: when are non string retvals allowed?
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.")
138 LY_ASSERT_SMOB (Font_metric, font, 1);
140 Font_metric *fm = unsmob_metrics (font);
141 SCM name = fm->font_file_name ();
143 return name;
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.")
152 LY_ASSERT_SMOB (Font_metric, font, 1);
153 Font_metric *fm = unsmob_metrics (font);
155 return ly_string2scm (fm->font_name ());
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.")
163 LY_ASSERT_SMOB (Font_metric, font, 1);
165 Font_metric *fm = unsmob_metrics (font);
166 return scm_cdr (fm->description_);
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.")
174 LY_ASSERT_SMOB (Font_metric, font, 1);
176 Font_metric *fm = unsmob_metrics (font);
177 return scm_from_double (fm->design_size ());