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
//! 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::page::PdfPoints;
use crate::{create_transform_getters, create_transform_setters};
use std::hash::{Hash, Hasher};
use vecmath::{mat3_det, row_mat3_mul};
pub type PdfMatrixValue = f32;
/// Six floating-point values 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 {
pub a: PdfMatrixValue,
pub b: PdfMatrixValue,
pub c: PdfMatrixValue,
pub d: PdfMatrixValue,
pub e: PdfMatrixValue,
pub f: 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 { a, b, c, d, e, f }
}
/// 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)
}
#[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,
}
}
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.a, self.b, 0.0],
[self.c, self.d, 0.0],
[self.e, self.f, 1.0],
],
[[a, b, 0.0], [c, d, 0.0], [e, f, 1.0]],
);
if mat3_det(result) == 0.0 {
Err(PdfiumError::InvalidTransformationMatrix)
} else {
self.a = result[0][0];
self.b = result[0][1];
self.c = result[1][0];
self.d = result[1][1];
self.e = result[2][0];
self.f = result[2][1];
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());
}
}