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