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

change test concerning text dimension and font

This commit is contained in:
Salem Harrache 2011-07-07 16:40:27 +02:00
parent be24da82b0
commit 8a7e9cbb31
4 changed files with 62 additions and 75 deletions

Binary file not shown.

Binary file not shown.

View File

@ -30,7 +30,7 @@ suite = Tests()
def test_line_content():
string = u"This is a text for test"
width = 120
line = text.WeasyInlineText(string, width)
line = text.LineTextFragment(string, width)
assert line.remaining_text == u'test'
assert u"%s%s" % (line.text, line.remaining_text) == string
line.width = 80
@ -39,101 +39,85 @@ def test_line_content():
@suite.test
def test_line_dimension():
def test_line_breaking():
string = u"This is a text for test"
width = 120
line = text.WeasyInlineText(string, width)
line = text.LineTextFragment(string, width)
line.font_size = 12
line.font_weight = 200
assert line.size == (114, 34)
assert line.remaining_text == u"test"
line.font_weight = 800
assert line.size == (98, 34)
assert line.remaining_text == u"for test"
line.font_size = 14
assert line.size == (109, 40)
assert line.remaining_text == u"for test"
@suite.test
def test_text_dimension():
string = u"This is a text for test. This is a test for text.py"
width = 200
weasytext = text.WeasyText(string, width)
weasytext.font_size = 12
assert weasytext.size == (183, 38)
fragment = text.TextFragment(string, width)
fragment.font_size = 12
weasytext.font_size = 14
assert weasytext.size == (198, 44)
dimension = list(fragment.size)
print dimension
fragment.font_size = 20
new_dimension = list(fragment.size)
print new_dimension
assert dimension[0]*dimension[1] < new_dimension[0]*new_dimension[1]
weasytext.spacing = 20
assert weasytext.size == (198, 64)
assert weasytext.text == string
dimension = list(fragment.size)
fragment.spacing = 20
new_dimension = list(fragment.size)
assert dimension[0]*dimension[1] < new_dimension[0]*new_dimension[1]
@suite.test
def test_text_font():
string = u"This is a text for test. This is a test for text.py"
width = 200
weasytext = text.WeasyText(string, width)
weasytext.font_family = u"Comic Sans MS"
assert weasytext.font_family == u"Comic Sans MS"
assert weasytext.size == (187, 44)
fragment = text.TextFragment(string, width)
fragment.font_family = u"Comic Sans MS"
assert fragment.font_family == u"Comic Sans MS"
assert fragment.size == (187, 44)
weasytext.font_family = u"Courier 10 Pitch"
assert weasytext.font_family == u"Courier 10 Pitch"
assert weasytext.size == (180, 54)
fragment.font_family = u"inexistante font, Comic Sans MS"
dimension = list(fragment.size)
fragment.font_family = u"Comic Sans MS"
new_dimension = list(fragment.size)
assert new_dimension == dimension
weasytext.font_family = u"Nimbus Roman No9 L"
assert weasytext.font_family == u"Nimbus Roman No9 L"
assert weasytext.size == (182, 38)
# weasytext.font_family = u"inexistante font"
# assert weasytext.font_family != u"inexistante font"
weasytext.font_family = u"Nimbus Roman No9 L"
weasytext.font_size = 12
assert weasytext.font_size == 12
fragment.font_size = 12
assert fragment.font_size == 12
for value in text.STYLE_PROPERTIES.keys():
weasytext.font_style = value
assert weasytext.font_style == value
fragment.font_style = value
assert fragment.font_style == value
with attest.raises(ValueError):
weasytext.font_style = "inexistante property"
fragment.font_style = "inexistante property"
for value in text.VARIANT_PROPERTIES.keys():
weasytext.font_variant = value
assert weasytext.font_variant == value
# weasytext.font_variant
# assert
# weasytext.font_weight
# assert
fragment.font_variant = value
assert fragment.font_variant == value
@suite.test
def test_text_other():
""" Test all properties """
""" Test other properties """
width = 200
weasytext = text.WeasyText(u"", 40)
weasytext.text = u"some text"
assert weasytext.text == u"some text"
weasytext.width = 20
assert weasytext.width == 20
weasytext.spacing = 20
assert weasytext.spacing == 20
fragment = text.TextFragment(u"", 40)
fragment.text = u"some text"
#The default value of alignement property is ``left`` for western script
assert weasytext.alignment == u"left"
assert fragment.alignment == u"left"
for value in text.ALIGN_PROPERTIES.keys():
weasytext.alignment = value
assert weasytext.alignment == value
fragment.alignment = value
assert fragment.alignment == value
weasytext.justify = True
assert weasytext.justify != False
fragment.justify = True
assert fragment.justify != False

