2 modified-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>
12 #include "modified-font-metric.hh"
16 Modified_font_metric::Modified_font_metric (String input_encoding,
17 Font_metric *m, Real magn)
21 THIS.coding_scheme == input encoding.
22 ORIG.coding_scheme () == font_encoding.
24 encoding is hairy enough by itself, should fix treacherous naming. */
26 coding_scheme_ = input_encoding;
28 coding_vector_ = SCM_EOL;
29 coding_mapping_ = SCM_EOL;
30 coding_table_ = SCM_EOL;
31 coding_description_ = SCM_EOL;
33 magnification_ = magn;
35 SCM desc = m->description_;
37 Real total_mag = magn * scm_to_double (scm_cdr (desc));
40 description_ = scm_cons (scm_car (desc), scm_make_real (total_mag));
43 if (coding_scheme_ != ""
44 && coding_scheme_ != "TeX"
45 && coding_scheme_ != "ASCII"
46 && coding_scheme_ != orig_->coding_scheme ())
49 = scm_call_1 (ly_scheme_function ("get-coding-vector"),
50 scm_makfrom0str (coding_scheme_.to_str0 ()));
52 if (!ly_c_vector_p (coding_vector_))
54 programming_error ("get-coding-vector should return vector");
55 coding_vector_ = scm_c_make_vector (256, ly_symbol2scm (".notdef"));
59 = scm_call_1 (ly_scheme_function ("get-coding-table"),
60 scm_makfrom0str (orig_->coding_scheme ().to_str0 ()));
63 = scm_call_2 (ly_scheme_function ("make-encoding-mapping"),
67 coding_description_ = SCM_EOL;
69 = scm_acons (ly_symbol2scm ("input-name"),
70 scm_makfrom0str (coding_scheme_.to_str0 ()),
73 = scm_acons (ly_symbol2scm ("input-vector"),
74 coding_vector_, coding_description_);
76 = scm_acons (ly_symbol2scm ("output-name"),
77 scm_makfrom0str (orig_->coding_scheme ().to_str0 ()),
80 = scm_acons (ly_symbol2scm ("output-table"),
84 = scm_acons (ly_symbol2scm ("char-mapping"),
92 LY_DEFINE (ly_font_encoding_alist, "ly:font-encoding-alist",
95 "Given the Modified_font_metric @var{font}, return an "
96 "alist. Keys are input-name, input-vector, "
97 "output-name, output-table, mapping.")
99 Modified_font_metric *fm
100 = dynamic_cast<Modified_font_metric*> (unsmob_metrics (font));
102 SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "Modified_font_metric");
103 return fm->coding_description_;
107 Modified_font_metric::make_scaled_font_metric (SCM coding,
108 Font_metric *m, Real s)
113 if (scm_is_symbol (coding))
114 coding = scm_symbol_to_string (coding);
116 String scheme = scm_is_string (coding) ? ly_scm2string (coding) : "";
118 Modified_font_metric *sfm = new Modified_font_metric (scheme, m, s);
120 return sfm->self_scm ();
124 Modified_font_metric::design_size () const
126 return orig_->design_size ();
131 Modified_font_metric::get_indexed_char (int i) const
133 Box b = orig_->get_indexed_char (i);
134 b.scale (magnification_);
139 Modified_font_metric::get_ascii_char (int i) const
141 Box b = orig_->get_ascii_char (i);
142 b.scale (magnification_);
147 Modified_font_metric::count () const
149 return orig_->count ();
153 Modified_font_metric::get_indexed_wxwy (int k) const
155 Offset o = orig_->get_indexed_wxwy (k);
156 return o * magnification_;
160 Modified_font_metric::name_to_index (String s) const
162 return orig_->name_to_index (s);
166 Modified_font_metric::index_to_ascii (int k) const
168 return orig_->index_to_ascii (k);
172 Modified_font_metric::coding_scheme () const
174 return coding_scheme_;
178 Modified_font_metric::derived_mark () const
180 scm_gc_mark (coding_vector_);
181 scm_gc_mark (coding_description_);
182 scm_gc_mark (coding_table_);
183 scm_gc_mark (coding_mapping_);
186 /* TODO: put this klutchness behind ly:option switch. */
188 Modified_font_metric::tex_kludge (String text) const
192 for (int i = 0; i < text.length (); i++)
197 /* Accent marks use width of base letter */
198 if (i +1 < text.length ())
200 if (text[i+1]=='\'' || text[i+1]=='`' || text[i+1]=='"'
206 /* For string width \\ is a \ and \_ is a _. */
207 if (text[i+1]=='\\' || text[i+1]=='_')
211 for (i++; (i < text.length ()) && !isspace (text[i])
212 && text[i]!='{' && text[i]!='}'; i++)
215 /* Compensate for the auto-increment in the outer loop. */
219 case '{': // Skip '{' and '}'
224 Box b = get_ascii_char ((unsigned char)text[i]);
226 /* Use the width of 'x' for unknown characters */
227 if (b[X_AXIS].length () == 0)
228 b = get_ascii_char ((unsigned char)'x');
230 w += b[X_AXIS].length ();
231 ydims.unite (b[Y_AXIS]);
236 if (ydims.is_empty ())
237 ydims = Interval (0, 0);
239 return Box (Interval (0, w), ydims);
243 Modified_font_metric::text_dimension (String text)
246 if (coding_scheme_ == "TeX")
247 b = tex_kludge (text);
248 else if (coding_scheme_ == "ASCII"
249 || coding_scheme_ == ""
250 || coding_scheme_ == orig_->coding_scheme ())
256 for (int i = 0; i < text.length (); i++)
258 Box b = get_ascii_char ((unsigned char)text[i]);
260 w += b[X_AXIS].length ();
261 ydims.unite (b[Y_AXIS]);
263 if (ydims.is_empty ())
264 ydims = Interval (0, 0);
266 b = Box(Interval(0,w), ydims);
273 for (int i = 0; i < text.length (); i++)
275 SCM sym = scm_vector_ref (coding_vector_,
276 scm_from_int((unsigned char) text[i]));
280 if (!scm_is_symbol (sym))
283 char const * chars = scm_i_string_chars (scm_symbol_to_string(sym));
285 int idx = orig_->name_to_index (chars);
287 char_box = orig_->get_indexed_char (idx);
289 char_box.scale (magnification_);
290 if (!char_box[X_AXIS].is_empty ())
292 w += char_box[X_AXIS][RIGHT];
294 ydims.unite (char_box[Y_AXIS]);
297 if (ydims.is_empty ())
298 ydims = Interval (0, 0);
300 b = Box (Interval (0, w), ydims);
307 Modified_font_metric::original_font () const