From 9fa527ea1184893123c5cf826b491b8cc89f7abb Mon Sep 17 00:00:00 2001 From: Jan Petykiewicz Date: Mon, 11 May 2020 18:42:31 -0700 Subject: [PATCH] improve handling of patterns with no bounding box --- masque/file/svg.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/masque/file/svg.py b/masque/file/svg.py index f63af3b..8752ca2 100644 --- a/masque/file/svg.py +++ b/masque/file/svg.py @@ -4,6 +4,7 @@ SVG file format readers and writers import svgwrite import numpy +import warnings from .utils import mangle_name from .. import Pattern @@ -40,7 +41,12 @@ def writefile(pattern: Pattern, # Polygonize pattern pattern.polygonize() - [bounds_min, bounds_max] = pattern.get_bounds() + bounds = pattern.get_bounds() + if bounds is None: + bounds_min, bounds_max = numpy.array([[-1, -1], [1, 1]]) + warnings.warn('Pattern had no bounds (empty?); setting arbitrary viewbox') + else: + bounds_min, bounds_max = bounds viewbox = numpy.hstack((bounds_min - 1, (bounds_max - bounds_min) + 2)) viewbox_string = '{:g} {:g} {:g} {:g}'.format(*viewbox) @@ -100,7 +106,12 @@ def writefile_inverted(pattern: Pattern, filename: str): # Polygonize and flatten pattern pattern.polygonize().flatten() - [bounds_min, bounds_max] = pattern.get_bounds() + bounds = pattern.get_bounds() + if bounds is None: + bounds_min, bounds_max = numpy.array([[-1, -1], [1, 1]]) + warnings.warn('Pattern had no bounds (empty?); setting arbitrary viewbox') + else: + bounds_min, bounds_max = bounds viewbox = numpy.hstack((bounds_min - 1, (bounds_max - bounds_min) + 2)) viewbox_string = '{:g} {:g} {:g} {:g}'.format(*viewbox)