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 at.magn_ = gh_int2scm (0);
125 m.dim_ = cm.dimensions();
131 Lookup::notehead (int j, String type) const
136 return afm_find (String ("noteheads-") + to_str (j) + type);
140 Lookup::simple_bar (String type, Real h, Paper_def* paper_l) const
142 SCM thick = ly_symbol ("barthick_" + type);
145 if (paper_l->scope_p_->elem_b (thick))
147 w = paper_l->get_realvar (thick);
151 programming_error ("No bar thickness set ! ");
154 return filledbox (Box (Interval(0,w), Interval(-h/2, h/2)));
159 Lookup::bar (String str, Real h, Paper_def *paper_l) const
161 if (str == "bracket")
162 return staff_bracket (h);
163 else if (str == "brace")
165 Real staffht = paper_l->get_var ("staffheight");
166 return staff_brace (h,staffht);
168 Real kern = paper_l->get_var ("bar_kern");
169 Real thinkern = paper_l->get_var ("bar_thinkern");
171 Molecule thin = simple_bar ("thin", h, paper_l);
172 Molecule thick = simple_bar ("thick", h, paper_l);
173 Molecule colon = afm_find ("dots-repeatcolon", paper_l);
179 return fill (Box (Interval(0, 0), Interval (-h/2, h/2)));
181 if (str == "scorepostbreak")
183 return simple_bar ("score", h, paper_l);
189 else if (str == "|.")
191 m.add_at_edge (X_AXIS, LEFT, thick, 0);
192 m.add_at_edge (X_AXIS, LEFT, thin, kern);
194 else if (str == ".|")
196 m.add_at_edge (X_AXIS, RIGHT, thick, 0);
197 m.add_at_edge (X_AXIS, RIGHT, thin, kern);
199 else if (str == ":|")
201 m.add_at_edge (X_AXIS, LEFT, thick, 0);
202 m.add_at_edge (X_AXIS, LEFT, thin, kern);
203 m.add_at_edge (X_AXIS, LEFT, colon, kern);
205 else if (str == "|:")
207 m.add_at_edge (X_AXIS, RIGHT, thick, 0);
208 m.add_at_edge (X_AXIS, RIGHT, thin, kern);
209 m.add_at_edge (X_AXIS, RIGHT, colon, kern);
211 else if (str == ":|:")
213 m.add_at_edge (X_AXIS, LEFT, thick, thinkern);
214 m.add_at_edge (X_AXIS, LEFT, colon, kern);
215 m.add_at_edge (X_AXIS, RIGHT, thick, kern);
216 m.add_at_edge (X_AXIS, RIGHT, colon, kern);
218 else if (str == ".|.")
220 m.add_at_edge (X_AXIS, LEFT, thick, thinkern);
221 m.add_at_edge (X_AXIS, RIGHT, thick, kern);
223 else if (str == "||")
225 m.add_at_edge (X_AXIS, RIGHT, thin, 0);
226 m.add_at_edge (X_AXIS, RIGHT, thin, thinkern);
233 Lookup::beam (Real slope, Real width, Real thick) const
235 Real height = slope * width;
236 Real min_y = (0 <? height) - thick/2;
237 Real max_y = (0 >? height) + thick/2;
242 (gh_list (beam_scm_sym,
243 gh_double2scm (width),
244 gh_double2scm (slope),
245 gh_double2scm (thick),
248 m.dim_[X_AXIS] = Interval (0, width);
249 m.dim_[Y_AXIS] = Interval (min_y, max_y);
255 Lookup::clef (String st) const
257 return afm_find (String ("clefs-" + st));
261 offset2scm (Offset o)
263 return gh_list (gh_double2scm (o[X_AXIS]), gh_double2scm(o[Y_AXIS]),
268 Lookup::dashed_slur (Array<Offset> controls, Real thick, Real dash) const
270 assert (controls.size () == 8);
271 Offset d = controls[3] - controls[0];
279 m.dim_[X_AXIS] = Interval (0, dx);
280 m.dim_[Y_AXIS] = Interval (0 <? dy, 0 >? dy);
283 for (int i=0; i< 4; i++)
285 sc[i] = offset2scm (controls[i]);
289 (gh_list (ly_symbol ("dashed-slur"),
290 gh_double2scm (thick),
291 gh_double2scm (dash),
292 ly_quote_scm (array_to_list (sc, 4)),
301 Lookup::dots () const
303 return afm_find (String ("dots-dot"));
309 Lookup::fill (Box b) const
317 Lookup::rest (int j, bool o, String style) const
319 return afm_find (String ("rests-") + to_str (j) + (o ? "o" : "") + style);
324 Lookup::special_time_signature (String s, int n, int d, Paper_def*pap) const
326 // First guess: s contains only the signature style
327 String symbolname = "timesig-" + s + to_str (n) + "/" + to_str (d);
329 Molecule m = afm_find (symbolname, false);
333 // Second guess: s contains the full signature name
334 m = afm_find ("timesig-"+s, false);
338 // Resort to default layout with numbers
339 return time_signature (n,d,pap);
343 Lookup::filledbox (Box b ) const
347 Atom at (gh_list (filledbox_scm_sym,
348 gh_double2scm (-b[X_AXIS][LEFT]),
349 gh_double2scm (b[X_AXIS][RIGHT]),
350 gh_double2scm (-b[Y_AXIS][DOWN]),
351 gh_double2scm (b[Y_AXIS][UP]),
362 Magnification steps. These are powers of 1.2. The numbers are
363 taken from Knuth's plain.tex: */
364 static Real mag_steps[] = {1, 1, 1.200, 1.440, 1.7280, 2.074, 2.488};
367 TODO: THIS IS UGLY. Since the user has direct access to TeX
368 strings, we try some halfbaked attempt to detect TeX trickery.
372 Lookup::text (String style, String text, Paper_def *paper_l) const
375 if (style.empty_b ())
379 Real font_h = paper_l->get_var ("font_normal");
380 if (paper_l->scope_p_->elem_b ("font_" + style))
382 font_h = paper_l->get_var ("font_" + style);
385 if (paper_l->scope_p_->elem_b ("magnification_" + style))
387 font_mag = (int)paper_l->get_var ("magnification_" + style);
393 SCM l = gh_eval_str (("(style-to-cmr \"" + style + "\")").ch_C());
396 style = ly_scm2string (SCM_CDR(l)) +to_str ((int)font_h);
400 Interval ydims (0,0);
402 Font_metric* afm_l = all_fonts_global_p->find_font (style);
407 for (int i = 0; i < text.length_i (); i++)
412 for (i++; (i < text.length_i ()) && isalpha(text[i]); i++)
414 i--; // Compensate for the increment in the outer loop!
420 else if (text[i] == '}')
422 Character_metric *c = afm_l->get_char ((unsigned char)text[i],false);
424 w += c->dimensions()[X_AXIS].length ();
425 ydims.unite (c->dimensions()[Y_AXIS]);
429 if (font_mag > 1 && font_mag < 7 )
432 w *= mag_steps[font_mag];
433 ydims *= mag_steps[font_mag];
438 warning (_f ("Non-matching braces in text `%s', adding braces.", text.ch_C()));
442 text = to_str ('{', -brace_count) + text;
446 text = text + to_str ('}', brace_count);
451 DOUT << "\n" << to_str (w) << "\n";
452 m.dim_.x () = Interval (0, w);
456 Atom at (gh_list (text_scm_sym,
457 gh_str02scm (text.ch_C()),
459 at.font_ = ly_symbol (style);
460 at.magn_ = gh_int2scm (font_mag);
468 Lookup::time_signature (int num, int den, Paper_def *paper_l) const
470 String sty = "number";
471 Molecule n (text (sty, to_str (num), paper_l));
472 Molecule d (text (sty, to_str (den), paper_l));
473 n.align_to (X_AXIS, CENTER);
474 d.align_to (X_AXIS, CENTER);
478 m.add_at_edge (Y_AXIS, UP, n, 0.0);
479 m.add_at_edge (Y_AXIS, DOWN, d, 0.0);
484 m.align_to (Y_AXIS, CENTER);
490 Lookup::staff_brace (Real y, int staff_size) const
494 (define (pianobrace y staffht)
496 (minht (* 2 staffht))
500 (select-font (string-append "feta-braces" (number->string (inexact->exact staffht))) 0)
501 (char (max 0 (/ (- (min y (- maxht step)) minht) step))))
507 Atom at (gh_list (pianobrace_scm_sym,
509 gh_int2scm (staff_size),
513 m.dim_[Y_AXIS] = Interval (-y/2,y/2);
514 m.dim_[X_AXIS] = Interval (0,0);
520 Lookup::hairpin (Real width, Real height, Real thick, bool decresc, bool continued) const
524 String hairpin = String (decresc ? "de" : "") + "crescendo";
525 Atom at (gh_list (ly_symbol (hairpin),
526 gh_double2scm (thick),
527 gh_double2scm (width),
528 gh_double2scm (height),
529 gh_double2scm (continued ? height/2 : 0.0),
531 m.dim_.x () = Interval (0, width);
532 m.dim_.y () = Interval (-2*height, 2*height);
539 Lookup::tuplet_bracket (Real dy , Real dx, Real thick, Real gap, Real interline_f, Direction dir) const
543 Atom at (gh_list(tuplet_scm_sym,
544 gh_double2scm (interline_f),
548 gh_double2scm (thick),
557 Make a smooth curve along the points
560 Lookup::slur (Array<Offset> controls, Real linethick) const
562 Offset delta_off = controls[3]- controls[0];
566 int indices[] = {5,6,7,4,1,2,3,0};
568 for (int i= 0; i < 8; i++)
569 scontrols[i] = offset2scm (controls[indices[i]]);
572 Atom at (gh_list (ly_symbol ("bezier-sandwich"),
573 ly_quote_scm (array_to_list (scontrols, 8)),
574 gh_double2scm (linethick),
577 m.dim_[X_AXIS] = Interval (0, delta_off[X_AXIS]);
578 m.dim_[Y_AXIS] = Interval (0 <? delta_off[Y_AXIS], 0 >? delta_off[Y_AXIS]);
584 Lookup::staff_bracket (Real y) const
587 Atom at ( gh_list (bracket_scm_sym,
591 m.dim_[Y_AXIS] = Interval (-y/2,y/2);
592 m.dim_[X_AXIS] = Interval (0,4 PT);
594 m.translate_axis (- 4. / 3. * m.dim_[X_AXIS].length (), X_AXIS);
599 Lookup::volta (Real h, Real w, Real thick, bool vert_start, bool vert_end) const
603 Atom at (gh_list (volta_scm_sym,
606 gh_double2scm (thick),
607 gh_int2scm (vert_start),
608 gh_int2scm (vert_end),
611 m.dim_[Y_AXIS] = Interval (- h/2, h/2);
612 m.dim_[X_AXIS] = Interval (0, w);
619 Lookup::accordion (SCM s, Real interline_f) const
622 String sym = ly_scm2string(SCM_CAR(s));
623 String reg = ly_scm2string(SCM_CAR(SCM_CDR(s)));
625 if (sym == "Discant")
627 Molecule r = afm_find("scripts-accDiscant");
629 if (reg.left_str(1) == "F")
631 Molecule d = afm_find("scripts-accDot");
632 d.translate_axis(interline_f * 2.5 PT, Y_AXIS);
634 reg = reg.right_str(reg.length_i()-1);
637 if (reg.left_str(3) == "EEE")
640 reg = reg.right_str(reg.length_i()-3);
642 else if (reg.left_str(2) == "EE")
645 reg = reg.right_str(reg.length_i()-2);
647 else if (reg.left_str(2) == "Eh")
650 reg = reg.right_str(reg.length_i()-2);
652 else if (reg.left_str(1) == "E")
655 reg = reg.right_str(reg.length_i()-1);
659 Molecule d = afm_find("scripts-accDot");
660 d.translate_axis(interline_f * 1.5 PT, Y_AXIS);
665 Molecule d = afm_find("scripts-accDot");
666 d.translate_axis(interline_f * 1.5 PT, Y_AXIS);
667 d.translate_axis(0.8 * interline_f PT, X_AXIS);
672 Molecule d = afm_find("scripts-accDot");
673 d.translate_axis(interline_f * 1.5 PT, Y_AXIS);
674 d.translate_axis(-0.8 * interline_f PT, X_AXIS);
677 if (reg.left_str(2) == "SS")
679 Molecule d = afm_find("scripts-accDot");
680 d.translate_axis(0.5 * interline_f PT, Y_AXIS);
681 d.translate_axis(0.4 * interline_f PT, X_AXIS);
683 d.translate_axis(-0.8 * interline_f PT, X_AXIS);
685 reg = reg.right_str(reg.length_i()-2);
687 if (reg.left_str(1) == "S")
689 Molecule d = afm_find("scripts-accDot");
690 d.translate_axis(0.5 * interline_f PT, Y_AXIS);
692 reg = reg.right_str(reg.length_i()-1);
695 else if (sym == "Freebase")
697 Molecule r = afm_find("scripts-accFreebase");
699 if (reg.left_str(1) == "F")
701 Molecule d = afm_find("scripts-accDot");
702 d.translate_axis(interline_f * 1.5 PT, Y_AXIS);
704 reg = reg.right_str(reg.length_i()-1);
708 Molecule d = afm_find("scripts-accDot");
709 d.translate_axis(interline_f * 0.5 PT, Y_AXIS);
713 else if (sym == "Bayanbase")
715 Molecule r = afm_find("scripts-accBayanbase");
717 if (reg.left_str(1) == "T")
719 Molecule d = afm_find("scripts-accDot");
720 d.translate_axis(interline_f * 2.5 PT, Y_AXIS);
722 reg = reg.right_str(reg.length_i()-1);
724 /* include 4' reed just for completeness. You don't want to use this. */
725 if (reg.left_str(1) == "F")
727 Molecule d = afm_find("scripts-accDot");
728 d.translate_axis(interline_f * 1.5 PT, Y_AXIS);
730 reg = reg.right_str(reg.length_i()-1);
732 if (reg.left_str(2) == "EE")
734 Molecule d = afm_find("scripts-accDot");
735 d.translate_axis(interline_f * 0.5 PT, Y_AXIS);
736 d.translate_axis(0.4 * interline_f PT, X_AXIS);
738 d.translate_axis(-0.8 * interline_f PT, X_AXIS);
740 reg = reg.right_str(reg.length_i()-2);
742 if (reg.left_str(1) == "E")
744 Molecule d = afm_find("scripts-accDot");
745 d.translate_axis(interline_f * 0.5 PT, Y_AXIS);
747 reg = reg.right_str(reg.length_i()-1);
750 else if (sym == "Stdbase")
752 Molecule r = afm_find("scripts-accStdbase");
754 if (reg.left_str(1) == "T")
756 Molecule d = afm_find("scripts-accDot");
757 d.translate_axis(interline_f * 3.5 PT, Y_AXIS);
759 reg = reg.right_str(reg.length_i()-1);
761 if (reg.left_str(1) == "F")
763 Molecule d = afm_find("scripts-accDot");
764 d.translate_axis(interline_f * 2.5 PT, Y_AXIS);
766 reg = reg.right_str(reg.length_i()-1);
768 if (reg.left_str(1) == "M")
770 Molecule d = afm_find("scripts-accDot");
771 d.translate_axis(interline_f * 2 PT, Y_AXIS);
772 d.translate_axis(interline_f PT, X_AXIS);
774 reg = reg.right_str(reg.length_i()-1);
776 if (reg.left_str(1) == "E")
778 Molecule d = afm_find("scripts-accDot");
779 d.translate_axis(interline_f * 1.5 PT, Y_AXIS);
781 reg = reg.right_str(reg.length_i()-1);
783 if (reg.left_str(1) == "S")
785 Molecule d = afm_find("scripts-accDot");
786 d.translate_axis(interline_f * 0.5 PT, Y_AXIS);
788 reg = reg.right_str(reg.length_i()-1);
791 /* ugh maybe try to use regular font for S.B. and B.B and only use one font
793 else if (sym == "SB")
795 Molecule r = afm_find("scripts-accSB");
798 else if (sym == "BB")
800 Molecule r = afm_find("scripts-accBB");
803 else if (sym == "OldEE")
805 Molecule r = afm_find("scripts-accOldEE");
808 else if (sym == "OldEES")
810 Molecule r = afm_find("scripts-accOldEES");