Nitpick: ly:spanner-bound grob name slur -> spanner.
[lilypond.git] / lily / rest-collision.cc
blob6448492b8f5f925b954fe9fb4064b4b0cb50cb2a
1 /*
2 rest-collision.cc -- implement Rest_collision
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2009 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
9 #include "rest-collision.hh"
11 #include <cmath> // ceil.
12 using namespace std;
14 #include "directional-element-interface.hh"
15 #include "duration.hh"
16 #include "international.hh"
17 #include "item.hh"
18 #include "note-column.hh"
19 #include "output-def.hh"
20 #include "pointer-group-interface.hh"
21 #include "rest.hh"
22 #include "rhythmic-head.hh"
23 #include "staff-symbol-referencer.hh"
24 #include "stem.hh"
25 #include "grob.hh"
26 #include "warn.hh"
28 MAKE_SCHEME_CALLBACK (Rest_collision, force_shift_callback, 1);
29 SCM
30 Rest_collision::force_shift_callback (SCM smob)
32 Grob *them = unsmob_grob (smob);
33 if (Note_column::has_rests (them))
35 Grob *collision = unsmob_grob (them->get_object ("rest-collision"));
37 if (collision)
39 (void) collision->get_property ("positioning-done");
42 return scm_from_double (0.0);
45 MAKE_SCHEME_CALLBACK_WITH_OPTARGS (Rest_collision, force_shift_callback_rest, 2, 1, "");
46 SCM
47 Rest_collision::force_shift_callback_rest (SCM rest, SCM offset)
49 Grob *rest_grob = unsmob_grob (rest);
50 Grob *parent = rest_grob->get_parent (X_AXIS);
53 translate REST; we need the result of this translation later on,
54 while the offset probably still is 0/calculation-in-progress.
56 if (scm_is_number (offset))
57 rest_grob->translate_axis (scm_to_double (offset), Y_AXIS);
59 if (Note_column::has_interface (parent))
60 force_shift_callback (parent->self_scm ());
62 return scm_from_double (0.0);
65 void
66 Rest_collision::add_column (Grob *me, Grob *p)
68 Pointer_group_interface::add_grob (me, ly_symbol2scm ("elements"), p);
71 only add callback for the rests, since we don't move anything
72 else.
74 (not?)
76 add_offset_callback (p, Rest_collision::force_shift_callback_proc, Y_AXIS);
77 p->set_object ("rest-collision", me->self_scm ());
79 Grob *rest = unsmob_grob (p->get_object ("rest"));
80 if (rest)
82 chain_offset_callback (rest,
83 Rest_collision::force_shift_callback_rest_proc, Y_AXIS);
88 TODO: look at horizontal-shift to determine ordering between rests
89 for more than two voices.
91 MAKE_SCHEME_CALLBACK (Rest_collision, calc_positioning_done, 1);
92 SCM
93 Rest_collision::calc_positioning_done (SCM smob)
95 Grob *me = unsmob_grob (smob);
97 me->set_property ("positioning-done", SCM_BOOL_T);
99 extract_grob_set (me, "elements", elts);
101 vector<Grob*> rests;
102 vector<Grob*> notes;
104 for (vsize i = 0; i < elts.size (); i++)
106 Grob *e = elts[i];
107 if (unsmob_grob (e->get_object ("rest")))
108 rests.push_back (e);
109 else
110 notes.push_back (e);
114 handle rest-rest and rest-note collisions
116 [todo]
117 * decide not to print rest if too crowded?
121 no partners to collide with
123 if (rests.size () + notes.size () < 2)
124 return SCM_BOOL_T;
126 Real staff_space = Staff_symbol_referencer::staff_space (me);
128 only rests
130 if (!notes.size ())
134 This is incomplete: in case of an uneven number of rests, the
135 center one should be centered on the staff.
137 Drul_array<vector<Grob*> > ordered_rests;
138 for (vsize i = 0; i < rests.size (); i++)
140 Grob *r = Note_column::get_rest (rests[i]);
142 Direction d = get_grob_direction (r);
143 if (d)
144 ordered_rests[d].push_back (rests[i]);
145 else
146 rests[d]->warning (_ ("cannot resolve rest collision: rest direction not set"));
149 Direction d = LEFT;
151 vector_sort (ordered_rests[d], Note_column::shift_less);
152 while (flip (&d) != LEFT)
157 if (ordered_rests[d].size () < 1)
159 if (ordered_rests[-d].size () > 1)
160 ordered_rests[-d][0]->warning (_ ("too many colliding rests"));
162 return SCM_BOOL_T;
165 while (flip (&d) != LEFT);
167 Grob *common = common_refpoint_of_array (ordered_rests[DOWN], me, Y_AXIS);
168 common = common_refpoint_of_array (ordered_rests[UP], common, Y_AXIS);
170 Real diff
171 = (ordered_rests[DOWN].back ()->extent (common, Y_AXIS)[UP]
172 - ordered_rests[UP].back ()->extent (common, Y_AXIS)[DOWN]) / staff_space;
174 if (diff > 0)
176 int amount_down = (int) ceil (diff / 2);
177 diff -= amount_down;
178 Note_column::translate_rests (ordered_rests[DOWN].back (),
179 -2 * amount_down);
180 if (diff > 0)
181 Note_column::translate_rests (ordered_rests[UP].back (),
182 2 * int (ceil (diff)));
187 for (vsize i = ordered_rests[d].size () -1; i-- > 0;)
189 Real last_y = ordered_rests[d][i + 1]->extent (common, Y_AXIS)[d];
190 Real y = ordered_rests[d][i]->extent (common, Y_AXIS)[-d];
192 Real diff = d * ((last_y - y) / staff_space);
193 if (diff > 0)
194 Note_column::translate_rests (ordered_rests[d][i], d * (int) ceil (diff) * 2);
197 while (flip (&d) != LEFT);
199 else
202 Rests and notes.
204 if (rests.size () > 1)
205 warning (_ ("too many colliding rests"));
206 Grob *rcol = 0;
207 Direction dir = CENTER;
209 for (vsize i = rests.size (); !rcol && i--;)
210 if (Note_column::dir (rests[i]))
212 rcol = rests[i];
213 dir = Note_column::dir (rcol);
216 if (!rcol)
217 return SCM_BOOL_T;
219 Grob *rest = Note_column::get_rest (rcol);
220 Grob *common = common_refpoint_of_array (notes, rcol, Y_AXIS);
222 Interval restdim = rcol->extent (common, Y_AXIS);
223 if (restdim.is_empty ())
224 return SCM_BOOL_T;
226 Real staff_space = Staff_symbol_referencer::staff_space (rcol);
227 Real minimum_dist = robust_scm2double (me->get_property ("minimum-distance"), 1.0) * staff_space;
229 Interval notedim;
230 for (vsize i = 0; i < notes.size (); i++)
232 if (Note_column::dir (notes[i]) == -dir
233 // If the note has already happened (but it has a long duration, so there is a collision),
234 // don't look at the stem. If we do, the rest gets shifted down a lot and it looks bad.
235 || dynamic_cast<Item*> (notes[i])->get_column () != dynamic_cast<Item*> (rest)->get_column ())
237 /* try not to look at the stem, as looking at a beamed
238 note may trigger beam positioning prematurely.
240 This happens with dotted rests, which need Y
241 positioning to compute X-positioning.
243 Grob *head = Note_column::first_head (notes[i]);
244 if (head)
245 notedim.unite (head->extent (common, Y_AXIS));
246 else
247 programming_error ("Note_column without first_head()");
249 else
250 notedim.unite (notes[i]->extent (common, Y_AXIS));
253 Real y = dir * max (0.0,
254 -dir * restdim[-dir] + dir * notedim[dir] + minimum_dist);
256 int stafflines = Staff_symbol_referencer::line_count (me);
257 if (!stafflines)
259 programming_error ("no staff line count");
260 stafflines = 5;
263 // move discretely by half spaces.
264 int discrete_y = dir * int (ceil (y / (0.5 * dir * staff_space)));
266 // move by whole spaces inside the staff.
267 if (fabs (Staff_symbol_referencer::get_position (rest)
268 + discrete_y) < stafflines + 1)
270 discrete_y = dir * int (ceil (dir * discrete_y / 2.0) * 2.0);
273 Note_column::translate_rests (rcol, discrete_y);
275 return SCM_BOOL_T;
278 ADD_INTERFACE (Rest_collision,
279 "Move around ordinary rests (not multi-measure-rests) to avoid"
280 " conflicts.",
282 /* properties */
283 "minimum-distance "
284 "positioning-done "
285 "elements "