""" Very simple SVG library. Author: Sergey Vartanov (me@enzet.ru) """ import math class SVG: def __init__(self, file_): self.file = file_ self.index = 0 def begin(self, width, height): self.file.write( '\n\n') self.file.write( f'''\n''') def end(self): self.file.write('\n') def path( self, path: str, color: str = "black", width: float = 1, fill: str = "none", end: str = "butt", id: str = None, color2: str = None, gx1: float = 0, gy1: float = 0, gx2: float = 0, gy2: float = 0, dash: str = None, dashoffset: str = None, opacity: float = 1, transform: str = None): if color2: self.index += 1 self.file.write( f'' f'' f'\n') self.file.write(' \n') def line( self, x1, y1, x2, y2, width=1, color='black', end='butt', id_=None, color2=None, gx1=None, gy1=None, gx2=None, gy2=None, dash=None, dashoffset=None, opacity=None): if color2: if not gx1: gx1 = x1 if not gy1: gy1 = y1 if not gx2: gx2 = x2 if not gy2: gy2 = y2 self.index += 1 self.file.write( f'\n' f'' f'' f'\n') self.file.write( f' \n') def rect( self, x, y, width, height, color='black', id=None, rx=0, ry=0, opacity=1.0, stroke_color='none', stroke_width=1.0): self.file.write(' \n') def curve(self, x1, y1, x2, y2, x3, y3, x4, y4, id=None, width=1, color='black'): self.file.write(' \n') def rhombus(self, x, y, width, height, color='black', id=None): self.file.write(''' \n') def circle(self, x, y, d, color='black', color2='white', fill='none', opacity=None, width=1, id_=None, gx=0, gy=0, gr=0, fx=0, fy=0): is_grad = gx != 0 or gy != 0 or gr != 0 if is_grad: self.index += 1 self.file.write( f'' f'' f'\n') c = 0.577 self.file.write( f' \n') def text(self, x, y, text, font='Myriad Pro', size='10', align='left', color='black', id=None, weight=None, letter_spacing=None, angle=None, opacity=None): """ Drawing SVG element. """ if angle is None: self.file.write(f' 0: trans = 'transform = "matrix(' + str(math.sin(angle)) + ',' + str(math.cos(angle)) + ',' + \ str(-math.cos(angle)) + ',' + str(math.sin(angle)) + ',' + str(x) + ',' + str(y) + ')"' else: trans = 'transform = "matrix(' + str(math.sin(angle + math.pi)) + ',' + str(math.cos(angle + math.pi)) + ',' + \ str(-math.cos(angle + math.pi)) + ',' + str(math.sin(angle + math.pi)) + ',' + str(x) + ',' + str(y) + ')"' self.file.write(' ' + trans) self.file.write('>') self.file.write(text) self.file.write('\n') @staticmethod def get_color(color): if color == 'none': return 'none' if color == 'black': return 'black' return '#' + str(color) def begin_layer(self, name): self.file.write(f'\n') def end_layer(self): self.file.write('\n') def write(self, raw_code): self.file.write(raw_code)