1. grestore

Restores the graphics state from the top of the graphics state stack.

1.1. Syntax

– grestore → –

1.2. Stack Effects

Table 1. Before/After
Level Object

(none)

Graphics state stack decreased by one, current state restored

1.3. Description

grestore resets the current graphics state from the one on top of the graphics state stack and pops that stack.

If there is no matching gsave, or if the most recent gsave preceded the most recent unmatched save, grestore does not pop the stack, but still restores the state from the top.

1.4. PostScript Level

Level 1 and later

1.5. Examples

Basic restore
gsave
  2 setlinewidth
  0.5 setgray
  % Draw something
grestore
% Line width and gray restored
Multiple levels
gsave
  1 setlinewidth
  gsave
    5 setlinewidth
  grestore  % Back to 1
grestore    % Back to original
After save
/mysave save def
gsave
  % State changes
grestore  % Doesn't pop (save intervened)
mysave restore  % Restores including gsave level

1.6. Common Use Cases

1.6.1. Isolating Modifications

/drawElement {
  gsave
    applyTransforms
    applyColors
    renderElement
  grestore
} def

1.6.2. Clipping Regions

gsave
  clipPath clip
  % Draw clipped content
grestore
% Clip path restored

1.6.3. Color Isolation

gsave
  1 0 0 setrgbcolor
  fillShape
grestore
% Original color restored

1.7. Common Pitfalls

Must Have Matching gsave - Unmatched grestore doesn’t error but doesn’t pop stack.
grestore  % No matching gsave
% State restored from top but stack not popped
save Intervenes - grestore after save doesn’t pop stack.
gsave
save
grestore  % Doesn't pop due to save
restore   % Pops graphics state stack
Always Pair with gsave - Use gsave/grestore pairs consistently.

1.8. Error Conditions

Error Condition

(none directly)

No errors generated by grestore itself

1.9. Implementation Notes

  • Restores all graphics state parameters atomically

  • Fast operation

  • Common pattern in PostScript programs

  • Stack-based state management

1.10. What Gets Restored

All parameters saved by gsave: * CTM, path, current point * Clipping path * Color space and color * Font * All line parameters * All device-dependent parameters

1.11. See Also

  • gsave - Save graphics state

  • grestoreall - Restore all levels

  • setgstate - Set from gstate object

  • save, restore - VM and graphics state


Back to top

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