#pdftransform

    2D affine transformation matrices for PDF graphics.

    #Overview

    The pdftransform package provides:

    • Affine transformation matrices (translate, scale, rotate, shear)
    • Matrix composition and inversion
    • Transform points through matrices
    • Decomposition and recomposition of transforms

    #TransformMatrix

    The 2D affine transformation matrix:

    ///|
    pub(all) struct TransformMatrix {
    a : Double // scale x
    b : Double // shear y
    c : Double // shear x
    d : Double // scale y
    e : Double // translate x
    f : Double // translate y
    }

    Represents the matrix:
    | a b 0 | | c d 0 | | e f 1 |

    #Identity Matrix

    ///|
    test "identity matrix" {
    let m = @pdftransform.TransformMatrix::identity()
    inspect(m.a, content="1")
    inspect(m.d, content="1")
    inspect(m.e, content="0")
    }

    #Creating Transforms

    #Translation

    ///|
    test "translate" {
    let m = @pdftransform.TransformMatrix::translate(10.0, 20.0)
    inspect(m.e, content="10")
    inspect(m.f, content="20")
    }

    #Scaling

    // Scale by (sx, sy) around center point

    ///|
    let m = @pdftransform.TransformMatrix::scale((0.0, 0.0), 2.0, 2.0)

    #Rotation

    // Rotate by angle (radians) around center point

    ///|
    let m = @pdftransform.TransformMatrix::rotate((0.0, 0.0), 1.5708) // 90 degrees

    #Shearing

    // Horizontal shear

    ///|
    let m = @pdftransform.TransformMatrix::shear_x((0.0, 0.0), 0.5)

    // Vertical shear

    ///|
    let m = @pdftransform.TransformMatrix::shear_y((0.0, 0.0), 0.5)

    #Transform Operations

    #TransformOp Enum

    ///|
    pub(all) enum TransformOp {
    Scale((Double, Double), Double, Double) // center, sx, sy
    Rotate((Double, Double), Double) // center, angle
    Translate(Double, Double) // tx, ty
    ShearX((Double, Double), Double) // center, factor
    ShearY((Double, Double), Double) // center, factor
    }

    #Composing Operations

    // Build transform from operations

    ///|
    let tr = @pdftransform.Transform::identity().compose(
    @pdftransform.TransformOp::Translate(100.0, 100.0),
    )

    #Appending Transforms

    ///|
    let combined = tr1.append(tr2)

    #Matrix Operations

    #Composition

    ///|
    test "matrix compose" {
    let t1 = @pdftransform.TransformMatrix::translate(10.0, 0.0)
    let t2 = @pdftransform.TransformMatrix::translate(0.0, 20.0)
    let combined = t1.compose(t2)
    inspect(combined.e, content="10")
    inspect(combined.f, content="20")
    }

    #Inversion

    try {
    let inv = m.invert!()
    // Use inverted matrix...
    } catch {
    @pdftransform.NonInvertable => println("Matrix not invertible")
    }

    #Converting Operations to Matrix

    ///|
    let op = @pdftransform.TransformOp::Translate(50.0, 50.0)

    ///|
    let matrix = op.to_matrix()

    #Transforming Points

    #With Matrix

    ///|
    test "matrix applies to point" {
    let m = @pdftransform.TransformMatrix::translate(10.0, 20.0)
    let (x, y) = m.apply((5.0, 5.0))
    inspect(x, content="15")
    inspect(y, content="25")
    }

    #With Transform

    let (new_x, new_y) = tr.apply((x, y))

    #Decomposition

    Extract scale, rotation, shear, and translation from a matrix:

    let (scale, aspect, rotation, shear, tx, ty) = m.decompose()

    #Recomposition

    Rebuild a matrix from components:

    ///|
    let m = @pdftransform.TransformMatrix::recompose(
    scale, aspect, rotation, shear, tx, ty,
    )

    #Debug Utilities

    ///|
    test "matrix to_string" {
    let m = @pdftransform.TransformMatrix::identity()
    let s = m.to_string()
    assert_true(s.contains("1"))
    }

    NonInvertable

    pub suberror NonInvertable {
    NonInvertable
    }

    Exception raised if a matrix is non-invertible.

    Transform

    pub struct Transform {
    ops : Array[TransformOp]
    } derive(
    Debug
    )

    A list of transformations, stored with the most recent op at the head.

    Transform::Transform

    fn Transform::Transform(ops : Array[TransformOp]) -> Transform

    Transform::append

    fn Transform::append(self : Transform, other : Transform) -> Transform

    Append two transforms (perform b then a).

    Transform::apply

    fn Transform::apply(self : Transform, point : (Double, Double)) -> (Double, Double)

    Transform a coordinate by a given transform.

    Transform::compose

    fn Transform::compose(self : Transform, op : TransformOp) -> Transform

    Compose a transformation operation onto an existing transform.

    Transform::identity

    fn Transform::identity() -> Transform

    The identity transform.

    Transform::new

    Create a transform from an array of ops.

    Transform::to_matrix

    fn Transform::to_matrix(self : Transform) -> TransformMatrix

    Make a matrix from a transform.

    Transform::to_string

    fn Transform::to_string(self : Transform) -> String

    Make a string of a transform for debug purposes.

    TransformMatrix

    pub struct TransformMatrix {
    a : Double
    b : Double
    c : Double
    d : Double
    e : Double
    f : Double
    } derive(
    Debug
    )

    A transformation matrix (a c e / b d f / 0 0 1).

    TransformMatrix::TransformMatrix

    fn TransformMatrix::TransformMatrix(a~ : Double, b~ : Double, c~ : Double, d~ : Double, e~ : Double, f~ : Double) -> TransformMatrix

    TransformMatrix::apply

    fn TransformMatrix::apply(self : TransformMatrix, point : (Double, Double)) -> (Double, Double)

    Transform a coordinate by a given transformation matrix.

    TransformMatrix::compose

    Compose two matrices. Applying the result is equivalent to applying m then m'.

    TransformMatrix::decompose

    fn TransformMatrix::decompose(self : TransformMatrix) -> (Double, Double, Double, Double, Double, Double)

    Decompose a transformation matrix into scale, aspect, rotation, shear, translation in x, translation in y.

    TransformMatrix::identity

    The identity matrix.

    TransformMatrix::invert

    Matrix inversion. Raises NonInvertable if no inverse.

    TransformMatrix::recompose

    fn TransformMatrix::recompose(scale : Double, aspect : Double, rotation : Double, shear : Double, tx : Double, ty : Double) -> TransformMatrix

    Recompose a matrix from components.

    TransformMatrix::rotate

    fn TransformMatrix::rotate(center : (Double, Double), angle : Double) -> TransformMatrix

    Make a rotation matrix about a center point.

    TransformMatrix::scale

    fn TransformMatrix::scale(center : (Double, Double), sx : Double, sy : Double) -> TransformMatrix

    Make a scale matrix about a center point.

    TransformMatrix::shear_x

    fn TransformMatrix::shear_x(center : (Double, Double), factor : Double) -> TransformMatrix

    Matrix to shear in x about a point.

    TransformMatrix::shear_y

    fn TransformMatrix::shear_y(center : (Double, Double), factor : Double) -> TransformMatrix

    Matrix to shear in y about a point.

    TransformMatrix::to_string

    fn TransformMatrix::to_string(self : TransformMatrix) -> String

    String of a transformation matrix.

    TransformMatrix::translate

    fn TransformMatrix::translate(tx : Double, ty : Double) -> TransformMatrix

    Make a translation matrix.

    TransformOp

    pub(all) enum TransformOp {
    Scale((Double, Double), Double, Double)
    Rotate((Double, Double), Double)
    Translate(Double, Double)
    ShearX((Double, Double), Double)
    ShearY((Double, Double), Double)
    } derive(
    Debug
    )

    A single transformation operation.

    TransformOp::to_matrix

    fn TransformOp::to_matrix(self : TransformOp) -> TransformMatrix

    Make a matrix from a single transformation operation.

    Source Files

    Powered by MoonBit

    Site sourceReport issuePackagesBuild queueSkillsStatistics

    © 2026 mooncakes.io