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