2 lookup.cc -- implement simple Lookup methods.
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2000 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"
38 Lookup::Lookup (Lookup const& s)
40 font_name_ = s.font_name_;
46 build a ledger line for small pieces.
49 Lookup::ledger_line (Interval xwid) const
51 Drul_array<Molecule> endings;
52 endings[LEFT] = afm_find ("noteheads-ledgerending");
53 Molecule * e = &endings[LEFT];
56 Real thick = e->dim_[Y_AXIS].length();
57 Real len = e->dim_[X_AXIS].length () - thick;
62 endings[d].translate_axis (xwid[d] - endings[d].dim_[X_AXIS][d], X_AXIS);
63 total.add_molecule (endings[d]);
64 } while ((flip(&d)) != LEFT);
66 Real xpos = xwid [LEFT] + len;
68 while (xpos + len + thick /2 <= xwid[RIGHT])
70 e->translate_axis (len, X_AXIS);
71 total.add_molecule (*e);
80 Lookup::afm_find (String s, bool warn) const
84 Lookup * me = (Lookup*)(this);
85 me->afm_l_ = all_fonts_global_p->find_afm (font_name_);
88 warning (_f ("Can't find font: `%s'", font_name_));
89 warning (_f ("(search path `%s')", global_path.str ().ch_C()));
90 error (_ ("Aborting"));
93 AFM_CharMetricInfo const *cm = afm_l_->find_char_metric (s, warn);
101 Atom at (gh_list (ly_symbol2scm ("char"),
102 gh_int2scm (cm->code),
106 m.dim_ = afm_bbox_to_box (cm->charBBox);
112 Lookup::simple_bar (String type, Real h, Paper_def* paper_l) const
114 SCM thick = ly_symbol2scm (("barthick_" + type).ch_C());
117 if (paper_l->scope_p_->elem_b (thick))
119 w = paper_l->get_realvar (thick);
123 programming_error ("No bar thickness set ! ");
126 return filledbox (Box (Interval(0,w), Interval(-h/2, h/2)));
131 Lookup::bar (String str, Real h, Paper_def *paper_l) const
133 if (str == "bracket")
134 return staff_bracket (h, paper_l);
135 else if (str == "brace")
137 Real staffht = paper_l->get_var ("staffheight");
138 return staff_brace (h,staffht);
140 Real kern = paper_l->get_var ("bar_kern");
141 Real thinkern = paper_l->get_var ("bar_thinkern");
143 Molecule thin = simple_bar ("thin", h, paper_l);
144 Molecule thick = simple_bar ("thick", h, paper_l);
145 Molecule colon = afm_find ("dots-repeatcolon", paper_l);
151 return fill (Box (Interval(0, 0), Interval (-h/2, h/2)));
153 if (str == "scorepostbreak")
155 return simple_bar ("score", h, paper_l);
161 else if (str == "|.")
163 m.add_at_edge (X_AXIS, LEFT, thick, 0);
164 m.add_at_edge (X_AXIS, LEFT, thin, kern);
166 else if (str == ".|")
168 m.add_at_edge (X_AXIS, RIGHT, thick, 0);
169 m.add_at_edge (X_AXIS, RIGHT, thin, kern);
171 else if (str == ":|")
173 m.add_at_edge (X_AXIS, LEFT, thick, 0);
174 m.add_at_edge (X_AXIS, LEFT, thin, kern);
175 m.add_at_edge (X_AXIS, LEFT, colon, 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);
181 m.add_at_edge (X_AXIS, RIGHT, colon, kern);
183 else if (str == ":|:")
185 m.add_at_edge (X_AXIS, LEFT, thick, thinkern);
186 m.add_at_edge (X_AXIS, LEFT, colon, kern);
187 m.add_at_edge (X_AXIS, RIGHT, thick, kern);
188 m.add_at_edge (X_AXIS, RIGHT, colon, kern);
190 else if (str == ".|.")
192 m.add_at_edge (X_AXIS, LEFT, thick, thinkern);
193 m.add_at_edge (X_AXIS, RIGHT, thick, kern);
195 else if (str == "||")
197 m.add_at_edge (X_AXIS, RIGHT, thin, 0);
198 m.add_at_edge (X_AXIS, RIGHT, thin, thinkern);
205 Lookup::beam (Real slope, Real width, Real thick) const
207 Real height = slope * width;
208 Real min_y = (0 <? height) - thick/2;
209 Real max_y = (0 >? height) + thick/2;
214 (gh_list (ly_symbol2scm ("beam"),
215 gh_double2scm (width),
216 gh_double2scm (slope),
217 gh_double2scm (thick),
220 m.dim_[X_AXIS] = Interval (0, width);
221 m.dim_[Y_AXIS] = Interval (min_y, max_y);
232 Lookup::dashed_slur (Bezier b, Real thick, Real dash) const
235 for (int i= 4; i -- ;)
237 l = gh_cons (ly_offset2scm (b.control_[i]), l);
240 Atom at (gh_list (ly_symbol2scm ("dashed-slur"),
241 gh_double2scm (thick),
242 gh_double2scm (dash),
254 Lookup::fill (Box b) const
264 Lookup::special_time_signature (String s, int n, int d, Paper_def*pap) const
266 // First guess: s contains only the signature style
267 String symbolname = "timesig-" + s + to_str (n) + "/" + to_str (d);
269 Molecule m = afm_find (symbolname, false);
273 // Second guess: s contains the full signature name
274 m = afm_find ("timesig-"+s, false);
278 // Resort to default layout with numbers
279 return time_signature (n,d,pap);
283 Lookup::filledbox (Box b ) const
287 Atom at (gh_list (ly_symbol2scm ("filledbox"),
288 gh_double2scm (-b[X_AXIS][LEFT]),
289 gh_double2scm (b[X_AXIS][RIGHT]),
290 gh_double2scm (-b[Y_AXIS][DOWN]),
291 gh_double2scm (b[Y_AXIS][UP]),
300 TODO: THIS IS UGLY. Since the user has direct access to TeX
301 strings, we try some halfbaked attempt to detect TeX trickery.
304 sanitise_TeX_string (String text)
307 for (int i= 0; i < text.length_i (); i++)
314 else if (text[i] == '}')
320 warning (_f ("Non-matching braces in text `%s', adding braces", text.ch_C()));
324 text = to_str ('{', -brace_count) + text;
328 text = text + to_str ('}', brace_count);
339 sanitise_PS_string (String t)
348 Lookup::text (String style, String text, Paper_def *paper_l) const
351 if (style.empty_b ())
355 Real font_h = paper_l->get_var ("font_normal");
356 if (paper_l->scope_p_->elem_b ("font_" + style))
358 font_h = paper_l->get_var ("font_" + style);
362 if (paper_l->scope_p_->elem_b ("magnification_" + style))
364 font_mag = (int)paper_l->get_var ("magnification_" + style);
370 SCM l = ly_eval_str (("(style-to-cmr \"" + style + "\")").ch_C());
373 style = ly_scm2string (gh_cdr(l)) +to_str ((int)font_h);
378 Font_metric* metric_l = 0;
381 metric_l = all_fonts_global_p->find_scaled (style, font_mag);
383 metric_l = all_fonts_global_p->find_font (style);
386 if (output_global_ch == "tex")
387 text = sanitise_TeX_string (text);
388 else if (output_global_ch == "ps")
389 text = sanitise_PS_string (text);
391 m.dim_ = metric_l->text_dimension (text);
393 Atom at (gh_list (ly_symbol2scm ("text"),
394 ly_str02scm (text.ch_C()),
396 at.fontify (metric_l);
406 Lookup::time_signature (int num, int den, Paper_def *paper_l) const
408 String sty = "number";
409 Molecule n (text (sty, to_str (num), paper_l));
410 Molecule d (text (sty, to_str (den), paper_l));
411 n.align_to (X_AXIS, CENTER);
412 d.align_to (X_AXIS, CENTER);
416 m.add_at_edge (Y_AXIS, UP, n, 0.0);
417 m.add_at_edge (Y_AXIS, DOWN, d, 0.0);
422 m.align_to (Y_AXIS, CENTER);
428 Lookup::staff_brace (Real y, int staff_size) const
433 int minht = 2 * staff_size;
434 int maxht = 7 * minht;
435 int idx = int (((maxht - step) <? y - minht) / step);
439 String nm = String ("feta-braces" + to_str (staff_size));
440 SCM e =gh_list (ly_symbol2scm ("char"), gh_int2scm (idx), SCM_UNDEFINED);
443 at.fontify (all_fonts_global_p->find_font (nm));
445 m.dim_[Y_AXIS] = Interval (-y/2,y/2);
446 m.dim_[X_AXIS] = Interval (0,0);
454 Lookup::tuplet_bracket (Real dy , Real dx, Real thick, Real gap,
455 Real height, Direction dir) const
459 Atom at (gh_list(ly_symbol2scm ("tuplet"),
460 gh_double2scm (height),
464 gh_double2scm (thick),
473 Make a smooth curve along the points
476 Lookup::slur (Bezier curve, Real curvethick, Real linethick) const
478 Real alpha = (curve.control_[3] - curve.control_[0]).arg ();
482 back.control_[1] += curvethick * complex_exp (Offset (0, alpha + M_PI/2));
483 back.control_[2] += curvethick * complex_exp (Offset (0, alpha + M_PI/2));
487 scontrols[ i ] = ly_offset2scm (back.control_[i]);
489 scontrols[i+4] = ly_offset2scm (curve.control_[i]);
492 Need the weird order b.o. the way PS want its arguments
494 int indices[]= {5, 6, 7, 4, 1, 2, 3, 0};
496 for (int i= 8; i--; )
498 list = gh_cons (scontrols[indices[i]], list);
502 Atom at (gh_list (ly_symbol2scm ("bezier-sandwich"),
504 gh_double2scm (linethick),
508 m.dim_[X_AXIS] = curve.extent (X_AXIS);
509 m.dim_[Y_AXIS] = curve.extent (Y_AXIS);
515 Lookup::staff_bracket (Real height, Paper_def* paper_l) const
518 Atom at ( gh_list (ly_symbol2scm ("bracket"),
519 gh_double2scm (paper_l->get_var("bracket_arch_angle")),
520 gh_double2scm (paper_l->get_var("bracket_arch_width")),
521 gh_double2scm (paper_l->get_var("bracket_arch_height")),
522 gh_double2scm (paper_l->get_var("bracket_width")),
523 gh_double2scm (height),
524 gh_double2scm (paper_l->get_var("bracket_arch_thick")),
525 gh_double2scm (paper_l->get_var("bracket_thick")),
529 m.dim_[Y_AXIS] = Interval (-height/2,height/2);
530 m.dim_[X_AXIS] = Interval (0,4 PT);
532 m.translate_axis (- 4. / 3. * m.dim_[X_AXIS].length (), X_AXIS);
537 Lookup::volta (Real h, Real w, Real thick, bool vert_start, bool vert_end) const
541 Atom at (gh_list (ly_symbol2scm ("volta"),
544 gh_double2scm (thick),
545 gh_int2scm (vert_start),
546 gh_int2scm (vert_end),
549 m.dim_[Y_AXIS] = Interval (- h/2, h/2);
550 m.dim_[X_AXIS] = Interval (0, w);
557 Lookup::accordion (SCM s, Real staff_space) const
560 String sym = ly_scm2string(gh_car (s));
561 String reg = ly_scm2string(gh_car (gh_cdr(s)));
563 if (sym == "Discant")
565 Molecule r = afm_find("scripts-accDiscant");
567 if (reg.left_str(1) == "F")
569 Molecule d = afm_find("scripts-accDot");
570 d.translate_axis(staff_space * 2.5 PT, Y_AXIS);
572 reg = reg.right_str(reg.length_i()-1);
575 if (reg.left_str(3) == "EEE")
578 reg = reg.right_str(reg.length_i()-3);
580 else if (reg.left_str(2) == "EE")
583 reg = reg.right_str(reg.length_i()-2);
585 else if (reg.left_str(2) == "Eh")
588 reg = reg.right_str(reg.length_i()-2);
590 else if (reg.left_str(1) == "E")
593 reg = reg.right_str(reg.length_i()-1);
597 Molecule d = afm_find("scripts-accDot");
598 d.translate_axis(staff_space * 1.5 PT, Y_AXIS);
603 Molecule d = afm_find("scripts-accDot");
604 d.translate_axis(staff_space * 1.5 PT, Y_AXIS);
605 d.translate_axis(0.8 * staff_space PT, X_AXIS);
610 Molecule d = afm_find("scripts-accDot");
611 d.translate_axis(staff_space * 1.5 PT, Y_AXIS);
612 d.translate_axis(-0.8 * staff_space PT, X_AXIS);
615 if (reg.left_str(2) == "SS")
617 Molecule d = afm_find("scripts-accDot");
618 d.translate_axis(0.5 * staff_space PT, Y_AXIS);
619 d.translate_axis(0.4 * staff_space PT, X_AXIS);
621 d.translate_axis(-0.8 * staff_space PT, X_AXIS);
623 reg = reg.right_str(reg.length_i()-2);
625 if (reg.left_str(1) == "S")
627 Molecule d = afm_find("scripts-accDot");
628 d.translate_axis(0.5 * staff_space PT, Y_AXIS);
630 reg = reg.right_str(reg.length_i()-1);
633 else if (sym == "Freebase")
635 Molecule r = afm_find("scripts-accFreebase");
637 if (reg.left_str(1) == "F")
639 Molecule d = afm_find("scripts-accDot");
640 d.translate_axis(staff_space * 1.5 PT, Y_AXIS);
642 reg = reg.right_str(reg.length_i()-1);
646 Molecule d = afm_find("scripts-accDot");
647 d.translate_axis(staff_space * 0.5 PT, Y_AXIS);
651 else if (sym == "Bayanbase")
653 Molecule r = afm_find("scripts-accBayanbase");
655 if (reg.left_str(1) == "T")
657 Molecule d = afm_find("scripts-accDot");
658 d.translate_axis(staff_space * 2.5 PT, Y_AXIS);
660 reg = reg.right_str(reg.length_i()-1);
662 /* include 4' reed just for completeness. You don't want to use this. */
663 if (reg.left_str(1) == "F")
665 Molecule d = afm_find("scripts-accDot");
666 d.translate_axis(staff_space * 1.5 PT, Y_AXIS);
668 reg = reg.right_str(reg.length_i()-1);
670 if (reg.left_str(2) == "EE")
672 Molecule d = afm_find("scripts-accDot");
673 d.translate_axis(staff_space * 0.5 PT, Y_AXIS);
674 d.translate_axis(0.4 * staff_space PT, X_AXIS);
676 d.translate_axis(-0.8 * staff_space PT, X_AXIS);
678 reg = reg.right_str(reg.length_i()-2);
680 if (reg.left_str(1) == "E")
682 Molecule d = afm_find("scripts-accDot");
683 d.translate_axis(staff_space * 0.5 PT, Y_AXIS);
685 reg = reg.right_str(reg.length_i()-1);
688 else if (sym == "Stdbase")
690 Molecule r = afm_find("scripts-accStdbase");
692 if (reg.left_str(1) == "T")
694 Molecule d = afm_find("scripts-accDot");
695 d.translate_axis(staff_space * 3.5 PT, Y_AXIS);
697 reg = reg.right_str(reg.length_i()-1);
699 if (reg.left_str(1) == "F")
701 Molecule d = afm_find("scripts-accDot");
702 d.translate_axis(staff_space * 2.5 PT, Y_AXIS);
704 reg = reg.right_str(reg.length_i()-1);
706 if (reg.left_str(1) == "M")
708 Molecule d = afm_find("scripts-accDot");
709 d.translate_axis(staff_space * 2 PT, Y_AXIS);
710 d.translate_axis(staff_space PT, X_AXIS);
712 reg = reg.right_str(reg.length_i()-1);
714 if (reg.left_str(1) == "E")
716 Molecule d = afm_find("scripts-accDot");
717 d.translate_axis(staff_space * 1.5 PT, Y_AXIS);
719 reg = reg.right_str(reg.length_i()-1);
721 if (reg.left_str(1) == "S")
723 Molecule d = afm_find("scripts-accDot");
724 d.translate_axis(staff_space * 0.5 PT, Y_AXIS);
726 reg = reg.right_str(reg.length_i()-1);
729 /* ugh maybe try to use regular font for S.B. and B.B and only use one font
731 else if (sym == "SB")
733 Molecule r = afm_find("scripts-accSB");
736 else if (sym == "BB")
738 Molecule r = afm_find("scripts-accBB");
741 else if (sym == "OldEE")
743 Molecule r = afm_find("scripts-accOldEE");
746 else if (sym == "OldEES")
748 Molecule r = afm_find("scripts-accOldEES");