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