Files @ 0137299a43da
Branch filter:

Location: Diana/src/drawer/svg.py - annotation

Laman
split drawing classes into a separate modules in a separate package
c=28
padding=15
highNumbers=True


class Svg:
	content=""
	footer=""
	extension="svg"

	padding=15
	gridSize=28
	highNumbers=True
	
	def __init__(self):
		self.content='''<?xml version="1.0" standalone="no"?>
<svg width="{0}" height="{0}" version="1.1" xmlns="http://www.w3.org/2000/svg" alignment-baseline="center">
	<defs>
		<style type="text/css"><![CDATA[
		text{{font-family:"DejaVu Sans";text-anchor:middle;}}
		line{{stroke:black;stroke-width:0.7}}
		circle{{stroke:black}}
		.b{{fill:black}}
		.w{{fill:white}}
		]]></style>
	</defs>
	<rect width="{0}" height="{0}" x="0" y="0" style="fill:white;stroke:white"/>\n'''.format(2*self.padding+18*self.gridSize)
		self.footer="</svg>\n"

		grid='  <line x1="{0}" x2="{1}" y1="{2}" y2="{3}" />\n'

		# okraje desky
		for i in (0,18):
			self.content+='  <line x1="{0}" x2="{1}" y1="{2}" y2="{3}" style="stroke-width:1"/>\n'.format(self.padding, 18*self.gridSize+self.padding, self.gridSize*i+self.padding, self.gridSize*i+self.padding)
			self.content+='  <line x1="{0}" x2="{1}" y1="{2}" y2="{3}" style="stroke-width:1"/>\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)

		# hvězdy
		for i in range(3):
			for j in range(3):
				self.content+='  <circle cx="{0}" cy="{1}" r="{2}" class="b"/>\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+='  <circle cx="{0}" cy="{1}" r="{2}" class="{3}" />\n'.format(padding+x*c, padding+y*c, c/2-1, color)

	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)
		
	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