From 7f8545c560ef26bb4a6c15e7b28b191e48617110 Mon Sep 17 00:00:00 2001 From: aschmitz <29508+aschmitz@users.noreply.github.com> Date: Tue, 1 Feb 2022 18:13:39 -0600 Subject: [PATCH] fix: handle float rounding when extending boxes over page breaks When a box would break over the edge of a page, its height is extended to the bottom of that page (per https://www.w3.org/TR/css-break-3/#box-splitting , primarily to allow backgrounds and borders to continue to the end of the page). When this happened, sometimes the values that would be calculated for the height of the extended element would be rounded *over* the calculated height that remained on the page, forcing the entire containing box to wrap to the next page. Rather than trying to carefully manage the order of operations to try to be safe in IEEE floats for directions, we apply a small "fudge factor": if an element fits very nearly (within a thousandth of a pixel) into the remaining space, it is still accepted. --- tests/layout/test_page.py | 34 ++++++++++++++++++++++++++++++++++ weasyprint/layout/block.py | 8 +++++++- 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/tests/layout/test_page.py b/tests/layout/test_page.py index 36e9a026..87811286 100644 --- a/tests/layout/test_page.py +++ b/tests/layout/test_page.py @@ -141,6 +141,40 @@ def test_page_breaks(html): for page_child in page_children] == [[10, 40], [10, 40], [10]] +@assert_no_logs +def test_page_breaks_box_split(): + # If floats round the wrong way, a block that gets filled to the end of a + # page due to breaking over the page may be forced onto the next page + # because it is slightly taller than can fit on the previous page, even if + # it wouldn't have been without being filled. These numbers aren't ideal, + # but they do seem to trigger the issue. + page_1, page_2 = render_pages(''' + +
text
+
text
+
+
line1
+
line2
+
line3
+
line4
+
+ ''') + html, = page_1.children + body, = html.children + assert len(body.children) == 3 + div1, div2, section = body.children + assert len(section.children) == 2 + + html, = page_2.children + body, = html.children + section, = body.children + assert len(section.children) == 2 + + @assert_no_logs def test_page_breaks_complex_1(): page_1, page_2, page_3, page_4 = render_pages(''' diff --git a/weasyprint/layout/block.py b/weasyprint/layout/block.py index 0a2608d7..a03f080d 100644 --- a/weasyprint/layout/block.py +++ b/weasyprint/layout/block.py @@ -472,7 +472,13 @@ def _in_flow_layout(context, box, index, child, new_children, page_is_empty, new_position_y = ( new_child.border_box_y() + new_child.border_height()) - if (new_content_position_y > context.page_bottom - bottom_space and + # Use a small fudge factor on this due to box splitting setting the + # height of some elements to the remaining height of the page: + # https://www.w3.org/TR/css-break-3/#box-splitting + # (Occasionally the order of this calculation would otherwise come + # out with unequal float values, forcing the box to the next page.) + if (new_content_position_y > + context.page_bottom - bottom_space + 0.001 and not page_is_empty_with_no_children): # The child content overflows the page area, display it on the # next page.