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