Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _get_matrix(box):
"""Return the matrix for the CSS transforms on this box.
:returns: a :class:`cairocffi.Matrix` object or :obj:`None`.
"""
# "Transforms apply to block-level and atomic inline-level elements,
# but do not apply to elements which may be split into
# multiple inline-level boxes."
# http://www.w3.org/TR/css3-2d-transforms/#introduction
if box.style['transform'] and not isinstance(box, boxes.InlineBox):
border_width = box.border_width()
border_height = box.border_height()
origin_x, origin_y = box.style['transform_origin']
offset_x = percentage(origin_x, border_width)
offset_y = percentage(origin_y, border_height)
origin_x = box.border_box_x() + offset_x
origin_y = box.border_box_y() + offset_y
matrix = cairo.Matrix()
matrix.translate(origin_x, origin_y)
for name, args in box.style['transform']:
if name == 'scale':
matrix.scale(*args)
elif name == 'rotate':
matrix.rotate(args)
elif name == 'translate':
def min_content_width(context, box, outer=True):
"""Return the min-content width for ``box``.
This is the width by breaking at every line-break opportunity.
"""
if isinstance(box, (boxes.BlockContainerBox, boxes.TableColumnBox)):
if box.is_table_wrapper:
return table_and_columns_preferred_widths(context, box, outer)[0]
else:
return block_min_content_width(context, box, outer)
elif isinstance(box, boxes.TableColumnGroupBox):
return column_group_content_width(context, box)
elif isinstance(box, (boxes.InlineBox, boxes.LineBox)):
return inline_min_content_width(
context, box, outer, is_line_start=True)
elif isinstance(box, boxes.ReplacedBox):
return replaced_min_content_width(box, outer)
else:
raise TypeError(
'min-content width for %s not handled yet' %
type(box).__name__)
is_start = skip_stack is None
if is_start:
skip = 0
else:
skip, skip_stack = skip_stack
for i, child in enumerate(box.children[skip:]):
index = i + skip
if isinstance(child, boxes.BlockLevelBox) and \
child.is_in_normal_flow():
assert skip_stack is None # Should not skip here
block_level_box = child
index += 1 # Resume *after* the block
else:
if isinstance(child, boxes.InlineBox):
recursion = _inner_block_in_inline(child, skip_stack)
skip_stack = None
new_child, block_level_box, resume_at = recursion
else:
assert skip_stack is None # Should not skip here
new_child = block_in_inline(child)
# block_level_box is still None.
if new_child is not child:
changed = True
new_children.append(new_child)
if block_level_box is not None:
resume_at = (index, resume_at)
box = box.copy_with_children(new_children)
break
else:
if changed or skip:
skip_stack = None
while child.text:
next_letter = child.text[0]
category = unicodedata.category(next_letter)
if category not in ('Ps', 'Pe', 'Pi', 'Pf', 'Po'):
if character_found:
break
character_found = True
first_letter += next_letter
child.text = child.text[1:]
if first_letter.lstrip('\n'):
# "This type of initial letter is similar to an
# inline-level element if its 'float' property is 'none',
# otherwise it is similar to a floated element."
if first_letter_style['float'] == 'none':
letter_box = boxes.InlineBox(
'%s::first-letter' % box.element_tag,
first_letter_style, [])
text_box = boxes.TextBox(
'%s::first-letter' % box.element_tag, letter_style,
first_letter)
letter_box.children = (text_box,)
box.children = (letter_box,) + tuple(box.children)
else:
letter_box = boxes.BlockBox(
'%s::first-letter' % box.element_tag,
first_letter_style, [])
letter_box.first_letter_style = None
line_box = boxes.LineBox(
'%s::first-letter' % box.element_tag, letter_style,
[])
letter_box.children = (line_box,)
skip_stack = None
while child.text:
next_letter = child.text[0]
category = unicodedata.category(next_letter)
if category not in ('Ps', 'Pe', 'Pi', 'Pf', 'Po'):
if character_found:
break
character_found = True
first_letter += next_letter
child.text = child.text[1:]
if first_letter.lstrip('\n'):
# "This type of initial letter is similar to an
# inline-level element if its 'float' property is 'none',
# otherwise it is similar to a floated element."
if first_letter_style['float'] == 'none':
letter_box = boxes.InlineBox(
'%s::first-letter' % box.element_tag,
first_letter_style, [])
text_box = boxes.TextBox(
'%s::first-letter' % box.element_tag,
letter_box.style.inherit_from(), first_letter)
letter_box.children = (text_box,)
box.children = (letter_box,) + tuple(box.children)
else:
letter_box = boxes.BlockBox(
'%s::first-letter' % box.element_tag,
first_letter_style, [])
letter_box.first_letter_style = None
line_box = boxes.LineBox(
'%s::first-letter' % box.element_tag,
letter_box.style.inherit_from(), [])
letter_box.children = (line_box,)
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
box.translate(translate_x, translate_y)
if isinstance(box, (boxes.InlineBox, boxes.LineBox)):
for child in box.children:
relative_positioning(child, containing_block)
import copy
import re
import unicodedata
import tinycss2.color3
from .. import html
from ..css import computed_values, properties
from ..logger import LOGGER
from . import boxes, counters
# Maps values of the ``display`` CSS property to box types.
BOX_TYPE_FROM_DISPLAY = {
'block': boxes.BlockBox,
'list-item': boxes.BlockBox,
'inline': boxes.InlineBox,
'inline-block': boxes.InlineBlockBox,
'table': boxes.TableBox,
'inline-table': boxes.InlineTableBox,
'table-row': boxes.TableRowBox,
'table-row-group': boxes.TableRowGroupBox,
'table-header-group': boxes.TableRowGroupBox,
'table-footer-group': boxes.TableRowGroupBox,
'table-column': boxes.TableColumnBox,
'table-column-group': boxes.TableColumnGroupBox,
'table-cell': boxes.TableCellBox,
'table-caption': boxes.TableCaptionBox,
'flex': boxes.FlexBox,
'inline-flex': boxes.InlineFlexBox,
}
# Point 3
for child_context in stacking_context.negative_z_contexts:
draw_stacking_context(context, child_context, enable_hinting)
# Point 4
for block in stacking_context.block_level_boxes:
draw_box_background_and_border(
context, stacking_context.page, block, enable_hinting)
# Point 5
for child_context in stacking_context.float_contexts:
draw_stacking_context(context, child_context, enable_hinting)
# Point 6
if isinstance(box, boxes.InlineBox):
draw_inline_level(
context, stacking_context.page, box, enable_hinting)
# Point 7
for block in [box] + stacking_context.blocks_and_cells:
if block.outside_list_marker:
draw_inline_level(
context, stacking_context.page,
block.outside_list_marker, enable_hinting)
if isinstance(block, boxes.ReplacedBox):
draw_replacedbox(context, block)
else:
for child in block.children:
if isinstance(child, boxes.LineBox):
# TODO: draw inline tables
return
if parent_style['list_style_position'] == 'outside':
marker_box = boxes.BlockBox.anonymous_from(box, children)
# We can safely edit everything that can't be changed by user style
# See https://drafts.csswg.org/css-pseudo-4/#marker-pseudo
marker_box.style['position'] = 'absolute'
if parent_style['direction'] == 'ltr':
translate_x = properties.Dimension(-100, '%')
else:
translate_x = properties.Dimension(100, '%')
translate_y = computed_values.ZERO_PIXELS
marker_box.style['transform'] = (
('translate', (translate_x, translate_y)),)
else:
marker_box = boxes.InlineBox.anonymous_from(box, children)
yield marker_box
def remove_last_whitespace(context, box):
"""Remove in place space characters at the end of a line.
This also reduces the width of the inline parents of the modified text.
"""
ancestors = []
while isinstance(box, (boxes.LineBox, boxes.InlineBox)):
ancestors.append(box)
if not box.children:
return
box = box.children[-1]
if not (isinstance(box, boxes.TextBox) and
box.style['white_space'] in ('normal', 'nowrap', 'pre-line')):
return
new_text = box.text.rstrip(' ')
if new_text:
if len(new_text) == len(box.text):
return
box.text = new_text
new_box, resume, _ = split_text_box(context, box, None, 0)
assert new_box is not None
assert resume is None
space_width = box.width - new_box.width