Skip to main content

decode_text

Function decode_text 

Source
pub fn decode_text(bytes: &[u8]) -> Cow<'_, str>
Expand description

Read a text string’s bytes as text (ISO 32000-1 §7.9.2.2).

A leading FE FF selects UTF-16BE and EF BB BF selects UTF-8; FF FE selects UTF-16LE, which is an extension beyond the specification. Anything else is PDFDocEncoding, one byte per character. In the marked encodings, language-code regions are stripped.

Unpaired surrogates and invalid UTF-8 become U+FFFD (Rust’s str cannot carry either), and a trailing odd byte in a UTF-16 payload is ignored.

use pdfrum_object::decode_text;

assert_eq!(decode_text(b"the quick\tfox"), "the quick\tfox");
assert_eq!(decode_text(b"\xFE\xFF\xD8\x3C\xDF\xA8"), "\u{1F3A8}");
assert_eq!(decode_text(b"\xEF\xBB\xBF\xCC\xB0"), "\u{0330}");
// 0x80 is a bullet in PDFDocEncoding, not a Latin-1 control.
assert_eq!(decode_text(b"\x80"), "\u{2022}");