2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 2016 Paul Morris
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.
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.
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/>.
20 #include "international.hh"
21 #include "one-page-breaking.hh"
22 #include "output-def.hh"
23 #include "page-spacing.hh"
24 #include "paper-book.hh"
25 #include "paper-score.hh"
28 One_page_breaking::One_page_breaking (Paper_book *pb)
29 : Page_breaking (pb, 0, 0)
33 One_page_breaking::~One_page_breaking ()
38 One_page_breaking::read_spacing_alist (SCM spec, SCM sym)
40 SCM pair = scm_sloppy_assq (sym, spec);
41 if (scm_is_pair (pair) && scm_is_number (scm_cdr (pair)))
42 return scm_cdr (pair);
44 return scm_from_int (0);
48 This is a somewhat unconventional page-breaking algorithm. The
49 @code{paper-height} setting (in the paper block) is automatically
50 modified to fit the height of the content so that everything exactly
51 fits on a single page with no compression. As usual (in the paper
52 block) the width of the page can be customized with
53 @code{paper-width} or @code{set-paper-size}, and the spacing between
54 the footer and the last system (or top level markup) can be customized
55 with @code{last-bottom-spacing}.
57 It works by (1) temporarily setting the page height to a very large
58 value, (2) doing line breaking and page breaking, much like in
59 @code{ly:minimal-line-breaking}, (3) calculate and set the final
60 height of the page based on the results, taking last-bottom-spacing,
61 footer, top and bottom margins, etc. into account.
64 One_page_breaking::solve ()
66 // TEMPORARILY SET VERY LARGE PAPER HEIGHT
67 // Stencil::translate throws a programming error (for the tagline
68 // position) if this is set any larger than 1e6
69 book_->paper_->set_variable (ly_symbol2scm ("paper-height"), scm_from_double (1e6));
72 message (_ ("Calculating line breaks..."));
73 vsize end = last_break_position ();
74 set_to_ideal_line_configuration (0, end);
75 break_into_pieces (0, end, current_configuration (0));
78 message (_ ("Fitting music on 1 page..."));
79 vsize first_page_num = robust_scm2int (book_->paper_->c_variable ("first-page-number"), 1);
80 Page_spacing_result res = space_systems_on_n_pages (0, 1, first_page_num);
81 SCM lines = systems ();
82 SCM pages = make_pages (res.systems_per_page_, lines);
84 // GET VERTICAL POSITIONS
85 // Larger values are lower on the page. We can't just use the last
86 // one, because the last does not necessarily have the lowest bound.
87 vector<Real> line_posns;
88 SCM lowest_line_pos = scm_from_int (0);
90 Prob *page_pb = unsmob<Prob> (scm_car (pages));
91 SCM config = page_pb->internal_get_property (ly_symbol2scm ("configuration"));
93 for (SCM c = config; scm_is_pair (c); c = scm_cdr (c))
95 SCM this_pos = scm_car (c);
96 line_posns.push_back (scm_to_double (this_pos));
97 if (scm_gr_p (this_pos, lowest_line_pos))
98 lowest_line_pos = this_pos;
101 // CALCULATE THE LOWEST LOWER BOUND OF ALL LINES ON THE PAGE
102 vector<Real> line_heights;
103 for (vsize i = 0; i < system_specs_.size (); i++)
105 if (Paper_score *ps = system_specs_[i].pscore_)
108 vsize broken_intos_size = ps->root_system ()->broken_intos_.size ();
109 for (vsize s = 0; s < broken_intos_size; s++)
111 Grob *system = ps->root_system ()->broken_intos_[s];
112 line_heights.push_back (system->extent (system, Y_AXIS).length ());
115 else if (Prob *pb = system_specs_[i].prob_)
118 Stencil *stil = unsmob<Stencil> (pb->internal_get_property (ly_symbol2scm ("stencil")));
119 line_heights.push_back (stil->extent (Y_AXIS).length ());
123 Real lowest_bound = 0;
124 for (vsize i = 0; i < line_heights.size (); i++)
126 Real low_bound = line_heights[i] + line_posns[i];
127 if (low_bound > lowest_bound)
128 lowest_bound = low_bound;
131 // HANDLE LAST-BOTTOM-SPACING
132 SCM last_bottom = book_->paper_->c_variable ("last-bottom-spacing");
134 SCM padding = read_spacing_alist (last_bottom, ly_symbol2scm("padding"));
135 lowest_bound += scm_to_double (padding);
137 SCM basic_dist = read_spacing_alist (last_bottom, ly_symbol2scm("basic-distance"));
138 SCM minimum_dist = read_spacing_alist (last_bottom, ly_symbol2scm("minimum-distance"));
139 SCM max_dist = scm_max (basic_dist, minimum_dist);
141 // If the last line is a musical system get the distance between its
142 // refpoint and its upper bound. If it is a top level markup its
144 SCM refpoint_dist = scm_from_int (0);
146 SCM lines_probs = page_pb->internal_get_property (ly_symbol2scm ("lines"));
147 Prob *last_line_pb = unsmob<Prob> (scm_list_ref (lines_probs, scm_oneminus (scm_length (lines_probs))));
149 SCM refpoint_extent = last_line_pb->internal_get_property (ly_symbol2scm ("staff-refpoint-extent"));
151 if (scm_is_pair (refpoint_extent) && scm_is_number (scm_car (refpoint_extent)))
152 refpoint_dist = scm_product (scm_car (refpoint_extent), scm_from_int (-1));
154 Real last_bottom_bound = scm_to_double (scm_sum (lowest_line_pos, scm_sum (refpoint_dist, max_dist)));
155 if (last_bottom_bound > lowest_bound)
156 lowest_bound = last_bottom_bound;
158 // SET FINAL PAPER HEIGHT
159 Stencil *foot_stil = unsmob<Stencil> (page_pb->internal_get_property (ly_symbol2scm ("foot-stencil")));
160 Real foot_height = foot_stil->extent (Y_AXIS).length ();
162 SCM top_margin = book_->paper_->c_variable ("top-margin");
163 SCM bottom_margin = book_->paper_->c_variable ("bottom-margin");
164 SCM margins = scm_sum (top_margin, bottom_margin);
166 SCM ppr_height = scm_sum (margins, scm_from_double (lowest_bound + foot_height));
168 book_->paper_->set_variable (ly_symbol2scm ("paper-height"), ppr_height);
170 // bottom-edge determines placement of footer (tagline, footnotes, etc.)
171 page_pb->set_property ("bottom-edge", scm_difference (ppr_height, bottom_margin));