Debian Codegraph @

src/tools/classic_tools/brushes/brush_nib.py

raw symbols SCIP
1 # Licensed under GPL3 https://github.com/maoschanz/drawing/blob/master/LICENSE
2
3 import cairo
4 from .abstract_brush import AbstractBrush
5
6 class BrushNib(AbstractBrush):
7 __gtype_name__ = 'BrushNib'
8
9 def _get_tips(self, use_pressure, brush_direction):
10 label_width = _("Calligraphic nib") + " - "
11 if use_pressure:
12 label_width += _("Width depends on the stylus pressure")
13 else:
14 label_width += _("Width depends on the line orientation")
15
16 label_direction = _("Calligraphic nib") + " - "
17 if brush_direction == 'right':
18 label_direction += _("Right-handed nib")
19 elif brush_direction == 'vertical':
20 label_direction += _("Vertical nib")
21 elif brush_direction == 'left':
22 label_direction += _("Left-handed nib")
23 else: # brush_direction == 'horizontal':
24 label_direction += _("Horizontal nib")
25
26 return [label_width, label_direction]
27
28 def draw_preview(self, operation, cairo_context):
29 cairo_context.set_line_cap(cairo.LineCap.BUTT)
30 cairo_context.set_line_join(cairo.LineJoin.ROUND)
31 cairo_context.set_source_rgba(*operation['rgba'])
32 super().draw_preview(operation, cairo_context)
33
34 ############################################################################
35
36 def do_brush_operation(self, cairo_context, operation):
37 """Straight-shaped brush, like an orientable feather pen. The width is
38 pressure-sensitive but otherwise it's NOT speed sensitive."""
39
40 if operation['is_preview']: # Previewing helps performance & debug
41 return self.draw_preview(operation, cairo_context)
42
43 self.operation_on_mask(operation, cairo_context)
44
45 def do_masked_brush_op(self, cairo_context, operation):
46 # cairo_context.set_line_cap(cairo.LineCap.BUTT)
47 # cairo_context.set_line_join(cairo.LineJoin.BEVEL)
48 # cairo_context.set_line_width(1)
49
50 line_width = operation['line_width'] / 2
51 two_ways_path = []
52
53 if operation['nib_dir'] == 'left':
54 feather_def = {'x': 1, 'y': 1}
55 elif operation['nib_dir'] == 'horizontal':
56 feather_def = {'x': 1, 'y': 0}
57 elif operation['nib_dir'] == 'vertical':
58 feather_def = {'x': 0, 'y': 1}
59 else: # operation['nib_dir'] == 'right':
60 feather_def = {'x': 1, 'y': -1}
61
62 dx_base = feather_def['x'] * line_width
63 dy_base = feather_def['y'] * line_width
64 use_pressure = operation['path'][0]['p'] is not None
65 if not use_pressure:
66 dx1 = dx_base
67 dy1 = dy_base
68 dx2 = dx_base
69 dy2 = dy_base
70
71 former_point = None
72 for current_point in operation['path']:
73 if former_point is not None:
74 if use_pressure:
75 dx1 = dx_base * former_point['p']
76 dy1 = dy_base * former_point['p']
77 dx2 = dx_base * current_point['p']
78 dy2 = dy_base * current_point['p']
79 cairo_context.new_path()
80 x = current_point['x'] + dx2
81 y = current_point['y'] + dy2
82 cairo_context.move_to(x, y)
83 x = current_point['x'] + dx2 * -1
84 y = current_point['y'] + dy2 * -1
85 cairo_context.line_to(x, y)
86 x = former_point['x'] + dx1 * -1
87 y = former_point['y'] + dy1 * -1
88 cairo_context.line_to(x, y)
89 x = former_point['x'] + dx1
90 y = former_point['y'] + dy1
91 cairo_context.line_to(x, y)
92 # cairo_context.stroke_preserve() # TODO trouver autre chose
93 cairo_context.fill()
94 former_point = current_point
95
96 ############################################################################
97 ################################################################################
98