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