R7.4: an unembedded font is no damage (#5)

The warning watch skips MuPDF's non-embedded font notice and its
repeat counts. Text and search reads are watched too: damage they meet
is kept on the document, told by the next draw or picture check, and
fails image and text exports until then.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
ZacharyZhang-NYandClaude Opus 5.5 committed 2026-10-16 13:44:17 -04:00
1 parent 969cb67c96
commit 6b676e06e6
9 files changed
+191 -9

No files matched your search

+129
View File
@@ -148,6 +148,7 @@ fn a_broken_stream_under_a_sound_table_is_damage_and_fails_exports() {
let report = worker.repair_report(doc).expect("asks").expect("damage is reported");
assert!(!report.trim().is_empty());
assert!(worker.export_text(doc, &dir.path().join("doc.txt")).is_err(), "partial text is no export");
assert!(worker.export_image(doc, 0, 72.0, &dir.path().join("page.png")).is_err(), "nor a partial page");
worker.rotate_pages(doc, &[0], 90).expect("edits the copy");
doc = worker.now(doc).expect("the state");
worker.save(doc, &dir.path().join("fixed.pdf")).expect("saves the copy");
@@ -186,3 +187,131 @@ fn a_broken_picture_shows_when_drawn_or_checked() {
assert!(other.check_pictures(again).expect("checks"), "the check finds it undrawn");
assert_eq!(std::fs::read(&path).expect("reads"), bytes);
}
/// As many Chinese files name their fonts.
const SIMSUN: &str = "<< /Type /Font /Subtype /Type0 /BaseFont /SimSun /Encoding /Identity-H /DescendantFonts [<< /Type /Font /Subtype /CIDFontType2 /BaseFont /SimSun /CIDSystemInfo << /Registry (Adobe) /Ordering (GB1) /Supplement 2 >> /FontDescriptor << /Type /FontDescriptor /FontName /SimSun /Flags 6 /FontBBox [0 -141 996 855] /ItalicAngle 0 /Ascent 859 /Descent -141 /CapHeight 859 /StemV 91 >> >>] >>";
/// Two runs, each in its own unembedded font.
const TWO_RUNS: &str = "BT /F1 24 Tf 72 700 Td <00410042> Tj /F2 24 Tf <0043> Tj ET";
#[test]
fn an_unembedded_font_is_no_damage() {
let dir = tempfile::tempdir().expect("a temp dir");
let path = dir.path().join("doc.pdf");
// Two fonts, one notice: a repeat is no damage either.
let fonts = " /Resources << /Font << /F1 5 0 R /F2 6 0 R >> >>";
let bytes = one_page("", fonts, TWO_RUNS, &[SIMSUN.to_string(), SIMSUN.to_string()]);
std::fs::write(&path, &bytes).expect("writes");
// Each on a fresh worker: no font loaded before.
let (mut worker, doc) = opened(&path);
worker.export_text(doc, &dir.path().join("doc.txt")).expect("the text exports");
let (mut worker, doc) = opened(&path);
worker.export_image(doc, 0, 72.0, &dir.path().join("page.png")).expect("the page exports");
let (mut worker, doc) = opened(&path);
// Read unwatched first: its repeat is pending.
worker.text(doc, 0).expect("reads");
let tile = worker.render(doc, 0, (1.0, 0), [0, 0, 612, 792], false).expect("draws");
assert!(!tile.damaged, "a substituted font draws whole");
assert_eq!(worker.repair_report(doc).expect("asks"), None);
}
#[test]
fn unreadable_content_still_draws_as_damage() {
let dir = tempfile::tempdir().expect("a temp dir");
// Text with no font; a stray bracket.
for content in ["BT 72 700 Td (Missing text) Tj ET", "BT ] ET"] {
let path = dir.path().join("doc.pdf");
std::fs::write(&path, one_page("", "", content, &[])).expect("writes");
let (mut worker, doc) = opened(&path);
let tile = worker.render(doc, 0, (1.0, 0), [0, 0, 612, 792], false).expect("draws");
assert!(tile.damaged, "{content} draws as damage");
}
}
/// Two pictures whose zlib checksums are wrong.
fn broken_pictures(dir: &Path) -> std::path::PathBuf {
let hex = format!("{}>", "ff0000".repeat(16));
// One stored deflate block, its checksum wrong.
let mut zlib = vec![0x78, 0x01, 0x01];
zlib.extend_from_slice(&(hex.len() as u16).to_le_bytes());
zlib.extend_from_slice(&(!(hex.len() as u16)).to_le_bytes());
zlib.extend_from_slice(hex.as_bytes());
zlib.extend_from_slice(&[0, 0, 0, 0]);
let slot = "#".repeat(zlib.len());
let image = format!("<< /Type /XObject /Subtype /Image /Width 4 /Height 4 /ColorSpace /DeviceRGB /BitsPerComponent 8 /Filter [/FlateDecode /ASCIIHexDecode] /Length {} >>
stream
{slot}
endstream", zlib.len());
let names = " /Resources << /XObject << /Im1 5 0 R /Im2 6 0 R >> >>";
let mut bytes = one_page("", names, "q 100 0 0 100 72 600 cm /Im1 Do Q q 100 0 0 100 72 400 cm /Im2 Do Q", &[image.clone(), image]);
for _ in 0..2 {
let at = bytes.windows(slot.len()).position(|w| w == slot.as_bytes()).expect("the slot");
bytes[at..at + zlib.len()].copy_from_slice(&zlib);
}
let path = dir.join("doc.pdf");
std::fs::write(&path, &bytes).expect("writes");
path
}
/// A picture whose colour key mask is invalid.
fn masked() -> String {
let hex = format!("{}>", "ff0000".repeat(16));
format!("<< /Type /XObject /Subtype /Image /Width 4 /Height 4 /ColorSpace /DeviceRGB /BitsPerComponent 8 /Mask [null null null null null null] /Filter /ASCIIHexDecode /Length {} >>
stream
{hex}
endstream", hex.len())
}
/// The bad mask, alone or drawn beside two font notices.
fn bad_mask(dir: &Path, fonts: Option<bool>) -> std::path::PathBuf {
let picture = "q 100 0 0 100 72 600 cm /Im1 Do Q";
let (resources, content, objects) = match fonts {
None => (String::new(), picture.to_string(), vec![masked()]),
Some(first) => (
" /Font << /F1 6 0 R /F2 7 0 R >>".to_string(),
if first { format!("{picture} {TWO_RUNS}") } else { format!("{TWO_RUNS} {picture}") },
vec![masked(), SIMSUN.to_string(), SIMSUN.to_string()],
),
};
let names = format!(" /Resources << /XObject << /Im1 5 0 R >>{resources} >>");
let path = dir.join(format!("mask-{fonts:?}.pdf"));
std::fs::write(&path, one_page("", &names, &content, &objects)).expect("writes");
path
}
#[test]
fn a_broken_picture_under_two_filters_fails_exports() {
let dir = tempfile::tempdir().expect("a temp dir");
let path = broken_pictures(dir.path());
let (mut worker, doc) = opened(&path);
assert!(worker.export_text(doc, &dir.path().join("doc.txt")).is_err(), "its text read whole");
let (mut worker, doc) = opened(&path);
assert!(worker.export_image(doc, 0, 72.0, &dir.path().join("page.png")).is_err(), "its page read whole");
let (mut worker, doc) = opened(&path);
let tile = worker.render(doc, 0, (1.0, 0), [0, 0, 612, 792], false).expect("draws");
assert!(tile.damaged, "the picture draws as damage");
}
#[test]
fn a_broken_picture_read_before_is_still_damage() {
let dir = tempfile::tempdir().expect("a temp dir");
let files = [broken_pictures(dir.path()), bad_mask(dir.path(), None), bad_mask(dir.path(), Some(true)), bad_mask(dir.path(), Some(false))];
// Reading caches the pictures; their repeat waits.
let read: [fn(&mut Worker, Doc); 2] = [|w, d| drop(w.text(d, 0).expect("reads")), |w, d| drop(w.search(d, 0, "x").expect("searches"))];
for (path, first) in files.iter().flat_map(|p| read.map(|r| (p, r))) {
let (mut worker, doc) = opened(&path);
first(&mut worker, doc);
let tile = worker.render(doc, 0, (1.0, 0), [0, 0, 612, 792], false).expect("draws");
assert!(tile.damaged, "the picture draws as damage");
assert!(worker.repair_report(doc).expect("asks").is_some());
let (mut worker, doc) = opened(&path);
first(&mut worker, doc);
assert!(worker.export_image(doc, 0, 72.0, &dir.path().join("page.png")).is_err(), "its page read whole");
let (mut worker, doc) = opened(&path);
first(&mut worker, doc);
assert!(worker.export_text(doc, &dir.path().join("doc.txt")).is_err(), "its text read whole");
let (mut worker, doc) = opened(&path);
first(&mut worker, doc);
assert!(worker.check_pictures(doc).expect("checks"), "the check finds it");
}
}