2 stem.cc -- implement Stem
4 source file of the GNU LilyPond music typesetter
6 (c) 1996, 1997--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 Jan Nieuwenhuizen <janneke@gnu.org>
9 TODO: This is way too hairy
11 #include <math.h> // m_pi
13 #include "directional-element-interface.hh"
14 #include "dimension-cache.hh"
17 #include "paper-def.hh"
18 #include "note-head.hh"
20 #include "molecule.hh"
21 #include "paper-column.hh"
25 #include "group-interface.hh"
26 #include "cross-staff.hh"
27 #include "staff-symbol-referencer.hh"
28 #include "lily-guile.icc"
32 Stem::set_beaming (int i, Direction d )
34 SCM pair = get_elt_property ("beaming");
36 if (!gh_pair_p (pair))
38 pair = gh_cons (gh_int2scm (0),gh_int2scm (0));
39 set_elt_property ("beaming", pair);
41 index_set_cell (pair, d, gh_int2scm (i));
45 Stem::beam_count (Direction d) const
47 SCM p=get_elt_property ("beaming");
49 return gh_scm2int (index_cell (p,d));
55 Stem::head_positions () const
64 Drul_array<Note_head*> e (extremal_heads ());
66 return Interval (staff_symbol_referencer (e[DOWN]).position_f (),
67 staff_symbol_referencer( e[UP]).position_f ());
72 Stem::chord_start_f () const
74 return head_positions()[get_direction ()]
75 * Staff_symbol_referencer_interface (this).staff_space ()/2.0;
79 Stem::stem_end_position () const
81 SCM p =get_elt_property ("stem-end-position");
85 Stem * me = (Stem*) this;
86 pos = get_default_stem_end_position ();
87 me->set_elt_property ("stem-end-position", gh_double2scm (pos));
90 pos = gh_scm2double (p);
96 Stem::get_direction () const
98 Direction d = directional_element (this).get ();
102 Stem * me = (Stem*) this;
103 d = get_default_dir ();
105 directional_element (me).set (d);
112 Stem::set_stemend (Real se)
115 Direction d= get_direction ();
117 if (d && d * head_positions()[get_direction ()] >= se*d)
118 warning (_ ("Weird stem size; check for narrow beams"));
120 set_elt_property ("stem-end-position", gh_double2scm (se));
124 Stem::type_i () const
126 return first_head () ? first_head ()->balltype_i () : 2;
130 Note head that determines hshift for upstems
133 Stem::support_head ()const
135 SCM h = get_elt_property ("support-head");
136 Score_element * nh = unsmob_element (h);
139 else if (heads_i () == 1)
145 return unsmob_element (gh_car (get_elt_property ("heads")));
148 return first_head ();
153 Stem::heads_i ()const
155 Group_interface gi (this, "heads");
160 The note head which forms one end of the stem.
163 Stem::first_head () const
165 return extremal_heads ()[-get_direction ()];
169 START is part where stem reaches `last' head.
171 Drul_array<Note_head*>
172 Stem::extremal_heads () const
174 const int inf = 1000000;
175 Drul_array<int> extpos;
179 Drul_array<Note_head *> exthead;
180 exthead[LEFT] = exthead[RIGHT] =0;
182 for (SCM s = get_elt_property ("heads"); gh_pair_p (s); s = gh_cdr (s))
184 Note_head * n = dynamic_cast<Note_head*> (unsmob_element (gh_car (s)));
185 Staff_symbol_referencer_interface si (n);
187 int p = int(si.position_f ());
191 if (d* p > d* extpos[d])
196 } while (flip (&d) != DOWN);
203 Stem::add_head (Rhythmic_head *n)
205 n->set_elt_property ("stem", this->self_scm_);
206 n->add_dependency (this);
208 Group_interface gi (this);
209 if (Note_head *nh = dynamic_cast<Note_head *> (n))
219 set_elt_property ("heads", SCM_EOL);
220 set_elt_property ("rests", SCM_EOL);
222 dim_cache_[X_AXIS]->off_callbacks_.push ( &Stem::off_callback);
226 Stem::invisible_b () const
229 UGH. Who determines balltype for stem?
231 Note_head * nh = dynamic_cast<Note_head*> (support_head ());
232 return !(heads_i () && nh->balltype_i () >= 1);
236 Stem::get_center_distance (Direction d) const
238 int staff_center = 0;
239 int distance = (int) (d*(head_positions()[d] - staff_center));
240 return distance >? 0;
244 Stem::get_default_dir () const
246 int du = get_center_distance (UP);
247 int dd = get_center_distance (DOWN);
250 return Direction (sign (dd -du));
252 return Direction (int(paper_l ()->get_var ("stem_default_neutral_direction")));
256 Stem::get_default_stem_end_position () const
258 bool grace_b = to_boolean (get_elt_property ("grace"));
259 String type_str = grace_b ? "grace-" : "";
264 SCM scm_len = get_elt_property("length");
265 if (gh_number_p (scm_len))
267 length_f = gh_scm2double (scm_len);
271 s = ly_eval_str (type_str + "stem-length");
272 scm_to_array (s, &a);
273 // stem uses half-spaces
274 length_f = a[((flag_i () - 2) >? 0) <? (a.size () - 1)] * 2;
278 s = ly_eval_str (type_str + "stem-shorten");
279 scm_to_array (s, &a);
281 // stem uses half-spaces
283 // fixme: use gh_list_ref () iso. array[]
284 Real shorten_f = a[((flag_i () - 2) >? 0) <? (a.size () - 1)] * 2;
287 'set-default-stemlen' sets direction too
289 Direction dir = get_direction ();
292 dir = get_default_dir ();
293 directional_element (this).set (dir);
297 stems in unnatural (forced) direction should be shortened,
298 according to [Roush & Gourlay]
300 if (((int)chord_start_f ())
301 && (get_direction () != get_default_dir ()))
302 length_f -= shorten_f;
305 Real st = head_positions()[dir] + dir * length_f;
307 bool no_extend_b = to_boolean (get_elt_property ("no-stem-extend"));
308 if (!grace_b && !no_extend_b && dir * st < 0)
318 Stem::flag_i () const
320 SCM s = get_elt_property ("duration-log");
321 return (gh_number_p (s)) ? gh_scm2int (s) : 2;
325 Stem::position_noteheads ()
330 Link_array<Score_element> heads =
331 Group_interface__extract_elements (this, (Score_element*)0, "heads");
333 heads.sort (compare_position);
334 Direction dir =get_direction ();
340 Real w = support_head ()->extent (X_AXIS)[dir];
341 for (int i=0; i < heads.size (); i++)
343 heads[i]->translate_axis (w - heads[i]->extent (X_AXIS)[dir], X_AXIS);
346 bool parity= true; // todo: make this settable.
347 int lastpos = int (Staff_symbol_referencer_interface (heads[0]).position_f ());
348 for (int i=1; i < heads.size (); i ++)
350 Real p = Staff_symbol_referencer_interface (heads[i]).position_f ();
351 int dy =abs (lastpos- (int)p);
357 Real l = heads[i]->extent (X_AXIS).length ();
358 heads[i]->translate_axis (l * get_direction (), X_AXIS);
370 Stem::do_pre_processing ()
372 stem_end_position (); // ugh. Trigger direction calc.
373 position_noteheads ();
377 set_elt_property ("transparent", SCM_BOOL_T);
382 set_spacing_hints ();
388 set stem directions for hinting the optical spacing correction.
390 Modifies DIR_LIST property of the Stem's Paper_column
392 TODO: more advanced: supply height of noteheads as well, for more advanced spacing possibilities
395 Stem::set_spacing_hints ()
399 SCM scmdir = gh_int2scm (get_direction ());
400 SCM dirlist = column_l ()->get_elt_property ("dir-list");
401 if (dirlist == SCM_UNDEFINED)
404 if (scm_sloppy_memq (scmdir, dirlist) == SCM_EOL)
406 dirlist = gh_cons (scmdir, dirlist);
407 column_l ()->set_elt_property ("dir-list", dirlist);
416 SCM st = get_elt_property ("flag-style");
417 if ( gh_string_p (st))
419 style = ly_scm2string (st);
422 char c = (get_direction () == UP) ? 'u' : 'd';
423 Molecule m = lookup_l ()->afm_find (String ("flags-") + to_str (c) +
425 if (!style.empty_b ())
426 m.add_molecule(lookup_l ()->afm_find (String ("flags-") + to_str (c) + style));
431 Stem::dim_callback (Dimension_cache const* c)
433 Stem * s = dynamic_cast<Stem*> (c->element_l ());
436 if (unsmob_element (s->get_elt_property ("beam")) || abs (s->flag_i ()) <= 2)
440 r = s->flag ().dim_.x ();
446 const Real ANGLE = 20* (2.0*M_PI/360.0); // ugh!
449 Stem::do_brew_molecule () const
453 Staff_symbol_referencer_interface si (first_head ());
455 Real y1 = si.position_f();
456 Real y2 = stem_end_position ();
458 Interval stem_y(y1,y2);
459 stem_y.unite (Interval (y2,y1));
461 Real dy = staff_symbol_referencer (this).staff_space ()/2.0;
464 head_wid = support_head ()->extent (X_AXIS).length ();
465 stem_y[Direction(-get_direction ())] += get_direction () * head_wid * tan(ANGLE)/(2*dy);
469 Real stem_width = paper_l ()->get_var ("stemthickness");
470 Molecule ss =lookup_l ()->filledbox (Box (Interval (-stem_width/2, stem_width/2),
471 Interval (stem_y[DOWN]*dy, stem_y[UP]*dy)));
472 mol.add_molecule (ss);
475 if (!beam_l () && abs (flag_i ()) > 2)
477 Molecule fl = flag ();
478 fl.translate_axis(stem_y[get_direction ()]*dy, Y_AXIS);
479 mol.add_molecule (fl);
486 Stem::off_callback (Dimension_cache const * c)
488 Stem * st = dynamic_cast<Stem*> (c->element_l ());
491 if (Note_head * f = st->first_head ())
493 Interval head_wid(0, f->extent (X_AXIS).length ());
495 if (to_boolean (st->get_elt_property ("stem-centered")))
496 return head_wid.center ();
498 Real rule_thick = st->paper_l ()->get_var ("stemthickness");
499 Direction d = st->get_direction ();
500 r = head_wid[d] - d * rule_thick ;
510 SCM b= get_elt_property ("beam");
511 return dynamic_cast<Beam*> (unsmob_element (b));
515 // ugh still very long.
517 Stem::calc_stem_info () const
521 Direction beam_dir = directional_element (beam_l ()).get ();
524 programming_error ("Beam dir not set.");
528 Staff_symbol_referencer_interface st (this);
529 Real staff_space = st.staff_space ();
530 Real half_space = staff_space / 2;
531 Real interbeam_f = paper_l ()->interbeam_f (beam_l ()->get_multiplicity ());
532 Real thick = gh_scm2double (beam_l ()->get_elt_property ("beam-thickness"));
533 int multiplicity = beam_l ()->get_multiplicity ();
536 info.idealy_f_ = chord_start_f ();
538 // for simplicity, we calculate as if dir == UP
539 info.idealy_f_ *= beam_dir;
540 SCM grace_prop = get_elt_property ("grace");
542 bool grace_b = to_boolean (grace_prop);
546 String type_str = grace_b ? "grace-" : "";
548 s = ly_eval_str (type_str + "beamed-stem-minimum-length");
549 scm_to_array (s, &a);
550 Real minimum_length = a[multiplicity <? (a.size () - 1)] * staff_space;
552 s = ly_eval_str (type_str + "beamed-stem-length");
553 scm_to_array (s, &a);
554 Real stem_length = a[multiplicity <? (a.size () - 1)] * staff_space;
556 if (!beam_dir || (beam_dir == directional_element (this).get ()))
557 /* normal beamed stem */
561 info.idealy_f_ += thick + (multiplicity - 1) * interbeam_f;
563 info.miny_f_ = info.idealy_f_;
564 info.maxy_f_ = INT_MAX;
566 info.idealy_f_ += stem_length;
567 info.miny_f_ += minimum_length;
570 lowest beam of (UP) beam must never be lower than second staffline
572 Hmm, reference (Wanske?)
574 Although this (additional) rule is probably correct,
575 I expect that highest beam (UP) should also never be lower
576 than middle staffline, just as normal stems.
579 bool no_extend_b = to_boolean (get_elt_property ("no-stem-extend"));
580 if (!grace_b && !no_extend_b)
582 /* highest beam of (UP) beam must never be lower than middle
584 lowest beam of (UP) beam must never be lower than second staffline
588 >? (- 2 * half_space - thick
589 + (multiplicity > 0) * thick
590 + interbeam_f * (multiplicity - 1));
596 info.idealy_f_ -= thick;
597 info.maxy_f_ = info.idealy_f_;
598 info.miny_f_ = -INT_MAX;
600 info.idealy_f_ -= stem_length;
601 info.maxy_f_ -= minimum_length;
604 info.idealy_f_ = (info.maxy_f_ <? info.idealy_f_) >? info.miny_f_;
606 s = beam_l ()->get_elt_property ("shorten");
608 info.idealy_f_ -= gh_double2scm (s);
610 Real interstaff_f = -beam_dir* calc_interstaff_dist (this, beam_l ());
612 info.idealy_f_ += interstaff_f;
613 info.miny_f_ += interstaff_f;
614 info.maxy_f_ += interstaff_f ;