Skip to content

Commit 0331051

Browse files
committed
Restore nested SVG viewport size on the SVG object, not the drawing function
1 parent 515f676 commit 0331051

2 files changed

Lines changed: 79 additions & 1 deletion

File tree

‎tests/draw/svg/test_units.py‎

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,81 @@ def test_units_unknown(assert_pixels):
9999
stroke-width="2px" stroke="red" fill="none" />
100100
</svg>
101101
''')
102+
103+
104+
@assert_no_logs
105+
def test_units_percentage(assert_pixels):
106+
assert_pixels('''
107+
________
108+
________
109+
________
110+
________
111+
____BB__
112+
____BB__
113+
________
114+
________
115+
''', '''
116+
<style>
117+
@page { size: 8px }
118+
svg { display: block }
119+
</style>
120+
<svg width="8px" height="8px" xmlns="http://www.w3.org/2000/svg">
121+
<rect x="50%" y="50%" width="2" height="2" fill="blue" />
122+
</svg>
123+
''')
124+
125+
126+
@assert_no_logs
127+
def test_units_percentage_after_nested_svg(assert_pixels):
128+
# Percentages must be resolved against the current viewport, not against
129+
# the viewport of a nested "svg" tag that has already been drawn.
130+
assert_pixels('''
131+
RR______
132+
RR______
133+
________
134+
________
135+
____BB__
136+
____BB__
137+
________
138+
________
139+
''', '''
140+
<style>
141+
@page { size: 8px }
142+
svg { display: block }
143+
</style>
144+
<svg width="8px" height="8px" xmlns="http://www.w3.org/2000/svg">
145+
<svg width="100%" height="100%" viewBox="0 0 4 4">
146+
<rect width="1" height="1" fill="red" />
147+
</svg>
148+
<rect x="50%" y="50%" width="2" height="2" fill="blue" />
149+
</svg>
150+
''')
151+
152+
153+
@assert_no_logs
154+
def test_units_percentage_sibling_nested_svg(assert_pixels):
155+
# Each nested "svg" tag must resolve its own percentages against the
156+
# parent viewport, including when a sibling has been drawn before.
157+
assert_pixels('''
158+
________
159+
________
160+
__RR____
161+
__RR____
162+
____BB__
163+
____BB__
164+
________
165+
________
166+
''', '''
167+
<style>
168+
@page { size: 8px }
169+
svg { display: block }
170+
</style>
171+
<svg width="8px" height="8px" xmlns="http://www.w3.org/2000/svg">
172+
<svg x="25%" y="25%" width="1" height="1" overflow="visible">
173+
<rect width="2" height="2" fill="red" />
174+
</svg>
175+
<svg x="50%" y="50%" width="1" height="1" overflow="visible">
176+
<rect width="2" height="2" fill="blue" />
177+
</svg>
178+
</svg>
179+
''')

‎weasyprint/svg/__init__.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,7 @@ def draw_node(self, node, font_size, fill_stroke=True, context=None):
559559

560560
# Restore concrete and inner size of root svg tag
561561
if node.tag == 'svg':
562-
self.tree.set_svg_size(svg, concrete_width, concrete_height)
562+
self.tree.set_svg_size(self, concrete_width, concrete_height)
563563

564564
# Handle text anchor
565565
if text_anchor_shift:

0 commit comments

Comments
 (0)