2 cluster.cc -- implement Cluster
4 source file of the GNU LilyPond music typesetter
6 (c) 2002--2003 Juergen Reuter <reuter@ipd.uka.de>
8 Han-Wen Nienhuys <hanwen@cs.uu.nl>
18 #include "staff-symbol-referencer.hh"
21 #include "interval.hh"
22 #include "paper-def.hh"
27 TODO: Add support for cubic spline segments.
31 brew_cluster_piece (Grob *me, Array<Offset> bottom_points, Array<Offset> top_points)
33 Real blotdiameter = Staff_symbol_referencer::staff_space (me)/2;
35 Real padding =robust_scm2double ( me->get_grob_property ("padding"), 0.0);
37 Offset vpadding = Offset (0, padding);
38 Offset hpadding = Offset (0.5 * blotdiameter, 0);
39 Offset hvpadding = 0.5 * hpadding + vpadding;
41 SCM shape_scm = me->get_grob_property ("style");
44 if (gh_symbol_p (shape_scm))
46 shape = ly_symbol2string (shape_scm);
50 programming_error ("#'style should be symbol.");
56 Molecule out = Molecule ();
59 int size = bottom_points.size ();
60 if (String::compare (shape, "leftsided-stairs") == 0)
62 for (int i = 0; i < size - 1; i++)
65 box.add_point (bottom_points[i] - hvpadding);
66 box.add_point (Offset(top_points[i + 1][X_AXIS],
67 top_points[i][Y_AXIS]) + hvpadding);
68 out.add_molecule (Lookup::round_filled_box (box, blotdiameter));
71 else if (String::compare (shape, "rightsided-stairs") == 0)
73 for (int i = 0; i < size - 1; i++)
76 box.add_point (Offset(bottom_points[i][X_AXIS],
77 bottom_points[i + 1][Y_AXIS]) - hvpadding);
78 box.add_point (top_points[i + 1] + hvpadding);
79 out.add_molecule (Lookup::round_filled_box (box, blotdiameter));
82 else if (String::compare (shape, "centered-stairs") == 0)
84 Real left_xmid = bottom_points[0][X_AXIS];
85 for (int i = 0; i < size - 1; i++)
88 0.5 * (bottom_points[i][X_AXIS] + bottom_points[i + 1][X_AXIS]);
90 box.add_point (Offset (left_xmid, bottom_points[i][Y_AXIS]) -
92 box.add_point (Offset (right_xmid, top_points[i][Y_AXIS]) +
94 out.add_molecule (Lookup::round_filled_box (box, blotdiameter));
95 left_xmid = right_xmid;
97 Real right_xmid = bottom_points[size - 1][X_AXIS];
99 box.add_point (Offset (left_xmid, bottom_points[size - 1][Y_AXIS]) -
101 box.add_point (Offset (right_xmid, top_points[size - 1][Y_AXIS]) +
103 out.add_molecule (Lookup::round_filled_box (box, blotdiameter));
105 else if (String::compare (shape, "ramp") == 0)
107 points.push (bottom_points[0] - vpadding + hpadding);
108 for (int i = 1; i < size - 1; i++)
110 points.push (bottom_points[i] - vpadding);
112 points.push (bottom_points[size - 1] - vpadding - hpadding);
113 points.push (top_points[size - 1] + vpadding - hpadding);
114 for (int i = size - 2; i > 0; i--)
116 points.push (top_points[i] + vpadding);
118 points.push (top_points[0] + vpadding + hpadding);
119 out.add_molecule (Lookup::round_filled_polygon (points, blotdiameter));
123 me->warning (_f ("unknown cluster style `%s'", shape.to_str0 ()));
128 MAKE_SCHEME_CALLBACK (Cluster,brew_molecule,1);
130 Cluster::brew_molecule (SCM smob)
132 Grob *me = unsmob_grob (smob);
134 Spanner *spanner = dynamic_cast<Spanner*> (me);
137 me->programming_error ("Cluster::brew_molecule(): not a spanner");
141 Item *left_bound = spanner->get_bound (LEFT);
142 Item *right_bound = spanner->get_bound (RIGHT);
144 Grob *common = left_bound->common_refpoint (right_bound, X_AXIS);
145 SCM cols =me->get_grob_property ("columns");
147 if (!gh_pair_p (cols))
149 me->warning ("junking empty cluster");
154 common = common_refpoint_of_list (cols, common, X_AXIS);
155 Array<Offset> bottom_points;
156 Array<Offset> top_points;
159 Real left_coord = left_bound->relative_coordinate (common, X_AXIS);
161 Real unit = Staff_symbol_referencer::staff_space (me) *0.5;
164 TODO: should we move the cluster a little to the right to be in
165 line with the center of the note heads?
168 for (SCM s = cols; gh_pair_p (s); s = ly_cdr (s))
170 Grob * col = unsmob_grob (ly_car (s));
172 SCM posns = col->get_grob_property ("positions");
175 if (is_number_pair (posns))
176 s = Slice (gh_scm2int (gh_car (posns)),
177 gh_scm2int (gh_cdr (posns)));
179 Real x = col->relative_coordinate (common, X_AXIS) - left_coord;
180 bottom_points.push (Offset (x, s[DOWN] *unit));
181 top_points.push (Offset (x, s[UP] * unit));
185 Across a line break we anticipate on the next pitches.
187 if (spanner->original_)
189 Spanner *orig = dynamic_cast<Spanner*> (spanner->original_);
191 if (spanner->get_break_index () < orig->broken_intos_.size()-1)
193 Spanner * next = orig->broken_intos_[spanner->get_break_index () + 1];
194 SCM cols = next->get_grob_property ("columns");
195 if (gh_pair_p (cols))
197 Grob * col = unsmob_grob (ly_car (scm_last_pair (cols)));
198 SCM posns = col->get_grob_property ("positions");
201 if (is_number_pair (posns))
202 s = Slice (gh_scm2int (gh_car (posns)),
203 gh_scm2int (gh_cdr (posns)));
205 Real x = right_bound->relative_coordinate (common,X_AXIS) - left_coord;
207 bottom_points.insert (Offset (x, s[DOWN] * unit),0);
208 top_points.insert (Offset (x, s[UP] * unit),0);
213 bottom_points.reverse ();
214 top_points.reverse ();
216 Molecule out = brew_cluster_piece (me, bottom_points, top_points);
217 return out.smobbed_copy ();
220 ADD_INTERFACE (Cluster,"cluster-interface",
221 "A graphically drawn musical cluster. "
223 "@code{padding} adds to the vertical extent of the shape (top and "
224 "bottom) and is expressed in units of staffspace. Since the pitch "
225 "range of a single pitch is infinitely small, if padding is set to "
226 "@code{0.0}, this possibly results in an invisible shape, if you,for "
227 "example, say @code{c-\\startCluster d e-\\endCluster}. The default "
228 "value for @code{padding} therefore is @code{0.25}, such that a single "
229 "pitch roughly shows the same height as a note head. "
231 "@code{style} controls the shape of cluster segments. Valid values include 'leftsided-stairs', 'rightsided-stairs', 'centered-stairs', and 'ramp'.\n"
233 "style padding columns");