2 lookup.cc -- implement simple Lookup methods.
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2002 Han-Wen Nienhuys <hanwen@cs.uu.nl>
8 Jan Nieuwenhuizen <janneke@gnu.org>
17 #include "dimensions.hh"
19 #include "string-convert.hh"
20 #include "file-path.hh"
22 #include "lily-guile.hh"
23 #include "molecule.hh"
25 #include "font-metric.hh"
28 Lookup::beam (Real slope, Real width, Real thick)
30 Real height = slope * width;
31 Real min_y = (0 <? height) - thick/2;
32 Real max_y = (0 >? height) + thick/2;
36 Box b (Interval (0, width),
37 Interval (min_y, max_y));
40 SCM at = scm_list_n (ly_symbol2scm ("beam"),
41 gh_double2scm (width),
42 gh_double2scm (slope),
43 gh_double2scm (thick),
45 return Molecule (b, at);
49 Lookup::dashed_slur (Bezier b, Real thick, Real dash)
53 for (int i= 4; i -- ;)
55 l = gh_cons (ly_offset2scm (b.control_[i]), l);
58 SCM at = (scm_list_n (ly_symbol2scm ("dashed-slur"),
59 gh_double2scm (thick),
64 Box box (Interval (0,0),Interval (0,0));
65 return Molecule (box, at);
69 Lookup::line (Real th, Offset f, Offset t)
71 SCM at = (scm_list_n (ly_symbol2scm ("draw-line"),
73 gh_double2scm (f[X_AXIS]),
74 gh_double2scm (f[Y_AXIS]),
75 gh_double2scm (t[X_AXIS]),
76 gh_double2scm (t[Y_AXIS]),
83 box[X_AXIS].widen (th/2);
84 box[Y_AXIS].widen (th/2);
86 return Molecule (box, at);
93 return Molecule (b, SCM_EOL);
97 Lookup::filledbox (Box b)
99 SCM at = (scm_list_n (ly_symbol2scm ("filledbox"),
100 gh_double2scm (-b[X_AXIS][LEFT]),
101 gh_double2scm (b[X_AXIS][RIGHT]),
102 gh_double2scm (-b[Y_AXIS][DOWN]),
103 gh_double2scm (b[Y_AXIS][UP]),
106 return Molecule (b,at);
112 * __________________________________
117 * |\ _ _ / v \ _ _ /| |
120 * | <------>| | extent
121 * | blot | | (Y_AXIS)
129 * x\_____/______________\_____/|_____v
133 * |<-------------------------->|
137 Lookup::roundfilledbox (Box b, Real blotdiameter)
139 if (b.x ().length () < blotdiameter)
141 programming_error (_f ("round filled box horizontal extent smaller than blot; decreasing blot"));
142 blotdiameter = b.x ().length ();
144 if (b.y ().length () < blotdiameter)
146 programming_error (_f ("round filled box vertical extent smaller than blot; decreasing blot"));
147 blotdiameter = b.y ().length ();
150 SCM at = (scm_list_n (ly_symbol2scm ("roundfilledbox"),
151 gh_double2scm (-b[X_AXIS][LEFT]),
152 gh_double2scm (b[X_AXIS][RIGHT]),
153 gh_double2scm (-b[Y_AXIS][DOWN]),
154 gh_double2scm (b[Y_AXIS][UP]),
155 gh_double2scm (blotdiameter),
158 return Molecule (b,at);
162 Lookup::frame (Box b, Real thick)
171 Axis o = Axis ((a+1)%NO_AXES);
174 edges[a] = b[a][d] + 0.5 * thick * Interval (-1, 1);
175 edges[o][DOWN] = b[o][DOWN] - thick/2;
176 edges[o][UP] = b[o][UP] + thick/2;
178 m.add_molecule (filledbox (edges));
180 while (flip (&d) != LEFT);
187 Make a smooth curve along the points
190 Lookup::slur (Bezier curve, Real curvethick, Real linethick)
192 Real alpha = (curve.control_[3] - curve.control_[0]).arg ();
194 Offset perp = curvethick * complex_exp (Offset (0, alpha + M_PI/2)) * 0.5;
196 back.control_[1] += perp;
197 back.control_[2] += perp;
199 curve.control_[1] -= perp;
200 curve.control_[2] -= perp;
205 scontrols[ i ] = ly_offset2scm (back.control_[i]);
207 scontrols[i+4] = ly_offset2scm (curve.control_[i]);
210 Need the weird order b.o. the way PS want its arguments
212 int indices[]= {5, 6, 7, 4, 1, 2, 3, 0};
216 list = gh_cons (scontrols[indices[i]], list);
220 SCM at = (scm_list_n (ly_symbol2scm ("bezier-sandwich"),
222 gh_double2scm (linethick),
224 Box b(curve.extent (X_AXIS),
225 curve.extent (Y_AXIS));
227 b[X_AXIS].unite (back.extent (X_AXIS));
228 b[Y_AXIS].unite (back.extent (Y_AXIS));
230 return Molecule (b, at);
257 Lookup::bezier_sandwich (Bezier top_curve, Bezier bottom_curve)
260 Need the weird order b.o. the way PS want its arguments
263 list = gh_cons (ly_offset2scm (bottom_curve.control_[3]), list);
264 list = gh_cons (ly_offset2scm (bottom_curve.control_[0]), list);
265 list = gh_cons (ly_offset2scm (bottom_curve.control_[1]), list);
266 list = gh_cons (ly_offset2scm (bottom_curve.control_[2]), list);
267 list = gh_cons (ly_offset2scm (top_curve.control_[0]), list);
268 list = gh_cons (ly_offset2scm (top_curve.control_[3]), list);
269 list = gh_cons (ly_offset2scm (top_curve.control_[2]), list);
270 list = gh_cons (ly_offset2scm (top_curve.control_[1]), list);
272 SCM horizontal_bend = scm_list_n (ly_symbol2scm ("bezier-sandwich"),
277 Interval x_extent = top_curve.extent (X_AXIS);
278 x_extent.unite (bottom_curve.extent (X_AXIS));
279 Interval y_extent = top_curve.extent (Y_AXIS);
280 y_extent.unite (bottom_curve.extent (Y_AXIS));
281 Box b (x_extent, y_extent);
283 return Molecule (b, horizontal_bend);
296 * (0,0) x /slope=dy/dx
304 Lookup::horizontal_slope (Real width, Real slope, Real height)
306 SCM width_scm = gh_double2scm (width);
307 SCM slope_scm = gh_double2scm (slope);
308 SCM height_scm = gh_double2scm (height);
309 SCM horizontal_slope = scm_list_n (ly_symbol2scm ("beam"),
310 width_scm, slope_scm,
311 height_scm, SCM_UNDEFINED);
312 Box b (Interval (0, width),
313 Interval (-height/2, height/2 + width*slope));
314 return Molecule (b, horizontal_slope);
321 Lookup::accordion (SCM s, Real staff_space, Font_metric *fm)
324 String sym = ly_scm2string (ly_car (s));
325 String reg = ly_scm2string (ly_car (ly_cdr (s)));
327 if (sym == "Discant")
329 Molecule r = fm->find_by_name ("accordion-accDiscant");
331 if (reg.left_str (1) == "F")
333 Molecule d = fm->find_by_name ("accordion-accDot");
334 d.translate_axis (staff_space * 2.5 PT, Y_AXIS);
336 reg = reg.right_str (reg.length_i ()-1);
339 if (reg.left_str (3) == "EEE")
342 reg = reg.right_str (reg.length_i ()-3);
344 else if (reg.left_str (2) == "EE")
347 reg = reg.right_str (reg.length_i ()-2);
349 else if (reg.left_str (2) == "Eh")
352 reg = reg.right_str (reg.length_i ()-2);
354 else if (reg.left_str (1) == "E")
357 reg = reg.right_str (reg.length_i ()-1);
361 Molecule d = fm->find_by_name ("accordion-accDot");
362 d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
367 Molecule d = fm->find_by_name ("accordion-accDot");
368 d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
369 d.translate_axis (0.8 * staff_space PT, X_AXIS);
374 Molecule d = fm->find_by_name ("accordion-accDot");
375 d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
376 d.translate_axis (-0.8 * staff_space PT, X_AXIS);
379 if (reg.left_str (2) == "SS")
381 Molecule d = fm->find_by_name ("accordion-accDot");
382 d.translate_axis (0.5 * staff_space PT, Y_AXIS);
383 d.translate_axis (0.4 * staff_space PT, X_AXIS);
385 d.translate_axis (-0.8 * staff_space PT, X_AXIS);
387 reg = reg.right_str (reg.length_i ()-2);
389 if (reg.left_str (1) == "S")
391 Molecule d = fm->find_by_name ("accordion-accDot");
392 d.translate_axis (0.5 * staff_space PT, Y_AXIS);
394 reg = reg.right_str (reg.length_i ()-1);
397 else if (sym == "Freebase")
399 Molecule r = fm->find_by_name ("accordion-accFreebase");
401 if (reg.left_str (1) == "F")
403 Molecule d = fm->find_by_name ("accordion-accDot");
404 d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
406 reg = reg.right_str (reg.length_i ()-1);
410 Molecule d = fm->find_by_name ("accordion-accDot");
411 d.translate_axis (staff_space * 0.5 PT, Y_AXIS);
415 else if (sym == "Bayanbase")
417 Molecule r = fm->find_by_name ("accordion-accBayanbase");
419 if (reg.left_str (1) == "T")
421 Molecule d = fm->find_by_name ("accordion-accDot");
422 d.translate_axis (staff_space * 2.5 PT, Y_AXIS);
424 reg = reg.right_str (reg.length_i ()-1);
426 /* include 4' reed just for completeness. You don't want to use this. */
427 if (reg.left_str (1) == "F")
429 Molecule d = fm->find_by_name ("accordion-accDot");
430 d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
432 reg = reg.right_str (reg.length_i ()-1);
434 if (reg.left_str (2) == "EE")
436 Molecule d = fm->find_by_name ("accordion-accDot");
437 d.translate_axis (staff_space * 0.5 PT, Y_AXIS);
438 d.translate_axis (0.4 * staff_space PT, X_AXIS);
440 d.translate_axis (-0.8 * staff_space PT, X_AXIS);
442 reg = reg.right_str (reg.length_i ()-2);
444 if (reg.left_str (1) == "E")
446 Molecule d = fm->find_by_name ("accordion-accDot");
447 d.translate_axis (staff_space * 0.5 PT, Y_AXIS);
449 reg = reg.right_str (reg.length_i ()-1);
452 else if (sym == "Stdbase")
454 Molecule r = fm->find_by_name ("accordion-accStdbase");
456 if (reg.left_str (1) == "T")
458 Molecule d = fm->find_by_name ("accordion-accDot");
459 d.translate_axis (staff_space * 3.5 PT, Y_AXIS);
461 reg = reg.right_str (reg.length_i ()-1);
463 if (reg.left_str (1) == "F")
465 Molecule d = fm->find_by_name ("accordion-accDot");
466 d.translate_axis (staff_space * 2.5 PT, Y_AXIS);
468 reg = reg.right_str (reg.length_i ()-1);
470 if (reg.left_str (1) == "M")
472 Molecule d = fm->find_by_name ("accordion-accDot");
473 d.translate_axis (staff_space * 2 PT, Y_AXIS);
474 d.translate_axis (staff_space PT, X_AXIS);
476 reg = reg.right_str (reg.length_i ()-1);
478 if (reg.left_str (1) == "E")
480 Molecule d = fm->find_by_name ("accordion-accDot");
481 d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
483 reg = reg.right_str (reg.length_i ()-1);
485 if (reg.left_str (1) == "S")
487 Molecule d = fm->find_by_name ("accordion-accDot");
488 d.translate_axis (staff_space * 0.5 PT, Y_AXIS);
490 reg = reg.right_str (reg.length_i ()-1);
493 /* ugh maybe try to use regular font for S.B. and B.B and only use one font
495 else if (sym == "SB")
497 Molecule r = fm->find_by_name ("accordion-accSB");
500 else if (sym == "BB")
502 Molecule r = fm->find_by_name ("accordion-accBB");
505 else if (sym == "OldEE")
507 Molecule r = fm->find_by_name ("accordion-accOldEE");
510 else if (sym == "OldEES")
512 Molecule r = fm->find_by_name ("accordion-accOldEES");
519 Lookup::repeat_slash (Real w, Real s, Real t)
521 SCM wid = gh_double2scm (w);
522 SCM sl = gh_double2scm (s);
523 SCM thick = gh_double2scm (t);
524 SCM slashnodot = scm_list_n (ly_symbol2scm ("repeat-slash"),
525 wid, sl, thick, SCM_UNDEFINED);
527 Box b (Interval (0, w + sqrt (sqr(t/s) + sqr (t))),
528 Interval (0, w * s));
530 return Molecule (b, slashnodot); // http://slashnodot.org
534 Lookup::bracket (Axis a, Interval iv, Real thick, Real protude)
537 Axis other = Axis((a+1)%2);
539 b[other] = Interval(-1, 1) * thick * 0.5;
541 Molecule m = filledbox (b);
543 b[a] = Interval (iv[UP] - thick, iv[UP]);
544 Interval oi = Interval (-thick/2, thick/2 + fabs (protude)) ;
545 oi *= sign (protude);
547 m.add_molecule (filledbox (b));
548 b[a] = Interval (iv[DOWN], iv[DOWN] +thick);
549 m.add_molecule (filledbox(b));
555 TODO: use rounded boxes.
557 LY_DEFINE(ly_bracket ,"ly-bracket",
559 (SCM a, SCM iv, SCM t, SCM p),
560 "Make a bracket in direction @var{a}. The extent of the bracket is
561 given by @var{iv}. The wings protude by an amount of @var{p}, which
562 may be negative. The thickness is given by @var{t}.")
564 SCM_ASSERT_TYPE(ly_axis_p (a), a, SCM_ARG1, __FUNCTION__, "axis") ;
565 SCM_ASSERT_TYPE(ly_number_pair_p (iv), iv, SCM_ARG2, __FUNCTION__, "number pair") ;
566 SCM_ASSERT_TYPE(gh_number_p (t), a, SCM_ARG3, __FUNCTION__, "number") ;
567 SCM_ASSERT_TYPE(gh_number_p(p), a, SCM_ARG4, __FUNCTION__, "number") ;
570 return Lookup::bracket ((Axis)gh_scm2int (a), ly_scm2interval (iv),
572 gh_scm2double (p)).smobbed_copy ();