2018-03-14 03:32:01 +03:00
|
|
|
"""
|
|
|
|
weasyprint.tests.layout.shrink_to_fit
|
|
|
|
-------------------------------------
|
|
|
|
|
|
|
|
Tests for shrink-to-fit algorithm.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
from ..test_boxes import render_pages as parse
|
|
|
|
from ..testing_utils import assert_no_logs
|
|
|
|
|
|
|
|
|
|
|
|
@assert_no_logs
|
|
|
|
@pytest.mark.parametrize('margin_left', range(1, 10))
|
|
|
|
@pytest.mark.parametrize('font_size', range(1, 10))
|
|
|
|
def test_shrink_to_fit_floating_point_error_1(margin_left, font_size):
|
|
|
|
# See bugs #325 and #288, see commit fac5ee9.
|
|
|
|
page, = parse('''
|
2018-03-27 01:44:33 +03:00
|
|
|
<style>
|
2020-12-11 02:22:33 +03:00
|
|
|
@font-face { src: url(weasyprint.otf); font-family: weasyprint }
|
2018-03-27 01:44:33 +03:00
|
|
|
@page { size: 100000px 100px }
|
2020-05-30 16:48:24 +03:00
|
|
|
p { float: left; margin-left: 0.%din; font-size: 0.%dem;
|
2020-12-11 02:22:33 +03:00
|
|
|
font-family: weasyprint }
|
2018-03-27 01:44:33 +03:00
|
|
|
</style>
|
|
|
|
<p>this parrot is dead</p>
|
2018-03-14 03:32:01 +03:00
|
|
|
''' % (margin_left, font_size))
|
|
|
|
html, = page.children
|
|
|
|
body, = html.children
|
|
|
|
p, = body.children
|
|
|
|
assert len(p.children) == 1
|
|
|
|
|
|
|
|
|
2018-03-27 01:44:33 +03:00
|
|
|
@assert_no_logs
|
2018-03-14 03:32:01 +03:00
|
|
|
@pytest.mark.parametrize('font_size', (1, 5, 10, 50, 100, 1000, 10000))
|
|
|
|
def test_shrink_to_fit_floating_point_error_2(font_size):
|
|
|
|
letters = 1
|
|
|
|
while True:
|
|
|
|
page, = parse('''
|
2018-03-27 01:44:33 +03:00
|
|
|
<style>
|
2020-12-11 02:22:33 +03:00
|
|
|
@font-face { src: url(weasyprint.otf); font-family: weasyprint }
|
2020-05-30 16:48:24 +03:00
|
|
|
@page { size: %d0pt %d0px }
|
2020-12-11 02:22:33 +03:00
|
|
|
p { font-size: %dpt; font-family: weasyprint }
|
2018-03-27 01:44:33 +03:00
|
|
|
</style>
|
|
|
|
<p>mmm <b>%s a</b></p>
|
2018-03-14 03:32:01 +03:00
|
|
|
''' % (font_size, font_size, font_size, 'i' * letters))
|
|
|
|
html, = page.children
|
|
|
|
body, = html.children
|
|
|
|
p, = body.children
|
|
|
|
assert len(p.children) in (1, 2)
|
|
|
|
assert len(p.children[0].children) == 2
|
|
|
|
text = p.children[0].children[1].children[0].text
|
|
|
|
assert text
|
|
|
|
if text.endswith('i'):
|
|
|
|
letters = 1
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
letters += 1
|