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