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