Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class SVG:
- def __init__(self, width, height):
- self.width = width
- self.height = height
- self.objects = []
- def serialize(self):
- lines = []
- lines.append('<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="'+str(self.width)+'" height="'+str(self.height)+'" viewBox="0 0 '+str(self.width)+' '+str(self.height)+'">\n')
- for i in range(len(self.objects)):
- lines.extend(map(lambda x:'\t'+x, self.objects[i].serialize()))
- lines.append('</svg>')
- return lines
- def save(self, file):
- provided = True
- if isinstance(file, str):
- file = open(file, "w")
- provided = False
- file.writelines(self.serialize())
- if not provided:
- file.close()
- return file
- def add(self, object, uptop=False):
- if uptop:
- self.objects.insert(0, object)
- else:
- self.objects.append(object)
- return self
- class SVGGroup:
- def __init__(self, translate=None, rotate=None, scale=None, origin=None):
- self.objects = []
- self.translate = translate
- self.rotate = rotate
- self.scale = scale
- self.origin = origin
- def add(self, object, uptop=False):
- if uptop:
- self.objects.insert(0, object)
- else:
- self.objects.append(object)
- return self
- def serialize(self):
- lines = []
- text = '<g'
- if self.translate is not None or (self.rotate is not None and self.rotate!=0) or self.scale is not None:
- text = text + ' transform="'
- space = ''
- if self.origin is not None:
- text = text + space + 'translate' + str(self.origin)
- space = ' '
- if self.rotate is not None and self.rotate!=0:
- text = text + space + 'rotate('+str(self.rotate) + ')'
- space = ' '
- if self.scale is not None:
- if isinstance(self.scale, tuple):
- text = text + space + 'scale' + str(self.scale)
- else:
- text = text + space + 'scale(' + str(self.scale) + ')'
- space = ' '
- if self.translate is not None:
- if self.origin is not None:
- text = text + space + 'translate(' + str(-self.origin[0]+self.translate[0]) + ', ' + str(-self.origin[1]+self.translate[1]) + ')'
- else:
- text = text + space + 'translate' + str(self.translate)
- text = text + '"'
- text = text + '>\n'
- lines.append(text)
- for i in range(len(self.objects)):
- lines.extend(map(lambda x:'\t' + x, self.objects[i].serialize()))
- lines.append('</g>\n')
- return lines
- class SVGLine:
- def __init__(self, start, end, stroke_width, color, linecap="butt"):
- self.x1 = start[0]
- self.y1 = start[1]
- self.x2 = end[0]
- self.y2 = end[1]
- self.stroke_width = stroke_width
- self.color = color
- self.stroke_linecap = linecap #butt/round/square
- #self.stroke_dasharray = stroke_dasharray #tupple of lengths of segment and spaces
- def serialize(self):
- text = '<line x1="'+str(self.x1)+'" y1="'+str(self.y1)+'" x2="'+str(self.x2)+'" y2="'+str(self.y2)+'" stroke="'+self.color+'" stroke-width="'+str(self.stroke_width)+'"'
- if self.stroke_linecap!="butt":
- text = text + ' stroke-linecap="'+self.stroke_linecap+'"'
- text = text + '/>\n'
- return [text]
- def Color(r, g, b, a=1):
- if a==1:
- return "rgb(" + str(r) + "," + str(g) + "," + str(b) + ")"
- else:
- return "rgb(" + str(r) + "," + str(g) + "," + str(b) + "," + str(a) + ")"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement