1mod scan;
4
5use std::sync::{Arc, Mutex, PoisonError};
6
7use alpha2_model::{MAX_PDF_PAGES, MAX_SVG_DOCUMENT_BYTES, ProviderSource, Rgba};
8use hayro_svg::hayro_interpret::font::FontQuery;
9use hayro_svg::hayro_interpret::{InterpreterSettings, InterpreterWarning};
10use hayro_svg::hayro_syntax::{DecryptionError, LoadPdfError, Pdf};
11use hayro_svg::{RenderCache, SvgRenderSettings};
12
13use crate::api::{
14 Diagnostic, ExtensionError, Format, NoResources, ProbeReport, ResourceResolver, Timing,
15 VisualProvider, VisualSource,
16};
17use crate::budget::{check_canvas, check_document_size};
18use crate::svg::open_document_within;
19
20const PIXELS_PER_POINT: f32 = 4.0 / 3.0;
22
23pub(crate) const MAX_IMAGE_DATA_BYTES: usize = 256 * 1024 * 1024;
26
27pub struct PdfProvider;
29
30impl VisualProvider for PdfProvider {
31 fn name(&self) -> &'static str {
32 "pdf"
33 }
34
35 fn probe(&self, bytes: &[u8]) -> Option<Result<ProbeReport, ExtensionError>> {
36 if !looks_like_pdf(bytes) {
37 return None;
38 }
39 Some(load(bytes.to_vec().into()).and_then(|pdf| {
40 let first = &pdf.pages()[0];
41 let (width, height) = page_size(first.render_dimensions());
42 check_canvas(width, height)?;
43 Ok(ProbeReport {
44 format: Format::Pdf,
45 width,
46 height,
47 timing: Timing::Static,
48 loop_count: None,
49 items: page_numbers(pdf.pages().len()),
50 item: Some("1".into()),
51 color: None,
52 diagnostics: Vec::new(),
53 })
54 }))
55 }
56
57 fn open(
58 &self,
59 bytes: Arc<[u8]>,
60 source: &ProviderSource,
61 _resolver: &dyn ResourceResolver,
62 ) -> Result<Box<dyn VisualSource>, ExtensionError> {
63 if !looks_like_pdf(&bytes) {
64 return Err(ExtensionError::Mismatch("not a PDF document".into()));
65 }
66 let ProviderSource::Pdf { page, background } = source else {
67 return Err(ExtensionError::Mismatch("not a PDF source".into()));
68 };
69 Ok(Box::new(open_page(bytes, *page, *background)?))
70 }
71}
72
73fn looks_like_pdf(bytes: &[u8]) -> bool {
75 bytes[..bytes.len().min(1024)]
76 .windows(5)
77 .any(|window| window == b"%PDF-")
78}
79
80fn load(bytes: Arc<[u8]>) -> Result<Pdf, ExtensionError> {
82 check_document_size(&any_page(), &bytes, bytes.len() as u64)?;
83 let pdf = Pdf::new(Arc::new(bytes)).map_err(|error| match error {
84 LoadPdfError::Decryption(DecryptionError::PasswordProtected) => {
85 ExtensionError::Unsupported("the PDF is password-protected".into())
86 }
87 LoadPdfError::Decryption(error) => {
88 ExtensionError::Unsupported(format!("the PDF's encryption is not read: {error:?}"))
89 }
90 LoadPdfError::Invalid => ExtensionError::Malformed("not a readable PDF document".into()),
91 })?;
92 let pages = pdf.pages().len();
93 if pages == 0 {
94 return Err(ExtensionError::Malformed("the PDF has no pages".into()));
95 }
96 if pages > MAX_PDF_PAGES as usize {
97 return Err(ExtensionError::Budget(format!(
98 "the PDF has {pages} pages, more than the {MAX_PDF_PAGES} a provider reads"
99 )));
100 }
101 Ok(pdf)
102}
103
104fn any_page() -> ProviderSource {
105 ProviderSource::Pdf {
106 page: 1,
107 background: Rgba::new(1.0, 1.0, 1.0, 1.0),
108 }
109}
110
111#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
114fn page_size((width, height): (f32, f32)) -> (u32, u32) {
115 let side = |points: f32| (points * PIXELS_PER_POINT).round().max(1.0) as u32;
116 (side(width), side(height))
117}
118
119fn page_numbers(pages: usize) -> Vec<String> {
120 (1..=pages).map(|page| page.to_string()).collect()
121}
122
123fn open_page(
126 bytes: Arc<[u8]>,
127 number: u32,
128 background: Rgba,
129) -> Result<crate::svg::SvgSource, ExtensionError> {
130 let pdf = load(bytes)?;
131 let pages = pdf.pages();
132 let page_count = pages.len();
133 let page = usize::try_from(number)
134 .ok()
135 .and_then(|number| number.checked_sub(1))
136 .and_then(|index| pages.get(index))
137 .ok_or_else(|| {
138 ExtensionError::Malformed(format!("page {number} of {} does not exist", pages.len()))
139 })?;
140 let (width, height) = page_size(page.render_dimensions());
141 check_canvas(width, height)?;
142 let substituted = scan::check_page(page)?;
143 let missing_fonts = Arc::new(Mutex::new(substituted));
144 let warnings = Arc::new(Mutex::new(Vec::<InterpreterWarning>::new()));
145 let settings = InterpreterSettings {
146 font_resolver: {
147 let missing = Arc::clone(&missing_fonts);
148 Arc::new(move |query| match query {
149 FontQuery::Standard(font) => Some(font.get_font_data()),
150 FontQuery::Fallback(query) => {
151 let name = query
152 .post_script_name
153 .clone()
154 .or_else(|| query.font_name.clone())
155 .or_else(|| query.font_family.clone())
156 .unwrap_or_else(|| "an unnamed font".into());
157 let mut missing = missing.lock().unwrap_or_else(PoisonError::into_inner);
158 if !missing.contains(&name) {
159 missing.push(name);
160 }
161 Some(query.pick_standard_font().get_font_data())
162 }
163 })
164 },
165 warning_sink: {
166 let warnings = Arc::clone(&warnings);
167 Arc::new(move |warning| {
168 warnings
169 .lock()
170 .unwrap_or_else(PoisonError::into_inner)
171 .push(warning);
172 })
173 },
174 ..InterpreterSettings::default()
175 };
176 let svg = hayro_svg::convert(
177 page,
178 &RenderCache::new(),
179 &settings,
180 &SvgRenderSettings::default(),
181 );
182 drop(settings);
183 drop(pdf);
184 check_svg(&svg)?;
185 let mut source = open_document_within(svg.as_bytes(), &NoResources, svg.len())?
186 .scaled(PIXELS_PER_POINT)?
187 .with_background(background);
188 let report = source.report_mut();
189 report.format = Format::Pdf;
190 report.items = page_numbers(page_count);
191 report.item = Some(number.to_string());
192 let missing_fonts = missing_fonts
193 .lock()
194 .unwrap_or_else(PoisonError::into_inner)
195 .clone();
196 let warnings = warnings
197 .lock()
198 .unwrap_or_else(PoisonError::into_inner)
199 .clone();
200 report
201 .diagnostics
202 .extend(diagnostics(number, &missing_fonts, &warnings));
203 Ok(source)
204}
205
206fn check_svg(svg: &str) -> Result<(), ExtensionError> {
209 let mut data = 0;
210 let mut rest = svg;
211 while let Some(at) = rest.find("\"data:") {
212 let url = &rest[at + 1..];
213 let end = url.find('"').unwrap_or(url.len());
214 data += end;
215 rest = &url[end..];
216 }
217 if data > MAX_IMAGE_DATA_BYTES {
218 return Err(ExtensionError::Budget(format!(
219 "the page's images are {data} bytes in SVG, more than the {MAX_IMAGE_DATA_BYTES} a page may hold"
220 )));
221 }
222 let markup = svg.len() - data;
223 if markup > MAX_SVG_DOCUMENT_BYTES {
224 return Err(ExtensionError::Budget(format!(
225 "the page is {markup} bytes of SVG besides its images, more than the {MAX_SVG_DOCUMENT_BYTES} a document may be"
226 )));
227 }
228 Ok(())
229}
230
231fn diagnostics(
232 page: u32,
233 missing_fonts: &[String],
234 warnings: &[InterpreterWarning],
235) -> Vec<Diagnostic> {
236 let mut diagnostics = Vec::new();
237 if !missing_fonts.is_empty() {
238 diagnostics.push(Diagnostic::SubstitutedFont {
239 families: missing_fonts.join(", "),
240 });
241 }
242 let count = |wanted: fn(&InterpreterWarning) -> bool| {
243 warnings.iter().filter(|warning| wanted(warning)).count()
244 };
245 let images = count(|warning| matches!(warning, InterpreterWarning::ImageDecodeFailure));
246 if images > 0 {
247 diagnostics.push(Diagnostic::UnreadableImage {
248 href: format!("page {page}"),
249 reason: format!("{images} images do not decode"),
250 });
251 }
252 for (feature, found) in [
253 (
254 "an annotation appearance that does not load",
255 count(|warning| matches!(warning, InterpreterWarning::UnresolvedAnnotationAppearance)),
256 ),
257 (
258 "an embedded font that does not load",
259 count(|warning| matches!(warning, InterpreterWarning::UnsupportedFont)),
260 ),
261 ] {
262 if found > 0 {
263 diagnostics.push(Diagnostic::UnsupportedFeature {
264 feature: feature.into(),
265 path: format!("page {page}"),
266 count: found,
267 });
268 }
269 }
270 diagnostics
271}