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