]> git.donarmstrong.com Git - lilypond.git/blob - lily/stem.cc
release: 1.3.3
[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  (!get_direction ())
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   set_direction (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(-get_direction ())];
87 }
88
89 Real
90 Stem::chord_start_f () const
91 {
92   return head_positions()[get_direction ()] * staff_line_leading_f ()/2.0;
93 }
94
95 Real
96 Stem::stem_end_f () const
97 {
98   return yextent_drul_[get_direction ()];
99 }
100
101 void
102 Stem::set_stemend (Real se)
103 {
104   // todo: margins
105   if (get_direction () && get_direction () * head_positions()[get_direction ()] >= se*get_direction ())
106     warning (_ ("Weird stem size; check for narrow beams"));
107
108   
109   yextent_drul_[get_direction ()]  =  se;
110   yextent_drul_[Direction(-get_direction ())] = head_positions()[-get_direction ()];
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
162
163 void
164 Stem::set_default_stemlen ()
165 {
166   Real length_f = 0.;
167   SCM scm_len = get_elt_property("length");
168   if (scm_len != SCM_UNDEFINED)
169     {
170       length_f = gh_scm2double (scm_len);
171     }
172   else
173     length_f = paper_l ()->get_var ("stem_length0");
174
175   bool grace_b = get_elt_property ("grace") != SCM_UNDEFINED;
176   String type_str = grace_b ? "grace_" : "";
177
178   Real shorten_f = paper_l ()->get_var (type_str + "forced_stem_shorten0");
179
180   if (!get_direction ())
181     set_direction (get_default_dir ());
182
183   /* 
184     stems in unnatural (forced) direction should be shortened, 
185     according to [Roush & Gourlay]
186    */
187   if (((int)chord_start_f ())
188       && (get_direction () != get_default_dir ()))
189     length_f -= shorten_f;
190
191   if (flag_i_ >= 5)
192     length_f += 2.0;
193   if (flag_i_ >= 6)
194     length_f += 1.0;
195   
196   set_stemend ((get_direction () > 0) ? head_positions()[BIGGER] + length_f:
197                head_positions()[SMALLER] - length_f);
198
199   bool no_extend_b = get_elt_property ("no-stem-extend") != SCM_UNDEFINED;
200   if (!grace_b && !no_extend_b && (get_direction () * stem_end_f () < 0))
201     set_stemend (0);
202 }
203
204 //xxx
205 void
206 Stem::set_default_extents ()
207 {
208   if (!stem_length_f ())
209     set_default_stemlen ();
210
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 (get_direction () < 0)
220     head_l_arr_.reverse ();
221
222   Note_head * beginhead =   head_l_arr_[0];
223   beginhead->set_elt_property ("extremal", SCM_BOOL_T);
224   if  (beginhead !=   head_l_arr_.top ())
225     head_l_arr_.top ()->set_elt_property ("extremal", SCM_BOOL_T);
226   
227   int parity=1;
228   int lastpos = int (beginhead->position_f ());
229   for (int i=1; i < head_l_arr_.size (); i ++)
230     {
231       int dy =abs (lastpos- (int)head_l_arr_[i]->position_f ());
232
233       if (dy <= 1)
234         {
235           if (parity)
236             head_l_arr_[i]->flip_around_stem (get_direction ());
237           parity = !parity;
238         }
239       else
240         parity = 1;
241       lastpos = int (head_l_arr_[i]->position_f ());
242     }
243 }
244
245 void
246 Stem::do_pre_processing ()
247 {
248   if (yextent_drul_[DOWN]== yextent_drul_[UP])
249     set_default_extents ();
250   set_noteheads ();
251
252   if (invisible_b ())
253     {
254       set_elt_property ("transparent", SCM_BOOL_T);
255     }
256   set_empty (invisible_b (), X_AXIS, Y_AXIS);
257   set_spacing_hints ();
258 }
259
260
261
262 /**
263    set stem directions for hinting the optical spacing correction.
264
265    Modifies DIR_LIST property of the Stem's Score_column
266
267    TODO: more advanced: supply height of noteheads as well, for more advanced spacing possibilities
268  */
269
270 void
271 Stem::set_spacing_hints () 
272 {
273   if (!invisible_b ())
274     {
275       SCM scmdir  = gh_int2scm (get_direction ());
276       SCM dirlist = column_l ()->get_elt_property ("dir-list");
277       if (dirlist == SCM_UNDEFINED)
278         dirlist = SCM_EOL;
279
280       if (scm_sloppy_memq (scmdir, dirlist) == SCM_EOL)
281         {
282           dirlist = gh_cons (scmdir, dirlist);
283           column_l ()->set_elt_property ("dir-list", dirlist);
284         }
285     }
286 }
287
288 Molecule
289 Stem::flag () const
290 {
291   String style;
292   SCM st = get_elt_property ("style");
293   if ( st != SCM_UNDEFINED)
294     {
295       style = ly_scm2string (st);
296     }
297
298   char c = (get_direction () == UP) ? 'u' : 'd';
299   Molecule m = lookup_l ()->afm_find (String ("flags-") + to_str (c) + 
300                                       to_str (flag_i_));
301   if (!style.empty_b ())
302     m.add_molecule(lookup_l ()->afm_find (String ("flags-") + to_str (c) + style));
303   return m;
304 }
305
306 Interval
307 Stem::do_width () const
308 {
309   Interval r (0, 0);
310   if (beam_l_ || abs (flag_i_) <= 2)
311     ;   // TODO!
312   else
313     {
314       r = flag ().dim_.x ();
315       r += note_delta_f ();
316     }
317   return r;
318 }
319
320
321
322
323 const Real ANGLE = 20* (2.0*M_PI/360.0); // ugh!
324
325 Molecule*
326 Stem::do_brew_molecule_p () const
327 {
328   Molecule *mol_p =new Molecule;
329   Drul_array<Real> stem_y = yextent_drul_;
330   Real dy = staff_line_leading_f ()/2.0;
331
332   Real head_wid = 0;
333   if (head_l_arr_.size ())
334     head_wid = head_l_arr_[0]->extent (X_AXIS).length ();
335   stem_y[Direction(-get_direction ())] += get_direction () * head_wid * tan(ANGLE)/(2*dy);
336   
337   if (!invisible_b ())
338     {
339       Real stem_width = paper_l ()->get_var ("stemthickness");
340       Molecule ss =lookup_l ()->filledbox (Box (Interval (-stem_width/2, stem_width/2),
341                                                  Interval (stem_y[DOWN]*dy, stem_y[UP]*dy)));
342       mol_p->add_molecule (ss);
343     }
344
345   if (!beam_l_ && abs (flag_i_) > 2)
346     {
347       Molecule fl = flag ();
348       fl.translate_axis(stem_y[get_direction ()]*dy, Y_AXIS);
349       mol_p->add_molecule (fl);
350     }
351
352   if (head_l_arr_.size())
353     {
354       mol_p->translate_axis (note_delta_f (), X_AXIS);
355     }
356   return mol_p;
357 }
358
359 Real
360 Stem::note_delta_f () const
361 {
362   Real r=0;
363   if (head_l_arr_.size())
364     {
365       Interval head_wid(0,  head_l_arr_[0]->extent (X_AXIS).length ());
366          Real rule_thick = paper_l ()->get_var ("stemthickness");
367
368       Interval stem_wid(-rule_thick/2, rule_thick/2);
369       if (get_direction () == CENTER)
370         r = head_wid.center ();
371       else
372         r = head_wid[get_direction ()] - stem_wid[get_direction ()];
373     }
374   return r;
375 }
376
377 Real
378 Stem::hpos_f () const
379 {
380   return note_delta_f () + Item::hpos_f ();
381 }
382
383 void
384 Stem::do_substitute_element_pointer (Score_element*o,Score_element*n)
385 {
386   if (Note_head*h=dynamic_cast<Note_head*> (o))
387     head_l_arr_.substitute (h, dynamic_cast<Note_head*>(n));
388   if (Rest *r=dynamic_cast<Rest*> (o))
389     rest_l_arr_.substitute (r, dynamic_cast<Rest*>(n));
390   if (Beam* b = dynamic_cast<Beam*> (o))
391     {
392       if (b == beam_l_) 
393         beam_l_ = dynamic_cast<Beam*> (n);
394     }
395   Staff_symbol_referencer::do_substitute_element_pointer (o,n);
396 }
397