1. grestoreall

Restores graphics state to bottommost saved level.

1.1. Syntax

– grestoreall → –

1.2. Stack Effects

Table 1. Before
Level Object

(empty)

Table 2. After
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.4. PostScript Level

Level 1 and later

1.5. Examples

Error recovery
% May have unknown gsave depth
drawComplexFigure  % Unknown number of gsaves

% Ensure clean state
grestoreall
% Now at bottommost level
Cleanup after nested operations
gsave
  gsave
    gsave
      % Deep nesting
      drawContent
    grestore
  grestore
grestore

% Simpler cleanup
gsave
gsave
gsave
drawContent
grestoreall  % All undone at once
Guaranteed reset
/DrawSafe {
  % Save known state
  gsave

  % Call potentially unsafe code
  drawUnknownContent

  % Ensure cleanup regardless of gsave count
  grestoreall

  % Restore to known state
  grestore
} def
Exception handling
{
  % 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.6.2. Nested Drawing Cleanup

/DrawWithUnknownDepth {
  % Don't know how many gsaves were done
  callThirdPartyCode

  % Ensure we're back to base
  grestoreall
} def

1.6.3. State Reset

/ResetGraphicsState {
  grestoreall
  initgraphics
  initmatrix
} def

1.6.4. Page Boundary

/EndPage {
  % Restore to page start state
  grestoreall

  % Show page
  showpage
} 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 grestore until 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

Before grestoreall
Current State (level 3) ← current
Saved State 2
Saved State 1
Saved State 0          ← bottommost
After grestoreall
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

1.12. See Also


Back to top

Copyright © 2025 Ribose. PostScript is a trademark of Adobe. Distributed under the MIT License.