]> git.donarmstrong.com Git - lilypond.git/blob - lily/lookup.cc
Gets rid of dot stencil command
[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   SCM at = (scm_list_n (ly_symbol2scm ("round-filled-box"),
178                         scm_from_double (-b[X_AXIS][LEFT]),
179                         scm_from_double (b[X_AXIS][RIGHT]),
180                         scm_from_double (-b[Y_AXIS][DOWN]),
181                         scm_from_double (b[Y_AXIS][UP]),
182                         scm_from_double (blotdiameter),
183                         SCM_UNDEFINED));
184
185   return Stencil (b, at);
186 }
187
188 /*
189  * Create Stencil that represents a filled polygon with round edges.
190  *
191  * LIMITATIONS:
192  *
193  * (a) Only outer (convex) edges are rounded.
194  *
195  * (b) This algorithm works as expected only for polygons whose edges
196  * do not intersect.  For example, the polygon ((0, 0), (q, 0), (0,
197  * q), (q, q)) has an intersection at point (q/2, q/2) and therefore
198  * will give a strange result.  Even non-adjacent edges that just
199  * touch each other will in general not work as expected for non-null
200  * blotdiameter.
201  *
202  * (c) Given a polygon ((x0, y0), (x1, y1), ... , (x (n-1), y (n-1))),
203  * if there is a natural number k such that blotdiameter is greater
204  * than the maximum of { | (x (k mod n), y (k mod n)) - (x ((k+1) mod n),
205  * y ((k+1) mod n)) |, | (x (k mod n), y (k mod n)) - (x ((k+2) mod n),
206  * y ((k+2) mod n)) |, | (x ((k+1) mod n), y ((k+1) mod n)) - (x ((k+2)
207  * mod n), y ((k+2) mod n)) | }, then the outline of the rounded
208  * polygon will exceed the outline of the core polygon.  In other
209  * words: Do not draw rounded polygons that have a leg smaller or
210  * thinner than blotdiameter (or set blotdiameter to a sufficiently
211  * small value -- maybe even 0.0)!
212  *
213  * NOTE: Limitations (b) and (c) arise from the fact that round edges
214  * are made by moulding sharp edges to round ones rather than adding
215  * to a core filled polygon.  For details of these two different
216  * approaches, see the thread upon the ledger lines patch that started
217  * on March 25, 2002 on the devel mailing list.  The below version of
218  * round_filled_polygon () sticks to the moulding model, which the
219  * majority of the list participants finally voted for.  This,
220  * however, results in the above limitations and a much increased
221  * complexity of the algorithm, since it has to compute a shrinked
222  * polygon -- which is not trivial define precisely and unambigously.
223  * With the other approach, one simply could move a circle of size
224  * blotdiameter along all edges of the polygon (which is what the
225  * postscript routine in the backend effectively does, but on the
226  * shrinked polygon). --jr
227  */
228 Stencil
229 Lookup::round_filled_polygon (vector<Offset> const &points,
230                               Real blotdiameter)
231 {
232   /* TODO: Maybe print a warning if one of the above limitations
233      applies to the given polygon.  However, this is quite complicated
234      to check. */
235
236   const Real epsilon = 0.01;
237
238 #ifndef NDEBUG
239   /* remove consecutive duplicate points */
240   for (vsize i = 0; i < points.size (); i++)
241     {
242       int next = (i + 1) % points.size ();
243       Real d = (points[i] - points[next]).length ();
244       if (d < epsilon)
245         programming_error ("Polygon should not have duplicate points");
246     }
247 #endif
248
249   /* special cases: degenerated polygons */
250   if (points.size () == 0)
251     return Stencil ();
252   if (points.size () == 1)
253     {
254       Stencil circ = circle (0.5 * blotdiameter, 0, true);
255       circ.translate (points[0]);
256       return circ;
257     }
258   if (points.size () == 2)
259     return Line_interface::make_line (blotdiameter, points[0], points[1]);
260
261   /* shrink polygon in size by 0.5 * blotdiameter */
262   vector<Offset> shrunk_points;
263   shrunk_points.resize (points.size ());
264   bool ccw = 1; // true, if three adjacent points are counterclockwise ordered
265   for (vsize i = 0; i < points.size (); i++)
266     {
267       int i0 = i;
268       int i1 = (i + 1) % points.size ();
269       int i2 = (i + 2) % points.size ();
270       Offset p0 = points[i0];
271       Offset p1 = points[i1];
272       Offset p2 = points[i2];
273       Offset p10 = p0 - p1;
274       Offset p12 = p2 - p1;
275       if (p10.length () != 0.0)
276         {
277           // recompute ccw
278           Real phi = p10.arg ();
279           // rotate (p2 - p0) by (-phi)
280           Offset q = complex_multiply (p2 - p0, complex_exp (Offset (1.0, -phi)));
281
282           if (q[Y_AXIS] > 0)
283             ccw = 1;
284           else if (q[Y_AXIS] < 0)
285             ccw = 0;
286           else {} // keep ccw unchanged
287         }
288       else {} // keep ccw unchanged
289       Offset p10n = (1.0 / p10.length ()) * p10; // normalize length to 1.0
290       Offset p12n = (1.0 / p12.length ()) * p12;
291       Offset p13n = 0.5 * (p10n + p12n);
292       Offset p14n = 0.5 * (p10n - p12n);
293       Offset p13;
294       Real d = p13n.length () * p14n.length (); // distance p3n to line (p1..p0)
295       if (d < epsilon)
296         // special case: p0, p1, p2 are on a single line => build
297         // vector orthogonal to (p2-p0) of length 0.5 blotdiameter
298         {
299           p13[X_AXIS] = p10[Y_AXIS];
300           p13[Y_AXIS] = -p10[X_AXIS];
301           p13 = (0.5 * blotdiameter / p13.length ()) * p13;
302         }
303       else
304         p13 = (0.5 * blotdiameter / d) * p13n;
305       shrunk_points[i1] = p1 + ((ccw) ? p13 : -p13);
306     }
307
308   /* build scm expression and bounding box */
309   SCM shrunk_points_scm = SCM_EOL;
310   Box box;
311   for (vsize i = 0; i < shrunk_points.size (); i++)
312     {
313       SCM x = scm_from_double (shrunk_points[i][X_AXIS]);
314       SCM y = scm_from_double (shrunk_points[i][Y_AXIS]);
315       shrunk_points_scm = scm_cons (x, scm_cons (y, shrunk_points_scm));
316       box.add_point (points[i]);
317     }
318   SCM polygon_scm = scm_list_n (ly_symbol2scm ("polygon"),
319                                 ly_quote_scm (shrunk_points_scm),
320                                 scm_from_double (blotdiameter),
321                                 SCM_BOOL_T,
322                                 SCM_UNDEFINED);
323
324   Stencil polygon = Stencil (box, polygon_scm);
325   shrunk_points.clear ();
326   return polygon;
327 }
328
329 /*
330   TODO: deprecate?
331 */
332 Stencil
333 Lookup::frame (Box b, Real thick, Real blot)
334 {
335   Stencil m;
336   Direction d = LEFT;
337   for (Axis a = X_AXIS; a < NO_AXES; a = Axis (a + 1))
338     {
339       Axis o = Axis ((a + 1) % NO_AXES);
340       do
341         {
342           Box edges;
343           edges[a] = b[a][d] + 0.5 * thick * Interval (-1, 1);
344           edges[o][DOWN] = b[o][DOWN] - thick / 2;
345           edges[o][UP] = b[o][UP] + thick / 2;
346
347           m.add_stencil (round_filled_box (edges, blot));
348         }
349       while (flip (&d) != LEFT);
350     }
351   return m;
352 }
353
354 /*
355   Make a smooth curve along the points
356 */
357 Stencil
358 Lookup::slur (Bezier curve, Real curvethick, Real linethick,
359               SCM dash_details)
360 {
361   Stencil return_value;
362
363   /*
364       calculate the offset for the two beziers that make the sandwich
365       for the slur
366   */
367   Real alpha = (curve.control_[3] - curve.control_[0]).arg ();
368   Bezier back = curve;
369   Offset perp = curvethick * complex_exp (Offset (0, alpha + M_PI / 2)) * 0.5;
370   back.control_[1] += perp;
371   back.control_[2] += perp;
372
373   curve.control_[1] -= perp;
374   curve.control_[2] -= perp;
375
376   if (!scm_is_pair (dash_details))
377     {
378       /* solid slur  */
379       return_value = bezier_sandwich (back, curve, linethick);
380     }
381   else
382     {
383       /* dashed or combination slur */
384       int num_segments = scm_to_int (scm_length (dash_details));
385       for (int i = 0; i < num_segments; i++)
386         {
387           SCM dash_pattern = scm_list_ref (dash_details, scm_from_int (i));
388           Real t_min = robust_scm2double (scm_car (dash_pattern), 0);
389           Real t_max = robust_scm2double (scm_cadr (dash_pattern), 1.0);
390           Real dash_fraction
391             = robust_scm2double (scm_caddr (dash_pattern), 1.0);
392           Real dash_period
393             = robust_scm2double (scm_cadddr (dash_pattern), 0.75);
394           Bezier back_segment = back.extract (t_min, t_max);
395           Bezier curve_segment = curve.extract (t_min, t_max);
396           if (dash_fraction == 1.0)
397             return_value.add_stencil (bezier_sandwich (back_segment,
398                                                        curve_segment,
399                                                        linethick));
400           else
401             {
402               Bezier back_dash, curve_dash;
403               Real seg_length = (back_segment.control_[3]
404                                  - back_segment.control_[0]).length ();
405               int pattern_count = (int) (seg_length / dash_period);
406               Real pattern_length = 1.0 / (pattern_count + dash_fraction);
407               Real start_t, end_t;
408               for (int p = 0; p <= pattern_count; p++)
409                 {
410                   start_t = p * pattern_length;
411                   end_t = (p + dash_fraction) * pattern_length;
412                   back_dash
413                     = back_segment.extract (start_t, end_t);
414                   curve_dash
415                     = curve_segment.extract (start_t, end_t);
416                   return_value.add_stencil (bezier_sandwich (back_dash,
417                                                              curve_dash,
418                                                              linethick));
419                 }
420             }
421         }
422     }
423   return return_value;
424 }
425
426 /*
427  * Bezier Sandwich:
428  *
429  *                               .|
430  *                        .       |
431  *              top .             |
432  *              . curve           |
433  *          .                     |
434  *       .                        |
435  *     .                          |
436  *    |                           |
437  *    |                          .|
438  *    |                     .
439  *    |         bottom .
440  *    |            . curve
441  *    |         .
442  *    |      .
443  *    |   .
444  *    | .
445  *    |.
446  *    |
447  *
448  */
449 Stencil
450 Lookup::bezier_sandwich (Bezier top_curve, Bezier bottom_curve, Real thickness)
451 {
452   /*
453     Need the weird order b.o. the way PS want its arguments
454   */
455   SCM list = SCM_EOL;
456   list = scm_cons (ly_offset2scm (bottom_curve.control_[3]), list);
457   list = scm_cons (ly_offset2scm (bottom_curve.control_[0]), list);
458   list = scm_cons (ly_offset2scm (bottom_curve.control_[1]), list);
459   list = scm_cons (ly_offset2scm (bottom_curve.control_[2]), list);
460   list = scm_cons (ly_offset2scm (top_curve.control_[0]), list);
461   list = scm_cons (ly_offset2scm (top_curve.control_[3]), list);
462   list = scm_cons (ly_offset2scm (top_curve.control_[2]), list);
463   list = scm_cons (ly_offset2scm (top_curve.control_[1]), list);
464
465   SCM horizontal_bend = scm_list_n (ly_symbol2scm ("bezier-sandwich"),
466                                     ly_quote_scm (list),
467                                     scm_from_double (thickness),
468                                     SCM_UNDEFINED);
469
470   Interval x_extent = top_curve.extent (X_AXIS);
471   x_extent.unite (bottom_curve.extent (X_AXIS));
472   Interval y_extent = top_curve.extent (Y_AXIS);
473   y_extent.unite (bottom_curve.extent (Y_AXIS));
474   Box b (x_extent, y_extent);
475
476   b.widen (0.5 * thickness, 0.5 * thickness);
477   return Stencil (b, horizontal_bend);
478 }
479
480 Stencil
481 Lookup::repeat_slash (Real w, Real s, Real t)
482 {
483 #if 0 /*  TODO */
484   vector<Offset> points;
485   Real blotdiameter = 0.0;
486
487   Offset p1 (0, 0);
488   Offset p2 (w, w * s);
489
490   return Lookup::round_filled_polygon (points, blotdiameter);
491 #endif
492
493   SCM wid = scm_from_double (w);
494   SCM sl = scm_from_double (s);
495   SCM thick = scm_from_double (t);
496   SCM slashnodot = scm_list_n (ly_symbol2scm ("repeat-slash"),
497                                wid, sl, thick, SCM_UNDEFINED);
498
499   Box b (Interval (0, w + sqrt (sqr (t / s) + sqr (t))),
500          Interval (0, w * s));
501
502   return Stencil (b, slashnodot); //  http://slashnodot.org
503 }
504
505 Stencil
506 Lookup::bracket (Axis a, Interval iv, Real thick, Real protrude, Real blot)
507 {
508   Box b;
509   Axis other = Axis ((a + 1) % 2);
510   b[a] = iv;
511   b[other] = Interval (-1, 1) * thick * 0.5;
512
513   Stencil m = round_filled_box (b, blot);
514
515   b[a] = Interval (iv[UP] - thick, iv[UP]);
516   Interval oi = Interval (-thick / 2, thick / 2 + fabs (protrude));
517   oi *= sign (protrude);
518   b[other] = oi;
519   m.add_stencil (round_filled_box (b, blot));
520   b[a] = Interval (iv[DOWN], iv[DOWN] + thick);
521   m.add_stencil (round_filled_box (b, blot));
522
523   return m;
524 }
525
526 Stencil
527 Lookup::triangle (Interval iv, Real thick, Real protrude)
528 {
529   Box b;
530   b[X_AXIS] = Interval (0, iv.length ());
531   b[Y_AXIS] = Interval (min (0., protrude), max (0.0, protrude));
532
533   vector<Offset> points;
534   points.push_back (Offset (iv[LEFT], 0));
535   points.push_back (Offset (iv[RIGHT], 0));
536   points.push_back (Offset (iv.center (), protrude));
537   points.push_back (Offset (iv[LEFT], 0));  // close triangle
538
539   return points_to_line_stencil (thick, points);
540
541 }
542
543 Stencil
544 Lookup::points_to_line_stencil (Real thick, vector<Offset> const &points)
545 {
546   Stencil ret;
547   for (vsize i = 1; i < points.size (); i++)
548     {
549       if (points[i - 1].is_sane () && points[i].is_sane ())
550         {
551           Stencil line
552             = Line_interface::make_line (thick, points[i - 1], points[i]);
553           ret.add_stencil (line);
554         }
555     }
556   return ret;
557 }