2 cluster.cc -- implement Cluster
4 source file of the GNU LilyPond music typesetter
6 (c) 2002--2005 Juergen Reuter <reuter@ipd.uka.de>
8 Han-Wen Nienhuys <hanwen@cs.uu.nl>
18 #include "staff-symbol-referencer.hh"
20 #include "output-def.hh"
24 TODO: Add support for cubic spline segments.
27 brew_cluster_piece (Grob *me, Array<Offset> bottom_points, Array<Offset> top_points)
29 Real blotdiameter = Staff_symbol_referencer::staff_space (me) / 2;
31 Real padding = robust_scm2double (me->get_property ("padding"), 0.0);
33 Offset vpadding = Offset (0, padding);
34 Offset hpadding = Offset (0.5 * blotdiameter, 0);
35 Offset hvpadding = 0.5 * hpadding + vpadding;
37 SCM shape_scm = me->get_property ("style");
40 if (scm_is_symbol (shape_scm))
42 shape = ly_symbol2string (shape_scm);
46 programming_error ("#'style should be symbol.");
54 int size = bottom_points.size ();
55 if (String::compare (shape, "leftsided-stairs") == 0)
57 for (int i = 0; i < size - 1; i++)
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));
66 else if (String::compare (shape, "rightsided-stairs") == 0)
68 for (int i = 0; i < size - 1; i++)
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));
77 else if (String::compare (shape, "centered-stairs") == 0)
79 Real left_xmid = bottom_points[0][X_AXIS];
80 for (int i = 0; i < size - 1; i++)
83 = 0.5 * (bottom_points[i][X_AXIS] + bottom_points[i + 1][X_AXIS]);
85 box.add_point (Offset (left_xmid, bottom_points[i][Y_AXIS])
87 box.add_point (Offset (right_xmid, top_points[i][Y_AXIS])
89 out.add_stencil (Lookup::round_filled_box (box, blotdiameter));
90 left_xmid = right_xmid;
92 Real right_xmid = bottom_points[size - 1][X_AXIS];
94 box.add_point (Offset (left_xmid, bottom_points[size - 1][Y_AXIS])
96 box.add_point (Offset (right_xmid, top_points[size - 1][Y_AXIS])
98 out.add_stencil (Lookup::round_filled_box (box, blotdiameter));
100 else if (String::compare (shape, "ramp") == 0)
102 points.push (bottom_points[0] - vpadding + hpadding);
103 for (int i = 1; i < size - 1; i++)
105 points.push (bottom_points[i] - vpadding);
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--)
111 points.push (top_points[i] + vpadding);
113 points.push (top_points[0] + vpadding + hpadding);
114 out.add_stencil (Lookup::round_filled_polygon (points, blotdiameter));
118 me->warning (_f ("unknown cluster style `%s'", shape.to_str0 ()));
123 MAKE_SCHEME_CALLBACK (Cluster, print, 1);
125 Cluster::print (SCM smob)
127 Grob *me = unsmob_grob (smob);
129 Spanner *spanner = dynamic_cast<Spanner *> (me);
132 me->programming_error ("Cluster::print (): not a spanner");
136 Item *left_bound = spanner->get_bound (LEFT);
137 Item *right_bound = spanner->get_bound (RIGHT);
139 Grob *commonx = left_bound->common_refpoint (right_bound, X_AXIS);
140 SCM cols = me->get_property ("columns");
142 if (!scm_is_pair (cols))
144 me->warning (_ ("junking empty cluster"));
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;
155 Real left_coord = left_bound->relative_coordinate (commonx, X_AXIS);
158 TODO: should we move the cluster a little to the right to be in
159 line with the center of the note heads?
162 for (SCM s = cols; scm_is_pair (s); s = scm_cdr (s))
164 Grob *col = unsmob_grob (scm_car (s));
165 Interval yext = col->extent (commony, Y_AXIS);
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]));
173 Across a line break we anticipate on the next pitches.
175 if (spanner->original_)
177 Spanner *orig = dynamic_cast<Spanner *> (spanner->original_);
179 if (spanner->get_break_index () < orig->broken_intos_.size () - 1)
181 Spanner *next = orig->broken_intos_[spanner->get_break_index () + 1];
182 SCM cols = next->get_property ("columns");
183 if (scm_is_pair (cols))
185 Grob *next_commony = common_refpoint_of_list (cols, next, Y_AXIS);
186 Grob *col = unsmob_grob (scm_car (scm_last_pair (cols)));
188 Interval v = col->extent (next_commony, Y_AXIS);
189 Real x = right_bound->relative_coordinate (commonx, X_AXIS) - left_coord;
191 bottom_points.insert (Offset (x, v[DOWN]), 0);
192 top_points.insert (Offset (x, v[UP]), 0);
197 bottom_points.reverse ();
198 top_points.reverse ();
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 ();
205 ADD_INTERFACE (Cluster, "cluster-interface",
206 "A graphically drawn musical cluster. "
208 "@code{padding} adds to the vertical extent of the shape (top and "
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");
215 struct Cluster_beacon
218 DECLARE_SCHEME_CALLBACK (height, (SCM, SCM));
219 static bool has_interface (Grob *);
222 MAKE_SCHEME_CALLBACK (Cluster_beacon, height, 2);
224 Cluster_beacon::height (SCM g, SCM)
226 Grob *me = unsmob_grob (g);
227 Interval v = robust_scm2interval (me->get_property ("positions"),
229 return ly_interval2scm (Staff_symbol_referencer::staff_space (me) * 0.5 * v);
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.",