2011-07-22 18:34:32 +04:00
|
|
|
|
# coding: utf8
|
2012-03-22 02:19:27 +04:00
|
|
|
|
"""
|
|
|
|
|
weasyprint.layout.blocks
|
|
|
|
|
------------------------
|
2011-07-22 18:34:32 +04:00
|
|
|
|
|
2012-03-22 02:19:27 +04:00
|
|
|
|
Page breaking and layout for block-level and block-container boxes.
|
2011-07-22 18:34:32 +04:00
|
|
|
|
|
2012-03-22 02:19:27 +04:00
|
|
|
|
:copyright: Copyright 2011-2012 Simon Sapin and contributors, see AUTHORS.
|
|
|
|
|
:license: BSD, see LICENSE for details.
|
2011-08-24 12:25:45 +04:00
|
|
|
|
|
|
|
|
|
"""
|
2011-07-22 18:34:32 +04:00
|
|
|
|
|
2012-02-17 21:49:58 +04:00
|
|
|
|
from __future__ import division, unicode_literals
|
2011-08-20 20:02:04 +04:00
|
|
|
|
|
2012-05-25 14:03:48 +04:00
|
|
|
|
from .absolute import absolute_layout, AbsolutePlaceholder
|
2012-05-30 21:16:36 +04:00
|
|
|
|
from .float import float_layout, get_clearance, FloatPlaceholder
|
2012-04-06 21:44:09 +04:00
|
|
|
|
from .inlines import (iter_line_boxes, replaced_box_width, replaced_box_height,
|
|
|
|
|
handle_min_max_width, min_max_replaced_height,
|
|
|
|
|
min_max_auto_replaced)
|
2011-08-22 17:37:10 +04:00
|
|
|
|
from .markers import list_marker_layout
|
2012-04-12 14:51:21 +04:00
|
|
|
|
from .tables import table_layout, table_wrapper_width
|
2012-05-10 07:57:06 +04:00
|
|
|
|
from .percentages import resolve_percentages, resolve_position_percentages
|
2011-08-22 17:37:10 +04:00
|
|
|
|
from ..formatting_structure import boxes
|
2011-07-22 18:34:32 +04:00
|
|
|
|
|
2011-08-22 13:38:54 +04:00
|
|
|
|
|
2011-12-02 18:31:23 +04:00
|
|
|
|
def block_level_layout(document, box, max_position_y, skip_stack,
|
2012-02-23 22:30:31 +04:00
|
|
|
|
containing_block, device_size, page_is_empty,
|
2012-05-09 21:01:32 +04:00
|
|
|
|
absolute_boxes, adjoining_margins):
|
2011-08-24 12:25:45 +04:00
|
|
|
|
"""Lay out the block-level ``box``.
|
|
|
|
|
|
2011-08-22 20:14:37 +04:00
|
|
|
|
:param max_position_y: the absolute vertical position (as in
|
2011-08-24 12:25:45 +04:00
|
|
|
|
``some_box.position_y``) of the bottom of the
|
|
|
|
|
content box of the current page area.
|
|
|
|
|
|
2011-08-22 20:14:37 +04:00
|
|
|
|
"""
|
2012-05-30 21:16:36 +04:00
|
|
|
|
clearance = get_clearance(document, box)
|
|
|
|
|
if clearance:
|
|
|
|
|
box.position_y += clearance
|
|
|
|
|
adjoining_margins = []
|
|
|
|
|
|
2011-11-22 16:06:50 +04:00
|
|
|
|
if isinstance(box, boxes.TableBox):
|
2012-05-09 21:01:32 +04:00
|
|
|
|
return table_layout(
|
|
|
|
|
document, box, max_position_y, skip_stack, containing_block,
|
|
|
|
|
device_size, page_is_empty, absolute_boxes)
|
2011-11-22 16:06:50 +04:00
|
|
|
|
elif isinstance(box, boxes.BlockBox):
|
2012-04-12 14:51:21 +04:00
|
|
|
|
return block_box_layout(document, box, max_position_y, skip_stack,
|
|
|
|
|
containing_block, device_size, page_is_empty,
|
2012-05-09 21:01:32 +04:00
|
|
|
|
absolute_boxes, adjoining_margins)
|
2011-12-05 17:24:43 +04:00
|
|
|
|
elif isinstance(box, boxes.BlockReplacedBox):
|
2012-05-09 21:01:32 +04:00
|
|
|
|
box = block_replaced_box_layout(box, containing_block, device_size)
|
2012-02-23 15:51:19 +04:00
|
|
|
|
resume_at = None
|
|
|
|
|
next_page = 'any'
|
|
|
|
|
adjoining_margins = []
|
2012-02-28 17:50:35 +04:00
|
|
|
|
collapsing_through = False
|
|
|
|
|
return box, resume_at, next_page, adjoining_margins, collapsing_through
|
2012-04-02 16:45:44 +04:00
|
|
|
|
else: # pragma: no cover
|
2011-08-22 13:38:54 +04:00
|
|
|
|
raise TypeError('Layout for %s not handled yet' % type(box).__name__)
|
|
|
|
|
|
|
|
|
|
|
2011-12-02 18:31:23 +04:00
|
|
|
|
def block_box_layout(document, box, max_position_y, skip_stack,
|
2012-02-23 22:30:31 +04:00
|
|
|
|
containing_block, device_size, page_is_empty,
|
2012-05-09 21:01:32 +04:00
|
|
|
|
absolute_boxes, adjoining_margins):
|
2011-08-24 12:25:45 +04:00
|
|
|
|
"""Lay out the block ``box``."""
|
2011-10-06 19:47:04 +04:00
|
|
|
|
resolve_percentages(box, containing_block)
|
2012-05-11 22:50:28 +04:00
|
|
|
|
if box.is_table_wrapper:
|
2012-05-25 19:13:38 +04:00
|
|
|
|
table_wrapper_width(box, (containing_block.width,
|
|
|
|
|
containing_block.height), absolute_boxes)
|
2011-10-06 18:20:03 +04:00
|
|
|
|
block_level_width(box, containing_block)
|
2012-05-30 21:16:36 +04:00
|
|
|
|
|
2012-02-28 17:50:35 +04:00
|
|
|
|
new_box, resume_at, next_page, adjoining_margins, collapsing_through = \
|
2012-04-06 18:42:06 +04:00
|
|
|
|
block_container_layout(
|
2012-02-28 17:50:35 +04:00
|
|
|
|
document, box, max_position_y, skip_stack,
|
2012-05-09 21:01:32 +04:00
|
|
|
|
device_size, page_is_empty, absolute_boxes, adjoining_margins)
|
2012-05-11 22:50:28 +04:00
|
|
|
|
list_marker_layout(document, new_box)
|
2012-02-28 17:50:35 +04:00
|
|
|
|
return new_box, resume_at, next_page, adjoining_margins, collapsing_through
|
2011-08-20 17:07:14 +04:00
|
|
|
|
|
|
|
|
|
|
2012-04-06 21:44:09 +04:00
|
|
|
|
def block_replaced_width(box, containing_block, device_size):
|
2011-08-26 18:16:40 +04:00
|
|
|
|
# http://www.w3.org/TR/CSS21/visudet.html#block-replaced-width
|
2011-10-06 17:36:19 +04:00
|
|
|
|
replaced_box_width(box, device_size)
|
2011-10-06 18:20:03 +04:00
|
|
|
|
block_level_width(box, containing_block)
|
2011-08-25 19:32:23 +04:00
|
|
|
|
|
2012-04-06 21:44:09 +04:00
|
|
|
|
min_max_block_replaced_width = handle_min_max_width(block_replaced_width)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def block_replaced_box_layout(box, containing_block, device_size):
|
|
|
|
|
"""Lay out the block :class:`boxes.ReplacedBox` ``box``."""
|
|
|
|
|
resolve_percentages(box, containing_block)
|
2011-08-26 18:16:40 +04:00
|
|
|
|
if box.margin_top == 'auto':
|
|
|
|
|
box.margin_top = 0
|
|
|
|
|
if box.margin_bottom == 'auto':
|
|
|
|
|
box.margin_bottom = 0
|
|
|
|
|
|
2012-04-06 21:44:09 +04:00
|
|
|
|
if box.style.width == 'auto' and box.style.height == 'auto':
|
|
|
|
|
block_replaced_width(box, containing_block, device_size)
|
|
|
|
|
replaced_box_height(box, device_size)
|
|
|
|
|
min_max_auto_replaced(box)
|
|
|
|
|
else:
|
|
|
|
|
min_max_block_replaced_width(box, containing_block, device_size)
|
|
|
|
|
min_max_replaced_height(box, device_size)
|
2011-07-22 18:34:32 +04:00
|
|
|
|
|
2012-04-06 21:44:09 +04:00
|
|
|
|
return box
|
2012-04-06 18:19:16 +04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@handle_min_max_width
|
2011-10-06 18:20:03 +04:00
|
|
|
|
def block_level_width(box, containing_block):
|
2011-08-24 12:25:45 +04:00
|
|
|
|
"""Set the ``box`` width."""
|
|
|
|
|
# 'cb' stands for 'containing block'
|
2011-10-06 18:20:03 +04:00
|
|
|
|
cb_width = containing_block.width
|
2011-07-22 18:34:32 +04:00
|
|
|
|
|
|
|
|
|
# http://www.w3.org/TR/CSS21/visudet.html#blockwidth
|
|
|
|
|
|
|
|
|
|
# These names are waaay too long
|
|
|
|
|
margin_l = box.margin_left
|
|
|
|
|
margin_r = box.margin_right
|
|
|
|
|
padding_l = box.padding_left
|
|
|
|
|
padding_r = box.padding_right
|
2011-12-30 20:19:02 +04:00
|
|
|
|
border_l = box.border_left_width
|
|
|
|
|
border_r = box.border_right_width
|
2011-07-22 18:34:32 +04:00
|
|
|
|
width = box.width
|
|
|
|
|
|
|
|
|
|
# Only margin-left, margin-right and width can be 'auto'.
|
|
|
|
|
# We want: width of containing block ==
|
|
|
|
|
# margin-left + border-left-width + padding-left + width
|
|
|
|
|
# + padding-right + border-right-width + margin-right
|
|
|
|
|
|
|
|
|
|
paddings_plus_borders = padding_l + padding_r + border_l + border_r
|
|
|
|
|
if box.width != 'auto':
|
|
|
|
|
total = paddings_plus_borders + width
|
|
|
|
|
if margin_l != 'auto':
|
|
|
|
|
total += margin_l
|
|
|
|
|
if margin_r != 'auto':
|
|
|
|
|
total += margin_r
|
|
|
|
|
if total > cb_width:
|
|
|
|
|
if margin_l == 'auto':
|
|
|
|
|
margin_l = box.margin_left = 0
|
|
|
|
|
if margin_r == 'auto':
|
|
|
|
|
margin_r = box.margin_right = 0
|
|
|
|
|
if width != 'auto' and margin_l != 'auto' and margin_r != 'auto':
|
|
|
|
|
# The equation is over-constrained
|
|
|
|
|
margin_sum = cb_width - paddings_plus_borders - width
|
2012-01-27 14:07:49 +04:00
|
|
|
|
if containing_block.style.direction == 'ltr':
|
2011-07-22 18:34:32 +04:00
|
|
|
|
margin_r = box.margin_right = margin_sum - margin_l
|
|
|
|
|
else:
|
|
|
|
|
margin_l = box.margin_left = margin_sum - margin_r
|
|
|
|
|
if width == 'auto':
|
|
|
|
|
if margin_l == 'auto':
|
|
|
|
|
margin_l = box.margin_left = 0
|
|
|
|
|
if margin_r == 'auto':
|
|
|
|
|
margin_r = box.margin_right = 0
|
|
|
|
|
width = box.width = cb_width - (
|
|
|
|
|
paddings_plus_borders + margin_l + margin_r)
|
|
|
|
|
margin_sum = cb_width - paddings_plus_borders - width
|
|
|
|
|
if margin_l == 'auto' and margin_r == 'auto':
|
|
|
|
|
box.margin_left = margin_sum / 2.
|
|
|
|
|
box.margin_right = margin_sum / 2.
|
|
|
|
|
elif margin_l == 'auto' and margin_r != 'auto':
|
|
|
|
|
box.margin_left = margin_sum - margin_r
|
|
|
|
|
elif margin_l != 'auto' and margin_r == 'auto':
|
|
|
|
|
box.margin_right = margin_sum - margin_l
|
|
|
|
|
|
|
|
|
|
|
2012-05-09 19:07:01 +04:00
|
|
|
|
def relative_positioning(box, containing_block):
|
|
|
|
|
"""Translate the ``box`` if it is relatively positioned."""
|
|
|
|
|
if box.style.position == 'relative':
|
2012-05-10 07:57:06 +04:00
|
|
|
|
resolve_position_percentages(box, containing_block)
|
2012-05-09 19:07:01 +04:00
|
|
|
|
|
|
|
|
|
if box.left != 'auto' and box.right != 'auto':
|
2012-05-25 17:57:13 +04:00
|
|
|
|
if box.style.direction == 'ltr':
|
|
|
|
|
translate_x = box.left
|
|
|
|
|
else:
|
|
|
|
|
translate_x = -box.right
|
2012-05-09 19:07:01 +04:00
|
|
|
|
elif box.left != 'auto':
|
|
|
|
|
translate_x = box.left
|
|
|
|
|
elif box.right != 'auto':
|
|
|
|
|
translate_x = -box.right
|
|
|
|
|
else:
|
|
|
|
|
translate_x = 0
|
|
|
|
|
|
|
|
|
|
if box.top != 'auto':
|
|
|
|
|
translate_y = box.top
|
|
|
|
|
elif box.style.bottom != 'auto':
|
|
|
|
|
translate_y = -box.bottom
|
|
|
|
|
else:
|
|
|
|
|
translate_y = 0
|
|
|
|
|
|
2012-05-25 14:03:48 +04:00
|
|
|
|
box.translate(translate_x, translate_y)
|
2012-05-09 19:07:01 +04:00
|
|
|
|
|
|
|
|
|
if isinstance(box, (boxes.InlineBox, boxes.LineBox)):
|
|
|
|
|
for child in box.children:
|
|
|
|
|
relative_positioning(child, containing_block)
|
|
|
|
|
|
|
|
|
|
|
2012-04-06 18:42:06 +04:00
|
|
|
|
def block_container_layout(document, box, max_position_y, skip_stack,
|
2012-05-09 21:01:32 +04:00
|
|
|
|
device_size, page_is_empty, absolute_boxes,
|
|
|
|
|
adjoining_margins=None):
|
2011-08-24 12:25:45 +04:00
|
|
|
|
"""Set the ``box`` height."""
|
2011-11-22 16:06:50 +04:00
|
|
|
|
assert isinstance(box, boxes.BlockContainerBox)
|
2011-08-22 20:14:37 +04:00
|
|
|
|
|
2012-04-06 18:42:06 +04:00
|
|
|
|
# TODO: this should make a difference, but that is currently neglected.
|
2012-02-07 19:59:22 +04:00
|
|
|
|
# See http://www.w3.org/TR/CSS21/visudet.html#normal-block
|
|
|
|
|
# http://www.w3.org/TR/CSS21/visudet.html#root-height
|
|
|
|
|
|
|
|
|
|
#if box.style.overflow != 'visible':
|
|
|
|
|
# ...
|
2011-07-22 18:34:32 +04:00
|
|
|
|
|
|
|
|
|
if box.margin_top == 'auto':
|
|
|
|
|
box.margin_top = 0
|
|
|
|
|
if box.margin_bottom == 'auto':
|
|
|
|
|
box.margin_bottom = 0
|
|
|
|
|
|
2012-02-23 22:30:31 +04:00
|
|
|
|
if adjoining_margins is None:
|
|
|
|
|
adjoining_margins = []
|
|
|
|
|
|
|
|
|
|
adjoining_margins.append(box.margin_top)
|
|
|
|
|
this_box_adjoining_margins = adjoining_margins
|
|
|
|
|
|
|
|
|
|
collapsing_with_children = not (box.border_top_width or box.padding_top
|
2012-02-28 17:50:48 +04:00
|
|
|
|
or establishes_formatting_context(box) or box.is_for_root_element)
|
2012-02-23 22:30:31 +04:00
|
|
|
|
if collapsing_with_children:
|
|
|
|
|
# XXX not counting margins in adjoining_margins, if any
|
|
|
|
|
position_y = box.position_y
|
|
|
|
|
else:
|
|
|
|
|
box.position_y += collapse_margin(adjoining_margins) - box.margin_top
|
|
|
|
|
adjoining_margins = []
|
|
|
|
|
position_y = box.content_box_y()
|
|
|
|
|
|
2011-07-22 18:34:32 +04:00
|
|
|
|
position_x = box.content_box_x()
|
2011-07-26 19:34:55 +04:00
|
|
|
|
|
2012-05-29 19:35:20 +04:00
|
|
|
|
if box.style.position == 'relative':
|
|
|
|
|
# New containing block, use a new absolute list
|
|
|
|
|
absolute_boxes = []
|
|
|
|
|
|
2011-10-03 20:57:26 +04:00
|
|
|
|
new_children = []
|
2012-01-20 14:55:06 +04:00
|
|
|
|
next_page = 'any'
|
2011-09-30 21:04:05 +04:00
|
|
|
|
|
2012-05-21 16:22:32 +04:00
|
|
|
|
is_start = skip_stack is None
|
|
|
|
|
if is_start:
|
2011-09-30 21:04:05 +04:00
|
|
|
|
skip = 0
|
|
|
|
|
else:
|
|
|
|
|
skip, skip_stack = skip_stack
|
|
|
|
|
|
|
|
|
|
for index, child in box.enumerate_skip(skip):
|
2011-07-22 18:34:32 +04:00
|
|
|
|
child.position_x = position_x
|
2012-02-23 22:30:31 +04:00
|
|
|
|
# XXX does not count margins in adjoining_margins:
|
2011-07-22 18:34:32 +04:00
|
|
|
|
child.position_y = position_y
|
2012-02-23 22:30:31 +04:00
|
|
|
|
|
2012-05-09 21:01:32 +04:00
|
|
|
|
if not child.is_in_normal_flow():
|
2012-05-30 05:27:08 +04:00
|
|
|
|
child.position_y += collapse_margin(adjoining_margins)
|
2012-05-25 14:03:48 +04:00
|
|
|
|
if child.style.position in ('absolute', 'fixed'):
|
|
|
|
|
placeholder = AbsolutePlaceholder(child)
|
|
|
|
|
if child.style.position == 'absolute':
|
|
|
|
|
absolute_boxes.append(placeholder)
|
|
|
|
|
new_children.append(placeholder)
|
|
|
|
|
else:
|
|
|
|
|
document.fixed_boxes.append(placeholder)
|
2012-05-29 18:01:59 +04:00
|
|
|
|
elif child.style.float in ('left', 'right'):
|
|
|
|
|
placeholder = FloatPlaceholder(child)
|
2012-05-30 21:16:36 +04:00
|
|
|
|
float_layout(document, placeholder, box, absolute_boxes)
|
|
|
|
|
document.excluded_shapes.append(placeholder)
|
2012-05-29 18:01:59 +04:00
|
|
|
|
new_children.append(placeholder)
|
2012-05-09 21:01:32 +04:00
|
|
|
|
continue
|
|
|
|
|
|
2011-07-26 19:34:55 +04:00
|
|
|
|
if isinstance(child, boxes.LineBox):
|
2011-10-13 18:25:08 +04:00
|
|
|
|
assert len(box.children) == 1, (
|
|
|
|
|
'line box with siblings before layout')
|
2012-02-23 22:30:31 +04:00
|
|
|
|
if adjoining_margins:
|
|
|
|
|
position_y += collapse_margin(adjoining_margins)
|
|
|
|
|
adjoining_margins = []
|
2012-03-14 22:33:24 +04:00
|
|
|
|
new_containing_block = box
|
|
|
|
|
lines_iterator = iter_line_boxes(
|
|
|
|
|
document, child, position_y, skip_stack,
|
2012-05-09 21:01:32 +04:00
|
|
|
|
new_containing_block, device_size, absolute_boxes)
|
2012-03-14 22:33:24 +04:00
|
|
|
|
new_lines = []
|
|
|
|
|
is_page_break = False
|
|
|
|
|
for line, resume_at in lines_iterator:
|
2012-05-31 01:40:54 +04:00
|
|
|
|
new_position_y = line.position_y + line.height
|
2011-10-21 13:14:38 +04:00
|
|
|
|
# Allow overflow if the first line of the page is higher
|
|
|
|
|
# than the page itself so that we put *something* on this
|
|
|
|
|
# page and can advance in the document.
|
2012-03-14 22:33:24 +04:00
|
|
|
|
if new_position_y > max_position_y and (
|
|
|
|
|
new_lines or not page_is_empty):
|
|
|
|
|
over_orphans = len(new_lines) - box.style.orphans
|
|
|
|
|
if over_orphans < 0 and not page_is_empty:
|
|
|
|
|
# Reached the bottom of the page before we had
|
|
|
|
|
# enough lines for orphans, cancel the whole box.
|
|
|
|
|
return None, None, 'any', [], False
|
|
|
|
|
# How many lines we need on the next page to satisfy widows
|
|
|
|
|
# -1 for the current line.
|
|
|
|
|
needed = box.style.widows - 1
|
|
|
|
|
if needed:
|
|
|
|
|
for _ in lines_iterator:
|
|
|
|
|
needed -= 1
|
|
|
|
|
if needed == 0:
|
|
|
|
|
break
|
|
|
|
|
if needed > over_orphans and not page_is_empty:
|
|
|
|
|
# Total number of lines < orphans + widows
|
2012-02-28 17:50:35 +04:00
|
|
|
|
return None, None, 'any', [], False
|
2012-03-14 22:33:24 +04:00
|
|
|
|
if needed and needed <= over_orphans:
|
|
|
|
|
# Remove lines to keep them for the next page
|
|
|
|
|
del new_lines[-needed:]
|
2011-10-03 19:55:41 +04:00
|
|
|
|
# Page break here, resume before this line
|
|
|
|
|
resume_at = (index, skip_stack)
|
|
|
|
|
is_page_break = True
|
|
|
|
|
break
|
2012-05-28 13:20:01 +04:00
|
|
|
|
# TODO: this is incomplete.
|
|
|
|
|
# See http://dev.w3.org/csswg/css3-page/#allowed-pg-brk
|
|
|
|
|
# "When an unforced page break occurs here, both the adjoining
|
|
|
|
|
# ‘margin-top’ and ‘margin-bottom’ are set to zero."
|
2012-05-28 02:05:09 +04:00
|
|
|
|
elif page_is_empty and new_position_y > max_position_y:
|
|
|
|
|
# Remove the top border when a page is empty and the box is
|
|
|
|
|
# too high to be drawn in one page
|
|
|
|
|
new_position_y -= box.margin_top
|
|
|
|
|
line.translate(0, -box.margin_top)
|
|
|
|
|
box.margin_top = 0
|
2012-03-14 22:33:24 +04:00
|
|
|
|
new_lines.append((line, resume_at))
|
2011-10-03 19:55:41 +04:00
|
|
|
|
position_y = new_position_y
|
|
|
|
|
skip_stack = resume_at
|
2012-03-14 22:33:24 +04:00
|
|
|
|
new_children = [line for line, resume_at in new_lines]
|
|
|
|
|
if new_lines:
|
|
|
|
|
_, resume_at = new_lines[-1]
|
|
|
|
|
resume_at = (index, resume_at)
|
2011-10-03 19:55:41 +04:00
|
|
|
|
if is_page_break:
|
2011-08-22 20:38:15 +04:00
|
|
|
|
break
|
2011-07-26 19:34:55 +04:00
|
|
|
|
else:
|
2012-03-16 19:45:31 +04:00
|
|
|
|
if new_children:
|
2012-03-14 19:26:20 +04:00
|
|
|
|
# between siblings, but not before the first child
|
|
|
|
|
# or after the last child.
|
|
|
|
|
break_here, next_page = forced_page_break(
|
|
|
|
|
new_children[-1], child)
|
|
|
|
|
if break_here:
|
|
|
|
|
resume_at = (index, None)
|
|
|
|
|
break
|
2012-01-20 14:55:06 +04:00
|
|
|
|
|
2011-10-06 18:20:03 +04:00
|
|
|
|
new_containing_block = box
|
2012-02-28 17:50:35 +04:00
|
|
|
|
(new_child, resume_at, next_page, next_adjoining_margins,
|
|
|
|
|
collapsing_through) = block_level_layout(
|
2012-02-23 15:51:19 +04:00
|
|
|
|
document, child, max_position_y, skip_stack,
|
2012-03-16 19:45:31 +04:00
|
|
|
|
new_containing_block, device_size,
|
|
|
|
|
page_is_empty and not new_children,
|
2012-05-09 21:01:32 +04:00
|
|
|
|
absolute_boxes,
|
2012-02-23 22:30:31 +04:00
|
|
|
|
adjoining_margins)
|
2012-03-14 22:33:24 +04:00
|
|
|
|
skip_stack = None
|
2012-02-22 21:57:56 +04:00
|
|
|
|
|
2012-02-29 15:23:25 +04:00
|
|
|
|
if new_child is not None:
|
|
|
|
|
# We need to do this after the child layout to have the
|
|
|
|
|
# used value for margin_top (eg. it might be a percentage.)
|
|
|
|
|
if not isinstance(new_child, boxes.BlockBox):
|
|
|
|
|
adjoining_margins.append(new_child.margin_top)
|
|
|
|
|
offset_y = (collapse_margin(adjoining_margins)
|
|
|
|
|
- new_child.margin_top)
|
2012-05-25 14:03:48 +04:00
|
|
|
|
new_child.translate(0, offset_y)
|
2012-02-29 15:23:25 +04:00
|
|
|
|
adjoining_margins = []
|
|
|
|
|
#else: blocks handle that themselves.
|
|
|
|
|
|
|
|
|
|
adjoining_margins = next_adjoining_margins
|
|
|
|
|
adjoining_margins.append(new_child.margin_bottom)
|
|
|
|
|
|
|
|
|
|
if not collapsing_through:
|
|
|
|
|
new_position_y = (
|
|
|
|
|
new_child.border_box_y() + new_child.border_height())
|
|
|
|
|
|
2012-03-16 19:45:31 +04:00
|
|
|
|
if (new_position_y > max_position_y and (
|
|
|
|
|
new_children or not page_is_empty)
|
2012-02-29 15:23:25 +04:00
|
|
|
|
and not isinstance(child, boxes.BlockBox)):
|
|
|
|
|
# The child overflows the page area, put it on the
|
|
|
|
|
# next page. (But don’t delay whole blocks if eg.
|
|
|
|
|
# only the bottom border overflows.)
|
|
|
|
|
new_child = None
|
|
|
|
|
else:
|
|
|
|
|
position_y = new_position_y
|
|
|
|
|
|
2011-10-13 18:25:08 +04:00
|
|
|
|
if new_child is None:
|
|
|
|
|
if new_children:
|
|
|
|
|
resume_at = (index, None)
|
2011-10-14 18:58:57 +04:00
|
|
|
|
break
|
2011-10-13 18:25:08 +04:00
|
|
|
|
else:
|
|
|
|
|
# This was the first child of this box, cancel the box
|
|
|
|
|
# completly
|
2012-02-28 17:50:35 +04:00
|
|
|
|
return None, None, 'any', [], False
|
2012-02-22 21:57:56 +04:00
|
|
|
|
|
2011-10-13 18:25:08 +04:00
|
|
|
|
# Bottom borders may overflow here
|
|
|
|
|
# TODO: back-track somehow when all lines fit but not borders
|
2011-10-03 20:57:26 +04:00
|
|
|
|
new_children.append(new_child)
|
2011-09-30 21:04:05 +04:00
|
|
|
|
if resume_at is not None:
|
|
|
|
|
resume_at = (index, resume_at)
|
2011-08-22 20:14:37 +04:00
|
|
|
|
break
|
2012-01-20 14:55:06 +04:00
|
|
|
|
|
2011-09-30 21:04:05 +04:00
|
|
|
|
else:
|
|
|
|
|
resume_at = None
|
2011-08-22 20:14:37 +04:00
|
|
|
|
|
2012-03-16 19:45:31 +04:00
|
|
|
|
if resume_at is not None and box.style.page_break_inside == 'avoid' \
|
|
|
|
|
and not page_is_empty:
|
|
|
|
|
return None, None, 'any', [], False
|
|
|
|
|
|
2012-02-23 22:30:31 +04:00
|
|
|
|
if collapsing_with_children:
|
2012-05-10 22:12:47 +04:00
|
|
|
|
# this_adjoining_margins contains box.margin_top
|
|
|
|
|
border_box_y = box.position_y + collapse_margin(
|
|
|
|
|
this_box_adjoining_margins)
|
2012-02-23 22:30:31 +04:00
|
|
|
|
box.position_y = border_box_y - box.margin_top
|
|
|
|
|
|
2012-02-28 17:50:35 +04:00
|
|
|
|
collapsing_through = False
|
2012-02-23 22:30:31 +04:00
|
|
|
|
if new_children:
|
|
|
|
|
# bottom margin of the last child and bottom margin of this box ...
|
|
|
|
|
if box.height != 'auto':
|
|
|
|
|
# not adjoining. (position_y is not used afterwards.)
|
|
|
|
|
adjoining_margins = []
|
|
|
|
|
else:
|
|
|
|
|
# top and bottom margin of this box
|
2012-05-25 17:57:13 +04:00
|
|
|
|
if box.height in ('auto', 0) and all(v == 0 for v in [
|
|
|
|
|
box.min_height, box.border_top_width, box.padding_top,
|
|
|
|
|
box.border_bottom_width, box.padding_bottom]):
|
2012-02-28 17:50:35 +04:00
|
|
|
|
collapsing_through = True
|
|
|
|
|
else:
|
2012-05-25 17:57:13 +04:00
|
|
|
|
position_y += collapse_margin(adjoining_margins)
|
2012-02-23 22:30:31 +04:00
|
|
|
|
adjoining_margins = []
|
|
|
|
|
|
|
|
|
|
if box.border_bottom_width or box.padding_bottom or (
|
2012-02-28 17:50:48 +04:00
|
|
|
|
establishes_formatting_context(box) or box.is_for_root_element):
|
2012-02-23 22:30:31 +04:00
|
|
|
|
position_y += collapse_margin(adjoining_margins)
|
|
|
|
|
adjoining_margins = []
|
|
|
|
|
|
2012-05-21 16:22:32 +04:00
|
|
|
|
new_box = box.copy_with_children(
|
|
|
|
|
new_children, is_start=is_start, is_end=resume_at is None)
|
2011-10-03 20:57:26 +04:00
|
|
|
|
|
2012-02-23 22:30:31 +04:00
|
|
|
|
# TODO: See corner cases in
|
|
|
|
|
# http://www.w3.org/TR/CSS21/visudet.html#normal-block
|
2011-08-22 20:14:37 +04:00
|
|
|
|
if new_box.height == 'auto':
|
2012-02-23 22:30:31 +04:00
|
|
|
|
new_box.height = position_y - new_box.content_box_y()
|
2012-04-06 18:42:06 +04:00
|
|
|
|
new_box.height = max(
|
|
|
|
|
min(new_box.height, new_box.max_height),
|
|
|
|
|
new_box.min_height)
|
2011-08-22 20:14:37 +04:00
|
|
|
|
|
2012-05-10 15:56:21 +04:00
|
|
|
|
if new_box.style.position == 'relative':
|
2012-05-10 07:57:06 +04:00
|
|
|
|
# New containing block, resolve the layout of the absolute descendants
|
|
|
|
|
for absolute_box in absolute_boxes:
|
2012-05-10 22:12:47 +04:00
|
|
|
|
absolute_layout(document, absolute_box, new_box)
|
2012-05-10 07:57:06 +04:00
|
|
|
|
|
2012-05-11 21:31:31 +04:00
|
|
|
|
for child in new_box.children:
|
2012-05-25 17:57:13 +04:00
|
|
|
|
relative_positioning(child, (new_box.width, new_box.height))
|
2012-05-11 21:31:31 +04:00
|
|
|
|
|
2012-02-28 17:50:35 +04:00
|
|
|
|
return new_box, resume_at, next_page, adjoining_margins, collapsing_through
|
2011-11-22 16:06:50 +04:00
|
|
|
|
|
|
|
|
|
|
2012-02-23 15:51:19 +04:00
|
|
|
|
def collapse_margin(adjoining_margins):
|
|
|
|
|
"""Return the amount of collapsed margin for a list of adjoining margins.
|
|
|
|
|
"""
|
|
|
|
|
# Add 0 to make sure that neither max() or min() get an empty list
|
|
|
|
|
margins = [0]
|
|
|
|
|
margins.extend(adjoining_margins)
|
|
|
|
|
positives = (m for m in margins if m >= 0)
|
|
|
|
|
negatives = (m for m in margins if m <= 0)
|
|
|
|
|
return max(positives) + min(negatives)
|
2012-02-23 22:30:31 +04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def establishes_formatting_context(box):
|
|
|
|
|
"""Return wether a box establishes a block formatting context.
|
|
|
|
|
|
|
|
|
|
See http://www.w3.org/TR/CSS2/visuren.html#block-formatting
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
return box.is_floated() or box.is_absolutely_positioned() or (
|
|
|
|
|
isinstance(box, boxes.BlockContainerBox)
|
|
|
|
|
and not isinstance(box, boxes.BlockBox)
|
|
|
|
|
) or (
|
|
|
|
|
isinstance(box, boxes.BlockBox) and box.style.overflow != 'visible'
|
|
|
|
|
)
|
2012-03-14 19:26:20 +04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def forced_page_break(sibling_before, sibling_after):
|
|
|
|
|
"""Return the value of ``page-break-before`` or ``page-break-after``
|
|
|
|
|
that "wins" for boxes that meet at the margin between two sibling boxes.
|
|
|
|
|
|
|
|
|
|
For boxes before the margin, the 'page-break-after' value is considered;
|
|
|
|
|
for boxes after the margin the 'page-break-before' value is considered.
|
|
|
|
|
|
|
|
|
|
Return (break_here, next_page) where break_here is a boolean, and
|
|
|
|
|
next_page the side the next page should be on: 'left', 'right', or 'any'
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
counts = dict(
|
|
|
|
|
avoid=False,
|
|
|
|
|
auto=False,
|
|
|
|
|
left=False,
|
|
|
|
|
right=False,
|
|
|
|
|
always=False,
|
|
|
|
|
)
|
|
|
|
|
for box, index, property_name in [
|
|
|
|
|
(sibling_before, -1, 'page_break_after'),
|
|
|
|
|
(sibling_after, 0, 'page_break_before')]:
|
|
|
|
|
while 1:
|
|
|
|
|
counts[box.style[property_name]] = True
|
|
|
|
|
if not getattr(box, 'children', None):
|
|
|
|
|
break
|
|
|
|
|
box = box.children[index]
|
|
|
|
|
if not isinstance(box, boxes.BlockLevelBox):
|
|
|
|
|
break
|
|
|
|
|
left = counts['left']
|
|
|
|
|
right = counts['right']
|
|
|
|
|
if left and right:
|
|
|
|
|
# Nonsense. Just do a single page break
|
|
|
|
|
return True, 'any'
|
|
|
|
|
if left:
|
|
|
|
|
return True, 'left'
|
|
|
|
|
if right:
|
|
|
|
|
return True, 'right'
|
|
|
|
|
if counts['always']:
|
|
|
|
|
return True, 'any'
|
|
|
|
|
return False, 'any'
|
|
|
|
|
# TODO: support 'avoid'
|