3 # Draw an SVG tetraflexagon which can be edited live in Inkscape.
5 # Copyright (C) 2018 Antonio Ospite <ao2@ao2.it>
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.
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.
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/>.
22 from diagram.svgwrite_diagram import SvgwriteDiagram
23 from flexagon.tetraflexagon_diagram import TetraflexagonDiagram
26 class SvgwriteTetraflexagonDiagram(TetraflexagonDiagram):
27 def __init__(self, *args, **kwargs):
28 super(SvgwriteTetraflexagonDiagram, self).__init__(*args, **kwargs)
30 svg = self.backend.svg
32 # create some layers and groups
34 "Squares": svg.layer(label="Squares"),
35 "Tetraflexagon": svg.layer(label="Tetraflexagon"),
36 "Template": svg.layer(label="Template")
38 for layer in layers.values():
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)
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)
56 for square in self.tetraflexagon.squares:
57 cx, cy = self.get_square_center(square)
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
66 self.backend.active_group = old_active_group
68 # Draw the normal template for squares
69 for square in self.tetraflexagon.squares:
70 self.draw_square_template(square)
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],
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
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')
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]
100 super(SvgwriteTetraflexagonDiagram, self).draw_tile_template(tile, cx, cy, theta)
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
105 for element in self.backend.active_group.elements:
106 if isinstance(element, svgwrite.shapes.Rect):
107 element['id'] = group_name + "-outline"
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)
115 self.backend.active_group = old_active_group
122 x_border = width / 50
123 font_size = width / 80
124 stroke_width = width / 480
126 svg_backend = SvgwriteDiagram(width, height, font_size=font_size, stroke_width=stroke_width)
127 tetraflexagon = SvgwriteTetraflexagonDiagram(x_border, backend=svg_backend)
129 svg_backend.save_svg("inkscape-tetraflexagon-editor.svg")
132 if __name__ == "__main__":