X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=lily%2Frest-collision.cc;h=490f17fbc6c90ac3a4f1a2ec716bbb8ba62dbe3d;hb=3e07faa176c300184b2e41d0dcfa779360519359;hp=2a38b27d2ba391b1bf294eb6ba68d5f0609bcfc9;hpb=40c8e12b8afa8e9c7e44cf6ea3b724295884091b;p=lilypond.git diff --git a/lily/rest-collision.cc b/lily/rest-collision.cc index 2a38b27d2b..490f17fbc6 100644 --- a/lily/rest-collision.cc +++ b/lily/rest-collision.cc @@ -3,12 +3,12 @@ source file of the GNU LilyPond music typesetter - (c) 1997--2000 Han-Wen Nienhuys + (c) 1997--2004 Han-Wen Nienhuys */ #include // ceil. -#include "debug.hh" +#include "warn.hh" #include "rest-collision.hh" #include "note-column.hh" #include "stem.hh" @@ -18,91 +18,91 @@ #include "group-interface.hh" #include "staff-symbol-referencer.hh" #include "duration.hh" +#include "directional-element-interface.hh" -Real -Rest_collision::force_shift_callback (Score_element const*them, Axis a) +MAKE_SCHEME_CALLBACK (Rest_collision,force_shift_callback,2); +SCM +Rest_collision::force_shift_callback (SCM element_smob, SCM axis) { + Grob *them = unsmob_grob (element_smob); + Axis a = (Axis) ly_scm2int (axis); assert (a == Y_AXIS); - Score_element * rc = unsmob_element (them->get_elt_pointer ("rest-collision")); + Grob * rc = unsmob_grob (them->get_property ("rest-collision")); - if (rc) + if (rc && !to_boolean (rc->get_property ("positioning-done"))) { - /* - Done: destruct pointers, so we do the shift only once. - */ - SCM elts = rc->get_elt_pointer ("elements"); - rc->set_elt_pointer ("elements", SCM_EOL); + rc->set_property ("positioning-done", SCM_BOOL_T); - do_shift (rc, elts); + do_shift (rc); } - return 0.0; + return scm_make_real (0.0); } -void -Rest_collision::add_column (Note_column *p) -{ - elt_l_->add_dependency (p); - Pointer_group_interface gi (elt_l_); - gi.add_element (p); - p->add_offset_callback (&Rest_collision::force_shift_callback, Y_AXIS); - p->set_elt_pointer ("rest-collision", elt_l_->self_scm_); -} -/* - these 3 have to go, because they're unnecessary complications. - */ -static Duration -to_duration (int type, int dots) +void +Rest_collision::add_column (Grob*me,Grob *p) { - Duration d; - d.durlog_i_ = type; - d.dots_i_ = dots; - return d; -} + me->add_dependency (p); + Pointer_group_interface::add_grob (me, ly_symbol2scm ("elements"), p); -/* - UGH + /* + only add callback for the rests, since we don't move anything + else. - elt_l_ should be "duration" independent - */ -static Moment -rhythmic_head2mom (Rhythmic_head* r) -{ - return to_duration (r->balltype_i (), r->dot_count ()).length_mom (); + (not?) + */ + p->add_offset_callback (Rest_collision::force_shift_callback_proc, Y_AXIS); + p->set_property ("rest-collision", me->self_scm ()); } + /* - ugh + Combination of dot-count and duration-log. */ -static Rhythmic_head* -col2rhythmic_head (Score_element* c) +static SCM +head_characteristic (Grob * col) { - return dynamic_cast (unsmob_element (c->get_elt_pointer ("rest"))); -} + Grob * s = unsmob_grob (col->get_property ("rest")); + if (!s) + return SCM_BOOL_F; + else + return scm_cons (s->get_property ("duration-log"), + scm_int2num (Rhythmic_head::dot_count (s))); +} /* - TODO: fixme, fucks up if called twice on the same set of rests. + TODO: look at horizontal-shift to determine ordering between rests + for more than two voices. + */ SCM -Rest_collision::do_shift (Score_element *me, SCM elts) +Rest_collision::do_shift (Grob *me) { - /* - ugh. -> score elt type - */ - Link_array rests; - Link_array notes; + SCM elts = me->get_property ("elements"); - for (SCM s = elts; gh_pair_p (s); s = gh_cdr (s)) + Link_array rests; + Link_array notes; + + for (SCM s = elts; ly_c_pair_p (s); s = ly_cdr (s)) { - Score_element * e = unsmob_element (gh_car (s)); - if (e && unsmob_element (e->get_elt_pointer ("rest"))) - rests.push (dynamic_cast (e)); + Grob * e = unsmob_grob (ly_car (s)); + if (unsmob_grob (e->get_property ("rest"))) + { + /* + Ignore rests under beam. + */ + Grob* st = unsmob_grob (e->get_property ("stem")); + if (st && unsmob_grob (st->get_property ("beam"))) + continue; + + rests.push (e); + } else - notes.push (dynamic_cast (e)); + notes.push (e); } @@ -111,108 +111,124 @@ Rest_collision::do_shift (Score_element *me, SCM elts) [todo] * decide not to print rest if too crowded? - - * ignore rests under beams. */ - // no rests to collide - if (!rests.size()) - return SCM_UNDEFINED; + /* + no partners to collide with + */ + if (rests.size () + notes.size () < 2) + return SCM_UNSPECIFIED; - // no partners to collide with - if (rests.size() + notes.size () < 2) - return SCM_UNDEFINED; - // meisjes met meisjes - if (!notes.size()) + Real staff_space = Staff_symbol_referencer::staff_space (me); + /* + only rests + */ + if (!notes.size ()) { - Moment m = rhythmic_head2mom (col2rhythmic_head (rests[0])); - int i = 1; - for (; i < rests.size (); i++) - { - Moment me = rhythmic_head2mom (col2rhythmic_head (rests[i])); - if (me != m) - break; - } /* - If all durations are the same, we'll check if there are more - rests than maximum-rest-count. - Otherwise (different durations), we'll try to display them all - (urg: all 3 of them, currently). + This is incomplete: in case of an uneven number of rests, the + center one should be centered on the staff. */ - int display_count; - SCM s = me->get_elt_property ("maximum-rest-count"); - if (i == rests.size () - && gh_number_p (s) && gh_scm2int (s) < rests.size ()) + Drul_array< Link_array > ordered_rests; + for (int i= 0; i < rests.size (); i++) { - display_count = gh_scm2int (s); - for (; i > display_count; i--) - col2rhythmic_head (rests[i-1]) - ->set_elt_property ("molecule-callback", SCM_BOOL_T); + Grob * r = Note_column::get_rest (rests[i]); + + Direction d = get_grob_direction (r); + if (d) + { + ordered_rests[d].push (rests[i]); + } + else + rests[d]->warning (_("rest direction not set. Cannot resolve collision.")); } - else - display_count = rests.size (); + + Direction d = LEFT; + do { + ordered_rests[d].sort (Note_column::shift_compare); + } while (flip (&d) != LEFT); - /* - UGH. Should get dims from table. Should have minimum dist. - */ - int dy = display_count > 2 ? 6 : 4; - if (display_count > 1) + if (ordered_rests[UP].size () + ordered_rests[DOWN].size () < 2) + return SCM_UNSPECIFIED; + + Grob *common = common_refpoint_of_array (ordered_rests[DOWN], me, Y_AXIS); + common = common_refpoint_of_array (ordered_rests[UP], common, Y_AXIS); + + Real diff = + (ordered_rests[DOWN].top ()->extent (common, Y_AXIS)[UP] + - ordered_rests[UP].top ()->extent (common, Y_AXIS)[DOWN]) /staff_space; + + if (diff > 0) { - rests[0]->translate_rests (dy); - rests[1]->translate_rests (-dy); + int amount_down = (int) ceil (diff / 2); + diff -= amount_down; + Note_column::translate_rests (ordered_rests[DOWN].top (), + -2 * amount_down); + if (diff > 0) + Note_column::translate_rests (ordered_rests[UP].top (), + 2 * int (ceil (diff))); } + + do { + for (int i = ordered_rests[d].size () -1; i-- > 0;) + { + Real last_y = ordered_rests[d][i+1]->extent (common, Y_AXIS)[d]; + Real y = ordered_rests[d][i]->extent (common, Y_AXIS)[-d]; + + Real diff = d * ((last_y - y) /staff_space); + if (diff > 0) + Note_column::translate_rests (ordered_rests[d][i],d * (int) ceil (diff) * 2); + } + } while (flip (&d) != LEFT); } - // meisjes met jongetjes else { + /* + Rests and notes. + */ if (rests.size () > 1) { - warning (_("too many colliding rests")); + warning (_ ("too many colliding rests")); } - if (notes.size () > 1) - { - warning (_("too many notes for rest collision")); - } - Note_column * rcol = rests[0]; - - // try to be opposite of noteheads. - Direction dir = - notes[0]->dir(); - - Interval restdim = rcol->rest_dim (); - if (restdim.empty_b ()) - return SCM_UNDEFINED; + Grob * rcol = 0; + Direction dir = CENTER; + + for (int i= rests.size (); !rcol && i--;) + if (Note_column::dir (rests[i])) + { + dir = Note_column::dir (rests[i]); + rcol = rests[i]; + } + + if (!rcol) + return SCM_UNSPECIFIED; - // staff ref'd? - Real staff_space = me->paper_l()->get_var ("interline"); - - /* FIXME - staff_space = rcol->rests[0]->staff_space (); - */ - Real minimum_dist = gh_scm2double (me->get_elt_property ("minimum-distance")) * staff_space; + Grob *common = common_refpoint_of_array (notes, rcol, Y_AXIS); - /* - assumption: ref points are the same. - */ + Interval restdim = rcol->extent (common, Y_AXIS); + if (restdim.is_empty ()) + return SCM_UNSPECIFIED; + + Real staff_space = Staff_symbol_referencer::staff_space (rcol); + Real minimum_dist = robust_scm2double (me->get_property ("minimum-distance"), 1.0) * staff_space; + Interval notedim; - for (int i = 0; i < notes.size(); i++) + for (int i = 0; i < notes.size (); i++) { - notedim.unite (notes[i]->extent (Y_AXIS)); + notedim.unite (notes[i]->extent (common, Y_AXIS)); } - Interval inter (notedim); - inter.intersect (restdim); - Real dist = minimum_dist + dir * (notedim[dir] - restdim[-dir]) >? 0; - - // FIXME - //int stafflines = 5; // rcol->rests[0]->line_count; - int stafflines = Staff_symbol_referencer_interface (me).line_count (); - // hurg? - stafflines = stafflines != 0 ? stafflines : 5; + int stafflines = Staff_symbol_referencer::line_count (me); + if (!stafflines) + { + programming_error ("No staff line count ? "); + stafflines =5; + } // move discretely by half spaces. int discrete_dist = int (ceil (dist / (0.5 *staff_space))); @@ -220,21 +236,15 @@ Rest_collision::do_shift (Score_element *me, SCM elts) // move by whole spaces inside the staff. if (discrete_dist < stafflines+1) discrete_dist = int (ceil (discrete_dist / 2.0)* 2.0); - - rcol->translate_rests (dir * discrete_dist); + + Note_column::translate_rests (rcol,dir * discrete_dist); } - return SCM_UNDEFINED; + return SCM_UNSPECIFIED; } -void -Rest_collision::set_interface () -{ - elt_l_->set_extent_callback (0, X_AXIS); - elt_l_->set_extent_callback (0, Y_AXIS); - elt_l_->set_elt_pointer ("elements", SCM_EOL); -} -Rest_collision::Rest_collision (Score_element* c) -{ - elt_l_ = c; -} +ADD_INTERFACE (Rest_collision,"rest-collision-interface", + "Move around ordinary rests (not multi-measure-rests) to avoid " +"conflicts.", + "minimum-distance positioning-done elements"); +