Files
@ 3207d998d598
Branch filter:
Location: Diana/src/diana/drawer/svg.py - annotation
3207d998d598
1.7 KiB
text/x-python
reintroduced tikz output format
afabea7d0e61 616c96178973 616c96178973 d57d0d4ede15 686166c7d5bc 686166c7d5bc 686166c7d5bc 686166c7d5bc 686166c7d5bc 686166c7d5bc 686166c7d5bc 616c96178973 616c96178973 afabea7d0e61 686166c7d5bc 616c96178973 686166c7d5bc d57d0d4ede15 616c96178973 686166c7d5bc 616c96178973 686166c7d5bc 686166c7d5bc 616c96178973 d57d0d4ede15 966ee650fabf 966ee650fabf 966ee650fabf 966ee650fabf 966ee650fabf 966ee650fabf 686166c7d5bc 616c96178973 686166c7d5bc 616c96178973 616c96178973 616c96178973 fdd10bc51cf8 686166c7d5bc 3207d998d598 3207d998d598 616c96178973 d57d0d4ede15 616c96178973 686166c7d5bc 966ee650fabf 966ee650fabf 966ee650fabf 966ee650fabf 966ee650fabf 966ee650fabf 686166c7d5bc 686166c7d5bc 616c96178973 616c96178973 616c96178973 | from .base import Drawer
def adjust_font(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 Svg(Drawer):
extension = "svg"
padding = 15
high_numbers = True
def __init__(self, start=0):
super().__init__(start)
self.boardSize = 480
self.padding = 30
def render(self, template_name, bgcolor=""):
"""Render the template with the current drawer state.
:param str template_name: a template file name in the templ directory
:param bgcolor: a background color
:type bgcolor: an SVG color, which is the same as a CSS color
:return: the rendered template string"""
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": adjust_font, "bgcolor": bgcolor
}
return self._env.get_template(template_name).render(params)
def save(self, filename, template="templ.svg", bgcolor=""):
"""Render the template and save it with the filename.
:param str filename: a path where to save the diagram. SVG and TXT suffixes will be appended
:param str template: a template file name in the templ directory
:param bgcolor: a background color
:type bgcolor: an SVG color, which is the same as a CSS color"""
file = open(filename+".svg", 'w')
file.write(self.render(template, bgcolor))
file.close()
super().save(filename)
|