3 # A diagram class to draw in Gimp
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/>.
23 from .diagram import Diagram
26 class GimpDiagram(Diagram):
27 def __init__(self, width, height, image, layer, **kwargs):
28 super(GimpDiagram, self).__init__(width, height, **kwargs)
33 def color_to_rgba(self, color):
34 color = super(GimpDiagram, self).color_to_rgba(color)
36 warnings.warn("Warning, trasparent colors are not supported", stacklevel=4)
43 def _polygon_path(self, verts):
44 path = pdb.gimp_vectors_new(self.image, "")
47 strokeid = pdb.gimp_vectors_bezier_stroke_new_moveto(path,
50 pdb.gimp_vectors_bezier_stroke_lineto(path, strokeid, v[0], v[1])
52 pdb.gimp_vectors_stroke_close(path, strokeid)
56 def _fill_path(self, path, fill_color):
58 orig_foreground = pdb.gimp_context_get_foreground()
59 orig_selection = pdb.gimp_selection_save(self.image)
61 color = self.color_to_rgba(fill_color)
62 pdb.gimp_context_set_foreground(color)
64 pdb.gimp_image_select_item(self.image, CHANNEL_OP_REPLACE, path)
66 pdb.gimp_edit_fill(self.drawable, FOREGROUND_FILL)
68 pdb.gimp_selection_load(orig_selection)
69 pdb.gimp_image_remove_channel(self.image, orig_selection)
70 pdb.gimp_context_set_foreground(orig_foreground)
72 def _stroke_path(self, path, stroke_color):
74 orig_paint_method = pdb.gimp_context_get_paint_method()
75 orig_foreground = pdb.gimp_context_get_foreground()
76 orig_brush = pdb.gimp_context_get_brush()
78 pdb.gimp_context_set_paint_method('gimp-paintbrush')
80 color = self.color_to_rgba(stroke_color)
81 pdb.gimp_context_set_foreground(color)
82 pdb.gimp_context_set_brush("1. Pixel")
83 pdb.gimp_context_set_brush_size(self.stroke_width)
85 pdb.gimp_edit_stroke_vectors(self.drawable, path)
87 pdb.gimp_context_set_brush(orig_brush)
88 pdb.gimp_context_set_foreground(orig_foreground)
89 pdb.gimp_context_set_paint_method(orig_paint_method)
91 def draw_star_by_verts(self, cx, cy, verts, stroke_color=(0, 0, 0)):
92 path = pdb.gimp_vectors_new(self.image, "")
95 strokeid = pdb.gimp_vectors_bezier_stroke_new_moveto(path, cx, cy)
96 pdb.gimp_vectors_bezier_stroke_lineto(path, strokeid, v[0], v[1])
98 pdb.gimp_image_insert_vectors(self.image, path, None, -1)
99 pdb.gimp_image_set_active_vectors(self.image, path)
101 self._stroke_path(path, stroke_color)
103 pdb.gimp_image_remove_vectors(self.image, path)
105 def draw_polygon_by_verts(self, verts,
106 stroke_color=(0, 0, 0),
108 path = self._polygon_path(verts)
110 pdb.gimp_image_insert_vectors(self.image, path, None, -1)
111 pdb.gimp_image_set_active_vectors(self.image, path)
113 self._fill_path(path, fill_color)
114 self._stroke_path(path, stroke_color)
116 pdb.gimp_image_remove_vectors(self.image, path)
118 def copy_polygon(self, src_drawable, verts, dest_drawable, matrix):
119 # flatten the verts list to be accepted by gimp_image_select_polygon()
120 segs = [coord for v in verts for coord in v]
121 pdb.gimp_image_select_polygon(self.image, CHANNEL_OP_REPLACE, len(segs), segs)
123 pdb.gimp_edit_copy(src_drawable)
124 floating_layer = pdb.gimp_edit_paste(dest_drawable, FALSE)
126 pdb.gimp_item_transform_matrix(floating_layer,
127 matrix[0], matrix[1], matrix[2],
128 matrix[3], matrix[4], matrix[5],
129 matrix[6], matrix[7], matrix[8])
131 pdb.gimp_floating_sel_anchor(floating_layer)
133 def draw_centered_text(self, cx, cy, text, theta, color,
134 align_baseline=False,
135 bb_stroke_color=None,
137 font_name = "Georgia"
139 width, height, ascent, descent = pdb.gimp_text_get_extents_fontname(text,
144 tx = cx - width / 2.0
145 ty = cy - height / 2.0
147 floating_selection = pdb.gimp_text_fontname(self.image, self.drawable,
148 tx, ty, text, 0, True,
153 text_color = self.color_to_rgba(color)
154 pdb.gimp_text_layer_set_color(floating_selection, text_color)
156 pdb.gimp_item_transform_rotate(floating_selection, theta, FALSE, cx, cy)
157 pdb.gimp_floating_sel_anchor(floating_selection)
160 warnings.warn("The align_baseline option has not been implemented yet.")
162 if bb_stroke_color or bb_fill_color:
163 warnings.warn("Drawing the bounding box has not been implemented yet.")