From 199918813ef2306f9538e8bc5804c56eb5c40b85 Mon Sep 17 00:00:00 2001 From: Guillaume Ayoub Date: Mon, 26 Aug 2024 09:04:53 +0200 Subject: [PATCH 1/6] Use bounding box for svg tags with no size --- weasyprint/svg/images.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/weasyprint/svg/images.py b/weasyprint/svg/images.py index d6462af10..b6dc38d30 100644 --- a/weasyprint/svg/images.py +++ b/weasyprint/svg/images.py @@ -1,5 +1,6 @@ """Draw image and svg tags.""" +from .bounding_box import bounding_box, is_valid_bounding_box from .utils import preserve_ratio @@ -10,8 +11,18 @@ def svg(svg, node, font_size): if svg.tree == node: width, height = svg.concrete_width, svg.concrete_height else: - width, height = svg.point( - node.get('width'), node.get('height'), font_size) + width, height = node.get('width'), node.get('height') + if None in (width, height): + node._etree_node.tag = 'g' + box = bounding_box(svg, node, font_size, stroke=True) + if is_valid_bounding_box(box): + width = box[0] + box[2] + height = box[1] + box[3] + else: + width = height = 0 + node._etree_node.tag = 'svg' + else: + width, height = svg.point(width, height, font_size) scale_x, scale_y, translate_x, translate_y = preserve_ratio( svg, node, font_size, width, height) if svg.tree != node and node.get('overflow', 'hidden') == 'hidden': From 0ed504564652d65c71835b0fe2dffec3b742d887 Mon Sep 17 00:00:00 2001 From: Guillaume Ayoub Date: Tue, 27 Aug 2024 09:15:38 +0200 Subject: [PATCH 2/6] Set svg inner size for nested svg tags --- weasyprint/svg/__init__.py | 8 ++++++++ weasyprint/svg/images.py | 2 ++ 2 files changed, 10 insertions(+) diff --git a/weasyprint/svg/__init__.py b/weasyprint/svg/__init__.py index add060eee..b164e05eb 100644 --- a/weasyprint/svg/__init__.py +++ b/weasyprint/svg/__init__.py @@ -463,6 +463,11 @@ def draw_node(self, node, font_size, fill_stroke=True): # Draw node children if node.display and node.tag not in DEF_TYPES: + if node.tag == 'svg': + inner_width = self.inner_width + inner_height = self.inner_height + self.inner_width = node.width + self.inner_height = node.height for child in node: self.draw_node(child, font_size, fill_stroke) visible_text_child = ( @@ -477,6 +482,9 @@ def draw_node(self, node, font_size, fill_stroke=True): y2 = y1 + child.text_bounding_box[3] node.text_bounding_box = extend_bounding_box( node.text_bounding_box, ((x1, y1), (x2, y2))) + if node.tag == 'svg': + self.inner_width = inner_width + self.inner_height = inner_height # Handle text anchor if node.tag == 'text' and text_anchor in ('middle', 'end'): diff --git a/weasyprint/svg/images.py b/weasyprint/svg/images.py index b6dc38d30..488c01632 100644 --- a/weasyprint/svg/images.py +++ b/weasyprint/svg/images.py @@ -23,6 +23,8 @@ def svg(svg, node, font_size): node._etree_node.tag = 'svg' else: width, height = svg.point(width, height, font_size) + node.width = width + node.height = height scale_x, scale_y, translate_x, translate_y = preserve_ratio( svg, node, font_size, width, height) if svg.tree != node and node.get('overflow', 'hidden') == 'hidden': From d566245d6a767fe153968f3e8826681359b0e229 Mon Sep 17 00:00:00 2001 From: Guillaume Ayoub Date: Mon, 26 Aug 2024 23:08:42 +0200 Subject: [PATCH 3/6] Display symbol but not its children --- weasyprint/svg/__init__.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/weasyprint/svg/__init__.py b/weasyprint/svg/__init__.py index b164e05eb..245cd182b 100644 --- a/weasyprint/svg/__init__.py +++ b/weasyprint/svg/__init__.py @@ -81,6 +81,7 @@ 'mask', 'path', 'pattern', + 'symbol', )) @@ -390,7 +391,7 @@ def draw(self, stream, concrete_width, concrete_height, base_url, def draw_node(self, node, font_size, fill_stroke=True): """Draw a node.""" - if node.tag in ('defs', 'symbol'): + if node.tag == 'defs': return # Update font size From ac165d5bd2803dd7590b87032b7369e8ff925421 Mon Sep 17 00:00:00 2001 From: Guillaume Ayoub Date: Mon, 26 Aug 2024 23:19:53 +0200 Subject: [PATCH 4/6] Avoid storing svg size as attributes --- weasyprint/svg/__init__.py | 18 ++++++++++-------- weasyprint/svg/images.py | 4 ++-- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/weasyprint/svg/__init__.py b/weasyprint/svg/__init__.py index 245cd182b..b047a2494 100644 --- a/weasyprint/svg/__init__.py +++ b/weasyprint/svg/__init__.py @@ -457,6 +457,11 @@ def draw_node(self, node, font_size, fill_stroke=True): if node.display and TAGS.get(node.tag) == text: node.text_bounding_box = EMPTY_BOUNDING_BOX + # Save inner size of svg tags + if node.tag == 'svg': + inner_width = self.inner_width + inner_height = self.inner_height + # Draw node if node.visible and node.tag in TAGS: with suppress(PointError): @@ -464,11 +469,6 @@ def draw_node(self, node, font_size, fill_stroke=True): # Draw node children if node.display and node.tag not in DEF_TYPES: - if node.tag == 'svg': - inner_width = self.inner_width - inner_height = self.inner_height - self.inner_width = node.width - self.inner_height = node.height for child in node: self.draw_node(child, font_size, fill_stroke) visible_text_child = ( @@ -483,9 +483,11 @@ def draw_node(self, node, font_size, fill_stroke=True): y2 = y1 + child.text_bounding_box[3] node.text_bounding_box = extend_bounding_box( node.text_bounding_box, ((x1, y1), (x2, y2))) - if node.tag == 'svg': - self.inner_width = inner_width - self.inner_height = inner_height + + # Restore inner size of svg tags + if node.tag == 'svg': + self.inner_width = inner_width + self.inner_height = inner_height # Handle text anchor if node.tag == 'text' and text_anchor in ('middle', 'end'): diff --git a/weasyprint/svg/images.py b/weasyprint/svg/images.py index 488c01632..192096413 100644 --- a/weasyprint/svg/images.py +++ b/weasyprint/svg/images.py @@ -23,8 +23,8 @@ def svg(svg, node, font_size): node._etree_node.tag = 'svg' else: width, height = svg.point(width, height, font_size) - node.width = width - node.height = height + svg.inner_width = width + svg.inner_height = height scale_x, scale_y, translate_x, translate_y = preserve_ratio( svg, node, font_size, width, height) if svg.tree != node and node.get('overflow', 'hidden') == 'hidden': From 37002a588abb4fb51e7ad1699ef86fa07b57c92e Mon Sep 17 00:00:00 2001 From: Guillaume Ayoub Date: Tue, 27 Aug 2024 22:31:13 +0200 Subject: [PATCH 5/6] Use single method to set SVG sizes --- weasyprint/svg/__init__.py | 38 +++++++++++++++++++------------------- weasyprint/svg/images.py | 3 +-- 2 files changed, 20 insertions(+), 21 deletions(-) diff --git a/weasyprint/svg/__init__.py b/weasyprint/svg/__init__.py index b047a2494..46002e81e 100644 --- a/weasyprint/svg/__init__.py +++ b/weasyprint/svg/__init__.py @@ -307,6 +307,19 @@ def override_iter(self, iterator): self.__class__ = type( 'Node', (Node,), {'__iter__': lambda _: iterator}) + def set_svg_size(self, svg, concrete_width, concrete_height): + """"Set SVG concrete and inner widths and heights from svg node.""" + svg.concrete_width = concrete_width + svg.concrete_height = concrete_height + svg.normalized_diagonal = hypot(concrete_width, concrete_height) / sqrt(2) + + if viewbox := self.get_viewbox(): + svg.inner_width, svg.inner_height = viewbox[2], viewbox[3] + else: + svg.inner_width, svg.inner_height = svg.concrete_width, svg.concrete_height + svg.inner_diagonal = hypot(svg.inner_width, svg.inner_height) / sqrt(2) + + class SVG: """An SVG document.""" @@ -369,19 +382,7 @@ def draw(self, stream, concrete_width, concrete_height, base_url, """Draw image on a stream.""" self.stream = stream - self.concrete_width = concrete_width - self.concrete_height = concrete_height - self.normalized_diagonal = ( - hypot(concrete_width, concrete_height) / sqrt(2)) - - viewbox = self.get_viewbox() - if viewbox: - self.inner_width, self.inner_height = viewbox[2], viewbox[3] - else: - self.inner_width = self.concrete_width - self.inner_height = self.concrete_height - self.inner_diagonal = ( - hypot(self.inner_width, self.inner_height) / sqrt(2)) + self.tree.set_svg_size(self, concrete_width, concrete_height) self.base_url = base_url self.url_fetcher = url_fetcher @@ -457,10 +458,10 @@ def draw_node(self, node, font_size, fill_stroke=True): if node.display and TAGS.get(node.tag) == text: node.text_bounding_box = EMPTY_BOUNDING_BOX - # Save inner size of svg tags + # Save concrete size of root svg tag if node.tag == 'svg': - inner_width = self.inner_width - inner_height = self.inner_height + concrete_width = self.concrete_width + concrete_height = self.concrete_height # Draw node if node.visible and node.tag in TAGS: @@ -484,10 +485,9 @@ def draw_node(self, node, font_size, fill_stroke=True): node.text_bounding_box = extend_bounding_box( node.text_bounding_box, ((x1, y1), (x2, y2))) - # Restore inner size of svg tags + # Restore concrete and inner size of root svg tag if node.tag == 'svg': - self.inner_width = inner_width - self.inner_height = inner_height + self.tree.set_svg_size(svg, concrete_width, concrete_height) # Handle text anchor if node.tag == 'text' and text_anchor in ('middle', 'end'): diff --git a/weasyprint/svg/images.py b/weasyprint/svg/images.py index 192096413..4f0e88f42 100644 --- a/weasyprint/svg/images.py +++ b/weasyprint/svg/images.py @@ -23,8 +23,7 @@ def svg(svg, node, font_size): node._etree_node.tag = 'svg' else: width, height = svg.point(width, height, font_size) - svg.inner_width = width - svg.inner_height = height + node.set_svg_size(svg, width, height) scale_x, scale_y, translate_x, translate_y = preserve_ratio( svg, node, font_size, width, height) if svg.tree != node and node.get('overflow', 'hidden') == 'hidden': From 3d2d15dce49a822a0f9d82ab9641a259b37cb5bc Mon Sep 17 00:00:00 2001 From: Guillaume Ayoub Date: Wed, 25 Sep 2024 22:23:35 +0200 Subject: [PATCH 6/6] Add test of SVG with no explicit size --- tests/draw/svg/test_bounding_box.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/tests/draw/svg/test_bounding_box.py b/tests/draw/svg/test_bounding_box.py index 32ac5d446..f85a0af68 100644 --- a/tests/draw/svg/test_bounding_box.py +++ b/tests/draw/svg/test_bounding_box.py @@ -309,3 +309,22 @@ def test_bounding_box_path_s(assert_pixels): ''') + + +@assert_no_logs +def test_svg_empty_size(assert_pixels): + assert_pixels(''' + BBB__ + BBB__ + BBB__ + BBB__ + _____ + ''', ''' + + + + + ''')