pdf: fix swapped 90/270 page rotation matrices (issue #321)
- Commit
6afbc6244afdb2bb06cc81eeaee481f54a8ec870- Author
- Vayun Mathur <[email protected]>
- When
- 2026-08-08 00:35 UTC (4 months ago)
- Parents
fcd9013
Diff
pdf/src/main/rust/src/geometry.rs
diff --git a/pdf/src/main/rust/src/geometry.rs b/pdf/src/main/rust/src/geometry.rsindex 33c09460d6..2d2cf31f95 100644--- a/pdf/src/main/rust/src/geometry.rs+++ b/pdf/src/main/rust/src/geometry.rs@@ -119,13 +119,14 @@ pub(crate) fn page_rotation(doc: &Document, page_id: ObjectId) -> i64 { /// Matrix mapping raw page space (visible rect origin, before rotation) into /// displayed space: origin bottom-left, with dimensions swapped for 90/270. /// Visible rect is CropBox clipped to MediaBox, scaled by UserUnit.-/// Formula: normalize vb to [minx,miny,maxx,maxy], w=abs(x1-x0), h=abs(y1-y0),-/// t = translate(-minx,-miny) moves CropBox origin to 0, then r rotates and-/// translates to keep content in positive quadrant:-/// 90°: [0,1,-1,0,h,0] maps (0,0)->(h,0), (w,0)->(h,w), (0,h)->(0,0)-/// 180°: [-1,0,0,-1,w,h]-/// 270°: [0,-1,1,0,0,w] maps (0,0)->(0,w)-/// Verified for vb=[100,100,712,792], w=612,h=692.+///+/// `/Rotate` is clockwise (ISO 32000 §7.7.3.3). With page width `w`, height `h`+/// and device y-up, the spec-correct page→display maps are:+/// 90° (x,y) -> (y, w - x) => [0,-1, 1,0, 0, w]+/// 180° (x,y) -> (w - x, h - y) => [-1,0, 0,-1, w, h]+/// 270° (x,y) -> (h - y, x) => [0, 1,-1,0, h, 0]+/// (The 90° and 270° arms were previously swapped, rotating such pages 180°+/// off — content upside-down and mirrored; see issue #321 pagerotationexample.) pub(crate) fn page_base_matrix(doc: &Document, page_id: ObjectId) -> Mat { let vb = page_visible_box(doc, page_id); // Normalize to min/max to handle inverted boxes.@@ -135,9 +136,9 @@ pub(crate) fn page_base_matrix(doc: &Document, page_id: ObjectId) -> Mat { let h = (vb[3] - vb[1]).abs(); let t = translate(-minx, -miny); let r: Mat = match page_rotation(doc, page_id) {- 90 => [0.0, 1.0, -1.0, 0.0, h, 0.0],+ 90 => [0.0, -1.0, 1.0, 0.0, 0.0, w], 180 => [-1.0, 0.0, 0.0, -1.0, w, h],- 270 => [0.0, -1.0, 1.0, 0.0, 0.0, w],+ 270 => [0.0, 1.0, -1.0, 0.0, h, 0.0], _ => IDENTITY, }; mat_mul(&t, &r)
pdf/src/main/rust/src/issue321_tests.rs
diff --git a/pdf/src/main/rust/src/issue321_tests.rs b/pdf/src/main/rust/src/issue321_tests.rsindex 6638b22413..e7e3255c18 100644--- a/pdf/src/main/rust/src/issue321_tests.rs+++ b/pdf/src/main/rust/src/issue321_tests.rs@@ -261,6 +261,22 @@ mod issue321 { argb, (argb >> 24) & 0xFF, (argb >> 16) & 0xFF, (argb >> 8) & 0xFF, argb & 0xFF, n ); }+ // Dump the first text runs with their page-space origins so content+ // orientation can be checked against a reference render (rotation bugs).+ println!(" page {:.0}x{:.0} (display)", pd.width, pd.height);+ let mut tno = 0;+ for p in &pd.prims {+ if let Prim::Text { x, y, text, .. } = p {+ if text.trim().is_empty() {+ continue;+ }+ println!(" text @({:.0},{:.0}) {:?}", x, y, text);+ tno += 1;+ if tno >= 12 {+ break;+ }+ }+ } // Sample decoded image pixels (RGBA format 0) at a 3×3 grid so colors can // be diffed against a reference renderer. let mut img_no = 0;@@ -296,8 +312,6 @@ mod issue321 { /// Drive every fixture and print a per-PDF / per-page report. This test never /// fails on rendering defects itself (it is a diagnostic harness); it only /// fails if the harness cannot run. Skips cleanly when fixtures are absent.- /// fails on rendering defects itself (it is a diagnostic harness); it only- /// fails if the harness cannot run. Skips cleanly when fixtures are absent. #[test] fn issue321_report() { let dir = match fixtures_dir() {