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