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