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