2 lookup.cc -- implement simple Lookup methods.
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
8 Jan Nieuwenhuizen <janneke@gnu.org>
13 #include "line-interface.hh"
15 #include "dimensions.hh"
17 #include "string-convert.hh"
18 #include "file-path.hh"
20 #include "lily-guile.hh"
23 #include "font-metric.hh"
24 #include "interval.hh"
27 Lookup::dot (Offset p, Real radius)
29 SCM at = (scm_list_n (ly_symbol2scm ("dot"),
30 gh_double2scm (p[X_AXIS]),
31 gh_double2scm (p[Y_AXIS]),
32 gh_double2scm (radius),
35 box.add_point (p - Offset (radius, radius));
36 box.add_point (p + Offset (radius, radius));
37 return Stencil (box, at);
52 * (0,0) x /slope=dy/dx
60 Lookup::beam (Real slope, Real width, Real thick, Real blot)
62 Real height = slope * width;
63 Real min_y = (0 <? height) - thick/2;
64 Real max_y = (0 >? height) + thick/2;
66 Box b (Interval (0, width),
67 Interval (min_y, max_y));
69 SCM at = scm_list_n (ly_symbol2scm ("beam"),
70 gh_double2scm (width),
71 gh_double2scm (slope),
72 gh_double2scm (thick),
75 return Stencil (b, at);
79 Lookup::dashed_slur (Bezier b, Real thick, Real dash)
83 for (int i= 4; i -- ;)
85 l = gh_cons (ly_offset2scm (b.control_[i]), l);
88 SCM at = (scm_list_n (ly_symbol2scm ("dashed-slur"),
89 gh_double2scm (thick),
94 Box box (Interval (0,0),Interval (0,0));
95 return Stencil (box, at);
101 Lookup::horizontal_line (Interval w, Real th)
103 SCM at = scm_list_n (ly_symbol2scm ("horizontal-line"),
104 gh_double2scm (w[LEFT]),
105 gh_double2scm (w[RIGHT]),
112 box[Y_AXIS] = Interval (-th/2,th/2);
114 return Stencil (box, at);
119 Lookup::blank (Box b)
121 return Stencil (b, scm_makfrom0str (""));
125 Lookup::filled_box (Box b)
127 SCM at = (scm_list_n (ly_symbol2scm ("filledbox"),
128 gh_double2scm (-b[X_AXIS][LEFT]),
129 gh_double2scm (b[X_AXIS][RIGHT]),
130 gh_double2scm (-b[Y_AXIS][DOWN]),
131 gh_double2scm (b[Y_AXIS][UP]),
134 return Stencil (b,at);
140 * __________________________________
145 * |\ _ _ / v \ _ _ /| |
148 * | <------>| | extent
149 * | blot | | (Y_AXIS)
157 * x\_____/______________\_____/|_____v
161 * |<-------------------------->|
162 * Box extent (X_AXIS)
165 Lookup::round_filled_box (Box b, Real blotdiameter)
167 if (b.x ().length () < blotdiameter)
169 programming_error (_f ("round filled box horizontal extent smaller than blot; decreasing blot"));
170 blotdiameter = b.x ().length ();
172 if (b.y ().length () < blotdiameter)
174 programming_error (_f ("round filled box vertical extent smaller than blot; decreasing blot"));
175 blotdiameter = b.y ().length ();
178 SCM at = (scm_list_n (ly_symbol2scm ("round-filled-box"),
179 gh_double2scm (-b[X_AXIS][LEFT]),
180 gh_double2scm (b[X_AXIS][RIGHT]),
181 gh_double2scm (-b[Y_AXIS][DOWN]),
182 gh_double2scm (b[Y_AXIS][UP]),
183 gh_double2scm (blotdiameter),
186 return Stencil (b,at);
192 * Create Stencil that represents a filled polygon with round edges.
196 * (a) Only outer (convex) edges are rounded.
198 * (b) This algorithm works as expected only for polygons whose edges
199 * do not intersect. For example, the polygon ((0, 0), (q, 0), (0,
200 * q), (q, q)) has an intersection at point (q/2, q/2) and therefore
201 * will give a strange result. Even non-adjacent edges that just
202 * touch each other will in general not work as expected for non-null
205 * (c) Given a polygon ((x0, y0), (x1, y1), ... , (x (n-1), y (n-1))),
206 * if there is a natural number k such that blotdiameter is greater
207 * than the maximum of { | (x (k mod n), y (k mod n)) - (x ((k+1) mod n),
208 * y ((k+1) mod n)) |, | (x (k mod n), y (k mod n)) - (x ((k+2) mod n),
209 * y ((k+2) mod n)) |, | (x ((k+1) mod n), y ((k+1) mod n)) - (x ((k+2)
210 * mod n), y ((k+2) mod n)) | }, then the outline of the rounded
211 * polygon will exceed the outline of the core polygon. In other
212 * words: Do not draw rounded polygons that have a leg smaller or
213 * thinner than blotdiameter (or set blotdiameter to a sufficiently
214 * small value -- maybe even 0.0)!
216 * NOTE: Limitations (b) and (c) arise from the fact that round edges
217 * are made by moulding sharp edges to round ones rather than adding
218 * to a core filled polygon. For details of these two different
219 * approaches, see the thread upon the ledger lines patch that started
220 * on March 25, 2002 on the devel mailing list. The below version of
221 * round_filled_polygon () sticks to the moulding model, which the
222 * majority of the list participants finally voted for. This,
223 * however, results in the above limitations and a much increased
224 * complexity of the algorithm, since it has to compute a shrinked
225 * polygon -- which is not trivial define precisely and unambigously.
226 * With the other approach, one simply could move a circle of size
227 * blotdiameter along all edges of the polygon (which is what the
228 * postscript routine in the backend effectively does, but on the
229 * shrinked polygon). --jr
232 Lookup::round_filled_polygon (Array<Offset> points, Real blotdiameter)
234 /* TODO: Maybe print a warning if one of the above limitations
235 applies to the given polygon. However, this is quite complicated
238 /* remove consecutive duplicate points */
239 const Real epsilon = 0.01;
240 for (int i = 0; i < points.size ();)
242 int next_i = (i + 1) % points.size ();
243 Real d = (points[i] - points[next_i]).length ();
250 /* special cases: degenerated polygons */
251 if (points.size () == 0)
253 if (points.size () == 1)
254 return dot (points[0], 0.5 * blotdiameter);
255 if (points.size () == 2)
256 return Line_interface::make_line (blotdiameter, points[0], points[1]);
258 /* shrink polygon in size by 0.5 * blotdiameter */
259 Array<Offset> shrinked_points;
260 shrinked_points.set_size (points.size ());
261 bool ccw = 1; // true, if three adjacent points are counterclockwise ordered
262 for (int i = 0; i < points.size (); i++)
265 int i1 = (i + 1) % points.size ();
266 int i2 = (i + 2) % points.size ();
267 Offset p0 = points[i0];
268 Offset p1 = points[i1];
269 Offset p2 = points[i2];
270 Offset p10 = p0 - p1;
271 Offset p12 = p2 - p1;
272 if (p10.length () != 0.0)
274 Real phi = p10.arg ();
275 // rotate (p2 - p0) by (-phi)
276 Offset q = complex_multiply (p2 - p0, complex_exp (Offset (1.0, -phi)));
280 else if (q[Y_AXIS] < 0)
282 else {} // keep ccw unchanged
284 else {} // keep ccw unchanged
285 Offset p10n = (1.0 / p10.length ()) * p10; // normalize length to 1.0
286 Offset p12n = (1.0 / p12.length ()) * p12;
287 Offset p13n = 0.5 * (p10n + p12n);
288 Offset p14n = 0.5 * (p10n - p12n);
290 Real d = p13n.length () * p14n.length (); // distance p3n to line (p1..p0)
292 // special case: p0, p1, p2 are on a single line => build
293 // vector orthogonal to (p2-p0) of length 0.5 blotdiameter
295 p13[X_AXIS] = p10[Y_AXIS];
296 p13[Y_AXIS] = -p10[X_AXIS];
297 p13 = (0.5 * blotdiameter / p13.length ()) * p13;
300 p13 = (0.5 * blotdiameter / d) * p13n;
301 shrinked_points[i1] = p1 + ((ccw) ? p13 : -p13);
304 /* build scm expression and bounding box */
305 SCM shrinked_points_scm = SCM_EOL;
307 for (int i = 0; i < shrinked_points.size (); i++)
309 SCM x = gh_double2scm (shrinked_points[i][X_AXIS]);
310 SCM y = gh_double2scm (shrinked_points[i][Y_AXIS]);
311 shrinked_points_scm = gh_cons (x, gh_cons (y, shrinked_points_scm));
312 box.add_point (points[i]);
314 SCM polygon_scm = scm_list_n (ly_symbol2scm ("polygon"),
315 ly_quote_scm (shrinked_points_scm),
316 gh_double2scm (blotdiameter),
319 Stencil polygon = Stencil (box, polygon_scm);
320 shrinked_points.clear ();
329 Lookup::frame (Box b, Real thick, Real blot)
333 for (Axis a = X_AXIS; a < NO_AXES; a = Axis (a + 1))
335 Axis o = Axis ((a+1)%NO_AXES);
339 edges[a] = b[a][d] + 0.5 * thick * Interval (-1, 1);
340 edges[o][DOWN] = b[o][DOWN] - thick/2;
341 edges[o][UP] = b[o][UP] + thick/2;
343 m.add_stencil (round_filled_box (edges, blot));
345 while (flip (&d) != LEFT);
351 Make a smooth curve along the points
354 Lookup::slur (Bezier curve, Real curvethick, Real linethick)
356 Real alpha = (curve.control_[3] - curve.control_[0]).arg ();
358 Offset perp = curvethick * complex_exp (Offset (0, alpha + M_PI/2)) * 0.5;
360 back.control_[1] += perp;
361 back.control_[2] += perp;
363 curve.control_[1] -= perp;
364 curve.control_[2] -= perp;
369 scontrols[ i ] = ly_offset2scm (back.control_[i]);
371 scontrols[i+4] = ly_offset2scm (curve.control_[i]);
374 Need the weird order b.o. the way PS want its arguments
376 int indices[]= {5, 6, 7, 4, 1, 2, 3, 0};
380 list = gh_cons (scontrols[indices[i]], list);
384 SCM at = (scm_list_n (ly_symbol2scm ("bezier-sandwich"),
386 gh_double2scm (linethick),
388 Box b (curve.extent (X_AXIS),
389 curve.extent (Y_AXIS));
391 b[X_AXIS].unite (back.extent (X_AXIS));
392 b[Y_AXIS].unite (back.extent (Y_AXIS));
394 return Stencil (b, at);
421 Lookup::bezier_sandwich (Bezier top_curve, Bezier bottom_curve)
424 Need the weird order b.o. the way PS want its arguments
427 list = gh_cons (ly_offset2scm (bottom_curve.control_[3]), list);
428 list = gh_cons (ly_offset2scm (bottom_curve.control_[0]), list);
429 list = gh_cons (ly_offset2scm (bottom_curve.control_[1]), list);
430 list = gh_cons (ly_offset2scm (bottom_curve.control_[2]), list);
431 list = gh_cons (ly_offset2scm (top_curve.control_[0]), list);
432 list = gh_cons (ly_offset2scm (top_curve.control_[3]), list);
433 list = gh_cons (ly_offset2scm (top_curve.control_[2]), list);
434 list = gh_cons (ly_offset2scm (top_curve.control_[1]), list);
436 SCM horizontal_bend = scm_list_n (ly_symbol2scm ("bezier-sandwich"),
441 Interval x_extent = top_curve.extent (X_AXIS);
442 x_extent.unite (bottom_curve.extent (X_AXIS));
443 Interval y_extent = top_curve.extent (Y_AXIS);
444 y_extent.unite (bottom_curve.extent (Y_AXIS));
445 Box b (x_extent, y_extent);
447 return Stencil (b, horizontal_bend);
454 Lookup::accordion (SCM s, Real staff_space, Font_metric *fm)
457 String sym = ly_scm2string (ly_car (s));
458 String reg = ly_scm2string (ly_car (ly_cdr (s)));
460 if (sym == "Discant")
462 Stencil r = fm->find_by_name ("accordion-accDiscant");
464 if (reg.left_string (1) == "F")
466 Stencil d = fm->find_by_name ("accordion-accDot");
467 d.translate_axis (staff_space * 2.5 PT, Y_AXIS);
469 reg = reg.right_string (reg.length ()-1);
472 if (reg.left_string (3) == "EEE")
475 reg = reg.right_string (reg.length ()-3);
477 else if (reg.left_string (2) == "EE")
480 reg = reg.right_string (reg.length ()-2);
482 else if (reg.left_string (2) == "Eh")
485 reg = reg.right_string (reg.length ()-2);
487 else if (reg.left_string (1) == "E")
490 reg = reg.right_string (reg.length ()-1);
494 Stencil d = fm->find_by_name ("accordion-accDot");
495 d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
500 Stencil d = fm->find_by_name ("accordion-accDot");
501 d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
502 d.translate_axis (0.8 * staff_space PT, X_AXIS);
507 Stencil d = fm->find_by_name ("accordion-accDot");
508 d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
509 d.translate_axis (-0.8 * staff_space PT, X_AXIS);
512 if (reg.left_string (2) == "SS")
514 Stencil d = fm->find_by_name ("accordion-accDot");
515 d.translate_axis (0.5 * staff_space PT, Y_AXIS);
516 d.translate_axis (0.4 * staff_space PT, X_AXIS);
518 d.translate_axis (-0.8 * staff_space PT, X_AXIS);
520 reg = reg.right_string (reg.length ()-2);
522 if (reg.left_string (1) == "S")
524 Stencil d = fm->find_by_name ("accordion-accDot");
525 d.translate_axis (0.5 * staff_space PT, Y_AXIS);
527 reg = reg.right_string (reg.length ()-1);
530 else if (sym == "Freebase")
532 Stencil r = fm->find_by_name ("accordion-accFreebase");
534 if (reg.left_string (1) == "F")
536 Stencil d = fm->find_by_name ("accordion-accDot");
537 d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
539 reg = reg.right_string (reg.length ()-1);
543 Stencil d = fm->find_by_name ("accordion-accDot");
544 d.translate_axis (staff_space * 0.5 PT, Y_AXIS);
548 else if (sym == "Bayanbase")
550 Stencil r = fm->find_by_name ("accordion-accBayanbase");
552 if (reg.left_string (1) == "T")
554 Stencil d = fm->find_by_name ("accordion-accDot");
555 d.translate_axis (staff_space * 2.5 PT, Y_AXIS);
557 reg = reg.right_string (reg.length ()-1);
559 /* include 4' reed just for completeness. You don't want to use this. */
560 if (reg.left_string (1) == "F")
562 Stencil d = fm->find_by_name ("accordion-accDot");
563 d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
565 reg = reg.right_string (reg.length ()-1);
567 if (reg.left_string (2) == "EE")
569 Stencil d = fm->find_by_name ("accordion-accDot");
570 d.translate_axis (staff_space * 0.5 PT, Y_AXIS);
571 d.translate_axis (0.4 * staff_space PT, X_AXIS);
573 d.translate_axis (-0.8 * staff_space PT, X_AXIS);
575 reg = reg.right_string (reg.length ()-2);
577 if (reg.left_string (1) == "E")
579 Stencil d = fm->find_by_name ("accordion-accDot");
580 d.translate_axis (staff_space * 0.5 PT, Y_AXIS);
582 reg = reg.right_string (reg.length ()-1);
585 else if (sym == "Stdbase")
587 Stencil r = fm->find_by_name ("accordion-accStdbase");
589 if (reg.left_string (1) == "T")
591 Stencil d = fm->find_by_name ("accordion-accDot");
592 d.translate_axis (staff_space * 3.5 PT, Y_AXIS);
594 reg = reg.right_string (reg.length ()-1);
596 if (reg.left_string (1) == "F")
598 Stencil d = fm->find_by_name ("accordion-accDot");
599 d.translate_axis (staff_space * 2.5 PT, Y_AXIS);
601 reg = reg.right_string (reg.length ()-1);
603 if (reg.left_string (1) == "M")
605 Stencil d = fm->find_by_name ("accordion-accDot");
606 d.translate_axis (staff_space * 2 PT, Y_AXIS);
607 d.translate_axis (staff_space PT, X_AXIS);
609 reg = reg.right_string (reg.length ()-1);
611 if (reg.left_string (1) == "E")
613 Stencil d = fm->find_by_name ("accordion-accDot");
614 d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
616 reg = reg.right_string (reg.length ()-1);
618 if (reg.left_string (1) == "S")
620 Stencil d = fm->find_by_name ("accordion-accDot");
621 d.translate_axis (staff_space * 0.5 PT, Y_AXIS);
623 reg = reg.right_string (reg.length ()-1);
626 /* ugh maybe try to use regular font for S.B. and B.B and only use one font
628 else if (sym == "SB")
630 Stencil r = fm->find_by_name ("accordion-accSB");
633 else if (sym == "BB")
635 Stencil r = fm->find_by_name ("accordion-accBB");
638 else if (sym == "OldEE")
640 Stencil r = fm->find_by_name ("accordion-accOldEE");
643 else if (sym == "OldEES")
645 Stencil r = fm->find_by_name ("accordion-accOldEES");
652 Lookup::repeat_slash (Real w, Real s, Real t)
654 SCM wid = gh_double2scm (w);
655 SCM sl = gh_double2scm (s);
656 SCM thick = gh_double2scm (t);
657 SCM slashnodot = scm_list_n (ly_symbol2scm ("repeat-slash"),
658 wid, sl, thick, SCM_UNDEFINED);
660 Box b (Interval (0, w + sqrt (sqr (t/s) + sqr (t))),
661 Interval (0, w * s));
663 return Stencil (b, slashnodot); // http://slashnodot.org
668 Lookup::bracket (Axis a, Interval iv, Real thick, Real protude, Real blot)
671 Axis other = Axis ((a+1)%2);
673 b[other] = Interval (-1, 1) * thick * 0.5;
675 Stencil m = round_filled_box (b, blot);
677 b[a] = Interval (iv[UP] - thick, iv[UP]);
678 Interval oi = Interval (-thick/2, thick/2 + fabs (protude)) ;
679 oi *= sign (protude);
681 m.add_stencil (round_filled_box (b, blot));
682 b[a] = Interval (iv[DOWN], iv[DOWN] +thick);
683 m.add_stencil (round_filled_box (b,blot));
689 Lookup::triangle (Interval iv, Real thick, Real protude)
693 b[Y_AXIS] = Interval (0 <? protude , 0 >? protude);
695 SCM s = scm_list_n (ly_symbol2scm ("symmetric-x-triangle"),
696 gh_double2scm (thick),
697 gh_double2scm (iv.length ()),
698 gh_double2scm (protude), SCM_UNDEFINED);
700 return Stencil (b, s);
704 LY_DEFINE (ly_bracket ,"ly:bracket",
706 (SCM a, SCM iv, SCM t, SCM p),
707 "Make a bracket in direction @var{a}. The extent of the bracket is "
708 "given by @var{iv}. The wings protude by an amount of @var{p}, which "
709 "may be negative. The thickness is given by @var{t}.")
711 SCM_ASSERT_TYPE (is_axis (a), a, SCM_ARG1, __FUNCTION__, "axis") ;
712 SCM_ASSERT_TYPE (is_number_pair (iv), iv, SCM_ARG2, __FUNCTION__, "number pair") ;
713 SCM_ASSERT_TYPE (gh_number_p (t), a, SCM_ARG3, __FUNCTION__, "number") ;
714 SCM_ASSERT_TYPE (gh_number_p (p), a, SCM_ARG4, __FUNCTION__, "number") ;
717 return Lookup::bracket ((Axis)gh_scm2int (a), ly_scm2interval (iv),
720 0.95 * gh_scm2double (t)).smobbed_copy ();
725 LY_DEFINE (ly_filled_box ,"ly:round-filled-box",
727 (SCM xext, SCM yext, SCM blot),
728 "Make a @code{Stencil} "
729 "that prints a black box of dimensions @var{xext}, "
730 "@var{yext} and roundness @var{blot}."
733 SCM_ASSERT_TYPE (is_number_pair (xext), xext, SCM_ARG1, __FUNCTION__, "number pair") ;
734 SCM_ASSERT_TYPE (is_number_pair (yext), yext, SCM_ARG2, __FUNCTION__, "number pair") ;
735 SCM_ASSERT_TYPE (gh_number_p (blot), blot, SCM_ARG3, __FUNCTION__, "number") ;
737 return Lookup::round_filled_box (Box (ly_scm2interval (xext), ly_scm2interval (yext)),
738 gh_scm2double (blot)).smobbed_copy ();