Skip to content

Commit

Permalink
Correctly handle transforms for transparent SVG elements
Browse files Browse the repository at this point in the history
Fix #1976.
  • Loading branch information
liZe committed Oct 3, 2023
1 parent 01c5e97 commit 4dfe607
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
15 changes: 15 additions & 0 deletions tests/draw/svg/test_opacity.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,18 @@ def test_pattern_gradient_stroke_fill_opacity(assert_same_renderings):
''',
tolerance=1,
)


@assert_no_logs
def test_translate_opacity(assert_same_renderings):
# Regression test for https://github.com/Kozea/WeasyPrint/issues/1976
assert_same_renderings(
opacity_source % '''
<rect transform="translate(2, 2)" width="5" height="5"
fill="blue" opacity="0.5" />
''',
opacity_source % '''
<rect x="2" y="2" width="5" height="5"
fill="blue" opacity="0.5" />
''',
)
14 changes: 6 additions & 8 deletions weasyprint/svg/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,19 +394,17 @@ def draw_node(self, node, font_size, fill_stroke=True):
if filter_:
apply_filters(self, node, filter_, font_size)

# Apply transform attribute
self.transform(node.get('transform'), font_size)

# Create substream for opacity
opacity = float(node.get('opacity', 1))
if fill_stroke and 0 <= opacity < 1:
original_stream = self.stream
box = self.calculate_bounding_box(node, font_size)
if is_valid_bounding_box(box):
coords = (box[0], box[1], box[0] + box[2], box[1] + box[3])
else:
coords = (0, 0, self.inner_width, self.inner_height)
self.stream = self.stream.add_group(*coords)

# Apply transform attribute
self.transform(node.get('transform'), font_size)
if not is_valid_bounding_box(box):
box = (0, 0, self.inner_width, self.inner_height)
self.stream = self.stream.add_group(*box)

# Clip
clip_path = parse_url(node.get('clip-path')).fragment
Expand Down

0 comments on commit 4dfe607

Please sign in to comment.