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 + fThis follows the PDF specification (ISO 32000-2 §8.3.3).
Fields§
§a: f64Scale x / rotate component.
b: f64Rotate / skew component.
c: f64Rotate / skew component.
d: f64Scale y / rotate component.
e: f64Translate x.
f: f64Translate y.
Implementations§
Source§impl Matrix
impl Matrix
Sourcepub fn new(a: f64, b: f64, c: f64, d: f64, e: f64, f: f64) -> Self
pub fn new(a: f64, b: f64, c: f64, d: f64, e: f64, f: f64) -> Self
Create a new matrix from its six components.
Sourcepub fn from_translation(tx: f64, ty: f64) -> Self
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.
Sourcepub fn from_scale(sx: f64, sy: f64) -> Self
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.
Sourcepub fn from_rotation(angle_radians: f64) -> Self
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.
Sourcepub fn pre_concat(&self, other: &Matrix) -> Self
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.
Sourcepub fn transform_point(&self, point: Point) -> Point
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).
Sourcepub fn transform_vector(&self, v: Vector2D) -> Vector2D
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.
Sourcepub fn transform_rect(&self, rect: Rect) -> Rect
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.
Sourcepub fn determinant(&self) -> f64
pub fn determinant(&self) -> f64
Compute the determinant of the matrix.
Sourcepub fn inverse(&self) -> Option<Self>
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().
Sourcepub fn get_inverse(&self) -> Option<Self>
pub fn get_inverse(&self) -> Option<Self>
Upstream-aligned alias for Self::inverse().
Corresponds to CFX_Matrix::GetInverse().
Sourcepub fn inverse_or_identity(&self) -> Self
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.
Sourcepub fn is_identity(&self) -> bool
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.
Sourcepub fn will_scale(&self) -> bool
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.
Sourcepub fn is_90_rotated(&self) -> bool
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.
Sourcepub fn x_unit(&self) -> f64
pub fn x_unit(&self) -> f64
Return the length of the X basis vector (√(a² + b²)).
Equivalent to CFX_Matrix::GetXUnit() in PDFium.
Sourcepub fn get_x_unit(&self) -> f64
pub fn get_x_unit(&self) -> f64
Upstream-aligned alias for Self::x_unit().
Corresponds to CFX_Matrix::GetXUnit().
Sourcepub fn y_unit(&self) -> f64
pub fn y_unit(&self) -> f64
Return the length of the Y basis vector (√(c² + d²)).
Equivalent to CFX_Matrix::GetYUnit() in PDFium.
Sourcepub fn get_y_unit(&self) -> f64
pub fn get_y_unit(&self) -> f64
Upstream-aligned alias for Self::y_unit().
Corresponds to CFX_Matrix::GetYUnit().
Sourcepub fn is_scaled(&self) -> bool
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.
Sourcepub fn concat(&mut self, right: &Matrix)
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.
Sourcepub fn concat_assign(&mut self, right: &Matrix)
👎Deprecated: use concat() instead
pub fn concat_assign(&mut self, right: &Matrix)
use concat() instead
Alias for concat.
Corresponds to CFX_Matrix::Concat() in PDFium upstream.
Sourcepub fn translate(&mut self, x: f64, y: f64)
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.
Sourcepub fn translate_by(&mut self, tx: f64, ty: f64)
👎Deprecated: use translate() instead
pub fn translate_by(&mut self, tx: f64, ty: f64)
use translate() instead
Alias for translate.
Corresponds to CFX_Matrix::Translate() in PDFium upstream.
Sourcepub fn scale(&mut self, sx: f64, sy: f64)
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.
Sourcepub fn scale_by(&mut self, sx: f64, sy: f64)
👎Deprecated: use scale() instead
pub fn scale_by(&mut self, sx: f64, sy: f64)
use scale() instead
Alias for scale.
Corresponds to CFX_Matrix::Scale() in PDFium upstream.
Sourcepub fn translate_prepend(&mut self, tx: f64, ty: f64)
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.
Sourcepub fn rotate(&mut self, angle_radians: f64)
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.
Sourcepub fn rotate_by(&mut self, angle_radians: f64)
👎Deprecated: use rotate() instead
pub fn rotate_by(&mut self, angle_radians: f64)
use rotate() instead
Alias for rotate.
Corresponds to CFX_Matrix::Rotate() in PDFium upstream.
Sourcepub fn transform_distance(&self, dist: f64) -> f64
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.
Sourcepub fn match_rect(dest: &Rect, src: &Rect) -> Self
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.
Sourcepub fn transform_x_distance(&self, dx: f64) -> f64
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.
Sourcepub fn unit_rect(&self) -> Rect
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.
Sourcepub fn get_unit_rect(&self) -> Rect
pub fn get_unit_rect(&self) -> Rect
Upstream-aligned alias for Self::unit_rect().
Corresponds to CFX_Matrix::GetUnitRect().
Sourcepub fn transform(&self, point: Point) -> Point
pub fn transform(&self, point: Point) -> Point
ADR-019 alias for transform_point().
Corresponds to CFX_Matrix::Transform(point).