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