svg_tetraflexagon_editor: remove duplicate code
[flexagon-toolkit.git] / src / svg_tetraflexagon_editor.py
1 #!/usr/bin/env python3
2 #
3 # Draw an SVG tetraflexagon which can be edited live in Inkscape.
4 #
5 # Copyright (C) 2018  Antonio Ospite <ao2@ao2.it>
6 #
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
20 import svgwrite
21
22 from diagram.svgwrite_diagram import SvgwriteDiagram
23 from flexagon.tetraflexagon_diagram import TetraflexagonDiagram
24
25
26 class SvgwriteTetraflexagonDiagram(TetraflexagonDiagram):
27     def __init__(self, *args, **kwargs):
28         super(SvgwriteTetraflexagonDiagram, self).__init__(*args, **kwargs)
29
30         svg = self.backend.svg
31
32         # create some layers and groups
33         layers = {
34             "Squares": svg.layer(label="Squares"),
35             "Tetraflexagon": svg.layer(label="Tetraflexagon"),
36             "Template": svg.layer(label="Template")
37         }
38         for layer in layers.values():
39             svg.add(layer)
40
41         self.groups = layers
42
43         for square in self.tetraflexagon.squares:
44             name = "square%d-content" % square.index
45             layer = svg.layer(id=name, label="Square %d" % (square.index + 1))
46             self.groups[name] = layer
47             layers['Squares'].add(layer)
48
49             for tile in square.tiles:
50                 name = "square%d-tile%d" % (square.index, tile.index)
51                 group = svg.g(id=name)
52                 self.groups[name] = group
53                 layers['Template'].add(group)
54
55     def draw(self):
56         for square in self.tetraflexagon.squares:
57             cx, cy = self.get_square_center(square)
58
59             # Draw some default content
60             old_active_group = self.backend.active_group
61             self.backend.active_group = self.groups["square%d-content" % square.index]
62             self.backend.draw_rect_from_center(cx, cy, self.square_side, self.square_side, 0,
63                                                fill_color=(0.5, 0.5, 0.5, 0.2))
64             self.backend.active_group = old_active_group
65
66         # Draw the normal template for squares
67         for square in self.tetraflexagon.squares:
68             self.draw_square_template(square)
69
70         # draw plan using references
71         for square in self.tetraflexagon.squares:
72             for tile in square.tiles:
73                 m = self.get_tile_transform(tile)
74                 svg_matrix = "matrix(%f, %f, %f, %f, %f, %f)" % (m[0], m[3],
75                                                                  m[1], m[4],
76                                                                  m[2], m[5])
77
78                 # Reuse the squares tile for the tetraflexagon template
79                 group = self.groups["Template"]
80                 tile_href = "#square%d-tile%d" % (square.index, tile.index)
81                 ref = self.backend.svg.use(tile_href)
82                 ref['transform'] = svg_matrix
83                 group.add(ref)
84
85                 # Reuse the content to draw the final tetraflexagon
86                 group = self.groups["Tetraflexagon"]
87                 content_href = "#square%d-content" % square.index
88                 ref = self.backend.svg.use(content_href)
89                 ref['transform'] = svg_matrix
90                 ref['clip-path'] = "url(%s)" % (tile_href + '-clip-path')
91                 group.add(ref)
92
93     def draw_tile_template(self, tile, cx, cy, theta):
94         old_active_group = self.backend.active_group
95         group_name = "square%d-tile%d" % (tile.square.index, tile.index)
96         self.backend.active_group = self.groups[group_name]
97
98         super(SvgwriteTetraflexagonDiagram, self).draw_tile_template(tile, cx, cy, theta)
99
100         # The tile outline in the active group's element is the only polygon
101         # element, so get it and set its id so that it can be reused as
102         # a clip-path
103         for element in self.backend.active_group.elements:
104             if isinstance(element, svgwrite.shapes.Rect):
105                 element['id'] = group_name + "-outline"
106                 break
107
108         clip_path = self.backend.svg.clipPath(id=group_name + '-clip-path')
109         self.backend.svg.defs.add(clip_path)
110         ref = self.backend.svg.use('#%s-outline' % group_name)
111         clip_path.add(ref)
112
113         self.backend.active_group = old_active_group
114
115
116 def main():
117     width = 3508
118     height = 2480
119
120     x_border = width / 50
121     font_size = width / 80
122     stroke_width = width / 480
123
124     svg_backend = SvgwriteDiagram(width, height, font_size=font_size, stroke_width=stroke_width)
125     tetraflexagon = SvgwriteTetraflexagonDiagram(x_border, backend=svg_backend)
126     tetraflexagon.draw()
127     svg_backend.save_svg("inkscape-tetraflexagon-editor.svg")
128
129
130 if __name__ == "__main__":
131     main()