]> git.donarmstrong.com Git - lilypond.git/blob - lily/cluster.cc
Issue 4550 (1/2) Avoid "using namespace std;" in included files
[lilypond.git] / lily / cluster.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2002--2015 Juergen Reuter <reuter@ipd.uka.de>
5   Han-Wen Nienhuys <hanwen@xs4all.nl>
6
7   LilyPond is free software: you can redistribute it and/or modify
8   it under the terms of the GNU General Public License as published by
9   the Free Software Foundation, either version 3 of the License, or
10   (at your option) any later version.
11
12   LilyPond is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   GNU General Public License for more details.
16
17   You should have received a copy of the GNU General Public License
18   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "cluster.hh"
22 #include "international.hh"
23 #include "item.hh"
24 #include "lookup.hh"
25 #include "output-def.hh"
26 #include "pitch.hh"
27 #include "pointer-group-interface.hh"
28 #include "spanner.hh"
29 #include "staff-symbol-referencer.hh"
30 #include "warn.hh"
31
32 using std::string;
33 using std::vector;
34
35 /*
36   TODO: Add support for cubic spline segments.
37  */
38 Stencil
39 brew_cluster_piece (Grob *me, vector<Offset> bottom_points, vector<Offset> top_points)
40 {
41   Real blotdiameter = Staff_symbol_referencer::staff_space (me) / 2;
42
43   Real padding = robust_scm2double (me->get_property ("padding"), 0.0);
44
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_property ("style");
50   string shape;
51
52   if (scm_is_symbol (shape_scm))
53     shape = ly_symbol2string (shape_scm);
54   else
55     {
56       programming_error ("#'style should be symbol.");
57       me->suicide ();
58       return Stencil ();
59     }
60
61   Stencil out;
62   vector<Offset> points;
63   points.clear ();
64   int size = bottom_points.size ();
65   if (shape == "leftsided-stairs")
66     {
67       for (int i = 0; i < size - 1; i++)
68         {
69           Box box;
70           box.add_point (bottom_points[i] - hvpadding);
71           box.add_point (Offset (top_points[i + 1][X_AXIS],
72                                  top_points[i][Y_AXIS]) + hvpadding);
73           out.add_stencil (Lookup::round_filled_box (box, blotdiameter));
74         }
75     }
76   else if (shape == "rightsided-stairs")
77     {
78       for (int i = 0; i < size - 1; i++)
79         {
80           Box box;
81           box.add_point (Offset (bottom_points[i][X_AXIS],
82                                  bottom_points[i + 1][Y_AXIS]) - hvpadding);
83           box.add_point (top_points[i + 1] + hvpadding);
84           out.add_stencil (Lookup::round_filled_box (box, blotdiameter));
85         }
86     }
87   else if (shape == "centered-stairs")
88     {
89       Real left_xmid = bottom_points[0][X_AXIS];
90       for (int i = 0; i < size - 1; i++)
91         {
92           Real right_xmid
93             = 0.5 * (bottom_points[i][X_AXIS] + bottom_points[i + 1][X_AXIS]);
94           Box box;
95           box.add_point (Offset (left_xmid, bottom_points[i][Y_AXIS])
96                          - hvpadding);
97           box.add_point (Offset (right_xmid, top_points[i][Y_AXIS])
98                          + hvpadding);
99           out.add_stencil (Lookup::round_filled_box (box, blotdiameter));
100           left_xmid = right_xmid;
101         }
102       Real right_xmid = bottom_points[size - 1][X_AXIS];
103       Box box;
104       box.add_point (Offset (left_xmid, bottom_points[size - 1][Y_AXIS])
105                      - hvpadding);
106       box.add_point (Offset (right_xmid, top_points[size - 1][Y_AXIS])
107                      + hvpadding);
108       out.add_stencil (Lookup::round_filled_box (box, blotdiameter));
109     }
110   else if (shape == "ramp")
111     {
112       points.push_back (bottom_points[0] - vpadding + hpadding);
113       for (int i = 1; i < size - 1; i++)
114         points.push_back (bottom_points[i] - vpadding);
115       points.push_back (bottom_points[size - 1] - vpadding - hpadding);
116       points.push_back (top_points[size - 1] + vpadding - hpadding);
117       for (int i = size - 2; i > 0; i--)
118         points.push_back (top_points[i] + vpadding);
119       points.push_back (top_points[0] + vpadding + hpadding);
120       out.add_stencil (Lookup::round_filled_polygon (points, blotdiameter));
121     }
122   else
123     me->warning (_f ("unknown cluster style `%s'", shape.c_str ()));
124   return out;
125 }
126
127 MAKE_SCHEME_CALLBACK (Cluster, calc_cross_staff, 1);
128 SCM
129 Cluster::calc_cross_staff (SCM smob)
130 {
131   Grob *me = unsmob<Grob> (smob);
132
133   extract_grob_set (me, "columns", cols);
134   Grob *commony = common_refpoint_of_array (cols, me, Y_AXIS);
135
136   return scm_from_bool (commony != me->get_parent (Y_AXIS));
137 }
138
139 MAKE_SCHEME_CALLBACK (Cluster, print, 1);
140 SCM
141 Cluster::print (SCM smob)
142 {
143   Grob *me = unsmob<Grob> (smob);
144
145   Spanner *spanner = dynamic_cast<Spanner *> (me);
146   if (!spanner)
147     {
148       me->programming_error ("Cluster::print (): not a spanner");
149       return SCM_EOL;
150     }
151
152   Item *left_bound = spanner->get_bound (LEFT);
153   Item *right_bound = spanner->get_bound (RIGHT);
154
155   Grob *commonx = left_bound->common_refpoint (right_bound, X_AXIS);
156
157   vector<Grob *> const &cols = extract_grob_array (me, "columns");
158   if (cols.empty ())
159     {
160       me->warning (_ ("junking empty cluster"));
161       me->suicide ();
162
163       return SCM_EOL;
164     }
165
166   commonx = common_refpoint_of_array (cols, commonx, X_AXIS);
167   Grob *commony = common_refpoint_of_array (cols, me, Y_AXIS);
168   vector<Offset> bottom_points;
169   vector<Offset> top_points;
170
171   Real left_coord = left_bound->relative_coordinate (commonx, X_AXIS);
172
173   /*
174     TODO: should we move the cluster a little to the right to be in
175     line with the center of the note heads?
176
177   */
178   for (vsize i = 0; i < cols.size (); i++)
179     {
180       Grob *col = cols[i];
181
182       Interval yext = col->extent (commony, Y_AXIS);
183
184       Real x = col->relative_coordinate (commonx, X_AXIS) - left_coord;
185       bottom_points.push_back (Offset (x, yext[DOWN]));
186       top_points.push_back (Offset (x, yext[UP]));
187     }
188
189   /*
190     Across a line break we anticipate on the next pitches.
191   */
192   if (Spanner *next = spanner->broken_neighbor (RIGHT))
193     {
194       extract_grob_set (next, "columns", next_cols);
195       if (next_cols.size () > 0)
196         {
197           Grob *next_commony = common_refpoint_of_array (next_cols, next, Y_AXIS);
198           Grob *col = next_cols[0];
199
200           Interval v = col->extent (next_commony, Y_AXIS);
201           Real x = right_bound->relative_coordinate (commonx, X_AXIS) - left_coord;
202
203           bottom_points.push_back (Offset (x, v[DOWN]));
204           top_points.push_back (Offset (x, v[UP]));
205         }
206     }
207
208   Stencil out = brew_cluster_piece (me, bottom_points, top_points);
209   out.translate_axis (- me->relative_coordinate (commony, Y_AXIS), Y_AXIS);
210   return out.smobbed_copy ();
211 }
212
213 ADD_INTERFACE (Cluster,
214                "A graphically drawn musical cluster.\n"
215                "\n"
216                "@code{padding} adds to the vertical extent of the shape (top"
217                " and bottom).\n"
218                "\n"
219                "The property @code{style} controls the shape of cluster"
220                " segments.  Valid values include @code{leftsided-stairs},"
221                " @code{rightsided-stairs}, @code{centered-stairs}, and"
222                " @code{ramp}.\n",
223
224                /* properties */
225                "style "
226                "padding "
227                "columns "
228               );
229
230 struct Cluster_beacon
231 {
232 public:
233   DECLARE_SCHEME_CALLBACK (height, (SCM));
234 };
235
236 MAKE_SCHEME_CALLBACK (Cluster_beacon, height, 1);
237 SCM
238 Cluster_beacon::height (SCM g)
239 {
240   Grob *me = unsmob<Grob> (g);
241   Interval v = robust_scm2interval (me->get_property ("positions"),
242                                     Interval (0, 0));
243   return ly_interval2scm (Staff_symbol_referencer::staff_space (me) * 0.5 * v);
244 }
245
246 ADD_INTERFACE (Cluster_beacon,
247                "A place holder for the cluster spanner to determine the"
248                " vertical extents of a cluster spanner at this"
249                " X@tie{}position.",
250
251                /* properties */
252                "positions "
253               );