]> git.donarmstrong.com Git - lilypond.git/blob - lily/cluster.cc
Fix #210 & #211.
[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 #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, print, 1);
115 SCM
116 Cluster::print (SCM smob)
117 {
118   Grob *me = unsmob_grob (smob);
119
120   Spanner *spanner = dynamic_cast<Spanner *> (me);
121   if (!spanner)
122     {
123       me->programming_error ("Cluster::print (): not a spanner");
124       return SCM_EOL;
125     }
126
127   Item *left_bound = spanner->get_bound (LEFT);
128   Item *right_bound = spanner->get_bound (RIGHT);
129
130   Grob *commonx = left_bound->common_refpoint (right_bound, X_AXIS);
131
132   vector<Grob*> const &cols = extract_grob_array (me, "columns");
133   if (cols.empty ())
134     {
135       me->warning (_ ("junking empty cluster"));
136       me->suicide ();
137
138       return SCM_EOL;
139     }
140
141   commonx = common_refpoint_of_array (cols, commonx, X_AXIS);
142   Grob *commony = common_refpoint_of_array (cols, me, Y_AXIS);
143   vector<Offset> bottom_points;
144   vector<Offset> top_points;
145
146   Real left_coord = left_bound->relative_coordinate (commonx, X_AXIS);
147
148   /*
149     TODO: should we move the cluster a little to the right to be in
150     line with the center of the note heads?
151
152   */
153   for (vsize i = 0; i < cols.size (); i++)
154     {
155       Grob *col = cols[i];
156
157       Interval yext = col->extent (commony, Y_AXIS);
158
159       Real x = col->relative_coordinate (commonx, X_AXIS) - left_coord;
160       bottom_points.push_back (Offset (x, yext[DOWN]));
161       top_points.push_back (Offset (x, yext[UP]));
162     }
163
164   /*
165     Across a line break we anticipate on the next pitches.
166   */
167   if (Spanner *next = spanner->broken_neighbor (RIGHT))
168     {
169       extract_grob_set (next, "columns", next_cols);
170       if (next_cols.size () > 0)
171         {
172           Grob *next_commony = common_refpoint_of_array (next_cols, next, Y_AXIS);
173           Grob *col = next_cols[0];
174
175           Interval v = col->extent (next_commony, Y_AXIS);
176           Real x = right_bound->relative_coordinate (commonx, X_AXIS) - left_coord;
177
178           bottom_points.push_back (Offset (x, v[DOWN]));
179           top_points.push_back (Offset (x, v[UP]));
180         }
181     }
182
183   Stencil out = brew_cluster_piece (me, bottom_points, top_points);
184   out.translate_axis (- me->relative_coordinate (commony, Y_AXIS), Y_AXIS);
185   return out.smobbed_copy ();
186 }
187
188 ADD_INTERFACE (Cluster,
189                "A graphically drawn musical cluster. "
190                "\n\n"
191                "@code{padding} adds to the vertical extent of the shape (top and "
192                "bottom). \n\n"
193                "The property @code{style} controls the shape of cluster segments.  Valid values "
194                "include @code{leftsided-stairs}, @code{rightsided-stairs}, @code{centered-stairs}, "
195                "and @code{ramp}.\n",
196                "style "
197                "padding "
198                "columns ");
199
200 struct Cluster_beacon
201 {
202 public:
203   DECLARE_SCHEME_CALLBACK (height, (SCM));
204   DECLARE_GROB_INTERFACE();
205 };
206
207 MAKE_SCHEME_CALLBACK (Cluster_beacon, height, 1);
208 SCM
209 Cluster_beacon::height (SCM g)
210 {
211   Grob *me = unsmob_grob (g);
212   Interval v = robust_scm2interval (me->get_property ("positions"),
213                                     Interval (0, 0));
214   return ly_interval2scm (Staff_symbol_referencer::staff_space (me) * 0.5 * v);
215 }
216
217 ADD_INTERFACE (Cluster_beacon,
218                "A place holder for the cluster spanner to determine the vertical "
219                "extents of a cluster spanner at this X position.",
220
221                /* properties */
222                "positions ");