]> git.donarmstrong.com Git - lilypond.git/blob - lily/include/page-spacing.hh
8f524537bd5a5373fe0800efa4f4167f8f2beac2
[lilypond.git] / lily / include / page-spacing.hh
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2006--2012 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 #ifndef PAGE_SPACING_HH
21 #define PAGE_SPACING_HH
22
23 #include "constrained-breaking.hh"
24 #include "page-spacing-result.hh"
25
26 /* This is a penalty that we add whenever a page breaking solution
27    is not bad enough to completely discard, but bad enough that
28    it is worse than any "proper" solution. For example, if we didn't
29    manage to fit systems on the desired number of pages or if there was
30    too big for a page.
31
32    This constant is large enough that it dominates any reasonable penalty,
33    but small enough that nothing will overflow to infinity (so that we
34    can still distinguish bad spacings by the number of BAD_SPACING_PENALTYs
35    that they incur.
36
37    BAD_SPACING_PENALTY is for occasions where the spacing is bad.
38    TERRIBLE_SPACING_PENALTY is for when we are disregarding a user override
39    (for example, we are failing to satisfy min-systems-per-page). These user
40    overrides are more important than getting good spacing, so they get a
41    larger penalty.
42 */
43 const Real BAD_SPACING_PENALTY = 1e6;
44 const Real TERRIBLE_SPACING_PENALTY = 1e8;
45
46 /* for page_count > 2, we use a dynamic algorithm similar to
47    constrained-breaking -- we have a class that stores the intermediate
48    calculations so they can be reused for querying different page counts.
49 */
50 class Page_spacer
51 {
52 public:
53   Page_spacer (vector<Line_details> const &lines, vsize first_page_num, Page_breaking const *);
54   Page_spacing_result solve (vsize page_count);
55   Page_spacing_result solve ();
56
57 private:
58   struct Page_spacing_node
59   {
60     Page_spacing_node ()
61     {
62       demerits_ = infinity_f;
63       force_ = infinity_f;
64       penalty_ = infinity_f;
65       prev_ = VPOS;
66       system_count_status_ = SYSTEM_COUNT_OK;
67       page_ = 0;
68     }
69
70     Real demerits_;
71     Real force_;
72     Real penalty_;
73     vsize prev_;
74     vsize page_;
75     int system_count_status_;
76   };
77
78   Page_breaking const *breaker_;
79   vsize first_page_num_;
80   vector<Line_details> lines_;
81
82   // If a page-count is requested, we use state_, which
83   // is indexed by page*system, for our dynamic programming
84   // intermediate storage.  Otherwise, we use simple_state_,
85   // which is indexed only by system.
86   Matrix<Page_spacing_node> state_;
87   vector<Page_spacing_node> simple_state_;
88   vsize max_page_count_;
89
90   bool ragged_;
91   bool ragged_last_;
92
93   void resize (vsize page_count);
94   bool calc_subproblem (vsize page, vsize lines);
95 };
96
97 struct Page_spacing
98 {
99   Real force_;
100   Real page_height_;
101   Real rod_height_;
102   Real spring_len_;
103   Real inverse_spring_k_;
104   bool has_footnotes_;
105
106   Line_details last_line_;
107   Line_details first_line_;
108   Page_breaking const *breaker_;
109
110   Page_spacing (Real page_height, Page_breaking const *breaker)
111   {
112     page_height_ = page_height;
113     breaker_ = breaker;
114     has_footnotes_ = false;
115     clear ();
116   }
117
118   void calc_force ();
119   void resize (Real new_height);
120   Real account_for_footnotes (Line_details const &line);
121   void append_system (const Line_details &line);
122   void prepend_system (const Line_details &line);
123   void clear ();
124 };
125
126 #endif /* PAGE_SPACING_HH */