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 "modified-font-metric.hh"
17 #include "open-type-font.hh"
19 #include "virtual-methods.hh"
22 #include "ly-smobs.icc"
25 Font_metric::design_size () const
31 Font_metric::coding_scheme () const
33 return "FontSpecific";
37 Font_metric::find_by_name (String s) const
39 s.substitute_char ('-', "M");
40 int idx = name_to_index (s);
46 expr = scm_list_3 (ly_symbol2scm ("named-glyph"),
48 scm_makfrom0str (s.to_str0 ())
50 b = get_indexed_char (idx);
57 Font_metric::Font_metric ()
59 description_ = SCM_EOL;
64 Font_metric::Font_metric (Font_metric const &)
69 Font_metric::~Font_metric ()
74 Font_metric::count () const
80 Font_metric::get_ascii_char (int) const
82 return Box (Interval (0, 0), Interval (0, 0));
86 Font_metric::get_indexed_char (int k) const
88 return get_ascii_char (k);
92 Font_metric::name_to_index (String) const
98 Font_metric::get_indexed_wxwy (int) const
100 return Offset (0, 0);
104 Font_metric::derived_mark () const
109 Font_metric::mark_smob (SCM s)
111 Font_metric *m = (Font_metric*) SCM_CELL_WORD_1 (s);
113 return m->description_;
117 Font_metric::print_smob (SCM s, SCM port, scm_print_state *)
119 Font_metric *m = unsmob_metrics (s);
120 scm_puts ("#<", port);
121 scm_puts (classname (m), port);
122 scm_puts (" ", port);
123 scm_write (m->description_, port);
124 scm_puts (">", port);
130 IMPLEMENT_SMOBS (Font_metric);
131 IMPLEMENT_DEFAULT_EQUAL_P (Font_metric);
132 IMPLEMENT_TYPE_P (Font_metric, "ly:font-metric?");
135 LY_DEFINE (ly_font_get_glyph, "ly:font-get-glyph",
137 (SCM font, SCM name),
138 "Return a Stencil from @var{font} for the glyph named @var{name}. "
139 "@var{font} must be available as an AFM file. If the glyph "
140 "is not available, return @code{#f}.")
142 Font_metric *fm = unsmob_metrics (font);
143 SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
144 SCM_ASSERT_TYPE (scm_is_string (name), name, SCM_ARG2, __FUNCTION__, "string");
146 Stencil m = fm->find_by_name (ly_scm2string (name));
148 /* TODO: make optional argument for default if not found. */
149 return m.smobbed_copy ();
152 LY_DEFINE (ly_get_glyph, "ly:get-glyph",
154 (SCM font, SCM index),
155 "Retrieve a Stencil for the glyph numbered @var{index} "
158 Font_metric *fm = unsmob_metrics (font);
159 SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
160 SCM_ASSERT_TYPE (scm_is_number (index), index, SCM_ARG2, __FUNCTION__, "number");
162 return fm->get_ascii_char_stencil (scm_to_int (index)).smobbed_copy ();
165 LY_DEFINE (ly_font_glyph_name_to_index, "ly:font-glyph-name-to-index",
167 (SCM font, SCM name),
168 "Return the index for @var{name} in @var{font}.")
170 Font_metric *fm = unsmob_metrics (font);
171 SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
172 SCM_ASSERT_TYPE (scm_is_string (name), name, SCM_ARG2, __FUNCTION__, "string");
174 return scm_from_int (fm->name_to_index (ly_scm2string (name)));
177 LY_DEFINE (ly_font_index_to_charcode, "ly:font-index-to-charcode",
179 (SCM font, SCM index),
180 "Return the character code for @var{index} @var{font}.")
182 Font_metric *fm = unsmob_metrics (font);
183 SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
184 SCM_ASSERT_TYPE (scm_is_integer (index), index, SCM_ARG2, __FUNCTION__, "index");
186 return scm_from_unsigned_integer (fm->index_to_charcode (scm_to_int (index)));
189 LY_DEFINE (ly_font_glyph_name_to_charcode, "ly:font-glyph-name-to-charcode",
191 (SCM font, SCM name),
192 "Return the character code for glyph @var{name} in @var{font}.")
194 Font_metric *fm = unsmob_metrics (font);
195 SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
196 SCM_ASSERT_TYPE (scm_is_string (name), name, SCM_ARG2, __FUNCTION__, "string");
198 return scm_from_unsigned_integer (fm->index_to_charcode (fm->name_to_index (ly_scm2string (name))));
200 return scm_from_unsigned_integer (fm->glyph_name_to_charcode (ly_scm2string (name)));
204 LY_DEFINE (ly_text_dimension, "ly:text-dimension",
206 (SCM font, SCM text),
207 "Given the font metric in @var{font} and the string @var{text}, "
208 "compute the extents of that text in that font. "
209 "The return value is a pair of number-pairs.")
212 Modified_font_metric*fm = dynamic_cast<Modified_font_metric*>
213 (unsmob_metrics (font));
215 SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "modified font metric");
216 SCM_ASSERT_TYPE (scm_is_string (text), text, SCM_ARG2, __FUNCTION__, "string");
217 Stencil stc (fm->text_stencil (ly_scm2string (text)));
218 return scm_cons (ly_interval2scm (stc.extent (X_AXIS)),
219 ly_interval2scm (stc.extent (Y_AXIS)));
222 LY_DEFINE (ly_font_file_name, "ly:font-file-name",
225 "Given the font metric @var{font}, "
226 "return the corresponding file name.")
228 Font_metric *fm = unsmob_metrics (font);
229 SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
230 return scm_car (fm->description_);
234 Font_metric::font_name () const
236 String s ("unknown");
242 LY_DEFINE (ly_font_name, "ly:font-name",
245 "Given the font metric @var{font}, "
246 "return the corresponding name.")
248 Font_metric *fm = unsmob_metrics (font);
250 SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
251 return scm_makfrom0str (fm->font_name().to_str0 ());
254 LY_DEFINE (ly_font_magnification, "ly:font-magnification", 1, 0, 0,
256 "Given the font metric @var{font}, return the "
257 "magnification, relative to the current outputscale.")
259 Font_metric *fm = unsmob_metrics (font);
260 SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
261 return scm_cdr (fm->description_);
264 LY_DEFINE (ly_font_design_size, "ly:font-design-size", 1, 0, 0,
266 "Given the font metric @var{font}, return the "
267 "design size, relative to the current outputscale.")
269 Font_metric *fm = unsmob_metrics (font);
270 SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
271 return scm_make_real (fm->design_size ());
277 Font_metric::index_to_ascii (int i) const
283 Font_metric::index_to_charcode (int i) const
285 return (unsigned) index_to_ascii (i);
289 Font_metric::get_ascii_char_stencil (int code) const
291 SCM at = scm_list_3 (ly_symbol2scm ("char"), self_scm (),
293 Box b = get_ascii_char (code);
294 return Stencil (b, at);
298 Font_metric::get_indexed_char_stencil (int code) const
300 int idx = index_to_ascii (code);
301 SCM at = scm_list_3 (ly_symbol2scm ("char"), self_scm (), scm_int2num (idx));
302 Box b = get_indexed_char (code);
303 return Stencil (b, at);
308 get_encoded_index (Font_metric *m, String input_coding, int code)
310 String font_coding = m->coding_scheme ();
311 if (font_coding == input_coding)
313 SCM s = scm_call_3 (ly_scheme_function ("encoded-index"),
314 scm_makfrom0str (input_coding.to_str0 ()),
315 scm_makfrom0str (font_coding.to_str0 ()),
317 return scm_to_int (s);
321 Font_metric::attachment_point (String) const
323 return Offset (0, 0);
327 Font_metric::sub_fonts () const
333 Font_metric::text_stencil (String str) const
335 SCM lst = scm_list_3 (ly_symbol2scm ("text"),
337 scm_makfrom0str (str.to_str0 ()));
339 Box b = text_dimension (str);
340 return Stencil (b, lst);
344 Font_metric::text_dimension (String) const
346 return Box (Interval (0,0), Interval (0,0));