2020
2121import turtlethread
2222from turtlethread import fills
23+ from turtlethread import stitches
2324from bs4 import BeautifulSoup
2425
2526from 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
417432def 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
427441def 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 ()
0 commit comments