MCPcopy Create free account
hub / github.com/LibPDF-js/core / decodePdfDocEncoding

Function decodePdfDocEncoding

src/helpers/encoding.ts:134–169  ·  view source on GitHub ↗
(bytes: Uint8Array)

Source from the content-addressed store, hash-verified

132 * Decode PDFDocEncoding bytes to string.
133 */
134export function decodePdfDocEncoding(bytes: Uint8Array): string {
135 const chars: string[] = [];
136
137 for (const byte of bytes) {
138 if (byte < 0x18) {
139 // Control chars - preserve tab, newline, carriage return
140 if (byte === 0x09 || byte === 0x0a || byte === 0x0d) {
141 chars.push(String.fromCharCode(byte));
142 }
143 // Skip other control chars
144 } else if (byte <= 0x1f) {
145 // Special low bytes (accents)
146 const code = PDF_DOC_LOW[byte];
147
148 if (code) {
149 chars.push(String.fromCharCode(code));
150 }
151 } else if (byte < 0x80) {
152 // ASCII range - direct mapping
153 chars.push(String.fromCharCode(byte));
154 } else if (byte <= 0xa0) {
155 // High bytes with special mapping
156 const code = PDF_DOC_HIGH[byte];
157
158 if (code) {
159 chars.push(String.fromCharCode(code));
160 }
161 // 0x9F is undefined, skip
162 } else if (byte !== 0xad) {
163 // 0xA1-0xFF map to Unicode (same as Latin-1), except 0xAD (undefined)
164 chars.push(String.fromCharCode(byte));
165 }
166 }
167
168 return chars.join("");
169}
170
171/**
172 * Decode PDF text string bytes (auto-detects encoding).

Callers 2

encoding.test.tsFile · 0.90
decodeTextStringFunction · 0.85

Calls 1

pushMethod · 0.80

Tested by

no test coverage detected