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: */
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);
387 if (paper_l->scope_p_->elem_b ("magnification_" + style))
389 font_mag = (int)paper_l->get_var ("magnification_" + style);
390 realmag = pow (1.2, font_mag);
396 SCM l = gh_eval_str (("(style-to-cmr \"" + style + "\")").ch_C());
399 style = ly_scm2string (SCM_CDR(l)) +to_str ((int)font_h);
403 Interval ydims (0,0);
405 Font_metric* afm_l = all_fonts_global_p->find_font (style);
410 for (int i = 0; i < text.length_i (); i++)
415 for (i++; (i < text.length_i ()) && isalpha(text[i]); i++)
417 i--; // Compensate for the increment in the outer loop!
423 else if (text[i] == '}')
425 Character_metric *c = afm_l->get_char ((unsigned char)text[i],false);
427 w += c->dimensions()[X_AXIS].length ();
428 ydims.unite (c->dimensions()[Y_AXIS]);
434 warning (_f ("Non-matching braces in text `%s', adding braces.", text.ch_C()));
438 text = to_str ('{', -brace_count) + text;
442 text = text + to_str ('}', brace_count);
447 m.dim_.x () = Interval (0, w*realmag);
451 Atom at (gh_list (text_scm_sym,
452 gh_str02scm (text.ch_C()),
454 at.font_ = ly_symbol (style);
455 at.magn_ = gh_int2scm (font_mag);
463 Lookup::time_signature (int num, int den, Paper_def *paper_l) const
465 String sty = "number";
466 Molecule n (text (sty, to_str (num), paper_l));
467 Molecule d (text (sty, to_str (den), paper_l));
468 n.align_to (X_AXIS, CENTER);
469 d.align_to (X_AXIS, CENTER);
473 m.add_at_edge (Y_AXIS, UP, n, 0.0);
474 m.add_at_edge (Y_AXIS, DOWN, d, 0.0);
479 m.align_to (Y_AXIS, CENTER);
485 Lookup::staff_brace (Real y, int staff_size) const
489 (define (pianobrace y staffht)
491 (minht (* 2 staffht))
495 (select-font (string-append "feta-braces" (number->string (inexact->exact staffht))) 0)
496 (char (max 0 (/ (- (min y (- maxht step)) minht) step))))
502 int minht = 2 * staff_size;
503 int maxht = 7 * minht;
504 int idx = int (((maxht - step) <? y - minht) / step);
507 SCM f = ly_symbol (String ("feta-braces" + to_str (staff_size)));
508 SCM e =gh_list (char_scm_sym, gh_int2scm (idx), SCM_UNDEFINED);
512 m.dim_[Y_AXIS] = Interval (-y/2,y/2);
513 m.dim_[X_AXIS] = Interval (0,0);
519 Lookup::hairpin (Real width, Real height, Real thick, bool decresc, bool continued) const
523 String hairpin = String (decresc ? "de" : "") + "crescendo";
524 Atom at (gh_list (ly_symbol (hairpin),
525 gh_double2scm (thick),
526 gh_double2scm (width),
527 gh_double2scm (height),
528 gh_double2scm (continued ? height/2 : 0.0),
530 m.dim_.x () = Interval (0, width);
531 m.dim_.y () = Interval (-2*height, 2*height);
538 Lookup::tuplet_bracket (Real dy , Real dx, Real thick, Real gap, Real interline_f, Direction dir) const
542 Atom at (gh_list(tuplet_scm_sym,
543 gh_double2scm (interline_f),
547 gh_double2scm (thick),
556 Make a smooth curve along the points
559 Lookup::slur (Array<Offset> controls, Real linethick) const
561 Offset delta_off = controls[3]- controls[0];
565 int indices[] = {5,6,7,4,1,2,3,0};
567 for (int i= 0; i < 8; i++)
568 scontrols[i] = offset2scm (controls[indices[i]]);
571 Atom at (gh_list (ly_symbol ("bezier-sandwich"),
572 ly_quote_scm (array_to_list (scontrols, 8)),
573 gh_double2scm (linethick),
576 m.dim_[X_AXIS] = Interval (0, delta_off[X_AXIS]);
577 m.dim_[Y_AXIS] = Interval (0 <? delta_off[Y_AXIS], 0 >? delta_off[Y_AXIS]);
583 Lookup::staff_bracket (Real y) const
586 Atom at ( gh_list (bracket_scm_sym,
590 m.dim_[Y_AXIS] = Interval (-y/2,y/2);
591 m.dim_[X_AXIS] = Interval (0,4 PT);
593 m.translate_axis (- 4. / 3. * m.dim_[X_AXIS].length (), X_AXIS);
598 Lookup::volta (Real h, Real w, Real thick, bool vert_start, bool vert_end) const
602 Atom at (gh_list (volta_scm_sym,
605 gh_double2scm (thick),
606 gh_int2scm (vert_start),
607 gh_int2scm (vert_end),
610 m.dim_[Y_AXIS] = Interval (- h/2, h/2);
611 m.dim_[X_AXIS] = Interval (0, w);
618 Lookup::accordion (SCM s, Real interline_f) const
621 String sym = ly_scm2string(SCM_CAR(s));
622 String reg = ly_scm2string(SCM_CAR(SCM_CDR(s)));
624 if (sym == "Discant")
626 Molecule r = afm_find("scripts-accDiscant");
628 if (reg.left_str(1) == "F")
630 Molecule d = afm_find("scripts-accDot");
631 d.translate_axis(interline_f * 2.5 PT, Y_AXIS);
633 reg = reg.right_str(reg.length_i()-1);
636 if (reg.left_str(3) == "EEE")
639 reg = reg.right_str(reg.length_i()-3);
641 else if (reg.left_str(2) == "EE")
644 reg = reg.right_str(reg.length_i()-2);
646 else if (reg.left_str(2) == "Eh")
649 reg = reg.right_str(reg.length_i()-2);
651 else if (reg.left_str(1) == "E")
654 reg = reg.right_str(reg.length_i()-1);
658 Molecule d = afm_find("scripts-accDot");
659 d.translate_axis(interline_f * 1.5 PT, Y_AXIS);
664 Molecule d = afm_find("scripts-accDot");
665 d.translate_axis(interline_f * 1.5 PT, Y_AXIS);
666 d.translate_axis(0.8 * interline_f PT, X_AXIS);
671 Molecule d = afm_find("scripts-accDot");
672 d.translate_axis(interline_f * 1.5 PT, Y_AXIS);
673 d.translate_axis(-0.8 * interline_f PT, X_AXIS);
676 if (reg.left_str(2) == "SS")
678 Molecule d = afm_find("scripts-accDot");
679 d.translate_axis(0.5 * interline_f PT, Y_AXIS);
680 d.translate_axis(0.4 * interline_f PT, X_AXIS);
682 d.translate_axis(-0.8 * interline_f PT, X_AXIS);
684 reg = reg.right_str(reg.length_i()-2);
686 if (reg.left_str(1) == "S")
688 Molecule d = afm_find("scripts-accDot");
689 d.translate_axis(0.5 * interline_f PT, Y_AXIS);
691 reg = reg.right_str(reg.length_i()-1);
694 else if (sym == "Freebase")
696 Molecule r = afm_find("scripts-accFreebase");
698 if (reg.left_str(1) == "F")
700 Molecule d = afm_find("scripts-accDot");
701 d.translate_axis(interline_f * 1.5 PT, Y_AXIS);
703 reg = reg.right_str(reg.length_i()-1);
707 Molecule d = afm_find("scripts-accDot");
708 d.translate_axis(interline_f * 0.5 PT, Y_AXIS);
712 else if (sym == "Bayanbase")
714 Molecule r = afm_find("scripts-accBayanbase");
716 if (reg.left_str(1) == "T")
718 Molecule d = afm_find("scripts-accDot");
719 d.translate_axis(interline_f * 2.5 PT, Y_AXIS);
721 reg = reg.right_str(reg.length_i()-1);
723 /* include 4' reed just for completeness. You don't want to use this. */
724 if (reg.left_str(1) == "F")
726 Molecule d = afm_find("scripts-accDot");
727 d.translate_axis(interline_f * 1.5 PT, Y_AXIS);
729 reg = reg.right_str(reg.length_i()-1);
731 if (reg.left_str(2) == "EE")
733 Molecule d = afm_find("scripts-accDot");
734 d.translate_axis(interline_f * 0.5 PT, Y_AXIS);
735 d.translate_axis(0.4 * interline_f PT, X_AXIS);
737 d.translate_axis(-0.8 * interline_f PT, X_AXIS);
739 reg = reg.right_str(reg.length_i()-2);
741 if (reg.left_str(1) == "E")
743 Molecule d = afm_find("scripts-accDot");
744 d.translate_axis(interline_f * 0.5 PT, Y_AXIS);
746 reg = reg.right_str(reg.length_i()-1);
749 else if (sym == "Stdbase")
751 Molecule r = afm_find("scripts-accStdbase");
753 if (reg.left_str(1) == "T")
755 Molecule d = afm_find("scripts-accDot");
756 d.translate_axis(interline_f * 3.5 PT, Y_AXIS);
758 reg = reg.right_str(reg.length_i()-1);
760 if (reg.left_str(1) == "F")
762 Molecule d = afm_find("scripts-accDot");
763 d.translate_axis(interline_f * 2.5 PT, Y_AXIS);
765 reg = reg.right_str(reg.length_i()-1);
767 if (reg.left_str(1) == "M")
769 Molecule d = afm_find("scripts-accDot");
770 d.translate_axis(interline_f * 2 PT, Y_AXIS);
771 d.translate_axis(interline_f PT, X_AXIS);
773 reg = reg.right_str(reg.length_i()-1);
775 if (reg.left_str(1) == "E")
777 Molecule d = afm_find("scripts-accDot");
778 d.translate_axis(interline_f * 1.5 PT, Y_AXIS);
780 reg = reg.right_str(reg.length_i()-1);
782 if (reg.left_str(1) == "S")
784 Molecule d = afm_find("scripts-accDot");
785 d.translate_axis(interline_f * 0.5 PT, Y_AXIS);
787 reg = reg.right_str(reg.length_i()-1);
790 /* ugh maybe try to use regular font for S.B. and B.B and only use one font
792 else if (sym == "SB")
794 Molecule r = afm_find("scripts-accSB");
797 else if (sym == "BB")
799 Molecule r = afm_find("scripts-accBB");
802 else if (sym == "OldEE")
804 Molecule r = afm_find("scripts-accOldEE");
807 else if (sym == "OldEES")
809 Molecule r = afm_find("scripts-accOldEES");