diff --git a/src/diana/config.py b/src/diana/config.py --- a/src/diana/config.py +++ b/src/diana/config.py @@ -13,7 +13,7 @@ parser.add_argument("-s", "--suffix") parser.add_argument("-r", "--recursive") parser.add_argument("-i", "--input", nargs="+", required=True) parser.add_argument("-o", "--output") -parser.add_argument("--format", choices={"svg"}) +parser.add_argument("--format", choices={"svg", "tikz"}) parser.add_argument("-k", "--keep-broken") parser.add_argument("--moves-p-d", type=int) parser.add_argument("--min-p-d", type=int) diff --git a/src/diana/diana.py b/src/diana/diana.py --- a/src/diana/diana.py +++ b/src/diana/diana.py @@ -30,10 +30,11 @@ def collect_moves(root): class SourceFile: - def __init__(self, file_name): + def __init__(self, file_name, output_format): self.file_name = file_name self._short_name = "".join(re.split(r'[/\\]', file_name)[-1].split('.')[:-1]) self._game = go.Go() + self.drawer = Tikz if output_format == "tikz" else Svg with open(self.file_name, 'r', encoding=cfg.encoding) as f: games = Collection(f.read()).list_games() @@ -46,7 +47,7 @@ class SourceFile: i = 1 for k in range(0, len(self._moves), cfg.moves_per_diagram): filename = os.path.join(cfg.output_dir, "{0}-{1}".format(self._short_name, i)) - self.create_diagram(k, k + cfg.moves_per_diagram).save(filename, "templ-pleb.svg") + self.create_diagram(k, k+cfg.moves_per_diagram).save(filename) i += 1 info_str = """{GN} @@ -68,7 +69,7 @@ W: {PW} {WR} :rtype: Drawer""" # initialize the diagram - template = Svg() + template = self.drawer(start) self._set_move(start) @@ -97,7 +98,7 @@ W: {PW} {WR} return False # draw the move - template.add_move(c, r, color, k + 1) + template.add_move(c, r, color, k+1) return template @@ -153,7 +154,7 @@ def main(): for item in files: if os.path.isfile(item): try: - f = SourceFile(item) + f = SourceFile(item, cfg.output_format) f.process() except ParserError as e: print("Couldn't parse {0}, following error occured: {1}".format(item, e)) diff --git a/src/diana/drawer/svg.py b/src/diana/drawer/svg.py --- a/src/diana/drawer/svg.py +++ b/src/diana/drawer/svg.py @@ -37,7 +37,8 @@ class Svg(Drawer): params = { "boardSize": self.boardSize, "padding": self.padding, "stones": stones, "moves": moves, - "labels": labels, "adjustFont": adjust_font, "bgcolor": bgcolor} + "labels": labels, "adjustFont": adjust_font, "bgcolor": bgcolor + } return self._env.get_template(template_name).render(params) 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) diff --git a/src/diana/templ/templ.tikz b/src/diana/templ/templ.tikz new file mode 100644 --- /dev/null +++ b/src/diana/templ/templ.tikz @@ -0,0 +1,36 @@ +\documentclass{article} +\usepackage{tikz} +\newlength{\boardSquare} +\setlength{\boardSquare}{6mm} + +\begin{document} +\begin{tikzpicture} + +% grid +\draw[step=\boardSquare,gray,very thin] (0,0) grid (18\boardSquare,18\boardSquare); +\draw (0,0) rectangle (18\boardSquare,18\boardSquare); + +% stars +{% for y in [3,9,15] %} + {% for x in [3,9,15] %} + \filldraw[fill=black] ({{x}}\boardSquare,{{y}}\boardSquare) circle[radius=0.04]; + {% endfor %} +{% endfor %} + +% stones +{% for p in stones %} + \filldraw[draw=black,fill={{stone_color(p.color)}}] ({{p.x}}\boardSquare,{{p.y}}\boardSquare) circle[radius=0.5\boardSquare]; +{% endfor %} + +% moves +{% for p in moves -%} + \filldraw[draw=black,fill={{stone_color(p.color)}}] ({{p.x}}\boardSquare,{{p.y}}\boardSquare) circle[radius=0.5\boardSquare] node[color={{label_color(p.color)}}]{{ '{'+p.label|string+'}' }}; +{% endfor %} + +% labels +{% for p in labels -%} + {{labeledPoint(p)}} +{% endfor %} + +\end{tikzpicture} +\end{document}