team-3/src/c3nav/mapdata/render/svg.py

116 lines
4.2 KiB
Python
Raw Normal View History

import re
2017-05-12 17:22:10 +02:00
import xml.etree.ElementTree as ET
from shapely.affinity import scale
class SVGImage:
2017-05-12 17:22:10 +02:00
def __init__(self, width: int, height: int, scale: float=1):
self.width = width
self.height = height
self.scale = scale
self.g = ET.Element('g', {})
2017-05-12 23:37:03 +02:00
self.defs = ET.Element('defs')
self.def_i = 0
2017-05-12 17:22:10 +02:00
blur_filter = ET.Element('filter', {'id': 'wallblur'})
2017-05-13 19:19:28 +02:00
blur_filter.append(ET.Element('feGaussianBlur',
{'in': 'SourceGraphic',
'stdDeviation': str(int(0.7 * self.scale))}))
self.defs.append(blur_filter)
2017-05-12 23:37:03 +02:00
def get_element(self):
2017-05-12 17:22:10 +02:00
root = ET.Element('svg', {
2017-05-12 23:37:03 +02:00
'width': str(self.width*self.scale),
'height': str(self.height*self.scale),
2017-05-12 17:22:10 +02:00
'xmlns:svg': 'http://www.w3.org/2000/svg',
'xmlns': 'http://www.w3.org/2000/svg',
'xmlns:xlink': 'http://www.w3.org/1999/xlink',
})
2017-05-12 23:37:03 +02:00
root.append(self.defs)
2017-05-12 17:22:10 +02:00
root.append(self.g)
return root
def get_xml(self):
return ET.tostring(self.get_element()).decode()
2017-05-12 23:37:03 +02:00
def new_defid(self):
defid = 's'+str(self.def_i)
self.def_i += 1
return defid
2017-05-12 17:22:10 +02:00
def _trim_decimals(self, data):
return re.sub(r'([0-9]+)\.0', r'\1', re.sub(r'([0-9]+\.[0-9])[0-9]+', r'\1', data))
2017-05-13 20:10:12 +02:00
def _create_geometry(self, geometry):
geometry = scale(geometry, xfact=1, yfact=-1, origin=(self.width / 2, self.height / 2))
geometry = scale(geometry, xfact=self.scale, yfact=self.scale, origin=(0, 0))
element = ET.fromstring(self._trim_decimals(geometry.svg(0, '#FFFFFF')))
2017-05-12 17:22:10 +02:00
if element.tag != 'g':
new_element = ET.Element('g')
new_element.append(element)
element = new_element
paths = element.findall('polyline')
if len(paths) == 0:
paths = element.findall('path')
for path in paths:
2017-05-12 23:37:03 +02:00
path.attrib.pop('opacity', None)
path.attrib.pop('fill', None)
path.attrib.pop('fill-rule', None)
path.attrib.pop('stroke', None)
path.attrib.pop('stroke-width', None)
2017-05-13 20:10:12 +02:00
return element
def register_geometry(self, geometry, defid=None, as_clip_path=False, comment=None):
if defid is None:
defid = self.new_defid()
2017-05-12 23:37:03 +02:00
2017-05-13 20:10:12 +02:00
element = self._create_geometry(geometry)
if as_clip_path:
element.tag = 'clipPath'
2017-05-12 23:37:03 +02:00
element.set('id', defid)
self.defs.append(element)
return defid
2017-05-13 19:19:28 +02:00
def add_clip_path(self, *geometries, inverted=False, subtract=False, defid=None):
2017-05-12 23:37:03 +02:00
if defid is None:
defid = self.new_defid()
2017-05-13 19:19:28 +02:00
clippath = ET.Element('clipPath', {'id': defid})
clippath.append(ET.Element('use', {'xlink:href': '#' + geometries[0]}))
self.defs.append(clippath)
2017-05-12 23:37:03 +02:00
return defid
2017-05-13 20:12:42 +02:00
def add_geometry(self, geometry=None, fill_color=None, fill_opacity=None, opacity=None, filter=None,
2017-05-13 19:19:28 +02:00
stroke_width=0.0, stroke_color=None, stroke_opacity=None, stroke_linejoin=None, clip_path=None):
if geometry is not None:
2017-05-13 20:48:27 +02:00
if not geometry:
return
2017-05-13 20:10:12 +02:00
if isinstance(geometry, str):
element = ET.Element('use', {'xlink:href': '#'+geometry})
else:
element = self._create_geometry(geometry)
2017-05-13 19:19:28 +02:00
else:
element = ET.Element('rect', {'width': '100%', 'height': '100%'})
2017-05-12 23:37:03 +02:00
element.set('fill', fill_color or 'none')
if fill_opacity:
element.set('fill-opacity', str(fill_opacity)[:4])
2017-05-12 23:37:03 +02:00
if stroke_width:
element.set('stroke-width', self._trim_decimals(str(stroke_width * self.scale)))
2017-05-12 23:37:03 +02:00
if stroke_color:
element.set('stroke', stroke_color)
if stroke_opacity:
element.set('stroke-opacity', str(stroke_opacity)[:4])
2017-05-12 23:37:03 +02:00
if stroke_linejoin:
element.set('stroke-linejoin', stroke_linejoin)
2017-05-12 23:37:03 +02:00
if opacity:
element.set('opacity', str(opacity)[:4])
2017-05-12 23:37:03 +02:00
if filter:
element.set('filter', 'url(#'+filter+')')
2017-05-13 19:19:28 +02:00
if clip_path:
element.set('clip-path', 'url(#'+clip_path+')')
2017-05-12 17:22:10 +02:00
2017-05-12 23:37:03 +02:00
self.g.append(element)
2017-05-12 17:22:10 +02:00
return element