X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;ds=sidebyside;f=lily%2Fscore-element.cc;h=bdc28e626aff13416e360958c365f685a23cd1e7;hb=ffe548cfbb3c3b37c8969f49b5aba04ef998d080;hp=f3f0322ba4c597cd8ab9022be7ccc69d15560d67;hpb=876ccb4004d92f3b7bd539753f8f9339b7d48490;p=lilypond.git diff --git a/lily/score-element.cc b/lily/score-element.cc index f3f0322ba4..bdc28e626a 100644 --- a/lily/score-element.cc +++ b/lily/score-element.cc @@ -3,12 +3,13 @@ source file of the GNU LilyPond music typesetter - (c) 1997--1999 Han-Wen Nienhuys + (c) 1997--2000 Han-Wen Nienhuys */ #include +#include "group-interface.hh" #include "misc.hh" #include "paper-score.hh" #include "paper-def.hh" @@ -24,48 +25,19 @@ #include "misc.hh" #include "paper-outputter.hh" #include "dimension-cache.hh" - - -Interval -Score_element::dim_cache_callback (Dimension_cache const*c) -{ - Score_element * e =dynamic_cast (c->element_l()); - if(e->dim_cache_[X_AXIS] == c) - return e->do_width (); - else - return e->do_height (); -} - - - -Real -set_alignment_callback (Dimension_cache const *c) -{ - String s ("self-alignment-"); - Axis ax = c->axis (); - s += (ax == X_AXIS) ? "X" : "Y"; - Score_element *elm = dynamic_cast (c->element_l ()); - SCM align (elm->get_elt_property (s)); - if (isdir_b (align)) - { - Direction d = to_dir (align); - Interval ext(elm->extent (ax)); - if (d) - { - return - ext[d]; - } - return - ext.center (); - } - else - return 0.0; -} - +#include "side-position-interface.hh" +#include "item.hh" Score_element::Score_element() { - output_p_ =0; - dim_cache_[X_AXIS]->set_callback (dim_cache_callback); - dim_cache_[Y_AXIS]->set_callback (dim_cache_callback); + dim_cache_[X_AXIS] = new Dimension_cache; + dim_cache_[Y_AXIS] = new Dimension_cache; + dim_cache_[X_AXIS]->elt_l_ = dim_cache_[Y_AXIS]->elt_l_ = this; + + used_b_ = false; + + dim_cache_[X_AXIS]->set_callback (molecule_extent); + dim_cache_[Y_AXIS]->set_callback (molecule_extent); used_b_ = false; pscore_l_=0; lookup_l_ =0; @@ -75,18 +47,24 @@ Score_element::Score_element() element_property_alist_ = SCM_EOL; smobify_self (); + + + set_elt_property ("dependencies", SCM_EOL); + set_elt_property ("interfaces", SCM_EOL); } + Score_element::Score_element (Score_element const&s) - : Graphical_element (s) { + dim_cache_[X_AXIS] = new Dimension_cache (*s.dim_cache_[X_AXIS]); + dim_cache_[Y_AXIS] = new Dimension_cache (*s.dim_cache_[Y_AXIS]); + dim_cache_[X_AXIS]->elt_l_ = dim_cache_[Y_AXIS]->elt_l_ = this; self_scm_ = SCM_EOL; used_b_ = true; original_l_ =(Score_element*) &s; - element_property_alist_ = scm_protect_object (scm_list_copy (s.element_property_alist_)); - dependency_arr_ = s.dependency_arr_; - output_p_ =0; + element_property_alist_ = SCM_EOL; // onstack; + status_i_ = s.status_i_; lookup_l_ = s.lookup_l_; pscore_l_ = s.pscore_l_; @@ -96,21 +74,24 @@ Score_element::Score_element (Score_element const&s) Score_element::~Score_element() { - assert (!output_p_); assert (status_i_ >=0); status_i_ = -1; + + delete dim_cache_[X_AXIS]; + delete dim_cache_[Y_AXIS]; } -Score_element* -Score_element::dependency (int i) const + +Real +Score_element::get_real (String s) const { - return dependency_arr_ [i]; + return gh_scm2double (get_elt_property (s)); } -int -Score_element::dependency_size () const +void +Score_element::set_real (String s, Real r) { - return dependency_arr_.size (); + set_elt_property (s, gh_double2scm (r)); } // should also have one that takes SCM arg. @@ -121,7 +102,7 @@ Score_element::get_elt_property (String nm) const SCM s = scm_assq(sym, element_property_alist_); if (s != SCM_BOOL_F) - return SCM_CDR (s); + return gh_cdr (s); if (pscore_l_) { @@ -159,42 +140,25 @@ Score_element::set_elt_property (String k, SCM v) } Interval -Score_element::do_width() const +Score_element::molecule_extent(Dimension_cache const *c) { - Interval r; - - Molecule*m = output_p_ ? output_p_ : do_brew_molecule_p(); - r = m->extent().x (); - - if (!output_p_) - delete m; - - return r; + Score_element *s = dynamic_cast(c->element_l()); + Molecule m = s->do_brew_molecule(); + return m.extent()[c->axis ()]; } -Interval -Score_element::do_height() const -{ - Interval r; - Molecule*m = output_p_ ? output_p_ : do_brew_molecule_p(); - r = m->extent().y (); - if (!output_p_) - delete m; - - return r; -} void Score_element::print() const { #ifndef NPRINT DEBUG_OUT << classname(this) << "{\n"; + if (flower_dstream && !flower_dstream->silent_b ("Score_element")) ly_display_scm (element_property_alist_); - DEBUG_OUT << "dependencies: " << dependency_size(); + if (original_l_) DEBUG_OUT << "Copy "; - Graphical_element::do_print (); do_print(); DEBUG_OUT << "}\n"; @@ -204,7 +168,7 @@ Score_element::print() const Paper_def* Score_element::paper_l () const { - return pscore_l_->paper_l_; + return pscore_l_ ? pscore_l_->paper_l_ : 0; } Lookup const * @@ -214,7 +178,7 @@ Score_element::lookup_l () const { Score_element * urg = (Score_element*)this; SCM sz = urg->remove_elt_property ("fontsize"); - int i = (sz != SCM_UNDEFINED) + int i = (gh_number_p (sz)) ? gh_scm2int (sz) : 0; @@ -231,15 +195,23 @@ Score_element::add_processing() return; status_i_ ++; - if (get_elt_property ("self-alignment-X") != SCM_UNDEFINED) +#if 0 + /* + UGH. UGH. UGH. + */ + if (get_elt_property ("self-alignment-X") != SCM_UNDEFINED + && !dim_cache_[X_AXIS]->off_callback_l_) { - dim_cache_[X_AXIS]->set_offset_callback (set_alignment_callback); + dim_cache_[X_AXIS]->off_callbacks_.push (Side_position_interface::aligned_on_self); } - if (get_elt_property ("self-alignment-Y") != SCM_UNDEFINED) + if (get_elt_property ("self-alignment-Y") != SCM_UNDEFINED + && !dim_cache_[X_AXIS]->off_callback_l_) + { - dim_cache_[Y_AXIS]->set_offset_callback (set_alignment_callback); + dim_cache_[Y_AXIS]->set_offset_callback (Side_position_interface::aligned_on_self); } +#endif do_add_processing(); } @@ -256,15 +228,16 @@ Score_element::calculate_dependencies (int final, int busy, assert (status_i_!= busy); status_i_= busy; - for (int i=0; i < dependency_arr_.size(); i++) - dependency_arr_[i]->calculate_dependencies (final, busy, funcptr); + Link_array dependency_arr = + Group_interface__extract_elements (this, (Score_element*)0, "dependencies"); + + for (int i=0; i < dependency_arr.size(); i++) + dependency_arr[i]->calculate_dependencies (final, busy, funcptr); Link_array extra (get_extra_dependencies()); for (int i=0; i < extra.size(); i++) extra[i]->calculate_dependencies (final, busy, funcptr); - invalidate_cache (X_AXIS); - invalidate_cache (Y_AXIS); (this->*funcptr)(); status_i_= final; } @@ -272,14 +245,11 @@ Score_element::calculate_dependencies (int final, int busy, void Score_element::output_processing () { - if (get_elt_property ("transparent") != SCM_UNDEFINED) + if (to_boolean (get_elt_property ("transparent"))) return; - // we're being silly here. - if (output_p_) - delete output_p_; - output_p_ = do_brew_molecule_p (); + Molecule m (do_brew_molecule ()); Offset o (relative_coordinate (0, X_AXIS), relative_coordinate (0, Y_AXIS)); SCM s = get_elt_property ("extra-offset"); @@ -290,12 +260,7 @@ Score_element::output_processing () o[Y_AXIS] += il * gh_scm2double (gh_cdr (s)); } - pscore_l_->outputter_l_->output_molecule (output_p_, - o, - classname(this)); - - delete output_p_; - output_p_ =0; + pscore_l_->outputter_l_->output_molecule (m.expr_, o, classname(this)); } /* @@ -306,7 +271,6 @@ Score_element::output_processing () void Score_element::do_break_processing() { - handle_broken_dependencies(); } void @@ -335,17 +299,23 @@ Score_element::do_add_processing() { } -void -Score_element::do_substitute_element_pointer (Score_element*,Score_element*) -{ -} -Molecule* -Score_element::do_brew_molecule_p() const +Molecule +Score_element::do_brew_molecule() const { - Molecule a (lookup_l ()->fill (Box (Interval (0,0), Interval (0,0)))); - return new Molecule (a); + SCM glyph = get_elt_property ("glyph"); + if (gh_string_p (glyph)) + { + return lookup_l ()->afm_find (String (ly_scm2string (glyph))); + + } + else + { + Interval emp; + emp.set_empty (); + return lookup_l ()->fill (Box (emp,emp)); + } } @@ -360,75 +330,109 @@ Score_element::add_dependency (Score_element*e) { if (e) { - dependency_arr_.push (e); - e->used_b_ = true; + Group_interface gi (this, "dependencies"); + gi.add_element (e); } else programming_error ("Null dependency added"); } -void -Score_element::substitute_dependency (Score_element* old, Score_element* new_l) -{ - do_substitute_element_pointer (old,new_l); - old->do_substitute_element_pointer (this, 0); -} + /** - Do break substitution, and return new value. - */ -SCM -Score_element::handle_broken_smobs (SCM s, Line_of_score * line) + Do break substitution in S, using CRITERION. Return new value. + CRITERION is either a SMOB pointer to the desired line, or a number + representing the break direction. Do not modify SRC. +*/ +SCM +Score_element::handle_broken_smobs (SCM src, SCM criterion) { - if (SMOB_IS_TYPE_B (Score_element, s)) + again: + + + Score_element *sc = unsmob_element (src); + if (sc) { - Score_element *sc = SMOB_TO_TYPE (Score_element, s); - Score_element * br =0; - if (sc->line_l () != line) + if (criterion == SCM_UNDEFINED) + return SCM_UNDEFINED; + else if (gh_number_p (criterion)) { - br = sc->find_broken_piece (line); + Item * i = dynamic_cast (sc); + Direction d = to_dir (criterion); + if (i && i->break_status_dir () != d) + { + Item *br = i->find_broken_piece (d); + return (br) ? br->self_scm_ : SCM_UNDEFINED; + } + } + else + { + Score_element * ln = unsmob_element ( criterion); + Line_of_score * line = dynamic_cast (ln); + Score_element * br =0; + Line_of_score * dep_line = sc->line_l (); + if (dep_line != line) + { + br = sc->find_broken_piece (line); + return (br) ? br->self_scm_ : SCM_UNDEFINED; + } + if (!dep_line) + return SCM_UNDEFINED; } - - if (br) - return br->self_scm_; } - else if (gh_pair_p (s)) + else if (gh_pair_p (src)) { /* UGH! breaks on circular lists. - */ - gh_set_car_x (s, handle_broken_smobs (gh_car (s), line)); - gh_set_cdr_x (s, handle_broken_smobs (gh_cdr (s), line)); + */ + SCM car = handle_broken_smobs (gh_car (src), criterion); + SCM cdr = gh_cdr (src); + + if (car == SCM_UNDEFINED + && (gh_pair_p (cdr) || cdr == SCM_EOL)) + { + /* + This is tail-recursion, ie. + + return handle_broken_smobs (cdr, criterion); + + We don't want to rely on the compiler to do this. */ + src = cdr; + goto again; + } + + return gh_cons (car, handle_broken_smobs (cdr, criterion)); } - return s; + else + return src; + + return src; } void Score_element::handle_broken_dependencies() { - Line_of_score *line = line_l(); - if (!line) + Spanner * s= dynamic_cast (this); + if (original_l_ && s) return; - element_property_alist_ = handle_broken_smobs (element_property_alist_, line); - - Link_array new_deps; - - for (int i=0; i < dependency_size(); i++) + if (s) { - Score_element * elt = dependency (i); - if (elt->line_l() != line) + for (int i = 0; i< s->broken_into_l_arr_ .size (); i++) { - Score_element * broken = elt->find_broken_piece (line); - substitute_dependency (elt, broken); - elt = broken ; + Score_element * sc = s->broken_into_l_arr_[i]; + Line_of_score * l = sc->line_l (); + s->broken_into_l_arr_[i]->element_property_alist_ = + handle_broken_smobs (element_property_alist_, + l ? l->self_scm_ : SCM_UNDEFINED); } - if (elt) - new_deps.push (elt); } - dependency_arr_ = new_deps; + Line_of_score *line = line_l(); + element_property_alist_ + = handle_broken_smobs (element_property_alist_, + line ? line->self_scm_ : SCM_UNDEFINED); } @@ -438,98 +442,181 @@ Score_element::handle_broken_dependencies() void Score_element::handle_prebroken_dependencies() { - element_property_alist_ - = handle_broken_smobs (element_property_alist_, line_l ()); - - Link_array old_arr, new_arr; - - for (int i=0; i < dependency_size(); i++) + if (Item*i =dynamic_cast (this)) { - Score_element * elt = dependency (i); - Item *it_l = dynamic_cast (elt); - if (it_l && it_l->broken_original_b ()) - if (Item *me = dynamic_cast (this) ) - { - Score_element *new_l = it_l->find_broken_piece (me->break_status_dir ()); - if (new_l != elt) - { - new_arr.push (new_l); - old_arr.push (elt); - } - } - else - { - Direction d = LEFT; - do { - old_arr.push (0); - new_arr.push (it_l->find_broken_piece (d)); - } while (flip(&d)!= LEFT); - } + if (original_l_) + { + element_property_alist_ + = handle_broken_smobs (original_l_->element_property_alist_, + gh_int2scm (i->break_status_dir ())); + } } - - for (int i=0; i < old_arr.size(); i++) - if (old_arr[i]) - substitute_dependency (old_arr[i], new_arr[i]); } -void -Score_element::handle_prebroken_dependents() +Link_array +Score_element::get_extra_dependencies() const +{ + Link_array empty; + return empty; +} + +bool +Score_element::linked_b() const { + return used_b_; } void -Score_element::handle_broken_dependents() +Score_element::do_print () const { } +Score_element* +Score_element::find_broken_piece (Line_of_score*) const +{ + return 0; +} +void +Score_element::translate_axis (Real y, Axis a) +{ + dim_cache_[a]->translate (y); +} -Link_array -Score_element::get_extra_dependencies() const +Real +Score_element::relative_coordinate (Score_element const*e, Axis a) const { - Link_array empty; - return empty; + return dim_cache_[a]->relative_coordinate (e ? e->dim_cache_[a] : 0); } -bool -Score_element::linked_b() const +Score_element * +Score_element::common_refpoint (Score_element const* s, Axis a) const { - return used_b_; + Dimension_cache *dim = dim_cache_[a]->common_refpoint (s->dim_cache_[a]); + return dim ? dim->element_l () : 0; } void -Score_element::do_print () const +Score_element::set_empty (Axis a) { + dim_cache_[a]->callback_l_ =0; } +bool +Score_element::empty_b (Axis a)const +{ + return !dim_cache_[a]->callback_l_; +} + +Interval +Score_element::extent (Axis a) const +{ + Dimension_cache const * d = dim_cache_[a]; + + return d->get_dim (); +} Score_element* -Score_element::find_broken_piece (Line_of_score*) const +Score_element::parent_l (Axis a) const { - return 0; + Dimension_cache*d= dim_cache_[a]->parent_l_; + return d ? d->elt_l_ : 0; +} + +Score_element * +Score_element::common_refpoint (Link_array gs, Axis a) const +{ + Dimension_cache * common = dim_cache_[a]; + for (int i=0; i < gs.size (); i++) + { + common = common->common_refpoint (gs[i]->dim_cache_[a]); + } + + return common->element_l (); +} + +char const * +Score_element::name () const +{ + return classname (this); +} + + +void +Score_element::set_parent (Score_element *g, Axis a) +{ + dim_cache_[a]->parent_l_ = g ? g->dim_cache_[a]: 0; +} + +void +Score_element::fixup_refpoint () +{ + for (int a = X_AXIS; a < NO_AXES; a ++) + { + Axis ax = (Axis)a; + Score_element * par = parent_l (ax); + + if (!par) + continue; + + if (par->line_l () != line_l ()) + { + Score_element * newpar = par->find_broken_piece (line_l ()); + set_parent (newpar, ax); + } + + if (Item * i = dynamic_cast (this)) + { + Item *pari = dynamic_cast (par); + + if (pari && i) + { + Direction my_dir = i->break_status_dir () ; + if (my_dir!= pari->break_status_dir()) + { + Item *newpar = pari->find_broken_piece (my_dir); + set_parent (newpar, ax); + } + } + } + } } + + +/**************************************************** + SMOB funcs + ****************************************************/ + + +#include "ly-smobs.icc" + +IMPLEMENT_UNSMOB(Score_element, element); +IMPLEMENT_SMOBS(Score_element); SCM Score_element::mark_smob (SCM ses) { - void * mp = (void*) SCM_CDR(ses); - Score_element * s = (Score_element*) mp; - - assert (s->self_scm_ == ses); + Score_element * s = SMOB_TO_TYPE (Score_element, ses); + if (s->self_scm_ != ses) + { + programming_error ("SMOB marking gone awry"); + return SCM_EOL; + } return s->element_property_alist_; } - int Score_element::print_smob (SCM s, SCM port, scm_print_state *) { - Score_element *sc = (Score_element *) SCM_CDR (s); + Score_element *sc = (Score_element *) gh_cdr (s); scm_puts ("#name (), port); - scm_puts ("properties = ", port); - scm_display (sc->element_property_alist_, port); + + /* + don't try to print properties, that is too much hassle. + */ scm_puts (" >", port); return 1; } @@ -537,14 +624,69 @@ Score_element::print_smob (SCM s, SCM port, scm_print_state *) void Score_element::do_smobify_self () { - scm_unprotect_object (element_property_alist_); // ugh } -#include "ly-smobs.icc" -IMPLEMENT_SMOBS(Score_element); SCM Score_element::equal_p (SCM a, SCM b) { - return SCM_CDR(a) == SCM_CDR(b) ? SCM_BOOL_T : SCM_BOOL_F; + return gh_cdr(a) == gh_cdr(b) ? SCM_BOOL_T : SCM_BOOL_F; +} + + +SCM +Score_element::ly_set_elt_property (SCM elt, SCM sym, SCM val) +{ + Score_element * sc = unsmob_element (elt); + + if (!gh_symbol_p (sym)) + { + error ("Not a symbol"); + ly_display_scm (sym); + return SCM_UNDEFINED; + } + + if (sc) + { + sc->element_property_alist_ = scm_assoc_set_x (sc->element_property_alist_, sym, val); + } + else + { + error ("Not a score element"); + ly_display_scm (elt); + } + + return SCM_UNDEFINED; +} + + +SCM +Score_element::ly_get_elt_property (SCM elt, SCM sym) +{ + Score_element * sc = unsmob_element (elt); + + if (sc) + { + SCM s = scm_assq(sym, sc->element_property_alist_); + + if (s != SCM_BOOL_F) + return gh_cdr (s); + else + return SCM_UNDEFINED; + } + else + { + error ("Not a score element"); + ly_display_scm (elt); + } + return SCM_UNDEFINED; +} + + +static void +init_functions () +{ + scm_make_gsubr ("ly-get-elt-property", 2, 0, 0, (SCM(*)(...))Score_element::ly_get_elt_property); + scm_make_gsubr ("ly-set-elt-property", 3, 0, 0, (SCM(*)(...))Score_element::ly_set_elt_property); } +ADD_SCM_INIT_FUNC(scoreelt, init_functions);