]> git.donarmstrong.com Git - lilypond.git/blob - lily/cluster.cc
Merge branch 'jneeman' of git+ssh://jneem@git.sv.gnu.org/srv/git/lilypond into jneeman
[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->original ())
168     {
169       Spanner *orig = dynamic_cast<Spanner *> (spanner->original ());
170
171       if (spanner->get_break_index () < orig->broken_intos_.size () - 1)
172         {
173           Spanner *next = orig->broken_intos_[spanner->get_break_index () + 1];
174           vector<Grob*> const &next_cols = extract_grob_array (next, "columns");
175           if (next_cols.size () > 0)
176             {
177               Grob *next_commony = common_refpoint_of_array (next_cols, next, Y_AXIS);
178               Grob *col = next_cols[0];
179
180               Interval v = col->extent (next_commony, Y_AXIS);
181               Real x = right_bound->relative_coordinate (commonx, X_AXIS) - left_coord;
182
183               bottom_points.insert (bottom_points.begin (),
184                                     Offset (x, v[DOWN]));
185               top_points.insert (top_points.begin (), Offset (x, v[UP]));
186             }
187         }
188     }
189
190   reverse (bottom_points);
191   reverse (top_points);
192
193   Stencil out = brew_cluster_piece (me, bottom_points, top_points);
194   out.translate_axis (- me->relative_coordinate (commony, Y_AXIS), Y_AXIS);
195   return out.smobbed_copy ();
196 }
197
198 ADD_INTERFACE (Cluster,
199                "A graphically drawn musical cluster. "
200                "\n\n"
201                "@code{padding} adds to the vertical extent of the shape (top and "
202                "bottom). \n\n"
203                "The property @code{style} controls the shape of cluster segments.  Valid values "
204                "include @code{leftsided-stairs}, @code{rightsided-stairs}, @code{centered-stairs}, "
205                "and @code{ramp}.\n",
206                "style "
207                "padding "
208                "columns ");
209
210 struct Cluster_beacon
211 {
212 public:
213   DECLARE_SCHEME_CALLBACK (height, (SCM));
214   DECLARE_GROB_INTERFACE();
215 };
216
217 MAKE_SCHEME_CALLBACK (Cluster_beacon, height, 1);
218 SCM
219 Cluster_beacon::height (SCM g)
220 {
221   Grob *me = unsmob_grob (g);
222   Interval v = robust_scm2interval (me->get_property ("positions"),
223                                     Interval (0, 0));
224   return ly_interval2scm (Staff_symbol_referencer::staff_space (me) * 0.5 * v);
225 }
226
227 ADD_INTERFACE (Cluster_beacon,
228                "A place holder for the cluster spanner to determine the vertical "
229                "extents of a cluster spanner at this X position.",
230
231                /* properties */
232                "positions ");