/*
page-breaking.cc -- implement a superclass and utility
- functions for use by various page-breaking algorithms
+ functions shared by various page-breaking algorithms
source file of the GNU LilyPond music typesetter
- (c) 2006 Joe Neeman <joeneeman@gmail.com>
+ (c) 2006--2007 Joe Neeman <joeneeman@gmail.com>
*/
#include "page-breaking.hh"
#include "system.hh"
#include "warn.hh"
-System_spec::System_spec (Paper_score *ps, int break_count)
-{
- pscore_ = ps;
- prob_ = 0;
- score_break_count_ = break_count;
-}
-
-System_spec::System_spec (Prob *pb)
-{
- pscore_ = 0;
- prob_ = pb;
- score_break_count_ = 0;
-}
-
-System_spec::System_spec ()
-{
- pscore_ = 0;
- prob_ = 0;
- score_break_count_ = 0;
-}
-
/* for Page_breaking, the start index (when we are dealing with the stuff
between a pair of breakpoints) refers to the break_ index of the end of
the previous page. So the system index of the start of the current page
it. */
/* Turn a break index into the sys index that starts the next page */
-vsize Page_breaking::next_system (vsize start) const
+vsize
+Page_breaking::next_system (Break_position const &break_pos) const
{
- vsize sys = breaks_[start].sys_;
+ vsize sys = break_pos.sys_;
- if (sys == VPOS) /* beginning of the piece */
+ if (sys == VPOS) /* beginning of the book */
return 0;
- if (all_[sys].pscore_ && all_[sys].score_break_count_ > breaks_[start].score_break_)
+ if (all_[sys].pscore_ && !break_pos.score_ender_)
return sys; /* the score overflows the previous page */
return sys + 1; /* this page starts with a new sys */
}
-Page_breaking::Page_breaking (Paper_book *pb, bool allow_intra_score_breaks)
+Page_breaking::Page_breaking (Paper_book *pb, Break_predicate is_break)
{
book_ = pb;
- create_system_list (allow_intra_score_breaks);
+ create_system_list ();
+ find_chunks_and_breaks (is_break);
}
Page_breaking::~Page_breaking ()
/* translate indices into breaks_ into start-end parameters for the line breaker */
void
-Page_breaking::line_breaker_args (vsize i, vsize *start, vsize *end)
+Page_breaking::line_breaker_args (vsize sys,
+ Break_position const &start,
+ Break_position const &end,
+ vsize *line_breaker_start,
+ vsize *line_breaker_end)
{
- assert (all_[i].pscore_);
- assert (next_system (*start) <= i && i <= breaks_[*end].sys_);
+ assert (all_[sys].pscore_);
+ assert (next_system (start) <= sys && sys <= end.sys_);
- vsize start_col = 0;
- vsize end_col = VPOS;
+ if (start.sys_ == sys)
+ *line_breaker_start = start.score_break_;
+ else
+ *line_breaker_start = 0;
- if (breaks_[*start].sys_ == i)
- start_col = breaks_[*start].score_break_;
- if (breaks_[*end].sys_ == i)
- end_col = breaks_[*end].score_break_;
+ if (end.sys_ == sys)
+ *line_breaker_end = end.score_break_;
+ else
+ *line_breaker_end = VPOS;
+}
- assert (end_col && (end_col == VPOS || end_col <= all_[breaks_[*end].sys_].score_break_count_));
- *start = start_col;
- *end = end_col;
+void
+Page_breaking::break_into_pieces (vsize start_break, vsize end_break, Line_division const &div)
+{
+ vector<Break_position> chunks = chunk_list (start_break, end_break);
+ assert (chunks.size () == div.size () + 1);
+
+ for (vsize i = 0; i + 1 < chunks.size (); i++)
+ {
+ vsize sys = next_system (chunks[i]);
+ if (all_[sys].pscore_)
+ {
+ vsize start;
+ vsize end;
+ line_breaker_args (sys, chunks[i], chunks[i+1], &start, &end);
+
+ vector<Column_x_positions> pos = line_breaking_[sys].solve (start, end, div[i]);
+ all_[sys].pscore_->root_system ()->break_into_pieces (pos);
+ }
+ }
}
-vector<Column_x_positions>
-Page_breaking::get_line_breaks (vsize i, vsize sys_count, vsize start, vsize end)
+SCM
+Page_breaking::systems ()
{
- assert (all_[i].pscore_);
- line_breaker_args (i, &start, &end);
- return line_breaking_[i].get_solution (start, end, sys_count);
+ SCM ret = SCM_EOL;
+ for (vsize sys = 0; sys < all_.size (); sys++)
+ {
+ if (all_[sys].pscore_)
+ {
+ all_[sys].pscore_->root_system ()->do_break_substitution_and_fixup_refpoints ();
+ SCM lines = all_[sys].pscore_->root_system ()->get_broken_system_grobs ();
+ ret = scm_cons (lines, ret);
+ }
+ else
+ {
+ Prob *pb = all_[sys].prob_;
+ ret = scm_cons (scm_list_1 (pb->self_scm ()), ret);
+ pb->unprotect ();
+ }
+ }
+ return scm_append (scm_reverse (ret));
}
vector<Line_details>
-Page_breaking::get_line_details (vsize start_break, vsize end_break, vector<vsize> const &div)
+Page_breaking::line_details (vsize start_break, vsize end_break, Line_division const &div)
{
+ vector<Break_position> chunks = chunk_list (start_break, end_break);
vector<Line_details> ret;
+ assert (chunks.size () == div.size () + 1);
- for (vsize i = next_system (start_break); i <= breaks_[end_break].sys_; i++)
+ for (vsize i = 0; i + 1 < chunks.size (); i++)
{
- if (all_[i].pscore_)
+ vsize sys = next_system (chunks[i]);
+ if (all_[sys].pscore_)
{
- vsize div_index = i - next_system (start_break);
- vsize start = start_break;
- vsize end = end_break;
+ vsize start;
+ vsize end;
+ line_breaker_args (sys, chunks[i], chunks[i+1], &start, &end);
- line_breaker_args (i, &start, &end);
- vector<Line_details> l = line_breaking_[i].get_details (start, end, div[div_index]);
- ret.insert (ret.end (), l.begin (), l.end ());
+ vector<Line_details> details = line_breaking_[sys].line_details (start, end, div[i]);
+ ret.insert (ret.end (), details.begin (), details.end ());
}
else
- ret.push_back (Line_details (all_[i].prob_));
+ {
+ assert (div[i] == 1);
+ ret.push_back (Line_details (all_[sys].prob_));
+ }
}
return ret;
}
-vsize
-Page_breaking::get_min_systems (vsize i, vsize start, vsize end)
-{
- line_breaker_args (i, &start, &end);
- return line_breaking_[i].get_min_systems (start, end);
-}
-
-vsize
-Page_breaking::get_max_systems (vsize i, vsize start, vsize end)
-{
- line_breaker_args (i, &start, &end);
- return line_breaking_[i].get_max_systems (start, end);
-}
-
Real
Page_breaking::page_height (int page_num, bool last)
{
return scm_to_double (height) - scm_to_double (book_->paper_->c_variable ("page-top-space"));
}
+SCM
+Page_breaking::breakpoint_property (vsize breakpoint, char const *str)
+{
+ Break_position const &pos = breaks_[breakpoint];
+
+ if (pos.sys_ == VPOS)
+ return SCM_EOL;
+ if (all_[pos.sys_].pscore_)
+ return pos.col_->get_property (str);
+ return all_[pos.sys_].prob_->get_property (str);
+}
+
SCM
Page_breaking::make_pages (vector<vsize> lines_per_page, SCM systems)
{
- SCM module = scm_c_resolve_module ("scm layout-page-layout");
- SCM make_page = scm_c_module_lookup (module, "make-page-from-systems");
+ SCM layout_module = scm_c_resolve_module ("scm layout-page-layout");
+ SCM page_module = scm_c_resolve_module ("scm page");
+
+ SCM make_page = scm_c_module_lookup (layout_module, "stretch-and-draw-page");
+ SCM page_stencil = scm_c_module_lookup (page_module, "page-stencil");
make_page = scm_variable_ref (make_page);
+ page_stencil = scm_variable_ref (page_stencil);
+
SCM book = book_->self_scm ();
bool ragged_all = to_boolean (book_->paper_->c_variable ("ragged-bottom"));
bool ragged_last = to_boolean (book_->paper_->c_variable ("ragged-last-bottom"));
+ int first_page_number = robust_scm2int (book_->paper_->c_variable ("first-page-number"), 1);
SCM ret = SCM_EOL;
for (vsize i = 0; i < lines_per_page.size (); i++)
{
- SCM page_num = scm_from_int (i + 1);
+ SCM page_num = scm_from_int (i + first_page_number);
SCM last = scm_from_bool (i == lines_per_page.size () - 1);
SCM ragged = scm_from_bool (ragged_all || (to_boolean (last) && ragged_last));
SCM line_count = scm_from_int (lines_per_page[i]);
SCM page = scm_apply_0 (make_page,
scm_list_n (book, lines, page_num, ragged, last, SCM_UNDEFINED));
+ scm_apply_1 (page_stencil, page, SCM_EOL);
ret = scm_cons (page, ret);
systems = scm_list_tail (systems, line_count);
}
- return scm_reverse (ret);
+ ret = scm_reverse (ret);
+ return ret;
}
-/* if allow_intra_score_breaks is false, that doesn't necessarily mean that there will
- be no page turns in the middle of a score, only that we don't give special
- consideration to any internal part of a score.
+/* The page-turn-page-breaker needs to have a line-breaker between any two
+ columns with non-NULL page-turn-permission.
+
+ The optimal-breaker needs to have a line-breaker between any two columns
+ with page-break-permission = 'force.
- Corollary: if allow_intra_score_breaks is false, any \pageTurn or \noPageTurn commands
- in the middle of a score will be ignored.
+ By using a grob predicate, we can accommodate both of these uses.
*/
void
-Page_breaking::create_system_list (bool allow_intra_score_breaks)
+Page_breaking::create_system_list ()
{
- breaks_.push_back (Break_position ());
-
SCM specs = book_->get_system_specs ();
- for (SCM s = specs; s != SCM_EOL; s = scm_cdr (s))
+ for (SCM s = specs; scm_is_pair (s); s = scm_cdr (s))
{
if (Paper_score *ps = dynamic_cast<Paper_score*> (unsmob_music_output (scm_car (s))))
- {
- /* add a breakpoint at the end of the last score, if necessary */
- if (all_.size () && all_.back ().pscore_)
- breaks_.push_back (Break_position (all_.size () - 1,
- all_.back ().score_break_count_));
-
- vector<vsize> score_brk;
- if (allow_intra_score_breaks)
- score_brk = find_page_break_indices (ps);
-
- all_.push_back (System_spec (ps, score_brk.size () + 1));
-
- for (vsize i = 0; i < score_brk.size(); i++)
- breaks_.push_back (Break_position (all_.size () - 1, i + 1));
-
- /* include a line breaker at the start of the score */
- score_brk.insert (score_brk.begin (), 0);
- line_breaking_.push_back (Constrained_breaking (score_brk));
- line_breaking_.back ().set_pscore (ps);
- }
+ {
+ SCM system_count = ps->layout ()->c_variable ("system-count");
+
+ if (scm_is_number (system_count))
+ s = scm_append (scm_list_3 (scm_list_1 (scm_car (s)),
+ scm_vector_to_list (ps->get_paper_systems ()),
+ scm_cdr (s)));
+ else
+ all_.push_back (System_spec (ps));
+ }
else
{
Prob *pb = unsmob_prob (scm_car (s));
assert (pb);
pb->protect ();
- // ignore penalties (from break-before) in favour of using \pageBreak at the
- // end of the score
- if (all_.size () && all_.back ().pscore_)
- breaks_.push_back (Break_position (all_.size () - 1, all_.back ().score_break_count_));
all_.push_back (System_spec (pb));
- line_breaking_.push_back (Constrained_breaking ());
}
}
+}
- /* add the ending breakpoint */
- if (all_.back ().pscore_)
- breaks_.push_back (Break_position (all_.size () - 1, all_.back ().score_break_count_));
- else
- breaks_.push_back (Break_position (all_.size () - 1));
+void
+Page_breaking::find_chunks_and_breaks (Break_predicate is_break)
+{
+ SCM force_sym = ly_symbol2scm ("force");
+
+ chunks_.push_back (Break_position ());
+ breaks_.push_back (Break_position ());
+
+ for (vsize i = 0; i < all_.size (); i++)
+ {
+ if (all_[i].pscore_)
+ {
+ vector<Grob*> cols = all_[i].pscore_->root_system ()->used_columns ();
+ vector<vsize> line_breaker_columns;
+ line_breaker_columns.push_back (0);
+
+ for (vsize j = 1; j < cols.size (); j++)
+ {
+ bool last = j == cols.size () - 1;
+ bool break_point = is_break (cols[j]);
+ bool chunk_end = cols[j]->get_property ("page-break-permission") == force_sym;
+ Break_position cur_pos = Break_position (i,
+ line_breaker_columns.size (),
+ cols[j],
+ last);
+
+ if (break_point || (i == all_.size () - 1 && last))
+ breaks_.push_back (cur_pos);
+ if (chunk_end || last)
+ chunks_.push_back (cur_pos);
+
+ if ((break_point || chunk_end) && !last)
+ line_breaker_columns.push_back (j);
+ }
+ line_breaking_.push_back (Constrained_breaking (all_[i].pscore_, line_breaker_columns));
+ }
+ else
+ {
+ /* TODO: we want some way of applying Break_p to a prob? */
+ if (i == all_.size () - 1)
+ breaks_.push_back (Break_position (i));
+
+ chunks_.push_back (Break_position (i));
+ line_breaking_.push_back (Constrained_breaking (NULL));
+ }
+ }
}
-vector<vsize>
-Page_breaking::find_page_break_indices (Paper_score *pscore)
+vector<Break_position>
+Page_breaking::chunk_list (vsize start_index, vsize end_index)
{
- vector<Grob*> all = pscore->root_system ()->columns ();
- vector<vsize> ret;
+ Break_position start = breaks_[start_index];
+ Break_position end = breaks_[end_index];
+
+ vsize i;
+ for (i = 0; i < chunks_.size () && chunks_[i] <= start; i++)
+ ;
+
+ vector<Break_position> ret;
+ ret.push_back (start);
+ for (; i < chunks_.size () && chunks_[i] < end; i++)
+ ret.push_back (chunks_[i]);
+ ret.push_back (end);
+ return ret;
+}
- /* we don't include breaks at the beginning and end */
- for (vsize i = 0; i < all.size () - 1; i++)
- if (scm_is_symbol (all[i]->get_property ("page-turn-permission")))
- ret.push_back(i);
+vsize
+Page_breaking::min_system_count (vsize start, vsize end)
+{
+ vector<Break_position> chunks = chunk_list (start, end);
+ Line_division div = system_count_bounds (chunks, true);
+ vsize ret = 0;
+ for (vsize i = 0; i < div.size (); i++)
+ ret += div[i];
return ret;
}
-void
-Page_breaking::calc_system_count_bounds (vsize start, vsize end,
- vector<vsize> *min,
- vector<vsize> *max)
+vsize
+Page_breaking::max_system_count (vsize start, vsize end)
{
- for (vsize i = next_system (start); i <= breaks_[end].sys_; i++)
+ vector<Break_position> chunks = chunk_list (start, end);
+ Line_division div = system_count_bounds (chunks, false);
+ vsize ret = 0;
+
+ for (vsize i = 0; i < div.size (); i++)
+ ret += div[i];
+ return ret;
+}
+
+Page_breaking::Line_division
+Page_breaking::system_count_bounds (vector<Break_position> const &chunks, bool min)
+{
+ assert (chunks.size () >= 2);
+
+ Line_division ret;
+ ret.resize (chunks.size () - 1, 1);
+
+ for (vsize i = 0; i + 1 < chunks.size (); i++)
{
- if (all_[i].pscore_)
- {
- min->push_back (get_min_systems (i, start, end));
- max->push_back (get_max_systems (i, start, end));
- }
- else
- {
- min->push_back (1);
- max->push_back (1);
- }
+ vsize sys = next_system (chunks[i]);
+ if (all_[sys].pscore_)
+ {
+ vsize start;
+ vsize end;
+ line_breaker_args (sys, chunks[i], chunks[i+1], &start, &end);
+ ret[i] = min
+ ? line_breaking_[sys].min_system_count (start, end)
+ : line_breaking_[sys].max_system_count (start, end);
+ }
}
+
+ return ret;
+}
+
+vector<Page_breaking::Line_division>
+Page_breaking::line_divisions (vsize start,
+ vsize end,
+ vsize system_count,
+ Line_division lower_bound,
+ Line_division upper_bound)
+{
+ vector<Break_position> chunks = chunk_list (start, end);
+
+ if (!lower_bound.size ())
+ lower_bound = system_count_bounds (chunks, true);
+ if (!upper_bound.size ())
+ upper_bound = system_count_bounds (chunks, false);
+
+ assert (lower_bound.size () == chunks.size () - 1);
+ assert (upper_bound.size () == chunks.size () - 1);
+
+ vector<Line_division> ret;
+ Line_division work_in_progress;
+
+ line_divisions_rec (system_count,
+ lower_bound,
+ upper_bound,
+ &ret,
+ &work_in_progress);
+ return ret;
}
-/* calculate all possible ways of dividing system_count between the System_specs */
void
-Page_breaking::divide_systems (vsize system_count,
- vector<vsize> const &min_sys,
- vector<vsize> const &max_sys,
- vector<vector<vsize> > *result,
- vector<vsize> *cur_division)
+Page_breaking::line_divisions_rec (vsize system_count,
+ Line_division const &min_sys,
+ Line_division const &max_sys,
+ vector<Line_division > *result,
+ Line_division *cur_division)
{
vsize my_index = cur_division->size ();
vsize others_min = 0;
if (my_index == min_sys.size () - 1)
result->push_back (*cur_division);
else
- divide_systems (system_count - i, min_sys, max_sys, result, cur_division);
+ line_divisions_rec (system_count - i, min_sys, max_sys, result, cur_division);
cur_division->pop_back ();
}
}
-