Skip to content

Offer rendered height and width on text objects#425

Open
petero-dk wants to merge 6 commits into
modesty:masterfrom
zealofzebras:renderedheight
Open

Offer rendered height and width on text objects#425
petero-dk wants to merge 6 commits into
modesty:masterfrom
zealofzebras:renderedheight

Conversation

@petero-dk

Copy link
Copy Markdown

This pull request introduces improvements to the PDF text rendering pipeline, ensuring more accurate vertical text metrics and enhancing the output data structure. The main changes involve passing and calculating the rendered text height (renderedHeight) throughout the rendering and data extraction layers, updating method signatures, and reflecting these changes in the output and documentation. Additionally, a new test script is added for verifying hidden field extraction.

Rendering and Text Metrics Improvements:

  • Updated the CanvasGraphics rendering logic in base/display/canvas.js to calculate and pass renderedHeight when rendering both Type3 and regular fonts, and to compute vertical and horizontal scaling factors for more accurate text box metrics. [1] [2] [3]
  • Modified the CanvasRenderingContext2D_ class in lib/pdfcanvas.js to accept and propagate the new renderedHeight parameter in fillText and strokeText methods. [1] [2]
  • Enhanced the PDFFont class in lib/pdffont.js to receive renderedHeight, calculate horizontal scaling, and output both rendered width (rw) and rendered height (rh) in the text block data. [1] [2]

Output Data Structure and Documentation:

  • Updated the documentation in readme.md to describe the new h (rendered text box height) field in the Texts array, clarifying its meaning and independence from horizontal metrics.

Testing:

  • Added a new test script test/test-hidden-field.cjs to verify extraction and display of text, field, and boxset metrics, including the new rendered width and height fields.

petero-dk and others added 6 commits July 21, 2026 18:04
… units

rw and rh were computed in raw PDF text-space units rather than viewport
pixels, making them ~1.5x too small relative to x/y coordinates.

In showText(), canvasWidth, curFontSize, and renderedHeight are all
accumulated in PDF text-space (before the viewport transform). x/y come
from getCoords_(0,0) which applies the full cumulative canvas matrix
(viewport × text matrix), so they ARE in viewport pixels.

Fix: multiply the PDF-space components by the viewport scale factor
(derived from this.baseTransform) before passing to fillText, so that
rw and rh end up in the same coordinate system as x and y.

Result for a 20pt×20pt box at (20pt, 20pt):
  Before: x=1, y=1.199, rw=0.799, rh=0.737
  After:  x=1, y=1.199, rw=1.198, rh=1.043  (rw≈y, rh≈x as expected)

Applied to both the regular-font and Type3-font rendering paths.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant