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

124 lines
3.8 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/>.
import os.path
from attest import Tests, assert_hook
import attest
from .. import text
suite = Tests()
@suite.test
def test_line_content():
string = u"This is a text for test"
width = 120
line = text.TextLineFragment(string, width)
2011-07-08 20:15:07 +04:00
assert line.get_remaining_text() == u'test'
line.set_width(80)
assert line.get_remaining_text() == u'text for test'
assert u"%s%s" % (line.get_text(), line.get_remaining_text()) == string
2011-07-07 12:08:22 +04:00
@suite.test
def test_line_breaking():
2011-07-07 12:08:22 +04:00
string = u"This is a text for test"
width = 120
line = text.TextLineFragment(string, width)
2011-07-07 12:08:22 +04:00
2011-07-08 20:15:07 +04:00
line.set_font_size(12)
line.set_font_weight(200)
assert line.get_remaining_text() == u"test"
2011-07-07 12:08:22 +04:00
2011-07-08 20:15:07 +04:00
line.set_font_weight(800)
assert line.get_remaining_text() == u"for test"
2011-07-07 12:08:22 +04:00
2011-07-08 20:15:07 +04:00
line.set_font_size(14)
assert line.get_remaining_text() == u"for test"
2011-07-07 12:08:22 +04:00
@suite.test
def test_text_dimension():
string = u"This is a text for test. This is a test for text.py"
width = 200
fragment = text.TextFragment(string, width)
2011-07-08 20:15:07 +04:00
fragment.set_font_size(12)
2011-07-07 12:08:22 +04:00
2011-07-08 20:15:07 +04:00
dimension = list(fragment.get_size())
print dimension
2011-07-08 20:15:07 +04:00
fragment.set_font_size(20)
new_dimension = list(fragment.get_size())
print new_dimension
assert dimension[0]*dimension[1] < new_dimension[0]*new_dimension[1]
2011-07-07 12:08:22 +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())
assert dimension[0]*dimension[1] < new_dimension[0]*new_dimension[1]
2011-07-07 12:08:22 +04:00
@suite.test
def test_text_font():
string = u"This is a text for test. This is a test for text.py"
width = 200
fragment = text.TextFragment(string, width)
2011-07-08 20:15:07 +04:00
fragment.set_font_family(u"Comic Sans MS")
assert fragment.get_font_family() == u"Comic Sans MS"
assert fragment.get_size() == (187, 44)
2011-07-07 12:08:22 +04:00
2011-07-08 20:15:07 +04:00
fragment.set_font_family(u"inexistante font, Comic Sans MS")
dimension = list(fragment.get_size())
fragment.set_font_family(u"Comic Sans MS")
new_dimension = list(fragment.get_size())
assert new_dimension == dimension
2011-07-07 12:08:22 +04:00
2011-07-08 20:15:07 +04:00
fragment.set_font_size(12)
assert fragment.get_font_size() == 12
2011-07-07 12:08:22 +04:00
for value in text.STYLE_PROPERTIES.keys():
2011-07-08 20:15:07 +04:00
fragment.set_font_style(value)
assert fragment.get_font_style() == value
2011-07-07 12:08:22 +04:00
with attest.raises(ValueError):
2011-07-08 20:15:07 +04:00
fragment.set_font_style("inexistante property")
2011-07-07 12:08:22 +04:00
for value in text.VARIANT_PROPERTIES.keys():
2011-07-08 20:15:07 +04:00
fragment.set_font_variant(value)
assert fragment.get_font_variant() == value
with attest.raises(ValueError):
fragment.set_font_style("inexistante property")
2011-07-07 12:08:22 +04:00
@suite.test
def test_text_other():
""" Test other properties """
2011-07-07 12:08:22 +04:00
width = 200
fragment = text.TextFragment(u"", 40)
2011-07-08 20:15:07 +04:00
fragment.set_text(u"some text")
2011-07-07 12:08:22 +04:00
#The default value of alignement property is ``left`` for western script
2011-07-08 20:15:07 +04:00
assert fragment.get_alignment() == u"left"
2011-07-07 12:08:22 +04:00
for value in text.ALIGN_PROPERTIES.keys():
2011-07-08 20:15:07 +04:00
fragment.set_alignment(value)
assert fragment.get_alignment() == value
2011-07-07 12:08:22 +04:00
fragment.justify = True
assert fragment.justify != False
2011-07-07 12:08:22 +04:00