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