]> git.donarmstrong.com Git - lilypond.git/blob - lily/rest-collision.cc
patch::: 0.0.68pre.jcn1: Re: patsen?
[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     Real inter_f = paper()->internote_f();
68     int dir_i = rest_l_arr_[0]->dir_i_;
69     int midpos = 4;
70 #if 1
71     // ugh
72     int stem_length_i = 7 - 2;
73     // ugh, Stem::stem_start vs Stem::stem_end
74     int pos = (stem_l->stem_end_f() - midpos) - dir_i * stem_length_i;
75 #else // nogo: stem_start not set for rests?
76     int pos = (stem_l->stem_start_f() - midpos) + dir_i * 2;
77 #endif
78     Real dy = pos * inter_f;
79     rest_l_arr_[0]->translate_y(dy);    
80 }
81
82 void
83 Rest_collision::do_pre_processing()
84 {
85     /* 
86       handle rest-rest and rest-note collisions
87
88       [todo]
89       decide not to print rest if too crowded?
90      */
91
92     // no rests to collide
93     if (!rest_l_arr_.size())
94         return;
95
96     // no partners to collide with
97     if (rest_l_arr_.size() + ncol_l_arr_.size() < 2 )
98         return;
99
100     Real inter_f = paper()->internote_f();
101
102     // meisjes met meisjes
103     if (!ncol_l_arr_.size()) {
104         Real dy = rest_l_arr_.size() > 2 ? 6 * inter_f : 4 * inter_f;
105         rest_l_arr_[0]->translate_y(dy);        
106         // top is last element...
107         rest_l_arr_.top()->translate_y(-dy);    
108     }
109     // meisjes met jongetjes
110     else {
111 #if 0 // breendet: rests go always under
112         // geen gemug, trug op je rug
113         int dir_i = -1;
114         rest_l_arr_[0]->translate_y(dir_i * 3 * inter_f);       
115 #else
116         // int dir_i = - ncol_l_arr_[0]->dir_i_;
117         int dir_i = rest_l_arr_[0]->dir_i_;
118         // hope it's 4: if it works->doco
119         int midpos = 4;
120         // minimum move
121         int minpos = 4;
122         // quart rest height
123         int size_i = 6;
124         int sep_i = 3 + size_i / 2;
125         for (int i = 0; i < ncol_l_arr_.size(); i++) {
126             // how to know whether to sort?
127             ncol_l_arr_[i]->sort();
128             for ( int j = 0; j < ncol_l_arr_[i]->head_l_arr_.size(); j++ )
129                 minpos = minpos >? dir_i * ( ncol_l_arr_[i]->head_l_arr_[j]->position_i_ - midpos ) + sep_i;
130         }
131         Real dy = dir_i * minpos * inter_f;
132         rest_l_arr_[0]->translate_y(dy);        
133 #endif
134     }
135 }
136
137 void
138 Rest_collision::do_print() const
139 {
140 #ifndef NPRINT
141     mtor << "rests: " << rest_l_arr_.size() << ", ";
142     mtor << "cols: " << ncol_l_arr_.size();
143 #endif
144 }
145
146 void
147 Rest_collision::do_substitute_dependency(Score_elem*o,Score_elem*n)
148 {
149     Item*o_l = o->item();
150     Item*n_l = n?n->item():0;
151     
152     rest_l_arr_.substitute((Rest_column*)o_l,(Rest_column*)n_l);
153     ncol_l_arr_.substitute((Note_column*)o_l,(Note_column*)n_l);
154 }