]> git.donarmstrong.com Git - lilypond.git/blob - lily/stem.cc
patch::: 0.1.12.jcn2: kliener pats
[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
9 #include "stem.hh"
10 #include "dimen.hh" 
11 #include "debug.hh"
12 #include "paper-def.hh"
13 #include "note-head.hh"
14 #include "lookup.hh"
15 #include "molecule.hh"
16 #include "p-col.hh"
17 #include "misc.hh"
18 #include "beam.hh"
19
20 const int STEMLEN=7;
21
22 IMPLEMENT_IS_TYPE_B1 (Stem,Item);
23
24 Stem::Stem () 
25 {
26   /*
27     TODO: staff-size
28    */
29   abbrev_flag_i_ = 0;
30   beam_l_ = 0;
31   beams_left_i_ = 0;
32   beams_right_i_ = 0;
33
34   stem_bottom_f_ = stem_top_f_ = 0;
35   flag_i_ = 4;
36   dir_ =CENTER;
37   staff_size_i_ = 8;
38
39   stem_xoffset_f_ =0;
40 }
41
42 int
43 Stem::min_head_i () const
44 {
45   int m = 1000;
46   for (int i =0; i < head_l_arr_.size (); i++)
47     m = m <? head_l_arr_[i]->position_i_;
48   return m;
49 }
50
51 int
52 Stem::max_head_i () const
53 {
54   int m = -1000;
55   for (int i =0; i < head_l_arr_.size (); i++)
56     m = m >? head_l_arr_[i]->position_i_;
57   return m;
58   
59 }
60
61 void
62 Stem::do_print () const
63 {
64 #ifndef NPRINT
65   DOUT << "flag "<< flag_i_ << " print_flag? " << !(bool)beam_l_ << "abbrev_flag_i_" << abbrev_flag_i_;
66 #endif
67 }
68
69 Real 
70 Stem::stem_length_f () const
71 {
72   return stem_top_f_-stem_bottom_f_ ;
73 }
74
75 Real
76 Stem::stem_start_f () const
77 {
78   return (dir_ < 0)? stem_top_f_ : stem_bottom_f_;
79 }
80
81 Real
82 Stem::stem_end_f () const
83 {
84   return (dir_ < 0)? stem_bottom_f_ : stem_top_f_;
85 }
86
87
88 void
89 Stem::set_stemend (Real se)
90 {
91   // todo: margins
92   if (!  ((dir_ > 0 && se >= max_head_i ()) || 
93           (se <= min_head_i () && dir_ <0)))    
94     warning ("Weird stem size; check for narrow beams");
95
96   stem_top_f_  = (dir_ < 0) ? max_head_i () : se;
97   stem_bottom_f_  = (dir_ < 0) ? se  : min_head_i ();
98 }
99
100 void
101 Stem::add (Note_head *n)
102 {
103   n->add_dependency (this);
104   if (n->rest_b_) 
105       rest_l_arr_.push (n);
106   else 
107       head_l_arr_.push (n);
108
109 #if 0
110   else if (1) //why different? (n->balltype_i_) 
111       head_l_arr_.push (n);
112   else
113       whole_l_arr_.push (n);
114 #endif
115 }
116
117 bool
118 Stem::invisible_b () const
119 {
120   return !head_l_arr_.size();
121 }
122
123 // if dir_ is set we return fake values.
124
125 int
126 Stem::get_center_distance_from_top ()
127 {
128   if (dir_)
129     return (dir_ > 0) ? 0 : 1;
130
131   int staff_center = staff_size_i_ / 2;
132   int max = max_head_i () - staff_center;
133   return max >? 0;
134 }
135
136 // if dir_ is set we return fake values.
137 int
138 Stem::get_center_distance_from_bottom ()
139 {
140   if (dir_)
141     return (dir_ > 0) ? 1 : 0;
142
143   int staff_center = staff_size_i_ / 2;
144   int min = staff_center - min_head_i ();
145   return min >? 0;
146 }
147
148 Direction
149 Stem::get_default_dir ()
150 {
151   if (dir_)
152     return dir_;
153   return (get_center_distance_from_top () >=
154     get_center_distance_from_bottom ()) ? 
155     (Direction)-1 : (Direction)1;
156 }
157
158
159 void
160 Stem::set_default_dir ()
161 {
162   dir_ = get_default_dir ();
163 }
164
165 void
166 Stem::set_default_stemlen ()
167 {
168   if (!dir_)
169     set_default_dir ();
170
171   // ugh... how about non 5-line staffs?
172   bool on_ledger_line_b = ((max_head_i () < -2 && dir_ == 1)
173 //    || (min_head_i () > staff_size_i_ && dir_ == -1));
174     || (min_head_i () > staff_size_i_ + 3 && dir_ == -1));
175   if (on_ledger_line_b)
176     {
177       set_stemend (staff_size_i_ / 2 - 1);
178     }
179   else 
180     {
181       Real dy = paper ()->interbeam_f ();
182       Real len = STEMLEN;
183       // ugh, should get nice *rule* for this
184       if (abbrev_flag_i_ > 1)
185         len += (abbrev_flag_i_ - 1)* dy / 2;
186       set_stemend ((dir_ > 0) ? max_head_i () + len :
187                    min_head_i () - len);
188     }
189 }
190
191 void
192 Stem::set_default_extents ()
193 {
194   if (!stem_length_f ())
195     set_default_stemlen ();
196
197   set_stemend ((dir_< 0) ? 
198                max_head_i ()-stem_length_f (): min_head_i () +stem_length_f ());
199   if (dir_ > 0){        
200     stem_xoffset_f_ = paper ()->note_width ()-paper ()->rule_thickness ();
201   }
202   else
203     stem_xoffset_f_ = 0;
204 }
205
206 /*
207   TODO
208   
209   move into note_column.cc
210
211   */
212 void
213 Stem::set_noteheads ()
214 {
215   if (!head_l_arr_.size ())
216     return;
217   head_l_arr_.sort (Note_head::compare);
218   if (dir_ < 0) 
219     head_l_arr_.reverse ();
220   
221   head_l_arr_[0]->extremal_i_ = -1;
222   head_l_arr_.top ()->extremal_i_ = 1;
223   int parity=1;
224   int lastpos = head_l_arr_[0]->position_i_;
225   for (int i=1; i < head_l_arr_.size (); i ++) 
226     {
227       int dy =abs (lastpos- head_l_arr_[i]->position_i_);
228         
229       if (dy <= 1) 
230         {
231           if (parity)
232             head_l_arr_[i]->x_dir_ = (stem_xoffset_f_>0) ? UP:DOWN;
233           parity = !parity;
234         }
235       else
236         parity = 0;
237       lastpos = head_l_arr_[i]->position_i_;
238     }
239 }
240
241 void
242 Stem::do_pre_processing ()
243 {
244   if (stem_bottom_f_== stem_top_f_)
245     set_default_extents ();
246   set_noteheads ();
247   flag_i_ = dir_*abs (flag_i_);
248   transparent_b_ = invisible_b ();
249   empty_b_ = invisible_b ();
250 }
251
252 Interval
253 Stem::do_width () const
254 {
255   if (beam_l_ || abs (flag_i_) <= 4)
256     return Interval (0,0);      // TODO!
257   Paper_def*p= paper ();
258   Interval r (p->lookup_l ()->flag (flag_i_).dim.x ());
259   r+= stem_xoffset_f_;
260   return r;
261 }
262
263 Molecule
264 Stem::abbrev_mol () const
265 {
266   Real dy = paper ()->interbeam_f ();
267   Real w = 1.5 * paper ()->lookup_l ()->ball (2).dim.x ().length ();
268   Real beamdy = paper ()->interline_f () / 2;
269
270   int beams_i = 0;
271   Real slope = paper ()->internote_f () / 4;
272
273   if (beam_l_) {
274     // huh?
275     slope = 2 * beam_l_->slope;
276     // ugh, rather calc from Abbreviation_req
277     beams_i = beams_right_i_ >? beams_left_i_; 
278   }
279   paper()->lookup_l ()->beam (slope, 20 PT);
280
281   Molecule beams;
282   Atom a (paper ()->lookup_l ()->beam (slope, w));
283   a.translate (Offset(- w / 2, stem_end_f () - (w / 2 * slope)));
284   // ugh
285   if (!beams_i)
286     a.translate (dy + beamdy - dir_ * dy, Y_AXIS);
287   else
288     a.translate (2 * beamdy - dir_ * (beamdy - dy), Y_AXIS);
289         
290   for (int i = 0; i < abbrev_flag_i_; i++) 
291     {
292       Atom b (a);
293       b.translate (-dir_ * dy * (beams_i + i), Y_AXIS);
294       beams.add (b);
295     }
296
297   return beams;
298 }
299
300 Molecule*
301 Stem::brew_molecule_p () const 
302 {
303   Real bot  = stem_bottom_f_;
304   Real top = stem_top_f_;
305   
306   assert (bot!=top);
307  
308   Paper_def *p =paper ();
309
310   Real dy = p->internote_f ();
311   Symbol ss =p->lookup_l ()->stem (bot*dy,top*dy);
312
313   Molecule* mol_p = new Molecule;
314   if (head_l_arr_.size() && head_l_arr_[0]->balltype_i_)
315     mol_p->add (Atom (ss));
316
317   if (!beam_l_ && abs (flag_i_) > 4)
318     {
319       Symbol fl = p->lookup_l ()->flag (flag_i_);
320       Molecule m (fl);
321       if (flag_i_ < -4){                
322         mol_p->add_bottom (m);
323       }
324       else if (flag_i_ > 4) 
325         {
326           mol_p->add_top (m);
327         }
328       else
329         assert (false); 
330       assert (!abbrev_flag_i_);
331     }
332
333   if (abbrev_flag_i_)
334     mol_p->add (abbrev_mol ());
335
336   mol_p->translate (stem_xoffset_f_, X_AXIS);
337   return mol_p;
338 }
339
340 Real
341 Stem::hpos_f () const
342 {
343   return Item::hpos_f () + stem_xoffset_f_;
344 }
345
346
347 void
348 Stem::do_substitute_dependency (Score_elem*o,Score_elem*n)
349 {
350   Item * o_l = o->item ();
351   Item * n_l = n? n->item ():0;
352 //  whole_l_arr_.substitute ((Note_head*)o_l, (Note_head*)n_l);
353   head_l_arr_.substitute ((Note_head*)o_l, (Note_head*)n_l);
354   rest_l_arr_.substitute ((Note_head*)o_l, (Note_head*)n_l);
355 }