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__
+ _____
+ ''', '''
+
+
+ ''')
diff --git a/weasyprint/svg/__init__.py b/weasyprint/svg/__init__.py
index add060eee..46002e81e 100644
--- a/weasyprint/svg/__init__.py
+++ b/weasyprint/svg/__init__.py
@@ -81,6 +81,7 @@
'mask',
'path',
'pattern',
+ 'symbol',
))
@@ -306,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."""
@@ -368,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
@@ -390,7 +392,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
@@ -456,6 +458,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 concrete size of root svg tag
+ if node.tag == 'svg':
+ concrete_width = self.concrete_width
+ concrete_height = self.concrete_height
+
# Draw node
if node.visible and node.tag in TAGS:
with suppress(PointError):
@@ -478,6 +485,10 @@ 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 concrete and inner size of root svg tag
+ if node.tag == 'svg':
+ self.tree.set_svg_size(svg, concrete_width, concrete_height)
+
# Handle text anchor
if node.tag == 'text' and text_anchor in ('middle', 'end'):
group_id = self.stream.id
diff --git a/weasyprint/svg/images.py b/weasyprint/svg/images.py
index d6462af10..4f0e88f42 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,19 @@ 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)
+ 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':