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"
20 #include "paper-def.hh"
21 #include "string-convert.hh"
22 #include "file-path.hh"
24 #include "lily-guile.hh"
25 #include "all-font-metrics.hh"
28 #include "molecule.hh"
30 #include "lily-guile.hh"
33 ly_offset2scm (Offset o)
35 return gh_list (gh_double2scm (o[X_AXIS]), gh_double2scm(o[Y_AXIS]),
44 Lookup::Lookup (Lookup const& s)
46 font_name_ = s.font_name_;
52 build a ledger line for small pieces.
55 Lookup::ledger_line (Interval xwid) const
57 Drul_array<Molecule> endings;
58 endings[LEFT] = afm_find ("noteheads-ledgerending");
59 Molecule * e = &endings[LEFT];
62 Real thick = e->dim_[Y_AXIS].length();
63 Real len = e->dim_[X_AXIS].length () - thick;
68 endings[d].translate_axis (xwid[d] - endings[d].dim_[X_AXIS][d], X_AXIS);
69 total.add_molecule (endings[d]);
70 } while ((flip(&d)) != LEFT);
72 Real xpos = xwid [LEFT] + len;
74 while (xpos + len + thick /2 <= xwid[RIGHT])
76 e->translate_axis (len, X_AXIS);
77 total.add_molecule (*e);
88 Lookup::afm_find (String s, bool warn) const
92 Lookup * me = (Lookup*)(this);
93 me->afm_l_ = all_fonts_global_p->find_afm (font_name_);
96 warning (_f ("Can't find font: `%s'", font_name_));
97 warning (_f ("(search path: `%s')", global_path.str ().ch_C()));
98 error (_ ("Aborting"));
101 Adobe_font_char_metric cm = afm_l_->find_char (s, warn);
106 don't want people relying on this kind of dimension.
112 Atom at (gh_list (ly_symbol2scm ("char"),
113 gh_int2scm (cm.code ()),
117 m.dim_ = cm.dimensions();
123 Lookup::simple_bar (String type, Real h, Paper_def* paper_l) const
125 SCM thick = ly_symbol2scm (("barthick_" + type).ch_C());
128 if (paper_l->scope_p_->elem_b (thick))
130 w = paper_l->get_realvar (thick);
134 programming_error ("No bar thickness set ! ");
137 return filledbox (Box (Interval(0,w), Interval(-h/2, h/2)));
142 Lookup::bar (String str, Real h, Paper_def *paper_l) const
144 if (str == "bracket")
145 return staff_bracket (h, paper_l);
146 else if (str == "brace")
148 Real staffht = paper_l->get_var ("staffheight");
149 return staff_brace (h,staffht);
151 Real kern = paper_l->get_var ("bar_kern");
152 Real thinkern = paper_l->get_var ("bar_thinkern");
154 Molecule thin = simple_bar ("thin", h, paper_l);
155 Molecule thick = simple_bar ("thick", h, paper_l);
156 Molecule colon = afm_find ("dots-repeatcolon", paper_l);
162 return fill (Box (Interval(0, 0), Interval (-h/2, h/2)));
164 if (str == "scorepostbreak")
166 return simple_bar ("score", h, paper_l);
172 else if (str == "|.")
174 m.add_at_edge (X_AXIS, LEFT, thick, 0);
175 m.add_at_edge (X_AXIS, LEFT, thin, kern);
177 else if (str == ".|")
179 m.add_at_edge (X_AXIS, RIGHT, thick, 0);
180 m.add_at_edge (X_AXIS, RIGHT, thin, kern);
182 else if (str == ":|")
184 m.add_at_edge (X_AXIS, LEFT, thick, 0);
185 m.add_at_edge (X_AXIS, LEFT, thin, kern);
186 m.add_at_edge (X_AXIS, LEFT, colon, kern);
188 else if (str == "|:")
190 m.add_at_edge (X_AXIS, RIGHT, thick, 0);
191 m.add_at_edge (X_AXIS, RIGHT, thin, kern);
192 m.add_at_edge (X_AXIS, RIGHT, colon, kern);
194 else if (str == ":|:")
196 m.add_at_edge (X_AXIS, LEFT, thick, thinkern);
197 m.add_at_edge (X_AXIS, LEFT, colon, kern);
198 m.add_at_edge (X_AXIS, RIGHT, thick, kern);
199 m.add_at_edge (X_AXIS, RIGHT, colon, kern);
201 else if (str == ".|.")
203 m.add_at_edge (X_AXIS, LEFT, thick, thinkern);
204 m.add_at_edge (X_AXIS, RIGHT, thick, kern);
206 else if (str == "||")
208 m.add_at_edge (X_AXIS, RIGHT, thin, 0);
209 m.add_at_edge (X_AXIS, RIGHT, thin, thinkern);
216 Lookup::beam (Real slope, Real width, Real thick) const
218 Real height = slope * width;
219 Real min_y = (0 <? height) - thick/2;
220 Real max_y = (0 >? height) + thick/2;
225 (gh_list (ly_symbol2scm ("beam"),
226 gh_double2scm (width),
227 gh_double2scm (slope),
228 gh_double2scm (thick),
231 m.dim_[X_AXIS] = Interval (0, width);
232 m.dim_[Y_AXIS] = Interval (min_y, max_y);
243 Lookup::dashed_slur (Bezier b, Real thick, Real dash) const
246 for (int i= 4; i -- ;)
248 l = gh_cons (ly_offset2scm (b.control_[i]), l);
251 Atom at (gh_list (ly_symbol2scm ("dashed-slur"),
252 gh_double2scm (thick),
253 gh_double2scm (dash),
265 Lookup::fill (Box b) const
275 Lookup::special_time_signature (String s, int n, int d, Paper_def*pap) const
277 // First guess: s contains only the signature style
278 String symbolname = "timesig-" + s + to_str (n) + "/" + to_str (d);
280 Molecule m = afm_find (symbolname, false);
284 // Second guess: s contains the full signature name
285 m = afm_find ("timesig-"+s, false);
289 // Resort to default layout with numbers
290 return time_signature (n,d,pap);
294 Lookup::filledbox (Box b ) const
298 Atom at (gh_list (ly_symbol2scm ("filledbox"),
299 gh_double2scm (-b[X_AXIS][LEFT]),
300 gh_double2scm (b[X_AXIS][RIGHT]),
301 gh_double2scm (-b[Y_AXIS][DOWN]),
302 gh_double2scm (b[Y_AXIS][UP]),
311 TODO: THIS IS UGLY. Since the user has direct access to TeX
312 strings, we try some halfbaked attempt to detect TeX trickery.
315 sanitise_TeX_string (String text)
318 for (int i= 0; i < text.length_i (); i++)
325 else if (text[i] == '}')
331 warning (_f ("Non-matching braces in text `%s', adding braces", text.ch_C()));
335 text = to_str ('{', -brace_count) + text;
339 text = text + to_str ('}', brace_count);
350 sanitise_PS_string (String t)
359 Lookup::text (String style, String text, Paper_def *paper_l) const
362 if (style.empty_b ())
366 Real font_h = paper_l->get_var ("font_normal");
367 if (paper_l->scope_p_->elem_b ("font_" + style))
369 font_h = paper_l->get_var ("font_" + style);
373 if (paper_l->scope_p_->elem_b ("magnification_" + style))
375 font_mag = (int)paper_l->get_var ("magnification_" + style);
381 SCM l = ly_eval_str (("(style-to-cmr \"" + style + "\")").ch_C());
384 style = ly_scm2string (gh_cdr(l)) +to_str ((int)font_h);
389 Font_metric* metric_l = 0;
392 metric_l = all_fonts_global_p->find_scaled (style, font_mag);
394 metric_l = all_fonts_global_p->find_font (style);
397 if (output_global_ch == "tex")
398 text = sanitise_TeX_string (text);
399 else if (output_global_ch == "ps")
400 text = sanitise_PS_string (text);
402 m.dim_ = metric_l->text_dimension (text);
404 Atom at (gh_list (ly_symbol2scm ("text"),
405 ly_str02scm (text.ch_C()),
407 at.fontify (metric_l);
417 Lookup::time_signature (int num, int den, Paper_def *paper_l) const
419 String sty = "number";
420 Molecule n (text (sty, to_str (num), paper_l));
421 Molecule d (text (sty, to_str (den), paper_l));
422 n.align_to (X_AXIS, CENTER);
423 d.align_to (X_AXIS, CENTER);
427 m.add_at_edge (Y_AXIS, UP, n, 0.0);
428 m.add_at_edge (Y_AXIS, DOWN, d, 0.0);
433 m.align_to (Y_AXIS, CENTER);
439 Lookup::staff_brace (Real y, int staff_size) const
444 int minht = 2 * staff_size;
445 int maxht = 7 * minht;
446 int idx = int (((maxht - step) <? y - minht) / step);
450 String nm = String ("feta-braces" + to_str (staff_size));
451 SCM e =gh_list (ly_symbol2scm ("char"), gh_int2scm (idx), SCM_UNDEFINED);
454 at.fontify (all_fonts_global_p->find_font (nm));
456 m.dim_[Y_AXIS] = Interval (-y/2,y/2);
457 m.dim_[X_AXIS] = Interval (0,0);
465 Lookup::tuplet_bracket (Real dy , Real dx, Real thick, Real gap,
466 Real height, Direction dir) const
470 Atom at (gh_list(ly_symbol2scm ("tuplet"),
471 gh_double2scm (height),
475 gh_double2scm (thick),
484 Make a smooth curve along the points
487 Lookup::slur (Bezier curve, Real curvethick, Real linethick) const
489 Real alpha = (curve.control_[3] - curve.control_[0]).arg ();
493 back.control_[1] += curvethick * complex_exp (Offset (0, alpha + M_PI/2));
494 back.control_[2] += curvethick * complex_exp (Offset (0, alpha + M_PI/2));
498 scontrols[ i ] = ly_offset2scm (back.control_[i]);
500 scontrols[i+4] = ly_offset2scm (curve.control_[i]);
503 Need the weird order b.o. the way PS want its arguments
505 int indices[]= {5, 6, 7, 4, 1, 2, 3, 0};
507 for (int i= 8; i--; )
509 list = gh_cons (scontrols[indices[i]], list);
513 Atom at (gh_list (ly_symbol2scm ("bezier-sandwich"),
515 gh_double2scm (linethick),
519 m.dim_[X_AXIS] = curve.extent (X_AXIS);
520 m.dim_[Y_AXIS] = curve.extent (Y_AXIS);
526 Lookup::staff_bracket (Real height, Paper_def* paper_l) const
529 Atom at ( gh_list (ly_symbol2scm ("bracket"),
530 gh_double2scm (paper_l->get_var("bracket_arch_angle")),
531 gh_double2scm (paper_l->get_var("bracket_arch_width")),
532 gh_double2scm (paper_l->get_var("bracket_arch_height")),
533 gh_double2scm (paper_l->get_var("bracket_width")),
534 gh_double2scm (height),
535 gh_double2scm (paper_l->get_var("bracket_arch_thick")),
536 gh_double2scm (paper_l->get_var("bracket_thick")),
540 m.dim_[Y_AXIS] = Interval (-height/2,height/2);
541 m.dim_[X_AXIS] = Interval (0,4 PT);
543 m.translate_axis (- 4. / 3. * m.dim_[X_AXIS].length (), X_AXIS);
548 Lookup::volta (Real h, Real w, Real thick, bool vert_start, bool vert_end) const
552 Atom at (gh_list (ly_symbol2scm ("volta"),
555 gh_double2scm (thick),
556 gh_int2scm (vert_start),
557 gh_int2scm (vert_end),
560 m.dim_[Y_AXIS] = Interval (- h/2, h/2);
561 m.dim_[X_AXIS] = Interval (0, w);
568 Lookup::accordion (SCM s, Real interline_f) const
571 String sym = ly_scm2string(gh_car (s));
572 String reg = ly_scm2string(gh_car (gh_cdr(s)));
574 if (sym == "Discant")
576 Molecule r = afm_find("scripts-accDiscant");
578 if (reg.left_str(1) == "F")
580 Molecule d = afm_find("scripts-accDot");
581 d.translate_axis(interline_f * 2.5 PT, Y_AXIS);
583 reg = reg.right_str(reg.length_i()-1);
586 if (reg.left_str(3) == "EEE")
589 reg = reg.right_str(reg.length_i()-3);
591 else if (reg.left_str(2) == "EE")
594 reg = reg.right_str(reg.length_i()-2);
596 else if (reg.left_str(2) == "Eh")
599 reg = reg.right_str(reg.length_i()-2);
601 else if (reg.left_str(1) == "E")
604 reg = reg.right_str(reg.length_i()-1);
608 Molecule d = afm_find("scripts-accDot");
609 d.translate_axis(interline_f * 1.5 PT, Y_AXIS);
614 Molecule d = afm_find("scripts-accDot");
615 d.translate_axis(interline_f * 1.5 PT, Y_AXIS);
616 d.translate_axis(0.8 * interline_f PT, X_AXIS);
621 Molecule d = afm_find("scripts-accDot");
622 d.translate_axis(interline_f * 1.5 PT, Y_AXIS);
623 d.translate_axis(-0.8 * interline_f PT, X_AXIS);
626 if (reg.left_str(2) == "SS")
628 Molecule d = afm_find("scripts-accDot");
629 d.translate_axis(0.5 * interline_f PT, Y_AXIS);
630 d.translate_axis(0.4 * interline_f PT, X_AXIS);
632 d.translate_axis(-0.8 * interline_f PT, X_AXIS);
634 reg = reg.right_str(reg.length_i()-2);
636 if (reg.left_str(1) == "S")
638 Molecule d = afm_find("scripts-accDot");
639 d.translate_axis(0.5 * interline_f PT, Y_AXIS);
641 reg = reg.right_str(reg.length_i()-1);
644 else if (sym == "Freebase")
646 Molecule r = afm_find("scripts-accFreebase");
648 if (reg.left_str(1) == "F")
650 Molecule d = afm_find("scripts-accDot");
651 d.translate_axis(interline_f * 1.5 PT, Y_AXIS);
653 reg = reg.right_str(reg.length_i()-1);
657 Molecule d = afm_find("scripts-accDot");
658 d.translate_axis(interline_f * 0.5 PT, Y_AXIS);
662 else if (sym == "Bayanbase")
664 Molecule r = afm_find("scripts-accBayanbase");
666 if (reg.left_str(1) == "T")
668 Molecule d = afm_find("scripts-accDot");
669 d.translate_axis(interline_f * 2.5 PT, Y_AXIS);
671 reg = reg.right_str(reg.length_i()-1);
673 /* include 4' reed just for completeness. You don't want to use this. */
674 if (reg.left_str(1) == "F")
676 Molecule d = afm_find("scripts-accDot");
677 d.translate_axis(interline_f * 1.5 PT, Y_AXIS);
679 reg = reg.right_str(reg.length_i()-1);
681 if (reg.left_str(2) == "EE")
683 Molecule d = afm_find("scripts-accDot");
684 d.translate_axis(interline_f * 0.5 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) == "E")
693 Molecule d = afm_find("scripts-accDot");
694 d.translate_axis(interline_f * 0.5 PT, Y_AXIS);
696 reg = reg.right_str(reg.length_i()-1);
699 else if (sym == "Stdbase")
701 Molecule r = afm_find("scripts-accStdbase");
703 if (reg.left_str(1) == "T")
705 Molecule d = afm_find("scripts-accDot");
706 d.translate_axis(interline_f * 3.5 PT, Y_AXIS);
708 reg = reg.right_str(reg.length_i()-1);
710 if (reg.left_str(1) == "F")
712 Molecule d = afm_find("scripts-accDot");
713 d.translate_axis(interline_f * 2.5 PT, Y_AXIS);
715 reg = reg.right_str(reg.length_i()-1);
717 if (reg.left_str(1) == "M")
719 Molecule d = afm_find("scripts-accDot");
720 d.translate_axis(interline_f * 2 PT, Y_AXIS);
721 d.translate_axis(interline_f PT, X_AXIS);
723 reg = reg.right_str(reg.length_i()-1);
725 if (reg.left_str(1) == "E")
727 Molecule d = afm_find("scripts-accDot");
728 d.translate_axis(interline_f * 1.5 PT, Y_AXIS);
730 reg = reg.right_str(reg.length_i()-1);
732 if (reg.left_str(1) == "S")
734 Molecule d = afm_find("scripts-accDot");
735 d.translate_axis(interline_f * 0.5 PT, Y_AXIS);
737 reg = reg.right_str(reg.length_i()-1);
740 /* ugh maybe try to use regular font for S.B. and B.B and only use one font
742 else if (sym == "SB")
744 Molecule r = afm_find("scripts-accSB");
747 else if (sym == "BB")
749 Molecule r = afm_find("scripts-accBB");
752 else if (sym == "OldEE")
754 Molecule r = afm_find("scripts-accOldEE");
757 else if (sym == "OldEES")
759 Molecule r = afm_find("scripts-accOldEES");