Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

incorect dimensions with only one of width or height? #260

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions svgelements/svgelements.py
Original file line number Diff line number Diff line change
Expand Up @@ -9146,6 +9146,20 @@ def parse(
# explicit transform, parent transforms, attribute transforms, viewport transforms
s = SVG(values)

# if we have only one of `s.width` and `s.height` we can deduce the other one
# assuming we have a viewbox.
if s.viewbox is not None:
has_absolute_width = isinstance(s.width, float) or (
isinstance(s.width, Length) and s.width.units != "%"
)
has_absolute_height = isinstance(s.height, float) or (
isinstance(s.height, Length) and s.height.units != "%"
)
if width is None and has_absolute_width and not has_absolute_height:
s.height = s.width * s.viewbox.height / s.viewbox.width
elif height is None and not has_absolute_width and has_absolute_height:
s.width = s.height * s.viewbox.width / s.viewbox.height

if width is None:
# If a dim was not provided but a viewbox was, use the viewbox dim as physical size, else 1000
width = (
Expand Down
44 changes: 44 additions & 0 deletions test/test_viewbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,50 @@ def test_viewbox_incomplete_height_viewbox(self):
self.assertEqual(m.width, 200)
self.assertEqual(m.height, 200)

def test_viewbox_incomplete_height_viewbox_default_dimensions(self):
q = io.StringIO(u'''<?xml version="1.0" encoding="utf-8" ?>
<svg viewBox="0, 0, 100, 200" height="400"/>''')
m = SVG.parse(q)
self.assertEqual(Matrix(m.viewbox_transform), 'scale(2)')
self.assertEqual(m.width, 2 * 100)
self.assertEqual(m.height, 2 * 200)

q = io.StringIO(u'''<?xml version="1.0" encoding="utf-8" ?>
<svg viewBox="0, 0, 100, 200" height="10in"/>''')
m = SVG.parse(q)
self.assertEqual(Matrix(m.viewbox_transform), 'scale(4.8)')
self.assertEqual(m.width, 4.8 * 100)
self.assertEqual(m.height, 4.8 * 200)

q = io.StringIO(u'''<?xml version="1.0" encoding="utf-8" ?>
<svg viewBox="0, 0, 100, 200" height="100%"/>''')
m = SVG.parse(q)
self.assertEqual(Matrix(m.viewbox_transform), 'scale(1)')
self.assertEqual(m.width, 100)
self.assertEqual(m.height, 200)

def test_viewbox_incomplete_width_viewbox_default_dimensions(self):
q = io.StringIO(u'''<?xml version="1.0" encoding="utf-8" ?>
<svg viewBox="0, 0, 100, 200" width="400"/>''')
m = SVG.parse(q)
self.assertEqual(Matrix(m.viewbox_transform), 'scale(4)')
self.assertEqual(m.width, 4 * 100)
self.assertEqual(m.height, 4 * 200)

q = io.StringIO(u'''<?xml version="1.0" encoding="utf-8" ?>
<svg viewBox="0, 0, 100, 200" width="10in"/>''')
m = SVG.parse(q)
self.assertEqual(Matrix(m.viewbox_transform), 'scale(9.6)')
self.assertEqual(m.width, 9.6 * 100)
self.assertEqual(m.height, 9.6 * 200)

q = io.StringIO(u'''<?xml version="1.0" encoding="utf-8" ?>
<svg viewBox="0, 0, 100, 200" width="100%"/>''')
m = SVG.parse(q)
self.assertEqual(Matrix(m.viewbox_transform), 'scale(1)')
self.assertEqual(m.width, 100)
self.assertEqual(m.height, 200)

def test_viewbox_aspect_ratio_xMinMax(self):
q = io.StringIO(u'''<?xml version="1.0" encoding="utf-8" ?>
<svg preserveAspectRatio="xMid" viewBox="0 0 100 100" height="100" width="300"/>''')
Expand Down