1. grestoreall
Restores graphics state to bottommost saved level.
1.2. Stack Effects
| Level | Object |
|---|---|
(empty) |
– |
| Level | Object |
|---|---|
(empty) |
Graphics state stack cleared to bottommost level |
1.3. Description
grestoreall performs the equivalent of executing grestore repeatedly until there are no saved graphics states remaining on the graphics state stack. After executing grestoreall, the current graphics state is the bottommost one that was saved by the first gsave or by save.
This operator:
-
Restores graphics state to the initial level
-
Clears all intermediate saved states
-
Resets the current path to empty
-
Does not affect the operand or dictionary stacks
grestoreall is particularly useful for error recovery and cleanup, ensuring a known graphics state regardless of how many gsave operations have been performed.
1.5. Examples
% May have unknown gsave depth
drawComplexFigure % Unknown number of gsaves
% Ensure clean state
grestoreall
% Now at bottommost level
gsave
gsave
gsave
% Deep nesting
drawContent
grestore
grestore
grestore
% Simpler cleanup
gsave
gsave
gsave
drawContent
grestoreall % All undone at once
/DrawSafe {
% Save known state
gsave
% Call potentially unsafe code
drawUnknownContent
% Ensure cleanup regardless of gsave count
grestoreall
% Restore to known state
grestore
} def
{
% Operation that might gsave multiple times
complexOperation
} stopped {
% On error, clean up graphics state
grestoreall
handleError
} if
1.6. Common Use Cases
1.6.1. Error Recovery
/SafeExecute {
% code
{
gsave
exec
grestore
} stopped {
% Clean up on error
grestoreall
% Re-establish base state
initializeGraphicsState
} if
} def
1.7. Common Pitfalls
Clears ALL Saved States - grestoreall removes all graphics states, not just one.
|
gsave % State 1
gsave % State 2
gsave % State 3
grestoreall % Back to state 0 (not state 2)
| Does Not Restore Operand Stack - Only graphics state is affected. |
1 2 3
gsave
4 5 6
grestoreall
% Stack: 1 2 3 4 5 6 (not 1 2 3)
Current Path Reset - Like grestore, the current path becomes empty.
|
newpath 0 0 moveto 100 100 lineto
gsave
grestoreall
% Path is now empty
| Use for Error Handling - Ideal for ensuring clean state after errors. |
1.8. Error Conditions
No errors under normal operation. grestoreall succeeds even if no graphics states were saved.
1.9. Implementation Notes
-
Equivalent to repeated
grestoreuntil no saved states remain -
Very fast operation
-
Safe to call even if no states are saved
-
Does not generate errors
-
Clears current path
-
Ideal for cleanup and error recovery
-
Often used with [
stopped]
1.10. Graphics State Stack Behavior
Current State (level 3) ← current
Saved State 2
Saved State 1
Saved State 0 ← bottommost
Saved State 0 ← current (bottommost)
1.11. Interaction with save/restore
grestoreall restores graphics state to the bottommost level within the current save context. It does not cross save boundaries:
gsave % Graphics state 1
save % VM snapshot
gsave % Graphics state 2
grestoreall % Restores to state after save
% Still within save context
restore % Returns to before save
% Now at graphics state 1