Skip to content

Fix incorrect text stroke width caused by stale pixel-width cache - #21829

Merged
calixteman merged 1 commit into
mozilla:masterfrom
HyeryongChoi:getSinglePixelWidth-rm-cache
Aug 31, 2026
Merged

calixteman merged 1 commit into
mozilla:masterfrom
HyeryongChoi:getSinglePixelWidth-rm-cache

Conversation

@HyeryongChoi

@HyeryongChoi HyeryongChoi commented Aug 26, 2026 •

Copy link
Copy Markdown
Contributor

1. Symptom

Issue #20294 reports what appears to be a hidden speech-bubble shape being rendered black on page 5 of a PDF, covering the text underneath.

No hidden shape is actually being rendered. Some text is rendered with an excessively wide stroke, so that the glyph outlines cover an entire column of the document, and that is what reads as a black shape.

2. Cause

getSinglePixelWidth computes the conversion factor from one screen pixel to the current coordinate system, so its return value depends on the current transform.

However, the result is cached in _cachedGetSinglePixelWidth, while showText applies the text matrix to the canvas without invalidating that cache.

A ratio computed for one text run can therefore be reused by the next one under a different transform.

In the PDF from #20294, a text run with a text-matrix scale of 0.1 caches 10.
The following run at scale 36 should recompute approximately 0.03, but uses the cached 10 instead.

The stroke thus ends up about 334 times wider than expected.

3. Why the cache is removed rather than invalidated

The cache itself is removed, instead of adding another invalidation in showText.

Keeping the cache would require validating that the stored ratio is still correct for the current transform. Doing that means reading the current transform, which accounts for most of the cost the caching was meant to avoid.

getSinglePixelWidth also has a single caller, and is called at most once per text-showing operation. With correct invalidation there would therefore be no opportunity for a cache hit in the first place.

4. How often the code is actually reached

The call-site is only reached when rendering stroked text with a line width of 0.

Rendering the first three pages of each of the 977 PDF files that were already committed in test/pdfs, zerowidthline.pdf is the only one that reaches getSinglePixelWidth at all, and even there the cache is never hit.

The practical benefit of keeping the cache is therefore very limited, and removing it seems like the simpler and safer fix.

5. Testing

A reduced test case is added: a 629-byte file with two stroked text runs whose only meaningful difference is the text matrix scale, which is the smallest input that reproduces the bug. Without the patch the glyph outlines of the second run cover half the page.

The unit tests pass, with the same single failure that already occurs on master: bug1820909.pdf no longer matches the MD5 recorded in the manifest.

Fixes #20294

Issue mozilla#20294 reports what appears to be a hidden black shape on page 5 of a PDF,
covering the text underneath. No hidden shape is actually being rendered: some
text is rendered with an excessively wide stroke, causing the glyph outlines to
cover an entire column.

`getSinglePixelWidth` computes the conversion factor from one screen pixel to
the current coordinate system, so its result depends on the current transform.
However, the result is cached in `_cachedGetSinglePixelWidth`, while `showText`
applies the text matrix to the canvas without invalidating that cache. A value
computed for one text run can therefore be reused by the next under a different
transform.

In the PDF from mozilla#20294, a run with a text-matrix scale of `0.1` caches `10`, and
the following run at scale `36` reuses it instead of recomputing approximately
`0.03`, making the stroke about 334 times wider than expected.

Remove the cache rather than adding another invalidation in `showText`. Keeping
it correct would require reading the current transform to validate the cached
value, which accounts for most of the cost the cache was intended to avoid.
`getSinglePixelWidth` also has a single caller and is called at most once per
text-showing operation, so with correct invalidation there would be no
opportunity for a cache hit.
Copilot AI lite review requested due to automatic review settings August 26, 2026 16:04

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@codecov-commenter

codecov-commenter commented Aug 28, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 42.85714% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.18%. Comparing base (49ff3dc) to head (3d282d4).
⚠️ Report is 37 commits behind head on master.

Files with missing lines Patch % Lines
src/display/canvas.js 42.85% 4 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##           master   #21829   +/-   ##
=======================================
  Coverage   90.18%   90.18%           
=======================================
  Files         264      264           
  Lines       67312    67306    -6     
=======================================
- Hits        60707    60702    -5     
+ Misses       6605     6604    -1     
Flag Coverage Δ
browsertest 66.27% <42.85%> (-0.05%) ⬇️
fonttest 8.92% <ø> (ø)
integrationtest 69.40% <0.00%> (+<0.01%) ⬆️
unittest 58.37% <0.00%> (+<0.01%) ⬆️
unittestcli 56.94% <0.00%> (-0.02%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@timvandermeij

Copy link
Copy Markdown
Collaborator

/botio browsertest

@moz-tools-bot

Copy link
Copy Markdown
Collaborator

From: Bot.io (Linux m4)


Received

Command cmd_browsertest from @timvandermeij received. Current queue size: 0

Live output at: http://54.241.84.105:8877/32d52cb7e014126/output.txt

@moz-tools-bot

Copy link
Copy Markdown
Collaborator

From: Bot.io (Windows)


Received

Command cmd_browsertest from @timvandermeij received. Current queue size: 0

Live output at: http://54.193.163.58:8877/86d20c5a2b43299/output.txt

@moz-tools-bot

Copy link
Copy Markdown
Collaborator

From: Bot.io (Linux m4)


Success

Full output at http://54.241.84.105:8877/32d52cb7e014126/output.txt

Total script time: 18.94 mins

  • Regression tests: Passed

@moz-tools-bot

Copy link
Copy Markdown
Collaborator

From: Bot.io (Windows)


Success

Full output at http://54.193.163.58:8877/86d20c5a2b43299/output.txt

Total script time: 23.73 mins

  • Regression tests: Passed

@calixteman calixteman left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thank you.

@calixteman
calixteman merged commit 4ed57bc into mozilla:master Aug 31, 2026
21 checks passed
@calixteman

Copy link
Copy Markdown
Contributor

/botio makeref

@moz-tools-bot

Copy link
Copy Markdown
Collaborator

From: Bot.io (Windows)


Received

Command cmd_makeref from @calixteman received. Current queue size: 0

Live output at: http://54.193.163.58:8877/d5a02c3e079d4d5/output.txt

@moz-tools-bot

Copy link
Copy Markdown
Collaborator

From: Bot.io (Linux m4)


Received

Command cmd_makeref from @calixteman received. Current queue size: 0

Live output at: http://54.241.84.105:8877/fc46b7e84407355/output.txt

@moz-tools-bot

Copy link
Copy Markdown
Collaborator

From: Bot.io (Linux m4)


Success

Full output at http://54.241.84.105:8877/fc46b7e84407355/output.txt

Total script time: 18.99 mins

  • Make references: Passed
  • Check references: Passed

@moz-tools-bot

Copy link
Copy Markdown
Collaborator

From: Bot.io (Windows)


Success

Full output at http://54.193.163.58:8877/d5a02c3e079d4d5/output.txt

Total script time: 23.66 mins

  • Make references: Passed
  • Check references: Passed

This branch was previously deployed

1 inactive deployment
code-coverage — 3d282d44 Deployed Aug 28, 2026 by HyeryongChoi via Test (26) #16895
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Hidden shapes in a PDF document are rendering as black objects and obscuring the underlying page

6 participants