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);
70 SCM at = (gh_list (ly_symbol2scm ("char"),
71 gh_int2scm (cm->code),
74 at= fontify_atom (afm_l_,at);
75 return Molecule ( afm_bbox_to_box (cm->charBBox), at);
79 Lookup::simple_bar (String type, Real h, Paper_def* paper_l) const
81 SCM thick = ly_symbol2scm (("barthick_" + type).ch_C());
84 if (paper_l->scope_p_->elem_b (thick))
86 w = paper_l->get_realvar (thick);
90 programming_error ("No bar thickness set ! ");
93 return filledbox (Box (Interval(0,w), Interval(-h/2, h/2)));
98 Lookup::bar (String str, Real h, Paper_def *paper_l) const
100 if (str == "bracket")
101 return staff_bracket (h, paper_l);
102 else if (str == "brace")
104 Real staffht = paper_l->get_var ("staffheight");
105 return staff_brace (h,staffht);
107 Real kern = paper_l->get_var ("bar_kern");
108 Real thinkern = paper_l->get_var ("bar_thinkern");
110 Molecule thin = simple_bar ("thin", h, paper_l);
111 Molecule thick = simple_bar ("thick", h, paper_l);
112 Molecule colon = afm_find ("dots-repeatcolon", paper_l);
118 return fill (Box (Interval(0, 0), Interval (-h/2, h/2)));
120 if (str == "scorepostbreak")
122 return simple_bar ("score", h, paper_l);
128 else if (str == "|.")
130 m.add_at_edge (X_AXIS, LEFT, thick, 0);
131 m.add_at_edge (X_AXIS, LEFT, thin, kern);
133 else if (str == ".|")
135 m.add_at_edge (X_AXIS, RIGHT, thick, 0);
136 m.add_at_edge (X_AXIS, RIGHT, thin, kern);
138 else if (str == ":|")
140 m.add_at_edge (X_AXIS, LEFT, thick, 0);
141 m.add_at_edge (X_AXIS, LEFT, thin, kern);
142 m.add_at_edge (X_AXIS, LEFT, colon, kern);
144 else if (str == "|:")
146 m.add_at_edge (X_AXIS, RIGHT, thick, 0);
147 m.add_at_edge (X_AXIS, RIGHT, thin, kern);
148 m.add_at_edge (X_AXIS, RIGHT, colon, kern);
150 else if (str == ":|:")
152 m.add_at_edge (X_AXIS, LEFT, thick, thinkern);
153 m.add_at_edge (X_AXIS, LEFT, colon, kern);
154 m.add_at_edge (X_AXIS, RIGHT, thick, kern);
155 m.add_at_edge (X_AXIS, RIGHT, colon, kern);
157 else if (str == ".|.")
159 m.add_at_edge (X_AXIS, LEFT, thick, thinkern);
160 m.add_at_edge (X_AXIS, RIGHT, thick, kern);
162 else if (str == "||")
164 m.add_at_edge (X_AXIS, RIGHT, thin, 0);
165 m.add_at_edge (X_AXIS, RIGHT, thin, thinkern);
172 Lookup::beam (Real slope, Real width, Real thick)
174 Real height = slope * width;
175 Real min_y = (0 <? height) - thick/2;
176 Real max_y = (0 >? height) + thick/2;
180 Box b( Interval (0, width),
181 Interval (min_y, max_y));
184 SCM at = gh_list (ly_symbol2scm ("beam"),
185 gh_double2scm (width),
186 gh_double2scm (slope),
187 gh_double2scm (thick),
189 return Molecule (b, at);
195 Lookup::dashed_slur (Bezier b, Real thick, Real dash)
198 // this is silly, we have array_to_scm
199 for (int i= 4; i -- ;)
201 l = gh_cons (to_scm (b.control_[i]), l);
204 SCM at = (gh_list (ly_symbol2scm ("dashed-slur"),
205 gh_double2scm (thick),
206 gh_double2scm (dash),
210 Box box (Interval(0,0),Interval( 0,0));
211 return Molecule (box, at);
227 Lookup::filledbox (Box b )
229 SCM at = (gh_list (ly_symbol2scm ("filledbox"),
230 gh_double2scm (-b[X_AXIS][LEFT]),
231 gh_double2scm (b[X_AXIS][RIGHT]),
232 gh_double2scm (-b[Y_AXIS][DOWN]),
233 gh_double2scm (b[Y_AXIS][UP]),
236 return Molecule ( b,at);
240 Lookup::frame (Box b, Real thick)
249 Axis o = Axis ((a+1)%NO_AXES);
252 edges[a] = b[a][d] + 0.5 * thick * Interval (-1, 1);
253 edges[o][DOWN] = b[o][DOWN] - thick/2;
254 edges[o][UP] = b[o][UP] + thick/2;
257 m.add_molecule (filledbox (edges));
259 while (flip (&d) != LEFT);
267 TODO: THIS IS UGLY. Since the user has direct access to TeX
268 strings, we try some halfbaked attempt to detect TeX trickery.
271 sanitise_TeX_string (String text)
274 for (int i= 0; i < text.length_i (); i++)
281 else if (text[i] == '}')
287 warning (_f ("Non-matching braces in text `%s', adding braces", text.ch_C()));
291 text = to_str ('{', -brace_count) + text;
295 text = text + to_str ('}', brace_count);
306 sanitise_PS_string (String t)
315 Lookup::text (String style, String text, Paper_def *paper_l)
317 if (style.empty_b ())
321 Real font_h = paper_l->get_var ("font_normal");
322 if (paper_l->scope_p_->elem_b ("font_" + style))
324 font_h = paper_l->get_var ("font_" + style);
328 if (paper_l->scope_p_->elem_b ("magnification_" + style))
330 font_mag = (int)paper_l->get_var ("magnification_" + style);
336 SCM l = ly_eval_str (("(style-to-cmr \"" + style + "\")").ch_C());
339 style = ly_scm2string (gh_cdr(l)) +to_str ((int)font_h);
344 Font_metric* metric_l = 0;
347 metric_l = all_fonts_global_p->find_scaled (style, font_mag);
349 metric_l = all_fonts_global_p->find_font (style);
352 if (output_global_ch == "tex")
353 text = sanitise_TeX_string (text);
354 else if (output_global_ch == "ps")
355 text = sanitise_PS_string (text);
359 SCM at = (gh_list (ly_symbol2scm ("text"),
360 ly_str02scm (text.ch_C()),
362 at = fontify_atom (metric_l,at);
363 return Molecule ( metric_l->text_dimension (text),
370 Lookup::staff_brace (Real y, int staff_size)
374 int minht = 2 * staff_size;
375 int maxht = 7 * minht;
376 int idx = int (((maxht - step) <? y - minht) / step);
379 SCM l = ly_eval_str ("(style-to-cmr \"brace\")");
380 String nm = "feta-braces";
382 nm = ly_scm2string (gh_cdr (l));
383 nm += to_str (staff_size);
384 SCM e =gh_list (ly_symbol2scm ("char"), gh_int2scm (idx), SCM_UNDEFINED);
387 at = fontify_atom (all_fonts_global_p->find_font (nm), at);
389 Box b ( Interval (-y/2,y/2),
391 return Molecule(b, at);
396 Make a smooth curve along the points
399 Lookup::slur (Bezier curve, Real curvethick, Real linethick)
401 Real alpha = (curve.control_[3] - curve.control_[0]).arg ();
405 back.control_[1] += curvethick * complex_exp (Offset (0, alpha + M_PI/2));
406 back.control_[2] += curvethick * complex_exp (Offset (0, alpha + M_PI/2));
409 // this is silly, we have array_to_scm
411 scontrols[ i ] = to_scm (back.control_[i]);
413 scontrols[i+4] = to_scm (curve.control_[i]);
416 Need the weird order b.o. the way PS want its arguments
418 int indices[]= {5, 6, 7, 4, 1, 2, 3, 0};
420 for (int i= 8; i--; )
422 list = gh_cons (scontrols[indices[i]], list);
426 SCM at = (gh_list (ly_symbol2scm ("bezier-sandwich"),
428 gh_double2scm (linethick),
431 Box b ( curve.extent (X_AXIS), curve.extent (Y_AXIS));
432 return Molecule (b, at);
436 Lookup::staff_bracket (Real height, Paper_def* paper_l)
438 SCM at = ( gh_list (ly_symbol2scm ("bracket"),
439 gh_double2scm (paper_l->get_var("bracket_arch_angle")),
440 gh_double2scm (paper_l->get_var("bracket_arch_width")),
441 gh_double2scm (paper_l->get_var("bracket_arch_height")),
442 gh_double2scm (paper_l->get_var("bracket_width")),
443 gh_double2scm (height),
444 gh_double2scm (paper_l->get_var("bracket_arch_thick")),
445 gh_double2scm (paper_l->get_var("bracket_thick")),
448 Box b ( Interval (-height/2,height/2), Interval (0,4 PT));
451 m.translate_axis (- 4. / 3. * m.dim_[X_AXIS].length (), X_AXIS);
457 Lookup::accordion (SCM s, Real staff_space) const
460 String sym = ly_scm2string(gh_car (s));
461 String reg = ly_scm2string(gh_car (gh_cdr(s)));
463 if (sym == "Discant")
465 Molecule r = afm_find("scripts-accDiscant");
467 if (reg.left_str(1) == "F")
469 Molecule d = afm_find("scripts-accDot");
470 d.translate_axis(staff_space * 2.5 PT, Y_AXIS);
472 reg = reg.right_str(reg.length_i()-1);
475 if (reg.left_str(3) == "EEE")
478 reg = reg.right_str(reg.length_i()-3);
480 else if (reg.left_str(2) == "EE")
483 reg = reg.right_str(reg.length_i()-2);
485 else if (reg.left_str(2) == "Eh")
488 reg = reg.right_str(reg.length_i()-2);
490 else if (reg.left_str(1) == "E")
493 reg = reg.right_str(reg.length_i()-1);
497 Molecule d = afm_find("scripts-accDot");
498 d.translate_axis(staff_space * 1.5 PT, Y_AXIS);
503 Molecule d = afm_find("scripts-accDot");
504 d.translate_axis(staff_space * 1.5 PT, Y_AXIS);
505 d.translate_axis(0.8 * staff_space PT, X_AXIS);
510 Molecule d = afm_find("scripts-accDot");
511 d.translate_axis(staff_space * 1.5 PT, Y_AXIS);
512 d.translate_axis(-0.8 * staff_space PT, X_AXIS);
515 if (reg.left_str(2) == "SS")
517 Molecule d = afm_find("scripts-accDot");
518 d.translate_axis(0.5 * staff_space PT, Y_AXIS);
519 d.translate_axis(0.4 * staff_space PT, X_AXIS);
521 d.translate_axis(-0.8 * staff_space PT, X_AXIS);
523 reg = reg.right_str(reg.length_i()-2);
525 if (reg.left_str(1) == "S")
527 Molecule d = afm_find("scripts-accDot");
528 d.translate_axis(0.5 * staff_space PT, Y_AXIS);
530 reg = reg.right_str(reg.length_i()-1);
533 else if (sym == "Freebase")
535 Molecule r = afm_find("scripts-accFreebase");
537 if (reg.left_str(1) == "F")
539 Molecule d = afm_find("scripts-accDot");
540 d.translate_axis(staff_space * 1.5 PT, Y_AXIS);
542 reg = reg.right_str(reg.length_i()-1);
546 Molecule d = afm_find("scripts-accDot");
547 d.translate_axis(staff_space * 0.5 PT, Y_AXIS);
551 else if (sym == "Bayanbase")
553 Molecule r = afm_find("scripts-accBayanbase");
555 if (reg.left_str(1) == "T")
557 Molecule d = afm_find("scripts-accDot");
558 d.translate_axis(staff_space * 2.5 PT, Y_AXIS);
560 reg = reg.right_str(reg.length_i()-1);
562 /* include 4' reed just for completeness. You don't want to use this. */
563 if (reg.left_str(1) == "F")
565 Molecule d = afm_find("scripts-accDot");
566 d.translate_axis(staff_space * 1.5 PT, Y_AXIS);
568 reg = reg.right_str(reg.length_i()-1);
570 if (reg.left_str(2) == "EE")
572 Molecule d = afm_find("scripts-accDot");
573 d.translate_axis(staff_space * 0.5 PT, Y_AXIS);
574 d.translate_axis(0.4 * staff_space PT, X_AXIS);
576 d.translate_axis(-0.8 * staff_space PT, X_AXIS);
578 reg = reg.right_str(reg.length_i()-2);
580 if (reg.left_str(1) == "E")
582 Molecule d = afm_find("scripts-accDot");
583 d.translate_axis(staff_space * 0.5 PT, Y_AXIS);
585 reg = reg.right_str(reg.length_i()-1);
588 else if (sym == "Stdbase")
590 Molecule r = afm_find("scripts-accStdbase");
592 if (reg.left_str(1) == "T")
594 Molecule d = afm_find("scripts-accDot");
595 d.translate_axis(staff_space * 3.5 PT, Y_AXIS);
597 reg = reg.right_str(reg.length_i()-1);
599 if (reg.left_str(1) == "F")
601 Molecule d = afm_find("scripts-accDot");
602 d.translate_axis(staff_space * 2.5 PT, Y_AXIS);
604 reg = reg.right_str(reg.length_i()-1);
606 if (reg.left_str(1) == "M")
608 Molecule d = afm_find("scripts-accDot");
609 d.translate_axis(staff_space * 2 PT, Y_AXIS);
610 d.translate_axis(staff_space PT, X_AXIS);
612 reg = reg.right_str(reg.length_i()-1);
614 if (reg.left_str(1) == "E")
616 Molecule d = afm_find("scripts-accDot");
617 d.translate_axis(staff_space * 1.5 PT, Y_AXIS);
619 reg = reg.right_str(reg.length_i()-1);
621 if (reg.left_str(1) == "S")
623 Molecule d = afm_find("scripts-accDot");
624 d.translate_axis(staff_space * 0.5 PT, Y_AXIS);
626 reg = reg.right_str(reg.length_i()-1);
629 /* ugh maybe try to use regular font for S.B. and B.B and only use one font
631 else if (sym == "SB")
633 Molecule r = afm_find("scripts-accSB");
636 else if (sym == "BB")
638 Molecule r = afm_find("scripts-accBB");
641 else if (sym == "OldEE")
643 Molecule r = afm_find("scripts-accOldEE");
646 else if (sym == "OldEES")
648 Molecule r = afm_find("scripts-accOldEES");