]> git.donarmstrong.com Git - lilypond.git/blob - lily/cluster.cc
Run `make grand-replace'.
[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--2008 Juergen Reuter <reuter@ipd.uka.de>
7
8   Han-Wen Nienhuys <hanwen@xs4all.nl>
9 */
10
11 #include "cluster.hh"
12 #include "international.hh"
13 #include "item.hh"
14 #include "lookup.hh"
15 #include "output-def.hh"
16 #include "pitch.hh"
17 #include "pointer-group-interface.hh"
18 #include "spanner.hh"
19 #include "staff-symbol-referencer.hh"
20 #include "warn.hh"
21
22 /*
23   TODO: Add support for cubic spline segments.
24  */
25 Stencil
26 brew_cluster_piece (Grob *me, vector<Offset> bottom_points, vector<Offset> top_points)
27 {
28   Real blotdiameter = Staff_symbol_referencer::staff_space (me) / 2;
29
30   Real padding = robust_scm2double (me->get_property ("padding"), 0.0);
31
32   Offset vpadding = Offset (0, padding);
33   Offset hpadding = Offset (0.5 * blotdiameter, 0);
34   Offset hvpadding = 0.5 * hpadding + vpadding;
35
36   SCM shape_scm = me->get_property ("style");
37   string shape;
38
39   if (scm_is_symbol (shape_scm))
40     shape = ly_symbol2string (shape_scm);
41   else
42     {
43       programming_error ("#'style should be symbol.");
44       me->suicide ();
45       return Stencil ();
46     }
47
48   Stencil out;
49   vector<Offset> points;
50   points.clear ();
51   int size = bottom_points.size ();
52   if (shape == "leftsided-stairs")
53     {
54       for (int i = 0; i < size - 1; i++)
55         {
56           Box box;
57           box.add_point (bottom_points[i] - hvpadding);
58           box.add_point (Offset (top_points[i + 1][X_AXIS],
59                                  top_points[i][Y_AXIS]) + hvpadding);
60           out.add_stencil (Lookup::round_filled_box (box, blotdiameter));
61         }
62     }
63   else if (shape == "rightsided-stairs")
64     {
65       for (int i = 0; i < size - 1; i++)
66         {
67           Box box;
68           box.add_point (Offset (bottom_points[i][X_AXIS],
69                                  bottom_points[i + 1][Y_AXIS]) - hvpadding);
70           box.add_point (top_points[i + 1] + hvpadding);
71           out.add_stencil (Lookup::round_filled_box (box, blotdiameter));
72         }
73     }
74   else if (shape == "centered-stairs")
75     {
76       Real left_xmid = bottom_points[0][X_AXIS];
77       for (int i = 0; i < size - 1; i++)
78         {
79           Real right_xmid
80             = 0.5 * (bottom_points[i][X_AXIS] + bottom_points[i + 1][X_AXIS]);
81           Box box;
82           box.add_point (Offset (left_xmid, bottom_points[i][Y_AXIS])
83                          - hvpadding);
84           box.add_point (Offset (right_xmid, top_points[i][Y_AXIS])
85                          + hvpadding);
86           out.add_stencil (Lookup::round_filled_box (box, blotdiameter));
87           left_xmid = right_xmid;
88         }
89       Real right_xmid = bottom_points[size - 1][X_AXIS];
90       Box box;
91       box.add_point (Offset (left_xmid, bottom_points[size - 1][Y_AXIS])
92                      - hvpadding);
93       box.add_point (Offset (right_xmid, top_points[size - 1][Y_AXIS])
94                      + hvpadding);
95       out.add_stencil (Lookup::round_filled_box (box, blotdiameter));
96     }
97   else if (shape == "ramp")
98     {
99       points.push_back (bottom_points[0] - vpadding + hpadding);
100       for (int i = 1; i < size - 1; i++)
101         points.push_back (bottom_points[i] - vpadding);
102       points.push_back (bottom_points[size - 1] - vpadding - hpadding);
103       points.push_back (top_points[size - 1] + vpadding - hpadding);
104       for (int i = size - 2; i > 0; i--)
105         points.push_back (top_points[i] + vpadding);
106       points.push_back (top_points[0] + vpadding + hpadding);
107       out.add_stencil (Lookup::round_filled_polygon (points, blotdiameter));
108     }
109   else
110     me->warning (_f ("unknown cluster style `%s'", shape.c_str ()));
111   return out;
112 }
113
114 MAKE_SCHEME_CALLBACK (Cluster, calc_cross_staff, 1);
115 SCM
116 Cluster::calc_cross_staff (SCM smob)
117 {
118   Grob *me = unsmob_grob (smob);
119
120   extract_grob_set (me, "columns", cols);
121   Grob *commony = common_refpoint_of_array (cols, me, Y_AXIS);
122
123   return scm_from_bool (commony != me->get_parent (Y_AXIS));
124 }
125
126 MAKE_SCHEME_CALLBACK (Cluster, print, 1);
127 SCM
128 Cluster::print (SCM smob)
129 {
130   Grob *me = unsmob_grob (smob);
131
132   Spanner *spanner = dynamic_cast<Spanner *> (me);
133   if (!spanner)
134     {
135       me->programming_error ("Cluster::print (): not a spanner");
136       return SCM_EOL;
137     }
138
139   Item *left_bound = spanner->get_bound (LEFT);
140   Item *right_bound = spanner->get_bound (RIGHT);
141
142   Grob *commonx = left_bound->common_refpoint (right_bound, X_AXIS);
143
144   vector<Grob*> const &cols = extract_grob_array (me, "columns");
145   if (cols.empty ())
146     {
147       me->warning (_ ("junking empty cluster"));
148       me->suicide ();
149
150       return SCM_EOL;
151     }
152
153   commonx = common_refpoint_of_array (cols, commonx, X_AXIS);
154   Grob *commony = common_refpoint_of_array (cols, me, Y_AXIS);
155   vector<Offset> bottom_points;
156   vector<Offset> top_points;
157
158   Real left_coord = left_bound->relative_coordinate (commonx, X_AXIS);
159
160   /*
161     TODO: should we move the cluster a little to the right to be in
162     line with the center of the note heads?
163
164   */
165   for (vsize i = 0; i < cols.size (); i++)
166     {
167       Grob *col = cols[i];
168
169       Interval yext = col->extent (commony, Y_AXIS);
170
171       Real x = col->relative_coordinate (commonx, X_AXIS) - left_coord;
172       bottom_points.push_back (Offset (x, yext[DOWN]));
173       top_points.push_back (Offset (x, yext[UP]));
174     }
175
176   /*
177     Across a line break we anticipate on the next pitches.
178   */
179   if (Spanner *next = spanner->broken_neighbor (RIGHT))
180     {
181       extract_grob_set (next, "columns", next_cols);
182       if (next_cols.size () > 0)
183         {
184           Grob *next_commony = common_refpoint_of_array (next_cols, next, Y_AXIS);
185           Grob *col = next_cols[0];
186
187           Interval v = col->extent (next_commony, Y_AXIS);
188           Real x = right_bound->relative_coordinate (commonx, X_AXIS) - left_coord;
189
190           bottom_points.push_back (Offset (x, v[DOWN]));
191           top_points.push_back (Offset (x, v[UP]));
192         }
193     }
194
195   Stencil out = brew_cluster_piece (me, bottom_points, top_points);
196   out.translate_axis (- me->relative_coordinate (commony, Y_AXIS), Y_AXIS);
197   return out.smobbed_copy ();
198 }
199
200 ADD_INTERFACE (Cluster,
201                "A graphically drawn musical cluster.\n"
202                "\n"
203                "@code{padding} adds to the vertical extent of the shape (top"
204                " and bottom).\n"
205                "\n"
206                "The property @code{style} controls the shape of cluster"
207                " segments.  Valid values include @code{leftsided-stairs},"
208                " @code{rightsided-stairs}, @code{centered-stairs}, and"
209                " @code{ramp}.\n",
210
211                /* properties */
212                "style "
213                "padding "
214                "columns "
215                );
216
217 struct Cluster_beacon
218 {
219 public:
220   DECLARE_SCHEME_CALLBACK (height, (SCM));
221   DECLARE_GROB_INTERFACE ();
222 };
223
224 MAKE_SCHEME_CALLBACK (Cluster_beacon, height, 1);
225 SCM
226 Cluster_beacon::height (SCM g)
227 {
228   Grob *me = unsmob_grob (g);
229   Interval v = robust_scm2interval (me->get_property ("positions"),
230                                     Interval (0, 0));
231   return ly_interval2scm (Staff_symbol_referencer::staff_space (me) * 0.5 * v);
232 }
233
234 ADD_INTERFACE (Cluster_beacon,
235                "A place holder for the cluster spanner to determine the"
236                " vertical extents of a cluster spanner at this"
237                " X@tie{}position.",
238
239                /* properties */
240                "positions "
241                );