summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/extensions/palette_split_text.py32
-rw-r--r--lib/stitches/contour_fill.py25
2 files changed, 37 insertions, 20 deletions
diff --git a/lib/extensions/palette_split_text.py b/lib/extensions/palette_split_text.py
index 3257d694..19b70782 100644
--- a/lib/extensions/palette_split_text.py
+++ b/lib/extensions/palette_split_text.py
@@ -11,32 +11,38 @@ from .base import InkstitchExtension
class PaletteSplitText(InkstitchExtension):
# Splits sublines of text into it's own text elements in order to color them with the color picker
- def __init__(self, *args, **kwargs):
- InkstitchExtension.__init__(self, *args, **kwargs)
- self.arg_parser.add_argument("-l", "--line-height", type=int, default=6, dest="line_height")
-
def effect(self):
if not self.svg.selection:
inkex.errormsg(_("Please select one or more text elements to split lines."))
return
- line_height = self.options.line_height
-
for text in self.svg.selection.get(inkex.elements.TextElement):
parent = text.getparent()
content = text.get_text()
lines = content.split('\n')
lines.reverse()
- style = text.get('style')
- x = text.get('x')
- y = text.get('y')
- y = float(y) + (len(lines) - 1) * line_height
+ lines = [line for line in lines if not len(line) == 0]
+
+ style = text.style
+ # If shape-inside style is used, it will lead to bad placement
+ style.pop('shape-inside', None)
+
+ transform = text.transform
+ text.pop('transform')
+
+ bbox = text.get_inkscape_bbox()
+ x = bbox.left
+ y = bbox.bottom
+ height = bbox.height / (len(lines))
+
for line in lines:
element = inkex.TextElement()
element.text = line
- element.set('style', style)
- element.set('x', x)
+ element.set('style', str(style))
+ element.set('x', str(x))
element.set('y', str(y))
- y = float(y) - line_height
+ element.set('transform', str(transform))
+
+ y -= height
parent.insert(0, element)
parent.remove(text)
diff --git a/lib/stitches/contour_fill.py b/lib/stitches/contour_fill.py
index f2ab2aef..86a55406 100644
--- a/lib/stitches/contour_fill.py
+++ b/lib/stitches/contour_fill.py
@@ -4,16 +4,18 @@ from itertools import chain
import networkx as nx
import numpy as np
import trimesh
-from shapely.geometry import GeometryCollection, MultiPolygon, Polygon, LineString, Point
+from shapely.geometry import (GeometryCollection, LineString, MultiPolygon,
+ Point, Polygon)
from shapely.geometry.polygon import orient
-from shapely.ops import nearest_points
-from shapely.ops import polygonize
+from shapely.ops import nearest_points, polygonize
+from shapely.validation import make_valid
-from .running_stitch import running_stitch
from ..stitch_plan import Stitch
from ..utils import DotDict
-from ..utils.geometry import cut, reverse_line_string, roll_linear_ring
-from ..utils.geometry import ensure_geometry_collection, ensure_multi_polygon
+from ..utils.geometry import (cut, ensure_geometry_collection,
+ ensure_multi_polygon, reverse_line_string,
+ roll_linear_ring)
+from .running_stitch import running_stitch
class Tree(nx.DiGraph):
@@ -134,6 +136,7 @@ def offset_polygon(polygon, offset, join_style, clockwise):
current_holes = active_holes.pop()
outer, inners = _offset_polygon_and_holes(tree, current_poly, current_holes, offset, join_style)
+
polygons = _match_polygons_and_holes(outer, inners)
for polygon in polygons.geoms:
@@ -183,7 +186,10 @@ def _offset_polygon_and_holes(tree, poly, holes, offset, join_style):
def _match_polygons_and_holes(outer, inners):
result = MultiPolygon(polygonize(outer.geoms))
if len(inners) > 0:
- result = ensure_geometry_collection(result.difference(MultiPolygon(inners)))
+ inners = MultiPolygon(inners)
+ if not inners.is_valid:
+ inners = make_valid(MultiPolygon(inners))
+ result = ensure_geometry_collection(result.difference(inners))
return result
@@ -279,6 +285,11 @@ def _create_nearest_points_list(travel_line, tree, children, threshold, threshol
# up to 2 * used offset can arise
result = _get_nearest_points_closer_than_thresh(travel_line, tree.nodes[child].val, threshold_hard)
+ # if we still didn't get a result, ignore this child
+ # this may lead to oddities, but at least it doesn't fail
+ if result is None:
+ continue
+
proj = travel_line.project(result[0])
children_nearest_points.append(
nearest_neighbor_tuple(