return false;
}
+// FIXME: divide the page breaking into subproblems wherever there is a page break.
+// This will allow us to tweak line-breaking independently on both sides of a
+// page break (currently, we only get to request a number of systems for an entire score
+// at a time), which is necessary for min-systems-per-page and \pageBreak to interact
+// correctly.
Optimal_page_breaking::Optimal_page_breaking (Paper_book *pb)
: Page_breaking (pb, is_break)
{
else
cur = space_systems_on_n_or_one_more_pages (i, page_count-1, first_page_num);
- if (cur.demerits_ < best_for_this_sys_count.demerits_ || isinf (best_for_this_sys_count.demerits_))
+ if (cur.demerits_ < best_for_this_sys_count.demerits_)
{
best_for_this_sys_count = cur;
bound = current_configuration (i);
}
}
- if (best_for_this_sys_count.demerits_ < best.demerits_ || isinf (best.demerits_))
+ if (best_for_this_sys_count.demerits_ < best.demerits_)
{
best = best_for_this_sys_count;
best_division = bound;
}
- /* if the pages are stretched on average, stop trying to reduce sys_count */
- if (best_for_this_sys_count.page_count () < page_count
- && best_for_this_sys_count.average_force () > 0)
- break;
-
+ /* Check to see if we already have too few systems. There are two ways
+ we check this: if we are trying one less than the ideal number of pages
+ and the pages are stretched on average then we have too
+ few systems. If the spacing is worse than BAD_SPACING_PENALTY, then we
+ have too few systems. In either case, though, we need to continue reducing
+ the number of systems if max-systems-per-page requires it. */
+ if (!(best.system_count_status_ & SYSTEM_COUNT_TOO_MANY))
+ {
+ if (best_for_this_sys_count.page_count () < page_count
+ && best_for_this_sys_count.average_force () > 0)
+ break;
- if (isinf (best_for_this_sys_count.demerits_))
- break;
+ if (best_for_this_sys_count.demerits_ >= BAD_SPACING_PENALTY)
+ break;
+ }
}
/* try a larger number of systems than the ideal line breaking number. This
else
cur = space_systems_on_n_pages (i, page_count, first_page_num);
- if (cur.demerits_ < best.demerits_ || isinf (best.demerits_))
+ if (cur.demerits_ < best.demerits_)
{
best = cur;
best_division = current_configuration (i);
}
- if (cur.demerits_ < best_demerits_for_this_sys_count || isinf (best_demerits_for_this_sys_count))
+ if (cur.demerits_ < best_demerits_for_this_sys_count)
{
best_demerits_for_this_sys_count = cur.demerits_;
bound = current_configuration (i);
}
}
- if (isinf (best_demerits_for_this_sys_count))
+ if (best_demerits_for_this_sys_count >= BAD_SPACING_PENALTY
+ && !(best.system_count_status_ & SYSTEM_COUNT_TOO_FEW))
break;
}
ragged_ = to_boolean (pb->paper_->c_variable ("ragged-bottom"));
ragged_last_ = to_boolean (pb->paper_->c_variable ("ragged-last-bottom"));
page_top_space_ = robust_scm2double (pb->paper_->c_variable ("page-top-space"), 0);
- systems_per_page_ = robust_scm2int (pb->paper_->c_variable ("systems-per-page"), 0);
- max_systems_per_page_ = robust_scm2int (pb->paper_->c_variable ("max-systems-per-page"), 0);
- min_systems_per_page_ = robust_scm2int (pb->paper_->c_variable ("min-systems-per-page"), 0);
+ systems_per_page_ = max (0, robust_scm2int (pb->paper_->c_variable ("systems-per-page"), 0));
+ max_systems_per_page_ = max (0, robust_scm2int (pb->paper_->c_variable ("max-systems-per-page"), 0));
+ min_systems_per_page_ = max (0, robust_scm2int (pb->paper_->c_variable ("min-systems-per-page"), 0));
+
+ if (systems_per_page_ && (max_systems_per_page_ || min_systems_per_page_))
+ {
+ warning (_f ("ignoring min-systems-per-page and max-systems-per-page because systems-per-page was set"));
+ min_systems_per_page_ = max_systems_per_page_ = 0;
+ }
+ if (max_systems_per_page_ && min_systems_per_page_ > max_systems_per_page_)
+ {
+ warning (_f ("min-systems-per-page is larger than max-systems-per-page, ignoring both values"));
+ min_systems_per_page_ = max_systems_per_page_ = 0;
+ }
create_system_list ();
find_chunks_and_breaks (is_break);
return 0;
}
+int
+Page_breaking::line_count_status (int line_count) const
+{
+ if (too_many_lines (line_count))
+ return SYSTEM_COUNT_TOO_MANY;
+ if (too_few_lines (line_count))
+ return SYSTEM_COUNT_TOO_FEW;
+
+ return SYSTEM_COUNT_OK;
+}
+
/* translate indices into breaks_ into start-end parameters for the line breaker */
void
Page_breaking::line_breaker_args (vsize sys,
+ ((cur_rod_height > 0) ? cached_line_details_[i].padding_: 0);
Real next_spring_height = cur_spring_height + cached_line_details_[i].space_;
Real next_height = next_rod_height + (ragged () ? next_spring_height : 0);
+ int next_line_count = line_count + cached_line_details_[i].compressed_nontitle_lines_count_;
- line_count += cached_line_details_[i].compressed_nontitle_lines_count_;
-
- if ((next_height > cur_page_height && cur_rod_height > 0)
- || too_many_lines (line_count)
+ if ((!too_few_lines (line_count) && (next_height > cur_page_height && cur_rod_height > 0))
+ || too_many_lines (next_line_count)
|| (i > 0
&& cached_line_details_[i-1].page_permission_ == ly_symbol2scm ("force")))
{
{
cur_rod_height = next_rod_height;
cur_spring_height = next_spring_height;
+ line_count = next_line_count;
}
}
cur_page_height = page_height (first_page_num + ret - 1, true);
Real cur_height = cur_rod_height + ((ragged_last () || ragged ()) ? cur_spring_height : 0);
- if (cur_height > cur_page_height
+ if (!too_few_lines (line_count - cached_line_details_.back ().compressed_nontitle_lines_count_)
+ && cur_height > cur_page_height
/* don't increase the page count if the last page had only one system */
&& cur_rod_height > cached_line_details_.back ().extent_.length ())
ret++;
Page_spacing_result
Page_breaking::pack_systems_on_least_pages (vsize configuration, vsize first_page_num)
{
+ // TODO: add support for min/max-systems-per-page.
Page_spacing_result res;
vsize page = 0;
vsize page_first_line = 0;
Real line_force = 0;
Real line_penalty = 0;
- Real page_force = 0;
+ Real page_demerits = res.penalty_;
Real page_weighting = robust_scm2double (book_->paper_->c_variable ("page-spacing-weight"), 10);
for (vsize i = 0; i < uncompressed_line_details_.size (); i++)
for (vsize i = 0; i < res.force_.size (); i++)
{
Real f = res.force_[i];
- if (isinf (f) && res.systems_per_page_[i] == 1)
- page_force += BAD_SPACING_PENALTY;
- else
- page_force += f * f;
+
+ page_demerits += min(f*f, BAD_SPACING_PENALTY);
}
/* for a while we tried averaging page and line forces across pages instead
with a very bad page force (for example because of a forced page break),
the page breaker will put in a _lot_ of pages so that the bad force
becomes averaged out over many pages. */
- res.demerits_ = line_force + line_penalty + (page_force + res.penalty_) * page_weighting;
+ res.demerits_ = line_force + line_penalty + page_demerits * page_weighting;
return res;
}
ret.systems_per_page_.push_back (lines.size ());
ret.force_.push_back (ragged ? min (space.force_, 0.0) : space.force_);
ret.penalty_ = line_count_penalty (line_count) + lines.back ().page_penalty_ + lines.back ().turn_penalty_;
+ ret.system_count_status_ |= line_count_status (line_count);
/* don't do finalize_spacing_result () because we are only an internal function */
return ret;
// on min-systems-per-page and max-systems-per-page.
vector<Real> page1_penalty;
vector<Real> page2_penalty;
+
+ // page1_status and page2_status keep track of whether the min-systems-per-page
+ // and max-systems-per-page constraints are satisfied.
+ vector<int> page1_status;
+ vector<int> page2_status;
+
Page_spacing page1 (page1_height, page_top_space_);
Page_spacing page2 (page2_height, page_top_space_);
int page1_line_count = 0;
page2_force.resize (cached_line_details_.size () - 1, infinity_f);
page1_penalty.resize (cached_line_details_.size () - 1, infinity_f);
page2_penalty.resize (cached_line_details_.size () - 1, infinity_f);
+ page1_status.resize (cached_line_details_.size () - 1, 0);
+ page2_status.resize (cached_line_details_.size () - 1, 0);
/* find the page 1 and page 2 forces for each page-breaking position */
for (vsize i = 0; i < page1_force.size (); i++)
page1_force[i] = (ragged1 && page1.force_ < 0 && i > 0) ? infinity_f : page1.force_;
page1_penalty[i] = line_count_penalty (page1_line_count);
+ page1_status[i] = line_count_status (page1_line_count);
if (ragged2)
page2_force[page2_force.size () - 1 - i] =
else
page2_force[page2_force.size () - 1 - i] = page2.force_;
page2_penalty[page2_penalty.size () - 1 - i] = line_count_penalty (page2_line_count);
+ page2_status[page2_penalty.size () - 1 - i] = line_count_status (page2_line_count);
}
/* find the position that minimises the sum of the page forces */
for (vsize i = 0; i < page1_force.size (); i++)
{
Real f = page1_force[i] * page1_force[i] + page2_force[i] * page2_force[i];
- // FIXME: Why do we look at unevenness here? We don't use it in
- // finalize_spacing_result, so this gives us a different measure of demerits.
- Real uneven = 2 * (page1_force[i] - page2_force[i]);
// NOTE: we treat max-systems-per-page and min-systems-per-page as soft
// constraints. That is, we penalize harshly when they don't happen
// but we don't completely reject.
- Real dem = uneven * uneven + f
+ Real dem = f
+ page1_penalty[i] + page2_penalty[i]
+ cached_line_details_[i+1].page_penalty_
+ cached_line_details_.back ().page_penalty_ + cached_line_details_.back ().turn_penalty_;
ret.systems_per_page_.push_back (cached_line_details_.size () - best_sys_count);
ret.force_.push_back (page1_force[best_sys_count-1]);
ret.force_.push_back (page2_force[best_sys_count-1]);
+ ret.system_count_status_ = page1_status[best_sys_count-1] | page2_status[best_sys_count-1];
ret.penalty_ = cached_line_details_[best_sys_count-1].page_penalty_
+ cached_line_details_.back ().page_penalty_
+ cached_line_details_.back ().turn_penalty_
for (vsize page_start = line+1; page_start > page && page_start--;)
{
Page_spacing_node const *prev = page > 0 ? &state_.at (page_start-1, page-1) : 0;
- line_count += lines_[page_start].compressed_nontitle_lines_count_;
space.prepend_system (lines_[page_start]);
- // FIXME: The following line doesn't respect min-systems-per-page. More
- // generally, what should we do when respecting min-systems-per-page
- // would cause a page to be overfull?
- if (page_start < line && (isinf (space.force_) || (space.force_ < 0 && ragged)))
+
+ // This 'if' statement is a little hard to parse. It won't consider this configuration
+ // if it is overfull unless the current configuration is the first one with this start
+ // point. We also make an exception (and consider this configuration) if the previous
+ // configuration we tried had fewer lines than min-systems-per-page.
+ if (!breaker_->too_few_lines (line_count)
+ && page_start < line
+ && (isinf (space.force_) || (space.force_ < 0 && ragged)))
break;
+ line_count += lines_[page_start].compressed_nontitle_lines_count_;
if (page > 0 || page_start == 0)
{
if (line == lines_.size () - 1 && ragged && last && space.force_ > 0)
Real penalty = breaker_->line_count_penalty (line_count);
if (page_start > 0)
- penalty = lines_[page_start-1].page_penalty_
+ penalty += lines_[page_start-1].page_penalty_
+ (page % 2 == 0) ? lines_[page_start-1].turn_penalty_ : 0;
demerits += penalty;
cur.demerits_ = demerits;
cur.force_ = space.force_;
cur.penalty_ = penalty + (prev ? prev->penalty_ : 0);
+ cur.system_count_status_ = breaker_->line_count_status (line_count)
+ | (prev ? prev->system_count_status_ : 0);
cur.prev_ = page_start - 1;
}
}