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

Function encodePdfDocEncoding

src/helpers/encoding.ts:219–261  ·  view source on GitHub ↗
(text: string)

Source from the content-addressed store, hash-verified

217 * Returns null if string contains characters not in PDFDocEncoding.
218 */
219export function encodePdfDocEncoding(text: string): Uint8Array | null {
220 const bytes: number[] = [];
221
222 for (const char of text) {
223 // biome-ignore lint/style/noNonNullAssertion: char will exist since it's a string
224 const code = char.codePointAt(0)!;
225
226 // Check reverse mapping first (for special chars like €, •, —)
227 const mapped = UNICODE_TO_PDF_DOC.get(code);
228
229 if (mapped !== undefined) {
230 bytes.push(mapped);
231
232 continue;
233 }
234
235 // Tab, newline, carriage return
236 if (code === TAB || code === LF || code === CR) {
237 bytes.push(code);
238
239 continue;
240 }
241
242 // ASCII printable range
243 if (code >= 0x20 && code <= 0x7f) {
244 bytes.push(code);
245
246 continue;
247 }
248
249 // Latin-1 supplement (0xA1-0xFF, except 0xAD which is undefined)
250 if (code >= 0xa1 && code <= 0xff && code !== 0xad) {
251 bytes.push(code);
252
253 continue;
254 }
255
256 // Cannot encode this character
257 return null;
258 }
259
260 return new Uint8Array(bytes);
261}
262
263/**
264 * Encode string as UTF-16BE with BOM.

Callers 2

encoding.test.tsFile · 0.90
encodeTextStringFunction · 0.85

Calls 2

pushMethod · 0.80
getMethod · 0.45

Tested by

no test coverage detected