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_;
48 Lookup::afm_find (String s, bool warn) const
52 Lookup * me = (Lookup*)(this);
53 me->afm_l_ = all_fonts_global_p->find_afm (font_name_);
56 warning (_f ("Can't find font: `%s'", font_name_));
57 warning (_f ("(search path `%s')", global_path.str ().ch_C()));
58 error (_ ("Aborting"));
61 AFM_CharMetricInfo const *cm = afm_l_->find_char_metric (s, warn);
69 Atom* at = new Atom (gh_list (ly_symbol2scm ("char"),
70 gh_int2scm (cm->code),
74 m.dim_ = afm_bbox_to_box (cm->charBBox);
75 m.add_atom (at->self_scm_);
80 Lookup::simple_bar (String type, Real h, Paper_def* paper_l) const
82 SCM thick = ly_symbol2scm (("barthick_" + type).ch_C());
85 if (paper_l->scope_p_->elem_b (thick))
87 w = paper_l->get_realvar (thick);
91 programming_error ("No bar thickness set ! ");
94 return filledbox (Box (Interval(0,w), Interval(-h/2, h/2)));
99 Lookup::bar (String str, Real h, Paper_def *paper_l) const
101 if (str == "bracket")
102 return staff_bracket (h, paper_l);
103 else if (str == "brace")
105 Real staffht = paper_l->get_var ("staffheight");
106 return staff_brace (h,staffht);
108 Real kern = paper_l->get_var ("bar_kern");
109 Real thinkern = paper_l->get_var ("bar_thinkern");
111 Molecule thin = simple_bar ("thin", h, paper_l);
112 Molecule thick = simple_bar ("thick", h, paper_l);
113 Molecule colon = afm_find ("dots-repeatcolon", paper_l);
119 return fill (Box (Interval(0, 0), Interval (-h/2, h/2)));
121 if (str == "scorepostbreak")
123 return simple_bar ("score", h, paper_l);
129 else if (str == "|.")
131 m.add_at_edge (X_AXIS, LEFT, thick, 0);
132 m.add_at_edge (X_AXIS, LEFT, thin, kern);
134 else if (str == ".|")
136 m.add_at_edge (X_AXIS, RIGHT, thick, 0);
137 m.add_at_edge (X_AXIS, RIGHT, thin, kern);
139 else if (str == ":|")
141 m.add_at_edge (X_AXIS, LEFT, thick, 0);
142 m.add_at_edge (X_AXIS, LEFT, thin, kern);
143 m.add_at_edge (X_AXIS, LEFT, colon, kern);
145 else if (str == "|:")
147 m.add_at_edge (X_AXIS, RIGHT, thick, 0);
148 m.add_at_edge (X_AXIS, RIGHT, thin, kern);
149 m.add_at_edge (X_AXIS, RIGHT, colon, kern);
151 else if (str == ":|:")
153 m.add_at_edge (X_AXIS, LEFT, thick, thinkern);
154 m.add_at_edge (X_AXIS, LEFT, colon, kern);
155 m.add_at_edge (X_AXIS, RIGHT, thick, kern);
156 m.add_at_edge (X_AXIS, RIGHT, colon, kern);
158 else if (str == ".|.")
160 m.add_at_edge (X_AXIS, LEFT, thick, thinkern);
161 m.add_at_edge (X_AXIS, RIGHT, thick, kern);
163 else if (str == "||")
165 m.add_at_edge (X_AXIS, RIGHT, thin, 0);
166 m.add_at_edge (X_AXIS, RIGHT, thin, thinkern);
173 Lookup::beam (Real slope, Real width, Real thick)
175 Real height = slope * width;
176 Real min_y = (0 <? height) - thick/2;
177 Real max_y = (0 >? height) + thick/2;
181 m.dim_[X_AXIS] = Interval (0, width);
182 m.dim_[Y_AXIS] = Interval (min_y, max_y);
186 (gh_list (ly_symbol2scm ("beam"),
187 gh_double2scm (width),
188 gh_double2scm (slope),
189 gh_double2scm (thick),
192 m.add_atom (at->self_scm_);
199 Lookup::dashed_slur (Bezier b, Real thick, Real dash)
202 for (int i= 4; i -- ;)
204 l = gh_cons (ly_offset2scm (b.control_[i]), l);
207 Atom *at = new Atom(gh_list (ly_symbol2scm ("dashed-slur"),
208 gh_double2scm (thick),
209 gh_double2scm (dash),
213 m.add_atom (at->self_scm_);
230 Lookup::filledbox (Box b )
234 Atom* at = new Atom(gh_list (ly_symbol2scm ("filledbox"),
235 gh_double2scm (-b[X_AXIS][LEFT]),
236 gh_double2scm (b[X_AXIS][RIGHT]),
237 gh_double2scm (-b[Y_AXIS][DOWN]),
238 gh_double2scm (b[Y_AXIS][UP]),
242 m.add_atom (at->self_scm_);
247 Lookup::frame (Box b, Real thick)
256 Axis o = Axis ((a+1)%NO_AXES);
259 edges[a] = b[a][d] + 0.5 * thick * Interval (-1, 1);
260 edges[o][DOWN] = b[o][DOWN] - thick/2;
261 edges[o][UP] = b[o][UP] + thick/2;
264 m.add_molecule (filledbox (edges));
266 while (flip (&d) != LEFT);
274 TODO: THIS IS UGLY. Since the user has direct access to TeX
275 strings, we try some halfbaked attempt to detect TeX trickery.
278 sanitise_TeX_string (String text)
281 for (int i= 0; i < text.length_i (); i++)
288 else if (text[i] == '}')
294 warning (_f ("Non-matching braces in text `%s', adding braces", text.ch_C()));
298 text = to_str ('{', -brace_count) + text;
302 text = text + to_str ('}', brace_count);
313 sanitise_PS_string (String t)
322 Lookup::text (String style, String text, Paper_def *paper_l)
325 if (style.empty_b ())
329 Real font_h = paper_l->get_var ("font_normal");
330 if (paper_l->scope_p_->elem_b ("font_" + style))
332 font_h = paper_l->get_var ("font_" + style);
336 if (paper_l->scope_p_->elem_b ("magnification_" + style))
338 font_mag = (int)paper_l->get_var ("magnification_" + style);
344 SCM l = ly_eval_str (("(style-to-cmr \"" + style + "\")").ch_C());
347 style = ly_scm2string (gh_cdr(l)) +to_str ((int)font_h);
352 Font_metric* metric_l = 0;
355 metric_l = all_fonts_global_p->find_scaled (style, font_mag);
357 metric_l = all_fonts_global_p->find_font (style);
360 if (output_global_ch == "tex")
361 text = sanitise_TeX_string (text);
362 else if (output_global_ch == "ps")
363 text = sanitise_PS_string (text);
365 m.dim_ = metric_l->text_dimension (text);
367 Atom *at = new Atom (gh_list (ly_symbol2scm ("text"),
368 ly_str02scm (text.ch_C()),
370 at->fontify (metric_l);
372 m.add_atom (at->self_scm_);
379 Lookup::staff_brace (Real y, int staff_size)
385 int minht = 2 * staff_size;
386 int maxht = 7 * minht;
387 int idx = int (((maxht - step) <? y - minht) / step);
390 SCM l = ly_eval_str ("(style-to-cmr \"brace\")");
391 String nm = "feta-braces";
393 nm = ly_scm2string (gh_cdr (l));
394 nm += to_str (staff_size);
395 SCM e =gh_list (ly_symbol2scm ("char"), gh_int2scm (idx), SCM_UNDEFINED);
396 Atom *at = new Atom (e);
398 at->fontify (all_fonts_global_p->find_font (nm));
400 m.dim_[Y_AXIS] = Interval (-y/2,y/2);
401 m.dim_[X_AXIS] = Interval (0,0);
402 m.add_atom (at->self_scm_);
408 Make a smooth curve along the points
411 Lookup::slur (Bezier curve, Real curvethick, Real linethick)
413 Real alpha = (curve.control_[3] - curve.control_[0]).arg ();
417 back.control_[1] += curvethick * complex_exp (Offset (0, alpha + M_PI/2));
418 back.control_[2] += curvethick * complex_exp (Offset (0, alpha + M_PI/2));
422 scontrols[ i ] = ly_offset2scm (back.control_[i]);
424 scontrols[i+4] = ly_offset2scm (curve.control_[i]);
427 Need the weird order b.o. the way PS want its arguments
429 int indices[]= {5, 6, 7, 4, 1, 2, 3, 0};
431 for (int i= 8; i--; )
433 list = gh_cons (scontrols[indices[i]], list);
437 Atom *at = new Atom (gh_list (ly_symbol2scm ("bezier-sandwich"),
439 gh_double2scm (linethick),
443 m.dim_[X_AXIS] = curve.extent (X_AXIS);
444 m.dim_[Y_AXIS] = curve.extent (Y_AXIS);
445 m.add_atom (at->self_scm_);
450 Lookup::staff_bracket (Real height, Paper_def* paper_l)
453 Atom *at = new Atom ( gh_list (ly_symbol2scm ("bracket"),
454 gh_double2scm (paper_l->get_var("bracket_arch_angle")),
455 gh_double2scm (paper_l->get_var("bracket_arch_width")),
456 gh_double2scm (paper_l->get_var("bracket_arch_height")),
457 gh_double2scm (paper_l->get_var("bracket_width")),
458 gh_double2scm (height),
459 gh_double2scm (paper_l->get_var("bracket_arch_thick")),
460 gh_double2scm (paper_l->get_var("bracket_thick")),
463 m.add_atom (at->self_scm_);
464 m.dim_[Y_AXIS] = Interval (-height/2,height/2);
465 m.dim_[X_AXIS] = Interval (0,4 PT);
467 m.translate_axis (- 4. / 3. * m.dim_[X_AXIS].length (), X_AXIS);
473 Lookup::accordion (SCM s, Real staff_space) const
476 String sym = ly_scm2string(gh_car (s));
477 String reg = ly_scm2string(gh_car (gh_cdr(s)));
479 if (sym == "Discant")
481 Molecule r = afm_find("scripts-accDiscant");
483 if (reg.left_str(1) == "F")
485 Molecule d = afm_find("scripts-accDot");
486 d.translate_axis(staff_space * 2.5 PT, Y_AXIS);
488 reg = reg.right_str(reg.length_i()-1);
491 if (reg.left_str(3) == "EEE")
494 reg = reg.right_str(reg.length_i()-3);
496 else if (reg.left_str(2) == "EE")
499 reg = reg.right_str(reg.length_i()-2);
501 else if (reg.left_str(2) == "Eh")
504 reg = reg.right_str(reg.length_i()-2);
506 else if (reg.left_str(1) == "E")
509 reg = reg.right_str(reg.length_i()-1);
513 Molecule d = afm_find("scripts-accDot");
514 d.translate_axis(staff_space * 1.5 PT, Y_AXIS);
519 Molecule d = afm_find("scripts-accDot");
520 d.translate_axis(staff_space * 1.5 PT, Y_AXIS);
521 d.translate_axis(0.8 * staff_space PT, X_AXIS);
526 Molecule d = afm_find("scripts-accDot");
527 d.translate_axis(staff_space * 1.5 PT, Y_AXIS);
528 d.translate_axis(-0.8 * staff_space PT, X_AXIS);
531 if (reg.left_str(2) == "SS")
533 Molecule d = afm_find("scripts-accDot");
534 d.translate_axis(0.5 * staff_space PT, Y_AXIS);
535 d.translate_axis(0.4 * staff_space PT, X_AXIS);
537 d.translate_axis(-0.8 * staff_space PT, X_AXIS);
539 reg = reg.right_str(reg.length_i()-2);
541 if (reg.left_str(1) == "S")
543 Molecule d = afm_find("scripts-accDot");
544 d.translate_axis(0.5 * staff_space PT, Y_AXIS);
546 reg = reg.right_str(reg.length_i()-1);
549 else if (sym == "Freebase")
551 Molecule r = afm_find("scripts-accFreebase");
553 if (reg.left_str(1) == "F")
555 Molecule d = afm_find("scripts-accDot");
556 d.translate_axis(staff_space * 1.5 PT, Y_AXIS);
558 reg = reg.right_str(reg.length_i()-1);
562 Molecule d = afm_find("scripts-accDot");
563 d.translate_axis(staff_space * 0.5 PT, Y_AXIS);
567 else if (sym == "Bayanbase")
569 Molecule r = afm_find("scripts-accBayanbase");
571 if (reg.left_str(1) == "T")
573 Molecule d = afm_find("scripts-accDot");
574 d.translate_axis(staff_space * 2.5 PT, Y_AXIS);
576 reg = reg.right_str(reg.length_i()-1);
578 /* include 4' reed just for completeness. You don't want to use this. */
579 if (reg.left_str(1) == "F")
581 Molecule d = afm_find("scripts-accDot");
582 d.translate_axis(staff_space * 1.5 PT, Y_AXIS);
584 reg = reg.right_str(reg.length_i()-1);
586 if (reg.left_str(2) == "EE")
588 Molecule d = afm_find("scripts-accDot");
589 d.translate_axis(staff_space * 0.5 PT, Y_AXIS);
590 d.translate_axis(0.4 * staff_space PT, X_AXIS);
592 d.translate_axis(-0.8 * staff_space PT, X_AXIS);
594 reg = reg.right_str(reg.length_i()-2);
596 if (reg.left_str(1) == "E")
598 Molecule d = afm_find("scripts-accDot");
599 d.translate_axis(staff_space * 0.5 PT, Y_AXIS);
601 reg = reg.right_str(reg.length_i()-1);
604 else if (sym == "Stdbase")
606 Molecule r = afm_find("scripts-accStdbase");
608 if (reg.left_str(1) == "T")
610 Molecule d = afm_find("scripts-accDot");
611 d.translate_axis(staff_space * 3.5 PT, Y_AXIS);
613 reg = reg.right_str(reg.length_i()-1);
615 if (reg.left_str(1) == "F")
617 Molecule d = afm_find("scripts-accDot");
618 d.translate_axis(staff_space * 2.5 PT, Y_AXIS);
620 reg = reg.right_str(reg.length_i()-1);
622 if (reg.left_str(1) == "M")
624 Molecule d = afm_find("scripts-accDot");
625 d.translate_axis(staff_space * 2 PT, Y_AXIS);
626 d.translate_axis(staff_space PT, X_AXIS);
628 reg = reg.right_str(reg.length_i()-1);
630 if (reg.left_str(1) == "E")
632 Molecule d = afm_find("scripts-accDot");
633 d.translate_axis(staff_space * 1.5 PT, Y_AXIS);
635 reg = reg.right_str(reg.length_i()-1);
637 if (reg.left_str(1) == "S")
639 Molecule d = afm_find("scripts-accDot");
640 d.translate_axis(staff_space * 0.5 PT, Y_AXIS);
642 reg = reg.right_str(reg.length_i()-1);
645 /* ugh maybe try to use regular font for S.B. and B.B and only use one font
647 else if (sym == "SB")
649 Molecule r = afm_find("scripts-accSB");
652 else if (sym == "BB")
654 Molecule r = afm_find("scripts-accBB");
657 else if (sym == "OldEE")
659 Molecule r = afm_find("scripts-accOldEE");
662 else if (sym == "OldEES")
664 Molecule r = afm_find("scripts-accOldEES");