70 lines
2.0 KiB
Rust
70 lines
2.0 KiB
Rust
//! Graphics state produced by the pdfTeX extension whatsits.
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
|
|
pub enum PdfGraphics {
|
|
Save,
|
|
Matrix(PdfMatrix),
|
|
Restore { shift: [i64; 2] },
|
|
}
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
|
|
pub struct PdfMatrix([u64; 4]);
|
|
|
|
impl PdfMatrix {
|
|
pub(crate) fn parse(text: &str) -> Result<Self, &'static str> {
|
|
let mut words = text.split_ascii_whitespace();
|
|
let mut coefficients = [0; 4];
|
|
for coefficient in &mut coefficients {
|
|
*coefficient = words
|
|
.next()
|
|
.and_then(|word| word.parse::<f64>().ok())
|
|
.filter(|value| value.is_finite())
|
|
.ok_or("Unrecognized format")?
|
|
.to_bits();
|
|
}
|
|
if words.next().is_some() {
|
|
return Err("Unrecognized format");
|
|
}
|
|
Ok(Self(coefficients))
|
|
}
|
|
|
|
pub(crate) fn about(self, x: f64, y: f64, y_down: bool) -> Affine {
|
|
let [a, mut b, mut c, d] = self.0.map(f64::from_bits);
|
|
if y_down {
|
|
b = -b;
|
|
c = -c;
|
|
}
|
|
Affine([a, b, c, d, (1.0 - a) * x - c * y, (1.0 - d) * y - b * x])
|
|
}
|
|
}
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq)]
|
|
pub(crate) struct Affine(pub [f64; 6]);
|
|
|
|
impl Affine {
|
|
pub const IDENTITY: Self = Self([1.0, 0.0, 0.0, 1.0, 0.0, 0.0]);
|
|
|
|
pub fn translation(x: f64, y: f64) -> Self {
|
|
Self([1.0, 0.0, 0.0, 1.0, x, y])
|
|
}
|
|
|
|
pub fn concatenate(self, incoming: Self) -> Self {
|
|
let [a, b, c, d, ..] = self.0;
|
|
let [p, q, r, s, t, u] = incoming.0;
|
|
let [tx, ty] = self.point(t, u);
|
|
Self([
|
|
a * p + c * q,
|
|
b * p + d * q,
|
|
a * r + c * s,
|
|
b * r + d * s,
|
|
tx,
|
|
ty,
|
|
])
|
|
}
|
|
|
|
pub fn point(self, x: f64, y: f64) -> [f64; 2] {
|
|
let [a, b, c, d, e, f] = self.0;
|
|
[a * x + c * y + e, b * x + d * y + f]
|
|
}
|
|
}
|