]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/gourlay-breaking.cc
(do_chroot_jail): paranoia security for webserver
[lilypond.git] / lily / gourlay-breaking.cc
index 157fda61605a6dbcf785ca2d55c8627c7c4ef2aa..531c8701456f0305f176bf22d4c4eb5d39c0225b 100644 (file)
@@ -3,18 +3,19 @@
 
   source file of the GNU LilyPond music typesetter
 
-  (c)  1997--2003 Han-Wen Nienhuys <hanwen@cs.uu.nl>
+  (c) 1997--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
 */
-#include <math.h>              // rint
-#include <stdio.h>
 
 #include "gourlay-breaking.hh"
-#include "column-x-positions.hh"
+
+#include <math.h>              // rint
+#include <cstdio>
+
 #include "warn.hh"
 #include "main.hh"
 #include "paper-column.hh"
 #include "paper-score.hh"
-#include "paper-def.hh"
+#include "output-def.hh"
 #include "simple-spacer.hh"
 #include "system.hh"
 
@@ -55,7 +56,7 @@ struct Break_node {
 void
 print_break_nodes (Array<Break_node> const & arr)
 {
-  for (int i =0; i < arr.size (); i++)
+  for (int i = 0; i < arr.size (); i++)
     {
       printf ( "node %d: ", i); 
       arr[i].print ();
@@ -66,6 +67,9 @@ print_break_nodes (Array<Break_node> const & arr)
   This algorithms is adapted from the OSU Tech report on breaking lines.
 
   this function is longish, but not very complicated.
+
+  TODO: should rewrite. See the function in scm/page-layout.scm for
+  inspiration.
   
  */
 Array<Column_x_positions>
@@ -80,9 +84,11 @@ Gourlay_breaking::do_solve () const
   Break_node first_node ;
   optimal_paths.push (first_node);
 
+  bool ragged_right = to_boolean (pscore_->layout_->c_variable ("raggedright"));
+  bool ragged_last = to_boolean (pscore_->layout_->c_variable ("raggedlast"));
+
   Real worst_force = 0.0;
-  
-  for (int break_idx=1; break_idx< breaks.size (); break_idx++) 
+  for (int break_idx = 1; break_idx< breaks.size (); break_idx++) 
     {
       /*
        start with a short line, add measures. At some point 
@@ -94,24 +100,30 @@ Gourlay_breaking::do_solve () const
       
       Real minimal_demerits = infinity_f;
 
-      bool ragged = to_boolean (pscore_->paper_->get_scmvar ("raggedright"));
-
       for (int start_idx = break_idx; start_idx--;)
        {
          Link_array<Grob> line = all.slice (breaks[start_idx], breaks[break_idx]+1);
   
-         line[0]     = dynamic_cast<Item*> (line[0])    ->find_prebroken_piece (RIGHT);
+         line[0]     = dynamic_cast<Item*> (line[0])->find_prebroken_piece (RIGHT);
          line.top () = dynamic_cast<Item*> (line.top ())->find_prebroken_piece (LEFT);
            
          Column_x_positions cp;
          cp.cols_ = line;
 
          Interval line_dims
-           = pscore_->paper_->line_dimensions_int (optimal_paths[start_idx].line_);
-         Simple_spacer * sp = generate_spacing_problem (line, line_dims);
+           = line_dimensions_int (pscore_->layout_, optimal_paths[start_idx].line_);
+         Simple_spacer_wrapper * sp = generate_spacing_problem (line, line_dims);
+         bool last_line = break_idx == breaks.size ()-1;
+         bool ragged = ragged_right
+           || (last_line && ragged_last);
+         
          sp->solve (&cp, ragged);
+         
          delete sp;
 
+         if (ragged && last_line)
+           cp.force_ = 0.0;
+         
          if (fabs (cp.force_) > worst_force)
            worst_force = fabs (cp.force_);
 
@@ -140,7 +152,7 @@ Gourlay_breaking::do_solve () const
            we couldn't satisfy the constraints, this won't get better
            if we add more columns, so we get on with the next one
          */
-         if (!cp.satisfies_constraints_b_)
+         if (!cp.satisfies_constraints_)
            break ; 
        }
 
@@ -167,7 +179,7 @@ Gourlay_breaking::do_solve () const
 
   /* do the last one */
   if (breaks.size () % HAPPY_DOTS_I)
-    progress_indication (String ("[") + to_string (breaks.size()) + "]");    
+    progress_indication (String ("[") + to_string (breaks.size ()) + "]");    
 
   progress_indication ("\n");
 
@@ -183,7 +195,7 @@ Gourlay_breaking::do_solve () const
       i = prev;
     }
 
-  if (verbose_global_b)
+  if (be_verbose_global)
     {
       progress_indication (_f ("Optimal demerits: %f",
                               optimal_paths.top ().demerits_) + "\n");
@@ -192,12 +204,12 @@ Gourlay_breaking::do_solve () const
   if (optimal_paths.top ().demerits_ >= infinity_f)
     warning (_ ("No feasible line breaking found"));
   
-  for (int i= final_breaks.size (); i--;)
+  for (int i = final_breaks.size (); i--;)
     {
       Column_x_positions cp (optimal_paths[final_breaks[i]].line_config_);
       
       lines.push (cp);
-      if(!cp.satisfies_constraints_b_)
+      if (!cp.satisfies_constraints_)
        warning ("Could not find line breaking that satisfies constraints.");
     }
   return lines;
@@ -225,26 +237,29 @@ Gourlay_breaking::combine_demerits (Column_x_positions const &prev,
   Grob * pc = this_one.cols_.top ();
   if (pc->original_)
     {
-      SCM pen = pc->get_grob_property ("penalty");
-      if (gh_number_p (pen) && fabs (gh_scm2double (pen)) < 10000)
+      SCM pen = pc->get_property ("penalty");
+      if (scm_is_number (pen) && fabs (scm_to_double (pen)) < 10000)
        {
-         break_penalties += gh_scm2double (pen);
+         break_penalties += scm_to_double (pen);
        }
     }
+
   /*
-    Q: do want globally non-cramped lines, or locally equally cramped lines. 
+    Q: do we want globally non-cramped lines, or locally equally
+    cramped lines?
+
+    There used to be an example file input/test/uniform-breaking to
+    demonstrate problems with this approach. When music is gradually
+    becoming denser, the uniformity requirement makes lines go from
+    cramped to even more cramped (because going from cramped
+    3meas/line to relatively loose 2meas/line is such a big step.
+    
    */
+
   Real demerit = abs (this_one.force_) +  abs (prev.force_ - this_one.force_)
     + break_penalties;
-
-  /*
-    This is rather ugly (not scale free), but we have to put a
-    penalty breaking two zero-force lines. Otherwise, we don't get a
-    unique solution (i.e.: line-breaks in short raggedright
-    fragments.)
-  */
   
-  if (!this_one.satisfies_constraints_b_)
+  if (!this_one.satisfies_constraints_)
      {
        /*
         If it doesn't satisfy constraints, we make this one