Skip to main content

Matrix

Struct Matrix 

Source
pub struct Matrix {
    pub a: f64,
    pub b: f64,
    pub c: f64,
    pub d: f64,
    pub e: f64,
    pub f: f64,
}
Expand description

A 2D affine transformation matrix.

The matrix represents the transformation:

| a  b  0 |
| c  d  0 |
| e  f  1 |

Applied to a point (x, y):

x' = a*x + c*y + e
y' = b*x + d*y + f

This follows the PDF specification (ISO 32000-2 §8.3.3).

Fields§

§a: f64

Scale x / rotate component.

§b: f64

Rotate / skew component.

§c: f64

Rotate / skew component.

§d: f64

Scale y / rotate component.

§e: f64

Translate x.

§f: f64

Translate y.

Implementations§

Source§

impl Matrix

Source

pub fn new(a: f64, b: f64, c: f64, d: f64, e: f64, f: f64) -> Self

Create a new matrix from its six components.

Source

pub fn identity() -> Self

The identity matrix (no transformation).

Source

pub fn from_translation(tx: f64, ty: f64) -> Self

Create a translation matrix.

Constructs the matrix [1 0 0 1 tx ty]. This is a Rust constructor idiom; the upstream mutating CFX_Matrix::Translate() is exposed via the translate() method alias on &mut self.

Source

pub fn from_scale(sx: f64, sy: f64) -> Self

Create a scaling matrix.

Constructs the matrix [sx 0 0 sy 0 0]. This is a Rust constructor idiom; the upstream mutating CFX_Matrix::Scale() is exposed via the scale() method alias on &mut self.

Source

pub fn from_rotation(angle_radians: f64) -> Self

Create a rotation matrix for the given angle in radians.

Rotation is counter-clockwise in the standard PDF coordinate system. This is a Rust constructor idiom; the upstream mutating CFX_Matrix::Rotate() is exposed via the rotate() method alias on &mut self.

Source

pub fn pre_concat(&self, other: &Matrix) -> Self

Return a new matrix that is the concatenation of other then self.

The resulting matrix applies other first, then self. This matches the PDF specification’s matrix concatenation semantics where points are row vectors: [x, y, 1] * M.

This is a value-returning Rust idiom; the upstream mutating CFX_Matrix::Concat() is exposed via the concat() method alias on &mut self.

Source

pub fn transform_point(&self, point: Point) -> Point

Transform a point by this matrix.

Computes (a*x + c*y + e, b*x + d*y + f).

Source

pub fn transform_vector(&self, v: Vector2D) -> Vector2D

Transform a vector by the linear part of this matrix (no translation).

Applies only the a/b/c/d coefficients; the e/f translation is not added. Use this when transforming a displacement rather than a position.

Equivalent to CFX_Matrix::TransformPoint applied to a vector and then subtracting the translation, but more explicit about intent.

Source

pub fn transform_rect(&self, rect: Rect) -> Rect

Transform a rectangle by this matrix.

Transforms all four corners and returns the axis-aligned bounding box of the result. The returned rectangle is always normalized.

Source

pub fn determinant(&self) -> f64

Compute the determinant of the matrix.

Source

pub fn inverse(&self) -> Option<Self>

Compute the inverse of this matrix, if it exists.

Returns None if the matrix is singular (determinant is zero). Corresponds to CFX_Matrix::GetInverse().

Source

pub fn get_inverse(&self) -> Option<Self>

Upstream-aligned alias for Self::inverse().

Corresponds to CFX_Matrix::GetInverse().

Source

pub fn inverse_or_identity(&self) -> Self

Returns the inverse matrix, or identity if the matrix is singular.

Equivalent to PDFium’s CFX_Matrix::GetInverse() which returns the identity matrix for singular (non-invertible) matrices.

Source

pub fn is_identity(&self) -> bool

Returns true if this is the identity matrix.

Uses exact bitwise comparison matching PDFium’s C++ implementation, which checks for exact equality rather than approximate epsilon comparison.

Source

pub fn will_scale(&self) -> bool

Returns true if this matrix applies any scaling (not a pure translation).

Equivalent to CFX_Matrix::WillScale() in PDFium.

Source

pub fn is_90_rotated(&self) -> bool

Returns true if this matrix represents a 90-degree rotation (a ≈ 0, d ≈ 0).

Equivalent to CFX_Matrix::Is90Rotated() in PDFium.

Source

pub fn x_unit(&self) -> f64

Return the length of the X basis vector (√(a² + b²)).

