*** empty log message ***
[lilypond/patrick.git] / lily / rest-collision.cc
blob5f7f10f2559cbc1a1b6ace43b470bbe4a32eadb4
1 /*
2 rest-collision.cc -- implement Rest_collision
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
9 #include "rest-collision.hh"
11 #include <math.h> // ceil.
13 #include "warn.hh"
14 #include "note-column.hh"
15 #include "stem.hh"
16 #include "rhythmic-head.hh"
17 #include "output-def.hh"
18 #include "rest.hh"
19 #include "group-interface.hh"
20 #include "staff-symbol-referencer.hh"
21 #include "duration.hh"
22 #include "directional-element-interface.hh"
24 MAKE_SCHEME_CALLBACK (Rest_collision, force_shift_callback, 2);
25 SCM
26 Rest_collision::force_shift_callback (SCM element_smob, SCM axis)
28 Grob *them = unsmob_grob (element_smob);
29 Axis a = (Axis) scm_to_int (axis);
30 assert (a == Y_AXIS);
32 if (Note_column::has_rests (them))
34 Grob *rc = unsmob_grob (them->get_property ("rest-collision"));
36 if (rc && !to_boolean (rc->get_property ("positioning-done")))
38 rc->set_property ("positioning-done", SCM_BOOL_T);
39 do_shift (rc);
42 return scm_make_real (0.0);
45 MAKE_SCHEME_CALLBACK (Rest_collision, force_shift_callback_rest, 2);
46 SCM
47 Rest_collision::force_shift_callback_rest (SCM rest, SCM axis)
49 Grob *rest_grob = unsmob_grob (rest);
50 assert ((Axis) scm_to_int (axis) == Y_AXIS);
52 Grob *parent = rest_grob->get_parent (X_AXIS);
53 if (Note_column::has_interface (parent))
54 return force_shift_callback (parent->self_scm (), axis);
55 else
56 return scm_make_real (0.0);
59 void
60 Rest_collision::add_column (Grob *me, Grob *p)
62 me->add_dependency (p);
63 Pointer_group_interface::add_grob (me, ly_symbol2scm ("elements"), p);
66 only add callback for the rests, since we don't move anything
67 else.
69 (not?)
71 p->add_offset_callback (Rest_collision::force_shift_callback_proc, Y_AXIS);
72 p->set_property ("rest-collision", me->self_scm ());
74 Grob *rest = unsmob_grob (p->get_property ("rest"));
75 if (rest)
77 rest->add_offset_callback (Rest_collision::force_shift_callback_rest_proc,
78 Y_AXIS);
83 TODO: look at horizontal-shift to determine ordering between rests
84 for more than two voices.
86 SCM
87 Rest_collision::do_shift (Grob *me)
89 SCM elts = me->get_property ("elements");
91 Link_array<Grob> rests;
92 Link_array<Grob> notes;
94 for (SCM s = elts; scm_is_pair (s); s = scm_cdr (s))
96 Grob *e = unsmob_grob (scm_car (s));
97 if (unsmob_grob (e->get_property ("rest")))
100 Ignore rests under beam.
102 Grob *st = unsmob_grob (e->get_property ("stem"));
103 if (st && unsmob_grob (st->get_property ("beam")))
104 continue;
106 rests.push (e);
108 else
109 notes.push (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_UNSPECIFIED;
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<Link_array<Grob> > ordered_rests;
137 for (int 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)
144 ordered_rests[d].push (rests[i]);
146 else
147 rests[d]->warning (_ ("rest direction not set. Cannot resolve collision."));
150 Direction d = LEFT;
153 ordered_rests[d].sort (Note_column::shift_compare);
155 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);
172 Real diff
173 = (ordered_rests[DOWN].top ()->extent (common, Y_AXIS)[UP]
174 - ordered_rests[UP].top ()->extent (common, Y_AXIS)[DOWN]) / staff_space;
176 if (diff > 0)
178 int amount_down = (int) ceil (diff / 2);
179 diff -= amount_down;
180 Note_column::translate_rests (ordered_rests[DOWN].top (),
181 -2 * amount_down);
182 if (diff > 0)
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);
195 if (diff > 0)
196 Note_column::translate_rests (ordered_rests[d][i], d * (int) ceil (diff) * 2);
199 while (flip (&d) != LEFT);
201 else
204 Rests and notes.
206 if (rests.size () > 1)
208 warning (_ ("too many colliding rests"));
210 Grob *rcol = 0;
211 Direction dir = CENTER;
213 for (int i = rests.size (); !rcol && i--;)
214 if (Note_column::dir (rests[i]))
216 dir = Note_column::dir (rests[i]);
217 rcol = rests[i];
220 if (!rcol)
221 return SCM_UNSPECIFIED;
223 Grob *common = common_refpoint_of_array (notes, rcol, Y_AXIS);
225 Interval restdim = rcol->extent (common, Y_AXIS);
226 if (restdim.is_empty ())
227 return SCM_UNSPECIFIED;
229 Real staff_space = Staff_symbol_referencer::staff_space (rcol);
230 Real minimum_dist = robust_scm2double (me->get_property ("minimum-distance"), 1.0) * staff_space;
232 Interval notedim;
233 for (int i = 0; i < notes.size (); i++)
235 notedim.unite (notes[i]->extent (common, Y_AXIS));
238 Real dist
239 = minimum_dist + dir * (notedim[dir] - restdim[-dir]) >? 0;
241 int stafflines = Staff_symbol_referencer::line_count (me);
242 if (!stafflines)
244 programming_error ("no staff line count");
245 stafflines = 5;
248 // move discretely by half spaces.
249 int discrete_dist = int (ceil (dist / (0.5 * staff_space)));
251 // move by whole spaces inside the staff.
252 if (discrete_dist < stafflines + 1)
253 discrete_dist = int (ceil (discrete_dist / 2.0) * 2.0);
255 Note_column::translate_rests (rcol, dir * discrete_dist);
257 return SCM_UNSPECIFIED;
260 ADD_INTERFACE (Rest_collision, "rest-collision-interface",
261 "Move around ordinary rests (not multi-measure-rests) to avoid "
262 "conflicts.",
263 "minimum-distance positioning-done elements");