2 rest-collision.cc -- implement Rest_collision
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
9 #include "rest-collision.hh"
11 #include <cmath> // ceil.
15 #include "note-column.hh"
17 #include "rhythmic-head.hh"
18 #include "output-def.hh"
20 #include "pointer-group-interface.hh"
21 #include "staff-symbol-referencer.hh"
22 #include "duration.hh"
23 #include "directional-element-interface.hh"
25 MAKE_SCHEME_CALLBACK (Rest_collision, force_shift_callback, 1);
27 Rest_collision::force_shift_callback (SCM smob)
29 Grob *them = unsmob_grob (smob);
30 if (Note_column::has_rests (them))
32 Grob *collision = unsmob_grob (them->get_object ("rest-collision"));
36 (void) collision->get_property ("positioning-done");
39 return scm_from_double (0.0);
42 MAKE_SCHEME_CALLBACK (Rest_collision, force_shift_callback_rest, 2);
44 Rest_collision::force_shift_callback_rest (SCM rest, SCM offset)
46 Grob *rest_grob = unsmob_grob (rest);
47 Grob *parent = rest_grob->get_parent (X_AXIS);
50 translate REST; we need the result of this translation later on,
51 while the offset probably still is 0/calculation-in-progress.
53 rest_grob->translate_axis (scm_to_double (offset), Y_AXIS);
55 if (Note_column::has_interface (parent))
56 force_shift_callback (parent->self_scm ());
58 return scm_from_double (0.0);
62 Rest_collision::add_column (Grob *me, Grob *p)
64 Pointer_group_interface::add_grob (me, ly_symbol2scm ("elements"), p);
67 only add callback for the rests, since we don't move anything
72 add_offset_callback (p, Rest_collision::force_shift_callback_proc, Y_AXIS);
73 p->set_object ("rest-collision", me->self_scm ());
75 Grob *rest = unsmob_grob (p->get_object ("rest"));
78 chain_offset_callback (rest,
79 Rest_collision::force_shift_callback_rest_proc, Y_AXIS);
84 TODO: look at horizontal-shift to determine ordering between rests
85 for more than two voices.
87 MAKE_SCHEME_CALLBACK(Rest_collision, calc_positioning_done, 1);
89 Rest_collision::calc_positioning_done (SCM smob)
91 Grob *me = unsmob_grob (smob);
92 extract_grob_set (me, "elements", elts);
94 Link_array<Grob> rests;
95 Link_array<Grob> notes;
97 for (int i = 0; i < elts.size (); i++)
100 if (unsmob_grob (e->get_object ("rest")))
103 Ignore rests under beam.
105 Grob *st = unsmob_grob (e->get_object ("stem"));
106 if (st && unsmob_grob (st->get_object ("beam")))
116 handle rest-rest and rest-note collisions
119 * decide not to print rest if too crowded?
123 no partners to collide with
125 if (rests.size () + notes.size () < 2)
126 return SCM_UNSPECIFIED;
128 Real staff_space = Staff_symbol_referencer::staff_space (me);
136 This is incomplete: in case of an uneven number of rests, the
137 center one should be centered on the staff.
139 Drul_array<Link_array<Grob> > ordered_rests;
140 for (int i = 0; i < rests.size (); i++)
142 Grob *r = Note_column::get_rest (rests[i]);
144 Direction d = get_grob_direction (r);
146 ordered_rests[d].push (rests[i]);
148 rests[d]->warning (_ ("can't resolve rest collision: rest direction not set"));
153 ordered_rests[d].sort (Note_column::shift_compare);
154 while (flip (&d) != LEFT)
159 if (ordered_rests[d].size () < 1)
161 if (ordered_rests[-d].size () > 1)
162 ordered_rests[-d][0]->warning (_ ("too many colliding rests"));
164 return SCM_UNSPECIFIED;
167 while (flip (&d) != LEFT);
169 Grob *common = common_refpoint_of_array (ordered_rests[DOWN], me, Y_AXIS);
170 common = common_refpoint_of_array (ordered_rests[UP], common, Y_AXIS);
173 = (ordered_rests[DOWN].top ()->extent (common, Y_AXIS)[UP]
174 - ordered_rests[UP].top ()->extent (common, Y_AXIS)[DOWN]) / staff_space;
178 int amount_down = (int) ceil (diff / 2);
180 Note_column::translate_rests (ordered_rests[DOWN].top (),
183 Note_column::translate_rests (ordered_rests[UP].top (),
184 2 * int (ceil (diff)));
189 for (int i = ordered_rests[d].size () -1; i-- > 0;)
191 Real last_y = ordered_rests[d][i + 1]->extent (common, Y_AXIS)[d];
192 Real y = ordered_rests[d][i]->extent (common, Y_AXIS)[-d];
194 Real diff = d * ((last_y - y) / staff_space);
196 Note_column::translate_rests (ordered_rests[d][i], d * (int) ceil (diff) * 2);
199 while (flip (&d) != LEFT);
206 if (rests.size () > 1)
207 warning (_ ("too many colliding rests"));
209 Direction dir = CENTER;
211 for (int i = rests.size (); !rcol && i--;)
212 if (Note_column::dir (rests[i]))
214 dir = Note_column::dir (rests[i]);
219 return SCM_UNSPECIFIED;
221 Grob *common = common_refpoint_of_array (notes, rcol, Y_AXIS);
223 Interval restdim = rcol->extent (common, Y_AXIS);
224 if (restdim.is_empty ())
225 return SCM_UNSPECIFIED;
227 Real staff_space = Staff_symbol_referencer::staff_space (rcol);
228 Real minimum_dist = robust_scm2double (me->get_property ("minimum-distance"), 1.0) * staff_space;
231 for (int i = 0; i < notes.size (); i++)
232 notedim.unite (notes[i]->extent (common, Y_AXIS));
235 = minimum_dist + dir * max (notedim[dir] - restdim[-dir], 0.0);
237 int stafflines = Staff_symbol_referencer::line_count (me);
240 programming_error ("no staff line count");
244 // move discretely by half spaces.
245 int discrete_dist = int (ceil (dist / (0.5 * staff_space)));
247 // move by whole spaces inside the staff.
248 if (discrete_dist < stafflines + 1)
249 discrete_dist = int (ceil (discrete_dist / 2.0) * 2.0);
251 Note_column::translate_rests (rcol, dir * discrete_dist);
253 return SCM_UNSPECIFIED;
256 ADD_INTERFACE (Rest_collision, "rest-collision-interface",
257 "Move around ordinary rests (not multi-measure-rests) to avoid "