Skip to content
Merged
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
22 changes: 11 additions & 11 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[build-system]
requires = ["setuptools"]
requires = ["setuptools >= 77.0.3"]
build-backend = "setuptools.build_meta"

[project]
Expand All @@ -19,17 +19,17 @@ license = "MIT"
license-files = ["LICENSE"]
keywords = ["embroidery", "file conversion"]
classifiers = [
"Development Status :: 5 - Production/Stable",
"Intended Audience :: Developers",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: 3.13",
"Operating System :: OS Independent",
"Topic :: Software Development :: Libraries :: Python Modules"
"Development Status :: 5 - Production/Stable",
"Intended Audience :: Developers",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: 3.13",
"Operating System :: OS Independent",
"Topic :: Software Development :: Libraries :: Python Modules"
]

[project.urls]
Repository = "https://github.com/inkstitch/pystitch"
Repository = "https://github.com/inkstitch/pystitch.git"
Issues = "https://github.com/inkstitch/pystitch/issues"
92 changes: 38 additions & 54 deletions src/pystitch/EmbPattern.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
from pathlib import PurePath

from .EmbEncoder import Transcoder as Normalizer
from .EmbFunctions import *
Expand Down Expand Up @@ -86,7 +87,7 @@ def __iadd__(self, other):
self.add_command(other)
elif isinstance(other, list) or isinstance(other, tuple): # tuple or list
if len(other) == 0:
return
return self
v = other[0]
if isinstance(v, list) or isinstance(
v, tuple
Expand Down Expand Up @@ -245,8 +246,8 @@ def bounds(self):
min_x, min_y, max_x, max_y"""
min_x = float("inf")
min_y = float("inf")
max_x = -float("inf")
max_y = -float("inf")
max_x = float("-inf")
max_y = float("-inf")

for stitch in self.stitches:
if stitch[0] > max_x:
Expand Down Expand Up @@ -541,7 +542,7 @@ def add_pattern(self, pattern, dx=None, dy=None, sx=None, sy=None, rotate=None):
if dy is None:
dy = 0
self.add_command(MATRIX_TRANSLATE, dx, dy)
if sx is not None or sx is not None:
if sx is not None or sy is not None:
if sx is None:
sx = sy
if sy is None:
Expand Down Expand Up @@ -625,15 +626,9 @@ def interpolate_frame_eject(self):
while position < ie:
stitch = self.stitches[position]
data = stitch[2] & COMMAND_MASK
if (
data == STITCH
or data == SEW_TO
or data == NEEDLE_AT
or data == COLOR_CHANGE
or data == COLOR_BREAK
or data == NEEDLE_SET
):
if data in (STITCH, SEW_TO, NEEDLE_AT, COLOR_CHANGE, COLOR_BREAK, NEEDLE_SET):
if mode == 3:
assert sequence_start_position is not None
del self.stitches[sequence_start_position:position]
position = sequence_start_position
self.stitches.insert(position, [stop_x, stop_y, FRAME_EJECT])
Expand All @@ -652,6 +647,7 @@ def interpolate_frame_eject(self):
stop_y = stitch[1]
position += 1
if mode >= 2: # Frame_eject at end.
assert sequence_start_position is not None
del self.stitches[sequence_start_position:position]
position = sequence_start_position
self.stitches.insert(position, [stop_x, stop_y, FRAME_EJECT])
Expand Down Expand Up @@ -798,7 +794,7 @@ def append_translation(self, x, y):
@staticmethod
def get_extension_by_filename(filename):
"""extracts the extension from a filename"""
return os.path.splitext(filename)[1][1:]
return PurePath(filename).suffix[1:]

@staticmethod
def read_embroidery(reader, f, settings=None, pattern=None):
Expand All @@ -808,109 +804,97 @@ def read_embroidery(reader, f, settings=None, pattern=None):
if pattern is None:
pattern = EmbPattern()

if isinstance(f, str):
text_mode = False
try:
text_mode = reader.READ_FILE_IN_TEXT_MODE
except AttributeError:
pass
if isinstance(f, (str, os.PathLike)):
text_mode = getattr(reader, "READ_FILE_IN_TEXT_MODE", False)
if text_mode:
try:
with open(f, "r", errors='ignore') as stream:
reader.read(stream, pattern, settings)
stream.close()
return EmbPattern.read_embroidery(reader, stream, settings, pattern)
except IOError:
pass
return pattern
else:
with open(f, "rb") as stream:
reader.read(stream, pattern, settings)
else:
reader.read(f, pattern, settings)
return EmbPattern.read_embroidery(reader, stream, settings, pattern)

reader.read(f, pattern, settings)

return pattern


@staticmethod
def write_embroidery(writer, pattern, stream, settings=None):
if pattern is None:
return

if isinstance(stream, (str, os.PathLike)):
text_mode = getattr(writer, "WRITE_FILE_IN_TEXT_MODE", False)
file_mode = "w" if text_mode else "wb"
with open(stream, file_mode) as stream:
return EmbPattern.write_embroidery(writer, pattern, stream, settings)

if settings is None:
settings = {}
else:
settings = settings.copy()
try:
encode = writer.ENCODE
except AttributeError:
encode = True

encode = getattr(writer, "ENCODE", True)

if settings.get("encode", encode):
if not ("max_jump" in settings):
if "max_jump" not in settings:
try:
settings["max_jump"] = writer.MAX_JUMP_DISTANCE
except AttributeError:
pass
if not ("max_stitch" in settings):
if "max_stitch" not in settings:
try:
settings["max_stitch"] = writer.MAX_STITCH_DISTANCE
except AttributeError:
pass
if not ("full_jump" in settings):
if "full_jump" not in settings:
try:
settings["full_jump"] = writer.FULL_JUMP
except AttributeError:
pass
if not ("round" in settings):
if "round" not in settings:
try:
settings["round"] = writer.ROUND
except AttributeError:
pass
if not ("writes_speeds" in settings):
if "writes_speeds" not in settings:
try:
settings["writes_speeds"] = writer.WRITES_SPEEDS
except AttributeError:
pass
if not ("sequin_contingency" in settings):
if "sequin_contingency" not in settings:
try:
settings["sequin_contingency"] = writer.SEQUIN_CONTINGENCY
except AttributeError:
pass
if not ("thread_change_command" in settings):
if "thread_change_command" not in settings:
try:
settings["thread_change_command"] = writer.THREAD_CHANGE_COMMAND
except AttributeError:
pass
if not ("explicit_trim" in settings):
if "explicit_trim" not in settings:
try:
settings["explicit_trim"] = writer.EXPLICIT_TRIM
except AttributeError:
pass
if not ("translate" in settings):
if "translate" not in settings:
try:
settings["translate"] = writer.TRANSLATE
except AttributeError:
pass
if not ("scale" in settings):
if "scale" not in settings:
try:
settings["scale"] = writer.SCALE
except AttributeError:
pass
if not ("rotate" in settings):
if "rotate" not in settings:
try:
settings["rotate"] = writer.ROTATE
except AttributeError:
pass
pattern = pattern.get_normalized_pattern(settings)

if isinstance(stream, str):
text_mode = False
try:
text_mode = writer.WRITE_FILE_IN_TEXT_MODE
except AttributeError:
pass
if text_mode:
with open(stream, "w") as stream:
writer.write(pattern, stream, settings)
else:
with open(stream, "wb") as stream:
writer.write(pattern, stream, settings)
else:
writer.write(pattern, stream, settings)
writer.write(pattern, stream, settings)
Loading
Loading