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