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