Skip to content

Commit d537dfc

Browse files
committed
apply expand value before cross stitching
1 parent 7ace6b0 commit d537dfc

File tree

1 file changed

+34
-11
lines changed

1 file changed

+34
-11
lines changed

lib/elements/fill_stitch.py

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -890,6 +890,12 @@ def to_stitch_groups(self, previous_stitch_group, next_element=None): # noqa: C
890890
start = self.get_starting_point(previous_stitch_group)
891891
final_end = self.get_ending_point(self.next_stitch(next_element))
892892

893+
if self.fill_method == 'cross_stitch':
894+
# for cross stitch we can expand the shape before the fact,
895+
# as we can say for sure that there is not going to a mess up with the underlay shapes
896+
# and different expand values
897+
stitch_groups.extend(self.do_cross_stitch(previous_stitch_group, start, final_end))
898+
893899
# sort shapes to get a nicer routing
894900
shapes = list(self.shape.geoms)
895901
if start:
@@ -898,13 +904,17 @@ def to_stitch_groups(self, previous_stitch_group, next_element=None): # noqa: C
898904
shapes.sort(key=lambda shape: shape.bounds[0])
899905

900906
for i, shape in enumerate(shapes):
907+
if self.fill_method == 'cross_stitch':
908+
# we already created the cross stitch at this point
909+
break
910+
901911
start = self.get_starting_point(previous_stitch_group)
902912
if i < len(shapes) - 1:
903913
end = nearest_points(shape, shapes[i+1])[0].coords
904914
else:
905915
end = final_end
906916

907-
if self.fill_underlay and not self.fill_method == 'legacy_fill':
917+
if self.fill_underlay and self.fill_method not in ['legacy_fill', 'cross_stitch']:
908918
underlay_shapes = self.underlay_shape(shape)
909919
for underlay_shape in underlay_shapes.geoms:
910920
underlay_stitch_groups, start = self.do_underlay(underlay_shape, start)
@@ -918,8 +928,6 @@ def to_stitch_groups(self, previous_stitch_group, next_element=None): # noqa: C
918928
stitch_groups.extend(self.do_circular_fill(fill_shape, start, end))
919929
elif self.fill_method == 'contour_fill':
920930
stitch_groups.extend(self.do_contour_fill(fill_shape, start))
921-
elif self.fill_method == 'cross_stitch':
922-
stitch_groups.extend(self.do_cross_stitch(fill_shape, start, end))
923931
elif self.fill_method == 'guided_fill':
924932
stitch_groups.extend(self.do_guided_fill(fill_shape, start, end))
925933
elif self.fill_method == 'linear_gradient_fill':
@@ -1148,14 +1156,29 @@ def do_meander_fill(self, shape, original_shape, i, starting_point, ending_point
11481156
)
11491157
return [stitch_group]
11501158

1151-
def do_cross_stitch(self, shape, starting_point, ending_point):
1152-
stitch_group = StitchGroup(
1153-
color=self.color,
1154-
tags=("cross_stitch"),
1155-
stitches=cross_stitch(self, shape, starting_point, ending_point),
1156-
force_lock_stitches=self.force_lock_stitches,
1157-
lock_stitches=self.lock_stitches
1158-
)
1159+
def do_cross_stitch(self, previous_stitch_group, start, end):
1160+
fill_shapes = ensure_multi_polygon(make_valid(self.fill_shape(self.shape)))
1161+
fill_shapes = list(fill_shapes.geoms)
1162+
if start:
1163+
fill_shapes.sort(key=lambda shape: shape.distance(shgeo.Point(start)))
1164+
else:
1165+
fill_shapes.sort(key=lambda shape: shape.bounds[0])
1166+
final_end = end
1167+
1168+
for i, shape in enumerate(fill_shapes):
1169+
start = self.get_starting_point(previous_stitch_group)
1170+
if i < len(fill_shapes) - 1:
1171+
end = nearest_points(shape, fill_shapes[i+1])[0].coords
1172+
else:
1173+
end = final_end
1174+
stitch_group = StitchGroup(
1175+
color=self.color,
1176+
tags=("cross_stitch"),
1177+
stitches=cross_stitch(self, shape, start, end),
1178+
force_lock_stitches=self.force_lock_stitches,
1179+
lock_stitches=self.lock_stitches
1180+
)
1181+
previous_stitch_group = stitch_group
11591182
return [stitch_group]
11601183

11611184
def do_circular_fill(self, shape, starting_point, ending_point):

0 commit comments

Comments
 (0)