]> git.donarmstrong.com Git - lilypond.git/blob - lily/stem.cc
release: 1.3.10
[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 #include "dimension-cache.hh"
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 #include "group-interface.hh"
22
23 Stem::Stem ()
24 {
25   beams_i_drul_[LEFT] = beams_i_drul_[RIGHT] = -1;
26   yextent_drul_[DOWN] = yextent_drul_[UP] = 0;
27   flag_i_ = 2;
28 }
29
30 Interval_t<int>
31 Stem::head_positions () const
32 {
33   /* 
34     Mysterious FreeBSD fix by John Galbraith.  Somehow, the empty intervals 
35     trigger FP exceptions on FreeBSD.  Fix: do not return infinity 
36
37    */
38   if (!first_head ())
39     {
40       return Interval_t<int> (100,-100);        
41     }
42
43   Link_array<Note_head> head_l_arr =
44     Group_interface__extract_elements (this, (Note_head*)0, "heads");
45   
46   Interval_t<int> r;
47   for (int i =0; i < head_l_arr.size (); i++)
48     {
49       int p = (int)head_l_arr[i]->position_f ();
50       r[BIGGER] = r[BIGGER] >? p;
51       r[SMALLER] = r[SMALLER] <? p;
52     }
53   return r;
54 }
55
56 void
57 Stem::do_print () const
58 {
59 #ifndef NPRINT
60   DEBUG_OUT << "flag "<< flag_i_;
61 #endif
62 }
63
64 Real
65 Stem::stem_length_f () const
66 {
67   return yextent_drul_[UP]-yextent_drul_[DOWN] ;
68 }
69
70 Real
71 Stem::stem_begin_f () const
72 {
73   return yextent_drul_[Direction(-get_direction ())];
74 }
75
76 Real
77 Stem::chord_start_f () const
78 {
79   return head_positions()[get_direction ()] * staff_line_leading_f ()/2.0;
80 }
81
82 Real
83 Stem::stem_end_f () const
84 {
85   return yextent_drul_[get_direction ()];
86 }
87
88 void
89 Stem::set_stemend (Real se)
90 {
91   // todo: margins
92   if (get_direction () && get_direction () * head_positions()[get_direction ()] >= se*get_direction ())
93     warning (_ ("Weird stem size; check for narrow beams"));
94
95   
96   yextent_drul_[get_direction ()]  =  se;
97   yextent_drul_[Direction(-get_direction ())] = head_positions()[-get_direction ()];
98 }
99
100 int
101 Stem::type_i () const
102 {
103   
104   return first_head ()->balltype_i_;
105 }
106
107 Note_head*
108 Stem::first_head () const
109 {
110   SCM h =get_elt_property ("heads");
111   if (!gh_pair_p (h))
112     return 0;
113
114   Score_element * sc = unsmob_element (gh_car (h));
115
116   return dynamic_cast<Note_head*> (sc);
117 }
118
119 void
120 Stem::add_head (Rhythmic_head *n)
121 {
122   n->set_elt_property ("stem", this->self_scm_);
123   n->add_dependency (this);     // ?
124   
125
126   Group_interface gi (this);
127   if (Note_head *nh = dynamic_cast<Note_head *> (n))
128     gi.name_ = "heads";
129   else
130     gi.name_ = "rests";
131
132   gi.add_element (n);
133 }
134
135 bool
136 Stem::invisible_b () const
137 {
138   return !(first_head () && first_head()->balltype_i_ >= 1);
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 (!first_head ())
217     return;
218
219   
220   Link_array<Note_head> head_l_arr =
221     Group_interface__extract_elements (this, (Note_head*)0, "heads");
222
223   head_l_arr.sort (Note_head::compare);
224   if (get_direction () < 0)
225     head_l_arr.reverse ();
226
227   Note_head * beginhead =   first_head ();
228   beginhead->set_elt_property ("extremal", SCM_BOOL_T);
229   if  (beginhead !=   head_l_arr.top ())
230     head_l_arr.top ()->set_elt_property ("extremal", 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 (get_direction ());
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_BOOL_T);
260       set_empty (Y_AXIS);      
261       set_empty (X_AXIS);      
262     }
263
264
265   set_spacing_hints ();
266 }
267
268
269
270 /**
271    set stem directions for hinting the optical spacing correction.
272
273    Modifies DIR_LIST property of the Stem's Score_column
274
275    TODO: more advanced: supply height of noteheads as well, for more advanced spacing possibilities
276  */
277
278 void
279 Stem::set_spacing_hints () 
280 {
281   if (!invisible_b ())
282     {
283       SCM scmdir  = gh_int2scm (get_direction ());
284       SCM dirlist = column_l ()->get_elt_property ("dir-list");
285       if (dirlist == SCM_UNDEFINED)
286         dirlist = SCM_EOL;
287
288       if (scm_sloppy_memq (scmdir, dirlist) == SCM_EOL)
289         {
290           dirlist = gh_cons (scmdir, dirlist);
291           column_l ()->set_elt_property ("dir-list", dirlist);
292         }
293     }
294 }
295
296 Molecule
297 Stem::flag () const
298 {
299   String style;
300   SCM st = get_elt_property ("style");
301   if ( st != SCM_UNDEFINED)
302     {
303       style = ly_scm2string (st);
304     }
305
306   char c = (get_direction () == 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::dim_callback (Dimension_cache const* c) 
316 {
317   Stem * s = dynamic_cast<Stem*> (c->element_l ());
318   
319   Interval r (0, 0);
320   if (s->get_elt_property ("beam") != SCM_UNDEFINED || abs (s->flag_i_) <= 2)
321     ;   // TODO!
322   else
323     {
324       r = s->flag ().dim_.x ();
325       r += s->note_delta_f ();
326     }
327   return r;
328 }
329
330
331
332
333 const Real ANGLE = 20* (2.0*M_PI/360.0); // ugh!
334
335 Molecule*
336 Stem::do_brew_molecule_p () const
337 {
338   Molecule *mol_p =new Molecule;
339   Drul_array<Real> stem_y = yextent_drul_;
340   Real dy = staff_line_leading_f ()/2.0;
341
342   Real head_wid = 0;
343   if (first_head ())
344     head_wid = first_head ()->extent (X_AXIS).length ();
345   stem_y[Direction(-get_direction ())] += get_direction () * head_wid * tan(ANGLE)/(2*dy);
346   
347   if (!invisible_b ())
348     {
349       Real stem_width = paper_l ()->get_var ("stemthickness");
350       Molecule ss =lookup_l ()->filledbox (Box (Interval (-stem_width/2, stem_width/2),
351                                                  Interval (stem_y[DOWN]*dy, stem_y[UP]*dy)));
352       mol_p->add_molecule (ss);
353     }
354
355   if (get_elt_property ("beam") == SCM_UNDEFINED
356       && abs (flag_i_) > 2)
357     {
358       Molecule fl = flag ();
359       fl.translate_axis(stem_y[get_direction ()]*dy, Y_AXIS);
360       mol_p->add_molecule (fl);
361     }
362
363   if (first_head ())
364     {
365       mol_p->translate_axis (note_delta_f (), X_AXIS);
366     }
367   return mol_p;
368 }
369
370 Real
371 Stem::note_delta_f () const
372 {
373   Real r=0;
374   if (first_head ())
375     {
376       Interval head_wid(0,  first_head()->extent (X_AXIS).length ());
377          Real rule_thick = paper_l ()->get_var ("stemthickness");
378
379       Interval stem_wid(-rule_thick/2, rule_thick/2);
380       if (get_direction () == CENTER)
381         r = head_wid.center ();
382       else
383         r = head_wid[get_direction ()] - stem_wid[get_direction ()];
384     }
385   return r;
386 }
387
388 Real
389 Stem::hpos_f () const
390 {
391   return note_delta_f () + Item::hpos_f ();
392 }
393
394
395 Beam*
396 Stem::beam_l ()const
397 {
398   SCM b=  get_elt_property ("beam");
399   return dynamic_cast<Beam*> (unsmob_element (b));
400 }