summaryrefslogtreecommitdiff
path: root/lib/utils
diff options
context:
space:
mode:
Diffstat (limited to 'lib/utils')
-rw-r--r--lib/utils/__init__.py4
-rw-r--r--lib/utils/cache.py8
-rw-r--r--lib/utils/geometry.py102
-rw-r--r--lib/utils/inkscape.py15
-rw-r--r--lib/utils/io.py17
5 files changed, 146 insertions, 0 deletions
diff --git a/lib/utils/__init__.py b/lib/utils/__init__.py
new file mode 100644
index 00000000..ff06d4a9
--- /dev/null
+++ b/lib/utils/__init__.py
@@ -0,0 +1,4 @@
+from geometry import *
+from cache import cache
+from io import *
+from inkscape import *
diff --git a/lib/utils/cache.py b/lib/utils/cache.py
new file mode 100644
index 00000000..38fe8f2c
--- /dev/null
+++ b/lib/utils/cache.py
@@ -0,0 +1,8 @@
+try:
+ from functools import lru_cache
+except ImportError:
+ from backports.functools_lru_cache import lru_cache
+
+# simplify use of lru_cache decorator
+def cache(*args, **kwargs):
+ return lru_cache(maxsize=None)(*args, **kwargs)
diff --git a/lib/utils/geometry.py b/lib/utils/geometry.py
new file mode 100644
index 00000000..61b98bcb
--- /dev/null
+++ b/lib/utils/geometry.py
@@ -0,0 +1,102 @@
+from shapely.geometry import LineString, Point as ShapelyPoint
+import math
+
+
+def cut(line, distance):
+ """ Cuts a LineString in two at a distance from its starting point.
+
+ This is an example in the Shapely documentation.
+ """
+ if distance <= 0.0 or distance >= line.length:
+ return [LineString(line), None]
+ coords = list(line.coords)
+ for i, p in enumerate(coords):
+ # TODO: I think this doesn't work if the path doubles back on itself
+ pd = line.project(ShapelyPoint(p))
+ if pd == distance:
+ return [
+ LineString(coords[:i+1]),
+ LineString(coords[i:])]
+ if pd > distance:
+ cp = line.interpolate(distance)
+ return [
+ LineString(coords[:i] + [(cp.x, cp.y)]),
+ LineString([(cp.x, cp.y)] + coords[i:])]
+
+
+def cut_path(points, length):
+ """Return a subsection of at the start of the path that is length units long.
+
+ Given a path denoted by a set of points, walk along it until we've travelled
+ the specified length and return a new path up to that point.
+
+ If the original path isn't that long, just return it as is.
+ """
+
+ if len(points) < 2:
+ return points
+
+ path = LineString(points)
+ subpath, rest = cut(path, length)
+
+ return [Point(*point) for point in subpath.coords]
+
+
+class Point:
+ def __init__(self, x, y):
+ self.x = x
+ self.y = y
+
+ def __add__(self, other):
+ return Point(self.x + other.x, self.y + other.y)
+
+ def __sub__(self, other):
+ return Point(self.x - other.x, self.y - other.y)
+
+ def mul(self, scalar):
+ return Point(self.x * scalar, self.y * scalar)
+
+ def __mul__(self, other):
+ if isinstance(other, Point):
+ # dot product
+ return self.x * other.x + self.y * other.y
+ elif isinstance(other, (int, float)):
+ return Point(self.x * other, self.y * other)
+ else:
+ raise ValueError("cannot multiply Point by %s" % type(other))
+
+ def __rmul__(self, other):
+ if isinstance(other, (int, float)):
+ return self.__mul__(other)
+ else:
+ raise ValueError("cannot multiply Point by %s" % type(other))
+
+ def __repr__(self):
+ return "Point(%s,%s)" % (self.x, self.y)
+
+ def length(self):
+ return math.sqrt(math.pow(self.x, 2.0) + math.pow(self.y, 2.0))
+
+ def unit(self):
+ return self.mul(1.0 / self.length())
+
+ def rotate_left(self):
+ return Point(-self.y, self.x)
+
+ def rotate(self, angle):
+ return Point(self.x * math.cos(angle) - self.y * math.sin(angle), self.y * math.cos(angle) + self.x * math.sin(angle))
+
+ def as_int(self):
+ return Point(int(round(self.x)), int(round(self.y)))
+
+ def as_tuple(self):
+ return (self.x, self.y)
+
+ def __cmp__(self, other):
+ return cmp(self.as_tuple(), other.as_tuple())
+
+ def __getitem__(self, item):
+ return self.as_tuple()[item]
+
+ def __len__(self):
+ return 2
diff --git a/lib/utils/inkscape.py b/lib/utils/inkscape.py
new file mode 100644
index 00000000..2d0298bc
--- /dev/null
+++ b/lib/utils/inkscape.py
@@ -0,0 +1,15 @@
+from os.path import realpath, expanduser, join as path_join
+import sys
+
+def guess_inkscape_config_path():
+ if getattr(sys, 'frozen', None):
+ path = realpath(path_join(sys._MEIPASS, "..", "..", ".."))
+ 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/io.py b/lib/utils/io.py
new file mode 100644
index 00000000..e87b9881
--- /dev/null
+++ b/lib/utils/io.py
@@ -0,0 +1,17 @@
+import os
+import sys
+from cStringIO import StringIO
+
+def save_stderr():
+ # GTK likes to spam stderr, which inkscape will show in a dialog.
+ null = open(os.devnull, 'w')
+ sys.stderr_dup = os.dup(sys.stderr.fileno())
+ os.dup2(null.fileno(), 2)
+ sys.stderr_backup = sys.stderr
+ sys.stderr = StringIO()
+
+
+def restore_stderr():
+ os.dup2(sys.stderr_dup, 2)
+ sys.stderr_backup.write(sys.stderr.getvalue())
+ sys.stderr = sys.stderr_backup