]> git.donarmstrong.com Git - lilypond.git/blob - lily/rest-collision.cc
b09bf842585def0e1d08f0eda3798e86383b38b7
[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--2000 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   Score_element *them = unsmob_element (element_smob);
27   Axis a = (Axis) gh_scm2int (axis);
28   assert (a == Y_AXIS);
29
30   Score_element * rc = unsmob_element (them->get_elt_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_elt_property ("elements");
38       rc->set_elt_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 (Score_element*me,Score_element *p)
48 {
49   me->add_dependency (p);
50   Pointer_group_interface::add_element (me, "elements", p);
51
52   p->add_offset_callback (Rest_collision::force_shift_callback_proc, Y_AXIS);
53   p->set_elt_property ("rest-collision", me->self_scm ());
54 }
55
56
57 /*
58   Combination of dot-count and duration-log.
59  */
60 static SCM
61 head_characteristic (Score_element * col)
62 {
63   Score_element * s = unsmob_element (col->get_elt_property ("rest"));
64
65   if (!s)
66     return SCM_BOOL_F;
67   else
68     return gh_cons (s->get_elt_property ("duration-log"),
69                     gh_int2scm (Rhythmic_head::dot_count (s)));
70 }
71
72 /*
73   TODO: fixme, fucks up if called twice on the same set of rests.
74  */
75 SCM
76 Rest_collision::do_shift (Score_element *me, SCM elts)
77 {
78   /*
79     ugh. -> score  elt type
80    */
81   Link_array<Score_element> rests;
82   Link_array<Score_element> notes;
83
84   for (SCM s = elts; gh_pair_p (s); s = gh_cdr (s))
85     {
86       Score_element * e = unsmob_element (gh_car (s));
87       if (e && unsmob_element (e->get_elt_property ("rest")))
88         rests.push (e);
89       else
90         notes.push (e);
91     }
92
93   
94   /* 
95      handle rest-rest and rest-note collisions
96
97      [todo]
98      * decide not to print rest if too crowded?
99
100      * ignore rests under beams.
101    */
102
103   // no rests to collide
104   if (!rests.size())
105     return SCM_UNSPECIFIED;
106
107   // no partners to collide with
108   if (rests.size() + notes.size () < 2)
109     return SCM_UNSPECIFIED;
110
111   // meisjes met meisjes
112   if (!notes.size()) 
113     {
114       SCM characteristic = head_characteristic  (rests[0]);
115       int i = 1;
116       for (; i < rests.size (); i++)
117         {
118           if (!gh_equal_p (head_characteristic  (rests[i]), characteristic))
119             break;
120         }
121
122       /*
123         If all durations are the same, we'll check if there are more
124         rests than maximum-rest-count.
125         Otherwise (different durations), we'll try to display them all
126         (urg: all 3 of them, currently).
127        */
128       int display_count;
129       SCM s = me->get_elt_property ("maximum-rest-count");
130       if (i == rests.size ()
131           && gh_number_p (s) && gh_scm2int (s) < rests.size ())
132         {
133           display_count = gh_scm2int (s);
134           for (; i > display_count; i--)
135             {
136               Score_element* r = unsmob_element (rests[i-1]->get_elt_property ("rest"));
137               if (r)
138                 r->suicide ();
139               rests[i-1]->suicide ();
140             }
141         }
142       else
143         display_count = rests.size ();
144       
145       /*
146         Ugh. Should have minimum dist.
147
148         Ugh. What do we do if we have three different rests?
149         
150        */
151       int dy = display_count > 2 ? 6 : 4; // FIXME Should get dims from table.
152       if (display_count > 1)
153         {
154           Direction d0 = Note_column::dir (rests[0]);
155           Direction d1 = Note_column::dir (rests[1]);     
156
157           if (!d0 && !d1)
158             {
159               d0= UP;
160               d1 = DOWN;
161             }
162           else if (!d0)
163             d0 = - d1;
164           else if (!d1)
165             d1 = -d0;
166                 
167           Note_column::translate_rests (rests[0],d0 *dy);       
168           Note_column::translate_rests (rests[1], d1 *dy);
169         }
170     }
171   // meisjes met jongetjes
172   else 
173     {
174       if (rests.size () > 1)
175         {
176           warning (_("too many colliding rests"));
177         }
178       if (notes.size () > 1)
179         {
180           warning (_("too many notes for rest collision"));
181         }
182       Score_element * rcol = rests[0];
183
184       // try to be opposite of noteheads. 
185       Direction dir = - Note_column::dir (notes[0]);
186
187       Interval restdim = Note_column::rest_dim (rcol);
188       if (restdim.empty_b ())
189         return SCM_UNSPECIFIED;
190       
191       // staff ref'd?
192       Real staff_space = me->paper_l()->get_var ("staffspace");
193
194         /* FIXME
195           staff_space =  rcol->rests[0]->staff_space ();
196         */
197       Real minimum_dist = gh_scm2double (me->get_elt_property ("minimum-distance")) * staff_space;
198       
199       /*
200         assumption: ref points are the same. 
201        */
202       Interval notedim;
203       for (int i = 0; i < notes.size(); i++) 
204         {
205           notedim.unite (notes[i]->extent (notes[i],Y_AXIS)); // refp?
206         }
207
208       Interval inter (notedim);
209       inter.intersect (restdim);
210
211       Real dist =
212         minimum_dist +  dir * (notedim[dir] - restdim[-dir]) >? 0;
213
214
215       // FIXME
216       //int stafflines = 5; // rcol->rests[0]->line_count;
217       int stafflines = Staff_symbol_referencer::line_count (me);
218       // hurg?
219       stafflines = stafflines != 0 ? stafflines : 5;
220       
221       // move discretely by half spaces.
222       int discrete_dist = int (ceil (dist / (0.5 *staff_space)));
223
224       // move by whole spaces inside the staff.
225       if (discrete_dist < stafflines+1)
226         discrete_dist = int (ceil (discrete_dist / 2.0)* 2.0);
227       
228       Note_column::translate_rests (rcol,dir * discrete_dist);
229     }
230   return SCM_UNSPECIFIED;
231 }
232
233 void
234 Rest_collision::set_interface (Score_element*me)
235 {
236   me->set_extent_callback (SCM_EOL, X_AXIS);
237   me->set_extent_callback (SCM_EOL, Y_AXIS);
238 }
239