diff --git a/crates/pdf/src/streams.rs b/crates/pdf/src/streams.rs index e29d828f48..7314e930ae 100644 --- a/crates/pdf/src/streams.rs +++ b/crates/pdf/src/streams.rs @@ -1034,6 +1034,19 @@ mod tests { assert!(unread.is_empty(), "{unread:#?}"); } + #[test] + fn a_hex_string_broken_by_a_non_hex_byte_hides_no_stream_after_it() { + // A `<` followed by hex digits and then a byte that is neither a hex digit nor white space + // is no hex string: the scan goes on from the next byte instead of skipping to a `>`. + let bad = b"<< /Type /XRef /DecodeParms 9 0 R >> stream\nx\nendstream"; + for lead in [&b"<4G "[..], b"<41 4z ", b"<\x00 "] { + let pdf = [lead, &bad[..]].concat(); + assert!(unsafe_file_streams(&pdf), "{}", String::from_utf8_lossy(&pdf)); + } + // A hex string that ends is skipped, and the stream after it is still read. + assert!(unsafe_file_streams(&[&b"<41 42> "[..], &bad[..]].concat())); + } + #[test] fn a_run_of_lt_in_an_image_a_string_or_a_comment_opens() { // #3924 review: every `<<` in a run of `<` (an image's flat area of grey 60) was read as a diff --git a/crates/pdf/src/tests/file_fonts.rs b/crates/pdf/src/tests/file_fonts.rs index d0d1b949ab..b27dac4f0d 100644 --- a/crates/pdf/src/tests/file_fonts.rs +++ b/crates/pdf/src/tests/file_fonts.rs @@ -10,7 +10,7 @@ use kurbo::{Point, Rect, Shape}; use vectorcraft_doc::{Document, FILE_FONT_MIME, TextObject}; use vectorcraft_text::{FontDb, FontMatch}; -use crate::tests::live_text::{SOURCE_SERIF, outlines, texts, uninstalled_serif}; +use crate::tests::live_text::{SOURCE_SERIF, outlines, renamed, texts, uninstalled_serif}; use crate::*; /// Lines of (x, y from the top, size, text): kerned pairs, a long line, two sizes. @@ -274,3 +274,29 @@ fn two_glyphs_for_one_character_both_survive_import() { }; close(&glyph_ink(db, &imported), "imported"); } + +/// The license flags (OS/2 `fsType`) of the font a PDF carries go with the font the document keeps: +/// a restricted font (0x0002) still draws its type, and a PDF saved from the document draws that +/// type as outlines with the license warning instead of embedding the font again. +#[test] +fn a_restricted_font_the_file_carries_is_not_embedded_again() { + let mut font = renamed(SOURCE_SERIF, &[("SourceSerif4", "SourceSerif7"), ("Source Serif 4", "Source Serif 7")]); + let tables = usize::from(u16::from_be_bytes([font[4], font[5]])); + let os2 = (0..tables).map(|i| 12 + 16 * i).find(|r| &font[*r..*r + 4] == b"OS/2").unwrap(); + let at = u32::from_be_bytes(font[os2 + 8..os2 + 12].try_into().unwrap()) as usize; + font[at + 8..at + 10].copy_from_slice(&0x0002u16.to_be_bytes()); + + let carried = import(&page_pdf(font)); + let t = texts(&carried.document); + assert!(t.iter().any(|t| t.file_fonts.is_some()), "the file's font draws its type"); + + let saved = export_with_report(&carried.document, &PdfOptions::default()).unwrap(); + assert!(saved.warnings.iter().any(|w| w.contains("Source Serif 7") && w.contains("doesn't allow embedding")), "{:?}", saved.warnings); + let back = import(&saved.bytes); + assert!( + texts(&back.document).is_empty(), + "no type in the saved file: {:?}", + texts(&back.document).iter().map(TextObject::plain_text).collect::>() + ); + assert!(!back.document.images.values().any(|b| b.mime == FILE_FONT_MIME), "no font in the saved file"); +} diff --git a/crates/pdf/src/tests/live_text.rs b/crates/pdf/src/tests/live_text.rs index 90341ca28b..f6a2e8229b 100644 --- a/crates/pdf/src/tests/live_text.rs +++ b/crates/pdf/src/tests/live_text.rs @@ -271,7 +271,7 @@ fn invisible_text_is_type_whatever_its_glyphs_look_like() { } /// `font` with each of `names` (`from`, `to`, the same length) replaced, in ASCII and UTF-16. -fn renamed(font: &[u8], names: &[(&str, &str)]) -> Vec { +pub(crate) fn renamed(font: &[u8], names: &[(&str, &str)]) -> Vec { let mut bytes = font.to_vec(); let utf16 = |s: &str| s.encode_utf16().flat_map(u16::to_be_bytes).collect::>(); for &(from, to) in names {