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