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

Method showString

src/text/text-extractor.ts:240–288  ·  view source on GitHub ↗

* Show a string and extract characters.

(bytes: Uint8Array)

Source from the content-addressed store, hash-verified

238 * Show a string and extract characters.
239 */
240 private showString(bytes: Uint8Array): void {
241 const font = this.state.font;
242
243 if (!font) {
244 // No font set - can't decode text
245 return;
246 }
247
248 // Decode bytes to character codes based on font type
249 const codes = this.decodeStringToCodes(bytes, font);
250
251 for (const code of codes) {
252 // Get Unicode character from font
253 const char = font.toUnicode(code);
254
255 // Skip if we can't decode to Unicode
256 if (!char) {
257 // Still advance position even if we can't decode
258 const width = font.getWidth(code);
259 this.state.advanceChar(width, false);
260 continue;
261 }
262
263 // Get glyph width
264 const width = font.getWidth(code);
265
266 // Calculate bounding box
267 const bbox = this.state.getCharBbox(width);
268
269 // Create extracted character
270 this.chars.push({
271 char,
272 bbox: {
273 x: bbox.x,
274 y: bbox.y,
275 width: bbox.width,
276 height: bbox.height,
277 },
278 fontSize: this.state.effectiveFontSize,
279 fontName: font.baseFontName,
280 baseline: bbox.baseline,
281 sequenceIndex: this.chars.length,
282 });
283
284 // Advance text position
285 const isSpace = char === " " || char === "\u00A0"; // Space or non-breaking space
286 this.state.advanceChar(width, isSpace);
287 }
288 }
289
290 /**
291 * Decode string bytes to character codes.

Callers 2

handleTjMethod · 0.95
handleTJMethod · 0.95

Calls 6

decodeStringToCodesMethod · 0.95
advanceCharMethod · 0.80
getCharBboxMethod · 0.80
pushMethod · 0.80
toUnicodeMethod · 0.45
getWidthMethod · 0.45

Tested by

no test coverage detected