Skip to content

Commit

Permalink
Fixed crash when reading SVG with simplify active (read --simplify
Browse files Browse the repository at this point in the history
…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 <[email protected]>
  • Loading branch information
nataquinones and abey79 authored Jun 3, 2024
1 parent 1a571b2 commit ce0b7cf
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 -"),
Expand Down
3 changes: 2 additions & 1 deletion vpype/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down

0 comments on commit ce0b7cf

Please sign in to comment.