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 ());
104 a.lambda_ = gh_list (ly_symbol ("char"),
105 gh_int2scm (m.code ()),
107 a.str_ = "afm_find: " + s;
113 Lookup::ball (int j) const
118 return afm_find (String ("balls") + String ("-") + to_str (j));
122 Lookup::bar (String str, Real h) const
126 Atom a = (*symtables_p_) ("bars")->lookup (str);
129 a.lambda_ = gh_list (ly_symbol (a.str_.ch_C()),
134 a.dim_.y () = Interval (-h/2, h/2);
140 Lookup::beam (Real slope, Real width, Real thick) const
142 Real height = slope * width;
143 Real min_y = (0 <? height) - thick/2;
144 Real max_y = (0 >? height) + thick/2;
152 a.lambda_ = (lambda_scm ("beam", arr));
154 a.dim_[X_AXIS] = Interval (0, width);
155 a.dim_[Y_AXIS] = Interval (min_y, max_y);
160 Lookup::clef (String st) const
162 return afm_find (String ("clefs") + String ("-") + st);
166 offset2scm (Offset o)
168 return gh_list (gh_double2scm (o[X_AXIS]), gh_double2scm(o[Y_AXIS]),
173 Lookup::dashed_slur (Array<Offset> controls, Real thick, Real dash) const
175 assert (controls.size () == 8);
176 Offset d = controls[3] - controls[0];
183 a.dim_[X_AXIS] = Interval (0, dx);
184 a.dim_[Y_AXIS] = Interval (0 <? dy, 0 >? dy);
187 for (int i=0; i< 4; i++)
189 sc[i] = offset2scm (controls[i]);
192 // (lambda (o) (dashed-slur o thick dash '(stuff))
194 gh_list (ly_symbol ("dashed-slur"),
195 gh_double2scm (thick),
196 gh_double2scm (dash),
197 gh_list (sc[1], sc[2], sc[3], sc[0], SCM_UNDEFINED),
200 a.str_ = "dashed_slur";
205 Lookup::dots () const
207 return afm_find (String ("dots") + String ("-") + String ("dot"));
211 Lookup::dynamic (String st) const
213 return (*symtables_p_) ("dynamics")->lookup (st);
217 Lookup::extender (Real width) const
219 Atom a = (*symtables_p_) ("param")->lookup ("extender");
222 a.lambda_ = (lambda_scm (a.str_, arr));
229 Lookup::fill (Box b) const
237 Lookup::flag (int j, Direction d) const
239 char c = (d == UP) ? 'u' : 'd';
240 Atom a = afm_find (String ("flags") + String ("-") + to_str (c) + to_str (j));
245 Lookup::print () const
248 DOUT << "Lookup {\n";
249 symtables_p_->print ();
255 Lookup::rest (int j, bool o) const
257 return afm_find (String ("rests")
258 + String ("-") + to_str (j) + (o ? "o" : ""));
262 Lookup::rule_symbol (Real height, Real width) const
265 a.lambda_ = gh_list (ly_symbol ("rulesym"),
266 gh_double2scm (height),
267 gh_double2scm (width),
269 a.dim_.x () = Interval (0, width);
270 a.dim_.y () = Interval (0, height);
275 Lookup::script (String str) const
277 return afm_find (String ("scripts") + String ("-") + str);
281 Lookup::special_time_signature (String s, Array<int> arr) const
283 // First guess: s contains only the signature style
284 assert (arr.size () >1);
285 String symbolname = "timesig-" + s + to_str (arr[0]) + "/" + to_str (arr[1]);
287 Atom a = afm_find (symbolname, false);
291 // Second guess: s contains the full signature name
292 a = afm_find ("timesig-"+s, false);
296 // Resort to default layout with numbers
297 return time_signature (arr);
301 Lookup::stem (Real y1, Real y2) const
311 a.dim_.x () = Interval (0,0);
312 a.dim_.y () = Interval (y1,y2);
316 Real stem_width = paper_l_->get_var ("stemthickness");
318 gh_list (ly_symbol ("stem"),
319 gh_double2scm(-stem_width /2),
320 gh_double2scm(stem_width),
330 Lookup::streepje (int type) const
335 return afm_find ("balls" + String ("-") +to_str (type) + "l");
338 Dictionary<String> cmr_dict;
339 Dictionary<Adobe_font_metric*> afm_p_dict;
342 Lookup::text (String style, String text) const
347 Atom a = (*symtables_p_) ("style")->lookup (style);
348 a.lambda_ = lambda_scm (a.str_, arr);
349 Real font_w = a.dim_.x ().length ();
350 Real font_h = a.dim_.y ().length ();
353 // if (!cmr_dict.length_i ())
354 if (!cmr_dict.elem_b ("roman"))
357 cmr_dict.elem ("bold") = "cmbx";
358 cmr_dict.elem ("dynamic") = "feta-din";
359 cmr_dict.elem ("finger") = "feta-nummer";
360 cmr_dict.elem ("italic") = "cmti";
361 cmr_dict.elem ("roman") = "cmr";
364 if (!afm_p_dict.elem_b (style))
366 Adobe_font_metric* afm_p = 0;
367 String cmr_str = cmr_dict.elem (style) + to_str ((int) font_h) + ".afm";
368 String font_path = global_path.find (cmr_str);
369 if (!font_path.length_i ())
371 warning (_f("can't open file: `%s'", cmr_str.ch_C ()));
372 warning (_f("guessing dimensions for font style: `%s'", style.ch_C ()));
376 *mlog << "[" << font_path;
377 afm_p = new Adobe_font_metric (read_afm (font_path));
378 DOUT << afm_p->str ();
379 *mlog << "]" << flush ;
381 afm_p_dict.elem (style) = afm_p;
384 Adobe_font_metric* afm_p = afm_p_dict.elem (style);
386 for (int i = 0; i < text.length_i (); i++)
389 for (i++; (i < text.length_i ()) && isalpha(text[i]); i++)
394 if (afm_p && ((c >= 0) && (c < afm_p->char_metrics_.size ())))
396 Adobe_font_char_metric m = afm_p->char_metrics_[c];
397 w += m.B_.x ().length ();
398 DOUT << to_str (m.B_.x ().length ()) << " ";
404 DOUT << "\n" << to_str (w) << "\n";
405 a.dim_.x () = Interval (0, w);
413 Lookup::time_signature (Array<int> a) const
415 Atom s ((*symtables_p_) ("param")->lookup ("time_signature"));
416 s.lambda_ = (lambda_scm (s.str_, a));
422 should be handled via Tex_ code and Lookup::bar ()
425 Lookup::vbrace (Real &y) const
427 Atom a = (*symtables_p_) ("param")->lookup ( "brace");
428 Interval ydims = a.dim_[Y_AXIS];
429 Real min_y = ydims[LEFT];
430 Real max_y = ydims[RIGHT];
435 warning (_ ("piano brace")
436 + " " + _ ("too small") + " (" + print_dimen (y) + ")");
441 warning (_ ("piano brace")
442 + " " + _ ("too big") + " (" + print_dimen (y) + ")");
447 int idx = int (rint ( (y- min_y)/step)) + 1;
451 a.lambda_ = (lambda_scm (a.str_, arr));
453 a.dim_[Y_AXIS] = Interval (-y/2,y/2);
459 Lookup::hairpin (Real width, bool decresc, bool continued) const
462 Real height = paper_l_->staffheight_f () / 6;
466 arr.push (continued ? height/2 : 0);
467 String hairpin = String (decresc ? "de" : "") + "crescendo\n";
468 a.lambda_ = lambda_scm (hairpin, arr);
470 a.dim_.x () = Interval (0, width);
471 a.dim_.y () = Interval (-2*height, 2*height);
477 Lookup::plet (Real dy , Real dx, Direction dir) const
484 a.lambda_ = (lambda_scm ("tuplet", arr));
490 array_to_list (SCM *a , int l)
493 for (int i= l; i--; )
495 list = gh_cons (a[i], list);
501 Lookup::slur (Array<Offset> controls) const
503 assert (controls.size () == 8);
504 Real dx = controls[3].x () - controls[0].x ();
505 Real dy = controls[3].y () - controls[0].y ();
509 int indices[] = {5,6,7,4,1,2,3,0};
511 for (int i= 0; i < 8; i++)
512 scontrols[i] = offset2scm (controls[indices[i]]);
515 a.lambda_ =gh_list (ly_symbol ("slur"),
516 array_to_list (scontrols, 8),
519 a.dim_[X_AXIS] = Interval (0, dx);
520 a.dim_[Y_AXIS] = Interval (0 <? dy, 0 >? dy);
526 Lookup::vbracket (Real &y) const
529 Real min_y = paper_l_->staffheight_f ();
532 warning (_ ("bracket")
533 + " " + _ ("too small") + " (" + print_dimen (y) + ")");
538 a.lambda_ = (lambda_scm ("bracket", arr));
540 a.dim_[Y_AXIS] = Interval (-y/2,y/2);
541 a.dim_[X_AXIS] = Interval (0,4 PT);
546 Lookup::volta (Real w, bool last_b) const
552 a.lambda_ = (lambda_scm ("volta", arr));