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