]> git.donarmstrong.com Git - lilypond.git/blob - lily/rest-collision.cc
patch::: 1.3.33.jcn3
[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 #include <math.h>               // ceil.
9
10 #include "beam.hh"
11 #include "debug.hh"
12 #include "rest-collision.hh"
13 #include "note-column.hh"
14 #include "stem.hh"
15 #include "note-head.hh"
16 #include "collision.hh"
17 #include "paper-def.hh"
18 #include "rest.hh"
19 #include "group-interface.hh"
20 #include "staff-symbol-referencer.hh"
21 #include "duration.hh"
22
23 void
24 Rest_collision::add_column (Note_column *nc_l)
25 {
26   add_dependency (nc_l);
27   Group_interface gi (this);  
28   if (nc_l->rest_b ())
29     gi.name_ = "rests";
30   else
31     gi.name_ = "notes";
32   
33   gi.add_element (nc_l);
34 }
35
36 static Duration
37 to_duration (int type, int dots)
38 {
39   Duration d;
40   d.durlog_i_ = type;
41   d.dots_i_ = dots;
42   return d;
43 }
44
45 static Moment
46 rhythmic_head2mom (Rhythmic_head* r)
47 {
48   return to_duration (r->balltype_i (), r->dot_count ()).length_mom ();
49 }
50
51 static Rhythmic_head*
52 col2rhythmic_head (Note_column* c)
53 {
54   SCM s = c->get_elt_property ("rests");
55   assert (gh_pair_p (s));
56   Score_element* e = unsmob_element (gh_car (s));
57   return dynamic_cast<Rhythmic_head*> (e);
58 }
59
60 void
61 Rest_collision::before_line_breaking ()
62 {
63   Link_array<Note_column> rest_l_arr =
64     Group_interface__extract_elements (this, (Note_column*) 0, "rests");
65   Link_array<Note_column> ncol_l_arr =
66     Group_interface__extract_elements (this, (Note_column*) 0, "notes");
67                                       
68   
69   /* 
70      handle rest-rest and rest-note collisions
71
72      [todo]
73      * decide not to print rest if too crowded?
74
75      * ignore rests under beams.
76    */
77
78   // no rests to collide
79   if (!rest_l_arr.size())
80     return;
81
82   // no partners to collide with
83   if (rest_l_arr.size() + ncol_l_arr.size () < 2)
84     return;
85
86   // meisjes met meisjes
87   if (!ncol_l_arr.size()) 
88     {
89
90       /*
91         Oeps, nu zie ik 't pas: colommen met noten.
92         Het commentaar, dat ik ooit geschreven had: meisjes met meisjes,
93         is achterhaald: urg.
94
95        */
96
97       Moment m = rhythmic_head2mom (col2rhythmic_head (rest_l_arr[0]));
98       int i = 1;
99       for (; i < rest_l_arr.size (); i++)
100         {
101           Moment me = rhythmic_head2mom (col2rhythmic_head (rest_l_arr[i]));
102           if (me != m)
103             break;
104         }
105
106       /*
107         If all durations are the same, we'll check if there are more
108         rests than maximum-rest-count.
109         Otherwise (different durations), we'll try to display them all
110         (urg: all 3 of them, currently).
111        */
112       int display_count;
113       SCM s = get_elt_property ("maximum-rest-count");
114       if (i == rest_l_arr.size ()
115           && gh_number_p (s) && gh_int2scm (s) < rest_l_arr.size ())
116         {
117           display_count = gh_int2scm (s);
118 #if 0
119           /*
120             Setting surplus rests to transparent.
121             Only really necessary for maximum-rest-count == 0,
122             because rests wil be set at exact the same position
123             and thus overlap.
124           */
125           for (i--;i > display_count; i--)
126             col2rhythmic_head (rest_l_arr[i-1])
127               ->set_elt_property ("transparent", SCM_BOOL_T);
128 #endif
129         }
130       else
131         display_count = rest_l_arr.size ();
132       
133       /*
134         UGH.  Should get dims from table.  Should have minimum dist.
135        */
136       int dy = display_count > 2 ? 6 : 4;
137       if (display_count > 1)
138         {
139           rest_l_arr[0]->translate_rests (dy);  
140           rest_l_arr[1]->translate_rests (-dy);
141         }
142     }
143   // meisjes met jongetjes
144   else 
145     {
146       if (rest_l_arr.size () > 1)
147         {
148           warning (_("too many colliding rests"));
149         }
150       if (ncol_l_arr.size () > 1)
151         {
152           warning (_("too many notes for rest collision"));
153         }
154       Note_column * rcol = rest_l_arr[0];
155
156       // try to be opposite of noteheads. 
157       Direction dir = - ncol_l_arr[0]->dir();
158
159       Interval restdim = rcol->rest_dim ();
160       if (restdim.empty_b ())
161         return;
162       
163       // staff ref'd?
164       Real staff_space = paper_l()->get_var ("interline");
165
166         /* FIXME
167           staff_space =  rcol->rest_l_arr[0]->staff_space ();
168         */
169       Real half_staff_space_f = staff_space/2;
170       Real minimum_dist = paper_l ()->get_var ("restcollision_minimum_dist")
171         * half_staff_space_f;
172       
173       /*
174         assumption: ref points are the same. 
175        */
176       Interval notedim;
177       for (int i = 0; i < ncol_l_arr.size(); i++) 
178         {
179           notedim.unite (ncol_l_arr[i]->extent (Y_AXIS));
180         }
181
182       Interval inter (notedim);
183       inter.intersect (restdim);
184
185       Real dist =
186         minimum_dist +  dir * (notedim[dir] - restdim[-dir]) >? 0;
187
188
189       // FIXME
190       //int stafflines = 5; // rcol->rest_l_arr[0]->line_count;
191       int stafflines = Staff_symbol_referencer_interface (this).line_count ();
192       // hurg?
193       stafflines = stafflines != 0 ? stafflines : 5;
194       
195       // move discretely by half spaces.
196       int discrete_dist = int (ceil (dist / (0.5 *staff_space)));
197
198       // move by whole spaces inside the staff.
199       if (discrete_dist < stafflines+1)
200         discrete_dist = int (ceil (discrete_dist / 2.0)* 2.0);
201       
202       rcol->translate_rests (dir * discrete_dist);
203     }
204 }
205
206
207 Rest_collision::Rest_collision()
208 {
209   set_elt_property ("rests", SCM_EOL);
210   set_elt_property ("notes", SCM_EOL);
211   set_elt_property ("transparent", SCM_BOOL_T);
212   set_empty (X_AXIS);
213   set_empty (Y_AXIS);
214 }
215