pdf renderer: P1 shading — Type4 flag0 holes + Coons edge reversal
- Commit
2977bed3220faf864a35655bd3a845409c1808d4- Author
- Vayun Mathur <[email protected]>
- When
- 2026-07-28 03:14 UTC (4 months ago)
- Parents
d53082b
- Type4 free-form Gouraud: long all-zero flag streams are independent triangles; previously retained vb/vc causing holes. Now resets va/vb/vc to None after each emit on flag 0 (fixes high #9) - Coons Type6-7 shared_edge: reverse winding [6,5,4,3], [9,8,7,6], [0,11,10,9] for continuation flags 1/2/3 to avoid twisted/bow-tie patches per PDF Table 7.10.5.7 (fixes high #10) 78 tests green. Co-Authored-By: internal-model
Diff
pdf/src/main/rust/src/shading.rs
diff --git a/pdf/src/main/rust/src/shading.rs b/pdf/src/main/rust/src/shading.rsindex f7bb495a61..0cf4e17b3c 100644--- a/pdf/src/main/rust/src/shading.rs+++ b/pdf/src/main/rust/src/shading.rs@@ -242,30 +242,23 @@ fn parse_type4( let mut guard = 0usize; while br.remaining_bits() >= (bps_flag + 2 * bps_coord + ncomp as u32 * bps_comp) as usize { guard += 1;- if guard > 2_000_000 {- break;- }+ if guard > 2_000_000 { break; } let flag = br.read(bps_flag).unwrap_or(0);- let v = match read_vertex(&mut br, bps_coord, bps_comp, ncomp, decode) {- Some(v) => v,- None => break,- };+ let v = match read_vertex(&mut br, bps_coord, bps_comp, ncomp, decode) { Some(v) => v, None => break }; match flag { 0 => {- // Start of a new triangle: shift in as first vertex, then need- // two more flag-0 vertices to complete.- if va.is_none() {- va = Some(v);- } else if vb.is_none() {- vb = Some(v);- } else if vc.is_none() {+ // Flag 0 = new triangle, but PDF allows long all-zero streams: emit whenever 3 verts accumulated, then reset+ if va.is_none() { va = Some(v); }+ else if vb.is_none() { vb = Some(v); }+ else {+ // vb Some, vc will become v -> triangle vc = Some(v); if let (Some(a), Some(b), Some(c)) = (&va, &vb, &vc) { tris.push((a.clone(), b.clone(), c.clone())); }- } else {- // Begin a fresh triangle.- va = Some(v);+ // P0 fix #9: Reset for next independent triangle on flag 0, not retain vb/vc (would cause holes)+ // Spec streams often emit all flag-0 vertices as independent triangles, not strip. So reset all.+ va = None; vb = None; vc = None; }@@ -572,16 +565,18 @@ fn parse_type6_7( /// Select the shared edge (4 control points + 2 corner colors) from the /// previous patch for a flag-1/2/3 continuation. Best-effort per PDF 7.10.5.7.+/// Fix high #10: shared edge must be reversed (opposite winding) to avoid twisted Coons patches fn shared_edge(prev_pts: &[(f64, f64)], prev_cols: &[Vec<f64>], flag: u64) -> ([(f64, f64); 4], Vec<f64>, Vec<f64>) { // Boundary points p1..p12 = index 0..11; corners at 0,3,6,9.- // Edges (as continuation input) per spec for flags 1,2,3.+ // Edges per spec Table 7.10.5.7 for flags 1,2,3 — reversed winding for continuation let (ia, ib, ic, id, ca, cb) = match flag {- 1 => (3usize, 4, 5, 6, 1usize, 2usize),- 2 => (6usize, 7, 8, 9, 2usize, 3usize),- _ => (9usize, 10, 11, 0, 3usize, 0usize),+ 1 => (6usize, 5, 4, 3, 2usize, 1usize), // reverse of 3,4,5,6+ 2 => (9usize, 8, 7, 6, 3usize, 2usize), // reverse of 6,7,8,9+ _ => (0usize, 11, 10, 9, 0usize, 3usize), // reverse of 9,10,11,0 }; let g = |i: usize| prev_pts.get(i).copied().unwrap_or((0.0, 0.0)); let col = |i: usize| prev_cols.get(i).cloned().unwrap_or_default();+ // Return reversed edge per spec: g(ia),g(ib),g(ic),g(id) now already reversed ([g(ia), g(ib), g(ic), g(id)], col(ca), col(cb)) }