Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion docs/embedpdf/fork-ledger.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,15 @@ upstream accessor to the same state needs the same seam.
### view-scope
**Rule.** A base document resolves references through the current `CPDF_DocumentViewScope`: the
effective view (the base as a layer sees it) or the frozen base. Read entry points open a scope.
`EPDFPage_ApplyRedactions` holds the page's view while selecting marks and applying their regions.
**Why.** One frozen base serves many layers, and a reference must resolve to the version this
layer sees.
**Where.** `cpdf_document_view_scope.{h,cpp}`, `CPDF_BaseDocument`, read entry points in
`fpdfsdk/`. Every function in `epdf_annot.cpp` opens one before it walks the document's objects.
**Guarded by.** `cpdf_base_document_unittest.cpp`, `cpdf_layer_document_unittest.cpp`,
`EPDFAnnotEmbedderTest.ReadsResolveThroughTheLayer`,
`EPDFPageLabelsEmbedderTest.WritesInALayerTransaction` (`FPDF_GetPageLabel`).
`EPDFPageLabelsEmbedderTest.WritesInALayerTransaction` (`FPDF_GetPageLabel`),
`EPDFRedactEmbedderTest.MovedMarkRedactsItsNewPositionInTheSavedLayer`.
**When upstream touches this.** A new entry point that walks objects beyond its own page opens a
scope, whether it reads or writes: `FPDFPage_Flatten()` writes one page but reads every page's
`/Annots`. Without a scope, a base page with layer `/Annots` crashes the view detector.
Expand Down
3 changes: 2 additions & 1 deletion fpdfsdk/epdf_redact.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,8 @@ EPDFPage_ApplyRedactions(FPDF_PAGE page,
*out_removed_annot_count = 0;
}

CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
ScopedFPDFPageView page_view(page);
CPDF_Page* pPage = page_view.Get();
if (!pPage || count < 0 || (count > 0 && !marks) || (marks && count == 0)) {
return false;
}
Expand Down
46 changes: 46 additions & 0 deletions fpdfsdk/epdf_redact_embeddertest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1766,6 +1766,52 @@ TEST_F(EPDFRedactEmbedderTest, AnImageThatCannotBeDecodedFailsTheApply) {
EXPECT_FALSE(EPDFPage_ApplyRedactions(page.get(), nullptr, 0, nullptr));
}

TEST_F(EPDFRedactEmbedderTest, MovedMarkRedactsItsNewPositionInTheSavedLayer) {
const std::string input = PdfOf({
"<< /Type /Catalog /Pages 2 0 R >>",
"<< /Type /Pages /Kids [3 0 R] /Count 1 >>",
"<< /Type /Page /Parent 2 0 R /MediaBox [0 0 200 120] /Contents 4 0 R "
"/Resources << /Font << /F1 5 0 R >> >> /Annots [6 0 R] >>",
StreamObject("",
"BT /F1 20 Tf 10 80 Td (KEEP) Tj "
"0 -50 Td (REMOVE) Tj ET"),
"<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>",
"<< /Type /Annot /Subtype /Redact /Rect [5 75 150 105] >>",
});
for (bool apply_all : {false, true}) {
SCOPED_TRACE(apply_all ? "all marks" : "selected mark");
OpenedPdf opened = OpenPdf(input, /*layer=*/true);
ASSERT_TRUE(opened.doc);
ScopedFPDFPage page(FPDF_LoadPage(opened.doc.get(), 0));
ASSERT_TRUE(page);
ASSERT_EQ(L"KEEP\r\nREMOVE", ReadText(page.get()));
ScopedFPDFAnnotation mark(FPDFPage_GetAnnot(page.get(), 0));
ASSERT_TRUE(mark);

ASSERT_TRUE(EPDFLayer_BeginTransaction(opened.doc.get()));
const FS_RECTF moved{5, 55, 150, 25};
ASSERT_TRUE(EPDFAnnot_SetRect(mark.get(), &moved));
ASSERT_TRUE(EPDFLayer_CommitTransaction(opened.doc.get()));

ASSERT_TRUE(EPDFLayer_BeginTransaction(opened.doc.get()));
const FPDF_ANNOTATION selected[] = {mark.get()};
ASSERT_TRUE(EPDFPage_ApplyRedactions(page.get(),
apply_all ? nullptr : selected,
apply_all ? 0 : 1, nullptr));
ASSERT_TRUE(FPDFPage_GenerateContent(page.get()));
ASSERT_TRUE(EPDFLayer_CommitTransaction(opened.doc.get()));

ClearString();
ASSERT_TRUE(FPDF_SaveAsCopy(opened.doc.get(), this, FPDF_NO_INCREMENTAL));
ScopedSavedDoc saved_doc = OpenScopedSavedDocument();
ASSERT_TRUE(saved_doc);
ScopedSavedPage saved_page = LoadScopedSavedPage(0);
ASSERT_TRUE(saved_page);
EXPECT_EQ(L"KEEP", ReadText(saved_page.get()));
ExpectNoSentinel(CPDFDocumentFromFPDFDocument(saved_doc.get()), "REMOVE");
}
}

