summaryrefslogtreecommitdiff
path: root/lib/utils
diff options
context:
space:
mode:
Diffstat (limited to 'lib/utils')
-rw-r--r--lib/utils/dotdict.py2
-rw-r--r--lib/utils/geometry.py67
-rw-r--r--lib/utils/inkscape.py19
-rwxr-xr-x[-rw-r--r--]lib/utils/paths.py10
-rwxr-xr-x[-rw-r--r--]lib/utils/version.py5
5 files changed, 89 insertions, 14 deletions
diff --git a/lib/utils/dotdict.py b/lib/utils/dotdict.py
index acd575b9..12cf6e79 100644
--- a/lib/utils/dotdict.py
+++ b/lib/utils/dotdict.py
@@ -15,7 +15,7 @@ class DotDict(dict):
def update(self, *args, **kwargs):
super(DotDict, self).update(*args, **kwargs)
- self.dotdictify()
+ self._dotdictify()
def _dotdictify(self):
for k, v in self.items():
diff --git a/lib/utils/geometry.py b/lib/utils/geometry.py
index bce278ed..8d29ddb0 100644
--- a/lib/utils/geometry.py
+++ b/lib/utils/geometry.py
@@ -5,7 +5,7 @@
import math
-from shapely.geometry import LineString
+from shapely.geometry import LineString, LinearRing, MultiLineString, Polygon, MultiPolygon, GeometryCollection
from shapely.geometry import Point as ShapelyPoint
@@ -39,6 +39,62 @@ def cut(line, distance, normalized=False):
LineString([(cp.x, cp.y)] + coords[i:])]
+def roll_linear_ring(ring, distance, normalized=False):
+ """Make a linear ring start at a different point.
+
+ Example: A B C D E F G A -> D E F G A B C
+
+ Same linear ring, different ordering of the coordinates.
+ """
+
+ if not isinstance(ring, LinearRing):
+ # In case they handed us a LineString
+ ring = LinearRing(ring)
+
+ pieces = cut(LinearRing(ring), distance, normalized=False)
+
+ if None in pieces:
+ # We cut exactly at the start or end.
+ return ring
+
+ # The first and last point in a linear ring are duplicated, so we omit one
+ # copy
+ return LinearRing(pieces[1].coords[:] + pieces[0].coords[1:])
+
+
+def reverse_line_string(line_string):
+ return LineString(line_string.coords[::-1])
+
+
+def ensure_multi_line_string(thing):
+ """Given either a MultiLineString or a single LineString, return a MultiLineString"""
+
+ if isinstance(thing, LineString):
+ return MultiLineString([thing])
+ else:
+ return thing
+
+
+def ensure_geometry_collection(thing):
+ """Given either some kind of geometry or a GeometryCollection, return a GeometryCollection"""
+
+ if isinstance(thing, (MultiLineString, MultiPolygon)):
+ return GeometryCollection(thing.geoms)
+ elif isinstance(thing, GeometryCollection):
+ return thing
+ else:
+ return GeometryCollection([thing])
+
+
+def ensure_multi_polygon(thing):
+ """Given either a MultiPolygon or a single Polygon, return a MultiPolygon"""
+
+ if isinstance(thing, Polygon):
+ return MultiPolygon([thing])
+ else:
+ return thing
+
+
def cut_path(points, length):
"""Return a subsection of at the start of the path that is length units long.
@@ -69,6 +125,10 @@ class Point:
self.x = x
self.y = y
+ @classmethod
+ def from_shapely_point(cls, point):
+ return cls(point.x, point.y)
+
def __json__(self):
return vars(self)
@@ -99,12 +159,15 @@ class Point:
else:
raise ValueError("cannot multiply %s by %s" % (type(self), type(other)))
- def __div__(self, other):
+ def __truediv__(self, other):
if isinstance(other, (int, float)):
return self * (1.0 / other)
else:
raise ValueError("cannot divide %s by %s" % (type(self), type(other)))
+ def __eq__(self, other):
+ return self.x == other.x and self.y == other.y
+
def __repr__(self):
return "%s(%s,%s)" % (type(self), self.x, self.y)
diff --git a/lib/utils/inkscape.py b/lib/utils/inkscape.py
index 62442650..58c4ea9b 100644
--- a/lib/utils/inkscape.py
+++ b/lib/utils/inkscape.py
@@ -4,18 +4,21 @@
# Licensed under the GNU GPL version 3.0 or later. See the file LICENSE for details.
import sys
-from os.path import expanduser, realpath
+from os.path import expanduser, realpath, split
+
+from inkex.utils import get_user_directory
def guess_inkscape_config_path():
if getattr(sys, 'frozen', None):
- path = realpath(sys._MEIPASS.split('extensions', 1)[0])
- if sys.platform == "win32":
- import win32api
-
- # This expands ugly things like EXTENS~1
- path = win32api.GetLongPathName(path)
+ if get_user_directory() is not None:
+ path = split(get_user_directory())[0]
+ else:
+ path = realpath(sys._MEIPASS.split('extensions', 1)[0])
+ if sys.platform == "win32":
+ import win32api
+ # This expands ugly things like EXTENS~1
+ path = win32api.GetLongPathName(path)
else:
path = expanduser("~/.config/inkscape")
-
return path
diff --git a/lib/utils/paths.py b/lib/utils/paths.py
index 938c5f33..2a95f6e7 100644..100755
--- a/lib/utils/paths.py
+++ b/lib/utils/paths.py
@@ -10,13 +10,19 @@ from os.path import dirname, realpath
def get_bundled_dir(name):
if getattr(sys, 'frozen', None) is not None:
- return realpath(os.path.join(sys._MEIPASS, "..", name))
+ if sys.platform == "darwin":
+ return realpath(os.path.join(sys._MEIPASS, "..", 'Resources', name))
+ else:
+ return realpath(os.path.join(sys._MEIPASS, "..", name))
else:
return realpath(os.path.join(dirname(realpath(__file__)), '..', '..', name))
def get_resource_dir(name):
if getattr(sys, 'frozen', None) is not None:
- return realpath(os.path.join(sys._MEIPASS, name))
+ if sys.platform == "darwin":
+ return realpath(os.path.join(sys._MEIPASS, "..", 'Resources', name))
+ else:
+ return realpath(os.path.join(sys._MEIPASS, name))
else:
return realpath(os.path.join(dirname(realpath(__file__)), '..', '..', name))
diff --git a/lib/utils/version.py b/lib/utils/version.py
index 2186ca23..0b46669a 100644..100755
--- a/lib/utils/version.py
+++ b/lib/utils/version.py
@@ -11,7 +11,10 @@ from ..i18n import _
def get_inkstitch_version():
if getattr(sys, 'frozen', False) and hasattr(sys, '_MEIPASS'):
- version = realpath(join(sys._MEIPASS, "..", "VERSION"))
+ if sys.platform == "darwin":
+ version = realpath(join(sys._MEIPASS, "..", 'Resources', "VERSION"))
+ else:
+ version = realpath(join(sys._MEIPASS, "..", "VERSION"))
else:
version = realpath(join(realpath(__file__), "..", "..", "..", 'VERSION'))
if isfile(version):