2 font-metric.cc -- implement Font_metric
4 source file of the GNU LilyPond music typesetter
6 (c) 1999--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
8 Mats Bengtsson <matsb@s3.kth.se> (the ugly TeX parsing in text_dimension)
11 #include "font-metric.hh"
16 #include "dimensions.hh"
17 #include "modified-font-metric.hh"
18 #include "open-type-font.hh"
20 #include "virtual-methods.hh"
23 #include "ly-smobs.icc"
26 Font_metric::design_size () const
28 return 1.0 * point_constant;
33 Font_metric::find_by_name (String s) const
35 s.substitute_char ('-', "M");
36 int idx = name_to_index (s);
42 expr = scm_list_3 (ly_symbol2scm ("named-glyph"),
44 scm_makfrom0str (s.to_str0 ())
46 b = get_indexed_char (idx);
53 Font_metric::Font_metric ()
55 description_ = SCM_EOL;
60 Font_metric::Font_metric (Font_metric const &)
65 Font_metric::~Font_metric ()
70 Font_metric::count () const
76 Font_metric::get_ascii_char (int) const
78 return Box (Interval (0, 0), Interval (0, 0));
82 Font_metric::get_indexed_char (int k) const
84 return get_ascii_char (k);
88 Font_metric::name_to_index (String) const
94 Font_metric::get_indexed_wxwy (int) const
100 Font_metric::derived_mark () const
105 Font_metric::mark_smob (SCM s)
107 Font_metric *m = (Font_metric*) SCM_CELL_WORD_1 (s);
109 return m->description_;
113 Font_metric::print_smob (SCM s, SCM port, scm_print_state *)
115 Font_metric *m = unsmob_metrics (s);
116 scm_puts ("#<", port);
117 scm_puts (classname (m), port);
118 scm_puts (" ", port);
119 scm_write (m->description_, port);
120 scm_puts (">", port);
126 IMPLEMENT_SMOBS (Font_metric);
127 IMPLEMENT_DEFAULT_EQUAL_P (Font_metric);
128 IMPLEMENT_TYPE_P (Font_metric, "ly:font-metric?");
131 LY_DEFINE (ly_font_get_glyph, "ly:font-get-glyph",
133 (SCM font, SCM name),
134 "Return a Stencil from @var{font} for the glyph named @var{name}. "
135 "@var{font} must be available as an AFM file. If the glyph "
136 "is not available, return @code{#f}.")
138 Font_metric *fm = unsmob_metrics (font);
139 SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
140 SCM_ASSERT_TYPE (scm_is_string (name), name, SCM_ARG2, __FUNCTION__, "string");
142 Stencil m = fm->find_by_name (ly_scm2string (name));
144 /* TODO: make optional argument for default if not found. */
145 return m.smobbed_copy ();
148 LY_DEFINE (ly_get_glyph, "ly:get-glyph",
150 (SCM font, SCM index),
151 "Retrieve a Stencil for the glyph numbered @var{index} "
154 Font_metric *fm = unsmob_metrics (font);
155 SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
156 SCM_ASSERT_TYPE (scm_is_number (index), index, SCM_ARG2, __FUNCTION__, "number");
158 return fm->get_ascii_char_stencil (scm_to_int (index)).smobbed_copy ();
161 LY_DEFINE (ly_font_glyph_name_to_index, "ly:font-glyph-name-to-index",
163 (SCM font, SCM name),
164 "Return the index for @var{name} in @var{font}.")
166 Font_metric *fm = unsmob_metrics (font);
167 SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
168 SCM_ASSERT_TYPE (scm_is_string (name), name, SCM_ARG2, __FUNCTION__, "string");
170 return scm_from_int (fm->name_to_index (ly_scm2string (name)));
173 LY_DEFINE (ly_font_index_to_charcode, "ly:font-index-to-charcode",
175 (SCM font, SCM index),
176 "Return the character code for @var{index} @var{font}.")
178 Font_metric *fm = unsmob_metrics (font);
179 SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
180 SCM_ASSERT_TYPE (scm_is_integer (index), index, SCM_ARG2, __FUNCTION__, "index");
182 return scm_from_unsigned_integer (fm->index_to_charcode (scm_to_int (index)));
185 LY_DEFINE (ly_font_glyph_name_to_charcode, "ly:font-glyph-name-to-charcode",
187 (SCM font, SCM name),
188 "Return the character code for glyph @var{name} in @var{font}.")
190 Font_metric *fm = unsmob_metrics (font);
191 SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
192 SCM_ASSERT_TYPE (scm_is_string (name), name, SCM_ARG2, __FUNCTION__, "string");
194 return scm_from_unsigned_integer (fm->index_to_charcode (fm->name_to_index (ly_scm2string (name))));
196 return scm_from_unsigned_integer (fm->glyph_name_to_charcode (ly_scm2string (name)));
200 LY_DEFINE (ly_text_dimension, "ly:text-dimension",
202 (SCM font, SCM text),
203 "Given the font metric in @var{font} and the string @var{text}, "
204 "compute the extents of that text in that font. "
205 "The return value is a pair of number-pairs.")
208 Modified_font_metric*fm = dynamic_cast<Modified_font_metric*>
209 (unsmob_metrics (font));
211 SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "modified font metric");
212 SCM_ASSERT_TYPE (scm_is_string (text), text, SCM_ARG2, __FUNCTION__, "string");
213 Stencil stc (fm->text_stencil (ly_scm2string (text)));
214 return scm_cons (ly_interval2scm (stc.extent (X_AXIS)),
215 ly_interval2scm (stc.extent (Y_AXIS)));
218 LY_DEFINE (ly_font_file_name, "ly:font-file-name",
221 "Given the font metric @var{font}, "
222 "return the corresponding file name.")
224 Font_metric *fm = unsmob_metrics (font);
225 SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
226 return fm->font_file_name();
230 Font_metric::font_file_name () const
232 return scm_car (description_);
236 Font_metric::font_name () const
238 String s ("unknown");
244 LY_DEFINE (ly_font_name, "ly:font-name",
247 "Given the font metric @var{font}, "
248 "return the corresponding name.")
250 Font_metric *fm = unsmob_metrics (font);
252 SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
253 return scm_makfrom0str (fm->font_name().to_str0 ());
256 LY_DEFINE (ly_font_magnification, "ly:font-magnification", 1, 0, 0,
258 "Given the font metric @var{font}, return the "
259 "magnification, relative to the current outputscale.")
261 Font_metric *fm = unsmob_metrics (font);
262 SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
263 return scm_cdr (fm->description_);
266 LY_DEFINE (ly_font_design_size, "ly:font-design-size", 1, 0, 0,
268 "Given the font metric @var{font}, return the "
269 "design size, relative to the current outputscale.")
271 Font_metric *fm = unsmob_metrics (font);
272 SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
273 return scm_make_real (fm->design_size ());
279 Font_metric::index_to_ascii (int i) const
285 Font_metric::index_to_charcode (int i) const
287 return (unsigned) index_to_ascii (i);
291 Font_metric::get_ascii_char_stencil (int code) const
293 SCM at = scm_list_3 (ly_symbol2scm ("char"), self_scm (),
295 Box b = get_ascii_char (code);
296 return Stencil (b, at);
300 Font_metric::get_indexed_char_stencil (int code) const
302 int idx = index_to_ascii (code);
303 SCM at = scm_list_3 (ly_symbol2scm ("char"), self_scm (), scm_int2num (idx));
304 Box b = get_indexed_char (code);
305 return Stencil (b, at);
310 Font_metric::attachment_point (String) const
312 return Offset (0, 0);
316 Font_metric::sub_fonts () const
322 Font_metric::text_stencil (String str) const
324 SCM lst = scm_list_3 (ly_symbol2scm ("text"),
326 scm_makfrom0str (str.to_str0 ()));
328 Box b = text_dimension (str);
329 return Stencil (b, lst);
333 Font_metric::text_dimension (String) const
335 return Box (Interval (0,0), Interval (0,0));