]> git.donarmstrong.com Git - lilypond.git/blob - lily/rest-collision.cc
2569c7ab2b9153f5241faa423e4ba0d659b39633
[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 SCM
86 Rest_collision::do_shift (Grob *me, SCM elts)
87 {
88   /*
89     ugh. -> score  elt type
90    */
91   Link_array<Grob> rests;
92   Link_array<Grob> notes;
93   Grob * commony = 0;
94   for (SCM s = elts; gh_pair_p (s); s = ly_cdr (s))
95     {
96       
97       Grob * e = unsmob_grob (ly_car (s));
98       if (!e)
99         continue;
100       
101       if (!commony)
102         commony = e;
103       else
104         commony= commony->common_refpoint (e, Y_AXIS);
105       
106       if (unsmob_grob (e->get_grob_property ("rest")))
107         rests.push (e);
108       else
109         notes.push (e);
110     }
111
112   
113   /* 
114      handle rest-rest and rest-note collisions
115
116      [todo]
117      * decide not to print rest if too crowded?
118
119      * ignore rests under beams.
120    */
121
122   // no rests to collide
123   if (!rests.size ())
124     return SCM_UNSPECIFIED;
125
126   // no partners to collide with
127   if (rests.size () + notes.size () < 2)
128     return SCM_UNSPECIFIED;
129
130   // meisjes met meisjes
131   if (!notes.size ()) 
132     {
133       SCM characteristic = head_characteristic (rests[0]);
134       int i = 1;
135       for (; i < rests.size (); i++)
136         {
137           if (!gh_equal_p (head_characteristic (rests[i]), characteristic))
138             break;
139         }
140
141       /*
142         If all durations are the same, we'll check if there are more
143         rests than maximum-rest-count.
144         Otherwise (different durations), we'll try to display them all
145  (urg: all 3 of them, currently).
146        */
147       int display_count;
148       SCM s = me->get_grob_property ("maximum-rest-count");
149       if (i == rests.size ()
150           && gh_number_p (s) && gh_scm2int (s) < rests.size ())
151         {
152           display_count = gh_scm2int (s);
153           for (; i > display_count; i--)
154             {
155               Grob* r = unsmob_grob (rests[i-1]->get_grob_property ("rest"));
156               if (r)
157                 r->suicide ();
158               rests[i-1]->suicide ();
159             }
160         }
161       else
162         display_count = rests.size ();
163       
164       /*
165         Ugh. Should have minimum dist.
166
167         Ugh. What do we do if we have three different rests?
168         
169        */
170       int dy = display_count > 2 ? 6 : 4; // FIXME Should get dims from table.
171       if (display_count > 1)
172         {
173           Direction d0 = Note_column::dir (rests[0]);
174           Direction d1 = Note_column::dir (rests[1]);     
175
176           if (!d0 && !d1)
177             {
178               d0= UP;
179               d1 = DOWN;
180             }
181           else if (!d0)
182             d0 = - d1;
183           else if (!d1)
184             d1 = -d0;
185                 
186           Note_column::translate_rests (rests[0],d0 *dy);       
187           Note_column::translate_rests (rests[1], d1 *dy);
188         }
189     }
190   // meisjes met jongetjes
191   else 
192     {
193       if (rests.size () > 1)
194         {
195           warning (_ ("too many colliding rests"));
196         }
197       Grob * rcol = rests[0];
198       Direction dir = Note_column::dir (rests[0]);
199  
200       Grob * r = unsmob_grob (rcol->get_grob_property ("rest"));
201       Interval restdim = r->extent (r, Y_AXIS); // ??
202
203       if (restdim.empty_b ())
204         return SCM_UNSPECIFIED;
205       
206
207       Real staff_space = Staff_symbol_referencer::staff_space (rcol);
208
209       Real minimum_dist = gh_scm2double (me->get_grob_property ("minimum-distance")) * staff_space;
210
211
212       Grob *common = rcol;
213       for (int i = 0; i < notes.size (); i++)
214         common = common->common_refpoint (notes[i], Y_AXIS);
215
216       Interval notedim;
217       for (int i = 0; i < notes.size (); i++) 
218         {
219           notedim.unite (notes[i]->extent (common, Y_AXIS));
220         }
221
222       Interval inter (notedim);
223       inter.intersect (restdim);
224
225       Real dist =
226         minimum_dist +  dir * (notedim[dir] - restdim[-dir]) >? 0;
227
228       int stafflines = Staff_symbol_referencer::line_count (me);
229       if (!stafflines)
230         {
231           programming_error ("No staff line count ? ");
232           stafflines =5;
233         }
234       
235       // move discretely by half spaces.
236       int discrete_dist = int (ceil (dist / (0.5 *staff_space)));
237
238       // move by whole spaces inside the staff.
239       if (discrete_dist < stafflines+1)
240         discrete_dist = int (ceil (discrete_dist / 2.0)* 2.0);
241       
242       Note_column::translate_rests (rcol,dir * discrete_dist);
243     }
244   return SCM_UNSPECIFIED;
245 }
246
247
248 ADD_INTERFACE (Rest_collision,"rest-collision-interface",
249   "Move around ordinary rests (not multi-measure-rests) to avoid
250 conflicts.",
251   "maximum-rest-count minimum-distance elements");
252