]> git.donarmstrong.com Git - lilypond.git/commitdiff
Cleanup and simplify dot column formatting logic.
authorHan-Wen Nienhuys <hanwen@xs4all.nl>
Sun, 25 Mar 2007 17:45:32 +0000 (14:45 -0300)
committerHan-Wen Nienhuys <hanwen@xs4all.nl>
Sun, 25 Mar 2007 17:45:32 +0000 (14:45 -0300)
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
lily/dot-configuration.cc
lily/dot-formatting-problem.cc [new file with mode: 0644]
lily/include/dot-configuration.hh
lily/include/dot-formatting-problem.hh [new file with mode: 0644]
lily/stem.cc
scm/define-grobs.scm
scm/safe-lily.scm

index 6b0ebaeac1a11c4804fa2485f7d159942550e583..aed84ddf9d98abb2d6f02dfe553a3e87f9311435 100644 (file)
@@ -11,6 +11,8 @@
 #include <cstdio>
 #include <cmath>
 #include <map>
+#include <set>
+
 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<Grob*> dots
     = extract_grob_array (me, "dots");
 
+  vector<Grob*> 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<Box> boxes;
+  set<Grob*> 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<Grob*>::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 "
index 7676c14dd4cc3628d804b20016c147464d8aa862..c4cf852d1a7a154d254b4d315745eeafbf5aae15 100644 (file)
@@ -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 (file)
index 0000000..81dc908
--- /dev/null
@@ -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<Box> const &boxes, Interval base_x)
+  : head_skyline_ (boxes, 0.0, Y_AXIS, RIGHT)
+{
+  best_ = 0;
+  head_skyline_.set_minimum_height (base_x[RIGHT]);
+}
index 1eed8f3c359a1b127a1bc3250bb2812c383d8e91..a7ab09fab3fc0ea3b48402f4211a19e9dd2eef6f 100644 (file)
@@ -13,6 +13,7 @@
 
 #include "lily-proto.hh"
 #include "direction.hh"
+#include "box.hh"
 
 #include <map>
 
@@ -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<int, Dot_position>
 {
+  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 (file)
index 0000000..da24854
--- /dev/null
@@ -0,0 +1,22 @@
+#ifndef DOT_FORMATTING_PROBLEM_HH
+#define DOT_FORMATTING_PROBLEM_HH
+
+
+#include "skyline.hh"
+#include "std-vector.hh"
+
+#include <map>
+
+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<Box> const &boxes, Interval base_x);
+  ~Dot_formatting_problem();
+};
+
+#endif
index 97f1cd24bfe1572c6d6f47331150c3ae7492f74a..e83b8fbaaff8cba230dd94c34eb9d878b87b34e6 100644 (file)
@@ -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);
 }
 
index d44d3412cb187499f9bc3f5cc0a924d539ef25da..3e97d102cbe51bcca76e94bacbdff45e58934d36 100644 (file)
        (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))))))
index 2e3f2afe654fb0bb4d3ba8101483b6b7ce0da995..12657af88a2974aac5d5c18f7ba4f8c673578c7b 100644 (file)
    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