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