2 cluster.cc -- implement Cluster
4 source file of the GNU LilyPond music typesetter
6 (c) 2002 Juergen Reuter <reuter@ipd.uka.de>
15 #include "staff-symbol-referencer.hh"
18 #include "interval.hh"
19 #include "paper-def.hh"
20 #include "paper-column.hh"
23 * TODO: Add support for cubic spline segments.
26 brew_cluster_piece (Grob *me, Array<Offset> bottom_points, Array<Offset> top_points)
29 Real blotdiameter = me->get_paper ()->get_var ("blotdiameter");
31 Real blotdiameter = Staff_symbol_referencer::staff_space (me)/2;
35 SCM padding_scm = me->get_grob_property ("padding");
36 if (gh_number_p (padding_scm))
37 padding = gh_scm2double (padding_scm);
40 Offset vpadding = Offset (0, padding);
41 Offset hpadding = Offset (0.5 * blotdiameter, 0);
42 Offset hvpadding = 0.5 * hpadding + vpadding;
44 SCM shape_scm = me->get_grob_property ("shape");
46 if (gh_symbol_p (shape_scm))
48 shape = ly_symbol2string (shape_scm);
52 shape = "leftsided-stairs";
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::roundfilledbox (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::roundfilledbox (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::roundfilledbox (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::roundfilledbox (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 shape `%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);
136 me->programming_error ("Cluster::brew_molecule(): not a spanner");
140 Item *left_bound = spanner->get_bound (LEFT);
141 Item *right_bound = spanner->get_bound (RIGHT);
142 bool right_broken = right_bound->break_status_dir () != CENTER;
144 Grob *common = left_bound->common_refpoint (right_bound, X_AXIS);
147 Array<Offset> bottom_points;
148 Array<Offset> top_points;
149 bottom_points.clear ();
151 SCM column_scm = SCM_EOL;
152 for (SCM columns_scm = me->get_grob_property ("segments");
153 columns_scm != SCM_EOL;
154 columns_scm = ly_cdr (columns_scm)) {
155 column_scm = ly_car (columns_scm);
156 SCM col_scm = ly_car (column_scm);
157 if (gh_number_p (col_scm))
158 // broken spanner: this column not in this piece
160 continue; // still have to expect columns
162 break; // ok, we have seen all columns
163 column = unsmob_grob (col_scm);
164 column_scm = ly_cdr (column_scm);
165 Real y = 0.5 * gh_scm2double (ly_car (column_scm));
166 column_scm = ly_cdr (column_scm);
167 Pitch *pitch_min = unsmob_pitch (ly_car (column_scm));
168 column_scm = ly_cdr (column_scm);
169 Pitch *pitch_max = unsmob_pitch (ly_car (column_scm));
170 Real height = 0.5 * (pitch_max->steps () - pitch_min->steps ());
171 Real x = column->relative_coordinate (common, X_AXIS);
173 x -= left_bound->relative_coordinate (common, X_AXIS);
174 bottom_points.push (Offset (x, y));
175 top_points.push (Offset (x, y + height));
179 Real y = 0.5 * gh_scm2double (ly_car (column_scm));
180 column_scm = ly_cdr (column_scm);
181 Pitch *pitch_min = unsmob_pitch (ly_car (column_scm));
182 column_scm = ly_cdr (column_scm);
183 Pitch *pitch_max = unsmob_pitch (ly_car (column_scm));
184 Real height = 0.5 * (pitch_max->steps () - pitch_min->steps ());
186 right_bound->relative_coordinate (common, X_AXIS) -
187 left_bound->relative_coordinate (common, X_AXIS);
188 bottom_points.push (Offset (x, y));
189 top_points.push (Offset (x, y + height));
191 Molecule out = brew_cluster_piece (me, bottom_points, top_points);
192 return out.smobbed_copy ();
195 ADD_INTERFACE (Cluster,"cluster-interface",
196 "A graphically drawn musical cluster.",
197 "shape padding segments");