Debian Codegraph @

src/tools/classic_tools/brushes/brush_hairy.py

raw symbols SCIP
1 # Licensed under GPL3 https://github.com/maoschanz/drawing/blob/master/LICENSE
2
3 import cairo, random
4 from .abstract_brush import AbstractBrush
5
6 class BrushHairy(AbstractBrush):
7 __gtype_name__ = 'BrushHairy'
8
9 def _get_tips(self, use_pressure, brush_direction):
10 label = _("Hairy brush")
11 if use_pressure:
12 label += " - " + _("Width depends on the stylus pressure")
13 return [label]
14
15 def draw_preview(self, operation, cairo_context):
16 cairo_context.set_line_cap(cairo.LineCap.ROUND)
17 cairo_context.set_line_join(cairo.LineJoin.BEVEL)
18 cairo_context.set_source_rgba(*operation['rgba'])
19 super().draw_preview(operation, cairo_context)
20
21 ############################################################################
22
23 def do_brush_operation(self, cairo_context, operation):
24 """Straight-shaped brush, like an orientable feather pen. The width is
25 pressure-sensitive but otherwise it's NOT speed sensitive."""
26
27 if operation['is_preview']: # Previewing helps performance & debug
28 return self.draw_preview(operation, cairo_context)
29
30 self.operation_on_mask(operation, cairo_context)
31
32 def do_masked_brush_op(self, cairo_context, operation):
33 cairo_context.set_line_cap(cairo.LineCap.ROUND)
34 cairo_context.set_line_join(cairo.LineJoin.ROUND)
35 cairo_context.set_line_width(1)
36
37 line_width = max(1, int(operation['line_width'] / 2))
38
39 matrix = []
40 random.seed(1) # this hardcoded seed avoids the hairs changing their
41 # positions when the user undoes an following operation
42 for i in range(line_width * 2):
43 matrix.append({
44 'dx': random.randint(-1 * line_width, line_width),
45 'dy': random.randint(-1 * line_width, line_width)
46 })
47 # TODO other kinds of loops to define the matrix?
48
49 use_pressure = operation['path'][0]['p'] is not None
50 pressure = 1
51 for hair in matrix:
52 cairo_context.new_path()
53 for point in operation['path']:
54 if use_pressure and point['p'] > 0:
55 pressure = point['p'] * 2
56 px = point['x'] + hair['dx'] * pressure
57 py = point['y'] + hair['dy'] * pressure
58 cairo_context.line_to(px, py)
59 cairo_context.stroke()
60
61 ############################################################################
62 ################################################################################
63