Consistently call ProcessGraphics() in CPDF_PageContentGenerator
Call ProcessGraphics() for forms and images, not just paths and texts.
Add an EndProcessGraphics() to make it obvious when the
ProcessGraphics() caller is trying to balance out the "q" operator
ProcessGraphics() emitted.
There are no tests to allow for easy cherry-picking.
Bug: 406530484
Change-Id: I412116172d178a98e4149a93124da13cb339698e
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/130275
Reviewed-by: Thomas Sepez <tsepez@google.com>
Reviewed-by: Tom Sepez <tsepez@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp b/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp
index 0590b6a..1137403 100644
--- a/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp
+++ b/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp
@@ -70,6 +70,11 @@
return true;
}
+// Balances the "q" operator ProcessGraphics() emitted.
+void EndProcessGraphics(fxcrt::ostringstream& buf) {
+ buf << " Q\n";
+}
+
void RecordPageObjectResourceUsage(const CPDF_PageObject* page_object,
ResourcesMap& seen_resources) {
const ByteString& resource_name = page_object->GetResourceName();
@@ -571,7 +576,7 @@
if (!pStream)
return;
- *buf << "q ";
+ ProcessGraphics(buf, pImageObj);
if (!matrix.IsIdentity()) {
WriteMatrix(*buf, matrix) << " cm ";
@@ -589,7 +594,8 @@
pImageObj->SetImage(pPageData->GetImage(pStream->GetObjNum()));
}
- *buf << "/" << PDF_NameEncode(name) << " Do Q\n";
+ *buf << "/" << PDF_NameEncode(name) << " Do";
+ EndProcessGraphics(*buf);
}
void CPDF_PageContentGenerator::ProcessForm(fxcrt::ostringstream* buf,
@@ -606,13 +612,14 @@
ByteString name = RealizeResource(pStream.Get(), "XObject");
pFormObj->SetResourceName(name);
- *buf << "q\n";
+ ProcessGraphics(buf, pFormObj);
if (!matrix.IsIdentity()) {
WriteMatrix(*buf, matrix) << " cm ";
}
- *buf << "/" << PDF_NameEncode(name) << " Do Q\n";
+ *buf << "/" << PDF_NameEncode(name) << " Do";
+ EndProcessGraphics(*buf);
}
// Processing path construction with operators from Table 4.9 of PDF spec 1.7:
@@ -665,7 +672,6 @@
// Processing path painting with operators from Table 4.10 of PDF spec 1.7:
// Path painting operators: "S", "n", "B", "f", "B*", "f*", depending on
// the filling mode and whether we want stroking the path or not.
-// "Q" restores the graphics state imposed by the ProcessGraphics method.
void CPDF_PageContentGenerator::ProcessPath(fxcrt::ostringstream* buf,
CPDF_PathObject* pPathObj) {
ProcessGraphics(buf, pPathObj);
@@ -683,7 +689,7 @@
*buf << (pPathObj->stroke() ? " B" : " f");
else if (pPathObj->has_alternate_filltype())
*buf << (pPathObj->stroke() ? " B*" : " f*");
- *buf << " Q\n";
+ EndProcessGraphics(*buf);
}
// This method supports color operators rg and RGB from Table 4.24 of PDF spec
@@ -882,5 +888,5 @@
}
}
*buf << PDF_HexEncodeString(text.AsStringView()) << " Tj ET";
- *buf << " Q\n";
+ EndProcessGraphics(*buf);
}