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
135 if (type == "harmonic" || type == "cross")
138 return afm_find (String ("noteheads-") + to_str (j) + type);
142 Lookup::simple_bar (String type, Real h, Paper_def* paper_l) const
144 SCM thick = ly_symbol ("barthick_" + type);
147 if (paper_l->scope_p_->elem_b (thick))
149 w = paper_l->get_realvar (thick);
153 programming_error ("No bar thickness set ! ");
156 return filledbox (Box (Interval(0,w), Interval(-h/2, h/2)));
161 Lookup::bar (String str, Real h, Paper_def *paper_l) const
163 if (str == "bracket")
164 return staff_bracket (h);
165 else if (str == "brace")
167 Real staffht = paper_l->get_var ("staffheight");
168 return staff_brace (h,staffht);
170 Real kern = paper_l->get_var ("bar_kern");
171 Real thinkern = paper_l->get_var ("bar_thinkern");
173 Molecule thin = simple_bar ("thin", h, paper_l);
174 Molecule thick = simple_bar ("thick", h, paper_l);
175 Molecule colon = afm_find ("dots-repeatcolon", paper_l);
181 return fill (Box (Interval(0, 0), Interval (-h/2, h/2)));
183 if (str == "scorepostbreak")
185 return simple_bar ("score", h, paper_l);
191 else if (str == "|.")
193 m.add_at_edge (X_AXIS, LEFT, thick, 0);
194 m.add_at_edge (X_AXIS, LEFT, thin, kern);
196 else if (str == ".|")
198 m.add_at_edge (X_AXIS, RIGHT, thick, 0);
199 m.add_at_edge (X_AXIS, RIGHT, thin, kern);
201 else if (str == ":|")
203 m.add_at_edge (X_AXIS, LEFT, thick, 0);
204 m.add_at_edge (X_AXIS, LEFT, thin, kern);
205 m.add_at_edge (X_AXIS, LEFT, colon, kern);
207 else if (str == "|:")
209 m.add_at_edge (X_AXIS, RIGHT, thick, 0);
210 m.add_at_edge (X_AXIS, RIGHT, thin, kern);
211 m.add_at_edge (X_AXIS, RIGHT, colon, kern);
213 else if (str == ":|:")
215 m.add_at_edge (X_AXIS, LEFT, thick, thinkern);
216 m.add_at_edge (X_AXIS, LEFT, colon, kern);
217 m.add_at_edge (X_AXIS, RIGHT, thick, kern);
218 m.add_at_edge (X_AXIS, RIGHT, colon, kern);
220 else if (str == ".|.")
222 m.add_at_edge (X_AXIS, LEFT, thick, thinkern);
223 m.add_at_edge (X_AXIS, RIGHT, thick, kern);
225 else if (str == "||")
227 m.add_at_edge (X_AXIS, RIGHT, thin, 0);
228 m.add_at_edge (X_AXIS, RIGHT, thin, thinkern);
235 Lookup::beam (Real slope, Real width, Real thick) const
237 Real height = slope * width;
238 Real min_y = (0 <? height) - thick/2;
239 Real max_y = (0 >? height) + thick/2;
244 (gh_list (beam_scm_sym,
245 gh_double2scm (width),
246 gh_double2scm (slope),
247 gh_double2scm (thick),
250 m.dim_[X_AXIS] = Interval (0, width);
251 m.dim_[Y_AXIS] = Interval (min_y, max_y);
257 Lookup::clef (String st) const
259 return afm_find (String ("clefs-" + st));
263 offset2scm (Offset o)
265 return gh_list (gh_double2scm (o[X_AXIS]), gh_double2scm(o[Y_AXIS]),
270 Lookup::dashed_slur (Array<Offset> controls, Real thick, Real dash) const
272 assert (controls.size () == 8);
273 Offset d = controls[3] - controls[0];
281 m.dim_[X_AXIS] = Interval (0, dx);
282 m.dim_[Y_AXIS] = Interval (0 <? dy, 0 >? dy);
285 for (int i=0; i< 4; i++)
287 sc[i] = offset2scm (controls[i]);
291 (gh_list (ly_symbol ("dashed-slur"),
292 gh_double2scm (thick),
293 gh_double2scm (dash),
294 ly_quote_scm (array_to_list (sc, 4)),
303 Lookup::dots () const
305 return afm_find (String ("dots-dot"));
311 Lookup::fill (Box b) const
319 Lookup::rest (int j, bool o, String style) const
321 return afm_find (String ("rests-") + to_str (j) + (o ? "o" : "") + style);
326 Lookup::special_time_signature (String s, int n, int d, Paper_def*pap) const
328 // First guess: s contains only the signature style
329 String symbolname = "timesig-" + s + to_str (n) + "/" + to_str (d);
331 Molecule m = afm_find (symbolname, false);
335 // Second guess: s contains the full signature name
336 m = afm_find ("timesig-"+s, false);
340 // Resort to default layout with numbers
341 return time_signature (n,d,pap);
345 Lookup::filledbox (Box b ) const
349 Atom at (gh_list (filledbox_scm_sym,
350 gh_double2scm (-b[X_AXIS][LEFT]),
351 gh_double2scm (b[X_AXIS][RIGHT]),
352 gh_double2scm (-b[Y_AXIS][DOWN]),
353 gh_double2scm (b[Y_AXIS][UP]),
364 Magnification steps. These are powers of 1.2. The numbers are
365 taken from Knuth's plain.tex: */
369 TODO: THIS IS UGLY. Since the user has direct access to TeX
370 strings, we try some halfbaked attempt to detect TeX trickery.
374 Lookup::text (String style, String text, Paper_def *paper_l) const
377 if (style.empty_b ())
381 Real font_h = paper_l->get_var ("font_normal");
382 if (paper_l->scope_p_->elem_b ("font_" + style))
384 font_h = paper_l->get_var ("font_" + style);
389 if (paper_l->scope_p_->elem_b ("magnification_" + style))
391 font_mag = (int)paper_l->get_var ("magnification_" + style);
392 realmag = pow (1.2, font_mag);
398 SCM l = ly_ch_C_eval_scm (("(style-to-cmr \"" + style + "\")").ch_C());
401 style = ly_scm2string (SCM_CDR(l)) +to_str ((int)font_h);
405 Interval ydims (0,0);
407 Font_metric* afm_l = all_fonts_global_p->find_font (style);
412 for (int i = 0; i < text.length_i (); i++)
417 for (i++; (i < text.length_i ()) && isalpha(text[i]); i++)
419 i--; // Compensate for the increment in the outer loop!
425 else if (text[i] == '}')
427 Character_metric *c = (Character_metric*)afm_l->get_char ((unsigned char)text[i],false);
429 w += c->dimensions()[X_AXIS].length ();
430 ydims.unite (c->dimensions()[Y_AXIS]);
436 warning (_f ("Non-matching braces in text `%s', adding braces.", text.ch_C()));
440 text = to_str ('{', -brace_count) + text;
444 text = text + to_str ('}', brace_count);
449 m.dim_.x () = Interval (0, w*realmag);
453 Atom at (gh_list (text_scm_sym,
454 ly_ch_C_to_scm (text.ch_C()),
456 at.font_ = ly_symbol (style);
457 at.magn_ = gh_int2scm (font_mag);
465 Lookup::time_signature (int num, int den, Paper_def *paper_l) const
467 String sty = "number";
468 Molecule n (text (sty, to_str (num), paper_l));
469 Molecule d (text (sty, to_str (den), paper_l));
470 n.align_to (X_AXIS, CENTER);
471 d.align_to (X_AXIS, CENTER);
475 m.add_at_edge (Y_AXIS, UP, n, 0.0);
476 m.add_at_edge (Y_AXIS, DOWN, d, 0.0);
481 m.align_to (Y_AXIS, CENTER);
487 Lookup::staff_brace (Real y, int staff_size) const
491 (define (pianobrace y staffht)
493 (minht (* 2 staffht))
497 (select-font (string-append "feta-braces" (number->string (inexact->exact staffht))) 0)
498 (char (max 0 (/ (- (min y (- maxht step)) minht) step))))
504 int minht = 2 * staff_size;
505 int maxht = 7 * minht;
506 int idx = int (((maxht - step) <? y - minht) / step);
509 SCM f = ly_symbol (String ("feta-braces" + to_str (staff_size)));
510 SCM e =gh_list (char_scm_sym, gh_int2scm (idx), SCM_UNDEFINED);
514 m.dim_[Y_AXIS] = Interval (-y/2,y/2);
515 m.dim_[X_AXIS] = Interval (0,0);
521 Lookup::hairpin (Real width, Real height, Real thick, bool decresc, bool continued) const
525 String hairpin = String (decresc ? "de" : "") + "crescendo";
526 Atom at (gh_list (ly_symbol (hairpin),
527 gh_double2scm (thick),
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::tuplet_bracket (Real dy , Real dx, Real thick, Real gap, Real interline_f, Direction dir) const
544 Atom at (gh_list(tuplet_scm_sym,
545 gh_double2scm (interline_f),
549 gh_double2scm (thick),
558 Make a smooth curve along the points
561 Lookup::slur (Array<Offset> controls, Real linethick) const
563 Offset delta_off = controls[3]- controls[0];
567 int indices[] = {5,6,7,4,1,2,3,0};
569 for (int i= 0; i < 8; i++)
570 scontrols[i] = offset2scm (controls[indices[i]]);
573 Atom at (gh_list (ly_symbol ("bezier-sandwich"),
574 ly_quote_scm (array_to_list (scontrols, 8)),
575 gh_double2scm (linethick),
578 m.dim_[X_AXIS] = Interval (0, delta_off[X_AXIS]);
579 m.dim_[Y_AXIS] = Interval (0 <? delta_off[Y_AXIS], 0 >? delta_off[Y_AXIS]);
585 Lookup::staff_bracket (Real y) const
588 Atom at ( gh_list (bracket_scm_sym,
592 m.dim_[Y_AXIS] = Interval (-y/2,y/2);
593 m.dim_[X_AXIS] = Interval (0,4 PT);
595 m.translate_axis (- 4. / 3. * m.dim_[X_AXIS].length (), X_AXIS);
600 Lookup::volta (Real h, Real w, Real thick, bool vert_start, bool vert_end) const
604 Atom at (gh_list (volta_scm_sym,
607 gh_double2scm (thick),
608 gh_int2scm (vert_start),
609 gh_int2scm (vert_end),
612 m.dim_[Y_AXIS] = Interval (- h/2, h/2);
613 m.dim_[X_AXIS] = Interval (0, w);
620 Lookup::accordion (SCM s, Real interline_f) const
623 String sym = ly_scm2string(SCM_CAR(s));
624 String reg = ly_scm2string(SCM_CAR(SCM_CDR(s)));
626 if (sym == "Discant")
628 Molecule r = afm_find("scripts-accDiscant");
630 if (reg.left_str(1) == "F")
632 Molecule d = afm_find("scripts-accDot");
633 d.translate_axis(interline_f * 2.5 PT, Y_AXIS);
635 reg = reg.right_str(reg.length_i()-1);
638 if (reg.left_str(3) == "EEE")
641 reg = reg.right_str(reg.length_i()-3);
643 else if (reg.left_str(2) == "EE")
646 reg = reg.right_str(reg.length_i()-2);
648 else if (reg.left_str(2) == "Eh")
651 reg = reg.right_str(reg.length_i()-2);
653 else if (reg.left_str(1) == "E")
656 reg = reg.right_str(reg.length_i()-1);
660 Molecule d = afm_find("scripts-accDot");
661 d.translate_axis(interline_f * 1.5 PT, Y_AXIS);
666 Molecule d = afm_find("scripts-accDot");
667 d.translate_axis(interline_f * 1.5 PT, Y_AXIS);
668 d.translate_axis(0.8 * interline_f PT, X_AXIS);
673 Molecule d = afm_find("scripts-accDot");
674 d.translate_axis(interline_f * 1.5 PT, Y_AXIS);
675 d.translate_axis(-0.8 * interline_f PT, X_AXIS);
678 if (reg.left_str(2) == "SS")
680 Molecule d = afm_find("scripts-accDot");
681 d.translate_axis(0.5 * interline_f PT, Y_AXIS);
682 d.translate_axis(0.4 * interline_f PT, X_AXIS);
684 d.translate_axis(-0.8 * interline_f PT, X_AXIS);
686 reg = reg.right_str(reg.length_i()-2);
688 if (reg.left_str(1) == "S")
690 Molecule d = afm_find("scripts-accDot");
691 d.translate_axis(0.5 * interline_f PT, Y_AXIS);
693 reg = reg.right_str(reg.length_i()-1);
696 else if (sym == "Freebase")
698 Molecule r = afm_find("scripts-accFreebase");
700 if (reg.left_str(1) == "F")
702 Molecule d = afm_find("scripts-accDot");
703 d.translate_axis(interline_f * 1.5 PT, Y_AXIS);
705 reg = reg.right_str(reg.length_i()-1);
709 Molecule d = afm_find("scripts-accDot");
710 d.translate_axis(interline_f * 0.5 PT, Y_AXIS);
714 else if (sym == "Bayanbase")
716 Molecule r = afm_find("scripts-accBayanbase");
718 if (reg.left_str(1) == "T")
720 Molecule d = afm_find("scripts-accDot");
721 d.translate_axis(interline_f * 2.5 PT, Y_AXIS);
723 reg = reg.right_str(reg.length_i()-1);
725 /* include 4' reed just for completeness. You don't want to use this. */
726 if (reg.left_str(1) == "F")
728 Molecule d = afm_find("scripts-accDot");
729 d.translate_axis(interline_f * 1.5 PT, Y_AXIS);
731 reg = reg.right_str(reg.length_i()-1);
733 if (reg.left_str(2) == "EE")
735 Molecule d = afm_find("scripts-accDot");
736 d.translate_axis(interline_f * 0.5 PT, Y_AXIS);
737 d.translate_axis(0.4 * interline_f PT, X_AXIS);
739 d.translate_axis(-0.8 * interline_f PT, X_AXIS);
741 reg = reg.right_str(reg.length_i()-2);
743 if (reg.left_str(1) == "E")
745 Molecule d = afm_find("scripts-accDot");
746 d.translate_axis(interline_f * 0.5 PT, Y_AXIS);
748 reg = reg.right_str(reg.length_i()-1);
751 else if (sym == "Stdbase")
753 Molecule r = afm_find("scripts-accStdbase");
755 if (reg.left_str(1) == "T")
757 Molecule d = afm_find("scripts-accDot");
758 d.translate_axis(interline_f * 3.5 PT, Y_AXIS);
760 reg = reg.right_str(reg.length_i()-1);
762 if (reg.left_str(1) == "F")
764 Molecule d = afm_find("scripts-accDot");
765 d.translate_axis(interline_f * 2.5 PT, Y_AXIS);
767 reg = reg.right_str(reg.length_i()-1);
769 if (reg.left_str(1) == "M")
771 Molecule d = afm_find("scripts-accDot");
772 d.translate_axis(interline_f * 2 PT, Y_AXIS);
773 d.translate_axis(interline_f PT, X_AXIS);
775 reg = reg.right_str(reg.length_i()-1);
777 if (reg.left_str(1) == "E")
779 Molecule d = afm_find("scripts-accDot");
780 d.translate_axis(interline_f * 1.5 PT, Y_AXIS);
782 reg = reg.right_str(reg.length_i()-1);
784 if (reg.left_str(1) == "S")
786 Molecule d = afm_find("scripts-accDot");
787 d.translate_axis(interline_f * 0.5 PT, Y_AXIS);
789 reg = reg.right_str(reg.length_i()-1);
792 /* ugh maybe try to use regular font for S.B. and B.B and only use one font
794 else if (sym == "SB")
796 Molecule r = afm_find("scripts-accSB");
799 else if (sym == "BB")
801 Molecule r = afm_find("scripts-accBB");
804 else if (sym == "OldEE")
806 Molecule r = afm_find("scripts-accOldEE");
809 else if (sym == "OldEES")
811 Molecule r = afm_find("scripts-accOldEES");