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