1. currentmatrix

Gets the current transformation matrix from the graphics state.

1.1. Syntax

matrix currentmatrix → matrix

1.2. Stack Effects

Table 1. Before
Level Object

0

matrix (array)

Table 2. After
Level Object

0

matrix (modified array)

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.

The CTM defines the transformation from user space coordinates to device space coordinates. It is modified by transformation operators like translate, scale, rotate, and concat.

1.4. PostScript Level

Level 1 and later

1.5. Examples

Getting the CTM
matrix currentmatrix  % Get current transformation
% → e.g., [2 0 0 2 100 100]
Saving and restoring CTM
matrix currentmatrix  % Save CTM
/savedCTM exch def

% ... perform transformations ...

savedCTM setmatrix    % Restore CTM
Examining transformations
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.6.3. Building on Current Transformation

% Get current, modify, apply
matrix currentmatrix
45 rotate  % Modify matrix
concat     % Apply modified matrix

1.6.4. Coordinate Space Conversion

% Get CTM for manual coordinate transformation
/ctm matrix currentmatrix def
% Use with transform operator

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
Use with gsave/grestore - For complete state saving, use gsave/grestore:
% 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

[rangecheck]

Array has fewer than 6 elements

[stackunderflow]

No operand on stack

[typecheck]

Operand is not an array

1.9. Implementation Notes

  • The CTM is stored internally in device space

  • Very large transformations may experience precision loss

  • The CTM is part of the graphics state

  • Modified by gsave/grestore

  • The array must have at least 6 elements

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.13. Relationship to Graphics State

The CTM is modified by:

1.14. See Also


Back to top

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