Skip to content

Commit

Permalink
Use single method to set SVG sizes
Browse files Browse the repository at this point in the history
  • Loading branch information
liZe committed Aug 27, 2024
1 parent ac165d5 commit 37002a5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 21 deletions.
38 changes: 19 additions & 19 deletions weasyprint/svg/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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'):
Expand Down
3 changes: 1 addition & 2 deletions weasyprint/svg/images.py
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand Down

0 comments on commit 37002a5

Please sign in to comment.