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