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 find font: `%s'", font_name_));
104 warning (_f ("(search path: `%s')", global_path.str ().ch_C()));
105 error (_ ("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);
408 DEBUG_OUT << "\nChars: ";
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->
428 get_char ((unsigned char)text[i],false);
430 // Ugh, use the width of 'x' for unknown characters
431 if (c->dimensions()[X_AXIS].length () == 0)
432 c = (Character_metric*)afm_l->get_char ((unsigned char)'x',false);
433 w += c->dimensions()[X_AXIS].length ();
434 ydims.unite (c->dimensions()[Y_AXIS]);
440 warning (_f ("Non-matching braces in text `%s', adding braces", text.ch_C()));
444 text = to_str ('{', -brace_count) + text;
448 text = text + to_str ('}', brace_count);
453 m.dim_.x () = Interval (0, w*realmag);
457 Atom at (gh_list (text_scm_sym,
458 ly_ch_C_to_scm (text.ch_C()),
460 at.font_ = ly_symbol (style);
461 at.magn_ = gh_int2scm (font_mag);
469 Lookup::time_signature (int num, int den, Paper_def *paper_l) const
471 String sty = "number";
472 Molecule n (text (sty, to_str (num), paper_l));
473 Molecule d (text (sty, to_str (den), paper_l));
474 n.align_to (X_AXIS, CENTER);
475 d.align_to (X_AXIS, CENTER);
479 m.add_at_edge (Y_AXIS, UP, n, 0.0);
480 m.add_at_edge (Y_AXIS, DOWN, d, 0.0);
485 m.align_to (Y_AXIS, CENTER);
491 Lookup::staff_brace (Real y, int staff_size) const
495 (define (pianobrace y staffht)
497 (minht (* 2 staffht))
501 (select-font (string-append "feta-braces" (number->string (inexact->exact staffht))) 0)
502 (char (max 0 (/ (- (min y (- maxht step)) minht) step))))
508 int minht = 2 * staff_size;
509 int maxht = 7 * minht;
510 int idx = int (((maxht - step) <? y - minht) / step);
513 SCM f = ly_symbol (String ("feta-braces" + to_str (staff_size)));
514 SCM e =gh_list (char_scm_sym, gh_int2scm (idx), SCM_UNDEFINED);
518 m.dim_[Y_AXIS] = Interval (-y/2,y/2);
519 m.dim_[X_AXIS] = Interval (0,0);
525 Lookup::hairpin (Real width, Real height, Real thick, bool decresc, bool continued) const
529 String hairpin = String (decresc ? "de" : "") + "crescendo";
530 Atom at (gh_list (ly_symbol (hairpin),
531 gh_double2scm (thick),
532 gh_double2scm (width),
533 gh_double2scm (height),
534 gh_double2scm (continued ? height/2 : 0.0),
536 m.dim_.x () = Interval (0, width);
537 m.dim_.y () = Interval (-2*height, 2*height);
544 Lookup::tuplet_bracket (Real dy , Real dx, Real thick, Real gap, Real interline_f, Direction dir) const
548 Atom at (gh_list(tuplet_scm_sym,
549 gh_double2scm (interline_f),
553 gh_double2scm (thick),
562 Make a smooth curve along the points
565 Lookup::slur (Array<Offset> controls, Real linethick) const
567 Offset delta_off = controls[3]- controls[0];
571 int indices[] = {5,6,7,4,1,2,3,0};
573 for (int i= 0; i < 8; i++)
574 scontrols[i] = offset2scm (controls[indices[i]]);
577 Atom at (gh_list (ly_symbol ("bezier-sandwich"),
578 ly_quote_scm (array_to_list (scontrols, 8)),
579 gh_double2scm (linethick),
582 m.dim_[X_AXIS] = Interval (0, delta_off[X_AXIS]);
583 m.dim_[Y_AXIS] = Interval (0 <? delta_off[Y_AXIS], 0 >? delta_off[Y_AXIS]);
589 Lookup::staff_bracket (Real y) const
592 Atom at ( gh_list (bracket_scm_sym,
596 m.dim_[Y_AXIS] = Interval (-y/2,y/2);
597 m.dim_[X_AXIS] = Interval (0,4 PT);
599 m.translate_axis (- 4. / 3. * m.dim_[X_AXIS].length (), X_AXIS);
604 Lookup::volta (Real h, Real w, Real thick, bool vert_start, bool vert_end) const
608 Atom at (gh_list (volta_scm_sym,
611 gh_double2scm (thick),
612 gh_int2scm (vert_start),
613 gh_int2scm (vert_end),
616 m.dim_[Y_AXIS] = Interval (- h/2, h/2);
617 m.dim_[X_AXIS] = Interval (0, w);
624 Lookup::accordion (SCM s, Real interline_f) const
627 String sym = ly_scm2string(SCM_CAR(s));
628 String reg = ly_scm2string(SCM_CAR(SCM_CDR(s)));
630 if (sym == "Discant")
632 Molecule r = afm_find("scripts-accDiscant");
634 if (reg.left_str(1) == "F")
636 Molecule d = afm_find("scripts-accDot");
637 d.translate_axis(interline_f * 2.5 PT, Y_AXIS);
639 reg = reg.right_str(reg.length_i()-1);
642 if (reg.left_str(3) == "EEE")
645 reg = reg.right_str(reg.length_i()-3);
647 else if (reg.left_str(2) == "EE")
650 reg = reg.right_str(reg.length_i()-2);
652 else if (reg.left_str(2) == "Eh")
655 reg = reg.right_str(reg.length_i()-2);
657 else if (reg.left_str(1) == "E")
660 reg = reg.right_str(reg.length_i()-1);
664 Molecule d = afm_find("scripts-accDot");
665 d.translate_axis(interline_f * 1.5 PT, Y_AXIS);
670 Molecule d = afm_find("scripts-accDot");
671 d.translate_axis(interline_f * 1.5 PT, Y_AXIS);
672 d.translate_axis(0.8 * interline_f PT, X_AXIS);
677 Molecule d = afm_find("scripts-accDot");
678 d.translate_axis(interline_f * 1.5 PT, Y_AXIS);
679 d.translate_axis(-0.8 * interline_f PT, X_AXIS);
682 if (reg.left_str(2) == "SS")
684 Molecule d = afm_find("scripts-accDot");
685 d.translate_axis(0.5 * interline_f PT, Y_AXIS);
686 d.translate_axis(0.4 * interline_f PT, X_AXIS);
688 d.translate_axis(-0.8 * interline_f PT, X_AXIS);
690 reg = reg.right_str(reg.length_i()-2);
692 if (reg.left_str(1) == "S")
694 Molecule d = afm_find("scripts-accDot");
695 d.translate_axis(0.5 * interline_f PT, Y_AXIS);
697 reg = reg.right_str(reg.length_i()-1);
700 else if (sym == "Freebase")
702 Molecule r = afm_find("scripts-accFreebase");
704 if (reg.left_str(1) == "F")
706 Molecule d = afm_find("scripts-accDot");
707 d.translate_axis(interline_f * 1.5 PT, Y_AXIS);
709 reg = reg.right_str(reg.length_i()-1);
713 Molecule d = afm_find("scripts-accDot");
714 d.translate_axis(interline_f * 0.5 PT, Y_AXIS);
718 else if (sym == "Bayanbase")
720 Molecule r = afm_find("scripts-accBayanbase");
722 if (reg.left_str(1) == "T")
724 Molecule d = afm_find("scripts-accDot");
725 d.translate_axis(interline_f * 2.5 PT, Y_AXIS);
727 reg = reg.right_str(reg.length_i()-1);
729 /* include 4' reed just for completeness. You don't want to use this. */
730 if (reg.left_str(1) == "F")
732 Molecule d = afm_find("scripts-accDot");
733 d.translate_axis(interline_f * 1.5 PT, Y_AXIS);
735 reg = reg.right_str(reg.length_i()-1);
737 if (reg.left_str(2) == "EE")
739 Molecule d = afm_find("scripts-accDot");
740 d.translate_axis(interline_f * 0.5 PT, Y_AXIS);
741 d.translate_axis(0.4 * interline_f PT, X_AXIS);
743 d.translate_axis(-0.8 * interline_f PT, X_AXIS);
745 reg = reg.right_str(reg.length_i()-2);
747 if (reg.left_str(1) == "E")
749 Molecule d = afm_find("scripts-accDot");
750 d.translate_axis(interline_f * 0.5 PT, Y_AXIS);
752 reg = reg.right_str(reg.length_i()-1);
755 else if (sym == "Stdbase")
757 Molecule r = afm_find("scripts-accStdbase");
759 if (reg.left_str(1) == "T")
761 Molecule d = afm_find("scripts-accDot");
762 d.translate_axis(interline_f * 3.5 PT, Y_AXIS);
764 reg = reg.right_str(reg.length_i()-1);
766 if (reg.left_str(1) == "F")
768 Molecule d = afm_find("scripts-accDot");
769 d.translate_axis(interline_f * 2.5 PT, Y_AXIS);
771 reg = reg.right_str(reg.length_i()-1);
773 if (reg.left_str(1) == "M")
775 Molecule d = afm_find("scripts-accDot");
776 d.translate_axis(interline_f * 2 PT, Y_AXIS);
777 d.translate_axis(interline_f PT, X_AXIS);
779 reg = reg.right_str(reg.length_i()-1);
781 if (reg.left_str(1) == "E")
783 Molecule d = afm_find("scripts-accDot");
784 d.translate_axis(interline_f * 1.5 PT, Y_AXIS);
786 reg = reg.right_str(reg.length_i()-1);
788 if (reg.left_str(1) == "S")
790 Molecule d = afm_find("scripts-accDot");
791 d.translate_axis(interline_f * 0.5 PT, Y_AXIS);
793 reg = reg.right_str(reg.length_i()-1);
796 /* ugh maybe try to use regular font for S.B. and B.B and only use one font
798 else if (sym == "SB")
800 Molecule r = afm_find("scripts-accSB");
803 else if (sym == "BB")
805 Molecule r = afm_find("scripts-accBB");
808 else if (sym == "OldEE")
810 Molecule r = afm_find("scripts-accOldEE");
813 else if (sym == "OldEES")
815 Molecule r = afm_find("scripts-accOldEES");