mirror of
https://github.com/storytold/pdfcraft.git
synced 2026-11-16 07:25:23 +00:00
fix(render): a form field's widget is drawn above the page's comments (hayro-interpret patch 86)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Pq7HyDBXygTf7jcqdsjcfn
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
102cae3bef
commit
2bace2144f
@@ -16,3 +16,4 @@ mod stroke_degenerate_cubic;
|
||||
mod substitute_widths;
|
||||
mod tiling_cell_placement;
|
||||
mod type3_text_clip;
|
||||
mod widgets_above_annotations;
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
//! A contributor-original synthetic page: a check box widget listed before a filled square
|
||||
//! comment that covers it. Form fields' widgets are drawn after the page's other annotations
|
||||
//! (hayro-interpret patch (86)), so the check box shows; the comment covered it before.
|
||||
|
||||
use pdfcraft_render::{PageRenderer, RenderConfig, RenderRequest};
|
||||
use std::sync::Arc;
|
||||
|
||||
/// A 200 × 200 pt page whose `/Annots` lists `order` of: `W`, a check box drawing a green box,
|
||||
/// `S`, a square comment drawing a black one over it, and `R`, a larger red square comment.
|
||||
fn pdf(order: &str) -> Vec<u8> {
|
||||
let annot = |c: char| match c {
|
||||
'W' => "6 0 R",
|
||||
'S' => "7 0 R",
|
||||
_ => "8 0 R",
|
||||
};
|
||||
let annots: Vec<&str> = order.chars().map(annot).collect();
|
||||
let form = |rgb: &str| {
|
||||
let content = format!("{rgb} rg 0 0 100 100 re f");
|
||||
format!("<< /Type /XObject /Subtype /Form /BBox [0 0 100 100] /Length {} >>\nstream\n{content}\nendstream", content.len())
|
||||
};
|
||||
let objects = [
|
||||
"<< /Type /Catalog /Pages 2 0 R /AcroForm << /Fields [6 0 R] >> >>".to_string(),
|
||||
"<< /Type /Pages /Kids [3 0 R] /Count 1 >>".to_string(),
|
||||
format!("<< /Type /Page /Parent 2 0 R /MediaBox [0 0 200 200] /Annots [{}] >>", annots.join(" ")),
|
||||
form("0 1 0"),
|
||||
form("0 0 0"),
|
||||
"<< /Type /Annot /Subtype /Widget /FT /Btn /T (box) /F 4 /Rect [50 50 150 150] /AS /Off /AP << /N << /Off 4 0 R /Yes 4 0 R >> >> >>"
|
||||
.to_string(),
|
||||
"<< /Type /Annot /Subtype /Square /F 4 /C [0 0 0] /Rect [40 40 160 160] /AP << /N 5 0 R >> >>".to_string(),
|
||||
"<< /Type /Annot /Subtype /Square /F 4 /C [1 0 0] /Rect [30 30 170 170] /AP << /N 9 0 R >> >>".to_string(),
|
||||
form("1 0 0"),
|
||||
];
|
||||
let mut bytes = b"%PDF-1.7\n".to_vec();
|
||||
let mut offsets = Vec::new();
|
||||
for (i, object) in objects.iter().enumerate() {
|
||||
offsets.push(bytes.len());
|
||||
bytes.extend_from_slice(format!("{} 0 obj\n{object}\nendobj\n", i + 1).as_bytes());
|
||||
}
|
||||
let xref = bytes.len();
|
||||
bytes.extend_from_slice(format!("xref\n0 {}\n0000000000 65535 f \n", objects.len() + 1).as_bytes());
|
||||
for offset in offsets {
|
||||
bytes.extend_from_slice(format!("{offset:010} 00000 n \n").as_bytes());
|
||||
}
|
||||
bytes.extend_from_slice(format!("trailer << /Size {} /Root 1 0 R >>\nstartxref\n{xref}\n%%EOF\n", objects.len() + 1).as_bytes());
|
||||
bytes
|
||||
}
|
||||
|
||||
/// The colour at the page's centre.
|
||||
fn centre(order: &str) -> [u8; 3] {
|
||||
let mut renderer = PageRenderer::new(Arc::new(pdf(order)), RenderConfig::default());
|
||||
let out = renderer.render(RenderRequest { page: 0, scale: 1.0, ..Default::default() });
|
||||
assert!(out.error.is_none(), "{:?}", out.error);
|
||||
let at = (100 * out.width as usize + 100) * 4;
|
||||
let px = out.rgba.get(at..at + 3).expect("the centre lies on the page");
|
||||
[px[0], px[1], px[2]]
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_widget_is_drawn_above_the_comments_on_its_page() {
|
||||
assert_eq!(centre("WS"), [0, 255, 0], "a check box listed before a comment over it");
|
||||
assert_eq!(centre("SW"), [0, 255, 0], "a check box listed after it");
|
||||
// Comments keep their `/Annots` order among themselves.
|
||||
assert_eq!(centre("RS"), [0, 0, 0], "the black square listed last is on top");
|
||||
assert_eq!(centre("SR"), [255, 0, 0], "the red square listed last is on top");
|
||||
}
|
||||
Vendored
+1
-1
File diff suppressed because one or more lines are too long
+6
-1
@@ -200,7 +200,12 @@ pub fn interpret_page<'a>(
|
||||
// PdfCraft patch (29): bracket the annotations so devices can tell their paints from
|
||||
// the document's own (the reading-colours setting recolours the document, not comments).
|
||||
device.begin_annotations();
|
||||
for annot in annot_arr.iter::<Dict<'_>>() {
|
||||
// PdfCraft patch (86): form fields' widgets are drawn after the page's other
|
||||
// annotations, each group in `/Annots` order, as other readers draw them: a filled
|
||||
// comment listed after a check box covered the box. ISO 32000-2 doesn't order the two.
|
||||
let mut annots: Vec<Dict<'_>> = annot_arr.iter::<Dict<'_>>().collect();
|
||||
annots.sort_by_key(|a| a.get::<hayro_syntax::object::Name<'_>>(SUBTYPE).as_deref() == Some(b"Widget"));
|
||||
for annot in annots {
|
||||
let flags = annot.get::<u32>(F).unwrap_or(0);
|
||||
|
||||
// Annotation should be hidden (Hidden = bit 2, NoView = bit 6).
|
||||
|
||||
Reference in New Issue
Block a user