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 int minht = 2 * staff_size;
508 int maxht = 7 * minht;
509 int idx = int (((maxht - step) <? y - minht) / step);
512 SCM f = ly_symbol (String ("feta-braces" + to_str (staff_size)));
513 SCM e =gh_list (char_scm_sym, gh_int2scm (idx), SCM_UNDEFINED);
517 m.dim_[Y_AXIS] = Interval (-y/2,y/2);
518 m.dim_[X_AXIS] = Interval (0,0);
524 Lookup::hairpin (Real width, Real height, Real thick, bool decresc, bool continued) const
528 String hairpin = String (decresc ? "de" : "") + "crescendo";
529 Atom at (gh_list (ly_symbol (hairpin),
530 gh_double2scm (thick),
531 gh_double2scm (width),
532 gh_double2scm (height),
533 gh_double2scm (continued ? height/2 : 0.0),
535 m.dim_.x () = Interval (0, width);
536 m.dim_.y () = Interval (-2*height, 2*height);
543 Lookup::tuplet_bracket (Real dy , Real dx, Real thick, Real gap, Real interline_f, Direction dir) const
547 Atom at (gh_list(tuplet_scm_sym,
548 gh_double2scm (interline_f),
552 gh_double2scm (thick),
561 Make a smooth curve along the points
564 Lookup::slur (Array<Offset> controls, Real linethick) const
566 Offset delta_off = controls[3]- controls[0];
570 int indices[] = {5,6,7,4,1,2,3,0};
572 for (int i= 0; i < 8; i++)
573 scontrols[i] = offset2scm (controls[indices[i]]);
576 Atom at (gh_list (ly_symbol ("bezier-sandwich"),
577 ly_quote_scm (array_to_list (scontrols, 8)),
578 gh_double2scm (linethick),
581 m.dim_[X_AXIS] = Interval (0, delta_off[X_AXIS]);
582 m.dim_[Y_AXIS] = Interval (0 <? delta_off[Y_AXIS], 0 >? delta_off[Y_AXIS]);
588 Lookup::staff_bracket (Real y) const
591 Atom at ( gh_list (bracket_scm_sym,
595 m.dim_[Y_AXIS] = Interval (-y/2,y/2);
596 m.dim_[X_AXIS] = Interval (0,4 PT);
598 m.translate_axis (- 4. / 3. * m.dim_[X_AXIS].length (), X_AXIS);
603 Lookup::volta (Real h, Real w, Real thick, bool vert_start, bool vert_end) const
607 Atom at (gh_list (volta_scm_sym,
610 gh_double2scm (thick),
611 gh_int2scm (vert_start),
612 gh_int2scm (vert_end),
615 m.dim_[Y_AXIS] = Interval (- h/2, h/2);
616 m.dim_[X_AXIS] = Interval (0, w);
623 Lookup::accordion (SCM s, Real interline_f) const
626 String sym = ly_scm2string(SCM_CAR(s));
627 String reg = ly_scm2string(SCM_CAR(SCM_CDR(s)));
629 if (sym == "Discant")
631 Molecule r = afm_find("scripts-accDiscant");
633 if (reg.left_str(1) == "F")
635 Molecule d = afm_find("scripts-accDot");
636 d.translate_axis(interline_f * 2.5 PT, Y_AXIS);
638 reg = reg.right_str(reg.length_i()-1);
641 if (reg.left_str(3) == "EEE")
644 reg = reg.right_str(reg.length_i()-3);
646 else if (reg.left_str(2) == "EE")
649 reg = reg.right_str(reg.length_i()-2);
651 else if (reg.left_str(2) == "Eh")
654 reg = reg.right_str(reg.length_i()-2);
656 else if (reg.left_str(1) == "E")
659 reg = reg.right_str(reg.length_i()-1);
663 Molecule d = afm_find("scripts-accDot");
664 d.translate_axis(interline_f * 1.5 PT, Y_AXIS);
669 Molecule d = afm_find("scripts-accDot");
670 d.translate_axis(interline_f * 1.5 PT, Y_AXIS);
671 d.translate_axis(0.8 * interline_f PT, X_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);
681 if (reg.left_str(2) == "SS")
683 Molecule d = afm_find("scripts-accDot");
684 d.translate_axis(0.5 * interline_f PT, Y_AXIS);
685 d.translate_axis(0.4 * interline_f PT, X_AXIS);
687 d.translate_axis(-0.8 * interline_f PT, X_AXIS);
689 reg = reg.right_str(reg.length_i()-2);
691 if (reg.left_str(1) == "S")
693 Molecule d = afm_find("scripts-accDot");
694 d.translate_axis(0.5 * interline_f PT, Y_AXIS);
696 reg = reg.right_str(reg.length_i()-1);
699 else if (sym == "Freebase")
701 Molecule r = afm_find("scripts-accFreebase");
703 if (reg.left_str(1) == "F")
705 Molecule d = afm_find("scripts-accDot");
706 d.translate_axis(interline_f * 1.5 PT, Y_AXIS);
708 reg = reg.right_str(reg.length_i()-1);
712 Molecule d = afm_find("scripts-accDot");
713 d.translate_axis(interline_f * 0.5 PT, Y_AXIS);
717 else if (sym == "Bayanbase")
719 Molecule r = afm_find("scripts-accBayanbase");
721 if (reg.left_str(1) == "T")
723 Molecule d = afm_find("scripts-accDot");
724 d.translate_axis(interline_f * 2.5 PT, Y_AXIS);
726 reg = reg.right_str(reg.length_i()-1);
728 /* include 4' reed just for completeness. You don't want to use this. */
729 if (reg.left_str(1) == "F")
731 Molecule d = afm_find("scripts-accDot");
732 d.translate_axis(interline_f * 1.5 PT, Y_AXIS);
734 reg = reg.right_str(reg.length_i()-1);
736 if (reg.left_str(2) == "EE")
738 Molecule d = afm_find("scripts-accDot");
739 d.translate_axis(interline_f * 0.5 PT, Y_AXIS);
740 d.translate_axis(0.4 * interline_f PT, X_AXIS);
742 d.translate_axis(-0.8 * interline_f PT, X_AXIS);
744 reg = reg.right_str(reg.length_i()-2);
746 if (reg.left_str(1) == "E")
748 Molecule d = afm_find("scripts-accDot");
749 d.translate_axis(interline_f * 0.5 PT, Y_AXIS);
751 reg = reg.right_str(reg.length_i()-1);
754 else if (sym == "Stdbase")
756 Molecule r = afm_find("scripts-accStdbase");
758 if (reg.left_str(1) == "T")
760 Molecule d = afm_find("scripts-accDot");
761 d.translate_axis(interline_f * 3.5 PT, Y_AXIS);
763 reg = reg.right_str(reg.length_i()-1);
765 if (reg.left_str(1) == "F")
767 Molecule d = afm_find("scripts-accDot");
768 d.translate_axis(interline_f * 2.5 PT, Y_AXIS);
770 reg = reg.right_str(reg.length_i()-1);
772 if (reg.left_str(1) == "M")
774 Molecule d = afm_find("scripts-accDot");
775 d.translate_axis(interline_f * 2 PT, Y_AXIS);
776 d.translate_axis(interline_f PT, X_AXIS);
778 reg = reg.right_str(reg.length_i()-1);
780 if (reg.left_str(1) == "E")
782 Molecule d = afm_find("scripts-accDot");
783 d.translate_axis(interline_f * 1.5 PT, Y_AXIS);
785 reg = reg.right_str(reg.length_i()-1);
787 if (reg.left_str(1) == "S")
789 Molecule d = afm_find("scripts-accDot");
790 d.translate_axis(interline_f * 0.5 PT, Y_AXIS);
792 reg = reg.right_str(reg.length_i()-1);
795 /* ugh maybe try to use regular font for S.B. and B.B and only use one font
797 else if (sym == "SB")
799 Molecule r = afm_find("scripts-accSB");
802 else if (sym == "BB")
804 Molecule r = afm_find("scripts-accBB");
807 else if (sym == "OldEE")
809 Molecule r = afm_find("scripts-accOldEE");
812 else if (sym == "OldEES")
814 Molecule r = afm_find("scripts-accOldEES");