From c3884188664f0b104963779990524613854051f1 Mon Sep 17 00:00:00 2001
From: Paul Morris <paulwmorris@gmail.com>
Date: Mon, 11 Jan 2016 12:47:47 -0500
Subject: [PATCH] initial attempt at one-page-breaking

---
 lily/include/one-page-breaking.hh | 35 +++++++++++++++
 lily/one-page-breaking.cc         | 94 +++++++++++++++++++++++++++++++++++++++
 lily/page-breaking-scheme.cc      | 11 +++++
 3 files changed, 140 insertions(+)
 create mode 100644 lily/include/one-page-breaking.hh
 create mode 100644 lily/one-page-breaking.cc

diff --git a/lily/include/one-page-breaking.hh b/lily/include/one-page-breaking.hh
new file mode 100644
index 0000000..596d357
--- /dev/null
+++ b/lily/include/one-page-breaking.hh
@@ -0,0 +1,35 @@
+/*
+  This file is part of LilyPond, the GNU music typesetter.
+
+  Copyright (C) 2016 Paul Morris
+
+  LilyPond is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 3 of the License, or
+  (at your option) any later version.
+
+  LilyPond is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifndef ONE_PAGE_BREAKING_HH
+#define ONE_PAGE_BREAKING_HH
+
+#include "page-breaking.hh"
+#include "page-spacing.hh"
+
+class One_page_breaking: public Page_breaking
+{
+public:
+  virtual SCM solve ();
+
+  One_page_breaking (Paper_book *pb);
+  virtual ~One_page_breaking ();
+};
+
+#endif /* ONE_PAGE_BREAKING_HH */
diff --git a/lily/one-page-breaking.cc b/lily/one-page-breaking.cc
new file mode 100644
index 0000000..b400117
--- /dev/null
+++ b/lily/one-page-breaking.cc
@@ -0,0 +1,94 @@
+/*
+  This file is part of LilyPond, the GNU music typesetter.
+
+  Copyright (C) 2016 Paul Morris
+
+  LilyPond is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 3 of the License, or
+  (at your option) any later version.
+
+  LilyPond is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include "international.hh"
+#include "one-page-breaking.hh"
+#include "output-def.hh"
+#include "page-spacing.hh"
+#include "paper-book.hh"
+
+// TODO: are these includes needed?
+#include "paper-score.hh"
+#include "simple-spacer.hh"
+#include "system.hh"
+
+One_page_breaking::One_page_breaking (Paper_book *pb)
+  : Page_breaking (pb, 0, 0)
+{
+}
+
+One_page_breaking::~One_page_breaking ()
+{
+}
+
+SCM
+One_page_breaking::solve ()
+{
+  // LINE BREAKING - same as minimal page breaking
+  vsize end = last_break_position ();
+  message (_ ("Calculating line breaks..."));
+  set_to_ideal_line_configuration (0, end);
+  break_into_pieces (0, end, current_configuration (0));
+  
+  // SET PAPER HEIGHT
+  // following the lead of One_line_page_breaking::solve
+  
+  message (_ ("Calculating page height..."));
+  Real max_height = 0;
+  SCM lines = systems ();
+  int lines_count = scm_to_int (scm_length (lines));
+  
+  for (int i = 0; i < lines_count; i++)
+    {
+      Grob *system = unsmob<System> (scm_list_ref (lines, scm_from_int (i)));
+      
+      Real sys_height = system->extent (system, Y_AXIS).length ();
+      message (_f ("sys_height: %d", (int) int (sys_height)));
+      
+      max_height = max_height + sys_height;
+      message (_f ("new max_height: %d ", (int) int (max_height)));
+	}
+  
+  // Alter paper-height so that it is large enough to fit every system.
+  Real top_margin = robust_scm2double (book_->paper_->c_variable ("top-margin"), 0.0);
+  Real bottom_margin = robust_scm2double (book_->paper_->c_variable ("bottom-margin"), 0.0);
+  
+  // SCM basic_dist = scm_from_utf8_symbol ("basic-distance");
+  SCM basic_dist = ly_symbol2scm ("basic-distance");
+  
+  Real system_system = robust_scm2double (scm_assoc_ref (book_->paper_->c_variable ("system-system-spacing"), basic_dist), 0.0);
+  
+  message (_f ("system_system: %d ", (int) int (system_system)));
+  
+  Real total_system_system = (lines_count - 1) * system_system;
+  message (_f ("total_system_system: %d ", (int) int (total_system_system)));
+    
+  Real height = max_height + top_margin + bottom_margin + total_system_system;
+  book_->paper_->set_variable (ly_symbol2scm ("paper-height"), scm_from_double (height));
+
+  // PAGE BREAKING
+  message (_ ("Fitting music on 1 page..."));
+  
+  vsize first_page_num = robust_scm2int (book_->paper_->c_variable ("first-page-number"), 1);
+  
+  // Page_spacing_result res = pack_systems_on_least_pages (0, first_page_num);
+  Page_spacing_result res = space_systems_on_n_pages (0, 1, first_page_num);
+  
+  return make_pages (res.systems_per_page_, lines);
+}
diff --git a/lily/page-breaking-scheme.cc b/lily/page-breaking-scheme.cc
index 70ec639..2c7158a 100644
--- a/lily/page-breaking-scheme.cc
+++ b/lily/page-breaking-scheme.cc
@@ -20,6 +20,7 @@
 #include "paper-book.hh"
 #include "page-turn-page-breaking.hh"
 #include "one-line-page-breaking.hh"
+#include "one-page-breaking.hh"
 #include "optimal-page-breaking.hh"
 #include "minimal-page-breaking.hh"
 
@@ -62,3 +63,13 @@ LY_DEFINE (ly_one_line_breaking, "ly:one-line-breaking",
   One_line_page_breaking b (unsmob<Paper_book> (pb));
   return b.solve ();
 }
+
+LY_DEFINE (ly_one_page_breaking, "ly:one-page-breaking",
+           1, 0, 0, (SCM pb),
+           "Put each score on a single page.  The paper-height settings"
+           " are modified so each score fits on one page, and the"
+           " height of the page matches the height of the full score.")
+{
+  One_page_breaking b (unsmob<Paper_book> (pb));
+  return b.solve ();
+}
-- 
2.1.4

