2 font-metric.cc -- implement Font_metric
4 source file of the GNU LilyPond music typesetter
6 (c) 1999--2002 Han-Wen Nienhuys <hanwen@cs.uu.nl>
8 Mats Bengtsson <matsb@s3.kth.se> (the ugly TeX parsing in text_dimension)
15 #include "molecule.hh"
16 #include "ly-smobs.icc"
17 #include "font-metric.hh"
21 Font_metric::text_dimension (String text) const
26 for (int i = 0; i < text.length_i (); i++)
32 // accent marks use width of base letter
33 if (i +1 < text.length_i ())
35 if (text[i+1]=='\'' || text[i+1]=='`' || text[i+1]=='"' ||
41 // for string width \\ is a \ and \_ is a _.
42 if (text[i+1]=='\\' || text[i+1]=='_')
48 for (i++; (i < text.length_i ()) && !isspace (text[i])
49 && text[i]!='{' && text[i]!='}'; i++)
52 i--; // Compensate for the increment in the outer loop!
54 case '{': // Skip '{' and '}'
59 Box b = get_char ((unsigned char)text[i]);
61 // Ugh, use the width of 'x' for unknown characters
62 if (b[X_AXIS].length () == 0)
63 b = get_char ((unsigned char)'x');
65 w += b[X_AXIS].length ();
66 ydims.unite (b[Y_AXIS]);
71 ydims = Interval (0,0);
73 return Box (Interval (0, w), ydims);
78 Font_metric::~Font_metric ()
82 Font_metric::Font_metric ()
84 description_ = SCM_EOL;
89 Font_metric::Font_metric (Font_metric const &)
94 Font_metric::count () const
100 Font_metric::get_char (int)const
102 return Box (Interval (0,0),Interval (0,0));
107 Font_metric::mark_smob (SCM s)
109 Font_metric * m = (Font_metric*) SCM_CELL_WORD_1 (s);
110 return m->description_;
114 Font_metric::print_smob (SCM s, SCM port, scm_print_state *)
116 Font_metric *m = unsmob_metrics (s);
117 scm_puts ("#<Font_metric ", port);
118 scm_write (m->description_, port);
119 scm_puts (">", port);
125 IMPLEMENT_SMOBS (Font_metric);
126 IMPLEMENT_DEFAULT_EQUAL_P (Font_metric);
127 IMPLEMENT_TYPE_P (Font_metric, "font-metric?");
130 Font_metric::find_by_name (String) const
138 ly_find_glyph_by_name (SCM font, SCM name)
140 if (!unsmob_metrics (font) || !gh_string_p (name))
142 warning ("ly-find-glyph-by-name: invalid argument.");
144 return m.smobbed_copy ();
147 return unsmob_metrics (font)->find_by_name (ly_scm2string (name)).smobbed_copy ();
152 ly_text_dimension (SCM font, SCM text)
156 if (!unsmob_metrics (font) || !gh_string_p(text))
158 warning ("ly-find-glyph-by-name: invalid argument.");
160 return m.smobbed_copy ();
164 b = unsmob_metrics (font)->text_dimension (ly_scm2string (text));
167 return gh_cons (ly_interval2scm (b[X_AXIS]), ly_interval2scm(b[Y_AXIS]));
174 scm_c_define_gsubr ("ly-find-glyph-by-name", 2 , 0, 0,
175 (Scheme_function_unknown) ly_find_glyph_by_name);
176 scm_c_define_gsubr ("ly-text-dimension", 2 , 0, 0,
177 (Scheme_function_unknown) ly_text_dimension);
180 ADD_SCM_INIT_FUNC (font_metric_init, font_metric_init);