Repository navigation
Fix incorrect text stroke width caused by stale pixel-width cache - #21829
Conversation
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.
Codecov Report❌ Patch coverage is
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
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
/botio browsertest |
From: Bot.io (Linux m4)ReceivedCommand cmd_browsertest from @timvandermeij received. Current queue size: 0 Live output at: http://54.241.84.105:8877/32d52cb7e014126/output.txt |
From: Bot.io (Windows)ReceivedCommand cmd_browsertest from @timvandermeij received. Current queue size: 0 Live output at: http://54.193.163.58:8877/86d20c5a2b43299/output.txt |
From: Bot.io (Linux m4)SuccessFull output at http://54.241.84.105:8877/32d52cb7e014126/output.txt Total script time: 18.94 mins
|
From: Bot.io (Windows)SuccessFull output at http://54.193.163.58:8877/86d20c5a2b43299/output.txt Total script time: 23.73 mins
|
|
/botio makeref |
From: Bot.io (Windows)ReceivedCommand cmd_makeref from @calixteman received. Current queue size: 0 Live output at: http://54.193.163.58:8877/d5a02c3e079d4d5/output.txt |
From: Bot.io (Linux m4)ReceivedCommand cmd_makeref from @calixteman received. Current queue size: 0 Live output at: http://54.241.84.105:8877/fc46b7e84407355/output.txt |
From: Bot.io (Linux m4)SuccessFull output at http://54.241.84.105:8877/fc46b7e84407355/output.txt Total script time: 18.99 mins
|
From: Bot.io (Windows)SuccessFull output at http://54.193.163.58:8877/d5a02c3e079d4d5/output.txt Total script time: 23.66 mins
|
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
getSinglePixelWidthcomputes 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, whileshowTextapplies 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.1caches10.The following run at scale
36should recompute approximately0.03, but uses the cached10instead.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.
getSinglePixelWidthalso 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.pdfis the only one that reachesgetSinglePixelWidthat 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.pdfno longer matches the MD5 recorded in the manifest.zerowidthline.pdfrenders identically before and after the change.Fixes #20294