Skip to content

Commit

Permalink
Fix more text-anchor cases
Browse files Browse the repository at this point in the history
  • Loading branch information
liZe committed Feb 16, 2024
1 parent 9405a84 commit 3469118
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 7 deletions.
40 changes: 40 additions & 0 deletions tests/draw/svg/test_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -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('''
Expand Down
22 changes: 15 additions & 7 deletions weasyprint/svg/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,8 @@ def text_children(self, element, trailing_space, text_root=False):
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)
Expand All @@ -262,13 +263,16 @@ def text_children(self, element, trailing_space, text_root=False):
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:
Expand Down Expand Up @@ -445,8 +449,12 @@ def draw_node(self, node, font_size, fill_stroke=True):
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
Expand Down Expand Up @@ -478,7 +486,7 @@ def draw_node(self, node, font_size, fill_stroke=True):
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()
Expand Down

0 comments on commit 3469118

Please sign in to comment.