]> git.donarmstrong.com Git - lilypond.git/blob - lily/cluster.cc
*** empty log message ***
[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--2006 Juergen Reuter <reuter@ipd.uka.de>
7
8   Han-Wen Nienhuys <hanwen@xs4all.nl>
9 */
10
11 #include "cluster.hh"
12
13 #include <cstdio>
14 using namespace std;
15
16 #include "international.hh"
17 #include "item.hh"
18 #include "lookup.hh"
19 #include "output-def.hh"
20 #include "pitch.hh"
21 #include "pointer-group-interface.hh"
22 #include "spanner.hh"
23 #include "staff-symbol-referencer.hh"
24 #include "warn.hh"
25
26 /*
27   TODO: Add support for cubic spline segments.
28 */
29 Stencil
30 brew_cluster_piece (Grob *me, Array<Offset> bottom_points, Array<Offset> top_points)
31 {
32   Real blotdiameter = Staff_symbol_referencer::staff_space (me) / 2;
33
34   Real padding = robust_scm2double (me->get_property ("padding"), 0.0);
35
36   Offset vpadding = Offset (0, padding);
37   Offset hpadding = Offset (0.5 * blotdiameter, 0);
38   Offset hvpadding = 0.5 * hpadding + vpadding;
39
40   SCM shape_scm = me->get_property ("style");
41   std::string shape;
42
43   if (scm_is_symbol (shape_scm))
44     shape = ly_symbol2string (shape_scm);
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 (shape == "leftsided-stairs")
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 (shape == "rightsided-stairs")
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 (shape == "centered-stairs")
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 (shape == "ramp")
102     {
103       points.push (bottom_points[0] - vpadding + hpadding);
104       for (int i = 1; i < size - 1; i++)
105         points.push (bottom_points[i] - vpadding);
106       points.push (bottom_points[size - 1] - vpadding - hpadding);
107       points.push (top_points[size - 1] + vpadding - hpadding);
108       for (int i = size - 2; i > 0; i--)
109         points.push (top_points[i] + vpadding);
110       points.push (top_points[0] + vpadding + hpadding);
111       out.add_stencil (Lookup::round_filled_polygon (points, blotdiameter));
112     }
113   else
114     me->warning (_f ("unknown cluster style `%s'", shape.c_str ()));
115   return out;
116 }
117
118 MAKE_SCHEME_CALLBACK (Cluster, print, 1);
119 SCM
120 Cluster::print (SCM smob)
121 {
122   Grob *me = unsmob_grob (smob);
123
124   Spanner *spanner = dynamic_cast<Spanner *> (me);
125   if (!spanner)
126     {
127       me->programming_error ("Cluster::print (): not a spanner");
128       return SCM_EOL;
129     }
130
131   Item *left_bound = spanner->get_bound (LEFT);
132   Item *right_bound = spanner->get_bound (RIGHT);
133
134   Grob *commonx = left_bound->common_refpoint (right_bound, X_AXIS);
135
136   Link_array<Grob> const &cols = extract_grob_array (me, "columns");
137   if (cols.is_empty ())
138     {
139       me->warning (_ ("junking empty cluster"));
140       me->suicide ();
141
142       return SCM_EOL;
143     }
144
145   commonx = common_refpoint_of_array (cols, commonx, X_AXIS);
146   Grob *commony = common_refpoint_of_array (cols, me, Y_AXIS);
147   Array<Offset> bottom_points;
148   Array<Offset> top_points;
149
150   Real left_coord = left_bound->relative_coordinate (commonx, X_AXIS);
151
152   /*
153     TODO: should we move the cluster a little to the right to be in
154     line with the center of the note heads?
155
156   */
157   for (int i = 0; i < cols.size (); i++)
158     {
159       Grob *col = cols[i];
160
161       Interval yext = col->extent (commony, Y_AXIS);
162
163       Real x = col->relative_coordinate (commonx, X_AXIS) - left_coord;
164       bottom_points.push (Offset (x, yext[DOWN]));
165       top_points.push (Offset (x, yext[UP]));
166     }
167
168   /*
169     Across a line break we anticipate on the next pitches.
170   */
171   if (spanner->original ())
172     {
173       Spanner *orig = dynamic_cast<Spanner *> (spanner->original ());
174
175       if (spanner->get_break_index () < orig->broken_intos_.size () - 1)
176         {
177           Spanner *next = orig->broken_intos_[spanner->get_break_index () + 1];
178           Link_array<Grob> const &next_cols = extract_grob_array (next, "columns");
179           if (next_cols.size () > 0)
180             {
181               Grob *next_commony = common_refpoint_of_array (next_cols, next, Y_AXIS);
182               Grob *col = next_cols[0];
183
184               Interval v = col->extent (next_commony, Y_AXIS);
185               Real x = right_bound->relative_coordinate (commonx, X_AXIS) - left_coord;
186
187               bottom_points.insert (Offset (x, v[DOWN]), 0);
188               top_points.insert (Offset (x, v[UP]), 0);
189             }
190         }
191     }
192
193   bottom_points.reverse ();
194   top_points.reverse ();
195
196   Stencil out = brew_cluster_piece (me, bottom_points, top_points);
197   out.translate_axis (- me->relative_coordinate (commony, Y_AXIS), Y_AXIS);
198   return out.smobbed_copy ();
199 }
200
201 ADD_INTERFACE (Cluster, "cluster-interface",
202                "A graphically drawn musical cluster. "
203                "\n\n"
204                "@code{padding} adds to the vertical extent of the shape (top and "
205                "bottom). \n\n"
206                "The property @code{style} controls the shape of cluster segments.  Valid values "
207                "include @code{leftsided-stairs}, @code{rightsided-stairs}, @code{centered-stairs}, "
208                "and @code{ramp}.\n",
209                "style padding columns");
210
211 struct Cluster_beacon
212 {
213 public:
214   DECLARE_SCHEME_CALLBACK (height, (SCM));
215   static bool has_interface (Grob *);
216 };
217
218 MAKE_SCHEME_CALLBACK (Cluster_beacon, height, 1);
219 SCM
220 Cluster_beacon::height (SCM g)
221 {
222   Grob *me = unsmob_grob (g);
223   Interval v = robust_scm2interval (me->get_property ("positions"),
224                                     Interval (0, 0));
225   return ly_interval2scm (Staff_symbol_referencer::staff_space (me) * 0.5 * v);
226 }
227
228 ADD_INTERFACE (Cluster_beacon,
229                "cluster-beacon-interface",
230                "A place holder for the cluster spanner to determine the vertical "
231                "extents of a cluster spanner at this X position.",
232
233                /* properties */
234                "positions ");