2 scaled-font-metric.cc -- declare Modified_font_metric
4 source file of the GNU LilyPond music typesetter
6 (c) 1999--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
13 #include "scaled-font-metric.hh"
17 Modified_font_metric::Modified_font_metric (String coding, Font_metric* m, Real magn)
19 coding_vector_ = SCM_EOL;
20 coding_mapping_ = SCM_EOL;
21 coding_table_ = SCM_EOL;
22 coding_description_ = SCM_EOL;
24 coding_scheme_ = coding;
25 magnification_ = magn;
27 SCM desc = m->description_;
29 Real total_mag = magn * ly_scm2double (ly_cdr (desc));
32 description_ = scm_cons (ly_car (desc), scm_make_real (total_mag));
35 if (coding_scheme_ != "TeX"
36 && coding_scheme_ != "ASCII"
37 && coding_scheme_ != orig_->coding_scheme ())
39 coding_vector_ = scm_call_1 (ly_scheme_function ("get-coding-vector"),
40 scm_makfrom0str (coding_scheme_.to_str0 ()));
42 if (!ly_c_vector_p (coding_vector_))
44 programming_error ("get-coding-vector should return vector");
45 coding_vector_ = scm_c_make_vector (256, ly_symbol2scm (".notdef"));
49 coding_table_ = scm_call_1 (ly_scheme_function ("get-coding-table"),
50 scm_makfrom0str (orig_->coding_scheme ().to_str0 ()));
52 coding_mapping_ = scm_call_2 (ly_scheme_function ("make-encoding-mapping"),
56 coding_description_= SCM_EOL;
58 coding_description_ = scm_acons (ly_symbol2scm ("input-name"),
59 scm_makfrom0str (coding_scheme_.to_str0 ()),
62 coding_description_ = scm_acons (ly_symbol2scm ("input-vector"),
63 coding_vector_, coding_description_);
64 coding_description_ = scm_acons (ly_symbol2scm ("output-name"),
65 scm_makfrom0str (orig_->coding_scheme ().to_str0 ()),
67 coding_description_ = scm_acons (ly_symbol2scm ("output-table"),
71 coding_description_ = scm_acons (ly_symbol2scm ("char-mapping"),
79 LY_DEFINE (ly_font_encoding_alist, "ly:font-encoding-alist",
82 "Given the Modified_font_metric @var{font}, return an "
83 "alist. Keys are input-name, input-vector, "
84 "output-name, output-table, mapping.")
86 Modified_font_metric *fm
87 = dynamic_cast<Modified_font_metric*> (unsmob_metrics (font));
89 SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "Modified_font_metric");
90 return fm->coding_description_;
94 Modified_font_metric::make_scaled_font_metric (SCM coding, Font_metric *m, Real s)
99 if (ly_c_symbol_p (coding))
100 coding = scm_symbol_to_string (coding);
102 String scheme = ly_scm2string (coding);
104 Modified_font_metric *sfm = new Modified_font_metric (scheme, m, s);
106 return sfm->self_scm ();
110 Modified_font_metric::design_size () const
112 return orig_->design_size ();
117 Modified_font_metric::get_indexed_char (int i) const
119 Box b = orig_->get_indexed_char (i);
120 b.scale (magnification_);
125 Modified_font_metric::get_ascii_char (int i) const
127 Box b = orig_->get_ascii_char (i);
128 b.scale (magnification_);
133 Modified_font_metric::count () const
135 return orig_->count ();
139 Modified_font_metric::get_indexed_wxwy (int k) const
141 Offset o = orig_->get_indexed_wxwy (k);
142 return o * magnification_;
146 Modified_font_metric::name_to_index (String s) const
148 return orig_->name_to_index (s);
152 Modified_font_metric::index_to_ascii (int k) const
154 return orig_->index_to_ascii (k);
158 Modified_font_metric::coding_scheme () const
160 return coding_scheme_;
164 Modified_font_metric::derived_mark () const
166 scm_gc_mark (coding_vector_);
167 scm_gc_mark (coding_description_);
168 scm_gc_mark (coding_table_);
169 scm_gc_mark (coding_mapping_);
173 Modified_font_metric::tex_kludge (String text) const
179 TODO: put this klutchness behind ly:option switch.
181 for (int i = 0; i < text.length (); i++)
186 // accent marks use width of base letter
187 if (i +1 < text.length ())
189 if (text[i+1]=='\'' || text[i+1]=='`' || text[i+1]=='"' ||
195 // for string width \\ is a \ and \_ is a _.
196 if (text[i+1]=='\\' || text[i+1]=='_')
202 for (i++; (i < text.length ()) && !isspace (text[i])
203 && text[i]!='{' && text[i]!='}'; i++)
207 i--; // Compensate for the increment in the outer loop!
209 case '{': // Skip '{' and '}'
214 Box b = get_ascii_char ((unsigned char)text[i]);
216 // Ugh, use the width of 'x' for unknown characters
217 if (b[X_AXIS].length () == 0)
218 b = get_ascii_char ((unsigned char)'x');
220 w += b[X_AXIS].length ();
221 ydims.unite (b[Y_AXIS]);
226 if (ydims.is_empty ())
227 ydims = Interval (0, 0);
229 return Box (Interval (0, w), ydims);
233 Modified_font_metric::text_dimension (String text)
236 if (coding_scheme_ == "TeX")
238 b = tex_kludge (text);
240 else if (coding_scheme_ == "ASCII"
241 || coding_scheme_ == orig_->coding_scheme ())
247 for (int i = 0; i < text.length (); i++)
249 Box b = get_ascii_char ((unsigned char)text[i]);
251 w += b[X_AXIS].length ();
252 ydims.unite (b[Y_AXIS]);
254 if (ydims.is_empty ())
255 ydims = Interval (0, 0);
257 b = Box(Interval(0,w), ydims);
264 for (int i = 0; i < text.length (); i++)
266 SCM sym = scm_vector_ref (coding_vector_,
267 scm_from_int((unsigned char) text[i]));
271 if (!ly_c_symbol_p (sym))
274 char const * chars = SCM_SYMBOL_CHARS(sym);
276 int idx = orig_->name_to_index (chars);
279 char_box = orig_->get_indexed_char (idx);
282 char_box.scale (magnification_);
283 if (!char_box[X_AXIS].is_empty ())
284 w += char_box[X_AXIS][RIGHT]; // length ?
286 ydims.unite (char_box[Y_AXIS]);
289 if (ydims.is_empty ())
290 ydims = Interval (0, 0);
293 b = Box (Interval (0, w), ydims);
300 Modified_font_metric::original_font () const