1# Licensed under GPL3 https://github.com/maoschanz/drawing/blob/master/LICENSE
2
3import cairo
4
5class AbstractBrush():
6	__gtype_name__ = 'AbstractBrush'
7
8	def __init__(self, brush_id, brush_tool, *args):
9		self._id = brush_id
10		self._tool = brush_tool
11
12	def _get_status(self, use_pressure, brush_direction):
13		return _("Brush")
14
15	############################################################################
16
17	def draw_preview(self, operation, cairo_context):
18		cairo_context.set_operator(operation['operator'])
19		cairo_context.set_line_width(operation['line_width'])
20		cairo_context.new_path()
21		for pt in operation['path']:
22			cairo_context.line_to(pt['x'], pt['y'])
23		cairo_context.stroke()
24
25	def operation_on_mask(self, operation, original_context):
26		if operation['operator'] == cairo.Operator.CLEAR \
27		or operation['operator'] == cairo.Operator.SOURCE:
28			# When using CLEAR or SOURCE, we don't need to use a temporary
29			# surface, and actually we can't because using it as a mask would
30			# just erase the entire image.
31			original_context.set_operator(operation['operator'])
32			c = operation['rgba']
33			original_context.set_source_rgba(c.red, c.green, c.blue, c.alpha)
34			self.do_masked_brush_op(original_context, operation)
35			return
36
37		# Creation of a blank surface with a new context; each brush decides how
38		# to apply the options set by the user (`operation`), except for the
39		# operator which has to be "wrongly" set to SOURCE.
40		w = self._tool.get_surface().get_width()
41		h = self._tool.get_surface().get_height()
42		mask = cairo.ImageSurface(cairo.Format.ARGB32, w, h)
43		context2 = cairo.Context(mask)
44
45		if operation['antialias']:
46			antialias = cairo.Antialias.DEFAULT
47		else:
48			antialias = cairo.Antialias.NONE
49		context2.set_antialias(antialias)
50
51		context2.set_operator(cairo.Operator.SOURCE)
52		rgba = operation['rgba']
53		context2.set_source_rgba(rgba.red, rgba.green, rgba.blue, rgba.alpha)
54
55		self.do_masked_brush_op(context2, operation)
56
57		# Paint the surface onto the actual image with the chosen operator
58		original_context.set_operator(operation['operator'])
59		original_context.set_source_surface(mask)
60		original_context.paint()
61
62	############################################################################
63
64	def do_brush_operation(self, cairo_context, operation):
65		pass
66
67	def do_masked_brush_op(self, cairo_context, operation):
68		pass
69
70	############################################################################
71################################################################################
72
73