use std::panic::{AssertUnwindSafe, catch_unwind};
use std::sync::Arc;
use hayro::hayro_interpret::InterpreterSettings;
use hayro::hayro_interpret::util::TransformExt as _;
use hayro::hayro_syntax::page::Rotation as HayroRotation;
use hayro::hayro_syntax::{Pdf, PdfData};
use hayro::kurbo::Affine;
use hayro::vello_cpu::color::AlphaColor;
use hayro::vello_cpu::color::palette::css::TRANSPARENT;
use hayro::vello_cpu::{Pixmap, RasterizerSettings, RenderContext, Resources, TargetInit};
use hayro::{RenderCache, RenderSettings};
use laminapdf_cos::ByteSource;
use laminapdf_syntax::CancelToken;
use laminapdf_syntax::Rotation;
use crate::backend::{RenderBackend, RenderDocument};
use crate::error::RenderError;
use crate::request::{RenderLimits, RenderOpenOptions, RenderPage, RenderRequest, Rgba8, TilePlan};
use crate::source::read_all;
const NAME: &str = "hayro";
#[derive(Debug, Clone, Copy, Default)]
pub struct HayroBackend;
impl HayroBackend {
#[must_use]
pub const fn new() -> Self {
Self
}
}
impl RenderBackend for HayroBackend {
fn name(&self) -> &'static str {
NAME
}
fn version(&self) -> &'static str {
"0.8.0"
}
fn open(
&self,
source: Arc<dyn ByteSource>,
options: &RenderOpenOptions,
) -> Result<Box<dyn RenderDocument>, RenderError> {
let limits = *options.limits();
let data: PdfData = if source.as_slice().is_some() {
if source.len() > limits.max_input_bytes {
return Err(RenderError::InputTooLarge {
len: source.len(),
limit: limits.max_input_bytes,
});
}
Arc::new(InMemory(source)).into()
} else {
Arc::new(read_all(source.as_ref(), limits.max_input_bytes)?).into()
};
let password = options.password().unwrap_or("");
let loaded =
catch_unwind(AssertUnwindSafe(|| Pdf::new_with_password(data, password))).map_err(|_| failed("open"))?;
let pdf = loaded.map_err(|e| match e {
hayro::hayro_syntax::LoadPdfError::Decryption(hayro::hayro_syntax::DecryptionError::PasswordProtected) => {
if options.password().is_some() {
RenderError::WrongPassword
} else {
RenderError::PasswordRequired
}
}
_ => failed("open"),
})?;
Ok(Box::new(HayroDocument { pdf, limits }))
}
}
struct InMemory(Arc<dyn ByteSource>);
impl AsRef<[u8]> for InMemory {
fn as_ref(&self) -> &[u8] {
self.0.as_slice().unwrap_or_default()
}
}
struct HayroDocument {
pdf: Pdf,
limits: RenderLimits,
}
impl HayroDocument {
fn count(&self) -> u32 {
u32::try_from(self.pdf.pages().len()).unwrap_or(u32::MAX)
}
fn out_of_range(&self, index: u32) -> RenderError {
RenderError::PageIndexOutOfRange {
index,
count: self.count(),
}
}
}
impl RenderDocument for HayroDocument {
fn page_count(&self) -> u32 {
self.count()
}
fn page(&self, index: u32) -> Result<RenderPage, RenderError> {
let pages = self.pdf.pages();
let page = usize::try_from(index)
.ok()
.and_then(|i| pages.get(i))
.ok_or_else(|| self.out_of_range(index))?;
Ok(geometry(page))
}
fn limits(&self) -> &RenderLimits {
&self.limits
}
fn render(&self, request: &RenderRequest, out: &mut [u8], cancel: &CancelToken) -> Result<TilePlan, RenderError> {
let pages = self.pdf.pages();
let page = usize::try_from(request.page)
.ok()
.and_then(|i| pages.get(i))
.ok_or_else(|| self.out_of_range(request.page))?;
let info = geometry(page);
let plan = TilePlan::new(&info, request, &self.limits)?;
plan.check_buffer(out)?;
let (Ok(tw), Ok(th)) = (u16::try_from(plan.tile.width), u16::try_from(plan.tile.height)) else {
return Err(RenderError::TooLarge {
width: plan.tile.width.into(),
height: plan.tile.height.into(),
limit: u16::MAX.into(),
unit: "pixels per side (hayro)",
});
};
if cancel.is_cancelled() {
return Err(RenderError::Cancelled);
}
let transform =
Affine::new(plan.page_to_tile(&info, request.rotation)) * page.initial_transform(true).to_kurbo();
let settings = InterpreterSettings {
render_annotations: request.flags.annotations,
..InterpreterSettings::default()
};
let grayscale = request.flags.grayscale;
let background = match request.background {
Some(c) if !grayscale => AlphaColor::from_rgba8(c.r, c.g, c.b, c.a),
_ => TRANSPARENT,
};
let pixmap = catch_unwind(AssertUnwindSafe(|| {
let cache = RenderCache::new();
let mut ctx = RenderContext::new(tw, th);
hayro::render_into(page, &cache, &settings, &RenderSettings::default(), &mut ctx, transform);
ctx.flush();
let mut pixmap = Pixmap::new(tw, th);
ctx.render_with(
&mut pixmap,
&mut Resources::default(),
RasterizerSettings {
target_init: TargetInit::Clear(background),
..RasterizerSettings::default()
},
);
pixmap
}))
.map_err(|_| failed("render"))?;
if cancel.is_cancelled() {
return Err(RenderError::Cancelled);
}
let under = if grayscale { request.background } else { None };
unpremultiply_into(pixmap.data_as_u8_slice(), out, grayscale, under);
Ok(plan)
}
}
fn geometry(page: &hayro::hayro_syntax::page::Page<'_>) -> RenderPage {
let (width, height) = page.render_dimensions();
RenderPage {
width,
height,
rotate: match page.rotation() {
HayroRotation::None => Rotation::None,
HayroRotation::Horizontal => Rotation::Clockwise90,
HayroRotation::Flipped => Rotation::Half,
HayroRotation::FlippedHorizontal => Rotation::Clockwise270,
},
}
}
const fn failed(stage: &'static str) -> RenderError {
RenderError::Failed { backend: NAME, stage }
}
fn unpremultiply_into(src: &[u8], out: &mut [u8], grayscale: bool, under: Option<Rgba8>) {
for (&[r, g, b, a], d) in src.as_chunks::<4>().0.iter().zip(out.as_chunks_mut::<4>().0) {
let [r, g, b] = if grayscale { [luma(r, g, b); 3] } else { [r, g, b] };
let (r, g, b, a) = match under {
Some(u) if a < 255 => {
let rest = 255 - u32::from(a);
let ua = u32::from(u.a);
let over = |c: u8, uc: u8| clamp(u32::from(c) + (u32::from(uc) * ua * rest + 255 * 127) / (255 * 255));
(
over(r, u.r),
over(g, u.g),
over(b, u.b),
clamp(u32::from(a) + (ua * rest + 127) / 255),
)
}
_ => (r, g, b, a),
};
*d = if a == 0 {
[0, 0, 0, 0]
} else if a == 255 {
[r, g, b, a]
} else {
[unpremultiply(r, a), unpremultiply(g, a), unpremultiply(b, a), a]
};
}
}
fn clamp(v: u32) -> u8 {
u8::try_from(v.min(255)).unwrap_or(u8::MAX)
}
fn unpremultiply(c: u8, a: u8) -> u8 {
let v = (u32::from(c) * 255 + u32::from(a) / 2) / u32::from(a);
u8::try_from(v.min(255)).unwrap_or(u8::MAX)
}
fn luma(r: u8, g: u8, b: u8) -> u8 {
let v = (u32::from(r) * 299 + u32::from(g) * 587 + u32::from(b) * 114 + 500) / 1000;
u8::try_from(v.min(255)).unwrap_or(u8::MAX)
}
#[cfg(test)]
mod tests {
use super::{luma, unpremultiply, unpremultiply_into};
use crate::Rgba8;
#[test]
fn unpremultiply_restores_straight_channels() {
assert_eq!(unpremultiply(64, 128), 128);
assert_eq!(unpremultiply(128, 128), 255);
assert_eq!(unpremultiply(200, 128), 255);
}
#[test]
fn transparent_pixels_become_zero() {
let mut out = [9; 4];
unpremultiply_into(&[10, 20, 30, 0], &mut out, false, None);
assert_eq!(out, [0, 0, 0, 0]);
}
#[test]
fn grayscale_keeps_the_background_colour() {
let sepia = Rgba8 {
r: 255,
g: 240,
b: 200,
a: 255,
};
let mut out = [0; 4];
unpremultiply_into(&[0, 0, 0, 0], &mut out, true, Some(sepia));
assert_eq!(out, [255, 240, 200, 255]);
unpremultiply_into(&[255, 0, 0, 255], &mut out, true, Some(sepia));
assert_eq!(out, [76, 76, 76, 255]);
}
#[test]
fn grayscale_uses_rec_601_luma() {
assert_eq!(luma(255, 255, 255), 255);
assert_eq!(luma(255, 0, 0), 76);
}
}