1
1
mirror of https://github.com/Kozea/WeasyPrint.git synced 2024-10-05 00:21:15 +03:00
WeasyPrint/weasy/tests/test_text.py

130 lines
3.9 KiB
Python
Raw Normal View History

2011-07-07 12:08:22 +04:00
# coding: utf8
# WeasyPrint converts web documents (HTML, CSS, ...) to PDF.
# Copyright (C) 2011 Simon Sapin
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2011-08-24 14:39:34 +04:00
"""
Test the text management.
2011-07-07 12:08:22 +04:00
2011-08-24 14:39:34 +04:00
"""
2011-07-07 12:08:22 +04:00
2011-08-24 14:39:34 +04:00
from attest import Tests, assert_hook # pylint: disable=W0611
2011-07-07 12:08:22 +04:00
2011-08-24 14:39:34 +04:00
from ..text import TextFragment, TextLineFragment, ALIGN_PROPERTIES
from .test_layout import parse, body_children
2011-08-24 14:39:34 +04:00
SUITE = Tests()
2011-07-07 12:08:22 +04:00
2011-07-29 03:13:07 +04:00
FONTS = u"Nimbus Mono L, Liberation Mono, FreeMono, Monospace"
2011-07-07 12:08:22 +04:00
2011-08-24 14:39:34 +04:00
@SUITE.test
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."""
2011-07-07 12:08:22 +04:00
string = u"This is a text for test"
width = 120
2011-08-24 14:39:34 +04:00
line = TextLineFragment(string, width)
2011-07-11 13:30:32 +04:00
line.set_font_size(12)
line.set_font_family(FONTS)
2011-07-08 20:15:07 +04:00
assert line.get_remaining_text() == u'text for test'
2011-07-11 13:30:32 +04:00
line.set_width(60)
assert line.get_remaining_text() == u'is a text for test'
2011-07-08 20:15:07 +04:00
assert u"%s%s" % (line.get_text(), line.get_remaining_text()) == string
2011-07-07 12:08:22 +04:00
2011-08-24 14:39:34 +04:00
@SUITE.test
def test_line_with_any_width():
2011-08-24 14:39:34 +04:00
"""Test the auto-fit width of lines."""
line = TextLineFragment(u'some text')
line.set_font_family(FONTS)
width = line.get_size()[0]
2011-08-24 14:39:34 +04:00
line.set_text('some some some text some some some text')
new_width = line.get_size()[0]
2011-07-19 13:38:54 +04:00
assert width < new_width
2011-08-24 14:39:34 +04:00
@SUITE.test
def test_line_breaking():
2011-08-24 14:39:34 +04:00
"""Test the line breaking."""
string = u'This is a text for test'
2011-07-07 12:08:22 +04:00
width = 120
2011-08-24 14:39:34 +04:00
line = TextLineFragment(string, width)
2011-07-11 13:30:32 +04:00
line.set_font_family(FONTS)
2011-07-08 20:15:07 +04:00
line.set_font_size(12)
line.set_font_weight(200)
2011-08-24 14:39:34 +04:00
assert line.get_remaining_text() == u'text for test'
2011-07-19 13:38:54 +04:00
2011-07-08 20:15:07 +04:00
line.set_font_weight(800)
2011-08-24 14:39:34 +04:00
assert line.get_remaining_text() == u'text for test'
2011-07-19 13:38:54 +04:00
2011-07-08 20:15:07 +04:00
line.set_font_size(14)
2011-08-24 14:39:34 +04:00
assert line.get_remaining_text() == u'text for test'
2011-07-07 12:08:22 +04:00
2011-08-24 14:39:34 +04:00
@SUITE.test
2011-07-07 12:08:22 +04:00
def test_text_dimension():
2011-08-24 14:39:34 +04:00
"""Test the font size and spacing size impact on the text dimension."""
string = u'This is a text for test. This is a test for text.py'
2011-07-07 12:08:22 +04:00
width = 200
2011-08-24 14:39:34 +04:00
fragment = TextFragment(string, width)
2011-07-08 20:15:07 +04:00
fragment.set_font_size(12)
2011-07-19 13:38:54 +04:00
2011-07-08 20:15:07 +04:00
dimension = list(fragment.get_size())
fragment.set_font_size(20)
new_dimension = list(fragment.get_size())
2011-08-24 14:39:34 +04:00
assert dimension[0] * dimension[1] < new_dimension[0] * new_dimension[1]
2011-07-19 13:38:54 +04:00
2011-07-08 20:15:07 +04:00
dimension = list(fragment.get_size())
fragment.set_spacing(20)
new_dimension = list(fragment.get_size())
2011-08-24 14:39:34 +04:00
assert dimension[0] * dimension[1] < new_dimension[0] * new_dimension[1]
2011-07-07 12:08:22 +04:00
2011-08-24 14:39:34 +04:00
@SUITE.test
2011-07-07 12:08:22 +04:00
def test_text_other():
2011-08-24 14:39:34 +04:00
"""Test various text properties."""
fragment = TextFragment(u'', 40)
fragment.set_text(u'some text')
2011-07-19 13:38:54 +04:00
2011-08-24 14:39:34 +04:00
# The default value of alignement property is ``left`` for western script
assert fragment.layout.get_alignment() == ALIGN_PROPERTIES['left']
2011-08-16 00:56:37 +04:00
assert not fragment.layout.get_justify()
2011-08-24 14:39:34 +04:00
for key, value in ALIGN_PROPERTIES.iteritems():
2011-08-16 00:56:37 +04:00
fragment.set_alignment(key)
assert fragment.layout.get_alignment() == value
fragment.set_alignment('justify')
assert fragment.layout.get_justify()
2011-07-19 13:38:54 +04:00
fragment.justify = True
assert fragment.justify != False
@SUITE.test
def test_text_font_size_zero():
page, = parse('''
<style>
p { font-size: 0; }
</style>
<p>test font size zero</p>
''')
textbox = body_children(page)[0].children[0].children[0]
assert textbox.height == 0
assert textbox.width == 0