diff --git a/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp b/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp index b14d525b8..15a8104aa 100644 --- a/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp +++ b/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp @@ -639,6 +639,14 @@ CPDF_PageContentGenerator::GenerateModifiedStreams() { for (int32_t i = 0; i <= last_index; ++i) { all_dirty_streams.insert(i); } + // Every stream is written again from objects that carry their whole + // matrix, so no stream takes a CTM from the one before it or leaves one + // for the next: where the content was cut into streams stops mattering. + // Without this, each stream would undo the CTM the old stream began with + // and re-create the one it ended with, which fails where that CTM has no + // inverse, and upstream's `cm` for it below multiplies the wrong way + // round (see EPDFContentRewriteEmbedderTest.KeepsContentCutIntoStreams). + obj_holder_->ForgetStreamCTMs(); } // --- end embedpdf diff --git a/core/fpdfapi/page/cpdf_pageobjectholder.cpp b/core/fpdfapi/page/cpdf_pageobjectholder.cpp index 6d2f0e309..a8f361cfa 100644 --- a/core/fpdfapi/page/cpdf_pageobjectholder.cpp +++ b/core/fpdfapi/page/cpdf_pageobjectholder.cpp @@ -441,6 +441,11 @@ CFX_Matrix CPDF_PageObjectHolder::GetCTMAtEndOfStream(int32_t stream) { return it != all_ctms_.end() ? it->second : all_ctms_.rbegin()->second; } +// EmbedPDF(faithful-content-rewrite): see the header. +void CPDF_PageObjectHolder::ForgetStreamCTMs() { + all_ctms_.clear(); +} + void CPDF_PageObjectHolder::LoadTransparencyInfo() { RetainPtr pGroup = dict_->GetDictFor("Group"); if (!pGroup) { diff --git a/core/fpdfapi/page/cpdf_pageobjectholder.h b/core/fpdfapi/page/cpdf_pageobjectholder.h index d1e6440c7..dcab2e4cb 100644 --- a/core/fpdfapi/page/cpdf_pageobjectholder.h +++ b/core/fpdfapi/page/cpdf_pageobjectholder.h @@ -197,6 +197,12 @@ class CPDF_PageObjectHolder { // `stream` must be non-negative. CFX_Matrix GetCTMAtEndOfStream(int32_t stream); + // EmbedPDF(faithful-content-rewrite): for a rewrite of every content + // stream. The parser recorded the CTM each stream ended at, which the next + // one began with. Streams written again take nothing from each other, so + // every stream now begins and ends at the CTM the content begins with. + void ForgetStreamCTMs(); + AllRemovedResourcesMap& all_removed_resources_map() { return all_removed_resources_map_; } diff --git a/docs/embedpdf/fork-ledger.md b/docs/embedpdf/fork-ledger.md index c2a1c6909..66d49957f 100644 --- a/docs/embedpdf/fork-ledger.md +++ b/docs/embedpdf/fork-ledger.md @@ -234,13 +234,21 @@ neither is secret: the `/Perms` filler bytes (encrypted under the file key) and ### faithful-content-rewrite **Rule.** Regenerating a page's or form's content writes back what the parser read: colour spaces by their names, the matrix a form's content starts at, soft-mask matrices, Type3 glyphs, `TJ` -arrays, a form XObject's own resource names. +arrays, a form XObject's own resource names. A change to existing content writes every stream +again, and each stands alone (`q ... Q`, no `cm` outside it): where the content was cut into +streams makes no difference. **Why.** Redaction and editing rewrite content, and anything the writer drops shows up far from the -edit (embedpdf/runtime#58, #59). +edit (embedpdf/runtime#58, #59). Upstream writes only the streams that changed and gives each next +stream its CTM with a `cm` whose matrix product is the wrong way round. On a page cut into streams +inside `q ... cm ... Q`, as "Microsoft: Print To PDF" writes them, a redaction moved everything +from the third stream on. **Where.** `CPDF_PageContentGenerator`, `CPDF_PageContentManager`, `CPDF_StreamContentParser`, -`CPDF_ColorState`, `CPDF_Type3Font`. -**Guarded by.** `epdf_content_rewrite_embeddertest.cpp`, `cpdf_contentnames_unittest.cpp`. +`CPDF_ColorState`, `CPDF_Type3Font`, `CPDF_PageObjectHolder::ForgetStreamCTMs`. +**Guarded by.** `epdf_content_rewrite_embeddertest.cpp` (`WhereContentIsCutIntoStreamsChangesNothing` +runs every case page again with its content cut into streams), `cpdf_contentnames_unittest.cpp`. **When upstream touches this.** The content generator, and what the parser records about state. +The fork never reaches upstream's `cm` between streams: a pass appends a stream or writes them all. +If upstream corrects or moves it, a rewrite of every stream still forgets the stream CTMs first. ### redaction **Rule.** Redaction removes text, paths, images and marked content under its regions, and draws diff --git a/fpdfsdk/epdf_content_rewrite_embeddertest.cpp b/fpdfsdk/epdf_content_rewrite_embeddertest.cpp index 09b0240b7..667f257ad 100644 --- a/fpdfsdk/epdf_content_rewrite_embeddertest.cpp +++ b/fpdfsdk/epdf_content_rewrite_embeddertest.cpp @@ -7,6 +7,10 @@ // did: each test changes one thing, saves, reloads and compares the page // with how it rendered before, everywhere outside the change. Every test runs // on a document and on a layer in a transaction, as engine jobs do. +// +// Where a page's content is cut into streams must make no difference either: +// a reader treats the streams as one. See +// WhereContentIsCutIntoStreamsChangesNothing. #include #include @@ -18,11 +22,15 @@ #include #include "core/fpdfapi/font/cpdf_tounicodemap.h" +#include "core/fpdfapi/page/cpdf_pageobject.h" +#include "core/fpdfapi/page/cpdf_streamparser.h" #include "core/fpdfapi/parser/cpdf_array.h" #include "core/fpdfapi/parser/cpdf_dictionary.h" #include "core/fpdfapi/parser/cpdf_document.h" +#include "core/fpdfapi/parser/cpdf_reference.h" #include "core/fpdfapi/parser/cpdf_stream.h" #include "core/fpdfapi/parser/cpdf_stream_acc.h" +#include "core/fxcrt/span.h" #include "fpdfsdk/cpdfsdk_helpers.h" #include "public/cpp/fpdf_scopers.h" #include "public/epdf_layer.h" @@ -100,6 +108,18 @@ enum CasePage : int { kType3FontSelectedAgainByAForm = 50, }; +// The fixture pages of redact_split_content_streams.in. +enum SplitStreamsPage : int { + kCutAsPrintToPDFDoes = 0, + kCutInsideATransformWithoutAnInverse = 1, +}; + +// How CutIntoStreams() cuts a page's content. +enum class Cut { + kIntoThree, + kAfterEveryOperator, +}; + // The page on white at a pixel per point, its annotations included. ScopedFPDFBitmap RenderAtPageSize(FPDF_PAGE page) { const int width = static_cast(FPDF_GetPageWidthF(page)); @@ -260,8 +280,44 @@ std::wstring TextInside(FPDF_PAGE page, const std::vector& areas) { return found; } -// The decoded content of page `index` of `doc`. -std::string PageContent(FPDF_DOCUMENT doc, int index) { +// A character `page` shows, and its origin in page space. +struct PlacedChar { + unsigned int unicode; + double x; + double y; +}; + +// The characters `page` shows whose box centre lies outside `areas`. +std::vector CharsOutside(FPDF_PAGE page, + const std::vector& areas) { + std::vector chars; + ScopedFPDFTextPage text(FPDFText_LoadPage(page)); + for (int i = 0; i < FPDFText_CountChars(text.get()); ++i) { + PlacedChar placed = {FPDFText_GetUnicode(text.get(), i), 0, 0}; + double left; + double right; + double bottom; + double top; + if (placed.unicode <= ' ' || + !FPDFText_GetCharBox(text.get(), i, &left, &right, &bottom, &top) || + !FPDFText_GetCharOrigin(text.get(), i, &placed.x, &placed.y)) { + continue; + } + const float x = static_cast((left + right) / 2); + const float y = static_cast((bottom + top) / 2); + bool inside = false; + for (const FS_RECTF& area : areas) { + inside = inside || Contains(area, x, y, 0); + } + if (!inside) { + chars.push_back(placed); + } + } + return chars; +} + +// The decoded content of each content stream of page `index` of `doc`. +std::vector PageContentStreams(FPDF_DOCUMENT doc, int index) { CPDF_Document* pdf = CPDFDocumentFromFPDFDocument(doc); RetainPtr contents = pdf->GetPageDictionary(index)->GetDirectObjectFor("Contents"); @@ -273,16 +329,76 @@ std::string PageContent(FPDF_DOCUMENT doc, int index) { } else if (contents && contents->IsStream()) { streams.push_back(pdfium::WrapRetain(contents->AsStream())); } - std::string content; + std::vector content; for (const auto& stream : streams) { auto acc = pdfium::MakeRetain(stream); acc->LoadAllDataFiltered(); const auto bytes = acc->GetSpan(); - content.append(bytes.begin(), bytes.end()); + content.emplace_back(bytes.begin(), bytes.end()); } return content; } +// The decoded content of page `index` of `doc`. +std::string PageContent(FPDF_DOCUMENT doc, int index) { + std::string content; + for (const std::string& stream : PageContentStreams(doc, index)) { + content += stream; + } + return content; +} + +// Calls `visit` with each operator of `content` and the offset just after +// it. An inline image (BI ... EI) counts as one operator, EI: its data is no +// tokens. +void ForEachOperator(const std::string& content, + const std::function& visit) { + CPDF_StreamParser parser(pdfium::as_byte_span(content)); + bool in_image = false; + while (true) { + const CPDF_StreamParser::ElementType type = parser.ParseNextElement(); + if (type == CPDF_StreamParser::ElementType::kEndOfData) { + return; + } + if (type != CPDF_StreamParser::ElementType::kKeyword) { + continue; + } + const ByteStringView op = parser.GetWord(); + if (op == "BI") { + in_image = true; + } else if (op == "EI") { + in_image = false; + } + if (!in_image) { + visit(op, parser.GetPos()); + } + } +} + +// Expects each content stream of page `index` of `doc` to stand alone: it +// restores what it saves (q ... Q) and sets no CTM outside of that. Such a +// stream leaves the one after it nothing, so where one stream ends and the +// next begins has no effect. +void ExpectEachStreamStandsAlone(FPDF_DOCUMENT doc, int index) { + for (const std::string& content : PageContentStreams(doc, index)) { + int depth = 0; + ForEachOperator(content, [&](ByteStringView op, size_t) { + if (op == "q") { + ++depth; + } else if (op == "Q") { + --depth; + EXPECT_GE(depth, 0) + << "a Q without its q on page " << index << ": " << content; + } else if (op == "cm") { + EXPECT_GT(depth, 0) + << "a cm outside q ... Q on page " << index << ": " << content; + } + }); + EXPECT_EQ(0, depth) << "a q without its Q on page " << index << ": " + << content; + } +} + } // namespace // The parameter: true to work on a layer in a transaction, false on a @@ -302,7 +418,15 @@ class EPDFContentRewriteEmbedderTest // Opens the test file `name`, as a layer of it when the parameter says so. FPDF_DOCUMENT Open(const char* name) { const std::string path = PathService::GetTestFilePath(name); - bytes_ = GetFileContents(path.c_str()); + return OpenBytes(GetFileContents(path.c_str())); + } + + // Opens the file `bytes` hold, as Open() does, in place of the open one. + FPDF_DOCUMENT OpenBytes(std::vector bytes) { + saved_.reset(); + doc_.reset(); + base_.reset(); + bytes_ = std::move(bytes); if (layer()) { base_.reset( EPDF_LoadMemBaseDocument64(bytes_.data(), bytes_.size(), nullptr)); @@ -427,6 +551,119 @@ class EPDFContentRewriteEmbedderTest L"REDACTME", inspect); } + // A copy of the test file `name` with every page's content cut into + // streams: after the operators at a third and at two thirds, or after every + // operator, wherever that falls - inside q ... Q, a path, a text object. A + // reader treats a page's streams as one, so the copy draws what the file + // draws. + std::vector CutIntoStreams(const char* name, Cut cut) { + const std::string path = PathService::GetTestFilePath(name); + const std::vector bytes = GetFileContents(path.c_str()); + ScopedFPDFDocument source( + FPDF_LoadMemDocument64(bytes.data(), bytes.size(), nullptr)); + EXPECT_TRUE(source); + if (!source) { + return {}; + } + CPDF_Document* pdf = CPDFDocumentFromFPDFDocument(source.get()); + for (int i = 0; i < FPDF_GetPageCount(source.get()); ++i) { + std::string content; + for (const std::string& stream : PageContentStreams(source.get(), i)) { + content += stream + "\n"; + } + std::vector ends; + ForEachOperator(content, + [&](ByteStringView, size_t end) { ends.push_back(end); }); + if (ends.size() < 3) { + continue; + } + if (cut == Cut::kIntoThree) { + ends = {ends[ends.size() / 3 - 1], ends[2 * ends.size() / 3 - 1], + ends.back()}; + } + auto streams = pdfium::MakeRetain(); + size_t begin = 0; + for (size_t end : ends) { + auto stream = pdf->NewIndirect( + pdfium::as_byte_span(content).subspan(begin, end - begin)); + streams->AppendNew(pdf, stream->GetObjNum()); + begin = end; + } + pdf->GetMutablePageDictionary(i)->SetFor("Contents", std::move(streams)); + } + ClearString(); + EXPECT_TRUE(FPDF_SaveAsCopy(source.get(), this, FPDF_NO_INCREMENTAL)); + const std::string& saved = GetString(); + return std::vector(saved.begin(), saved.end()); + } + + // Opens the file `bytes` hold and writes every page's content again in one + // change: a page showing REDACTME, SECRET or Jane Doe has it redacted, any + // other has its first object marked as changed. Gives each page's render + // from the saved copy, and in `before` each page's render before the + // change. Expects the content written to stand alone + // (ExpectEachStreamStandsAlone()). + std::vector RewriteEveryPage( + std::vector bytes, + std::vector* before) { + std::vector after; + if (!OpenBytes(std::move(bytes))) { + ADD_FAILURE() << "the file does not open"; + return after; + } + const int count = FPDF_GetPageCount(doc_.get()); + std::vector pages; + std::vector redacts; + for (int i = 0; i < count; ++i) { + pages.emplace_back(FPDF_LoadPage(doc_.get(), i)); + FPDF_PAGE page = pages.back().get(); + EXPECT_TRUE(page) << i; + if (!page) { + return after; + } + before->push_back(RenderAtPageSize(page)); + std::vector areas; + for (const wchar_t* word : {L"REDACTME", L"SECRET", L"Jane Doe"}) { + if (areas.empty()) { + areas = AreasOf(page, word); + } + } + Mark(page, areas); + redacts.push_back(!areas.empty()); + } + std::vector written(count); + ChangeAndSave([&] { + for (int i = 0; i < count; ++i) { + FPDF_PAGE page = pages[i].get(); + if (redacts[i]) { + ApplyRedactions(page); + written[i] = true; + } else if (FPDFPage_CountObjects(page) > 0) { + CPDFPageObjectFromFPDFPageObject(FPDFPage_GetObject(page, 0)) + ->SetDirty(true); + ASSERT_TRUE(FPDFPage_GenerateContent(page)); + written[i] = true; + } + } + }); + if (HasFatalFailure()) { + return after; + } + for (int i = 0; i < count; ++i) { + ScopedSavedPage saved = LoadScopedSavedPage(i); + EXPECT_TRUE(saved) << i; + if (!saved) { + return after; + } + after.push_back(RenderAtPageSize(saved.get())); + if (written[i]) { + ExpectEachStreamStandsAlone(saved_document(), i); + } + } + CloseSaved(); + return after; + } + // Page `index` of the open document, rendered (RenderAtPageSize()). ScopedFPDFBitmap RenderOtherPage(int index) { ScopedFPDFPage page(FPDF_LoadPage(doc_.get(), index)); @@ -1338,3 +1575,185 @@ TEST_P(EPDFContentRewriteEmbedderTest, "SELECTIONPRIVATE"); }); } + +// A page as "Microsoft: Print To PDF" writes it: one cm for the whole page, +// never undone, and the content cut into streams in the middle of +// q ... cm ... Q, so each stream ends at another CTM than it began with. +// Every stream is written again and stands alone: none hands the next a CTM. +// (Upstream's cm for that multiplies the wrong way round, which moved +// everything from the third stream on.) +TEST_P(EPDFContentRewriteEmbedderTest, KeepsContentCutIntoStreams) { + ExpectRedactingWordChangesOnlyIt( + "redact_split_content_streams.pdf", kCutAsPrintToPDFDoes, L"SECRET", + [](FPDF_DOCUMENT saved, FPDF_PAGE) { + ExpectEachStreamStandsAlone(saved, kCutAsPrintToPDFDoes); + }); +} + +// The cut falls after a cm with no inverse (a strip of no height): no stream +// written again can undo it to draw in place, so none may begin under it. +TEST_P(EPDFContentRewriteEmbedderTest, + KeepsContentCutInsideATransformWithoutAnInverse) { + ExpectRedactingWordChangesOnlyIt( + "redact_split_content_streams.pdf", kCutInsideATransformWithoutAnInverse, + L"SECRET", [](FPDF_DOCUMENT saved, FPDF_PAGE) { + ExpectEachStreamStandsAlone(saved, + kCutInsideATransformWithoutAnInverse); + }); +} + +// A second rewrite of the loaded page starts from the streams the first one +// wrote, not from the CTMs the page was parsed with. +TEST_P(EPDFContentRewriteEmbedderTest, + RewritingAgainKeepsContentCutIntoStreams) { + ASSERT_TRUE(Open("redact_split_content_streams.pdf")); + ScopedFPDFPage page(FPDF_LoadPage(doc_.get(), kCutAsPrintToPDFDoes)); + ASSERT_TRUE(page); + ScopedFPDFBitmap before = RenderAtPageSize(page.get()); + std::vector areas; + for (const wchar_t* word : {L"SECRET", L"STAYS"}) { + const std::vector found = AreasOf(page.get(), word); + ASSERT_EQ(1u, found.size()); + areas.push_back(found[0]); + Mark(page.get(), found); + Change([&] { ApplyRedactions(page.get()); }); + ASSERT_FALSE(HasFatalFailure()); + } + SaveAndReopen(); + ASSERT_FALSE(HasFatalFailure()); + ExpectSavedPageChangedOnlyIn(kCutAsPrintToPDFDoes, before.get(), areas, + [](FPDF_DOCUMENT saved, FPDF_PAGE) { + ExpectEachStreamStandsAlone( + saved, kCutAsPrintToPDFDoes); + }); +} + +// An object added to the page goes into a stream of its own, which begins by +// undoing the CTM the last stream ended at. Added after a rewrite, it finds +// the streams the rewrite wrote; added before one, its stream is written +// again with the others. Either way it is drawn where it was put. +TEST_P(EPDFContentRewriteEmbedderTest, + AnObjectAddedAroundARewriteIsDrawnInPlace) { + for (const bool add_first : {false, true}) { + SCOPED_TRACE(add_first ? "added before the rewrite" + : "added after the rewrite"); + ASSERT_TRUE(Open("redact_split_content_streams.pdf")); + ScopedFPDFPage page(FPDF_LoadPage(doc_.get(), kCutAsPrintToPDFDoes)); + ASSERT_TRUE(page); + ScopedFPDFBitmap before = RenderAtPageSize(page.get()); + std::vector areas = AreasOf(page.get(), L"SECRET"); + ASSERT_EQ(1u, areas.size()); + Mark(page.get(), areas); + // A red square on empty paper. + const FS_RECTF square = {200, 60, 240, 40}; + const auto add = [&] { + FPDF_PAGEOBJECT rect = FPDFPageObj_CreateNewRect(200, 40, 40, 20); + ASSERT_TRUE(FPDFPageObj_SetFillColor(rect, 255, 0, 0, 255)); + ASSERT_TRUE(FPDFPath_SetDrawMode(rect, FPDF_FILLMODE_ALTERNATE, false)); + FPDFPage_InsertObject(page.get(), rect); + ASSERT_TRUE(FPDFPage_GenerateContent(page.get())); + }; + const auto redact = [&] { ApplyRedactions(page.get()); }; + Change(add_first ? std::function(add) : redact); + ASSERT_FALSE(HasFatalFailure()); + Change(add_first ? std::function(redact) : add); + ASSERT_FALSE(HasFatalFailure()); + SaveAndReopen(); + ASSERT_FALSE(HasFatalFailure()); + { + ScopedSavedPage saved = LoadScopedSavedPage(kCutAsPrintToPDFDoes); + ASSERT_TRUE(saved); + ScopedFPDFBitmap after = RenderAtPageSize(saved.get()); + EXPECT_EQ(0xff0000u, PixelAt(after.get(), 220, 50)); + areas.push_back(square); + std::string where; + EXPECT_EQ(0, CountChangedPixelsOutside(before.get(), after.get(), areas, + &where)) + << where; + } + CloseSaved(); + } +} + +// Where a page's content is cut into streams makes no difference to a +// reader, and so none to a rewrite. Every page of the rewrite cases is +// written again - redacted, or unchanged where it shows no word to redact - +// with its content as it is, cut into three streams, and with every operator +// in a stream of its own: each cut copy draws what the file draws, before +// the rewrite and after it. +TEST_P(EPDFContentRewriteEmbedderTest, + WhereContentIsCutIntoStreamsChangesNothing) { + for (const char* name : + {"redact_rewrite_cases.pdf", "redact_special_colorspaces.pdf", + "edit_form_with_matrix.pdf", "redact_split_content_streams.pdf"}) { + const std::string path = PathService::GetTestFilePath(name); + std::vector uncut_before; + const std::vector uncut_after = + RewriteEveryPage(GetFileContents(path.c_str()), &uncut_before); + ASSERT_FALSE(HasFatalFailure()); + ASSERT_FALSE(uncut_after.empty()); + for (const Cut cut : {Cut::kIntoThree, Cut::kAfterEveryOperator}) { + std::vector cut_before; + const std::vector cut_after = + RewriteEveryPage(CutIntoStreams(name, cut), &cut_before); + ASSERT_FALSE(HasFatalFailure()); + ASSERT_EQ(uncut_after.size(), cut_after.size()); + for (size_t i = 0; i < cut_after.size(); ++i) { + SCOPED_TRACE(std::string(name) + " page " + std::to_string(i) + + (cut == Cut::kIntoThree ? ", cut into three streams" + : ", cut after every operator")); + std::string where; + EXPECT_EQ(0, CountChangedPixelsOutside(uncut_before[i].get(), + cut_before[i].get(), {}, &where)) + << "the cut copy draws another page: " << where; + EXPECT_EQ(0, CountChangedPixelsOutside(uncut_after[i].get(), + cut_after[i].get(), {}, &where)) + << "the rewrite of the cut copy draws another page: " << where; + } + } + } +} + +// The text left after a redaction starts a text object of its own, and each +// character of it keeps its origin. Renders can't show this: PDFium draws +// each glyph on a whole pixel, so text that drifts by less than half a pixel +// renders as before - and a glyph lying on a pixel edge may be drawn a pixel +// over when its origin moves by a millionth of a point, as float rounding +// moves it. +TEST_P(EPDFContentRewriteEmbedderTest, TextLeftAfterARedactionKeepsItsPlace) { + struct Case { + const char* name; + int index; + const wchar_t* word; + }; + for (const Case& test : + {Case{"redact_split_content_streams.pdf", kCutAsPrintToPDFDoes, L"tail"}, + Case{"redact_split_content_streams.pdf", + kCutInsideATransformWithoutAnInverse, L"SECRET"}, + Case{"redact_rewrite_cases.pdf", kTextScalingRiseAndSpacing, L"and"}}) { + SCOPED_TRACE(std::string(test.name) + " page " + + std::to_string(test.index)); + ASSERT_TRUE(Open(test.name)); + ScopedFPDFPage page(FPDF_LoadPage(doc_.get(), test.index)); + ASSERT_TRUE(page); + const std::vector areas = AreasOf(page.get(), test.word); + ASSERT_FALSE(areas.empty()); + const std::vector before = CharsOutside(page.get(), areas); + ASSERT_FALSE(before.empty()); + Mark(page.get(), areas); + ChangeAndSave([&] { ApplyRedactions(page.get()); }); + ASSERT_FALSE(HasFatalFailure()); + { + ScopedSavedPage saved = LoadScopedSavedPage(test.index); + ASSERT_TRUE(saved); + const std::vector after = CharsOutside(saved.get(), {}); + ASSERT_EQ(before.size(), after.size()); + for (size_t i = 0; i < before.size(); ++i) { + EXPECT_EQ(before[i].unicode, after[i].unicode) << i; + EXPECT_NEAR(before[i].x, after[i].x, 0.001) << i; + EXPECT_NEAR(before[i].y, after[i].y, 0.001) << i; + } + } + CloseSaved(); + } +} diff --git a/testing/resources/redact_split_content_streams.in b/testing/resources/redact_split_content_streams.in new file mode 100644 index 000000000..887c12fa4 --- /dev/null +++ b/testing/resources/redact_split_content_streams.in @@ -0,0 +1,113 @@ +{{header}} +{{object 1 0}} << /Type /Catalog /Pages 2 0 R >> +endobj +{{object 2 0}} << /Type /Pages /Count 2 /Kids [10 0 R 20 0 R] >> +endobj +{{object 3 0}} << /Type /Font /Subtype /Type1 /BaseFont /Courier >> +endobj +% Content cut into streams in the middle of q ... cm ... Q, so that a stream +% ends at another CTM than it began with (see +% EPDFContentRewriteEmbedderTest.KeepsContentCutIntoStreams). +% +% The text sits off whole pixels at a pixel per point. The text left after a +% redaction starts a text object of its own, placed to within float rounding +% of where it was, and PDFium draws a glyph that lies on a pixel edge on +% either side of it. +% +% Page 0: the shape "Microsoft: Print To PDF" writes. One cm at the top of +% the first stream flips and scales the whole page and is never undone; the +% content is then cut into streams wherever a size limit falls, here in the +% middle of a path inside q ... cm ... Q each time. Every stream after the +% first so begins and ends at a different CTM. +{{object 10 0}} << /Type /Page /Parent 2 0 R /MediaBox [0 0 300 200] + /Resources << /Font << /F1 3 0 R >> >> + /Contents [11 0 R 12 0 R 13 0 R 14 0 R] >> +endobj +{{object 11 0}} << {{streamlen}} >> +stream +.75 0 0 -.75 0 200 cm +BT /F1 16 Tf 1 0 0 -1 21 41 Tm (keep SECRET keep) Tj ET +q +1 0 0 1 40 80 cm +0 0 0 rg +0 0 m +60 0 l +endstream +endobj +{{object 12 0}} << {{streamlen}} >> +stream +60 30 l +0 30 l +h +f +Q +q +1 0 0 1 150 120 cm +0 .4 .8 rg +0 0 m +80 0 l +endstream +endobj +{{object 13 0}} << {{streamlen}} >> +stream +80 40 l +0 40 l +h +f +Q +q +1 0 0 1 280 60 cm +.8 .2 .2 rg +0 0 m +70 0 l +endstream +endobj +{{object 14 0}} << {{streamlen}} >> +stream +70 50 l +0 50 l +h +f +Q +BT /F1 14 Tf 1 0 0 -1 21 239 Tm (tail text STAYS) Tj ET +endstream +endobj +% Page 1: a stream cut inside q ... cm ... Q where the cm cannot be inverted +% (a strip of no height). Nothing after it can be drawn by undoing that CTM. +{{object 20 0}} << /Type /Page /Parent 2 0 R /MediaBox [0 0 300 200] + /Resources << /Font << /F1 3 0 R >> >> + /Contents [21 0 R 22 0 R 23 0 R] >> +endobj +{{object 21 0}} << {{streamlen}} >> +stream +BT /F1 16 Tf 20.25 150.25 Td (keep SECRET keep) Tj ET +q +120 0 0 0 40 100 cm +endstream +endobj +{{object 22 0}} << {{streamlen}} >> +stream +0 0 1 1 re +f +Q +q +1 0 0 1 60 40 cm +0 .5 0 rg +0 0 m +80 0 l +endstream +endobj +{{object 23 0}} << {{streamlen}} >> +stream +80 30 l +0 30 l +h +f +Q +BT /F1 14 Tf 20.25 20.25 Td (tail text STAYS) Tj ET +endstream +endobj +{{xref}} +{{trailer}} +{{startxref}} +%%EOF diff --git a/testing/resources/redact_split_content_streams.pdf b/testing/resources/redact_split_content_streams.pdf new file mode 100644 index 000000000..75377ce66 Binary files /dev/null and b/testing/resources/redact_split_content_streams.pdf differ