ce2794b3a3a0c349a3e074e2a1098f79b3d6c60f
[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             self.backend.active_group = old_active_group
67
68         # Draw the normal template for squares
69         for square in self.tetraflexagon.squares:
70             self.draw_square_template(square)
71
72         # draw plan using references
73         for square in self.tetraflexagon.squares:
74             for tile in square.tiles:
75                 m = self.get_tile_transform(tile)
76                 svg_matrix = "matrix(%f, %f, %f, %f, %f, %f)" % (m[0], m[3],
77                                                                  m[1], m[4],
78                                                                  m[2], m[5])
79
80                 # Reuse the squares tile for the tetraflexagon template
81                 group = self.groups["Template"]
82                 tile_href = "#square%d-tile%d" % (square.index, tile.index)
83                 ref = self.backend.svg.use(tile_href)
84                 ref['transform'] = svg_matrix
85                 group.add(ref)
86
87                 # Reuse the content to draw the final tetraflexagon
88                 group = self.groups["Tetraflexagon"]
89                 content_href = "#square%d-content" % square.index
90                 ref = self.backend.svg.use(content_href)
91                 ref['transform'] = svg_matrix
92                 ref['clip-path'] = "url(%s)" % (tile_href + '-clip-path')
93                 group.add(ref)
94
95     def draw_tile_template(self, tile, cx, cy, theta):
96         old_active_group = self.backend.active_group
97         group_name = "square%d-tile%d" % (tile.square.index, tile.index)
98         self.backend.active_group = self.groups[group_name]
99
100         super(SvgwriteTetraflexagonDiagram, self).draw_tile_template(tile, cx, cy, theta)
101
102         # The tile outline in the active group's element is the only polygon
103         # element, so get it and set its id so that it can be reused as
104         # a clip-path
105         for element in self.backend.active_group.elements:
106             if isinstance(element, svgwrite.shapes.Rect):
107                 element['id'] = group_name + "-outline"
108                 break
109
110         clip_path = self.backend.svg.clipPath(id=group_name + '-clip-path')
111         self.backend.svg.defs.add(clip_path)
112         ref = self.backend.svg.use('#%s-outline' % group_name)
113         clip_path.add(ref)
114
115         self.backend.active_group = old_active_group
116
117 def main():
118     width = 3508
119     height = 2480
120
121     x_border = width / 50
122     font_size = width / 80
123     stroke_width = width / 480
124
125     svg_backend = SvgwriteDiagram(width, height, font_size=font_size, stroke_width=stroke_width)
126     tetraflexagon = SvgwriteTetraflexagonDiagram(x_border, backend=svg_backend)
127     tetraflexagon.draw()
128     svg_backend.save_svg("inkscape-tetraflexagon-editor.svg")
129
130
131 if __name__ == "__main__":
132     main()