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 "paper-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 Grob * rc = unsmob_grob (them->get_property ("rest-collision"));
33 if (rc && !to_boolean (rc->get_property ("positioning-done")))
35 rc->set_property ("positioning-done", SCM_BOOL_T);
40 return scm_make_real (0.0);
46 Rest_collision::add_column (Grob*me,Grob *p)
48 me->add_dependency (p);
49 Pointer_group_interface::add_grob (me, ly_symbol2scm ("elements"), p);
52 only add callback for the rests, since we don't move anything
57 p->add_offset_callback (Rest_collision::force_shift_callback_proc, Y_AXIS);
58 p->set_property ("rest-collision", me->self_scm ());
63 Combination of dot-count and duration-log.
66 head_characteristic (Grob * col)
68 Grob * s = unsmob_grob (col->get_property ("rest"));
73 return scm_cons (s->get_property ("duration-log"),
74 scm_int2num (Rhythmic_head::dot_count (s)));
78 TODO: look at horizontal-shift to determine ordering between rests
79 for more than two voices.
83 Rest_collision::do_shift (Grob *me)
85 SCM elts = me->get_property ("elements");
87 Link_array<Grob> rests;
88 Link_array<Grob> notes;
90 for (SCM s = elts; is_pair (s); s = ly_cdr (s))
92 Grob * e = unsmob_grob (ly_car (s));
93 if (unsmob_grob (e->get_property ("rest")))
96 Ignore rests under beam.
98 Grob* st = unsmob_grob (e->get_property ("stem"));
99 if (st && unsmob_grob (st->get_property ("beam")))
110 handle rest-rest and rest-note collisions
113 * decide not to print rest if too crowded?
117 no partners to collide with
119 if (rests.size () + notes.size () < 2)
120 return SCM_UNSPECIFIED;
123 Real staff_space = Staff_symbol_referencer::staff_space (me);
131 This is incomplete: in case of an uneven number of rests, the
132 center one should be centered on the staff.
134 Drul_array< Link_array <Grob > > ordered_rests;
135 for (int i= 0; i < rests.size (); i++)
137 Grob * r = Note_column::get_rest (rests[i]);
139 Direction d = get_grob_direction (r);
142 ordered_rests[d].push (rests[i]);
145 rests[d]->warning (_("rest direction not set. Cannot resolve collision."));
150 ordered_rests[d].sort (Note_column::shift_compare);
151 } while (flip (&d) != LEFT);
153 if (ordered_rests[UP].size () + ordered_rests[DOWN].size () < 2)
154 return SCM_UNSPECIFIED;
156 Grob *common = common_refpoint_of_array (ordered_rests[DOWN], me, Y_AXIS);
157 common = common_refpoint_of_array (ordered_rests[UP], common, Y_AXIS);
160 (ordered_rests[DOWN].top ()->extent (common, Y_AXIS)[UP]
161 - ordered_rests[UP].top ()->extent (common, Y_AXIS)[DOWN]) /staff_space;
165 int amount_down = (int) ceil (diff / 2);
167 Note_column::translate_rests (ordered_rests[DOWN].top (),
170 Note_column::translate_rests (ordered_rests[UP].top (),
171 2 * int (ceil (diff)));
175 for (int i = ordered_rests[d].size () -1; i-- > 0;)
177 Real last_y = ordered_rests[d][i+1]->extent (common, Y_AXIS)[d];
178 Real y = ordered_rests[d][i]->extent (common, Y_AXIS)[-d];
180 Real diff = d * ((last_y - y) /staff_space);
182 Note_column::translate_rests (ordered_rests[d][i],d * (int) ceil (diff) * 2);
184 } while (flip (&d) != LEFT);
191 if (rests.size () > 1)
193 warning (_ ("too many colliding rests"));
196 Direction dir = CENTER;
198 for (int i= rests.size (); !rcol && i--;)
199 if (Note_column::dir (rests[i]))
201 dir = Note_column::dir (rests[i]);
206 return SCM_UNSPECIFIED;
208 Grob *common = common_refpoint_of_array (notes, rcol, Y_AXIS);
210 Interval restdim = rcol->extent (common, Y_AXIS);
211 if (restdim.is_empty ())
212 return SCM_UNSPECIFIED;
214 Real staff_space = Staff_symbol_referencer::staff_space (rcol);
215 Real minimum_dist = robust_scm2double (me->get_property ("minimum-distance"), 1.0) * staff_space;
218 for (int i = 0; i < notes.size (); i++)
220 notedim.unite (notes[i]->extent (common, Y_AXIS));
224 minimum_dist + dir * (notedim[dir] - restdim[-dir]) >? 0;
226 int stafflines = Staff_symbol_referencer::line_count (me);
229 programming_error ("No staff line count ? ");
233 // move discretely by half spaces.
234 int discrete_dist = int (ceil (dist / (0.5 *staff_space)));
236 // move by whole spaces inside the staff.
237 if (discrete_dist < stafflines+1)
238 discrete_dist = int (ceil (discrete_dist / 2.0)* 2.0);
240 Note_column::translate_rests (rcol,dir * discrete_dist);
242 return SCM_UNSPECIFIED;
246 ADD_INTERFACE (Rest_collision,"rest-collision-interface",
247 "Move around ordinary rests (not multi-measure-rests) to avoid "
249 "minimum-distance positioning-done elements");