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::text_dimension (String text) const
27 for (int i = 0; i < text.length (); i++)
33 // accent marks use width of base letter
34 if (i +1 < text.length ())
36 if (text[i+1]=='\'' || text[i+1]=='`' || text[i+1]=='"' ||
42 // for string width \\ is a \ and \_ is a _.
43 if (text[i+1]=='\\' || text[i+1]=='_')
49 for (i++; (i < text.length ()) && !isspace (text[i])
50 && text[i]!='{' && text[i]!='}'; i++)
53 i--; // Compensate for the increment in the outer loop!
55 case '{': // Skip '{' and '}'
60 Box b = get_ascii_char ((unsigned char)text[i]);
62 // Ugh, use the width of 'x' for unknown characters
63 if (b[X_AXIS].length () == 0)
64 b = get_ascii_char ((unsigned char)'x');
66 w += b[X_AXIS].length ();
67 ydims.unite (b[Y_AXIS]);
71 if (ydims.is_empty ())
72 ydims = Interval (0,0);
74 return Box (Interval (0, w), ydims);
79 Font_metric::~Font_metric ()
83 Font_metric::Font_metric ()
85 description_ = SCM_EOL;
90 Font_metric::Font_metric (Font_metric const &)
95 Font_metric::count () const
101 Font_metric::get_ascii_char (int) const
103 return Box (Interval (0,0),Interval (0,0));
107 Font_metric::get_indexed_char (int k) const
109 return get_ascii_char (k);
114 Font_metric::name_to_index (String) const
120 Font_metric::get_indexed_wxwy (int )const
126 Font_metric::derived_mark ()const
132 Font_metric::mark_smob (SCM s)
134 Font_metric * m = (Font_metric*) SCM_CELL_WORD_1 (s);
137 return m->description_;
141 Font_metric::print_smob (SCM s, SCM port, scm_print_state *)
143 Font_metric *m = unsmob_metrics (s);
144 scm_puts ("#<", port);
145 scm_puts (classname (m), port);
146 scm_puts (" ", port);
147 scm_write (m->description_, port);
148 scm_puts (">", port);
154 IMPLEMENT_SMOBS (Font_metric);
155 IMPLEMENT_DEFAULT_EQUAL_P (Font_metric);
156 IMPLEMENT_TYPE_P (Font_metric, "ly:font-metric?");
159 Font_metric::find_by_name (String) const
165 LY_DEFINE (ly_find_glyph_by_name, "ly:find-glyph-by-name", 2 , 0, 0,
166 (SCM font, SCM name),
167 "This function retrieves a Stencil for the glyph named @var{name} "
170 "The font must be available as an AFM file. If the glyph "
171 "is not found, @code{#f} is returned. ")
173 Font_metric *fm = unsmob_metrics (font);
174 SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
175 SCM_ASSERT_TYPE (gh_string_p (name), name, SCM_ARG2, __FUNCTION__, "string");
177 Stencil m = fm->find_by_name (ly_scm2string (name));
180 TODO: make optional argument for default if not found.
183 return m.smobbed_copy ();
186 LY_DEFINE (ly_get_glyph, "ly:get-glyph", 2 , 0, 0,
187 (SCM font, SCM index),
188 "This function retrieves a Stencil for the glyph numbered @var{index} in "
191 Font_metric *fm = unsmob_metrics (font);
192 SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
193 SCM_ASSERT_TYPE (gh_number_p (index), index, SCM_ARG2, __FUNCTION__, "number");
195 return fm->get_ascii_char_stencil (gh_scm2int (index)).smobbed_copy ();
198 LY_DEFINE (ly_text_dimension,"ly:text-dimension", 2 , 0, 0,
199 (SCM font, SCM text),
200 "Given the font metric in @var{font} and the string @var{text}, compute "
201 "the extents of that text in that font. The return value is a pair of "
205 Font_metric *fm = unsmob_metrics (font);
206 SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
207 SCM_ASSERT_TYPE (gh_string_p (text), text, SCM_ARG2, __FUNCTION__, "string");
209 b = fm->text_dimension (ly_scm2string (text));
211 return gh_cons (ly_interval2scm (b[X_AXIS]), ly_interval2scm (b[Y_AXIS]));
215 Font_metric::get_ascii_char_stencil (int code) const
217 SCM at = scm_list_n (ly_symbol2scm ("char"), gh_int2scm (code),
219 at = fontify_atom (this, at);
220 Box b = get_ascii_char (code);
221 return Stencil (b, at);
225 Font_metric::get_indexed_char_stencil (int code) const
227 SCM at = scm_list_n (ly_symbol2scm ("char"), gh_int2scm (code),
229 at = fontify_atom (this, at);
230 Box b = get_indexed_char (code);
231 return Stencil (b, at);