Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ltr for absolute boxes #1613

Merged
merged 2 commits into from
Mar 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 117 additions & 0 deletions tests/draw/test_absolute.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,3 +335,120 @@ def test_absolute_next_page():
aaaaa
'''
assert_pixels('absolute_next_page', 16, 8, expected_pixels, html)


@assert_no_logs
def test_absolute_rtl_1():
expected_pixels = '''
__________RRRRRR
__________RRRRRR
________________
'''
html = '''
<style>
@font-face {src: url(weasyprint.otf); font-family: weasyprint}
@page {
background: white;
size: 16px 3px;
}
div {
color: red;
direction: rtl;
font-family: weasyprint;
font-size: 2px;
line-height: 1;
position: absolute;
}
</style>
<div>bbb</div>
'''
assert_pixels('absolute_rtl_1', 16, 3, expected_pixels, html)


@assert_no_logs
def test_absolute_rtl_2():
expected_pixels = '''
________________
_________RRRRRR_
_________RRRRRR_
'''
html = '''
<style>
@font-face {src: url(weasyprint.otf); font-family: weasyprint}
@page {
background: white;
size: 16px 3px;
}
div {
color: red;
direction: rtl;
font-family: weasyprint;
font-size: 2px;
line-height: 1;
padding: 1px;
position: absolute;
}
</style>
<div>bbb</div>
'''
assert_pixels('absolute_rtl_2', 16, 3, expected_pixels, html)


@assert_no_logs
def test_absolute_rtl_3():
expected_pixels = '''
________________
RRRRRR__________
RRRRRR__________
'''
html = '''
<style>
@font-face {src: url(weasyprint.otf); font-family: weasyprint}
@page {
background: white;
size: 16px 3px;
}
div {
bottom: 0;
color: red;
direction: rtl;
font-family: weasyprint;
font-size: 2px;
left: 0;
line-height: 1;
position: absolute;
}
</style>
<div>bbb</div>
'''
assert_pixels('absolute_rtl_3', 16, 3, expected_pixels, html)


@assert_no_logs
def test_absolute_rtl_4():
expected_pixels = '''
________________
_________RRRRRR_
_________RRRRRR_
'''
html = '''
<style>
@font-face {src: url(weasyprint.otf); font-family: weasyprint}
@page {
background: white;
size: 16px 3px;
}
div {
color: red;
direction: rtl;
font-family: weasyprint;
font-size: 2px;
line-height: 1;
position: absolute;
right: 1px;
top: 1px;
}
</style>
<div>bbb</div>
'''
assert_pixels('absolute_rtl_4', 16, 3, expected_pixels, html)
25 changes: 19 additions & 6 deletions weasyprint/layout/absolute.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def absolute_width(box, context, containing_block):

cb_x, cb_y, cb_width, cb_height = containing_block

# TODO: handle bidi
ltr = box.style['direction'] == 'ltr'
padding_plus_borders_x = padding_l + padding_r + border_l + border_r
translate_x = 0
translate_box_width = False
Expand All @@ -80,21 +80,28 @@ def absolute_width(box, context, containing_block):
available_width = cb_width - (
padding_plus_borders_x + box.margin_left + box.margin_right)
box.width = shrink_to_fit(context, box, available_width)
if not ltr:
translate_box_width = True
translate_x = default_translate_x + available_width
elif left != 'auto' and right != 'auto' and width != 'auto':
width_for_margins = cb_width - (
right + left + width + padding_plus_borders_x)
if margin_l == margin_r == 'auto':
if width + padding_plus_borders_x + right + left <= cb_width:
box.margin_left = box.margin_right = width_for_margins / 2
else:
box.margin_left = 0
box.margin_right = width_for_margins
if ltr:
box.margin_left, box.margin_right = 0, width_for_margins
else:
box.margin_left, box.margin_right = width_for_margins, 0
elif margin_l == 'auto':
box.margin_left = width_for_margins
elif margin_r == 'auto':
box.margin_right = width_for_margins
else:
elif ltr:
box.margin_right = width_for_margins
else:
box.margin_left = width_for_margins
translate_x = left + default_translate_x
else:
if margin_l == 'auto':
Expand All @@ -108,9 +115,15 @@ def absolute_width(box, context, containing_block):
translate_x = cb_width - right - spacing + default_translate_x
translate_box_width = True
elif left == right == 'auto':
pass # Keep the static position
if not ltr:
available_width = cb_width - (
padding_plus_borders_x +
box.margin_left + box.margin_right)
translate_box_width = True
translate_x = default_translate_x + available_width
elif width == right == 'auto':
box.width = shrink_to_fit(context, box, cb_width - spacing - left)
box.width = shrink_to_fit(
context, box, cb_width - spacing - left)
translate_x = left + default_translate_x
elif left == 'auto':
translate_x = (
Expand Down