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 "virtual-methods.hh"
21 #include "ly-smobs.icc"
24 Font_metric::design_size () const
30 Font_metric::coding_scheme () const
32 return "FontSpecific";
36 Font_metric::find_by_name (String s) const
38 s.substitute_char ('-', "M");
39 int idx = name_to_index (s);
45 expr = scm_list_3 (ly_symbol2scm ("named-glyph"),
47 scm_makfrom0str (s.to_str0 ())
49 b = get_indexed_char (idx);
56 Font_metric::Font_metric ()
58 description_ = SCM_EOL;
63 Font_metric::Font_metric (Font_metric const &)
68 Font_metric::~Font_metric ()
73 Font_metric::count () const
79 Font_metric::get_ascii_char (int) const
81 return Box (Interval (0, 0), Interval (0, 0));
85 Font_metric::get_indexed_char (int k) const
87 return get_ascii_char (k);
91 Font_metric::name_to_index (String) const
97 Font_metric::get_indexed_wxwy (int) const
103 Font_metric::derived_mark () const
108 Font_metric::mark_smob (SCM s)
110 Font_metric *m = (Font_metric*) SCM_CELL_WORD_1 (s);
112 return m->description_;
116 Font_metric::print_smob (SCM s, SCM port, scm_print_state *)
118 Font_metric *m = unsmob_metrics (s);
119 scm_puts ("#<", port);
120 scm_puts (classname (m), port);
121 scm_puts (" ", port);
122 scm_write (m->description_, port);
123 scm_puts (">", port);
129 IMPLEMENT_SMOBS (Font_metric);
130 IMPLEMENT_DEFAULT_EQUAL_P (Font_metric);
131 IMPLEMENT_TYPE_P (Font_metric, "ly:font-metric?");
134 LY_DEFINE (ly_font_get_glyph, "ly:font-get-glyph",
136 (SCM font, SCM name),
137 "Return a Stencil from @var{font} for the glyph named @var{name}. "
138 "@var{font} must be available as an AFM file. If the glyph "
139 "is not available, return @code{#f}.")
141 Font_metric *fm = unsmob_metrics (font);
142 SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
143 SCM_ASSERT_TYPE (scm_is_string (name), name, SCM_ARG2, __FUNCTION__, "string");
145 Stencil m = fm->find_by_name (ly_scm2string (name));
147 /* TODO: make optional argument for default if not found. */
148 return m.smobbed_copy ();
151 LY_DEFINE (ly_get_glyph, "ly:get-glyph",
153 (SCM font, SCM index),
154 "Retrieve a Stencil for the glyph numbered @var{index} "
157 Font_metric *fm = unsmob_metrics (font);
158 SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
159 SCM_ASSERT_TYPE (scm_is_number (index), index, SCM_ARG2, __FUNCTION__, "number");
161 return fm->get_ascii_char_stencil (scm_to_int (index)).smobbed_copy ();
164 LY_DEFINE (ly_font_glyph_name_to_index, "ly:font-glyph-name-to-index",
166 (SCM font, SCM name),
167 "Return the index for @{name} in @var{font}.")
169 Font_metric *fm = unsmob_metrics (font);
170 SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
171 SCM_ASSERT_TYPE (scm_is_string (name), name, SCM_ARG2, __FUNCTION__, "string");
173 return scm_from_int (fm->name_to_index (ly_scm2string (name)));
176 LY_DEFINE (ly_font_index_to_charcode, "ly:font-index-to-charcode",
178 (SCM font, SCM index),
179 "Return the character code for @var{index} @var{font}.")
181 Font_metric *fm = unsmob_metrics (font);
182 SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
183 SCM_ASSERT_TYPE (scm_is_integer (index), index, SCM_ARG2, __FUNCTION__, "index");
185 return scm_from_unsigned_integer (fm->index_to_charcode (ly_scm2int (index)));
188 LY_DEFINE (ly_font_glyph_name_to_charcode, "ly:font-glyph-name-to-charcode",
190 (SCM font, SCM name),
191 "Return the character code for glyph @{name} in @var{font}.")
193 Font_metric *fm = unsmob_metrics (font);
194 SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
195 SCM_ASSERT_TYPE (scm_is_string (name), name, SCM_ARG2, __FUNCTION__, "string");
197 return scm_from_unsigned_integer (fm->index_to_charcode (fm->name_to_index (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));
210 SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "modified font metric");
211 SCM_ASSERT_TYPE (scm_is_string (text), text, SCM_ARG2, __FUNCTION__, "string");
213 b = fm->text_dimension (ly_scm2string (text));
215 return scm_cons (ly_interval2scm (b[X_AXIS]), ly_interval2scm (b[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 scm_car (fm->description_);
232 LY_DEFINE (ly_font_name,"ly:font-name",
235 "Given the font metric @var{font}, "
236 "return the corresponding name.")
238 Font_metric *fm = unsmob_metrics (font);
240 SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
243 if (Modified_font_metric* mfm = dynamic_cast<Modified_font_metric*> (fm))
244 return ly_font_name (mfm->original_font ()->self_scm ());
245 else if (Adobe_font_metric* afm = dynamic_cast<Adobe_font_metric*> (fm))
247 return scm_makfrom0str (afm->font_info_->gfi->fontName);
255 LY_DEFINE (ly_font_magnification,"ly:font-magnification", 1 , 0, 0,
257 "Given the font metric @var{font}, return the "
258 "magnification, relative to the current outputscale.")
260 Font_metric *fm = unsmob_metrics (font);
261 SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
262 return scm_cdr (fm->description_);
265 LY_DEFINE (ly_font_design_size,"ly:font-design-size", 1 , 0, 0,
267 "Given the font metric @var{font}, return the "
268 "design size, relative to the current outputscale.")
270 Font_metric *fm = unsmob_metrics (font);
271 SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
272 return scm_make_real (fm->design_size ());
278 Font_metric::index_to_ascii (int i) const
284 Font_metric::index_to_charcode (int i) const
286 return (unsigned) index_to_ascii (i);
290 Font_metric::get_ascii_char_stencil (int code) const
292 SCM at = scm_list_3 (ly_symbol2scm ("char"), self_scm (),
294 Box b = get_ascii_char (code);
295 return Stencil (b, at);
299 Font_metric::get_indexed_char_stencil (int code) const
301 int idx = index_to_ascii (code);
302 SCM at = scm_list_3 (ly_symbol2scm ("char"), self_scm (), scm_int2num (idx));
303 Box b = get_indexed_char (code);
304 return Stencil (b, at);
309 get_encoded_index (Font_metric *m, String input_coding, int code)
311 String font_coding = m->coding_scheme ();
312 if (font_coding == input_coding)
314 SCM s = scm_call_3 (ly_scheme_function ("encoded-index"),
315 scm_makfrom0str (input_coding.to_str0 ()),
316 scm_makfrom0str (font_coding.to_str0 ()),
318 return scm_to_int (s);
322 Font_metric::attachment_point (String) const
324 return Offset (0, 0);