2 font-metric-scheme.cc -- implement Font_metric scheme bindings
4 source file of the GNU LilyPond music typesetter
6 (c) 2005--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
11 #include "font-metric.hh"
12 #include "modified-font-metric.hh"
14 LY_DEFINE (ly_font_get_glyph, "ly:font-get-glyph",
17 "Return a Stencil from @var{font} for the glyph named @var{name}. "
18 "@var{font} must be available as an AFM file. If the glyph "
19 "is not available, return @code{#f}.")
21 Font_metric *fm = unsmob_metrics (font);
22 SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
23 SCM_ASSERT_TYPE (scm_is_string (name), name, SCM_ARG2, __FUNCTION__, "string");
25 Stencil m = fm->find_by_name (ly_scm2string (name));
27 /* TODO: make optional argument for default if not found. */
28 return m.smobbed_copy ();
31 LY_DEFINE (ly_get_glyph, "ly:get-glyph",
33 (SCM font, SCM index),
34 "Retrieve a Stencil for the glyph numbered @var{index} "
37 Font_metric *fm = unsmob_metrics (font);
38 SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
39 SCM_ASSERT_TYPE (scm_is_number (index), index, SCM_ARG2, __FUNCTION__, "number");
41 return fm->get_ascii_char_stencil (scm_to_int (index)).smobbed_copy ();
44 LY_DEFINE (ly_font_glyph_name_to_index, "ly:font-glyph-name-to-index",
47 "Return the index for @var{name} in @var{font}.")
49 Font_metric *fm = unsmob_metrics (font);
50 SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
51 SCM_ASSERT_TYPE (scm_is_string (name), name, SCM_ARG2, __FUNCTION__, "string");
53 return scm_from_int (fm->name_to_index (ly_scm2string (name)));
56 LY_DEFINE (ly_font_index_to_charcode, "ly:font-index-to-charcode",
58 (SCM font, SCM index),
59 "Return the character code for @var{index} @var{font}.")
61 Font_metric *fm = unsmob_metrics (font);
62 SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
63 SCM_ASSERT_TYPE (scm_is_integer (index), index, SCM_ARG2, __FUNCTION__, "index");
65 return scm_from_unsigned_integer (fm->index_to_charcode (scm_to_int (index)));
68 LY_DEFINE (ly_font_glyph_name_to_charcode, "ly:font-glyph-name-to-charcode",
71 "Return the character code for glyph @var{name} in @var{font}.")
73 Font_metric *fm = unsmob_metrics (font);
74 SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
75 SCM_ASSERT_TYPE (scm_is_string (name), name, SCM_ARG2, __FUNCTION__, "string");
77 return scm_from_unsigned_integer (fm->index_to_charcode (fm->name_to_index (ly_scm2string (name))));
80 LY_DEFINE (ly_text_dimension, "ly:text-dimension",
83 "Given the font metric in @var{font} and the string @var{text}, "
84 "compute the extents of that text in that font. "
85 "The return value is a pair of number-pairs.")
88 Modified_font_metric *fm = dynamic_cast<Modified_font_metric *>
89 (unsmob_metrics (font));
91 SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "modified font metric");
92 SCM_ASSERT_TYPE (scm_is_string (text), text, SCM_ARG2, __FUNCTION__, "string");
93 Stencil stc (fm->text_stencil (ly_scm2string (text)));
94 return scm_cons (ly_interval2scm (stc.extent (X_AXIS)),
95 ly_interval2scm (stc.extent (Y_AXIS)));
100 TODO: when are non string retvals allowed?
102 LY_DEFINE (ly_font_file_name, "ly:font-file-name",
105 "Given the font metric @var{font}, "
106 "return the corresponding file name.")
108 Font_metric *fm = unsmob_metrics (font);
109 SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
110 SCM name = fm->font_file_name ();
115 LY_DEFINE (ly_font_name, "ly:font-name",
118 "Given the font metric @var{font}, "
119 "return the corresponding name.")
121 Font_metric *fm = unsmob_metrics (font);
123 SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
124 return scm_makfrom0str (fm->font_name ().c_str ());
127 LY_DEFINE (ly_font_magnification, "ly:font-magnification", 1, 0, 0,
129 "Given the font metric @var{font}, return the "
130 "magnification, relative to the current outputs-cale.")
132 Font_metric *fm = unsmob_metrics (font);
133 SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
134 return scm_cdr (fm->description_);
137 LY_DEFINE (ly_font_design_size, "ly:font-design-size", 1, 0, 0,
139 "Given the font metric @var{font}, return the "
140 "design size, relative to the current output-scale.")
142 Font_metric *fm = unsmob_metrics (font);
143 SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
144 return scm_from_double (fm->design_size ());