]> git.donarmstrong.com Git - lilypond.git/blob - lily/stem.cc
patch::: 1.1.19.jcn4: jcn4
[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
23
24
25 Stem::~Stem ()
26 {
27 }
28
29 Stem::Stem ()
30 {
31   /*
32     TODO: staff-size
33     */
34   beams_i_drul_[LEFT] = beams_i_drul_[RIGHT] = -1;
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   beam_l_ = 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 (dynamic_cast<Note_head *> (n))
119     {
120       head_l_arr_.push ((Note_head*)n);
121     }
122   else if (dynamic_cast<Rest *> (n))
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   Real internote_f = paper ()->internote_f ();
168   Real length_f = paper ()->get_var ("stem_length0") / internote_f;
169   Real shorten_f = paper ()->get_var ("forced_stem_shorten0") / internote_f;
170
171   if (!dir_)
172     set_default_dir ();
173   /* 
174     stems in unnatural (forced) direction should be shortened, 
175     accoding to [Roush & Gourlay]
176    */
177   if (((int)chord_start_f ())
178       && (dir_ != get_default_dir ()))
179     length_f -= shorten_f;
180
181   if (flag_i_ >= 5)
182     length_f += 2.0;
183   if (flag_i_ >= 6)
184     length_f += 1.0;
185   
186   set_stemend ((dir_ > 0) ? head_positions()[BIGGER] + length_f:
187                head_positions()[SMALLER] - length_f);
188
189   if (dir_ * stem_end_f () < 0)
190     set_stemend (0);
191 }
192
193 //xxx
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       r = lookup_l ()->flag (flag_i_, dir_).dim_.x ();
263       r += note_delta_f ();
264     }
265   return r;
266 }
267
268
269
270
271 const Real ANGLE = 20* (2.0*M_PI/360.0); // ugh!
272
273 Molecule*
274 Stem::do_brew_molecule_p () const
275 {
276   Molecule *mol_p =new Molecule;
277   Drul_array<Real> stem_y = yextent_drul_;
278   Real dy = paper ()->internote_f ();
279
280   Real head_wid = 0;
281   if (head_l_arr_.size ())
282     head_wid = head_l_arr_[0]->extent (X_AXIS).length ();
283   stem_y[Direction(-dir_)] += dir_ * head_wid * tan(ANGLE)/(2*dy);
284   
285   if (!invisible_b ())
286     {
287       Atom ss =lookup_l ()->stem (stem_y[DOWN]*dy,
288                                      stem_y[UP]*dy);
289       mol_p->add_atom (ss);
290     }
291
292   if (!beam_l_ && abs (flag_i_) > 2)
293     {
294       Atom fl = lookup_l ()->flag (flag_i_, dir_);
295       fl.translate_axis(stem_y[dir_]*dy, Y_AXIS);
296       mol_p->add_atom (fl);
297     }
298
299   if (head_l_arr_.size())
300     {
301       mol_p->translate_axis (note_delta_f (), X_AXIS);
302     }
303   return mol_p;
304 }
305
306 Real
307 Stem::note_delta_f () const
308 {
309   Real r=0;
310   if (head_l_arr_.size())
311     {
312       Interval head_wid(0,  head_l_arr_[0]->extent (X_AXIS).length ());
313       Real rule_thick(paper ()->rule_thickness ());
314       Interval stem_wid(-rule_thick/2, rule_thick/2);
315       if (stem_xdir_ == CENTER)
316         r = head_wid.center ();
317       else
318         r = head_wid[stem_xdir_] - stem_wid[stem_xdir_];
319     }
320   return r;
321 }
322
323 Real
324 Stem::hpos_f () const
325 {
326   return note_delta_f () + Item::hpos_f ();
327 }
328
329 /*
330   TODO:  head_l_arr_/rest_l_arr_ in  do_substitute_dependent ()
331  */
332 void
333 Stem::do_substitute_dependency (Score_element*o,Score_element*n)
334 {
335   if (Note_head*h=dynamic_cast<Note_head*> (o))
336   head_l_arr_.substitute (h, dynamic_cast<Note_head*>(n));
337   if (Rest *r=dynamic_cast<Rest*> (o))
338     rest_l_arr_.substitute (r, dynamic_cast<Rest*>(n));
339 }