]> git.donarmstrong.com Git - lilypond.git/blob - lily/one-page-breaking.cc
Web-ja: update introduction
[lilypond.git] / lily / one-page-breaking.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2016 Paul Morris
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 "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"
26 #include "system.hh"
27
28 One_page_breaking::One_page_breaking (Paper_book *pb)
29   : Page_breaking (pb, 0, 0)
30 {
31 }
32
33 One_page_breaking::~One_page_breaking ()
34 {
35 }
36
37 SCM
38 One_page_breaking::read_spacing_alist (SCM spec, SCM sym)
39 {
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);
43   else
44     return scm_from_int (0);
45 }
46
47 /*
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}.
56
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.
62 */
63 SCM
64 One_page_breaking::solve ()
65 {
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));
70
71   // LINE BREAKING
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));
76
77   // PAGE BREAKING
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);
83
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);
89
90   Prob *page_pb = unsmob<Prob> (scm_car (pages));
91   SCM config = page_pb->internal_get_property (ly_symbol2scm ("configuration"));
92
93   for (SCM c = config; scm_is_pair (c); c = scm_cdr (c))
94     {
95       SCM this_pos = scm_car (c);
96       line_posns.push_back (scm_to_double (this_pos));
97       if (scm_is_true (scm_gr_p (this_pos, lowest_line_pos)))
98         lowest_line_pos = this_pos;
99     }
100
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++)
104     {
105       if (Paper_score *ps = system_specs_[i].pscore_)
106         {
107                   // musical systems
108                   vsize broken_intos_size = ps->root_system ()->broken_intos_.size ();
109               for (vsize s = 0; s < broken_intos_size; s++)
110                 {
111                   Grob *system = ps->root_system ()->broken_intos_[s];
112                   line_heights.push_back (system->extent (system, Y_AXIS).length ());
113                 }
114         }
115       else if (Prob *pb = system_specs_[i].prob_)
116         {
117               // top-level markups
118           Stencil *stil = unsmob<Stencil> (pb->internal_get_property (ly_symbol2scm ("stencil")));
119           line_heights.push_back (stil->extent (Y_AXIS).length ());
120         }
121     }
122
123   Real lowest_bound = 0;
124   for (vsize i = 0; i < line_heights.size (); i++)
125     {
126       Real low_bound = line_heights[i] + line_posns[i];
127       if (low_bound > lowest_bound)
128             lowest_bound = low_bound;
129         }
130
131   // HANDLE LAST-BOTTOM-SPACING
132   SCM last_bottom = book_->paper_->c_variable ("last-bottom-spacing");
133
134   SCM padding = read_spacing_alist (last_bottom, ly_symbol2scm("padding"));
135   lowest_bound += scm_to_double (padding);
136
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);
140
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
143   // refpoint is 0.
144   SCM refpoint_dist = scm_from_int (0);
145
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))));
148
149   SCM refpoint_extent = last_line_pb->internal_get_property (ly_symbol2scm ("staff-refpoint-extent"));
150
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));
153
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;
157
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 ();
161
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);
165
166   SCM ppr_height = scm_sum (margins, scm_from_double (lowest_bound + foot_height));
167
168   book_->paper_->set_variable (ly_symbol2scm ("paper-height"), ppr_height);
169
170   // bottom-edge determines placement of footer (tagline, footnotes, etc.)
171   page_pb->set_property ("bottom-edge", scm_difference (ppr_height, bottom_margin));
172
173   return pages;
174 }