Repository navigation
Keep content in place when a rewritten page is cut into streams - #125
Merged
Merged
Conversation
A redaction on a page from "Microsoft: Print To PDF" moved most of the page. That producer sets one cm for the whole page, never undone, and cuts the content into streams in the middle of q ... cm ... Q, so each stream ends at another CTM than it began with. A change to existing content writes every stream again. After each one the generator wrote a cm to give the next stream the CTM the old stream ended at. That line is upstream's and has the matrix product the wrong way round (prev^-1 * ctm where ctm * prev^-1 is needed): right only where the two commute, so everything from the third stream on moved. Each stream also began by undoing the CTM the old stream began with, which cannot work where that CTM has no inverse. Every object is written with its whole matrix, so nothing needs to pass from one stream to the next. A rewrite of every stream now forgets the CTMs the parser recorded per stream (CPDF_PageObjectHolder::ForgetStreamCTMs): each stream begins and ends at the CTM the content begins with, stands alone, and upstream's cm is never reached. Upstream's line is left as it is. v2 wrote one stream for this (936df8f), which 13d3251 took out again without a test for the case; no fixture carried a transform across a stream boundary. Tests, each on a document and on a layer in a transaction: - redact_split_content_streams.pdf: the Print To PDF shape, and a cut after a cm with no inverse. - A second rewrite of the loaded page, and an object added before and after a rewrite. - WhereContentIsCutIntoStreamsChangesNothing runs every page of the rewrite cases with its content as it is, cut into three streams, and with every operator in a stream of its own, and expects the same page each time. Every stream written must stand alone (q ... Q balanced, no cm outside it). - TextLeftAfterARedactionKeepsItsPlace: the characters left after a redaction keep their origins. Renders can't show a drift under half a pixel.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A redaction on a page written by "Microsoft: Print To PDF" moved most of the page: text, table lines and images ended up displaced or off the page. v2 does not do this. Any change to existing page content triggers it, not only redaction.
Cause
That producer sets one
cmfor the whole page (scale and flip, never undone) and then cuts the content into streams wherever a size limit falls, in the middle ofq ... cm ... Q. So each stream after the first ends at another CTM than it began with.A change to existing content writes every stream again (rule
faithful-content-rewrite). After each stream the generator wrote acmto give the next stream the CTM the old stream ended at. That line is upstream's and has the matrix product the wrong way round:CFX_Matrix ctm_difference = prev_ctm.GetInverse() * ctm; // needed: ctm * prev_ctm.GetInverse()It is right only where the two matrices commute (the first stream, or translations only), so everything from the third stream on moved. Upstream rewrites only the streams that changed and its one fixture for this (
rectangles_with_leaky_ctm) has a single stream, so it rarely runs there. Each stream also began by undoing the CTM the old stream began with, which cannot work where that CTM has no inverse.History: v2 wrote one canonical stream for this (936df8f, May 19), which had no test of its own. 13d3251 (Aug 5) took it out again in favour of rewriting every stream in place, and no fixture carried a transform across a stream boundary, so nothing failed.
Change
Every page object is written with its whole matrix, so nothing needs to pass from one stream to the next.
CPDF_PageObjectHolder::ForgetStreamCTMs()drops the CTMs the parser recorded per stream.CPDF_PageContentGenerator::GenerateModifiedStreams()calls it where a pass adds every stream to the dirty set. Each stream then begins and ends at the CTM the content begins with, stands alone, and upstream'scmis never reached: a pass either appends a stream or writes them all.faithful-content-rewriterule now says that where content is cut into streams makes no difference.Tests
In
epdf_content_rewrite_embeddertest.cpp, each on a document and on a layer in a transaction. All fail without the change.KeepsContentCutIntoStreamsandKeepsContentCutInsideATransformWithoutAnInverse: the new fixtureredact_split_content_streams.pdf, hand-written. Page 0 is the Print To PDF shape, page 1 a cut after acmwith no inverse.RewritingAgainKeepsContentCutIntoStreams: a second redaction on the loaded page.AnObjectAddedAroundARewriteIsDrawnInPlace: an object added before and after a rewrite.WhereContentIsCutIntoStreamsChangesNothing: every page ofredact_rewrite_cases.pdfand three other fixtures, written again with its content as it is, cut into three streams, and with every operator in a stream of its own. Each cut copy must draw what the file draws, before the rewrite and after it, and every stream written must stand alone (q ... Qbalanced, nocmoutside it). About 6 s per mode in a debug build.TextLeftAfterARedactionKeepsItsPlace: the characters left after a redaction keep their origins to 0.001 pt. Renders can't show a drift under half a pixel, because each glyph is drawn on a whole pixel.On the unchanged code, the cut copies of the 51 case pages fail only the stand-alone check (52 streams with a
cmoutsideq ... Q). Their transforms nest one level deep, where the wrong product still gives the right matrix. The pixels differ only on the new fixture.Checked
macOS arm64, against the same tree without the change:
FPDFEditEmbedderTest.SetTextRenderMode, the only one that writes content, fails with the same output either way.pdfium_testnever regenerates content.check-fork-tags.py: no untagged block.embedpdf_layer_mode_writers.txtis unchanged.Not run: the Linux suite. It should run before this merges, since the release runs it.
Left