[repo] Format, clippy clean, FORMATS.md, the full crate table in DEPENDENCIES.md

This commit is contained in:
2026-11-16 13:58:06 +01:00
parent 196802863a
commit a06e3f9b73
17 changed files with 2708 additions and 2125 deletions
+86 -27
View File
@@ -47,7 +47,9 @@ fn form_pdf() -> Vec<u8> {
}
doc.objects.insert(
radio_id,
Object::Dictionary(dictionary! {"FT" => "Btn", "Ff" => 49152, "T" => s("size"), "V" => "Off", "Kids" => kids.clone()}),
Object::Dictionary(
dictionary! {"FT" => "Btn", "Ff" => 49152, "T" => s("size"), "V" => "Off", "Kids" => kids.clone()},
),
);
let city = doc.add_object(dictionary! {
"Type" => "Annot", "Subtype" => "Widget", "FT" => "Ch", "Ff" => 131072, "T" => s("city"),
@@ -57,17 +59,23 @@ fn form_pdf() -> Vec<u8> {
let content = doc.add_object(Stream::new(dictionary! {}, b"BT /F1 12 Tf 72 780 Td (Form) Tj ET".to_vec()));
let mut annots = vec![name.into(), agree.into(), city.into()];
annots.extend(kids);
doc.objects.insert(page_id, Object::Dictionary(dictionary! {
"Type" => "Page", "Parent" => pages_id, "Contents" => content, "Annots" => annots,
"Resources" => dictionary! {"Font" => dictionary! {"F1" => helv}},
}));
doc.objects.insert(pages_id, Object::Dictionary(dictionary! {
"Type" => "Pages", "Kids" => vec![page_id.into()], "Count" => 1,
"MediaBox" => vec![0.into(), 0.into(), 595.into(), 842.into()],
}));
doc.objects.insert(
page_id,
Object::Dictionary(dictionary! {
"Type" => "Page", "Parent" => pages_id, "Contents" => content, "Annots" => annots,
"Resources" => dictionary! {"Font" => dictionary! {"F1" => helv}},
}),
);
doc.objects.insert(
pages_id,
Object::Dictionary(dictionary! {
"Type" => "Pages", "Kids" => vec![page_id.into()], "Count" => 1,
"MediaBox" => vec![0.into(), 0.into(), 595.into(), 842.into()],
}),
);
let catalog = doc.add_object(dictionary! {"Type" => "Catalog", "Pages" => pages_id,
"AcroForm" => dictionary! {"Fields" => vec![name.into(), agree.into(), radio_id.into(), city.into()],
"DA" => s("/Helv 12 Tf 0 g"), "DR" => dictionary! {"Font" => dictionary! {"Helv" => helv, "ZaDb" => zadb}}}});
"AcroForm" => dictionary! {"Fields" => vec![name.into(), agree.into(), radio_id.into(), city.into()],
"DA" => s("/Helv 12 Tf 0 g"), "DR" => dictionary! {"Font" => dictionary! {"Helv" => helv, "ZaDb" => zadb}}}});
doc.trailer.set("Root", catalog);
let mut out = Vec::new();
doc.save_to(&mut out).unwrap();
@@ -89,7 +97,10 @@ fn fill() -> Edits {
fn dark_pixels(img: &image::RgbImage, r: ViewRect, scale: f32) -> usize {
let (x0, y0) = ((r.x * scale) as u32, (r.y * scale) as u32);
let (x1, y1) = (((r.x + r.w) * scale) as u32, ((r.y + r.h) * scale) as u32);
(y0..y1.min(img.height())).flat_map(|y| (x0..x1.min(img.width())).map(move |x| (x, y))).filter(|&(x, y)| img.get_pixel(x, y).0.iter().map(|&c| c as u32).sum::<u32>() < 300).count()
(y0..y1.min(img.height()))
.flat_map(|y| (x0..x1.min(img.width())).map(move |x| (x, y)))
.filter(|&(x, y)| img.get_pixel(x, y).0.iter().map(|&c| c as u32).sum::<u32>() < 300)
.count()
}
#[test]
@@ -124,7 +135,13 @@ fn a_filled_form_flattens_into_page_text() {
let doc = Document::load_mem(&out).unwrap();
assert!(doc.catalog().unwrap().get(b"AcroForm").is_err(), "the form survived flattening");
let p = page_ids(&doc)[0];
assert!(doc.get_dictionary(p).unwrap().get(b"Annots").map(|a| a.as_array().map(|a| a.is_empty()).unwrap_or(false)).unwrap_or(true));
assert!(
doc.get_dictionary(p)
.unwrap()
.get(b"Annots")
.map(|a| a.as_array().map(|a| a.is_empty()).unwrap_or(false))
.unwrap_or(true)
);
}
#[test]
@@ -144,15 +161,36 @@ fn every_annotation() -> Vec<Annotation> {
let r = |x, y, w, h| ViewRect { x, y, w, h };
let a = |shape| Annotation { page: 0, shape, color: [1.0, 0.0, 0.0], opacity: 1.0, contents: None };
vec![
Annotation { color: [1.0, 0.9, 0.0], opacity: 0.5, ..a(Shape::Highlight { rects: vec![r(60.0, 100.0, 200.0, 30.0)] }) },
Annotation {
color: [1.0, 0.9, 0.0],
opacity: 0.5,
..a(Shape::Highlight { rects: vec![r(60.0, 100.0, 200.0, 30.0)] })
},
a(Shape::Underline { rects: vec![r(60.0, 150.0, 200.0, 20.0)] }),
a(Shape::Strike { rects: vec![r(60.0, 190.0, 200.0, 20.0)] }),
Annotation { contents: Some("Zkontrolovat čísla".into()), ..a(Shape::Note { at: ViewPoint { x: 400.0, y: 100.0 } }) },
a(Shape::FreeText { rect: r(60.0, 240.0, 400.0, 60.0), text: "Příliš žluťoučký kůň".into(), size: 18.0 }),
a(Shape::Ink { strokes: vec![vec![ViewPoint { x: 60.0, y: 330.0 }, ViewPoint { x: 160.0, y: 380.0 }, ViewPoint { x: 260.0, y: 330.0 }]], width: 3.0 }),
Annotation {
contents: Some("Zkontrolovat čísla".into()),
..a(Shape::Note { at: ViewPoint { x: 400.0, y: 100.0 } })
},
a(Shape::FreeText {
rect: r(60.0, 240.0, 400.0, 60.0), text: "Příliš žluťoučký kůň".into(), size: 18.0
}),
a(Shape::Ink {
strokes: vec![vec![
ViewPoint { x: 60.0, y: 330.0 },
ViewPoint { x: 160.0, y: 380.0 },
ViewPoint { x: 260.0, y: 330.0 },
]],
width: 3.0,
}),
a(Shape::Rectangle { rect: r(300.0, 330.0, 100.0, 50.0), width: 2.0 }),
a(Shape::Ellipse { rect: r(420.0, 330.0, 100.0, 50.0), width: 2.0 }),
a(Shape::Line { from: ViewPoint { x: 60.0, y: 420.0 }, to: ViewPoint { x: 300.0, y: 460.0 }, width: 3.0, arrow: true }),
a(Shape::Line {
from: ViewPoint { x: 60.0, y: 420.0 },
to: ViewPoint { x: 300.0, y: 460.0 },
width: 3.0,
arrow: true,
}),
]
}
@@ -173,7 +211,10 @@ fn every_annotation_kind_is_written_with_an_appearance_and_shows() {
String::from_utf8(d.get(b"Subtype").unwrap().as_name().unwrap().to_vec()).unwrap()
})
.collect();
assert_eq!(subtypes, ["Highlight", "Underline", "StrikeOut", "Text", "FreeText", "Ink", "Square", "Circle", "Line"]);
assert_eq!(
subtypes,
["Highlight", "Underline", "StrikeOut", "Text", "FreeText", "Ink", "Square", "Circle", "Line"]
);
let after = pictor_pdf::render_page(engine(), &out, None, 0, 595).unwrap().to_rgb8();
for a in every_annotation() {
let r = match &a.shape {
@@ -197,7 +238,9 @@ fn every_annotation_kind_is_written_with_an_appearance_and_shows() {
#[test]
fn a_free_text_on_a_rotated_page_lands_where_it_was_drawn() {
let input = pictor_pdf::reorganise(&numbered_pdf(1, "R"), None, &[pictor_pdf::PageSpec::Existing { index: 0, rotate: 90 }]).unwrap();
let input =
pictor_pdf::reorganise(&numbered_pdf(1, "R"), None, &[pictor_pdf::PageSpec::Existing { index: 0, rotate: 90 }])
.unwrap();
let rect = ViewRect { x: 100.0, y: 300.0, w: 300.0, h: 40.0 };
let edits = Edits {
annotations: vec![Annotation {
@@ -225,7 +268,9 @@ fn signature_png() -> Vec<u8> {
if on { image::Rgba([200, 0, 0, 255]) } else { image::Rgba([0, 0, 0, 0]) }
});
let mut out = Vec::new();
image::DynamicImage::ImageRgba8(img).write_to(&mut std::io::Cursor::new(&mut out), image::ImageFormat::Png).unwrap();
image::DynamicImage::ImageRgba8(img)
.write_to(&mut std::io::Cursor::new(&mut out), image::ImageFormat::Png)
.unwrap();
out
}
@@ -236,10 +281,13 @@ fn a_signature_stamp_is_burned_into_the_page() {
let edits = Edits { stamps: vec![Stamp { page: 1, rect, image: signature_png() }], ..Default::default() };
let out = pictor_pdf::apply(engine(), &input, None, &edits).unwrap().bytes;
let img = pictor_pdf::render_page(engine(), &out, None, 1, 595).unwrap().to_rgb8();
let red = (700..780).flat_map(|y| (300..500).map(move |x| (x, y))).filter(|&(x, y)| {
let p = img.get_pixel(x, y).0;
p[0] > 150 && p[1] < 80
}).count();
let red = (700..780)
.flat_map(|y| (300..500).map(move |x| (x, y)))
.filter(|&(x, y)| {
let p = img.get_pixel(x, y).0;
p[0] > 150 && p[1] < 80
})
.count();
assert!(red > 100, "the signature is not visible ({red} red pixels)");
// Transparent parts stay transparent: the page is white just above the signature's line.
assert_eq!(img.get_pixel(305, 702).0, [255, 255, 255]);
@@ -259,14 +307,25 @@ fn a_protected_document_stays_protected_with_its_password() {
let out = pictor_pdf::apply(engine(), &locked, Some("pw"), &edits).unwrap().bytes;
assert!(pictor_pdf::is_encrypted(&out));
assert_eq!(pictor_pdf::extract_text(engine(), &out, Some("pw")).unwrap()[0].trim(), "Secret page 1");
let removed = pictor_pdf::apply(engine(), &locked, Some("pw"), &Edits { security: Security::Remove, ..Default::default() }).unwrap().bytes;
let removed =
pictor_pdf::apply(engine(), &locked, Some("pw"), &Edits { security: Security::Remove, ..Default::default() })
.unwrap()
.bytes;
assert!(!pictor_pdf::is_encrypted(&removed));
assert!(pictor_pdf::apply(engine(), &locked, Some("wrong"), &edits).is_err());
}
#[test]
fn edits_serialise_for_the_worker() {
let edits = Edits { annotations: every_annotation(), stamps: vec![Stamp { page: 0, rect: ViewRect { x: 1.0, y: 1.0, w: 2.0, h: 2.0 }, image: vec![0, 1, 2, 250, 255] }], ..Default::default() };
let edits = Edits {
annotations: every_annotation(),
stamps: vec![Stamp {
page: 0,
rect: ViewRect { x: 1.0, y: 1.0, w: 2.0, h: 2.0 },
image: vec![0, 1, 2, 250, 255],
}],
..Default::default()
};
let json = serde_json::to_string(&edits).unwrap();
let back: Edits = serde_json::from_str(&json).unwrap();
assert_eq!(back.annotations.len(), 9);