From 26870f5b5c323368517154c8e2722f5c9d2ab221 Mon Sep 17 00:00:00 2001 From: Walter Bender Date: Sat, 23 Jan 2010 19:10:42 +0000 Subject: cleaned up svg rendering --- diff --git a/block.py b/block.py index 3133479..2dccaf4 100644 --- a/block.py +++ b/block.py @@ -114,8 +114,7 @@ class Block: svg.set_outie(False) svg.set_tab(True) svg.set_slot(True) - xoff = INNIE*scale-2 - yoff = SLOTY*scale-1 + if name in BASIC_STYLE: svg.expand(40,0) self._make_basic_block(sprite_list, svg, x, y) @@ -126,17 +125,17 @@ class Block: svg.set_slot(False) svg.set_cap(True) self._make_basic_block(sprite_list, svg, x, y) - self.docks = (('start', True, svg.docks[0][0], svg.docks[0][1]), - ('flow', False, svg.docks[1][0], svg.docks[1][1])) + self.docks = (('start', True, 0, 0), + ('flow', False, svg.docks[0][0], svg.docks[0][1])) elif name in BASIC_STYLE_HEAD_1ARG: svg.expand(40,0) svg.set_innie([True]) svg.set_slot(False) svg.set_cap(True) self._make_basic_block(sprite_list, svg, x, y) - self.docks = (('start', True, svg.docks[0][0], svg.docks[0][1]), - ('string', False, svg.docks[1][0], svg.docks[1][1]), - ('flow', False, svg.docks[2][0], svg.docks[2][1])) + self.docks = (('start', True, 0, 0), + ('string', False, svg.docks[0][0], svg.docks[0][1]), + ('flow', False, svg.docks[1][0], svg.docks[1][1])) elif name in BASIC_STYLE_TAIL: svg.expand(40,0) svg.set_tab(False) @@ -170,7 +169,7 @@ class Block: self.docks = (('number', True, svg.docks[0][0], svg.docks[0][1]), ('string', False, svg.docks[1][0], svg.docks[1][1])) elif name in NUMBER_STYLE: - svg.expand(25,0) + svg.expand(0,0) svg.set_innie([True,True]) svg.set_outie(True) svg.set_tab(False) @@ -185,7 +184,7 @@ class Block: ('number', False, svg.docks[0][0], svg.docks[0][1]), ('number', False, svg.docks[1][0], svg.docks[1][1])) elif name in NUMBER_STYLE_1ARG: - svg.expand(25,0) + svg.expand(0,0) svg.set_innie([True]) svg.set_outie(True) svg.set_tab(False) @@ -194,24 +193,24 @@ class Block: self.docks = (('number', True, svg.docks[1][0], svg.docks[1][1]), ('number', False, svg.docks[0][0], svg.docks[0][1])) elif name in NUMBER_STYLE_PORCH: - svg.expand(25,0) + svg.expand(0,0) svg.set_innie([True,True]) svg.set_outie(True) svg.set_tab(False) svg.set_slot(False) svg.set_porch(True) self._make_basic_block(sprite_list, svg, x, y) - xoff += svg._porch self.docks = (('number', True, svg.docks[2][0], svg.docks[2][1]), ('number', False, svg.docks[0][0], svg.docks[0][1]), ('number', False, svg.docks[1][0], svg.docks[1][1])) elif name in COMPARE_STYLE: + svg.expand(10,0) self._make_boolean_compare(sprite_list, svg, x, y) self.docks = (('bool', True, svg.docks[0][0], svg.docks[0][1]), ('number', False, svg.docks[1][0], svg.docks[1][1]), ('number', False, svg.docks[2][0], svg.docks[2][1])) elif name in BOOLEAN_STYLE: - svg.expand(15,0) + svg.expand(10,0) self._make_boolean_and_or(sprite_list, svg, x, y) self.docks = (('bool', True, svg.docks[0][0], svg.docks[0][1]), ('bool', False, svg.docks[1][0], svg.docks[1][1]), diff --git a/constants.py b/constants.py index 8f8b5ab..803d665 100644 --- a/constants.py +++ b/constants.py @@ -14,13 +14,6 @@ from gettext import gettext as _ # -# SVG constants -# -SLOTY = 2 -INNIE = 9 -SPACER = 9 - -# # sprite layers # @@ -51,6 +44,7 @@ BASIC_STYLE_2ARG = ['arc', 'set xy', 'fill screen', 'store in'] BOX_STYLE = ['number', 'xcor', 'ycor', 'heading', 'pen size', 'color', 'shade', 'text color', 'text size', 'box 1', 'box 2', 'string', 'left pos', 'scale', 'top pos', 'right pos', 'bottom pos', 'width', 'height'] +BOX_STYLE_1ARG = ['box'] NUMBER_STYLE = ['plus', 'multiply', 'random'] NUMBER_STYLE_PORCH = ['minus', 'divide', 'mod'] NUMBER_STYLE_1ARG = ['square root'] @@ -59,7 +53,7 @@ BOOLEAN_STYLE = ['and', 'or'] NOT_STYLE = ['not'] FLOW_STYLE = ['forever', 'hspace'] FLOW_STYLE_1ARG = ['repeat'] -FLOW_STYLE_BOOLEAN = ['if else'] +FLOW_STYLE_BOOLEAN = ['if then'] TURTLE_PALETTE = ['clean', 'forward', 'back', 'left', 'right', 'seth', 'show', 'set scale', 'arc', 'scale', 'xcor', 'ycor', 'heading'] PEN_PALETTE = ['pen up','pen down', 'set pen size', 'set text size', @@ -106,8 +100,8 @@ BLOCK_NAMES = {'clean':_('clean'), 'forward':_('forward'), 'back':_('back'), 'set shade':_('set shade'), 'fill screen':_('fill screen'), 'pen size':_('pen size'), 'text size':_('text size'), 'color':_('color'), 'plus':'+', 'minus':'–', 'multiply':'×', 'divide':'/', 'mod':_('mod'), - 'random':_('random'), 'square root':'√', 'less than':'<', - 'greater than':'>', 'equal to':'=', 'and':_('and'), 'or':_('or'), + 'random':_('random'), 'square root':'√', 'less than':'<', + 'greater than':">", 'equal to':'=', 'and':_('and'), 'or':_('or'), 'not':_('not'), 'print':_('print'), 'wait':_('wait'), 'forever':_('forever'), 'repeat':_('repeat'), 'if then':_('if then'), 'stop action':_('stop action'), 'hspace':_(' '), 'vspace':_(' '), @@ -159,7 +153,8 @@ DEFAULTS = {'forward':[100], 'back':[100], 'left':[90], 'right':[90], 'set pen size':[5], 'set text size':[32], 'set color':[0], 'set shade':[50], 'fill screen':[60,80], 'number':[100], 'random':[0,100], 'wait':[1], 'repeat':[4], 'set xy':[0,0], - 'store in':[_('box'),100]} + 'store in':[_('box'),100], 'box':[_('my box')], 'def action':[_('action')], + 'action':[_('action')]} # # 'dead key' Unicode dictionaries diff --git a/sprite_factory.py b/sprite_factory.py index 8d168f4..6ebd74e 100755 --- a/sprite_factory.py +++ b/sprite_factory.py @@ -24,7 +24,6 @@ import pygtk pygtk.require('2.0') import gtk import os -from constants import SLOTY, INNIE, SPACER from gettext import gettext as _ class SVG: @@ -44,11 +43,11 @@ class SVG: self._stroke_width = 1 self._innie = [False] self._outie = False - self._innie_x1 = (INNIE-self._stroke_width)/2 + self._innie_x1 = (9-self._stroke_width)/2 self._innie_y1 = 3 - self._innie_x2 = (INNIE-self._stroke_width)/2 - self._innie_y2 = (INNIE-self._stroke_width)/2 - self._innie_spacer = SPACER + self._innie_x2 = (9-self._stroke_width)/2 + self._innie_y2 = (9-self._stroke_width)/2 + self._innie_spacer = 9 self._slot = True self._cap = False self._tab = True @@ -67,12 +66,11 @@ class SVG: def basic_block(self): (x, y) = self._calculate_x_y() svg = self._new_path(x, y) - svg += self._rarc_to(1, -1) + svg += self._corner(1, -1) svg += self._do_slot() - svg += self._do_cap() svg += self._rline_to(self._expand_x, 0) xx = self._x - svg += self._rarc_to(1, 1) + svg += self._corner(1, 1) for i in range(len(self._innie)): if self._innie[i] is True: svg += self._do_innie() @@ -81,11 +79,11 @@ class SVG: elif len(self._innie)-1 > i: svg += self._rline_to(0, 2*self._innie_y2+self._innie_spacer) svg += self._rline_to(0, self._expand_y) - svg += self._rarc_to(-1, 1) + svg += self._corner(-1, 1) svg += self._line_to(xx, self._y) svg += self._rline_to(-self._expand_x, 0) svg += self._do_tab() - svg += self._rarc_to(-1, -1) + svg += self._corner(-1, -1) svg += self._rline_to(0, -self._expand_y) if True in self._innie: svg += self._line_to(x, @@ -167,6 +165,7 @@ class SVG: svg += self._do_boolean() svg += self._rline_to(0,self._radius/2.0) svg += self._line_to(xx, self._y) + svg += self._rline_to(-self._expand_x, 0) svg += self._end_boolean() return self._header() + svg @@ -181,6 +180,7 @@ class SVG: svg += self._do_boolean() svg += self._rline_to(0,self._radius/2.0) svg += self._line_to(xx, self._y) + svg += self._rline_to(-self._expand_x, 0) svg += self._end_boolean() return self._header() + svg @@ -206,6 +206,7 @@ class SVG: svg += self._do_innie() svg += self._rline_to(0, self._radius) svg += self._line_to(xx, self._y) + svg += self._rline_to(-self._expand_x, 0) svg += self._end_boolean() return self._header() + svg @@ -432,15 +433,15 @@ class SVG: else: return self._line_to(self._x+dx, self._y+dy) - def _arc_to(self, x, y, a=90, l=0, s=1): - if self._radius == 0: + def _arc_to(self, x, y, r, a=90, l=0, s=1): + if r == 0: return self._line_to(x, y) else: self._x = x self._y = y self._check_min_max() return "A %.1f %.1f %.1f %d %d %.1f %.1f " % ( - self._radius, self._radius, a, l, s, x, y) + r, r, a, l, s, x, y) def _rarc_to(self, sign_x, sign_y, a=90, l=0, s=1): if self._radius == 0: @@ -448,7 +449,24 @@ class SVG: else: x = self._x + sign_x*self._radius y = self._y + sign_y*self._radius - return self._arc_to(x, y, a, l, s) + return self._arc_to(x, y, self._radius, a, l, s) + + def _corner(self, sign_x, sign_y, a=90, l=0, s=1): + svg_str = "" + if self._radius > 0: + r2 = self._radius/2.0 + if sign_x*sign_y == 1: + svg_str +=self._rline_to(sign_x*r2, 0) + else: + svg_str +=self._rline_to(0, sign_y*r2) + x = self._x + sign_x*r2 + y = self._y + sign_y*r2 + svg_str += self._arc_to(x, y, r2, a, l, s) + if sign_x*sign_y == 1: + svg_str +=self._rline_to(0, sign_y*r2) + else: + svg_str +=self._rline_to(sign_x*r2, 0) + return svg_str def _new_path(self, x, y): self._min_x = x @@ -470,16 +488,12 @@ class SVG: self._rline_to(0, self._slot_y), self._rline_to(self._slot_x, 0), self._rline_to(0, -self._slot_y)) - else: - return self._rline_to(self._slot_x, 0) - - def _do_cap(self): - if self._cap is True: + elif self._cap is True: return "%s%s" % ( self._rline_to(self._slot_x/2.0, -self._slot_y*2.0), self._rline_to(self._slot_x/2.0, self._slot_y*2.0)) else: - return "" + return self._rline_to(self._slot_x, 0) def _do_tab(self): if self._tab is True: @@ -496,7 +510,7 @@ class SVG: return self._rline_to(-self._slot_x, 0) def _do_innie(self): - self.docks.append((int(self._x*self._scale), + self.docks.append((int((self._x+self._stroke_width)*self._scale), int((self._y+self._innie_y2)*self._scale))) return "%s%s%s%s%s%s%s" % ( self._rline_to(-self._innie_x1, 0), @@ -527,7 +541,7 @@ class SVG: return "%s%s%s" % ( self._rline_to(0, self._porch_y), self._rline_to(self._porch_x-self._radius, 0), - self._rarc_to(1, 1)) + self._corner(1, 1)) def _start_boolean(self, xoffset, yoffset): svg = self._new_path(xoffset, yoffset) @@ -538,7 +552,8 @@ class SVG: return svg + self._rline_to(self._stroke_width, 0) def _do_boolean(self): - self.docks.append((int((self._x-self._radius)*self._scale), + self.docks.append( + (int((self._x-self._radius+self._stroke_width)*self._scale), int((self._y+self._radius)*self._scale))) return self._rarc_to(-1, 1, 90, 0, 0) + self._rarc_to(1, 1, 90, 0, 0) -- cgit v0.9.1