Skip to content
This repository
Browse code

Put block-level images on the next page when they would overflow.

  • Loading branch information...
commit e6cee2f2aba72d01080e39772349ceb6b0505999 1 parent 3a4cf78
Simon Sapin SimonSapin authored
5 weasyprint/compat.py
@@ -29,10 +29,7 @@
29 29 import contextlib
30 30
31 31
32   -PY3 = sys.version_info[0] >= 3
33   -
34   -
35   -if PY3:
  32 +if sys.version_info[0] >= 3:
36 33 # Python 3
37 34 from urllib.parse import urljoin, urlparse, unquote_to_bytes
38 35 from urllib.request import urlopen, Request
44 weasyprint/layout/blocks.py
@@ -260,6 +260,33 @@ def block_level_height(document, box, max_position_y, skip_stack,
260 260 new_containing_block, device_size, page_is_empty,
261 261 adjoining_margins)
262 262
  263 + if new_child is not None:
  264 + # We need to do this after the child layout to have the
  265 + # used value for margin_top (eg. it might be a percentage.)
  266 + if not isinstance(new_child, boxes.BlockBox):
  267 + adjoining_margins.append(new_child.margin_top)
  268 + offset_y = (collapse_margin(adjoining_margins)
  269 + - new_child.margin_top)
  270 + new_child.translate(0, offset_y)
  271 + adjoining_margins = []
  272 + #else: blocks handle that themselves.
  273 +
  274 + adjoining_margins = next_adjoining_margins
  275 + adjoining_margins.append(new_child.margin_bottom)
  276 +
  277 + if not collapsing_through:
  278 + new_position_y = (
  279 + new_child.border_box_y() + new_child.border_height())
  280 +
  281 + if (new_position_y > max_position_y and not page_is_empty
  282 + and not isinstance(child, boxes.BlockBox)):
  283 + # The child overflows the page area, put it on the
  284 + # next page. (But don’t delay whole blocks if eg.
  285 + # only the bottom border overflows.)
  286 + new_child = None
  287 + else:
  288 + position_y = new_position_y
  289 +
263 290 skip_stack = None
264 291 if new_child is None:
265 292 if new_children:
@@ -270,22 +297,6 @@ def block_level_height(document, box, max_position_y, skip_stack,
270 297 # completly
271 298 return None, None, 'any', [], False
272 299
273   - # We need to do this after the child layout to have the used value
274   - # for margin_top (eg. it might be a percentage.)
275   - if not isinstance(new_child, boxes.BlockBox):
276   - adjoining_margins.append(new_child.margin_top)
277   - offset_y = (collapse_margin(adjoining_margins)
278   - - new_child.margin_top)
279   - new_child.translate(0, offset_y)
280   - adjoining_margins = []
281   - #else: blocks handle that themselves.
282   -
283   - adjoining_margins = next_adjoining_margins
284   - adjoining_margins.append(new_child.margin_bottom)
285   -
286   - if not collapsing_through:
287   - position_y = new_child.border_box_y() + new_child.border_height()
288   -
289 300 # Bottom borders may overflow here
290 301 # TODO: back-track somehow when all lines fit but not borders
291 302 new_children.append(new_child)
@@ -340,6 +351,7 @@ def block_level_height(document, box, max_position_y, skip_stack,
340 351 # TODO: See corner cases in
341 352 # http://www.w3.org/TR/CSS21/visudet.html#normal-block
342 353 if new_box.height == 'auto':
  354 + print(box, position_y, new_box.content_box_y())
343 355 new_box.height = position_y - new_box.content_box_y()
344 356
345 357 if resume_at is not None:
2  weasyprint/tests/test_api.py
@@ -36,7 +36,7 @@
36 36
37 37 from .testing_utils import (
38 38 resource_filename, assert_no_logs, TEST_UA_STYLESHEET)
39   -from ..compat import urljoin, PY3
  39 +from ..compat import urljoin
40 40 from .. import HTML, CSS
41 41 from .. import __main__
42 42
23 weasyprint/tests/test_layout.py
@@ -563,6 +563,29 @@ def test_page_breaks():
563 563 # positions_y = [[div.position_y for div in divs] for divs in page_divs]
564 564 # assert positions_y == [[10, 40], [10, 40], [10]]
565 565
  566 + pages = parse('''
  567 + <style>
  568 + @page { -weasy-size: 100px; margin: 10px }
  569 + img { height: 30px; display: block }
  570 + </style>
  571 + <body>
  572 + <img src=pattern.png>
  573 + <img src=pattern.png>
  574 + <img src=pattern.png>
  575 + <img src=pattern.png>
  576 + <img src=pattern.png>
  577 + ''')
  578 + page_images = []
  579 + for page in pages:
  580 + images = body_children(page)
  581 + assert all([img.element_tag == 'img' for img in images])
  582 + assert all([img.position_x == 10 for img in images])
  583 + page_images.append(images)
  584 + positions_y = [[img.position_y for img in images]
  585 + for images in page_images]
  586 + assert positions_y == [[10, 40], [10, 40], [10]]
  587 +
  588 +
566 589 page_1, page_2, page_3, page_4 = parse('''
567 590 <style>
568 591 @page { margin: 10px }

0 comments on commit e6cee2f

Please sign in to comment.
Something went wrong with that request. Please try again.