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