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