]> git.donarmstrong.com Git - lilypond.git/blob - lily/stem.cc
release: 1.1.35
[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--1999 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] = -1;
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
44   beam_gap_i_ = 0;
45   beam_l_ = 0;
46 }
47
48 Interval_t<int>
49 Stem::head_positions () const
50 {
51   /* 
52     Mysterious FreeBSD fix by John Galbraith.  Somehow, the empty intervals 
53     trigger FP exceptions on FreeBSD.  Fix: do not return infinity 
54
55    */
56   if (!head_l_arr_.size ())
57     {
58       return Interval_t<int> (100,-100);        
59     }
60
61   Interval_t<int> r;
62   for (int i =0; i < head_l_arr_.size (); i++)
63     {
64       int p = head_l_arr_[i]->position_i_;
65       r[BIGGER] = r[BIGGER] >? p;
66       r[SMALLER] = r[SMALLER] <? p;
67     }
68   return r;
69 }
70
71 void
72 Stem::do_print () const
73 {
74 #ifndef NPRINT
75   DOUT << "flag "<< flag_i_;
76   if (beam_l_)
77     DOUT << "beamed";
78 #endif
79 }
80
81 Real
82 Stem::stem_length_f () const
83 {
84   return yextent_drul_[UP]-yextent_drul_[DOWN] ;
85 }
86
87 Real
88 Stem::stem_begin_f () const
89 {
90   return yextent_drul_[Direction(-dir_)];
91 }
92
93 Real
94 Stem::chord_start_f () const
95 {
96   return head_positions()[dir_] * staff_line_leading_f ()/2.0;
97 }
98
99 Real
100 Stem::stem_end_f () const
101 {
102   return yextent_drul_[dir_];
103 }
104
105 void
106 Stem::set_stemend (Real se)
107 {
108   // todo: margins
109   if (dir_ && dir_ * head_positions()[dir_] >= se*dir_)
110     warning (_ ("weird stem size; check for narrow beams"));
111
112   
113   yextent_drul_[dir_]  =  se;
114   yextent_drul_[Direction(-dir_)] = head_positions()[-dir_];
115 }
116
117 int
118 Stem::type_i () const
119 {
120   return head_l_arr_[0]->balltype_i_;
121 }
122
123 void
124 Stem::add_head (Rhythmic_head *n)
125 {
126   n->add_dependency (this);     // ?
127   if (Note_head *nh = dynamic_cast<Note_head *> (n))
128     {
129       head_l_arr_.push (nh);
130     }
131   else if (Rest *r = dynamic_cast<Rest *> (n))
132     {
133       rest_l_arr_.push (r);
134     }
135 }
136
137 bool
138 Stem::invisible_b () const
139 {
140   return (!head_l_arr_.size () ||
141     head_l_arr_[0]->balltype_i_ <= 0);
142 }
143
144 int
145 Stem::get_center_distance (Direction d) const
146 {
147   int staff_center = 0;
148   int distance = d*(head_positions()[d] - staff_center);
149   return distance >? 0;
150 }
151
152 Direction
153 Stem::get_default_dir () const
154 {
155   return (get_center_distance (UP) >
156           get_center_distance (DOWN)) 
157     ? DOWN 
158     : UP;
159 }
160
161 Direction
162 Stem::get_dir () const
163 {
164   return dir_;
165 }
166
167 void
168 Stem::set_default_dir ()
169 {
170   dir_ = get_default_dir ();
171 }
172
173 void
174 Stem::set_default_stemlen ()
175 {
176   Real internote_f = staff_line_leading_f ()/2.0;
177   Real length_f = paper_l ()->get_var ("stem_length0") / internote_f;
178   Real shorten_f = paper_l ()->get_var ("forced_stem_shorten0") / internote_f;
179
180   if (!dir_)
181     set_default_dir ();
182   /* 
183     stems in unnatural (forced) direction should be shortened, 
184     accoding to [Roush & Gourlay]
185    */
186   if (((int)chord_start_f ())
187       && (dir_ != get_default_dir ()))
188     length_f -= shorten_f;
189
190   if (flag_i_ >= 5)
191     length_f += 2.0;
192   if (flag_i_ >= 6)
193     length_f += 1.0;
194   
195   set_stemend ((dir_ > 0) ? head_positions()[BIGGER] + length_f:
196                head_positions()[SMALLER] - length_f);
197
198   if (dir_ * stem_end_f () < 0)
199     set_stemend (0);
200 }
201
202 //xxx
203 void
204 Stem::set_default_extents ()
205 {
206   if (!stem_length_f ())
207     set_default_stemlen ();
208
209
210   if (dir_ == UP)
211     stem_xdir_ = RIGHT;
212   if (invisible_b ())
213     stem_xdir_ = CENTER;
214 }
215
216 /*
217   TODO
218
219   move into note_column.cc
220
221   */
222 void
223 Stem::set_noteheads ()
224 {
225   if (!head_l_arr_.size ())
226     return;
227   head_l_arr_.sort (Note_head::compare);
228   if (dir_ < 0)
229     head_l_arr_.reverse ();
230
231   head_l_arr_[0]->extremal_i_ = -1;
232   head_l_arr_.top ()->extremal_i_ = 1;
233   int parity=1;
234   int lastpos = head_l_arr_[0]->position_i_;
235   for (int i=1; i < head_l_arr_.size (); i ++)
236     {
237       int dy =abs (lastpos- head_l_arr_[i]->position_i_);
238
239       if (dy <= 1)
240         {
241           if (parity)
242             head_l_arr_[i]->x_dir_ = (stem_xdir_ == LEFT) ? LEFT : RIGHT;
243           parity = !parity;
244         }
245       else
246         parity = 1;
247       lastpos = head_l_arr_[i]->position_i_;
248     }
249 }
250
251 void
252 Stem::do_pre_processing ()
253 {
254   if (yextent_drul_[DOWN]== yextent_drul_[UP])
255     set_default_extents ();
256   set_noteheads ();
257   flag_i_ = flag_i_;
258   if (invisible_b ())
259     {
260       set_elt_property (transparent_scm_sym, SCM_BOOL_T);
261     }
262   set_empty (invisible_b ());
263 }
264
265
266 Interval
267 Stem::do_width () const
268 {
269   Interval r (0, 0);
270   if (beam_l_ || abs (flag_i_) <= 2)
271     ;   // TODO!
272   else
273     {
274       r = lookup_l ()->flag (flag_i_, dir_).dim_.x ();
275       r += note_delta_f ();
276     }
277   return r;
278 }
279
280
281
282
283 const Real ANGLE = 20* (2.0*M_PI/360.0); // ugh!
284
285 Molecule*
286 Stem::do_brew_molecule_p () const
287 {
288   Molecule *mol_p =new Molecule;
289   Drul_array<Real> stem_y = yextent_drul_;
290   Real dy = staff_line_leading_f ()/2.0;
291
292   Real head_wid = 0;
293   if (head_l_arr_.size ())
294     head_wid = head_l_arr_[0]->extent (X_AXIS).length ();
295   stem_y[Direction(-dir_)] += dir_ * head_wid * tan(ANGLE)/(2*dy);
296   
297   if (!invisible_b ())
298     {
299       Molecule ss =lookup_l ()->stem (stem_y[DOWN]*dy,
300                                      stem_y[UP]*dy);
301       mol_p->add_molecule (ss);
302     }
303
304   if (!beam_l_ && abs (flag_i_) > 2)
305     {
306       Molecule fl = lookup_l ()->flag (flag_i_, dir_);
307       fl.translate_axis(stem_y[dir_]*dy, Y_AXIS);
308       mol_p->add_molecule (fl);
309     }
310
311   if (head_l_arr_.size())
312     {
313       mol_p->translate_axis (note_delta_f (), X_AXIS);
314     }
315   return mol_p;
316 }
317
318 Real
319 Stem::note_delta_f () const
320 {
321   Real r=0;
322   if (head_l_arr_.size())
323     {
324       Interval head_wid(0,  head_l_arr_[0]->extent (X_AXIS).length ());
325       Real rule_thick(paper_l ()->rule_thickness ());
326       Interval stem_wid(-rule_thick/2, rule_thick/2);
327       if (stem_xdir_ == CENTER)
328         r = head_wid.center ();
329       else
330         r = head_wid[stem_xdir_] - stem_wid[stem_xdir_];
331     }
332   return r;
333 }
334
335 Real
336 Stem::hpos_f () const
337 {
338   return note_delta_f () + Item::hpos_f ();
339 }
340
341 /*
342   TODO:  head_l_arr_/rest_l_arr_ in  
343  */
344 void
345 Stem::do_substitute_element_pointer (Score_element*o,Score_element*n)
346 {
347   if (Note_head*h=dynamic_cast<Note_head*> (o))
348   head_l_arr_.substitute (h, dynamic_cast<Note_head*>(n));
349   if (Rest *r=dynamic_cast<Rest*> (o))
350     rest_l_arr_.substitute (r, dynamic_cast<Rest*>(n));
351   if (Beam* b = dynamic_cast<Beam*> (o))
352     {
353       if (b == beam_l_) 
354         {
355           beam_l_ = dynamic_cast<Beam*> (n);
356           if (!beam_l_)
357             {
358               beams_i_drul_[LEFT] = 0;
359               beams_i_drul_[RIGHT] = 0;
360               mult_i_ = 0;
361             }
362         }
363     }
364   Staff_symbol_referencer::do_substitute_element_pointer (o,n);
365       
366 }