]> git.donarmstrong.com Git - lilypond.git/blob - lily/rest-collision.cc
*** empty log message ***
[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 "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   Axis a = (Axis) scm_to_int (axis);
30   assert (a == Y_AXIS);
31
32   if (Note_column::has_rests (them))
33     {  
34       Grob * rc = unsmob_grob (them->get_property ("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_make_real (0.0);
43 }
44
45
46 MAKE_SCHEME_CALLBACK (Rest_collision, force_shift_callback_rest, 2);
47 SCM
48 Rest_collision::force_shift_callback_rest (SCM rest, SCM axis)
49 {
50   Grob *rest_grob = unsmob_grob (rest);
51   assert ((Axis) scm_to_int (axis) == Y_AXIS);
52
53   Grob *parent = rest_grob->get_parent (X_AXIS);
54   if (Note_column::has_interface (parent))
55     return force_shift_callback (parent->self_scm(), axis);
56   else
57     return scm_make_real (0.0);
58 }
59
60
61 void
62 Rest_collision::add_column (Grob*me, Grob *p)
63 {
64   me->add_dependency (p);
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   p->add_offset_callback (Rest_collision::force_shift_callback_proc, Y_AXIS);
74   p->set_property ("rest-collision", me->self_scm ());
75
76   Grob *rest = unsmob_grob (p->get_property ("rest"));
77   if (rest)
78     {
79       rest->add_offset_callback (Rest_collision::force_shift_callback_rest_proc,
80                                  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  */
89 SCM
90 Rest_collision::do_shift (Grob *me)
91 {
92   SCM elts = me->get_property ("elements");
93
94   Link_array<Grob> rests;
95   Link_array<Grob> notes;
96
97   for (SCM s = elts; scm_is_pair (s); s = scm_cdr (s))
98     {
99       Grob * e = unsmob_grob (scm_car (s));
100       if (unsmob_grob (e->get_property ("rest")))
101         {
102           /*
103             Ignore rests under beam.
104            */
105           Grob* st = unsmob_grob (e->get_property ("stem"));
106           if (st && unsmob_grob (st->get_property ("beam")))
107             continue;
108           
109           rests.push (e);
110         }
111       else
112         notes.push (e);
113     }
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
130   Real staff_space = Staff_symbol_referencer::staff_space (me);
131   /*
132     only rests
133   */
134   if (!notes.size ()) 
135     {
136
137       /*
138         This is incomplete: in case of an uneven number of rests, the
139         center one should be centered on the staff.
140        */
141       Drul_array< Link_array <Grob > > ordered_rests;
142       for (int i = 0; i < rests.size (); i++)
143         {
144           Grob * r = Note_column::get_rest (rests[i]);
145           
146           Direction d = get_grob_direction (r);
147           if (d)
148             {
149               ordered_rests[d].push (rests[i]);
150             }
151           else
152             rests[d]->warning (_("rest direction not set.  Cannot resolve collision."));
153         }
154
155       Direction d =  LEFT;
156       do {
157         ordered_rests[d].sort (Note_column::shift_compare);
158       } while (flip (&d) != LEFT);
159       
160       do {
161         if (ordered_rests[d].size () < 1)
162           {
163             if (ordered_rests[-d].size() > 1)
164               ordered_rests[-d][0]->warning (_ ("too many colliding rests"));
165           
166             return SCM_UNSPECIFIED;
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].top ()->extent (common, Y_AXIS)[UP]
175          - ordered_rests[UP].top ()->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].top (),
182                                         -2 * amount_down);
183           if (diff > 0)
184             Note_column::translate_rests (ordered_rests[UP].top (),
185                                           2 * int (ceil (diff)));
186         }
187
188       do {
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       } while (flip (&d) != LEFT);
199     }
200   else 
201     {
202       /*
203         Rests and notes.
204        */
205       if (rests.size () > 1)
206         {
207           warning (_ ("too many colliding rests"));
208         }
209       Grob * rcol = 0;
210       Direction dir = CENTER;
211
212       for (int 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 (int i = 0; i < notes.size (); i++) 
233         {
234           notedim.unite (notes[i]->extent (common, Y_AXIS));
235         }
236
237       Real dist =
238         minimum_dist +  dir * (notedim[dir] - restdim[-dir]) >? 0;
239
240       int stafflines = Staff_symbol_referencer::line_count (me);
241       if (!stafflines)
242         {
243           programming_error ("No staff line count ? ");
244           stafflines =5;
245         }
246       
247       // move discretely by half spaces.
248       int discrete_dist = int (ceil (dist / (0.5 *staff_space)));
249
250       // move by whole spaces inside the staff.
251       if (discrete_dist < stafflines+1)
252         discrete_dist = int (ceil (discrete_dist / 2.0)* 2.0);
253
254       Note_column::translate_rests (rcol, dir * discrete_dist);
255     }
256   return SCM_UNSPECIFIED;
257 }
258
259
260 ADD_INTERFACE (Rest_collision, "rest-collision-interface",
261                "Move around ordinary rests (not multi-measure-rests) to avoid "
262                "conflicts.",
263                "minimum-distance positioning-done elements");
264