1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3 
4   Copyright (C) 2012--2020 Joe Neeman <joeneeman@gmail.com>
5 
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10 
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15 
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #include "one-line-auto-height-breaking.hh"
21 
22 #include "column-x-positions.hh"
23 #include "international.hh"
24 #include "output-def.hh"
25 #include "page-spacing.hh"
26 #include "paper-book.hh"
27 #include "paper-score.hh"
28 #include "simple-spacer.hh"
29 #include "system.hh"
30 
31 #include <limits>
32 
33 using std::vector;
34 
One_line_auto_height_breaking(Paper_book * pb)35 One_line_auto_height_breaking::One_line_auto_height_breaking (Paper_book *pb)
36   : Page_breaking (pb, 0, 0)
37 {
38 }
39 
~One_line_auto_height_breaking()40 One_line_auto_height_breaking::~One_line_auto_height_breaking ()
41 {
42 }
43 
44 /*
45   This is a somewhat unconventional page-breaking algorithm.  Like
46   ly:one-line-breaking, every score is put on a single page, whose width
47   is enough to fit the entire score on one line.  Line breaks and page
48   breaks are ignored, and the paper-width setting in the paper
49   block is modified to fit the music.  Unlike ly:one-line-breaking
50   the paper-height setting in the paper block is also modified to fit
51   the music.
52 */
53 SCM
solve()54 One_line_auto_height_breaking::solve ()
55 {
56   SCM all_pages = SCM_EOL;
57   Real max_width = 0;
58   Real max_height = 0;
59 
60   for (vsize i = 0; i < system_specs_.size (); ++i)
61     {
62       if (Paper_score *ps = system_specs_[i].pscore_)
63         {
64           vector<Paper_column *> cols = ps->root_system ()->used_columns ();
65 
66           // No indent, "infinite" line width, ragged.
67           Column_x_positions pos = get_line_configuration (cols, std::numeric_limits<Real>::max (), 0, true);
68           vector<Column_x_positions> positions;
69           positions.push_back (pos);
70 
71           ps->root_system ()->break_into_pieces (positions);
72           ps->root_system ()->do_break_substitution_and_fixup_refpoints ();
73           Grob *system = ps->root_system ()->broken_intos_[0];
74 
75           vector<vsize> lines_per_page;
76           lines_per_page.push_back (1);
77           SCM systems = scm_list_1 (system->self_scm ());
78           SCM pages = make_pages (lines_per_page, systems);
79 
80           max_width = std::max (max_width, system->extent (system, X_AXIS).length ());
81           max_height = std::max (max_height, system->extent (system, Y_AXIS).length ());
82           all_pages = scm_cons (scm_car (pages), all_pages);
83         }
84       else if (Prob *pb = system_specs_[i].prob_)
85         // Because we don't call Page_breaking::systems in this algorithm,
86         // we need to manually unprotect the titles.
87         pb->unprotect ();
88     }
89 
90   // Alter paper-width so that it is large enough to fit every system.
91   // TODO: it might be nice to allow different pages to have different widths
92   // and heights.  This would need support in the backends (eg. framework-ps.scm).
93   Real right_margin = from_scm<double> (book_->paper_->c_variable ("right-margin"), 0.0);
94   Real left_margin = from_scm<double> (book_->paper_->c_variable ("left-margin"), 0.0);
95   Real width = max_width + right_margin + left_margin;
96   book_->paper_->set_variable (ly_symbol2scm ("paper-width"), to_scm (width));
97 
98   // Alter paper-height so that it fits the height of the tallest system.
99   Real top_margin = from_scm<double> (book_->paper_->c_variable ("top-margin"), 0.0);
100   Real bottom_margin = from_scm<double> (book_->paper_->c_variable ("bottom-margin"), 0.0);
101   Real height = max_height + top_margin + bottom_margin;
102   book_->paper_->set_variable (ly_symbol2scm ("paper-height"), to_scm (height));
103 
104   return scm_reverse_x (all_pages, SCM_EOL);
105 }
106