1
1
mirror of https://github.com/Kozea/WeasyPrint.git synced 2024-09-11 20:47:56 +03:00

Fix more text-anchor cases

This commit is contained in:
Guillaume Ayoub 2024-02-16 21:59:21 +01:00
parent 9405a84d59
commit 3469118d31
2 changed files with 55 additions and 7 deletions

View File

@ -319,6 +319,46 @@ def test_text_tspan_anchor_end(assert_pixels):
''')
@assert_no_logs
def test_text_anchor_middle_tspan(assert_pixels):
assert_pixels('''
_______BBBBBB_______
_______BBBBBB_______
''', '''
<style>
@font-face { src: url(weasyprint.otf); font-family: weasyprint }
@page { size: 20px 2px }
svg { display: block }
</style>
<svg width="20px" height="2px" xmlns="http://www.w3.org/2000/svg">
<text x="10" y="10" font-family="weasyprint" font-size="2" fill="blue"
text-anchor="middle">
<tspan x="10" y="1.5">ABC</tspan>
</text>
</svg>
''')
@assert_no_logs
def test_text_anchor_end_tspan(assert_pixels):
assert_pixels('''
____________BBBBBB__
____________BBBBBB__
''', '''
<style>
@font-face { src: url(weasyprint.otf); font-family: weasyprint }
@page { size: 20px 2px }
svg { display: block }
</style>
<svg width="20px" height="2px" xmlns="http://www.w3.org/2000/svg">
<text x="10" y="10" font-family="weasyprint" font-size="2" fill="blue"
text-anchor="end">
<tspan x="18" y="1.5">ABC</tspan>
</text>
</svg>
''')
@assert_no_logs
def test_text_rotate(assert_pixels):
assert_pixels('''

View File

@ -241,7 +241,8 @@ class Node:
self.pop_rotation(original_rotate, rotate)
if self.text:
trailing_space = self.text.endswith(' ')
for child_element in element.iter_children():
element_children = tuple(element.iter_children())
for child_element in element_children:
child = child_element.etree_element
if child.tag in ('{http://www.w3.org/2000/svg}tref', 'tref'):
child_node = Node(child_element, self._style)
@ -262,13 +263,16 @@ class Node:
if original_rotate and 'rotate' not in child_node:
child_node.pop_rotation(original_rotate, rotate)
children.append(child_node)
if child.tail:
tail = self.process_whitespace(child.tail, preserve)
if text_root and child_element is element_children[-1]:
if not preserve:
tail = tail.rstrip(' ')
if tail:
anonymous_etree = ElementTree.Element(
'{http://www.w3.org/2000/svg}tspan')
anonymous = Node(
ElementWrapper.from_xml_root(anonymous_etree), self._style)
anonymous._etree_node.text = self.process_whitespace(
child.tail, preserve)
anonymous._etree_node.text = tail
if original_rotate:
anonymous.pop_rotation(original_rotate, rotate)
if trailing_space and not preserve:
@ -445,8 +449,12 @@ class SVG:
self.stream.transform(*(old_ctm @ new_ctm.invert).values)
# Handle text anchor
text_anchor = node.get('text-anchor')
if TAGS.get(node.tag) == text and text_anchor in ('middle', 'end'):
if node.tag == 'text':
text_anchor = node.get('text-anchor')
children = tuple(node)
if children and not node.text:
text_anchor = children[0].get('text-anchor')
if node.tag == 'text' and text_anchor in ('middle', 'end'):
group = self.stream.add_group(0, 0, 0, 0) # BBox set after drawing
original_streams.append(self.stream)
self.stream = group
@ -478,7 +486,7 @@ class SVG:
node.text_bounding_box, ((x1, y1), (x2, y2)))
# Handle text anchor
if TAGS.get(node.tag) == text and text_anchor in ('middle', 'end'):
if node.tag == 'text' and text_anchor in ('middle', 'end'):
group_id = self.stream.id
self.stream = original_streams.pop()
self.stream.push_state()