README.md: mention also the tetraflexagon example
[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             "Backfaces": svg.layer(label="Backfaces"),
36             "Tetraflexagon": svg.layer(label="Tetraflexagon"),
37             "Template": svg.layer(label="Template")
38         }
39         for layer in layers.values():
40             svg.add(layer)
41
42         self.groups = layers
43
44         for square in self.tetraflexagon.squares:
45             name = "square%d-content" % square.index
46             layer = svg.layer(id=name, label="Square %d" % (square.index + 1))
47             self.groups[name] = layer
48             layers['Squares'].add(layer)
49
50             for tile in square.tiles:
51                 name = "square%d-tile%d" % (square.index, tile.index)
52                 group = svg.g(id=name)
53                 self.groups[name] = group
54                 layers['Template'].add(group)
55
56     def draw(self):
57         for square in self.tetraflexagon.squares:
58             cx, cy = self.get_square_center(square)
59
60             # Draw some default content
61             old_active_group = self.backend.active_group
62             self.backend.active_group = self.groups["square%d-content" % square.index]
63             self.backend.draw_rect_from_center(cx, cy, self.square_side, self.square_side, 0,
64                                                fill_color=(0.5, 0.5, 0.5, 0.2))
65             self.backend.active_group = old_active_group
66
67         # Draw the normal template for squares
68         for square in self.tetraflexagon.squares:
69             self.draw_square_template(square)
70
71         # draw plan using references
72         for square in self.tetraflexagon.squares:
73             for tile in square.tiles:
74                 m = self.get_tile_transform(tile)
75                 svg_matrix = "matrix(%f, %f, %f, %f, %f, %f)" % (m[0], m[3],
76                                                                  m[1], m[4],
77                                                                  m[2], m[5])
78
79                 # Reuse the squares tile for the tetraflexagon template
80                 group = self.groups["Template"]
81                 tile_href = "#square%d-tile%d" % (square.index, tile.index)
82                 ref = self.backend.svg.use(tile_href)
83                 ref['transform'] = svg_matrix
84                 group.add(ref)
85
86                 # Reuse the content to draw the final tetraflexagon
87                 group = self.groups["Tetraflexagon"]
88                 content_href = "#square%d-content" % square.index
89                 ref = self.backend.svg.use(content_href)
90                 ref['transform'] = svg_matrix
91                 ref['clip-path'] = "url(%s)" % (tile_href + '-clip-path')
92                 group.add(ref)
93
94         # A tri-tetraflexagon only have one visible backface, the first one.
95         group = self.groups["Backfaces"]
96         for tile in self.tetraflexagon.squares[0].tiles:
97             m = self.get_backface_tile_transform(tile)
98             svg_matrix = "matrix(%f, %f, %f, %f, %f, %f)" % (m[0], m[3],
99                                                              m[1], m[4],
100                                                              m[2], m[5])
101             tile_href = "#square0-tile%d" % tile.index
102             ref = self.backend.svg.use("#square0-content")
103             ref['transform'] = svg_matrix
104             ref['clip-path'] = "url(%s)" % (tile_href + '-clip-path')
105             group.add(ref)
106
107     def draw_tile_template(self, tile, cx, cy, theta):
108         old_active_group = self.backend.active_group
109         group_name = "square%d-tile%d" % (tile.square.index, tile.index)
110         self.backend.active_group = self.groups[group_name]
111
112         super(SvgwriteTetraflexagonDiagram, self).draw_tile_template(tile, cx, cy, theta)
113
114         # The tile outline in the active group's element is the only rectangle
115         # element, so get it and set its id so that it can be reused as
116         # a clip-path
117         for element in self.backend.active_group.elements:
118             if isinstance(element, svgwrite.shapes.Rect):
119                 element['id'] = group_name + "-outline"
120                 break
121
122         clip_path = self.backend.svg.clipPath(id=group_name + '-clip-path')
123         self.backend.svg.defs.add(clip_path)
124         ref = self.backend.svg.use('#%s-outline' % group_name)
125         clip_path.add(ref)
126
127         self.backend.active_group = old_active_group
128
129
130 def main():
131     width = 3508
132     height = 2480
133
134     x_border = width / 50
135     font_size = width / 80
136     stroke_width = width / 480
137
138     svg_backend = SvgwriteDiagram(width, height, font_size=font_size, stroke_width=stroke_width)
139     tetraflexagon = SvgwriteTetraflexagonDiagram(x_border, backend=svg_backend)
140     tetraflexagon.draw()
141     svg_backend.save_svg("inkscape-tetraflexagon-editor.svg")
142
143
144 if __name__ == "__main__":
145     main()