]> git.donarmstrong.com Git - lilypond.git/blob - lily/rest-collision.cc
release: 1.4.11
[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--2001 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       SCM elts = rc->get_grob_property ("elements");
38       rc->set_grob_property ("elements", SCM_EOL);
39
40       do_shift (rc, elts);
41     }
42   
43   return gh_double2scm (0.0);
44 }
45
46 void
47 Rest_collision::add_column (Grob*me,Grob *p)
48 {
49   me->add_dependency (p);
50   Pointer_group_interface::add_element (me, "elements", p);
51
52   /*
53     only add callback for the rests, since we don't move anything else.
54
55  (not?)
56   */
57   p->add_offset_callback (Rest_collision::force_shift_callback_proc, Y_AXIS);
58   p->set_grob_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_grob_property ("rest"));
69
70   if (!s)
71     return SCM_BOOL_F;
72   else
73     return gh_cons (s->get_grob_property ("duration-log"),
74                     gh_int2scm (Rhythmic_head::dot_count (s)));
75 }
76
77 /*
78   TODO: fixme, fucks up if called twice on the same set of rests.
79
80   TODO: look at horizontal-shift to determine ordering between rests
81   for more than two voices.
82   
83  */
84 SCM
85 Rest_collision::do_shift (Grob *me, SCM elts)
86 {
87   /*
88     ugh. -> score  elt type
89    */
90   Link_array<Grob> rests;
91   Link_array<Grob> notes;
92   Grob * commony = 0;
93   for (SCM s = elts; gh_pair_p (s); s = gh_cdr (s))
94     {
95       
96       Grob * e = unsmob_grob (gh_car (s));
97       if (!e)
98         continue;
99       
100       if (!commony)
101         commony = e;
102       else
103         commony= commony->common_refpoint (e, Y_AXIS);
104       
105       if (unsmob_grob (e->get_grob_property ("rest")))
106         rests.push (e);
107       else
108         notes.push (e);
109     }
110
111   
112   /* 
113      handle rest-rest and rest-note collisions
114
115      [todo]
116      * decide not to print rest if too crowded?
117
118      * ignore rests under beams.
119    */
120
121   // no rests to collide
122   if (!rests.size ())
123     return SCM_UNSPECIFIED;
124
125   // no partners to collide with
126   if (rests.size () + notes.size () < 2)
127     return SCM_UNSPECIFIED;
128
129   // meisjes met meisjes
130   if (!notes.size ()) 
131     {
132       SCM characteristic = head_characteristic (rests[0]);
133       int i = 1;
134       for (; i < rests.size (); i++)
135         {
136           if (!gh_equal_p (head_characteristic (rests[i]), characteristic))
137             break;
138         }
139
140       /*
141         If all durations are the same, we'll check if there are more
142         rests than maximum-rest-count.
143         Otherwise (different durations), we'll try to display them all
144  (urg: all 3 of them, currently).
145        */
146       int display_count;
147       SCM s = me->get_grob_property ("maximum-rest-count");
148       if (i == rests.size ()
149           && gh_number_p (s) && gh_scm2int (s) < rests.size ())
150         {
151           display_count = gh_scm2int (s);
152           for (; i > display_count; i--)
153             {
154               Grob* r = unsmob_grob (rests[i-1]->get_grob_property ("rest"));
155               if (r)
156                 r->suicide ();
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
197       Grob * rcol = rests[0];
198
199       Direction dir = Note_column::dir (rcol);
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       // FIXME: staff ref'd?
208       Real staff_space = Staff_symbol_referencer::staff_space (rcol);
209  
210
211
212       Real minimum_dist = gh_scm2double (me->get_grob_property ("minimum-distance")) * staff_space;
213       
214       Grob *common = rcol;
215       for (int i = 0; i < notes.size (); i++)
216         common = common->common_refpoint (notes[i], Y_AXIS);
217
218       Interval notedim;
219       for (int i = 0; i < notes.size (); i++) 
220         {
221           notedim.unite (notes[i]->extent (common, Y_AXIS));
222         }
223
224       Interval inter (notedim);
225       inter.intersect (restdim);
226
227       Real dist =
228         minimum_dist +  dir * (notedim[dir] - restdim[-dir]) >? 0;
229
230
231       int stafflines = Staff_symbol_referencer::line_count (me);
232       if (!stafflines)
233         {
234           programming_error ("No staff line count ? ");
235           stafflines =5;
236         }
237
238       // move discretely by half spaces.
239       int discrete_dist = int (ceil (dist / (0.5 *staff_space)));
240
241       // move by whole spaces inside the staff.
242       if (discrete_dist < stafflines+1)
243         discrete_dist = int (ceil (discrete_dist / 2.0)* 2.0);
244       
245       Note_column::translate_rests (rcol,dir * discrete_dist);
246     }
247   return SCM_UNSPECIFIED;
248 }
249
250 void
251 Rest_collision::set_interface (Grob*me)
252 {
253   me->set_extent_callback (SCM_EOL, X_AXIS);
254   me->set_extent_callback (SCM_EOL, Y_AXIS);
255 }
256