X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=lily%2Fstem.cc;h=2dd984cac7a9f698f915af59a4e7017fcfbb5aa5;hb=abc2e31b608746a1ac924085a87244cc32ee7178;hp=0cd5486d0fe23d4390f074d87f312984f6826e36;hpb=cec6dfa6bf0fb931eb7f42b1ee44ca2ec19a9f34;p=lilypond.git diff --git a/lily/stem.cc b/lily/stem.cc index 0cd5486d0f..2dd984cac7 100644 --- a/lily/stem.cc +++ b/lily/stem.cc @@ -3,238 +3,245 @@ source file of the GNU LilyPond music typesetter - (c) 1996,1997 Han-Wen Nienhuys + (c) 1996, 1997--1999 Han-Wen Nienhuys + + TODO: This is way too hairy */ #include "stem.hh" -#include "dimen.hh" #include "debug.hh" #include "paper-def.hh" #include "note-head.hh" #include "lookup.hh" #include "molecule.hh" -#include "p-col.hh" +#include "paper-column.hh" #include "misc.hh" #include "beam.hh" #include "rest.hh" -const int STEMLEN=7; - -IMPLEMENT_IS_TYPE_B1 (Stem,Item); - -Stem::Stem () +void +Stem::set_direction (Direction d) { - /* - TODO: staff-size - */ - abbrev_flag_i_ = 0; - beam_l_ = 0; - beams_left_i_ = 0; - beams_right_i_ = 0; + if (!dir_) + warning ("Stem direction set already!"); - stem_bottom_f_ = stem_top_f_ = 0; - flag_i_ = 4; - dir_ =CENTER; - staff_size_i_ = 8; + dir_ = d; - stem_xoffset_f_ =0; + /* + todo + */ } -int -Stem::min_head_i () const +Stem::Stem () { - int m = 1000; - for (int i =0; i < head_l_arr_.size (); i++) - m = m position_i_; - return m; + beams_i_drul_[LEFT] = beams_i_drul_[RIGHT] = -1; + yextent_drul_[DOWN] = yextent_drul_[UP] = 0; + flag_i_ = 2; + dir_ = CENTER; + beam_l_ = 0; } -int -Stem::max_head_i () const +Interval_t +Stem::head_positions () const { - int m = -1000; + /* + 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 ()) + { + return Interval_t (100,-100); + } + + Interval_t r; for (int i =0; i < head_l_arr_.size (); i++) - m = m >? head_l_arr_[i]->position_i_; - return m; - + { + int p = head_l_arr_[i]->position_i_; + r[BIGGER] = r[BIGGER] >? p; + r[SMALLER] = r[SMALLER] 0 && se >= max_head_i ()) || - (se <= min_head_i () && dir_ <0))) - warning ("Weird stem size; check for narrow beams"); + if (dir_ && dir_ * head_positions()[dir_] >= se*dir_) + warning (_ ("weird stem size; check for narrow beams")); - stem_top_f_ = (dir_ < 0) ? max_head_i () : se; - stem_bottom_f_ = (dir_ < 0) ? se : min_head_i (); + + yextent_drul_[dir_] = se; + yextent_drul_[Direction(-dir_)] = head_positions()[-dir_]; } -void -Stem::add (Note_head *n) +int +Stem::type_i () const { - n->add_dependency (this); - head_l_arr_.push (n); + return head_l_arr_[0]->balltype_i_; } - + void -Stem::add (Rest *r) +Stem::add_head (Rhythmic_head *n) { - rest_l_arr_.push (r); - r->add_dependency (this); // ? + n->stem_l_ = this; + n->add_dependency (this); // ? + if (Note_head *nh = dynamic_cast (n)) + { + head_l_arr_.push (nh); + } + else if (Rest *r = dynamic_cast (n)) + { + rest_l_arr_.push (r); + } } bool Stem::invisible_b () const { - - return !head_l_arr_.size () || - head_l_arr_[0]->balltype_i_ <= 0; - + return (!head_l_arr_.size () || + head_l_arr_[0]->balltype_i_ <= 0); } -// if dir_ is set we return fake values. - int -Stem::get_center_distance_from_top () +Stem::get_center_distance (Direction d) const { - if (dir_) - return (dir_ > 0) ? 0 : 1; - - int staff_center = staff_size_i_ / 2; - int max = max_head_i () - staff_center; - return max >? 0; + int staff_center = 0; + int distance = d*(head_positions()[d] - staff_center); + return distance >? 0; } -// if dir_ is set we return fake values. -int -Stem::get_center_distance_from_bottom () +Direction +Stem::get_default_dir () const { - if (dir_) - return (dir_ > 0) ? 1 : 0; + int du = get_center_distance (UP); + int dd = get_center_distance (DOWN); - int staff_center = staff_size_i_ / 2; - int min = staff_center - min_head_i (); - return min >? 0; + if (sign (dd - du)) + return Direction (sign (dd -du)); + + return Direction (int(paper_l ()->get_var ("stem_default_neutral_direction"))); } Direction -Stem::get_default_dir () +Stem::get_dir () const { - if (dir_) - return dir_; - return (get_center_distance_from_top () >= - get_center_distance_from_bottom ()) ? - (Direction)-1 : (Direction)1; + return dir_; } -void -Stem::set_default_dir () -{ - dir_ = get_default_dir (); -} - void Stem::set_default_stemlen () { - if (!dir_) - set_default_dir (); - - // ugh... how about non 5-line staffs? - bool on_ledger_line_b = ((max_head_i () < -2 && dir_ == 1) -// || (min_head_i () > staff_size_i_ && dir_ == -1)); - || (min_head_i () > staff_size_i_ + 3 && dir_ == -1)); - if (on_ledger_line_b) - { - set_stemend (staff_size_i_ / 2 - 1); - } - else + Real length_f = 0.; + SCM scm_len = get_elt_property(length_scm_sym); + if (scm_len != SCM_BOOL_F) { - Real dy = paper ()->interbeam_f (); - Real len = STEMLEN; - // ugh, should get nice *rule* for this - if (abbrev_flag_i_ > 1) - len += (abbrev_flag_i_ - 1)* dy / 2; - set_stemend ((dir_ > 0) ? max_head_i () + len : - min_head_i () - len); + length_f = gh_scm2double (SCM_CDR(scm_len)); } + else + length_f = paper_l ()->get_var ("stem_length0"); + + bool grace_b = get_elt_property (grace_scm_sym) != SCM_BOOL_F; + String type_str = grace_b ? "grace_" : ""; + + Real shorten_f = paper_l ()->get_var (type_str + "forced_stem_shorten0"); + + if (!dir_) + dir_ = get_default_dir (); + + /* + stems in unnatural (forced) direction should be shortened, + according to [Roush & Gourlay] + */ + if (((int)chord_start_f ()) + && (dir_ != get_default_dir ())) + length_f -= shorten_f; + + if (flag_i_ >= 5) + length_f += 2.0; + if (flag_i_ >= 6) + length_f += 1.0; + + set_stemend ((dir_ > 0) ? head_positions()[BIGGER] + length_f: + head_positions()[SMALLER] - length_f); + + if (!grace_b && (dir_ * stem_end_f () < 0)) + set_stemend (0); } +//xxx void Stem::set_default_extents () { if (!stem_length_f ()) set_default_stemlen (); - set_stemend ((dir_< 0) ? - max_head_i ()-stem_length_f (): min_head_i () +stem_length_f ()); - if (dir_ > 0){ - stem_xoffset_f_ = paper ()->note_width ()-paper ()->rule_thickness (); - } - else - stem_xoffset_f_ = 0; } -/* - TODO - - move into note_column.cc - - */ void Stem::set_noteheads () { if (!head_l_arr_.size ()) return; head_l_arr_.sort (Note_head::compare); - if (dir_ < 0) + 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); - head_l_arr_[0]->extremal_i_ = -1; - head_l_arr_.top ()->extremal_i_ = 1; int parity=1; - int lastpos = head_l_arr_[0]->position_i_; - for (int i=1; i < head_l_arr_.size (); i ++) + int lastpos = beginhead->position_i_; + for (int i=1; i < head_l_arr_.size (); i ++) { int dy =abs (lastpos- head_l_arr_[i]->position_i_); - - if (dy <= 1) + + if (dy <= 1) { if (parity) - head_l_arr_[i]->x_dir_ = (stem_xoffset_f_>0) ? UP:DOWN; + head_l_arr_[i]->flip_around_stem (dir_); parity = !parity; } else - parity = 0; + parity = 1; lastpos = head_l_arr_[i]->position_i_; } } @@ -242,120 +249,153 @@ Stem::set_noteheads () void Stem::do_pre_processing () { - if (stem_bottom_f_== stem_top_f_) + if (yextent_drul_[DOWN]== yextent_drul_[UP]) set_default_extents (); set_noteheads (); - flag_i_ = dir_ * abs (flag_i_); - transparent_b_ = invisible_b (); - empty_b_ = invisible_b (); + + if (invisible_b ()) + { + set_elt_property (transparent_scm_sym, SCM_BOOL_T); + } + set_empty (invisible_b ()); + set_spacing_hints (); } + +/** + set stem directions for hinting the optical spacing correction. + + Modifies DIR_LIST property of the Stem's Score_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) + 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); + } + } +} + +Molecule +Stem::flag () const +{ + String style; + SCM st = get_elt_property (style_scm_sym); + if ( st != SCM_BOOL_F) + { + st = SCM_CDR(st); + style = ly_scm2string (st); + } + + char c = (dir_ == UP) ? 'u' : 'd'; + Molecule m = lookup_l ()->afm_find (String ("flags-") + to_str (c) + + 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 { - if (beam_l_ || abs (flag_i_) <= 4) - return Interval (0,0); // TODO! - Paper_def*p= paper (); - Interval r (p->lookup_l ()->flag (flag_i_).dim.x ()); - r+= stem_xoffset_f_; + Interval r (0, 0); + if (beam_l_ || abs (flag_i_) <= 2) + ; // TODO! + else + { + r = flag ().dim_.x (); + r += note_delta_f (); + } return r; } - - Molecule - Stem::abbrev_mol () const - { - Real dy = paper ()->interbeam_f (); - Real w = 1.5 * paper ()->lookup_l ()->ball (2).dim.x ().length (); - Real beamdy = paper ()->interline_f () / 2; - - int beams_i = 0; - Real slope = paper ()->internote_f () / 4; - - if (beam_l_) { - // huh? - slope = 2 * beam_l_->slope; - // ugh, rather calc from Abbreviation_req - beams_i = beams_right_i_ >? beams_left_i_; - } - paper ()->lookup_l ()->beam (slope, 20 PT); - - Molecule beams; - Atom a (paper ()->lookup_l ()->beam (slope, w)); - a.translate (Offset(- w / 2, stem_end_f () - (w / 2 * slope))); - // ugh - if (!beams_i) - a.translate (dy + beamdy - dir_ * dy, Y_AXIS); - else - a.translate (2 * beamdy - dir_ * (beamdy - dy), Y_AXIS); - - for (int i = 0; i < abbrev_flag_i_; i++) - { - Atom b (a); - b.translate (-dir_ * dy * (beams_i + i), Y_AXIS); - beams.add (b); - } - - return beams; - } + + +const Real ANGLE = 20* (2.0*M_PI/360.0); // ugh! Molecule* -Stem::brew_molecule_p () const +Stem::do_brew_molecule_p () const { Molecule *mol_p =new Molecule; - - Real bot = stem_bottom_f_; - Real top = stem_top_f_; - - assert (bot!=top); - - Paper_def *p =paper (); + Drul_array stem_y = yextent_drul_; + Real dy = staff_line_leading_f ()/2.0; - Real dy = p->internote_f (); + 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 (!invisible_b ()) { - Symbol ss =p->lookup_l ()->stem (bot*dy,top*dy); - mol_p->add (Atom (ss)); + 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); } - - if (!beam_l_ &&abs (flag_i_) > 4) + + if (!beam_l_ && abs (flag_i_) > 2) { - Symbol fl = p->lookup_l ()->flag (flag_i_); - Molecule m (fl); - if (flag_i_ < -4) - { - mol_p->add_at_edge (Y_AXIS, DOWN, m); - } - else if (flag_i_ > 4) - { - mol_p->add_at_edge (Y_AXIS, UP, m); - } - else - assert (false); - assert (!abbrev_flag_i_); - } + Molecule fl = flag (); + fl.translate_axis(stem_y[dir_]*dy, Y_AXIS); + mol_p->add_molecule (fl); + } - if (abbrev_flag_i_) - mol_p->add (abbrev_mol ()); - - mol_p->translate (stem_xoffset_f_, X_AXIS); + if (head_l_arr_.size()) + { + mol_p->translate_axis (note_delta_f (), X_AXIS); + } return mol_p; } Real -Stem::hpos_f () const +Stem::note_delta_f () const { - return Item::hpos_f () + stem_xoffset_f_; + Real r=0; + if (head_l_arr_.size()) + { + Interval head_wid(0, head_l_arr_[0]->extent (X_AXIS).length ()); + Real rule_thick(paper_l ()->rule_thickness ()); + Interval stem_wid(-rule_thick/2, rule_thick/2); + if (dir_ == CENTER) + r = head_wid.center (); + else + r = head_wid[dir_] - stem_wid[dir_]; + } + return r; } +Real +Stem::hpos_f () const +{ + return note_delta_f () + Item::hpos_f (); +} void -Stem::do_substitute_dependency (Score_elem*o,Score_elem*n) +Stem::do_substitute_element_pointer (Score_element*o,Score_element*n) { - Item * o_l = o->item (); - Item * n_l = n? n->item () : 0; - head_l_arr_.substitute ((Note_head*)o_l, (Note_head*)n_l); - rest_l_arr_.substitute ((Rest*)o_l, (Rest*)n_l); + 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)) + { + if (b == beam_l_) + beam_l_ = dynamic_cast (n); + } + Staff_symbol_referencer::do_substitute_element_pointer (o,n); }