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

Function encodeUtf16BE

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

Source from the content-addressed store, hash-verified

264 * Encode string as UTF-16BE with BOM.
265 */
266export function encodeUtf16BE(text: string): Uint8Array {
267 const bytes: number[] = [0xfe, 0xff]; // BOM
268
269 for (const char of text) {
270 // biome-ignore lint/style/noNonNullAssertion: char will exist since it's a string
271 const code = char.codePointAt(0)!;
272
273 if (code > 0xffff) {
274 // Surrogate pair needed for characters outside BMP
275 const adjusted = code - 0x10000;
276 const high = 0xd800 + (adjusted >> 10);
277 const low = 0xdc00 + (adjusted & 0x3ff);
278
279 bytes.push(high >> 8, high & 0xff);
280 bytes.push(low >> 8, low & 0xff);
281 } else {
282 bytes.push(code >> 8, code & 0xff);
283 }
284 }
285
286 return new Uint8Array(bytes);
287}
288
289/**
290 * Encode string for PDF text string.

Callers 2

encoding.test.tsFile · 0.90
encodeTextStringFunction · 0.85

Calls 1

pushMethod · 0.80

Tested by

no test coverage detected