Skip to content

Commit f6a3a8b

Browse files
committed
fix docstring
attempted to add text satin functionality as well, but it was very slow
1 parent cb40716 commit f6a3a8b

File tree

3 files changed

+51
-21
lines changed

3 files changed

+51
-21
lines changed

src/turtlethread/draw_svg.py

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import turtlethread
2222
from turtlethread import fills
23+
from turtlethread import stitches
2324
from bs4 import BeautifulSoup
2425

2526
from concurrent.futures import ThreadPoolExecutor # to speed up computation
@@ -334,7 +335,22 @@ def move_turtle_to(te:turtlethread.Turtle, x, y):
334335
te.goto(pex, pey)
335336

336337

337-
# then travel the remaining distance
338+
# then travel the remaining distance -- split into steps so we can always use direct stitch
339+
dx = x-pex
340+
dy = y-pey
341+
m = math.sqrt(dx*dx + dy*dy)
342+
i = 1
343+
while (i+1)*min_turtle_dist < m:
344+
i += 1
345+
fdx = dx/i
346+
fdy = dy/i
347+
for _ in range(i-1):
348+
if save_to_debug:
349+
debug.append((pex+fdx*i, pey+fdy*i))
350+
if flip_y:
351+
te.goto(pex+fdx*i, -(pey+fdy*i))
352+
else:
353+
te.goto(pex+fdx*i, pey+fdy*i)
338354
if save_to_debug:
339355
debug.append((x, y))
340356
if flip_y:
@@ -409,25 +425,22 @@ def line(te, startx, starty, x1, y1, x2, y2): # 连接svg坐标下两点
409425
with te.jump_stitch():
410426
move_turtle_to(te, startx + x1, starty - y1)
411427
#te.pendown()
412-
with te.running_stitch(30):
413-
move_turtle_to(te, startx + x2, starty - y2)
428+
move_turtle_to(te, startx + x2, starty - y2)
414429
#te.penup()
415430

416431

417432
def Lineto_r(te, dx, dy): # 连接当前点和相对坐标(dx,dy)的点
418433
#te.pendown()
419-
with te.running_stitch(30):
420-
if flip_y:
421-
move_turtle_to(te, texcor() + dx, -teycor() - dy)
422-
else:
423-
move_turtle_to(te, texcor() + dx, teycor() - dy)
434+
if flip_y:
435+
move_turtle_to(te, texcor() + dx, -teycor() - dy)
436+
else:
437+
move_turtle_to(te, texcor() + dx, teycor() - dy)
424438
#te.penup()
425439

426440

427441
def Lineto(te, startx, starty, x, y): # 连接当前点和svg坐标下(x,y)
428442
#te.pendown()
429-
with te.running_stitch(30):
430-
move_turtle_to(te, startx + x, starty - y)
443+
move_turtle_to(te, startx + x, starty - y)
431444
#te.penup()
432445

433446

@@ -483,6 +496,8 @@ def Quadto_r(te, startx, starty, x1, y1, x, y): # 三阶贝塞尔曲线到相
483496
currpos = teposition()
484497
Bezier_2(te, X_now, Y_now, currpos[0] + x1, currpos[1] + y1,
485498
currpos[0] + x, currpos[1] + y)
499+
'''Bezier_2(te, X_now, Y_now, X_now + x1, Y_now + y1,
500+
X_now + x, Y_now + y)'''
486501

487502

488503

@@ -543,7 +558,7 @@ def reflect_point(cx, cy, px, py):
543558
return 2*cx - px, 2*cy - py
544559

545560

