]> git.donarmstrong.com Git - lilypond.git/blob - lily/cluster.cc
424e1d248bd8057cf400bd5b49d3df6ac880288b
[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--2003 Juergen Reuter <reuter@ipd.uka.de>
7
8   Han-Wen Nienhuys <hanwen@cs.uu.nl>
9
10 */
11
12 #include <stdio.h>
13
14 #include "cluster.hh"
15 #include "spanner.hh"
16 #include "item.hh"
17 #include "pitch.hh"
18 #include "staff-symbol-referencer.hh"
19 #include "lookup.hh"
20 #include "box.hh"
21 #include "interval.hh"
22 #include "paper-def.hh"
23 #include "warn.hh"
24
25
26 /*
27    TODO: Add support for cubic spline segments.
28
29  */
30 Molecule
31 brew_cluster_piece (Grob *me, Array<Offset> bottom_points, Array<Offset> top_points)
32 {
33 #if 0
34   Real blotdiameter = me->get_paper ()->get_realvar (ly_symbol2scm ("blotdiameter"));
35 #else
36   Real blotdiameter = Staff_symbol_referencer::staff_space (me)/2;
37 #endif
38
39   Real padding;
40   SCM padding_scm = me->get_grob_property ("padding");
41   if (gh_number_p (padding_scm))
42     padding = gh_scm2double (padding_scm);
43   else
44     padding = 0.0;
45   Offset vpadding = Offset (0, padding);
46   Offset hpadding = Offset (0.5 * blotdiameter, 0);
47   Offset hvpadding = 0.5 * hpadding + vpadding;
48
49   SCM shape_scm = me->get_grob_property ("style");
50   String shape;
51
52   if (gh_symbol_p (shape_scm))
53     {
54       shape = ly_symbol2string (shape_scm);
55     }
56   else
57     {
58       programming_error ("#'style should be symbol.");
59       me->suicide();
60       return  Molecule();
61     }
62
63
64   Molecule out = Molecule ();
65   Array<Offset> points;
66   points.clear ();
67   int size = bottom_points.size ();
68   if (String::compare (shape, "leftsided-stairs") == 0)
69     {
70       for (int i = 0; i < size - 1; i++)
71         {
72           Box box;
73           box.add_point (bottom_points[i] - hvpadding);
74           box.add_point (Offset(top_points[i + 1][X_AXIS],
75                                 top_points[i][Y_AXIS]) + hvpadding);
76           out.add_molecule (Lookup::round_filled_box (box, blotdiameter));
77         }
78     }
79   else if (String::compare (shape, "rightsided-stairs") == 0)
80     {
81       for (int i = 0; i < size - 1; i++)
82         {
83           Box box;
84           box.add_point (Offset(bottom_points[i][X_AXIS],
85                                 bottom_points[i + 1][Y_AXIS]) - hvpadding);
86           box.add_point (top_points[i + 1] + hvpadding);
87           out.add_molecule (Lookup::round_filled_box (box, blotdiameter));
88         }
89     }
90   else if (String::compare (shape, "centered-stairs") == 0)
91     {
92       Real left_xmid = bottom_points[0][X_AXIS];
93       for (int i = 0; i < size - 1; i++)
94         {
95           Real right_xmid =
96             0.5 * (bottom_points[i][X_AXIS] + bottom_points[i + 1][X_AXIS]);
97           Box box;
98           box.add_point (Offset (left_xmid, bottom_points[i][Y_AXIS]) -
99                          hvpadding);
100           box.add_point (Offset (right_xmid, top_points[i][Y_AXIS]) +
101                          hvpadding);
102           out.add_molecule (Lookup::round_filled_box (box, blotdiameter));
103           left_xmid = right_xmid;
104         }
105       Real right_xmid = bottom_points[size - 1][X_AXIS];
106       Box box;
107       box.add_point (Offset (left_xmid, bottom_points[size - 1][Y_AXIS]) -
108                      hvpadding);
109       box.add_point (Offset (right_xmid, top_points[size - 1][Y_AXIS]) +
110                      hvpadding);
111       out.add_molecule (Lookup::round_filled_box (box, blotdiameter));
112     }
113   else if (String::compare (shape, "ramp") == 0)
114     {
115       points.push (bottom_points[0] - vpadding + hpadding);
116       for (int i = 1; i < size - 1; i++)
117         {
118           points.push (bottom_points[i] - vpadding);
119         }
120       points.push (bottom_points[size - 1] - vpadding - hpadding);
121       points.push (top_points[size - 1] + vpadding - hpadding);
122       for (int i = size - 2; i > 0; i--)
123         {
124           points.push (top_points[i] + vpadding);
125         }
126       points.push (top_points[0] + vpadding + hpadding);
127       out.add_molecule (Lookup::round_filled_polygon (points, blotdiameter));
128     }
129   else
130     {
131       me->warning (_f ("unknown cluster style `%s'", shape.to_str0 ()));
132     }
133   return out;
134 }
135
136 MAKE_SCHEME_CALLBACK (Cluster,brew_molecule,1);
137 SCM
138 Cluster::brew_molecule (SCM smob)
139 {
140   Grob *me = unsmob_grob (smob);
141
142   Spanner *spanner = dynamic_cast<Spanner*> (me);
143   if (!spanner)
144     {
145       me->programming_error ("Cluster::brew_molecule(): not a spanner");
146       return SCM_EOL;
147     }
148
149   Item *left_bound = spanner->get_bound (LEFT);
150   Item *right_bound = spanner->get_bound (RIGHT);
151
152   Grob *common = left_bound->common_refpoint (right_bound, X_AXIS);
153   SCM cols  =me->get_grob_property ("columns");
154
155   if (!gh_pair_p (cols))
156     {
157       me->warning ("junking empty cluster");
158       me->suicide ();
159       
160       return SCM_EOL;
161     }
162   common = common_refpoint_of_list (cols, common, X_AXIS);
163   Array<Offset> bottom_points;
164   Array<Offset> top_points;
165
166
167   Real left_coord = left_bound->relative_coordinate (common, X_AXIS);
168
169   Real unit = Staff_symbol_referencer::staff_space (me) *0.5;
170
171   /*
172     TODO: should we move the cluster a little to the right to be in
173     line with the center of the note heads?
174     
175    */
176   for (SCM s = cols; gh_pair_p (s); s = ly_cdr (s))
177     {
178       Grob * col = unsmob_grob (ly_car (s));
179
180       SCM posns = col->get_grob_property ("positions");
181       
182       Slice s (0,0);
183       if (ly_number_pair_p (posns))
184         s = Slice (gh_scm2int (gh_car (posns)),
185                    gh_scm2int (gh_cdr (posns)));
186
187       Real x = col->relative_coordinate (common, X_AXIS) - left_coord;
188       bottom_points.push (Offset (x, s[DOWN] *unit));
189       top_points.push (Offset (x, s[UP] * unit));
190     }
191
192   /*
193     Across a line break we anticipate on the next pitches.
194    */
195   if (spanner->original_)
196     {
197       Spanner *orig = dynamic_cast<Spanner*> (spanner->original_);
198       
199       if (spanner->break_index_ < orig->broken_intos_.size()-1)
200         {
201           Spanner * next = orig->broken_intos_[spanner->break_index_+1];
202           SCM cols = next->get_grob_property ("columns");
203           if (gh_pair_p (cols))
204             {
205               Grob * col = unsmob_grob (ly_car (scm_last_pair (cols)));
206               SCM posns = col->get_grob_property ("positions");
207       
208               Slice s (0,0);
209               if (ly_number_pair_p (posns))
210                 s = Slice (gh_scm2int (gh_car (posns)),
211                            gh_scm2int (gh_cdr (posns)));
212
213               Real x = right_bound->relative_coordinate (common,X_AXIS) - left_coord;
214               
215               bottom_points.insert (Offset (x, s[DOWN] * unit),0);
216               top_points.insert (Offset (x, s[UP] * unit),0);
217             }
218         }
219     }
220
221   bottom_points.reverse ();
222   top_points.reverse ();
223
224   Molecule out = brew_cluster_piece (me, bottom_points, top_points);
225   return out.smobbed_copy ();
226 }
227
228 ADD_INTERFACE (Cluster,"cluster-interface",
229   "A graphically drawn musical cluster. " 
230 "\n\n"
231 "@code{padding} adds to the vertical extent of the shape (top and "
232 "bottom) and is expressed in units of staffspace.  Since the pitch "
233 "range of a single pitch is infinitely small, if padding is set to "
234 "@code{0.0}, this possibly results in an invisible shape, if you,for "
235 "example, say @code{c-\\startCluster d e-\\endCluster}.  The default "
236 "value for @code{padding} therefore is @code{0.25}, such that a single "
237 "pitch roughly shows the same height as a note head. "
238 "\n\n"
239 "@code{style} controls the shape of cluster segments.  Valid values include 'leftsided-stairs', 'rightsided-stairs', 'centered-stairs', and 'ramp'.\n"
240 ,
241   "style padding columns");