Advertisement
Tomlacko

Python SVG small custom lib

Nov 16th, 2018
598
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.24 KB | None | 0 0
  1. class SVG:
  2.     def __init__(self, width, height):
  3.         self.width = width
  4.         self.height = height
  5.         self.objects = []
  6.  
  7.     def serialize(self):
  8.         lines = []
  9.         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')
  10.         for i in range(len(self.objects)):
  11.             lines.extend(map(lambda x:'\t'+x, self.objects[i].serialize()))
  12.         lines.append('</svg>')
  13.  
  14.         return lines
  15.  
  16.     def save(self, file):
  17.         provided = True
  18.         if isinstance(file, str):
  19.             file = open(file, "w")
  20.             provided = False
  21.  
  22.         file.writelines(self.serialize())
  23.  
  24.         if not provided:
  25.             file.close()
  26.  
  27.         return file
  28.  
  29.     def add(self, object, uptop=False):
  30.         if uptop:
  31.             self.objects.insert(0, object)
  32.         else:
  33.             self.objects.append(object)
  34.  
  35.         return self
  36.  
  37.  
  38. class SVGGroup:
  39.     def __init__(self, translate=None, rotate=None, scale=None, origin=None):
  40.         self.objects = []
  41.         self.translate = translate
  42.         self.rotate = rotate
  43.         self.scale = scale
  44.         self.origin = origin
  45.  
  46.     def add(self, object, uptop=False):
  47.         if uptop:
  48.             self.objects.insert(0, object)
  49.         else:
  50.             self.objects.append(object)
  51.  
  52.         return self
  53.  
  54.     def serialize(self):
  55.         lines = []
  56.         text = '<g'
  57.         if self.translate is not None or (self.rotate is not None and self.rotate!=0) or self.scale is not None:
  58.             text = text + ' transform="'
  59.             space = ''
  60.             if self.origin is not None:
  61.                 text = text + space + 'translate' + str(self.origin)
  62.                 space = ' '
  63.             if self.rotate is not None and self.rotate!=0:
  64.                 text = text + space + 'rotate('+str(self.rotate) + ')'
  65.                 space = ' '
  66.             if self.scale is not None:
  67.                 if isinstance(self.scale, tuple):
  68.                     text = text + space + 'scale' + str(self.scale)
  69.                 else:
  70.                     text = text + space + 'scale(' + str(self.scale) + ')'
  71.                 space = ' '
  72.             if self.translate is not None:
  73.                 if self.origin is not None:
  74.                     text = text + space + 'translate(' + str(-self.origin[0]+self.translate[0]) + ', ' + str(-self.origin[1]+self.translate[1]) + ')'
  75.                 else:
  76.                     text = text + space + 'translate' + str(self.translate)
  77.  
  78.             text = text + '"'
  79.         text = text + '>\n'
  80.         lines.append(text)
  81.  
  82.         for i in range(len(self.objects)):
  83.             lines.extend(map(lambda x:'\t' + x, self.objects[i].serialize()))
  84.  
  85.         lines.append('</g>\n')
  86.  
  87.         return lines
  88.  
  89.  
  90. class SVGLine:
  91.     def __init__(self, start, end, stroke_width, color, linecap="butt"):
  92.         self.x1 = start[0]
  93.         self.y1 = start[1]
  94.         self.x2 = end[0]
  95.         self.y2 = end[1]
  96.         self.stroke_width = stroke_width
  97.         self.color = color
  98.         self.stroke_linecap = linecap #butt/round/square
  99.         #self.stroke_dasharray = stroke_dasharray #tupple of lengths of segment and spaces
  100.  
  101.     def serialize(self):
  102.         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)+'"'
  103.         if self.stroke_linecap!="butt":
  104.             text = text + ' stroke-linecap="'+self.stroke_linecap+'"'
  105.         text = text + '/>\n'
  106.         return [text]
  107.  
  108.  
  109. def Color(r, g, b, a=1):
  110.     if a==1:
  111.         return "rgb(" + str(r) + "," + str(g) + "," + str(b) + ")"
  112.     else:
  113.         return "rgb(" + str(r) + "," + str(g) + "," + str(b) + "," + str(a) + ")"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement