Skip to content

Commit

Permalink
Refactor bidi horizontal position using mixin
Browse files Browse the repository at this point in the history
  • Loading branch information
kygoh committed Feb 10, 2024
1 parent 8ef14df commit d29ba8c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 23 deletions.
15 changes: 13 additions & 2 deletions weasyprint/formatting_structure/boxes.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@
from ..css import computed_from_cascaded


class BidiAware:
def content_box_x(self):
"""Absolute horizontal position of the content box."""
if self.style['direction'] == 'rtl':
return self.position_x + self.margin_right + self.padding_right + \
self.border_right_width
else:
return self.position_x + self.margin_left + self.padding_left + \
self.border_left_width


class Box:
"""Abstract base class for all boxes."""
# Definitions for the rules generating anonymous table boxes
Expand Down Expand Up @@ -565,7 +576,7 @@ class InlineReplacedBox(ReplacedBox, AtomicInlineLevelBox):
"""


class TableBox(BlockLevelBox, ParentBox):
class TableBox(BidiAware, BlockLevelBox, ParentBox):
"""Box for elements with ``display: table``"""
# Definitions for the rules generating anonymous table boxes
# https://www.w3.org/TR/CSS21/tables.html#anonymous-boxes
Expand Down Expand Up @@ -674,7 +685,7 @@ def span(self):
return 1


class TableCellBox(BlockContainerBox):
class TableCellBox(BidiAware, BlockContainerBox):
"""Box for elements with ``display: table-cell``"""
internal_table_or_caption = True

Expand Down
14 changes: 3 additions & 11 deletions weasyprint/layout/float.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,17 +155,9 @@ def avoid_collisions(context, box, containing_block, outer=True):
if shape.style['float'] == 'right']

# Set the default maximum bounds
rtl = containing_block.style['direction'] == 'rtl'
if isinstance(containing_block, boxes.TableCellBox) and rtl:
cb = containing_block
cb_x = cb.position_x + cb.margin_right + cb.padding_right + \
cb.border_right_width
max_left_bound = cb_x
max_right_bound = cb_x + containing_block.width
else:
max_left_bound = containing_block.content_box_x()
max_right_bound = \
containing_block.content_box_x() + containing_block.width
max_left_bound = containing_block.content_box_x()
max_right_bound = \
containing_block.content_box_x() + containing_block.width

if not outer:
max_left_bound += box.margin_left
Expand Down
13 changes: 3 additions & 10 deletions weasyprint/layout/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,17 @@ def table_layout(context, table, bottom_space, skip_stack, containing_block,
border_spacing_x, border_spacing_y = table.style['border_spacing']

column_positions = table.column_positions = []

if table.style['direction'] == 'rtl':
content_box_x = table.position_x + table.margin_right + \
table.padding_right + table.border_right_width
else:
content_box_x = table.content_box_x()

rows_left_x = content_box_x + border_spacing_x
rows_left_x = table.content_box_x() + border_spacing_x
if table.style['direction'] == 'ltr':
position_x = content_box_x
position_x = table.content_box_x()
rows_x = position_x + border_spacing_x
for width in column_widths:
position_x += border_spacing_x
column_positions.append(position_x)
position_x += width
rows_width = position_x - rows_x
else:
position_x = content_box_x + table.width
position_x = table.content_box_x() + table.width
rows_x = position_x - border_spacing_x
for width in column_widths:
position_x -= border_spacing_x
Expand Down

0 comments on commit d29ba8c

Please sign in to comment.