2 lookup.cc -- implement simple Lookup methods.
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--1999 Han-Wen Nienhuys <hanwen@cs.uu.nl>
8 Jan Nieuwenhuizen <janneke@gnu.org>
17 #include "dimensions.hh"
19 #include "paper-def.hh"
20 #include "string-convert.hh"
21 #include "file-path.hh"
23 #include "lily-guile.hh"
24 #include "all-fonts.hh"
27 #include "molecule.hh"
31 array_to_list (SCM *a , int l)
36 list = gh_cons (a[i], list);
48 Lookup::Lookup (Lookup const& s)
50 font_name_ = s.font_name_;
57 Lookup::ledger_line (Interval xwid) const
59 Molecule end (afm_find ("noteheads-ledgerending"));
60 Interval ed = end.dim_[X_AXIS];
61 xwid = Interval (xwid[LEFT] - ed[LEFT],
62 xwid[RIGHT] - ed[RIGHT]);
63 Molecule mid = filledbox (Box (xwid, end.dim_[Y_AXIS]));
68 e1.translate_axis (xwid[RIGHT], X_AXIS);
69 e2.translate_axis (xwid[LEFT], X_AXIS);
78 Lookup::accidental (int j, bool cautionary) const
80 Molecule m(afm_find (String ("accidentals-") + to_str (j)));
83 Molecule open = afm_find (String ("accidentals-("));
84 Molecule close = afm_find (String ("accidentals-)"));
85 m.add_at_edge(X_AXIS, LEFT, Molecule(open), 0);
86 m.add_at_edge(X_AXIS, RIGHT, Molecule(close), 0);
94 Lookup::afm_find (String s, bool warn) const
98 Lookup * me = (Lookup*)(this);
99 me->afm_l_ = all_fonts_global_p->find_afm (font_name_);
102 warning (_f("Can't open `%s'\n", font_name_));
103 warning (_f("Search path %s\n", global_path.str ().ch_C()));
104 error (_f("Aborting"));
107 Adobe_font_char_metric cm = afm_l_->find_char (s, warn);
112 Atom at (gh_list (ly_symbol ("char"),
113 gh_int2scm (cm.code ()),
115 at.font_ = ly_symbol (font_name_.ch_C());
116 m.dim_ = cm.dimensions();
122 Lookup::ball (int j) const
127 return afm_find (String ("noteheads-") + to_str (j));
131 Lookup::simple_bar (String type, Real h) const
133 SCM thick = ly_symbol ("barthick_" + type);
135 if (paper_l_->scope_p_->elem_b (thick))
137 w = paper_l_->get_realvar (thick);
140 return filledbox (Box (Interval(0,w), Interval(-h/2, h/2)));
145 Lookup::bar (String str, Real h) const
148 return staff_bracket (h);
150 return staff_brace (h);
152 Real kern = paper_l_->get_var ("bar_kern");
153 Real thinkern = paper_l_->get_var ("bar_thinkern");
154 Molecule thin = simple_bar ("thin", h);
155 Molecule thick = simple_bar ("thick", h);
156 Molecule colon = afm_find ("dots-repeatcolon");
162 return fill (Box (Interval(0, 0), Interval (-h/2, h/2)));
168 else if (str == "|.")
170 m.add_at_edge (X_AXIS, LEFT, thick, 0);
171 m.add_at_edge (X_AXIS, LEFT, thin, kern);
173 else if (str == ".|")
175 m.add_at_edge (X_AXIS, RIGHT, thick, 0);
176 m.add_at_edge (X_AXIS, RIGHT, thin, kern);
178 else if (str == ":|")
180 m.add_at_edge (X_AXIS, LEFT, thick, 0);
181 m.add_at_edge (X_AXIS, LEFT, thin, kern);
182 m.add_at_edge (X_AXIS, LEFT, colon, kern);
184 else if (str == "|:")
186 m.add_at_edge (X_AXIS, RIGHT, thick, 0);
187 m.add_at_edge (X_AXIS, RIGHT, thin, kern);
188 m.add_at_edge (X_AXIS, RIGHT, colon, kern);
190 else if (str == ":|:")
192 m.add_at_edge (X_AXIS, LEFT, thick, thinkern);
193 m.add_at_edge (X_AXIS, LEFT, colon, kern);
194 m.add_at_edge (X_AXIS, RIGHT, thick, kern);
195 m.add_at_edge (X_AXIS, RIGHT, colon, kern);
197 else if (str == ".|.")
199 m.add_at_edge (X_AXIS, LEFT, thick, thinkern);
200 m.add_at_edge (X_AXIS, RIGHT, thick, kern);
202 else if (str == "||")
204 m.add_at_edge (X_AXIS, RIGHT, thin, 0);
205 m.add_at_edge (X_AXIS, RIGHT, thin, thinkern);
212 Lookup::beam (Real slope, Real width, Real thick) const
214 Real height = slope * width;
215 Real min_y = (0 <? height) - thick/2;
216 Real max_y = (0 >? height) + thick/2;
221 (gh_list (ly_symbol ("beam"),
222 gh_double2scm (width),
223 gh_double2scm (slope),
224 gh_double2scm (thick),
227 m.dim_[X_AXIS] = Interval (0, width);
228 m.dim_[Y_AXIS] = Interval (min_y, max_y);
234 Lookup::clef (String st) const
236 return afm_find (String ("clefs-" + st));
240 offset2scm (Offset o)
242 return gh_list (gh_double2scm (o[X_AXIS]), gh_double2scm(o[Y_AXIS]),
247 Lookup::dashed_slur (Array<Offset> controls, Real thick, Real dash) const
249 assert (controls.size () == 8);
250 Offset d = controls[3] - controls[0];
258 m.dim_[X_AXIS] = Interval (0, dx);
259 m.dim_[Y_AXIS] = Interval (0 <? dy, 0 >? dy);
262 for (int i=0; i< 4; i++)
264 sc[i] = offset2scm (controls[i]);
268 (gh_list (ly_symbol ("dashed-slur"),
269 gh_double2scm (thick),
270 gh_double2scm (dash),
271 ly_quote_scm (array_to_list (sc, 4)),
280 Lookup::dots () const
282 return afm_find (String ("dots-dot"));
288 Lookup::fill (Box b) const
296 Lookup::flag (int j, Direction d) const
298 char c = (d == UP) ? 'u' : 'd';
299 return afm_find (String ("flags-") + to_str (c) + to_str (j));
303 Lookup::rest (int j, bool o) const
305 return afm_find (String ("rests-") + to_str (j) + (o ? "o" : ""));
309 Lookup::rule_symbol (Real height, Real width) const
311 Atom at (gh_list (ly_symbol ("rulesym"),
312 gh_double2scm (height),
313 gh_double2scm (width),
317 m.dim_.x () = Interval (0, width);
318 m.dim_.y () = Interval (0, height);
326 Lookup::script (String str) const
328 return afm_find (String ("scripts-") + str);
332 Lookup::special_time_signature (String s, int n, int d) const
334 // First guess: s contains only the signature style
335 String symbolname = "timesig-" + s + to_str (n) + "/" + to_str (d);
337 Molecule m = afm_find (symbolname, false);
338 if (!m.dim_[X_AXIS].empty_b ())
341 // Second guess: s contains the full signature name
342 m = afm_find ("timesig-"+s, false);
343 if (!m.dim_[X_AXIS].empty_b ())
346 // Resort to default layout with numbers
347 return time_signature (n,d);
351 Lookup::filledbox (Box b ) const
355 Atom at (gh_list (ly_symbol ("filledbox"),
356 gh_double2scm (-b[X_AXIS][LEFT]),
357 gh_double2scm (b[X_AXIS][RIGHT]),
358 gh_double2scm (-b[Y_AXIS][DOWN]),
359 gh_double2scm (b[Y_AXIS][UP]),
368 Lookup::stem (Real y1, Real y2) const
376 Real stem_width = paper_l_->get_var ("stemthickness");
377 return filledbox (Box (Interval (-stem_width/2,stem_width/2),
382 static Dict_initialiser<char const*> cmr_init[] = {
384 {"dynamic", "feta-din"},
385 {"finger", "feta-nummer"},
386 {"typewriter", "cmtt"},
391 {"mark", "feta-nummer"},
392 {"number", "feta-nummer"},
393 {"volta", "feta-nummer"},
397 // Magic numbers from Knuths plain.tex:
398 static Real mag_steps[] = {1, 1, 1.200, 1.440, 1.7280, 2.074, 2.488};
400 static Dictionary<char const *> cmr_dict (cmr_init);
403 Lookup::text (String style, String text) const
408 Real font_h = paper_l_->get_var ("font_normal");
409 if (paper_l_->scope_p_->elem_b ("font_" + style))
411 font_h = paper_l_->get_var ("font_" + style);
414 if (paper_l_->scope_p_->elem_b ("magnification_" + style))
416 font_mag = (int)paper_l_->get_var ("magnification_" + style);
419 if (cmr_dict.elem_b (style))
421 style = String (cmr_dict [style]) + to_str ((int)font_h); // ugh
425 Interval ydims (0,0);
427 Font_metric* afm_l = all_fonts_global_p->find_font (style);
430 for (int i = 0; i < text.length_i (); i++)
433 for (i++; (i < text.length_i ()) && isalpha(text[i]); i++)
437 Character_metric *c = afm_l->get_char (text[i],false);
438 w += c->dimensions()[X_AXIS].length ();
439 ydims.unite (c->dimensions()[Y_AXIS]);
443 if (font_mag > 1 && font_mag < 7 )
445 style = style + String(" scaled \\magstep ") + to_str (font_mag);
446 w *= mag_steps[font_mag];
447 ydims *= mag_steps[font_mag];
450 DOUT << "\n" << to_str (w) << "\n";
451 m.dim_.x () = Interval (0, w);
453 Atom at (gh_list (ly_symbol ("text"),
454 gh_str02scm (text.ch_C()),
456 at.font_ = ly_symbol (style);
466 Lookup::time_signature (int num, int den) const
468 String sty = "number";
469 Molecule n (text (sty, to_str (num)));
470 Molecule d (text (sty, to_str (den)));
471 n.do_center (X_AXIS);
472 d.do_center (X_AXIS);
476 m.add_at_edge (Y_AXIS, UP, n, 0.0);
477 m.add_at_edge (Y_AXIS, DOWN, d, 0.0);
482 m.do_center (Y_AXIS);
488 Lookup::staff_brace (Real y) const
492 Atom at (gh_list (ly_symbol ("pianobrace"),
497 m.dim_[Y_AXIS] = Interval (-y/2,y/2);
498 m.dim_[X_AXIS] = Interval (0,0);
504 Lookup::hairpin (Real width, bool decresc, bool continued) const
507 Real height = paper_l_->staffheight_f () / 6;
509 String hairpin = String (decresc ? "de" : "") + "crescendo";
510 Atom at (gh_list (ly_symbol (hairpin),
511 gh_double2scm (width),
512 gh_double2scm (height),
513 gh_double2scm (continued ? height/2 : 0.0),
515 m.dim_.x () = Interval (0, width);
516 m.dim_.y () = Interval (-2*height, 2*height);
523 Lookup::plet (Real dy , Real dx, Direction dir) const
526 SCM thick = ly_symbol ("tuplet_thick");
528 if (paper_l_->scope_p_->elem_b (thick))
530 t = paper_l_->get_realvar (thick);
533 Atom at (gh_list(ly_symbol ("tuplet"),
545 Make a smooth curve along the points
548 Lookup::slur (Array<Offset> controls) const
550 Offset delta_off = controls[3]- controls[0];
554 int indices[] = {5,6,7,4,1,2,3,0};
556 for (int i= 0; i < 8; i++)
557 scontrols[i] = offset2scm (controls[indices[i]]);
560 Atom at (gh_list (ly_symbol ("bezier-sandwich"),
561 ly_quote_scm (array_to_list (scontrols, 8)),
564 m.dim_[X_AXIS] = Interval (0, delta_off[X_AXIS]);
565 m.dim_[Y_AXIS] = Interval (0 <? delta_off[Y_AXIS], 0 >? delta_off[Y_AXIS]);
571 Lookup::staff_bracket (Real y) const
574 Atom at ( gh_list (ly_symbol ("bracket"),
578 m.dim_[Y_AXIS] = Interval (-y/2,y/2);
579 m.dim_[X_AXIS] = Interval (0,4 PT);
581 m.translate_axis (- 4. / 3. * m.dim_[X_AXIS].length (), X_AXIS);
586 Lookup::volta (Real w, bool last_b) const
589 SCM thick = ly_symbol ("volta_thick");
591 if (paper_l_->scope_p_->elem_b (thick))
593 t = paper_l_->get_realvar (thick);
595 Atom at (gh_list (ly_symbol ("volta"),
601 Real interline_f = paper_l_->interline_f ();
603 m.dim_[Y_AXIS] = Interval (-interline_f, interline_f);
604 m.dim_[X_AXIS] = Interval (0, w);
612 Lookup::special_ball (int j, String kind_of_ball) const
617 return afm_find (String ("noteheads-") + kind_of_ball);