* 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)
| 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. |
no test coverage detected