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

Method decodeStringToCodes

src/text/text-extractor.ts:296–320  ·  view source on GitHub ↗

* Decode string bytes to character codes. * * For simple fonts (TrueType, Type1), each byte is a character code. * For composite fonts (Type0/CID), bytes are decoded as 2-byte codes.

(bytes: Uint8Array, font: PdfFont)

Source from the content-addressed store, hash-verified

294 * For composite fonts (Type0/CID), bytes are decoded as 2-byte codes.
295 */
296 private decodeStringToCodes(bytes: Uint8Array, font: PdfFont): number[] {
297 const codes: number[] = [];
298
299 // Check if this is a composite font (Type0)
300 // Composite fonts use 2-byte character codes
301 if (font.subtype === "Type0") {
302 // Read as big-endian 16-bit values
303 for (let i = 0; i < bytes.length - 1; i += 2) {
304 const code = (bytes[i] << 8) | bytes[i + 1];
305 codes.push(code);
306 }
307
308 // Handle odd byte at end (shouldn't happen in valid PDFs)
309 if (bytes.length % 2 === 1) {
310 codes.push(bytes[bytes.length - 1]);
311 }
312 } else {
313 // Simple font - each byte is a character code
314 for (const byte of bytes) {
315 codes.push(byte);
316 }
317 }
318
319 return codes;
320 }
321
322 /**
323 * Get a number from a content token.

Callers 1

showStringMethod · 0.95

Calls 1

pushMethod · 0.80

Tested by

no test coverage detected