2015-11-25 10:38:01 +03:00
|
|
|
|
# coding: utf-8
|
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
|
|
|
|
|
2014-01-10 18:27:02 +04:00
|
|
|
|
:copyright: Copyright 2011-2014 Simon Sapin and contributors, see AUTHORS.
|
2012-03-22 02:19:27 +04:00
|
|
|
|
: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
|
|
|
|
|
2016-08-15 00:12:30 +03:00
|
|
|
|
from math import floor
|
|
|
|
|
|
2017-03-25 02:33:36 +03:00
|
|
|
|
from ..compat import izip, xrange
|
|
|
|
|
from ..formatting_structure import boxes
|
|
|
|
|
from .absolute import AbsolutePlaceholder, absolute_layout
|
|
|
|
|
from .float import avoid_collisions, float_layout, get_clearance
|
|
|
|
|
from .inlines import (
|
|
|
|
|
iter_line_boxes, min_max_auto_replaced, replaced_box_height,
|
|
|
|
|
replaced_box_width)
|
2011-08-22 17:37:10 +04:00
|
|
|
|
from .markers import list_marker_layout
|
2012-06-04 21:22:18 +04:00
|
|
|
|
from .min_max import handle_min_max_width
|
2012-05-10 07:57:06 +04:00
|
|
|
|
from .percentages import resolve_percentages, resolve_position_percentages
|
2017-03-25 02:33:36 +03:00
|
|
|
|
from .tables import table_layout, table_wrapper_width
|
2011-07-22 18:34:32 +04:00
|
|
|
|
|
2011-08-22 13:38:54 +04:00
|
|
|
|
|
2012-07-12 19:13:21 +04:00
|
|
|
|
def block_level_layout(context, box, max_position_y, skip_stack,
|
2012-02-23 22:30:31 +04:00
|
|
|
|
containing_block, device_size, page_is_empty,
|
2012-06-06 14:04:09 +04:00
|
|
|
|
absolute_boxes, fixed_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
|
|
|
|
"""
|
2011-11-22 16:06:50 +04:00
|
|
|
|
if isinstance(box, boxes.TableBox):
|
2012-05-09 21:01:32 +04:00
|
|
|
|
return table_layout(
|
2012-07-12 19:13:21 +04:00
|
|
|
|
context, box, max_position_y, skip_stack, containing_block,
|
2012-06-06 14:04:09 +04:00
|
|
|
|
device_size, page_is_empty, absolute_boxes, fixed_boxes)
|
2012-06-05 19:14:33 +04:00
|
|
|
|
|
|
|
|
|
resolve_percentages(box, containing_block)
|
|
|
|
|
|
|
|
|
|
if box.margin_top == 'auto':
|
|
|
|
|
box.margin_top = 0
|
|
|
|
|
if box.margin_bottom == 'auto':
|
|
|
|
|
box.margin_bottom = 0
|
|
|
|
|
|
|
|
|
|
collapsed_margin = collapse_margin(adjoining_margins + [box.margin_top])
|
2012-07-12 19:13:21 +04:00
|
|
|
|
box.clearance = get_clearance(context, box, collapsed_margin)
|
2012-06-19 16:52:30 +04:00
|
|
|
|
if box.clearance is not None:
|
|
|
|
|
top_border_edge = box.position_y + collapsed_margin + box.clearance
|
2012-06-05 19:14:33 +04:00
|
|
|
|
box.position_y = top_border_edge - box.margin_top
|
|
|
|
|
adjoining_margins = []
|
|
|
|
|
|
|
|
|
|
if isinstance(box, boxes.BlockBox):
|
2016-08-15 00:12:30 +03:00
|
|
|
|
style = box.style
|
|
|
|
|
if style.column_width != 'auto' or style.column_count != 'auto':
|
|
|
|
|
return columns_layout(
|
|
|
|
|
context, box, max_position_y, skip_stack, containing_block,
|
2016-08-30 14:35:23 +03:00
|
|
|
|
device_size, page_is_empty, absolute_boxes, fixed_boxes,
|
|
|
|
|
adjoining_margins)
|
2016-08-15 00:12:30 +03:00
|
|
|
|
else:
|
|
|
|
|
return block_box_layout(
|
|
|
|
|
context, box, max_position_y, skip_stack, containing_block,
|
|
|
|
|
device_size, page_is_empty, absolute_boxes, fixed_boxes,
|
|
|
|
|
adjoining_margins)
|
2011-12-05 17:24:43 +04:00
|
|
|
|
elif isinstance(box, boxes.BlockReplacedBox):
|
2012-06-25 14:47:15 +04:00
|
|
|
|
box = block_replaced_box_layout(box, containing_block, device_size)
|
2012-06-25 12:17:39 +04:00
|
|
|
|
# Don't collide with floats
|
|
|
|
|
# http://www.w3.org/TR/CSS21/visuren.html#floats
|
|
|
|
|
box.position_x, box.position_y, _ = avoid_collisions(
|
2012-07-12 19:13:21 +04:00
|
|
|
|
context, box, containing_block, outer=False)
|
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__)
|
|
|
|
|
|
|
|
|
|
|
2012-07-12 19:13:21 +04:00
|
|
|
|
def block_box_layout(context, box, max_position_y, skip_stack,
|
2012-02-23 22:30:31 +04:00
|
|
|
|
containing_block, device_size, page_is_empty,
|
2012-06-06 14:04:09 +04:00
|
|
|
|
absolute_boxes, fixed_boxes, adjoining_margins):
|
2011-08-24 12:25:45 +04:00
|
|
|
|
"""Lay out the block ``box``."""
|
2012-05-11 22:50:28 +04:00
|
|
|
|
if box.is_table_wrapper:
|
2012-06-02 10:28:41 +04:00
|
|
|
|
table_wrapper_width(
|
2012-07-12 19:13:21 +04:00
|
|
|
|
context, box, (containing_block.width, containing_block.height))
|
2012-06-25 14:47:15 +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-07-12 19:13:21 +04:00
|
|
|
|
context, box, max_position_y, skip_stack, device_size,
|
2012-06-06 14:04:09 +04:00
|
|
|
|
page_is_empty, absolute_boxes, fixed_boxes, adjoining_margins)
|
2012-06-25 12:21:42 +04:00
|
|
|
|
if new_box and new_box.is_table_wrapper:
|
2012-06-25 12:17:39 +04:00
|
|
|
|
# Don't collide with floats
|
|
|
|
|
# http://www.w3.org/TR/CSS21/visuren.html#floats
|
|
|
|
|
position_x, position_y, _ = avoid_collisions(
|
2012-07-12 19:13:21 +04:00
|
|
|
|
context, new_box, containing_block, outer=False)
|
2012-06-25 12:17:39 +04:00
|
|
|
|
new_box.translate(
|
|
|
|
|
position_x - new_box.position_x, position_y - new_box.position_y)
|
2012-07-12 19:13:21 +04:00
|
|
|
|
list_marker_layout(context, 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
|
|
|
|
|
|
|
|
|
|
2016-08-15 00:12:30 +03:00
|
|
|
|
def columns_layout(context, box, max_position_y, skip_stack, containing_block,
|
2016-08-30 14:35:23 +03:00
|
|
|
|
device_size, page_is_empty, absolute_boxes, fixed_boxes,
|
|
|
|
|
adjoining_margins):
|
2016-08-15 00:12:30 +03:00
|
|
|
|
"""Lay out a multi-column ``box``."""
|
|
|
|
|
# Implementation of the multi-column pseudo-algorithm:
|
|
|
|
|
# https://www.w3.org/TR/css3-multicol/#pseudo-algorithm
|
|
|
|
|
count = None
|
|
|
|
|
width = None
|
|
|
|
|
style = box.style
|
2016-08-30 01:57:44 +03:00
|
|
|
|
|
|
|
|
|
if box.style.position == 'relative':
|
|
|
|
|
# New containing block, use a new absolute list
|
|
|
|
|
absolute_boxes = []
|
|
|
|
|
|
2016-12-16 19:37:39 +03:00
|
|
|
|
box = box.copy_with_children(box.children)
|
|
|
|
|
|
2016-08-15 03:38:14 +03:00
|
|
|
|
# TODO: the available width can be unknown if the containing block needs
|
2016-08-15 20:22:40 +03:00
|
|
|
|
# the size of this block to know its own size.
|
2016-08-15 03:38:14 +03:00
|
|
|
|
block_level_width(box, containing_block)
|
|
|
|
|
available_width = box.width
|
2016-08-15 00:12:30 +03:00
|
|
|
|
if count is None:
|
|
|
|
|
if style.column_width == 'auto' and style.column_count != 'auto':
|
|
|
|
|
count = style.column_count
|
|
|
|
|
width = max(
|
|
|
|
|
0, available_width - (count - 1) * style.column_gap) / count
|
|
|
|
|
elif style.column_width != 'auto' and style.column_count == 'auto':
|
2016-12-15 11:02:14 +03:00
|
|
|
|
count = max(1, int(floor(
|
2016-08-15 00:12:30 +03:00
|
|
|
|
(available_width + style.column_gap) /
|
2016-12-15 11:02:14 +03:00
|
|
|
|
(style.column_width + style.column_gap))))
|
2016-08-15 00:12:30 +03:00
|
|
|
|
width = (
|
|
|
|
|
(available_width + style.column_gap) / count -
|
|
|
|
|
style.column_gap)
|
|
|
|
|
else:
|
2016-12-15 11:02:14 +03:00
|
|
|
|
count = min(style.column_count, int(floor(
|
2016-08-15 00:12:30 +03:00
|
|
|
|
(available_width + style.column_gap) /
|
2016-12-15 11:02:14 +03:00
|
|
|
|
(style.column_width + style.column_gap))))
|
2016-08-15 00:12:30 +03:00
|
|
|
|
width = (
|
|
|
|
|
(available_width + style.column_gap) / count -
|
|
|
|
|
style.column_gap)
|
|
|
|
|
|
2016-08-15 03:38:14 +03:00
|
|
|
|
def create_column_box():
|
2016-08-30 01:57:44 +03:00
|
|
|
|
column_box = box.anonymous_from(box, children=[
|
|
|
|
|
child.copy() for child in box.children])
|
2016-08-15 03:38:14 +03:00
|
|
|
|
resolve_percentages(column_box, containing_block)
|
|
|
|
|
column_box.width = width
|
|
|
|
|
column_box.position_x = box.content_box_x()
|
|
|
|
|
column_box.position_y = box.content_box_y()
|
|
|
|
|
return column_box
|
2016-08-15 00:12:30 +03:00
|
|
|
|
|
2016-08-15 20:22:40 +03:00
|
|
|
|
def column_descendants(box):
|
2016-08-15 21:11:01 +03:00
|
|
|
|
# TODO: this filtering condition is probably wrong
|
|
|
|
|
if isinstance(box, (boxes.TableBox, boxes.LineBox, boxes.ReplacedBox)):
|
|
|
|
|
yield box
|
|
|
|
|
if hasattr(box, 'descendants') and box.is_in_normal_flow():
|
|
|
|
|
for child in box.children:
|
2016-08-15 20:22:40 +03:00
|
|
|
|
for grand_child in column_descendants(child):
|
|
|
|
|
yield grand_child
|
|
|
|
|
|
2016-08-15 18:15:57 +03:00
|
|
|
|
# Balance.
|
|
|
|
|
#
|
|
|
|
|
# The current algorithm starts from the ideal height (the total height
|
|
|
|
|
# divided by the number of columns). We then iterate until the last column
|
|
|
|
|
# is not the highest one. At the end of each loop, we add the minimal
|
|
|
|
|
# height needed to make one direct child at the top of one column go to the
|
|
|
|
|
# end of the previous column.
|
|
|
|
|
#
|
|
|
|
|
# We must probably rely on a real rendering for each loop, but with a
|
|
|
|
|
# stupid algorithm like this it can last minutes.
|
|
|
|
|
#
|
|
|
|
|
# TODO: Rewrite this!
|
|
|
|
|
# - We assume that the children are normal lines or blocks.
|
|
|
|
|
# - We ignore the forced and avoided column breaks.
|
2016-08-30 18:54:15 +03:00
|
|
|
|
|
|
|
|
|
# Find the total height of the content
|
2016-08-15 00:12:30 +03:00
|
|
|
|
original_max_position_y = max_position_y
|
2016-08-30 18:54:15 +03:00
|
|
|
|
column_box = create_column_box()
|
2016-12-16 19:37:39 +03:00
|
|
|
|
new_child, _, _, _, _ = block_box_layout(
|
2016-08-30 18:54:15 +03:00
|
|
|
|
context, column_box, float('inf'), skip_stack, containing_block,
|
|
|
|
|
device_size, page_is_empty, [], [], [])
|
|
|
|
|
height = new_child.margin_height()
|
2016-08-15 00:12:30 +03:00
|
|
|
|
if style.column_fill == 'balance':
|
2016-08-30 18:54:15 +03:00
|
|
|
|
height /= count
|
|
|
|
|
box_column_descendants = list(column_descendants(new_child))
|
|
|
|
|
# Increase the column height step by step.
|
|
|
|
|
while True:
|
|
|
|
|
i = 0
|
|
|
|
|
lost_spaces = []
|
|
|
|
|
column_top = new_child.content_box_y()
|
|
|
|
|
for child in box_column_descendants:
|
|
|
|
|
child_height = child.margin_height()
|
|
|
|
|
child_bottom = child.position_y + child_height - column_top
|
|
|
|
|
if child_bottom > height:
|
|
|
|
|
if i < count - 1:
|
|
|
|
|
lost_spaces.append(child_bottom - height)
|
|
|
|
|
i += 1
|
|
|
|
|
column_top = child.position_y
|
|
|
|
|
else:
|
|
|
|
|
break
|
|
|
|
|
else:
|
|
|
|
|
break
|
|
|
|
|
height += min(lost_spaces)
|
|
|
|
|
# TODO: check box.style.max-height
|
|
|
|
|
max_position_y = min(max_position_y, box.content_box_y() + height)
|
2016-08-15 00:12:30 +03:00
|
|
|
|
|
|
|
|
|
# Replace the current box children with columns
|
2016-08-15 03:38:14 +03:00
|
|
|
|
children = []
|
2016-12-17 02:43:55 +03:00
|
|
|
|
if box.children:
|
|
|
|
|
for i in range(count):
|
|
|
|
|
if i == count - 1:
|
|
|
|
|
max_position_y = original_max_position_y
|
|
|
|
|
column_box = create_column_box()
|
|
|
|
|
column_box.position_x += i * (width + style.column_gap)
|
|
|
|
|
new_child, skip_stack, next_page, _, _ = block_box_layout(
|
|
|
|
|
context, column_box, max_position_y, skip_stack,
|
|
|
|
|
containing_block, device_size, page_is_empty, absolute_boxes,
|
|
|
|
|
fixed_boxes, None)
|
|
|
|
|
if new_child is None:
|
|
|
|
|
break
|
|
|
|
|
children.append(new_child)
|
|
|
|
|
if skip_stack is None:
|
|
|
|
|
break
|
|
|
|
|
else:
|
|
|
|
|
next_page = 'any'
|
|
|
|
|
skip_stack = None
|
2016-08-15 03:38:14 +03:00
|
|
|
|
|
2016-08-30 01:57:44 +03:00
|
|
|
|
# Set the height of box and the columns
|
2016-08-15 03:38:14 +03:00
|
|
|
|
box.children = children
|
|
|
|
|
if box.children:
|
2016-08-30 18:54:15 +03:00
|
|
|
|
heights = [child.margin_height() for child in box.children]
|
|
|
|
|
if box.height != 'auto':
|
|
|
|
|
heights.append(box.height)
|
|
|
|
|
if box.min_height != 'auto':
|
|
|
|
|
heights.append(box.min_height)
|
|
|
|
|
box.height = max(heights)
|
2016-08-15 03:38:14 +03:00
|
|
|
|
for child in box.children:
|
2016-08-30 14:35:23 +03:00
|
|
|
|
child.height = box.margin_height()
|
2016-08-15 00:12:30 +03:00
|
|
|
|
else:
|
2016-08-15 03:38:14 +03:00
|
|
|
|
box.height = 0
|
2016-08-15 00:12:30 +03:00
|
|
|
|
|
2016-08-30 01:57:44 +03:00
|
|
|
|
if box.style.position == 'relative':
|
|
|
|
|
# New containing block, resolve the layout of the absolute descendants
|
|
|
|
|
for absolute_box in absolute_boxes:
|
|
|
|
|
absolute_layout(context, absolute_box, box, fixed_boxes)
|
2016-08-15 20:22:40 +03:00
|
|
|
|
|
2016-12-16 19:37:39 +03:00
|
|
|
|
return box, skip_stack, next_page, [0], False
|
2016-08-15 00:12:30 +03:00
|
|
|
|
|
|
|
|
|
|
2013-04-02 14:41:52 +04:00
|
|
|
|
@handle_min_max_width
|
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
|
2013-04-02 14:41:52 +04:00
|
|
|
|
replaced_box_width.without_min_max(box, device_size)
|
|
|
|
|
block_level_width.without_min_max(box, containing_block)
|
2012-04-06 21:44:09 +04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def block_replaced_box_layout(box, containing_block, device_size):
|
|
|
|
|
"""Lay out the block :class:`boxes.ReplacedBox` ``box``."""
|
|
|
|
|
if box.style.width == 'auto' and box.style.height == 'auto':
|
2013-04-02 14:41:52 +04:00
|
|
|
|
computed_margins = box.margin_left, box.margin_right
|
2013-04-11 14:08:53 +04:00
|
|
|
|
block_replaced_width.without_min_max(
|
|
|
|
|
box, containing_block, device_size)
|
2013-04-02 14:41:52 +04:00
|
|
|
|
replaced_box_height.without_min_max(box, device_size)
|
2012-04-06 21:44:09 +04:00
|
|
|
|
min_max_auto_replaced(box)
|
2013-04-02 14:41:52 +04:00
|
|
|
|
box.margin_left, box.margin_right = computed_margins
|
|
|
|
|
block_level_width.without_min_max(box, containing_block)
|
2012-04-06 21:44:09 +04:00
|
|
|
|
else:
|
2013-04-02 14:41:52 +04:00
|
|
|
|
block_replaced_width(box, containing_block, device_size)
|
|
|
|
|
replaced_box_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':
|
2013-04-09 20:30:25 +04:00
|
|
|
|
# The equation is over-constrained.
|
|
|
|
|
if containing_block.style.direction == 'rtl':
|
|
|
|
|
box.position_x += (
|
2013-04-15 03:30:05 +04:00
|
|
|
|
cb_width - paddings_plus_borders - width - margin_r - margin_l)
|
2013-04-09 20:30:25 +04:00
|
|
|
|
# Do nothing in ltr.
|
2011-07-22 18:34:32 +04:00
|
|
|
|
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-07-12 19:13:21 +04:00
|
|
|
|
def block_container_layout(context, box, max_position_y, skip_stack,
|
2012-05-09 21:01:32 +04:00
|
|
|
|
device_size, page_is_empty, absolute_boxes,
|
2012-06-06 14:04:09 +04:00
|
|
|
|
fixed_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
|
|
|
|
|
|
2014-04-27 15:29:55 +04:00
|
|
|
|
# if box.style.overflow != 'visible':
|
|
|
|
|
# ...
|
2011-07-22 18:34:32 +04:00
|
|
|
|
|
2012-05-31 22:03:10 +04:00
|
|
|
|
# See http://www.w3.org/TR/CSS21/visuren.html#block-formatting
|
|
|
|
|
if not isinstance(box, boxes.BlockBox):
|
2012-07-12 19:13:21 +04:00
|
|
|
|
context.create_block_formatting_context()
|
2012-05-31 22:03:10 +04:00
|
|
|
|
|
2012-10-02 15:29:57 +04:00
|
|
|
|
is_start = skip_stack is None
|
|
|
|
|
if not is_start:
|
|
|
|
|
# Remove top margin, border and padding:
|
2016-11-01 06:31:15 +03:00
|
|
|
|
box._remove_decoration(start=True, end=False)
|
2012-10-02 15:29:57 +04:00
|
|
|
|
|
2012-02-23 22:30:31 +04:00
|
|
|
|
if adjoining_margins is None:
|
|
|
|
|
adjoining_margins = []
|
|
|
|
|
|
|
|
|
|
adjoining_margins.append(box.margin_top)
|
2012-06-28 02:51:24 +04:00
|
|
|
|
this_box_adjoining_margins = adjoining_margins
|
2012-02-23 22:30:31 +04:00
|
|
|
|
|
2013-04-11 14:08:53 +04:00
|
|
|
|
collapsing_with_children = not (
|
2016-01-15 14:47:03 +03:00
|
|
|
|
box.border_top_width or box.padding_top 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
|
2012-10-02 15:29:57 +04:00
|
|
|
|
# (There are not padding or borders, see above.)
|
2012-02-23 22:30:31 +04:00
|
|
|
|
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-06-25 21:48:22 +04:00
|
|
|
|
last_in_flow_child = None
|
|
|
|
|
|
2012-05-21 16:22:32 +04:00
|
|
|
|
if is_start:
|
2012-06-21 21:05:54 +04:00
|
|
|
|
skip = 0
|
2017-01-02 15:44:03 +03:00
|
|
|
|
first_letter_style = getattr(box, 'first_letter_style', None)
|
2011-09-30 21:04:05 +04:00
|
|
|
|
else:
|
2012-06-21 21:05:54 +04:00
|
|
|
|
skip, skip_stack = skip_stack
|
2017-01-02 15:23:42 +03:00
|
|
|
|
first_letter_style = None
|
2012-06-21 21:05:54 +04:00
|
|
|
|
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-06-06 11:49:56 +04:00
|
|
|
|
if child.is_absolutely_positioned():
|
2012-05-25 14:03:48 +04:00
|
|
|
|
placeholder = AbsolutePlaceholder(child)
|
2013-03-18 18:26:11 +04:00
|
|
|
|
placeholder.index = index
|
2012-06-06 11:49:56 +04:00
|
|
|
|
new_children.append(placeholder)
|
2012-06-06 14:04:09 +04:00
|
|
|
|
if child.style.position == 'absolute':
|
|
|
|
|
absolute_boxes.append(placeholder)
|
|
|
|
|
else:
|
|
|
|
|
fixed_boxes.append(placeholder)
|
2012-06-19 02:00:28 +04:00
|
|
|
|
elif child.is_floated():
|
2013-03-18 18:26:11 +04:00
|
|
|
|
new_child = float_layout(
|
2016-04-25 10:55:25 +03:00
|
|
|
|
context, child, box, device_size, absolute_boxes,
|
|
|
|
|
fixed_boxes)
|
2014-04-27 11:48:46 +04:00
|
|
|
|
# New page if overflow
|
2014-04-27 15:29:55 +04:00
|
|
|
|
if (page_is_empty and not new_children) or not (
|
2016-01-15 14:47:03 +03:00
|
|
|
|
new_child.position_y + new_child.height >
|
|
|
|
|
max_position_y):
|
2014-04-17 16:02:04 +04:00
|
|
|
|
new_child.index = index
|
|
|
|
|
new_children.append(new_child)
|
|
|
|
|
else:
|
2014-05-28 13:19:41 +04:00
|
|
|
|
|
|
|
|
|
for previous_child in reversed(new_children):
|
|
|
|
|
if previous_child.is_in_normal_flow():
|
|
|
|
|
last_in_flow_child = previous_child
|
|
|
|
|
break
|
|
|
|
|
if new_children and block_level_page_break(
|
2017-03-25 02:24:27 +03:00
|
|
|
|
last_in_flow_child, child) == 'avoid':
|
2014-05-27 19:22:31 +04:00
|
|
|
|
result = find_earlier_page_break(
|
|
|
|
|
new_children, absolute_boxes, fixed_boxes)
|
|
|
|
|
if result:
|
|
|
|
|
new_children, resume_at = result
|
|
|
|
|
break
|
2014-04-17 16:02:04 +04:00
|
|
|
|
resume_at = (index, None)
|
2014-04-27 15:29:55 +04:00
|
|
|
|
break
|
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(
|
2012-07-12 19:13:21 +04:00
|
|
|
|
context, child, position_y, skip_stack,
|
2017-01-02 15:23:42 +03:00
|
|
|
|
new_containing_block, device_size, absolute_boxes, fixed_boxes,
|
|
|
|
|
first_letter_style)
|
2012-03-14 22:33:24 +04:00
|
|
|
|
is_page_break = False
|
|
|
|
|
for line, resume_at in lines_iterator:
|
2012-06-20 14:53:22 +04:00
|
|
|
|
line.resume_at = resume_at
|
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
|
2012-07-12 19:13:21 +04:00
|
|
|
|
# page and can advance in the context.
|
2012-03-14 22:33:24 +04:00
|
|
|
|
if new_position_y > max_position_y and (
|
2012-06-20 14:53:22 +04:00
|
|
|
|
new_children or not page_is_empty):
|
|
|
|
|
over_orphans = len(new_children) - box.style.orphans
|
2012-03-14 22:33:24 +04:00
|
|
|
|
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
|
2012-06-20 14:53:22 +04:00
|
|
|
|
del new_children[-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-06-20 14:53:22 +04:00
|
|
|
|
new_children.append(line)
|
2011-10-03 19:55:41 +04:00
|
|
|
|
position_y = new_position_y
|
|
|
|
|
skip_stack = resume_at
|
2012-06-20 14:53:22 +04:00
|
|
|
|
if new_children:
|
|
|
|
|
resume_at = (index, new_children[-1].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-06-20 14:53:22 +04:00
|
|
|
|
for previous_child in reversed(new_children):
|
|
|
|
|
if previous_child.is_in_normal_flow():
|
|
|
|
|
last_in_flow_child = previous_child
|
|
|
|
|
break
|
|
|
|
|
else:
|
|
|
|
|
last_in_flow_child = None
|
|
|
|
|
if last_in_flow_child is not None:
|
|
|
|
|
# Between in-flow siblings
|
|
|
|
|
page_break = block_level_page_break(last_in_flow_child, child)
|
2016-08-30 14:35:23 +03:00
|
|
|
|
# TODO: take care of text direction and writing mode
|
|
|
|
|
# https://www.w3.org/TR/css3-page/#progression
|
|
|
|
|
if page_break == 'recto':
|
|
|
|
|
page_break = 'right'
|
|
|
|
|
elif page_break == 'verso':
|
|
|
|
|
page_break = 'left'
|
|
|
|
|
if page_break in ('page', 'left', 'right'):
|
2012-06-20 14:53:22 +04:00
|
|
|
|
if page_break in ('left', 'right'):
|
|
|
|
|
next_page = page_break
|
|
|
|
|
else:
|
|
|
|
|
next_page = 'any'
|
2012-03-14 19:26:20 +04:00
|
|
|
|
resume_at = (index, None)
|
|
|
|
|
break
|
2012-06-20 14:53:22 +04:00
|
|
|
|
else:
|
|
|
|
|
page_break = 'auto'
|
2012-01-20 14:55:06 +04:00
|
|
|
|
|
2012-06-28 02:51:24 +04:00
|
|
|
|
new_containing_block = box
|
2012-06-28 06:14:06 +04:00
|
|
|
|
|
2012-06-28 02:51:24 +04:00
|
|
|
|
if not new_containing_block.is_table_wrapper:
|
2012-06-28 06:14:06 +04:00
|
|
|
|
# TODO: there's no collapsing margins inside tables, right?
|
2012-06-28 02:51:24 +04:00
|
|
|
|
resolve_percentages(child, new_containing_block)
|
2016-01-15 14:47:03 +03:00
|
|
|
|
if (child.is_in_normal_flow() and
|
|
|
|
|
last_in_flow_child is None and
|
|
|
|
|
collapsing_with_children):
|
2012-06-28 06:14:06 +04:00
|
|
|
|
# TODO: add the adjoining descendants' margin top to
|
|
|
|
|
# [child.margin_top]
|
|
|
|
|
old_collapsed_margin = collapse_margin(adjoining_margins)
|
|
|
|
|
if child.margin_top == 'auto':
|
|
|
|
|
child_margin_top = 0
|
|
|
|
|
else:
|
|
|
|
|
child_margin_top = child.margin_top
|
|
|
|
|
new_collapsed_margin = collapse_margin(
|
|
|
|
|
adjoining_margins + [child_margin_top])
|
|
|
|
|
collapsed_margin_difference = (
|
|
|
|
|
new_collapsed_margin - old_collapsed_margin)
|
2012-06-25 21:48:22 +04:00
|
|
|
|
for previous_new_child in new_children:
|
|
|
|
|
previous_new_child.translate(
|
2012-06-28 06:14:06 +04:00
|
|
|
|
dy=collapsed_margin_difference)
|
|
|
|
|
clearance = get_clearance(
|
2012-07-12 19:13:21 +04:00
|
|
|
|
context, child, new_collapsed_margin)
|
2012-06-28 06:14:06 +04:00
|
|
|
|
if clearance is not None:
|
|
|
|
|
for previous_new_child in new_children:
|
|
|
|
|
previous_new_child.translate(
|
|
|
|
|
dy=-collapsed_margin_difference)
|
|
|
|
|
|
|
|
|
|
collapsed_margin = collapse_margin(adjoining_margins)
|
|
|
|
|
box.position_y += collapsed_margin - box.margin_top
|
|
|
|
|
# Count box.margin_top as we emptied adjoining_margins
|
|
|
|
|
adjoining_margins = []
|
|
|
|
|
position_y = box.content_box_y()
|
2012-06-25 21:48:22 +04:00
|
|
|
|
|
2013-04-26 17:02:27 +04:00
|
|
|
|
if adjoining_margins and isinstance(child, boxes.TableBox):
|
|
|
|
|
collapsed_margin = collapse_margin(adjoining_margins)
|
|
|
|
|
child.position_y += collapsed_margin
|
|
|
|
|
position_y += collapsed_margin
|
|
|
|
|
adjoining_margins = []
|
|
|
|
|
|
2017-01-02 15:23:42 +03:00
|
|
|
|
if not getattr(child, 'first_letter_style', None):
|
|
|
|
|
child.first_letter_style = first_letter_style
|
2012-02-28 17:50:35 +04:00
|
|
|
|
(new_child, resume_at, next_page, next_adjoining_margins,
|
|
|
|
|
collapsing_through) = block_level_layout(
|
2012-07-12 19:13:21 +04:00
|
|
|
|
context, 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-06-06 14:04:09 +04:00
|
|
|
|
absolute_boxes, fixed_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:
|
2012-06-25 21:48:22 +04:00
|
|
|
|
# index in its non-laid-out parent, not in future new parent
|
2012-06-21 21:05:54 +04:00
|
|
|
|
# May be used in find_earlier_page_break()
|
|
|
|
|
new_child.index = index
|
|
|
|
|
|
2012-02-29 15:23:25 +04:00
|
|
|
|
# We need to do this after the child layout to have the
|
|
|
|
|
# used value for margin_top (eg. it might be a percentage.)
|
2013-04-26 17:02:27 +04:00
|
|
|
|
if not isinstance(
|
|
|
|
|
new_child, (boxes.BlockBox, boxes.TableBox)):
|
2012-02-29 15:23:25 +04:00
|
|
|
|
adjoining_margins.append(new_child.margin_top)
|
2016-01-15 14:47:03 +03:00
|
|
|
|
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 = []
|
2014-04-27 15:29:55 +04:00
|
|
|
|
# else: blocks handle that themselves.
|
2012-02-29 15:23:25 +04:00
|
|
|
|
|
|
|
|
|
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())
|
|
|
|
|
|
2016-01-15 14:47:03 +03:00
|
|
|
|
if (new_position_y > max_position_y and
|
|
|
|
|
(new_children or not page_is_empty) and
|
|
|
|
|
not (isinstance(child, boxes.TableBox) or (
|
|
|
|
|
# For blocks with children do this per child.
|
|
|
|
|
isinstance(child, boxes.BlockBox) and
|
|
|
|
|
child.children))):
|
2012-02-29 15:23:25 +04:00
|
|
|
|
# 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
|
|
|
|
|
|
2012-06-20 15:47:28 +04:00
|
|
|
|
if new_child is not None and new_child.clearance is not None:
|
2012-06-19 02:00:28 +04:00
|
|
|
|
position_y = (
|
|
|
|
|
new_child.border_box_y() + new_child.border_height())
|
|
|
|
|
|
2011-10-13 18:25:08 +04:00
|
|
|
|
if new_child is None:
|
2012-06-20 14:53:22 +04:00
|
|
|
|
# Nothing fits in the remaining space of this page: break
|
2016-08-30 14:35:23 +03:00
|
|
|
|
if page_break in ('avoid', 'avoid-page'):
|
2012-06-20 14:53:22 +04:00
|
|
|
|
result = find_earlier_page_break(
|
2012-06-21 21:05:54 +04:00
|
|
|
|
new_children, absolute_boxes, fixed_boxes)
|
2012-06-20 14:53:22 +04:00
|
|
|
|
if result:
|
|
|
|
|
new_children, resume_at = result
|
|
|
|
|
break
|
|
|
|
|
else:
|
|
|
|
|
# We did not find any page break opportunity
|
|
|
|
|
if not page_is_empty:
|
|
|
|
|
# The page has content *before* this block:
|
|
|
|
|
# cancel the block and try to find a break
|
|
|
|
|
# in the parent.
|
|
|
|
|
return None, None, 'any', [], False
|
|
|
|
|
# else:
|
|
|
|
|
# ignore this 'avoid' and break anyway.
|
|
|
|
|
|
2011-10-13 18:25:08 +04:00
|
|
|
|
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
|
2011-09-30 21:04:05 +04:00
|
|
|
|
else:
|
|
|
|
|
resume_at = None
|
2011-08-22 20:14:37 +04:00
|
|
|
|
|
2016-08-30 14:35:23 +03:00
|
|
|
|
if (resume_at is not None and
|
|
|
|
|
box.style.break_inside in ('avoid', 'avoid-page') and
|
|
|
|
|
not page_is_empty):
|
2012-03-16 19:45:31 +04:00
|
|
|
|
return None, None, 'any', [], False
|
|
|
|
|
|
2012-06-28 06:14:06 +04:00
|
|
|
|
if collapsing_with_children:
|
|
|
|
|
box.position_y += (
|
|
|
|
|
collapse_margin(this_box_adjoining_margins) - box.margin_top)
|
|
|
|
|
|
2012-06-25 21:48:22 +04:00
|
|
|
|
for previous_child in reversed(new_children):
|
|
|
|
|
if previous_child.is_in_normal_flow():
|
|
|
|
|
last_in_flow_child = previous_child
|
|
|
|
|
break
|
|
|
|
|
else:
|
|
|
|
|
last_in_flow_child = None
|
2012-02-28 17:50:35 +04:00
|
|
|
|
collapsing_through = False
|
2012-06-25 21:48:22 +04:00
|
|
|
|
if last_in_flow_child is None:
|
2012-06-18 12:06:18 +04:00
|
|
|
|
collapsed_margin = collapse_margin(adjoining_margins)
|
2012-02-23 22:30:31 +04:00
|
|
|
|
# top and bottom margin of this box
|
2012-06-18 12:06:18 +04:00
|
|
|
|
if (box.height in ('auto', 0) and
|
2012-07-12 19:13:21 +04:00
|
|
|
|
get_clearance(context, box, collapsed_margin) is None and
|
2012-06-18 12:06:18 +04:00
|
|
|
|
all(v == 0 for v in [
|
2012-05-25 17:57:13 +04:00
|
|
|
|
box.min_height, box.border_top_width, box.padding_top,
|
2012-06-18 12:06:18 +04:00
|
|
|
|
box.border_bottom_width, box.padding_bottom])):
|
2012-02-28 17:50:35 +04:00
|
|
|
|
collapsing_through = True
|
|
|
|
|
else:
|
2012-06-18 12:06:18 +04:00
|
|
|
|
position_y += collapsed_margin
|
2012-02-23 22:30:31 +04:00
|
|
|
|
adjoining_margins = []
|
2012-06-25 21:48:22 +04:00
|
|
|
|
else:
|
|
|
|
|
# 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 = []
|
2012-02-23 22:30:31 +04:00
|
|
|
|
|
|
|
|
|
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(
|
2016-11-01 06:31:15 +03:00
|
|
|
|
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
|
2016-05-16 15:56:34 +03:00
|
|
|
|
# TODO: See float.float_layout
|
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()
|
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-07-12 19:13:21 +04:00
|
|
|
|
absolute_layout(context, absolute_box, new_box, fixed_boxes)
|
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-06-01 00:33:28 +04:00
|
|
|
|
if not isinstance(new_box, boxes.BlockBox):
|
2012-07-12 19:13:21 +04:00
|
|
|
|
context.finish_block_formatting_context(new_box)
|
2012-05-31 22:03:10 +04:00
|
|
|
|
|
2012-06-04 21:22:18 +04:00
|
|
|
|
# After finish_block_formatting_context which may increment new_box.height
|
|
|
|
|
new_box.height = max(
|
|
|
|
|
min(new_box.height, new_box.max_height),
|
|
|
|
|
new_box.min_height)
|
|
|
|
|
|
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 (
|
2016-01-15 14:47:03 +03:00
|
|
|
|
isinstance(box, boxes.BlockContainerBox) and
|
|
|
|
|
not isinstance(box, boxes.BlockBox)
|
2012-02-23 22:30:31 +04:00
|
|
|
|
) or (
|
|
|
|
|
isinstance(box, boxes.BlockBox) and box.style.overflow != 'visible'
|
|
|
|
|
)
|
2012-03-14 19:26:20 +04:00
|
|
|
|
|
|
|
|
|
|
2012-06-20 14:53:22 +04:00
|
|
|
|
def block_level_page_break(sibling_before, sibling_after):
|
2012-03-14 19:26:20 +04:00
|
|
|
|
"""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.
|
|
|
|
|
|
2012-06-20 14:53:22 +04:00
|
|
|
|
* 'avoid' takes priority over 'auto'
|
2016-08-30 14:35:23 +03:00
|
|
|
|
* 'page' takes priority over 'avoid' or 'auto'
|
2012-06-20 14:53:22 +04:00
|
|
|
|
* 'left' or 'right' take priority over 'always', 'avoid' or 'auto'
|
|
|
|
|
* Among 'left' and 'right', later values in the tree take priority.
|
|
|
|
|
|
|
|
|
|
See http://dev.w3.org/csswg/css3-page/#allowed-pg-brk
|
2012-03-14 19:26:20 +04:00
|
|
|
|
|
|
|
|
|
"""
|
2012-06-20 14:53:22 +04:00
|
|
|
|
values = []
|
|
|
|
|
box = sibling_before
|
|
|
|
|
while isinstance(box, boxes.BlockLevelBox):
|
2016-08-30 14:35:23 +03:00
|
|
|
|
values.append(box.style.break_after)
|
2012-06-20 14:53:22 +04:00
|
|
|
|
if not (isinstance(box, boxes.ParentBox) and box.children):
|
|
|
|
|
break
|
|
|
|
|
box = box.children[-1]
|
|
|
|
|
values.reverse() # Have them in tree order
|
|
|
|
|
|
|
|
|
|
box = sibling_after
|
|
|
|
|
while isinstance(box, boxes.BlockLevelBox):
|
2016-08-30 14:35:23 +03:00
|
|
|
|
values.append(box.style.break_before)
|
2012-06-20 14:53:22 +04:00
|
|
|
|
if not (isinstance(box, boxes.ParentBox) and box.children):
|
|
|
|
|
break
|
|
|
|
|
box = box.children[0]
|
|
|
|
|
|
|
|
|
|
result = 'auto'
|
|
|
|
|
for value in values:
|
2016-08-30 14:35:23 +03:00
|
|
|
|
if value in ('left', 'right', 'recto', 'verso') or (value, result) in (
|
|
|
|
|
('page', 'auto'),
|
|
|
|
|
('page', 'avoid'),
|
|
|
|
|
('avoid', 'auto'),
|
|
|
|
|
('page', 'avoid-page'),
|
|
|
|
|
('avoid-page', 'auto')):
|
2012-06-20 14:53:22 +04:00
|
|
|
|
result = value
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
2012-06-21 21:05:54 +04:00
|
|
|
|
def find_earlier_page_break(children, absolute_boxes, fixed_boxes):
|
2012-06-20 14:53:22 +04:00
|
|
|
|
"""Because of a `page-break-before: avoid` or a `page-break-after: avoid`
|
|
|
|
|
we need to find an earlier page break opportunity inside `children`.
|
|
|
|
|
|
|
|
|
|
Absolute or fixed placeholders removed from children should also be
|
|
|
|
|
removed from `absolute_boxes` or `fixed_boxes`.
|
|
|
|
|
|
|
|
|
|
Return (new_children, resume_at)
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
if children and isinstance(children[0], boxes.LineBox):
|
|
|
|
|
# Normally `orphans` and `widows` apply to the block container, but
|
|
|
|
|
# line boxes inherit them.
|
|
|
|
|
orphans = children[0].style.orphans
|
|
|
|
|
widows = children[0].style.widows
|
|
|
|
|
index = len(children) - widows # how many lines we keep
|
|
|
|
|
if index < orphans:
|
|
|
|
|
return None
|
|
|
|
|
new_children = children[:index]
|
|
|
|
|
resume_at = (0, new_children[-1].resume_at)
|
|
|
|
|
remove_placeholders(children[index:], absolute_boxes, fixed_boxes)
|
|
|
|
|
return new_children, resume_at
|
|
|
|
|
|
2012-06-21 21:05:54 +04:00
|
|
|
|
previous_in_flow = None
|
2012-06-20 14:53:22 +04:00
|
|
|
|
for index, child in reversed_enumerate(children):
|
|
|
|
|
if child.is_in_normal_flow() and (
|
2016-08-30 14:35:23 +03:00
|
|
|
|
child.style.break_inside not in ('avoid', 'avoid-page')):
|
2012-06-20 14:53:22 +04:00
|
|
|
|
if isinstance(child, boxes.BlockBox):
|
|
|
|
|
result = find_earlier_page_break(
|
|
|
|
|
child.children, absolute_boxes, fixed_boxes)
|
|
|
|
|
if result:
|
|
|
|
|
new_grand_children, resume_at = result
|
2016-11-01 06:31:15 +03:00
|
|
|
|
new_child = child.copy_with_children(new_grand_children)
|
2012-06-22 16:34:36 +04:00
|
|
|
|
new_children = list(children[:index]) + [new_child]
|
2012-06-21 21:05:54 +04:00
|
|
|
|
# Index in the original parent
|
|
|
|
|
resume_at = (new_child.index, resume_at)
|
2013-03-28 21:25:16 +04:00
|
|
|
|
index += 1 # Remove placeholders after child
|
2012-06-20 14:53:22 +04:00
|
|
|
|
break
|
|
|
|
|
elif isinstance(child, boxes.TableBox):
|
2012-06-25 21:48:22 +04:00
|
|
|
|
pass # TODO: find an earlier break between table rows.
|
2012-06-20 14:53:22 +04:00
|
|
|
|
if child.is_in_normal_flow():
|
2012-06-21 21:05:54 +04:00
|
|
|
|
if previous_in_flow is not None and (
|
2016-01-15 14:47:03 +03:00
|
|
|
|
block_level_page_break(child, previous_in_flow) !=
|
|
|
|
|
'avoid'):
|
2012-06-20 14:53:22 +04:00
|
|
|
|
index += 1 # break after child
|
|
|
|
|
new_children = children[:index]
|
2012-06-21 21:05:54 +04:00
|
|
|
|
# Get the index in the original parent
|
|
|
|
|
resume_at = (children[index].index, None)
|
2012-03-14 19:26:20 +04:00
|
|
|
|
break
|
2012-06-21 21:05:54 +04:00
|
|
|
|
previous_in_flow = child
|
2012-06-20 14:53:22 +04:00
|
|
|
|
else:
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
remove_placeholders(children[index:], absolute_boxes, fixed_boxes)
|
|
|
|
|
return new_children, resume_at
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def reversed_enumerate(seq):
|
|
|
|
|
"""Like reversed(list(enumerate(seq))) without copying the whole seq."""
|
|
|
|
|
return izip(reversed(xrange(len(seq))), reversed(seq))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def remove_placeholders(box_list, absolute_boxes, fixed_boxes):
|
|
|
|
|
"""For boxes that have been removed in find_earlier_page_break(),
|
|
|
|
|
also remove the matching placeholders in absolute_boxes and fixed_boxes.
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
for box in box_list:
|
|
|
|
|
if isinstance(box, boxes.ParentBox):
|
|
|
|
|
remove_placeholders(box.children, absolute_boxes, fixed_boxes)
|
2013-12-30 17:43:41 +04:00
|
|
|
|
if box.style.position == 'absolute' and box in absolute_boxes:
|
|
|
|
|
# box is not in absolute_boxes if its parent has position: relative
|
2012-06-20 14:53:22 +04:00
|
|
|
|
absolute_boxes.remove(box)
|
|
|
|
|
elif box.style.position == 'fixed':
|
|
|
|
|
fixed_boxes.remove(box)
|