]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/page-layout-problem.cc
Merge branch 'lilypond/translation'
[lilypond.git] / lily / page-layout-problem.cc
index c1cfeb0656bbdff88e9eb0c2b56cb4ba3e91de55..17c2912e148b2c33b8d721c315df27303f24ae61 100644 (file)
 #include "prob.hh"
 #include "skyline-pair.hh"
 #include "system.hh"
+#include "text-interface.hh"
 
-Page_layout_problem::Page_layout_problem (Paper_book *pb, SCM page_scm, SCM systems)
+/*
+ Returns the number of footntoes associated with a given line.
+*/
+
+vsize
+Page_layout_problem::get_footnote_count (SCM lines)
+{
+  vsize fn_count = 0;
+  for (SCM s = lines; scm_is_pair (s); s = scm_cdr (s))
+    {
+      if (Grob *g = unsmob_grob (scm_car (s)))
+       {
+         System *sys = dynamic_cast<System *> (g);
+         if (!sys)
+            {
+              programming_error ("got a grob for footnotes that wasn't a System");
+              continue;
+            }
+          fn_count += sys->num_footnotes ();
+        }
+      else if (Prob *p = unsmob_prob (scm_car (s)))
+        {
+          SCM stencils = p->get_property ("footnotes");
+          if (stencils == SCM_EOL)
+            continue;
+          for (SCM st = stencils; scm_is_pair (st); st = scm_cdr (st))
+            fn_count++;
+        }
+    }
+
+  return fn_count;
+}
+
+/*
+   Returns a stencil for the footnote of each system.  This stencil may
+   itself be comprised of several footnotes.
+
+   This is a long function, but it seems better to keep it intact rather than
+   splitting it into parts.
+*/
+
+SCM
+Page_layout_problem::get_footnotes_from_lines (SCM lines, int counter, Paper_book *pb)
+{
+  /*
+    first, we have to see how many footnotes are on this page.
+    we need to do this first so that we can line them up
+  */
+
+  Output_def *paper = pb->paper_;
+
+  if (!paper)
+    {
+      programming_error ("Cannot get footnotes because there is no valid paper block.");
+      return SCM_EOL;
+    }
+
+  SCM number_footnote_table = pb->top_paper ()->c_variable ("number-footnote-table");
+  if (!scm_is_pair (number_footnote_table))
+    number_footnote_table = SCM_EOL;
+  SCM numbering_function = paper->c_variable ("footnote-numbering-function");
+  SCM layout = paper->self_scm ();
+  SCM props = scm_call_1 (ly_lily_module_constant ("layout-extract-page-properties"),
+                          paper->self_scm ());
+  Real padding = robust_scm2double (paper->c_variable ("footnote-padding"), 0.0);
+  Real number_raise = robust_scm2double (paper->c_variable ("footnote-number-raise"), 0.0);
+
+  vsize fn_count = get_footnote_count (lines);
+
+  // now, make the footnote stencils with the numbering function
+  SCM numbers = SCM_EOL;
+  SCM in_text_numbers = SCM_EOL;
+  bool do_numbering = to_boolean (paper->c_variable ("footnote-auto-numbering"));
+  // if there's no numbering, skip all this
+  /*
+    TODO: This recalculates numbering every time this function is called, including once
+    after the balloon prints are called.  Although it is not a huge computational drain,
+    it'd be more elegant to turn this calculation off when it is no longer needed.
+    
+    In a separate commit, it'd be nice to streamline the way that page layout property
+    is handled so that the process of building `config's in page-breaking does result
+    in duplicated work, either by making this process less complicated or (preferably)
+    by passing its results downstream.
+  */
+  if (do_numbering)
+    {
+      vector<SCM> footnote_number_markups; // Holds the numbering markups.
+      vector<Stencil *> footnote_number_stencils; // Holds translated versions of the stencilized numbering markups.
+      for (vsize i = 0; i < fn_count; i++)
+        {
+          SCM markup = scm_call_1 (numbering_function, scm_from_int (counter));
+          Stencil *s = unsmob_stencil (Text_interface::interpret_markup (layout, props, markup));
+          if (!s)
+            {
+              programming_error ("Your numbering function needs to return a stencil.");
+              markup = SCM_EOL;
+              s = new Stencil (Box (Interval (0,0), Interval (0,0)), SCM_EOL);
+            }
+          footnote_number_markups.push_back (markup);
+          footnote_number_stencils.push_back (s);
+          counter++;
+        }
+
+      // find the maximum X_AXIS length
+      Real max_length = -infinity_f;
+      for (vsize i = 0; i < fn_count; i++)
+        max_length = max (max_length, footnote_number_stencils[i]->extent (X_AXIS).length ());
+
+      /*
+        translate each stencil such that it attains the correct maximum length and bundle the
+        footnotes into a scheme object.
+      */
+      SCM *tail = &numbers;
+      SCM *in_text_tail = &in_text_numbers;
+
+      for (vsize i = 0; i < fn_count; i++)
+        {
+          *in_text_tail = scm_cons (footnote_number_markups[i], SCM_EOL);
+          in_text_tail = SCM_CDRLOC (*in_text_tail);
+          footnote_number_stencils[i]->translate_axis (max_length - footnote_number_stencils[i]->extent (X_AXIS).length (), X_AXIS);
+          *tail = scm_cons (footnote_number_stencils[i]->smobbed_copy (), SCM_EOL);
+          tail = SCM_CDRLOC (*tail);
+        }
+    }
+  // build the footnotes
+
+  SCM footnotes = SCM_EOL;
+
+  for (SCM s = lines; scm_is_pair (s); s = scm_cdr (s))
+    {
+      // Take care of musical systems.
+      if (Grob *g = unsmob_grob (scm_car (s)))
+       {
+         System *sys = dynamic_cast<System *> (g);
+         if (!sys)
+            {
+              programming_error ("got a grob for footnotes that wasn't a System");
+              continue;
+            }
+            Stencil mol;
+
+            for (vsize i = 0; i < sys->footnote_grobs ()->size (); i++)
+              {
+                Grob *footnote = sys->footnote_grobs ()->at (i);
+                SCM footnote_markup = footnote->get_property ("footnote-text");
+                if (Spanner *orig = dynamic_cast<Spanner *>(footnote))
+                  if (orig->is_broken ())
+                    footnote_markup = orig->broken_intos_[0]->get_property ("footnote-text");
+
+                if (!Text_interface::is_markup (footnote_markup))
+                  continue;
+
+                SCM props = scm_call_1 (ly_lily_module_constant ("layout-extract-page-properties"),
+                                        paper->self_scm ());
+
+                SCM footnote_stl = Text_interface::interpret_markup (paper->self_scm (),
+                                                                     props, footnote_markup);
+
+                Stencil *footnote_stencil = unsmob_stencil (footnote_stl);
+                if (do_numbering)
+                  {
+                    SCM annotation_scm = scm_car (in_text_numbers);
+                    footnote->set_property ("text", annotation_scm);
+                    if (Spanner *orig = dynamic_cast<Spanner *>(footnote))
+                      {
+                        orig->set_property ("text", annotation_scm);
+                        if (orig->is_broken ())
+                          for (vsize i = 0; i < orig->broken_intos_.size (); i++)
+                              orig->broken_intos_[i]->set_property ("text", annotation_scm);
+                      }
+                    
+                    Stencil *annotation = unsmob_stencil (scm_car (numbers));
+                    annotation->translate_axis (footnote_stencil->extent (Y_AXIS)[UP] + number_raise - annotation->extent(Y_AXIS)[UP], Y_AXIS);
+                    footnote_stencil->add_at_edge (X_AXIS, LEFT, *annotation, 0.0);
+                    numbers = scm_cdr (numbers);
+                    in_text_numbers = scm_cdr (in_text_numbers);
+                  }
+                mol.add_at_edge (Y_AXIS, DOWN, *footnote_stencil, padding);
+              }
+            footnotes = scm_cons (mol.smobbed_copy (), footnotes);
+        }
+      // Take care of top-level markups
+      else if (Prob *p = unsmob_prob (scm_car (s)))
+        {
+          SCM stencils = p->get_property ("footnotes");
+          if (stencils == SCM_EOL)
+            continue;
+          Stencil footnote_stencil;
+
+          for (SCM st = stencils; scm_is_pair (st); st = scm_cdr (st))
+            {
+              Stencil mol;
+              Stencil *footnote = unsmob_stencil (scm_cadar (st));
+              mol.add_stencil (*footnote);
+              if (do_numbering)
+                {
+                  Stencil *annotation = unsmob_stencil (scm_car (numbers));
+                  SCM in_text_annotation = scm_car (in_text_numbers);
+                  SCM in_text_stencil = Text_interface::interpret_markup (layout, props, in_text_annotation);
+                  if (!unsmob_stencil (in_text_stencil))
+                    in_text_stencil = SCM_EOL;
+                  number_footnote_table = scm_cons (scm_cons (scm_caar (st), in_text_stencil), number_footnote_table);
+                  annotation->translate_axis (mol.extent (Y_AXIS)[UP] + number_raise - annotation->extent(Y_AXIS)[UP], Y_AXIS);
+                  mol.add_at_edge (X_AXIS, LEFT, *annotation, 0.0);
+                  numbers = scm_cdr (numbers);
+                  in_text_numbers = scm_cdr (in_text_numbers);
+                }
+              footnote_stencil.add_at_edge (Y_AXIS, DOWN, mol, padding);
+            }
+          footnotes = scm_cons (footnote_stencil.smobbed_copy (), footnotes);
+        }
+    }
+
+  // note that this line of code doesn't do anything if numbering isn't turned on
+  pb->top_paper ()->set_variable (ly_symbol2scm ("number-footnote-table"), number_footnote_table);
+  if (!scm_is_pair (footnotes))
+    return SCM_EOL;
+
+  return scm_reverse (footnotes);
+}
+
+Stencil*
+Page_layout_problem::get_footnote_separator_stencil (Output_def *paper)
+{
+  SCM props = scm_call_1 (ly_lily_module_constant ("layout-extract-page-properties"),
+                          paper->self_scm ());
+
+  SCM markup = paper->c_variable ("footnote-separator-markup");
+
+  if (!Text_interface::is_markup (markup))
+    return NULL;
+
+  SCM footnote_stencil = Text_interface::interpret_markup (paper->self_scm (),
+                                                           props, markup);
+
+  Stencil *footnote_separator = unsmob_stencil (footnote_stencil);
+
+  return footnote_separator;
+}
+
+void
+Page_layout_problem::add_footnotes_to_footer (SCM footnotes, Stencil *foot, Paper_book *pb)
+{
+  bool footnotes_found = false;
+  Real footnote_padding = robust_scm2double (pb->paper_->c_variable ("footnote-padding"), 0.0);
+  Real footnote_footer_padding = robust_scm2double (pb->paper_->c_variable ("footnote-footer-padding"), 0.0);
+  
+  footnotes = scm_reverse (footnotes);
+
+  for (SCM s = footnotes; scm_is_pair (s); s = scm_cdr (s))
+    {
+      Stencil *stencil = unsmob_stencil (scm_car (s));
+
+      if (!stencil)
+        continue;
+
+      if (!stencil->is_empty ())
+        {
+          foot->add_at_edge (Y_AXIS, UP, *stencil, (!footnotes_found ? footnote_footer_padding : footnote_padding));
+          footnotes_found = true;
+        }
+    }
+
+  if (footnotes_found)
+    {
+      Stencil *separator = get_footnote_separator_stencil (pb->paper_);
+      if (separator)
+        foot->add_at_edge (Y_AXIS, UP, *separator, footnote_padding);
+    }
+}
+
+Page_layout_problem::Page_layout_problem (Paper_book *pb, SCM page_scm, SCM systems, int footnote_count)
   : bottom_skyline_ (DOWN)
 {
   Prob *page = unsmob_prob (page_scm);
@@ -48,6 +320,16 @@ Page_layout_problem::Page_layout_problem (Paper_book *pb, SCM page_scm, SCM syst
       Stencil *head = unsmob_stencil (page->get_property ("head-stencil"));
       Stencil *foot = unsmob_stencil (page->get_property ("foot-stencil"));
 
+      if (pb && pb->paper_)
+        {
+          if (to_boolean (pb->paper_->c_variable ("reset-footnotes-on-new-page")))
+            footnote_count = 0;
+          SCM footnotes = get_footnotes_from_lines (systems, footnote_count, pb);
+          add_footnotes_to_footer (footnotes, foot, pb);
+        }
+      else
+        warning ("A page layout problem has been initiated that cannot accommodate footnotes.");
+
       header_height_ = head ? head->extent (Y_AXIS).length () : 0;
       footer_height_ = foot ? foot->extent (Y_AXIS).length () : 0;
       page_height_ = robust_scm2double (page->get_property ("paper-height"), 100);
@@ -214,7 +496,7 @@ Page_layout_problem::append_system (System *sys, Spring const& spring, Real inde
   springs_.push_back (spring_copy);
 
   bottom_skyline_ = down_skyline;
-  elements_.push_back (Element (elts, minimum_offsets));
+  elements_.push_back (Element (elts, minimum_offsets, padding));
 
   // Add the springs for the VerticalAxisGroups in this system.
 
@@ -242,7 +524,7 @@ Page_layout_problem::append_system (System *sys, Spring const& spring, Real inde
          alter_spring_from_spacing_spec (spec, &spring);
 
          springs_.push_back (spring);
-         Real min_distance = (found_spaceable_staff ? minimum_offsets[last_spaceable_staff] : 0) - minimum_offsets[i];
+         Real min_distance = (found_spaceable_staff ? minimum_offsets_with_min_dist[last_spaceable_staff] : 0) - minimum_offsets_with_min_dist[i];
          springs_.back ().ensure_min_distance (min_distance);
 
          if (scm_is_pair (manual_dists))
@@ -300,7 +582,7 @@ Page_layout_problem::append_prob (Prob *prob, Spring const& spring, Real padding
     spring_copy.ensure_min_distance (minimum_distance + padding);
 
   springs_.push_back (spring_copy);
-  elements_.push_back (Element (prob));
+  elements_.push_back (Element (prob, padding));
 }
 
 void
@@ -319,10 +601,10 @@ Page_layout_problem::solve_rod_spring_problem (bool ragged)
       Real overflow = spacer.configuration_length (spacer.force ())
                      - page_height_;
       if (ragged && overflow < 1e-6)
-       warning (_ ("couldn't fit music on page: ragged-spacing was requested, but page was compressed"));
+       warning (_ ("cannot fit music on page: ragged-spacing was requested, but page was compressed"));
       else
        {
-         warning (_f ("couldn't fit music on page: overflow is %f",
+         warning (_f ("cannot fit music on page: overflow is %f",
                       overflow));
          warning (_ ("compressing music to fit"));
          vsize space_count = solution_.size ();
@@ -364,7 +646,8 @@ Page_layout_problem::find_system_offsets ()
          if (loose_lines.size ())
            {
              Interval loose_extent = loose_lines.back ()->extent (loose_lines.back (), Y_AXIS);
-             Real min_distance = -loose_extent[DOWN] + prob_extent[UP]; // TODO: include padding/minimum-distance
+             Real min_distance = (-loose_extent[DOWN] + prob_extent[UP]
+                                  + elements_[i].padding);
 
              loose_line_min_distances.push_back (min_distance);
              loose_lines.push_back (0);
@@ -397,7 +680,6 @@ Page_layout_problem::find_system_offsets ()
          Real system_position = first_staff_position + first_staff_min_translation;
 
          // Position the staves within this system.
-         Real translation = 0;
          vector<Real> const& min_offsets = elements_[i].min_offsets;
          bool found_spaceable_staff = false;
          for (vsize staff_idx = 0; staff_idx < elements_[i].staves.size (); ++staff_idx)
@@ -408,26 +690,27 @@ Page_layout_problem::find_system_offsets ()
              if (is_spaceable (staff))
                {
                  // this is relative to the system: negative numbers are down.
-                 translation = system_position - solution_[spring_idx];
-                 spring_idx++;
+                 staff->translate_axis (system_position - solution_[spring_idx], Y_AXIS);
 
                  // Lay out any non-spaceable lines between this line and
                  // the last one.
                  if (loose_lines.size ())
                    {
-                     loose_line_min_distances.push_back (min_offsets[staff_idx-1] - min_offsets[staff_idx]);
+                     if (staff_idx)
+                       loose_line_min_distances.push_back (min_offsets[staff_idx-1] - min_offsets[staff_idx]);
+                     else
+                       loose_line_min_distances.push_back (elements_[i].padding - min_offsets[staff_idx]);
                      loose_lines.push_back (staff);
 
                      distribute_loose_lines (loose_lines, loose_line_min_distances,
-                                             last_spaceable_line_translation, translation - system_position);
+                                             last_spaceable_line_translation, -solution_[spring_idx]);
                      loose_lines.clear ();
                      loose_line_min_distances.clear ();
                    }
                  last_spaceable_line = staff;
-                 last_spaceable_line_translation = -solution_[spring_idx - 1];
-
-                 staff->translate_axis (translation, Y_AXIS);
+                 last_spaceable_line_translation = -solution_[spring_idx];
                  found_spaceable_staff = true;
+                 spring_idx++;
                }
              else
                {
@@ -441,18 +724,20 @@ Page_layout_problem::find_system_offsets ()
                    // using a scheme similar to the one in set_column_rods.
                    loose_line_min_distances.push_back (min_offsets[staff_idx-1] - min_offsets[staff_idx]);
                  else
-                   {
+                   { // this is the first line in a system
                      Real min_dist = 0;
                      if (loose_lines.back ())
-                       min_dist = Axis_group_interface::minimum_distance (loose_lines.back (),
-                                                                          staff,
-                                                                          Y_AXIS);
+                       // distance to the final line in the preceding system,
+                       // including 'system-system-spacing 'padding
+                       min_dist = (Axis_group_interface::minimum_distance (loose_lines.back (),
+                                                                           staff,
+                                                                           Y_AXIS)
+                                   + elements_[i].padding);
                      else if (!last_title_extent.is_empty ())
-                       { // distance to the preceding title
-                         // TODO: add options for controlling the space between a loose line
-                         // and a title/markup preceding it.
-                         min_dist = staff->extent (staff, Y_AXIS)[UP] - last_title_extent[DOWN];
-                       }
+                       // distance to the preceding title,
+                       //  including 'markup-system-spacing 'padding
+                       min_dist = (staff->extent (staff, Y_AXIS)[UP] - last_title_extent[DOWN]
+                                   + elements_[i].padding);
                      else // distance to the top margin
                        min_dist = header_padding_ + header_height_ + staff->extent (staff, Y_AXIS)[UP];
 
@@ -674,12 +959,7 @@ Page_layout_problem::get_fixed_spacing (Grob *before, Grob *after, int spaceable
        return robust_scm2double (cached, 0.0);
     }
 
-  SCM spec = Page_layout_problem::get_spacing_spec (before, after, pure, start, end);
   Real ret = -infinity_f;
-  Real stretchability = 0;
-  if (Page_layout_problem::read_spacing_spec (spec, &stretchability, ly_symbol2scm ("stretchability"))
-      && stretchability == 0)
-    Page_layout_problem::read_spacing_spec (spec, &ret, ly_symbol2scm ("basic-distance"));
 
   // If we're pure, then paper-columns have not had their systems set,
   // and so elts[i]->get_system () is unreliable.
@@ -701,7 +981,7 @@ Page_layout_problem::get_fixed_spacing (Grob *before, Grob *after, int spaceable
   // Cache the result.  As above, we ignore "end."
   if (pure)
     after_sp->cache_pure_property (cache_symbol, start, 0, scm_from_double (ret));
-    
+
   return ret;
 }
 
@@ -760,10 +1040,12 @@ Page_layout_problem::get_spacing_spec (Grob *before, Grob *after, bool pure, int
        {
          Direction before_affinity = to_dir (before->get_maybe_pure_property ("staff-affinity", pure, start, end));
          Direction after_affinity = to_dir (after->get_maybe_pure_property ("staff-affinity", pure, start, end));
-         if (after_affinity > before_affinity)
+         static bool warned = false;
+         if (after_affinity > before_affinity
+             && !warned && !pure)
            {
              warning (_ ("staff-affinities should only decrease"));
-             after_affinity = before_affinity;
+             warned = true;
            }
          if (before_affinity != UP)
            return before->get_maybe_pure_property ("nonstaff-nonstaff-spacing", pure, start, end);
@@ -791,10 +1073,7 @@ Page_layout_problem::alter_spring_from_spacing_spec (SCM spec, Spring* spring)
   spring->set_default_strength ();
 
   if (read_spacing_spec (spec, &stretch, ly_symbol2scm ("stretchability")))
-    {
-      spring->set_inverse_stretch_strength (stretch);
-      spring->set_inverse_compress_strength (stretch);
-    }
+    spring->set_inverse_stretch_strength (stretch);
 }
 
 vector<Grob*>