View File

@ -31,16 +31,23 @@ VARIANT_PROPERTIES = {'normal':pango.VARIANT_NORMAL,
'small-caps':pango.VARIANT_SMALL_CAPS}
class WeasyText(object):
class TextFragment(object):
def __init__(self, text, width):
self.layout = gtk.DrawingArea().create_pango_layout(text)
self.context = gtk.DrawingArea().create_pango_context()
self.layout = pango.Layout(self.context)
self.layout.set_text(text.encode("utf-8"))
self.width = width
# Other properties
self.layout.set_wrap(pango.WRAP_WORD)
def update_font(self):
def _update_font(self):
self.layout.set_font_description(self._font)
@property
def availables_font(self):
for font in gtk.DrawingArea().create_pango_context().list_families():
yield font.get_name()
@property
def text(self):
return self.layout.get_text().decode('utf-8')
@ -104,7 +111,8 @@ class WeasyText(object):
def font(self):
self._font = self.layout.get_font_description()
if self._font is None:
self._font = pango.FontDescription()
self._font = self.layout.get_context().get_font_description()
self.layout.set_font_description(self._font)
return self._font
@property
@ -114,7 +122,7 @@ class WeasyText(object):
@font_family.setter
def font_family(self, value):
self.font.set_family(value.encode("utf-8"))
self.update_font()
self._update_font()
@property
def font_style(self):
@ -137,22 +145,20 @@ class WeasyText(object):
"""
if value in STYLE_PROPERTIES.keys():
self.font.set_style(value)
self.update_font()
self._update_font()
else:
raise ValueError('The style property must be in %s' \
% STYLE_PROPERTIES.keys())
@property
def font_size(self):
return self.font.get_size() / pango.SCALE
@font_size.setter
def font_size(self, value):
"""The value of size is specified in px units."""
self.font.set_size(pango.SCALE * value)
self.update_font()
self._update_font()
@property
def font_variant(self):
@ -169,19 +175,18 @@ class WeasyText(object):
description to the value specified by variant. The value of variant
must be either
pango.VARIANT_NORMAL
pango.VARIANT_SMALL_CAPS.
pango.VARIANT_SMALL_CAPS
"""
if value in VARIANT_PROPERTIES.keys():
self.font.set_variant(value)
self.update_font()
self._update_font()
else:
raise ValueError('The style property must be in %s' \
% VARIANT_PROPERTIES.keys())
@property
def font_weight(self):
return self.font.get_weight()
return int(float(self.font.get_weight()))
@font_weight.setter
def font_weight(self, value):
@ -197,27 +202,25 @@ class WeasyText(object):
pango.WEIGHT_ULTRABOLD the ultrabold weight (= 800)
"""
self.font.set_weight(value)
self.update_font()
self._update_font()
class WeasyInlineText(WeasyText):
class LineTextFragment(TextFragment):
def __init__(self, text, width):
super(WeasyInlineText, self).__init__(text, width)
super(LineTextFragment, self).__init__(text, width)
@property
def remaining_text(self):
first_line = self.layout.get_line(0)
return super(WeasyInlineText, self).text[first_line.length:]
return super(LineTextFragment, self).text[first_line.length:]
@property
def text(self):
first_line = self.layout.get_line(0)
return super(WeasyInlineText, self).text[:first_line.length]
return super(LineTextFragment, self).text[:first_line.length]
@property
def size(self):
""" Return the real text area dimension for this line in px unit """
extents = self.layout.get_line(0).get_pixel_extents()[1]
return (extents[2]-extents[0], extents[3]-extents[1])