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