1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
//! Defines the [PdfMatrix] struct, a container for six floating-point values that represent
//! the six configurable elements of a nine-element 3x3 PDF transformation matrix.
use crate::bindgen::FS_MATRIX;
use crate::error::PdfiumError;
use crate::points::PdfPoints;
use crate::{create_transform_getters, create_transform_setters};
use std::hash::{Hash, Hasher};
use std::ops::{Add, Mul, Sub};
use vecmath::{mat3_add, mat3_det, mat3_inv, mat3_sub, mat3_transposed, row_mat3_mul, Matrix3};
pub type PdfMatrixValue = f32;
/// Six floating-point values, labelled `a`, `b`, `c`, `d`, `e`, and `f`, that represent
/// the six configurable elements of a nine-element 3x3 PDF transformation matrix.
///
/// Applying the matrix to any transformable object containing a `set_matrix()` function - such as
/// a page, clip path, individual page object, or page object group - will result in a
/// transformation of that object. Depending on the values specified in the matrix, the object
/// can be moved, scaled, rotated, or skewed.
///
/// **It is rare that a matrix needs to be used directly.** All transformable objects provide
/// convenient and expressive access to the most commonly used transformation operations without
/// requiring a matrix.
///
/// However, a matrix can be convenient when the same transformation values need to be applied
/// to a large set of transformable objects.
///
/// An overview of PDF transformation matrices can be found in the PDF Reference Manual
/// version 1.7 on page 204; a detailed description can be founded in section 4.2.3 on page 207.
#[derive(Debug, Copy, Clone)]
pub struct PdfMatrix {
matrix: Matrix3<PdfMatrixValue>,
}
impl PdfMatrix {
/// A [PdfMatrix] object with all matrix values set to 0.0.
pub const ZERO: PdfMatrix = Self::zero();
/// A [PdfMatrix] object with matrix values a and d set to 1.0
/// and all other values set to 0.0.
pub const IDENTITY: PdfMatrix = Self::identity();
#[inline]
pub(crate) fn from_pdfium(matrix: FS_MATRIX) -> Self {
Self::new(matrix.a, matrix.b, matrix.c, matrix.d, matrix.e, matrix.f)
}
/// Creates a new [PdfMatrix] with the given matrix values.
#[inline]
pub const fn new(
a: PdfMatrixValue,
b: PdfMatrixValue,
c: PdfMatrixValue,
d: PdfMatrixValue,
e: PdfMatrixValue,
f: PdfMatrixValue,
) -> Self {
Self {
matrix: [[a, b, 0.0], [c, d, 0.0], [e, f, 1.0]],
}
}
/// Creates a new [PdfMatrix] object with all matrix values set to 0.0.
///
/// The return value of this function is identical to the constant [PdfMatrix::ZERO].
#[inline]
pub const fn zero() -> Self {
Self::new(0.0, 0.0, 0.0, 0.0, 0.0, 0.0)
}
/// Creates a new [PdfMatrix] object with matrix values a and d set to 1.0
/// and all other values set to 0.0.
///
/// The return value of this function is identical to the constant [PdfMatrix::IDENTITY].
#[inline]
pub const fn identity() -> Self {
Self::new(1.0, 0.0, 0.0, 1.0, 0.0, 0.0)
}
/// Returns the value of `a`, the first of six floating-point values that represent
/// the six configurable elements of a nine-element 3x3 PDF transformation matrix.
#[inline]
pub fn a(&self) -> PdfMatrixValue {
self.matrix[0][0]
}
/// Sets the value of `a`, the first of six floating-point values that represent
/// the six configurable elements of a nine-element 3x3 PDF transformation matrix.
#[inline]
pub fn set_a(&mut self, a: PdfMatrixValue) {
self.matrix[0][0] = a;
}
/// Returns the value of `b`, the second of six floating-point values that represent
/// the six configurable elements of a nine-element 3x3 PDF transformation matrix.
#[inline]
pub fn b(&self) -> PdfMatrixValue {
self.matrix[0][1]
}
/// Sets the value of `b`, the second of six floating-point values that represent
/// the six configurable elements of a nine-element 3x3 PDF transformation matrix.
#[inline]
pub fn set_b(&mut self, b: PdfMatrixValue) {
self.matrix[0][1] = b;
}
/// Returns the value of `c`, the third of six floating-point values that represent
/// the six configurable elements of a nine-element 3x3 PDF transformation matrix.
#[inline]
pub fn c(&self) -> PdfMatrixValue {
self.matrix[1][0]
}
/// Sets the value of `c`, the third of six floating-point values that represent
/// the six configurable elements of a nine-element 3x3 PDF transformation matrix.
#[inline]
pub fn set_c(&mut self, c: PdfMatrixValue) {
self.matrix[1][0] = c;
}
/// Returns the value of `d`, the fourth of six floating-point values that represent
/// the six configurable elements of a nine-element 3x3 PDF transformation matrix.
#[inline]
pub fn d(&self) -> PdfMatrixValue {
self.matrix[1][1]
}
/// Sets the value of `d`, the fourth of six floating-point values that represent
/// the six configurable elements of a nine-element 3x3 PDF transformation matrix.
#[inline]
pub fn set_d(&mut self, d: PdfMatrixValue) {
self.matrix[1][1] = d;
}
/// Returns the value of `e`, the fifth of six floating-point values that represent
/// the six configurable elements of a nine-element 3x3 PDF transformation matrix.
#[inline]
pub fn e(&self) -> PdfMatrixValue {
self.matrix[2][0]
}
/// Sets the value of `e`, the fifth of six floating-point values that represent
/// the six configurable elements of a nine-element 3x3 PDF transformation matrix.
#[inline]
pub fn set_e(&mut self, e: PdfMatrixValue) {
self.matrix[2][0] = e;
}
/// Returns the value of `f`, the sixth of six floating-point values that represent
/// the six configurable elements of a nine-element 3x3 PDF transformation matrix.
#[inline]
pub fn f(&self) -> PdfMatrixValue {
self.matrix[2][1]
}
/// Sets the value of `f`, the sixth of six floating-point values that represent
/// the six configurable elements of a nine-element 3x3 PDF transformation matrix.
#[inline]
pub fn set_f(&mut self, f: PdfMatrixValue) {
self.matrix[2][1] = f;
}
#[inline]
pub(crate) fn as_pdfium(&self) -> FS_MATRIX {
FS_MATRIX {
a: self.a(),
b: self.b(),
c: self.c(),
d: self.d(),
e: self.e(),
f: self.f(),
}
}
/// Returns the inverse of this [PdfMatrix].
#[inline]
pub fn invert(&self) -> PdfMatrix {
Self {
matrix: mat3_inv(self.matrix),
}
}
/// Returns the transpose of this [PdfMatrix].
#[inline]
pub fn transpose(&self) -> PdfMatrix {
Self {
matrix: mat3_transposed(self.matrix),
}
}
/// Returns the determinant of this [PdfMatrix].
#[inline]
pub fn determinant(&self) -> PdfMatrixValue {
mat3_det(self.matrix)
}
/// Returns the result of adding the given [PdfMatrix] to this [PdfMatrix].
#[inline]
pub fn add(&self, other: PdfMatrix) -> PdfMatrix {
Self {
matrix: mat3_add(self.matrix, other.matrix),
}
}
/// Returns the result of subtracting the given [PdfMatrix] from this [PdfMatrix].
#[inline]
pub fn subtract(&self, other: PdfMatrix) -> PdfMatrix {
Self {
matrix: mat3_sub(self.matrix, other.matrix),
}
}
/// Returns the result of multiplying this [PdfMatrix] by the given [PdfMatrix].
#[inline]
pub fn multiply(&self, other: PdfMatrix) -> PdfMatrix {
Self {
matrix: row_mat3_mul(self.matrix, other.matrix),
}
}
/// Returns the result of applying this [PdfMatrix] to the given coordinate pair expressed
/// as [PdfPoints].
#[inline]
pub fn apply_to_points(&self, x: PdfPoints, y: PdfPoints) -> (PdfPoints, PdfPoints) {
// The formula for applying transform to coordinates is provided in
// The PDF Reference Manual, version 1.7, on page 208.
(
PdfPoints::new(self.a() * x.value + self.c() * y.value + self.e()),
PdfPoints::new(self.b() * x.value + self.d() * y.value + self.f()),
)
}
create_transform_setters!(
Self,
Result<Self, PdfiumError>,
"this [PdfMatrix]",
"this [PdfMatrix].",
"this [PdfMatrix],"
);
// The internal implementation of the transform() function used by the create_transform_setters!() macro.
fn transform_impl(
mut self,
a: PdfMatrixValue,
b: PdfMatrixValue,
c: PdfMatrixValue,
d: PdfMatrixValue,
e: PdfMatrixValue,
f: PdfMatrixValue,
) -> Result<Self, PdfiumError> {
let result = row_mat3_mul(self.matrix, [[a, b, 0.0], [c, d, 0.0], [e, f, 1.0]]);
if mat3_det(result) == 0.0 {
Err(PdfiumError::InvalidTransformationMatrix)
} else {
self.matrix = result;
Ok(self)
}
}
// The internal implementation of the reset_matrix() function used by the create_transform_setters!() macro.
fn reset_matrix_impl(mut self, matrix: PdfMatrix) -> Result<Self, PdfiumError> {
self.set_a(matrix.a());
self.set_b(matrix.b());
self.set_c(matrix.c());
self.set_d(matrix.d());
self.set_e(matrix.e());
self.set_f(matrix.f());
Ok(self)
}
create_transform_getters!("this [PdfMatrix]", "this [PdfMatrix].", "this [PdfMatrix],");
// The internal implementation of the get_matrix_impl() function used by the create_transform_getters!() macro.
#[inline]
fn get_matrix_impl(&self) -> Result<PdfMatrix, PdfiumError> {
Ok(*self)
}
}
// We could derive PartialEq automatically, but it's good practice to implement PartialEq
// by hand when implementing Hash.
impl PartialEq for PdfMatrix {
fn eq(&self, other: &Self) -> bool {
self.a() == other.a()
&& self.b() == other.b()
&& self.c() == other.c()
&& self.d() == other.d()
&& self.e() == other.e()
&& self.f() == other.f()
}
}
// The PdfMatrixValue values inside PdfMatrix will never be NaN or Infinity, so these implementations
// of Eq and Hash are safe.
impl Eq for PdfMatrix {}
impl Hash for PdfMatrix {
fn hash<H: Hasher>(&self, state: &mut H) {
state.write_u32(self.a().to_bits());
state.write_u32(self.b().to_bits());
state.write_u32(self.c().to_bits());
state.write_u32(self.d().to_bits());
state.write_u32(self.e().to_bits());
state.write_u32(self.f().to_bits());
}
}
impl Add for PdfMatrix {
type Output = PdfMatrix;
#[inline]
fn add(self, rhs: Self) -> Self::Output {
// Add::add() shadows Self::add(), so we must be explicit about which function to call.
Self::add(&self, rhs)
}
}
impl Sub for PdfMatrix {
type Output = PdfMatrix;
#[inline]
fn sub(self, rhs: Self) -> Self::Output {
self.subtract(rhs)
}
}
impl Mul for PdfMatrix {
type Output = PdfMatrix;
#[inline]
fn mul(self, rhs: Self) -> Self::Output {
self.multiply(rhs)
}
}
#[cfg(test)]
mod tests {
use crate::matrix::PdfMatrix;
use crate::points::PdfPoints;
#[test]
fn test_matrix_apply_to_points() {
let delta_x = PdfPoints::new(50.0);
let delta_y = PdfPoints::new(-25.0);
let matrix = PdfMatrix::identity().translate(delta_x, delta_y).unwrap();
let x = PdfPoints::new(300.0);
let y = PdfPoints::new(400.0);
let result = matrix.apply_to_points(x, y);
assert_eq!(result.0, x + delta_x);
assert_eq!(result.1, y + delta_y);
}
}