]> git.donarmstrong.com Git - lilypond.git/blob - lily/rest-collision.cc
release: 1.3.19
[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
21 void
22 Rest_collision::add_column (Note_column *nc_l)
23 {
24   add_dependency (nc_l);
25   Group_interface gi (this);  
26   if (nc_l->rest_b ())
27     gi.name_ = "rests";
28   else
29     gi.name_ = "notes";
30   
31   gi.add_element (nc_l);
32 }
33
34 void
35 Rest_collision::do_pre_processing()
36 {
37   Link_array<Note_column> rest_l_arr =
38     Group_interface__extract_elements (this, (Note_column*) 0, "rests");
39   Link_array<Note_column> ncol_l_arr =
40     Group_interface__extract_elements (this, (Note_column*) 0, "notes");
41                                       
42   
43   /* 
44      handle rest-rest and rest-note collisions
45
46      [todo]
47      * decide not to print rest if too crowded?
48
49      * ignore rests under beams.
50    */
51
52   // no rests to collide
53   if (!rest_l_arr.size())
54     return;
55
56   // no partners to collide with
57   if (rest_l_arr.size() + ncol_l_arr.size () < 2)
58     return;
59
60   // meisjes met meisjes
61   if (!ncol_l_arr.size()) 
62     {
63       /*
64         UGH.  Should get dims from table.  Should have minimum dist.
65        */
66       int dy = rest_l_arr.size() > 2 ? 6 : 4;
67         
68       rest_l_arr[0]->translate_rests (rest_l_arr[0]->dir () *dy);       
69       rest_l_arr.top()->translate_rests (rest_l_arr.top ()->dir ()* dy);
70     }
71   // meisjes met jongetjes
72   else 
73     {
74       if (rest_l_arr.size () > 1)
75         {
76           warning (_("too many colliding rests"));
77         }
78       if (ncol_l_arr.size () > 1)
79         {
80           warning (_("too many notes for rest collision"));
81         }
82       Note_column * rcol = rest_l_arr[0];
83
84       // try to be opposite of noteheads. 
85       Direction dir = - ncol_l_arr[0]->dir();
86
87       Interval restdim = rcol->rest_dim ();
88       if (restdim.empty_b ())
89         return;
90       
91       // staff ref'd?
92       Real staff_space = paper_l()->get_var ("interline");
93
94         /* FIXME
95           staff_space =  rcol->rest_l_arr[0]->staff_space ();
96         */
97       Real half_staff_space_f = staff_space/2;
98       Real minimum_dist = paper_l ()->get_var ("restcollision_minimum_dist")
99         * half_staff_space_f;
100       
101       /*
102         assumption: ref points are the same. 
103        */
104       Interval notedim;
105       for (int i = 0; i < ncol_l_arr.size(); i++) 
106         {
107           notedim.unite (ncol_l_arr[i]->extent (Y_AXIS));
108         }
109
110       Interval inter (notedim);
111       inter.intersect (restdim);
112
113       Real dist =
114         minimum_dist +  dir * (notedim[dir] - restdim[-dir]) >? 0;
115
116
117       // FIXME
118       int stafflines = 5; // rcol->rest_l_arr[0]->line_count;
119
120       
121       // move discretely by half spaces.
122       int discrete_dist = int (ceil (dist / (0.5 *staff_space)));
123
124       // move by whole spaces inside the staff.
125       if (discrete_dist < stafflines+1)
126         discrete_dist = int (ceil (discrete_dist / 2.0)* 2.0);
127       
128       rcol->translate_rests (dir * discrete_dist);
129     }
130 }
131
132
133 Rest_collision::Rest_collision()
134 {
135   set_elt_property ("rests", SCM_EOL);
136   set_elt_property ("notes", SCM_EOL);
137   set_elt_property ("transparent", SCM_BOOL_T);
138   set_empty (X_AXIS);
139   set_empty (Y_AXIS);
140 }
141