2 font-metric-scheme.cc -- implement Font_metric scheme bindings
4 source file of the GNU LilyPond music typesetter
6 (c) 2005--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
9 #include "font-metric.hh"
13 #include "modified-font-metric.hh"
15 LY_DEFINE (ly_font_get_glyph
, "ly:font-get-glyph",
18 "Return a Stencil from @var{font} for the glyph named @var{name}. "
19 "@var{font} must be available as an AFM file. If the glyph "
20 "is not available, return @code{#f}.")
22 Font_metric
*fm
= unsmob_metrics (font
);
23 LY_ASSERT_SMOB (Font_metric
, font
, 1);
24 LY_ASSERT_TYPE (scm_is_string
, name
, 2);
26 Stencil m
= fm
->find_by_name (ly_scm2string (name
));
28 /* TODO: make optional argument for default if not found. */
29 return m
.smobbed_copy ();
32 LY_DEFINE (ly_get_glyph
, "ly:get-glyph",
34 (SCM font
, SCM index
),
35 "Retrieve a Stencil for the glyph numbered @var{index} "
38 Font_metric
*fm
= unsmob_metrics (font
);
39 LY_ASSERT_SMOB (Font_metric
, font
, 1);
40 LY_ASSERT_TYPE (scm_is_number
, index
,2);
42 return fm
->get_ascii_char_stencil (scm_to_int (index
)).smobbed_copy ();
45 LY_DEFINE (ly_font_glyph_name_to_index
, "ly:font-glyph-name-to-index",
48 "Return the index for @var{name} in @var{font}.")
50 Font_metric
*fm
= unsmob_metrics (font
);
51 LY_ASSERT_SMOB (Font_metric
, font
, 1);
52 LY_ASSERT_TYPE (scm_is_string
, name
, 2);
54 return scm_from_int (fm
->name_to_index (ly_scm2string (name
)));
57 LY_DEFINE (ly_font_index_to_charcode
, "ly:font-index-to-charcode",
59 (SCM font
, SCM index
),
60 "Return the character code for @var{index} @var{font}.")
62 Font_metric
*fm
= unsmob_metrics (font
);
63 LY_ASSERT_SMOB (Font_metric
, font
, 1);
64 LY_ASSERT_TYPE (scm_is_integer
, index
, 2);
66 return scm_from_unsigned_integer (fm
->index_to_charcode (scm_to_int (index
)));
69 LY_DEFINE (ly_font_glyph_name_to_charcode
, "ly:font-glyph-name-to-charcode",
72 "Return the character code for glyph @var{name} in @var{font}.")
74 Font_metric
*fm
= unsmob_metrics (font
);
75 LY_ASSERT_SMOB (Font_metric
, font
, 1);
76 LY_ASSERT_TYPE (scm_is_string
, name
, 2);
78 return scm_from_unsigned_integer (fm
->index_to_charcode (fm
->name_to_index (ly_scm2string (name
))));
81 LY_DEFINE (ly_text_dimension
, "ly:text-dimension",
84 "Given the font metric in @var{font} and the string @var{text}, "
85 "compute the extents of that text in that font. "
86 "The return value is a pair of number-pairs.")
89 Modified_font_metric
*fm
= dynamic_cast<Modified_font_metric
*>
90 (unsmob_metrics (font
));
92 LY_ASSERT_SMOB (Font_metric
, font
, 1);
93 LY_ASSERT_TYPE (scm_is_string
, text
, 2);
94 Stencil
stc (fm
->text_stencil (ly_scm2string (text
)));
95 return scm_cons (ly_interval2scm (stc
.extent (X_AXIS
)),
96 ly_interval2scm (stc
.extent (Y_AXIS
)));
101 TODO: when are non string retvals allowed?
103 LY_DEFINE (ly_font_file_name
, "ly:font-file-name",
106 "Given the font metric @var{font}, "
107 "return the corresponding file name.")
109 LY_ASSERT_SMOB (Font_metric
, font
, 1);
111 Font_metric
*fm
= unsmob_metrics (font
);
112 SCM name
= fm
->font_file_name ();
117 LY_DEFINE (ly_font_name
, "ly:font-name",
120 "Given the font metric @var{font}, "
121 "return the corresponding name.")
123 LY_ASSERT_SMOB (Font_metric
, font
, 1);
124 Font_metric
*fm
= unsmob_metrics (font
);
126 return ly_string2scm (fm
->font_name ());
129 LY_DEFINE (ly_font_magnification
, "ly:font-magnification", 1, 0, 0,
131 "Given the font metric @var{font}, return the "
132 "magnification, relative to the current output-scale.")
134 LY_ASSERT_SMOB (Font_metric
, font
, 1);
136 Font_metric
*fm
= unsmob_metrics (font
);
137 return scm_cdr (fm
->description_
);
140 LY_DEFINE (ly_font_design_size
, "ly:font-design-size", 1, 0, 0,
142 "Given the font metric @var{font}, return the "
143 "design size, relative to the current output-scale.")
145 LY_ASSERT_SMOB (Font_metric
, font
, 1);
147 Font_metric
*fm
= unsmob_metrics (font
);
148 return scm_from_double (fm
->design_size ());