| 1 |
|
| 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']: |
| 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 |
|
| 39 |
|
| 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 |
|
| 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 |
|
| 56 |
|
| 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 |
|
| 64 |
widths = self._build_widths(operation['path'], operation['line_width']) |
| 65 |
|
| 66 |
|
| 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 |
|
| 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 |
|
| 93 |
if p2 is not None: |
| 94 |
dists.append(self._get_dist(pt['x'], pt['y'], p2['x'], p2['y'])) |
| 95 |
else: |
| 96 |
|
| 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 |
|
| 103 |
|
| 104 |
widths.append(base_width * seg_width * 2) |
| 105 |
p2 = pt |
| 106 |
|
| 107 |
|
| 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: |
| 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 |
|