Skip to content

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

fix(sign): fill typed signature outlines with non-zero winding (#1786)

jonathanwxh-cellf536ac7a056e
diff --git a/crates/annot/src/appearance.rs b/crates/annot/src/appearance.rs
index 0f0706428..c692d09e3 100644
--- a/crates/annot/src/appearance.rs
+++ b/crates/annot/src/appearance.rs
@@ -559,7 +559,7 @@ pub fn build(d: &Dict) -> Option<Stream> {
c.push_str("h\n");
}
}
- c.push_str("f*\n");
+ c.push_str("f\n");
return Some(form(rect, c.as_bytes(), res));
}
// A custom stamp: its picture (an image, or a form mapped to /PCPictureSize) fills
diff --git a/crates/annot/src/lib.rs b/crates/annot/src/lib.rs
index f4973aa02..0c9eff742 100644
--- a/crates/annot/src/lib.rs
+++ b/crates/annot/src/lib.rs
@@ -386,7 +386,7 @@ pub enum Shape {
by: Option<String>,
},
/// A typed signature or initials: filled outlines (each contour as points normalised to
- /// `rect`, 0–1, y up), drawn with the even-odd rule.
+ /// `rect`, 0–1, y up), drawn with the non-zero winding rule.
TypedSignature {
rect: [f64; 4],
contours: Vec<Vec<[f64; 2]>>,
diff --git a/crates/annot/src/tests.rs b/crates/annot/src/tests.rs
index faed409ab..0ccfc6a73 100644
--- a/crates/annot/src/tests.rs
+++ b/crates/annot/src/tests.rs
@@ -1308,3 +1308,16 @@ fn text_box_font_size_changes_the_da_and_grows_the_box() {
assert_eq!(props(&doc, 0, i).unwrap().font_size, None);
}
}
+
+/// #1786: TrueType glyph contours (such as Inter's composite Ђ and ћ) overlap with the same
+/// winding direction where strokes cross, so typed signature appearances must fill with the
+/// non-zero winding operator (`f`), not even-odd (`f*`).
+#[test]
+fn typed_signature_uses_nonzero_winding_fill() {
+ let mut doc = fixture();
+ let contours = vec![vec![[0.4, 0.0], [0.6, 0.0], [0.6, 1.0], [0.4, 1.0]], vec![[0.0, 0.7], [1.0, 0.7], [1.0, 0.85], [0.0, 0.85]]];
+ let idx = add_annotation(&mut doc, &new(0, Shape::TypedSignature { rect: [100.0, 100.0, 200.0, 140.0], contours }), &meta("sig")).unwrap();
+ let ap = ap_content(&doc, &list(&doc, 0)[idx]);
+ assert!(ap.ends_with("h\nf\n"), "expected non-zero winding fill (`f`), got: {ap}");
+ assert!(!ap.contains("f*"), "must not use even-odd fill (`f*`): {ap}");
+}
diff --git a/crates/sign/src/graphic.rs b/crates/sign/src/graphic.rs
index 5d50c6f76..7b1535043 100644
--- a/crates/sign/src/graphic.rs
+++ b/crates/sign/src/graphic.rs
@@ -1,7 +1,7 @@
//! A picture in a visible signature, where the signer's name would be large (Acrobat: Configure
//! Signature Appearance ▸ Draw or Image, and typed signatures). Drawn as Fill & Sign draws its
-//! signatures: ink smoothed and stroked with round ends, typed outlines filled even-odd, images
-//! with their alpha as a soft mask.
+//! signatures: ink smoothed and stroked with round ends, typed outlines filled with the non-zero
+//! winding rule, images with their alpha as a soft mask.
use std::sync::Arc;
@@ -21,7 +21,7 @@ const MAX_PIXELS: u64 = 1 << 24;
pub enum Graphic {
/// Drawn strokes, y up, in units where the drawing pad is 1 wide (as Fill & Sign saves them).
Strokes(Vec<Vec<[f64; 2]>>),
- /// Closed outlines, y up, filled even-odd: a typed signature in the script font.
+ /// Closed outlines, y up, filled with the non-zero winding rule: a typed signature in the script font.
Outlines(Vec<Vec<[f64; 2]>>),
/// Straight (not premultiplied) RGBA pixels, rows from the top.
Image { width: u32, height: u32, rgba: Arc<Vec<u8>> },
@@ -141,7 +141,7 @@ impl Graphic {
}
c.push_str("h\n");
}
- c.push_str("f*\nQ\n");
+ c.push_str("f\nQ\n");
Ok((c, None))
}
Graphic::Image { width, height, rgba } => {
diff --git a/crates/sign/tests/pdf.rs b/crates/sign/tests/pdf.rs
index 908a5c0fe..f7d873daf 100644
--- a/crates/sign/tests/pdf.rs
+++ b/crates/sign/tests/pdf.rs
@@ -290,7 +290,7 @@ fn visible_signatures_show_a_drawn_typed_or_image_graphic() {
let typed = Graphic::Outlines(vec![vec![[0.0, 0.0], [1.0, 0.0], [1.0, 0.5], [0.0, 0.5]], vec![[0.2, 0.1], [0.8, 0.1], [0.5, 0.4]]]);
for (graphic, drawn) in [
(strokes, " c\n"),
- (typed, "f*\n"),
+ (typed, "f\n"),
// 40 x 10 in the left half (94 x 44 inside the padding): 94 wide, centred vertically.
(picture(true), "q 94 0 0 23.5 3 13.25 cm /Im1 Do Q"),
] {
diff --git a/crates/ui-egui/src/fill_sign.rs b/crates/ui-egui/src/fill_sign.rs
index 794fe0bf1..cebb97d60 100644
--- a/crates/ui-egui/src/fill_sign.rs
+++ b/crates/ui-egui/src/fill_sign.rs
@@ -495,7 +495,8 @@ pub(crate) fn script_preview(text: &str, w: usize, h: usize) -> egui::ColorImage
}
let k = ((h as f64 * 0.9) / span).min((w as f64 * 0.95) / width);
let x0 = (w as f64 - width * k) / 2.0;
- // Device points (y down), then an even-odd scanline fill.
+ // Device points (y down), then a non-zero winding scanline fill so overlapping same-direction
+ // TrueType contours (such as Inter's composite Ђ and ћ) stay filled where strokes cross.
let polys: Vec<Vec<(f64, f64)>> = o
.contours
.iter()
@@ -503,20 +504,29 @@ pub(crate) fn script_preview(text: &str, w: usize, h: usize) -> egui::ColorImage
.collect();
for y in 0..h {
let sy = y as f64 + 0.5;
- let mut xs: Vec<f64> = Vec::new();
+ let mut xs: Vec<(f64, i32)> = Vec::new();
for poly in &polys {
for i in 0..poly.len() {
let (a, b) = (poly[i], poly[(i + 1) % poly.len()]);
if (a.1 <= sy) != (b.1 <= sy) {
- xs.push(a.0 + (sy - a.1) / (b.1 - a.1) * (b.0 - a.0));
+ let dir = if a.1 < b.1 { 1 } else { -1 };
+ xs.push((a.0 + (sy - a.1) / (b.1 - a.1) * (b.0 - a.0), dir));
}
}
}
- xs.sort_by(|a, b| a.total_cmp(b));
- for pair in xs.as_chunks::<2>().0 {
- let (from, to) = (pair[0].round().clamp(0.0, w as f64) as usize, pair[1].round().clamp(0.0, w as f64) as usize);
- for x in from..to {
- img[(x, y)] = Color32::BLACK;
+ xs.sort_by(|a, b| a.0.total_cmp(&b.0));
+ let mut winding = 0i32;
+ let mut start = 0.0;
+ for (x, dir) in xs {
+ if winding == 0 {
+ start = x;
+ }
+ winding += dir;
+ if winding == 0 {
+ let (from, to) = (start.round().clamp(0.0, w as f64) as usize, x.round().clamp(0.0, w as f64) as usize);
+ for px in from..to {
+ img[(px, y)] = Color32::BLACK;
+ }
}
}
}
@@ -1270,4 +1280,24 @@ mod tests {
}
}
}
+
+ #[test]
+ fn overlapping_glyph_contours_fill_without_holes_while_keeping_counters() {
+ let runs =
+ |img: &egui::ColorImage, y: usize| (0..img.width()).filter(|&x| img[(x, y)].a() > 0 && (x == 0 || img[(x - 1, y)].a() == 0)).count();
+ // #1786: Inter's Ђ (T + right hook) and ћ (h + crossbar) have two same-direction contours
+ // that overlap on the stem; non-zero winding fills the join instead of punching a hole.
+ let dje = super::script_preview("Ђ", 200, 200);
+ assert_eq!(dje[(97, 108)], egui::Color32::BLACK, "stem-hook overlap of Ђ is filled");
+ assert_eq!(runs(&dje, 108), 2, "stem and right loop of Ђ are two solid strokes");
+ let tshe = super::script_preview("ћ", 200, 200);
+ assert_eq!(tshe[(93, 53)], egui::Color32::BLACK, "stem-crossbar overlap of ћ is filled");
+ assert_eq!(runs(&tshe, 53), 1, "crossbar of ћ is one unbroken stroke across the stem");
+ // Counter-clockwise inner contours remain open holes.
+ let omicron = super::script_preview("О", 200, 200);
+ assert_eq!(omicron[(109, 94)].a(), 0, "counter of О stays transparent");
+ assert_eq!(runs(&omicron, 94), 2);
+ assert!(super::script_preview("", 200, 200).pixels.iter().all(|p| p.a() == 0));
+ assert!(super::script_preview("Ђћ", 0, 0).pixels.is_empty());
+ }
}