]> git.donarmstrong.com Git - lilypond.git/blob - lily/rest-collision.cc
Run grand-replace (issue 3765)
[lilypond.git] / lily / rest-collision.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2014 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
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.
10
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.
15
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/>.
18 */
19
20 #include "rest-collision.hh"
21
22 #include <cmath>                // ceil.
23 using namespace std;
24
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 "unpure-pure-container.hh"
38 #include "warn.hh"
39
40 MAKE_SCHEME_CALLBACK_WITH_OPTARGS (Rest_collision, force_shift_callback_rest, 2, 1, "");
41 SCM
42 Rest_collision::force_shift_callback_rest (SCM rest, SCM offset)
43 {
44   Grob *rest_grob = unsmob_grob (rest);
45   Grob *parent = rest_grob->get_parent (X_AXIS);
46
47   /*
48     translate REST; we need the result of this translation later on,
49     while the offset probably still is 0/calculation-in-progress.
50    */
51   if (scm_is_number (offset))
52     rest_grob->translate_axis (scm_to_double (offset), Y_AXIS);
53
54   if (Note_column::has_interface (parent) && Note_column::has_rests (parent))
55     {
56       Grob *collision = unsmob_grob (parent->get_object ("rest-collision"));
57
58       if (collision)
59         (void) collision->get_property ("positioning-done");
60     }
61
62   return scm_from_double (0.0);
63 }
64
65 void
66 Rest_collision::add_column (Grob *me, Grob *p)
67 {
68   Pointer_group_interface::add_grob (me, ly_symbol2scm ("elements"), p);
69
70   p->set_object ("rest-collision", me->self_scm ());
71
72   Grob *rest = unsmob_grob (p->get_object ("rest"));
73   if (rest)
74     {
75       chain_offset_callback (rest,
76                              ly_make_unpure_pure_container
77                                (Rest_collision::force_shift_callback_rest_proc,
78                                 ly_lily_module_constant ("pure-chain-offset-callback")),
79                               Y_AXIS);
80     }
81 }
82
83 static bool
84 rest_shift_less (Grob *const &r1, Grob *const &r2)
85 {
86   Grob *col1 = r1->get_parent (X_AXIS);
87   Grob *col2 = r2->get_parent (X_AXIS);
88   return Note_column::shift_less (col1, col2);
89 }
90
91 /*
92   TODO: look at horizontal-shift to determine ordering between rests
93   for more than two voices.
94 */
95 MAKE_SCHEME_CALLBACK (Rest_collision, calc_positioning_done, 1);
96 SCM
97 Rest_collision::calc_positioning_done (SCM smob)
98 {
99   Grob *me = unsmob_grob (smob);
100
101   me->set_property ("positioning-done", SCM_BOOL_T);
102
103   extract_grob_set (me, "elements", elts);
104
105   vector<Grob *> rests;
106   vector<Grob *> notes;
107
108   for (vsize i = 0; i < elts.size (); i++)
109     {
110       Grob *e = elts[i];
111       if (unsmob_grob (e->get_object ("rest")))
112         rests.push_back (e);
113       else
114         notes.push_back (e);
115     }
116
117   /*
118     handle rest-rest and rest-note collisions
119
120     [todo]
121     * decide not to print rest if too crowded?
122     */
123
124   /*
125     no partners to collide with
126   */
127   if (rests.size () + notes.size () < 2)
128     return SCM_BOOL_T;
129
130   Real staff_space = Staff_symbol_referencer::staff_space (me);
131   /*
132     only rests
133   */
134   if (!notes.size ())
135     {
136
137       /*
138         This is incomplete: in case of an uneven number of rests, the
139         center one should be centered on the staff.
140       */
141       Drul_array<vector<Grob *> > ordered_rests;
142       for (vsize i = 0; i < rests.size (); i++)
143         {
144           Grob *r = Note_column::get_rest (rests[i]);
145
146           Direction d = get_grob_direction (r);
147           if (d)
148             ordered_rests[d].push_back (r);
149           else
150             rests[d]->warning (_ ("cannot resolve rest collision: rest direction not set"));
151         }
152
153       for (LEFT_and_RIGHT (d))
154         vector_sort (ordered_rests[d], rest_shift_less);
155
156       for (LEFT_and_RIGHT (d))
157         {
158           if (ordered_rests[d].size () < 1)
159             {
160               if (ordered_rests[-d].size () > 1)
161                 ordered_rests[-d][0]->warning (_ ("too many colliding rests"));
162
163               return SCM_BOOL_T;
164             }
165         }
166
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);
169
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;
173
174       if (diff > 0)
175         {
176           int amount_down = (int) ceil (diff / 2);
177           diff -= amount_down;
178           Rest::translate (ordered_rests[DOWN].back (),
179                            -2 * amount_down);
180           if (diff > 0)
181             Rest::translate (ordered_rests[UP].back (),
182                              2 * int (ceil (diff)));
183         }
184
185       for (LEFT_and_RIGHT (d))
186         {
187           for (vsize i = ordered_rests[d].size () - 1; i-- > 0;)
188             {
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];
191
192               Real diff = d * ((last_y - y) / staff_space);
193               if (diff > 0)
194                 Rest::translate (ordered_rests[d][i], d * (int) ceil (diff) * 2);
195             }
196         }
197     }
198   else
199     {
200       /*
201         Rests and notes.
202       */
203       // Count how many rests we move
204       Drul_array<int> rcount (0, 0);
205
206       for (vsize i = 0; i < rests.size (); i++)
207         {
208           Grob *rcol = rests[i];
209           Grob *rest = Note_column::get_rest (rcol);
210
211           Direction dir = get_grob_direction (rest);
212           if (!dir)
213             dir = Note_column::dir (rcol);
214
215           // Do not compute a translation for pre-positioned rests,
216           //  nor count them for the "too many colliding rests" warning
217           if (scm_is_number (rest->get_property ("staff-position")))
218             continue;
219
220           Grob *common = common_refpoint_of_array (notes, rcol, Y_AXIS);
221           Interval restdim = rest->extent (common, Y_AXIS);
222           if (restdim.is_empty ())
223             continue;
224
225           Real staff_space = Staff_symbol_referencer::staff_space (rcol);
226           Real minimum_dist = robust_scm2double (me->get_property ("minimum-distance"), 1.0) * staff_space;
227
228           Interval notedim;
229           for (vsize i = 0; i < notes.size (); i++)
230             {
231               if (Note_column::dir (notes[i]) == -dir
232                   // If the note has already happened (but it has a long
233                   // duration, so there is a collision), don't look at the stem.
234                   // 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 ())
236                 {
237                   /* try not to look at the stem, as looking at a beamed
238                      note may trigger beam positioning prematurely.
239
240                      This happens with dotted rests, which need Y
241                      positioning to compute X-positioning.
242                   */
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()");
248                 }
249               else
250                 notedim.unite (notes[i]->extent (common, Y_AXIS));
251             }
252
253           Real y = dir * max (0.0,
254                               -dir * restdim[-dir] + dir * notedim[dir] + minimum_dist);
255
256           // move discretely by half spaces.
257           int discrete_y = dir * int (ceil (y / (0.5 * dir * staff_space)));
258
259           Interval staff_span = Staff_symbol_referencer::staff_span (rest);
260           staff_span.widen (1);
261           // move by whole spaces inside the staff.
262           if (staff_span.contains
263               (Staff_symbol_referencer::get_position (rest) + discrete_y))
264             {
265               discrete_y = dir * int (ceil (dir * discrete_y / 2.0) * 2.0);
266             }
267
268           Rest::translate (rest, discrete_y);
269           if (rcount[dir]++)
270             rest->warning (_ ("too many colliding rests"));
271         }
272     }
273   return SCM_BOOL_T;
274 }
275
276 ADD_INTERFACE (Rest_collision,
277                "Move ordinary rests (not multi-measure nor pitched rests)"
278                " to avoid conflicts.",
279
280                /* properties */
281                "minimum-distance "
282                "positioning-done "
283                "elements "
284               );
285