svg_tetraflexagon_editor: remove duplicate code
[flexagon-toolkit.git] / src / gimp_hexaflexagon.py
1 #!/usr/bin/env python
2
3 '''
4 Gimp plugin "Hexaflexagon"
5
6 Create Hexaflexagons.
7
8 Copyright (C) 2018  Antonio Ospite <ao2@ao2.it>
9
10 This program is free software: you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation, either version 3 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program.  If not, see <http://www.gnu.org/licenses/>.
22 '''
23
24 # The plugin is inspired to flexagon.scm by Andrea Rossetti:
25 # http://andrear.altervista.org/home/gimp_flexagon.php
26 #
27 # It has been rewritten in python in the hope to simplify it and attract more
28 # contributors.
29
30 from gimpfu import *
31
32 from diagram.gimp_diagram import GimpDiagram
33 from flexagon import HexaflexagonDiagram
34
35 gettext.install("gimp20-python", gimp.locale_directory, unicode=True)
36
37
38 def build_plan(template_layer, hexaflexagon_layer, diagram):
39     for hexagon in diagram.hexaflexagon.hexagons:
40         for triangle in hexagon.triangles:
41             verts = diagram.get_triangle_verts(triangle)
42
43             matrix = diagram.get_triangle_transform(triangle)
44             diagram.backend.copy_polygon(template_layer, verts, hexaflexagon_layer, matrix)
45
46
47 def hexaflexagon_main(image):
48     x_border = image.width / 50
49     font_size = image.width / 80
50     stroke_width = image.width / 480
51
52     template_layer_name = "HexaflexagonTemplate"
53     content_layer_name = "Hexagons"
54     hexaflexagon_layer_name = "Hexaflexagon"
55
56     message = "Draw the hexagons content into the '%s' layer.\n" % content_layer_name
57     message += "Then call this script again."
58
59     pdb.gimp_image_undo_group_start(image)
60     pdb.gimp_context_push()
61     pdb.gimp_context_set_defaults()
62
63     template_layer = pdb.gimp_image_get_layer_by_name(image,
64                                                       template_layer_name)
65
66     content_layer = pdb.gimp_image_get_layer_by_name(image,
67                                                      content_layer_name)
68     if not content_layer:
69         content_layer = pdb.gimp_layer_new(image, image.width, image.height,
70                                            RGBA_IMAGE, content_layer_name,
71                                            100, NORMAL_MODE)
72         if template_layer:
73             template_layer_position = pdb.gimp_image_get_item_position(image, template_layer)
74             content_layer_position = template_layer_position - 1
75             pdb.gimp_image_insert_layer(image, content_layer, None, content_layer_position)
76
77             pdb.gimp_message(message)
78
79             pdb.gimp_context_pop()
80             pdb.gimp_image_undo_group_end(image)
81             return
82         else:
83             content_layer_position = -1
84             pdb.gimp_image_insert_layer(image, content_layer, None, content_layer_position)
85
86     if not template_layer:
87         template_layer = pdb.gimp_layer_new(image, image.width, image.height,
88                                             RGBA_IMAGE, template_layer_name,
89                                             100, NORMAL_MODE)
90         pdb.gimp_image_insert_layer(image, template_layer, None, -1)
91
92         gimp_backend = GimpDiagram(image.width, image.height,
93                                    image, template_layer,
94                                    font_size=font_size, stroke_width=stroke_width)
95         diagram = HexaflexagonDiagram(x_border, backend=gimp_backend)
96         diagram.draw_template()
97
98         pdb.gimp_message(message)
99         pdb.gimp_context_pop()
100         pdb.gimp_image_undo_group_end(image)
101         return
102
103     hexaflexagon_layer = pdb.gimp_image_get_layer_by_name(image,
104                                                           hexaflexagon_layer_name)
105     if hexaflexagon_layer:
106         pdb.gimp_message("There is already a generated hexaflexagon.")
107         pdb.gimp_context_pop()
108         pdb.gimp_image_undo_group_end(image)
109         return
110
111     hexaflexagon_layer = pdb.gimp_layer_new(image, image.width,
112                                             image.height, RGBA_IMAGE,
113                                             hexaflexagon_layer_name, 100,
114                                             NORMAL_MODE)
115     pdb.gimp_image_insert_layer(image, hexaflexagon_layer, None, -1)
116
117     gimp_backend = GimpDiagram(image.width, image.height,
118                                image, content_layer,
119                                font_size=font_size, stroke_width=stroke_width)
120     diagram = HexaflexagonDiagram(x_border, backend=gimp_backend)
121     build_plan(content_layer, hexaflexagon_layer, diagram)
122
123     pdb.gimp_context_pop()
124     pdb.gimp_image_undo_group_end(image)
125
126
127 if __name__ == "__main__":
128     register(
129         "python_fu_hexaflexagon",
130         N_("Create Hexaflexagons"),
131         "Create Hexaflexagons",
132         "Antonio Ospite <ao2@ao2.it>",
133         "Copyright (C) 2018  Antonio Ospite <ao2@ao2.it>",
134         "2015",
135         N_("Hexaflexagon..."),
136         "RGB*, GRAY*",
137         [
138             (PF_IMAGE, "image", "Input image", None),
139         ],
140         [],
141         hexaflexagon_main,
142         menu="<Image>/Filters/Render",
143         domain=("gimp20-python", gimp.locale_directory)
144     )
145
146     main()