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_permutation_ = 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 * gh_scm2double (ly_cdr (desc));
32 description_ = gh_cons (ly_car (desc), gh_double2scm (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 (!gh_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_permutation_ = scm_call_2 (ly_scheme_function ("make-encoding-permutation"),
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 ("coding-permutation"),
79 LY_DEFINE (ly_font_encoding, "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, permutation.")
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)
96 String scheme = ly_scm2string (coding);
98 Modified_font_metric *sfm = new Modified_font_metric (scheme, m, s);
100 return sfm->self_scm ();
104 Modified_font_metric::design_size () const
106 return orig_->design_size ();
111 Modified_font_metric::get_indexed_char (int i) const
113 Box b = orig_->get_indexed_char (i);
114 b.scale (magnification_);
119 Modified_font_metric::get_ascii_char (int i) const
121 Box b = orig_->get_ascii_char (i);
122 b.scale (magnification_);
127 Modified_font_metric::count () const
129 return orig_->count ();
133 Modified_font_metric::get_indexed_wxwy (int k) const
135 Offset o = orig_->get_indexed_wxwy (k);
136 return o * magnification_;
140 Modified_font_metric::name_to_index (String s) const
142 return orig_->name_to_index (s);
146 Modified_font_metric::index_to_ascii (int k) const
148 return orig_->index_to_ascii (k);
152 Modified_font_metric::coding_scheme () const
154 return coding_scheme_;
158 Modified_font_metric::derived_mark () const
160 scm_gc_mark (coding_vector_);
161 scm_gc_mark (coding_description_);
162 scm_gc_mark (coding_table_);
163 scm_gc_mark (coding_permutation_);
167 Modified_font_metric::tex_kludge (String text) const
173 TODO: put this klutchness behind ly:option switch.
175 for (int i = 0; i < text.length (); i++)
180 // accent marks use width of base letter
181 if (i +1 < text.length ())
183 if (text[i+1]=='\'' || text[i+1]=='`' || text[i+1]=='"' ||
189 // for string width \\ is a \ and \_ is a _.
190 if (text[i+1]=='\\' || text[i+1]=='_')
196 for (i++; (i < text.length ()) && !isspace (text[i])
197 && text[i]!='{' && text[i]!='}'; i++)
201 i--; // Compensate for the increment in the outer loop!
203 case '{': // Skip '{' and '}'
208 Box b = get_ascii_char ((unsigned char)text[i]);
210 // Ugh, use the width of 'x' for unknown characters
211 if (b[X_AXIS].length () == 0)
212 b = get_ascii_char ((unsigned char)'x');
214 w += b[X_AXIS].length ();
215 ydims.unite (b[Y_AXIS]);
220 if (ydims.is_empty ())
221 ydims = Interval (0, 0);
223 return Box (Interval (0, w), ydims);
227 Modified_font_metric::text_dimension (String text)
230 if (coding_scheme_ == "TeX")
232 b = tex_kludge (text);
234 else if (coding_scheme_ == "ASCII"
235 || coding_scheme_ == orig_->coding_scheme ())
241 for (int i = 0; i < text.length (); i++)
243 Box b = get_ascii_char ((unsigned char)text[i]);
245 w += b[X_AXIS].length ();
246 ydims.unite (b[Y_AXIS]);
248 if (ydims.is_empty ())
249 ydims = Interval (0, 0);
251 b = Box(Interval(0,w), ydims);
258 for (int i = 0; i < text.length (); i++)
260 SCM sym = scm_vector_ref (coding_vector_,
261 SCM_MAKINUM((unsigned char) text[i]));
265 if (!gh_symbol_p (sym))
268 char const * chars = SCM_SYMBOL_CHARS(sym);
270 int idx = orig_->name_to_index (chars);
273 char_box = orig_->get_indexed_char (idx);
276 if (!char_box[X_AXIS].is_empty ())
277 w += char_box[X_AXIS][RIGHT]; // length ?
279 ydims.unite (char_box[Y_AXIS]);
282 if (ydims.is_empty ())
283 ydims = Interval (0, 0);
286 b = Box (Interval (0, w), ydims);
289 b.scale (magnification_);