2 lookup.cc -- implement simple Lookup methods.
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
8 Jan Nieuwenhuizen <janneke@gnu.org>
16 #include "line-interface.hh"
18 #include "dimensions.hh"
20 #include "string-convert.hh"
21 #include "file-path.hh"
23 #include "lily-guile.hh"
24 #include "font-metric.hh"
27 Lookup::dot (Offset p, Real radius)
29 SCM at = (scm_list_n (ly_symbol2scm ("dot"),
30 scm_make_real (p[X_AXIS]),
31 scm_make_real (p[Y_AXIS]),
32 scm_make_real (radius),
35 box.add_point (p - Offset (radius, radius));
36 box.add_point (p + Offset (radius, radius));
37 return Stencil (box, at);
50 * (0, 0)x /slope = dy/dx
58 Lookup::beam (Real slope, Real width, Real thick, Real blot)
60 Real height = slope * width;
61 Real min_y = min (0., height) - thick / 2;
62 Real max_y = max (0., height) + thick / 2;
64 Box b (Interval (0, width),
65 Interval (min_y, max_y));
67 SCM at = scm_list_n (ly_symbol2scm ("beam"),
68 scm_make_real (width),
69 scm_make_real (slope),
70 scm_make_real (thick),
73 return Stencil (b, at);
77 Lookup::dashed_slur (Bezier b, Real thick, Real dash_period, Real dash_fraction)
81 Real on = dash_fraction * dash_period;
82 Real off = dash_period - on;
86 l = scm_cons (ly_offset2scm (b.control_[i]), l);
89 SCM at = (scm_list_n (ly_symbol2scm ("dashed-slur"),
90 scm_make_real (thick),
96 Box box (Interval (0, 0), Interval (0, 0));
97 return Stencil (box, at);
101 Lookup::horizontal_line (Interval w, Real th)
103 SCM at = scm_list_n (ly_symbol2scm ("horizontal-line"),
104 scm_make_real (w[LEFT]),
105 scm_make_real (w[RIGHT]),
111 box[Y_AXIS] = Interval (-th / 2, th / 2);
113 return Stencil (box, at);
117 Lookup::blank (Box b)
119 return Stencil (b, scm_makfrom0str (""));
123 Lookup::filled_box (Box b)
125 SCM at = (scm_list_n (ly_symbol2scm ("filledbox"),
126 scm_make_real (-b[X_AXIS][LEFT]),
127 scm_make_real (b[X_AXIS][RIGHT]),
128 scm_make_real (-b[Y_AXIS][DOWN]),
129 scm_make_real (b[Y_AXIS][UP]),
132 return Stencil (b, at);
138 * __________________________________
143 * |\ _ _ / v \ _ _ /| |
146 * | <------>| | extent
147 * | blot | | (Y_AXIS)
155 * x\_____/______________\_____/|_____v
159 * |<-------------------------->|
160 * Box extent (X_AXIS)
163 Lookup::round_filled_box (Box b, Real blotdiameter)
165 if (b.x ().length () < blotdiameter)
167 blotdiameter = b.x ().length ();
169 if (b.y ().length () < blotdiameter)
171 blotdiameter = b.y ().length ();
174 SCM at = (scm_list_n (ly_symbol2scm ("round-filled-box"),
175 scm_make_real (-b[X_AXIS][LEFT]),
176 scm_make_real (b[X_AXIS][RIGHT]),
177 scm_make_real (-b[Y_AXIS][DOWN]),
178 scm_make_real (b[Y_AXIS][UP]),
179 scm_make_real (blotdiameter),
182 return Stencil (b, at);
186 * Create Stencil that represents a filled polygon with round edges.
190 * (a) Only outer (convex) edges are rounded.
192 * (b) This algorithm works as expected only for polygons whose edges
193 * do not intersect. For example, the polygon ((0, 0), (q, 0), (0,
194 * q), (q, q)) has an intersection at point (q/2, q/2) and therefore
195 * will give a strange result. Even non-adjacent edges that just
196 * touch each other will in general not work as expected for non-null
199 * (c) Given a polygon ((x0, y0), (x1, y1), ... , (x (n-1), y (n-1))),
200 * if there is a natural number k such that blotdiameter is greater
201 * than the maximum of { | (x (k mod n), y (k mod n)) - (x ((k+1) mod n),
202 * y ((k+1) mod n)) |, | (x (k mod n), y (k mod n)) - (x ((k+2) mod n),
203 * y ((k+2) mod n)) |, | (x ((k+1) mod n), y ((k+1) mod n)) - (x ((k+2)
204 * mod n), y ((k+2) mod n)) | }, then the outline of the rounded
205 * polygon will exceed the outline of the core polygon. In other
206 * words: Do not draw rounded polygons that have a leg smaller or
207 * thinner than blotdiameter (or set blotdiameter to a sufficiently
208 * small value -- maybe even 0.0)!
210 * NOTE: Limitations (b) and (c) arise from the fact that round edges
211 * are made by moulding sharp edges to round ones rather than adding
212 * to a core filled polygon. For details of these two different
213 * approaches, see the thread upon the ledger lines patch that started
214 * on March 25, 2002 on the devel mailing list. The below version of
215 * round_filled_polygon () sticks to the moulding model, which the
216 * majority of the list participants finally voted for. This,
217 * however, results in the above limitations and a much increased
218 * complexity of the algorithm, since it has to compute a shrinked
219 * polygon -- which is not trivial define precisely and unambigously.
220 * With the other approach, one simply could move a circle of size
221 * blotdiameter along all edges of the polygon (which is what the
222 * postscript routine in the backend effectively does, but on the
223 * shrinked polygon). --jr
226 Lookup::round_filled_polygon (Array<Offset> points, Real blotdiameter)
228 /* TODO: Maybe print a warning if one of the above limitations
229 applies to the given polygon. However, this is quite complicated
232 /* remove consecutive duplicate points */
233 const Real epsilon = 0.01;
234 for (int i = 0; i < points.size ();)
236 int next_i = (i + 1) % points.size ();
237 Real d = (points[i] - points[next_i]).length ();
244 /* special cases: degenerated polygons */
245 if (points.size () == 0)
247 if (points.size () == 1)
248 return dot (points[0], 0.5 * blotdiameter);
249 if (points.size () == 2)
250 return Line_interface::make_line (blotdiameter, points[0], points[1]);
252 /* shrink polygon in size by 0.5 * blotdiameter */
253 Array<Offset> shrinked_points;
254 shrinked_points.set_size (points.size ());
255 bool ccw = 1; // true, if three adjacent points are counterclockwise ordered
256 for (int i = 0; i < points.size (); i++)
259 int i1 = (i + 1) % points.size ();
260 int i2 = (i + 2) % points.size ();
261 Offset p0 = points[i0];
262 Offset p1 = points[i1];
263 Offset p2 = points[i2];
264 Offset p10 = p0 - p1;
265 Offset p12 = p2 - p1;
266 if (p10.length () != 0.0)
268 Real phi = p10.arg ();
269 // rotate (p2 - p0) by (-phi)
270 Offset q = complex_multiply (p2 - p0, complex_exp (Offset (1.0, -phi)));
274 else if (q[Y_AXIS] < 0)
276 else {} // keep ccw unchanged
278 else {} // keep ccw unchanged
279 Offset p10n = (1.0 / p10.length ()) * p10; // normalize length to 1.0
280 Offset p12n = (1.0 / p12.length ()) * p12;
281 Offset p13n = 0.5 * (p10n + p12n);
282 Offset p14n = 0.5 * (p10n - p12n);
284 Real d = p13n.length () * p14n.length (); // distance p3n to line (p1..p0)
286 // special case: p0, p1, p2 are on a single line => build
287 // vector orthogonal to (p2-p0) of length 0.5 blotdiameter
289 p13[X_AXIS] = p10[Y_AXIS];
290 p13[Y_AXIS] = -p10[X_AXIS];
291 p13 = (0.5 * blotdiameter / p13.length ()) * p13;
294 p13 = (0.5 * blotdiameter / d) * p13n;
295 shrinked_points[i1] = p1 + ((ccw) ? p13 : -p13);
298 /* build scm expression and bounding box */
299 SCM shrinked_points_scm = SCM_EOL;
301 for (int i = 0; i < shrinked_points.size (); i++)
303 SCM x = scm_make_real (shrinked_points[i][X_AXIS]);
304 SCM y = scm_make_real (shrinked_points[i][Y_AXIS]);
305 shrinked_points_scm = scm_cons (x, scm_cons (y, shrinked_points_scm));
306 box.add_point (points[i]);
308 SCM polygon_scm = scm_list_n (ly_symbol2scm ("polygon"),
309 ly_quote_scm (shrinked_points_scm),
310 scm_make_real (blotdiameter),
314 Stencil polygon = Stencil (box, polygon_scm);
315 shrinked_points.clear ();
323 Lookup::frame (Box b, Real thick, Real blot)
327 for (Axis a = X_AXIS; a < NO_AXES; a = Axis (a + 1))
329 Axis o = Axis ((a + 1)%NO_AXES);
333 edges[a] = b[a][d] + 0.5 * thick * Interval (-1, 1);
334 edges[o][DOWN] = b[o][DOWN] - thick / 2;
335 edges[o][UP] = b[o][UP] + thick / 2;
337 m.add_stencil (round_filled_box (edges, blot));
339 while (flip (&d) != LEFT);
345 Make a smooth curve along the points
348 Lookup::slur (Bezier curve, Real curvethick, Real linethick)
350 Real alpha = (curve.control_[3] - curve.control_[0]).arg ();
352 Offset perp = curvethick * complex_exp (Offset (0, alpha + M_PI / 2)) * 0.5;
354 back.control_[1] += perp;
355 back.control_[2] += perp;
357 curve.control_[1] -= perp;
358 curve.control_[2] -= perp;
362 for (int i = 4; i--;)
363 scontrols[ i ] = ly_offset2scm (back.control_[i]);
364 for (int i = 4; i--;)
365 scontrols[i + 4] = ly_offset2scm (curve.control_[i]);
368 Need the weird order b.o. the way PS want its arguments
370 int indices[] = {5, 6, 7, 4, 1, 2, 3, 0};
372 for (int i = 8; i--;)
374 list = scm_cons (scontrols[indices[i]], list);
377 SCM at = (scm_list_n (ly_symbol2scm ("bezier-sandwich"),
379 scm_make_real (linethick),
381 Box b (curve.extent (X_AXIS),
382 curve.extent (Y_AXIS));
384 b[X_AXIS].unite (back.extent (X_AXIS));
385 b[Y_AXIS].unite (back.extent (Y_AXIS));
387 return Stencil (b, at);
414 Lookup::bezier_sandwich (Bezier top_curve, Bezier bottom_curve)
417 Need the weird order b.o. the way PS want its arguments
420 list = scm_cons (ly_offset2scm (bottom_curve.control_[3]), list);
421 list = scm_cons (ly_offset2scm (bottom_curve.control_[0]), list);
422 list = scm_cons (ly_offset2scm (bottom_curve.control_[1]), list);
423 list = scm_cons (ly_offset2scm (bottom_curve.control_[2]), list);
424 list = scm_cons (ly_offset2scm (top_curve.control_[0]), list);
425 list = scm_cons (ly_offset2scm (top_curve.control_[3]), list);
426 list = scm_cons (ly_offset2scm (top_curve.control_[2]), list);
427 list = scm_cons (ly_offset2scm (top_curve.control_[1]), list);
429 SCM horizontal_bend = scm_list_n (ly_symbol2scm ("bezier-sandwich"),
434 Interval x_extent = top_curve.extent (X_AXIS);
435 x_extent.unite (bottom_curve.extent (X_AXIS));
436 Interval y_extent = top_curve.extent (Y_AXIS);
437 y_extent.unite (bottom_curve.extent (Y_AXIS));
438 Box b (x_extent, y_extent);
440 return Stencil (b, horizontal_bend);
447 Lookup::accordion (SCM s, Real staff_space, Font_metric *fm)
450 String sym = ly_scm2string (scm_car (s));
451 String reg = ly_scm2string (scm_car (scm_cdr (s)));
453 if (sym == "Discant")
455 Stencil r = fm->find_by_name ("accordion.accDiscant");
457 if (reg.left_string (1) == "F")
459 Stencil d = fm->find_by_name ("accordion.accDot");
460 d.translate_axis (staff_space * 2.5 PT, Y_AXIS);
462 reg = reg.right_string (reg.length () - 1);
465 if (reg.left_string (3) == "EEE")
468 reg = reg.right_string (reg.length () - 3);
470 else if (reg.left_string (2) == "EE")
473 reg = reg.right_string (reg.length () - 2);
475 else if (reg.left_string (2) == "Eh")
478 reg = reg.right_string (reg.length () - 2);
480 else if (reg.left_string (1) == "E")
483 reg = reg.right_string (reg.length () - 1);
487 Stencil d = fm->find_by_name ("accordion.accDot");
488 d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
493 Stencil d = fm->find_by_name ("accordion.accDot");
494 d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
495 d.translate_axis (0.8 * staff_space PT, X_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);
505 if (reg.left_string (2) == "SS")
507 Stencil d = fm->find_by_name ("accordion.accDot");
508 d.translate_axis (0.5 * staff_space PT, Y_AXIS);
509 d.translate_axis (0.4 * staff_space PT, X_AXIS);
511 d.translate_axis (-0.8 * staff_space PT, X_AXIS);
513 reg = reg.right_string (reg.length () - 2);
515 if (reg.left_string (1) == "S")
517 Stencil d = fm->find_by_name ("accordion.accDot");
518 d.translate_axis (0.5 * staff_space PT, Y_AXIS);
520 reg = reg.right_string (reg.length () - 1);
523 else if (sym == "Freebase")
525 Stencil r = fm->find_by_name ("accordion.accFreebase");
527 if (reg.left_string (1) == "F")
529 Stencil d = fm->find_by_name ("accordion.accDot");
530 d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
532 reg = reg.right_string (reg.length () - 1);
536 Stencil d = fm->find_by_name ("accordion.accDot");
537 d.translate_axis (staff_space * 0.5 PT, Y_AXIS);
541 else if (sym == "Bayanbase")
543 Stencil r = fm->find_by_name ("accordion.accBayanbase");
545 if (reg.left_string (1) == "T")
547 Stencil d = fm->find_by_name ("accordion.accDot");
548 d.translate_axis (staff_space * 2.5 PT, Y_AXIS);
550 reg = reg.right_string (reg.length () - 1);
552 /* include 4' reed just for completeness. You don't want to use this. */
553 if (reg.left_string (1) == "F")
555 Stencil d = fm->find_by_name ("accordion.accDot");
556 d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
558 reg = reg.right_string (reg.length () - 1);
560 if (reg.left_string (2) == "EE")
562 Stencil d = fm->find_by_name ("accordion.accDot");
563 d.translate_axis (staff_space * 0.5 PT, Y_AXIS);
564 d.translate_axis (0.4 * staff_space PT, X_AXIS);
566 d.translate_axis (-0.8 * staff_space PT, X_AXIS);
568 reg = reg.right_string (reg.length () - 2);
570 if (reg.left_string (1) == "E")
572 Stencil d = fm->find_by_name ("accordion.accDot");
573 d.translate_axis (staff_space * 0.5 PT, Y_AXIS);
575 reg = reg.right_string (reg.length () - 1);
578 else if (sym == "Stdbase")
580 Stencil r = fm->find_by_name ("accordion.accStdbase");
582 if (reg.left_string (1) == "T")
584 Stencil d = fm->find_by_name ("accordion.accDot");
585 d.translate_axis (staff_space * 3.5 PT, Y_AXIS);
587 reg = reg.right_string (reg.length () - 1);
589 if (reg.left_string (1) == "F")
591 Stencil d = fm->find_by_name ("accordion.accDot");
592 d.translate_axis (staff_space * 2.5 PT, Y_AXIS);
594 reg = reg.right_string (reg.length () - 1);
596 if (reg.left_string (1) == "M")
598 Stencil d = fm->find_by_name ("accordion.accDot");
599 d.translate_axis (staff_space * 2 PT, Y_AXIS);
600 d.translate_axis (staff_space PT, X_AXIS);
602 reg = reg.right_string (reg.length () - 1);
604 if (reg.left_string (1) == "E")
606 Stencil d = fm->find_by_name ("accordion.accDot");
607 d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
609 reg = reg.right_string (reg.length () - 1);
611 if (reg.left_string (1) == "S")
613 Stencil d = fm->find_by_name ("accordion.accDot");
614 d.translate_axis (staff_space * 0.5 PT, Y_AXIS);
616 reg = reg.right_string (reg.length () - 1);
619 /* ugh maybe try to use regular font for S.B. and B.B and only use one font
621 else if (sym == "SB")
623 Stencil r = fm->find_by_name ("accordion.accSB");
626 else if (sym == "BB")
628 Stencil r = fm->find_by_name ("accordion.accBB");
631 else if (sym == "OldEE")
633 Stencil r = fm->find_by_name ("accordion.accOldEE");
636 else if (sym == "OldEES")
638 Stencil r = fm->find_by_name ("accordion.accOldEES");
645 Lookup::repeat_slash (Real w, Real s, Real t)
647 SCM wid = scm_make_real (w);
648 SCM sl = scm_make_real (s);
649 SCM thick = scm_make_real (t);
650 SCM slashnodot = scm_list_n (ly_symbol2scm ("repeat-slash"),
651 wid, sl, thick, SCM_UNDEFINED);
653 Box b (Interval (0, w + sqrt (sqr (t / s) + sqr (t))),
654 Interval (0, w * s));
656 return Stencil (b, slashnodot); // http://slashnodot.org
660 Lookup::bracket (Axis a, Interval iv, Real thick, Real protude, Real blot)
663 Axis other = Axis ((a + 1)%2);
665 b[other] = Interval (-1, 1) * thick * 0.5;
667 Stencil m = round_filled_box (b, blot);
669 b[a] = Interval (iv[UP] - thick, iv[UP]);
670 Interval oi = Interval (-thick / 2, thick / 2 + fabs (protude));
671 oi *= sign (protude);
673 m.add_stencil (round_filled_box (b, blot));
674 b[a] = Interval (iv[DOWN], iv[DOWN] + thick);
675 m.add_stencil (round_filled_box (b, blot));
681 Lookup::triangle (Interval iv, Real thick, Real protude)
684 b[X_AXIS] = Interval (0, iv.length ());
685 b[Y_AXIS] = Interval (min (0., protude), max (0.0, protude));
687 Offset z1 (iv[LEFT], 0);
688 Offset z2 (iv[RIGHT], 0);
689 Offset z3 ((z1 + z2)[X_AXIS] / 2, protude);
692 TODO: move Triangle to Line_interface ?
694 Stencil tri = Line_interface::make_line (thick, z1, z2);
695 tri.add_stencil (Line_interface::make_line (thick, z2, z3));
696 tri.add_stencil (Line_interface::make_line (thick, z3, z1));