Changeset - fdd10bc51cf8
[Not reviewed]
default
0 3 0
Laman - 6 years ago 2019-05-29 16:34:11

optional background color
3 files changed with 8 insertions and 6 deletions:
0 comments (0 inline, 0 general)
src/diana/drawer/svg.py
Show inline comments
 
from .base import Base
 

	
 

	
 
def adjustFont(base,text):
 
	text=str(text)
 
	if len(text)<2: return round(0.7*base)
 
	elif len(text)<3: return round(0.55*base)
 
	else: return round(0.4*base)
 

	
 

	
 
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(Base):
 
	extension="svg"
 

	
 
	padding=15
 
	highNumbers=True
 
	
 
	def __init__(self,start=0):
 
		super().__init__(start)
 
		self.boardSize=480
 
		self.padding=30
 

	
 
	def render(self, templateName):
 
	def render(self,templateName,bgcolor=""):
 
		points = [p for (i,p) in sorted(self._index.values(), key=lambda x: x[0])]
 

	
 
		stones = [p for p in points if p.color and 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]
 

	
 
		params = {"boardSize":self.boardSize, "padding":self.padding, "stones":stones, "moves":moves, "labels":labels, "adjustFont":adjustFont}
 
		params = {
 
			"boardSize":self.boardSize, "padding":self.padding, "stones":stones, "moves":moves,
 
			"labels":labels, "adjustFont":adjustFont, "bgcolor":bgcolor}
 

	
 
		return self._env.get_template(templateName).render(params)
 

	
 
	def save(self,filename,template="templ.svg"):
 
	def save(self,filename,template="templ.svg",bgcolor=""):
 
		file=open(filename+".svg",'w')
 
		file.write(self.render(template))
 
		file.write(self.render(template,bgcolor))
 
		file.close()
 

	
 
		super().save(filename)
src/diana/templ/templ-pleb.svg
Show inline comments
 
{% extends "templ.svg" %}
 

	
 
{% block style %}
 
rect{fill:white;stroke:black;stroke-width:1px}
 
rect{fill:{{bgcolor or "white"}};stroke:black;stroke-width:1px}
 
text{font-family:"DejaVu Sans";text-anchor:middle;}
 
line{stroke:black;stroke-width:0.7px}
 
circle{stroke:black}
 
circle.b, text.w, text.e{fill:black}
 
circle.w, text.b{fill:white}
 
circle.e {stroke:none;fill:white}
 
{% endblock %}
 

	
 
{% macro labeledPoint(p) %}
 
<g transform="translate({{(p.x*cellSize) | round(2)}},{{(p.y*cellSize) | round(2)}})">
 
	<circle class="{{p.color or 'e'}}" cx="0" cy="0" r="{{stoneRadius|round(2)}}"/>
 
	<text class="{{p.color or 'e'}}" y="0.35em" font-size="{{adjustFont(cellSize,p.label)}}">{{p.label}}</text>
 
</g>
 
{% endmacro %}
src/diana/templ/templ.svg
Show inline comments
 
<?xml version="1.0" encoding="utf-8"?>
 
{% set cellSize=boardSize/18 %}
 
{% set stoneRadius=cellSize/2-1 -%}
 

	
 
{% if not labeledPoint %}
 
	{% macro labeledPoint(p) %}
 
	<g class="{{p.color or 'e'}}" transform="translate({{(p.x*cellSize) | round(2)}},{{(p.y*cellSize) | round(2)}})">
 
		<circle cx="0" cy="0" r="{{stoneRadius|round(2)}}"/>
 
		<text y="0.35em" font-size="{{adjustFont(cellSize,p.label)}}">{{p.label}}</text>
 
	</g>
 
	{% endmacro %}
 
{% endif -%}
 

	
 
<svg width="{{boardSize+padding*2}}" height="{{boardSize+padding*2}}" version="1.1" xmlns="http://www.w3.org/2000/svg">
 
	<defs>
 
		<style type="text/css"><![CDATA[
 
		{% block style %}
 
			rect{fill:white;stroke:black;stroke-width:1px}
 
			rect{fill:{{bgcolor or "white"}};stroke:black;stroke-width:1px}
 
			text{font-family:"DejaVu Sans";text-anchor:middle}
 
			line{stroke:black;stroke-width:0.7px}
 
			circle{stroke:black}
 
			circle.b, .b circle, .w text, .e text{fill:black}
 
			circle.w, .w circle, .b text{fill:white}
 
			.e circle {stroke:none;fill:white}
 
		{% endblock %}
 
		]]></style>
 
	</defs>
 
	<g transform="translate({{padding}},{{padding}})">
 
		<rect x="0" y="0" width="{{boardSize}}" height="{{boardSize}}"/>
 
		{# grid #}
 
		{% for i in range(1,18) %}
 
			<line x1="0" x2="{{boardSize}}" y1="{{(i*cellSize) | round(2)}}" y2="{{(i*cellSize) | round(2)}}"/>
 
			<line x1="{{(i*cellSize) | round(2)}}" x2="{{(i*cellSize) | round(2)}}" y1="0" y2="{{boardSize}}"/>
 
		{% endfor %}
 

	
 
		{# stars #}
 
		{% for y in [3,9,15] %}
 
			{% for x in [3,9,15] %}
 
		  <circle cx="{{(x*cellSize) | round(2)}}" cy="{{(y*cellSize) | round(2)}}" r="2" class="b"/>
 
			{% endfor %}
 
		{% endfor %}
 

	
 
		{# stones #}
 
		{% for p in stones %}
 
			<circle cx="{{(p.x*cellSize) | round(2)}}" cy="{{(p.y*cellSize) | round(2)}}" r="{{stoneRadius|round(2)}}" class="{{p.color}}"/>
 
		{% endfor %}
 

	
 
		{# moves #}
 
		{% for p in moves -%}
 
			{{labeledPoint(p)}}
 
		{% endfor %}
 

	
 
		{# labels #}
 
		{% for p in labels -%}
 
			{{labeledPoint(p)}}
 
		{% endfor %}
 
	</g>
 
</svg>
0 comments (0 inline, 0 general)