]> git.donarmstrong.com Git - lilypond.git/blob - lily/cluster.cc
(warning): better robustness fix.
[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 Juergen Reuter <reuter@ipd.uka.de>
7 */
8
9 #include <stdio.h>
10 #include "cluster.hh"
11 #include "grob.hh"
12 #include "spanner.hh"
13 #include "item.hh"
14 #include "pitch.hh"
15 #include "staff-symbol-referencer.hh"
16 #include "lookup.hh"
17 #include "box.hh"
18 #include "interval.hh"
19 #include "paper-def.hh"
20 #include "paper-column.hh"
21
22 /*
23  * TODO: Add support for cubic spline segments.
24  */
25 Molecule
26 brew_cluster_piece (Grob *me, Array<Offset> bottom_points, Array<Offset> top_points)
27 {
28 #if 0
29   Real blotdiameter = me->get_paper ()->get_var ("blotdiameter");
30 #else
31   Real blotdiameter = Staff_symbol_referencer::staff_space (me)/2;
32 #endif
33
34   Real padding;
35   SCM padding_scm = me->get_grob_property ("padding");
36   if (gh_number_p (padding_scm))
37     padding = gh_scm2double (padding_scm);
38   else
39     padding = 0.0;
40   Offset vpadding = Offset (0, padding);
41   Offset hpadding = Offset (0.5 * blotdiameter, 0);
42   Offset hvpadding = 0.5 * hpadding + vpadding;
43
44   SCM shape_scm = me->get_grob_property ("shape");
45   String shape;
46   if (gh_symbol_p (shape_scm))
47     {
48       shape = ly_symbol2string (shape_scm);
49     }
50   else
51     {
52       shape = "leftsided-stairs";
53     }
54
55
56   Molecule out = Molecule ();
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_molecule (Lookup::roundfilledbox (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_molecule (Lookup::roundfilledbox (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_molecule (Lookup::roundfilledbox (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_molecule (Lookup::roundfilledbox (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_molecule (Lookup::round_filled_polygon (points, blotdiameter));
120     }
121   else
122     {
123       me->warning (_f ("unknown cluster shape `%s'", shape.to_str0 ()));
124     }
125   return out;
126 }
127
128 MAKE_SCHEME_CALLBACK (Cluster,brew_molecule,1);
129 SCM
130 Cluster::brew_molecule (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::brew_molecule(): 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   bool right_broken = right_bound->break_status_dir () != CENTER;
144
145   Grob *common = left_bound->common_refpoint (right_bound, X_AXIS);
146
147   Grob *column = 0;
148   Array<Offset> bottom_points;
149   Array<Offset> top_points;
150   bottom_points.clear ();
151   top_points.clear ();
152   SCM column_scm = SCM_EOL;
153
154   SCM columns_scm = me->get_grob_property ("segments");
155   if (columns_scm == SCM_EOL)
156     {
157       me->warning ("junking empty cluster");
158       return SCM_EOL;
159     }
160
161   for (;
162        columns_scm != SCM_EOL;
163        columns_scm = ly_cdr (columns_scm)) {
164     column_scm = ly_car (columns_scm);
165     SCM col_scm = ly_car (column_scm);
166     if (gh_number_p (col_scm))
167       // broken spanner: this column not in this piece
168       if (!column)
169         continue; // still have to expect columns
170       else
171         break; // ok, we have seen all columns
172     column = unsmob_grob (col_scm);
173     column_scm = ly_cdr (column_scm);
174     Real y_bottom = gh_scm2double (ly_car (column_scm));
175     column_scm = ly_cdr (column_scm);
176     Real y_top = gh_scm2double (ly_car (column_scm));
177     Real x = column->relative_coordinate (common, X_AXIS);
178     if (right_broken)
179       x -= left_bound->relative_coordinate (common, X_AXIS);
180     bottom_points.push (Offset (x, y_bottom));
181     top_points.push (Offset (x, y_top));
182   }
183   if (right_broken)
184     {
185       Real y_bottom = gh_scm2double (ly_car (column_scm));
186       column_scm = ly_cdr (column_scm);
187       Real y_top = gh_scm2double (ly_car (column_scm));
188       column_scm = ly_cdr (column_scm);
189       Real x =
190         right_bound->relative_coordinate (common, X_AXIS) -
191         left_bound->relative_coordinate (common, X_AXIS);
192       bottom_points.push (Offset (x, y_bottom));
193       top_points.push (Offset (x, y_top));
194     }
195   Molecule out = brew_cluster_piece (me, bottom_points, top_points);
196   return out.smobbed_copy ();
197 }
198
199 ADD_INTERFACE (Cluster,"cluster-interface",
200   "A graphically drawn musical cluster.",
201   "shape padding segments");