saveState and restoreState
Submitted by Jiujing Gu on Tue, 07/05/2011 - 17:34
The method drawBlock in MovieTimeBlocks contains under.saveState() and under.restoreState(). Why there aren't corresponding over.saveState() and over.restoreState()?
I tried to comment out under.saveState() and under.restoreState(). I didn't see any
difference in results. Did I overlook something?
Thanks in advance.
- Login to post comments
Content © 2010 1T3XT BVBA

Changing the state
Submitted by Bruno Lowagie on Wed, 07/06/2011 - 10:15.Good question!
The
saveState()andrestoreState()method are for theundercontent because we change the graphics state:under.setColorFill(color);.Suppose that you change the
colorto orange, and you would not usesaveState()/restoreState(), every shape you draw afterwards (including text) would be orange (unless you changed the state to another color).You don't see any difference, because we don't draw anything else, exept for the movie blocks for which we always set the color. The text is added to another layer (which adds
saveState()/restoreState()implicitly), so it's not affected by the color change.Summarized: you're correct when you say that
saveState()/restoreState()isn't needed in this case, butsaveState()/restoreState()are there because it's good practice to use them when you change the graphics state.They aren't needed for the
overcontent, because we construct a path (a rectangle) and we immediately stroke it. This doesn't change the graphics state, so there's no need for usingsaveState()/restoreState().