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_from_double (p[X_AXIS]),
31 scm_from_double (p[Y_AXIS]),
32 scm_from_double (radius),
35 box.add_point (p - Offset (radius, radius));
36 box.add_point (p + Offset (radius, radius));
37 return Stencil (box, at);
41 Lookup::beam (Real slope, Real width, Real thick, Real blot)
47 p = Offset (0, thick / 2);
49 p += Offset (1, -1) * (blot / 2);
53 points = scm_cons (scm_from_double (p[X_AXIS]),
54 scm_cons (scm_from_double (p[Y_AXIS]),
57 p = Offset (0, -thick / 2);
59 p += Offset (1, 1) * (blot / 2);
61 points = scm_cons (scm_from_double (p[X_AXIS]),
62 scm_cons (scm_from_double (p[Y_AXIS]),
65 p = Offset (width, width * slope - thick / 2);
67 p += Offset (-1, 1) * (blot / 2);
69 points = scm_cons (scm_from_double (p[X_AXIS]),
70 scm_cons (scm_from_double (p[Y_AXIS]),
73 p = Offset (width, width * slope + thick / 2);
75 p += Offset (-1, -1) * (blot / 2);
77 points = scm_cons (scm_from_double (p[X_AXIS]),
78 scm_cons (scm_from_double (p[Y_AXIS]),
81 SCM expr = scm_list_n (ly_symbol2scm ("polygon"),
82 ly_quote_scm (points),
83 scm_from_double (blot),
87 return Stencil (b, expr);
91 Lookup::dashed_slur (Bezier b, Real thick, Real dash_period, Real dash_fraction)
95 Real on = dash_fraction * dash_period;
96 Real off = dash_period - on;
99 l = scm_cons (ly_offset2scm (b.control_[i]), l);
101 SCM at = (scm_list_n (ly_symbol2scm ("dashed-slur"),
102 scm_from_double (thick),
103 scm_from_double (on),
104 scm_from_double (off),
108 Box box (Interval (0, 0), Interval (0, 0));
109 return Stencil (box, at);
113 Lookup::horizontal_line (Interval w, Real th)
115 SCM at = scm_list_n (ly_symbol2scm ("draw-line"),
116 scm_from_double (th),
117 scm_from_double (w[LEFT]),
119 scm_from_double (w[RIGHT]),
125 box[Y_AXIS] = Interval (-th / 2, th / 2);
127 return Stencil (box, at);
131 Lookup::blank (Box b)
133 return Stencil (b, scm_makfrom0str (""));
137 Lookup::filled_box (Box b)
139 return round_filled_box (b, 0.0);
145 * __________________________________
150 * |\ _ _ / v \ _ _ /| |
153 * | <------>| | extent
154 * | blot | | (Y_AXIS)
162 * x\_____/______________\_____/|_____v
166 * |<-------------------------->|
167 * Box extent (X_AXIS)
170 Lookup::round_filled_box (Box b, Real blotdiameter)
172 if (b.x ().length () < blotdiameter)
173 blotdiameter = b.x ().length ();
174 if (b.y ().length () < blotdiameter)
175 blotdiameter = b.y ().length ();
177 SCM at = (scm_list_n (ly_symbol2scm ("round-filled-box"),
178 scm_from_double (-b[X_AXIS][LEFT]),
179 scm_from_double (b[X_AXIS][RIGHT]),
180 scm_from_double (-b[Y_AXIS][DOWN]),
181 scm_from_double (b[Y_AXIS][UP]),
182 scm_from_double (blotdiameter),
185 return Stencil (b, at);
189 * Create Stencil that represents a filled polygon with round edges.
193 * (a) Only outer (convex) edges are rounded.
195 * (b) This algorithm works as expected only for polygons whose edges
196 * do not intersect. For example, the polygon ((0, 0), (q, 0), (0,
197 * q), (q, q)) has an intersection at point (q/2, q/2) and therefore
198 * will give a strange result. Even non-adjacent edges that just
199 * touch each other will in general not work as expected for non-null
202 * (c) Given a polygon ((x0, y0), (x1, y1), ... , (x (n-1), y (n-1))),
203 * if there is a natural number k such that blotdiameter is greater
204 * than the maximum of { | (x (k mod n), y (k mod n)) - (x ((k+1) mod n),
205 * y ((k+1) mod n)) |, | (x (k mod n), y (k mod n)) - (x ((k+2) mod n),
206 * y ((k+2) mod n)) |, | (x ((k+1) mod n), y ((k+1) mod n)) - (x ((k+2)
207 * mod n), y ((k+2) mod n)) | }, then the outline of the rounded
208 * polygon will exceed the outline of the core polygon. In other
209 * words: Do not draw rounded polygons that have a leg smaller or
210 * thinner than blotdiameter (or set blotdiameter to a sufficiently
211 * small value -- maybe even 0.0)!
213 * NOTE: Limitations (b) and (c) arise from the fact that round edges
214 * are made by moulding sharp edges to round ones rather than adding
215 * to a core filled polygon. For details of these two different
216 * approaches, see the thread upon the ledger lines patch that started
217 * on March 25, 2002 on the devel mailing list. The below version of
218 * round_filled_polygon () sticks to the moulding model, which the
219 * majority of the list participants finally voted for. This,
220 * however, results in the above limitations and a much increased
221 * complexity of the algorithm, since it has to compute a shrinked
222 * polygon -- which is not trivial define precisely and unambigously.
223 * With the other approach, one simply could move a circle of size
224 * blotdiameter along all edges of the polygon (which is what the
225 * postscript routine in the backend effectively does, but on the
226 * shrinked polygon). --jr
229 Lookup::round_filled_polygon (Array<Offset> const &points,
232 /* TODO: Maybe print a warning if one of the above limitations
233 applies to the given polygon. However, this is quite complicated
236 const Real epsilon = 0.01;
239 /* remove consecutive duplicate points */
240 for (int i = 0; i < points.size (); i++)
242 int next_i = (i + 1) % points.size ();
243 Real d = (points[i] - points[next_i]).length ();
245 programming_error ("Polygon should not have duplicate points");
249 /* special cases: degenerated polygons */
250 if (points.size () == 0)
252 if (points.size () == 1)
253 return dot (points[0], 0.5 * blotdiameter);
254 if (points.size () == 2)
255 return Line_interface::make_line (blotdiameter, points[0], points[1]);
257 /* shrink polygon in size by 0.5 * blotdiameter */
258 Array<Offset> shrunk_points;
259 shrunk_points.set_size (points.size ());
260 bool ccw = 1; // true, if three adjacent points are counterclockwise ordered
261 for (int i = 0; i < points.size (); i++)
264 int i1 = (i + 1) % points.size ();
265 int i2 = (i + 2) % points.size ();
266 Offset p0 = points[i0];
267 Offset p1 = points[i1];
268 Offset p2 = points[i2];
269 Offset p10 = p0 - p1;
270 Offset p12 = p2 - p1;
271 if (p10.length () != 0.0)
273 Real phi = p10.arg ();
274 // rotate (p2 - p0) by (-phi)
275 Offset q = complex_multiply (p2 - p0, complex_exp (Offset (1.0, -phi)));
279 else if (q[Y_AXIS] < 0)
281 else {} // keep ccw unchanged
283 else {} // keep ccw unchanged
284 Offset p10n = (1.0 / p10.length ()) * p10; // normalize length to 1.0
285 Offset p12n = (1.0 / p12.length ()) * p12;
286 Offset p13n = 0.5 * (p10n + p12n);
287 Offset p14n = 0.5 * (p10n - p12n);
289 Real d = p13n.length () * p14n.length (); // distance p3n to line (p1..p0)
291 // special case: p0, p1, p2 are on a single line => build
292 // vector orthogonal to (p2-p0) of length 0.5 blotdiameter
294 p13[X_AXIS] = p10[Y_AXIS];
295 p13[Y_AXIS] = -p10[X_AXIS];
296 p13 = (0.5 * blotdiameter / p13.length ()) * p13;
299 p13 = (0.5 * blotdiameter / d) * p13n;
300 shrunk_points[i1] = p1 + ((ccw) ? p13 : -p13);
303 /* build scm expression and bounding box */
304 SCM shrunk_points_scm = SCM_EOL;
306 for (int i = 0; i < shrunk_points.size (); i++)
308 SCM x = scm_from_double (shrunk_points[i][X_AXIS]);
309 SCM y = scm_from_double (shrunk_points[i][Y_AXIS]);
310 shrunk_points_scm = scm_cons (x, scm_cons (y, shrunk_points_scm));
311 box.add_point (points[i]);
313 SCM polygon_scm = scm_list_n (ly_symbol2scm ("polygon"),
314 ly_quote_scm (shrunk_points_scm),
315 scm_from_double (blotdiameter),
319 Stencil polygon = Stencil (box, polygon_scm);
320 shrunk_points.clear ();
328 Lookup::frame (Box b, Real thick, Real blot)
332 for (Axis a = X_AXIS; a < NO_AXES; a = Axis (a + 1))
334 Axis o = Axis ((a + 1)%NO_AXES);
338 edges[a] = b[a][d] + 0.5 * thick * Interval (-1, 1);
339 edges[o][DOWN] = b[o][DOWN] - thick / 2;
340 edges[o][UP] = b[o][UP] + thick / 2;
342 m.add_stencil (round_filled_box (edges, blot));
344 while (flip (&d) != LEFT);
350 Make a smooth curve along the points
353 Lookup::slur (Bezier curve, Real curvethick, Real linethick)
355 Real alpha = (curve.control_[3] - curve.control_[0]).arg ();
357 Offset perp = curvethick * complex_exp (Offset (0, alpha + M_PI / 2)) * 0.5;
359 back.control_[1] += perp;
360 back.control_[2] += perp;
362 curve.control_[1] -= perp;
363 curve.control_[2] -= perp;
367 for (int i = 4; i--;)
368 scontrols[ i ] = ly_offset2scm (back.control_[i]);
369 for (int i = 4; i--;)
370 scontrols[i + 4] = ly_offset2scm (curve.control_[i]);
373 Need the weird order b.o. the way PS want its arguments
375 int indices[] = {5, 6, 7, 4, 1, 2, 3, 0};
377 for (int i = 8; i--;)
378 list = scm_cons (scontrols[indices[i]], list);
380 SCM at = (scm_list_n (ly_symbol2scm ("bezier-sandwich"),
382 scm_from_double (linethick),
384 Box b (curve.extent (X_AXIS),
385 curve.extent (Y_AXIS));
387 b[X_AXIS].unite (back.extent (X_AXIS));
388 b[Y_AXIS].unite (back.extent (Y_AXIS));
390 return Stencil (b, at);
417 Lookup::bezier_sandwich (Bezier top_curve, Bezier bottom_curve)
420 Need the weird order b.o. the way PS want its arguments
423 list = scm_cons (ly_offset2scm (bottom_curve.control_[3]), list);
424 list = scm_cons (ly_offset2scm (bottom_curve.control_[0]), list);
425 list = scm_cons (ly_offset2scm (bottom_curve.control_[1]), list);
426 list = scm_cons (ly_offset2scm (bottom_curve.control_[2]), list);
427 list = scm_cons (ly_offset2scm (top_curve.control_[0]), list);
428 list = scm_cons (ly_offset2scm (top_curve.control_[3]), list);
429 list = scm_cons (ly_offset2scm (top_curve.control_[2]), list);
430 list = scm_cons (ly_offset2scm (top_curve.control_[1]), list);
432 SCM horizontal_bend = scm_list_n (ly_symbol2scm ("bezier-sandwich"),
434 scm_from_double (0.0),
437 Interval x_extent = top_curve.extent (X_AXIS);
438 x_extent.unite (bottom_curve.extent (X_AXIS));
439 Interval y_extent = top_curve.extent (Y_AXIS);
440 y_extent.unite (bottom_curve.extent (Y_AXIS));
441 Box b (x_extent, y_extent);
443 return Stencil (b, horizontal_bend);
450 Lookup::accordion (SCM s, Real staff_space, Font_metric *fm)
453 String sym = ly_scm2string (scm_car (s));
454 String reg = ly_scm2string (scm_car (scm_cdr (s)));
456 if (sym == "Discant")
458 Stencil r = fm->find_by_name ("accordion.accDiscant");
460 if (reg.left_string (1) == "F")
462 Stencil d = fm->find_by_name ("accordion.accDot");
463 d.translate_axis (staff_space * 2.5 PT, Y_AXIS);
465 reg = reg.right_string (reg.length () - 1);
468 if (reg.left_string (3) == "EEE")
471 reg = reg.right_string (reg.length () - 3);
473 else if (reg.left_string (2) == "EE")
476 reg = reg.right_string (reg.length () - 2);
478 else if (reg.left_string (2) == "Eh")
481 reg = reg.right_string (reg.length () - 2);
483 else if (reg.left_string (1) == "E")
486 reg = reg.right_string (reg.length () - 1);
490 Stencil d = fm->find_by_name ("accordion.accDot");
491 d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
496 Stencil d = fm->find_by_name ("accordion.accDot");
497 d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
498 d.translate_axis (0.8 * staff_space PT, X_AXIS);
503 Stencil d = fm->find_by_name ("accordion.accDot");
504 d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
505 d.translate_axis (-0.8 * staff_space PT, X_AXIS);
508 if (reg.left_string (2) == "SS")
510 Stencil d = fm->find_by_name ("accordion.accDot");
511 d.translate_axis (0.5 * staff_space PT, Y_AXIS);
512 d.translate_axis (0.4 * staff_space PT, X_AXIS);
514 d.translate_axis (-0.8 * staff_space PT, X_AXIS);
516 reg = reg.right_string (reg.length () - 2);
518 if (reg.left_string (1) == "S")
520 Stencil d = fm->find_by_name ("accordion.accDot");
521 d.translate_axis (0.5 * staff_space PT, Y_AXIS);
523 reg = reg.right_string (reg.length () - 1);
526 else if (sym == "Freebase")
528 Stencil r = fm->find_by_name ("accordion.accFreebase");
530 if (reg.left_string (1) == "F")
532 Stencil d = fm->find_by_name ("accordion.accDot");
533 d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
535 reg = reg.right_string (reg.length () - 1);
539 Stencil d = fm->find_by_name ("accordion.accDot");
540 d.translate_axis (staff_space * 0.5 PT, Y_AXIS);
544 else if (sym == "Bayanbase")
546 Stencil r = fm->find_by_name ("accordion.accBayanbase");
548 if (reg.left_string (1) == "T")
550 Stencil d = fm->find_by_name ("accordion.accDot");
551 d.translate_axis (staff_space * 2.5 PT, Y_AXIS);
553 reg = reg.right_string (reg.length () - 1);
555 /* include 4' reed just for completeness. You don't want to use this. */
556 if (reg.left_string (1) == "F")
558 Stencil d = fm->find_by_name ("accordion.accDot");
559 d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
561 reg = reg.right_string (reg.length () - 1);
563 if (reg.left_string (2) == "EE")
565 Stencil d = fm->find_by_name ("accordion.accDot");
566 d.translate_axis (staff_space * 0.5 PT, Y_AXIS);
567 d.translate_axis (0.4 * staff_space PT, X_AXIS);
569 d.translate_axis (-0.8 * staff_space PT, X_AXIS);
571 reg = reg.right_string (reg.length () - 2);
573 if (reg.left_string (1) == "E")
575 Stencil d = fm->find_by_name ("accordion.accDot");
576 d.translate_axis (staff_space * 0.5 PT, Y_AXIS);
578 reg = reg.right_string (reg.length () - 1);
581 else if (sym == "Stdbase")
583 Stencil r = fm->find_by_name ("accordion.accStdbase");
585 if (reg.left_string (1) == "T")
587 Stencil d = fm->find_by_name ("accordion.accDot");
588 d.translate_axis (staff_space * 3.5 PT, Y_AXIS);
590 reg = reg.right_string (reg.length () - 1);
592 if (reg.left_string (1) == "F")
594 Stencil d = fm->find_by_name ("accordion.accDot");
595 d.translate_axis (staff_space * 2.5 PT, Y_AXIS);
597 reg = reg.right_string (reg.length () - 1);
599 if (reg.left_string (1) == "M")
601 Stencil d = fm->find_by_name ("accordion.accDot");
602 d.translate_axis (staff_space * 2 PT, Y_AXIS);
603 d.translate_axis (staff_space PT, X_AXIS);
605 reg = reg.right_string (reg.length () - 1);
607 if (reg.left_string (1) == "E")
609 Stencil d = fm->find_by_name ("accordion.accDot");
610 d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
612 reg = reg.right_string (reg.length () - 1);
614 if (reg.left_string (1) == "S")
616 Stencil d = fm->find_by_name ("accordion.accDot");
617 d.translate_axis (staff_space * 0.5 PT, Y_AXIS);
619 reg = reg.right_string (reg.length () - 1);
622 /* ugh maybe try to use regular font for S.B. and B.B and only use one font
624 else if (sym == "SB")
626 Stencil r = fm->find_by_name ("accordion.accSB");
629 else if (sym == "BB")
631 Stencil r = fm->find_by_name ("accordion.accBB");
634 else if (sym == "OldEE")
636 Stencil r = fm->find_by_name ("accordion.accOldEE");
639 else if (sym == "OldEES")
641 Stencil r = fm->find_by_name ("accordion.accOldEES");
648 Lookup::repeat_slash (Real w, Real s, Real t)
651 Array<Offset> points;
652 Real blotdiameter = 0.0;
655 Offset p2 (w, w * s);
657 return Lookup::round_filled_polygon (points, blotdiameter);
660 SCM wid = scm_from_double (w);
661 SCM sl = scm_from_double (s);
662 SCM thick = scm_from_double (t);
663 SCM slashnodot = scm_list_n (ly_symbol2scm ("repeat-slash"),
664 wid, sl, thick, SCM_UNDEFINED);
666 Box b (Interval (0, w + sqrt (sqr (t / s) + sqr (t))),
667 Interval (0, w * s));
669 return Stencil (b, slashnodot); // http://slashnodot.org
673 Lookup::bracket (Axis a, Interval iv, Real thick, Real protude, Real blot)
676 Axis other = Axis ((a + 1)%2);
678 b[other] = Interval (-1, 1) * thick * 0.5;
680 Stencil m = round_filled_box (b, blot);
682 b[a] = Interval (iv[UP] - thick, iv[UP]);
683 Interval oi = Interval (-thick / 2, thick / 2 + fabs (protude));
684 oi *= sign (protude);
686 m.add_stencil (round_filled_box (b, blot));
687 b[a] = Interval (iv[DOWN], iv[DOWN] + thick);
688 m.add_stencil (round_filled_box (b, blot));
694 Lookup::triangle (Interval iv, Real thick, Real protude)
697 b[X_AXIS] = Interval (0, iv.length ());
698 b[Y_AXIS] = Interval (min (0., protude), max (0.0, protude));
700 Offset z1 (iv[LEFT], 0);
701 Offset z2 (iv[RIGHT], 0);
702 Offset z3 ((z1 + z2)[X_AXIS] / 2, protude);
705 TODO: move Triangle to Line_interface ?
707 Stencil tri = Line_interface::make_line (thick, z1, z2);
708 tri.add_stencil (Line_interface::make_line (thick, z2, z3));
709 tri.add_stencil (Line_interface::make_line (thick, z3, z1));