]> git.donarmstrong.com Git - lilypond.git/blob - lily/cluster.cc
f487cfb265a21f2371d8043ff48b8b4015e4a0f6
[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     me->programming_error ("Cluster::brew_molecule(): 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   bool right_broken = right_bound->break_status_dir () != CENTER;
143
144   Grob *common = left_bound->common_refpoint (right_bound, X_AXIS);
145
146   Grob *column = 0;
147   Array<Offset> bottom_points;
148   Array<Offset> top_points;
149   bottom_points.clear ();
150   top_points.clear ();
151   SCM column_scm = SCM_EOL;
152   for (SCM columns_scm = me->get_grob_property ("segments");
153        columns_scm != SCM_EOL;
154        columns_scm = ly_cdr (columns_scm)) {
155     column_scm = ly_car (columns_scm);
156     SCM col_scm = ly_car (column_scm);
157     if (gh_number_p (col_scm))
158       // broken spanner: this column not in this piece
159       if (!column)
160         continue; // still have to expect columns
161       else
162         break; // ok, we have seen all columns
163     column = unsmob_grob (col_scm);
164     column_scm = ly_cdr (column_scm);
165     Real y = 0.5 * gh_scm2double (ly_car (column_scm));
166     column_scm = ly_cdr (column_scm);
167     Pitch *pitch_min = unsmob_pitch (ly_car (column_scm));
168     column_scm = ly_cdr (column_scm);
169     Pitch *pitch_max = unsmob_pitch (ly_car (column_scm));
170     Real height = 0.5 * (pitch_max->steps () - pitch_min->steps ());
171     Real x = column->relative_coordinate (common, X_AXIS);
172     if (right_broken)
173       x -= left_bound->relative_coordinate (common, X_AXIS);
174     bottom_points.push (Offset (x, y));
175     top_points.push (Offset (x, y + height));
176   }
177   if (right_broken)
178     {
179       Real y = 0.5 * gh_scm2double (ly_car (column_scm));
180       column_scm = ly_cdr (column_scm);
181       Pitch *pitch_min = unsmob_pitch (ly_car (column_scm));
182       column_scm = ly_cdr (column_scm);
183       Pitch *pitch_max = unsmob_pitch (ly_car (column_scm));
184       Real height = 0.5 * (pitch_max->steps () - pitch_min->steps ());
185       Real x =
186         right_bound->relative_coordinate (common, X_AXIS) -
187         left_bound->relative_coordinate (common, X_AXIS);
188       bottom_points.push (Offset (x, y));
189       top_points.push (Offset (x, y + height));
190     }
191   Molecule out = brew_cluster_piece (me, bottom_points, top_points);
192   return out.smobbed_copy ();
193 }
194
195 ADD_INTERFACE (Cluster,"cluster-interface",
196   "A graphically drawn musical cluster.",
197   "shape padding segments");