2 lookup.cc -- implement simple Lookup methods.
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--1998 Han-Wen Nienhuys <hanwen@cs.uu.nl>
8 Jan Nieuwenhuizen <janneke@gnu.org>
11 Read spacing info from AFMs
17 #include "dimensions.hh"
18 #include "symtable.hh"
20 #include "paper-def.hh"
21 #include "string-convert.hh"
27 symtables_p_ = new Symtables;
31 Lookup::Lookup (Lookup const& s)
34 font_path_ = s.font_path_;
35 paper_l_ = s.paper_l_;
36 symtables_p_ = new Symtables (*s.symtables_p_);
40 Lookup::Lookup (Symtables const& s)
43 font_path_ = s.font_path_;
45 symtables_p_ = new Symtables (s);
56 Lookup::accidental (int j) const
58 return afm_find (String ("accidentals") + String ("-") + to_str (j));
62 Lookup::add (String s, Symtable*p)
64 symtables_p_->add (s, p);
68 Lookup::afm_find (String s, String str) const
72 *mlog << "[" << font_path_;
73 ( (Lookup*)this)->afm_p_ = new Adobe_font_metric (read_afm (font_path_));
74 *mlog << "]" << flush ;
75 DOUT << this->afm_p_->str ();
77 Adobe_font_char_metric m = afm_p_->find_char (s);
84 a.dim_[X_AXIS] *= 1 / 1000.0;
85 a.dim_[Y_AXIS] *= 1 / 1000.0;
86 a.str_ = String_convert::form_str (str.ch_C (), m.code ());
92 Lookup::ball (int j) const
97 return afm_find (String ("balls") + String ("-") + to_str (j));
101 Lookup::bar (String str, Real h) const
104 a.push (print_dimen (h));
105 Atom s = (*symtables_p_) ("bars")->lookup (str);
106 s.str_ = substitute_args (s.str_, a);
107 s.dim_.y () = Interval (-h/2, h/2);
113 Lookup::base_output_str () const
116 String str = paper_l_->get_default_output ();
120 str = default_outname_base_global;
121 int def = paper_l_->get_next_default_count ();
123 str += "-" + to_str (def);
129 Lookup::beam (Real slope, Real width, Real thick) const
131 Atom a (ps_beam (slope, width, thick));
132 Real height = slope * width;
133 Real min_y = (0 <? height) - thick/2;
134 Real max_y = (0 >? height) + thick/2;
136 a.dim_[X_AXIS] = Interval (0, width);
137 a.dim_[Y_AXIS] = Interval (min_y, max_y);
142 Lookup::character_str (int i) const
148 Lookup::clef (String st) const
150 return afm_find (String ("clefs") + String ("-") + st);
154 Lookup::dashed_slur (Array<Offset> controls, Real thick, Real dash) const
156 assert (controls.size () == 8);
158 Real dx = controls[3].x () - controls[0].x ();
159 Real dy = controls[3].y () - controls[0].y ();
163 a.dim_[X_AXIS] = Interval (0, dx);
164 a.dim_[Y_AXIS] = Interval (0 <? dy, 0 >? dy);
168 for (int i = 1; i < 4; i++)
169 ps += String_convert::double_str (controls[i].x ()) + " "
170 + String_convert::double_str (controls[i].y ()) + " ";
172 ps += String_convert::double_str (controls[0].x ()) + " "
173 + String_convert::double_str (controls[0].y ()) + " ";
175 ps += String_convert::double_str (thick) + " ";
176 Real on = dash > 1? thick * dash - thick : 0;
177 Real off = 2 * thick;
178 ps += "[" + String_convert::double_str (on) + " ";
179 ps += String_convert::double_str (off) + "] ";
180 ps += String_convert::int_str (0) + " ";
181 ps += "draw_dashed_slur ";
186 a.dim_[X_AXIS] = Interval (0, dx);
187 a.dim_[Y_AXIS] = Interval (0 <? dy, 0 >? dy);
193 SCM list_scm = gh_eval_str ("list");
194 // ((0.1 0.2) (1.1 1.2) (2.1 2.2) (3.1 3.2))
196 gh_cons (gh_call2 (list_scm, gh_double2scm (controls[0].x ()), gh_double2scm (controls[0].y ())),
197 gh_cons (gh_call2 (list_scm, gh_double2scm (controls[1].x ()), gh_double2scm (controls[1].y ())),
198 gh_cons (gh_call2 (list_scm, gh_double2scm (controls[2].x ()), gh_double2scm (controls[2].y ())),
199 gh_cons (gh_call2 (list_scm, gh_double2scm (controls[3].x ()), gh_double2scm (controls[3].y ())),
203 gh_display (controls_scm);
206 SCM append_scm = gh_eval_str ("append");
208 // (dashed-slur o ((0.1 0.2) (1.1 1.2) (2.1 2.2) (3.1 3.2)))
209 SCM dashed_slur_scm =
210 gh_call1 (gh_eval_str ("list"),
211 gh_call2 (append_scm,
212 gh_eval_str ("cons '(dashed-slur o)"),
213 gh_call1 (gh_eval_str ("list"), controls_scm)));
214 gh_display (dashed_slur_scm);
217 // (lambda (o) dashed-slur o ((0.1 0.2) (1.1 1.2) (2.1 2.2) (3.1 3.2)))
219 gh_call2 (append_scm, gh_eval_str ("'(lambda (o))"), dashed_slur_scm);
220 gh_display (a.lambda_);
229 Lookup::dots () const
231 return afm_find (String ("dots") + String ("-") + String ("dot"));
235 Lookup::dynamic (String st) const
237 return (*symtables_p_) ("dynamics")->lookup (st);
241 Lookup::fill (Box b) const
249 Lookup::flag (int j, Direction d) const
251 char c = (d == UP) ? 'u' : 'd';
252 return afm_find (String ("flags") + String ("-") + to_str (c) + to_str (j));
256 Lookup::print () const
259 DOUT << "Lookup {\n";
260 symtables_p_->print ();
266 Lookup::print_dimen (Real r) const
268 String s = to_str (r, "%.3f");
269 if (s.index_i ("NaN") != -1)
278 Lookup::rest (int j, bool o) const
280 return afm_find (String ("rests")
281 + String ("-") + to_str (j) + (o ? "o" : ""));
285 Lookup::rule_symbol (Real height, Real width) const
287 Atom bs= (*symtables_p_) ("param")->lookup ("rule");
289 args.push (print_dimen (height));
290 args.push (print_dimen (width));
291 bs.str_ = substitute_args (bs.str_, args);
292 bs.dim_.x () = Interval (0, width);
293 bs.dim_.y () = Interval (0, height);
298 Lookup::script (String str) const
300 return afm_find (String ("scripts") + String ("-") + str);
304 Lookup::special_time_signature (String s, Array<Scalar> arr) const
306 String symbolname = "timesig-"+s+"%/%";
307 Atom a (afm_find (substitute_args (symbolname, arr)));
310 // Try if the full name was given
311 a = afm_find ("timesig-"+s);
314 // Resort to default layout with numbers
315 return time_signature (arr);
319 substitute_arg (String& r, String arg)
321 int p = r.index_i ('%');
325 r = r.left_str (p) + arg + r.right_str (r.length_i () - p - 1);
329 Lookup::substitute_args (String source, Array<String> args) const
332 for (int i = 0 ; i < args.size (); i++)
333 substitute_arg (str, args[i]);
338 Lookup::substitute_args (String source, Array<Scalar> args) const
341 for (int i = 0 ; i < args.size (); i++)
343 return substitute_args (source, sv);
347 Lookup::stem (Real y1, Real y2, String str) const
357 s.dim_.x () = Interval (0,0);
358 s.dim_.y () = Interval (y1,y2);
362 Real stem_width = paper_l_->get_var ("stemthickness");
363 a.push (print_dimen (-stem_width /2));
364 a.push (print_dimen (stem_width));
365 a.push (print_dimen (y2));
366 a.push (print_dimen (-y1));
368 s.str_ = substitute_args (str, a);
374 Lookup::streepje (int type) const
379 return afm_find ("balls" + String ("-") +to_str (type) + "l");
383 Lookup::text (String style, String text) const
388 Atom s = (*symtables_p_) ("style")->lookup (style);
389 s.str_ = substitute_args (s.str_,a);
396 Lookup::time_signature (Array<Scalar> a) const
398 Atom s ((*symtables_p_) ("param")->lookup ("time_signature"));
399 s.str_ = substitute_args (s.str_, a);
405 should be handled via Tex_ code and Lookup::bar ()
408 Lookup::vbrace (Real &y) const
410 Atom brace = (*symtables_p_) ("param")->lookup ( "brace");
411 Interval ydims = brace.dim_[Y_AXIS];
412 Real min_y = ydims[LEFT];
413 Real max_y = ydims[RIGHT];
418 warning (_ ("piano brace")
419 + " " + _ ("too small") + " (" + print_dimen (y) + ")");
424 warning (_ ("piano brace")
425 + " " + _ ("too big") + " (" + print_dimen (y) + ")");
430 int idx = int (rint ( (y- min_y)/step)) + 1;
434 a.push (character_str (idx));
435 brace.str_ = substitute_args (brace.str_,a);
436 brace.dim_[Y_AXIS] = Interval (-y/2,y/2);