Svg with woff fonts: document as new feature in changes.
[lilypond/patrick.git] / lily / rest-collision.cc
blob079030bc883d640856da6dfce999c02620ecb26d
1 /*
2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1997--2010 Han-Wen Nienhuys <hanwen@xs4all.nl>
6 LilyPond is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 LilyPond is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
20 #include "rest-collision.hh"
22 #include <cmath> // ceil.
23 using namespace std;
25 #include "directional-element-interface.hh"
26 #include "duration.hh"
27 #include "international.hh"
28 #include "item.hh"
29 #include "note-column.hh"
30 #include "output-def.hh"
31 #include "pointer-group-interface.hh"
32 #include "rest.hh"
33 #include "rhythmic-head.hh"
34 #include "staff-symbol-referencer.hh"
35 #include "stem.hh"
36 #include "grob.hh"
37 #include "warn.hh"
39 MAKE_SCHEME_CALLBACK_WITH_OPTARGS (Rest_collision, force_shift_callback_rest, 2, 1, "");
40 SCM
41 Rest_collision::force_shift_callback_rest (SCM rest, SCM offset)
43 Grob *rest_grob = unsmob_grob (rest);
44 Grob *parent = rest_grob->get_parent (X_AXIS);
47 translate REST; we need the result of this translation later on,
48 while the offset probably still is 0/calculation-in-progress.
50 if (scm_is_number (offset))
51 rest_grob->translate_axis (scm_to_double (offset), Y_AXIS);
53 if (Note_column::has_interface (parent) && Note_column::has_rests (parent))
55 Grob *collision = unsmob_grob (parent->get_object ("rest-collision"));
57 if (collision)
58 (void) collision->get_property ("positioning-done");
61 return scm_from_double (0.0);
64 void
65 Rest_collision::add_column (Grob *me, Grob *p)
67 Pointer_group_interface::add_grob (me, ly_symbol2scm ("elements"), p);
69 p->set_object ("rest-collision", me->self_scm ());
71 Grob *rest = unsmob_grob (p->get_object ("rest"));
72 if (rest)
74 chain_offset_callback (rest,
75 Rest_collision::force_shift_callback_rest_proc, Y_AXIS);
80 TODO: look at horizontal-shift to determine ordering between rests
81 for more than two voices.
83 MAKE_SCHEME_CALLBACK (Rest_collision, calc_positioning_done, 1);
84 SCM
85 Rest_collision::calc_positioning_done (SCM smob)
87 Grob *me = unsmob_grob (smob);
89 me->set_property ("positioning-done", SCM_BOOL_T);
91 extract_grob_set (me, "elements", elts);
93 vector<Grob*> rests;
94 vector<Grob*> notes;
96 for (vsize i = 0; i < elts.size (); i++)
98 Grob *e = elts[i];
99 if (unsmob_grob (e->get_object ("rest")))
100 rests.push_back (e);
101 else
102 notes.push_back (e);
106 handle rest-rest and rest-note collisions
108 [todo]
109 * decide not to print rest if too crowded?
113 no partners to collide with
115 if (rests.size () + notes.size () < 2)
116 return SCM_BOOL_T;
118 Real staff_space = Staff_symbol_referencer::staff_space (me);
120 only rests
122 if (!notes.size ())
126 This is incomplete: in case of an uneven number of rests, the
127 center one should be centered on the staff.
129 Drul_array<vector<Grob*> > ordered_rests;
130 for (vsize i = 0; i < rests.size (); i++)
132 Grob *r = Note_column::get_rest (rests[i]);
134 Direction d = get_grob_direction (r);
135 if (d)
136 ordered_rests[d].push_back (rests[i]);
137 else
138 rests[d]->warning (_ ("cannot resolve rest collision: rest direction not set"));
141 Direction d = LEFT;
143 vector_sort (ordered_rests[d], Note_column::shift_less);
144 while (flip (&d) != LEFT)
149 if (ordered_rests[d].size () < 1)
151 if (ordered_rests[-d].size () > 1)
152 ordered_rests[-d][0]->warning (_ ("too many colliding rests"));
154 return SCM_BOOL_T;
157 while (flip (&d) != LEFT);
159 Grob *common = common_refpoint_of_array (ordered_rests[DOWN], me, Y_AXIS);
160 common = common_refpoint_of_array (ordered_rests[UP], common, Y_AXIS);
162 Real diff
163 = (ordered_rests[DOWN].back ()->extent (common, Y_AXIS)[UP]
164 - ordered_rests[UP].back ()->extent (common, Y_AXIS)[DOWN]) / staff_space;
166 if (diff > 0)
168 int amount_down = (int) ceil (diff / 2);
169 diff -= amount_down;
170 Note_column::translate_rests (ordered_rests[DOWN].back (),
171 -2 * amount_down);
172 if (diff > 0)
173 Note_column::translate_rests (ordered_rests[UP].back (),
174 2 * int (ceil (diff)));
179 for (vsize i = ordered_rests[d].size () -1; i-- > 0;)
181 Real last_y = ordered_rests[d][i + 1]->extent (common, Y_AXIS)[d];
182 Real y = ordered_rests[d][i]->extent (common, Y_AXIS)[-d];
184 Real diff = d * ((last_y - y) / staff_space);
185 if (diff > 0)
186 Note_column::translate_rests (ordered_rests[d][i], d * (int) ceil (diff) * 2);
189 while (flip (&d) != LEFT);
191 else
194 Rests and notes.
196 if (rests.size () > 1)
197 warning (_ ("too many colliding rests"));
198 Grob *rcol = 0;
199 Direction dir = CENTER;
201 for (vsize i = rests.size (); !rcol && i--;)
202 if (Note_column::dir (rests[i]))
204 rcol = rests[i];
205 dir = Note_column::dir (rcol);
208 if (!rcol)
209 return SCM_BOOL_T;
211 Grob *rest = Note_column::get_rest (rcol);
212 Grob *common = common_refpoint_of_array (notes, rcol, Y_AXIS);
214 Interval restdim = rcol->extent (common, Y_AXIS);
215 if (restdim.is_empty ())
216 return SCM_BOOL_T;
218 Real staff_space = Staff_symbol_referencer::staff_space (rcol);
219 Real minimum_dist = robust_scm2double (me->get_property ("minimum-distance"), 1.0) * staff_space;
221 Interval notedim;
222 for (vsize i = 0; i < notes.size (); i++)
224 if (Note_column::dir (notes[i]) == -dir
225 // If the note has already happened (but it has a long duration, so there is a collision),
226 // don't look at the stem. If we do, the rest gets shifted down a lot and it looks bad.
227 || dynamic_cast<Item*> (notes[i])->get_column () != dynamic_cast<Item*> (rest)->get_column ())
229 /* try not to look at the stem, as looking at a beamed
230 note may trigger beam positioning prematurely.
232 This happens with dotted rests, which need Y
233 positioning to compute X-positioning.
235 Grob *head = Note_column::first_head (notes[i]);
236 if (head)
237 notedim.unite (head->extent (common, Y_AXIS));
238 else
239 programming_error ("Note_column without first_head()");
241 else
242 notedim.unite (notes[i]->extent (common, Y_AXIS));
245 Real y = dir * max (0.0,
246 -dir * restdim[-dir] + dir * notedim[dir] + minimum_dist);
248 int stafflines = Staff_symbol_referencer::line_count (me);
249 if (!stafflines)
251 programming_error ("no staff line count");
252 stafflines = 5;
255 // move discretely by half spaces.
256 int discrete_y = dir * int (ceil (y / (0.5 * dir * staff_space)));
258 // move by whole spaces inside the staff.
259 if (fabs (Staff_symbol_referencer::get_position (rest)
260 + discrete_y) < stafflines + 1)
262 discrete_y = dir * int (ceil (dir * discrete_y / 2.0) * 2.0);
265 Note_column::translate_rests (rcol, discrete_y);
267 return SCM_BOOL_T;
270 ADD_INTERFACE (Rest_collision,
271 "Move around ordinary rests (not multi-measure-rests) to avoid"
272 " conflicts.",
274 /* properties */
275 "minimum-distance "
276 "positioning-done "
277 "elements "