mod scan;
use std::sync::{Arc, Mutex, PoisonError};
use alpha2_model::{MAX_PDF_PAGES, MAX_SVG_DOCUMENT_BYTES, ProviderSource, Rgba};
use hayro_svg::hayro_interpret::font::FontQuery;
use hayro_svg::hayro_interpret::{InterpreterSettings, InterpreterWarning};
use hayro_svg::hayro_syntax::{DecryptionError, LoadPdfError, Pdf};
use hayro_svg::{RenderCache, SvgRenderSettings};
use crate::api::{
Diagnostic, ExtensionError, Format, NoResources, ProbeReport, ResourceResolver, VisualProvider,
VisualSource,
};
use crate::budget::{check_canvas, check_document_size};
use crate::svg::open_document_within;
const PIXELS_PER_POINT: f32 = 4.0 / 3.0;
pub(crate) const MAX_IMAGE_DATA_BYTES: usize = 256 * 1024 * 1024;
pub struct PdfProvider;
impl VisualProvider for PdfProvider {
fn name(&self) -> &'static str {
"pdf"
}
fn probe(&self, bytes: &[u8]) -> Option<Result<ProbeReport, ExtensionError>> {
if !looks_like_pdf(bytes) {
return None;
}
Some(
open_page(bytes.to_vec().into(), 1, any_page_background())
.map(|page| page.report().clone()),
)
}
fn open(
&self,
bytes: Arc<[u8]>,
source: &ProviderSource,
_resolver: &dyn ResourceResolver,
) -> Result<Box<dyn VisualSource>, ExtensionError> {
if !looks_like_pdf(&bytes) {
return Err(ExtensionError::Mismatch("not a PDF document".into()));
}
let ProviderSource::Pdf { page, background } = source else {
return Err(ExtensionError::Mismatch("not a PDF source".into()));
};
Ok(Box::new(open_page(bytes, *page, *background)?))
}
}
fn looks_like_pdf(bytes: &[u8]) -> bool {
bytes[..bytes.len().min(1024)]
.windows(5)
.any(|window| window == b"%PDF-")
}
fn load(bytes: Arc<[u8]>) -> Result<Pdf, ExtensionError> {
check_document_size(&any_page(), &bytes, bytes.len() as u64)?;
let pdf = Pdf::new(Arc::new(bytes)).map_err(|error| match error {
LoadPdfError::Decryption(DecryptionError::PasswordProtected) => {
ExtensionError::Unsupported("the PDF is password-protected".into())
}
LoadPdfError::Decryption(error) => {
ExtensionError::Unsupported(format!("the PDF's encryption is not read: {error:?}"))
}
LoadPdfError::Invalid => ExtensionError::Malformed("not a readable PDF document".into()),
})?;
let pages = pdf.pages().len();
if pages == 0 {
return Err(ExtensionError::Malformed("the PDF has no pages".into()));
}
if pages > MAX_PDF_PAGES as usize {
return Err(ExtensionError::Budget(format!(
"the PDF has {pages} pages, more than the {MAX_PDF_PAGES} a provider reads"
)));
}
Ok(pdf)
}
fn any_page() -> ProviderSource {
ProviderSource::Pdf {
page: 1,
background: any_page_background(),
}
}
const fn any_page_background() -> Rgba {
Rgba::new(1.0, 1.0, 1.0, 1.0)
}
#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
fn page_size((width, height): (f32, f32)) -> (u32, u32) {
let side = |points: f32| (points * PIXELS_PER_POINT).round().max(1.0) as u32;
(side(width), side(height))
}
fn page_numbers(pages: usize) -> Vec<String> {
(1..=pages).map(|page| page.to_string()).collect()
}
fn open_page(
bytes: Arc<[u8]>,
number: u32,
background: Rgba,
) -> Result<crate::svg::SvgSource, ExtensionError> {
let pdf = load(bytes)?;
let pages = pdf.pages();
let page_count = pages.len();
let page = usize::try_from(number)
.ok()
.and_then(|number| number.checked_sub(1))
.and_then(|index| pages.get(index))
.ok_or_else(|| {
ExtensionError::Malformed(format!("page {number} of {} does not exist", pages.len()))
})?;
let (width, height) = page_size(page.render_dimensions());
check_canvas(width, height)?;
let substituted = scan::check_page(page)?;
let missing_fonts = Arc::new(Mutex::new(substituted));
let warnings = Arc::new(Mutex::new(Vec::<InterpreterWarning>::new()));
let settings = InterpreterSettings {
font_resolver: {
let missing = Arc::clone(&missing_fonts);
Arc::new(move |query| match query {
FontQuery::Standard(font) => Some(font.get_font_data()),
FontQuery::Fallback(query) => {
let name = query
.post_script_name
.clone()
.or_else(|| query.font_name.clone())
.or_else(|| query.font_family.clone())
.unwrap_or_else(|| "an unnamed font".into());
let mut missing = missing.lock().unwrap_or_else(PoisonError::into_inner);
if !missing.contains(&name) {
missing.push(name);
}
Some(query.pick_standard_font().get_font_data())
}
})
},
warning_sink: {
let warnings = Arc::clone(&warnings);
Arc::new(move |warning| {
warnings
.lock()
.unwrap_or_else(PoisonError::into_inner)
.push(warning);
})
},
..InterpreterSettings::default()
};
let svg = hayro_svg::convert(
page,
&RenderCache::new(),
&settings,
&SvgRenderSettings::default(),
);
drop(settings);
drop(pdf);
check_svg(&svg)?;
let images = u64::try_from(MAX_IMAGE_DATA_BYTES).unwrap_or(u64::MAX);
let mut source = open_document_within(svg.as_bytes(), &NoResources, svg.len(), images)?
.scaled(PIXELS_PER_POINT)?
.with_background(background);
let report = source.report_mut();
report.format = Format::Pdf;
report.items = page_numbers(page_count);
report.item = Some(number.to_string());
let missing_fonts = missing_fonts
.lock()
.unwrap_or_else(PoisonError::into_inner)
.clone();
let warnings = warnings
.lock()
.unwrap_or_else(PoisonError::into_inner)
.clone();
report
.diagnostics
.extend(diagnostics(number, &missing_fonts, &warnings));
Ok(source)
}
fn check_svg(svg: &str) -> Result<(), ExtensionError> {
let mut data = 0;
let mut rest = svg;
while let Some(at) = rest.find("\"data:") {
let url = &rest[at + 1..];
let end = url.find('"').unwrap_or(url.len());
data += end;
rest = &url[end..];
}
if data > MAX_IMAGE_DATA_BYTES {
return Err(ExtensionError::Budget(format!(
"the page's images are {data} bytes in SVG, more than the {MAX_IMAGE_DATA_BYTES} a page may hold"
)));
}
let markup = svg.len() - data;
if markup > MAX_SVG_DOCUMENT_BYTES {
return Err(ExtensionError::Budget(format!(
"the page is {markup} bytes of SVG besides its images, more than the {MAX_SVG_DOCUMENT_BYTES} a document may be"
)));
}
Ok(())
}
fn diagnostics(
page: u32,
missing_fonts: &[String],
warnings: &[InterpreterWarning],
) -> Vec<Diagnostic> {
let mut diagnostics = Vec::new();
if !missing_fonts.is_empty() {
diagnostics.push(Diagnostic::SubstitutedFont {
families: missing_fonts.join(", "),
});
}
let count = |wanted: fn(&InterpreterWarning) -> bool| {
warnings.iter().filter(|warning| wanted(warning)).count()
};
let images = count(|warning| matches!(warning, InterpreterWarning::ImageDecodeFailure));
if images > 0 {
diagnostics.push(Diagnostic::UnreadableImage {
href: format!("page {page}"),
reason: format!("{images} images do not decode"),
});
}
for (feature, found) in [
(
"an annotation appearance that does not load",
count(|warning| matches!(warning, InterpreterWarning::UnresolvedAnnotationAppearance)),
),
(
"an embedded font that does not load",
count(|warning| matches!(warning, InterpreterWarning::UnsupportedFont)),
),
] {
if found > 0 {
diagnostics.push(Diagnostic::UnsupportedFeature {
feature: feature.into(),
path: format!("page {page}"),
count: found,
});
}
}
diagnostics
}