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

Method encodeTextToBytes

src/document/forms/form-font.ts:204–225  ·  view source on GitHub ↗

* Encode text as bytes for use in a PdfString. * * For CID fonts with Identity-H/V encoding, the character codes in the * PDF string are CIDs. With Identity CIDToGIDMap, CID = GID, so the * character code must equal the glyph ID in the font program. * * The encoding pipeline is:

(text: string)

Source from the content-addressed store, hash-verified

202 * For simple fonts, produces single-byte codes.
203 */
204 encodeTextToBytes(text: string): Uint8Array {
205 if (this.isCIDFont) {
206 const codePoints = Array.from(text, char => char.codePointAt(0)!);
207 const bytes = new Uint8Array(codePoints.length * 2);
208
209 for (let i = 0; i < codePoints.length; i++) {
210 // Use CIDFont mapping if available, otherwise fall back to raw code point
211 const charCode = this.cidFont
212 ? this.cidFont.getCharCodeForUnicode(codePoints[i])
213 : codePoints[i];
214 bytes[i * 2] = (charCode >> 8) & 0xff;
215 bytes[i * 2 + 1] = charCode & 0xff;
216 }
217
218 return bytes;
219 }
220
221 // Simple font: single-byte encoding
222 const codes = this.encodeText(text);
223
224 return new Uint8Array(codes);
225 }
226
227 /**
228 * Get width of text in points at a given font size.

Callers 2

form-font.test.tsFile · 0.80
encodeTextForFontFunction · 0.80

Calls 3

encodeTextMethod · 0.95
fromMethod · 0.80
getCharCodeForUnicodeMethod · 0.80

Tested by

no test coverage detected