2 page-spacing.cc - implement routines for spacing
3 systems vertically on pages
5 source file of the GNU LilyPond music typesetter
7 (c) 2006 Joe Neeman <joeneeman@gmail.com>
10 #include "page-spacing.hh"
16 A much simplified rods-and-springs problem.
24 Real inverse_spring_k_;
26 Line_details last_line_;
28 Page_spacing (Real page_height)
30 page_height_ = page_height;
36 void append_system (const Line_details &line);
37 void prepend_system (const Line_details &line);
42 Page_spacing::calc_force ()
44 if (rod_height_ + last_line_.bottom_padding_ >= page_height_ || !inverse_spring_k_)
47 force_ = (page_height_ - rod_height_ - last_line_.bottom_padding_ - spring_len_) / inverse_spring_k_;
51 Page_spacing::append_system (const Line_details &line)
53 rod_height_ += last_line_.padding_;
55 rod_height_ += line.extent_.length ();
56 spring_len_ += line.space_;
57 inverse_spring_k_ += line.inverse_hooke_;
65 Page_spacing::prepend_system (const Line_details &line)
68 rod_height_ += line.padding_;
72 rod_height_ += line.extent_.length ();
73 spring_len_ += line.space_;
74 inverse_spring_k_ += line.inverse_hooke_;
80 Page_spacing::clear ()
82 force_ = rod_height_ = spring_len_ = 0;
83 inverse_spring_k_ = 0;
86 /* for each forbidden page break, merge the systems around it into one system. */
87 static vector<Line_details>
88 compress_lines (const vector<Line_details> &orig)
90 vector<Line_details> ret;
92 for (vsize i = 0; i < orig.size (); i++)
94 if (ret.size () && ret.back ().page_permission_ == SCM_EOL)
96 Line_details const &old = ret.back ();
97 Line_details compressed = orig[i];
98 compressed.extent_[DOWN] = old.extent_[DOWN];
99 compressed.extent_[UP] = old.extent_[UP] + orig[i].extent_.length () + old.padding_;
100 compressed.space_ += old.space_;
101 compressed.inverse_hooke_ += old.inverse_hooke_;
103 /* we don't need the force_ field for the vertical spacing,
104 so we use force_ = n to signal that the line was compressed,
105 reducing the number of lines by n (and force_ = 0 otherwise).
106 This makes uncompression much easier. */
107 compressed.force_ = old.force_ + 1;
108 ret.back () = compressed;
112 ret.push_back (orig[i]);
113 ret.back ().force_ = 0;
119 /* translate the number of systems-per-page into something meaningful for
120 the uncompressed lines.
123 uncompress_solution (vector<vsize> const &systems_per_page,
124 vector<Line_details> const &compressed)
129 for (vsize i = 0; i < systems_per_page.size (); i++)
131 int compressed_count = 0;
132 for (vsize j = start_sys; j < start_sys + systems_per_page[i]; j++)
133 compressed_count += (int)compressed[j].force_;
135 ret.push_back (systems_per_page[i] + compressed_count);
136 start_sys += systems_per_page[i];
141 /* the cases for page_count = 1 or 2 can be done in O(n) time. Since they
142 are by far the most common cases, we have special functions for them */
143 static Spacing_result
144 space_systems_on_1_page (vector<Line_details> const &lines, Real page_height, bool ragged)
146 Page_spacing space (page_height);
149 for (vsize i = 0; i < lines.size (); i++)
150 space.append_system (lines[i]);
152 ret.systems_per_page_.push_back (lines.size ());
153 ret.force_.push_back (ragged ? min (space.force_, 0.0) : space.force_);
154 ret.penalty_ = lines.back ().page_penalty_ + lines.back ().turn_penalty_;
155 ret.demerits_ = ret.force_.back () * ret.force_.back () + ret.penalty_;
160 static Spacing_result
161 space_systems_on_2_pages (vector<Line_details> const &lines,
166 /* if there is a forced break, this reduces to 2 1-page problems */
167 for (vsize i = 0; i < lines.size () - 1; i++)
168 if (lines[i].page_permission_ == ly_symbol2scm ("force"))
170 vector<Line_details> lines1 (lines.begin (), lines.begin () + i + 1);
171 vector<Line_details> lines2 (lines.begin () + i + 1, lines.end ());
172 Spacing_result p1 = space_systems_on_1_page (lines1, page_height, ragged);
173 Spacing_result p2 = space_systems_on_1_page (lines2, page_height, ragged || ragged_last);
175 p1.systems_per_page_.push_back (p2.systems_per_page_[0]);
176 p1.force_.push_back (p2.force_[0]);
177 p1.penalty_ += p2.penalty_ - lines[i].turn_penalty_;
178 p1.demerits_ += p2.demerits_ - lines[i].turn_penalty_;
182 vector<Real> page1_force;
183 vector<Real> page2_force;
184 Page_spacing page1 (page_height);
185 Page_spacing page2 (page_height);
187 page1_force.resize (lines.size () - 1, infinity_f);
188 page2_force.resize (lines.size () - 1, infinity_f);
190 for (vsize i = 0; i < page1_force.size (); i++)
192 page1.append_system (lines[i]);
193 page2.prepend_system (lines[lines.size () - 1 - i]);
194 page1_force[i] = (ragged && page1.force_ < 0 && i > 0) ? infinity_f : page1.force_;
196 if (ragged || ragged_last)
197 page2_force[page2_force.size () - 1 - i] =
198 (page2.force_ < 0 && i < page1_force.size () - 1) ? infinity_f : 0;
200 page2_force[page2_force.size () - 1 - i] = page2.force_;
203 vsize best_sys_count = 1;
204 Real best_demerits = infinity_f;
205 for (vsize i = 0; i < page1_force.size (); i++)
207 Real dem = page1_force[i] * page1_force[i]
208 + page2_force[i] * page2_force[i]
209 + lines[i+1].page_penalty_
210 + lines.back ().page_penalty_ + lines.back ().turn_penalty_;
211 if (dem < best_demerits)
214 best_sys_count = i+1;
219 ret.systems_per_page_.push_back (best_sys_count);
220 ret.systems_per_page_.push_back (lines.size () - best_sys_count);
221 ret.force_.push_back (page1_force[best_sys_count-1]);
222 ret.force_.push_back (page2_force[best_sys_count-1]);
223 ret.penalty_ = lines[best_sys_count-1].page_penalty_
224 + lines.back ().page_penalty_
225 + lines.back ().turn_penalty_;
226 ret.demerits_ = best_demerits;
231 Page_spacer::Page_spacer (vector<Line_details> const &lines, Real page_height, bool ragged, bool ragged_last)
234 page_height_ = page_height;
237 ragged_last_ = ragged_last;
241 Page_spacer::solve (vsize page_count)
243 if (page_count > max_page_count_)
247 ret.force_.resize (page_count);
248 ret.systems_per_page_.resize (page_count);
250 vsize system = lines_.size () - 1;
252 if (isinf (state_.at (system, page_count-1).demerits_))
254 programming_error ("tried to space systems on a bad number of pages");
255 return Spacing_result (); /* bad number of pages */
258 ret.penalty_ = state_.at (system, page_count-1).penalty_
259 + lines_.back ().page_penalty_ + lines_.back ().turn_penalty_;
262 for (vsize p = page_count; p--;)
264 assert (system != VPOS);
266 Page_spacing_node const &ps = state_.at (system, p);
267 ret.force_[p] = ps.force_;
268 ret.demerits_ += ps.force_ * ps.force_;
270 ret.systems_per_page_[p] = system + 1;
272 ret.systems_per_page_[p] = system - ps.prev_;
275 ret.demerits_ += ret.penalty_;
280 Page_spacer::resize (vsize page_count)
282 assert (page_count > 0);
284 if (max_page_count_ >= page_count)
287 state_.resize (lines_.size (), page_count, Page_spacing_node ());
288 for (vsize page = max_page_count_; page < page_count; page++)
289 for (vsize line = page; line < lines_.size (); line++)
290 if (!calc_subproblem (page, line))
293 max_page_count_ = page_count;
297 Page_spacer::calc_subproblem (vsize page, vsize line)
299 Page_spacing space (page_height_);
300 Page_spacing_node &cur = state_.at (line, page);
301 bool ragged = ragged_ || (ragged_last_ && line == lines_.size () - 1);
303 for (vsize page_start = line+1; page_start > page && page_start--;)
305 Page_spacing_node const *prev = page > 0 ? &state_.at (page_start-1, page-1) : 0;
307 space.prepend_system (lines_[page_start]);
308 if (page_start < line && (isinf (space.force_) || (space.force_ < 0 && ragged)))
311 if (page > 0 || page_start == 0)
313 if (line == lines_.size () - 1 && ragged_last_ && space.force_ > 0)
316 /* we may have to deal with single lines that are taller than a page */
317 if (isinf (space.force_) && page_start == line)
318 space.force_ = -200000;
320 Real dem = fabs (space.force_) + (prev ? prev->demerits_ : 0);
323 penalty = lines_[page_start-1].page_penalty_
324 + (page % 2 == 0) ? lines_[page_start-1].turn_penalty_ : 0;
327 if (dem < cur.demerits_ || page_start == line)
330 cur.force_ = space.force_;
331 cur.penalty_ = penalty + (prev ? prev->penalty_ : 0);
332 cur.prev_ = page_start - 1;
337 && lines_[page_start-1].page_permission_ == ly_symbol2scm ("force"))
340 return !isinf (cur.demerits_);
344 min_page_count (vector<Line_details> const &uncompressed_lines,
345 Real page_height, bool ragged, bool ragged_last)
348 Real cur_rod_height = 0;
349 vector<Line_details> lines = compress_lines (uncompressed_lines);
351 assert (lines.size ());
352 for (vsize i = lines.size (); i--;)
354 bool rag = ragged || (ragged_last && ret == 1);
355 Real ext_len = lines[i].extent_.length ();
356 Real next_height = cur_rod_height + ext_len
357 + (rag ? lines[i].space_ : 0)
358 + ((cur_rod_height > 0) ? lines[i].padding_: 0);
360 if ((next_height > page_height && cur_rod_height > 0)
361 || (i < lines.size () - 1 && lines[i].page_permission_ == ly_symbol2scm ("force")))
364 cur_rod_height = ext_len + (rag ? lines[i].space_ : 0);
367 cur_rod_height = next_height;
374 space_systems_on_n_pages (vector<Line_details> const &lines,
380 vector<Line_details> compressed_lines = compress_lines (lines);
382 assert (n >= min_page_count (lines, page_height, ragged, ragged_last));
384 if (n > compressed_lines.size ())
385 return Spacing_result ();
387 ret = space_systems_on_1_page (compressed_lines, page_height, ragged || ragged_last);
389 ret = space_systems_on_2_pages (compressed_lines, page_height, ragged, ragged_last);
391 Page_spacer ps (compressed_lines, page_height, ragged, ragged_last);
394 ret.systems_per_page_ = uncompress_solution (ret.systems_per_page_, compressed_lines);
399 space_systems_on_n_or_one_more_pages (vector<Line_details> const &lines,
402 Real odd_pages_penalty,
406 Spacing_result n_res = space_systems_on_n_pages (lines, n, page_height, ragged, ragged_last);
407 Spacing_result m_res = space_systems_on_n_pages (lines, n+1, page_height, ragged, ragged_last);
408 n_res.demerits_ += odd_pages_penalty;
409 n_res.force_.back () += odd_pages_penalty;
411 if (n_res.demerits_ < m_res.demerits_)
417 space_systems_on_best_pages (vector<Line_details> const &lines,
419 Real odd_pages_penalty,
423 vector<Line_details> compressed_lines = compress_lines (lines);
424 vsize min_p_count = min_page_count (compressed_lines, page_height, ragged, ragged_last);
426 Page_spacer ps (compressed_lines, page_height, ragged, ragged_last);
427 Spacing_result best = ps.solve (min_p_count);
428 best.force_.back () += (min_p_count % 2) ? odd_pages_penalty : 0;
429 best.demerits_ += (min_p_count % 2) ? odd_pages_penalty : 0;
431 for (vsize i = min_p_count+1; i <= compressed_lines.size (); i++)
433 Spacing_result cur = ps.solve (i);
434 cur.demerits_ += (i % 2) ? odd_pages_penalty : 0;
435 if (cur.demerits_ < best.demerits_)
439 best.systems_per_page_ = uncompress_solution (best.systems_per_page_, compressed_lines);