]> git.donarmstrong.com Git - lilypond.git/blob - lily/cluster.cc
* flower
[lilypond.git] / lily / cluster.cc
1 /*
2   cluster.cc -- implement Cluster
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2002--2005 Juergen Reuter <reuter@ipd.uka.de>
7
8   Han-Wen Nienhuys <hanwen@cs.uu.nl>
9 */
10
11 #include "cluster.hh"
12
13 #include <cstdio>
14
15 #include "spanner.hh"
16 #include "item.hh"
17 #include "pitch.hh"
18 #include "staff-symbol-referencer.hh"
19 #include "lookup.hh"
20 #include "output-def.hh"
21 #include "warn.hh"
22
23 /*
24   TODO: Add support for cubic spline segments.
25 */
26 Stencil
27 brew_cluster_piece (Grob *me, Array<Offset> bottom_points, Array<Offset> top_points)
28 {
29   Real blotdiameter = Staff_symbol_referencer::staff_space (me) / 2;
30
31   Real padding = robust_scm2double (me->get_property ("padding"), 0.0);
32
33   Offset vpadding = Offset (0, padding);
34   Offset hpadding = Offset (0.5 * blotdiameter, 0);
35   Offset hvpadding = 0.5 * hpadding + vpadding;
36
37   SCM shape_scm = me->get_property ("style");
38   String shape;
39
40   if (scm_is_symbol (shape_scm))
41     {
42       shape = ly_symbol2string (shape_scm);
43     }
44   else
45     {
46       programming_error ("#'style should be symbol.");
47       me->suicide ();
48       return Stencil ();
49     }
50
51   Stencil out;
52   Array<Offset> points;
53   points.clear ();
54   int size = bottom_points.size ();
55   if (String::compare (shape, "leftsided-stairs") == 0)
56     {
57       for (int i = 0; i < size - 1; i++)
58         {
59           Box box;
60           box.add_point (bottom_points[i] - hvpadding);
61           box.add_point (Offset (top_points[i + 1][X_AXIS],
62                                  top_points[i][Y_AXIS]) + hvpadding);
63           out.add_stencil (Lookup::round_filled_box (box, blotdiameter));
64         }
65     }
66   else if (String::compare (shape, "rightsided-stairs") == 0)
67     {
68       for (int i = 0; i < size - 1; i++)
69         {
70           Box box;
71           box.add_point (Offset (bottom_points[i][X_AXIS],
72                                  bottom_points[i + 1][Y_AXIS]) - hvpadding);
73           box.add_point (top_points[i + 1] + hvpadding);
74           out.add_stencil (Lookup::round_filled_box (box, blotdiameter));
75         }
76     }
77   else if (String::compare (shape, "centered-stairs") == 0)
78     {
79       Real left_xmid = bottom_points[0][X_AXIS];
80       for (int i = 0; i < size - 1; i++)
81         {
82           Real right_xmid
83             = 0.5 * (bottom_points[i][X_AXIS] + bottom_points[i + 1][X_AXIS]);
84           Box box;
85           box.add_point (Offset (left_xmid, bottom_points[i][Y_AXIS])
86                          - hvpadding);
87           box.add_point (Offset (right_xmid, top_points[i][Y_AXIS])
88                          + hvpadding);
89           out.add_stencil (Lookup::round_filled_box (box, blotdiameter));
90           left_xmid = right_xmid;
91         }
92       Real right_xmid = bottom_points[size - 1][X_AXIS];
93       Box box;
94       box.add_point (Offset (left_xmid, bottom_points[size - 1][Y_AXIS])
95                      - hvpadding);
96       box.add_point (Offset (right_xmid, top_points[size - 1][Y_AXIS])
97                      + hvpadding);
98       out.add_stencil (Lookup::round_filled_box (box, blotdiameter));
99     }
100   else if (String::compare (shape, "ramp") == 0)
101     {
102       points.push (bottom_points[0] - vpadding + hpadding);
103       for (int i = 1; i < size - 1; i++)
104         {
105           points.push (bottom_points[i] - vpadding);
106         }
107       points.push (bottom_points[size - 1] - vpadding - hpadding);
108       points.push (top_points[size - 1] + vpadding - hpadding);
109       for (int i = size - 2; i > 0; i--)
110         {
111           points.push (top_points[i] + vpadding);
112         }
113       points.push (top_points[0] + vpadding + hpadding);
114       out.add_stencil (Lookup::round_filled_polygon (points, blotdiameter));
115     }
116   else
117     {
118       me->warning (_f ("unknown cluster style `%s'", shape.to_str0 ()));
119     }
120   return out;
121 }
122
123 MAKE_SCHEME_CALLBACK (Cluster, print, 1);
124 SCM
125 Cluster::print (SCM smob)
126 {
127   Grob *me = unsmob_grob (smob);
128
129   Spanner *spanner = dynamic_cast<Spanner *> (me);
130   if (!spanner)
131     {
132       me->programming_error ("Cluster::print (): not a spanner");
133       return SCM_EOL;
134     }
135
136   Item *left_bound = spanner->get_bound (LEFT);
137   Item *right_bound = spanner->get_bound (RIGHT);
138
139   Grob *commonx = left_bound->common_refpoint (right_bound, X_AXIS);
140   SCM cols = me->get_property ("columns");
141
142   if (!scm_is_pair (cols))
143     {
144       me->warning ("junking empty cluster");
145       me->suicide ();
146
147       return SCM_EOL;
148     }
149
150   commonx = common_refpoint_of_list (cols, commonx, X_AXIS);
151   Grob *commony = common_refpoint_of_list (cols, me, Y_AXIS);
152   Array<Offset> bottom_points;
153   Array<Offset> top_points;
154
155   Real left_coord = left_bound->relative_coordinate (commonx, X_AXIS);
156
157   /*
158     TODO: should we move the cluster a little to the right to be in
159     line with the center of the note heads?
160
161   */
162   for (SCM s = cols; scm_is_pair (s); s = scm_cdr (s))
163     {
164       Grob *col = unsmob_grob (scm_car (s));
165       Interval yext = col->extent (commony, Y_AXIS);
166
167       Real x = col->relative_coordinate (commonx, X_AXIS) - left_coord;
168       bottom_points.push (Offset (x, yext[DOWN]));
169       top_points.push (Offset (x, yext[UP]));
170     }
171
172   /*
173     Across a line break we anticipate on the next pitches.
174   */
175   if (spanner->original_)
176     {
177       Spanner *orig = dynamic_cast<Spanner *> (spanner->original_);
178
179       if (spanner->get_break_index () < orig->broken_intos_.size () - 1)
180         {
181           Spanner *next = orig->broken_intos_[spanner->get_break_index () + 1];
182           SCM cols = next->get_property ("columns");
183           if (scm_is_pair (cols))
184             {
185               Grob *next_commony = common_refpoint_of_list (cols, next, Y_AXIS);
186               Grob *col = unsmob_grob (scm_car (scm_last_pair (cols)));
187
188               Interval v = col->extent (next_commony, Y_AXIS);
189               Real x = right_bound->relative_coordinate (commonx, X_AXIS) - left_coord;
190
191               bottom_points.insert (Offset (x, v[DOWN]), 0);
192               top_points.insert (Offset (x, v[UP]), 0);
193             }
194         }
195     }
196
197   bottom_points.reverse ();
198   top_points.reverse ();
199
200   Stencil out = brew_cluster_piece (me, bottom_points, top_points);
201   out.translate_axis (- me->relative_coordinate (commony, Y_AXIS), Y_AXIS);
202   return out.smobbed_copy ();
203 }
204
205 ADD_INTERFACE (Cluster, "cluster-interface",
206                "A graphically drawn musical cluster. "
207                "\n\n"
208                "@code{padding} adds to the vertical extent of the shape (top and "
209                "bottom). \n\n"
210                "The property @code{style} controls the shape of cluster segments.  Valid values "
211                "include @code{leftsided-stairs}, @code{rightsided-stairs}, @code{centered-stairs}, "
212                "and @code{ramp}.\n",
213                "style padding columns");
214
215 struct Cluster_beacon
216 {
217 public:
218   DECLARE_SCHEME_CALLBACK (height, (SCM, SCM));
219   static bool has_interface (Grob *);
220 };
221
222 MAKE_SCHEME_CALLBACK (Cluster_beacon, height, 2);
223 SCM
224 Cluster_beacon::height (SCM g, SCM)
225 {
226   Grob *me = unsmob_grob (g);
227   Interval v = robust_scm2interval (me->get_property ("positions"),
228                                     Interval (0, 0));
229   return ly_interval2scm (Staff_symbol_referencer::staff_space (me) * 0.5 * v);
230 }
231
232 ADD_INTERFACE (Cluster_beacon,
233                "cluster-beacon-interface",
234                "A place holder for the cluster spanner to determine the vertical "
235                "extents of a cluster spanner at this X position.",
236                "positions");