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
18 #include "dimensions.hh"
19 #include "symtable.hh"
21 #include "paper-def.hh"
22 #include "string-convert.hh"
23 #include "file-path.hh"
25 #include "lily-guile.hh"
31 symtables_p_ = new Symtables;
35 Lookup::Lookup (Lookup const& s)
38 font_path_ = s.font_path_;
39 paper_l_ = s.paper_l_;
40 symtables_p_ = new Symtables (*s.symtables_p_);
44 Lookup::Lookup (Symtables const& s)
47 font_path_ = s.font_path_;
49 symtables_p_ = new Symtables (s);
60 Lookup::accidental (int j, bool cautionary) const
62 Molecule m(afm_find (String ("accidentals") + String ("-") + to_str (j)));
65 m.add_at_edge(X_AXIS, LEFT,
66 Molecule(afm_find (String ("accidentals") + String ("-("))))
68 m.add_at_edge(X_AXIS, RIGHT,
69 Molecule(afm_find (String ("accidentals") + String ("-)"))))
76 Lookup::add (String s, Symtable*p)
78 symtables_p_->add (s, p);
82 Lookup::afm_find (String s, bool warn) const
86 *mlog << "[" << font_path_;
87 ( (Lookup*)this)->afm_p_ = new Adobe_font_metric (read_afm (font_path_));
88 *mlog << "]" << flush ;
89 DOUT << this->afm_p_->str ();
91 Adobe_font_char_metric m = afm_p_->find_char (s, warn);
98 a.dim_[X_AXIS] *= 1 / 1000.0;
99 a.dim_[Y_AXIS] *= 1 / 1000.0;
101 arr.push (m.code ());
102 a.lambda_ = (lambda_scm ("char", arr));
103 a.str_ = "afm_find: " + s;
109 Lookup::ball (int j) const
114 return afm_find (String ("balls") + String ("-") + to_str (j));
118 Lookup::bar (String str, Real h) const
122 Atom a = (*symtables_p_) ("bars")->lookup (str);
123 a.lambda_ = (lambda_scm (a.str_, arr));
125 a.dim_.y () = Interval (-h/2, h/2);
131 Lookup::beam (Real slope, Real width, Real thick) const
133 Real height = slope * width;
134 Real min_y = (0 <? height) - thick/2;
135 Real max_y = (0 >? height) + thick/2;
143 a.lambda_ = (lambda_scm ("beam", arr));
145 a.dim_[X_AXIS] = Interval (0, width);
146 a.dim_[Y_AXIS] = Interval (min_y, max_y);
151 Lookup::clef (String st) const
153 return afm_find (String ("clefs") + String ("-") + st);
157 offset2scm (Offset o)
159 return gh_list (gh_double2scm (o[X_AXIS]), gh_double2scm(o[Y_AXIS]), SCM_UNDEFINED);
163 Lookup::dashed_slur (Array<Offset> controls, Real thick, Real dash) const
165 assert (controls.size () == 8);
166 Offset d = controls[3] - controls[0];
173 a.dim_[X_AXIS] = Interval (0, dx);
174 a.dim_[Y_AXIS] = Interval (0 <? dy, 0 >? dy);
177 for (int i=0; i< 4; i++)
179 sc[i] = offset2scm (controls[i]);
182 // (lambda (o) (dashed-slur o thick dash '(stuff))
184 ly_append (ly_lambda_o (),
185 gh_list (ly_append (ly_func_o ("dashed-slur"),
186 gh_cons (gh_double2scm (thick),
187 gh_cons (gh_double2scm (dash),
188 gh_list (ly_quote_scm (gh_list (sc[1], sc[2], sc[3], sc[0], SCM_UNDEFINED)), SCM_UNDEFINED)))), SCM_UNDEFINED));
190 a.str_ = "dashed_slur";
195 Lookup::dots () const
197 return afm_find (String ("dots") + String ("-") + String ("dot"));
201 Lookup::dynamic (String st) const
203 return (*symtables_p_) ("dynamics")->lookup (st);
207 Lookup::fill (Box b) const
215 Lookup::flag (int j, Direction d) const
217 char c = (d == UP) ? 'u' : 'd';
218 Atom a = afm_find (String ("flags") + String ("-") + to_str (c) + to_str (j));
223 Lookup::print () const
226 DOUT << "Lookup {\n";
227 symtables_p_->print ();
233 Lookup::rest (int j, bool o) const
235 return afm_find (String ("rests")
236 + String ("-") + to_str (j) + (o ? "o" : ""));
240 Lookup::rule_symbol (Real height, Real width) const
242 Atom a = (*symtables_p_) ("param")->lookup ("rule");
246 a.lambda_ = (lambda_scm (a.str_, arr));
247 a.str_ = "rule_symbol";
248 a.dim_.x () = Interval (0, width);
249 a.dim_.y () = Interval (0, height);
254 Lookup::script (String str) const
256 return afm_find (String ("scripts") + String ("-") + str);
260 Lookup::special_time_signature (String s, Array<int> arr) const
262 // First guess: s contains only the signature style
263 assert (arr.size () >1);
264 String symbolname = "timesig-" + s + to_str (arr[0]) + "/" + to_str (arr[1]);
266 Atom a = afm_find (symbolname, false);
270 // Second guess: s contains the full signature name
271 a = afm_find ("timesig-"+s, false);
275 // Resort to default layout with numbers
276 return time_signature (arr);
280 Lookup::stem (Real y1, Real y2) const
290 a.dim_.x () = Interval (0,0);
291 a.dim_.y () = Interval (y1,y2);
295 Real stem_width = paper_l_->get_var ("stemthickness");
296 arr.push (-stem_width /2);
297 arr.push (stem_width);
301 a.lambda_ = (lambda_scm ("stem", arr));
308 Lookup::streepje (int type) const
313 return afm_find ("balls" + String ("-") +to_str (type) + "l");
316 Dictionary<String> cmr_dict;
317 Dictionary<Adobe_font_metric*> afm_p_dict;
320 Lookup::text (String style, String text) const
325 Atom a = (*symtables_p_) ("style")->lookup (style);
326 a.lambda_ = lambda_scm (a.str_, arr);
327 Real font_w = a.dim_.x ().length ();
328 Real font_h = a.dim_.y ().length ();
331 // if (!cmr_dict.length_i ())
332 if (!cmr_dict.elem_b ("roman"))
335 cmr_dict.elem ("bold") = "cmbx";
336 cmr_dict.elem ("dynamic") = "feta-din";
337 cmr_dict.elem ("finger") = "feta-nummer";
338 cmr_dict.elem ("italic") = "cmti";
339 cmr_dict.elem ("roman") = "cmr";
342 if (!afm_p_dict.elem_b (style))
344 Adobe_font_metric* afm_p = 0;
345 String cmr_str = cmr_dict.elem (style) + to_str ((int) font_h) + ".afm";
346 String font_path = global_path.find (cmr_str);
347 if (!font_path.length_i ())
349 warning (_f("can't open file: `%s'", cmr_str.ch_C ()));
350 warning (_f("guessing dimensions for font style: `%s'", style.ch_C ()));
354 *mlog << "[" << font_path;
355 afm_p = new Adobe_font_metric (read_afm (font_path));
356 DOUT << afm_p->str ();
357 *mlog << "]" << flush ;
359 afm_p_dict.elem (style) = afm_p;
362 Adobe_font_metric* afm_p = afm_p_dict.elem (style);
364 for (int i = 0; i < text.length_i (); i++)
367 for (i++; (i < text.length_i ()) && isalpha(text[i]); i++)
373 Adobe_font_char_metric m = afm_p->char_metrics_[(int)text[i]];
374 w += m.B_.x ().length ();
375 DOUT << to_str (m.B_.x ().length ()) << " ";
381 DOUT << "\n" << to_str (w) << "\n";
382 a.dim_.x () = Interval (0, w);
390 Lookup::time_signature (Array<int> a) const
392 Atom s ((*symtables_p_) ("param")->lookup ("time_signature"));
393 s.lambda_ = (lambda_scm (s.str_, a));
399 should be handled via Tex_ code and Lookup::bar ()
402 Lookup::vbrace (Real &y) const
404 Atom a = (*symtables_p_) ("param")->lookup ( "brace");
405 Interval ydims = a.dim_[Y_AXIS];
406 Real min_y = ydims[LEFT];
407 Real max_y = ydims[RIGHT];
412 warning (_ ("piano brace")
413 + " " + _ ("too small") + " (" + print_dimen (y) + ")");
418 warning (_ ("piano brace")
419 + " " + _ ("too big") + " (" + print_dimen (y) + ")");
424 int idx = int (rint ( (y- min_y)/step)) + 1;
428 a.lambda_ = (lambda_scm (a.str_, arr));
430 a.dim_[Y_AXIS] = Interval (-y/2,y/2);
436 Lookup::hairpin (Real width, bool decresc, bool continued) const
439 Real height = paper_l_->staffheight_f () / 6;
441 ps += to_str (width) + " "
442 + to_str (height) + " "
443 + to_str (continued ? height/2 : 0) +
444 + " draw_" + String (decresc ? "de" : "") + "cresc\n";
446 a.dim_.x () = Interval (0, width);
447 a.dim_.y () = Interval (-2*height, 2*height);
453 Lookup::plet (Real dy , Real dx, Direction dir) const
457 ps += String_convert::double_str (dx) + " "
458 + String_convert::double_str (dy) + " "
459 + String_convert::int_str ( (int)dir) +
468 Lookup::slur (Array<Offset> controls) const
470 assert (controls.size () == 8);
474 Real dx = controls[3].x () - controls[0].x ();
475 Real dy = controls[3].y () - controls[0].y ();
479 int indices[] = {5,6,7,4,1,2,3,0};
481 for (int i= 0; i < 8; i++)
482 scontrols[i] = offset2scm (controls[indices[i]]);
486 gh_append2 (ly_lambda_o (),
487 gh_list (gh_append2 (ly_func_o ("slur"),
488 gh_list (ly_quote_scm (gh_list (scontrols[0],
505 a.dim_[X_AXIS] = Interval (0, dx);
506 a.dim_[Y_AXIS] = Interval (0 <? dy, 0 >? dy);
512 Lookup::vbracket (Real &y) const
515 Real min_y = paper_l_->staffheight_f ();
518 warning (_ ("bracket")
519 + " " + _ ("too small") + " (" + print_dimen (y) + ")");
524 a.lambda_ = (lambda_scm ("bracket", arr));
526 a.dim_[Y_AXIS] = Interval (-y/2,y/2);
527 a.dim_[X_AXIS] = Interval (0,4 PT);