2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1997--2012 Han-Wen Nienhuys <hanwen@xs4all.nl>
6 Jan Nieuwenhuizen <janneke@gnu.org>
8 LilyPond is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 LilyPond is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
28 #include "line-interface.hh"
30 #include "dimensions.hh"
32 #include "file-path.hh"
34 #include "lily-guile.hh"
37 Lookup::beam (Real slope, Real width, Real thick, Real blot)
43 p = Offset (0, thick / 2);
45 p += Offset (1, -1) * (blot / 2);
49 points = scm_cons (scm_from_double (p[X_AXIS]),
50 scm_cons (scm_from_double (p[Y_AXIS]),
53 p = Offset (0, -thick / 2);
55 p += Offset (1, 1) * (blot / 2);
57 points = scm_cons (scm_from_double (p[X_AXIS]),
58 scm_cons (scm_from_double (p[Y_AXIS]),
61 p = Offset (width, width * slope - thick / 2);
63 p += Offset (-1, 1) * (blot / 2);
65 points = scm_cons (scm_from_double (p[X_AXIS]),
66 scm_cons (scm_from_double (p[Y_AXIS]),
69 p = Offset (width, width * slope + thick / 2);
71 p += Offset (-1, -1) * (blot / 2);
73 points = scm_cons (scm_from_double (p[X_AXIS]),
74 scm_cons (scm_from_double (p[Y_AXIS]),
77 SCM expr = scm_list_n (ly_symbol2scm ("polygon"),
78 ly_quote_scm (points),
79 scm_from_double (blot),
83 return Stencil (b, expr);
87 Lookup::rotated_box (Real slope, Real width, Real thick, Real blot)
90 Offset rot (1, slope);
94 rot /= sqrt (1 + slope * slope);
95 pts.push_back (Offset (0, -thick / 2) * rot);
96 pts.push_back (Offset (width, -thick / 2) * rot);
97 pts.push_back (Offset (width, thick / 2) * rot);
98 pts.push_back (Offset (0, thick / 2) * rot);
99 return Lookup::round_filled_polygon (pts, blot);
103 Lookup::horizontal_line (Interval w, Real th)
105 SCM at = scm_list_n (ly_symbol2scm ("draw-line"),
106 scm_from_double (th),
107 scm_from_double (w[LEFT]),
109 scm_from_double (w[RIGHT]),
115 box[Y_AXIS] = Interval (-th / 2, th / 2);
117 return Stencil (box, at);
121 Lookup::blank (Box b)
123 return Stencil (b, scm_from_locale_string (""));
127 Lookup::circle (Real rad, Real thick, bool filled)
129 Box b (Interval (-rad, rad), Interval (-rad, rad));
130 return Stencil (b, scm_list_4 (ly_symbol2scm ("circle"),
131 scm_from_double (rad),
132 scm_from_double (thick),
133 scm_from_bool (filled)));
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 (vector<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 (vsize i = 0; i < points.size (); i++)
242 int next = (i + 1) % points.size ();
243 Real d = (points[i] - points[next]).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)
254 Stencil circ = circle (0.5 * blotdiameter, 0, true);
255 circ.translate (points[0]);
258 if (points.size () == 2)
259 return Line_interface::make_line (blotdiameter, points[0], points[1]);
261 /* shrink polygon in size by 0.5 * blotdiameter */
262 vector<Offset> shrunk_points;
263 shrunk_points.resize (points.size ());
264 bool ccw = 1; // true, if three adjacent points are counterclockwise ordered
265 for (vsize i = 0; i < points.size (); i++)
268 int i1 = (i + 1) % points.size ();
269 int i2 = (i + 2) % points.size ();
270 Offset p0 = points[i0];
271 Offset p1 = points[i1];
272 Offset p2 = points[i2];
273 Offset p10 = p0 - p1;
274 Offset p12 = p2 - p1;
275 if (p10.length () != 0.0)
278 Real phi = p10.arg ();
279 // rotate (p2 - p0) by (-phi)
280 Offset q = complex_multiply (p2 - p0, complex_exp (Offset (1.0, -phi)));
284 else if (q[Y_AXIS] < 0)
286 else {} // keep ccw unchanged
288 else {} // keep ccw unchanged
289 Offset p10n = (1.0 / p10.length ()) * p10; // normalize length to 1.0
290 Offset p12n = (1.0 / p12.length ()) * p12;
291 Offset p13n = 0.5 * (p10n + p12n);
292 Offset p14n = 0.5 * (p10n - p12n);
294 Real d = p13n.length () * p14n.length (); // distance p3n to line (p1..p0)
296 // special case: p0, p1, p2 are on a single line => build
297 // vector orthogonal to (p2-p0) of length 0.5 blotdiameter
299 p13[X_AXIS] = p10[Y_AXIS];
300 p13[Y_AXIS] = -p10[X_AXIS];
301 p13 = (0.5 * blotdiameter / p13.length ()) * p13;
304 p13 = (0.5 * blotdiameter / d) * p13n;
305 shrunk_points[i1] = p1 + ((ccw) ? p13 : -p13);
308 /* build scm expression and bounding box */
309 SCM shrunk_points_scm = SCM_EOL;
311 for (vsize i = 0; i < shrunk_points.size (); i++)
313 SCM x = scm_from_double (shrunk_points[i][X_AXIS]);
314 SCM y = scm_from_double (shrunk_points[i][Y_AXIS]);
315 shrunk_points_scm = scm_cons (x, scm_cons (y, shrunk_points_scm));
316 box.add_point (points[i]);
318 SCM polygon_scm = scm_list_n (ly_symbol2scm ("polygon"),
319 ly_quote_scm (shrunk_points_scm),
320 scm_from_double (blotdiameter),
324 Stencil polygon = Stencil (box, polygon_scm);
325 shrunk_points.clear ();
333 Lookup::frame (Box b, Real thick, Real blot)
336 for (Axis a = X_AXIS; a < NO_AXES; a = Axis (a + 1))
338 Axis o = Axis ((a + 1) % NO_AXES);
339 for (LEFT_and_RIGHT (d))
342 edges[a] = b[a][d] + 0.5 * thick * Interval (-1, 1);
343 edges[o][DOWN] = b[o][DOWN] - thick / 2;
344 edges[o][UP] = b[o][UP] + thick / 2;
346 m.add_stencil (round_filled_box (edges, blot));
353 Make a smooth curve along the points
356 Lookup::slur (Bezier curve, Real curvethick, Real linethick,
359 Stencil return_value;
362 calculate the offset for the two beziers that make the sandwich
365 Real alpha = (curve.control_[3] - curve.control_[0]).arg ();
367 Offset perp = curvethick * complex_exp (Offset (0, alpha + M_PI / 2)) * 0.5;
368 back.control_[1] += perp;
369 back.control_[2] += perp;
371 curve.control_[1] -= perp;
372 curve.control_[2] -= perp;
374 if (!scm_is_pair (dash_details))
377 return_value = bezier_sandwich (back, curve, linethick);
381 /* dashed or combination slur */
382 int num_segments = scm_to_int (scm_length (dash_details));
383 for (int i = 0; i < num_segments; i++)
385 SCM dash_pattern = scm_list_ref (dash_details, scm_from_int (i));
386 Real t_min = robust_scm2double (scm_car (dash_pattern), 0);
387 Real t_max = robust_scm2double (scm_cadr (dash_pattern), 1.0);
389 = robust_scm2double (scm_caddr (dash_pattern), 1.0);
391 = robust_scm2double (scm_cadddr (dash_pattern), 0.75);
392 Bezier back_segment = back.extract (t_min, t_max);
393 Bezier curve_segment = curve.extract (t_min, t_max);
394 if (dash_fraction == 1.0)
395 return_value.add_stencil (bezier_sandwich (back_segment,
400 Bezier back_dash, curve_dash;
401 Real seg_length = (back_segment.control_[3]
402 - back_segment.control_[0]).length ();
403 int pattern_count = (int) (seg_length / dash_period);
404 Real pattern_length = 1.0 / (pattern_count + dash_fraction);
406 for (int p = 0; p <= pattern_count; p++)
408 start_t = p * pattern_length;
409 end_t = (p + dash_fraction) * pattern_length;
411 = back_segment.extract (start_t, end_t);
413 = curve_segment.extract (start_t, end_t);
414 return_value.add_stencil (bezier_sandwich (back_dash,
448 Lookup::bezier_sandwich (Bezier top_curve, Bezier bottom_curve, Real thickness)
450 SCM commands = scm_list_n (ly_symbol2scm ("moveto"),
451 scm_from_double (top_curve.control_[0][X_AXIS]),
452 scm_from_double (top_curve.control_[0][Y_AXIS]),
453 ly_symbol2scm ("curveto"),
454 scm_from_double (top_curve.control_[1][X_AXIS]),
455 scm_from_double (top_curve.control_[1][Y_AXIS]),
456 scm_from_double (top_curve.control_[2][X_AXIS]),
457 scm_from_double (top_curve.control_[2][Y_AXIS]),
458 scm_from_double (top_curve.control_[3][X_AXIS]),
459 scm_from_double (top_curve.control_[3][Y_AXIS]),
460 ly_symbol2scm ("curveto"),
461 scm_from_double (bottom_curve.control_[2][X_AXIS]),
462 scm_from_double (bottom_curve.control_[2][Y_AXIS]),
463 scm_from_double (bottom_curve.control_[1][X_AXIS]),
464 scm_from_double (bottom_curve.control_[1][Y_AXIS]),
465 scm_from_double (bottom_curve.control_[0][X_AXIS]),
466 scm_from_double (bottom_curve.control_[0][Y_AXIS]),
467 ly_symbol2scm ("closepath"),
470 SCM horizontal_bend = scm_list_n (ly_symbol2scm ("path"),
471 scm_from_double (thickness),
472 ly_quote_scm (commands),
473 ly_quote_scm (ly_symbol2scm ("round")),
474 ly_quote_scm (ly_symbol2scm ("round")),
478 Interval x_extent = top_curve.extent (X_AXIS);
479 x_extent.unite (bottom_curve.extent (X_AXIS));
480 Interval y_extent = top_curve.extent (Y_AXIS);
481 y_extent.unite (bottom_curve.extent (Y_AXIS));
482 Box b (x_extent, y_extent);
484 b.widen (0.5 * thickness, 0.5 * thickness);
485 return Stencil (b, horizontal_bend);
489 Lookup::repeat_slash (Real w, Real s, Real t)
492 Real x_width = sqrt ((t * t) + ((t / s) * (t / s)));
495 SCM controls = scm_list_n (ly_symbol2scm ("moveto"),
498 ly_symbol2scm ("rlineto"),
499 scm_from_double (x_width),
501 ly_symbol2scm ("rlineto"),
503 scm_from_double (height),
504 ly_symbol2scm ("rlineto"),
505 scm_from_double (-x_width),
507 ly_symbol2scm ("closepath"),
510 SCM slashnodot = scm_list_n (ly_symbol2scm ("path"),
512 ly_quote_scm (controls),
513 ly_quote_scm (ly_symbol2scm ("round")),
514 ly_quote_scm (ly_symbol2scm ("round")),
518 Box b (Interval (0, w + sqrt (sqr (t / s) + sqr (t))),
519 Interval (0, w * s));
521 return Stencil (b, slashnodot); // http://slashnodot.org
525 Lookup::bracket (Axis a, Interval iv, Real thick, Real protrude, Real blot)
528 Axis other = Axis ((a + 1) % 2);
530 b[other] = Interval (-1, 1) * thick * 0.5;
532 Stencil m = round_filled_box (b, blot);
534 b[a] = Interval (iv[UP] - thick, iv[UP]);
535 Interval oi = Interval (-thick / 2, thick / 2 + fabs (protrude));
536 oi *= sign (protrude);
538 m.add_stencil (round_filled_box (b, blot));
539 b[a] = Interval (iv[DOWN], iv[DOWN] + thick);
540 m.add_stencil (round_filled_box (b, blot));
546 Lookup::triangle (Interval iv, Real thick, Real protrude)
549 b[X_AXIS] = Interval (0, iv.length ());
550 b[Y_AXIS] = Interval (min (0., protrude), max (0.0, protrude));
552 vector<Offset> points;
553 points.push_back (Offset (iv[LEFT], 0));
554 points.push_back (Offset (iv[RIGHT], 0));
555 points.push_back (Offset (iv.center (), protrude));
556 points.push_back (Offset (iv[LEFT], 0)); // close triangle
558 return points_to_line_stencil (thick, points);
563 Lookup::points_to_line_stencil (Real thick, vector<Offset> const &points)
566 for (vsize i = 1; i < points.size (); i++)
568 if (points[i - 1].is_sane () && points[i].is_sane ())
571 = Line_interface::make_line (thick, points[i - 1], points[i]);
572 ret.add_stencil (line);