20
20
21
21
import turtlethread
22
22
from turtlethread import fills
23
+ from turtlethread import stitches
23
24
from bs4 import BeautifulSoup
24
25
25
26
from concurrent .futures import ThreadPoolExecutor # to speed up computation
@@ -334,7 +335,22 @@ def move_turtle_to(te:turtlethread.Turtle, x, y):
334
335
te .goto (pex , pey )
335
336
336
337
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 )
338
354
if save_to_debug :
339
355
debug .append ((x , y ))
340
356
if flip_y :
@@ -409,25 +425,22 @@ def line(te, startx, starty, x1, y1, x2, y2): # 连接svg坐标下两点
409
425
with te .jump_stitch ():
410
426
move_turtle_to (te , startx + x1 , starty - y1 )
411
427
#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 )
414
429
#te.penup()
415
430
416
431
417
432
def Lineto_r (te , dx , dy ): # 连接当前点和相对坐标(dx,dy)的点
418
433
#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 )
424
438
#te.penup()
425
439
426
440
427
441
def Lineto (te , startx , starty , x , y ): # 连接当前点和svg坐标下(x,y)
428
442
#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 )
431
444
#te.penup()
432
445
433
446
@@ -483,6 +496,8 @@ def Quadto_r(te, startx, starty, x1, y1, x, y): # 三阶贝塞尔曲线到相
483
496
currpos = teposition ()
484
497
Bezier_2 (te , X_now , Y_now , currpos [0 ] + x1 , currpos [1 ] + y1 ,
485
498
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)'''
486
501
487
502
488
503
@@ -543,7 +558,7 @@ def reflect_point(cx, cy, px, py):
543
558
return 2 * cx - px , 2 * cy - py
544
559
545
560
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
547
562
"""Function to draw an SVG file with a turtle.
548
563
549
564
Parameters
@@ -562,6 +577,8 @@ def drawSVG(te:turtlethread.Turtle, filename, height, width=None, w_color=None,
562
577
If True, the SVG will be outlined. Default is False.
563
578
full_fill : bool, optional
564
579
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
565
582
fill_min_y_dist : int, optional
566
583
The minimum distance between fill points in the y direction. Default is 10 (1mm).
567
584
fill_min_x_dist : int, optional
@@ -792,7 +809,7 @@ def setfill(curr, val:bool):
792
809
#with te.jump_stitch():
793
810
# #print("WITH JUMP STITCH:", te._stitch_group_stack[-1] )
794
811
# move_turtle_to(te, startx+p1[0], starty+p1[1])
795
- with te .direct_stitch ():
812
+ with te .fast_direct_stitch ():
796
813
#print("WITH RUNNING STITCH:", te._stitch_group_stack[-1] )
797
814
move_turtle_to (te , startx + p2 [0 ], starty + p2 [1 ])
798
815
@@ -808,7 +825,11 @@ def setfill(curr, val:bool):
808
825
if outline :
809
826
debug = []
810
827
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
812
833
#te.color(w_color) # TODO SWITCH COLOUR OF TEXT
813
834
814
835
def get_position ():
@@ -833,6 +854,7 @@ def set_firstpos():
833
854
attr = i .attrs ['d' ].replace ('\n ' , ' ' )
834
855
f = readPathAttrD (attr )
835
856
lastI = ''
857
+ firstpos = None
836
858
for i in f :
837
859
#print(i)
838
860
# if i.lower() in ['c', 'q', 'l', 'h' 'v', 'z']:
@@ -842,10 +864,12 @@ def set_firstpos():
842
864
#te.end_fill()
843
865
Moveto (te , startx , starty , next (f ) * scale [0 ], next (f ) * scale [1 ])
844
866
#te.begin_fill()
867
+ #if firstpos is None:
845
868
set_firstpos ()
846
869
elif i == 'm' :
847
870
#te.end_fill()
848
871
Moveto_r (te , next (f ) * scale [0 ], next (f ) * scale [1 ])
872
+ #if firstpos is None:
849
873
set_firstpos ()
850
874
#te.begin_fill()
851
875
elif i == 'C' :
@@ -871,7 +895,7 @@ def set_firstpos():
871
895
next (f ) * scale [0 ], next (f ) * scale [1 ],
872
896
next (f ) * scale [0 ], next (f ) * scale [1 ])
873
897
lastI = i
874
- elif i == 's' :
898
+ elif i == 's' :
875
899
if lastI .lower () == 'c' :
876
900
currpos = list (teposition ())
877
901
#print(*currpos, prev_ctrl)
@@ -918,7 +942,7 @@ def set_firstpos():
918
942
ctrl_point = [ctrl_point [0 ]- currpos [0 ], currpos [1 ]- ctrl_point [1 ]]
919
943
#print("REF")
920
944
else :
921
- ctrl_point = list (teposition ())
945
+ ctrl_point = list (teposition ()) #[0,0]
922
946
Quadto_r (te , startx , starty , * ctrl_point ,
923
947
next (f ) * scale [0 ], next (f ) * scale [1 ],)
924
948
lastI = i
@@ -1082,7 +1106,7 @@ def _fake_drawSVG(te:turtlethread.Turtle, filename, height, w_color=None, thickn
1082
1106
starty += round (Height ) # just to fix the calculations below, since the origin is somewhere else
1083
1107
if outline :
1084
1108
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
1086
1110
1087
1111
def get_position ():
1088
1112
posx , posy = teposition ()
0 commit comments