1
1
mirror of https://github.com/Kozea/WeasyPrint.git synced 2024-09-11 20:47:56 +03:00

Override use tag children instead of drawing their references

This change fixes cases when use tags are not supposed to be really drawn, for
example when used to clip paths.
This commit is contained in:
Guillaume Ayoub 2023-04-29 16:30:41 +02:00
parent 0ff8692741
commit d0ad5c1f28
2 changed files with 9 additions and 8 deletions

View File

@ -284,6 +284,13 @@ class Node:
str(rotate.pop(0) if rotate else original_rotate[-1])
for i in range(len(self.text)))
def override_iter(self, iterator):
"""Override nodes 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."""
@ -737,11 +744,7 @@ class SVG:
if key not in element.attrib:
element.attrib[key] = value
if next(iter(element), None) is None:
# Override elements __iter__ with parents __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."""

View File

@ -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):