]> git.donarmstrong.com Git - lilypond.git/blob - lily/stem.cc
release: 1.3.0
[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--1999 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 "paper-column.hh"
18 #include "misc.hh"
19 #include "beam.hh"
20 #include "rest.hh"
21
22 void
23 Stem::set_direction (Direction d)
24 {
25   if  (!dir_)
26     warning (_ ("stem direction set already!"));
27
28   dir_ = d;
29
30   /*
31     todo
32   */
33 }
34
35 Stem::Stem ()
36 {
37   beams_i_drul_[LEFT] = beams_i_drul_[RIGHT] = -1;
38   yextent_drul_[DOWN] = yextent_drul_[UP] = 0;
39   flag_i_ = 2;
40   dir_ = CENTER;
41   beam_l_ = 0;
42 }
43
44 Interval_t<int>
45 Stem::head_positions () const
46 {
47   /* 
48     Mysterious FreeBSD fix by John Galbraith.  Somehow, the empty intervals 
49     trigger FP exceptions on FreeBSD.  Fix: do not return infinity 
50
51    */
52   if (!head_l_arr_.size ())
53     {
54       return Interval_t<int> (100,-100);        
55     }
56
57   Interval_t<int> r;
58   for (int i =0; i < head_l_arr_.size (); i++)
59     {
60       int p = (int)head_l_arr_[i]->position_f ();
61       r[BIGGER] = r[BIGGER] >? p;
62       r[SMALLER] = r[SMALLER] <? p;
63     }
64   return r;
65 }
66
67 void
68 Stem::do_print () const
69 {
70 #ifndef NPRINT
71   DEBUG_OUT << "flag "<< flag_i_;
72   if (beam_l_)
73     DEBUG_OUT << "beamed";
74 #endif
75 }
76
77 Real
78 Stem::stem_length_f () const
79 {
80   return yextent_drul_[UP]-yextent_drul_[DOWN] ;
81 }
82
83 Real
84 Stem::stem_begin_f () const
85 {
86   return yextent_drul_[Direction(-dir_)];
87 }
88
89 Real
90 Stem::chord_start_f () const
91 {
92   return head_positions()[dir_] * staff_line_leading_f ()/2.0;
93 }
94
95 Real
96 Stem::stem_end_f () const
97 {
98   return yextent_drul_[dir_];
99 }
100
101 void
102 Stem::set_stemend (Real se)
103 {
104   // todo: margins
105   if (dir_ && dir_ * head_positions()[dir_] >= se*dir_)
106     warning (_ ("Weird stem size; check for narrow beams"));
107
108   
109   yextent_drul_[dir_]  =  se;
110   yextent_drul_[Direction(-dir_)] = head_positions()[-dir_];
111 }
112
113 int
114 Stem::type_i () const
115 {
116   return head_l_arr_[0]->balltype_i_;
117 }
118
119 void
120 Stem::add_head (Rhythmic_head *n)
121 {
122   n->stem_l_ = this;
123   n->add_dependency (this);     // ?
124   if (Note_head *nh = dynamic_cast<Note_head *> (n))
125     {
126       head_l_arr_.push (nh);
127     }
128   else if (Rest *r = dynamic_cast<Rest *> (n))
129     {
130       rest_l_arr_.push (r);
131     }
132 }
133
134 bool
135 Stem::invisible_b () const
136 {
137   return (!head_l_arr_.size () ||
138     head_l_arr_[0]->balltype_i_ <= 0);
139 }
140
141 int
142 Stem::get_center_distance (Direction d) const
143 {
144   int staff_center = 0;
145   int distance = d*(head_positions()[d] - staff_center);
146   return distance >? 0;
147 }
148
149 Direction
150 Stem::get_default_dir () const
151 {
152   int du = get_center_distance (UP);
153   int dd = get_center_distance (DOWN);
154
155   if (sign (dd - du))
156     return Direction (sign (dd -du));
157
158   return Direction (int(paper_l ()->get_var ("stem_default_neutral_direction")));
159 }
160
161 Direction
162 Stem::get_dir () const
163 {
164   return dir_;
165 }
166
167
168 void
169 Stem::set_default_stemlen ()
170 {
171   Real length_f = 0.;
172   SCM scm_len = get_elt_property(length_scm_sym);
173   if (scm_len != SCM_BOOL_F)
174     {
175       length_f = gh_scm2double (SCM_CDR(scm_len));
176     }
177   else
178     length_f = paper_l ()->get_var ("stem_length0");
179
180   bool grace_b = get_elt_property (grace_scm_sym) != SCM_BOOL_F;
181   String type_str = grace_b ? "grace_" : "";
182
183   Real shorten_f = paper_l ()->get_var (type_str + "forced_stem_shorten0");
184
185   if (!dir_)
186     dir_ = get_default_dir ();
187
188   /* 
189     stems in unnatural (forced) direction should be shortened, 
190     according to [Roush & Gourlay]
191    */
192   if (((int)chord_start_f ())
193       && (dir_ != get_default_dir ()))
194     length_f -= shorten_f;
195
196   if (flag_i_ >= 5)
197     length_f += 2.0;
198   if (flag_i_ >= 6)
199     length_f += 1.0;
200   
201   set_stemend ((dir_ > 0) ? head_positions()[BIGGER] + length_f:
202                head_positions()[SMALLER] - length_f);
203
204   bool no_extend_b = get_elt_property (no_stem_extend_scm_sym) != SCM_BOOL_F;
205   if (!grace_b && !no_extend_b && (dir_ * stem_end_f () < 0))
206     set_stemend (0);
207 }
208
209 //xxx
210 void
211 Stem::set_default_extents ()
212 {
213   if (!stem_length_f ())
214     set_default_stemlen ();
215
216 }
217
218 void
219 Stem::set_noteheads ()
220 {
221   if (!head_l_arr_.size ())
222     return;
223   head_l_arr_.sort (Note_head::compare);
224   if (dir_ < 0)
225     head_l_arr_.reverse ();
226
227   Note_head * beginhead =   head_l_arr_[0];
228   beginhead->set_elt_property (extremal_scm_sym, SCM_BOOL_T);
229   if  (beginhead !=   head_l_arr_.top ())
230     head_l_arr_.top ()->set_elt_property (extremal_scm_sym, SCM_BOOL_T);
231   
232   int parity=1;
233   int lastpos = int (beginhead->position_f ());
234   for (int i=1; i < head_l_arr_.size (); i ++)
235     {
236       int dy =abs (lastpos- (int)head_l_arr_[i]->position_f ());
237
238       if (dy <= 1)
239         {
240           if (parity)
241             head_l_arr_[i]->flip_around_stem (dir_);
242           parity = !parity;
243         }
244       else
245         parity = 1;
246       lastpos = int (head_l_arr_[i]->position_f ());
247     }
248 }
249
250 void
251 Stem::do_pre_processing ()
252 {
253   if (yextent_drul_[DOWN]== yextent_drul_[UP])
254     set_default_extents ();
255   set_noteheads ();
256
257   if (invisible_b ())
258     {
259       set_elt_property (transparent_scm_sym, SCM_BOOL_T);
260     }
261   set_empty (invisible_b (), X_AXIS, Y_AXIS);
262   set_spacing_hints ();
263 }
264
265
266
267 /**
268    set stem directions for hinting the optical spacing correction.
269
270    Modifies DIR_LIST property of the Stem's Score_column
271
272    TODO: more advanced: supply height of noteheads as well, for more advanced spacing possibilities
273  */
274
275 void
276 Stem::set_spacing_hints () 
277 {
278   if (!invisible_b ())
279     {
280       SCM scmdir  = gh_int2scm (dir_);
281       SCM dirlist = column_l ()->get_elt_property (dir_list_scm_sym);
282       if (dirlist == SCM_BOOL_F)
283         dirlist = SCM_EOL;
284       else
285         dirlist = SCM_CDR (dirlist);
286
287       if (scm_sloppy_memq (scmdir, dirlist) == SCM_EOL)
288         {
289           dirlist = gh_cons (scmdir, dirlist);
290           column_l ()->set_elt_property (dir_list_scm_sym, dirlist);
291         }
292     }
293 }
294
295 Molecule
296 Stem::flag () const
297 {
298   String style;
299   SCM st = get_elt_property (style_scm_sym);
300   if ( st != SCM_BOOL_F)
301     {
302       st = SCM_CDR(st);
303       style = ly_scm2string (st);
304     }
305
306   char c = (dir_ == UP) ? 'u' : 'd';
307   Molecule m = lookup_l ()->afm_find (String ("flags-") + to_str (c) + 
308                                       to_str (flag_i_));
309   if (!style.empty_b ())
310     m.add_molecule(lookup_l ()->afm_find (String ("flags-") + to_str (c) + style));
311   return m;
312 }
313
314 Interval
315 Stem::do_width () const
316 {
317   Interval r (0, 0);
318   if (beam_l_ || abs (flag_i_) <= 2)
319     ;   // TODO!
320   else
321     {
322       r = flag ().dim_.x ();
323       r += note_delta_f ();
324     }
325   return r;
326 }
327
328
329
330
331 const Real ANGLE = 20* (2.0*M_PI/360.0); // ugh!
332
333 Molecule*
334 Stem::do_brew_molecule_p () const
335 {
336   Molecule *mol_p =new Molecule;
337   Drul_array<Real> stem_y = yextent_drul_;
338   Real dy = staff_line_leading_f ()/2.0;
339
340   Real head_wid = 0;
341   if (head_l_arr_.size ())
342     head_wid = head_l_arr_[0]->extent (X_AXIS).length ();
343   stem_y[Direction(-dir_)] += dir_ * head_wid * tan(ANGLE)/(2*dy);
344   
345   if (!invisible_b ())
346     {
347       Real stem_width = paper_l ()->get_var ("stemthickness");
348       Molecule ss =lookup_l ()->filledbox (Box (Interval (-stem_width/2, stem_width/2),
349                                                  Interval (stem_y[DOWN]*dy, stem_y[UP]*dy)));
350       mol_p->add_molecule (ss);
351     }
352
353   if (!beam_l_ && abs (flag_i_) > 2)
354     {
355       Molecule fl = flag ();
356       fl.translate_axis(stem_y[dir_]*dy, Y_AXIS);
357       mol_p->add_molecule (fl);
358     }
359
360   if (head_l_arr_.size())
361     {
362       mol_p->translate_axis (note_delta_f (), X_AXIS);
363     }
364   return mol_p;
365 }
366
367 Real
368 Stem::note_delta_f () const
369 {
370   Real r=0;
371   if (head_l_arr_.size())
372     {
373       Interval head_wid(0,  head_l_arr_[0]->extent (X_AXIS).length ());
374          Real rule_thick = paper_l ()->get_var ("stemthickness");
375
376       Interval stem_wid(-rule_thick/2, rule_thick/2);
377       if (dir_ == CENTER)
378         r = head_wid.center ();
379       else
380         r = head_wid[dir_] - stem_wid[dir_];
381     }
382   return r;
383 }
384
385 Real
386 Stem::hpos_f () const
387 {
388   return note_delta_f () + Item::hpos_f ();
389 }
390
391 void
392 Stem::do_substitute_element_pointer (Score_element*o,Score_element*n)
393 {
394   if (Note_head*h=dynamic_cast<Note_head*> (o))
395     head_l_arr_.substitute (h, dynamic_cast<Note_head*>(n));
396   if (Rest *r=dynamic_cast<Rest*> (o))
397     rest_l_arr_.substitute (r, dynamic_cast<Rest*>(n));
398   if (Beam* b = dynamic_cast<Beam*> (o))
399     {
400       if (b == beam_l_) 
401         beam_l_ = dynamic_cast<Beam*> (n);
402     }
403   Staff_symbol_referencer::do_substitute_element_pointer (o,n);
404 }