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"
38 Lookup::Lookup (Lookup const& s)
40 font_name_ = s.font_name_;
47 build a ledger line for small pieces.
50 Lookup::ledger_line (Interval xwid) const
52 Drul_array<Molecule> endings;
53 endings[LEFT] = afm_find ("noteheads-ledgerending");
54 Molecule * e = &endings[LEFT];
57 Real thick = e->dim_[Y_AXIS].length();
58 Real len = e->dim_[X_AXIS].length () - thick;
63 endings[d].translate_axis (xwid[d] - endings[d].dim_[X_AXIS][d], X_AXIS);
64 total.add_molecule (endings[d]);
65 } while ((flip(&d)) != LEFT);
67 Real xpos = xwid [LEFT] + len;
69 while (xpos + len + thick /2 <= xwid[RIGHT])
71 e->translate_axis (len, X_AXIS);
72 total.add_molecule (*e);
81 Lookup::accidental (int j, bool cautionary) const
83 Molecule m(afm_find (String ("accidentals-") + to_str (j)));
86 Molecule open = afm_find (String ("accidentals-("));
87 Molecule close = afm_find (String ("accidentals-)"));
88 m.add_at_edge(X_AXIS, LEFT, Molecule(open), 0);
89 m.add_at_edge(X_AXIS, RIGHT, Molecule(close), 0);
97 Lookup::afm_find (String s, bool warn) const
101 Lookup * me = (Lookup*)(this);
102 me->afm_l_ = all_fonts_global_p->find_afm (font_name_);
105 warning (_f("Can't open `%s'\n", font_name_));
106 warning (_f("Search path %s\n", global_path.str ().ch_C()));
107 error (_f("Aborting"));
110 Adobe_font_char_metric cm = afm_l_->find_char (s, warn);
115 don't want people relying on this kind of dimension.
121 Atom at (gh_list (char_scm_sym,
122 gh_int2scm (cm.code ()),
124 at.font_ = ly_symbol (font_name_.ch_C());
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) 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) const
162 return staff_bracket (h);
164 return staff_brace (h);
166 Real kern = paper_l_->get_var ("bar_kern");
167 Real thinkern = paper_l_->get_var ("bar_thinkern");
168 Molecule thin = simple_bar ("thin", h);
169 Molecule thick = simple_bar ("thick", h);
170 Molecule colon = afm_find ("dots-repeatcolon");
176 return fill (Box (Interval(0, 0), Interval (-h/2, h/2)));
178 if (str == "scorepostbreak")
180 return simple_bar ("score", h);
186 else if (str == "|.")
188 m.add_at_edge (X_AXIS, LEFT, thick, 0);
189 m.add_at_edge (X_AXIS, LEFT, thin, kern);
191 else if (str == ".|")
193 m.add_at_edge (X_AXIS, RIGHT, thick, 0);
194 m.add_at_edge (X_AXIS, RIGHT, thin, kern);
196 else if (str == ":|")
198 m.add_at_edge (X_AXIS, LEFT, thick, 0);
199 m.add_at_edge (X_AXIS, LEFT, thin, kern);
200 m.add_at_edge (X_AXIS, LEFT, colon, kern);
202 else if (str == "|:")
204 m.add_at_edge (X_AXIS, RIGHT, thick, 0);
205 m.add_at_edge (X_AXIS, RIGHT, thin, kern);
206 m.add_at_edge (X_AXIS, RIGHT, colon, kern);
208 else if (str == ":|:")
210 m.add_at_edge (X_AXIS, LEFT, thick, thinkern);
211 m.add_at_edge (X_AXIS, LEFT, colon, kern);
212 m.add_at_edge (X_AXIS, RIGHT, thick, kern);
213 m.add_at_edge (X_AXIS, RIGHT, colon, kern);
215 else if (str == ".|.")
217 m.add_at_edge (X_AXIS, LEFT, thick, thinkern);
218 m.add_at_edge (X_AXIS, RIGHT, thick, kern);
220 else if (str == "||")
222 m.add_at_edge (X_AXIS, RIGHT, thin, 0);
223 m.add_at_edge (X_AXIS, RIGHT, thin, thinkern);
230 Lookup::beam (Real slope, Real width, Real thick) const
232 Real height = slope * width;
233 Real min_y = (0 <? height) - thick/2;
234 Real max_y = (0 >? height) + thick/2;
239 (gh_list (beam_scm_sym,
240 gh_double2scm (width),
241 gh_double2scm (slope),
242 gh_double2scm (thick),
245 m.dim_[X_AXIS] = Interval (0, width);
246 m.dim_[Y_AXIS] = Interval (min_y, max_y);
252 Lookup::clef (String st) const
254 return afm_find (String ("clefs-" + st));
258 offset2scm (Offset o)
260 return gh_list (gh_double2scm (o[X_AXIS]), gh_double2scm(o[Y_AXIS]),
265 Lookup::dashed_slur (Array<Offset> controls, Real thick, Real dash) const
267 assert (controls.size () == 8);
268 Offset d = controls[3] - controls[0];
276 m.dim_[X_AXIS] = Interval (0, dx);
277 m.dim_[Y_AXIS] = Interval (0 <? dy, 0 >? dy);
280 for (int i=0; i< 4; i++)
282 sc[i] = offset2scm (controls[i]);
286 (gh_list (ly_symbol ("dashed-slur"),
287 gh_double2scm (thick),
288 gh_double2scm (dash),
289 ly_quote_scm (array_to_list (sc, 4)),
298 Lookup::dots () const
300 return afm_find (String ("dots-dot"));
306 Lookup::fill (Box b) const
314 Lookup::flag (int j, Direction d) const
316 char c = (d == UP) ? 'u' : 'd';
317 return afm_find (String ("flags-") + to_str (c) + to_str (j));
321 Lookup::rest (int j, bool o, String style) const
323 return afm_find (String ("rests-") + to_str (j) + (o ? "o" : "") + style);
328 Lookup::special_time_signature (String s, int n, int d) const
330 // First guess: s contains only the signature style
331 String symbolname = "timesig-" + s + to_str (n) + "/" + to_str (d);
333 Molecule m = afm_find (symbolname, false);
337 // Second guess: s contains the full signature name
338 m = afm_find ("timesig-"+s, false);
342 // Resort to default layout with numbers
343 return time_signature (n,d);
347 Lookup::filledbox (Box b ) const
351 Atom at (gh_list (filledbox_scm_sym,
352 gh_double2scm (-b[X_AXIS][LEFT]),
353 gh_double2scm (b[X_AXIS][RIGHT]),
354 gh_double2scm (-b[Y_AXIS][DOWN]),
355 gh_double2scm (b[Y_AXIS][UP]),
364 Lookup::stem (Real y1, Real y2) const
372 Real stem_width = paper_l_->get_var ("stemthickness");
373 return filledbox (Box (Interval (-stem_width/2,stem_width/2),
380 Magnification steps. These are powers of 1.2. The numbers are
381 taken from Knuth's plain.tex: */
382 static Real mag_steps[] = {1, 1, 1.200, 1.440, 1.7280, 2.074, 2.488};
385 TODO: THIS IS UGLY. Since the user has direct access to TeX
386 strings, we try some halfbaked attempt to detect TeX trickery.
390 Lookup::text (String style, String text) const
393 if (style.empty_b ())
397 Real font_h = paper_l_->get_var ("font_normal");
398 if (paper_l_->scope_p_->elem_b ("font_" + style))
400 font_h = paper_l_->get_var ("font_" + style);
403 if (paper_l_->scope_p_->elem_b ("magnification_" + style))
405 font_mag = (int)paper_l_->get_var ("magnification_" + style);
411 SCM l = gh_eval_str (("(style-to-cmr \"" + style + "\")").ch_C());
414 style = ly_scm2string (SCM_CDR(l)) +to_str ((int)font_h);
418 Interval ydims (0,0);
420 Font_metric* afm_l = all_fonts_global_p->find_font (style);
425 for (int i = 0; i < text.length_i (); i++)
429 for (i++; (i < text.length_i ()) && isalpha(text[i]); i++)
435 else if (text[i] == '}')
437 Character_metric *c = afm_l->get_char ((unsigned char)text[i],false);
439 w += c->dimensions()[X_AXIS].length ();
440 ydims.unite (c->dimensions()[Y_AXIS]);
444 if (font_mag > 1 && font_mag < 7 )
447 style = style + String(" scaled \\magstep ") + to_str (font_mag);
448 w *= mag_steps[font_mag];
449 ydims *= mag_steps[font_mag];
454 warning (_f ("Non-matching braces in text `%s', adding braces.", text.ch_C()));
458 text = to_str ('{', -brace_count) + text;
462 text = text + to_str ('}', brace_count);
467 DOUT << "\n" << to_str (w) << "\n";
468 m.dim_.x () = Interval (0, w);
472 Atom at (gh_list (text_scm_sym,
473 gh_str02scm (text.ch_C()),
475 at.font_ = ly_symbol (style);
483 Lookup::time_signature (int num, int den) const
485 String sty = "number";
486 Molecule n (text (sty, to_str (num)));
487 Molecule d (text (sty, to_str (den)));
488 n.align_to (X_AXIS, CENTER);
489 d.align_to (X_AXIS, CENTER);
493 m.add_at_edge (Y_AXIS, UP, n, 0.0);
494 m.add_at_edge (Y_AXIS, DOWN, d, 0.0);
499 m.align_to (Y_AXIS, CENTER);
505 Lookup::staff_brace (Real y) const
509 Atom at (gh_list (pianobrace_scm_sym,
514 m.dim_[Y_AXIS] = Interval (-y/2,y/2);
515 m.dim_[X_AXIS] = Interval (0,0);
521 Lookup::hairpin (Real width, bool decresc, bool continued) const
524 Real height = paper_l_->staffheight_f () / 6;
526 String hairpin = String (decresc ? "de" : "") + "crescendo";
527 Atom at (gh_list (ly_symbol (hairpin),
528 gh_double2scm (width),
529 gh_double2scm (height),
530 gh_double2scm (continued ? height/2 : 0.0),
532 m.dim_.x () = Interval (0, width);
533 m.dim_.y () = Interval (-2*height, 2*height);
540 Lookup::plet (Real dy , Real dx, Direction dir) const
543 SCM thick = tuplet_thick_scm_sym;
545 if (paper_l_->scope_p_->elem_b (thick))
547 t = paper_l_->get_realvar (thick);
550 Real interline_f = paper_l_->get_realvar (interline_scm_sym);
551 Atom at (gh_list(tuplet_scm_sym,
552 gh_double2scm (interline_f),
564 Make a smooth curve along the points
567 Lookup::slur (Array<Offset> controls) const
569 Offset delta_off = controls[3]- controls[0];
573 int indices[] = {5,6,7,4,1,2,3,0};
575 for (int i= 0; i < 8; i++)
576 scontrols[i] = offset2scm (controls[indices[i]]);
579 Atom at (gh_list (ly_symbol ("bezier-sandwich"),
580 ly_quote_scm (array_to_list (scontrols, 8)),
583 m.dim_[X_AXIS] = Interval (0, delta_off[X_AXIS]);
584 m.dim_[Y_AXIS] = Interval (0 <? delta_off[Y_AXIS], 0 >? delta_off[Y_AXIS]);
590 Lookup::staff_bracket (Real y) const
593 Atom at ( gh_list (bracket_scm_sym,
597 m.dim_[Y_AXIS] = Interval (-y/2,y/2);
598 m.dim_[X_AXIS] = Interval (0,4 PT);
600 m.translate_axis (- 4. / 3. * m.dim_[X_AXIS].length (), X_AXIS);
605 Lookup::volta (Real w, bool last_b) const
608 SCM thick = volta_thick_scm_sym;
610 if (paper_l_->scope_p_->elem_b (thick))
612 t = paper_l_->get_realvar (thick);
614 Atom at (gh_list (volta_scm_sym,
620 Real interline_f = paper_l_->get_realvar (interline_scm_sym);
622 m.dim_[Y_AXIS] = Interval (-interline_f, interline_f);
623 m.dim_[X_AXIS] = Interval (0, w);
630 Lookup::accordion (SCM s) const
633 String sym = ly_scm2string(SCM_CAR(s));
634 String reg = ly_scm2string(SCM_CAR(SCM_CDR(s)));
635 Real interline_f = paper_l_->get_realvar(interline_scm_sym);
636 if (sym == "Discant")
638 Molecule r = afm_find("scripts-accDiscant");
640 if (reg.left_str(1) == "F")
642 Molecule d = afm_find("scripts-accDot");
643 d.translate_axis(interline_f * 2.5 PT, Y_AXIS);
645 reg = reg.right_str(reg.length_i()-1);
648 if (reg.left_str(3) == "EEE")
651 reg = reg.right_str(reg.length_i()-3);
653 else if (reg.left_str(2) == "EE")
656 reg = reg.right_str(reg.length_i()-2);
658 else if (reg.left_str(2) == "Eh")
661 reg = reg.right_str(reg.length_i()-2);
663 else if (reg.left_str(1) == "E")
666 reg = reg.right_str(reg.length_i()-1);
670 Molecule d = afm_find("scripts-accDot");
671 d.translate_axis(interline_f * 1.5 PT, Y_AXIS);
676 Molecule d = afm_find("scripts-accDot");
677 d.translate_axis(interline_f * 1.5 PT, Y_AXIS);
678 d.translate_axis(0.8 * interline_f PT, X_AXIS);
683 Molecule d = afm_find("scripts-accDot");
684 d.translate_axis(interline_f * 1.5 PT, Y_AXIS);
685 d.translate_axis(-0.8 * interline_f PT, X_AXIS);
688 if (reg.left_str(2) == "SS")
690 Molecule d = afm_find("scripts-accDot");
691 d.translate_axis(0.5 * interline_f PT, Y_AXIS);
692 d.translate_axis(0.4 * interline_f PT, X_AXIS);
694 d.translate_axis(-0.8 * interline_f PT, X_AXIS);
696 reg = reg.right_str(reg.length_i()-2);
698 if (reg.left_str(1) == "S")
700 Molecule d = afm_find("scripts-accDot");
701 d.translate_axis(0.5 * interline_f PT, Y_AXIS);
703 reg = reg.right_str(reg.length_i()-1);
706 else if (sym == "Freebase")
708 Molecule r = afm_find("scripts-accFreebase");
710 if (reg.left_str(1) == "F")
712 Molecule d = afm_find("scripts-accDot");
713 d.translate_axis(interline_f * 1.5 PT, Y_AXIS);
715 reg = reg.right_str(reg.length_i()-1);
719 Molecule d = afm_find("scripts-accDot");
720 d.translate_axis(interline_f * 0.5 PT, Y_AXIS);
724 else if (sym == "Bayanbase")
726 Molecule r = afm_find("scripts-accBayanbase");
728 if (reg.left_str(1) == "T")
730 Molecule d = afm_find("scripts-accDot");
731 d.translate_axis(interline_f * 2.5 PT, Y_AXIS);
733 reg = reg.right_str(reg.length_i()-1);
735 /* include 4' reed just for completeness. You don't want to use this. */
736 if (reg.left_str(1) == "F")
738 Molecule d = afm_find("scripts-accDot");
739 d.translate_axis(interline_f * 1.5 PT, Y_AXIS);
741 reg = reg.right_str(reg.length_i()-1);
743 if (reg.left_str(2) == "EE")
745 Molecule d = afm_find("scripts-accDot");
746 d.translate_axis(interline_f * 0.5 PT, Y_AXIS);
747 d.translate_axis(0.4 * interline_f PT, X_AXIS);
749 d.translate_axis(-0.8 * interline_f PT, X_AXIS);
751 reg = reg.right_str(reg.length_i()-2);
753 if (reg.left_str(1) == "E")
755 Molecule d = afm_find("scripts-accDot");
756 d.translate_axis(interline_f * 0.5 PT, Y_AXIS);
758 reg = reg.right_str(reg.length_i()-1);
761 else if (sym == "Stdbase")
763 Molecule r = afm_find("scripts-accStdbase");
765 if (reg.left_str(1) == "T")
767 Molecule d = afm_find("scripts-accDot");
768 d.translate_axis(interline_f * 3.5 PT, Y_AXIS);
770 reg = reg.right_str(reg.length_i()-1);
772 if (reg.left_str(1) == "F")
774 Molecule d = afm_find("scripts-accDot");
775 d.translate_axis(interline_f * 2.5 PT, Y_AXIS);
777 reg = reg.right_str(reg.length_i()-1);
779 if (reg.left_str(1) == "M")
781 Molecule d = afm_find("scripts-accDot");
782 d.translate_axis(interline_f * 2 PT, Y_AXIS);
783 d.translate_axis(interline_f PT, X_AXIS);
785 reg = reg.right_str(reg.length_i()-1);
787 if (reg.left_str(1) == "E")
789 Molecule d = afm_find("scripts-accDot");
790 d.translate_axis(interline_f * 1.5 PT, Y_AXIS);
792 reg = reg.right_str(reg.length_i()-1);
794 if (reg.left_str(1) == "S")
796 Molecule d = afm_find("scripts-accDot");
797 d.translate_axis(interline_f * 0.5 PT, Y_AXIS);
799 reg = reg.right_str(reg.length_i()-1);
802 /* ugh maybe try to use regular font for S.B. and B.B and only use one font
804 else if (sym == "SB")
806 Molecule r = afm_find("scripts-accSB");
809 else if (sym == "BB")
811 Molecule r = afm_find("scripts-accBB");
814 else if (sym == "OldEE")
816 Molecule r = afm_find("scripts-accOldEE");
819 else if (sym == "OldEES")
821 Molecule r = afm_find("scripts-accOldEES");