546-
def drawSVG(te:turtlethread.Turtle, filename, height, width=None, w_color=None, thickness=1, fill=True, outline=False, fill_min_y_dist:int=10, fill_min_x_dist=10, full_fill=True, flip_y_in:bool=False): # TODO consider colour
561+
def drawSVG(te:turtlethread.Turtle, filename, height, width=None, w_color=None, thickness=1, fill=True, outline=False, fill_min_y_dist:int=10, fill_min_x_dist=10, full_fill=True, outline_satin_thickness=None, flip_y_in:bool=False): # TODO consider colour
547562
"""Function to draw an SVG file with a turtle.
548563
549564
Parameters
@@ -562,6 +577,8 @@ def drawSVG(te:turtlethread.Turtle, filename, height, width=None, w_color=None,
562577
If True, the SVG will be outlined. Default is False.
563578
full_fill : bool, optional
564579
If True, the SVG will be fully filled if set to fill, otherwise it will be partialy filled. Default is True.
580+
outline_satin_thickness : int, optional, can be None
581+
If not None, the SVG's lines will use satin stitch rather than direct stitch
565582
fill_min_y_dist : int, optional
566583
The minimum distance between fill points in the y direction. Default is 10 (1mm).
567584
fill_min_x_dist : int, optional
@@ -792,7 +809,7 @@ def setfill(curr, val:bool):
792809
#with te.jump_stitch():
793810
# #print("WITH JUMP STITCH:", te._stitch_group_stack[-1] )
794811
# move_turtle_to(te, startx+p1[0], starty+p1[1])
795-
with te.direct_stitch():
812+
with te.fast_direct_stitch():
796813
#print("WITH RUNNING STITCH:", te._stitch_group_stack[-1] )
797814
move_turtle_to(te, startx+p2[0], starty+p2[1])
798815

@@ -808,7 +825,11 @@ def setfill(curr, val:bool):
808825
if outline:
809826
debug = []
810827
te.begin_fill(closed=False)
811-
with te.direct_stitch(): # 99999 will make sure we won't have gaps
828+
if outline_satin_thickness is None:
829+
stitch_grp = turtlethread.stitches.DirectStitch(te.pos(), te.curr_color)
830+
else:
831+
stitch_grp = turtlethread.stitches.SatinStitch(te.pos(), te.curr_color, outline_satin_thickness, center=True)
832+
with te.use_stitch_group(stitch_grp): # 99999 will make sure we won't have gaps
812833
#te.color(w_color) # TODO SWITCH COLOUR OF TEXT
813834

814835
def get_position():
@@ -833,6 +854,7 @@ def set_firstpos():
833854
attr = i.attrs['d'].replace('\n', ' ')
834855
f = readPathAttrD(attr)
835856
lastI = ''
857+
firstpos = None
836858
for i in f:
837859
#print(i)
838860
# if i.lower() in ['c', 'q', 'l', 'h' 'v', 'z']:
@@ -842,10 +864,12 @@ def set_firstpos():
842864
#te.end_fill()
843865
Moveto(te, startx, starty, next(f) * scale[0], next(f) * scale[1])
844866
#te.begin_fill()
867+
#if firstpos is None:
845868
set_firstpos()
846869
elif i == 'm':
847870
#te.end_fill()
848871
Moveto_r(te, next(f) * scale[0], next(f) * scale[1])
872+
#if firstpos is None:
849873
set_firstpos()
850874
#te.begin_fill()
851875
elif i == 'C':
@@ -871,7 +895,7 @@ def set_firstpos():
871895
next(f) * scale[0], next(f) * scale[1],
872896
next(f) * scale[0], next(f) * scale[1])
873897
lastI = i
874-
elif i=='s':
898+
elif i=='s':
875899
if lastI.lower() == 'c':
876900
currpos = list(teposition())
877901
#print(*currpos, prev_ctrl)
@@ -918,7 +942,7 @@ def set_firstpos():
918942
ctrl_point = [ctrl_point[0]-currpos[0], currpos[1]-ctrl_point[1]]
919943
#print("REF")
920944
else:
921-
ctrl_point = list(teposition())
945+
ctrl_point = list(teposition()) #[0,0]
922946
Quadto_r(te, startx, starty, *ctrl_point,
923947
next(f) * scale[0], next(f) * scale[1],)
924948
lastI = i
@@ -1082,7 +1106,7 @@ def _fake_drawSVG(te:turtlethread.Turtle, filename, height, w_color=None, thickn
10821106
starty += round(Height) # just to fix the calculations below, since the origin is somewhere else
10831107
if outline:
10841108
debug = []
1085-
with te.running_stitch(30): # 99999 will make sure we won't have gaps
1109+
with te.direct_stitch(): # 99999 will make sure we won't have gaps
10861110

10871111
def get_position():
10881112
posx, posy = teposition()

src/turtlethread/text.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ def get_loaded_fontnames(self):
142142
return list(self.loaded_fonts.keys())
143143

144144

145-
def draw_one_letter(self, fontname, lettername, fontsize=20, colour='#000000', thickness=1, fill=False, outline=True, fill_min_y_dist:int=10, fill_min_x_dist=10, full_fill=True, turtle=None, flip_y=False):
145+
def draw_one_letter(self, fontname, lettername, fontsize=20, colour='#000000', thickness=1, fill=False, outline=True, fill_min_y_dist:int=10, fill_min_x_dist=10, full_fill=True, outline_satin_thickness=None, turtle=None, flip_y=False):
146146
"""This function draws a single letter.
147147
148148
Parameters
@@ -160,6 +160,8 @@ def draw_one_letter(self, fontname, lettername, fontsize=20, colour='#000000', t
160160
Note that ``full_fill`` can have bugs at small font sizes (120 is considered small), while ``partial_fill`` might cross over the boundaries of the text.
161161
outline : bool (optional, default=True)
162162
Specify whether the text should be outlined (it is recommended to outline when not filling or using ```partial_fill```, but not ```full_fill```).
163+
outline_satin_thickness : int, optional, can be None
164+
If not None, the SVG's lines will use satin stitch rather than direct stitch
163165
flip_y : bool (optional, default=False)
164166
Allow you to vertically flip the text if desired. Defaults to ``False``.
165167
fill_min_x_dist : int (optional, default=10)
@@ -190,7 +192,7 @@ def draw_one_letter(self, fontname, lettername, fontsize=20, colour='#000000', t
190192
# DRAW ONE LETTER OF A FONT WITH A LOADED NAME, GIVEN A COLOUR
191193
if fontname in self.loaded_fonts.keys():
192194
try:
193-
drawSVG(turtle, self.loaded_fonts[fontname][lettername], fontsize, fontsize, colour, thickness, fill, outline, fill_min_y_dist, fill_min_x_dist, full_fill, flip_y)
195+
drawSVG(turtle, self.loaded_fonts[fontname][lettername], fontsize, fontsize, colour, thickness, fill, outline, fill_min_y_dist, fill_min_x_dist, full_fill, outline_satin_thickness, flip_y)
194196
#print("DREW SVG")
195197
except Exception as e:
196198
print("OR, it might be some other error({})".format(e))
@@ -211,7 +213,7 @@ def draw_letter_gap(self, fontsize, letter_gap=None):
211213
self.turtle.goto(currpos[0] + letter_gap*fontsize, currpos[1])
212214
#print("DRAEW")
213215

214-
def draw_string(self, fontname, string, fontsize, colours='#000000', thicknesses = 1, fills=False, outlines=True, fill_min_y_dist=10, fill_min_x_dist=10, full_fill=True, letter_gaps=None, turtle=None, flip_y=False):
216+
def draw_string(self, fontname, string, fontsize, colours='#000000', thicknesses = 1, fills=False, outlines=True, fill_min_y_dist=10, fill_min_x_dist=10, full_fill=True, outline_satin_thickness=None, letter_gaps=None, turtle=None, flip_y=False):
215217
"""This function draws a string of letters.
216218
217219
Parameters
@@ -230,6 +232,8 @@ def draw_string(self, fontname, string, fontsize, colours='#000000', thicknesses
230232
outlines : bool/list[bool] (optional, default=True)
231233
Specify whether the text should be outlined (it is recommended to outline when not filling or using ```partial_fill```, but not ```full_fill```).
232234
Also accepts a list with one element per letter in the string.
235+
outline_satin_thickness : int, optional, can be None
236+
If not None, the SVG's lines will use satin stitch rather than direct stitch
233237
flip_y : bool (optional, default=False)
234238
Allow you to vertically flip the text if desired. Defaults to ``False``.
235239
fill_min_x_dist : int (optional, default=10)
@@ -299,7 +303,7 @@ def draw_string(self, fontname, string, fontsize, colours='#000000', thicknesses
299303
outline = outlines[cidx]
300304

301305
#print("DRAWING LETTER", string[cidx], "FILL", fill)
302-
self.draw_one_letter(fontname, LetterDrawer.char_to_name(string[cidx]), fontsize, col, thickness, fill, outline, fill_min_y_dist, fill_min_x_dist, full_fill, turtle, flip_y)
306+
self.draw_one_letter(fontname, LetterDrawer.char_to_name(string[cidx]), fontsize, col, thickness, fill, outline, fill_min_y_dist, fill_min_x_dist, full_fill, outline_satin_thickness, turtle, flip_y)
303307

304308
if isinstance(letter_gaps, list):
305309
letter_gap = letter_gaps[cidx]
@@ -336,7 +340,7 @@ def draw_string(self, fontname, string, fontsize, colours='#000000', thicknesses
336340
outline = outlines[cidx]
337341

338342
#print("DRAWING LETTER", string[-1], "FILL", fill)
339-
self.draw_one_letter(fontname, LetterDrawer.char_to_name(string[cidx]), fontsize, col, thickness, fill, outline, fill_min_y_dist, fill_min_x_dist, full_fill, turtle, flip_y)
343+
self.draw_one_letter(fontname, LetterDrawer.char_to_name(string[cidx]), fontsize, col, thickness, fill, outline, fill_min_y_dist, fill_min_x_dist, full_fill, outline_satin_thickness, turtle, flip_y)
340344

341345
if isinstance(letter_gaps, list) and len(letter_gaps) > cidx: # if we have another letter gap, include it.
342346
letter_gap = letter_gaps[cidx]

src/turtlethread/turtle.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,8 @@ def fast_visualise(self, turtle=None, width=800, height=800, scale=1, speed=6, e
548548
Factor the embroidery length's are scaled by.
549549
speed : int
550550
Speed that the turtle object moves at.
551+
extra_speed : int
552+
Extra speed boost on top of everything. Defaults to 1, which is no speed boost. 0 is infinite speed boost
551553
trace_jump : bool
552554
If True, then draw a grey line connecting the origin and destination of jumps.
553555
skip : bool

0 commit comments

Comments
 (0)