]> git.donarmstrong.com Git - lilypond.git/blob - lily/rest-collision.cc
Nitpick run.
[lilypond.git] / lily / rest-collision.cc
1 /*
2   rest-collision.cc -- implement Rest_collision
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1997--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include "rest-collision.hh"
10
11 #include <math.h>               // ceil.
12
13 #include "warn.hh"
14 #include "note-column.hh"
15 #include "stem.hh"
16 #include "rhythmic-head.hh"
17 #include "output-def.hh"
18 #include "rest.hh"
19 #include "pointer-group-interface.hh"
20 #include "staff-symbol-referencer.hh"
21 #include "duration.hh"
22 #include "directional-element-interface.hh"
23
24 MAKE_SCHEME_CALLBACK (Rest_collision, force_shift_callback, 2);
25 SCM
26 Rest_collision::force_shift_callback (SCM element_smob, SCM axis)
27 {
28   Grob *them = unsmob_grob (element_smob);
29   (void) axis;
30   assert (scm_to_int (axis) == Y_AXIS);
31
32   if (Note_column::has_rests (them))
33     {
34       Grob *rc = unsmob_grob (them->get_object ("rest-collision"));
35
36       if (rc && !to_boolean (rc->get_property ("positioning-done")))
37         {
38           rc->set_property ("positioning-done", SCM_BOOL_T);
39           do_shift (rc);
40         }
41     }
42   return scm_from_double (0.0);
43 }
44
45 MAKE_SCHEME_CALLBACK (Rest_collision, force_shift_callback_rest, 2);
46 SCM
47 Rest_collision::force_shift_callback_rest (SCM rest, SCM axis)
48 {
49   Grob *rest_grob = unsmob_grob (rest);
50   assert ((Axis) scm_to_int (axis) == Y_AXIS);
51
52   Grob *parent = rest_grob->get_parent (X_AXIS);
53   if (Note_column::has_interface (parent))
54     return force_shift_callback (parent->self_scm (), axis);
55   else
56     return scm_from_double (0.0);
57 }
58
59 void
60 Rest_collision::add_column (Grob *me, Grob *p)
61 {
62   me->add_dependency (p);
63   Pointer_group_interface::add_grob (me, ly_symbol2scm ("elements"), p);
64
65   /*
66     only add callback for the rests, since we don't move anything
67     else.
68
69     (not?)
70   */
71   p->add_offset_callback (Rest_collision::force_shift_callback_proc, Y_AXIS);
72   p->set_object ("rest-collision", me->self_scm ());
73
74   Grob *rest = unsmob_grob (p->get_object ("rest"));
75   if (rest)
76     {
77       rest->add_offset_callback (Rest_collision::force_shift_callback_rest_proc,
78                                  Y_AXIS);
79     }
80 }
81
82 /*
83   TODO: look at horizontal-shift to determine ordering between rests
84   for more than two voices.
85 */
86 SCM
87 Rest_collision::do_shift (Grob *me)
88 {
89   extract_grob_set (me, "elements", elts);
90
91   Link_array<Grob> rests;
92   Link_array<Grob> notes;
93
94   for (int i = 0; i < elts.size (); i++)
95     {
96       Grob *e = elts[i];
97       if (unsmob_grob (e->get_object ("rest")))
98         {
99           /*
100             Ignore rests under beam.
101           */
102           Grob *st = unsmob_grob (e->get_object ("stem"));
103           if (st && unsmob_grob (st->get_object ("beam")))
104             continue;
105
106           rests.push (e);
107         }
108       else
109         notes.push (e);
110     }
111
112   /*
113     handle rest-rest and rest-note collisions
114
115     [todo]
116     * decide not to print rest if too crowded?
117     */
118
119   /*
120     no partners to collide with
121   */
122   if (rests.size () + notes.size () < 2)
123     return SCM_UNSPECIFIED;
124
125   Real staff_space = Staff_symbol_referencer::staff_space (me);
126   /*
127     only rests
128   */
129   if (!notes.size ())
130     {
131
132       /*
133         This is incomplete: in case of an uneven number of rests, the
134         center one should be centered on the staff.
135       */
136       Drul_array<Link_array<Grob> > ordered_rests;
137       for (int i = 0; i < rests.size (); i++)
138         {
139           Grob *r = Note_column::get_rest (rests[i]);
140
141           Direction d = get_grob_direction (r);
142           if (d)
143             ordered_rests[d].push (rests[i]);
144           else
145             rests[d]->warning (_ ("can't resolve rest collission: rest direction not set"));
146         }
147
148       Direction d = LEFT;
149       do
150         ordered_rests[d].sort (Note_column::shift_compare);
151       while (flip (&d) != LEFT)
152         ;
153
154       do
155         {
156           if (ordered_rests[d].size () < 1)
157             {
158               if (ordered_rests[-d].size () > 1)
159                 ordered_rests[-d][0]->warning (_ ("too many colliding rests"));
160
161               return SCM_UNSPECIFIED;
162             }
163         }
164       while (flip (&d) != LEFT);
165
166       Grob *common = common_refpoint_of_array (ordered_rests[DOWN], me, Y_AXIS);
167       common = common_refpoint_of_array (ordered_rests[UP], common, Y_AXIS);
168
169       Real diff
170         = (ordered_rests[DOWN].top ()->extent (common, Y_AXIS)[UP]
171            - ordered_rests[UP].top ()->extent (common, Y_AXIS)[DOWN]) / staff_space;
172
173       if (diff > 0)
174         {
175           int amount_down = (int) ceil (diff / 2);
176           diff -= amount_down;
177           Note_column::translate_rests (ordered_rests[DOWN].top (),
178                                         -2 * amount_down);
179           if (diff > 0)
180             Note_column::translate_rests (ordered_rests[UP].top (),
181                                           2 * int (ceil (diff)));
182         }
183
184       do
185         {
186           for (int i = ordered_rests[d].size () -1; i-- > 0;)
187             {
188               Real last_y = ordered_rests[d][i + 1]->extent (common, Y_AXIS)[d];
189               Real y = ordered_rests[d][i]->extent (common, Y_AXIS)[-d];
190
191               Real diff = d * ((last_y - y) / staff_space);
192               if (diff > 0)
193                 Note_column::translate_rests (ordered_rests[d][i], d * (int) ceil (diff) * 2);
194             }
195         }
196       while (flip (&d) != LEFT);
197     }
198   else
199     {
200       /*
201         Rests and notes.
202       */
203       if (rests.size () > 1)
204         warning (_ ("too many colliding rests"));
205       Grob *rcol = 0;
206       Direction dir = CENTER;
207
208       for (int i = rests.size (); !rcol && i--;)
209         if (Note_column::dir (rests[i]))
210           {
211             dir = Note_column::dir (rests[i]);
212             rcol = rests[i];
213           }
214
215       if (!rcol)
216         return SCM_UNSPECIFIED;
217
218       Grob *common = common_refpoint_of_array (notes, rcol, Y_AXIS);
219
220       Interval restdim = rcol->extent (common, Y_AXIS);
221       if (restdim.is_empty ())
222         return SCM_UNSPECIFIED;
223
224       Real staff_space = Staff_symbol_referencer::staff_space (rcol);
225       Real minimum_dist = robust_scm2double (me->get_property ("minimum-distance"), 1.0) * staff_space;
226
227       Interval notedim;
228       for (int i = 0; i < notes.size (); i++)
229         notedim.unite (notes[i]->extent (common, Y_AXIS));
230
231       Real dist
232         = minimum_dist + dir * max (notedim[dir] - restdim[-dir], 0.0);
233
234       int stafflines = Staff_symbol_referencer::line_count (me);
235       if (!stafflines)
236         {
237           programming_error ("no staff line count");
238           stafflines = 5;
239         }
240
241       // move discretely by half spaces.
242       int discrete_dist = int (ceil (dist / (0.5 * staff_space)));
243
244       // move by whole spaces inside the staff.
245       if (discrete_dist < stafflines + 1)
246         discrete_dist = int (ceil (discrete_dist / 2.0) * 2.0);
247
248       Note_column::translate_rests (rcol, dir * discrete_dist);
249     }
250   return SCM_UNSPECIFIED;
251 }
252
253 ADD_INTERFACE (Rest_collision, "rest-collision-interface",
254                "Move around ordinary rests (not multi-measure-rests) to avoid "
255                "conflicts.",
256                "minimum-distance positioning-done elements");
257