gg
gg copied to clipboard
Context.Pop() does not restore to previous state
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.
@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.
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.
@dsamarin Anyway, in the meantime you can simply use ResetClip instead of Push/Pop.
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)