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