Equivalent to CFX_Matrix::GetXUnit() in PDFium.

Source

pub fn get_x_unit(&self) -> f64

Upstream-aligned alias for Self::x_unit().

Corresponds to CFX_Matrix::GetXUnit().

Source

pub fn y_unit(&self) -> f64

Return the length of the Y basis vector (√(c² + d²)).

Equivalent to CFX_Matrix::GetYUnit() in PDFium.

Source

pub fn get_y_unit(&self) -> f64

Upstream-aligned alias for Self::y_unit().

Corresponds to CFX_Matrix::GetYUnit().

Source

pub fn is_scaled(&self) -> bool

Returns true if this matrix is a pure scale/translation (no significant rotation).

Checks that b and c (shear/rotation components) are negligible relative to a and d respectively (threshold: 1000×). Equivalent to CFX_Matrix::IsScaled() in PDFium.

Source

pub fn concat(&mut self, right: &Matrix)

Concatenate right into this matrix in-place (*self = *self * right).

After the call, self applies the original transformation first, then right.

Corresponds to CFX_Matrix::Concat() in PDFium upstream.

Source

pub fn concat_assign(&mut self, right: &Matrix)

👎Deprecated:

use concat() instead

Alias for concat.

Corresponds to CFX_Matrix::Concat() in PDFium upstream.

Source

pub fn translate(&mut self, x: f64, y: f64)

Shift the translation components by (x, y) in-place.

Corresponds to CFX_Matrix::Translate() in PDFium upstream.

Source

pub fn translate_by(&mut self, tx: f64, ty: f64)

👎Deprecated:

use translate() instead

Alias for translate.

Corresponds to CFX_Matrix::Translate() in PDFium upstream.

Source

pub fn scale(&mut self, sx: f64, sy: f64)

Scale all six matrix components by (sx, sy) in-place.

Corresponds to CFX_Matrix::Scale() in PDFium upstream.

Source

pub fn scale_by(&mut self, sx: f64, sy: f64)

👎Deprecated:

use scale() instead

Alias for scale.

Corresponds to CFX_Matrix::Scale() in PDFium upstream.

Source

pub fn translate_prepend(&mut self, tx: f64, ty: f64)

Prepend a translation: modifies e and f based on current a/b/c/d.

Equivalent to CFX_Matrix::TranslatePrepend(x, y) in PDFium.

Source

pub fn rotate(&mut self, angle_radians: f64)

Rotate this matrix in-place by angle_radians radians.

Corresponds to CFX_Matrix::Rotate() in PDFium upstream.

Source

pub fn rotate_by(&mut self, angle_radians: f64)

👎Deprecated:

use rotate() instead

Alias for rotate.

Corresponds to CFX_Matrix::Rotate() in PDFium upstream.

Source

pub fn transform_distance(&self, dist: f64) -> f64

Transform a scalar distance using the average scale factor.

Equivalent to CFX_Matrix::TransformDistance(d) in PDFium.

Source

pub fn match_rect(dest: &Rect, src: &Rect) -> Self

Construct a matrix that maps rectangle src onto rectangle dest.

If src has zero width or height in a dimension, the corresponding scale is set to 1. The result has no rotation (b = c = 0).

Equivalent to CFX_Matrix::MatchRect(dest, src) in PDFium.

Source

pub fn transform_x_distance(&self, dx: f64) -> f64

Return the length of a horizontal distance dx after transformation.

Computes hypot(a * dx, b * dx) — the Euclidean length of the transformed horizontal vector (dx, 0).

Equivalent to CFX_Matrix::TransformXDistance(dx) in PDFium.

Source

pub fn unit_rect(&self) -> Rect

Return the axis-aligned bounding box of the unit square [0,1]×[0,1] after transformation by this matrix.

Equivalent to CFX_Matrix::GetUnitRect() in PDFium.

Source

pub fn get_unit_rect(&self) -> Rect

Upstream-aligned alias for Self::unit_rect().

Corresponds to CFX_Matrix::GetUnitRect().

Source

pub fn transform(&self, point: Point) -> Point

ADR-019 alias for transform_point().

Corresponds to CFX_Matrix::Transform(point).

Trait Implementations§

Source§

impl Clone for Matrix

Source§

fn clone(&self) -> Matrix

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Matrix

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Matrix

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl PartialEq for Matrix

Source§

fn eq(&self, other: &Matrix) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Copy for Matrix

Source§

impl StructuralPartialEq for Matrix

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.