diff --git a/src/drawer/svg.py b/src/drawer/svg.py old mode 100644 new mode 100755 --- a/src/drawer/svg.py +++ b/src/drawer/svg.py @@ -1,72 +1,64 @@ +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: - content="" - footer="" extension="svg" padding=15 - gridSize=28 highNumbers=True def __init__(self): - self.content=''' - - - - - \n'''.format(2*self.padding+18*self.gridSize) - self.footer="\n" + 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)) - grid=' \n' + def addMove(self,x,y,color,label): + if (not self.highNumbers) and isinstance(label,int) and label%100!=0: + label%=100 - # okraje desky - for i in (0,18): - self.content+=' \n'.format(self.padding, 18*self.gridSize+self.padding, self.gridSize*i+self.padding, self.gridSize*i+self.padding) - self.content+=' \n'.format(self.gridSize*i+self.padding, self.gridSize*i+self.padding, self.padding, 18*self.gridSize+self.padding) - - # mřížka - for i in range(1,18): - self.content+=grid.format(self.padding, 18*self.gridSize+self.padding, c*i+self.padding, c*i+self.padding) - self.content+=grid.format(c*i+self.padding, c*i+self.padding, self.padding, 18*c+self.padding) + 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)) - # hvězdy - for i in range(3): - for j in range(3): - self.content+=' \n'.format(padding+3*c+6*i*c, padding+3*c+6*j*c, 2) - - def __str__(self): - return self.content+self.footer - - def drawStone(self,x,y,color): - self.content+=' \n'.format(padding+x*c, padding+y*c, c/2-1, color) + def render(self, template): + points = [p for (i,p) in sorted(self._index.values())] - 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) + 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} - def writeLabel(self,x,y,label,color): - label=str(label) - fontSize=self.getFontSize(label) - self.content+=' {4}\n'.format(padding+x*c, padding+y*c+0.35*fontSize, color, fontSize, label) - - def drawMove(self,x,y,label,color): - labelColor="w" if color=="b" else "b" - - if (not self.highNumbers) and isinstance(label,int) and label%100!=0: label=label%100 # dost neobratná logika - - self.drawStone(x,y,color) - self.writeLabel(x,y,label,labelColor) - - def getContent(self): - return self.content+self.footer + 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+=' {4}\n'.format(padding+x*c, padding+y*c+0.35*fontSize, color, fontSize, label)