1. gstate
Creates a graphics state object.
1.2. Stack Effects
| Level | Object |
|---|---|
(empty) |
– |
| Level | Object |
|---|---|
0 |
|
1.3. Description
gstate creates a new gstate (graphics state) object and pushes it on the operand stack. Its initial value is a copy of the current graphics state, including all graphics parameters such as:
-
Current transformation matrix (CTM)
-
Color space and color
-
Line width, cap, join, and dash pattern
-
Miter limit
-
Current font
-
Clipping path
-
And all other graphics state parameters
This operator consumes VM; it is the only graphics state operator that does. The gstate is allocated in either local or global VM according to the current VM allocation mode.
The gstate object can later be used with setgstate to restore the graphics state to the captured values, or with currentgstate to update the saved state.
1.5. Examples
gstate % Create gstate object
/MyState exch def % Save it
% Modify graphics state
2 2 scale
0.5 setgray
3 setlinewidth
% Restore previous state
MyState setgstate
% Save initial state
gstate /InitialState exch def
% Make some changes
1 0 0 setrgbcolor
/Helvetica findfont 12 scalefont setfont
% Save modified state
gstate /ModifiedState exch def
% Switch between states
InitialState setgstate % Back to initial
ModifiedState setgstate % To modified
/DrawWithState {
% Create state snapshot
gstate
% Draw with custom settings
0.5 setlinewidth
1 0 0 setrgbcolor
drawShape
% Restore state
setgstate
} def
1.6. Common Use Cases
1.6.1. Preserving Graphics State
% Save entire state for later
gstate /SavedState exch def
% Do complex operations
modifyManyParameters
drawComplexGraphic
% Restore everything at once
SavedState setgstate
1.6.2. Multiple State Snapshots
/StateLibrary 10 dict def
% Save different states
gstate StateLibrary /NormalState 3 -1 roll put
1 0 0 setrgbcolor
gstate StateLibrary /RedState 3 -1 roll put
0 1 0 setrgbcolor
gstate StateLibrary /GreenState 3 -1 roll put
% Use saved states
StateLibrary /RedState get setgstate
StateLibrary /NormalState get setgstate
1.7. Common Pitfalls
VM Consumption - gstate allocates memory; excessive use can exhaust VM.
|
% Bad: Creating gstate in loop
1 1 1000 {
gstate /temp exch def % VM grows!
} for
| Global VM Restrictions - Creating gstate in global VM when graphics state contains local VM objects causes error. |
true setglobal
/LocalFont /Helvetica findfont def % Local VM
gstate % Error: invalidaccess
% Inefficient
gstate /temp exch def
drawShape
temp setgstate
% Better
gsave
drawShape
grestore
Use for Templates - gstate is ideal for creating reusable state configurations.
|
1.8. Error Conditions
| Error | Condition |
|---|---|
[ |
gstate in global VM but current state contains local VM objects |
[ |
No room on operand stack |
[ |
Insufficient VM to allocate gstate object |
1.10. Graphics State Components
The gstate object captures all of the following:
-
Transformation matrix (CTM)
-
Color space and color values
-
Line parameters (width, cap, join, dash, miter limit)
-
Current font
-
Current path (not saved - always empty)
-
Clipping path
-
Flatness tolerance
-
Stroke adjustment
-
Halftone, transfer function, black generation
-
Rendering intent (Level 3)
1.11. See Also
-
setgstate- Replace graphics state from gstate -
currentgstate- Update gstate with current state -
gsave- Save graphics state on stack -
grestore- Restore graphics state from stack -
grestoreall- Restore all saved states