From 6284382ad57e1e57339acdff17b773cb56da90d8 Mon Sep 17 00:00:00 2001 From: Han-Wen Nienhuys Date: Sun, 25 Mar 2007 14:45:32 -0300 Subject: [PATCH] Cleanup and simplify dot column formatting logic. Use skylines to determine horizontal dot position for given configuration. Remove hack where stem adds itself to dot column support. Add dot-formatting-problem.{cc,hh} --- lily/dot-column.cc | 132 ++++++++++++++++--------- lily/dot-configuration.cc | 19 +++- lily/dot-formatting-problem.cc | 35 +++++++ lily/include/dot-configuration.hh | 12 ++- lily/include/dot-formatting-problem.hh | 22 +++++ lily/stem.cc | 35 ------- scm/define-grobs.scm | 1 - scm/safe-lily.scm | 1 - 8 files changed, 167 insertions(+), 90 deletions(-) create mode 100644 lily/dot-formatting-problem.cc create mode 100644 lily/include/dot-formatting-problem.hh diff --git a/lily/dot-column.cc b/lily/dot-column.cc index 6b0ebaeac1..aed84ddf9d 100644 --- a/lily/dot-column.cc +++ b/lily/dot-column.cc @@ -11,6 +11,8 @@ #include #include #include +#include + using namespace std; #include "dots.hh" @@ -24,38 +26,9 @@ using namespace std; #include "grob.hh" #include "pointer-group-interface.hh" #include "dot-configuration.hh" - -/* - TODO: let Dot_column communicate with stem via Note_column. -*/ - -MAKE_SCHEME_CALLBACK (Dot_column, side_position, 1); -SCM -Dot_column::side_position (SCM smob) -{ - Grob *me = unsmob_grob (smob); - extract_grob_set (me, "dots", dots); - - for (vsize i = 0; i < dots.size (); i++) - { - Grob *head = dots[i]->get_parent (Y_AXIS); - Grob *stem = head ? unsmob_grob (head->get_object ("stem")) : 0; - if (stem - && !Stem::get_beam (stem) - && Stem::duration_log (stem) > 2 - && !Stem::is_invisible (stem)) - { - /* - trigger stem end & direction calculation. - - This will add the stem to the support if a flag collision happens. - */ - stem->get_property ("stem-end-position"); - } - } - - return Side_position_interface::x_aligned_side (smob, SCM_EOL); -} +#include "note-head.hh" +#include "skyline.hh" +#include "dot-formatting-problem.hh" MAKE_SCHEME_CALLBACK (Dot_column, calc_positioning_done, 1); SCM @@ -68,33 +41,82 @@ Dot_column::calc_positioning_done (SCM smob) vector dots = extract_grob_array (me, "dots"); + vector main_heads; + + Grob *commonx = me; { /* Trigger note collision resolution first, since that may kill off dots when merging. */ - Grob *c = 0; - for (vsize i = dots.size (); i--;) - { - Grob *n = dots[i]->get_parent (Y_AXIS); - if (c) - c = n->common_refpoint (c, X_AXIS); - else - c = n; - } - for (vsize i = dots.size (); i--;) + for (vsize i = 0; i < dots.size (); i++) { Grob *n = dots[i]->get_parent (Y_AXIS); - n->relative_coordinate (c, X_AXIS); + commonx = n->common_refpoint (commonx, X_AXIS); + + if (Grob *stem = unsmob_grob (n->get_object("stem"))) + { + commonx = stem->common_refpoint (commonx, X_AXIS); + + if (Stem::first_head (stem) == n) + main_heads.push_back (n); + } } } - + + vector boxes; + set stems; + + extract_grob_set(me, "side-support-elements", support); + + Interval base_x; + for (vsize i = 0; i < main_heads.size (); i++) + base_x.unite (main_heads[i]->extent (commonx, X_AXIS)); + + for (vsize i = 0; i < support.size (); i++) + { + if (Note_head::has_interface (support[i])) + { + Interval y(-1, 1); + y += Staff_symbol_referencer::get_position (support[i]); + + Box b (support[i]->extent (commonx, X_AXIS), y); + boxes.push_back (b); + + if (Grob *s = unsmob_grob (support[i]->get_object ("stem"))) + stems.insert (s); + } + else + programming_error ("unknown grob in dot col support"); + } + + + for (set::const_iterator i(stems.begin()); + i != stems.end (); i++) + { + Grob *stem = (*i); + Stencil flag = Stem::flag (stem); + if (!flag.is_empty ()) + { + Interval y = flag.extent (Y_AXIS) + * (2 / Staff_symbol_referencer::staff_space (stem)) + + Stem::stem_end_position (stem); + + Interval x = stem->relative_coordinate (commonx, X_AXIS) + + flag.extent (X_AXIS); + + boxes.push_back (Box (x,y)); + } + } + vector_sort (dots, position_less); for (vsize i = dots.size (); i--;) if (!dots[i]->is_live ()) dots.erase (dots.begin () + i); - Dot_configuration cfg; - for (vsize i = 0;i < dots.size (); i++) + Dot_formatting_problem problem (boxes, base_x); + + Dot_configuration cfg (problem); + for (vsize i = 0; i < dots.size (); i++) { Dot_position dp; dp.dot_ = dots[i]; @@ -104,7 +126,13 @@ Dot_column::calc_positioning_done (SCM smob) { Grob *stem = unsmob_grob (note->get_object ("stem")); if (stem) - dp.extremal_head_ = Stem::first_head (stem) == note; + { + dp.extremal_head_ = Stem::first_head (stem) == note; + + + } + + dp.x_extent_ = note->extent (commonx, X_AXIS); } int p = Staff_symbol_referencer::get_rounded_position (dp.dot_); @@ -113,7 +141,6 @@ Dot_column::calc_positioning_done (SCM smob) offset callback but adding a dot overwrites Y-offset. */ p += (int) robust_scm2double (dp.dot_->get_property ("staff-position"), 0.0); dp.pos_ = p; - if (dp.extremal_head_) dp.dir_ = to_dir (dp.dot_->get_property ("direction")); @@ -123,6 +150,8 @@ Dot_column::calc_positioning_done (SCM smob) cfg.remove_collision (p); } + problem.register_configuration (cfg); + for (Dot_configuration::const_iterator i (cfg.begin ()); i != cfg.end (); i++) { @@ -131,6 +160,10 @@ Dot_column::calc_positioning_done (SCM smob) */ Staff_symbol_referencer::set_position (i->second.dot_, i->first); } + + + me->translate_axis (cfg.x_offset () - me->relative_coordinate (commonx, X_AXIS), + X_AXIS); return SCM_BOOL_T; } @@ -144,6 +177,7 @@ Dot_column::add_head (Grob *me, Grob *rh) Pointer_group_interface::add_grob (me, ly_symbol2scm ("dots"), d); d->set_property ("Y-offset", Grob::x_parent_positioning_proc); + d->set_property ("X-offset", Grob::x_parent_positioning_proc); Axis_group_interface::add_element (me, d); } } @@ -151,7 +185,7 @@ Dot_column::add_head (Grob *me, Grob *rh) ADD_INTERFACE (Dot_column, "Groups dot objects so they form a column, and position dots so they do not " - "clash with staff lines ", + "clash with staff lines. ", /* properties */ "dots " diff --git a/lily/dot-configuration.cc b/lily/dot-configuration.cc index 7676c14dd4..c4cf852d1a 100644 --- a/lily/dot-configuration.cc +++ b/lily/dot-configuration.cc @@ -9,6 +9,7 @@ */ #include "dot-configuration.hh" +#include "dot-formatting-problem.hh" #include "staff-symbol-referencer.hh" @@ -60,7 +61,7 @@ Dot_configuration::print () const Dot_configuration Dot_configuration::shifted (int k, Direction d) const { - Dot_configuration new_cfg; + Dot_configuration new_cfg (*problem_); int offset = 0; if (d > 0) @@ -141,3 +142,19 @@ Dot_configuration::remove_collision (int p) *this = (b_up < b_down) ? cfg_up : cfg_down; } } + +Dot_configuration::Dot_configuration (Dot_formatting_problem const &problem) +{ + problem_ = &problem; +} + +Real +Dot_configuration::x_offset () const +{ + Real off = 0.0; + for (Dot_configuration::const_iterator i (begin ()); + i != end (); i++) + off = max (off, problem_->head_skyline_.height ((*i).first)); + + return off; +} diff --git a/lily/dot-formatting-problem.cc b/lily/dot-formatting-problem.cc new file mode 100644 index 0000000000..81dc908d0a --- /dev/null +++ b/lily/dot-formatting-problem.cc @@ -0,0 +1,35 @@ + +#include "dot-formatting-problem.hh" +#include "dot-configuration.hh" +#include "skyline.hh" + +Dot_formatting_problem::~Dot_formatting_problem() +{ + delete best_; +} + +void +Dot_formatting_problem::register_configuration (Dot_configuration const &src) +{ + int b = src.badness (); + if (b < score_) + { + delete best_; + best_ = new Dot_configuration (src); + } +} + +Dot_configuration * +Dot_formatting_problem::best () const +{ + return best_; +} + + + +Dot_formatting_problem::Dot_formatting_problem (vector const &boxes, Interval base_x) + : head_skyline_ (boxes, 0.0, Y_AXIS, RIGHT) +{ + best_ = 0; + head_skyline_.set_minimum_height (base_x[RIGHT]); +} diff --git a/lily/include/dot-configuration.hh b/lily/include/dot-configuration.hh index 1eed8f3c35..a7ab09fab3 100644 --- a/lily/include/dot-configuration.hh +++ b/lily/include/dot-configuration.hh @@ -13,6 +13,7 @@ #include "lily-proto.hh" #include "direction.hh" +#include "box.hh" #include @@ -21,8 +22,10 @@ struct Dot_position int pos_; Direction dir_; Grob *dot_; + Box dot_extents_; bool extremal_head_; - + Interval x_extent_; + Dot_position () { dot_ = 0; @@ -34,11 +37,14 @@ struct Dot_position struct Dot_configuration : public map { + Dot_formatting_problem const *problem_; + + Dot_configuration (Dot_formatting_problem const &); + Real x_offset () const; int badness () const; void print () const; Dot_configuration shifted (int k, Direction d) const; - - void remove_collision (int p); + void remove_collision (int p); }; #endif diff --git a/lily/include/dot-formatting-problem.hh b/lily/include/dot-formatting-problem.hh new file mode 100644 index 0000000000..da248545bb --- /dev/null +++ b/lily/include/dot-formatting-problem.hh @@ -0,0 +1,22 @@ +#ifndef DOT_FORMATTING_PROBLEM_HH +#define DOT_FORMATTING_PROBLEM_HH + + +#include "skyline.hh" +#include "std-vector.hh" + +#include + +struct Dot_formatting_problem +{ + Skyline head_skyline_; + Dot_configuration *best_; + int score_; + + void register_configuration (Dot_configuration const &); + Dot_configuration *best () const; + Dot_formatting_problem (vector const &boxes, Interval base_x); + ~Dot_formatting_problem(); +}; + +#endif diff --git a/lily/stem.cc b/lily/stem.cc index 97f1cd24bf..e83b8fbaaf 100644 --- a/lily/stem.cc +++ b/lily/stem.cc @@ -291,41 +291,6 @@ Stem::calc_stem_end_position (SCM smob) if (!no_extend_b && dir * stem_end < 0) stem_end = 0.0; - - /* Make a little room if we have a upflag and there is a dot. - previous approach was to lengthen the stem. This is not - good typesetting practice. */ - if (!get_beam (me) && dir == UP - && durlog > 2) - { - Grob *closest_to_flag = extremal_heads (me)[dir]; - Grob *dots = closest_to_flag - ? Rhythmic_head::get_dots (closest_to_flag) : 0; - - if (dots) - { - Real dp = Staff_symbol_referencer::get_position (dots); - Interval flag_yext = flag (me).extent (Y_AXIS) * (2 / ss) + stem_end; - - /* Very gory: add myself to the X-support of the parent, - which should be a dot-column. */ - - if (flag_yext.distance (dp) < 0.5) - { - Grob *par = dots->get_parent (X_AXIS); - - if (Dot_column::has_interface (par)) - { - Side_position_interface::add_support (par, me); - - /* TODO: apply some better logic here. The flag is - curved inwards, so this will typically be too - much. */ - } - } - } - } - return scm_from_double (stem_end); } diff --git a/scm/define-grobs.scm b/scm/define-grobs.scm index d44d3412cb..3e97d102cb 100644 --- a/scm/define-grobs.scm +++ b/scm/define-grobs.scm @@ -538,7 +538,6 @@ (direction . ,RIGHT) (positioning-done . ,ly:dot-column::calc-positioning-done) (X-extent . ,ly:axis-group-interface::width) - (X-offset . ,ly:dot-column::side-position) (meta . ((class . Item) (interfaces . (dot-column-interface axis-group-interface)))))) diff --git a/scm/safe-lily.scm b/scm/safe-lily.scm index 2e3f2afe65..12657af88a 100644 --- a/scm/safe-lily.scm +++ b/scm/safe-lily.scm @@ -152,7 +152,6 @@ ly:cluster::print ly:cluster-beacon::height ly:custos::print - ly:dot-column::side-position ly:dots::print ly:hairpin::print ly:hara-kiri-group-spanner::force-hara-kiri-callback -- 2.39.5