Files @ b9d29dcfe3d8
Branch filter:

Location: Diana/src/drawer/svg.py

Laman
svg rendering
from jinja2 import Template


c=28
padding=15
highNumbers=True


class DiagramPoint:
	def __init__(self,x,y,color="",label=""):
		self.x=x
		self.y=y
		self.color=color
		self.label=label

	def __repr__(self):
		return 'DiagramPoint({0},{1},"{2}","{3}")'.format(self.x,self.y,self.color,self.label)


class Svg:
	extension="svg"

	padding=15
	highNumbers=True
	
	def __init__(self):
		self.boardSize=480
		self.padding=30

		self._index=dict()

	def addStone(self,x,y,color):
		self._index[(x,y)]=(len(self._index),DiagramPoint(x,y,color))

	def addMove(self,x,y,color,label):
		if (not self.highNumbers) and isinstance(label,int) and label%100!=0:
			label%=100

		self._index[(x,y)]=(len(self._index),DiagramPoint(x,y,color,label))

	def addLabel(self,x,y,label):
		self._index[(x,y)]=(len(self._index),DiagramPoint(x,y,"",label))

	def render(self, template):
		points = [p for (i,p) in sorted(self._index.values())]

		stones = [p for p in points if p.color and not p.label]
		moves = [p for p in points if p.color and p.label]
		labels = [p for p in points if not p.color and p.label]

		svg = Template(template)
		params = {"boardSize":self.boardSize, "padding":self.padding, "stones":stones, "moves":moves, "labels":labels}

		return svg.render(params)

	# def getFontSize(self,text):
	# 	if len(text)<2: return round(0.7*c)
	# 	elif len(text)<3: return round(0.55*c)
	# 	else: return round(0.4*c)
	#
	# def writeLabel(self,x,y,label,color):
	# 	label=str(label)
	# 	fontSize=self.getFontSize(label)
	# 	self.content+='  <text x="{0}" y="{1}" class="{2}" font-size="{3}">{4}</text>\n'.format(padding+x*c, padding+y*c+0.35*fontSize, color, fontSize, label)