]> git.donarmstrong.com Git - lilypond.git/blob - lily/stem.cc
partial: 1.0.1.jcn
[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@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   beam_l_ = 0;
33   beams_left_i_ = 0;
34   beams_right_i_ = 0;
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 }
47
48 Interval_t<int>
49 Stem::head_positions () const
50 {
51   Interval_t<int> r;
52   for (int i =0; i < head_l_arr_.size (); i++)
53     {
54       int p = head_l_arr_[i]->position_i_;
55       r[BIGGER] = r[BIGGER] >? p;
56       r[SMALLER] = r[SMALLER] <? p;
57     }
58   return r;
59 }
60
61 void
62 Stem::do_print () const
63 {
64 #ifndef NPRINT
65   DOUT << "flag "<< flag_i_ ;
66   if (beam_l_)
67     DOUT << "beamed";
68 #endif
69 }
70
71 Real
72 Stem::stem_length_f () const
73 {
74   return yextent_drul_[UP]-yextent_drul_[DOWN] ;
75 }
76
77 Real
78 Stem::stem_begin_f () const
79 {
80   return yextent_drul_[Direction(-dir_)];
81 }
82
83 Real
84 Stem::chord_start_f () const
85 {
86   return head_positions()[dir_] * paper ()->internote_f ();
87 }
88
89 Real
90 Stem::stem_end_f () const
91 {
92   return yextent_drul_[dir_];
93 }
94
95 void
96 Stem::set_stemend (Real se)
97 {
98   // todo: margins
99   if (dir_ && dir_ * head_positions()[dir_] >= se*dir_)
100     warning (_("Weird stem size; check for narrow beams"));
101
102   
103   yextent_drul_[dir_]  =  se;
104   yextent_drul_[Direction(-dir_)] = head_positions()[-dir_];
105 }
106
107 int
108 Stem::type_i () const
109 {
110   return head_l_arr_[0]->balltype_i_;
111 }
112
113 void
114 Stem::add (Rhythmic_head *n)
115 {
116   n->add_dependency (this);     // ?
117   if (n->is_type_b (Note_head::static_name ()))
118     {
119       head_l_arr_.push ((Note_head*)n);
120     }
121   else if (n->is_type_b (Rest::static_name ()))
122     {
123       rest_l_arr_.push ((Rest*)n);
124     }
125 }
126
127 bool
128 Stem::invisible_b () const
129 {
130   return (!head_l_arr_.size () ||
131     head_l_arr_[0]->balltype_i_ <= 0);
132 }
133
134 int
135 Stem::get_center_distance (Direction d)
136 {
137   int staff_center = 0;
138   int distance = d*(head_positions()[d] - staff_center);
139   return distance >? 0;
140 }
141
142 Direction
143 Stem::get_default_dir ()
144 {
145   return (get_center_distance (UP) >
146           get_center_distance (DOWN)) 
147     ? DOWN 
148     : UP;
149 }
150
151 Direction
152 Stem::get_dir ()
153 {
154   return dir_;
155 }
156
157 void
158 Stem::set_default_dir ()
159 {
160   dir_ = get_default_dir ();
161 }
162
163 void
164 Stem::set_default_stemlen ()
165 {
166   Real len = STEMLEN;
167   Real dy = paper ()->interbeam_f (mult_i_);
168
169   if (!dir_)
170     set_default_dir ();
171
172   /* If the stem points in the "wrong" direction, it should be
173      shortened, according to [Roush & Gourlay].  Their suggestion to knock off
174      a whole staffspace is a bit drastical though.
175      */
176   else if (dir_ != get_default_dir ())
177     len  -= 1.0;
178
179   if (flag_i_ >= 5)
180     len += 2.0;
181   if (flag_i_ >= 6)
182     len += 1.0;
183   
184   set_stemend ((dir_ > 0) ? head_positions()[BIGGER] + len :
185                head_positions()[SMALLER] - len);
186
187
188   if (dir_ * stem_end_f () < 0)
189     {
190       set_stemend (0);
191     }
192 }
193
194 void
195 Stem::set_default_extents ()
196 {
197   if (!stem_length_f ())
198     set_default_stemlen ();
199
200
201   if (dir_ == UP)
202     stem_xdir_ = RIGHT;
203   if (invisible_b ())
204     stem_xdir_ = CENTER;
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_xdir_ == LEFT) ? LEFT : RIGHT;
234           parity = !parity;
235         }
236       else
237         parity = 1;
238       lastpos = head_l_arr_[i]->position_i_;
239     }
240 }
241
242 void
243 Stem::do_pre_processing ()
244 {
245   if (yextent_drul_[DOWN]== yextent_drul_[UP])
246     set_default_extents ();
247   set_noteheads ();
248   flag_i_ = flag_i_;
249   transparent_b_ = invisible_b ();
250   set_empty (invisible_b ());
251 }
252
253
254 Interval
255 Stem::do_width () const
256 {
257   Interval r (0, 0);
258   if (beam_l_ || abs (flag_i_) <= 2)
259     ;   // TODO!
260   else
261     {
262       Paper_def*p= paper ();
263       r = p->lookup_l ()->flag (flag_i_, dir_).dim_.x ();
264       r += note_delta_f ();
265     }
266   return r;
267 }
268
269
270
271
272 const Real ANGLE = 20* (2.0*M_PI/360.0); // ugh!
273
274 Molecule*
275 Stem::brew_molecule_p () const
276 {
277   Molecule *mol_p =new Molecule;
278   Paper_def *p =paper ();
279   Drul_array<Real> stem_y = yextent_drul_;
280   Real dy = p->internote_f ();
281   
282
283   Real head_wid = 0;
284   if (head_l_arr_.size ())
285     head_wid = head_l_arr_[0]->width ().length ();
286   stem_y[Direction(-dir_)] += dir_ * head_wid * tan(ANGLE)/(2*dy);
287   
288   if (!invisible_b ())
289     {
290       Atom ss =p->lookup_l ()->stem (stem_y[DOWN]*dy,
291                                      stem_y[UP]*dy);
292       mol_p->add (Atom (ss));
293     }
294
295   if (!beam_l_ &&abs (flag_i_) > 2)
296     {
297       Atom fl = p->lookup_l ()->flag (flag_i_, dir_);
298       fl.translate_axis(stem_y[dir_]*dy, Y_AXIS);
299       mol_p->add(fl);
300     }
301
302   if (head_l_arr_.size())
303     {
304       mol_p->translate_axis (note_delta_f (), X_AXIS);
305     }
306   return mol_p;
307 }
308
309 Real
310 Stem::note_delta_f () const
311 {
312   Real r=0;
313   if (head_l_arr_.size())
314     {
315       Interval head_wid(0,  head_l_arr_[0]->width ().length ());
316       Real rule_thick(paper ()->rule_thickness ());
317       Interval stem_wid(-rule_thick/2, rule_thick/2);
318       if (stem_xdir_ == CENTER)
319         r = head_wid.center ();
320       else
321         r = head_wid[stem_xdir_] - stem_wid[stem_xdir_];
322     }
323   return r;
324 }
325
326 Real
327 Stem::hpos_f () const
328 {
329   return note_delta_f () + Item::hpos_f ();
330 }
331
332 /*
333   TODO:  head_l_arr_/rest_l_arr_ in  do_substitute_dependent ()
334  */
335 void
336  Stem::do_substitute_dependency (Score_elem*o,Score_elem*n)
337 {
338   Item * o_l = o->item ();
339   Item * n_l = n? n->item () : 0;
340   head_l_arr_.substitute ((Note_head*)o_l, (Note_head*)n_l);
341   rest_l_arr_.substitute ((Rest*)o_l, (Rest*)n_l);
342 }
343