]> git.donarmstrong.com Git - lilypond.git/blob - lily/stem.cc
release: 0.1.47
[lilypond.git] / lily / stem.cc
1 /*
2   stem.cc -- implement Stem
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1996,1997 Han-Wen Nienhuys <hanwen@stack.nl>
7
8   TODO: This is way too hairy
9 */
10
11 #include "stem.hh"
12 #include "dimen.hh"
13 #include "debug.hh"
14 #include "paper-def.hh"
15 #include "note-head.hh"
16 #include "lookup.hh"
17 #include "molecule.hh"
18 #include "p-col.hh"
19 #include "misc.hh"
20 #include "beam.hh"
21 #include "rest.hh"
22
23 const int STEMLEN = 7;
24
25 IMPLEMENT_IS_TYPE_B1 (Stem,Item);
26
27 Stem::Stem ()
28 {
29   /*
30     TODO: staff-size
31     */
32   beam_l_ = 0;
33   beams_left_i_ = 0;
34   beams_right_i_ = 0;
35
36   yextent_drul_[DOWN] = yextent_drul_[UP] = 0;
37   flag_i_ = 2;
38   dir_ = CENTER;
39   stem_xdir_ = LEFT;
40   staff_size_i_ = 8;
41
42   beam_gap_i_ = 0;
43 }
44
45 Interval_t<int>
46 Stem::head_positions () const
47 {
48   Interval_t<int> r;
49   for (int i =0; i < head_l_arr_.size (); i++)
50     {
51       int p = head_l_arr_[i]->position_i_;
52       r[BIGGER] = r[BIGGER] >? p;
53       r[SMALLER] = r[SMALLER] <? p;
54     }
55   return r;
56 }
57
58 void
59 Stem::do_print () const
60 {
61 #ifndef NPRINT
62   DOUT << "flag "<< flag_i_ ;
63   if (beam_l_)
64     DOUT << "beamed";
65 #endif
66 }
67
68 Real
69 Stem::stem_length_f () const
70 {
71   return yextent_drul_[UP]-yextent_drul_[DOWN] ;
72 }
73
74 Real
75 Stem::stem_begin_f () const
76 {
77   return yextent_drul_[Direction(-dir_)];
78 }
79
80 Real
81 Stem::chord_start_f () const
82 {
83   return head_positions()[dir_] * paper ()->internote_f ();
84 }
85
86 Real
87 Stem::stem_end_f () const
88 {
89   return yextent_drul_[dir_];
90 }
91
92 void
93 Stem::set_stemend (Real se)
94 {
95   // todo: margins
96   if (dir_ && dir_ * head_positions()[dir_] >= se*dir_)
97     warning (_("Weird stem size; check for narrow beams"));
98
99   
100   yextent_drul_[dir_]  =  se;
101   yextent_drul_[Direction(-dir_)] = head_positions()[-dir_];
102 }
103
104 int
105 Stem::type_i () const
106 {
107   return head_l_arr_[0]->balltype_i_;
108 }
109
110 void
111 Stem::add (Rhythmic_head *n)
112 {
113   n->add_dependency (this);     // ?
114   if (n->is_type_b (Note_head::static_name ()))
115     {
116       head_l_arr_.push ((Note_head*)n);
117     }
118   else if (n->is_type_b (Rest::static_name ()))
119     {
120       rest_l_arr_.push ((Rest*)n);
121     }
122 }
123
124 bool
125 Stem::invisible_b () const
126 {
127   return (!head_l_arr_.size () ||
128     head_l_arr_[0]->balltype_i_ <= 0);
129 }
130
131 int
132 Stem::get_center_distance (Direction d)
133 {
134   int staff_center = 0;
135   int distance = d*(head_positions()[d] - staff_center);
136   return distance >? 0;
137 }
138
139 Direction
140 Stem::get_default_dir ()
141 {
142   return (get_center_distance (UP) >=
143           get_center_distance (DOWN)) 
144     ? DOWN 
145     : UP;
146 }
147
148 Direction
149 Stem::get_dir ()
150 {
151   return dir_;
152 }
153
154 void
155 Stem::set_default_dir ()
156 {
157   dir_ = get_default_dir ();
158 }
159
160 void
161 Stem::set_default_stemlen ()
162 {
163   Real len = STEMLEN;
164   Real dy = paper ()->interbeam_f ();
165
166   if (!dir_)
167     set_default_dir ();
168
169   /* If the stem points in the "wrong" direction, it should be
170      shortened, according to [Roush & Gourlay].  Their suggestion to knock off
171      a whole staffspace is a bit drastical though.
172      */
173   else if (dir_ != get_default_dir ())
174     len  -= 1.0;
175
176   if (flag_i_ >= 5)
177     len += 2.0;
178   if (flag_i_ >= 6)
179     len += 1.0;
180   
181   set_stemend ((dir_ > 0) ? head_positions()[BIGGER] + len :
182                head_positions()[SMALLER] - len);
183
184
185   if (dir_ * stem_end_f () < 0)
186     {
187       set_stemend (0);
188     }
189 }
190
191 void
192 Stem::set_default_extents ()
193 {
194   if (!stem_length_f ())
195     set_default_stemlen ();
196
197
198   if (dir_ == UP)
199     stem_xdir_ = RIGHT;
200   if (invisible_b ())
201     stem_xdir_ = CENTER;
202 }
203
204 /*
205   TODO
206
207   move into note_column.cc
208
209   */
210 void
211 Stem::set_noteheads ()
212 {
213   if (!head_l_arr_.size ())
214     return;
215   head_l_arr_.sort (Note_head::compare);
216   if (dir_ < 0)
217     head_l_arr_.reverse ();
218
219   head_l_arr_[0]->extremal_i_ = -1;
220   head_l_arr_.top ()->extremal_i_ = 1;
221   int parity=1;
222   int lastpos = head_l_arr_[0]->position_i_;
223   for (int i=1; i < head_l_arr_.size (); i ++)
224     {
225       int dy =abs (lastpos- head_l_arr_[i]->position_i_);
226
227       if (dy <= 1)
228         {
229           if (parity)
230             head_l_arr_[i]->x_dir_ = (stem_xdir_ == LEFT) ? LEFT : RIGHT;
231           parity = !parity;
232         }
233       else
234         parity = 1;
235       lastpos = head_l_arr_[i]->position_i_;
236     }
237 }
238
239 void
240 Stem::do_pre_processing ()
241 {
242   if (yextent_drul_[DOWN]== yextent_drul_[UP])
243     set_default_extents ();
244   set_noteheads ();
245   flag_i_ = flag_i_;
246   transparent_b_ = invisible_b ();
247   set_empty (invisible_b ());
248 }
249
250
251 Interval
252 Stem::do_width () const
253 {
254   Interval r (0, 0);
255   if (beam_l_ || abs (flag_i_) <= 2)
256     ;   // TODO!
257   else
258     {
259       Paper_def*p= paper ();
260       r = p->lookup_l ()->flag (flag_i_, dir_).dim_.x ();
261       r += note_delta_f ();
262     }
263   return r;
264 }
265
266
267
268
269 const Real ANGLE = 20* (2.0*M_PI/360.0); // ugh!
270
271 Molecule*
272 Stem::brew_molecule_p () const
273 {
274   Molecule *mol_p =new Molecule;
275   Paper_def *p =paper ();
276   Drul_array<Real> stem_y = yextent_drul_;
277   Real dy = p->internote_f ();
278   
279
280   Real head_wid = 0;
281   if (head_l_arr_.size ())
282     head_wid = head_l_arr_[0]->width ().length ();
283   stem_y[Direction(-dir_)] += dir_ * head_wid * tan(ANGLE)/(2*dy);
284   
285   if (!invisible_b ())
286     {
287       Atom ss =p->lookup_l ()->stem (stem_y[DOWN]*dy,
288                                      stem_y[UP]*dy);
289       mol_p->add (Atom (ss));
290     }
291
292   if (!beam_l_ &&abs (flag_i_) > 2)
293     {
294       Atom fl = p->lookup_l ()->flag (flag_i_, dir_);
295       fl.translate_axis(stem_y[dir_]*dy, Y_AXIS);
296       mol_p->add(fl);
297     }
298
299   if (head_l_arr_.size())
300     {
301       mol_p->translate_axis (note_delta_f (), X_AXIS);
302     }
303   return mol_p;
304 }
305
306 Real
307 Stem::note_delta_f () const
308 {
309   Real r=0;
310   if (head_l_arr_.size() && stem_xdir_ != CENTER)
311     {
312       Interval head_wid(0,  head_l_arr_[0]->width ().length ());
313       Real rule_thick(paper ()->rule_thickness ());
314       Interval stem_wid(-rule_thick/2, rule_thick/2);
315       r = head_wid[stem_xdir_] - stem_wid[stem_xdir_];
316     }
317   return r;
318 }
319 Real
320 Stem::hpos_f () const
321 {
322   return note_delta_f () +Item::hpos_f ();
323 }
324
325 /*
326   TODO:  head_l_arr_/rest_l_arr_ in  do_substitute_dependent ()
327  */
328 void
329  Stem::do_substitute_dependency (Score_elem*o,Score_elem*n)
330 {
331   Item * o_l = o->item ();
332   Item * n_l = n? n->item () : 0;
333   head_l_arr_.substitute ((Note_head*)o_l, (Note_head*)n_l);
334   rest_l_arr_.substitute ((Rest*)o_l, (Rest*)n_l);
335 }