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"
29 #include "lily-guile.hh"
37 Lookup::Lookup (Lookup const& s)
39 font_name_ = s.font_name_;
45 build a ledger line for small pieces.
48 Lookup::ledger_line (Interval xwid) const
50 Drul_array<Molecule> endings;
51 endings[LEFT] = afm_find ("noteheads-ledgerending");
52 Molecule * e = &endings[LEFT];
55 Real thick = e->dim_[Y_AXIS].length();
56 Real len = e->dim_[X_AXIS].length () - thick;
61 endings[d].translate_axis (xwid[d] - endings[d].dim_[X_AXIS][d], X_AXIS);
62 total.add_molecule (endings[d]);
63 } while ((flip(&d)) != LEFT);
65 Real xpos = xwid [LEFT] + len;
67 while (xpos + len + thick /2 <= xwid[RIGHT])
69 e->translate_axis (len, X_AXIS);
70 total.add_molecule (*e);
79 Lookup::accidental (int j, bool cautionary) const
81 Molecule m(afm_find (String ("accidentals-") + to_str (j)));
84 Molecule open = afm_find (String ("accidentals-("));
85 Molecule close = afm_find (String ("accidentals-)"));
86 m.add_at_edge(X_AXIS, LEFT, Molecule(open), 0);
87 m.add_at_edge(X_AXIS, RIGHT, Molecule(close), 0);
95 Lookup::afm_find (String s, bool warn) const
99 Lookup * me = (Lookup*)(this);
100 me->afm_l_ = all_fonts_global_p->find_afm (font_name_);
103 warning (_f("Can't open `%s'\n", font_name_));
104 warning (_f("Search path %s\n", global_path.str ().ch_C()));
105 error (_f("Aborting"));
108 Adobe_font_char_metric cm = afm_l_->find_char (s, warn);
113 don't want people relying on this kind of dimension.
119 Atom at (gh_list (char_scm_sym,
120 gh_int2scm (cm.code ()),
122 at.font_ = ly_symbol (font_name_.ch_C());
123 m.dim_ = cm.dimensions();
129 Lookup::notehead (int j, String type) const
134 return afm_find (String ("noteheads-") + to_str (j) + type);
138 Lookup::simple_bar (String type, Real h, Paper_def* paper_l) const
140 SCM thick = ly_symbol ("barthick_" + type);
143 if (paper_l->scope_p_->elem_b (thick))
145 w = paper_l->get_realvar (thick);
149 programming_error ("No bar thickness set ! ");
152 return filledbox (Box (Interval(0,w), Interval(-h/2, h/2)));
157 Lookup::bar (String str, Real h, Paper_def *paper_l) const
159 if (str == "bracket")
160 return staff_bracket (h);
161 else if (str == "brace")
163 Real staffht = paper_l->get_var ("staffheight");
164 return staff_brace (h,staffht);
166 Real kern = paper_l->get_var ("bar_kern");
167 Real thinkern = paper_l->get_var ("bar_thinkern");
169 Molecule thin = simple_bar ("thin", h, paper_l);
170 Molecule thick = simple_bar ("thick", h, paper_l);
171 Molecule colon = afm_find ("dots-repeatcolon", paper_l);
177 return fill (Box (Interval(0, 0), Interval (-h/2, h/2)));
179 if (str == "scorepostbreak")
181 return simple_bar ("score", h, paper_l);
187 else if (str == "|.")
189 m.add_at_edge (X_AXIS, LEFT, thick, 0);
190 m.add_at_edge (X_AXIS, LEFT, thin, kern);
192 else if (str == ".|")
194 m.add_at_edge (X_AXIS, RIGHT, thick, 0);
195 m.add_at_edge (X_AXIS, RIGHT, thin, kern);
197 else if (str == ":|")
199 m.add_at_edge (X_AXIS, LEFT, thick, 0);
200 m.add_at_edge (X_AXIS, LEFT, thin, kern);
201 m.add_at_edge (X_AXIS, LEFT, colon, kern);
203 else if (str == "|:")
205 m.add_at_edge (X_AXIS, RIGHT, thick, 0);
206 m.add_at_edge (X_AXIS, RIGHT, thin, kern);
207 m.add_at_edge (X_AXIS, RIGHT, colon, kern);
209 else if (str == ":|:")
211 m.add_at_edge (X_AXIS, LEFT, thick, thinkern);
212 m.add_at_edge (X_AXIS, LEFT, colon, kern);
213 m.add_at_edge (X_AXIS, RIGHT, thick, kern);
214 m.add_at_edge (X_AXIS, RIGHT, colon, kern);
216 else if (str == ".|.")
218 m.add_at_edge (X_AXIS, LEFT, thick, thinkern);
219 m.add_at_edge (X_AXIS, RIGHT, thick, kern);
221 else if (str == "||")
223 m.add_at_edge (X_AXIS, RIGHT, thin, 0);
224 m.add_at_edge (X_AXIS, RIGHT, thin, thinkern);
231 Lookup::beam (Real slope, Real width, Real thick) const
233 Real height = slope * width;
234 Real min_y = (0 <? height) - thick/2;
235 Real max_y = (0 >? height) + thick/2;
240 (gh_list (beam_scm_sym,
241 gh_double2scm (width),
242 gh_double2scm (slope),
243 gh_double2scm (thick),
246 m.dim_[X_AXIS] = Interval (0, width);
247 m.dim_[Y_AXIS] = Interval (min_y, max_y);
253 Lookup::clef (String st) const
255 return afm_find (String ("clefs-" + st));
259 offset2scm (Offset o)
261 return gh_list (gh_double2scm (o[X_AXIS]), gh_double2scm(o[Y_AXIS]),
266 Lookup::dashed_slur (Array<Offset> controls, Real thick, Real dash) const
268 assert (controls.size () == 8);
269 Offset d = controls[3] - controls[0];
277 m.dim_[X_AXIS] = Interval (0, dx);
278 m.dim_[Y_AXIS] = Interval (0 <? dy, 0 >? dy);
281 for (int i=0; i< 4; i++)
283 sc[i] = offset2scm (controls[i]);
287 (gh_list (ly_symbol ("dashed-slur"),
288 gh_double2scm (thick),
289 gh_double2scm (dash),
290 ly_quote_scm (array_to_list (sc, 4)),
299 Lookup::dots () const
301 return afm_find (String ("dots-dot"));
307 Lookup::fill (Box b) const
315 Lookup::rest (int j, bool o, String style) const
317 return afm_find (String ("rests-") + to_str (j) + (o ? "o" : "") + style);
322 Lookup::special_time_signature (String s, int n, int d, Paper_def*pap) const
324 // First guess: s contains only the signature style
325 String symbolname = "timesig-" + s + to_str (n) + "/" + to_str (d);
327 Molecule m = afm_find (symbolname, false);
331 // Second guess: s contains the full signature name
332 m = afm_find ("timesig-"+s, false);
336 // Resort to default layout with numbers
337 return time_signature (n,d,pap);
341 Lookup::filledbox (Box b ) const
345 Atom at (gh_list (filledbox_scm_sym,
346 gh_double2scm (-b[X_AXIS][LEFT]),
347 gh_double2scm (b[X_AXIS][RIGHT]),
348 gh_double2scm (-b[Y_AXIS][DOWN]),
349 gh_double2scm (b[Y_AXIS][UP]),
360 Magnification steps. These are powers of 1.2. The numbers are
361 taken from Knuth's plain.tex: */
362 static Real mag_steps[] = {1, 1, 1.200, 1.440, 1.7280, 2.074, 2.488};
365 TODO: THIS IS UGLY. Since the user has direct access to TeX
366 strings, we try some halfbaked attempt to detect TeX trickery.
370 Lookup::text (String style, String text, Paper_def *paper_l) const
373 if (style.empty_b ())
377 Real font_h = paper_l->get_var ("font_normal");
378 if (paper_l->scope_p_->elem_b ("font_" + style))
380 font_h = paper_l->get_var ("font_" + style);
383 if (paper_l->scope_p_->elem_b ("magnification_" + style))
385 font_mag = (int)paper_l->get_var ("magnification_" + style);
391 SCM l = gh_eval_str (("(style-to-cmr \"" + style + "\")").ch_C());
394 style = ly_scm2string (SCM_CDR(l)) +to_str ((int)font_h);
398 Interval ydims (0,0);
400 Font_metric* afm_l = all_fonts_global_p->find_font (style);
405 for (int i = 0; i < text.length_i (); i++)
410 for (i++; (i < text.length_i ()) && isalpha(text[i]); i++)
412 i--; // Compensate for the increment in the outer loop!
418 else if (text[i] == '}')
420 Character_metric *c = afm_l->get_char ((unsigned char)text[i],false);
422 w += c->dimensions()[X_AXIS].length ();
423 ydims.unite (c->dimensions()[Y_AXIS]);
427 if (font_mag > 1 && font_mag < 7 )
430 style = style + String(" scaled \\magstep ") + to_str (font_mag);
431 w *= mag_steps[font_mag];
432 ydims *= mag_steps[font_mag];
437 warning (_f ("Non-matching braces in text `%s', adding braces.", text.ch_C()));
441 text = to_str ('{', -brace_count) + text;
445 text = text + to_str ('}', brace_count);
450 DOUT << "\n" << to_str (w) << "\n";
451 m.dim_.x () = Interval (0, w);
455 Atom at (gh_list (text_scm_sym,
456 gh_str02scm (text.ch_C()),
458 at.font_ = ly_symbol (style);
466 Lookup::time_signature (int num, int den, Paper_def *paper_l) const
468 String sty = "number";
469 Molecule n (text (sty, to_str (num), paper_l));
470 Molecule d (text (sty, to_str (den), paper_l));
471 n.align_to (X_AXIS, CENTER);
472 d.align_to (X_AXIS, CENTER);
476 m.add_at_edge (Y_AXIS, UP, n, 0.0);
477 m.add_at_edge (Y_AXIS, DOWN, d, 0.0);
482 m.align_to (Y_AXIS, CENTER);
488 Lookup::staff_brace (Real y, int staff_size) const
492 Atom at (gh_list (pianobrace_scm_sym,
494 gh_int2scm (staff_size),
498 m.dim_[Y_AXIS] = Interval (-y/2,y/2);
499 m.dim_[X_AXIS] = Interval (0,0);
505 Lookup::hairpin (Real width, Real height, Real thick, bool decresc, bool continued) const
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),
514 gh_double2scm (thick),
516 m.dim_.x () = Interval (0, width);
517 m.dim_.y () = Interval (-2*height, 2*height);
524 Lookup::tuplet_bracket (Real dy , Real dx, Real thick, Real gap, Real interline_f, Direction dir) const
528 Atom at (gh_list(tuplet_scm_sym,
529 gh_double2scm (interline_f),
533 gh_double2scm (thick),
542 Make a smooth curve along the points
545 Lookup::slur (Array<Offset> controls, Real linethick) const
547 Offset delta_off = controls[3]- controls[0];
551 int indices[] = {5,6,7,4,1,2,3,0};
553 for (int i= 0; i < 8; i++)
554 scontrols[i] = offset2scm (controls[indices[i]]);
557 Atom at (gh_list (ly_symbol ("bezier-sandwich"),
558 ly_quote_scm (array_to_list (scontrols, 8)),
559 gh_double2scm (linethick),
562 m.dim_[X_AXIS] = Interval (0, delta_off[X_AXIS]);
563 m.dim_[Y_AXIS] = Interval (0 <? delta_off[Y_AXIS], 0 >? delta_off[Y_AXIS]);
569 Lookup::staff_bracket (Real y) const
572 Atom at ( gh_list (bracket_scm_sym,
576 m.dim_[Y_AXIS] = Interval (-y/2,y/2);
577 m.dim_[X_AXIS] = Interval (0,4 PT);
579 m.translate_axis (- 4. / 3. * m.dim_[X_AXIS].length (), X_AXIS);
584 Lookup::volta (Real h, Real w, Real thick, bool last_b) const
588 Atom at (gh_list (volta_scm_sym,
591 gh_double2scm (thick),
595 m.dim_[Y_AXIS] = Interval (- h/2, h/2);
596 m.dim_[X_AXIS] = Interval (0, w);
603 Lookup::accordion (SCM s, Real interline_f) const
606 String sym = ly_scm2string(SCM_CAR(s));
607 String reg = ly_scm2string(SCM_CAR(SCM_CDR(s)));
609 if (sym == "Discant")
611 Molecule r = afm_find("scripts-accDiscant");
613 if (reg.left_str(1) == "F")
615 Molecule d = afm_find("scripts-accDot");
616 d.translate_axis(interline_f * 2.5 PT, Y_AXIS);
618 reg = reg.right_str(reg.length_i()-1);
621 if (reg.left_str(3) == "EEE")
624 reg = reg.right_str(reg.length_i()-3);
626 else if (reg.left_str(2) == "EE")
629 reg = reg.right_str(reg.length_i()-2);
631 else if (reg.left_str(2) == "Eh")
634 reg = reg.right_str(reg.length_i()-2);
636 else if (reg.left_str(1) == "E")
639 reg = reg.right_str(reg.length_i()-1);
643 Molecule d = afm_find("scripts-accDot");
644 d.translate_axis(interline_f * 1.5 PT, Y_AXIS);
649 Molecule d = afm_find("scripts-accDot");
650 d.translate_axis(interline_f * 1.5 PT, Y_AXIS);
651 d.translate_axis(0.8 * interline_f PT, X_AXIS);
656 Molecule d = afm_find("scripts-accDot");
657 d.translate_axis(interline_f * 1.5 PT, Y_AXIS);
658 d.translate_axis(-0.8 * interline_f PT, X_AXIS);
661 if (reg.left_str(2) == "SS")
663 Molecule d = afm_find("scripts-accDot");
664 d.translate_axis(0.5 * interline_f PT, Y_AXIS);
665 d.translate_axis(0.4 * interline_f PT, X_AXIS);
667 d.translate_axis(-0.8 * interline_f PT, X_AXIS);
669 reg = reg.right_str(reg.length_i()-2);
671 if (reg.left_str(1) == "S")
673 Molecule d = afm_find("scripts-accDot");
674 d.translate_axis(0.5 * interline_f PT, Y_AXIS);
676 reg = reg.right_str(reg.length_i()-1);
679 else if (sym == "Freebase")
681 Molecule r = afm_find("scripts-accFreebase");
683 if (reg.left_str(1) == "F")
685 Molecule d = afm_find("scripts-accDot");
686 d.translate_axis(interline_f * 1.5 PT, Y_AXIS);
688 reg = reg.right_str(reg.length_i()-1);
692 Molecule d = afm_find("scripts-accDot");
693 d.translate_axis(interline_f * 0.5 PT, Y_AXIS);
697 else if (sym == "Bayanbase")
699 Molecule r = afm_find("scripts-accBayanbase");
701 if (reg.left_str(1) == "T")
703 Molecule d = afm_find("scripts-accDot");
704 d.translate_axis(interline_f * 2.5 PT, Y_AXIS);
706 reg = reg.right_str(reg.length_i()-1);
708 /* include 4' reed just for completeness. You don't want to use this. */
709 if (reg.left_str(1) == "F")
711 Molecule d = afm_find("scripts-accDot");
712 d.translate_axis(interline_f * 1.5 PT, Y_AXIS);
714 reg = reg.right_str(reg.length_i()-1);
716 if (reg.left_str(2) == "EE")
718 Molecule d = afm_find("scripts-accDot");
719 d.translate_axis(interline_f * 0.5 PT, Y_AXIS);
720 d.translate_axis(0.4 * interline_f PT, X_AXIS);
722 d.translate_axis(-0.8 * interline_f PT, X_AXIS);
724 reg = reg.right_str(reg.length_i()-2);
726 if (reg.left_str(1) == "E")
728 Molecule d = afm_find("scripts-accDot");
729 d.translate_axis(interline_f * 0.5 PT, Y_AXIS);
731 reg = reg.right_str(reg.length_i()-1);
734 else if (sym == "Stdbase")
736 Molecule r = afm_find("scripts-accStdbase");
738 if (reg.left_str(1) == "T")
740 Molecule d = afm_find("scripts-accDot");
741 d.translate_axis(interline_f * 3.5 PT, Y_AXIS);
743 reg = reg.right_str(reg.length_i()-1);
745 if (reg.left_str(1) == "F")
747 Molecule d = afm_find("scripts-accDot");
748 d.translate_axis(interline_f * 2.5 PT, Y_AXIS);
750 reg = reg.right_str(reg.length_i()-1);
752 if (reg.left_str(1) == "M")
754 Molecule d = afm_find("scripts-accDot");
755 d.translate_axis(interline_f * 2 PT, Y_AXIS);
756 d.translate_axis(interline_f PT, X_AXIS);
758 reg = reg.right_str(reg.length_i()-1);
760 if (reg.left_str(1) == "E")
762 Molecule d = afm_find("scripts-accDot");
763 d.translate_axis(interline_f * 1.5 PT, Y_AXIS);
765 reg = reg.right_str(reg.length_i()-1);
767 if (reg.left_str(1) == "S")
769 Molecule d = afm_find("scripts-accDot");
770 d.translate_axis(interline_f * 0.5 PT, Y_AXIS);
772 reg = reg.right_str(reg.length_i()-1);
775 /* ugh maybe try to use regular font for S.B. and B.B and only use one font
777 else if (sym == "SB")
779 Molecule r = afm_find("scripts-accSB");
782 else if (sym == "BB")
784 Molecule r = afm_find("scripts-accBB");
787 else if (sym == "OldEE")
789 Molecule r = afm_find("scripts-accOldEE");
792 else if (sym == "OldEES")
794 Molecule r = afm_find("scripts-accOldEES");