From 290582e58a295e4448963a919a8e61db3bc208f6 Mon Sep 17 00:00:00 2001 From: Walter Bender Date: Sun, 28 Feb 2010 03:18:06 +0000 Subject: save as SVG --- diff --git a/taconstants.py b/taconstants.py index 0e283bf..cf7cec0 100644 --- a/taconstants.py +++ b/taconstants.py @@ -134,7 +134,7 @@ PALETTES = [['clean', 'forward', 'back', 'show', 'left', 'right', ['kbinput', 'push', 'printheap', 'keyboard', 'pop', 'clearheap', 'myfunc1arg', 'userdefined', 'addturtle', 'comment', 'print', 'cartesian', 'width', 'height', 'polar', 'sandwichtop', - 'sandwichbottom'], + 'sandwichbottom', 'savesvg'], ['journal', 'audio', 'description', 'savepix', 'hideblocks', 'showblocks', 'fullscreen', 'picturelist', 'picture1x1a', 'picture1x1', 'picture2x2', 'picture2x1', 'picture1x2'], diff --git a/talogo.py b/talogo.py index c1d1290..4e0bfa5 100644 --- a/talogo.py +++ b/talogo.py @@ -339,6 +339,7 @@ class LogoCode: 'rightx':[0, lambda self: self.tw.rightx], 'rpos':[0, lambda self: self.tw.canvas.width/(self.tw.coord_scale*2)], 'savepix':[1, lambda self, x: self.save_picture(x)], + 'savesvg':[1, lambda self, x: self.save_svg(x)], 'scale':[0, lambda self: self.scale], 'setcolor':[1, lambda self, x: self.tw.canvas.setcolor(x)], 'seth':[1, lambda self, x: self.tw.canvas.seth(x)], @@ -445,6 +446,7 @@ class LogoCode: self.stacks[k] = None self.stacks['stack1'] = None self.stacks['stack2'] = None + self.tw.saving_svg = False for b in blocks: b.unhighlight() @@ -480,6 +482,8 @@ class LogoCode: dock = blk.docks[0] if len(dock)>4: # There could be a '(', ')', '[' or ']'. code.append(dock[4]) + if blk.name == 'savesvg': + self.tw.saving_svg = True if blk.primitive is not None: # make a tuple (prim, blk) code.append((blk.primitive, self.tw.block_list.list.index(blk))) elif len(blk.values)>0: # Extract the value from content blocks. @@ -1002,6 +1006,10 @@ class LogoCode: def save_picture(self, name): self.tw.save_as_image(name) + def save_svg(self, name): + self.tw.canvas.svg_close() + self.tw.save_as_image(name, True) + def show_list(self, sarray): x = self.tw.canvas.xcor/self.tw.coord_scale y = self.tw.canvas.ycor/self.tw.coord_scale diff --git a/tasprite_factory.py b/tasprite_factory.py index e7a0e44..be2b676 100755 --- a/tasprite_factory.py +++ b/tasprite_factory.py @@ -30,10 +30,10 @@ class SVG: def __init__(self): self._x = 0 self._y = 0 - self._min_x = 0 - self._min_y = 0 - self._max_x = 0 - self._max_y = 0 + self._min_x = 10000 + self._min_y = 10000 + self._max_x = -10000 + self._max_y = -10000 self._width = 0 self._height = 0 self.docks = [] @@ -545,6 +545,12 @@ class SVG: def set_no_arm(self, flag=True): self._no_arm = flag + def reset_min_max(self): + self._min_x = 10000 + self._min_y = 10000 + self._max_x = -10000 + self._max_y = -10000 + # # Exotic methods # @@ -574,7 +580,7 @@ class SVG: # SVG helper methods # - def _header(self): + def _header(self, center=False): return "%s%s%s%s%s%s%s%s%.1f%s%s%.1f%s%s%s" % ( "\n", "\n", @@ -586,7 +592,7 @@ class SVG: " width=\"", self._width, "\"\n", " height=\"", self._height, "\">\n", self._defs(), - self._transform()) + self._transform(center)) def _defs(self): if self._gradiant is True: @@ -608,14 +614,18 @@ class SVG: else: return "" - def _transform(self): + def _transform(self, center): if self._orientation != 0: orientation = "\n" % ( self._orientation, self._width/2.0, self._height/2.0) else: orientation = "" - return "\n%s" % ( - self._scale, self._scale, orientation ) + if center: + return "\n" % ( + -self._min_x, -self._min_y) + else: + return "\n%s" % ( + self._scale, self._scale, orientation ) def _footer(self): if self._orientation != 0: @@ -735,10 +745,12 @@ class SVG: return svg_str def new_path(self, x, y): + """ self._min_x = x self._min_y = y self._max_x = x self._max_y = y + """ self._x = x self._y = y return "