1
1
mirror of https://github.com/Kozea/WeasyPrint.git synced 2024-10-04 16:07:57 +03:00
WeasyPrint/weasyprint/tests/test_text.py

97 lines
2.9 KiB
Python
Raw Normal View History

2011-07-07 12:08:22 +04:00
# coding: utf8
"""
weasyprint.tests.test_text
--------------------------
2011-07-07 12:08:22 +04:00
Test the text layout.
2011-07-07 12:08:22 +04:00
:copyright: Copyright 2011-2012 Simon Sapin and contributors, see AUTHORS.
:license: BSD, see LICENSE for details.
2011-07-07 12:08:22 +04:00
2011-08-24 14:39:34 +04:00
"""
2011-07-07 12:08:22 +04:00
from __future__ import division, unicode_literals
2012-05-22 18:34:41 +04:00
from ..css import StyleDict
2012-03-25 03:45:52 +04:00
from ..css.properties import INITIAL_VALUES
from ..text import split_first_line
from .test_layout import parse, body_children
2011-12-16 14:19:56 +04:00
from .testing_utils import FONTS, assert_no_logs
2011-08-24 14:39:34 +04:00
FONTS = FONTS.split(', ')
def make_text(text, width=None, **style):
"""Wrapper for split_first_line() creating a StyleDict."""
2012-03-25 03:45:52 +04:00
style = StyleDict({
'font_family': ['Nimbus Mono L', 'Liberation Mono', 'FreeMono',
'monospace'],
2012-03-25 03:45:52 +04:00
}, INITIAL_VALUES).updated_copy(style)
return split_first_line(
text, style, hinting=False, max_width=width, line_width=None)
2011-07-07 12:08:22 +04:00
2011-08-24 14:39:34 +04:00
2012-02-20 16:04:35 +04:00
@assert_no_logs
2011-07-07 12:08:22 +04:00
def test_line_content():
2011-08-24 14:39:34 +04:00
"""Test the line break for various fixed-width lines."""
for width, remaining in [(100, 'text for test'),
(45, 'is a text for test')]:
text = 'This is a text for test'
_, length, resume_at, _, _, _ = make_text(
2012-03-25 03:45:52 +04:00
text, width, font_family=FONTS, font_size=19)
assert text[resume_at:] == remaining
assert length + 1 == resume_at # +1 is for the removed trailing space
2011-07-07 12:08:22 +04:00
2012-02-20 16:04:35 +04:00
@assert_no_logs
def test_line_with_any_width():
2011-08-24 14:39:34 +04:00
"""Test the auto-fit width of lines."""
_, _, _, width_1, _, _ = make_text('some text')
_, _, _, width_2, _, _ = make_text('some text some text')
assert width_1 < width_2
2011-08-24 14:39:34 +04:00
2012-02-20 16:04:35 +04:00
@assert_no_logs
def test_line_breaking():
2011-08-24 14:39:34 +04:00
"""Test the line breaking."""
string = 'This is a text for test'
2011-07-11 13:30:32 +04:00
# These two tests do not really rely on installed fonts
_, _, resume_at, _, _, _ = make_text(string, 90, font_size=1)
assert resume_at is None
2011-07-19 13:38:54 +04:00
_, _, resume_at, _, _, _ = make_text(string, 90, font_size=100)
assert string[resume_at:] == 'is a text for test'
2011-07-07 12:08:22 +04:00
_, _, resume_at, _, _, _ = make_text(string, 100, font_family=FONTS,
font_size=19)
assert string[resume_at:] == 'text for test'
2011-08-24 14:39:34 +04:00
2012-02-20 16:04:35 +04:00
@assert_no_logs
2011-07-07 12:08:22 +04:00
def test_text_dimension():
"""Test the font size impact on the text dimension."""
string = 'This is a text for test. This is a test for text.py'
_, _, _, width_1, height_1, _ = make_text(string, 200, font_size=12)
2011-09-27 17:19:31 +04:00
_, _, _, width_2, height_2, _ = make_text(string, 200, font_size=20)
assert width_1 * height_1 < width_2 * height_2
2011-07-19 13:38:54 +04:00
2012-02-20 16:04:35 +04:00
@assert_no_logs
def test_text_font_size_zero():
2011-09-02 20:54:24 +04:00
"""Test a text with a font size set to 0."""
page, = parse('''
<style>
p { font-size: 0; }
</style>
<p>test font size zero</p>
''')
paragraph, = body_children(page)
2011-10-19 17:14:43 +04:00
# zero-sized text boxes are removed
line, = paragraph.children
assert not line.children
assert line.height == 0
assert paragraph.height == 0