2 lookup.cc -- implement simple Lookup methods.
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2003 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"
26 #include "interval.hh"
29 Lookup::dot (Offset p, Real radius)
31 SCM at = (scm_list_n (ly_symbol2scm ("dot"),
32 gh_double2scm (p[X_AXIS]),
33 gh_double2scm (p[Y_AXIS]),
34 gh_double2scm (radius),
37 box.add_point (p - Offset (radius, radius));
38 box.add_point (p + Offset (radius, radius));
39 return Molecule (box, at);
43 Lookup::beam (Real slope, Real width, Real thick, Real blot)
45 Real height = slope * width;
46 Real min_y = (0 <? height) - thick/2;
47 Real max_y = (0 >? height) + thick/2;
49 Box b (Interval (0, width),
50 Interval (min_y, max_y));
52 SCM at = scm_list_n (ly_symbol2scm ("beam"),
53 gh_double2scm (width),
54 gh_double2scm (slope),
55 gh_double2scm (thick),
58 return Molecule (b, at);
62 Lookup::dashed_slur (Bezier b, Real thick, Real dash)
66 for (int i= 4; i -- ;)
68 l = gh_cons (ly_offset2scm (b.control_[i]), l);
71 SCM at = (scm_list_n (ly_symbol2scm ("dashed-slur"),
72 gh_double2scm (thick),
77 Box box (Interval (0,0),Interval (0,0));
78 return Molecule (box, at);
82 Lookup::line (Real th, Offset from, Offset to)
84 SCM at = scm_list_n (ly_symbol2scm ("draw-line"),
86 gh_double2scm (from[X_AXIS]),
87 gh_double2scm (from[Y_AXIS]),
88 gh_double2scm (to[X_AXIS]),
89 gh_double2scm (to[Y_AXIS]),
96 box[X_AXIS].widen (th/2);
97 box[Y_AXIS].widen (th/2);
99 return Molecule (box, at);
103 Lookup::dashed_line (Real thick, Offset from, Offset to,
104 Real dash_period, Real dash_fraction)
106 dash_fraction = (dash_fraction >? 0) <? 1.0;
107 Real on = dash_fraction * dash_period + thick;
108 Real off = dash_period - on;
110 SCM at = scm_list_n (ly_symbol2scm ("dashed-line"),
111 gh_double2scm (thick),
114 gh_double2scm (to[X_AXIS] - from[X_AXIS]),
115 gh_double2scm (to[Y_AXIS] - from[Y_AXIS]),
119 box.add_point (Offset (0,0));
120 box.add_point (to - from);
122 box[X_AXIS].widen (thick/2);
123 box[Y_AXIS].widen (thick/2);
125 Molecule m = Molecule (box, at);
131 Lookup::horizontal_line (Interval w, Real th)
133 SCM at = scm_list_n (ly_symbol2scm ("horizontal-line"),
134 gh_double2scm (w[LEFT]),
135 gh_double2scm (w[RIGHT]),
142 box[Y_AXIS] = Interval (-th/2,th/2);
144 return Molecule (box, at);
149 Lookup::blank (Box b)
151 return Molecule (b, scm_makfrom0str (""));
155 Lookup::filled_box (Box b)
157 SCM at = (scm_list_n (ly_symbol2scm ("filledbox"),
158 gh_double2scm (-b[X_AXIS][LEFT]),
159 gh_double2scm (b[X_AXIS][RIGHT]),
160 gh_double2scm (-b[Y_AXIS][DOWN]),
161 gh_double2scm (b[Y_AXIS][UP]),
164 return Molecule (b,at);
170 * __________________________________
175 * |\ _ _ / v \ _ _ /| |
178 * | <------>| | extent
179 * | blot | | (Y_AXIS)
187 * x\_____/______________\_____/|_____v
191 * |<-------------------------->|
195 Lookup::round_filled_box (Box b, Real blotdiameter)
197 if (b.x ().length () < blotdiameter)
199 programming_error (_f ("round filled box horizontal extent smaller than blot; decreasing blot"));
200 blotdiameter = b.x ().length ();
202 if (b.y ().length () < blotdiameter)
204 programming_error (_f ("round filled box vertical extent smaller than blot; decreasing blot"));
205 blotdiameter = b.y ().length ();
208 SCM at = (scm_list_n (ly_symbol2scm ("round-filled-box"),
209 gh_double2scm (-b[X_AXIS][LEFT]),
210 gh_double2scm (b[X_AXIS][RIGHT]),
211 gh_double2scm (-b[Y_AXIS][DOWN]),
212 gh_double2scm (b[Y_AXIS][UP]),
213 gh_double2scm (blotdiameter),
216 return Molecule (b,at);
222 * Create Molecule that represents a filled polygon with round edges.
226 * (a) Only outer (convex) edges are rounded.
228 * (b) This algorithm works as expected only for polygons whose edges
229 * do not intersect. For example, the polygon ((0, 0), (q, 0), (0,
230 * q), (q, q)) has an intersection at point (q/2, q/2) and therefore
231 * will give a strange result. Even non-adjacent edges that just
232 * touch each other will in general not work as expected for non-null
235 * (c) Given a polygon ((x0, y0), (x1, y1), ... , (x(n-1), y(n-1))),
236 * if there is a natural number k such that blotdiameter is greater
237 * than the maximum of { | (x(k mod n), y(k mod n)) - (x((k+1) mod n),
238 * y((k+1) mod n)) |, | (x(k mod n), y(k mod n)) - (x((k+2) mod n),
239 * y((k+2) mod n)) |, | (x((k+1) mod n), y((k+1) mod n)) - (x((k+2)
240 * mod n), y((k+2) mod n)) | }, then the outline of the rounded
241 * polygon will exceed the outline of the core polygon. In other
242 * words: Do not draw rounded polygons that have a leg smaller or
243 * thinner than blotdiameter (or set blotdiameter to a sufficiently
244 * small value -- maybe even 0.0)!
246 * NOTE: Limitations (b) and (c) arise from the fact that round edges
247 * are made by moulding sharp edges to round ones rather than adding
248 * to a core filled polygon. For details of these two different
249 * approaches, see the thread upon the ledger lines patch that started
250 * on March 25, 2002 on the devel mailing list. The below version of
251 * round_filled_polygon() sticks to the moulding model, which the
252 * majority of the list participants finally voted for. This,
253 * however, results in the above limitations and a much increased
254 * complexity of the algorithm, since it has to compute a shrinked
255 * polygon -- which is not trivial define precisely and unambigously.
256 * With the other approach, one simply could move a circle of size
257 * blotdiameter along all edges of the polygon (which is what the
258 * postscript routine in the backend effectively does, but on the
259 * shrinked polygon). --jr
262 Lookup::round_filled_polygon (Array<Offset> points, Real blotdiameter)
264 /* TODO: Maybe print a warning if one of the above limitations
265 applies to the given polygon. However, this is quite complicated
268 /* remove consecutive duplicate points */
269 const Real epsilon = 0.01;
270 for (int i = 0; i < points.size ();)
272 int next_i = (i + 1) % points.size ();
273 Real d = (points[i] - points[next_i]).length ();
280 /* special cases: degenerated polygons */
281 if (points.size () == 0)
283 if (points.size () == 1)
284 return dot (points[0], 0.5 * blotdiameter);
285 if (points.size () == 2)
286 return line (blotdiameter, points[0], points[1]);
288 /* shrink polygon in size by 0.5 * blotdiameter */
289 Array<Offset> shrinked_points;
290 shrinked_points.set_size (points.size ());
291 bool ccw = 1; // true, if three adjacent points are counterclockwise ordered
292 for (int i = 0; i < points.size (); i++)
295 int i1 = (i + 1) % points.size ();
296 int i2 = (i + 2) % points.size ();
297 Offset p0 = points[i0];
298 Offset p1 = points[i1];
299 Offset p2 = points[i2];
300 Offset p10 = p0 - p1;
301 Offset p12 = p2 - p1;
302 if (p10.length () != 0.0)
304 Real phi = p10.arg ();
305 // rotate (p2 - p0) by (-phi)
306 Offset q = complex_multiply (p2 - p0, complex_exp (Offset (1.0, -phi)));
310 else if (q[Y_AXIS] < 0)
312 else {} // keep ccw unchanged
314 else {} // keep ccw unchanged
315 Offset p10n = (1.0 / p10.length ()) * p10; // normalize length to 1.0
316 Offset p12n = (1.0 / p12.length ()) * p12;
317 Offset p13n = 0.5 * (p10n + p12n);
318 Offset p14n = 0.5 * (p10n - p12n);
320 Real d = p13n.length () * p14n.length (); // distance p3n to line(p1..p0)
322 // special case: p0, p1, p2 are on a single line => build
323 // vector orthogonal to (p2-p0) of length 0.5 blotdiameter
325 p13[X_AXIS] = p10[Y_AXIS];
326 p13[Y_AXIS] = -p10[X_AXIS];
327 p13 = (0.5 * blotdiameter / p13.length ()) * p13;
330 p13 = (0.5 * blotdiameter / d) * p13n;
331 shrinked_points[i1] = p1 + ((ccw) ? p13 : -p13);
334 /* build scm expression and bounding box */
335 SCM shrinked_points_scm = SCM_EOL;
337 for (int i = 0; i < shrinked_points.size (); i++)
339 SCM x = gh_double2scm (shrinked_points[i][X_AXIS]);
340 SCM y = gh_double2scm (shrinked_points[i][Y_AXIS]);
341 shrinked_points_scm = gh_cons (x, gh_cons (y, shrinked_points_scm));
342 box.add_point (points[i]);
344 SCM polygon_scm = scm_list_n (ly_symbol2scm ("polygon"),
345 ly_quote_scm (shrinked_points_scm),
346 gh_double2scm (blotdiameter),
349 Molecule polygon = Molecule (box, polygon_scm);
350 shrinked_points.clear ();
358 should use rounded corners.
361 Lookup::frame (Box b, Real thick, Real blot)
365 for (Axis a = X_AXIS; a < NO_AXES; a = Axis (a + 1))
367 Axis o = Axis ((a+1)%NO_AXES);
371 edges[a] = b[a][d] + 0.5 * thick * Interval (-1, 1);
372 edges[o][DOWN] = b[o][DOWN] - thick/2;
373 edges[o][UP] = b[o][UP] + thick/2;
375 m.add_molecule (round_filled_box (edges, blot));
377 while (flip (&d) != LEFT);
383 Make a smooth curve along the points
386 Lookup::slur (Bezier curve, Real curvethick, Real linethick)
388 Real alpha = (curve.control_[3] - curve.control_[0]).arg ();
390 Offset perp = curvethick * complex_exp (Offset (0, alpha + M_PI/2)) * 0.5;
392 back.control_[1] += perp;
393 back.control_[2] += perp;
395 curve.control_[1] -= perp;
396 curve.control_[2] -= perp;
401 scontrols[ i ] = ly_offset2scm (back.control_[i]);
403 scontrols[i+4] = ly_offset2scm (curve.control_[i]);
406 Need the weird order b.o. the way PS want its arguments
408 int indices[]= {5, 6, 7, 4, 1, 2, 3, 0};
412 list = gh_cons (scontrols[indices[i]], list);
416 SCM at = (scm_list_n (ly_symbol2scm ("bezier-sandwich"),
418 gh_double2scm (linethick),
420 Box b(curve.extent (X_AXIS),
421 curve.extent (Y_AXIS));
423 b[X_AXIS].unite (back.extent (X_AXIS));
424 b[Y_AXIS].unite (back.extent (Y_AXIS));
426 return Molecule (b, at);
453 Lookup::bezier_sandwich (Bezier top_curve, Bezier bottom_curve)
456 Need the weird order b.o. the way PS want its arguments
459 list = gh_cons (ly_offset2scm (bottom_curve.control_[3]), list);
460 list = gh_cons (ly_offset2scm (bottom_curve.control_[0]), list);
461 list = gh_cons (ly_offset2scm (bottom_curve.control_[1]), list);
462 list = gh_cons (ly_offset2scm (bottom_curve.control_[2]), list);
463 list = gh_cons (ly_offset2scm (top_curve.control_[0]), list);
464 list = gh_cons (ly_offset2scm (top_curve.control_[3]), list);
465 list = gh_cons (ly_offset2scm (top_curve.control_[2]), list);
466 list = gh_cons (ly_offset2scm (top_curve.control_[1]), list);
468 SCM horizontal_bend = scm_list_n (ly_symbol2scm ("bezier-sandwich"),
473 Interval x_extent = top_curve.extent (X_AXIS);
474 x_extent.unite (bottom_curve.extent (X_AXIS));
475 Interval y_extent = top_curve.extent (Y_AXIS);
476 y_extent.unite (bottom_curve.extent (Y_AXIS));
477 Box b (x_extent, y_extent);
479 return Molecule (b, horizontal_bend);
492 * (0,0) x /slope=dy/dx
500 Lookup::horizontal_slope (Real width, Real slope, Real height)
502 SCM width_scm = gh_double2scm (width);
503 SCM slope_scm = gh_double2scm (slope);
504 SCM height_scm = gh_double2scm (height);
505 SCM horizontal_slope = scm_list_n (ly_symbol2scm ("beam"),
506 width_scm, slope_scm,
507 height_scm, SCM_UNDEFINED);
508 Box b (Interval (0, width),
509 Interval (-height/2, height/2 + width*slope));
510 return Molecule (b, horizontal_slope);
517 Lookup::accordion (SCM s, Real staff_space, Font_metric *fm)
520 String sym = ly_scm2string (ly_car (s));
521 String reg = ly_scm2string (ly_car (ly_cdr (s)));
523 if (sym == "Discant")
525 Molecule r = fm->find_by_name ("accordion-accDiscant");
527 if (reg.left_string (1) == "F")
529 Molecule d = fm->find_by_name ("accordion-accDot");
530 d.translate_axis (staff_space * 2.5 PT, Y_AXIS);
532 reg = reg.right_string (reg.length ()-1);
535 if (reg.left_string (3) == "EEE")
538 reg = reg.right_string (reg.length ()-3);
540 else if (reg.left_string (2) == "EE")
543 reg = reg.right_string (reg.length ()-2);
545 else if (reg.left_string (2) == "Eh")
548 reg = reg.right_string (reg.length ()-2);
550 else if (reg.left_string (1) == "E")
553 reg = reg.right_string (reg.length ()-1);
557 Molecule d = fm->find_by_name ("accordion-accDot");
558 d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
563 Molecule d = fm->find_by_name ("accordion-accDot");
564 d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
565 d.translate_axis (0.8 * staff_space PT, X_AXIS);
570 Molecule d = fm->find_by_name ("accordion-accDot");
571 d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
572 d.translate_axis (-0.8 * staff_space PT, X_AXIS);
575 if (reg.left_string (2) == "SS")
577 Molecule d = fm->find_by_name ("accordion-accDot");
578 d.translate_axis (0.5 * staff_space PT, Y_AXIS);
579 d.translate_axis (0.4 * staff_space PT, X_AXIS);
581 d.translate_axis (-0.8 * staff_space PT, X_AXIS);
583 reg = reg.right_string (reg.length ()-2);
585 if (reg.left_string (1) == "S")
587 Molecule d = fm->find_by_name ("accordion-accDot");
588 d.translate_axis (0.5 * staff_space PT, Y_AXIS);
590 reg = reg.right_string (reg.length ()-1);
593 else if (sym == "Freebase")
595 Molecule r = fm->find_by_name ("accordion-accFreebase");
597 if (reg.left_string (1) == "F")
599 Molecule d = fm->find_by_name ("accordion-accDot");
600 d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
602 reg = reg.right_string (reg.length ()-1);
606 Molecule d = fm->find_by_name ("accordion-accDot");
607 d.translate_axis (staff_space * 0.5 PT, Y_AXIS);
611 else if (sym == "Bayanbase")
613 Molecule r = fm->find_by_name ("accordion-accBayanbase");
615 if (reg.left_string (1) == "T")
617 Molecule d = fm->find_by_name ("accordion-accDot");
618 d.translate_axis (staff_space * 2.5 PT, Y_AXIS);
620 reg = reg.right_string (reg.length ()-1);
622 /* include 4' reed just for completeness. You don't want to use this. */
623 if (reg.left_string (1) == "F")
625 Molecule d = fm->find_by_name ("accordion-accDot");
626 d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
628 reg = reg.right_string (reg.length ()-1);
630 if (reg.left_string (2) == "EE")
632 Molecule d = fm->find_by_name ("accordion-accDot");
633 d.translate_axis (staff_space * 0.5 PT, Y_AXIS);
634 d.translate_axis (0.4 * staff_space PT, X_AXIS);
636 d.translate_axis (-0.8 * staff_space PT, X_AXIS);
638 reg = reg.right_string (reg.length ()-2);
640 if (reg.left_string (1) == "E")
642 Molecule d = fm->find_by_name ("accordion-accDot");
643 d.translate_axis (staff_space * 0.5 PT, Y_AXIS);
645 reg = reg.right_string (reg.length ()-1);
648 else if (sym == "Stdbase")
650 Molecule r = fm->find_by_name ("accordion-accStdbase");
652 if (reg.left_string (1) == "T")
654 Molecule d = fm->find_by_name ("accordion-accDot");
655 d.translate_axis (staff_space * 3.5 PT, Y_AXIS);
657 reg = reg.right_string (reg.length ()-1);
659 if (reg.left_string (1) == "F")
661 Molecule d = fm->find_by_name ("accordion-accDot");
662 d.translate_axis (staff_space * 2.5 PT, Y_AXIS);
664 reg = reg.right_string (reg.length ()-1);
666 if (reg.left_string (1) == "M")
668 Molecule d = fm->find_by_name ("accordion-accDot");
669 d.translate_axis (staff_space * 2 PT, Y_AXIS);
670 d.translate_axis (staff_space PT, X_AXIS);
672 reg = reg.right_string (reg.length ()-1);
674 if (reg.left_string (1) == "E")
676 Molecule d = fm->find_by_name ("accordion-accDot");
677 d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
679 reg = reg.right_string (reg.length ()-1);
681 if (reg.left_string (1) == "S")
683 Molecule d = fm->find_by_name ("accordion-accDot");
684 d.translate_axis (staff_space * 0.5 PT, Y_AXIS);
686 reg = reg.right_string (reg.length ()-1);
689 /* ugh maybe try to use regular font for S.B. and B.B and only use one font
691 else if (sym == "SB")
693 Molecule r = fm->find_by_name ("accordion-accSB");
696 else if (sym == "BB")
698 Molecule r = fm->find_by_name ("accordion-accBB");
701 else if (sym == "OldEE")
703 Molecule r = fm->find_by_name ("accordion-accOldEE");
706 else if (sym == "OldEES")
708 Molecule r = fm->find_by_name ("accordion-accOldEES");
715 Lookup::repeat_slash (Real w, Real s, Real t)
717 SCM wid = gh_double2scm (w);
718 SCM sl = gh_double2scm (s);
719 SCM thick = gh_double2scm (t);
720 SCM slashnodot = scm_list_n (ly_symbol2scm ("repeat-slash"),
721 wid, sl, thick, SCM_UNDEFINED);
723 Box b (Interval (0, w + sqrt (sqr(t/s) + sqr (t))),
724 Interval (0, w * s));
726 return Molecule (b, slashnodot); // http://slashnodot.org
731 Lookup::bracket (Axis a, Interval iv, Real thick, Real protude, Real blot)
734 Axis other = Axis((a+1)%2);
736 b[other] = Interval(-1, 1) * thick * 0.5;
738 Molecule m = round_filled_box (b, blot);
740 b[a] = Interval (iv[UP] - thick, iv[UP]);
741 Interval oi = Interval (-thick/2, thick/2 + fabs (protude)) ;
742 oi *= sign (protude);
744 m.add_molecule (round_filled_box (b, blot));
745 b[a] = Interval (iv[DOWN], iv[DOWN] +thick);
746 m.add_molecule (round_filled_box (b,blot));
752 Lookup::triangle (Interval iv, Real thick, Real protude)
756 b[Y_AXIS] = Interval (0 <? protude , 0 >? protude);
758 SCM s = scm_list_n (ly_symbol2scm ("symmetric-x-triangle"),
759 gh_double2scm (thick),
760 gh_double2scm (iv.length()),
761 gh_double2scm (protude), SCM_UNDEFINED);
763 return Molecule (b, s);
768 TODO: use rounded boxes.
770 LY_DEFINE(ly_bracket ,"ly:bracket",
772 (SCM a, SCM iv, SCM t, SCM p),
773 "Make a bracket in direction @var{a}. The extent of the bracket is "
774 "given by @var{iv}. The wings protude by an amount of @var{p}, which "
775 "may be negative. The thickness is given by @var{t}.")
777 SCM_ASSERT_TYPE(is_axis (a), a, SCM_ARG1, __FUNCTION__, "axis") ;
778 SCM_ASSERT_TYPE(is_number_pair (iv), iv, SCM_ARG2, __FUNCTION__, "number pair") ;
779 SCM_ASSERT_TYPE(gh_number_p (t), a, SCM_ARG3, __FUNCTION__, "number") ;
780 SCM_ASSERT_TYPE(gh_number_p (p), a, SCM_ARG4, __FUNCTION__, "number") ;
783 return Lookup::bracket ((Axis)gh_scm2int (a), ly_scm2interval (iv),
786 gh_scm2double (t)).smobbed_copy ();
791 LY_DEFINE(ly_filled_box ,"ly:round-filled-box",
793 (SCM xext, SCM yext, SCM blot),
794 "Make a filled-box of dimensions @var{xext}, @var{yext} and roundness @var{blot}.")
796 SCM_ASSERT_TYPE(is_number_pair (xext), xext, SCM_ARG1, __FUNCTION__, "number pair") ;
797 SCM_ASSERT_TYPE(is_number_pair (yext), yext, SCM_ARG2, __FUNCTION__, "number pair") ;
798 SCM_ASSERT_TYPE(gh_number_p (blot), blot, SCM_ARG3, __FUNCTION__, "number") ;
800 return Lookup::round_filled_box (Box (ly_scm2interval (xext), ly_scm2interval (yext)),
801 gh_scm2double (blot)).smobbed_copy ();