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