Skip to content

Commit

Permalink
Override use tag children instead of drawing their references
Browse files Browse the repository at this point in the history
This change fixes cases when use tags are not supposed to be really drawn, for
example when used to clip paths.
  • Loading branch information
liZe committed Apr 29, 2023
1 parent 0ff8692 commit d0ad5c1
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
13 changes: 8 additions & 5 deletions weasyprint/svg/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,13 @@ def pop_rotation(self, original_rotate, rotate):
str(rotate.pop(0) if rotate else original_rotate[-1])
for i in range(len(self.text)))

def override_iter(self, iterator):
"""Override node’s children iterator."""
# As special methods are bound to classes and not instances, we have to
# create and assign a new type.
self.__class__ = type(
'Node', (Node,), {'__iter__': lambda _: iterator})


class SVG:
"""An SVG document."""
Expand Down Expand Up @@ -737,11 +744,7 @@ def inherit_element(self, element, defs):
if key not in element.attrib:
element.attrib[key] = value
if next(iter(element), None) is None:
# Override element’s __iter__ with parent’s __iter__. As special
# methods are bound to classes and not instances, we have to create
# and assign a new type.
element.__class__ = type(
'Node', (Node,), {'__iter__': lambda self: parent.__iter__()})
element.override_iter(parent.__iter__())

def calculate_bounding_box(self, node, font_size, stroke=True):
"""Calculate the bounding box of a node."""
Expand Down
4 changes: 1 addition & 3 deletions weasyprint/svg/defs.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,8 @@ def use(svg, node, font_size):
if key not in tree.attrib:
tree.attrib[key] = value

svg.stream.push_state()
node.override_iter(iter((tree,)))
svg.stream.transform(e=x, f=y)
svg.draw_node(tree, font_size)
svg.stream.pop_state()


def draw_gradient_or_pattern(svg, node, name, font_size, opacity, stroke):
Expand Down

0 comments on commit d0ad5c1

Please sign in to comment.