Debian Codegraph @

src/tools/classic_tools/brushes/brush_simple.py

raw symbols SCIP
1 # Licensed under GPL3 https://github.com/maoschanz/drawing/blob/master/LICENSE
2
3 import cairo, math
4 from gi.repository import Gdk
5 from .abstract_brush import AbstractBrush
6 from .utilities_paths import utilities_smooth_path
7
8 class BrushSimple(AbstractBrush):
9 __gtype_name__ = 'BrushSimple'
10
11 def _get_tips(self, use_pressure, brush_direction):
12 label = _("Simple brush") + " - "
13 if use_pressure:
14 label += _("Width depends on the stylus pressure")
15 else:
16 label += _("Width depends on the mouse speed")
17 return [label]
18
19 def draw_preview(self, operation, cairo_context):
20 cairo_context.set_line_cap(cairo.LineCap.ROUND)
21 cairo_context.set_line_join(cairo.LineJoin.ROUND)
22 cairo_context.set_source_rgba(*operation['rgba'])
23 super().draw_preview(operation, cairo_context)
24
25 ############################################################################
26
27 def do_brush_operation(self, cairo_context, operation):
28 """Brush with dynamic width, where the variation of width is drawn by a
29 succession of segments. If pressure is detected, the width is pressure-
30 sensitive, otherwise it's speed-sensitive (with a heavy ponderation to
31 make it less ugly)."""
32
33 if operation['is_preview']: # Previewing helps performance & debug
34 operation['line_width'] = max(1, int(operation['line_width'] * 0.8))
35 return self.draw_preview(operation, cairo_context)
36
37 if len(operation['path']) < 3:
38 # XXX minimum 3 points to get minimum 2 segments to avoid "list
39 # index out of range" errors when running the for loops
40 return
41
42 self.operation_on_mask(operation, cairo_context)
43
44 def do_masked_brush_op(self, cairo_context, operation):
45 cairo_context.set_line_cap(cairo.LineCap.ROUND)
46 cairo_context.set_line_join(cairo.LineJoin.ROUND)
47
48 # Build a raw path with lines between the points
49 cairo_context.new_path()
50 for pt in operation['path']:
51 cairo_context.line_to(pt['x'], pt['y'])
52 raw_path = cairo_context.copy_path()
53
54 if operation['smooth']:
55 # When the zoom is less than 400% (no great precision required by
56 # the user), this "raw" path is smoothed.
57 cairo_context.new_path()
58 utilities_smooth_path(cairo_context, raw_path)
59 smoothed_path = cairo_context.copy_path()
60 else:
61 smoothed_path = raw_path
62
63 # Build an array with all the widths for each segment
64 widths = self._build_widths(operation['path'], operation['line_width'])
65
66 # Run through the path to manually draw each segment with its width
67 i = 0
68 cairo_context.new_path()
69 for segment in smoothed_path:
70 i = i + 1
71 ok, future_x, future_y = self._future_point(segment)
72 if not ok:
73 cairo_context.move_to(future_x, future_y)
74 continue
75 current_x, current_y = cairo_context.get_current_point()
76 cairo_context.set_line_width(widths[i - 1])
77 self._add_segment(cairo_context, segment)
78 cairo_context.stroke()
79 cairo_context.move_to(future_x, future_y)
80
81 ############################################################################
82 # Private methods ##########################################################
83
84 def _build_widths(self, manual_path, base_width):
85 """Build an array of widths from the raw data, either using the value of
86 the pressure or based on the estimated speed of the movement."""
87 widths = []
88 dists = []
89 p2 = None
90 for pt in manual_path:
91 if pt['p'] is None:
92 # No data about pressure
93 if p2 is not None:
94 dists.append(self._get_dist(pt['x'], pt['y'], p2['x'], p2['y']))
95 else:
96 # There are data about pressure
97 if p2 is not None:
98 if p2['p'] == 0 or pt['p'] == 0:
99 seg_width = 0
100 else:
101 seg_width = (p2['p'] + pt['p']) / 2
102 # A segment whose 2 points have a 50% pressure shall have a
103 # width of "100%" of the base_width, so "base * mean * 2"
104 widths.append(base_width * seg_width * 2)
105 p2 = pt
106
107 # If nothing in widths, it has to be filled from dists
108 if len(widths) == 0:
109 min_dist = min(dists)
110 max_dist = max(dists)
111 temp_width = 0
112 for dist in dists:
113 new_width = 1 + int(base_width / max(1, 0.05 * dist))
114 if temp_width == 0:
115 temp_width = (new_width + base_width) / 2
116 else:
117 temp_width = (new_width + temp_width + temp_width) / 3
118 width = max(1, int(temp_width))
119 widths.append(width)
120
121 return widths
122
123 def _add_segment(self, cairo_context, pts):
124 if pts[0] == cairo.PathDataType.CURVE_TO:
125 cairo_context.curve_to(pts[1][0], pts[1][1], pts[1][2], pts[1][3], \
126 pts[1][4], pts[1][5])
127 elif pts[0] == cairo.PathDataType.LINE_TO:
128 cairo_context.line_to(pts[1][0], pts[1][1])
129
130 def _future_point(self, pts):
131 if pts[0] == cairo.PathDataType.CURVE_TO:
132 return True, pts[1][4], pts[1][5]
133 elif pts[0] == cairo.PathDataType.LINE_TO:
134 return True, pts[1][0], pts[1][1]
135 else: # all paths start with a cairo.PathDataType.MOVE_TO
136 return False, pts[1][0], pts[1][1]
137
138 def _get_dist(self, x1, y1, x2, y2):
139 dist2 = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)
140 return math.sqrt(dist2)
141
142 ############################################################################
143 ################################################################################
144