]> git.donarmstrong.com Git - lilypond.git/blob - lily/rest-collision.cc
9f4588d0ed75bacfc166afc36b3776acd1a9cabb
[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--2002 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include <math.h>               // ceil.
10
11 #include "debug.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)
33     {
34       /*
35         Done: destruct pointers, so we do the shift only once.
36
37         TODO: use rest-collision-done
38       */
39       SCM elts = rc->get_grob_property ("elements");
40       rc->set_grob_property ("elements", SCM_EOL);
41
42       do_shift (rc, elts);
43     }
44   
45   return gh_double2scm (0.0);
46 }
47
48 void
49 Rest_collision::add_column (Grob*me,Grob *p)
50 {
51   me->add_dependency (p);
52   Pointer_group_interface::add_grob (me, ly_symbol2scm ("elements"), p);
53
54   /*
55     only add callback for the rests, since we don't move anything else.
56
57  (not?)
58   */
59   p->add_offset_callback (Rest_collision::force_shift_callback_proc, Y_AXIS);
60   p->set_grob_property ("rest-collision", me->self_scm ());
61 }
62
63
64 /*
65   Combination of dot-count and duration-log.
66  */
67 static SCM
68 head_characteristic (Grob * col)
69 {
70   Grob * s = unsmob_grob (col->get_grob_property ("rest"));
71
72   if (!s)
73     return SCM_BOOL_F;
74   else
75     return gh_cons (s->get_grob_property ("duration-log"),
76                     gh_int2scm (Rhythmic_head::dot_count (s)));
77 }
78
79 /*
80   TODO: fixme, fucks up if called twice on the same set of rests.
81
82   TODO: look at horizontal-shift to determine ordering between rests
83   for more than two voices.
84   
85  */
86 SCM
87 Rest_collision::do_shift (Grob *me, SCM elts)
88 {
89   /*
90     ugh. -> score  elt type
91    */
92   Link_array<Grob> rests;
93   Link_array<Grob> notes;
94   Grob * commony = 0;
95   for (SCM s = elts; gh_pair_p (s); s = ly_cdr (s))
96     {
97       
98       Grob * e = unsmob_grob (ly_car (s));
99       if (!e)
100         continue;
101       
102       if (!commony)
103         commony = e;
104       else
105         commony= commony->common_refpoint (e, Y_AXIS);
106       
107       if (unsmob_grob (e->get_grob_property ("rest")))
108         rests.push (e);
109       else
110         notes.push (e);
111     }
112
113   
114   /* 
115      handle rest-rest and rest-note collisions
116
117      [todo]
118      * decide not to print rest if too crowded?
119
120      * ignore rests under beams.
121    */
122
123   // no rests to collide
124   if (!rests.size ())
125     return SCM_UNSPECIFIED;
126
127   // no partners to collide with
128   if (rests.size () + notes.size () < 2)
129     return SCM_UNSPECIFIED;
130
131   // meisjes met meisjes
132   if (!notes.size ()) 
133     {
134       SCM characteristic = head_characteristic (rests[0]);
135       int i = 1;
136       for (; i < rests.size (); i++)
137         {
138           if (!gh_equal_p (head_characteristic (rests[i]), characteristic))
139             break;
140         }
141
142       /*
143         If all durations are the same, we'll check if there are more
144         rests than maximum-rest-count.
145         Otherwise (different durations), we'll try to display them all
146  (urg: all 3 of them, currently).
147        */
148       int display_count;
149       SCM s = me->get_grob_property ("maximum-rest-count");
150       if (i == rests.size ()
151           && gh_number_p (s) && gh_scm2int (s) < rests.size ())
152         {
153           display_count = gh_scm2int (s);
154           for (; i > display_count; i--)
155             {
156               Grob* r = unsmob_grob (rests[i-1]->get_grob_property ("rest"));
157               if (r)
158                 r->suicide ();
159               rests[i-1]->suicide ();
160             }
161         }
162       else
163         display_count = rests.size ();
164       
165       /*
166         Ugh. Should have minimum dist.
167
168         Ugh. What do we do if we have three different rests?
169         
170        */
171       int dy = display_count > 2 ? 6 : 4; // FIXME Should get dims from table.
172       if (display_count > 1)
173         {
174           Direction d0 = Note_column::dir (rests[0]);
175           Direction d1 = Note_column::dir (rests[1]);     
176
177           if (!d0 && !d1)
178             {
179               d0= UP;
180               d1 = DOWN;
181             }
182           else if (!d0)
183             d0 = - d1;
184           else if (!d1)
185             d1 = -d0;
186                 
187           Note_column::translate_rests (rests[0],d0 *dy);       
188           Note_column::translate_rests (rests[1], d1 *dy);
189         }
190     }
191   // meisjes met jongetjes
192   else 
193     {
194       if (rests.size () > 1)
195         {
196           warning (_ ("too many colliding rests"));
197         }
198       Grob * rcol = rests[0];
199       Direction dir = Note_column::dir (rests[0]);
200  
201       Grob * r = unsmob_grob (rcol->get_grob_property ("rest"));
202       Interval restdim = r->extent (r, Y_AXIS); // ??
203
204       if (restdim.empty_b ())
205         return SCM_UNSPECIFIED;
206       
207
208       Real staff_space = Staff_symbol_referencer::staff_space (rcol);
209
210       Real minimum_dist = gh_scm2double (me->get_grob_property ("minimum-distance")) * staff_space;
211
212
213       Grob *common = rcol;
214       for (int i = 0; i < notes.size (); i++)
215         common = common->common_refpoint (notes[i], 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 void
249 Rest_collision::set_interface (Grob*me)
250 {
251   me->set_extent_callback (SCM_EOL, X_AXIS);
252   me->set_extent_callback (SCM_EOL, Y_AXIS);
253 }
254
255
256