Use `transform'.
[lilypond.git] / lily / rest-collision.cc
blob063b773a92ae2f3ccf58792ba566095bf884b0a2
1 /*
2 rest-collision.cc -- implement Rest_collision
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
9 #include "rest-collision.hh"
11 #include <cmath> // ceil.
12 using namespace std;
14 #include "directional-element-interface.hh"
15 #include "duration.hh"
16 #include "international.hh"
17 #include "note-column.hh"
18 #include "output-def.hh"
19 #include "pointer-group-interface.hh"
20 #include "rest.hh"
21 #include "rhythmic-head.hh"
22 #include "staff-symbol-referencer.hh"
23 #include "stem.hh"
24 #include "grob.hh"
25 #include "warn.hh"
27 MAKE_SCHEME_CALLBACK (Rest_collision, force_shift_callback, 1);
28 SCM
29 Rest_collision::force_shift_callback (SCM smob)
31 Grob *them = unsmob_grob (smob);
32 if (Note_column::has_rests (them))
34 Grob *collision = unsmob_grob (them->get_object ("rest-collision"));
36 if (collision)
38 (void) collision->get_property ("positioning-done");
41 return scm_from_double (0.0);
44 MAKE_SCHEME_CALLBACK_WITH_OPTARGS (Rest_collision, force_shift_callback_rest, 2, 1, "");
45 SCM
46 Rest_collision::force_shift_callback_rest (SCM rest, SCM offset)
48 Grob *rest_grob = unsmob_grob (rest);
49 Grob *parent = rest_grob->get_parent (X_AXIS);
52 translate REST; we need the result of this translation later on,
53 while the offset probably still is 0/calculation-in-progress.
55 if (scm_is_number (offset))
56 rest_grob->translate_axis (scm_to_double (offset), Y_AXIS);
58 if (Note_column::has_interface (parent))
59 force_shift_callback (parent->self_scm ());
61 return scm_from_double (0.0);
64 void
65 Rest_collision::add_column (Grob *me, Grob *p)
67 Pointer_group_interface::add_grob (me, ly_symbol2scm ("elements"), p);
70 only add callback for the rests, since we don't move anything
71 else.
73 (not?)
75 add_offset_callback (p, Rest_collision::force_shift_callback_proc, Y_AXIS);
76 p->set_object ("rest-collision", me->self_scm ());
78 Grob *rest = unsmob_grob (p->get_object ("rest"));
79 if (rest)
81 chain_offset_callback (rest,
82 Rest_collision::force_shift_callback_rest_proc, Y_AXIS);
87 TODO: look at horizontal-shift to determine ordering between rests
88 for more than two voices.
90 MAKE_SCHEME_CALLBACK (Rest_collision, calc_positioning_done, 1);
91 SCM
92 Rest_collision::calc_positioning_done (SCM smob)
94 Grob *me = unsmob_grob (smob);
96 me->set_property ("positioning-done", SCM_BOOL_T);
98 extract_grob_set (me, "elements", elts);
100 vector<Grob*> rests;
101 vector<Grob*> notes;
103 for (vsize i = 0; i < elts.size (); i++)
105 Grob *e = elts[i];
106 if (unsmob_grob (e->get_object ("rest")))
107 rests.push_back (e);
108 else
109 notes.push_back (e);
113 handle rest-rest and rest-note collisions
115 [todo]
116 * decide not to print rest if too crowded?
120 no partners to collide with
122 if (rests.size () + notes.size () < 2)
123 return SCM_BOOL_T;
125 Real staff_space = Staff_symbol_referencer::staff_space (me);
127 only rests
129 if (!notes.size ())
133 This is incomplete: in case of an uneven number of rests, the
134 center one should be centered on the staff.
136 Drul_array<vector<Grob*> > ordered_rests;
137 for (vsize i = 0; i < rests.size (); i++)
139 Grob *r = Note_column::get_rest (rests[i]);
141 Direction d = get_grob_direction (r);
142 if (d)
143 ordered_rests[d].push_back (rests[i]);
144 else
145 rests[d]->warning (_ ("cannot resolve rest collision: rest direction not set"));
148 Direction d = LEFT;
150 vector_sort (ordered_rests[d], Note_column::shift_less);
151 while (flip (&d) != LEFT)
156 if (ordered_rests[d].size () < 1)
158 if (ordered_rests[-d].size () > 1)
159 ordered_rests[-d][0]->warning (_ ("too many colliding rests"));
161 return SCM_BOOL_T;
164 while (flip (&d) != LEFT);
166 Grob *common = common_refpoint_of_array (ordered_rests[DOWN], me, Y_AXIS);
167 common = common_refpoint_of_array (ordered_rests[UP], common, Y_AXIS);
169 Real diff
170 = (ordered_rests[DOWN].back ()->extent (common, Y_AXIS)[UP]
171 - ordered_rests[UP].back ()->extent (common, Y_AXIS)[DOWN]) / staff_space;
173 if (diff > 0)
175 int amount_down = (int) ceil (diff / 2);
176 diff -= amount_down;
177 Note_column::translate_rests (ordered_rests[DOWN].back (),
178 -2 * amount_down);
179 if (diff > 0)
180 Note_column::translate_rests (ordered_rests[UP].back (),
181 2 * int (ceil (diff)));
186 for (vsize i = ordered_rests[d].size () -1; i-- > 0;)
188 Real last_y = ordered_rests[d][i + 1]->extent (common, Y_AXIS)[d];
189 Real y = ordered_rests[d][i]->extent (common, Y_AXIS)[-d];
191 Real diff = d * ((last_y - y) / staff_space);
192 if (diff > 0)
193 Note_column::translate_rests (ordered_rests[d][i], d * (int) ceil (diff) * 2);
196 while (flip (&d) != LEFT);
198 else
201 Rests and notes.
203 if (rests.size () > 1)
204 warning (_ ("too many colliding rests"));
205 Grob *rcol = 0;
206 Direction dir = CENTER;
208 for (vsize i = rests.size (); !rcol && i--;)
209 if (Note_column::dir (rests[i]))
211 rcol = rests[i];
212 dir = Note_column::dir (rcol);
215 if (!rcol)
216 return SCM_BOOL_T;
218 Grob *rest = Note_column::get_rest (rcol);
219 Grob *common = common_refpoint_of_array (notes, rcol, Y_AXIS);
221 Interval restdim = rcol->extent (common, Y_AXIS);
222 if (restdim.is_empty ())
223 return SCM_BOOL_T;
225 Real staff_space = Staff_symbol_referencer::staff_space (rcol);
226 Real minimum_dist = robust_scm2double (me->get_property ("minimum-distance"), 1.0) * staff_space;
228 Interval notedim;
229 for (vsize i = 0; i < notes.size (); i++)
230 notedim.unite (notes[i]->extent (common, Y_AXIS));
233 Real y = dir * max (0.0,
234 -dir * restdim[-dir] + dir * notedim[dir] + minimum_dist);
236 int stafflines = Staff_symbol_referencer::line_count (me);
237 if (!stafflines)
239 programming_error ("no staff line count");
240 stafflines = 5;
243 // move discretely by half spaces.
244 int discrete_y = dir * int (ceil (y / (0.5 * dir * staff_space)));
246 // move by whole spaces inside the staff.
247 if (fabs (Staff_symbol_referencer::get_position (rest)
248 + discrete_y) < stafflines + 1)
250 discrete_y = dir * int (ceil (dir * discrete_y / 2.0) * 2.0);
253 Note_column::translate_rests (rcol, discrete_y);
255 return SCM_BOOL_T;
258 ADD_INTERFACE (Rest_collision,
259 "Move around ordinary rests (not multi-measure-rests) to avoid "
260 "conflicts.",
262 /* properties */
263 "minimum-distance "
264 "positioning-done "
265 "elements");