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