#!/usr/bin/env python
# -*- coding: utf-8 -*-
#Copyright (c) 2009,10 Walter Bender
#Permission is hereby granted, free of charge, to any person obtaining a copy
#of this software and associated documentation files (the "Software"), to deal
#in the Software without restriction, including without limitation the rights
#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#copies of the Software, and to permit persons to whom the Software is
#furnished to do so, subject to the following conditions:
#The above copyright notice and this permission notice shall be included in
#all copies or substantial portions of the Software.
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
#THE SOFTWARE.
import os
from gettext import gettext as _
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._width = 0
self._height = 0
self._scale = 1
self._radius = 8
self._stroke_width = 1
self._innie = [False]
self._outie = False
self._innie_x1 = 4
self._innie_y1 = 3
self._innie_x2 = 4
self._innie_y2 = 4
self._innie_spacer = 9
self._slot = True
self._tab = True
self._bool = False
self._slot_x = 12
self._slot_y = 4
self._porch = False
self._porch_x = self._innie_x1+self._innie_x2+4*self._stroke_width
self._porch_y = self._innie_y1+self._innie_y2+4*self._stroke_width
self._expand_x = 0
self._expand_y = 0
self._fill = "#00FF00"
self._stroke = "#00A000"
self._gradiant = False
def basic_block(self):
(x, y) = self._calculate_x_y()
svg = self._new_path(x, y)
svg += self._rarc_to(1, -1)
svg += self._do_slot()
svg += self._rline_to(self._expand_x, 0)
xx = self._x
svg += self._rarc_to(1, 1)
for i in range(len(self._innie)):
if self._innie[i] is True:
svg += self._do_innie()
if i==0 and self._porch is True:
svg += self._do_porch()
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._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._rline_to(0, -self._expand_y)
if True in self._innie:
svg += self._line_to(x,
self._radius+self._innie_y2+self._stroke_width)
svg += self._do_outie()
svg += self._close_path()
self._calculate_w_h()
svg += self._style()
svg += self._footer()
return self._header() + svg
def basic_flow(self):
(x, y) = self._calculate_x_y()
svg = self._new_path(x, y)
svg += self._rarc_to(1, -1)
svg += self._do_slot()
svg += self._rline_to(self._expand_x, 0)
xx = self._x
svg += self._rarc_to(1, 1)
for i in range(len(self._innie)):
if self._innie[i] is True:
svg += self._do_innie()
svg += self._rline_to(0, 2*self._innie_y2+self._innie_spacer)
if self._bool is True:
svg += self._rline_to(0,self._radius/2.0)
svg += self._do_boolean()
svg += self._rline_to(0,self._radius/2.0)
svg += self._rline_to(self._radius+self._slot_x, 0)
svg += self._rarc_to(1,1)
svg += self._rline_to(-self._radius,0)
svg += self._do_tab()
svg += self._rline_to(-self._radius, 0)
svg += self._rline_to(0, self._expand_y)
svg += self._rarc_to(-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._rline_to(0, -self._expand_y)
if True in self._innie:
svg += self._line_to(x,
self._radius+self._innie_y2+self._stroke_width)
svg += self._do_outie()
svg += self._close_path()
self._calculate_w_h()
svg += self._style()
svg += self._footer()
return self._header() + svg
def basic_box(self):
self.set_outie(True)
x = self._stroke_width/2.0+self._innie_x1+self._innie_x2
svg = self._new_path(x, self._stroke_width/2.0)
svg += self._rline_to(self._expand_x, 0)
svg += self._rline_to(0, 2*self._radius+self._innie_y2+self._expand_y)
svg += self._rline_to(-self._expand_x, 0)
svg += self._line_to(x, self._radius+self._innie_y2+\
self._stroke_width/2.0)
svg += self._do_outie()
svg += self._close_path()
self._calculate_w_h()
svg += self._style()
svg += self._footer()
return self._header() + svg
def boolean_and_or(self):
svg = self._start_boolean(self._stroke_width/2.0,
self._radius*5.5+self._stroke_width/2.0+\
self._innie_y2+self._innie_spacer)
svg += self._rline_to(0,-self._radius*3.5-self._innie_y2-\
self._innie_spacer-self._stroke_width)
svg += self._rarc_to(1, -1)
svg += self._rline_to(self._radius/2.0+self._expand_x, 0)
svg += self._rline_to(0,self._radius/2.0)
svg += self._do_boolean()
svg += self._rline_to(0,self._radius*1.5+self._innie_y2+\
self._innie_spacer)
svg += self._do_boolean()
svg += self._rline_to(0,self._radius/2.0)
svg += self._end_boolean()
return self._header() + svg
def boolean_not(self):
svg = self._start_boolean(self._stroke_width/2.0,
self._radius*2.0+self._stroke_width/2.0)
svg += self._rline_to(0,-self._stroke_width)
svg += self._rarc_to(1, -1)
svg += self._rline_to(self._radius/2.0+self._expand_x, 0)
svg += self._rline_to(0,self._radius/2.0)
svg += self._do_boolean()
svg += self._rline_to(0,self._radius/2.0)
svg += self._end_boolean()
return self._header() + svg
def boolean_compare(self):
yoffset = self._radius*2+2*self._innie_y2+\
self._innie_spacer+self._stroke_width/2.0
if self._porch is True:
yoffset += self._porch_y
svg = self._start_boolean(self._stroke_width/2.0, yoffset)
yoffset = -2*self._innie_y2-self._innie_spacer-self._stroke_width
if self._porch is True:
yoffset -= self._porch_y
svg += self._rline_to(0, yoffset)
svg += self._rarc_to(1, -1)
svg += self._rline_to(self._radius/2.0+self._expand_x, 0)
svg += self._rline_to(0,self._radius)
xx = self._x
svg += self._do_innie()
if self._porch is True:
svg += self._do_porch()
else:
svg += self._rline_to(0, 2*self._innie_y2+self._innie_spacer)
svg += self._do_innie()
svg += self._rline_to(0, self._radius)
svg += self._line_to(xx, self._y)
svg += self._end_boolean()
return self._header() + svg
#
# Utility methods
#
def set_scale(self, scale=1):
self._scale = scale
def expand(self, w=0, h=0):
self._expand_x = w
self._expand_y = h
def set_stroke_width(self, stroke_width=1.5):
self._stroke_width = stroke_width
self._calc_porch_params()
def set_colors(self, colors=["#00FF00","#00A000"]):
self._fill = colors[0]
self._stroke = colors[1]
def set_gradiant(self, flag=False):
self._gradiant = flag
def set_innie(self, innie_array=[False]):
self._innie = innie_array
def set_outie(self, flag=False):
self._outie = flag
def set_slot(self, flag=True):
self._slot = flag
def set_tab(self, flag=True):
self._tab = flag
def set_porch(self, flag=False):
self._porch = flag
def set_boolean(self, flag=False):
self._bool = flag
#
# Exotic methods
#
def set_radius(self, radius=8):
self._radius = radius
def set_innie_params(self, x1=4, y1=3, x2=4, y2=4):
self._innie_x1 = x1
self._innie_y1 = y1
self._innie_x2 = x2
self._innie_y2 = y2
self._calc_porch_params()
def set_innie_spacer(self, innie_spacer = 0):
self._innie_spacer = innie_spacer
def set_slot_params(self, x=12, y=4):
self._slot_x = x
self._slot_y = y
def _calc_porch_params(self):
self._porch_x = self._innie_x1+self._innie_x2+4*self._stroke_width
self._porch_y = self._innie_y1+self._innie_y2+4*self._stroke_width
#
# SVG helper methods
#
def _header(self):
return "%s%s%s%s%s%s%s%s%.1f%s%s%.1f%s%s%s" % (
"\n",
"\n",
"\n"
def _style(self):
if self._gradiant is True:
fill = "url(#linearGradient5678)"
else:
fill = self._fill
return "%s%s;%s%s;%s%.1f;%s%s" % (
" style=\"fill:",fill,
"fill-opacity:1;stroke:",self._stroke,
"stroke-width:",self._stroke_width,
"stroke-linecap:square;",
"stroke-opacity:1;\" />\n")
def _check_min_max(self):
if self._x < self._min_x:
self._min_x = self._x
if self._y < self._min_y:
self._min_y = self._y
if self._x > self._max_x:
self._max_x = self._x
if self._y > self._max_y:
self._max_y = self._y
def _line_to(self, x, y):
if self._x == x and self._y == y:
return ""
else:
self._x = x
self._y = y
self._check_min_max()
return "L %.1f %.1f " % (x, y)
def _rline_to(self, dx, dy):
if dx == 0 and dy == 0:
return ""
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:
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)
def _rarc_to(self, sign_x, sign_y, a=90, l=0, s=1):
if self._radius == 0:
return ""
else:
x = self._x + sign_x*self._radius
y = self._y + sign_y*self._radius
return self._arc_to(x, y, a, l, s)
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 "