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

Do not create new TextFragment’s in draw, use the ones from layout.

This commit is contained in:
Simon Sapin 2011-10-08 20:38:33 +02:00
parent 8115e07aa5
commit 8dfae70560
5 changed files with 23 additions and 19 deletions

View File

@ -323,8 +323,7 @@ def draw_text(context, textbox):
return
context.move_to(textbox.position_x, textbox.position_y)
fragment = TextFragment(textbox.utf8_text, textbox.style, context)
fragment.show_layout(context)
textbox.show_line(context)
values = textbox.style.text_decoration
for value in values:
if value == 'overline':

View File

@ -421,13 +421,15 @@ def split_text_box(textbox, available_width, skip):
fragment = TextFragment(utf8_text, textbox.style,
cairo.Context(textbox.document.surface), available_width)
length, width, height, baseline, resume_at = fragment.split_first_line()
show_line, length, width, height, baseline, resume_at = \
fragment.split_first_line()
if length > 0:
textbox = textbox.copy_with_text(utf8_text[:length])
textbox.width = width
textbox.height = height
textbox.baseline = baseline
textbox.show_line = show_line
else:
textbox = None

View File

@ -43,7 +43,7 @@ def list_marker_layout(box, containing_block):
text_fragment = TextFragment(marker.utf8_text, marker.style,
context=cairo.Context(marker.document.surface))
result = text_fragment.split_first_line()
_, marker.width, marker.height, _, _ = result
marker.show_line, _, marker.width, marker.height, _, _ = result
else:
# Image marker
marker.width, marker.height = list_style_image_size(marker)

View File

@ -53,7 +53,7 @@ def test_line_content():
text = 'This is a text for test'
line = make_text(
text, width, 'font-family: "%s"; font-size: 19px' % FONTS)
length, _width, _height, _baseline, resume_at = line.split_first_line()
_, length, _, _, _, resume_at = line.split_first_line()
assert text[resume_at:] == remaining
assert length == resume_at
@ -62,10 +62,10 @@ def test_line_content():
def test_line_with_any_width():
"""Test the auto-fit width of lines."""
line = make_text(u'some text')
_, width, _, _, _ = line.split_first_line()
_, _, width, _, _, _ = line.split_first_line()
line = make_text('some some some text some some some text')
_, new_width, _, _, _ = line.split_first_line()
_, _, new_width, _, _, _ = line.split_first_line()
assert width < new_width
@ -77,15 +77,15 @@ def test_line_breaking():
# These two tests do not really rely on installed fonts
line = make_text(string, 120, 'font-size: 1px')
_length, _width, _height, _baseline, resume_at = line.split_first_line()
_, _, _, _, _, resume_at = line.split_first_line()
assert resume_at is None
line = make_text(string, 120, 'font-size: 100px')
_length, _width, _height, _baseline, resume_at = line.split_first_line()
_, _, _, _, _, resume_at = line.split_first_line()
assert string[resume_at:] == u'is a text for test'
line = make_text(string, 120, 'font-family: "%s"; font-size: 19px' % FONTS)
_length, _width, _height, _baseline, resume_at = line.split_first_line()
_, _, _, _, _, resume_at = line.split_first_line()
assert string[resume_at:] == u'text for test'
@ -94,10 +94,10 @@ def test_text_dimension():
"""Test the font size impact on the text dimension."""
string = u'This is a text for test. This is a test for text.py'
fragment = make_text(string, 200, 'font-size: 12px')
_, width_1, height_1, _, _ = fragment.split_first_line()
_, _, width_1, height_1, _, _ = fragment.split_first_line()
fragment = make_text(string, 200, 'font-size: 20px')
_, width_2, height_2, _, _ = fragment.split_first_line()
_, _, width_2, height_2, _, _ = fragment.split_first_line()
assert width_1 * height_1 < width_2 * height_2

View File

@ -70,17 +70,14 @@ class TextFragment(object):
_, attributes_list, _, _ = Pango.parse_markup(markup, -1, '\x00')
self.layout.set_attributes(attributes_list)
def show_layout(self, context):
"""Draw the text to the Cairo ``context``."""
PangoCairo.update_layout(context, self.layout)
PangoCairo.show_layout(context, self.layout)
# TODO: use get_line instead of get_lines when it is not broken anymore
def split_first_line(self):
"""Fit as much as possible in the available width for one line of text.
Return ``(length, width, height, resume_at)``.
Return ``(show_line, length, width, height, resume_at)``.
``show_line``: a closure that takes a cairo Context and draws the
first line.
``length``: length in UTF-8 bytes of the first line
``width``: width in pixels of the first line
``height``: height in pixels of the first line
@ -104,4 +101,10 @@ class TextFragment(object):
resume_at = lines[1].start_index
else:
resume_at = None
return length, width, height, baseline, resume_at
def show_line(context):
"""Draw the given ``line`` to the Cairo ``context``."""
PangoCairo.update_layout(context, self.layout)
PangoCairo.show_layout_line(context, first_line)
return show_line, length, width, height, baseline, resume_at