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