diff --git a/src/diana/drawer/tikz.py b/src/diana/drawer/tikz.py --- a/src/diana/drawer/tikz.py +++ b/src/diana/drawer/tikz.py @@ -1,39 +1,39 @@ -class Tikz: - content = "" - footer = "" - extension = "tex" +from .base import Drawer + +def stone_color(c): + if c == "b": + return "black" + elif c == "w": + return "white" + else: + return c + + +def label_color(c): + return stone_color("b" if c == "w" else "w" if c == "b" else c) + + +class Tikz(Drawer): high_numbers = True - def __init__(self): - self.content = r'''\begin{tikzpicture} - \draw[step=\boardSquare,gray,very thin] (0,0) grid (18\boardSquare,18\boardSquare); - \draw (0,0) rectangle (18\boardSquare,18\boardSquare); - - ''' + def render(self, template_name, bgcolor=""): + points = [p for (i, p) in sorted(self._index.values(), key=lambda x: x[0])] - # stars - for i in range(3): - for j in range(3): - self.content += r''' \filldraw[fill=black] ({0}\boardSquare, {1}\boardSquare) circle[radius=0.04];'''.format(6*i+3, 6*j+3)+'\n' - self.content += '\n' - - self.footer = r'\end{tikzpicture}' '\n' + 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] - def __str__(self): - return self.content+self.footer + params = { + "stones": stones, "moves": moves, "labels": labels, "bgcolor": bgcolor, + "stone_color": stone_color, "label_color": label_color + } - def draw_stone(self, x, y, color): - fill = "black" if color == "b" else "white" - self.content += r' \filldraw[draw=black, fill={0}] ({1}\boardSquare, {2}\boardSquare) circle[radius=0.5\boardSquare];'.format(fill, x, 18-y)+'\n' + return self._env.get_template(template_name).render(params) - def draw_move(self, x, y, label, color): - fill = "black" if color == "b" else "white" - label_color = "white" if color == "b" else "black" - if (not self.high_numbers) and isinstance(label, int) and label%100 != 0: - label = label%100 + def save(self, filename, template="templ.tex", bgcolor=""): + file = open(filename + ".tikz", 'w') + file.write(self.render(template, bgcolor)) + file.close() - self.content += r' \filldraw[draw=black, fill={0}] ({1}\boardSquare, {2}\boardSquare) circle[radius=0.5\boardSquare] node[color={3}]{{{4}}};'.format(fill, x, 18-y, label_color, label)+'\n' - - def get_content(self): - return self.content+self.footer + super().save(filename)