![]() |
Page breaking and the construction of pages | ![]() |
At the end of each typesetting pass, the typesetter owns the array l of page items of the whole document (see incremental typesetting). It hands this array to a pager_rep (
Typeset/Page/pager.hpp
), whose job is to cut it into pages and to produce the final document box:
pager ppp= tm_new<pager_rep> (br->ip, env, l);
box rb= ppp->make_pages (); |
The constructor of pager_rep (
Typeset/Page/pager.cpp
) reads the page geometry with env->get_page_pars (text_width, text_height, width, height, the margins odd, even, top, bot) and the page parameters: page-extend and page-shrink (may_extend, may_shrink), the header and footer separations, the separations around footnotes and floats (fn_sep, fnote_sep, float_sep), the footnote bar length, and the quality of page breaking page-breaking (quality is 0 for sloppy, 1 for medium and 2 otherwise). The flag paper is true when page-medium is paper.
pager_rep::make_pages calls either pages_make (paper) or papyrus_make (screen modes), which fill the array pages with one box per page. It then arranges the pages on the screen: when page-packet is larger than one, several pages are put side by side; on paper, each page may be framed with a page_border_box according to page-border. The result is a scatter_box of the pages, wrapped in a move_box.
The page height is the space space (text_height- may_shrink, text_height, text_height+ may_extend). The page breaker returns one pagelet per page, and pages_make_page turns each of them into a page box.
The page height is essentially infinite (MAX_SI >> 1). The page breaker is still used, since it is responsible for placing floats and footnotes, but it must return a single pagelet. The page is as tall as its contents (for the beamer medium at least as tall as the user page height).
The result of page breaking is a skeleton, i.e. an array<pagelet> (
Typeset/Page/skeleton.hpp
). A pagelet describes the contents of one page (or one column) as a list of insertions, together with its total height ht (a space), its penalty pen and the stretch factor chosen for it. An insertion is a vertical piece of material:
struct insertion_rep: concrete_struct {
tree type; // type of insertion
path begin; // begin location in array of page_items
path end; // end location in array of page_items
skeleton sk; // or possible subpagelets (used for multicolumns)
space ht; // height of pagelet
space xh; // extra stretchable height (used for certain floats)
vpenalty pen; // penalty associated to pagelet
double stretch; // between -1 and 1 for determining final height
SI top_cor; // top correction
SI bot_cor; // bottom correction
int nr_cols; // number of columns
…
}; |
The type is the empty string for a portion of the main text, and a tuple starting with "float", "footnote", "if-page-break" or "multi-column" otherwise. The paths begin and end delimit a range of page items. For the main text they are paths (i) into the page items of the document. Floats and footnotes are addressed by longer paths (i, j, k): item k of the j-th float attached to page item i (see the functions access and sub in
Typeset/Page/page_breaker.cpp
). Multi-column insertions carry one sub-pagelet per column in sk.
Costs are measured by a vpenalty (
Typeset/Page/vpenalty.hpp
), a pair of integers compared lexicographically: the main penalty pen and the excentricity exc, a squared deviation from the ideal page height computed by as_vpenalty. The main penalty is the sum of the penalties of the page items at which the document is broken (0 between paragraphs, 1 inside paragraphs, 10 or 100 near the borders of a paragraph, see the stacker in paragraph formatting) plus the following constants when a page does not fit exactly:
The contents only fit when the page is extended or shrunk within the allowed tolerance.
The page is underfull or overfull even with the tolerances; these are scaled by the ratio between the actual and the desired height.
The columns of a multi-column portion cannot be balanced.
The floats cannot be placed consistently with their placement specifications.
The helper stretch_space (spc, stretch) maps a stretch factor in \([- 1, 1]\) to a length between spc->min and spc->max.
Floating material travels from the concater to the page breaker as follows.
For a float tag (which is also used by the style files to implement footnotes, with type footnote), concater_rep::typeset_float evaluates the type and the placement string (for instance "tbh"; forced to "h" when env->page_floats is false), and calls make_lazy_vstream. This typesets the body at the width of the page into a lazy_vstream, a vertical stream of page items whose channel is the tuple (type, placement). The stream is wrapped in a control_box and printed as a FLOAT_ITEM. The if-page-break tag, which inserts material only if a page break occurs at a given position, works in the same way with the channel ("if-page-break" pos sep).
During paragraph formatting, lazy_paragraph_rep::line_print retrieves the lazy stream (get_leaf_lazy) and collects it in the array fl of the current line. line_end attaches this array to the page item of the line: floats are therefore anchored to the line on which they occur.
The page breaker turns every attached stream into an insertion, and decides on which page, and where on it, the insertion is placed.
Marginal notes (line-note, page-note) are not floats: they are placed next to their line by line_end.
The page breaker is called through
Defined in
Typeset/Page/page_breaker.cpp
. If the user preference "new style page breaking" is not "off" (it is "on" by default, see
texmacs/texmacs/tm-server.scm
), the call is delegated to new_break_pages in
Typeset/Page/new_breaker.cpp
. Otherwise the older page_breaker_rep is used.
The constructor of new_breaker_rep computes, for each page item i:
its height including the following glue, body_ht[i], and the cumulated heights body_tot[i], so that the natural height of any range of lines is a difference of two entries;
top and bottom corrections body_cor[i], which account for lines whose ink extends above or below the normal font height;
the insertions attached to the item (ins_list[i], built with make_insertion (lazy_vstream, path)), and the cumulated heights of footnotes (foot_tot), floats (float_tot), and of single-column floats and footnotes inside multi-column text (wide_tot), as well as the extra height of if-page-break material (break_ht);
the number of columns (col_number, col_same) and whether a page break or a new page is compulsory after the item (must_break, must_new, from PAGE_CONTROL_ITEMs page-break, new-page and new-dpage).
With these tables, compute_space (b1, b2) returns the total height (as a space) of the page which starts at break b1 and ends at break b2, including the footnotes and floats anchored in between, without looking at the individual items.
A page break point is a path. The simplest form (i) means a break before page item i. A break may also carry a list of postponed floats: \((i, i_1, j_1, i_2, j_2, \ldots)\) means a break before item i, where the floats with indices \((i_k, j_k)\) in ins_list, although anchored before the break, are deferred to the following page. When enumerating candidates, the breaker considers, for each line, all ways of postponing a suffix of the pending floats. A float whose placement contains f forces the pending floats to be resolved; if their placement letters are incompatible (the local variable float_status reaches 3), the break receives BAD_FLOATS_PENALTY (or the search from this start stops if an acceptable break has already been found).
The search is a shortest-path computation over break points, similar to line breaking. For each reached break b, the hashmaps best_prev and best_pens store the best previous break and the accumulated vpenalty. find_page_breaks () maintains a worklist todo_list of breaks from which pages still have to be started, beginning with path (0), and calls find_page_breaks (b1) for them.
find_page_breaks (b1) enumerates the candidate ends b2 of a page starting at b1, in increasing order. For each candidate whose break penalty is below HYPH_INVALID, it computes the height spc= compute_space (b1, b2) of the page and the cost
where the fit penalties are EXTEND_PAGE_PENALTY, REDUCE_PAGE_PENALTY, TOO_SHORT_PENALTY or TOO_LONG_PENALTY as described above. The excentricity and the fit The excentricity is not charged for the last page (last_break), and an underfull page is not penalized when it is the last one or ends at a forced page break. If the cost is better than the one stored for b2, b2 is updated and queued. The enumeration stops as soon as the minimal height of the page exceeds the maximal page height, or at a forced page break. On papyrus, the only candidate end is the end of the document.
The parameter quality controls the search. With quality 2 (professional, the default), all queued breaks are expanded, which yields a globally optimal solution. With lower qualities, only the most promising queued break is expanded in each round, which is faster but may give worse pages.
Once the search is finished, assemble_skeleton follows the best_prev links back from path (N(l)) and creates one pagelet per page with assemble (start, end) (or assemble_multi_columns for pages with several columns):
The floats anchored on the page (minus those postponed by end), together with the floats postponed by start, are distributed according to their placement letters. Roughly, floats allowing t go to the top, floats allowing b to the bottom, and the remaining ones stay here, i.e. in the text flow right after the line to which they are anchored.
The pagelet is built from the top floats, pending if-page-break material, portions of the main text (make_insertion (i1, i2)) interleaved with the here floats, the bottom floats, and finally the footnotes, with the separations float_sep, fnote_sep and fn_sep in between.
format_pagelet (pg, height, last_page) determines the stretch factor of the page, between \(- 1\) (maximally shrunk) and \(1\) (maximally stretched), and propagates it to the insertions. The last page is not stretched when it is not full.
Multi-column material (page items with nr_cols > 1, produced by paragraphs with par-columns larger than one) is handled in
Typeset/Page/columns_breaker.cpp
. break_uniform splits a page into portions with a uniform number of columns, and break_columns finds column breaks which balance the columns of each portion (searching around the fractions \(k / n\) of the total height with break_columns_at). The resulting portions become insertions of type ("multi-column" n) with one sub-pagelet per column.
The class page_breaker_rep in
Typeset/Page/page_breaker.cpp
implements the previous algorithm, still available by switching off the preference. It organizes the page items into three flows (MAIN_FLOW, FNOTE_FLOW, FLOAT_FLOW), represents break points as vectors of positions in each flow (vbreak), sorts them by estimated height and explores candidate page ends around the ideal position using ladders of incomparable breaks (find_next_breaks, propose_break). Its output is a skeleton of the same form.
The skeleton is turned into boxes by the methods in
Typeset/Page/make_pages.cpp
:
Stacks a range of page items into a box of the given height with format_stack, which stretches or shrinks the vertical glue between the items. On the way, page control items of the form env_page update the local page style (headers, footers, page number, background, margins for this page).
Extracts the items of an insertion with sub and formats them at the height stretch_space (ins->ht, ins->stretch); multi-column insertions are formatted column by column and placed side by side.
Places the insertions of a page below each other with the appropriate separations, and draws the footnote rule (a line_box of length page-fnote-barlen) above the first footnote.
Sets page-nr and page-the-page in the environment, typesets the header and footer (make_header, make_footer, which evaluate the macros page-odd-header, page-even-footer etc., or the one-shot page-this-header), determines the background and margin adjustments, and builds a page_box. Crop marks are added with crop_marks_box if requested.
The page boxes are the children of the document box returned by make_pages. Since headers and footers are regenerated at each pass, page numbers in headers are always up to date. Page numbers of labels are collected afterwards, on paper and during complete passes only, by typesetter_rep::determine_page_references, which asks the page boxes for them with collect_page_numbers.