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)
14 #include "virtual-methods.hh"
17 #include "ly-smobs.icc"
18 #include "font-metric.hh"
22 Font_metric::design_size () const
28 Font_metric::text_dimension (String text) const
33 for (int i = 0; i < text.length (); i++)
39 // accent marks use width of base letter
40 if (i +1 < text.length ())
42 if (text[i+1]=='\'' || text[i+1]=='`' || text[i+1]=='"' ||
48 // for string width \\ is a \ and \_ is a _.
49 if (text[i+1]=='\\' || text[i+1]=='_')
55 for (i++; (i < text.length ()) && !isspace (text[i])
56 && text[i]!='{' && text[i]!='}'; i++)
59 i--; // Compensate for the increment in the outer loop!
61 case '{': // Skip '{' and '}'
66 Box b = get_ascii_char ((unsigned char)text[i]);
68 // Ugh, use the width of 'x' for unknown characters
69 if (b[X_AXIS].length () == 0)
70 b = get_ascii_char ((unsigned char)'x');
72 w += b[X_AXIS].length ();
73 ydims.unite (b[Y_AXIS]);
77 if (ydims.is_empty ())
78 ydims = Interval (0,0);
80 return Box (Interval (0, w), ydims);
85 Font_metric::~Font_metric ()
89 Font_metric::Font_metric ()
91 description_ = SCM_EOL;
96 Font_metric::Font_metric (Font_metric const &)
101 Font_metric::count () const
107 Font_metric::get_ascii_char (int) const
109 return Box (Interval (0,0),Interval (0,0));
113 Font_metric::get_indexed_char (int k) const
115 return get_ascii_char (k);
120 Font_metric::name_to_index (String) const
126 Font_metric::get_indexed_wxwy (int )const
132 Font_metric::derived_mark ()const
138 Font_metric::mark_smob (SCM s)
140 Font_metric * m = (Font_metric*) SCM_CELL_WORD_1 (s);
143 return m->description_;
147 Font_metric::print_smob (SCM s, SCM port, scm_print_state *)
149 Font_metric *m = unsmob_metrics (s);
150 scm_puts ("#<", port);
151 scm_puts (classname (m), port);
152 scm_puts (" ", port);
153 scm_write (m->description_, port);
154 scm_puts (">", port);
160 IMPLEMENT_SMOBS (Font_metric);
161 IMPLEMENT_DEFAULT_EQUAL_P (Font_metric);
162 IMPLEMENT_TYPE_P (Font_metric, "ly:font-metric?");
165 Font_metric::find_by_name (String) const
171 LY_DEFINE (ly_find_glyph_by_name, "ly:find-glyph-by-name", 2 , 0, 0,
172 (SCM font, SCM name),
173 "This function retrieves a Stencil for the glyph named @var{name} "
176 "The font must be available as an AFM file. If the glyph "
177 "is not found, @code{#f} is returned. ")
179 Font_metric *fm = unsmob_metrics (font);
180 SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
181 SCM_ASSERT_TYPE (gh_string_p (name), name, SCM_ARG2, __FUNCTION__, "string");
183 Stencil m = fm->find_by_name (ly_scm2string (name));
186 TODO: make optional argument for default if not found.
188 return m.smobbed_copy ();
191 LY_DEFINE (ly_get_glyph, "ly:get-glyph", 2 , 0, 0,
192 (SCM font, SCM index),
193 "This function retrieves a Stencil for the glyph numbered @var{index} in "
196 Font_metric *fm = unsmob_metrics (font);
197 SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
198 SCM_ASSERT_TYPE (gh_number_p (index), index, SCM_ARG2, __FUNCTION__, "number");
200 return fm->get_ascii_char_stencil (gh_scm2int (index)).smobbed_copy ();
203 LY_DEFINE (ly_text_dimension,"ly:text-dimension", 2 , 0, 0,
204 (SCM font, SCM text),
205 "Given the font metric in @var{font} and the string @var{text}, compute "
206 "the extents of that text in that font. The return value is a pair of "
210 Font_metric *fm = unsmob_metrics (font);
211 SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
212 SCM_ASSERT_TYPE (gh_string_p (text), text, SCM_ARG2, __FUNCTION__, "string");
214 b = fm->text_dimension (ly_scm2string (text));
216 return gh_cons (ly_interval2scm (b[X_AXIS]), ly_interval2scm (b[Y_AXIS]));
219 LY_DEFINE (ly_font_name,"ly:font-name", 1 , 0, 0,
221 "Given the font metric @var{font}, return the corresponding file name. ")
223 Font_metric *fm = unsmob_metrics (font);
224 SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
225 return gh_car (fm->description_);
228 LY_DEFINE (ly_font_magnification,"ly:font-magnification", 1 , 0, 0,
230 "Given the font metric @var{font}, return the "
231 "magnification, relative to the current outputscale.")
233 Font_metric *fm = unsmob_metrics (font);
234 SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
235 return gh_cdr (fm->description_);
238 LY_DEFINE (ly_font_design_size,"ly:font-design-size", 1 , 0, 0,
240 "Given the font metric @var{font}, return the "
241 "design size, relative to the current outputscale.")
243 Font_metric *fm = unsmob_metrics (font);
244 SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
245 return gh_cdr (fm->description_);
250 Font_metric::get_ascii_char_stencil (int code) const
252 SCM at = scm_list_n (ly_symbol2scm ("char"), gh_int2scm (code),
254 at = fontify_atom (this, at);
255 Box b = get_ascii_char (code);
256 return Stencil (b, at);
260 Font_metric::get_indexed_char_stencil (int code) const
262 SCM at = scm_list_n (ly_symbol2scm ("char"), gh_int2scm (code),
264 at = fontify_atom (this, at);
265 Box b = get_indexed_char (code);
266 return Stencil (b, at);