]> git.donarmstrong.com Git - lilypond.git/blob - lily/include/page-breaking.hh
Add systems-per-page variable in \paper.
[lilypond.git] / lily / include / page-breaking.hh
1 /*
2   page-breaking.hh -- declare a superclass and utility
3   functions for several different page-breaking algorithms
4
5   source file of the GNU LilyPond music typesetter
6
7   (c) 2006--2009 Joe Neeman <joeneeman@gmail.com>
8 */
9
10 #ifndef PAGE_BREAKING_HH
11 #define PAGE_BREAKING_HH
12
13 #include "constrained-breaking.hh"
14 #include "page-spacing.hh"
15
16 /* Either a paper-score, markup or header.
17  */
18 struct System_spec
19 {
20   System_spec (Paper_score *ps)
21   {
22     pscore_ = ps;
23     prob_ = NULL;
24   }
25
26   System_spec (Prob *pb)
27   {
28     prob_ = pb;
29     pscore_ = NULL;
30   }
31
32   System_spec ()
33   {
34     pscore_ = NULL;
35     prob_ = NULL;
36   }
37
38   Paper_score *pscore_;
39   Prob *prob_;
40 };
41
42 struct Break_position
43 {
44   /*
45     index in system_spec_index_, if VPOS start of book. 
46    */
47   vsize system_spec_index_;
48
49   /* if system_spec_index_ is a score, then we start at the score_brk_'th possible
50      page-break in the score */
51   vsize score_break_; 
52
53   /* if system_spec_index_ is a score, this points to the broken column */
54   Grob *col_;  
55   bool score_ender_;
56
57   Break_position (vsize s=VPOS, vsize brk=VPOS, Grob *g=NULL, bool end=false)
58   {
59     system_spec_index_ = s;
60     score_break_ = brk;
61     col_ = g;
62     score_ender_ = end;
63   }
64
65   /*
66     lexicographic in (system_spec_index_, score_break_)
67    */
68   bool operator< (const Break_position &other)
69   {
70     return (system_spec_index_ == VPOS && other.system_spec_index_ != VPOS)
71       || (system_spec_index_ < other.system_spec_index_)
72       || (system_spec_index_ == other.system_spec_index_ && score_break_ < other.score_break_);
73   }
74
75   bool operator<= (const Break_position &other)
76   {
77     return (system_spec_index_ == VPOS)
78       || (system_spec_index_ < other.system_spec_index_ && other.system_spec_index_ != VPOS)
79       || (system_spec_index_ == other.system_spec_index_ && score_break_ <= other.score_break_);
80   }
81 };
82
83 class Page_breaking
84 {
85 public:
86   typedef bool (*Break_predicate) (Grob *);
87   typedef vector<vsize> Line_division;
88   
89   /*
90     TODO: naming.
91
92     determine the page breaking, and break scores into lines
93     appropriately.
94    */
95   virtual SCM solve () = 0;
96
97   Page_breaking (Paper_book *pb, Break_predicate);
98   virtual ~Page_breaking ();
99
100   bool ragged () const;
101   bool ragged_last () const;
102   bool is_last () const;
103   bool ends_score () const;
104   Real page_height (int page_number, bool last) const;
105   Real page_top_space () const;
106   vsize system_count () const;
107
108 protected:
109   Paper_book *book_;
110
111   vsize next_system (Break_position const &break_pos) const;
112
113   SCM make_pages (vector<vsize> lines_per_page, SCM lines);
114
115   vsize min_system_count (vsize start, vsize end);
116   vsize max_system_count (vsize start, vsize end);
117
118
119   void break_into_pieces (vsize start, vsize end, Line_division const &div);
120   SCM systems ();
121
122   void set_current_breakpoints (vsize start,
123                                 vsize end,
124                                 vsize system_count,
125                                 Line_division lower_bound = Line_division (),
126                                 Line_division upper_bound = Line_division ());
127   void set_to_ideal_line_configuration (vsize start, vsize end);
128
129   vsize current_configuration_count () const;
130   Line_division current_configuration (vsize configuration_index) const;
131   Page_spacing_result space_systems_on_n_pages (vsize configuration_index,
132                                                 vsize n, vsize first_page_num,
133                                                 int systems_per_page=0);
134   Page_spacing_result space_systems_on_n_or_one_more_pages (vsize configuration_index, vsize n,
135                                                             vsize first_page_num,
136                                                             int systems_per_page=0);
137   Page_spacing_result space_systems_on_best_pages (vsize configuration_index,
138                                                    vsize first_page_num);
139   Page_spacing_result space_systems_with_fixed_number_per_page (vsize configuration_index,
140                                                                 vsize first_page_num,
141                                                                 int systems_per_page);
142   Page_spacing_result pack_systems_on_least_pages (vsize configuration_index,
143                                                    vsize first_page_num);
144   vsize min_page_count (vsize configuration_index, vsize first_page_num);
145   bool all_lines_stretched (vsize configuration_index);
146   Real blank_page_penalty () const;
147
148   SCM breakpoint_property (vsize breakpoint, char const *str);
149
150   vsize last_break_position () const;
151 private:
152   vector<Break_position> breaks_;
153   vector<Break_position> chunks_;
154   vector<System_spec> system_specs_;
155   vector<Constrained_breaking> line_breaking_;
156   bool ragged_;
157   bool ragged_last_;
158   Real page_top_space_;
159   vsize system_count_;
160
161   vector<Line_division> current_configurations_;
162   vector<Break_position> current_chunks_;
163   vsize current_start_breakpoint_;
164   vsize current_end_breakpoint_;
165
166   void cache_line_details (vsize configuration_index);
167   void clear_line_details_cache ();
168   vsize cached_configuration_index_;
169   vector<Line_details> cached_line_details_;
170   vector<Line_details> uncompressed_line_details_;
171
172   vector<Break_position> chunk_list (vsize start, vsize end);
173   Line_division system_count_bounds (vector<Break_position> const &chunks, bool min);
174   void line_breaker_args (vsize i,
175                           Break_position const &start,
176                           Break_position const &end,
177                           vsize *line_breaker_start,
178                           vsize *line_breaker_end);
179
180   void line_divisions_rec (vsize system_count,
181                            Line_division const &min,
182                            Line_division const &max,
183                            Line_division *cur);
184
185   vector<Line_details> line_details (vsize start, vsize end, Line_division const &div);
186   Page_spacing_result space_systems_on_1_page (vector<Line_details> const &lines, Real page_height, bool ragged);
187   Page_spacing_result space_systems_on_2_pages (vsize configuration_index, vsize first_page_num);
188   Page_spacing_result finalize_spacing_result (vsize configuration_index, Page_spacing_result);
189   void create_system_list ();
190   void find_chunks_and_breaks (Break_predicate);
191 };
192 #endif /* PAGE_BREAKING_HH */