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);
71 return Molecule (b, SCM_EOL);
75 Lookup::filledbox (Box b)
77 SCM at = (scm_list_n (ly_symbol2scm ("filledbox"),
78 gh_double2scm (-b[X_AXIS][LEFT]),
79 gh_double2scm (b[X_AXIS][RIGHT]),
80 gh_double2scm (-b[Y_AXIS][DOWN]),
81 gh_double2scm (b[Y_AXIS][UP]),
84 return Molecule (b,at);
90 * __________________________________
95 * |\ _ _ / v \ _ _ /| |
98 * | <------>| | extent
107 * x\_____/______________\_____/|_____v
111 * |<-------------------------->|
115 Lookup::roundfilledbox (Box b, Real blotdiameter)
117 if (b.x ().length () < blotdiameter)
119 programming_error (_f ("round filled box horizontal extent smaller than blot; decreasing blot"));
120 blotdiameter = b.x ().length ();
122 if (b.y ().length () < blotdiameter)
124 programming_error (_f ("round filled box vertical extent smaller than blot; decreasing blot"));
125 blotdiameter = b.y ().length ();
128 SCM at = (scm_list_n (ly_symbol2scm ("roundfilledbox"),
129 gh_double2scm (-b[X_AXIS][LEFT]),
130 gh_double2scm (b[X_AXIS][RIGHT]),
131 gh_double2scm (-b[Y_AXIS][DOWN]),
132 gh_double2scm (b[Y_AXIS][UP]),
133 gh_double2scm (blotdiameter),
136 return Molecule (b,at);
140 Lookup::frame (Box b, Real thick)
149 Axis o = Axis ((a+1)%NO_AXES);
152 edges[a] = b[a][d] + 0.5 * thick * Interval (-1, 1);
153 edges[o][DOWN] = b[o][DOWN] - thick/2;
154 edges[o][UP] = b[o][UP] + thick/2;
156 m.add_molecule (filledbox (edges));
158 while (flip (&d) != LEFT);
165 Make a smooth curve along the points
168 Lookup::slur (Bezier curve, Real curvethick, Real linethick)
170 Real alpha = (curve.control_[3] - curve.control_[0]).arg ();
172 Offset perp = curvethick * complex_exp (Offset (0, alpha + M_PI/2)) * 0.5;
174 back.control_[1] += perp;
175 back.control_[2] += perp;
177 curve.control_[1] -= perp;
178 curve.control_[2] -= perp;
183 scontrols[ i ] = ly_offset2scm (back.control_[i]);
185 scontrols[i+4] = ly_offset2scm (curve.control_[i]);
188 Need the weird order b.o. the way PS want its arguments
190 int indices[]= {5, 6, 7, 4, 1, 2, 3, 0};
194 list = gh_cons (scontrols[indices[i]], list);
198 SCM at = (scm_list_n (ly_symbol2scm ("bezier-sandwich"),
200 gh_double2scm (linethick),
202 Box b(curve.extent (X_AXIS),
203 curve.extent (Y_AXIS));
205 b[X_AXIS].unite (back.extent (X_AXIS));
206 b[Y_AXIS].unite (back.extent (Y_AXIS));
208 return Molecule (b, at);
235 Lookup::bezier_sandwich (Bezier top_curve, Bezier bottom_curve)
238 Need the weird order b.o. the way PS want its arguments
241 list = gh_cons (ly_offset2scm (bottom_curve.control_[3]), list);
242 list = gh_cons (ly_offset2scm (bottom_curve.control_[0]), list);
243 list = gh_cons (ly_offset2scm (bottom_curve.control_[1]), list);
244 list = gh_cons (ly_offset2scm (bottom_curve.control_[2]), list);
245 list = gh_cons (ly_offset2scm (top_curve.control_[0]), list);
246 list = gh_cons (ly_offset2scm (top_curve.control_[3]), list);
247 list = gh_cons (ly_offset2scm (top_curve.control_[2]), list);
248 list = gh_cons (ly_offset2scm (top_curve.control_[1]), list);
250 SCM horizontal_bend = scm_list_n (ly_symbol2scm ("bezier-sandwich"),
255 Interval x_extent = top_curve.extent (X_AXIS);
256 x_extent.unite (bottom_curve.extent (X_AXIS));
257 Interval y_extent = top_curve.extent (Y_AXIS);
258 y_extent.unite (bottom_curve.extent (Y_AXIS));
259 Box b (x_extent, y_extent);
261 return Molecule (b, horizontal_bend);
274 * (0,0) x /slope=dy/dx
282 Lookup::horizontal_slope (Real width, Real slope, Real height)
284 SCM width_scm = gh_double2scm (width);
285 SCM slope_scm = gh_double2scm (slope);
286 SCM height_scm = gh_double2scm (height);
287 SCM horizontal_slope = scm_list_n (ly_symbol2scm ("beam"),
288 width_scm, slope_scm,
289 height_scm, SCM_UNDEFINED);
290 Box b (Interval (0, width),
291 Interval (-height/2, height/2 + width*slope));
292 return Molecule (b, horizontal_slope);
299 Lookup::accordion (SCM s, Real staff_space, Font_metric *fm)
302 String sym = ly_scm2string (ly_car (s));
303 String reg = ly_scm2string (ly_car (ly_cdr (s)));
305 if (sym == "Discant")
307 Molecule r = fm->find_by_name ("accordion-accDiscant");
309 if (reg.left_str (1) == "F")
311 Molecule d = fm->find_by_name ("accordion-accDot");
312 d.translate_axis (staff_space * 2.5 PT, Y_AXIS);
314 reg = reg.right_str (reg.length_i ()-1);
317 if (reg.left_str (3) == "EEE")
320 reg = reg.right_str (reg.length_i ()-3);
322 else if (reg.left_str (2) == "EE")
325 reg = reg.right_str (reg.length_i ()-2);
327 else if (reg.left_str (2) == "Eh")
330 reg = reg.right_str (reg.length_i ()-2);
332 else if (reg.left_str (1) == "E")
335 reg = reg.right_str (reg.length_i ()-1);
339 Molecule d = fm->find_by_name ("accordion-accDot");
340 d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
345 Molecule d = fm->find_by_name ("accordion-accDot");
346 d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
347 d.translate_axis (0.8 * staff_space PT, X_AXIS);
352 Molecule d = fm->find_by_name ("accordion-accDot");
353 d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
354 d.translate_axis (-0.8 * staff_space PT, X_AXIS);
357 if (reg.left_str (2) == "SS")
359 Molecule d = fm->find_by_name ("accordion-accDot");
360 d.translate_axis (0.5 * staff_space PT, Y_AXIS);
361 d.translate_axis (0.4 * staff_space PT, X_AXIS);
363 d.translate_axis (-0.8 * staff_space PT, X_AXIS);
365 reg = reg.right_str (reg.length_i ()-2);
367 if (reg.left_str (1) == "S")
369 Molecule d = fm->find_by_name ("accordion-accDot");
370 d.translate_axis (0.5 * staff_space PT, Y_AXIS);
372 reg = reg.right_str (reg.length_i ()-1);
375 else if (sym == "Freebase")
377 Molecule r = fm->find_by_name ("accordion-accFreebase");
379 if (reg.left_str (1) == "F")
381 Molecule d = fm->find_by_name ("accordion-accDot");
382 d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
384 reg = reg.right_str (reg.length_i ()-1);
388 Molecule d = fm->find_by_name ("accordion-accDot");
389 d.translate_axis (staff_space * 0.5 PT, Y_AXIS);
393 else if (sym == "Bayanbase")
395 Molecule r = fm->find_by_name ("accordion-accBayanbase");
397 if (reg.left_str (1) == "T")
399 Molecule d = fm->find_by_name ("accordion-accDot");
400 d.translate_axis (staff_space * 2.5 PT, Y_AXIS);
402 reg = reg.right_str (reg.length_i ()-1);
404 /* include 4' reed just for completeness. You don't want to use this. */
405 if (reg.left_str (1) == "F")
407 Molecule d = fm->find_by_name ("accordion-accDot");
408 d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
410 reg = reg.right_str (reg.length_i ()-1);
412 if (reg.left_str (2) == "EE")
414 Molecule d = fm->find_by_name ("accordion-accDot");
415 d.translate_axis (staff_space * 0.5 PT, Y_AXIS);
416 d.translate_axis (0.4 * staff_space PT, X_AXIS);
418 d.translate_axis (-0.8 * staff_space PT, X_AXIS);
420 reg = reg.right_str (reg.length_i ()-2);
422 if (reg.left_str (1) == "E")
424 Molecule d = fm->find_by_name ("accordion-accDot");
425 d.translate_axis (staff_space * 0.5 PT, Y_AXIS);
427 reg = reg.right_str (reg.length_i ()-1);
430 else if (sym == "Stdbase")
432 Molecule r = fm->find_by_name ("accordion-accStdbase");
434 if (reg.left_str (1) == "T")
436 Molecule d = fm->find_by_name ("accordion-accDot");
437 d.translate_axis (staff_space * 3.5 PT, Y_AXIS);
439 reg = reg.right_str (reg.length_i ()-1);
441 if (reg.left_str (1) == "F")
443 Molecule d = fm->find_by_name ("accordion-accDot");
444 d.translate_axis (staff_space * 2.5 PT, Y_AXIS);
446 reg = reg.right_str (reg.length_i ()-1);
448 if (reg.left_str (1) == "M")
450 Molecule d = fm->find_by_name ("accordion-accDot");
451 d.translate_axis (staff_space * 2 PT, Y_AXIS);
452 d.translate_axis (staff_space PT, X_AXIS);
454 reg = reg.right_str (reg.length_i ()-1);
456 if (reg.left_str (1) == "E")
458 Molecule d = fm->find_by_name ("accordion-accDot");
459 d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
461 reg = reg.right_str (reg.length_i ()-1);
463 if (reg.left_str (1) == "S")
465 Molecule d = fm->find_by_name ("accordion-accDot");
466 d.translate_axis (staff_space * 0.5 PT, Y_AXIS);
468 reg = reg.right_str (reg.length_i ()-1);
471 /* ugh maybe try to use regular font for S.B. and B.B and only use one font
473 else if (sym == "SB")
475 Molecule r = fm->find_by_name ("accordion-accSB");
478 else if (sym == "BB")
480 Molecule r = fm->find_by_name ("accordion-accBB");
483 else if (sym == "OldEE")
485 Molecule r = fm->find_by_name ("accordion-accOldEE");
488 else if (sym == "OldEES")
490 Molecule r = fm->find_by_name ("accordion-accOldEES");
497 Lookup::repeat_slash (Real w, Real s, Real t)
499 SCM wid = gh_double2scm (w);
500 SCM sl = gh_double2scm (s);
501 SCM thick = gh_double2scm (t);
502 SCM slashnodot = scm_list_n (ly_symbol2scm ("repeat-slash"),
503 wid, sl, thick, SCM_UNDEFINED);
505 Box b (Interval (0, w + sqrt (sqr(t/s) + sqr (t))),
506 Interval (0, w * s));
508 return Molecule (b, slashnodot); // http://slashnodot.org
512 Lookup::bracket (Axis a, Interval iv, Direction d, Real thick, Real protude)
515 Axis other = Axis((a+1)%2);
517 b[other] = Interval(-1, 1) * thick * 0.5;
519 Molecule m = filledbox (b);
521 b[a] = Interval (iv[UP] - thick, iv[UP]);
522 Interval oi = Interval (-thick/2, thick/2 + protude) ;
525 m.add_molecule (filledbox (b));
526 b[a] = Interval (iv[DOWN], iv[DOWN] +thick);
527 m.add_molecule (filledbox(b));
533 ly_bracket (SCM a, SCM iv, SCM d, SCM t, SCM p)
535 SCM_ASSERT_TYPE(ly_axis_p (a), a, SCM_ARG1, __FUNCTION__, "axis") ;
536 SCM_ASSERT_TYPE(ly_number_pair_p (iv), iv, SCM_ARG1, __FUNCTION__, "number pair") ;
537 SCM_ASSERT_TYPE(ly_dir_p (d), a, SCM_ARG1, __FUNCTION__, "direction") ;
538 SCM_ASSERT_TYPE(gh_number_p (t), a, SCM_ARG1, __FUNCTION__, "number") ;
539 SCM_ASSERT_TYPE(gh_number_p(p), a, SCM_ARG1, __FUNCTION__, "number") ;
542 return Lookup::bracket ((Axis)gh_scm2int (a), ly_scm2interval (iv),
543 (Direction)gh_scm2int (d), gh_scm2double (t), gh_scm2double (p)).smobbed_copy ();
549 scm_c_define_gsubr ("ly-bracket", 5, 0, 0, (Scheme_function_unknown) ly_bracket);
552 ADD_SCM_INIT_FUNC (lookup,lookup_init);