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