Skip to content

Don't draw the measured text in heightOfString after a continued text call - #1832

Open
mkantautas wants to merge 1 commit into
foliojs:masterfrom
mkantautas:fix-heightofstring-continued-measurement
Open

mkantautas wants to merge 1 commit into
foliojs:masterfrom
mkantautas:fix-heightofstring-continued-measurement

Conversation

@mkantautas

Copy link
Copy Markdown

What kind of change does this PR introduce?

Bug fix. Fixes #1284.

doc.text('abc', 0, 0, { continued: true }) followed by doc.heightOfString('X') draws X into the page. heightOfString measures through _text, which reuses the line wrapper a continued text() call leaves in this._wrapper. That wrapper's line listener is this._line, which draws, and the measuring callback that heightOfString passes is never attached.

heightOfString now clears this._wrapper before a wrapping measurement, unless the measurement itself passes continued: true. _text then creates a new wrapper with the measuring callback. The returned height is measured from the start of a line, as when no text is pending, instead of from the pending text's offset. The default measurement still ends a pending run as before. Non-wrapping measurements preserve the pending wrapper, and measurements without a pending wrapper are unchanged.

Limitation: when continued: true is passed to heightOfString itself, the previous behavior is kept, including drawing the measured text. Clearing the wrapper in that case too would leave the measurement's own wrapper behind, and the next text() call would then not be drawn at all.

Tests, in the new tests/unit/heightOfString-1284.spec.js:

  • the calls from heightOfString renders the text #1284: fails without this change (X is drawn), passes with it
  • the same calls without continued: X is not drawn
  • heightOfString('X', { continued: true }) after a continued text(), then text('def'): def is drawn (this test fails if the wrapper is cleared unconditionally)
  • a non-wrapping measurement after a continued text() call: the next text() keeps its horizontal position (this test fails if the wrapper is cleared for a non-wrapping measurement)

yarn lint, yarn prettier, yarn build and all 583 tests pass on Node 20, 22 and 24. These local runs use yarn test --maxWorkers=2 in containers limited to two CPUs, with the normal test timeout unchanged. Earlier Node 20 runs at default concurrency in the same kind of container timed out in 16 visual tests on the unchanged base and in 17 with an earlier revision of this change, while Node reported 32 available CPUs.

Checklist:

  • Unit Tests
  • Documentation N/A
  • Update CHANGELOG.md
  • Ready to be merged

This branch has not been deployed

No deployments
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.

heightOfString renders the text

1 participant