]> git.donarmstrong.com Git - lilypond.git/blob - lily/stem.cc
9bdfd99f8029390698dcf58c833d425a63296a90
[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 Interval_t<int>
47 Stem::head_positions () const
48 {
49   Interval_t<int> r;
50   for (int i =0; i < head_l_arr_.size (); i++)
51     {
52       int p = head_l_arr_[i]->position_i_;
53       r[BIGGER] = r[BIGGER] >? p;
54       r[SMALLER] = r[SMALLER] <? p;
55     }
56   return r;
57 }
58
59 void
60 Stem::do_print () const
61 {
62 #ifndef NPRINT
63   DOUT << "flag "<< flag_i_ << "abbrev_flag_i_" << abbrev_flag_i_;
64   if (beam_l_)
65     DOUT << "beamed";
66 #endif
67 }
68
69 Real
70 Stem::stem_length_f () const
71 {
72   return yextent_drul_[UP]-yextent_drul_[DOWN] ;
73 }
74
75 Real
76 Stem::stem_start_f () const
77 {
78   return yextent_drul_[Direction(-dir_)];
79 }
80
81 Real
82 Stem::stem_end_f () const
83 {
84   return yextent_drul_[dir_];
85 }
86
87 void
88 Stem::set_stemend (Real se)
89 {
90   // todo: margins
91   if (dir_ && dir_ * head_positions()[dir_] >= se*dir_)
92     warning (_("Weird stem size; check for narrow beams"));
93
94   
95   yextent_drul_[dir_]  =  se;
96   yextent_drul_[Direction(-dir_)] = head_positions()[-dir_];
97 }
98
99 int
100 Stem::type_i () const
101 {
102   return head_l_arr_[0]->balltype_i_;
103 }
104
105 void
106 Stem::add (Rhythmic_head *n)
107 {
108   n->add_dependency (this);     // ?
109   if (n->is_type_b (Note_head::static_name ()))
110     {
111       head_l_arr_.push ((Note_head*)n);
112     }
113   else if (n->is_type_b (Rest::static_name ()))
114     {
115       rest_l_arr_.push ((Rest*)n);
116     }
117 }
118
119 bool
120 Stem::invisible_b () const
121 {
122   return (!head_l_arr_.size () ||
123     head_l_arr_[0]->balltype_i_ <= 0);
124 }
125
126 int
127 Stem::get_center_distance (Direction d)
128 {
129   int staff_center = 0;
130   int distance = d*(head_positions()[d] - staff_center);
131   return distance >? 0;
132 }
133
134 Direction
135 Stem::get_default_dir ()
136 {
137   return (get_center_distance (UP) >=
138           get_center_distance (DOWN)) 
139     ? DOWN 
140     : UP;
141 }
142
143
144 void
145 Stem::set_default_dir ()
146 {
147   dir_ = get_default_dir ();
148 }
149
150 void
151 Stem::set_default_stemlen ()
152 {
153   Real len = STEMLEN;
154   Real dy = paper ()->interbeam_f ();
155
156   // ugh, should get nice *rule* for this
157   if (abbrev_flag_i_ > 1)
158     len += (abbrev_flag_i_ - 1)* dy / 2;
159
160
161   if (!dir_)
162     set_default_dir ();
163
164   /* If the stem points in the "wrong" direction, it should be
165      shortened, according to [Roush & Gourlay].  Their suggestion to knock off
166      a whole staffspace is a bit drastical though.
167      */
168   else if (dir_ != get_default_dir ())
169     len  -= 1.0;
170
171   if (flag_i_ >= 5)
172     len += 2.0;
173   if (flag_i_ >= 6)
174     len += 1.0;
175   
176   set_stemend ((dir_ > 0) ? head_positions()[BIGGER] + len :
177                head_positions()[SMALLER] - len);
178
179
180   if (dir_ * stem_end_f () < 0)
181     {
182       set_stemend (0);
183     }
184 }
185
186 void
187 Stem::set_default_extents ()
188 {
189   if (!stem_length_f ())
190     set_default_stemlen ();
191
192
193   if (dir_ == UP)
194     stem_xdir_ = RIGHT;
195   if (invisible_b ())
196     stem_xdir_ = CENTER;
197 }
198
199 /*
200   TODO
201
202   move into note_column.cc
203
204   */
205 void
206 Stem::set_noteheads ()
207 {
208   if (!head_l_arr_.size ())
209     return;
210   head_l_arr_.sort (Note_head::compare);
211   if (dir_ < 0)
212     head_l_arr_.reverse ();
213
214   head_l_arr_[0]->extremal_i_ = -1;
215   head_l_arr_.top ()->extremal_i_ = 1;
216   int parity=1;
217   int lastpos = head_l_arr_[0]->position_i_;
218   for (int i=1; i < head_l_arr_.size (); i ++)
219     {
220       int dy =abs (lastpos- head_l_arr_[i]->position_i_);
221
222       if (dy <= 1)
223         {
224           if (parity)
225             head_l_arr_[i]->x_dir_ = (stem_xdir_ == LEFT) ? LEFT : RIGHT;
226           parity = !parity;
227         }
228       else
229         parity = 1;
230       lastpos = head_l_arr_[i]->position_i_;
231     }
232 }
233
234 void
235 Stem::do_pre_processing ()
236 {
237   if (yextent_drul_[DOWN]== yextent_drul_[UP])
238     set_default_extents ();
239   set_noteheads ();
240   flag_i_ = flag_i_;
241   transparent_b_ = invisible_b ();
242   set_empty (invisible_b ());
243 }
244
245
246 Interval
247 Stem::do_width () const
248 {
249   Interval r (0, 0);
250   if (abbrev_flag_i_)
251     {
252       r = abbrev_mol ().extent ().x ();
253     }
254   else if (beam_l_ || abs (flag_i_) <= 2)
255     ;   // TODO!
256   else
257     {
258       Paper_def*p= paper ();
259       r = p->lookup_l ()->flag (flag_i_, dir_).dim_.x ();
260       r += note_delta_f ();
261     }
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_f = paper ()->internote_f () / 4;
276
277   if (beam_l_) {
278     // huh?
279       slope_f = 2 * beam_l_->slope_f;
280     // ugh, rather calc from Abbreviation_req
281       beams_i = beams_right_i_ >? beams_left_i_;
282   }
283   paper ()->lookup_l ()->beam (slope_f, 20 PT);
284
285   Molecule beams;
286   Atom a (paper ()->lookup_l ()->beam (slope_f, w));
287   a.translate (Offset(- w / 2, stem_end_f () - (w / 2 * slope_f)));
288   
289   // ugh
290   if (!beams_i)
291     a.translate_axis (dy + beamdy - dir_ * dy, Y_AXIS);
292   else
293     a.translate_axis (2 * beamdy - dir_ * (beamdy - dy), Y_AXIS);
294
295   for (int i = 0; i < abbrev_flag_i_; i++)
296     {
297       Atom b (a);
298       b.translate_axis (-dir_ * dy * (beams_i + i), Y_AXIS);
299       beams.add (b);
300     }
301
302   return beams;
303 }
304
305 const Real ANGLE = 20* (2.0*M_PI/360.0); // ugh!
306
307 Molecule*
308 Stem::brew_molecule_p () const
309 {
310   Molecule *mol_p =new Molecule;
311   Paper_def *p =paper ();
312   Drul_array<Real> stem_y = yextent_drul_;
313   Real dy = p->internote_f ();
314   
315
316   Real head_wid = 0;
317   if (head_l_arr_.size ())
318     head_wid = head_l_arr_[0]->width ().length ();
319   stem_y[Direction(-dir_)] += dir_ * head_wid * tan(ANGLE)/(2*dy);
320   
321   if (!invisible_b ())
322     {
323       Atom ss =p->lookup_l ()->stem (stem_y[DOWN]*dy,
324                                      stem_y[UP]*dy);
325       mol_p->add (Atom (ss));
326     }
327
328   if (!beam_l_ &&abs (flag_i_) > 2)
329     {
330       Atom fl = p->lookup_l ()->flag (flag_i_, dir_);
331       fl.translate_axis(stem_y[dir_]*dy, Y_AXIS);
332       mol_p->add(fl);
333       assert (!abbrev_flag_i_);
334     }
335
336   if (abbrev_flag_i_)
337     mol_p->add (abbrev_mol ());
338
339   if (head_l_arr_.size())
340     {
341       mol_p->translate_axis (note_delta_f (), X_AXIS);
342     }
343   return mol_p;
344 }
345
346 Real
347 Stem::note_delta_f () const
348 {
349   Real r=0;
350   if (head_l_arr_.size() && stem_xdir_ != CENTER)
351     {
352       Interval head_wid(0,  head_l_arr_[0]->width ().length ());
353       Real rule_thick(paper ()->rule_thickness ());
354       Interval stem_wid(-rule_thick/2, rule_thick/2);
355       r = head_wid[stem_xdir_] - stem_wid[stem_xdir_];
356     }
357   return r;
358 }
359 Real
360 Stem::hpos_f () const
361 {
362   return note_delta_f () +Item::hpos_f ();
363 }
364
365 /*
366   TODO:  head_l_arr_/rest_l_arr_ in  do_substitute_dependent ()
367  */
368 void
369  Stem::do_substitute_dependency (Score_elem*o,Score_elem*n)
370 {
371   Item * o_l = o->item ();
372   Item * n_l = n? n->item () : 0;
373   head_l_arr_.substitute ((Note_head*)o_l, (Note_head*)n_l);
374   rest_l_arr_.substitute ((Rest*)o_l, (Rest*)n_l);
375 }