Skip to content

Commit

Permalink
Refactor stroke grouping into render_scribble() and fix eraser
Browse files Browse the repository at this point in the history
  • Loading branch information
Cimbali committed Jun 5, 2023
1 parent a83f5a9 commit 7d38f17
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions pympress/scribble.py
Original file line number Diff line number Diff line change
Expand Up @@ -452,17 +452,10 @@ def prerender(self):

draw = slice(self.next_render, -1 if self.scribble_drawing else None)

# Draw every stroke on a separate surface, then merge them all into the scribble cache
for color, width, points, pressure in self.scribble_list[draw]:
cairo_context.push_group()

cairo_context.set_line_cap(cairo.LINE_CAP_ROUND)
self.render_scribble(cairo_context, color, width * pen_scale_factor, [(x * ww, y * wh) for x, y in points],
pressure)

cairo_context.pop_group_to_source()
cairo_context.paint()

del cairo_context

self.next_render = draw.indices(len(self.scribble_list))[1]
Expand All @@ -481,6 +474,11 @@ def render_scribble(self, cairo_context, color, width, points, pressures):
if not points:
return

# Draw every stroke on a separate surface, then merge them all into the scribble cache
# Erasers do not have their own group as they are meant to interfere with strokes below
if color.alpha:
cairo_context.push_group()

# alpha == 0 -> Eraser mode
cairo_context.set_operator(cairo.OPERATOR_SOURCE if color.alpha else cairo.OPERATOR_CLEAR)
cairo_context.set_source_rgba(*color)
Expand All @@ -499,6 +497,10 @@ def render_scribble(self, cairo_context, color, width, points, pressures):
cairo_context.line_to(*points[-1])
cairo_context.stroke()

if color.alpha:
cairo_context.pop_group_to_source()
cairo_context.paint()


def draw_scribble(self, widget, cairo_context):
""" Perform the drawings by user.
Expand All @@ -511,6 +513,8 @@ def draw_scribble(self, widget, cairo_context):
ww, wh = widget.get_allocated_width(), widget.get_allocated_height()
cw, ch = self.scribble_cache.get_width(), self.scribble_cache.get_height()

cairo_context.push_group()

cairo_context.save()

cairo_context.scale(ww * scale / cw, wh * scale / ch)
Expand All @@ -519,8 +523,6 @@ def draw_scribble(self, widget, cairo_context):

cairo_context.restore()

cairo_context.push_group()

pen_scale_factor = max(ww / 900, wh / 900) # or sqrt of product
if self.scribble_drawing:
cairo_context.set_line_cap(cairo.LINE_CAP_ROUND)
Expand Down

0 comments on commit 7d38f17

Please sign in to comment.