2 rest-collision.cc -- implement Rest_collision
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
9 #include <math.h> // ceil.
12 #include "rest-collision.hh"
13 #include "note-column.hh"
15 #include "rhythmic-head.hh"
16 #include "output-def.hh"
18 #include "group-interface.hh"
19 #include "staff-symbol-referencer.hh"
20 #include "duration.hh"
21 #include "directional-element-interface.hh"
23 MAKE_SCHEME_CALLBACK (Rest_collision,force_shift_callback,2);
25 Rest_collision::force_shift_callback (SCM element_smob, SCM axis)
27 Grob *them = unsmob_grob (element_smob);
28 Axis a = (Axis) ly_scm2int (axis);
31 if (Note_column::has_rests (them))
33 Grob * rc = unsmob_grob (them->get_property ("rest-collision"));
35 if (rc && !to_boolean (rc->get_property ("positioning-done")))
37 rc->set_property ("positioning-done", SCM_BOOL_T);
41 return scm_make_real (0.0);
47 Rest_collision::add_column (Grob*me,Grob *p)
49 me->add_dependency (p);
50 Pointer_group_interface::add_grob (me, ly_symbol2scm ("elements"), p);
53 only add callback for the rests, since we don't move anything
58 p->add_offset_callback (Rest_collision::force_shift_callback_proc, Y_AXIS);
59 p->set_property ("rest-collision", me->self_scm ());
65 TODO: look at horizontal-shift to determine ordering between rests
66 for more than two voices.
70 Rest_collision::do_shift (Grob *me)
72 SCM elts = me->get_property ("elements");
74 Link_array<Grob> rests;
75 Link_array<Grob> notes;
77 for (SCM s = elts; ly_c_pair_p (s); s = ly_cdr (s))
79 Grob * e = unsmob_grob (ly_car (s));
80 if (unsmob_grob (e->get_property ("rest")))
83 Ignore rests under beam.
85 Grob* st = unsmob_grob (e->get_property ("stem"));
86 if (st && unsmob_grob (st->get_property ("beam")))
97 handle rest-rest and rest-note collisions
100 * decide not to print rest if too crowded?
104 no partners to collide with
106 if (rests.size () + notes.size () < 2)
107 return SCM_UNSPECIFIED;
110 Real staff_space = Staff_symbol_referencer::staff_space (me);
118 This is incomplete: in case of an uneven number of rests, the
119 center one should be centered on the staff.
121 Drul_array< Link_array <Grob > > ordered_rests;
122 for (int i= 0; i < rests.size (); i++)
124 Grob * r = Note_column::get_rest (rests[i]);
126 Direction d = get_grob_direction (r);
129 ordered_rests[d].push (rests[i]);
132 rests[d]->warning (_("rest direction not set. Cannot resolve collision."));
137 ordered_rests[d].sort (Note_column::shift_compare);
138 } while (flip (&d) != LEFT);
141 if (ordered_rests[d].size () < 1)
143 if (ordered_rests[-d].size() > 1)
144 ordered_rests[-d][0]->warning (_("Too many colliding rests."));
146 return SCM_UNSPECIFIED;
148 } while (flip (&d) != LEFT);
150 Grob *common = common_refpoint_of_array (ordered_rests[DOWN], me, Y_AXIS);
151 common = common_refpoint_of_array (ordered_rests[UP], common, Y_AXIS);
154 (ordered_rests[DOWN].top ()->extent (common, Y_AXIS)[UP]
155 - ordered_rests[UP].top ()->extent (common, Y_AXIS)[DOWN]) /staff_space;
159 int amount_down = (int) ceil (diff / 2);
161 Note_column::translate_rests (ordered_rests[DOWN].top (),
164 Note_column::translate_rests (ordered_rests[UP].top (),
165 2 * int (ceil (diff)));
169 for (int i = ordered_rests[d].size () -1; i-- > 0;)
171 Real last_y = ordered_rests[d][i+1]->extent (common, Y_AXIS)[d];
172 Real y = ordered_rests[d][i]->extent (common, Y_AXIS)[-d];
174 Real diff = d * ((last_y - y) /staff_space);
176 Note_column::translate_rests (ordered_rests[d][i],d * (int) ceil (diff) * 2);
178 } while (flip (&d) != LEFT);
185 if (rests.size () > 1)
187 warning (_ ("too many colliding rests"));
190 Direction dir = CENTER;
192 for (int i= rests.size (); !rcol && i--;)
193 if (Note_column::dir (rests[i]))
195 dir = Note_column::dir (rests[i]);
200 return SCM_UNSPECIFIED;
202 Grob *common = common_refpoint_of_array (notes, rcol, Y_AXIS);
204 Interval restdim = rcol->extent (common, Y_AXIS);
205 if (restdim.is_empty ())
206 return SCM_UNSPECIFIED;
208 Real staff_space = Staff_symbol_referencer::staff_space (rcol);
209 Real minimum_dist = robust_scm2double (me->get_property ("minimum-distance"), 1.0) * staff_space;
212 for (int i = 0; i < notes.size (); i++)
214 notedim.unite (notes[i]->extent (common, Y_AXIS));
218 minimum_dist + dir * (notedim[dir] - restdim[-dir]) >? 0;
220 int stafflines = Staff_symbol_referencer::line_count (me);
223 programming_error ("No staff line count ? ");
227 // move discretely by half spaces.
228 int discrete_dist = int (ceil (dist / (0.5 *staff_space)));
230 // move by whole spaces inside the staff.
231 if (discrete_dist < stafflines+1)
232 discrete_dist = int (ceil (discrete_dist / 2.0)* 2.0);
234 Note_column::translate_rests (rcol,dir * discrete_dist);
236 return SCM_UNSPECIFIED;
240 ADD_INTERFACE (Rest_collision,"rest-collision-interface",
241 "Move around ordinary rests (not multi-measure-rests) to avoid "
243 "minimum-distance positioning-done elements");