1. Graphics State

Graphics state operations control the rendering parameters that affect how graphics are drawn on the page. The graphics state includes color, line characteristics, transformations, clipping paths, and many other attributes.

2. Overview

The graphics state is a collection of parameters that control:

  • Color: Current color (gray, RGB, CMYK, HSB)

  • Line attributes: Width, cap style, join style, miter limit, dash pattern

  • Transformations: Current transformation matrix (CTM)

  • Clipping: Current clipping path

  • Font: Current font and text parameters

  • Device: Current output device

Each parameter has a current value that affects subsequent graphics operations.

3. Graphics State Stack

PostScript provides a stack for saving and restoring graphics state:

  • gsave saves current state

  • grestore restores saved state

  • State changes are isolated between save/restore pairs

  • Enables temporary modifications without losing previous state

4. Command Categories

4.1. State Save and Restore

Command Description

gsave

Push copy of graphics state onto graphics state stack

grestore

Restore graphics state from top of stack

grestoreall

Restore to bottommost save level

gstate

Create gstate object (Level 2)

setgstate

Set graphics state from gstate object (Level 2)

currentgstate

Copy current state to gstate object (Level 2)

4.2. Line Attributes

Command Description

setlinewidth

Set line width for stroking

setlinecap

Set line cap style (butt, round, square)

setlinejoin

Set line join style (miter, round, bevel)

setmiterlimit

Set miter limit for sharp corners

setdash

Set dash pattern for lines

currentlinewidth

Get current line width

currentlinecap

Get current line cap

currentlinejoin

Get current line join

currentmiterlimit

Get current miter limit

currentdash

Get current dash pattern

4.3. Color Operations

Command Description

setgray

Set gray level (0=black, 1=white)

setrgbcolor

Set RGB color

setcmykcolor

Set CMYK color (Level 2)

sethsbcolor

Set HSB color

currentgray

Get current gray value

currentrgbcolor

Get current RGB color

currentcmykcolor

Get current CMYK color (Level 2)

currenthsbcolor

Get current HSB color

4.4. Rendering Parameters

Command Description

sethalftone

Install a halftone dictionary for subsequent rendering (Level 2)

setsmoothness

Set the color tolerance for smooth shading (Level 3)

5. Common Patterns

5.1. Temporary State Changes

gsave
  1 setlinewidth
  0 setgray
  % Draw with these settings
  stroke
grestore
% Original settings restored

5.2. Setting Line Appearance

2 setlinewidth      % 2-unit wide lines
1 setlinecap        % Round caps
1 setlinejoin       % Round joins
[3 5] 0 setdash     % Dashed: 3 on, 5 off

5.3. Color Selection

% Grayscale
0.5 setgray         % 50% gray

% RGB
1 0 0 setrgbcolor   % Red

% CMYK (Level 2)
0 1 1 0 setcmykcolor  % Red in CMYK

6. Important Considerations

6.1. State Hierarchy

Graphics state changes affect all subsequent operations until:

  • Another state change occurs

  • grestore restores previous state

  • initgraphics resets to defaults

  • Page is complete (showpage)

6.2. save vs. gsave

Operator Saves Restores

save

VM + graphics state

[restore]

gsave

Graphics state only

grestore

6.3. Device Dependence

Some parameters are device-dependent:

  • Transfer functions

  • Halftone screens

  • Flatness

  • Should not be used in device-independent documents

7. Common Pitfalls

Unbalanced gsave/grestore - Every gsave must have matching grestore.
Color Space - Color operators may switch color space (e.g., setgray) → DeviceGray.
Dash Pattern Reset - Empty dash array [] resets to solid lines.
Bracket Graphics with gsave/grestore - Always wrap state changes:
gsave
  % Temporary changes
grestore

8. See Also

  • Coordinate Transformations (to be documented) - CTM operations

  • Path Construction (to be documented) - Path building

  • Painting (to be documented) - Using graphics state

  • Color (to be documented) - Advanced color spaces