]> 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--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) gh_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 gh_double2scm (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 gh_cons (s->get_property ("duration-log"),
74                     gh_int2scm (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; gh_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 () + ordered_rests[DOWN].size () < 2)
154         return SCM_UNSPECIFIED;
155
156       Grob *common = common_refpoint_of_array (ordered_rests[DOWN], me, Y_AXIS);
157       common =  common_refpoint_of_array (ordered_rests[UP], common, Y_AXIS);
158
159       Real diff = 
160         (ordered_rests[DOWN].top ()->extent (common, Y_AXIS)[UP]
161          - ordered_rests[UP].top ()->extent (common, Y_AXIS)[DOWN]) /staff_space;
162
163       if (diff > 0)
164         {
165           int amount_down = (int) ceil (diff / 2); 
166           diff -= amount_down;
167           Note_column::translate_rests (ordered_rests[DOWN].top (),
168                                         -2 * amount_down);
169           if (diff > 0)
170             Note_column::translate_rests (ordered_rests[UP].top (),
171                                           2 * int (ceil (diff)));
172         }
173
174       do {
175         for (int i = ordered_rests[d].size () -1; i-- > 0;)
176           {
177             Real last_y = ordered_rests[d][i+1]->extent (common, Y_AXIS)[d];
178             Real y = ordered_rests[d][i]->extent (common, Y_AXIS)[-d];
179
180             Real diff = d * ((last_y - y) /staff_space);
181             if (diff > 0)
182               Note_column::translate_rests (ordered_rests[d][i],d * (int) ceil (diff) * 2);
183           }
184       } while (flip (&d) != LEFT);
185     }
186   else 
187     {
188       /*
189         Rests and notes.
190        */
191       if (rests.size () > 1)
192         {
193           warning (_ ("too many colliding rests"));
194         }
195       Grob * rcol = 0;
196       Direction dir = CENTER;
197
198       for (int i= rests.size (); !rcol && i--;)
199         if (Note_column::dir (rests[i]))
200           {
201             dir = Note_column::dir (rests[i]);
202             rcol = rests[i];
203           }
204
205       if (!rcol)
206         return SCM_UNSPECIFIED;
207       
208       Grob *common = common_refpoint_of_array (notes, rcol, Y_AXIS);
209       
210       Interval restdim = rcol->extent (common, Y_AXIS);
211       if (restdim.is_empty ())
212         return SCM_UNSPECIFIED;
213       
214       Real staff_space = Staff_symbol_referencer::staff_space (rcol);
215       Real minimum_dist = robust_scm2double (me->get_property ("minimum-distance"), 1.0) * staff_space;
216
217       Interval notedim;
218       for (int i = 0; i < notes.size (); i++) 
219         {
220           notedim.unite (notes[i]->extent (common, Y_AXIS));
221         }
222
223       Real dist =
224         minimum_dist +  dir * (notedim[dir] - restdim[-dir]) >? 0;
225
226       int stafflines = Staff_symbol_referencer::line_count (me);
227       if (!stafflines)
228         {
229           programming_error ("No staff line count ? ");
230           stafflines =5;
231         }
232       
233       // move discretely by half spaces.
234       int discrete_dist = int (ceil (dist / (0.5 *staff_space)));
235
236       // move by whole spaces inside the staff.
237       if (discrete_dist < stafflines+1)
238         discrete_dist = int (ceil (discrete_dist / 2.0)* 2.0);
239
240       Note_column::translate_rests (rcol,dir * discrete_dist);
241     }
242   return SCM_UNSPECIFIED;
243 }
244
245
246 ADD_INTERFACE (Rest_collision,"rest-collision-interface",
247   "Move around ordinary rests (not multi-measure-rests) to avoid "
248 "conflicts.",
249   "minimum-distance positioning-done elements");
250