// Marks passed to EPDFPage_ApplyRedactions are applied, in any order; the
// page's other marks stay.
TEST_F(EPDFRedactEmbedderTest, ApplyingSomeMarksLeavesTheOthers) {
Expand Down
4 changes: 0 additions & 4 deletions testing/embedpdf_layer_mode_expectations.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,3 @@ EPDFLayerEmbedderTest.LayerDiagnosticsRejectPlainDocuments The test checks a pla
FPDFViewEmbedderTest.UnSupportedOperationsLoadCustomDocument EPDF_LoadBaseDocument doesn't call the unsupported-feature handler; the engine doesn't set one.
FPDFViewEmbedderTest.DocumentHasValidCrossReferenceTable FPDF_DocumentHasValidCrossReferenceTable reports false on a layer; the engine doesn't call it.
FPDFViewEmbedderTest.LoadDocumentWithEmptyXRefConsistently FPDF_DocumentHasValidCrossReferenceTable reports false on a layer; the engine doesn't call it.

# --- Fork bugs found by layer mode.
EPDFRedactEmbedderTest.FullyRemovedSpanLeavesNoPropertyOrStructureResidue BUG (rule view-scope): EPDFPage_ApplyRedactions reads references without a CPDF_DocumentViewScope (ApplyAllRedactionsCore), so it can read the base's old version of an object the layer changed.
EPDFAnnotEmbedderTest.ApplyRedactionFallsBackToRectForNonFiniteQuad BUG (rule view-scope): applying a redaction (EPDFPage_ApplyRedactions) reads references without a CPDF_DocumentViewScope.
2 changes: 2 additions & 0 deletions testing/embedpdf_layer_mode_writers.txt
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ EPDFAnnotEmbedderTest.ApplyRedactionCascadesPopupRemoval 5
EPDFAnnotEmbedderTest.ApplyRedactionClonesSharedFormBeforeVectorEdit 15
EPDFAnnotEmbedderTest.ApplyRedactionCountsIntersectingAnnotation 5
EPDFAnnotEmbedderTest.ApplyRedactionDoesNotRemoveTouchOnlyAnnotation 5
EPDFAnnotEmbedderTest.ApplyRedactionFallsBackToRectForNonFiniteQuad 9
EPDFAnnotEmbedderTest.ApplyRedactionHonorsOverlayTextAlignment 8
EPDFAnnotEmbedderTest.ApplyRedactionLabelWithoutDaFontUsesHelvetica 8
EPDFAnnotEmbedderTest.ApplyRedactionOnNeverParsedPageRemovesText 9
Expand Down Expand Up @@ -330,6 +331,7 @@ EPDFPpoEmbedderTest.RenamesAFontWithOtherWidths 20
EPDFPpoEmbedderTest.RenamesTheFontOfAnAppearanceWithAComment 20
EPDFPpoEmbedderTest.SharesAFontDefinedTheSameWay 19
EPDFRedactEmbedderTest.DirectReplacementTextIsRemovedFromSiblingObjects 3
EPDFRedactEmbedderTest.FullyRemovedSpanLeavesNoPropertyOrStructureResidue 8
EPDFRedactEmbedderTest.InheritedPropertiesLeaveNoUnusedOriginalOnPage 5
EPDFRedactEmbedderTest.NestedFormsUseTheRenderersTransformOrder 7
EPDFRedactEmbedderTest.RedactingAFlattenedStampLeavesAStampSharingItsDrawingAlone 22
Expand Down