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 ());
64 Combination of dot-count and duration-log.
67 head_characteristic (Grob * col)
69 Grob * s = unsmob_grob (col->get_property ("rest"));
74 return scm_cons (s->get_property ("duration-log"),
75 scm_int2num (Rhythmic_head::dot_count (s)));
79 TODO: look at horizontal-shift to determine ordering between rests
80 for more than two voices.
84 Rest_collision::do_shift (Grob *me)
86 SCM elts = me->get_property ("elements");
88 Link_array<Grob> rests;
89 Link_array<Grob> notes;
91 for (SCM s = elts; ly_c_pair_p (s); s = ly_cdr (s))
93 Grob * e = unsmob_grob (ly_car (s));
94 if (unsmob_grob (e->get_property ("rest")))
97 Ignore rests under beam.
99 Grob* st = unsmob_grob (e->get_property ("stem"));
100 if (st && unsmob_grob (st->get_property ("beam")))
111 handle rest-rest and rest-note collisions
114 * decide not to print rest if too crowded?
118 no partners to collide with
120 if (rests.size () + notes.size () < 2)
121 return SCM_UNSPECIFIED;
124 Real staff_space = Staff_symbol_referencer::staff_space (me);
132 This is incomplete: in case of an uneven number of rests, the
133 center one should be centered on the staff.
135 Drul_array< Link_array <Grob > > ordered_rests;
136 for (int i= 0; i < rests.size (); i++)
138 Grob * r = Note_column::get_rest (rests[i]);
140 Direction d = get_grob_direction (r);
143 ordered_rests[d].push (rests[i]);
146 rests[d]->warning (_("rest direction not set. Cannot resolve collision."));
151 ordered_rests[d].sort (Note_column::shift_compare);
152 } while (flip (&d) != LEFT);
155 if (ordered_rests[d].size () < 1)
157 if (ordered_rests[-d].size() > 1)
158 ordered_rests[-d][0]->warning (_("Too many colliding rests."));
160 return SCM_UNSPECIFIED;
162 } while (flip (&d) != LEFT);
164 Grob *common = common_refpoint_of_array (ordered_rests[DOWN], me, Y_AXIS);
165 common = common_refpoint_of_array (ordered_rests[UP], common, Y_AXIS);
168 (ordered_rests[DOWN].top ()->extent (common, Y_AXIS)[UP]
169 - ordered_rests[UP].top ()->extent (common, Y_AXIS)[DOWN]) /staff_space;
173 int amount_down = (int) ceil (diff / 2);
175 Note_column::translate_rests (ordered_rests[DOWN].top (),
178 Note_column::translate_rests (ordered_rests[UP].top (),
179 2 * int (ceil (diff)));
183 for (int i = ordered_rests[d].size () -1; i-- > 0;)
185 Real last_y = ordered_rests[d][i+1]->extent (common, Y_AXIS)[d];
186 Real y = ordered_rests[d][i]->extent (common, Y_AXIS)[-d];
188 Real diff = d * ((last_y - y) /staff_space);
190 Note_column::translate_rests (ordered_rests[d][i],d * (int) ceil (diff) * 2);
192 } while (flip (&d) != LEFT);
199 if (rests.size () > 1)
201 warning (_ ("too many colliding rests"));
204 Direction dir = CENTER;
206 for (int i= rests.size (); !rcol && i--;)
207 if (Note_column::dir (rests[i]))
209 dir = Note_column::dir (rests[i]);
214 return SCM_UNSPECIFIED;
216 Grob *common = common_refpoint_of_array (notes, rcol, Y_AXIS);
218 Interval restdim = rcol->extent (common, Y_AXIS);
219 if (restdim.is_empty ())
220 return SCM_UNSPECIFIED;
222 Real staff_space = Staff_symbol_referencer::staff_space (rcol);
223 Real minimum_dist = robust_scm2double (me->get_property ("minimum-distance"), 1.0) * staff_space;
226 for (int i = 0; i < notes.size (); i++)
228 notedim.unite (notes[i]->extent (common, Y_AXIS));
232 minimum_dist + dir * (notedim[dir] - restdim[-dir]) >? 0;
234 int stafflines = Staff_symbol_referencer::line_count (me);
237 programming_error ("No staff line count ? ");
241 // move discretely by half spaces.
242 int discrete_dist = int (ceil (dist / (0.5 *staff_space)));
244 // move by whole spaces inside the staff.
245 if (discrete_dist < stafflines+1)
246 discrete_dist = int (ceil (discrete_dist / 2.0)* 2.0);
248 Note_column::translate_rests (rcol,dir * discrete_dist);
250 return SCM_UNSPECIFIED;
254 ADD_INTERFACE (Rest_collision,"rest-collision-interface",
255 "Move around ordinary rests (not multi-measure-rests) to avoid "
257 "minimum-distance positioning-done elements");