2 lookup.cc -- implement simple Lookup methods.
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
8 Jan Nieuwenhuizen <janneke@gnu.org>
17 #include "line-interface.hh"
19 #include "dimensions.hh"
21 #include "string-convert.hh"
22 #include "file-path.hh"
24 #include "lily-guile.hh"
25 #include "font-metric.hh"
28 Lookup::dot (Offset p, Real radius)
30 SCM at = (scm_list_n (ly_symbol2scm ("dot"),
31 scm_from_double (p[X_AXIS]),
32 scm_from_double (p[Y_AXIS]),
33 scm_from_double (radius),
36 box.add_point (p - Offset (radius, radius));
37 box.add_point (p + Offset (radius, radius));
38 return Stencil (box, at);
42 Lookup::beam (Real slope, Real width, Real thick, Real blot)
48 p = Offset (0, thick / 2);
50 p += Offset (1, -1) * (blot / 2);
54 points = scm_cons (scm_from_double (p[X_AXIS]),
55 scm_cons (scm_from_double (p[Y_AXIS]),
58 p = Offset (0, -thick / 2);
60 p += Offset (1, 1) * (blot / 2);
62 points = scm_cons (scm_from_double (p[X_AXIS]),
63 scm_cons (scm_from_double (p[Y_AXIS]),
66 p = Offset (width, width * slope - thick / 2);
68 p += Offset (-1, 1) * (blot / 2);
70 points = scm_cons (scm_from_double (p[X_AXIS]),
71 scm_cons (scm_from_double (p[Y_AXIS]),
74 p = Offset (width, width * slope + thick / 2);
76 p += Offset (-1, -1) * (blot / 2);
78 points = scm_cons (scm_from_double (p[X_AXIS]),
79 scm_cons (scm_from_double (p[Y_AXIS]),
82 SCM expr = scm_list_n (ly_symbol2scm ("polygon"),
83 ly_quote_scm (points),
84 scm_from_double (blot),
88 return Stencil (b, expr);
92 Lookup::dashed_slur (Bezier b, Real thick, Real dash_period, Real dash_fraction)
96 Real on = dash_fraction * dash_period;
97 Real off = dash_period - on;
100 l = scm_cons (ly_offset2scm (b.control_[i]), l);
102 SCM at = (scm_list_n (ly_symbol2scm ("dashed-slur"),
103 scm_from_double (thick),
104 scm_from_double (on),
105 scm_from_double (off),
109 Box box (b.extent (X_AXIS), b.extent (Y_AXIS));
110 return Stencil (box, at);
114 Lookup::rotated_box (Real slope, Real width, Real thick, Real blot)
117 Offset rot (1, slope);
121 rot /= sqrt (1 + slope*slope);
122 pts.push_back (Offset (0, -thick / 2) * rot);
123 pts.push_back (Offset (width, -thick / 2) * rot);
124 pts.push_back (Offset (width, thick / 2) * rot);
125 pts.push_back (Offset (0, thick / 2) * rot);
126 return Lookup::round_filled_polygon (pts, blot);
130 Lookup::horizontal_line (Interval w, Real th)
132 SCM at = scm_list_n (ly_symbol2scm ("draw-line"),
133 scm_from_double (th),
134 scm_from_double (w[LEFT]),
136 scm_from_double (w[RIGHT]),
142 box[Y_AXIS] = Interval (-th / 2, th / 2);
144 return Stencil (box, at);
148 Lookup::blank (Box b)
150 return Stencil (b, scm_from_locale_string (""));
154 Lookup::filled_box (Box b)
156 return round_filled_box (b, 0.0);
162 * __________________________________
167 * |\ _ _ / v \ _ _ /| |
170 * | <------>| | extent
171 * | blot | | (Y_AXIS)
179 * x\_____/______________\_____/|_____v
183 * |<-------------------------->|
184 * Box extent (X_AXIS)
187 Lookup::round_filled_box (Box b, Real blotdiameter)
189 if (b.x ().length () < blotdiameter)
190 blotdiameter = b.x ().length ();
191 if (b.y ().length () < blotdiameter)
192 blotdiameter = b.y ().length ();
194 SCM at = (scm_list_n (ly_symbol2scm ("round-filled-box"),
195 scm_from_double (-b[X_AXIS][LEFT]),
196 scm_from_double (b[X_AXIS][RIGHT]),
197 scm_from_double (-b[Y_AXIS][DOWN]),
198 scm_from_double (b[Y_AXIS][UP]),
199 scm_from_double (blotdiameter),
202 return Stencil (b, at);
206 * Create Stencil that represents a filled polygon with round edges.
210 * (a) Only outer (convex) edges are rounded.
212 * (b) This algorithm works as expected only for polygons whose edges
213 * do not intersect. For example, the polygon ((0, 0), (q, 0), (0,
214 * q), (q, q)) has an intersection at point (q/2, q/2) and therefore
215 * will give a strange result. Even non-adjacent edges that just
216 * touch each other will in general not work as expected for non-null
219 * (c) Given a polygon ((x0, y0), (x1, y1), ... , (x (n-1), y (n-1))),
220 * if there is a natural number k such that blotdiameter is greater
221 * than the maximum of { | (x (k mod n), y (k mod n)) - (x ((k+1) mod n),
222 * y ((k+1) mod n)) |, | (x (k mod n), y (k mod n)) - (x ((k+2) mod n),
223 * y ((k+2) mod n)) |, | (x ((k+1) mod n), y ((k+1) mod n)) - (x ((k+2)
224 * mod n), y ((k+2) mod n)) | }, then the outline of the rounded
225 * polygon will exceed the outline of the core polygon. In other
226 * words: Do not draw rounded polygons that have a leg smaller or
227 * thinner than blotdiameter (or set blotdiameter to a sufficiently
228 * small value -- maybe even 0.0)!
230 * NOTE: Limitations (b) and (c) arise from the fact that round edges
231 * are made by moulding sharp edges to round ones rather than adding
232 * to a core filled polygon. For details of these two different
233 * approaches, see the thread upon the ledger lines patch that started
234 * on March 25, 2002 on the devel mailing list. The below version of
235 * round_filled_polygon () sticks to the moulding model, which the
236 * majority of the list participants finally voted for. This,
237 * however, results in the above limitations and a much increased
238 * complexity of the algorithm, since it has to compute a shrinked
239 * polygon -- which is not trivial define precisely and unambigously.
240 * With the other approach, one simply could move a circle of size
241 * blotdiameter along all edges of the polygon (which is what the
242 * postscript routine in the backend effectively does, but on the
243 * shrinked polygon). --jr
246 Lookup::round_filled_polygon (vector<Offset> const &points,
249 /* TODO: Maybe print a warning if one of the above limitations
250 applies to the given polygon. However, this is quite complicated
253 const Real epsilon = 0.01;
256 /* remove consecutive duplicate points */
257 for (vsize i = 0; i < points.size (); i++)
259 int next = (i + 1) % points.size ();
260 Real d = (points[i] - points[next]).length ();
262 programming_error ("Polygon should not have duplicate points");
266 /* special cases: degenerated polygons */
267 if (points.size () == 0)
269 if (points.size () == 1)
270 return dot (points[0], 0.5 * blotdiameter);
271 if (points.size () == 2)
272 return Line_interface::make_line (blotdiameter, points[0], points[1]);
274 /* shrink polygon in size by 0.5 * blotdiameter */
275 vector<Offset> shrunk_points;
276 shrunk_points.resize (points.size ());
277 bool ccw = 1; // true, if three adjacent points are counterclockwise ordered
278 for (vsize i = 0; i < points.size (); i++)
281 int i1 = (i + 1) % points.size ();
282 int i2 = (i + 2) % points.size ();
283 Offset p0 = points[i0];
284 Offset p1 = points[i1];
285 Offset p2 = points[i2];
286 Offset p10 = p0 - p1;
287 Offset p12 = p2 - p1;
288 if (p10.length () != 0.0)
290 Real phi = p10.arg ();
291 // rotate (p2 - p0) by (-phi)
292 Offset q = complex_multiply (p2 - p0, complex_exp (Offset (1.0, -phi)));
296 else if (q[Y_AXIS] < 0)
298 else {} // keep ccw unchanged
300 else {} // keep ccw unchanged
301 Offset p10n = (1.0 / p10.length ()) * p10; // normalize length to 1.0
302 Offset p12n = (1.0 / p12.length ()) * p12;
303 Offset p13n = 0.5 * (p10n + p12n);
304 Offset p14n = 0.5 * (p10n - p12n);
306 Real d = p13n.length () * p14n.length (); // distance p3n to line (p1..p0)
308 // special case: p0, p1, p2 are on a single line => build
309 // vector orthogonal to (p2-p0) of length 0.5 blotdiameter
311 p13[X_AXIS] = p10[Y_AXIS];
312 p13[Y_AXIS] = -p10[X_AXIS];
313 p13 = (0.5 * blotdiameter / p13.length ()) * p13;
316 p13 = (0.5 * blotdiameter / d) * p13n;
317 shrunk_points[i1] = p1 + ((ccw) ? p13 : -p13);
320 /* build scm expression and bounding box */
321 SCM shrunk_points_scm = SCM_EOL;
323 for (vsize i = 0; i < shrunk_points.size (); i++)
325 SCM x = scm_from_double (shrunk_points[i][X_AXIS]);
326 SCM y = scm_from_double (shrunk_points[i][Y_AXIS]);
327 shrunk_points_scm = scm_cons (x, scm_cons (y, shrunk_points_scm));
328 box.add_point (points[i]);
330 SCM polygon_scm = scm_list_n (ly_symbol2scm ("polygon"),
331 ly_quote_scm (shrunk_points_scm),
332 scm_from_double (blotdiameter),
336 Stencil polygon = Stencil (box, polygon_scm);
337 shrunk_points.clear ();
345 Lookup::frame (Box b, Real thick, Real blot)
349 for (Axis a = X_AXIS; a < NO_AXES; a = Axis (a + 1))
351 Axis o = Axis ((a + 1)%NO_AXES);
355 edges[a] = b[a][d] + 0.5 * thick * Interval (-1, 1);
356 edges[o][DOWN] = b[o][DOWN] - thick / 2;
357 edges[o][UP] = b[o][UP] + thick / 2;
359 m.add_stencil (round_filled_box (edges, blot));
361 while (flip (&d) != LEFT);
367 Make a smooth curve along the points
370 Lookup::slur (Bezier curve, Real curvethick, Real linethick)
372 Real alpha = (curve.control_[3] - curve.control_[0]).arg ();
374 Offset perp = curvethick * complex_exp (Offset (0, alpha + M_PI / 2)) * 0.5;
376 back.control_[1] += perp;
377 back.control_[2] += perp;
379 curve.control_[1] -= perp;
380 curve.control_[2] -= perp;
384 for (int i = 0; i < 4; i++)
385 scontrols[i] = ly_offset2scm (back.control_[i]);
386 for (int i = 0; i < 4; i++)
387 scontrols[i + 4] = ly_offset2scm (curve.control_[i]);
390 Need the weird order b.o. the way PS want its arguments
392 int indices[] = {5, 6, 7, 4, 1, 2, 3, 0};
394 for (int i = 8; i--;)
395 list = scm_cons (scontrols[indices[i]], list);
397 SCM at = (scm_list_n (ly_symbol2scm ("bezier-sandwich"),
399 scm_from_double (linethick),
401 Box b (curve.extent (X_AXIS),
402 curve.extent (Y_AXIS));
404 b[X_AXIS].unite (back.extent (X_AXIS));
405 b[Y_AXIS].unite (back.extent (Y_AXIS));
407 b.widen (0.5 * linethick, 0.5 * linethick);
408 return Stencil (b, at);
435 Lookup::bezier_sandwich (Bezier top_curve, Bezier bottom_curve)
438 Need the weird order b.o. the way PS want its arguments
441 list = scm_cons (ly_offset2scm (bottom_curve.control_[3]), list);
442 list = scm_cons (ly_offset2scm (bottom_curve.control_[0]), list);
443 list = scm_cons (ly_offset2scm (bottom_curve.control_[1]), list);
444 list = scm_cons (ly_offset2scm (bottom_curve.control_[2]), list);
445 list = scm_cons (ly_offset2scm (top_curve.control_[0]), list);
446 list = scm_cons (ly_offset2scm (top_curve.control_[3]), list);
447 list = scm_cons (ly_offset2scm (top_curve.control_[2]), list);
448 list = scm_cons (ly_offset2scm (top_curve.control_[1]), list);
450 SCM horizontal_bend = scm_list_n (ly_symbol2scm ("bezier-sandwich"),
452 scm_from_double (0.0),
455 Interval x_extent = top_curve.extent (X_AXIS);
456 x_extent.unite (bottom_curve.extent (X_AXIS));
457 Interval y_extent = top_curve.extent (Y_AXIS);
458 y_extent.unite (bottom_curve.extent (Y_AXIS));
459 Box b (x_extent, y_extent);
461 return Stencil (b, horizontal_bend);
468 Lookup::accordion (SCM s, Real staff_space, Font_metric *fm)
471 string sym = ly_scm2string (scm_car (s));
472 string reg = ly_scm2string (scm_car (scm_cdr (s)));
474 if (sym == "Discant")
476 Stencil r = fm->find_by_name ("accordion.accDiscant");
478 if (reg.substr (0, 1) == "F")
480 Stencil d = fm->find_by_name ("accordion.accDot");
481 d.translate_axis (staff_space * 2.5 PT, Y_AXIS);
483 reg = reg.substr (1);
486 if (reg.substr (0, 3) == "EEE")
489 reg = reg.substr (3);
491 else if (reg.substr (0, 2) == "EE")
494 reg = reg.substr (2);
496 else if (reg.substr (0, 2) == "Eh")
499 reg = reg.substr (2);
501 else if (reg.substr (0, 1) == "E")
504 reg = reg.substr (1);
508 Stencil d = fm->find_by_name ("accordion.accDot");
509 d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
514 Stencil d = fm->find_by_name ("accordion.accDot");
515 d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
516 d.translate_axis (0.8 * staff_space PT, X_AXIS);
521 Stencil d = fm->find_by_name ("accordion.accDot");
522 d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
523 d.translate_axis (-0.8 * staff_space PT, X_AXIS);
526 if (reg.substr (0, 2) == "SS")
528 Stencil d = fm->find_by_name ("accordion.accDot");
529 d.translate_axis (0.5 * staff_space PT, Y_AXIS);
530 d.translate_axis (0.4 * staff_space PT, X_AXIS);
532 d.translate_axis (-0.8 * staff_space PT, X_AXIS);
534 reg = reg.substr (2);
536 if (reg.substr (0, 1) == "S")
538 Stencil d = fm->find_by_name ("accordion.accDot");
539 d.translate_axis (0.5 * staff_space PT, Y_AXIS);
541 reg = reg.substr (1);
544 else if (sym == "Freebase")
546 Stencil r = fm->find_by_name ("accordion.accFreebase");
548 if (reg.substr (0, 1) == "F")
550 Stencil d = fm->find_by_name ("accordion.accDot");
551 d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
553 reg = reg.substr (1);
557 Stencil d = fm->find_by_name ("accordion.accDot");
558 d.translate_axis (staff_space * 0.5 PT, Y_AXIS);
562 else if (sym == "Bayanbase")
564 Stencil r = fm->find_by_name ("accordion.accBayanbase");
566 if (reg.substr (0, 1) == "T")
568 Stencil d = fm->find_by_name ("accordion.accDot");
569 d.translate_axis (staff_space * 2.5 PT, Y_AXIS);
571 reg = reg.substr (1);
573 /* include 4' reed just for completeness. You don't want to use this. */
574 if (reg.substr (0, 1) == "F")
576 Stencil d = fm->find_by_name ("accordion.accDot");
577 d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
579 reg = reg.substr (1);
581 if (reg.substr (0, 2) == "EE")
583 Stencil d = fm->find_by_name ("accordion.accDot");
584 d.translate_axis (staff_space * 0.5 PT, Y_AXIS);
585 d.translate_axis (0.4 * staff_space PT, X_AXIS);
587 d.translate_axis (-0.8 * staff_space PT, X_AXIS);
589 reg = reg.substr (2);
591 if (reg.substr (0, 1) == "E")
593 Stencil d = fm->find_by_name ("accordion.accDot");
594 d.translate_axis (staff_space * 0.5 PT, Y_AXIS);
596 reg = reg.substr (1);
599 else if (sym == "Stdbase")
601 Stencil r = fm->find_by_name ("accordion.accStdbase");
603 if (reg.substr (0, 1) == "T")
605 Stencil d = fm->find_by_name ("accordion.accDot");
606 d.translate_axis (staff_space * 3.5 PT, Y_AXIS);
608 reg = reg.substr (1);
610 if (reg.substr (0, 1) == "F")
612 Stencil d = fm->find_by_name ("accordion.accDot");
613 d.translate_axis (staff_space * 2.5 PT, Y_AXIS);
615 reg = reg.substr (1);
617 if (reg.substr (0, 1) == "M")
619 Stencil d = fm->find_by_name ("accordion.accDot");
620 d.translate_axis (staff_space * 2 PT, Y_AXIS);
621 d.translate_axis (staff_space PT, X_AXIS);
623 reg = reg.substr (1);
625 if (reg.substr (0, 1) == "E")
627 Stencil d = fm->find_by_name ("accordion.accDot");
628 d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
630 reg = reg.substr (1);
632 if (reg.substr (0, 1) == "S")
634 Stencil d = fm->find_by_name ("accordion.accDot");
635 d.translate_axis (staff_space * 0.5 PT, Y_AXIS);
637 reg = reg.substr (1);
640 /* ugh maybe try to use regular font for S.B. and B.B and only use one font
642 else if (sym == "SB")
644 Stencil r = fm->find_by_name ("accordion.accSB");
647 else if (sym == "BB")
649 Stencil r = fm->find_by_name ("accordion.accBB");
652 else if (sym == "OldEE")
654 Stencil r = fm->find_by_name ("accordion.accOldEE");
657 else if (sym == "OldEES")
659 Stencil r = fm->find_by_name ("accordion.accOldEES");
666 Lookup::repeat_slash (Real w, Real s, Real t)
669 vector<Offset> points;
670 Real blotdiameter = 0.0;
673 Offset p2 (w, w * s);
675 return Lookup::round_filled_polygon (points, blotdiameter);
678 SCM wid = scm_from_double (w);
679 SCM sl = scm_from_double (s);
680 SCM thick = scm_from_double (t);
681 SCM slashnodot = scm_list_n (ly_symbol2scm ("repeat-slash"),
682 wid, sl, thick, SCM_UNDEFINED);
684 Box b (Interval (0, w + sqrt (sqr (t / s) + sqr (t))),
685 Interval (0, w * s));
687 return Stencil (b, slashnodot); // http://slashnodot.org
691 Lookup::bracket (Axis a, Interval iv, Real thick, Real protrude, Real blot)
694 Axis other = Axis ((a + 1)%2);
696 b[other] = Interval (-1, 1) * thick * 0.5;
698 Stencil m = round_filled_box (b, blot);
700 b[a] = Interval (iv[UP] - thick, iv[UP]);
701 Interval oi = Interval (-thick / 2, thick / 2 + fabs (protrude));
702 oi *= sign (protrude);
704 m.add_stencil (round_filled_box (b, blot));
705 b[a] = Interval (iv[DOWN], iv[DOWN] + thick);
706 m.add_stencil (round_filled_box (b, blot));
712 Lookup::triangle (Interval iv, Real thick, Real protrude)
715 b[X_AXIS] = Interval (0, iv.length ());
716 b[Y_AXIS] = Interval (min (0., protrude), max (0.0, protrude));
718 vector<Offset> points;
719 points.push_back (Offset (iv[LEFT], 0));
720 points.push_back (Offset (iv[RIGHT], 0));
721 points.push_back (Offset (iv.center (), protrude));
723 return points_to_line_stencil (thick, points);
730 Lookup::points_to_line_stencil (Real thick, vector<Offset> const &points)
733 for (vsize i = 1; i < points.size (); i++)
735 if (points[i-1].is_sane () && points[i].is_sane ())
738 = Line_interface::make_line (thick, points[i-1], points[i]);
739 ret.add_stencil (line);