2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1997--2015 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 "international.hh"
31 #include "dimensions.hh"
33 #include "file-path.hh"
35 #include "lily-guile.hh"
38 Lookup::beam (Real slope, Real width, Real thick, Real blot)
44 p = Offset (0, thick / 2);
46 p += Offset (1, -1) * (blot / 2);
50 points = scm_cons (scm_from_double (p[X_AXIS]),
51 scm_cons (scm_from_double (p[Y_AXIS]),
54 p = Offset (0, -thick / 2);
56 p += Offset (1, 1) * (blot / 2);
58 points = scm_cons (scm_from_double (p[X_AXIS]),
59 scm_cons (scm_from_double (p[Y_AXIS]),
62 p = Offset (width, width * slope - thick / 2);
64 p += Offset (-1, 1) * (blot / 2);
66 points = scm_cons (scm_from_double (p[X_AXIS]),
67 scm_cons (scm_from_double (p[Y_AXIS]),
70 p = Offset (width, width * slope + thick / 2);
72 p += Offset (-1, -1) * (blot / 2);
74 points = scm_cons (scm_from_double (p[X_AXIS]),
75 scm_cons (scm_from_double (p[Y_AXIS]),
78 SCM expr = scm_list_4 (ly_symbol2scm ("polygon"),
79 ly_quote_scm (points),
80 scm_from_double (blot),
83 return Stencil (b, expr);
87 Lookup::rotated_box (Real slope, Real width, Real thick, Real blot)
90 Offset rot = Offset (1, slope).direction ();
92 pts.push_back (Offset (0, -thick / 2) * rot);
93 pts.push_back (Offset (width, -thick / 2) * rot);
94 pts.push_back (Offset (width, thick / 2) * rot);
95 pts.push_back (Offset (0, thick / 2) * rot);
96 return Lookup::round_filled_polygon (pts, blot);
100 Lookup::horizontal_line (Interval w, Real th)
102 SCM at = scm_list_n (ly_symbol2scm ("draw-line"),
103 scm_from_double (th),
104 scm_from_double (w[LEFT]),
106 scm_from_double (w[RIGHT]),
112 box[Y_AXIS] = Interval (-th / 2, th / 2);
114 return Stencil (box, at);
118 Lookup::blank (Box b)
120 return Stencil (b, scm_string (SCM_EOL));
124 Lookup::circle (Real rad, Real thick, bool filled)
126 Box b (Interval (-rad, rad), Interval (-rad, rad));
127 return Stencil (b, scm_list_4 (ly_symbol2scm ("circle"),
128 scm_from_double (rad),
129 scm_from_double (thick),
130 scm_from_bool (filled)));
134 Lookup::filled_box (Box b)
136 return round_filled_box (b, 0.0);
142 * __________________________________
147 * |\ _ _ / v \ _ _ /| |
150 * | <------>| | extent
151 * | blot | | (Y_AXIS)
159 * x\_____/______________\_____/|_____v
163 * |<-------------------------->|
164 * Box extent (X_AXIS)
167 Lookup::round_filled_box (Box b, Real blotdiameter)
169 Real width = b.x ().delta ();
170 blotdiameter = min (blotdiameter, width);
171 Real height = b.y ().delta ();
172 blotdiameter = min (blotdiameter, height);
174 if (blotdiameter < 0.0)
176 if (!isinf (blotdiameter))
177 warning (_f ("Not drawing a box with negative dimension, %.2f by %.2f.",
179 return Stencil (b, SCM_EOL);
182 SCM at = (scm_list_n (ly_symbol2scm ("round-filled-box"),
183 scm_from_double (-b[X_AXIS][LEFT]),
184 scm_from_double (b[X_AXIS][RIGHT]),
185 scm_from_double (-b[Y_AXIS][DOWN]),
186 scm_from_double (b[Y_AXIS][UP]),
187 scm_from_double (blotdiameter),
190 return Stencil (b, at);
194 * Create Stencil that represents a filled polygon with round edges.
198 * (a) Only outer (convex) edges are rounded.
200 * (b) This algorithm works as expected only for polygons whose edges
201 * do not intersect. For example, the polygon ((0, 0), (q, 0), (0,
202 * q), (q, q)) has an intersection at point (q/2, q/2) and therefore
203 * will give a strange result. Even non-adjacent edges that just
204 * touch each other will in general not work as expected for non-null
207 * (c) Given a polygon ((x0, y0), (x1, y1), ... , (x (n-1), y (n-1))),
208 * if there is a natural number k such that blotdiameter is greater
209 * than the maximum of { | (x (k mod n), y (k mod n)) - (x ((k+1) mod n),
210 * y ((k+1) mod n)) |, | (x (k mod n), y (k mod n)) - (x ((k+2) mod n),
211 * y ((k+2) mod n)) |, | (x ((k+1) mod n), y ((k+1) mod n)) - (x ((k+2)
212 * mod n), y ((k+2) mod n)) | }, then the outline of the rounded
213 * polygon will exceed the outline of the core polygon. In other
214 * words: Do not draw rounded polygons that have a leg smaller or
215 * thinner than blotdiameter (or set blotdiameter to a sufficiently
216 * small value -- maybe even 0.0)!
218 * NOTE: Limitations (b) and (c) arise from the fact that round edges
219 * are made by moulding sharp edges to round ones rather than adding
220 * to a core filled polygon. For details of these two different
221 * approaches, see the thread upon the ledger lines patch that started
222 * on March 25, 2002 on the devel mailing list. The below version of
223 * round_filled_polygon () sticks to the moulding model, which the
224 * majority of the list participants finally voted for. This,
225 * however, results in the above limitations and a much increased
226 * complexity of the algorithm, since it has to compute a shrinked
227 * polygon -- which is not trivial define precisely and unambigously.
228 * With the other approach, one simply could move a circle of size
229 * blotdiameter along all edges of the polygon (which is what the
230 * postscript routine in the backend effectively does, but on the
231 * shrinked polygon). --jr
233 * An extra parameter "extroversion" has been added since staying just
234 * inside of a polygon will reduce its visual size when tracing a
235 * rounded path. If extroversion is zero, the polygon is just traced
236 * as-is. If it is -1 (the default) the drawing will stay just within
237 * the given polygon. If it is 1, the traced line will stay just
238 * outside of the given polygon.
241 Lookup::round_filled_polygon (vector<Offset> const &points,
245 /* TODO: Maybe print a warning if one of the above limitations
246 applies to the given polygon. However, this is quite complicated
250 const Real epsilon = 0.01;
252 /* remove consecutive duplicate points */
253 for (vsize i = 0; i < points.size (); i++)
255 int next = (i + 1) % points.size ();
256 Real d = (points[i] - points[next]).length ();
258 programming_error ("Polygon should not have duplicate points");
262 /* special cases: degenerated polygons */
263 if (points.size () == 0)
265 if (points.size () == 1)
267 Stencil circ = circle (0.5 * (1.0 + extroversion) * blotdiameter, 0, true);
268 circ.translate (points[0]);
271 if (points.size () == 2)
272 return Line_interface::make_line ((1.0 + extroversion) * blotdiameter, points[0], points[1]);
274 vector<Offset> shrunk_points;
276 if (extroversion == 0.0)
278 shrunk_points = points;
282 /* shrink polygon in size by 0.5 * blotdiameter */
284 // first we need to determine the orientation of the polygon in
285 // order to decide whether shrinking means moving the polygon to the
286 // left or to the right of the outline. We do that by calculating
287 // (double) the oriented area of the polygon. We first determine the
288 // center and do the area calculations relative to it.
289 // Mathematically, the result is not affected by this shift, but
290 // numerically a lot of cancellation is going on and this keeps its
294 for (vsize i = 0; i < points.size (); i++)
296 center /= points.size ();
299 Offset last = points.back () - center;
301 for (vsize i = 0; i < points.size (); i++)
303 Offset here = points[i] - center;
304 area += cross_product (last, here);
308 bool ccw = area >= 0.0; // true if whole shape is counterclockwise oriented
310 shrunk_points.resize (points.size ());
312 for (vsize i = 0; i < points.size (); i++)
315 int i1 = (i + 1) % points.size ();
316 int i2 = (i + 2) % points.size ();
317 Offset p0 = points[i0];
318 Offset p1 = points[i1];
319 Offset p2 = points[i2];
320 Offset p01 = p1 - p0;
321 Offset p12 = p2 - p1;
322 Offset inward0 = Offset(-p01[Y_AXIS], p01[X_AXIS]).direction ();
323 Offset inward2 = Offset(-p12[Y_AXIS], p12[X_AXIS]).direction ();
331 Offset middle = 0.5*(inward0 + inward2);
333 // "middle" now is a vector in the right direction for the
334 // shrinkage. Its size needs to be large enough that the
335 // projection on either of the inward vectors has a size of 1.
337 Real proj = dot_product (middle, inward0);
339 // What's the size of proj? Assuming that we have a corner
340 // angle of phi where 0 corresponds to a continuing line, the
341 // length of middle is 0.5 |(1+cos phi, sin phi)| = cos (phi/2),
342 // so its projection has length
343 // cos^2 (phi/2) = 0.5 + 0.5 cos (phi).
344 // We don't really want to move inwards more than 3 blob
345 // diameters corresponding to 6 blob radii. So
346 // cos (phi/2) = 1/6 gives phi ~ 161, meaning that a 20 degree
347 // corner necessitates moving 3 blob diameters from the corner
348 // in order to stay inside the lines. Ruler and circle agree.
349 // 0.03 is close enough to 1/36. Basically we want to keep the
350 // shape from inverting from pulling too far inward.
351 // 3 diameters is pretty much a handwaving guess.
353 if (abs (proj) < 0.03)
354 proj = proj < 0 ? -0.03 : 0.03;
356 shrunk_points[i1] = p1 - (0.5 * blotdiameter / proj) * middle
361 /* build scm expression and bounding box */
362 SCM shrunk_points_scm = SCM_EOL;
365 for (vsize i = 0; i < shrunk_points.size (); i++)
367 SCM x = scm_from_double (shrunk_points[i][X_AXIS]);
368 SCM y = scm_from_double (shrunk_points[i][Y_AXIS]);
369 shrunk_points_scm = scm_cons (x, scm_cons (y, shrunk_points_scm));
370 box.add_point (points[i]);
371 shrunk_box.add_point (shrunk_points[i]);
373 shrunk_box.widen (0.5*blotdiameter, 0.5*blotdiameter);
374 box.unite (shrunk_box);
375 SCM polygon_scm = scm_list_4 (ly_symbol2scm ("polygon"),
376 ly_quote_scm (shrunk_points_scm),
377 scm_from_double (blotdiameter),
380 Stencil polygon = Stencil (box, polygon_scm);
388 Lookup::frame (Box b, Real thick, Real blot)
391 for (Axis a = X_AXIS; a < NO_AXES; a = Axis (a + 1))
393 Axis o = Axis ((a + 1) % NO_AXES);
394 for (LEFT_and_RIGHT (d))
397 edges[a] = b[a][d] + 0.5 * thick * Interval (-1, 1);
398 edges[o][DOWN] = b[o][DOWN] - thick / 2;
399 edges[o][UP] = b[o][UP] + thick / 2;
401 m.add_stencil (round_filled_box (edges, blot));
408 Make a smooth curve along the points
411 Lookup::slur (Bezier curve, Real curvethick, Real linethick,
414 Stencil return_value;
417 calculate the offset for the two beziers that make the sandwich
420 Offset dir = (curve.control_[3] - curve.control_[0]).direction ();
422 Offset perp = 0.5 * curvethick * Offset (-dir[Y_AXIS], dir[X_AXIS]);
423 back.control_[1] += perp;
424 back.control_[2] += perp;
426 curve.control_[1] -= perp;
427 curve.control_[2] -= perp;
429 if (!scm_is_pair (dash_details))
432 return_value = bezier_sandwich (back, curve, linethick);
436 /* dashed or combination slur */
437 int num_segments = scm_to_int (scm_length (dash_details));
438 for (int i = 0; i < num_segments; i++)
440 SCM dash_pattern = scm_list_ref (dash_details, scm_from_int (i));
441 Real t_min = robust_scm2double (scm_car (dash_pattern), 0);
442 Real t_max = robust_scm2double (scm_cadr (dash_pattern), 1.0);
444 = robust_scm2double (scm_caddr (dash_pattern), 1.0);
446 = robust_scm2double (scm_cadddr (dash_pattern), 0.75);
447 Bezier back_segment = back.extract (t_min, t_max);
448 Bezier curve_segment = curve.extract (t_min, t_max);
449 if (dash_fraction == 1.0)
450 return_value.add_stencil (bezier_sandwich (back_segment,
455 Bezier back_dash, curve_dash;
456 Real seg_length = (back_segment.control_[3]
457 - back_segment.control_[0]).length ();
458 int pattern_count = (int) (seg_length / dash_period);
459 Real pattern_length = 1.0 / (pattern_count + dash_fraction);
461 for (int p = 0; p <= pattern_count; p++)
463 start_t = p * pattern_length;
464 end_t = (p + dash_fraction) * pattern_length;
466 = back_segment.extract (start_t, end_t);
468 = curve_segment.extract (start_t, end_t);
469 return_value.add_stencil (bezier_sandwich (back_dash,
503 Lookup::bezier_sandwich (Bezier top_curve, Bezier bottom_curve, Real thickness)
505 SCM commands = scm_list_n (ly_symbol2scm ("moveto"),
506 scm_from_double (top_curve.control_[0][X_AXIS]),
507 scm_from_double (top_curve.control_[0][Y_AXIS]),
508 ly_symbol2scm ("curveto"),
509 scm_from_double (top_curve.control_[1][X_AXIS]),
510 scm_from_double (top_curve.control_[1][Y_AXIS]),
511 scm_from_double (top_curve.control_[2][X_AXIS]),
512 scm_from_double (top_curve.control_[2][Y_AXIS]),
513 scm_from_double (top_curve.control_[3][X_AXIS]),
514 scm_from_double (top_curve.control_[3][Y_AXIS]),
515 ly_symbol2scm ("lineto"),
516 scm_from_double (bottom_curve.control_[3][X_AXIS]),
517 scm_from_double (bottom_curve.control_[3][Y_AXIS]),
518 ly_symbol2scm ("curveto"),
519 scm_from_double (bottom_curve.control_[2][X_AXIS]),
520 scm_from_double (bottom_curve.control_[2][Y_AXIS]),
521 scm_from_double (bottom_curve.control_[1][X_AXIS]),
522 scm_from_double (bottom_curve.control_[1][Y_AXIS]),
523 scm_from_double (bottom_curve.control_[0][X_AXIS]),
524 scm_from_double (bottom_curve.control_[0][Y_AXIS]),
525 ly_symbol2scm ("closepath"),
528 SCM horizontal_bend = scm_list_n (ly_symbol2scm ("path"),
529 scm_from_double (thickness),
530 ly_quote_scm (commands),
531 ly_quote_scm (ly_symbol2scm ("round")),
532 ly_quote_scm (ly_symbol2scm ("round")),
536 Interval x_extent = top_curve.extent (X_AXIS);
537 x_extent.unite (bottom_curve.extent (X_AXIS));
538 Interval y_extent = top_curve.extent (Y_AXIS);
539 y_extent.unite (bottom_curve.extent (Y_AXIS));
540 Box b (x_extent, y_extent);
542 b.widen (0.5 * thickness, 0.5 * thickness);
543 return Stencil (b, horizontal_bend);
547 Lookup::repeat_slash (Real w, Real s, Real t)
550 Real x_width = hypot (t, t/s);
553 SCM controls = scm_list_n (ly_symbol2scm ("moveto"),
556 ly_symbol2scm ("rlineto"),
557 scm_from_double (x_width),
559 ly_symbol2scm ("rlineto"),
561 scm_from_double (height),
562 ly_symbol2scm ("rlineto"),
563 scm_from_double (-x_width),
565 ly_symbol2scm ("closepath"),
568 SCM slashnodot = scm_list_n (ly_symbol2scm ("path"),
570 ly_quote_scm (controls),
571 ly_quote_scm (ly_symbol2scm ("round")),
572 ly_quote_scm (ly_symbol2scm ("round")),
576 Box b (Interval (0, w + x_width),
577 Interval (0, height));
579 return Stencil (b, slashnodot); // http://slashnodot.org
583 Lookup::bracket (Axis a, Interval iv, Real thick, Real protrude, Real blot)
586 Axis other = other_axis (a);
588 b[other] = Interval (-1, 1) * thick * 0.5;
590 Stencil m = round_filled_box (b, blot);
592 b[a] = Interval (iv[UP] - thick, iv[UP]);
593 Interval oi = Interval (-thick / 2, thick / 2 + fabs (protrude));
594 oi *= sign (protrude);
596 m.add_stencil (round_filled_box (b, blot));
597 b[a] = Interval (iv[DOWN], iv[DOWN] + thick);
598 m.add_stencil (round_filled_box (b, blot));
604 Lookup::triangle (Interval iv, Real thick, Real protrude)
607 b[X_AXIS] = Interval (0, iv.length ());
608 b[Y_AXIS] = Interval (min (0., protrude), max (0.0, protrude));
610 vector<Offset> points;
611 points.push_back (Offset (iv[LEFT], 0));
612 points.push_back (Offset (iv[RIGHT], 0));
613 points.push_back (Offset (iv.center (), protrude));
614 points.push_back (Offset (iv[LEFT], 0)); // close triangle
616 return points_to_line_stencil (thick, points);
621 Lookup::points_to_line_stencil (Real thick, vector<Offset> const &points)
624 for (vsize i = 1; i < points.size (); i++)
626 if (points[i - 1].is_sane () && points[i].is_sane ())
629 = Line_interface::make_line (thick, points[i - 1], points[i]);
630 ret.add_stencil (line);