svgwrite_diagram: proper support for stroke_width
[flexagon-toolkit.git] / src / svg_hexaflexagon_editor.py
1 #!/usr/bin/env python3
2 #
3 # Draw an SVG hexaflexagon 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 from math import pi
21 import svgwrite
22
23 from diagram.svgwrite_diagram import SvgwriteDiagram
24 from flexagon.hexaflexagon_diagram import HexaflexagonDiagram
25
26
27 class SvgwriteHexaflexagonDiagram(HexaflexagonDiagram):
28     def __init__(self, *args, **kwargs):
29         super(SvgwriteHexaflexagonDiagram, self).__init__(*args, **kwargs)
30
31         svg = self.backend.svg
32
33         # create some layers and groups
34         layers = {
35             "Hexagons": svg.layer(label="Hexagons"),
36             "Hexaflexagon": svg.layer(label="Hexaflexagon"),
37             "Folding guide": svg.layer(label="Folding guide"),
38             "Template": svg.layer(label="Template")
39         }
40         for layer in layers.values():
41             svg.add(layer)
42
43         self.groups = layers
44
45         for hexagon in self.hexaflexagon.hexagons:
46             name = "hexagon%d-content" % hexagon.index
47             layer = svg.layer(id=name, label="Hexagon %d" % (hexagon.index + 1))
48             self.groups[name] = layer
49             layers['Hexagons'].add(layer)
50
51             for triangle in hexagon.triangles:
52                 name = "hexagon%d-triangle%d" % (hexagon.index, triangle.index)
53                 group = svg.g(id=name)
54                 self.groups[name] = group
55                 layers['Template'].add(group)
56
57     def draw(self):
58         for hexagon in self.hexaflexagon.hexagons:
59             cx, cy = self.get_hexagon_center(hexagon)
60
61             # Draw some default content
62             old_active_group = self.backend.active_group
63             self.backend.active_group = self.groups["hexagon%d-content" % hexagon.index]
64             self.backend.draw_regular_polygon(cx, cy, 6, self.hexagon_radius, pi / 6., fill_color=(0.5, 0.5, 0.5, 0.2), stroke_color=None)
65             self.backend.active_group = old_active_group
66
67             # Add folding guides
68             old_active_group = self.backend.active_group
69             self.backend.active_group = self.groups["Folding guide"]
70
71             for triangle in hexagon.triangles:
72                 cx, cy = self.get_triangle_center(triangle)
73                 theta = triangle.get_angle_in_hexagon()
74                 self.backend.draw_regular_polygon(cx, cy, 3, self.triangle_radius, theta, (0, 0, 0, 0.2))
75                 polygon = self.backend.active_group.elements[-1]
76                 polygon['id'] = "hexagon%d-triangle%d-folding" % (triangle.hexagon.index, triangle.index)
77
78             self.backend.active_group = old_active_group
79
80         # Draw the normal template for hexagons
81         for hexagon in self.hexaflexagon.hexagons:
82             self.draw_hexagon_template(hexagon)
83
84         # draw plan using references
85         for hexagon in self.hexaflexagon.hexagons:
86             for triangle in hexagon.triangles:
87                 m = self.get_triangle_transform(triangle)
88                 svg_matrix = "matrix(%f, %f, %f, %f, %f, %f)" % (m[0], m[3],
89                                                                  m[1], m[4],
90                                                                  m[2], m[5])
91
92                 # Reuse the hexagons triangle for the hexaflexagon template
93                 group = self.groups["Template"]
94                 triangle_href = "#hexagon%d-triangle%d" % (hexagon.index, triangle.index)
95                 ref = self.backend.svg.use(triangle_href)
96                 ref['transform'] = svg_matrix
97                 group.add(ref)
98
99                 # Reuse the folding guides
100                 group = self.groups["Folding guide"]
101                 folding_href = "#hexagon%d-triangle%d-folding" % (hexagon.index, triangle.index)
102                 ref = self.backend.svg.use(folding_href)
103                 ref['transform'] = svg_matrix
104                 group.add(ref)
105
106                 # Reuse the content to draw the final hexaflexagon
107                 group = self.groups["Hexaflexagon"]
108                 content_href = "#hexagon%d-content" % hexagon.index
109                 ref = self.backend.svg.use(content_href)
110                 ref['transform'] = svg_matrix
111                 ref['clip-path'] = "url(%s)" % (triangle_href + '-clip-path')
112                 group.add(ref)
113
114     def draw_triangle_template(self, triangle, cx, cy, theta):
115         old_active_group = self.backend.active_group
116         group_name = "hexagon%d-triangle%d" % (triangle.hexagon.index, triangle.index)
117         self.backend.active_group = self.groups[group_name]
118
119         super(SvgwriteHexaflexagonDiagram, self).draw_triangle_template(triangle, cx, cy, theta)
120
121         # The triangle outline in the active group's element is the only polygon
122         # element, so get it and set its id so that it can be reused as
123         # a clip-path
124         for element in self.backend.active_group.elements:
125             if isinstance(element, svgwrite.shapes.Polygon):
126                 element['id'] = group_name + "-outline"
127                 break
128
129         clip_path = self.backend.svg.clipPath(id=group_name + '-clip-path')
130         self.backend.svg.defs.add(clip_path)
131         ref = self.backend.svg.use('#%s-outline' % group_name)
132         clip_path.add(ref)
133
134         self.backend.active_group = old_active_group
135
136
137 def main():
138     width = 3508
139     height = 2480
140
141     x_border = width / 50
142     font_size = width / 80
143     stroke_width = width / 480
144
145     svg_backend = SvgwriteDiagram(width, height, font_size=font_size, stroke_width=stroke_width)
146     hexaflexagon = SvgwriteHexaflexagonDiagram(x_border, backend=svg_backend)
147     hexaflexagon.draw()
148     svg_backend.save_svg("inkscape-hexaflexagon-editor.svg")
149
150
151 if __name__ == "__main__":
152     main()