]> git.donarmstrong.com Git - lilypond.git/blob - lily/rest-collision.cc
e80e0d9cd751c57a7d59aa00d8b7a24191b8d436
[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
22 MAKE_SCHEME_CALLBACK (Rest_collision,force_shift_callback,2);
23 SCM
24 Rest_collision::force_shift_callback (SCM element_smob, SCM axis)
25 {
26   Grob *them = unsmob_grob (element_smob);
27   Axis a = (Axis) gh_scm2int (axis);
28   assert (a == Y_AXIS);
29
30   Grob * rc = unsmob_grob (them->get_grob_property ("rest-collision"));
31
32   if (rc && !to_boolean (rc->get_grob_property ("positioning-done")))
33     {
34       rc->set_grob_property ("positioning-done", SCM_BOOL_T);
35
36       do_shift (rc);
37     }
38   
39   return gh_double2scm (0.0);
40 }
41
42 void
43 Rest_collision::add_column (Grob*me,Grob *p)
44 {
45   me->add_dependency (p);
46   Pointer_group_interface::add_grob (me, ly_symbol2scm ("elements"), p);
47
48   /*
49     only add callback for the rests, since we don't move anything
50     else.
51
52  (not?)
53   */
54   p->add_offset_callback (Rest_collision::force_shift_callback_proc, Y_AXIS);
55   p->set_grob_property ("rest-collision", me->self_scm ());
56 }
57
58
59 /*
60   Combination of dot-count and duration-log.
61  */
62 static SCM
63 head_characteristic (Grob * col)
64 {
65   Grob * s = unsmob_grob (col->get_grob_property ("rest"));
66
67   if (!s)
68     return SCM_BOOL_F;
69   else
70     return gh_cons (s->get_grob_property ("duration-log"),
71                     gh_int2scm (Rhythmic_head::dot_count (s)));
72 }
73
74 /*
75   TODO: fixme, fucks up if called twice on the same set of rests.
76
77   TODO: look at horizontal-shift to determine ordering between rests
78   for more than two voices.
79
80   TODO: look at previous note to determine vertical position?
81   
82  */
83 SCM
84 Rest_collision::do_shift (Grob *me)
85 {
86   SCM elts = me->get_grob_property ("elements");
87
88   Link_array<Grob> rests;
89   Link_array<Grob> notes;
90
91   for (SCM s = elts; gh_pair_p (s); s = ly_cdr (s))
92     {
93       Grob * e = unsmob_grob (ly_car (s));
94       if (unsmob_grob (e->get_grob_property ("rest")))
95         rests.push (e);
96       else
97         notes.push (e);
98     }
99
100   
101   /* 
102      handle rest-rest and rest-note collisions
103
104      [todo]
105      * decide not to print rest if too crowded?
106
107      * ignore rests under beams.
108    */
109
110   // no rests to collide
111   if (!rests.size ())
112     return SCM_UNSPECIFIED;
113
114   // no partners to collide with
115   if (rests.size () + notes.size () < 2)
116     {
117       if (rests.size () == 1
118           && Note_column::dir (rests[0]))
119         {
120           Note_column::translate_rests (rests[0],
121                                         4 * Note_column::dir (rests[0]));
122         }
123     }
124   // meisjes met meisjes
125   if (!notes.size ()) 
126     {
127       SCM characteristic = head_characteristic (rests[0]);
128       int i = 1;
129       for (; i < rests.size (); i++)
130         {
131           if (!gh_equal_p (head_characteristic (rests[i]), characteristic))
132             break;
133         }
134
135       /*
136         If all durations are the same, we'll check if there are more
137         rests than maximum-rest-count.
138         Otherwise (different durations), we'll try to display them all
139  (urg: all 3 of them, currently).
140        */
141       int display_count;
142       SCM s = me->get_grob_property ("maximum-rest-count");
143       if (i == rests.size ()
144           && gh_number_p (s) && gh_scm2int (s) < rests.size ())
145         {
146           display_count = gh_scm2int (s);
147           for (; i > display_count; i--)
148             {
149               Grob* r = unsmob_grob (rests[i-1]->get_grob_property ("rest"));
150               if (r)
151                 {
152                   Grob * d = unsmob_grob (r->get_grob_property ("dot"));
153                   if (d)
154                     d->suicide();
155                   r->suicide ();
156                 }
157               rests[i-1]->suicide ();
158             }
159         }
160       else
161         display_count = rests.size ();
162       
163       /*
164         Ugh. Should have minimum dist.
165
166         Ugh. What do we do if we have three different rests?
167         
168        */
169       int dy = display_count > 2 ? 6 : 4; // FIXME Should get dims from table.
170       if (display_count > 1)
171         {
172           Direction d0 = Note_column::dir (rests[0]);
173           Direction d1 = Note_column::dir (rests[1]);     
174
175           if (!d0 && !d1)
176             {
177               d0= UP;
178               d1 = DOWN;
179             }
180           else if (!d0)
181             d0 = - d1;
182           else if (!d1)
183             d1 = -d0;
184                 
185           Note_column::translate_rests (rests[0],d0 *dy);       
186           Note_column::translate_rests (rests[1], d1 *dy);
187         }
188     }
189   // meisjes met jongetjes
190   else 
191     {
192       if (rests.size () > 1)
193         {
194           warning (_ ("too many colliding rests"));
195         }
196       Grob * rcol = rests[0];
197       Direction dir = Note_column::dir (rests[0]);
198
199       if (!dir)
200         {
201           dir = - Note_column::dir (notes[0]);
202         }
203       Grob * r = unsmob_grob (rcol->get_grob_property ("rest"));
204       Interval restdim = r->extent (r, Y_AXIS); // ??
205
206       if (restdim.is_empty ())
207         return SCM_UNSPECIFIED;
208       
209
210       Real staff_space = Staff_symbol_referencer::staff_space (rcol);
211
212       Real minimum_dist = robust_scm2double (me->get_grob_property ("minimum-distance"), 1.0) * staff_space;
213
214
215       Grob *common = common_refpoint_of_array (notes, rcol, Y_AXIS);
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       Interval inter (notedim);
224       inter.intersect (restdim);
225
226       Real dist =
227         minimum_dist +  dir * (notedim[dir] - restdim[-dir]) >? 0;
228
229       int stafflines = Staff_symbol_referencer::line_count (me);
230       if (!stafflines)
231         {
232           programming_error ("No staff line count ? ");
233           stafflines =5;
234         }
235       
236       // move discretely by half spaces.
237       int discrete_dist = int (ceil (dist / (0.5 *staff_space)));
238
239       // move by whole spaces inside the staff.
240       if (discrete_dist < stafflines+1)
241         discrete_dist = int (ceil (discrete_dist / 2.0)* 2.0);
242       
243       Note_column::translate_rests (rcol,dir * discrete_dist);
244     }
245   return SCM_UNSPECIFIED;
246 }
247
248
249 ADD_INTERFACE (Rest_collision,"rest-collision-interface",
250   "Move around ordinary rests (not multi-measure-rests) to avoid "
251 "conflicts.",
252   "maximum-rest-count minimum-distance positioning-done elements");
253