gg icon indicating copy to clipboard operation
gg copied to clipboard

Context.Pop() does not restore to previous state

Open ghost opened this issue 8 years ago • 4 comments

When applying a clipping mask after saving the current context state, calling context.Pop() does not revert to the previous clipping mask. (It appears that this does not revert anything, but the effect on the mask is more obvious than other side effects.) Objects drawn after the call to Pop() are still affected by the mask.

Demonstration of issue. PR on the way.

ghost avatar Oct 01 '17 02:10 ghost

@fogleman Do you have any comments on the pull request for this issue? I would love to use this library for my project because the API is very well-written. This problem is coming up for me when I use the recursive Push(), DrawRectangle(), Clip(), render(), Pop() pattern for a scene graph.

dsamarin avatar May 14 '18 02:05 dsamarin

One thing to note is that the current path is not affected by Push/Pop. This behavior is borrowed from Cairo. From their docs:

One interesting change in cairo is that the path is no longer
part of the graphics state managed by
cairo_save/restore. This allows functions to construct paths
without interfering with the graphics state. But it prevents
the traditional idiom for fill-and-stroke:

	cairo_save; cairo_fill; cairo_restore; cairo_stroke

Instead we know have alternate versions cairo cairo_fill,
cairo_stroke, and cairo_clip that preserve the current path
rather than consuming it. So the idiom now becomes simply:

	cairo_fill_preserve; cairo_stroke

I seem to have treated the clipping region the same way, but I can't remember why. I'd be okay with changing that, but I think @derekschaab 's PR breaks the behavior mentioned above.

fogleman avatar May 14 '18 14:05 fogleman

@dsamarin Anyway, in the meantime you can simply use ResetClip instead of Push/Pop.

fogleman avatar May 14 '18 14:05 fogleman

ResetClip doesn't work a hierarchy of clipping paths: ResetClip will reset all clipping paths, instead of the just the clipping path(s) set after the last Push. Instead this does seems to work better:

d.Canvas.Push()
clip := d.Canvas.AsMask()
defer d.Canvas.Pop()
defer d.Canvas.SetMask(clip)

markusheukelom avatar Oct 14 '22 15:10 markusheukelom