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")
162 return staff_brace (h);
164 Real kern = paper_l->get_var ("bar_kern");
165 Real thinkern = paper_l->get_var ("bar_thinkern");
167 Molecule thin = simple_bar ("thin", h, paper_l);
168 Molecule thick = simple_bar ("thick", h, paper_l);
169 Molecule colon = afm_find ("dots-repeatcolon", paper_l);
175 return fill (Box (Interval(0, 0), Interval (-h/2, h/2)));
177 if (str == "scorepostbreak")
179 return simple_bar ("score", h, paper_l);
185 else if (str == "|.")
187 m.add_at_edge (X_AXIS, LEFT, thick, 0);
188 m.add_at_edge (X_AXIS, LEFT, thin, kern);
190 else if (str == ".|")
192 m.add_at_edge (X_AXIS, RIGHT, thick, 0);
193 m.add_at_edge (X_AXIS, RIGHT, thin, kern);
195 else if (str == ":|")
197 m.add_at_edge (X_AXIS, LEFT, thick, 0);
198 m.add_at_edge (X_AXIS, LEFT, thin, kern);
199 m.add_at_edge (X_AXIS, LEFT, colon, kern);
201 else if (str == "|:")
203 m.add_at_edge (X_AXIS, RIGHT, thick, 0);
204 m.add_at_edge (X_AXIS, RIGHT, thin, kern);
205 m.add_at_edge (X_AXIS, RIGHT, colon, kern);
207 else if (str == ":|:")
209 m.add_at_edge (X_AXIS, LEFT, thick, thinkern);
210 m.add_at_edge (X_AXIS, LEFT, colon, kern);
211 m.add_at_edge (X_AXIS, RIGHT, thick, kern);
212 m.add_at_edge (X_AXIS, RIGHT, colon, kern);
214 else if (str == ".|.")
216 m.add_at_edge (X_AXIS, LEFT, thick, thinkern);
217 m.add_at_edge (X_AXIS, RIGHT, thick, kern);
219 else if (str == "||")
221 m.add_at_edge (X_AXIS, RIGHT, thin, 0);
222 m.add_at_edge (X_AXIS, RIGHT, thin, thinkern);
229 Lookup::beam (Real slope, Real width, Real thick) const
231 Real height = slope * width;
232 Real min_y = (0 <? height) - thick/2;
233 Real max_y = (0 >? height) + thick/2;
238 (gh_list (beam_scm_sym,
239 gh_double2scm (width),
240 gh_double2scm (slope),
241 gh_double2scm (thick),
244 m.dim_[X_AXIS] = Interval (0, width);
245 m.dim_[Y_AXIS] = Interval (min_y, max_y);
251 Lookup::clef (String st) const
253 return afm_find (String ("clefs-" + st));
257 offset2scm (Offset o)
259 return gh_list (gh_double2scm (o[X_AXIS]), gh_double2scm(o[Y_AXIS]),
264 Lookup::dashed_slur (Array<Offset> controls, Real thick, Real dash) const
266 assert (controls.size () == 8);
267 Offset d = controls[3] - controls[0];
275 m.dim_[X_AXIS] = Interval (0, dx);
276 m.dim_[Y_AXIS] = Interval (0 <? dy, 0 >? dy);
279 for (int i=0; i< 4; i++)
281 sc[i] = offset2scm (controls[i]);
285 (gh_list (ly_symbol ("dashed-slur"),
286 gh_double2scm (thick),
287 gh_double2scm (dash),
288 ly_quote_scm (array_to_list (sc, 4)),
297 Lookup::dots () const
299 return afm_find (String ("dots-dot"));
305 Lookup::fill (Box b) const
313 Lookup::rest (int j, bool o, String style) const
315 return afm_find (String ("rests-") + to_str (j) + (o ? "o" : "") + style);
320 Lookup::special_time_signature (String s, int n, int d, Paper_def*pap) const
322 // First guess: s contains only the signature style
323 String symbolname = "timesig-" + s + to_str (n) + "/" + to_str (d);
325 Molecule m = afm_find (symbolname, false);
329 // Second guess: s contains the full signature name
330 m = afm_find ("timesig-"+s, false);
334 // Resort to default layout with numbers
335 return time_signature (n,d,pap);
339 Lookup::filledbox (Box b ) const
343 Atom at (gh_list (filledbox_scm_sym,
344 gh_double2scm (-b[X_AXIS][LEFT]),
345 gh_double2scm (b[X_AXIS][RIGHT]),
346 gh_double2scm (-b[Y_AXIS][DOWN]),
347 gh_double2scm (b[Y_AXIS][UP]),
358 Magnification steps. These are powers of 1.2. The numbers are
359 taken from Knuth's plain.tex: */
360 static Real mag_steps[] = {1, 1, 1.200, 1.440, 1.7280, 2.074, 2.488};
363 TODO: THIS IS UGLY. Since the user has direct access to TeX
364 strings, we try some halfbaked attempt to detect TeX trickery.
368 Lookup::text (String style, String text, Paper_def *paper_l) const
371 if (style.empty_b ())
375 Real font_h = paper_l->get_var ("font_normal");
376 if (paper_l->scope_p_->elem_b ("font_" + style))
378 font_h = paper_l->get_var ("font_" + style);
381 if (paper_l->scope_p_->elem_b ("magnification_" + style))
383 font_mag = (int)paper_l->get_var ("magnification_" + style);
389 SCM l = gh_eval_str (("(style-to-cmr \"" + style + "\")").ch_C());
392 style = ly_scm2string (SCM_CDR(l)) +to_str ((int)font_h);
396 Interval ydims (0,0);
398 Font_metric* afm_l = all_fonts_global_p->find_font (style);
403 for (int i = 0; i < text.length_i (); i++)
408 for (i++; (i < text.length_i ()) && isalpha(text[i]); i++)
410 i--; // Compensate for the increment in the outer loop!
416 else if (text[i] == '}')
418 Character_metric *c = afm_l->get_char ((unsigned char)text[i],false);
420 w += c->dimensions()[X_AXIS].length ();
421 ydims.unite (c->dimensions()[Y_AXIS]);
425 if (font_mag > 1 && font_mag < 7 )
428 style = style + String(" scaled \\magstep ") + to_str (font_mag);
429 w *= mag_steps[font_mag];
430 ydims *= mag_steps[font_mag];
435 warning (_f ("Non-matching braces in text `%s', adding braces.", text.ch_C()));
439 text = to_str ('{', -brace_count) + text;
443 text = text + to_str ('}', brace_count);
448 DOUT << "\n" << to_str (w) << "\n";
449 m.dim_.x () = Interval (0, w);
453 Atom at (gh_list (text_scm_sym,
454 gh_str02scm (text.ch_C()),
456 at.font_ = ly_symbol (style);
464 Lookup::time_signature (int num, int den, Paper_def *paper_l) const
466 String sty = "number";
467 Molecule n (text (sty, to_str (num), paper_l));
468 Molecule d (text (sty, to_str (den), paper_l));
469 n.align_to (X_AXIS, CENTER);
470 d.align_to (X_AXIS, CENTER);
474 m.add_at_edge (Y_AXIS, UP, n, 0.0);
475 m.add_at_edge (Y_AXIS, DOWN, d, 0.0);
480 m.align_to (Y_AXIS, CENTER);
486 Lookup::staff_brace (Real y) const
490 Atom at (gh_list (pianobrace_scm_sym,
495 m.dim_[Y_AXIS] = Interval (-y/2,y/2);
496 m.dim_[X_AXIS] = Interval (0,0);
502 Lookup::hairpin (Real width, Real height, bool decresc, bool continued) const
506 String hairpin = String (decresc ? "de" : "") + "crescendo";
507 Atom at (gh_list (ly_symbol (hairpin),
508 gh_double2scm (width),
509 gh_double2scm (height),
510 gh_double2scm (continued ? height/2 : 0.0),
512 m.dim_.x () = Interval (0, width);
513 m.dim_.y () = Interval (-2*height, 2*height);
520 Lookup::tuplet_bracket (Real dy , Real dx, Real thick, Real gap, Real interline_f, Direction dir) const
524 Atom at (gh_list(tuplet_scm_sym,
525 gh_double2scm (interline_f),
529 gh_double2scm (thick),
538 Make a smooth curve along the points
541 Lookup::slur (Array<Offset> controls) const
543 Offset delta_off = controls[3]- controls[0];
547 int indices[] = {5,6,7,4,1,2,3,0};
549 for (int i= 0; i < 8; i++)
550 scontrols[i] = offset2scm (controls[indices[i]]);
553 Atom at (gh_list (ly_symbol ("bezier-sandwich"),
554 ly_quote_scm (array_to_list (scontrols, 8)),
557 m.dim_[X_AXIS] = Interval (0, delta_off[X_AXIS]);
558 m.dim_[Y_AXIS] = Interval (0 <? delta_off[Y_AXIS], 0 >? delta_off[Y_AXIS]);
564 Lookup::staff_bracket (Real y) const
567 Atom at ( gh_list (bracket_scm_sym,
571 m.dim_[Y_AXIS] = Interval (-y/2,y/2);
572 m.dim_[X_AXIS] = Interval (0,4 PT);
574 m.translate_axis (- 4. / 3. * m.dim_[X_AXIS].length (), X_AXIS);
579 Lookup::volta (Real h, Real w, Real thick, bool last_b) const
583 Atom at (gh_list (volta_scm_sym,
586 gh_double2scm (thick),
590 m.dim_[Y_AXIS] = Interval (- h/2, h/2);
591 m.dim_[X_AXIS] = Interval (0, w);
598 Lookup::accordion (SCM s, Real interline_f) const
601 String sym = ly_scm2string(SCM_CAR(s));
602 String reg = ly_scm2string(SCM_CAR(SCM_CDR(s)));
604 if (sym == "Discant")
606 Molecule r = afm_find("scripts-accDiscant");
608 if (reg.left_str(1) == "F")
610 Molecule d = afm_find("scripts-accDot");
611 d.translate_axis(interline_f * 2.5 PT, Y_AXIS);
613 reg = reg.right_str(reg.length_i()-1);
616 if (reg.left_str(3) == "EEE")
619 reg = reg.right_str(reg.length_i()-3);
621 else if (reg.left_str(2) == "EE")
624 reg = reg.right_str(reg.length_i()-2);
626 else if (reg.left_str(2) == "Eh")
629 reg = reg.right_str(reg.length_i()-2);
631 else if (reg.left_str(1) == "E")
634 reg = reg.right_str(reg.length_i()-1);
638 Molecule d = afm_find("scripts-accDot");
639 d.translate_axis(interline_f * 1.5 PT, Y_AXIS);
644 Molecule d = afm_find("scripts-accDot");
645 d.translate_axis(interline_f * 1.5 PT, Y_AXIS);
646 d.translate_axis(0.8 * interline_f PT, X_AXIS);
651 Molecule d = afm_find("scripts-accDot");
652 d.translate_axis(interline_f * 1.5 PT, Y_AXIS);
653 d.translate_axis(-0.8 * interline_f PT, X_AXIS);
656 if (reg.left_str(2) == "SS")
658 Molecule d = afm_find("scripts-accDot");
659 d.translate_axis(0.5 * interline_f PT, Y_AXIS);
660 d.translate_axis(0.4 * interline_f PT, X_AXIS);
662 d.translate_axis(-0.8 * interline_f PT, X_AXIS);
664 reg = reg.right_str(reg.length_i()-2);
666 if (reg.left_str(1) == "S")
668 Molecule d = afm_find("scripts-accDot");
669 d.translate_axis(0.5 * interline_f PT, Y_AXIS);
671 reg = reg.right_str(reg.length_i()-1);
674 else if (sym == "Freebase")
676 Molecule r = afm_find("scripts-accFreebase");
678 if (reg.left_str(1) == "F")
680 Molecule d = afm_find("scripts-accDot");
681 d.translate_axis(interline_f * 1.5 PT, Y_AXIS);
683 reg = reg.right_str(reg.length_i()-1);
687 Molecule d = afm_find("scripts-accDot");
688 d.translate_axis(interline_f * 0.5 PT, Y_AXIS);
692 else if (sym == "Bayanbase")
694 Molecule r = afm_find("scripts-accBayanbase");
696 if (reg.left_str(1) == "T")
698 Molecule d = afm_find("scripts-accDot");
699 d.translate_axis(interline_f * 2.5 PT, Y_AXIS);
701 reg = reg.right_str(reg.length_i()-1);
703 /* include 4' reed just for completeness. You don't want to use this. */
704 if (reg.left_str(1) == "F")
706 Molecule d = afm_find("scripts-accDot");
707 d.translate_axis(interline_f * 1.5 PT, Y_AXIS);
709 reg = reg.right_str(reg.length_i()-1);
711 if (reg.left_str(2) == "EE")
713 Molecule d = afm_find("scripts-accDot");
714 d.translate_axis(interline_f * 0.5 PT, Y_AXIS);
715 d.translate_axis(0.4 * interline_f PT, X_AXIS);
717 d.translate_axis(-0.8 * interline_f PT, X_AXIS);
719 reg = reg.right_str(reg.length_i()-2);
721 if (reg.left_str(1) == "E")
723 Molecule d = afm_find("scripts-accDot");
724 d.translate_axis(interline_f * 0.5 PT, Y_AXIS);
726 reg = reg.right_str(reg.length_i()-1);
729 else if (sym == "Stdbase")
731 Molecule r = afm_find("scripts-accStdbase");
733 if (reg.left_str(1) == "T")
735 Molecule d = afm_find("scripts-accDot");
736 d.translate_axis(interline_f * 3.5 PT, Y_AXIS);
738 reg = reg.right_str(reg.length_i()-1);
740 if (reg.left_str(1) == "F")
742 Molecule d = afm_find("scripts-accDot");
743 d.translate_axis(interline_f * 2.5 PT, Y_AXIS);
745 reg = reg.right_str(reg.length_i()-1);
747 if (reg.left_str(1) == "M")
749 Molecule d = afm_find("scripts-accDot");
750 d.translate_axis(interline_f * 2 PT, Y_AXIS);
751 d.translate_axis(interline_f PT, X_AXIS);
753 reg = reg.right_str(reg.length_i()-1);
755 if (reg.left_str(1) == "E")
757 Molecule d = afm_find("scripts-accDot");
758 d.translate_axis(interline_f * 1.5 PT, Y_AXIS);
760 reg = reg.right_str(reg.length_i()-1);
762 if (reg.left_str(1) == "S")
764 Molecule d = afm_find("scripts-accDot");
765 d.translate_axis(interline_f * 0.5 PT, Y_AXIS);
767 reg = reg.right_str(reg.length_i()-1);
770 /* ugh maybe try to use regular font for S.B. and B.B and only use one font
772 else if (sym == "SB")
774 Molecule r = afm_find("scripts-accSB");
777 else if (sym == "BB")
779 Molecule r = afm_find("scripts-accBB");
782 else if (sym == "OldEE")
784 Molecule r = afm_find("scripts-accOldEE");
787 else if (sym == "OldEES")
789 Molecule r = afm_find("scripts-accOldEES");