#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Matrix {
pub a: f64,
pub b: f64,
pub c: f64,
pub d: f64,
pub e: f64,
pub f: f64,
}
impl Default for Matrix {
fn default() -> Self {
Self::IDENTITY
}
}
impl Matrix {
pub const IDENTITY: Self = Self::new([1.0, 0.0, 0.0, 1.0, 0.0, 0.0]);
#[must_use]
#[expect(clippy::many_single_char_names, reason = "a–f are the matrix entries of §8.3.3")]
pub const fn new([a, b, c, d, e, f]: [f64; 6]) -> Self {
Self { a, b, c, d, e, f }
}
#[must_use]
pub const fn to_array(self) -> [f64; 6] {
[self.a, self.b, self.c, self.d, self.e, self.f]
}
#[must_use]
pub const fn translate(x: f64, y: f64) -> Self {
Self::new([1.0, 0.0, 0.0, 1.0, x, y])
}
#[must_use]
pub const fn scale(sx: f64, sy: f64) -> Self {
Self::new([sx, 0.0, 0.0, sy, 0.0, 0.0])
}
#[must_use]
pub fn rotate(degrees: f64) -> Self {
let (s, c) = degrees.to_radians().sin_cos();
Self::new([c, s, -s, c, 0.0, 0.0])
}
#[must_use]
pub fn then(&self, next: &Self) -> Self {
Self::new([
self.a * next.a + self.b * next.c,
self.a * next.b + self.b * next.d,
self.c * next.a + self.d * next.c,
self.c * next.b + self.d * next.d,
self.e * next.a + self.f * next.c + next.e,
self.e * next.b + self.f * next.d + next.f,
])
}
#[must_use]
pub fn after(&self, first: &Self) -> Self {
first.then(self)
}
#[must_use]
pub fn invert(&self) -> Option<Self> {
let det = self.a * self.d - self.b * self.c;
if det == 0.0 || !det.is_finite() {
return None;
}
let (a, b, c, d) = (self.d / det, -self.b / det, -self.c / det, self.a / det);
Some(Self::new([
a,
b,
c,
d,
-(a * self.e + c * self.f),
-(b * self.e + d * self.f),
]))
}
#[must_use]
pub fn apply(&self, x: f64, y: f64) -> (f64, f64) {
(self.a * x + self.c * y + self.e, self.b * x + self.d * y + self.f)
}
#[must_use]
pub fn from_slice(values: &[f64]) -> Option<Self> {
<[f64; 6]>::try_from(values).ok().map(Self::new)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum FillRule {
#[default]
NonZero,
EvenOdd,
}
impl FillRule {
pub const ALL: [Self; 2] = [Self::NonZero, Self::EvenOdd];
}
#[derive(Debug, Clone, PartialEq, Default)]
#[non_exhaustive]
pub struct Dash {
pub array: Vec<f64>,
pub phase: f64,
}
impl Dash {
#[must_use]
pub const fn new(array: Vec<f64>, phase: f64) -> Self {
Self { array, phase }
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Default)]
#[non_exhaustive]
pub enum BlendMode {
#[default]
Normal,
Multiply,
Screen,
Overlay,
Darken,
Lighten,
ColorDodge,
ColorBurn,
HardLight,
SoftLight,
Difference,
Exclusion,
Hue,
Saturation,
Color,
Luminosity,
}
impl BlendMode {
pub const ALL: [Self; 16] = [
Self::Normal,
Self::Multiply,
Self::Screen,
Self::Overlay,
Self::Darken,
Self::Lighten,
Self::ColorDodge,
Self::ColorBurn,
Self::HardLight,
Self::SoftLight,
Self::Difference,
Self::Exclusion,
Self::Hue,
Self::Saturation,
Self::Color,
Self::Luminosity,
];
#[must_use]
pub const fn pdf_name(self) -> &'static str {
match self {
Self::Normal => "Normal",
Self::Multiply => "Multiply",
Self::Screen => "Screen",
Self::Overlay => "Overlay",
Self::Darken => "Darken",
Self::Lighten => "Lighten",
Self::ColorDodge => "ColorDodge",
Self::ColorBurn => "ColorBurn",
Self::HardLight => "HardLight",
Self::SoftLight => "SoftLight",
Self::Difference => "Difference",
Self::Exclusion => "Exclusion",
Self::Hue => "Hue",
Self::Saturation => "Saturation",
Self::Color => "Color",
Self::Luminosity => "Luminosity",
}
}
#[must_use]
pub fn from_pdf_name(name: &[u8]) -> Option<Self> {
if name == b"Compatible" {
return Some(Self::Normal);
}
Self::ALL.into_iter().find(|mode| mode.pdf_name().as_bytes() == name)
}
}
#[cfg(test)]
mod tests {
use super::{BlendMode, FillRule, Matrix};
const SCALE: Matrix = Matrix::scale(2.0, 3.0);
const SHIFT: Matrix = Matrix::translate(10.0, 20.0);
#[test]
fn then_should_apply_the_receiver_first() {
assert_eq!(SCALE.then(&SHIFT).apply(1.0, 1.0), (12.0, 23.0));
assert_eq!(SHIFT.then(&SCALE).apply(1.0, 1.0), (22.0, 63.0));
}
#[test]
fn after_should_apply_its_argument_first() {
assert_eq!(SHIFT.after(&SCALE).apply(1.0, 1.0), (12.0, 23.0));
assert_eq!(SHIFT.after(&SCALE), SCALE.then(&SHIFT));
}
#[test]
fn then_should_be_the_spec_product_cm_uses() {
let m = Matrix::new([2.0, 0.0, 0.0, 2.0, 0.0, 0.0]);
let ctm = Matrix::new([1.0, 0.0, 0.0, 1.0, 5.0, 7.0]);
assert_eq!(m.then(&ctm).to_array(), [2.0, 0.0, 0.0, 2.0, 5.0, 7.0]);
}
#[test]
fn invert_should_undo_the_matrix_and_refuse_a_singular_one() {
let m = Matrix::rotate(30.0).then(&SCALE).then(&SHIFT);
let back = m.invert().map(|i| {
let (x, y) = m.apply(4.0, -2.0);
i.apply(x, y)
});
let Some((x, y)) = back else {
panic!("a rotation, scale and shift is invertible");
};
assert!((x - 4.0).abs() < 1e-9 && (y + 2.0).abs() < 1e-9);
assert_eq!(Matrix::scale(0.0, 1.0).invert(), None);
}
#[test]
fn rotate_should_turn_the_x_axis_towards_the_y_axis() {
let (x, y) = Matrix::rotate(90.0).apply(1.0, 0.0);
assert!(x.abs() < 1e-12 && (y - 1.0).abs() < 1e-12);
}
#[test]
fn from_slice_should_take_exactly_six_numbers() {
assert_eq!(
Matrix::from_slice(&[1.0, 0.0, 0.0, 1.0, 0.0, 0.0]),
Some(Matrix::IDENTITY)
);
assert_eq!(Matrix::from_slice(&[1.0; 5]), None);
assert_eq!(Matrix::from_slice(&[1.0; 7]), None);
}
#[test]
fn all_should_list_every_fill_rule_once_in_declaration_order() {
let index = |v: FillRule| match v {
FillRule::NonZero => 0,
FillRule::EvenOdd => 1,
};
assert!(FillRule::ALL.iter().enumerate().all(|(i, v)| index(*v) == i));
}
#[test]
fn all_should_list_every_blend_mode_once_in_declaration_order() {
let index = |v: BlendMode| match v {
BlendMode::Normal => 0,
BlendMode::Multiply => 1,
BlendMode::Screen => 2,
BlendMode::Overlay => 3,
BlendMode::Darken => 4,
BlendMode::Lighten => 5,
BlendMode::ColorDodge => 6,
BlendMode::ColorBurn => 7,
BlendMode::HardLight => 8,
BlendMode::SoftLight => 9,
BlendMode::Difference => 10,
BlendMode::Exclusion => 11,
BlendMode::Hue => 12,
BlendMode::Saturation => 13,
BlendMode::Color => 14,
BlendMode::Luminosity => 15,
};
assert_eq!(
BlendMode::ALL.map(index),
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
);
}
#[test]
fn from_pdf_name_should_read_every_mode_and_compatible_as_normal() {
let read = BlendMode::ALL.map(|m| BlendMode::from_pdf_name(m.pdf_name().as_bytes()));
assert_eq!(read, BlendMode::ALL.map(Some));
assert_eq!(BlendMode::from_pdf_name(b"Compatible"), Some(BlendMode::Normal));
assert_eq!(BlendMode::from_pdf_name(b"Sparkle"), None);
}
}