From a1c2139521fb8f3a7e99229c2f8f06b75d1bf716 Mon Sep 17 00:00:00 2001 From: Kaalleen <36401965+kaalleen@users.noreply.github.com> Date: Sat, 28 Mar 2020 11:18:52 +0100 Subject: preserveAspectRatio (#641) --- lib/svg/units.py | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'lib/svg/units.py') diff --git a/lib/svg/units.py b/lib/svg/units.py index 739dcbb4..d97ea418 100644 --- a/lib/svg/units.py +++ b/lib/svg/units.py @@ -127,6 +127,17 @@ def get_viewbox_transform(node): try: sx = doc_width / float(viewbox[2]) sy = doc_height / float(viewbox[3]) + + # preserve aspect ratio + aspect_ratio = node.get('preserveAspectRatio', 'xMidYMid meet') + if aspect_ratio != 'none': + unit = parse_length_with_units(node.get('width'))[1] + if float(viewbox[2]) > parse_length_with_units(node.get('width'))[0]: + sx = convert_length(viewbox[2] + unit) / float(viewbox[2]) if 'slice' in aspect_ratio else 1.0 + if float(viewbox[3]) > parse_length_with_units(node.get('height'))[0]: + sy = convert_length(viewbox[3] + unit) / float(viewbox[3]) if 'slice' in aspect_ratio else 1.0 + sx = sy = max(sx, sy) if 'slice' in aspect_ratio else min(sx, sy) + scale_transform = simpletransform.parseTransform("scale(%f, %f)" % (sx, sy)) transform = simpletransform.composeTransform(transform, scale_transform) except ZeroDivisionError: -- cgit v1.2.3