- 1. currentmatrix
- 1.1. Syntax
- 1.2. Stack Effects
- 1.3. Description
- 1.4. PostScript Level
- 1.5. Examples
- 1.6. Common Use Cases
- 1.7. Common Pitfalls
- 1.8. Error Conditions
- 1.9. Implementation Notes
- 1.10. Matrix Interpretation
- 1.11. Extracting Transformation Components
- 1.12. Performance Considerations
- 1.13. Relationship to Graphics State
- 1.14. See Also
1. currentmatrix
Gets the current transformation matrix from the graphics state.
1.2. Stack Effects
| Level | Object |
|---|---|
0 |
|
| Level | Object |
|---|---|
0 |
|
1.3. Description
currentmatrix replaces the value of matrix with the current transformation matrix (CTM) from the graphics state and pushes the modified matrix back on the operand stack.
1.5. Examples
matrix currentmatrix % Get current transformation
% → e.g., [2 0 0 2 100 100]
matrix currentmatrix % Save CTM
/savedCTM exch def
% ... perform transformations ...
savedCTM setmatrix % Restore CTM
100 100 translate
2 2 scale
45 rotate
matrix currentmatrix
% Shows combined transformation matrix
1.6. Common Use Cases
1.6.1. Preserving Transformation State
% Better than gsave/grestore when you only need CTM
matrix currentmatrix
/savedMatrix exch def
% Complex transformations
100 200 translate
3 1.5 scale
% Restore
savedMatrix setmatrix
1.6.2. Analyzing Current Transformation
% Check current scale
matrix currentmatrix
dup 0 get % a component (x-scale)
exch 3 get % d component (y-scale)
% Now have x and y scale factors on stack
1.7. Common Pitfalls
Modifies Input Array - currentmatrix modifies the array you provide.
|
/m [0 0 0 0 0 0] def
m currentmatrix pop
% m now contains CTM, original values lost
| Not a Copy - The returned array is the one you provided, not a new array. |
matrix currentmatrix
dup setmatrix
% Both stack entries refer to same array
% Only saves CTM:
matrix currentmatrix /saved exch def
% ... transformations ...
saved setmatrix
% Saves entire graphics state:
gsave
% ... transformations ...
grestore
| Reuse Arrays - For efficiency, reuse matrix arrays: |
/workMatrix 6 array def
% Reuse many times:
workMatrix currentmatrix ...
workMatrix currentmatrix ...
1.8. Error Conditions
| Error | Condition |
|---|---|
[ |
Array has fewer than 6 elements |
[ |
No operand on stack |
[ |
Operand is not an array |
1.10. Matrix Interpretation
The returned matrix [a b c d tx ty] transforms coordinates:
x_device = a×x_user + c×y_user + tx y_device = b×x_user + d×y_user + ty
Components represent:
-
a,d: scaling in x and y -
b,c: rotation/shearing -
tx,ty: translation
1.11. Extracting Transformation Components
% Get translation
matrix currentmatrix
dup 4 get /tx exch def
5 get /ty exch def
% Get scale (assuming no rotation)
matrix currentmatrix
dup 0 get /sx exch def
3 get /sy exch def
% Get rotation (assuming uniform scale)
matrix currentmatrix
dup 1 get exch 0 get
atan /angle exch def
1.12. Performance Considerations
-
Very fast operation
-
No matrix computation required
-
Just copies 6 values from graphics state
-
Reusing arrays avoids allocation overhead
1.14. See Also
-
setmatrix- Set the CTM -
defaultmatrix- Get device default matrix -
initmatrix- Reset CTM to default -
concat- Concatenate matrix with CTM -
transform- Transform coordinates by CTM -
gsave- Save graphics state -
grestore- Restore graphics state