]> git.donarmstrong.com Git - lilypond.git/blob - lily/include/page-breaking.hh
formatting nits
[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--2007 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   vsize sys_; /* our index in the all_ list */
45   vsize score_break_; /* if sys_ is a score, then we start at the score_brk_'th
46                          possible page-break in the score */
47   Grob *col_;  /* if sys_ is a score, this points to the broken column */
48   bool score_ender_;
49
50   Break_position (vsize s=VPOS, vsize brk=VPOS, Grob *g=NULL, bool end=false)
51   {
52     sys_ = s;
53     score_break_ = brk;
54     col_ = g;
55     score_ender_ = end;
56   }
57
58   bool operator< (const Break_position &other)
59   {
60     return (sys_ == VPOS && other.sys_ != VPOS)
61       || (sys_ < other.sys_)
62       || (sys_ == other.sys_ && score_break_ < other.score_break_);
63   }
64
65   bool operator<= (const Break_position &other)
66   {
67     return (sys_ == VPOS)
68       || (sys_ < other.sys_ && other.sys_ != VPOS)
69       || (sys_ == other.sys_ && score_break_ <= other.score_break_);
70   }
71 };
72
73 class Page_breaking
74 {
75 public:
76   typedef bool (*Break_predicate) (Grob *);
77   typedef vector<vsize> Line_division;
78   
79   /*
80     TODO: naming.
81
82     determine the page breaking, and break scores into lines
83     appropriately.
84    */
85   virtual SCM solve () = 0;
86
87   Page_breaking (Paper_book *pb, Break_predicate);
88   virtual ~Page_breaking ();
89
90   bool ragged () const;
91   bool ragged_last () const;
92   bool last () const;
93   Real page_height (int page_number, bool last) const;
94
95 protected:
96   Paper_book *book_;
97
98   vsize next_system (Break_position const &break_pos) const;
99
100   SCM make_pages (vector<vsize> lines_per_page, SCM lines);
101
102   vsize min_system_count (vsize start, vsize end);
103   vsize max_system_count (vsize start, vsize end);
104
105
106   void break_into_pieces (vsize start, vsize end, Line_division const &div);
107   SCM systems ();
108
109   void set_current_breakpoints (vsize start,
110                                 vsize end,
111                                 vsize system_count,
112                                 Line_division lower_bound = Line_division (),
113                                 Line_division upper_bound = Line_division ());
114   void set_to_ideal_line_configuration (vsize start, vsize end);
115
116   vsize current_configuration_count () const;
117   Line_division current_configuration (vsize configuration_index) const;
118   Spacing_result space_systems_on_n_pages (vsize configuration_index,
119                                            vsize n, vsize first_page_num);
120   Spacing_result space_systems_on_n_or_one_more_pages (vsize configuration_index, vsize n,
121                                                        vsize first_page_num);
122   Spacing_result space_systems_on_best_pages (vsize configuration_index,
123                                               vsize first_page_num);
124   vsize min_page_count (vsize configuration_index, vsize first_page_num);
125   bool all_lines_stretched (vsize configuration_index);
126   Real blank_page_penalty () const;
127
128   SCM breakpoint_property (vsize breakpoint, char const *str);
129   vector<Break_position> breaks_;
130
131 private:
132   vector<Break_position> chunks_;
133   vector<System_spec> all_;
134   vector<Constrained_breaking> line_breaking_;
135   bool ragged_;
136   bool ragged_last_;
137
138   vector<Line_division> current_configurations_;
139   vector<Break_position> current_chunks_;
140   vsize current_start_breakpoint_;
141   vsize current_end_breakpoint_;
142
143   void cache_line_details (vsize configuration_index);
144   void clear_line_details_cache ();
145   vsize cached_configuration_index_;
146   vector<Line_details> cached_line_details_;
147   vector<Line_details> uncompressed_line_details_;
148
149   vector<Break_position> chunk_list (vsize start, vsize end);
150   Line_division system_count_bounds (vector<Break_position> const &chunks, bool min);
151   void line_breaker_args (vsize i,
152                           Break_position const &start,
153                           Break_position const &end,
154                           vsize *line_breaker_start,
155                           vsize *line_breaker_end);
156
157   void line_divisions_rec (vsize system_count,
158                            Line_division const &min,
159                            Line_division const &max,
160                            Line_division *cur);
161
162   vector<Line_details> line_details (vsize start, vsize end, Line_division const &div);
163   Spacing_result space_systems_on_1_page (vector<Line_details> const &lines, Real page_height, bool ragged);
164   Spacing_result space_systems_on_2_pages (vsize configuration_index, vsize first_page_num);
165   Spacing_result finalize_spacing_result (vsize configuration_index, Spacing_result);
166   void create_system_list ();
167   void find_chunks_and_breaks (Break_predicate);
168 };
169 #endif /* PAGE_BREAKING_HH */