From 495b615220bd8dd7eb57ac7a601a0f5b5369dc5f Mon Sep 17 00:00:00 2001 From: GloamXun Date: Tue, 20 Oct 2026 18:56:04 +0800 Subject: [PATCH] Encode Latin-1 text bytes in the PDF writer --- crates/texrra-tex-core/src/pdf.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/crates/texrra-tex-core/src/pdf.rs b/crates/texrra-tex-core/src/pdf.rs index 274e2e8..76c9d76 100755 --- a/crates/texrra-tex-core/src/pdf.rs +++ b/crates/texrra-tex-core/src/pdf.rs @@ -15,6 +15,7 @@ fn escape(text: &str) -> String { ')' => out.push_str("\\)"), '\\' => out.push_str("\\\\"), c if c.is_ascii() => out.push(c), + c if (c as u32) <= 0xFF => out.push_str(&format!("\\{:03o}", c as u32)), _ => out.push('?'), } } @@ -278,3 +279,15 @@ pub fn display_lists_to_pdf(pages: &[DisplayList], page_height: i64) -> Vec pub fn display_list_to_pdf(list: &DisplayList, page_height: i64) -> Vec { display_lists_to_pdf(std::slice::from_ref(list), page_height) } + +#[cfg(test)] +mod tests { + use super::escape; + + #[test] + fn latin1_text_is_octal_escaped_in_the_content_stream() { + assert_eq!(escape("café"), "caf\\351"); + assert_eq!(escape("über"), "\\374ber"); + assert_eq!(escape("中文"), "??"); + } +}