pdf_forms/encodings/
mod.rs1pub mod cmap;
2mod differences;
3mod glyphnames;
4mod mappings;
5
6pub use self::differences::Differences;
7pub use self::glyphnames::Glyph;
8pub use self::mappings::*;
9use crate::Error;
10use crate::Result;
11use crate::parser_aux::substr;
12use cmap::ToUnicodeCMap;
13use encoding_rs::UTF_16BE;
14use log::debug;
15
16pub fn bytes_to_string(encoding: &CodedCharacterSet, bytes: &[u8], out: &mut String) -> Result<()> {
17 for b in bytes {
18 let Some(g) = encoding.get(*b as usize).copied().flatten() else {
19 continue;
20 };
21
22 for ch in char::decode_utf16([g.utf16_code_unit()]).flatten() {
23 out.push(ch);
24 }
25 }
26
27 Ok(())
28}
29
30pub fn string_to_bytes(encoding: &CodedCharacterSet, text: &str) -> Vec<u8> {
31 let mut out = Vec::new();
32 write_to_bytes(encoding, text, &mut out);
33 out
34}
35
36pub fn write_to_bytes(encoding: &CodedCharacterSet, text: &str, out: &mut Vec<u8>) {
37 for c in text.encode_utf16() {
38 let g = Glyph::from_utf16_code_unit(c);
39
40 let Some(n) = encoding
41 .iter()
42 .position(|glyph| glyph.is_some_and(|f| f == g))
43 else {
44 continue;
45 };
46
47 out.push(n as u8);
48 }
49}
50
51pub enum Encoding<'a> {
52 OneByteEncoding(&'a CodedCharacterSet),
53 SimpleEncoding(&'a [u8]),
54 UnicodeMapEncoding(ToUnicodeCMap),
55 Differences(Differences<'a>),
56}
57
58impl std::fmt::Debug for Encoding<'_> {
59 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
60 match self {
61 Self::OneByteEncoding(_arg0) => f.debug_tuple("OneByteEncoding").finish(),
63 Self::SimpleEncoding(arg0) => f.debug_tuple("SimpleEncoding").field(arg0).finish(),
64 Self::UnicodeMapEncoding(_arg0) => f.debug_tuple("UnicodeMapEncoding").finish(),
65 Self::Differences(_arg0) => f.debug_tuple("Differences").finish(),
66 }
67 }
68}
69
70impl Encoding<'_> {
71 pub fn bytes_to_string(&self, bytes: &[u8]) -> Result<String> {
72 let mut out = String::new();
73 self.write_to_string(bytes, &mut out)?;
74 Ok(out)
75 }
76
77 pub fn write_to_string(&self, bytes: &[u8], out: &mut String) -> Result<()> {
78 match self {
79 Self::OneByteEncoding(map) => {
80 bytes_to_string(map, bytes, out)?;
81 Ok(())
82 }
83 Self::SimpleEncoding(b"UniGB-UCS2-H") | Self::SimpleEncoding(b"UniGB-UTF16-H") => {
84 out.push_str(UTF_16BE.decode(bytes).0.as_ref());
85 Ok(())
86 }
87 Self::UnicodeMapEncoding(unicode_map) => {
88 let mut output_bytes = Vec::new();
89
90 let mut bytes_in_considered_code = 0u8;
92 let mut considered_source_code = 0u32;
93 for byte in bytes {
94 if bytes_in_considered_code == 4 {
95 let mut value =
96 unicode_map.get_or_replacement_char(considered_source_code, 4);
97 considered_source_code = 0;
98 bytes_in_considered_code = 0;
99 output_bytes.append(&mut value);
100 }
101 bytes_in_considered_code += 1;
102 considered_source_code = considered_source_code * 256 + *byte as u32;
103 if let Some(mut value) =
104 unicode_map.get(considered_source_code, bytes_in_considered_code)
105 {
106 considered_source_code = 0;
107 bytes_in_considered_code = 0;
108 output_bytes.append(&mut value);
109 }
110 }
111 if bytes_in_considered_code > 0 {
112 let mut value = unicode_map
113 .get_or_replacement_char(considered_source_code, bytes_in_considered_code);
114 output_bytes.append(&mut value);
115 }
116 let utf16_str: Vec<u8> = output_bytes
117 .iter()
118 .flat_map(|it| [(it / 256) as u8, (it % 256) as u8])
119 .collect();
120
121 out.push_str(UTF_16BE.decode(&utf16_str).0.as_ref());
122 Ok(())
123 }
124 Self::SimpleEncoding(b"WinAnsiEncoding") => {
125 bytes_to_string(&WIN_ANSI_ENCODING, bytes, out)?;
126 Ok(())
127 }
128 Self::SimpleEncoding(_) => Err(Error::CharacterEncoding),
129 Self::Differences(differences) => differences.bytes_to_string(bytes, out),
130 }
131 }
132
133 pub fn string_to_bytes(&self, text: &str) -> Vec<u8> {
134 let mut bytes = Vec::new();
135 self.write_to_bytes(text, &mut bytes);
136 bytes
137 }
138
139 pub fn write_to_bytes(&self, text: &str, out: &mut Vec<u8>) {
140 match self {
141 Self::OneByteEncoding(map) => write_to_bytes(map, text, out),
142 Self::SimpleEncoding(b"UniGB-UCS2-H") | Self::SimpleEncoding(b"UniGB-UTF16-H") => {
143 encode_utf16_be(text, out)
144 }
145 Self::SimpleEncoding(b"WinAnsiEncoding") => {
146 write_to_bytes(&WIN_ANSI_ENCODING, text, out)
147 }
148 Self::UnicodeMapEncoding(unicode_map) => {
149 let mut i = 0;
150 while i < text.chars().count() {
151 let current_unicode_seq: Vec<u16> = substr(text, i, 1).encode_utf16().collect();
152
153 if let Some(entries) =
154 unicode_map.get_source_codes_for_unicode(¤t_unicode_seq)
155 {
156 if let Some(entry) = entries.first() {
157 let mut bytes_for_code = Vec::new();
159 let val = entry.source_code;
160 match entry.code_len {
161 1 => bytes_for_code.push(val as u8),
162 2 => bytes_for_code.extend_from_slice(&(val as u16).to_be_bytes()),
163 3 => {
164 bytes_for_code.push((val >> 16) as u8);
165 bytes_for_code.push((val >> 8) as u8);
166 bytes_for_code.push(val as u8);
167 }
168 4 => bytes_for_code.extend_from_slice(&val.to_be_bytes()),
169 _ => { }
170 }
171 out.extend(bytes_for_code);
172 } else {
173 log::warn!(
175 "Unicode sequence {current_unicode_seq:04X?} found in map but no entries, skipping."
176 );
177 }
178 } else {
179 log::warn!(
181 "Unicode sequence {current_unicode_seq:04X?} not found in ToUnicode CMap, skipping."
182 );
183 }
184 i += 1;
185 }
186 }
187 Self::SimpleEncoding(_) => {
188 debug!("Unknown encoding used to encode text {self:?}");
189 out.extend_from_slice(text.as_bytes());
190 }
191 Self::Differences(differences) => {
192 differences.string_to_bytes(text, out);
193 }
194 }
195 }
196}
197
198pub fn encode_utf16_be(text: &str, out: &mut Vec<u8>) {
202 let bom_be: [u8; 2] = [0xFE, 0xFF];
204 out.extend_from_slice(&bom_be);
205 out.extend(text.encode_utf16().flat_map(|b| b.to_be_bytes()));
206}
207
208pub fn encode_utf8(text: &str) -> Vec<u8> {
213 let mut bytes = vec![0xEF, 0xBB, 0xBF];
215 bytes.extend(text.bytes());
216 bytes
217}
218
219#[cfg(test)]
220mod tests {
221
222 use super::*;
223
224 #[test]
225 fn unicode_with_2byte_code_does_not_convert_single_bytes() {
226 let mut cmap = ToUnicodeCMap::new();
227
228 cmap.put(
229 0x0000,
230 0x0002,
231 2,
232 cmap::BfRangeTarget::UTF16CodePoint { offset: 0 },
233 );
234 cmap.put(
235 0x0024,
236 0x0025,
237 2,
238 cmap::BfRangeTarget::UTF16CodePoint { offset: 0 },
239 );
240
241 let bytes: [u8; 2] = [0x00, 0x24];
242
243 let result = Encoding::UnicodeMapEncoding(cmap).bytes_to_string(&bytes);
244
245 assert_eq!(result.unwrap(), "\u{0024}");
246 }
247
248 #[test]
249 fn winansi_bytes_to_string() {
250 let bytes = [0x41, 0xe9, 0x42, 0xfc, 0xdf]; let result = Encoding::SimpleEncoding(b"WinAnsiEncoding")
253 .bytes_to_string(&bytes)
254 .expect("WinAnsi decode should succeed");
255 assert_eq!(result, "AéBüß");
256 }
257
258 #[test]
259 fn winansi_string_to_bytes() {
260 let text = "Sébastien 0,019€ ü ÄÖÜ ß";
261 let bytes = Encoding::SimpleEncoding(b"WinAnsiEncoding").string_to_bytes(text);
262 let decoded = Encoding::OneByteEncoding(&WIN_ANSI_ENCODING)
264 .bytes_to_string(&bytes)
265 .expect("WinAnsi decode should succeed");
266 assert_eq!(decoded, text);
267 }
268}