From 4dfe6079c2d1bd91cccfd9a7d78f8924e2dfabef Mon Sep 17 00:00:00 2001 From: Guillaume Ayoub Date: Tue, 3 Oct 2023 14:30:21 +0200 Subject: [PATCH] Correctly handle transforms for transparent SVG elements Fix #1976. --- tests/draw/svg/test_opacity.py | 15 +++++++++++++++ weasyprint/svg/__init__.py | 14 ++++++-------- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/tests/draw/svg/test_opacity.py b/tests/draw/svg/test_opacity.py index 5aa5d353f0..b3f780de3a 100644 --- a/tests/draw/svg/test_opacity.py +++ b/tests/draw/svg/test_opacity.py @@ -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 % ''' + + ''', + opacity_source % ''' + + ''', + ) diff --git a/weasyprint/svg/__init__.py b/weasyprint/svg/__init__.py index ffb6720e6c..f9c8f0cb9c 100644 --- a/weasyprint/svg/__init__.py +++ b/weasyprint/svg/__init__.py @@ -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