Skip to content

PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust

Issue #2368: flattening keeps a NoRotate widget upright on a rotated page

wstock6e3a16f96a25
diff --git a/crates/edit/src/flatten.rs b/crates/edit/src/flatten.rs
index 8202c18d8..dbc356656 100644
--- a/crates/edit/src/flatten.rs
+++ b/crates/edit/src/flatten.rs
@@ -16,6 +16,7 @@ use pdfcraft_cos::{Dict, Document, ObjRef, Object};
use crate::{EditError, check, n, page_list, place_tagged};
const HIDDEN: i64 = 2;
+const NO_ROTATE: i64 = 16;
const NO_VIEW: i64 = 32;
fn nums(doc: &Document, o: Option<&Object>) -> Option<Vec<f64>> {
@@ -52,6 +53,28 @@ fn placement(doc: &Document, form: &Dict, rect: [f64; 4]) -> Option<[f64; 6]> {
Some([sx, 0.0, 0.0, sy, rect[0] - x0 * sx, rect[1] - y0 * sy])
}
+/// `m` followed by a turn of `degrees` (the page's /Rotate) about the upper-left corner of
+/// `rect`: a NoRotate annotation stays upright on a rotated page (ISO 32000-2 §12.5.3), its
+/// upper-left corner where it was, which is how MuPDF and Poppler draw it.
+fn upright(m: [f64; 6], degrees: i64, rect: [f64; 4]) -> [f64; 6] {
+ let (sin, cos) = match degrees.rem_euclid(360) {
+ 90 => (1.0, 0.0),
+ 180 => (0.0, -1.0),
+ 270 => (-1.0, 0.0),
+ _ => return m,
+ };
+ let (px, py) = (rect[0], rect[3]);
+ let t = [cos, sin, -sin, cos, px - px * cos + py * sin, py - px * sin - py * cos];
+ [
+ m[0] * t[0] + m[1] * t[2],
+ m[0] * t[1] + m[1] * t[3],
+ m[2] * t[0] + m[3] * t[2],
+ m[2] * t[1] + m[3] * t[3],
+ m[4] * t[0] + m[5] * t[2] + t[4],
+ m[4] * t[1] + m[5] * t[3] + t[5],
+ ]
+}
+
#[derive(Clone, Copy)]
enum Which {
/// Every comment and/or every form field.
@@ -157,6 +180,7 @@ fn flatten_which(doc: &mut Document, pages: &[usize], which: Which) -> Result<us
let mut gone_inline: HashSet<usize> = HashSet::new();
// Selected, but nothing was drawn for it: it stays, pop-up and reply handling included.
let mut undrawn: HashSet<ObjRef> = HashSet::new();
+ let rotation = page.dict.get(b"Rotate").map(|r| doc.resolve(r)).and_then(|r| r.as_int()).unwrap_or(0);
// A viewer draws many comments from their dictionary even when the file stores no
// appearance (notes, FreeText, ink, stamps…). Build the appearance it would show, so
// flattening keeps a comment that still has ink on screen instead of deleting it.
@@ -193,6 +217,7 @@ fn flatten_which(doc: &mut Document, pages: &[usize], which: Which) -> Result<us
if let (Some(ap), Some(rect)) = (appearance(doc, d), rect) {
let form = doc.get(ap).as_dict().cloned().unwrap_or_default();
if let Some(m) = placement(doc, &form, rect) {
+ let m = if flags & NO_ROTATE != 0 { upright(m, rotation, rect) } else { m };
let name = xobject_name(&existing_xo, &xobjects, &mut name_n);
content.push_str(&format!("q {} {} {} {} {} {} cm /{name} Do Q\n", n(m[0]), n(m[1]), n(m[2]), n(m[3]), n(m[4]), n(m[5])));
xobjects.set(name.into_bytes(), Object::Ref(ap));
diff --git a/crates/edit/src/tests.rs b/crates/edit/src/tests.rs
index b542142df..b023d6f7a 100644
--- a/crates/edit/src/tests.rs
+++ b/crates/edit/src/tests.rs
@@ -232,6 +232,42 @@ fn flattening_draws_appearances_into_the_page_and_removes_the_comments() {
assert!(marks_present(&doc).is_empty(), "flattened content is not a removable mark");
}
+/// #2368: a widget with /NoRotate on a rotated page is shown upright, turned with the page's
+/// /Rotate about its rectangle's upper-left corner (§12.5.3, as MuPDF and Poppler draw it).
+/// Flattening drew its appearance at the rectangle as it is, so the widget turned sideways and
+/// moved; a neighbour without the flag is unchanged, and so is everything on an unrotated page.
+#[test]
+fn flattening_keeps_a_no_rotate_widget_upright_on_a_rotated_page() {
+ let page = stream("", " ");
+ let form = "<< /Type /XObject /Subtype /Form /BBox [0 0 100 50] /Length 15 >>\nstream\n0 0 100 50 re f\nendstream";
+ for (rotate, plain, upright) in [
+ (0, "q 1 0 0 1 100 500 cm /PCFl0 Do Q", "q 1 0 0 1 100 500 cm /PCFl1 Do Q"),
+ (90, "q 1 0 0 1 100 500 cm /PCFl0 Do Q", "q 0 1 -1 0 150 550 cm /PCFl1 Do Q"),
+ (180, "q 1 0 0 1 100 500 cm /PCFl0 Do Q", "q -1 0 0 -1 100 600 cm /PCFl1 Do Q"),
+ (270, "q 1 0 0 1 100 500 cm /PCFl0 Do Q", "q 0 -1 1 0 50 550 cm /PCFl1 Do Q"),
+ ] {
+ let widget =
+ |flags: i64| format!("<< /Type /Annot /Subtype /Widget /FT /Tx /T (f{flags}) /Rect [100 500 200 550] /F {flags} /AP << /N 7 0 R >> >>");
+ let mut doc = build(&[
+ "<< /Type /Catalog /Pages 2 0 R /AcroForm << /Fields [4 0 R 5 0 R] >> >>",
+ "<< /Type /Pages /Kids [3 0 R] /Count 1 >>",
+ &format!(
+ "<< /Type /Page /Parent 2 0 R /MediaBox [0 0 600 800] /Rotate {rotate} /Resources << >> /Contents 6 0 R /Annots [4 0 R 5 0 R] >>"
+ ),
+ &widget(4),
+ &widget(4 | 16),
+ &page,
+ form,
+ ]);
+ assert_eq!(flatten(&mut doc, &[0], false, true).unwrap(), 2);
+ let doc = reopen(&doc);
+ let s = streams(&doc, 0);
+ let flat = s.last().unwrap();
+ assert!(flat.contains(plain), "/Rotate {rotate}: {flat}");
+ assert!(flat.contains(upright), "/Rotate {rotate}: {flat}");
+ }
+}
+
/// #1317: a cloudy square this builder cannot draw stays on the page. A solid square, a hidden
/// cloudy square and a cloudy square that already has an appearance keep their previous results.
#[test]