PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust
M7: Type 3 fallback glyphs get a well-formed d1, so Acrobat draws them (#548)
Dimitarbd275555193e
diff --git a/crates/edit/src/tests.rs b/crates/edit/src/tests.rsindex 6e746ca0b..fca770b55 100644--- a/crates/edit/src/tests.rs+++ b/crates/edit/src/tests.rs@@ -574,6 +574,34 @@ fn japanese_paragraph_uses_unicode_type3_fallback() {assert_eq!(text::text_blocks(&reopened, 0).unwrap()[0].text, replacement);}+/// Every Type 3 fallback glyph starts with a well-formed `d1`: six operands, the second 0, and a+/// box that encloses every point of the glyph. Acrobat shows a bullet for a glyph whose `d1`+/// has the wrong operand count.+#[test]+fn type3_fallback_glyphs_declare_a_well_formed_d1() {+ if without_craft_fonts("type3_fallback_glyphs_declare_a_well_formed_d1") {+ return;+ }+ let mut doc = text_page("BT /F2 12 Tf 72 700 Td (ab) Tj ET");+ text::replace_line(&mut doc, 0, 0, "見本商会 御中").unwrap();+ let glyphs = fallback_paths(&reopen(&doc));+ assert!(!glyphs.is_empty());+ for glyph in glyphs {+ let text = String::from_utf8(glyph).unwrap();+ let mut lines = text.lines();+ let d1: Vec<&str> = lines.next().unwrap().split_whitespace().collect();+ assert_eq!(d1.len(), 7, "six operands and d1: {d1:?}");+ assert_eq!(d1[6], "d1");+ let n: Vec<f64> = d1[..6].iter().map(|v| v.parse().unwrap()).collect();+ assert_eq!(n[1], 0.0, "wy");+ assert!(n[2] <= n[4] && n[3] <= n[5], "box: {n:?}");+ for line in lines.filter(|l| l.ends_with(" m") || l.ends_with(" l")) {+ let p: Vec<f64> = line.split_whitespace().take(2).map(|v| v.parse().unwrap()).collect();+ assert!(p[0] >= n[2] && p[0] <= n[4] && p[1] >= n[3] && p[1] <= n[5], "{line} outside {n:?}");+ }+ }+}+#[test]fn japanese_paragraph_keeps_ideographic_spaces() {if without_craft_fonts("japanese_paragraph_keeps_ideographic_spaces") {diff --git a/crates/edit/src/text.rs b/crates/edit/src/text.rsindex 7eb311451..7753b56fe 100644--- a/crates/edit/src/text.rs+++ b/crates/edit/src/text.rs@@ -542,7 +542,18 @@ fn type3_path(face: &CraftFont, ch: char) -> Result<(Vec<u8>, f64), EditError> {GlyphError::TooComplex => EditError::Invalid(format!("Japanese fallback glyph U+{:04X} is too complex", ch as u32)),})?;let scale = 1000.0;- let mut out = format!("{} 0 0 0 0 1000 1000 d1\n", pdf_num(glyph.width * scale)).into_bytes();+ // d1 is `wx wy llx lly urx ury` (ISO 32000-2 §9.6.4) with a box enclosing the glyph;+ // Acrobat draws a bullet in place of a glyph whose d1 is malformed.+ let b = if glyph.contours.is_empty() { [0.0; 4] } else { glyph.bbox };+ let mut out = format!(+ "{} 0 {} {} {} {} d1\n",+ pdf_num(glyph.width * scale),+ pdf_num((b[0] * scale).floor()),+ pdf_num((b[1] * scale).floor()),+ pdf_num((b[2] * scale).ceil()),+ pdf_num((b[3] * scale).ceil())+ )+ .into_bytes();for contour in glyph.contours {let Some(first) = contour.first() else { continue };out.extend_from_slice(format!("{} {} m\n", pdf_num(first[0] * scale), pdf_num(first[1] * scale)).as_bytes());