]> git.donarmstrong.com Git - lilypond.git/blob - lily/lookup.cc
Web-ja: update introduction
[lilypond.git] / lily / lookup.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2015 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_4 (ly_symbol2scm ("polygon"),
79                          ly_quote_scm (points),
80                          scm_from_double (blot),
81                          SCM_BOOL_T);
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 = Offset (1, slope).direction ();
91
92   pts.push_back (Offset (0, -thick / 2) * rot);
93   pts.push_back (Offset (width, -thick / 2) * rot);
94   pts.push_back (Offset (width, thick / 2) * rot);
95   pts.push_back (Offset (0, thick / 2) * rot);
96   return Lookup::round_filled_polygon (pts, blot);
97 }
98
99 Stencil
100 Lookup::horizontal_line (Interval w, Real th)
101 {
102   SCM at = scm_list_n (ly_symbol2scm ("draw-line"),
103                        scm_from_double (th),
104                        scm_from_double (w[LEFT]),
105                        scm_from_double (0),
106                        scm_from_double (w[RIGHT]),
107                        scm_from_double (0),
108                        SCM_UNDEFINED);
109
110   Box box;
111   box[X_AXIS] = w;
112   box[Y_AXIS] = Interval (-th / 2, th / 2);
113
114   return Stencil (box, at);
115 }
116
117 Stencil
118 Lookup::blank (Box b)
119 {
120   return Stencil (b, scm_string (SCM_EOL));
121 }
122
123 Stencil
124 Lookup::circle (Real rad, Real thick, bool filled)
125 {
126   Box b (Interval (-rad, rad), Interval (-rad, rad));
127   return Stencil (b, scm_list_4 (ly_symbol2scm ("circle"),
128                                  scm_from_double (rad),
129                                  scm_from_double (thick),
130                                  scm_from_bool (filled)));
131 }
132
133 Stencil
134 Lookup::filled_box (Box b)
135 {
136   return round_filled_box (b, 0.0);
137 }
138
139 /*
140  * round filled box:
141  *
142  *   __________________________________
143  *  /     \  ^           /     \      ^
144  * |         |blot              |     |
145  * |       | |dia       |       |     |
146  * |         |meter             |     |
147  * |\ _ _ /  v           \ _ _ /|     |
148  * |                            |     |
149  * |                            |     | Box
150  * |                    <------>|     | extent
151  * |                      blot  |     | (Y_AXIS)
152  * |                    diameter|     |
153  * |                            |     |
154  * |  _ _                  _ _  |     |
155  * |/     \              /     \|     |
156  * |                            |     |
157  * |       |            |       |     |
158  * |                            |     |
159  * x\_____/______________\_____/|_____v
160  * |(0, 0)                       |
161  * |                            |
162  * |                            |
163  * |<-------------------------->|
164  *       Box extent (X_AXIS)
165  */
166 Stencil
167 Lookup::round_filled_box (Box b, Real blotdiameter)
168 {
169   Real width = b.x ().delta ();
170   blotdiameter = min (blotdiameter, width);
171   Real height = b.y ().delta ();
172   blotdiameter = min (blotdiameter, height);
173
174   if (blotdiameter < 0.0)
175     {
176       if (!isinf (blotdiameter))
177         warning (_f ("Not drawing a box with negative dimension, %.2f by %.2f.",
178                      width, height));
179       return Stencil (b, SCM_EOL);
180     }
181
182   SCM at = (scm_list_n (ly_symbol2scm ("round-filled-box"),
183                         scm_from_double (-b[X_AXIS][LEFT]),
184                         scm_from_double (b[X_AXIS][RIGHT]),
185                         scm_from_double (-b[Y_AXIS][DOWN]),
186                         scm_from_double (b[Y_AXIS][UP]),
187                         scm_from_double (blotdiameter),
188                         SCM_UNDEFINED));
189
190   return Stencil (b, at);
191 }
192
193 /*
194  * Create Stencil that represents a filled polygon with round edges.
195  *
196  * LIMITATIONS:
197  *
198  * (a) Only outer (convex) edges are rounded.
199  *
200  * (b) This algorithm works as expected only for polygons whose edges
201  * do not intersect.  For example, the polygon ((0, 0), (q, 0), (0,
202  * q), (q, q)) has an intersection at point (q/2, q/2) and therefore
203  * will give a strange result.  Even non-adjacent edges that just
204  * touch each other will in general not work as expected for non-null
205  * blotdiameter.
206  *
207  * (c) Given a polygon ((x0, y0), (x1, y1), ... , (x (n-1), y (n-1))),
208  * if there is a natural number k such that blotdiameter is greater
209  * than the maximum of { | (x (k mod n), y (k mod n)) - (x ((k+1) mod n),
210  * y ((k+1) mod n)) |, | (x (k mod n), y (k mod n)) - (x ((k+2) mod n),
211  * y ((k+2) mod n)) |, | (x ((k+1) mod n), y ((k+1) mod n)) - (x ((k+2)
212  * mod n), y ((k+2) mod n)) | }, then the outline of the rounded
213  * polygon will exceed the outline of the core polygon.  In other
214  * words: Do not draw rounded polygons that have a leg smaller or
215  * thinner than blotdiameter (or set blotdiameter to a sufficiently
216  * small value -- maybe even 0.0)!
217  *
218  * NOTE: Limitations (b) and (c) arise from the fact that round edges
219  * are made by moulding sharp edges to round ones rather than adding
220  * to a core filled polygon.  For details of these two different
221  * approaches, see the thread upon the ledger lines patch that started
222  * on March 25, 2002 on the devel mailing list.  The below version of
223  * round_filled_polygon () sticks to the moulding model, which the
224  * majority of the list participants finally voted for.  This,
225  * however, results in the above limitations and a much increased
226  * complexity of the algorithm, since it has to compute a shrinked
227  * polygon -- which is not trivial define precisely and unambigously.
228  * With the other approach, one simply could move a circle of size
229  * blotdiameter along all edges of the polygon (which is what the
230  * postscript routine in the backend effectively does, but on the
231  * shrinked polygon). --jr
232  *
233  * An extra parameter "extroversion" has been added since staying just
234  * inside of a polygon will reduce its visual size when tracing a
235  * rounded path.  If extroversion is zero, the polygon is just traced
236  * as-is.  If it is -1 (the default) the drawing will stay just within
237  * the given polygon.  If it is 1, the traced line will stay just
238  * outside of the given polygon.
239  */
240 Stencil
241 Lookup::round_filled_polygon (vector<Offset> const &points,
242                               Real blotdiameter,
243                               Real extroversion)
244 {
245   /* TODO: Maybe print a warning if one of the above limitations
246      applies to the given polygon.  However, this is quite complicated
247      to check. */
248
249 #ifdef DEBUG
250   const Real epsilon = 0.01;
251
252   /* remove consecutive duplicate points */
253   for (vsize i = 0; i < points.size (); i++)
254     {
255       int next = (i + 1) % points.size ();
256       Real d = (points[i] - points[next]).length ();
257       if (d < epsilon)
258         programming_error ("Polygon should not have duplicate points");
259     }
260 #endif
261
262   /* special cases: degenerated polygons */
263   if (points.size () == 0)
264     return Stencil ();
265   if (points.size () == 1)
266     {
267       Stencil circ = circle (0.5 * (1.0 + extroversion) * blotdiameter, 0, true);
268       circ.translate (points[0]);
269       return circ;
270     }
271   if (points.size () == 2)
272     return Line_interface::make_line ((1.0 + extroversion) * blotdiameter, points[0], points[1]);
273
274   vector<Offset> shrunk_points;
275
276   if (extroversion == 0.0)
277     {
278       shrunk_points = points;
279     }
280   else
281     {
282       /* shrink polygon in size by 0.5 * blotdiameter */
283
284       // first we need to determine the orientation of the polygon in
285       // order to decide whether shrinking means moving the polygon to the
286       // left or to the right of the outline.  We do that by calculating
287       // (double) the oriented area of the polygon.  We first determine the
288       // center and do the area calculations relative to it.
289       // Mathematically, the result is not affected by this shift, but
290       // numerically a lot of cancellation is going on and this keeps its
291       // effects in check.
292
293       Offset center;
294       for (vsize i = 0; i < points.size (); i++)
295         center += points[i];
296       center /= points.size ();
297
298       Real area = 0.0;
299       Offset last = points.back () - center;
300
301       for (vsize i = 0; i < points.size (); i++)
302         {
303           Offset here = points[i] - center;
304           area += cross_product (last, here);
305           last = here;
306         }
307
308       bool ccw = area >= 0.0;  // true if whole shape is counterclockwise oriented
309
310       shrunk_points.resize (points.size ());
311
312       for (vsize i = 0; i < points.size (); i++)
313         {
314           int i0 = i;
315           int i1 = (i + 1) % points.size ();
316           int i2 = (i + 2) % points.size ();
317           Offset p0 = points[i0];
318           Offset p1 = points[i1];
319           Offset p2 = points[i2];
320           Offset p01 = p1 - p0;
321           Offset p12 = p2 - p1;
322           Offset inward0 = Offset(-p01[Y_AXIS], p01[X_AXIS]).direction ();
323           Offset inward2 = Offset(-p12[Y_AXIS], p12[X_AXIS]).direction ();
324
325           if (!ccw)
326             {
327               inward0 = -inward0;
328               inward2 = -inward2;
329             }
330
331           Offset middle = 0.5*(inward0 + inward2);
332
333           // "middle" now is a vector in the right direction for the
334           // shrinkage.  Its size needs to be large enough that the
335           // projection on either of the inward vectors has a size of 1.
336
337           Real proj = dot_product (middle, inward0);
338
339           // What's the size of proj?  Assuming that we have a corner
340           // angle of phi where 0 corresponds to a continuing line, the
341           // length of middle is 0.5 |(1+cos phi, sin phi)| = cos (phi/2),
342           // so its projection has length
343           // cos^2 (phi/2) = 0.5 + 0.5 cos (phi).
344           // We don't really want to move inwards more than 3 blob
345           // diameters corresponding to 6 blob radii.  So
346           // cos (phi/2) = 1/6 gives phi ~ 161, meaning that a 20 degree
347           // corner necessitates moving 3 blob diameters from the corner
348           // in order to stay inside the lines.  Ruler and circle agree.
349           // 0.03 is close enough to 1/36.  Basically we want to keep the
350           // shape from inverting from pulling too far inward.
351           // 3 diameters is pretty much a handwaving guess.
352
353           if (abs (proj) < 0.03)
354             proj = proj < 0 ? -0.03 : 0.03;
355
356           shrunk_points[i1] = p1 - (0.5 * blotdiameter / proj) * middle
357                           * extroversion;
358         }
359     }
360
361   /* build scm expression and bounding box */
362   SCM shrunk_points_scm = SCM_EOL;
363   Box box;
364   Box shrunk_box;
365   for (vsize i = 0; i < shrunk_points.size (); i++)
366     {
367       SCM x = scm_from_double (shrunk_points[i][X_AXIS]);
368       SCM y = scm_from_double (shrunk_points[i][Y_AXIS]);
369       shrunk_points_scm = scm_cons (x, scm_cons (y, shrunk_points_scm));
370       box.add_point (points[i]);
371       shrunk_box.add_point (shrunk_points[i]);
372     }
373   shrunk_box.widen (0.5*blotdiameter, 0.5*blotdiameter);
374   box.unite (shrunk_box);
375   SCM polygon_scm = scm_list_4 (ly_symbol2scm ("polygon"),
376                                 ly_quote_scm (shrunk_points_scm),
377                                 scm_from_double (blotdiameter),
378                                 SCM_BOOL_T);
379
380   Stencil polygon = Stencil (box, polygon_scm);
381   return polygon;
382 }
383
384 /*
385   TODO: deprecate?
386 */
387 Stencil
388 Lookup::frame (Box b, Real thick, Real blot)
389 {
390   Stencil m;
391   for (Axis a = X_AXIS; a < NO_AXES; a = Axis (a + 1))
392     {
393       Axis o = Axis ((a + 1) % NO_AXES);
394       for (LEFT_and_RIGHT (d))
395         {
396           Box edges;
397           edges[a] = b[a][d] + 0.5 * thick * Interval (-1, 1);
398           edges[o][DOWN] = b[o][DOWN] - thick / 2;
399           edges[o][UP] = b[o][UP] + thick / 2;
400
401           m.add_stencil (round_filled_box (edges, blot));
402         }
403     }
404   return m;
405 }
406
407 /*
408   Make a smooth curve along the points
409 */
410 Stencil
411 Lookup::slur (Bezier curve, Real curvethick, Real linethick,
412               SCM dash_details)
413 {
414   Stencil return_value;
415
416   /*
417       calculate the offset for the two beziers that make the sandwich
418       for the slur
419   */
420   Offset dir = (curve.control_[3] - curve.control_[0]).direction ();
421   Bezier back = curve;
422   Offset perp = 0.5 * curvethick * Offset (-dir[Y_AXIS], dir[X_AXIS]);
423   back.control_[1] += perp;
424   back.control_[2] += perp;
425
426   curve.control_[1] -= perp;
427   curve.control_[2] -= perp;
428
429   if (!scm_is_pair (dash_details))
430     {
431       /* solid slur  */
432       return_value = bezier_sandwich (back, curve, linethick);
433     }
434   else
435     {
436       /* dashed or combination slur */
437       int num_segments = scm_to_int (scm_length (dash_details));
438       for (int i = 0; i < num_segments; i++)
439         {
440           SCM dash_pattern = scm_list_ref (dash_details, scm_from_int (i));
441           Real t_min = robust_scm2double (scm_car (dash_pattern), 0);
442           Real t_max = robust_scm2double (scm_cadr (dash_pattern), 1.0);
443           Real dash_fraction
444             = robust_scm2double (scm_caddr (dash_pattern), 1.0);
445           Real dash_period
446             = robust_scm2double (scm_cadddr (dash_pattern), 0.75);
447           Bezier back_segment = back.extract (t_min, t_max);
448           Bezier curve_segment = curve.extract (t_min, t_max);
449           if (dash_fraction == 1.0)
450             return_value.add_stencil (bezier_sandwich (back_segment,
451                                                        curve_segment,
452                                                        linethick));
453           else
454             {
455               Bezier back_dash, curve_dash;
456               Real seg_length = (back_segment.control_[3]
457                                  - back_segment.control_[0]).length ();
458               int pattern_count = (int) (seg_length / dash_period);
459               Real pattern_length = 1.0 / (pattern_count + dash_fraction);
460               Real start_t, end_t;
461               for (int p = 0; p <= pattern_count; p++)
462                 {
463                   start_t = p * pattern_length;
464                   end_t = (p + dash_fraction) * pattern_length;
465                   back_dash
466                     = back_segment.extract (start_t, end_t);
467                   curve_dash
468                     = curve_segment.extract (start_t, end_t);
469                   return_value.add_stencil (bezier_sandwich (back_dash,
470                                                              curve_dash,
471                                                              linethick));
472                 }
473             }
474         }
475     }
476   return return_value;
477 }
478
479 /*
480  * Bezier Sandwich:
481  *
482  *                               .|
483  *                        .       |
484  *              top .             |
485  *              . curve           |
486  *          .                     |
487  *       .                        |
488  *     .                          |
489  *    |                           |
490  *    |                          .|
491  *    |                     .
492  *    |         bottom .
493  *    |            . curve
494  *    |         .
495  *    |      .
496  *    |   .
497  *    | .
498  *    |.
499  *    |
500  *
501  */
502 Stencil
503 Lookup::bezier_sandwich (Bezier top_curve, Bezier bottom_curve, Real thickness)
504 {
505   SCM commands = scm_list_n (ly_symbol2scm ("moveto"),
506                              scm_from_double (top_curve.control_[0][X_AXIS]),
507                              scm_from_double (top_curve.control_[0][Y_AXIS]),
508                              ly_symbol2scm ("curveto"),
509                              scm_from_double (top_curve.control_[1][X_AXIS]),
510                              scm_from_double (top_curve.control_[1][Y_AXIS]),
511                              scm_from_double (top_curve.control_[2][X_AXIS]),
512                              scm_from_double (top_curve.control_[2][Y_AXIS]),
513                              scm_from_double (top_curve.control_[3][X_AXIS]),
514                              scm_from_double (top_curve.control_[3][Y_AXIS]),
515                              ly_symbol2scm ("lineto"),
516                              scm_from_double (bottom_curve.control_[3][X_AXIS]),
517                              scm_from_double (bottom_curve.control_[3][Y_AXIS]),
518                              ly_symbol2scm ("curveto"),
519                              scm_from_double (bottom_curve.control_[2][X_AXIS]),
520                              scm_from_double (bottom_curve.control_[2][Y_AXIS]),
521                              scm_from_double (bottom_curve.control_[1][X_AXIS]),
522                              scm_from_double (bottom_curve.control_[1][Y_AXIS]),
523                              scm_from_double (bottom_curve.control_[0][X_AXIS]),
524                              scm_from_double (bottom_curve.control_[0][Y_AXIS]),
525                              ly_symbol2scm ("closepath"),
526                              SCM_UNDEFINED);
527
528   SCM horizontal_bend = scm_list_n (ly_symbol2scm ("path"),
529                                     scm_from_double (thickness),
530                                     ly_quote_scm (commands),
531                                     ly_quote_scm (ly_symbol2scm ("round")),
532                                     ly_quote_scm (ly_symbol2scm ("round")),
533                                     SCM_BOOL_T,
534                                     SCM_UNDEFINED);
535
536   Interval x_extent = top_curve.extent (X_AXIS);
537   x_extent.unite (bottom_curve.extent (X_AXIS));
538   Interval y_extent = top_curve.extent (Y_AXIS);
539   y_extent.unite (bottom_curve.extent (Y_AXIS));
540   Box b (x_extent, y_extent);
541
542   b.widen (0.5 * thickness, 0.5 * thickness);
543   return Stencil (b, horizontal_bend);
544 }
545
546 Stencil
547 Lookup::repeat_slash (Real w, Real s, Real t)
548 {
549
550   Real x_width = hypot (t, t/s);
551   Real height = w * s;
552
553   SCM controls = scm_list_n (ly_symbol2scm ("moveto"),
554                              scm_from_double (0),
555                              scm_from_double (0),
556                              ly_symbol2scm ("rlineto"),
557                              scm_from_double (x_width),
558                              scm_from_double (0),
559                              ly_symbol2scm ("rlineto"),
560                              scm_from_double (w),
561                              scm_from_double (height),
562                              ly_symbol2scm ("rlineto"),
563                              scm_from_double (-x_width),
564                              scm_from_double (0),
565                              ly_symbol2scm ("closepath"),
566                              SCM_UNDEFINED);
567
568   SCM slashnodot = scm_list_n (ly_symbol2scm ("path"),
569                                scm_from_double (0),
570                                ly_quote_scm (controls),
571                                ly_quote_scm (ly_symbol2scm ("round")),
572                                ly_quote_scm (ly_symbol2scm ("round")),
573                                SCM_BOOL_T,
574                                SCM_UNDEFINED);
575
576   Box b (Interval (0, w + x_width),
577          Interval (0, height));
578
579   return Stencil (b, slashnodot); //  http://slashnodot.org
580 }
581
582 Stencil
583 Lookup::bracket (Axis a, Interval iv, Real thick, Real protrude, Real blot)
584 {
585   Box b;
586   Axis other = other_axis (a);
587   b[a] = iv;
588   b[other] = Interval (-1, 1) * thick * 0.5;
589
590   Stencil m = round_filled_box (b, blot);
591
592   b[a] = Interval (iv[UP] - thick, iv[UP]);
593   Interval oi = Interval (-thick / 2, thick / 2 + fabs (protrude));
594   oi *= sign (protrude);
595   b[other] = oi;
596   m.add_stencil (round_filled_box (b, blot));
597   b[a] = Interval (iv[DOWN], iv[DOWN] + thick);
598   m.add_stencil (round_filled_box (b, blot));
599
600   return m;
601 }
602
603 Stencil
604 Lookup::triangle (Interval iv, Real thick, Real protrude)
605 {
606   Box b;
607   b[X_AXIS] = Interval (0, iv.length ());
608   b[Y_AXIS] = Interval (min (0., protrude), max (0.0, protrude));
609
610   vector<Offset> points;
611   points.push_back (Offset (iv[LEFT], 0));
612   points.push_back (Offset (iv[RIGHT], 0));
613   points.push_back (Offset (iv.center (), protrude));
614   points.push_back (Offset (iv[LEFT], 0));  // close triangle
615
616   return points_to_line_stencil (thick, points);
617
618 }
619
620 Stencil
621 Lookup::points_to_line_stencil (Real thick, vector<Offset> const &points)
622 {
623   Stencil ret;
624   for (vsize i = 1; i < points.size (); i++)
625     {
626       if (points[i - 1].is_sane () && points[i].is_sane ())
627         {
628           Stencil line
629             = Line_interface::make_line (thick, points[i - 1], points[i]);
630           ret.add_stencil (line);
631         }
632     }
633   return ret;
634 }