]> git.donarmstrong.com Git - lilypond.git/blob - lily/rest-collision.cc
release: 1.3.56
[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   Pointer_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 /*
52   ugh
53  */
54 static Rhythmic_head*
55 col2rhythmic_head (Note_column* c)
56 {
57   SCM s = c->get_elt_pointer ("rests");
58   assert (gh_pair_p (s));
59   Score_element* e = unsmob_element (gh_car (s));
60   return dynamic_cast<Rhythmic_head*> (e);
61 }
62
63 void
64 Rest_collision::before_line_breaking ()
65 {
66   Link_array<Note_column> rest_l_arr =
67     Pointer_group_interface__extract_elements (this, (Note_column*) 0, "rests");
68   Link_array<Note_column> ncol_l_arr =
69     Pointer_group_interface__extract_elements (this, (Note_column*) 0, "notes");
70                                       
71   
72   /* 
73      handle rest-rest and rest-note collisions
74
75      [todo]
76      * decide not to print rest if too crowded?
77
78      * ignore rests under beams.
79    */
80
81   // no rests to collide
82   if (!rest_l_arr.size())
83     return;
84
85   // no partners to collide with
86   if (rest_l_arr.size() + ncol_l_arr.size () < 2)
87     return;
88
89   // meisjes met meisjes
90   if (!ncol_l_arr.size()) 
91     {
92       Moment m = rhythmic_head2mom (col2rhythmic_head (rest_l_arr[0]));
93       int i = 1;
94       for (; i < rest_l_arr.size (); i++)
95         {
96           Moment me = rhythmic_head2mom (col2rhythmic_head (rest_l_arr[i]));
97           if (me != m)
98             break;
99         }
100
101       /*
102         If all durations are the same, we'll check if there are more
103         rests than maximum-rest-count.
104         Otherwise (different durations), we'll try to display them all
105         (urg: all 3 of them, currently).
106        */
107       int display_count;
108       SCM s = get_elt_property ("maximum-rest-count");
109       if (i == rest_l_arr.size ()
110           && gh_number_p (s) && gh_scm2int (s) < rest_l_arr.size ())
111         {
112           display_count = gh_scm2int (s);
113           for (; i > display_count; i--)
114             col2rhythmic_head (rest_l_arr[i-1])
115               ->set_elt_property ("molecule-callback", SCM_BOOL_T);
116         }
117       else
118         display_count = rest_l_arr.size ();
119       
120       /*
121         UGH.  Should get dims from table.  Should have minimum dist.
122        */
123       int dy = display_count > 2 ? 6 : 4;
124       if (display_count > 1)
125         {
126           rest_l_arr[0]->translate_rests (dy);  
127           rest_l_arr[1]->translate_rests (-dy);
128         }
129     }
130   // meisjes met jongetjes
131   else 
132     {
133       if (rest_l_arr.size () > 1)
134         {
135           warning (_("too many colliding rests"));
136         }
137       if (ncol_l_arr.size () > 1)
138         {
139           warning (_("too many notes for rest collision"));
140         }
141       Note_column * rcol = rest_l_arr[0];
142
143       // try to be opposite of noteheads. 
144       Direction dir = - ncol_l_arr[0]->dir();
145
146       Interval restdim = rcol->rest_dim ();
147       if (restdim.empty_b ())
148         return;
149       
150       // staff ref'd?
151       Real staff_space = paper_l()->get_var ("interline");
152
153         /* FIXME
154           staff_space =  rcol->rest_l_arr[0]->staff_space ();
155         */
156       Real half_staff_space_f = staff_space/2;
157       Real minimum_dist = paper_l ()->get_var ("restcollision_minimum_dist")
158         * half_staff_space_f;
159       
160       /*
161         assumption: ref points are the same. 
162        */
163       Interval notedim;
164       for (int i = 0; i < ncol_l_arr.size(); i++) 
165         {
166           notedim.unite (ncol_l_arr[i]->extent (Y_AXIS));
167         }
168
169       Interval inter (notedim);
170       inter.intersect (restdim);
171
172       Real dist =
173         minimum_dist +  dir * (notedim[dir] - restdim[-dir]) >? 0;
174
175
176       // FIXME
177       //int stafflines = 5; // rcol->rest_l_arr[0]->line_count;
178       int stafflines = Staff_symbol_referencer_interface (this).line_count ();
179       // hurg?
180       stafflines = stafflines != 0 ? stafflines : 5;
181       
182       // move discretely by half spaces.
183       int discrete_dist = int (ceil (dist / (0.5 *staff_space)));
184
185       // move by whole spaces inside the staff.
186       if (discrete_dist < stafflines+1)
187         discrete_dist = int (ceil (discrete_dist / 2.0)* 2.0);
188       
189       rcol->translate_rests (dir * discrete_dist);
190     }
191 }
192
193
194 Rest_collision::Rest_collision(SCM s)
195   : Item (s)
196 {
197   set_elt_pointer ("rests", SCM_EOL);
198   set_elt_pointer ("notes", SCM_EOL);
199   set_extent_callback (0, X_AXIS);
200   set_extent_callback (0, Y_AXIS);
201 }
202