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::coding_scheme () const
30 return "FontSpecific";
34 Font_metric::text_dimension (String text) const
39 for (int i = 0; i < text.length (); i++)
45 // accent marks use width of base letter
46 if (i +1 < text.length ())
48 if (text[i+1]=='\'' || text[i+1]=='`' || text[i+1]=='"' ||
54 // for string width \\ is a \ and \_ is a _.
55 if (text[i+1]=='\\' || text[i+1]=='_')
61 for (i++; (i < text.length ()) && !isspace (text[i])
62 && text[i]!='{' && text[i]!='}'; i++)
65 i--; // Compensate for the increment in the outer loop!
67 case '{': // Skip '{' and '}'
72 Box b = get_ascii_char ((unsigned char)text[i]);
74 // Ugh, use the width of 'x' for unknown characters
75 if (b[X_AXIS].length () == 0)
76 b = get_ascii_char ((unsigned char)'x');
78 w += b[X_AXIS].length ();
79 ydims.unite (b[Y_AXIS]);
83 if (ydims.is_empty ())
84 ydims = Interval (0, 0);
86 return Box (Interval (0, w), ydims);
90 Font_metric::Font_metric ()
92 description_ = SCM_EOL;
97 Font_metric::Font_metric (Font_metric const &)
102 Font_metric::~Font_metric ()
107 Font_metric::count () const
113 Font_metric::get_ascii_char (int) const
115 return Box (Interval (0, 0), Interval (0, 0));
119 Font_metric::get_indexed_char (int k) const
121 return get_ascii_char (k);
125 Font_metric::name_to_index (String) const
131 Font_metric::get_indexed_wxwy (int )const
133 return Offset (0, 0);
137 Font_metric::derived_mark ()const
142 Font_metric::mark_smob (SCM s)
144 Font_metric *m = (Font_metric*) SCM_CELL_WORD_1 (s);
146 return m->description_;
150 Font_metric::print_smob (SCM s, SCM port, scm_print_state *)
152 Font_metric *m = unsmob_metrics (s);
153 scm_puts ("#<", port);
154 scm_puts (classname (m), port);
155 scm_puts (" ", port);
156 scm_write (m->description_, port);
157 scm_puts (">", port);
163 IMPLEMENT_SMOBS (Font_metric);
164 IMPLEMENT_DEFAULT_EQUAL_P (Font_metric);
165 IMPLEMENT_TYPE_P (Font_metric, "ly:font-metric?");
168 Font_metric::find_by_name (String) const
174 LY_DEFINE (ly_find_glyph_by_name, "ly:find-glyph-by-name",
176 (SCM font, SCM name),
177 "This function retrieves a Stencil for the glyph named @var{name} "
180 "The font must be available as an AFM file. If the glyph "
181 "is not found, @code{#f} is returned. ")
183 Font_metric *fm = unsmob_metrics (font);
184 SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
185 SCM_ASSERT_TYPE (gh_string_p (name), name, SCM_ARG2, __FUNCTION__, "string");
187 Stencil m = fm->find_by_name (ly_scm2string (name));
189 /* TODO: make optional argument for default if not found. */
190 return m.smobbed_copy ();
193 LY_DEFINE (ly_get_glyph, "ly:get-glyph",
195 (SCM font, SCM index),
196 "Retrieve a Stencil for the glyph numbered @var{index} "
199 Font_metric *fm = unsmob_metrics (font);
200 SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
201 SCM_ASSERT_TYPE (gh_number_p (index), index, SCM_ARG2, __FUNCTION__, "number");
203 return fm->get_ascii_char_stencil (gh_scm2int (index)).smobbed_copy ();
206 LY_DEFINE (ly_text_dimension,"ly:text-dimension",
208 (SCM font, SCM text),
209 "Given the font metric in @var{font} and the string @var{text}, "
210 "compute the extents of that text in that font. "
211 "The return value is a pair of number-pairs.")
214 Font_metric *fm = unsmob_metrics (font);
215 SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
216 SCM_ASSERT_TYPE (gh_string_p (text), text, SCM_ARG2, __FUNCTION__, "string");
218 b = fm->text_dimension (ly_scm2string (text));
220 return gh_cons (ly_interval2scm (b[X_AXIS]), ly_interval2scm (b[Y_AXIS]));
223 LY_DEFINE (ly_font_name,"ly:font-name",
226 "Given the font metric @var{font}, "
227 "return the corresponding file name.")
229 Font_metric *fm = unsmob_metrics (font);
230 SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
231 return gh_car (fm->description_);
234 LY_DEFINE (ly_font_magnification,"ly:font-magnification", 1 , 0, 0,
236 "Given the font metric @var{font}, return the "
237 "magnification, relative to the current outputscale.")
239 Font_metric *fm = unsmob_metrics (font);
240 SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
241 return gh_cdr (fm->description_);
244 LY_DEFINE (ly_font_design_size,"ly:font-design-size", 1 , 0, 0,
246 "Given the font metric @var{font}, return the "
247 "design size, relative to the current outputscale.")
249 Font_metric *fm = unsmob_metrics (font);
250 SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
251 return gh_double2scm (fm->design_size ());
255 Font_metric::get_ascii_char_stencil (int code) const
257 SCM at = scm_list_2 (ly_symbol2scm ("char"), gh_int2scm (code));
258 at = fontify_atom (this, at);
259 Box b = get_ascii_char (code);
260 return Stencil (b, at);
264 Font_metric::get_indexed_char_stencil (int code) const
266 SCM at = scm_list_2 (ly_symbol2scm ("char"), gh_int2scm (code));
267 at = fontify_atom (this, at);
268 Box b = get_indexed_char (code);
269 return Stencil (b, at);
274 get_encoded_index (Font_metric *m, String input_coding, int code)
276 String font_coding = m->coding_scheme ();
277 if (font_coding == input_coding)
279 SCM s = scm_call_3 (ly_scheme_function ("encoded-index"),
280 scm_makfrom0str (input_coding.to_str0 ()),
281 scm_makfrom0str (font_coding.to_str0 ()),
283 return gh_scm2int (s);