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