]> git.donarmstrong.com Git - lilypond.git/blob - lily/stem.cc
release: 0.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 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   abbrev_flag_i_ = 0;
33   beam_l_ = 0;
34   beams_left_i_ = 0;
35   beams_right_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 int
47 Stem::min_head_i () const
48 {
49   int m = 1000;
50   for (int i =0; i < head_l_arr_.size (); i++)
51     m = m <? head_l_arr_[i]->position_i_;
52   return m;
53 }
54
55 int
56 Stem::max_head_i () const
57 {
58   int m = -1000;
59   for (int i =0; i < head_l_arr_.size (); i++)
60     m = m >? head_l_arr_[i]->position_i_;
61   return m;
62   
63 }
64
65 void
66 Stem::do_print () const
67 {
68 #ifndef NPRINT
69   DOUT << "flag "<< flag_i_ << "abbrev_flag_i_" << abbrev_flag_i_;
70   if (beam_l_)
71     DOUT << "beamed";
72 #endif
73 }
74
75 Real 
76 Stem::stem_length_f () const
77 {
78   return yextent_drul_[UP]-yextent_drul_[DOWN] ;
79 }
80
81 Real
82 Stem::stem_start_f () const
83 {
84   return (dir_ < 0)? yextent_drul_[UP] : yextent_drul_[DOWN];
85 }
86
87 Real
88 Stem::stem_end_f () const
89 {
90   return (dir_ < 0)? yextent_drul_[DOWN] : yextent_drul_[UP];
91 }
92
93
94 void
95 Stem::set_stemend (Real se)
96 {
97   // todo: margins
98   if (!  ((dir_ > 0 && se >= max_head_i ()) || 
99           (se <= min_head_i () && dir_ <0)))    
100     warning ("Weird stem size; check for narrow beams");
101
102   yextent_drul_[UP]  = (dir_ < 0) ? max_head_i () : se;
103   yextent_drul_[DOWN]  = (dir_ < 0) ? se  : min_head_i ();
104 }
105
106 int
107 Stem::type_i () const
108 {
109   return head_l_arr_[0]->balltype_i_;
110 }
111
112 void
113 Stem::add (Note_head *n)
114 {
115   n->add_dependency (this);
116   head_l_arr_.push (n);
117 }
118  
119 void
120 Stem::add (Rest *r)
121 {
122   rest_l_arr_.push (r);
123   r->add_dependency (this);     // ?
124 }
125
126 bool
127 Stem::invisible_b () const
128 {
129   
130   return (!head_l_arr_.size () || 
131     head_l_arr_[0]->balltype_i_ <= 0);
132
133 }
134
135 // if dir_ is set we return fake values.
136 int
137 Stem::get_center_distance_from_top ()
138 {
139   if (dir_)
140     return (dir_ > 0) ? 0 : 1;
141
142   int staff_center = staff_size_i_ / 2;
143   int max = max_head_i () - staff_center;
144   return max >? 0;
145 }
146
147 // if dir_ is set we return fake values.
148 int
149 Stem::get_center_distance_from_bottom ()
150 {
151   if (dir_)
152     return (dir_ > 0) ? 1 : 0;
153
154   int staff_center = staff_size_i_ / 2;
155   int min = staff_center - min_head_i ();
156   return min >? 0;
157 }
158
159 Direction
160 Stem::get_default_dir ()
161 {
162   if (dir_)
163     return dir_;
164   return (get_center_distance_from_top () >=
165           get_center_distance_from_bottom ()) ? 
166     (Direction)-1 : (Direction)1;
167 }
168
169
170 void
171 Stem::set_default_dir ()
172 {
173   dir_ = get_default_dir ();
174 }
175
176 void
177 Stem::set_default_stemlen ()
178 {
179   if (!dir_)
180     set_default_dir ();
181
182   // ugh... how about non 5-line staffs?
183   bool on_ledger_line_b = ((max_head_i () < -2 && dir_ == 1)
184                            //    || (min_head_i () > staff_size_i_ && dir_ == -1));
185                            || (min_head_i () > staff_size_i_ + 3 && dir_ == -1));
186   if (on_ledger_line_b)
187     {
188       set_stemend (staff_size_i_ / 2 - 1);
189     }
190   else 
191     {
192       Real dy = paper ()->interbeam_f ();
193       Real len = STEMLEN;
194       // ugh, should get nice *rule* for this
195       if (abbrev_flag_i_ > 1)
196         len += (abbrev_flag_i_ - 1)* dy / 2;
197       set_stemend ((dir_ > 0) ? max_head_i () + len :
198                    min_head_i () - len);
199     }
200 }
201
202 void
203 Stem::set_default_extents ()
204 {
205   if (!stem_length_f ())
206     set_default_stemlen ();
207
208   set_stemend ((dir_< 0) ? 
209                max_head_i ()-stem_length_f (): min_head_i () + stem_length_f ());
210   
211   if (dir_ == UP)
212     stem_xdir_ = RIGHT;
213   if (head_l_arr_[0]->balltype_i_ <= 0)
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 = 0;
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 (abbrev_flag_i_)
269     {
270       r = abbrev_mol ().extent ().x ();
271     }
272   else if (beam_l_ || abs (flag_i_) <= 2)
273     ;   // TODO!
274   else
275     {
276       Paper_def*p= paper ();
277       r = p->lookup_l ()->flag (flag_i_, dir_).dim_.x ();
278       r += note_delta_f ();
279     }
280   return r;
281 }
282
283
284   
285 Molecule
286 Stem::abbrev_mol () const
287 {
288   Real dy = paper ()->interbeam_f ();
289   Real w = 1.5 * paper ()->lookup_l ()->ball (2).dim_.x ().length ();
290   Real beamdy = paper ()->interline_f () / 2;
291  
292   int beams_i = 0;
293   Real slope = paper ()->internote_f () / 4;
294  
295   if (beam_l_) {
296     // huh?
297       slope = 2 * beam_l_->slope;
298     // ugh, rather calc from Abbreviation_req
299       beams_i = beams_right_i_ >? beams_left_i_; 
300   }
301   paper ()->lookup_l ()->beam (slope, 20 PT);
302   
303   Molecule beams;
304   Atom a (paper ()->lookup_l ()->beam (slope, w));
305   a.translate (Offset(- w / 2, stem_end_f () - (w / 2 * slope)));
306   // ugh
307     if (!beams_i)
308       a.translate (dy + beamdy - dir_ * dy, Y_AXIS);
309     else
310       a.translate (2 * beamdy - dir_ * (beamdy - dy), Y_AXIS);
311   
312   for (int i = 0; i < abbrev_flag_i_; i++) 
313     {
314       Atom b (a);
315       b.translate (-dir_ * dy * (beams_i + i), Y_AXIS);
316       beams.add (b);
317     }
318   
319   return beams;
320 }
321
322 Molecule*
323  Stem::brew_molecule_p () const 
324 {
325   Molecule *mol_p =new Molecule;
326   
327   Real bot  = yextent_drul_[DOWN];
328   Real top = yextent_drul_[UP];
329   
330   assert (bot!=top);
331   
332   Paper_def *p =paper ();
333   
334   Real dy = p->internote_f ();
335   if (!invisible_b ())
336     {
337       Atom ss =p->lookup_l ()->stem (bot*dy,top*dy);
338       mol_p->add (Atom (ss));
339     }
340   
341   if (!beam_l_ &&abs (flag_i_) > 2)
342     {
343       Atom fl = p->lookup_l ()->flag (flag_i_, dir_);
344       mol_p->add_at_edge (Y_AXIS, dir_, Molecule (Atom (fl)));
345       assert (!abbrev_flag_i_);
346     }
347   
348   if (abbrev_flag_i_)
349     mol_p->add (abbrev_mol ());
350
351   if (head_l_arr_.size())
352     {
353       mol_p->translate (note_delta_f (), X_AXIS);
354     }
355   return mol_p;
356 }
357
358 Real 
359 Stem::note_delta_f () const
360 {
361   Real r=0;
362   if (head_l_arr_.size())
363     {
364       r += head_l_arr_[0]->width ().length() * (stem_xdir_+1.0)/2.0;
365       if (stem_xdir_ == RIGHT)
366         r -= paper ()->rule_thickness ();
367     }
368   return r;
369 }
370 Real
371 Stem::hpos_f () const
372 {
373   return note_delta_f () +Item::hpos_f ();
374 }
375
376
377 void
378  Stem::do_substitute_dependency (Score_elem*o,Score_elem*n)
379 {
380   Item * o_l = o->item ();
381   Item * n_l = n? n->item () : 0;
382   head_l_arr_.substitute ((Note_head*)o_l, (Note_head*)n_l);
383   rest_l_arr_.substitute ((Rest*)o_l, (Rest*)n_l);
384 }