From ce0b7cf711d2898eebfb1b8d950cb4b3a9d29312 Mon Sep 17 00:00:00 2001 From: Natalia Quinones-Olvera Date: Mon, 3 Jun 2024 04:47:48 -0400 Subject: [PATCH] Fixed crash when reading SVG with simplify active (`read --simplify` or using the read APIs with `simplify=True`) (#732) * Fix TypeError in _process_path for curved elements simplification Refactored the line simplification in _process_path to avoid the numpy TypeError caused by changing data-type for an object array. Replaced np.array(LineString(line).simplify(tolerance=quantization)) with separate LineString simplification and conversion of its coordinates to a numpy array. This ensures the correct handling. * update CHANGELOG.md * added test --------- Co-authored-by: Antoine Beyeler --- CHANGELOG.md | 2 +- tests/test_commands.py | 1 + vpype/io.py | 3 ++- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 70898614..629c31d8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,7 +11,7 @@ Release date: UNRELEASED ### Bug fixes -* ... +* Fixed a crash when reading SVG with simplify active (via the `read --simplify` command or the read APIs with `simplify=True`) (thanks to @nataquinones) (#732) ## 1.14 diff --git a/tests/test_commands.py b/tests/test_commands.py index 4fa7fe13..25cc2f9e 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -43,6 +43,7 @@ class Command: Command("circle 0 0 1"), Command("ellipse 0 0 2 4"), Command(f"read '{EXAMPLE_SVG}'", preserves_metadata=False), + Command(f"read -s '{EXAMPLE_SVG}'", preserves_metadata=False), Command(f"read -m '{EXAMPLE_SVG}'", preserves_metadata=False), Command(f"read -a stroke '{EXAMPLE_SVG}'", preserves_metadata=False), Command("write -f svg -"), diff --git a/vpype/io.py b/vpype/io.py index 7bf0e97e..d1e3ded1 100644 --- a/vpype/io.py +++ b/vpype/io.py @@ -336,7 +336,8 @@ def _process_path(path): line = seg.npoint(np.linspace(0, 1, step)) if simplify: - line = np.array(LineString(line).simplify(tolerance=quantization)) + line = LineString(line).simplify(tolerance=quantization) + line = np.array(line.coords, dtype=float) line = line.view(dtype=complex).reshape(len(line))