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