source file of the GNU LilyPond music typesetter
- (c) 1996, 1997--1999 Han-Wen Nienhuys <hanwen@cs.uu.nl>
+ (c) 1996--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
Jan Nieuwenhuizen <janneke@gnu.org>
TODO: This is way too hairy
*/
+#include <math.h> // m_pi
+#include "directional-element-interface.hh"
#include "dimension-cache.hh"
#include "stem.hh"
#include "debug.hh"
#include "staff-symbol-referencer.hh"
+
void
Stem::set_beaming (int i, Direction d )
{
return 0;
}
-Interval_t<int>
+Interval
Stem::head_positions () const
{
- /*
- Mysterious FreeBSD fix by John Galbraith. Somehow, the empty intervals
- trigger FP exceptions on FreeBSD. Fix: do not return infinity
-
- */
- if (!first_head ())
+ if (!heads_i ())
{
- return Interval_t<int> (100,-100);
+ Interval iv;
+ return iv;
}
- Link_array<Note_head> head_l_arr =
- Group_interface__extract_elements (this, (Note_head*)0, "heads");
- Interval_t<int> r;
- for (int i =0; i < head_l_arr.size (); i++)
- {
- Staff_symbol_referencer_interface si (head_l_arr[i]);
- int p = (int)si.position_f ();
- r[BIGGER] = r[BIGGER] >? p;
- r[SMALLER] = r[SMALLER] <? p;
- }
- return r;
+ Drul_array<Note_head*> e (extremal_heads ());
+
+ return Interval (staff_symbol_referencer (e[DOWN]).position_f (),
+ staff_symbol_referencer( e[UP]).position_f ());
}
Stem::stem_end_position () const
{
SCM p =get_elt_property ("stem-end-position");
- Real len;
+ Real pos;
if (!gh_number_p (p))
{
Stem * me = (Stem*) this;
- len = get_default_stemlen ();
- me->set_elt_property ("stem-end-position", gh_double2scm (len));
+ pos = get_default_stem_end_position ();
+ me->set_elt_property ("stem-end-position", gh_double2scm (pos));
}
else
- len = gh_scm2double (p);
+ pos = gh_scm2double (p);
- return len;
+ return pos;
}
+Direction
+Stem::get_direction () const
+{
+ Direction d = directional_element (this).get ();
+
+ if (!d)
+ {
+ Stem * me = (Stem*) this;
+ d = get_default_dir ();
+ // urg, AAARGH!
+ directional_element (me).set (d);
+ }
+ return d ;
+}
+
+
void
Stem::set_stemend (Real se)
{
return first_head () ? first_head ()->balltype_i () : 2;
}
-
-
/*
Note head that determines hshift for upstems
*/
Score_element*
Stem::support_head ()const
{
- SCM h = get_elt_property ("support-head");
+ SCM h = get_elt_pointer ("support-head");
Score_element * nh = unsmob_element (h);
if (nh)
return nh;
+ else if (heads_i () == 1)
+ {
+ /*
+ UGH.
+ */
+
+ return unsmob_element (gh_car (get_elt_pointer ("heads")));
+ }
else
return first_head ();
}
+int
+Stem::heads_i ()const
+{
+ Pointer_group_interface gi (this, "heads");
+ return gi.count ();
+}
+
/*
The note head which forms one end of the stem.
*/
Note_head*
Stem::first_head () const
{
- const int inf = 1000000;
- int pos = -inf;
- Direction dir = get_direction ();
+ return extremal_heads ()[-get_direction ()];
+}
- Note_head *nh =0;
- for (SCM s = get_elt_property ("heads"); gh_pair_p (s); s = gh_cdr (s))
+/*
+ START is part where stem reaches `last' head.
+ */
+Drul_array<Note_head*>
+Stem::extremal_heads () const
+{
+ const int inf = 1000000;
+ Drul_array<int> extpos;
+ extpos[DOWN] = inf;
+ extpos[UP] = -inf;
+
+ Drul_array<Note_head *> exthead;
+ exthead[LEFT] = exthead[RIGHT] =0;
+
+ for (SCM s = get_elt_pointer ("heads"); gh_pair_p (s); s = gh_cdr (s))
{
Note_head * n = dynamic_cast<Note_head*> (unsmob_element (gh_car (s)));
Staff_symbol_referencer_interface si (n);
- int p = dir * int(si.position_f ());
- if (p > pos)
+
+ int p = int(si.position_f ());
+
+ Direction d = LEFT;
+ do {
+ if (d* p > d* extpos[d])
{
- nh = n;
- pos = p;
+ exthead[d] = n;
+ extpos[d] = p;
}
+ } while (flip (&d) != DOWN);
}
- return nh;
+ return exthead;
}
void
Stem::add_head (Rhythmic_head *n)
{
- n->set_elt_property ("stem", this->self_scm_);
- n->add_dependency (this); // ?
-
+ n->set_elt_pointer ("stem", this->self_scm_);
+ n->add_dependency (this);
- Group_interface gi (this);
+ Pointer_group_interface gi (this);
if (Note_head *nh = dynamic_cast<Note_head *> (n))
gi.name_ = "heads";
else
gi.add_element (n);
}
-Stem::Stem ()
+Stem::Stem (SCM s)
+ : Item (s)
{
- set_elt_property ("heads", SCM_EOL);
- set_elt_property ("rests", SCM_EOL);
+ set_elt_pointer ("heads", SCM_EOL);
+ set_elt_pointer ("rests", SCM_EOL);
+
+ add_offset_callback ( &Stem::off_callback, X_AXIS);
}
bool
Stem::invisible_b () const
{
- return !(first_head () && first_head()->balltype_i () >= 1);
+ /*
+ UGH. Who determines balltype for stem?
+ */
+ Note_head * nh = dynamic_cast<Note_head*> (support_head ());
+ return !(heads_i () && nh->balltype_i () >= 1);
}
int
Stem::get_center_distance (Direction d) const
{
int staff_center = 0;
- int distance = d*(head_positions()[d] - staff_center);
+ int distance = (int) (d*(head_positions()[d] - staff_center));
return distance >? 0;
}
return Direction (int(paper_l ()->get_var ("stem_default_neutral_direction")));
}
+/*
+ ugh. A is used for different purposes. This functionality should be
+ moved into scheme at some point to get rid of the silly
+ conversions. (but lets wait till we have namespaces in SCM)
+ */
Real
-Stem::get_default_stemlen () const
+Stem::get_default_stem_end_position () const
{
+ bool grace_b = to_boolean (get_elt_property ("grace"));
+ String type_str = grace_b ? "grace-" : "";
+ SCM s;
+ Array<Real> a;
+
Real length_f = 0.;
SCM scm_len = get_elt_property("length");
if (gh_number_p (scm_len))
length_f = gh_scm2double (scm_len);
}
else
- length_f = paper_l ()->get_var ("stem_length0");
+ {
+ s = scm_eval (ly_symbol2scm ((type_str + "stem-length").ch_C()));
+ for (SCM q = s; q != SCM_EOL; q = gh_cdr (q))
+ a.push (gh_scm2double (gh_car (q)));
+
+ // stem uses half-spaces
+ length_f = a[((flag_i () - 2) >? 0) <? (a.size () - 1)] * 2;
+ }
- bool grace_b = get_elt_property ("grace") != SCM_UNDEFINED;
- String type_str = grace_b ? "grace_" : "";
- Real shorten_f = paper_l ()->get_var (type_str + "forced_stem_shorten0");
+ a.clear ();
+ s = scm_eval (ly_symbol2scm ((type_str + "stem-shorten").ch_C()));
+ for (SCM q = s; q != SCM_EOL; q = gh_cdr (q))
+ a.push (gh_scm2double (gh_car (q)));
+
+
+ // stem uses half-spaces
+
+ // fixme: use gh_list_ref () iso. array[]
+ Real shorten_f = a[((flag_i () - 2) >? 0) <? (a.size () - 1)] * 2;
/* URGURGURG
'set-default-stemlen' sets direction too
Direction dir = get_direction ();
if (!dir)
{
- Stem * me = (Stem*) this;
dir = get_default_dir ();
- me->set_direction (dir);
+ directional_element (this).set (dir);
}
/*
&& (get_direction () != get_default_dir ()))
length_f -= shorten_f;
- /*
- UGK.!
- */
- if (flag_i () >= 5)
- length_f += 2.0;
- if (flag_i () >= 6)
- length_f += 1.0;
-
+ Real st = head_positions()[dir] + dir * length_f;
- Real st = head_positions()[-dir] + dir * length_f;
-
- bool no_extend_b = get_elt_property ("no-stem-extend") != SCM_UNDEFINED;
- if (!grace_b && !no_extend_b && dir * st < 0)
- st = 0.0;
+ bool no_extend_b = to_boolean (get_elt_property ("no-stem-extend"));
+ if (!grace_b && !no_extend_b && dir * st < 0)
+ st = 0.0;
return st;
}
+/*
+ FIXME: wrong name
+ */
int
Stem::flag_i () const
{
void
Stem::position_noteheads ()
{
- if (!first_head ())
+ if (!heads_i ())
return;
Link_array<Score_element> heads =
- Group_interface__extract_elements (this, (Score_element*)0, "heads");
+ Pointer_group_interface__extract_elements (this, (Score_element*)0, "heads");
heads.sort (compare_position);
Direction dir =get_direction ();
}
}
-void
-Stem::do_pre_processing ()
+GLUE_SCORE_ELEMENT(Stem,before_line_breaking);
+SCM
+Stem::member_before_line_breaking ()
{
- get_default_stemlen (); // ugh. Trigger direction calc.
+ stem_end_position (); // ugh. Trigger direction calc.
position_noteheads ();
if (invisible_b ())
{
- set_elt_property ("transparent", SCM_BOOL_T);
- set_empty (Y_AXIS);
- set_empty (X_AXIS);
+ remove_elt_property ("molecule-callback");
+ // suicide();
}
-
+
set_spacing_hints ();
+ return SCM_UNDEFINED;
}
Stem::flag () const
{
String style;
- SCM st = get_elt_property ("style");
- if ( st != SCM_UNDEFINED)
+ SCM st = get_elt_property ("flag-style");
+ if ( gh_string_p (st))
{
style = ly_scm2string (st);
}
}
Interval
-Stem::dim_callback (Dimension_cache const* c)
+Stem::dim_callback (Score_element const *se, Axis )
{
- Stem * s = dynamic_cast<Stem*> (c->element_l ());
+ Stem * s = dynamic_cast<Stem*> ((Score_element*)se);
Interval r (0, 0);
- if (s->get_elt_property ("beam") != SCM_UNDEFINED || abs (s->flag_i ()) <= 2)
+ if (unsmob_element (s->get_elt_pointer ("beam")) || abs (s->flag_i ()) <= 2)
; // TODO!
else
{
- r = s->flag ().dim_.x ();
- r += s->note_delta_f ();
+ r = s->flag ().extent (X_AXIS);
}
return r;
}
-const Real ANGLE = 20* (2.0*M_PI/360.0); // ugh!
+const Real ANGLE = 20* (2.0*M_PI/360.0); // ugh! Should be settable.
+
+
+GLUE_SCORE_ELEMENT(Stem,brew_molecule);
-Molecule*
-Stem::do_brew_molecule_p () const
+SCM
+Stem::member_brew_molecule () const
{
- Molecule *mol_p =new Molecule;
+ Molecule mol;
Staff_symbol_referencer_interface si (first_head ());
Interval stem_y(y1,y2);
stem_y.unite (Interval (y2,y1));
- Real dy = staff_symbol_referencer_interface (this)
- .staff_space ()/2.0;
-
+ Real dy = staff_symbol_referencer (this).staff_space ()/2.0;
Real head_wid = 0;
if (support_head ())
head_wid = support_head ()->extent (X_AXIS).length ();
Real stem_width = paper_l ()->get_var ("stemthickness");
Molecule ss =lookup_l ()->filledbox (Box (Interval (-stem_width/2, stem_width/2),
Interval (stem_y[DOWN]*dy, stem_y[UP]*dy)));
- mol_p->add_molecule (ss);
+ mol.add_molecule (ss);
}
if (!beam_l () && abs (flag_i ()) > 2)
{
Molecule fl = flag ();
fl.translate_axis(stem_y[get_direction ()]*dy, Y_AXIS);
- mol_p->add_molecule (fl);
+ mol.add_molecule (fl);
}
- if (first_head ())
- {
- mol_p->translate_axis (note_delta_f (), X_AXIS);
- }
- return mol_p;
+ return mol.create_scheme();
}
Real
-Stem::note_delta_f () const
+Stem::off_callback (Score_element const* se, Axis)
{
+ Stem *st = dynamic_cast<Stem*> ((Score_element*)se);
+
Real r=0;
- if (first_head ())
+ if (Note_head * f = st->first_head ())
{
- Interval head_wid(0, first_head()->extent (X_AXIS).length ());
- Real rule_thick = paper_l ()->get_var ("stemthickness");
+ Interval head_wid(0, f->extent (X_AXIS).length ());
- Interval stem_wid(-rule_thick/2, rule_thick/2);
- if (get_direction () == CENTER)
- r = head_wid.center ();
- else
- r = head_wid[get_direction ()] - stem_wid[get_direction ()];
+ if (to_boolean (st->get_elt_property ("stem-centered")))
+ return head_wid.center ();
+
+ Real rule_thick = st->paper_l ()->get_var ("stemthickness");
+ Direction d = st->get_direction ();
+ r = head_wid[d] - d * rule_thick ;
}
return r;
}
-Real
-Stem::hpos_f () const
-{
- return note_delta_f () + Item::hpos_f ();
-}
Beam*
Stem::beam_l ()const
{
- SCM b= get_elt_property ("beam");
+ SCM b= get_elt_pointer ("beam");
return dynamic_cast<Beam*> (unsmob_element (b));
}
{
assert (beam_l ());
- Direction beam_dir = beam_l ()->get_direction ();
+ Direction beam_dir = directional_element (beam_l ()).get ();
if (!beam_dir)
{
programming_error ("Beam dir not set.");
beam_dir = UP;
}
- Stem_info info;
- Real internote_f
- = staff_symbol_referencer_interface (this).staff_space ()/2;
+ Staff_symbol_referencer_interface st (this);
+ Real staff_space = st.staff_space ();
+ Real half_space = staff_space / 2;
Real interbeam_f = paper_l ()->interbeam_f (beam_l ()->get_multiplicity ());
- Real beam_f = gh_scm2double (beam_l ()->get_elt_property ("beam-thickness"));
-
+ Real thick = gh_scm2double (beam_l ()->get_elt_property ("beam-thickness"));
+ int multiplicity = beam_l ()->get_multiplicity ();
+
+ Stem_info info;
info.idealy_f_ = chord_start_f ();
// for simplicity, we calculate as if dir == UP
info.idealy_f_ *= beam_dir;
SCM grace_prop = get_elt_property ("grace");
- bool grace_b = gh_boolean_p (grace_prop) && gh_scm2bool (grace_prop);
- SCM extend_prop = get_elt_property ("no-stem-extend");
- bool no_extend_b = gh_boolean_p (extend_prop) && gh_scm2bool (extend_prop);
-
- int stem_max = (int)rint(paper_l ()->get_var ("stem_max"));
- String type_str = grace_b ? "grace_" : "";
- Real min_stem_f = paper_l ()->get_var (type_str + "minimum_stem_length"
- + to_str (beam_l ()->get_multiplicity () <? stem_max)) * internote_f;
- Real stem_f = paper_l ()->get_var (type_str + "stem_length"
- + to_str (beam_l ()->get_multiplicity () <? stem_max)) * internote_f;
-
- if (!beam_dir || (beam_dir == get_direction ()))
+
+ bool grace_b = to_boolean (grace_prop);
+
+ Array<Real> a;
+ SCM s;
+ String type_str = grace_b ? "grace-" : "";
+
+ s = scm_eval (ly_symbol2scm ((type_str + "beamed-stem-minimum-length").ch_C()));
+ a.clear ();
+ for (SCM q = s; q != SCM_EOL; q = gh_cdr (q))
+ a.push (gh_scm2double (gh_car (q)));
+
+
+ Real minimum_length = a[multiplicity <? (a.size () - 1)] * staff_space;
+ s = scm_eval (ly_symbol2scm ((type_str + "beamed-stem-length").ch_C()));
+
+ a.clear();
+ for (SCM q = s; q != SCM_EOL; q = gh_cdr (q))
+ a.push (gh_scm2double (gh_car (q)));
+
+ Real stem_length = a[multiplicity <? (a.size () - 1)] * staff_space;
+
+ if (!beam_dir || (beam_dir == directional_element (this).get ()))
/* normal beamed stem */
{
- if (beam_l ()->get_multiplicity ())
+ if (multiplicity)
{
- info.idealy_f_ += beam_f
- + (beam_l ()->get_multiplicity () - 1) * interbeam_f;
+ info.idealy_f_ += thick + (multiplicity - 1) * interbeam_f;
}
info.miny_f_ = info.idealy_f_;
info.maxy_f_ = INT_MAX;
- info.idealy_f_ += stem_f;
- info.miny_f_ += min_stem_f;
+ info.idealy_f_ += stem_length;
+ info.miny_f_ += minimum_length;
/*
lowest beam of (UP) beam must never be lower than second staffline
than middle staffline, just as normal stems.
*/
+ bool no_extend_b = to_boolean (get_elt_property ("no-stem-extend"));
if (!grace_b && !no_extend_b)
{
- /* highest beam of (UP) beam must never be lower than middle staffline
-
+ /* highest beam of (UP) beam must never be lower than middle
+ staffline
lowest beam of (UP) beam must never be lower than second staffline
*/
info.miny_f_ =
info.miny_f_ >? 0
- >? (- 2 * internote_f - beam_f
- + (beam_l ()->get_multiplicity () > 0) * beam_f
- + interbeam_f * (beam_l ()->get_multiplicity () - 1));
+ >? (- 2 * half_space - thick
+ + (multiplicity > 0) * thick
+ + interbeam_f * (multiplicity - 1));
}
}
else
/* knee */
{
- info.idealy_f_ -= beam_f;
+ info.idealy_f_ -= thick;
info.maxy_f_ = info.idealy_f_;
info.miny_f_ = -INT_MAX;
- info.idealy_f_ -= stem_f;
- info.maxy_f_ -= min_stem_f;
+ info.idealy_f_ -= stem_length;
+ info.maxy_f_ -= minimum_length;
}
info.idealy_f_ = (info.maxy_f_ <? info.idealy_f_) >? info.miny_f_;
- SCM s = beam_l ()->get_elt_property ("shorten");
+ s = beam_l ()->get_elt_property ("shorten");
if (gh_number_p (s))
- info.idealy_f_ -= gh_double2scm (s);
+ info.idealy_f_ -= gh_scm2double (s);
- Real interstaff_f = beam_dir* calc_interstaff_dist (this, beam_l ());
+ Real interstaff_f = -beam_dir* calc_interstaff_dist (this, beam_l ());
info.idealy_f_ += interstaff_f;
info.miny_f_ += interstaff_f;