Skip to content

Commit

Permalink
Merge pull request #2043 from michaellisitsa/fix-tspan-not-respecting…
Browse files Browse the repository at this point in the history
…-text-anchor

Fix tspan not respecting text-anchor
  • Loading branch information
grewn0uille authored Jan 26, 2024
2 parents 7d8fcb5 + e9656ab commit 4edb03d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
38 changes: 38 additions & 0 deletions tests/draw/svg/test_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,44 @@ def test_text_tspan(assert_pixels):
''')


@assert_no_logs
def test_text_tspan_anchor_middle(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">
<tspan x="10" y="1.5" text-anchor="middle">ABC</tspan>
</text>
</svg>
''')


@assert_no_logs
def test_text_tspan_anchor_end(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">
<tspan x="18" y="1.5" text-anchor="end">ABC</tspan>
</text>
</svg>
''')


@assert_no_logs
def test_text_rotate(assert_pixels):
assert_pixels('''
Expand Down
4 changes: 2 additions & 2 deletions weasyprint/svg/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ def draw_node(self, node, font_size, fill_stroke=True):

# Handle text anchor
text_anchor = node.get('text-anchor')
if node.tag == 'text' and text_anchor in ('middle', 'end'):
if TAGS.get(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 @@ -473,7 +473,7 @@ def draw_node(self, node, font_size, fill_stroke=True):
node.text_bounding_box, ((x1, y1), (x2, y2)))

# Handle text anchor
if node.tag == 'text' and text_anchor in ('middle', 'end'):
if TAGS.get(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 4edb03d

Please sign in to comment.