X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=lily%2Fstem.cc;h=6c111feee18050b85a78676aee9ad67fdacc05cf;hb=b0064942493df77833e6e41e05d362850f4874b0;hp=7c7797a385c9c43c4ec3256776b86fe1c9fc8309;hpb=3a0e9efb7f067e5b334ba0596b95e15d96d7cc49;p=lilypond.git diff --git a/lily/stem.cc b/lily/stem.cc index 7c7797a385..6c111feee1 100644 --- a/lily/stem.cc +++ b/lily/stem.cc @@ -3,11 +3,15 @@ source file of the GNU LilyPond music typesetter - (c) 1996, 1997--1999 Han-Wen Nienhuys + (c) 1996--2000 Han-Wen Nienhuys + Jan Nieuwenhuizen TODO: This is way too hairy */ +#include // m_pi +#include "directional-element-interface.hh" +#include "dimension-cache.hh" #include "stem.hh" #include "debug.hh" #include "paper-def.hh" @@ -18,131 +22,222 @@ #include "misc.hh" #include "beam.hh" #include "rest.hh" +#include "group-interface.hh" +#include "cross-staff.hh" +#include "staff-symbol-referencer.hh" -void -Stem::set_direction (Direction d) -{ - if (!dir_) - warning (_ ("stem direction set already!")); - dir_ = d; - /* - todo - */ +void +Stem::set_beaming (int i, Direction d ) +{ + SCM pair = get_elt_property ("beaming"); + + if (!gh_pair_p (pair)) + { + pair = gh_cons (gh_int2scm (0),gh_int2scm (0)); + set_elt_property ("beaming", pair); + } + index_set_cell (pair, d, gh_int2scm (i)); } -Stem::Stem () +int +Stem::beam_count (Direction d) const { - beams_i_drul_[LEFT] = beams_i_drul_[RIGHT] = -1; - yextent_drul_[DOWN] = yextent_drul_[UP] = 0; - flag_i_ = 2; - dir_ = CENTER; - beam_l_ = 0; + SCM p=get_elt_property ("beaming"); + if (gh_pair_p (p)) + return gh_scm2int (index_cell (p,d)); + else + return 0; } -Interval_t +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 (!head_l_arr_.size ()) + if (!heads_i ()) { - return Interval_t (100,-100); + Interval iv; + return iv; } - Interval_t r; - for (int i =0; i < head_l_arr_.size (); i++) - { - int p = (int)head_l_arr_[i]->position_f (); - r[BIGGER] = r[BIGGER] >? p; - r[SMALLER] = r[SMALLER] e (extremal_heads ()); -void -Stem::do_print () const -{ -#ifndef NPRINT - DEBUG_OUT << "flag "<< flag_i_; - if (beam_l_) - DEBUG_OUT << "beamed"; -#endif + return Interval (staff_symbol_referencer (e[DOWN]).position_f (), + staff_symbol_referencer( e[UP]).position_f ()); } -Real -Stem::stem_length_f () const -{ - return yextent_drul_[UP]-yextent_drul_[DOWN] ; -} Real -Stem::stem_begin_f () const +Stem::chord_start_f () const { - return yextent_drul_[Direction(-dir_)]; + return head_positions()[get_direction ()] + * Staff_symbol_referencer_interface (this).staff_space ()/2.0; } Real -Stem::chord_start_f () const +Stem::stem_end_position () const { - return head_positions()[dir_] * staff_line_leading_f ()/2.0; + SCM p =get_elt_property ("stem-end-position"); + Real pos; + if (!gh_number_p (p)) + { + Stem * me = (Stem*) this; + pos = get_default_stem_end_position (); + me->set_elt_property ("stem-end-position", gh_double2scm (pos)); + } + else + pos = gh_scm2double (p); + + return pos; } -Real -Stem::stem_end_f () const +Direction +Stem::get_direction () const { - return yextent_drul_[dir_]; + 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) { // todo: margins - if (dir_ && dir_ * head_positions()[dir_] >= se*dir_) + Direction d= get_direction (); + + if (d && d * head_positions()[get_direction ()] >= se*d) warning (_ ("Weird stem size; check for narrow beams")); - - yextent_drul_[dir_] = se; - yextent_drul_[Direction(-dir_)] = head_positions()[-dir_]; + set_elt_property ("stem-end-position", gh_double2scm (se)); } int Stem::type_i () const { - return head_l_arr_[0]->balltype_i_; + return first_head () ? first_head ()->balltype_i () : 2; } -void -Stem::add_head (Rhythmic_head *n) +/* + Note head that determines hshift for upstems + */ +Score_element* +Stem::support_head ()const { - n->stem_l_ = this; - n->add_dependency (this); // ? - if (Note_head *nh = dynamic_cast (n)) + SCM h = get_elt_pointer ("support-head"); + Score_element * nh = unsmob_element (h); + if (nh) + return nh; + else if (heads_i () == 1) { - head_l_arr_.push (nh); + /* + UGH. + */ + + return unsmob_element (gh_car (get_elt_pointer ("heads"))); } - else if (Rest *r = dynamic_cast (n)) + 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 +{ + return extremal_heads ()[-get_direction ()]; +} + +/* + START is part where stem reaches `last' head. + */ +Drul_array +Stem::extremal_heads () const +{ + const int inf = 1000000; + Drul_array extpos; + extpos[DOWN] = inf; + extpos[UP] = -inf; + + Drul_array exthead; + exthead[LEFT] = exthead[RIGHT] =0; + + for (SCM s = get_elt_pointer ("heads"); gh_pair_p (s); s = gh_cdr (s)) { - rest_l_arr_.push (r); + Note_head * n = dynamic_cast (unsmob_element (gh_car (s))); + Staff_symbol_referencer_interface si (n); + + int p = int(si.position_f ()); + + Direction d = LEFT; + do { + if (d* p > d* extpos[d]) + { + exthead[d] = n; + extpos[d] = p; + } + } while (flip (&d) != DOWN); } + + return exthead; +} + +void +Stem::add_head (Rhythmic_head *n) +{ + n->set_elt_pointer ("stem", this->self_scm_); + n->add_dependency (this); + + Pointer_group_interface gi (this); + if (Note_head *nh = dynamic_cast (n)) + gi.name_ = "heads"; + else + gi.name_ = "rests"; + + gi.add_element (n); +} + +Stem::Stem (SCM s) + : Item (s) +{ + 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 (!head_l_arr_.size () || - head_l_arr_[0]->balltype_i_ <= 0); + /* + UGH. Who determines balltype for stem? + */ + Note_head * nh = dynamic_cast (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; } @@ -158,107 +253,142 @@ Stem::get_default_dir () const return Direction (int(paper_l ()->get_var ("stem_default_neutral_direction"))); } -Direction -Stem::get_dir () const +/* + 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_stem_end_position () const { - return dir_; -} - + bool grace_b = to_boolean (get_elt_property ("grace")); + String type_str = grace_b ? "grace-" : ""; + SCM s; + Array a; -void -Stem::set_default_stemlen () -{ Real length_f = 0.; - SCM scm_len = get_elt_property(length_scm_sym); - if (scm_len != SCM_BOOL_F) + SCM scm_len = get_elt_property("length"); + if (gh_number_p (scm_len)) { - length_f = gh_scm2double (SCM_CDR(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) get_var (type_str + "forced_stem_shorten0"); + // stem uses half-spaces - if (!dir_) - dir_ = get_default_dir (); + // fixme: use gh_list_ref () iso. array[] + Real shorten_f = a[((flag_i () - 2) >? 0) = 5) - length_f += 2.0; - if (flag_i_ >= 6) - length_f += 1.0; + + Real st = head_positions()[dir] + dir * length_f; - set_stemend ((dir_ > 0) ? head_positions()[BIGGER] + length_f: - head_positions()[SMALLER] - length_f); + bool no_extend_b = to_boolean (get_elt_property ("no-stem-extend")); + if (!grace_b && !no_extend_b && dir * st < 0) + st = 0.0; - bool no_extend_b = get_elt_property (no_stem_extend_scm_sym) != SCM_BOOL_F; - if (!grace_b && !no_extend_b && (dir_ * stem_end_f () < 0)) - set_stemend (0); + return st; } -//xxx -void -Stem::set_default_extents () +/* + FIXME: wrong name + */ +int +Stem::flag_i () const { - if (!stem_length_f ()) - set_default_stemlen (); - + SCM s = get_elt_property ("duration-log"); + return (gh_number_p (s)) ? gh_scm2int (s) : 2; } void -Stem::set_noteheads () +Stem::position_noteheads () { - if (!head_l_arr_.size ()) + if (!heads_i ()) return; - head_l_arr_.sort (Note_head::compare); - if (dir_ < 0) - head_l_arr_.reverse (); - - Note_head * beginhead = head_l_arr_[0]; - beginhead->set_elt_property (extremal_scm_sym, SCM_BOOL_T); - if (beginhead != head_l_arr_.top ()) - head_l_arr_.top ()->set_elt_property (extremal_scm_sym, SCM_BOOL_T); - int parity=1; - int lastpos = int (beginhead->position_f ()); - for (int i=1; i < head_l_arr_.size (); i ++) + Link_array heads = + Pointer_group_interface__extract_elements (this, (Score_element*)0, "heads"); + + heads.sort (compare_position); + Direction dir =get_direction (); + + if (dir < 0) + heads.reverse (); + + + Real w = support_head ()->extent (X_AXIS)[dir]; + for (int i=0; i < heads.size (); i++) + { + heads[i]->translate_axis (w - heads[i]->extent (X_AXIS)[dir], X_AXIS); + } + + bool parity= true; // todo: make this settable. + int lastpos = int (Staff_symbol_referencer_interface (heads[0]).position_f ()); + for (int i=1; i < heads.size (); i ++) { - int dy =abs (lastpos- (int)head_l_arr_[i]->position_f ()); + Real p = Staff_symbol_referencer_interface (heads[i]).position_f (); + int dy =abs (lastpos- (int)p); if (dy <= 1) { if (parity) - head_l_arr_[i]->flip_around_stem (dir_); + { + Real l = heads[i]->extent (X_AXIS).length (); + heads[i]->translate_axis (l * get_direction (), X_AXIS); + } parity = !parity; } else - parity = 1; - lastpos = int (head_l_arr_[i]->position_f ()); + parity = true; + + lastpos = int (p); } } void -Stem::do_pre_processing () +Stem::before_line_breaking () { - if (yextent_drul_[DOWN]== yextent_drul_[UP]) - set_default_extents (); - set_noteheads (); + stem_end_position (); // ugh. Trigger direction calc. + position_noteheads (); if (invisible_b ()) { - set_elt_property (transparent_scm_sym, SCM_BOOL_T); + remove_elt_property ("molecule-callback"); + // suicide(); } - set_empty (invisible_b (), X_AXIS, Y_AXIS); + set_spacing_hints (); } @@ -267,27 +397,24 @@ Stem::do_pre_processing () /** set stem directions for hinting the optical spacing correction. - Modifies DIR_LIST property of the Stem's Score_column + Modifies DIR_LIST property of the Stem's Paper_column TODO: more advanced: supply height of noteheads as well, for more advanced spacing possibilities */ - void Stem::set_spacing_hints () { if (!invisible_b ()) { - SCM scmdir = gh_int2scm (dir_); - SCM dirlist = column_l ()->get_elt_property (dir_list_scm_sym); - if (dirlist == SCM_BOOL_F) + SCM scmdir = gh_int2scm (get_direction ()); + SCM dirlist = column_l ()->get_elt_property ("dir-list"); + if (dirlist == SCM_UNDEFINED) dirlist = SCM_EOL; - else - dirlist = SCM_CDR (dirlist); if (scm_sloppy_memq (scmdir, dirlist) == SCM_EOL) { dirlist = gh_cons (scmdir, dirlist); - column_l ()->set_elt_property (dir_list_scm_sym, dirlist); + column_l ()->set_elt_property ("dir-list", dirlist); } } } @@ -296,109 +423,214 @@ Molecule Stem::flag () const { String style; - SCM st = get_elt_property (style_scm_sym); - if ( st != SCM_BOOL_F) + SCM st = get_elt_property ("flag-style"); + if ( gh_string_p (st)) { - st = SCM_CDR(st); style = ly_scm2string (st); } - char c = (dir_ == UP) ? 'u' : 'd'; + char c = (get_direction () == UP) ? 'u' : 'd'; Molecule m = lookup_l ()->afm_find (String ("flags-") + to_str (c) + - to_str (flag_i_)); + to_str (flag_i ())); if (!style.empty_b ()) m.add_molecule(lookup_l ()->afm_find (String ("flags-") + to_str (c) + style)); return m; } Interval -Stem::do_width () const +Stem::dim_callback (Score_element const *se, Axis ) { + Stem * s = dynamic_cast ((Score_element*)se); + Interval r (0, 0); - if (beam_l_ || abs (flag_i_) <= 2) + if (unsmob_element (s->get_elt_pointer ("beam")) || abs (s->flag_i ()) <= 2) ; // TODO! else { - r = flag ().dim_.x (); - r += note_delta_f (); + r = s->flag ().extent (X_AXIS); } return r; } +const Real ANGLE = 20* (2.0*M_PI/360.0); // ugh! Should be settable. - -const Real ANGLE = 20* (2.0*M_PI/360.0); // ugh! - -Molecule* -Stem::do_brew_molecule_p () const +MAKE_SCHEME_SCORE_ELEMENT_CALLBACKS(Stem) +Molecule +Stem::do_brew_molecule () const { - Molecule *mol_p =new Molecule; - Drul_array stem_y = yextent_drul_; - Real dy = staff_line_leading_f ()/2.0; + Molecule mol; + + Staff_symbol_referencer_interface si (first_head ()); + + Real y1 = si.position_f(); + Real y2 = stem_end_position (); + + Interval stem_y(y1,y2); + stem_y.unite (Interval (y2,y1)); + Real dy = staff_symbol_referencer (this).staff_space ()/2.0; Real head_wid = 0; - if (head_l_arr_.size ()) - head_wid = head_l_arr_[0]->extent (X_AXIS).length (); - stem_y[Direction(-dir_)] += dir_ * head_wid * tan(ANGLE)/(2*dy); + if (support_head ()) + head_wid = support_head ()->extent (X_AXIS).length (); + stem_y[Direction(-get_direction ())] += get_direction () * head_wid * tan(ANGLE)/(2*dy); if (!invisible_b ()) { 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) + if (!beam_l () && abs (flag_i ()) > 2) { Molecule fl = flag (); - fl.translate_axis(stem_y[dir_]*dy, Y_AXIS); - mol_p->add_molecule (fl); + fl.translate_axis(stem_y[get_direction ()]*dy, Y_AXIS); + mol.add_molecule (fl); } - if (head_l_arr_.size()) - { - mol_p->translate_axis (note_delta_f (), X_AXIS); - } - return mol_p; + return mol; } Real -Stem::note_delta_f () const +Stem::off_callback (Score_element const* se, Axis) { + Stem *st = dynamic_cast ((Score_element*)se); + Real r=0; - if (head_l_arr_.size()) + if (Note_head * f = st->first_head ()) { - Interval head_wid(0, head_l_arr_[0]->extent (X_AXIS).length ()); - Real rule_thick = paper_l ()->get_var ("stemthickness"); - - Interval stem_wid(-rule_thick/2, rule_thick/2); - if (dir_ == CENTER) - r = head_wid.center (); - else - r = head_wid[dir_] - stem_wid[dir_]; + Interval head_wid(0, f->extent (X_AXIS).length ()); + + 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 + + +Beam* +Stem::beam_l ()const { - return note_delta_f () + Item::hpos_f (); + SCM b= get_elt_pointer ("beam"); + return dynamic_cast (unsmob_element (b)); } -void -Stem::do_substitute_element_pointer (Score_element*o,Score_element*n) + +// ugh still very long. +Stem_info +Stem::calc_stem_info () const { - if (Note_head*h=dynamic_cast (o)) - head_l_arr_.substitute (h, dynamic_cast(n)); - if (Rest *r=dynamic_cast (o)) - rest_l_arr_.substitute (r, dynamic_cast(n)); - if (Beam* b = dynamic_cast (o)) + assert (beam_l ()); + + Direction beam_dir = directional_element (beam_l ()).get (); + if (!beam_dir) + { + programming_error ("Beam dir not set."); + beam_dir = UP; + } + + 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 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 = to_boolean (grace_prop); + + Array 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 (n); + if (multiplicity) + { + info.idealy_f_ += thick + (multiplicity - 1) * interbeam_f; + } + info.miny_f_ = info.idealy_f_; + info.maxy_f_ = INT_MAX; + + info.idealy_f_ += stem_length; + info.miny_f_ += minimum_length; + + /* + lowest beam of (UP) beam must never be lower than second staffline + + Hmm, reference (Wanske?) + + Although this (additional) rule is probably correct, + I expect that highest beam (UP) should also never be lower + 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 + lowest beam of (UP) beam must never be lower than second staffline + */ + info.miny_f_ = + info.miny_f_ >? 0 + >? (- 2 * half_space - thick + + (multiplicity > 0) * thick + + interbeam_f * (multiplicity - 1)); + } } - Staff_symbol_referencer::do_substitute_element_pointer (o,n); + else + /* knee */ + { + info.idealy_f_ -= thick; + info.maxy_f_ = info.idealy_f_; + info.miny_f_ = -INT_MAX; + + info.idealy_f_ -= stem_length; + info.maxy_f_ -= minimum_length; + } + + info.idealy_f_ = (info.maxy_f_ ? info.miny_f_; + + s = beam_l ()->get_elt_property ("shorten"); + if (gh_number_p (s)) + info.idealy_f_ -= gh_double2scm (s); + + Real interstaff_f = -beam_dir* calc_interstaff_dist (this, beam_l ()); + + info.idealy_f_ += interstaff_f; + info.miny_f_ += interstaff_f; + info.maxy_f_ += interstaff_f ; + + return info; } +