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

Function canEncodePdfDoc

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

Source from the content-addressed store, hash-verified

182 * Check if a string can be encoded in PDFDocEncoding.
183 */
184export function canEncodePdfDoc(text: string): boolean {
185 for (const char of text) {
186 // biome-ignore lint/style/noNonNullAssertion: char will exist since it's a string
187 const code = char.codePointAt(0)!;
188
189 // Check if it's in the reverse mapping (special chars)
190 if (UNICODE_TO_PDF_DOC.has(code)) {
191 continue;
192 }
193
194 // Tab, newline, carriage return are allowed
195 if (code === TAB || code === LF || code === CR) {
196 continue;
197 }
198
199 // ASCII printable range
200 if (code >= 0x20 && code <= 0x7f) {
201 continue;
202 }
203
204 // Latin-1 supplement (0xA1-0xFF, except 0xAD)
205 if (code >= 0xa1 && code <= 0xff && code !== 0xad) {
206 continue;
207 }
208
209 // Character cannot be encoded
210 return false;
211 }
212 return true;
213}
214
215/**
216 * Encode string as PDFDocEncoding.

Callers 2

fromStringMethod · 0.90
encoding.test.tsFile · 0.90

Calls 1

hasMethod · 0.45

Tested by

no test coverage detected