]> git.donarmstrong.com Git - lilypond.git/blob - lily/lookup.cc
Run grand-replace (issue 3765)
[lilypond.git] / lily / lookup.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2014 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
6   Jan Nieuwenhuizen <janneke@gnu.org>
7
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.
12
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.
17
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/>.
20 */
21
22 #include "lookup.hh"
23
24 #include <cmath>
25 #include <cctype>
26 using namespace std;
27
28 #include "line-interface.hh"
29 #include "warn.hh"
30 #include "international.hh"
31 #include "dimensions.hh"
32 #include "bezier.hh"
33 #include "file-path.hh"
34 #include "main.hh"
35 #include "lily-guile.hh"
36
37 Stencil
38 Lookup::beam (Real slope, Real width, Real thick, Real blot)
39 {
40   Box b;
41
42   Offset p;
43
44   p = Offset (0, thick / 2);
45   b.add_point (p);
46   p += Offset (1, -1) * (blot / 2);
47
48   SCM points = SCM_EOL;
49
50   points = scm_cons (scm_from_double (p[X_AXIS]),
51                      scm_cons (scm_from_double (p[Y_AXIS]),
52                                points));
53
54   p = Offset (0, -thick / 2);
55   b.add_point (p);
56   p += Offset (1, 1) * (blot / 2);
57
58   points = scm_cons (scm_from_double (p[X_AXIS]),
59                      scm_cons (scm_from_double (p[Y_AXIS]),
60                                points));
61
62   p = Offset (width, width * slope - thick / 2);
63   b.add_point (p);
64   p += Offset (-1, 1) * (blot / 2);
65
66   points = scm_cons (scm_from_double (p[X_AXIS]),
67                      scm_cons (scm_from_double (p[Y_AXIS]),
68                                points));
69
70   p = Offset (width, width * slope + thick / 2);
71   b.add_point (p);
72   p += Offset (-1, -1) * (blot / 2);
73
74   points = scm_cons (scm_from_double (p[X_AXIS]),
75                      scm_cons (scm_from_double (p[Y_AXIS]),
76                                points));
77
78   SCM expr = scm_list_n (ly_symbol2scm ("polygon"),
79                          ly_quote_scm (points),
80                          scm_from_double (blot),
81                          SCM_BOOL_T,
82                          SCM_UNDEFINED);
83
84   return Stencil (b, expr);
85 }
86
87 Stencil
88 Lookup::rotated_box (Real slope, Real width, Real thick, Real blot)
89 {
90   vector<Offset> pts;
91   Offset rot (1, slope);
92
93   thick -= 2 * blot;
94   width -= 2 * blot;
95   rot /= sqrt (1 + slope * slope);
96   pts.push_back (Offset (0, -thick / 2) * rot);
97   pts.push_back (Offset (width, -thick / 2) * rot);
98   pts.push_back (Offset (width, thick / 2) * rot);
99   pts.push_back (Offset (0, thick / 2) * rot);
100   return Lookup::round_filled_polygon (pts, blot);
101 }
102
103 Stencil
104 Lookup::horizontal_line (Interval w, Real th)
105 {
106   SCM at = scm_list_n (ly_symbol2scm ("draw-line"),
107                        scm_from_double (th),
108                        scm_from_double (w[LEFT]),
109                        scm_from_double (0),
110                        scm_from_double (w[RIGHT]),
111                        scm_from_double (0),
112                        SCM_UNDEFINED);
113
114   Box box;
115   box[X_AXIS] = w;
116   box[Y_AXIS] = Interval (-th / 2, th / 2);
117
118   return Stencil (box, at);
119 }
120
121 Stencil
122 Lookup::blank (Box b)
123 {
124   return Stencil (b, scm_from_locale_string (""));
125 }
126
127 Stencil
128 Lookup::circle (Real rad, Real thick, bool filled)
129 {
130   Box b (Interval (-rad, rad), Interval (-rad, rad));
131   return Stencil (b, scm_list_4 (ly_symbol2scm ("circle"),
132                                  scm_from_double (rad),
133                                  scm_from_double (thick),
134                                  scm_from_bool (filled)));
135 }
136
137 Stencil
138 Lookup::filled_box (Box b)
139 {
140   return round_filled_box (b, 0.0);
141 }
142
143 /*
144  * round filled box:
145  *
146  *   __________________________________
147  *  /     \  ^           /     \      ^
148  * |         |blot              |     |
149  * |       | |dia       |       |     |
150  * |         |meter             |     |
151  * |\ _ _ /  v           \ _ _ /|     |
152  * |                            |     |
153  * |                            |     | Box
154  * |                    <------>|     | extent
155  * |                      blot  |     | (Y_AXIS)
156  * |                    diameter|     |
157  * |                            |     |
158  * |  _ _                  _ _  |     |
159  * |/     \              /     \|     |
160  * |                            |     |
161  * |       |            |       |     |
162  * |                            |     |
163  * x\_____/______________\_____/|_____v
164  * |(0, 0)                       |
165  * |                            |
166  * |                            |
167  * |<-------------------------->|
168  *       Box extent (X_AXIS)
169  */
170 Stencil
171 Lookup::round_filled_box (Box b, Real blotdiameter)
172 {
173   Real width = b.x ().delta ();
174   blotdiameter = min (blotdiameter, width);
175   Real height = b.y ().delta ();
176   blotdiameter = min (blotdiameter, height);
177
178   if (blotdiameter < 0.0)
179     {
180       if (!isinf (blotdiameter))
181         warning (_f ("Not drawing a box with negative dimension, %.2f by %.2f.",
182                      width, height));
183       return Stencil (b, SCM_EOL);
184     }
185
186   SCM at = (scm_list_n (ly_symbol2scm ("round-filled-box"),
187                         scm_from_double (-b[X_AXIS][LEFT]),
188                         scm_from_double (b[X_AXIS][RIGHT]),
189                         scm_from_double (-b[Y_AXIS][DOWN]),
190                         scm_from_double (b[Y_AXIS][UP]),
191                         scm_from_double (blotdiameter),
192                         SCM_UNDEFINED));
193
194   return Stencil (b, at);
195 }
196
197 /*
198  * Create Stencil that represents a filled polygon with round edges.
199  *
200  * LIMITATIONS:
201  *
202  * (a) Only outer (convex) edges are rounded.
203  *
204  * (b) This algorithm works as expected only for polygons whose edges
205  * do not intersect.  For example, the polygon ((0, 0), (q, 0), (0,
206  * q), (q, q)) has an intersection at point (q/2, q/2) and therefore
207  * will give a strange result.  Even non-adjacent edges that just
208  * touch each other will in general not work as expected for non-null
209  * blotdiameter.
210  *
211  * (c) Given a polygon ((x0, y0), (x1, y1), ... , (x (n-1), y (n-1))),
212  * if there is a natural number k such that blotdiameter is greater
213  * than the maximum of { | (x (k mod n), y (k mod n)) - (x ((k+1) mod n),
214  * y ((k+1) mod n)) |, | (x (k mod n), y (k mod n)) - (x ((k+2) mod n),
215  * y ((k+2) mod n)) |, | (x ((k+1) mod n), y ((k+1) mod n)) - (x ((k+2)
216  * mod n), y ((k+2) mod n)) | }, then the outline of the rounded
217  * polygon will exceed the outline of the core polygon.  In other
218  * words: Do not draw rounded polygons that have a leg smaller or
219  * thinner than blotdiameter (or set blotdiameter to a sufficiently
220  * small value -- maybe even 0.0)!
221  *
222  * NOTE: Limitations (b) and (c) arise from the fact that round edges
223  * are made by moulding sharp edges to round ones rather than adding
224  * to a core filled polygon.  For details of these two different
225  * approaches, see the thread upon the ledger lines patch that started
226  * on March 25, 2002 on the devel mailing list.  The below version of
227  * round_filled_polygon () sticks to the moulding model, which the
228  * majority of the list participants finally voted for.  This,
229  * however, results in the above limitations and a much increased
230  * complexity of the algorithm, since it has to compute a shrinked
231  * polygon -- which is not trivial define precisely and unambigously.
232  * With the other approach, one simply could move a circle of size
233  * blotdiameter along all edges of the polygon (which is what the
234  * postscript routine in the backend effectively does, but on the
235  * shrinked polygon). --jr
236  */
237 Stencil
238 Lookup::round_filled_polygon (vector<Offset> const &points,
239                               Real blotdiameter)
240 {
241   /* TODO: Maybe print a warning if one of the above limitations
242      applies to the given polygon.  However, this is quite complicated
243      to check. */
244
245   const Real epsilon = 0.01;
246
247 #ifndef NDEBUG
248   /* remove consecutive duplicate points */
249   for (vsize i = 0; i < points.size (); i++)
250     {
251       int next = (i + 1) % points.size ();
252       Real d = (points[i] - points[next]).length ();
253       if (d < epsilon)
254         programming_error ("Polygon should not have duplicate points");
255     }
256 #endif
257
258   /* special cases: degenerated polygons */
259   if (points.size () == 0)
260     return Stencil ();
261   if (points.size () == 1)
262     {
263       Stencil circ = circle (0.5 * blotdiameter, 0, true);
264       circ.translate (points[0]);
265       return circ;
266     }
267   if (points.size () == 2)
268     return Line_interface::make_line (blotdiameter, points[0], points[1]);
269
270   /* shrink polygon in size by 0.5 * blotdiameter */
271   vector<Offset> shrunk_points;
272   shrunk_points.resize (points.size ());
273   bool ccw = 1; // true, if three adjacent points are counterclockwise ordered
274   for (vsize i = 0; i < points.size (); i++)
275     {
276       int i0 = i;
277       int i1 = (i + 1) % points.size ();
278       int i2 = (i + 2) % points.size ();
279       Offset p0 = points[i0];
280       Offset p1 = points[i1];
281       Offset p2 = points[i2];
282       Offset p10 = p0 - p1;
283       Offset p12 = p2 - p1;
284       if (p10.length () != 0.0)
285         {
286           // recompute ccw
287           Real phi = p10.arg ();
288           // rotate (p2 - p0) by (-phi)
289           Offset q = complex_multiply (p2 - p0, complex_exp (Offset (1.0, -phi)));
290
291           if (q[Y_AXIS] > 0)
292             ccw = 1;
293           else if (q[Y_AXIS] < 0)
294             ccw = 0;
295           else {} // keep ccw unchanged
296         }
297       else {} // keep ccw unchanged
298       Offset p10n = (1.0 / p10.length ()) * p10; // normalize length to 1.0
299       Offset p12n = (1.0 / p12.length ()) * p12;
300       Offset p13n = 0.5 * (p10n + p12n);
301       Offset p14n = 0.5 * (p10n - p12n);
302       Offset p13;
303       Real d = p13n.length () * p14n.length (); // distance p3n to line (p1..p0)
304       if (d < epsilon)
305         // special case: p0, p1, p2 are on a single line => build
306         // vector orthogonal to (p2-p0) of length 0.5 blotdiameter
307         {
308           p13[X_AXIS] = p10[Y_AXIS];
309           p13[Y_AXIS] = -p10[X_AXIS];
310           p13 = (0.5 * blotdiameter / p13.length ()) * p13;
311         }
312       else
313         p13 = (0.5 * blotdiameter / d) * p13n;
314       shrunk_points[i1] = p1 + ((ccw) ? p13 : -p13);
315     }
316
317   /* build scm expression and bounding box */
318   SCM shrunk_points_scm = SCM_EOL;
319   Box box;
320   for (vsize i = 0; i < shrunk_points.size (); i++)
321     {
322       SCM x = scm_from_double (shrunk_points[i][X_AXIS]);
323       SCM y = scm_from_double (shrunk_points[i][Y_AXIS]);
324       shrunk_points_scm = scm_cons (x, scm_cons (y, shrunk_points_scm));
325       box.add_point (points[i]);
326     }
327   SCM polygon_scm = scm_list_n (ly_symbol2scm ("polygon"),
328                                 ly_quote_scm (shrunk_points_scm),
329                                 scm_from_double (blotdiameter),
330                                 SCM_BOOL_T,
331                                 SCM_UNDEFINED);
332
333   Stencil polygon = Stencil (box, polygon_scm);
334   shrunk_points.clear ();
335   return polygon;
336 }
337
338 /*
339   TODO: deprecate?
340 */
341 Stencil
342 Lookup::frame (Box b, Real thick, Real blot)
343 {
344   Stencil m;
345   for (Axis a = X_AXIS; a < NO_AXES; a = Axis (a + 1))
346     {
347       Axis o = Axis ((a + 1) % NO_AXES);
348       for (LEFT_and_RIGHT (d))
349         {
350           Box edges;
351           edges[a] = b[a][d] + 0.5 * thick * Interval (-1, 1);
352           edges[o][DOWN] = b[o][DOWN] - thick / 2;
353           edges[o][UP] = b[o][UP] + thick / 2;
354
355           m.add_stencil (round_filled_box (edges, blot));
356         }
357     }
358   return m;
359 }
360
361 /*
362   Make a smooth curve along the points
363 */
364 Stencil
365 Lookup::slur (Bezier curve, Real curvethick, Real linethick,
366               SCM dash_details)
367 {
368   Stencil return_value;
369
370   /*
371       calculate the offset for the two beziers that make the sandwich
372       for the slur
373   */
374   Real alpha = (curve.control_[3] - curve.control_[0]).arg ();
375   Bezier back = curve;
376   Offset perp = curvethick * complex_exp (Offset (0, alpha + M_PI / 2)) * 0.5;
377   back.control_[1] += perp;
378   back.control_[2] += perp;
379
380   curve.control_[1] -= perp;
381   curve.control_[2] -= perp;
382
383   if (!scm_is_pair (dash_details))
384     {
385       /* solid slur  */
386       return_value = bezier_sandwich (back, curve, linethick);
387     }
388   else
389     {
390       /* dashed or combination slur */
391       int num_segments = scm_to_int (scm_length (dash_details));
392       for (int i = 0; i < num_segments; i++)
393         {
394           SCM dash_pattern = scm_list_ref (dash_details, scm_from_int (i));
395           Real t_min = robust_scm2double (scm_car (dash_pattern), 0);
396           Real t_max = robust_scm2double (scm_cadr (dash_pattern), 1.0);
397           Real dash_fraction
398             = robust_scm2double (scm_caddr (dash_pattern), 1.0);
399           Real dash_period
400             = robust_scm2double (scm_cadddr (dash_pattern), 0.75);
401           Bezier back_segment = back.extract (t_min, t_max);
402           Bezier curve_segment = curve.extract (t_min, t_max);
403           if (dash_fraction == 1.0)
404             return_value.add_stencil (bezier_sandwich (back_segment,
405                                                        curve_segment,
406                                                        linethick));
407           else
408             {
409               Bezier back_dash, curve_dash;
410               Real seg_length = (back_segment.control_[3]
411                                  - back_segment.control_[0]).length ();
412               int pattern_count = (int) (seg_length / dash_period);
413               Real pattern_length = 1.0 / (pattern_count + dash_fraction);
414               Real start_t, end_t;
415               for (int p = 0; p <= pattern_count; p++)
416                 {
417                   start_t = p * pattern_length;
418                   end_t = (p + dash_fraction) * pattern_length;
419                   back_dash
420                     = back_segment.extract (start_t, end_t);
421                   curve_dash
422                     = curve_segment.extract (start_t, end_t);
423                   return_value.add_stencil (bezier_sandwich (back_dash,
424                                                              curve_dash,
425                                                              linethick));
426                 }
427             }
428         }
429     }
430   return return_value;
431 }
432
433 /*
434  * Bezier Sandwich:
435  *
436  *                               .|
437  *                        .       |
438  *              top .             |
439  *              . curve           |
440  *          .                     |
441  *       .                        |
442  *     .                          |
443  *    |                           |
444  *    |                          .|
445  *    |                     .
446  *    |         bottom .
447  *    |            . curve
448  *    |         .
449  *    |      .
450  *    |   .
451  *    | .
452  *    |.
453  *    |
454  *
455  */
456 Stencil
457 Lookup::bezier_sandwich (Bezier top_curve, Bezier bottom_curve, Real thickness)
458 {
459   SCM commands = scm_list_n (ly_symbol2scm ("moveto"),
460                              scm_from_double (top_curve.control_[0][X_AXIS]),
461                              scm_from_double (top_curve.control_[0][Y_AXIS]),
462                              ly_symbol2scm ("curveto"),
463                              scm_from_double (top_curve.control_[1][X_AXIS]),
464                              scm_from_double (top_curve.control_[1][Y_AXIS]),
465                              scm_from_double (top_curve.control_[2][X_AXIS]),
466                              scm_from_double (top_curve.control_[2][Y_AXIS]),
467                              scm_from_double (top_curve.control_[3][X_AXIS]),
468                              scm_from_double (top_curve.control_[3][Y_AXIS]),
469                              ly_symbol2scm ("curveto"),
470                              scm_from_double (bottom_curve.control_[2][X_AXIS]),
471                              scm_from_double (bottom_curve.control_[2][Y_AXIS]),
472                              scm_from_double (bottom_curve.control_[1][X_AXIS]),
473                              scm_from_double (bottom_curve.control_[1][Y_AXIS]),
474                              scm_from_double (bottom_curve.control_[0][X_AXIS]),
475                              scm_from_double (bottom_curve.control_[0][Y_AXIS]),
476                              ly_symbol2scm ("closepath"),
477                              SCM_UNDEFINED);
478
479   SCM horizontal_bend = scm_list_n (ly_symbol2scm ("path"),
480                                     scm_from_double (thickness),
481                                     ly_quote_scm (commands),
482                                     ly_quote_scm (ly_symbol2scm ("round")),
483                                     ly_quote_scm (ly_symbol2scm ("round")),
484                                     SCM_BOOL_T,
485                                     SCM_UNDEFINED);
486
487   Interval x_extent = top_curve.extent (X_AXIS);
488   x_extent.unite (bottom_curve.extent (X_AXIS));
489   Interval y_extent = top_curve.extent (Y_AXIS);
490   y_extent.unite (bottom_curve.extent (Y_AXIS));
491   Box b (x_extent, y_extent);
492
493   b.widen (0.5 * thickness, 0.5 * thickness);
494   return Stencil (b, horizontal_bend);
495 }
496
497 Stencil
498 Lookup::repeat_slash (Real w, Real s, Real t)
499 {
500
501   Real x_width = sqrt ((t * t) + ((t / s) * (t / s)));
502   Real height = w * s;
503
504   SCM controls = scm_list_n (ly_symbol2scm ("moveto"),
505                              scm_from_double (0),
506                              scm_from_double (0),
507                              ly_symbol2scm ("rlineto"),
508                              scm_from_double (x_width),
509                              scm_from_double (0),
510                              ly_symbol2scm ("rlineto"),
511                              scm_from_double (w),
512                              scm_from_double (height),
513                              ly_symbol2scm ("rlineto"),
514                              scm_from_double (-x_width),
515                              scm_from_double (0),
516                              ly_symbol2scm ("closepath"),
517                              SCM_UNDEFINED);
518
519   SCM slashnodot = scm_list_n (ly_symbol2scm ("path"),
520                                scm_from_double (0),
521                                ly_quote_scm (controls),
522                                ly_quote_scm (ly_symbol2scm ("round")),
523                                ly_quote_scm (ly_symbol2scm ("round")),
524                                SCM_BOOL_T,
525                                SCM_UNDEFINED);
526
527   Box b (Interval (0, w + sqrt (sqr (t / s) + sqr (t))),
528          Interval (0, w * s));
529
530   return Stencil (b, slashnodot); //  http://slashnodot.org
531 }
532
533 Stencil
534 Lookup::bracket (Axis a, Interval iv, Real thick, Real protrude, Real blot)
535 {
536   Box b;
537   Axis other = Axis ((a + 1) % 2);
538   b[a] = iv;
539   b[other] = Interval (-1, 1) * thick * 0.5;
540
541   Stencil m = round_filled_box (b, blot);
542
543   b[a] = Interval (iv[UP] - thick, iv[UP]);
544   Interval oi = Interval (-thick / 2, thick / 2 + fabs (protrude));
545   oi *= sign (protrude);
546   b[other] = oi;
547   m.add_stencil (round_filled_box (b, blot));
548   b[a] = Interval (iv[DOWN], iv[DOWN] + thick);
549   m.add_stencil (round_filled_box (b, blot));
550
551   return m;
552 }
553
554 Stencil
555 Lookup::triangle (Interval iv, Real thick, Real protrude)
556 {
557   Box b;
558   b[X_AXIS] = Interval (0, iv.length ());
559   b[Y_AXIS] = Interval (min (0., protrude), max (0.0, protrude));
560
561   vector<Offset> points;
562   points.push_back (Offset (iv[LEFT], 0));
563   points.push_back (Offset (iv[RIGHT], 0));
564   points.push_back (Offset (iv.center (), protrude));
565   points.push_back (Offset (iv[LEFT], 0));  // close triangle
566
567   return points_to_line_stencil (thick, points);
568
569 }
570
571 Stencil
572 Lookup::points_to_line_stencil (Real thick, vector<Offset> const &points)
573 {
574   Stencil ret;
575   for (vsize i = 1; i < points.size (); i++)
576     {
577       if (points[i - 1].is_sane () && points[i].is_sane ())
578         {
579           Stencil line
580             = Line_interface::make_line (thick, points[i - 1], points[i]);
581           ret.add_stencil (line);
582         }
583     }
584   return ret;
585 }