1. setgstate
Replaces current graphics state from a gstate object.
1.2. Stack Effects
| Level | Object |
|---|---|
0 |
|
| Level | Object |
|---|---|
(empty) |
Graphics state replaced |
1.3. Description
setgstate replaces the current graphics state by the value of the gstate object. This is a copying operation, so subsequent modifications to the value of gstate will not affect the current graphics state or vice versa.
This is a wholesale replacement of all components of the graphics state. In particular:
-
The current clipping path is replaced by the value in gstate, not intersected with it
-
The current path is not affected (it remains unchanged)
-
All other graphics parameters are replaced completely
The gstate object itself remains unchanged and can be reused to restore the same state multiple times.
1.5. Examples
% Save current state
gstate /SavedState exch def
% Make changes
2 setlinewidth
1 0 0 setrgbcolor
45 rotate
% Restore original state
SavedState setgstate
% All changes undone
% Create state templates
/HeaderStyle {
/Helvetica-Bold findfont 18 scalefont setfont
0 setgray
gstate
} def
/BodyStyle {
/Times-Roman findfont 12 scalefont setfont
0.3 setgray
gstate
} def
% Use templates
/header HeaderStyle def
/body BodyStyle def
header setgstate
100 700 moveto (Title) show
body setgstate
100 650 moveto (Content) show
% Save initial state
gstate /InitState exch def
% Create and save modified state
0.5 setgray
3 setlinewidth
gstate /ThickGray exch def
% Create another state
1 0 0 setrgbcolor
0.5 setlinewidth
gstate /ThinRed exch def
% Switch between states
ThickGray setgstate
drawOutline stroke
ThinRed setgstate
drawDetails stroke
InitState setgstate
% Back to original
/ComplexDrawing {
% Save state
gstate /temp exch def
% Perform many modifications
matrix currentmatrix
userMatrix concat
customColorSpace setcolorspace
customHalftone sethalftone
% Do drawing
drawComplexShape
% Restore everything at once
temp setgstate
} def
1.6. Common Use Cases
1.6.1. Reusable State Configurations
% Define standard states
/StandardStates <<
/Header {
/Helvetica-Bold findfont 14 scalefont setfont
0 setgray
1 setlinewidth
gstate
} def
/Body {
/Times-Roman findfont 10 scalefont setfont
0 setgray
0.5 setlinewidth
gstate
} def
/Caption {
/Helvetica findfont 8 scalefont setfont
0.4 setgray
0.25 setlinewidth
gstate
} def
>> def
% Use throughout document
StandardStates /Header get setgstate
1.7. Common Pitfalls
Clipping Path Replaced - setgstate replaces the clipping path, not intersects.
|
% Save state
gstate /saved exch def
% Establish clip
newpath 0 0 100 100 rectclip
% Restore state - clip is GONE, not combined
saved setgstate
| Current Path Unchanged - The current path is NOT saved in gstate. |
newpath 0 0 moveto 100 100 lineto
gstate /temp exch def
newpath % Clear path
temp setgstate
% Path is still empty, not restored
% Inefficient
gstate /temp exch def
modifyState
temp setgstate
% Better
gsave
modifyState
grestore
| Reusable Templates - Create gstate objects once, reuse many times. |
1.8. Error Conditions
| Error | Condition |
|---|---|
[ |
No operand on stack |
[ |
Operand not a gstate object |
1.9. Implementation Notes
-
Copying operation - gstate object unchanged
-
Replaces all graphics state components
-
Clipping path is replaced, not intersected
-
Current path is not affected
-
Very fast operation
-
gstate can be reused indefinitely
-
More efficient than multiple parameter sets
1.10. Graphics State Replacement
setgstate replaces:
-
✓ Transformation matrix (CTM)
-
✓ Color space and color
-
✓ Line width, cap, join, dash, miter limit
-
✓ Current font
-
✓ Clipping path (replaced, not intersected)
-
✓ Flatness, stroke adjustment
-
✓ Halftone, transfer, black generation
-
✗ Current path (never saved/restored)
1.11. See Also
-
gstate- Create graphics state object -
currentgstate- Update gstate with current state -
gsave- Save state on graphics state stack -
grestore- Restore state from stack -
grestoreall- Restore all saved states