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/>.
27 #include "line-interface.hh"
29 #include "international.hh"
30 #include "dimensions.hh"
32 #include "file-path.hh"
34 #include "lily-guile.hh"
39 Lookup::beam (Real slope, Real width, Real thick, Real blot)
45 p = Offset (0, thick / 2);
47 p += Offset (1, -1) * (blot / 2);
51 points = scm_cons (scm_from_double (p[X_AXIS]),
52 scm_cons (scm_from_double (p[Y_AXIS]),
55 p = Offset (0, -thick / 2);
57 p += Offset (1, 1) * (blot / 2);
59 points = scm_cons (scm_from_double (p[X_AXIS]),
60 scm_cons (scm_from_double (p[Y_AXIS]),
63 p = Offset (width, width * slope - thick / 2);
65 p += Offset (-1, 1) * (blot / 2);
67 points = scm_cons (scm_from_double (p[X_AXIS]),
68 scm_cons (scm_from_double (p[Y_AXIS]),
71 p = Offset (width, width * slope + thick / 2);
73 p += Offset (-1, -1) * (blot / 2);
75 points = scm_cons (scm_from_double (p[X_AXIS]),
76 scm_cons (scm_from_double (p[Y_AXIS]),
79 SCM expr = scm_list_n (ly_symbol2scm ("polygon"),
80 ly_quote_scm (points),
81 scm_from_double (blot),
85 return Stencil (b, expr);
89 Lookup::rotated_box (Real slope, Real width, Real thick, Real blot)
92 Offset rot = Offset (1, slope).direction ();
94 pts.push_back (Offset (0, -thick / 2) * rot);
95 pts.push_back (Offset (width, -thick / 2) * rot);
96 pts.push_back (Offset (width, thick / 2) * rot);
97 pts.push_back (Offset (0, thick / 2) * rot);
98 return Lookup::round_filled_polygon (pts, blot);
102 Lookup::horizontal_line (Interval w, Real th)
104 SCM at = scm_list_n (ly_symbol2scm ("draw-line"),
105 scm_from_double (th),
106 scm_from_double (w[LEFT]),
108 scm_from_double (w[RIGHT]),
114 box[Y_AXIS] = Interval (-th / 2, th / 2);
116 return Stencil (box, at);
120 Lookup::blank (Box b)
122 return Stencil (b, scm_string (SCM_EOL));
126 Lookup::circle (Real rad, Real thick, bool filled)
128 Box b (Interval (-rad, rad), Interval (-rad, rad));
129 return Stencil (b, scm_list_4 (ly_symbol2scm ("circle"),
130 scm_from_double (rad),
131 scm_from_double (thick),
132 scm_from_bool (filled)));
136 Lookup::filled_box (Box b)
138 return round_filled_box (b, 0.0);
144 * __________________________________
149 * |\ _ _ / v \ _ _ /| |
152 * | <------>| | extent
153 * | blot | | (Y_AXIS)
161 * x\_____/______________\_____/|_____v
165 * |<-------------------------->|
166 * Box extent (X_AXIS)
169 Lookup::round_filled_box (Box b, Real blotdiameter)
171 Real width = b.x ().delta ();
172 blotdiameter = std::min (blotdiameter, width);
173 Real height = b.y ().delta ();
174 blotdiameter = std::min (blotdiameter, height);
176 if (blotdiameter < 0.0)
178 if (!isinf (blotdiameter))
179 warning (_f ("Not drawing a box with negative dimension, %.2f by %.2f.",
181 return Stencil (b, SCM_EOL);
184 SCM at = (scm_list_n (ly_symbol2scm ("round-filled-box"),
185 scm_from_double (-b[X_AXIS][LEFT]),
186 scm_from_double (b[X_AXIS][RIGHT]),
187 scm_from_double (-b[Y_AXIS][DOWN]),
188 scm_from_double (b[Y_AXIS][UP]),
189 scm_from_double (blotdiameter),
192 return Stencil (b, at);
196 * Create Stencil that represents a filled polygon with round edges.
200 * (a) Only outer (convex) edges are rounded.
202 * (b) This algorithm works as expected only for polygons whose edges
203 * do not intersect. For example, the polygon ((0, 0), (q, 0), (0,
204 * q), (q, q)) has an intersection at point (q/2, q/2) and therefore
205 * will give a strange result. Even non-adjacent edges that just
206 * touch each other will in general not work as expected for non-null
209 * (c) Given a polygon ((x0, y0), (x1, y1), ... , (x (n-1), y (n-1))),
210 * if there is a natural number k such that blotdiameter is greater
211 * than the maximum of { | (x (k mod n), y (k mod n)) - (x ((k+1) mod n),
212 * y ((k+1) mod n)) |, | (x (k mod n), y (k mod n)) - (x ((k+2) mod n),
213 * y ((k+2) mod n)) |, | (x ((k+1) mod n), y ((k+1) mod n)) - (x ((k+2)
214 * mod n), y ((k+2) mod n)) | }, then the outline of the rounded
215 * polygon will exceed the outline of the core polygon. In other
216 * words: Do not draw rounded polygons that have a leg smaller or
217 * thinner than blotdiameter (or set blotdiameter to a sufficiently
218 * small value -- maybe even 0.0)!
220 * NOTE: Limitations (b) and (c) arise from the fact that round edges
221 * are made by moulding sharp edges to round ones rather than adding
222 * to a core filled polygon. For details of these two different
223 * approaches, see the thread upon the ledger lines patch that started
224 * on March 25, 2002 on the devel mailing list. The below version of
225 * round_filled_polygon () sticks to the moulding model, which the
226 * majority of the list participants finally voted for. This,
227 * however, results in the above limitations and a much increased
228 * complexity of the algorithm, since it has to compute a shrinked
229 * polygon -- which is not trivial define precisely and unambigously.
230 * With the other approach, one simply could move a circle of size
231 * blotdiameter along all edges of the polygon (which is what the
232 * postscript routine in the backend effectively does, but on the
233 * shrinked polygon). --jr
236 Lookup::round_filled_polygon (vector<Offset> const &points,
239 /* TODO: Maybe print a warning if one of the above limitations
240 applies to the given polygon. However, this is quite complicated
243 const Real epsilon = 0.01;
246 /* remove consecutive duplicate points */
247 for (vsize i = 0; i < points.size (); i++)
249 int next = (i + 1) % points.size ();
250 Real d = (points[i] - points[next]).length ();
252 programming_error ("Polygon should not have duplicate points");
256 /* special cases: degenerated polygons */
257 if (points.size () == 0)
259 if (points.size () == 1)
261 Stencil circ = circle (0.5 * blotdiameter, 0, true);
262 circ.translate (points[0]);
265 if (points.size () == 2)
266 return Line_interface::make_line (blotdiameter, points[0], points[1]);
268 /* shrink polygon in size by 0.5 * blotdiameter */
270 // first we need to determine the orientation of the polygon in
271 // order to decide whether shrinking means moving the polygon to the
272 // left or to the right of the outline. We do that by calculating
273 // (double) the oriented area of the polygon. We first determine the
274 // center and do the area calculations relative to it.
275 // Mathematically, the result is not affected by this shift, but
276 // numerically a lot of cancellation is going on and this keeps its
280 for (vsize i = 0; i < points.size (); i++)
282 center /= points.size ();
285 Offset last = points.back () - center;
287 for (vsize i = 0; i < points.size (); i++)
289 Offset here = points[i] - center;
290 area += cross_product (last, here);
294 bool ccw = area >= 0.0; // true if whole shape is counterclockwise oriented
296 vector<Offset> shrunk_points;
297 shrunk_points.resize (points.size ());
299 for (vsize i = 0; i < points.size (); i++)
302 int i1 = (i + 1) % points.size ();
303 int i2 = (i + 2) % points.size ();
304 Offset p0 = points[i0];
305 Offset p1 = points[i1];
306 Offset p2 = points[i2];
307 Offset p01 = p1 - p0;
308 Offset p12 = p2 - p1;
309 Offset inward0 = Offset(-p01[Y_AXIS], p01[X_AXIS]).direction ();
310 Offset inward2 = Offset(-p12[Y_AXIS], p12[X_AXIS]).direction ();
318 Offset middle = 0.5*(inward0 + inward2);
320 // "middle" now is a vector in the right direction for the
321 // shrinkage. Its size needs to be large enough that the
322 // projection on either of the inward vectors has a size of 1.
324 Real proj = dot_product (middle, inward0);
326 // What's the size of proj? Assuming that we have a corner
327 // angle of phi where 0 corresponds to a continuing line, the
328 // length of middle is 0.5 |(1+cos phi, sin phi)| = cos (phi/2),
329 // so its projection has length
330 // cos^2 (phi/2) = 0.5 + 0.5 cos (phi).
331 // We don't really want to move inwards more than 3 blob
332 // diameters corresponding to 6 blob radii. So
333 // cos (phi/2) = 1/6 gives phi ~ 161, meaning that a 20 degree
334 // corner necessitates moving 3 blob diameters from the corner
335 // in order to stay inside the lines. Ruler and circle agree.
336 // 0.03 is close enough to 1/36. Basically we want to keep the
337 // shape from inverting from pulling too far inward.
338 // 3 diameters is pretty much a handwaving guess.
340 if (abs (proj) < 0.03)
341 proj = proj < 0 ? -0.03 : 0.03;
343 shrunk_points[i1] = p1 + (0.5 * blotdiameter / proj) * middle;
346 /* build scm expression and bounding box */
347 SCM shrunk_points_scm = SCM_EOL;
349 for (vsize i = 0; i < shrunk_points.size (); i++)
351 SCM x = scm_from_double (shrunk_points[i][X_AXIS]);
352 SCM y = scm_from_double (shrunk_points[i][Y_AXIS]);
353 shrunk_points_scm = scm_cons (x, scm_cons (y, shrunk_points_scm));
354 box.add_point (points[i]);
356 SCM polygon_scm = scm_list_n (ly_symbol2scm ("polygon"),
357 ly_quote_scm (shrunk_points_scm),
358 scm_from_double (blotdiameter),
362 Stencil polygon = Stencil (box, polygon_scm);
363 shrunk_points.clear ();
371 Lookup::frame (Box b, Real thick, Real blot)
374 for (Axis a = X_AXIS; a < NO_AXES; a = Axis (a + 1))
376 Axis o = Axis ((a + 1) % NO_AXES);
377 for (LEFT_and_RIGHT (d))
380 edges[a] = b[a][d] + 0.5 * thick * Interval (-1, 1);
381 edges[o][DOWN] = b[o][DOWN] - thick / 2;
382 edges[o][UP] = b[o][UP] + thick / 2;
384 m.add_stencil (round_filled_box (edges, blot));
391 Make a smooth curve along the points
394 Lookup::slur (Bezier curve, Real curvethick, Real linethick,
397 Stencil return_value;
400 calculate the offset for the two beziers that make the sandwich
403 Real alpha = (curve.control_[3] - curve.control_[0]).arg ();
405 Offset perp = curvethick * complex_exp (Offset (0, alpha + M_PI / 2)) * 0.5;
406 back.control_[1] += perp;
407 back.control_[2] += perp;
409 curve.control_[1] -= perp;
410 curve.control_[2] -= perp;
412 if (!scm_is_pair (dash_details))
415 return_value = bezier_sandwich (back, curve, linethick);
419 /* dashed or combination slur */
420 int num_segments = scm_to_int (scm_length (dash_details));
421 for (int i = 0; i < num_segments; i++)
423 SCM dash_pattern = scm_list_ref (dash_details, scm_from_int (i));
424 Real t_min = robust_scm2double (scm_car (dash_pattern), 0);
425 Real t_max = robust_scm2double (scm_cadr (dash_pattern), 1.0);
427 = robust_scm2double (scm_caddr (dash_pattern), 1.0);
429 = robust_scm2double (scm_cadddr (dash_pattern), 0.75);
430 Bezier back_segment = back.extract (t_min, t_max);
431 Bezier curve_segment = curve.extract (t_min, t_max);
432 if (dash_fraction == 1.0)
433 return_value.add_stencil (bezier_sandwich (back_segment,
438 Bezier back_dash, curve_dash;
439 Real seg_length = (back_segment.control_[3]
440 - back_segment.control_[0]).length ();
441 int pattern_count = (int) (seg_length / dash_period);
442 Real pattern_length = 1.0 / (pattern_count + dash_fraction);
444 for (int p = 0; p <= pattern_count; p++)
446 start_t = p * pattern_length;
447 end_t = (p + dash_fraction) * pattern_length;
449 = back_segment.extract (start_t, end_t);
451 = curve_segment.extract (start_t, end_t);
452 return_value.add_stencil (bezier_sandwich (back_dash,
486 Lookup::bezier_sandwich (Bezier top_curve, Bezier bottom_curve, Real thickness)
488 SCM commands = scm_list_n (ly_symbol2scm ("moveto"),
489 scm_from_double (top_curve.control_[0][X_AXIS]),
490 scm_from_double (top_curve.control_[0][Y_AXIS]),
491 ly_symbol2scm ("curveto"),
492 scm_from_double (top_curve.control_[1][X_AXIS]),
493 scm_from_double (top_curve.control_[1][Y_AXIS]),
494 scm_from_double (top_curve.control_[2][X_AXIS]),
495 scm_from_double (top_curve.control_[2][Y_AXIS]),
496 scm_from_double (top_curve.control_[3][X_AXIS]),
497 scm_from_double (top_curve.control_[3][Y_AXIS]),
498 ly_symbol2scm ("lineto"),
499 scm_from_double (bottom_curve.control_[3][X_AXIS]),
500 scm_from_double (bottom_curve.control_[3][Y_AXIS]),
501 ly_symbol2scm ("curveto"),
502 scm_from_double (bottom_curve.control_[2][X_AXIS]),
503 scm_from_double (bottom_curve.control_[2][Y_AXIS]),
504 scm_from_double (bottom_curve.control_[1][X_AXIS]),
505 scm_from_double (bottom_curve.control_[1][Y_AXIS]),
506 scm_from_double (bottom_curve.control_[0][X_AXIS]),
507 scm_from_double (bottom_curve.control_[0][Y_AXIS]),
508 ly_symbol2scm ("closepath"),
511 SCM horizontal_bend = scm_list_n (ly_symbol2scm ("path"),
512 scm_from_double (thickness),
513 ly_quote_scm (commands),
514 ly_quote_scm (ly_symbol2scm ("round")),
515 ly_quote_scm (ly_symbol2scm ("round")),
519 Interval x_extent = top_curve.extent (X_AXIS);
520 x_extent.unite (bottom_curve.extent (X_AXIS));
521 Interval y_extent = top_curve.extent (Y_AXIS);
522 y_extent.unite (bottom_curve.extent (Y_AXIS));
523 Box b (x_extent, y_extent);
525 b.widen (0.5 * thickness, 0.5 * thickness);
526 return Stencil (b, horizontal_bend);
530 Lookup::repeat_slash (Real w, Real s, Real t)
533 Real x_width = sqrt ((t * t) + ((t / s) * (t / s)));
536 SCM controls = scm_list_n (ly_symbol2scm ("moveto"),
539 ly_symbol2scm ("rlineto"),
540 scm_from_double (x_width),
542 ly_symbol2scm ("rlineto"),
544 scm_from_double (height),
545 ly_symbol2scm ("rlineto"),
546 scm_from_double (-x_width),
548 ly_symbol2scm ("closepath"),
551 SCM slashnodot = scm_list_n (ly_symbol2scm ("path"),
553 ly_quote_scm (controls),
554 ly_quote_scm (ly_symbol2scm ("round")),
555 ly_quote_scm (ly_symbol2scm ("round")),
559 Box b (Interval (0, w + sqrt (sqr (t / s) + sqr (t))),
560 Interval (0, w * s));
562 return Stencil (b, slashnodot); // http://slashnodot.org
566 Lookup::bracket (Axis a, Interval iv, Real thick, Real protrude, Real blot)
569 Axis other = Axis ((a + 1) % 2);
571 b[other] = Interval (-1, 1) * thick * 0.5;
573 Stencil m = round_filled_box (b, blot);
575 b[a] = Interval (iv[UP] - thick, iv[UP]);
576 Interval oi = Interval (-thick / 2, thick / 2 + fabs (protrude));
577 oi *= sign (protrude);
579 m.add_stencil (round_filled_box (b, blot));
580 b[a] = Interval (iv[DOWN], iv[DOWN] + thick);
581 m.add_stencil (round_filled_box (b, blot));
587 Lookup::triangle (Interval iv, Real thick, Real protrude)
590 b[X_AXIS] = Interval (0, iv.length ());
591 b[Y_AXIS] = Interval (std::min (0., protrude), std::max (0.0, protrude));
593 vector<Offset> points;
594 points.push_back (Offset (iv[LEFT], 0));
595 points.push_back (Offset (iv[RIGHT], 0));
596 points.push_back (Offset (iv.center (), protrude));
597 points.push_back (Offset (iv[LEFT], 0)); // close triangle
599 return points_to_line_stencil (thick, points);
604 Lookup::points_to_line_stencil (Real thick, vector<Offset> const &points)
607 for (vsize i = 1; i < points.size (); i++)
609 if (points[i - 1].is_sane () && points[i].is_sane ())
612 = Line_interface::make_line (thick, points[i - 1], points[i]);
613 ret.add_stencil (line);