superturtle 1.3.0__tar.gz → 1.4.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {superturtle-1.3.0 → superturtle-1.4.0}/PKG-INFO +1 -1
- {superturtle-1.3.0 → superturtle-1.4.0}/pyproject.toml +1 -1
- superturtle-1.4.0/src/superturtle/__init__.py +1 -0
- {superturtle-1.3.0 → superturtle-1.4.0}/src/superturtle/image.py +27 -1
- {superturtle-1.3.0 → superturtle-1.4.0}/src/superturtle/svg.py +171 -14
- superturtle-1.3.0/src/superturtle/__init__.py +0 -1
- {superturtle-1.3.0 → superturtle-1.4.0}/README.md +0 -0
- {superturtle-1.3.0 → superturtle-1.4.0}/src/superturtle/animation.py +0 -0
- {superturtle-1.3.0 → superturtle-1.4.0}/src/superturtle/easing.py +0 -0
- {superturtle-1.3.0 → superturtle-1.4.0}/src/superturtle/fancy_drawing.py +0 -0
- {superturtle-1.3.0 → superturtle-1.4.0}/src/superturtle/movement.py +0 -0
- {superturtle-1.3.0 → superturtle-1.4.0}/src/superturtle/stroke.py +0 -0
- {superturtle-1.3.0 → superturtle-1.4.0}/src/superturtle/test.py +0 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = '1.4.0'
|
|
@@ -41,6 +41,24 @@ class save_svg:
|
|
|
41
41
|
`screensize(width, height)` function to set the canvas size. It helps to add a
|
|
42
42
|
couple of extra pixels to the window size to avoid getting scroll bars.
|
|
43
43
|
|
|
44
|
+
**SVG fidelity**
|
|
45
|
+
|
|
46
|
+
``turtle.circle()`` is normally approximated as a polygon. Inside
|
|
47
|
+
``save_svg``, circles and arcs (when ``steps`` is not given) are
|
|
48
|
+
exported as true SVG ``<circle>`` or ``<path>`` arc elements. Passing
|
|
49
|
+
an explicit ``steps`` argument preserves the polygon-approximation
|
|
50
|
+
behaviour, which is useful for drawing regular polygons.
|
|
51
|
+
|
|
52
|
+
Fill regions (``turtle.fill()``, or ``begin_fill()``/``end_fill()``)
|
|
53
|
+
are exported as closed SVG ``<polygon>`` elements whose ``fill``
|
|
54
|
+
attribute carries the turtle fill color and whose ``stroke`` attribute
|
|
55
|
+
carries the pen color. This means the closed boundary is visible to a
|
|
56
|
+
pen plotter as a single continuous path, and downstream tools such as
|
|
57
|
+
Inkscape or vpype can apply hatch fills or other processing to the
|
|
58
|
+
colored region. Note that the line strokes drawn *inside* a fill
|
|
59
|
+
region are merged into the polygon element and are not emitted as
|
|
60
|
+
separate ``<polyline>`` elements.
|
|
61
|
+
|
|
44
62
|
Arguments:
|
|
45
63
|
width (int): Width of resulting SVG file in pixels.
|
|
46
64
|
height (int): Height of resulting SVG file in pixels.
|
|
@@ -52,7 +70,7 @@ class save_svg:
|
|
|
52
70
|
|
|
53
71
|
::
|
|
54
72
|
|
|
55
|
-
from turtle import circle, setup, screensize
|
|
73
|
+
from turtle import circle, forward, left, setup, screensize
|
|
56
74
|
from superturtle.image import save_svg
|
|
57
75
|
|
|
58
76
|
width, height = 816, 1056
|
|
@@ -60,7 +78,15 @@ class save_svg:
|
|
|
60
78
|
screensize(width, height)
|
|
61
79
|
|
|
62
80
|
with save_svg(width, height, "image.svg"):
|
|
81
|
+
# Exported as a true SVG <circle>, not a polygon approximation.
|
|
63
82
|
circle(100)
|
|
83
|
+
|
|
84
|
+
# Filled square: exported as a closed <polygon> with fill and stroke.
|
|
85
|
+
color("black", "red")
|
|
86
|
+
with fill():
|
|
87
|
+
for _ in range(4):
|
|
88
|
+
forward(80)
|
|
89
|
+
left(90)
|
|
64
90
|
"""
|
|
65
91
|
def __init__(self, width, height, filename, dryrun=False, group_colors=True):
|
|
66
92
|
self.dryrun = dryrun
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
+
import math
|
|
1
2
|
import re
|
|
2
3
|
from collections import defaultdict
|
|
3
|
-
from dataclasses import dataclass
|
|
4
|
+
from dataclasses import dataclass
|
|
4
5
|
|
|
5
6
|
try:
|
|
6
7
|
import tkinter as tk
|
|
@@ -201,6 +202,9 @@ class SVGTurtle(RawTurtle):
|
|
|
201
202
|
screen = SVGScreen(canvas)
|
|
202
203
|
super().__init__(screen)
|
|
203
204
|
self.speed(0)
|
|
205
|
+
self._svg_fill_items_start = None
|
|
206
|
+
self._svg_fill_prev_line_item = None
|
|
207
|
+
self._svg_fill_line_same = False
|
|
204
208
|
|
|
205
209
|
def _drawturtle(self):
|
|
206
210
|
pass
|
|
@@ -213,12 +217,151 @@ class SVGTurtle(RawTurtle):
|
|
|
213
217
|
p['speed'] = 0
|
|
214
218
|
return super().pen(p)
|
|
215
219
|
|
|
220
|
+
def circle(self, radius, extent=None, steps=None):
|
|
221
|
+
"""Draw a circle or arc, emitting a true SVG circle/arc when steps is None.
|
|
222
|
+
|
|
223
|
+
When steps is explicitly given, the polygon-approximation behavior of the
|
|
224
|
+
standard turtle is preserved (useful for drawing regular polygons).
|
|
225
|
+
"""
|
|
226
|
+
if steps is None:
|
|
227
|
+
was_filling = self.filling()
|
|
228
|
+
was_drawing = self._drawing
|
|
229
|
+
pos_before = self.pos() # turtle coords (x, y)
|
|
230
|
+
heading_before = self.heading()
|
|
231
|
+
pen_color = self._pencolor
|
|
232
|
+
pen_size = self._pensize
|
|
233
|
+
|
|
234
|
+
# Lift the pen so super().circle() advances position and _fillpath
|
|
235
|
+
# without writing polygon-approximation coords into any line item.
|
|
236
|
+
if was_drawing:
|
|
237
|
+
self.penup()
|
|
238
|
+
|
|
239
|
+
super().circle(radius, extent, steps)
|
|
240
|
+
|
|
241
|
+
if was_drawing:
|
|
242
|
+
self.pendown()
|
|
243
|
+
|
|
244
|
+
# When inside a fill region the polygon's outline already traces the
|
|
245
|
+
# boundary, so we omit the separate arc stroke item.
|
|
246
|
+
if was_drawing and not was_filling:
|
|
247
|
+
k = self._degreesPerAU # angular-unit → degrees conversion
|
|
248
|
+
canvas = self.getscreen().cv
|
|
249
|
+
canvas._items.append(_CanvasItem(
|
|
250
|
+
method='arc',
|
|
251
|
+
coords=(
|
|
252
|
+
float(pos_before[0]),
|
|
253
|
+
float(pos_before[1]),
|
|
254
|
+
heading_before * k, # degrees
|
|
255
|
+
radius,
|
|
256
|
+
(extent if extent is not None else self._fullcircle) * k, # degrees
|
|
257
|
+
),
|
|
258
|
+
attribs={'fill': pen_color, 'width': pen_size},
|
|
259
|
+
z_order=len(canvas._items),
|
|
260
|
+
))
|
|
261
|
+
else:
|
|
262
|
+
super().circle(radius, extent, steps)
|
|
263
|
+
|
|
264
|
+
def begin_fill(self):
|
|
265
|
+
"""Begin a fill region; records the canvas state so end_fill can clean up."""
|
|
266
|
+
canvas = self.getscreen().cv
|
|
267
|
+
self._svg_fill_items_start = len(canvas._items)
|
|
268
|
+
# Save the current line item BEFORE begin_fill so we can detect whether
|
|
269
|
+
# _newLine (called inside begin_fill) reused it or replaced it.
|
|
270
|
+
# _newLine only creates a new item when len(currentLine) > 1; if the
|
|
271
|
+
# turtle hasn't drawn anything yet it reuses the existing item and that
|
|
272
|
+
# item will silently accumulate fill-region coordinates.
|
|
273
|
+
self._svg_fill_prev_line_item = self.currentLineItem
|
|
274
|
+
super().begin_fill()
|
|
275
|
+
# True when _newLine kept the same item (no pre-fill drawing existed).
|
|
276
|
+
self._svg_fill_line_same = (self.currentLineItem == self._svg_fill_prev_line_item)
|
|
277
|
+
|
|
278
|
+
def end_fill(self):
|
|
279
|
+
"""Close the fill region.
|
|
280
|
+
|
|
281
|
+
Suppresses the line items drawn inside the fill (their edges are already
|
|
282
|
+
captured by the fill polygon) and adds the current pen color as the
|
|
283
|
+
polygon's outline stroke so downstream tools can distinguish the boundary.
|
|
284
|
+
"""
|
|
285
|
+
fill_item_id = self._fillitem
|
|
286
|
+
pen_color = self._pencolor
|
|
287
|
+
pen_size = self._pensize
|
|
288
|
+
fill_items_start = self._svg_fill_items_start if self._svg_fill_items_start is not None else 0
|
|
289
|
+
prev_line_item = getattr(self, '_svg_fill_prev_line_item', None)
|
|
290
|
+
line_same = getattr(self, '_svg_fill_line_same', False)
|
|
291
|
+
|
|
292
|
+
super().end_fill()
|
|
293
|
+
|
|
294
|
+
canvas = self.getscreen().cv
|
|
295
|
+
# Suppress line items created during the fill region. The fill polygon
|
|
296
|
+
# already captures the boundary, so separate stroke items are redundant.
|
|
297
|
+
for i in range(fill_items_start, len(canvas._items)):
|
|
298
|
+
if canvas._items[i].method == 'line':
|
|
299
|
+
canvas._items[i].deleted = True
|
|
300
|
+
|
|
301
|
+
# When _newLine didn't create a fresh item at begin_fill time, the
|
|
302
|
+
# original currentLineItem was reused for fill content — delete it too.
|
|
303
|
+
if line_same and prev_line_item is not None:
|
|
304
|
+
canvas._items[prev_line_item].deleted = True
|
|
305
|
+
|
|
306
|
+
# Give the fill polygon a visible outline using the current pen.
|
|
307
|
+
if fill_item_id is not None:
|
|
308
|
+
canvas.itemconfigure(fill_item_id, outline=pen_color, width=pen_size)
|
|
309
|
+
|
|
310
|
+
self._svg_fill_items_start = None
|
|
311
|
+
self._svg_fill_prev_line_item = None
|
|
312
|
+
self._svg_fill_line_same = False
|
|
313
|
+
|
|
216
314
|
def save_as(self, filename: str, group_colors: bool = True):
|
|
217
315
|
canvas = self.getscreen().cv
|
|
218
316
|
_write_svg(canvas, canvas.winfo_width(), canvas.winfo_height(),
|
|
219
317
|
filename, group_colors=group_colors)
|
|
220
318
|
|
|
221
319
|
|
|
320
|
+
def _arc_svg_parts(coords, xoff: float, yoff: float) -> tuple[str, str]:
|
|
321
|
+
"""Return (svg_tag, geometry_attrs) for an arc canvas item.
|
|
322
|
+
|
|
323
|
+
coords is (start_x, start_y, heading_deg, radius, extent_deg) in turtle
|
|
324
|
+
coordinate space (y-up, origin at canvas centre). xoff/yoff shift to SVG
|
|
325
|
+
viewport coordinates (y-down, origin at top-left).
|
|
326
|
+
"""
|
|
327
|
+
start_x, start_y, heading_deg, radius, extent_deg = coords
|
|
328
|
+
r = abs(radius)
|
|
329
|
+
h_rad = math.radians(heading_deg)
|
|
330
|
+
|
|
331
|
+
# Centre of the circle in turtle coords.
|
|
332
|
+
# The centre is `radius` units to the LEFT of the turtle's heading:
|
|
333
|
+
# left direction = (-sin h, cos h) → centre = pos + radius * (-sin h, cos h)
|
|
334
|
+
cx = start_x - radius * math.sin(h_rad)
|
|
335
|
+
cy = start_y + radius * math.cos(h_rad)
|
|
336
|
+
|
|
337
|
+
if abs(extent_deg) >= 360:
|
|
338
|
+
# Full circle: SVG <circle> is exact and avoids the degenerate-arc problem.
|
|
339
|
+
cx_svg = cx + xoff
|
|
340
|
+
cy_svg = -cy + yoff
|
|
341
|
+
return 'circle', f'cx="{cx_svg:.3f}" cy="{cy_svg:.3f}" r="{r:.3f}"'
|
|
342
|
+
|
|
343
|
+
# Partial arc: SVG <path> with elliptical-arc command.
|
|
344
|
+
sx = start_x + xoff
|
|
345
|
+
sy = -start_y + yoff
|
|
346
|
+
|
|
347
|
+
# Angle from centre to start point, then sweep in the correct direction.
|
|
348
|
+
theta_start = math.atan2(start_y - cy, start_x - cx)
|
|
349
|
+
arc_dir = 1 if radius > 0 else -1 # positive radius → CCW in turtle (y-up)
|
|
350
|
+
theta_end = theta_start + math.radians(extent_deg) * arc_dir
|
|
351
|
+
|
|
352
|
+
ex_svg = (cx + r * math.cos(theta_end)) + xoff
|
|
353
|
+
ey_svg = -(cy + r * math.sin(theta_end)) + yoff
|
|
354
|
+
|
|
355
|
+
large_arc = 1 if abs(extent_deg) > 180 else 0
|
|
356
|
+
# CCW in turtle (y-up) → CW in SVG (y-down) → sweep-flag = 1
|
|
357
|
+
sweep = 1 if radius * extent_deg > 0 else 0
|
|
358
|
+
|
|
359
|
+
d = (f'M {sx:.3f},{sy:.3f}'
|
|
360
|
+
f' A {r:.3f},{r:.3f} 0 {large_arc} {sweep}'
|
|
361
|
+
f' {ex_svg:.3f},{ey_svg:.3f}')
|
|
362
|
+
return 'path', f'd="{d}"'
|
|
363
|
+
|
|
364
|
+
|
|
222
365
|
def _write_svg(canvas: SVGCanvas, width: int, height: int, filename: str,
|
|
223
366
|
group_colors: bool = True):
|
|
224
367
|
"""Write canvas contents to an SVG file.
|
|
@@ -238,12 +381,12 @@ def _write_svg(canvas: SVGCanvas, width: int, height: int, filename: str,
|
|
|
238
381
|
pairs.append(f'{x + xoff:.3f},{y + yoff:.3f}')
|
|
239
382
|
return ' '.join(pairs)
|
|
240
383
|
|
|
241
|
-
#
|
|
242
|
-
#
|
|
384
|
+
# Emit visible, non-deleted items: lines and arcs (stroke items) plus
|
|
385
|
+
# polygons whose fill attribute has been set (fill regions).
|
|
243
386
|
drawable = [
|
|
244
387
|
item for item in sorted(canvas._items, key=lambda i: i.z_order)
|
|
245
388
|
if not item.deleted
|
|
246
|
-
and item.method in ('line', 'polygon')
|
|
389
|
+
and item.method in ('line', 'polygon', 'arc')
|
|
247
390
|
and item.attribs.get('fill', '') != ''
|
|
248
391
|
]
|
|
249
392
|
|
|
@@ -260,26 +403,30 @@ def _write_svg(canvas: SVGCanvas, width: int, height: int, filename: str,
|
|
|
260
403
|
]
|
|
261
404
|
|
|
262
405
|
if group_colors:
|
|
263
|
-
|
|
406
|
+
strokes_by_color: dict[str, list[_CanvasItem]] = defaultdict(list)
|
|
264
407
|
fill_items: list[_CanvasItem] = []
|
|
265
408
|
|
|
266
409
|
for item in drawable:
|
|
267
|
-
if item.method
|
|
268
|
-
|
|
410
|
+
if item.method in ('line', 'arc'):
|
|
411
|
+
strokes_by_color[item.attribs.get('fill', 'black')].append(item)
|
|
269
412
|
else:
|
|
270
413
|
fill_items.append(item)
|
|
271
414
|
|
|
272
|
-
for color, group in
|
|
415
|
+
for color, group in strokes_by_color.items():
|
|
273
416
|
out.append(
|
|
274
417
|
f' <g stroke="{color}" fill="none"'
|
|
275
418
|
f' stroke-linecap="round" {clip}>'
|
|
276
419
|
)
|
|
277
420
|
for item in group:
|
|
278
421
|
sw = item.attribs.get('width', 1)
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
f' stroke-width="{sw}"/>'
|
|
282
|
-
|
|
422
|
+
if item.method == 'arc':
|
|
423
|
+
tag, geom = _arc_svg_parts(item.coords, xoff, yoff)
|
|
424
|
+
out.append(f' <{tag} {geom} stroke-width="{sw}"/>')
|
|
425
|
+
else:
|
|
426
|
+
out.append(
|
|
427
|
+
f' <polyline points="{point_str(item.coords)}"'
|
|
428
|
+
f' stroke-width="{sw}"/>'
|
|
429
|
+
)
|
|
283
430
|
out.append(' </g>')
|
|
284
431
|
|
|
285
432
|
for item in fill_items:
|
|
@@ -293,8 +440,17 @@ def _write_svg(canvas: SVGCanvas, width: int, height: int, filename: str,
|
|
|
293
440
|
)
|
|
294
441
|
else:
|
|
295
442
|
for item in drawable:
|
|
296
|
-
|
|
297
|
-
|
|
443
|
+
if item.method == 'arc':
|
|
444
|
+
color = item.attribs.get('fill', 'black')
|
|
445
|
+
sw = item.attribs.get('width', 1)
|
|
446
|
+
tag, geom = _arc_svg_parts(item.coords, xoff, yoff)
|
|
447
|
+
out.append(
|
|
448
|
+
f' <{tag} {geom} fill="none"'
|
|
449
|
+
f' stroke="{color}" stroke-width="{sw}"'
|
|
450
|
+
f' stroke-linecap="round" {clip}/>'
|
|
451
|
+
)
|
|
452
|
+
elif item.method == 'line':
|
|
453
|
+
pts = point_str(item.coords)
|
|
298
454
|
color = item.attribs.get('fill', 'black')
|
|
299
455
|
sw = item.attribs.get('width', 1)
|
|
300
456
|
out.append(
|
|
@@ -303,6 +459,7 @@ def _write_svg(canvas: SVGCanvas, width: int, height: int, filename: str,
|
|
|
303
459
|
f' fill="none" stroke-linecap="round" {clip}/>'
|
|
304
460
|
)
|
|
305
461
|
else:
|
|
462
|
+
pts = point_str(item.coords)
|
|
306
463
|
fill = item.attribs.get('fill', 'none')
|
|
307
464
|
stroke = item.attribs.get('outline', '') or 'none'
|
|
308
465
|
sw = item.attribs.get('width', 0)
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
__version__ = '1.3.0'
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|