]> git.donarmstrong.com Git - lilypond.git/blob - lily/rest-collision.cc
release: 0.0.76
[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 Han-Wen Nienhuys <hanwen@stack.nl>
7 */
8
9 #include "debug.hh"
10 #include "rest-collision.hh"
11 #include "rest-column.hh"
12 #include "note-column.hh"
13 #include "stem.hh"
14 #include "note-head.hh"
15 #include "collision.hh"
16 #include "paper-def.hh"
17
18 IMPLEMENT_STATIC_NAME(Rest_collision);
19 IMPLEMENT_IS_TYPE_B1(Rest_collision,Item);
20
21 void
22 Rest_collision::add(Note_column *nc_l)
23 {
24     add_dependency(nc_l);
25     ncol_l_arr_.push(nc_l);
26 }
27
28 void
29 Rest_collision::add(Rest_column *rc_l)
30 {
31     add_dependency(rc_l);
32     rest_l_arr_.push(rc_l);
33 }
34
35 void
36 Rest_collision::add(Collision * c_l)
37 {
38     add_dependency(c_l);
39     for (int i=0; i < c_l->clash_l_arr_.size(); i ++)
40         ncol_l_arr_.push(c_l->clash_l_arr_[i]);
41 }
42
43 void
44 Rest_collision::do_post_processing()
45 {
46     /*
47       handle rest under beam (do_post: beams are calculated now)
48
49       [todo]
50       i-d like to have access to the beam itself, 
51       iso only the (half-initialised?) stem
52
53       what about combination of collisions and rest under beam
54      */
55
56     // no rests to collide
57     if (!rest_l_arr_.size())
58         return;
59     // can this happen?
60     Stem* stem_l = rest_l_arr_[0]->stem_l_;
61     if (!stem_l)
62         return;
63     // no beam
64     if (!(stem_l->beams_left_i_ || stem_l->beams_right_i_))
65         return;
66
67     int dir_i = rest_l_arr_[0]->dir_i_;
68     int midpos = 4;
69 #if 1
70     // ugh
71     int stem_length_i = 7 - 2;
72     // ugh, Stem::stem_start vs Stem::stem_end
73     int pos = (stem_l->stem_end_f() - midpos) - dir_i * stem_length_i;
74 #else // nogo: stem_start not set for rests?
75     int pos = (stem_l->stem_start_f() - midpos) + dir_i * 2;
76 #endif
77     rest_l_arr_[0]->translate_heads(pos);       
78 }
79
80 void
81 Rest_collision::do_pre_processing()
82 {
83     /* 
84       handle rest-rest and rest-note collisions
85
86       [todo]
87       decide not to print rest if too crowded?
88      */
89
90     // no rests to collide
91     if (!rest_l_arr_.size())
92         return;
93
94     // no partners to collide with
95     if (rest_l_arr_.size() + ncol_l_arr_.size() < 2 )
96         return;
97
98     // meisjes met meisjes
99     if (!ncol_l_arr_.size()) {
100         int dy = rest_l_arr_.size() > 2 ? 6 : 4;
101         
102         rest_l_arr_[0]->translate_heads(rest_l_arr_[0]->dir_i_ *dy);    
103         // top is last element...
104         rest_l_arr_.top()->translate_heads(rest_l_arr_.top()->dir_i_* dy);      
105     }
106     // meisjes met jongetjes
107     else {
108 #if 0 // breendet: rests go always under
109         // geen gemug, trug op je rug
110         int dir_i = -1;
111         rest_l_arr_[0]->translate_heads(dir_i * 3 );    
112 #else
113         // int dir_i = - ncol_l_arr_[0]->dir_i_;
114         int dir_i = rest_l_arr_[0]->dir_i_;
115         // hope it's 4: if it works->doco
116         int midpos = 4;
117         
118         // minimum move
119         int minpos = 4;
120         
121         // quart rest height
122         // UGH Should get dims from table!
123         int size_i = 6;
124         
125         int sep_i = 3 + size_i / 2;
126         for (int i = 0; i < ncol_l_arr_.size(); i++) {
127             // how to know whether to sort?
128             ncol_l_arr_[i]->sort();
129             for ( int j = 0; j < ncol_l_arr_[i]->head_l_arr_.size(); j++ )
130                 minpos = minpos >? dir_i * 
131                     (ncol_l_arr_[i]->head_l_arr_[j]->position_i_ -midpos ) + sep_i;
132         }
133         rest_l_arr_[0]->translate_heads(dir_i * minpos );       
134 #endif
135     }
136 }
137
138 void
139 Rest_collision::do_print() const
140 {
141 #ifndef NPRINT
142     mtor << "rests: " << rest_l_arr_.size() << ", ";
143     mtor << "cols: " << ncol_l_arr_.size();
144 #endif
145 }
146
147 void
148 Rest_collision::do_substitute_dependency(Score_elem*o,Score_elem*n)
149 {
150     Item*o_l = o->item();
151     Item*n_l = n?n->item():0;
152     
153     rest_l_arr_.substitute((Rest_column*)o_l,(Rest_column*)n_l);
154     ncol_l_arr_.substitute((Note_column*)o_l,(Note_column*)n_l);
155 }
156
157 Rest_collision::Rest_collision()
158 {
159     transparent_b_ = true;
160     empty_b_ = true;
161 }