From ae33e57aff13857a4dd73b4bf59d399ab6d51da7 Mon Sep 17 00:00:00 2001 From: Tom Sepez Date: Mon, 23 Nov 2026 15:33:43 -0800 Subject: [PATCH 1/6] Harden non-GC references held by XFA GarbageCollected objects Garbage-collected objects can outlive non-GC C++ objects if collection is deferred or shares a heap across documents, so raw/unowned pointers into C++ memory should self-null or be explicitly cleared. Using observed ptrs should be relatively inexpensive following recent updates to them. None of these are issues today, but for defensive purposes: - Make CFX_XMLNode inherit from Observable and convert CXFA_Node::xml_node_ to ObservedPtr. - Convert CXFA_FFDoc::pdfdoc_ to ObservedPtr. - Make CallbackIfaces become observable. Change-Id: I7a20c6b15faef18e202dce0cf077d650946c60f1 Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/165130 Reviewed-by: Andy Phan Commit-Queue: Tom Sepez --- core/fxcrt/xml/cfx_xmlnode.h | 3 ++- xfa/fgas/font/cfgas_pdffontmgr.cpp | 19 ++++++++++++------- xfa/fgas/font/cfgas_pdffontmgr.h | 10 +++++----- xfa/fxfa/cxfa_ffapp.h | 8 ++++---- xfa/fxfa/cxfa_ffdoc.cpp | 8 ++++---- xfa/fxfa/cxfa_ffdoc.h | 9 +++++---- xfa/fxfa/parser/cxfa_node.cpp | 22 +++++++++++++--------- xfa/fxfa/parser/cxfa_node.h | 8 ++++---- 8 files changed, 49 insertions(+), 38 deletions(-) diff --git a/core/fxcrt/xml/cfx_xmlnode.h b/core/fxcrt/xml/cfx_xmlnode.h index 5934bc7036..e3e4cb3989 100644 --- a/core/fxcrt/xml/cfx_xmlnode.h +++ b/core/fxcrt/xml/cfx_xmlnode.h @@ -8,12 +8,13 @@ #define CORE_FXCRT_XML_CFX_XMLNODE_H_ #include "core/fxcrt/fx_stream.h" +#include "core/fxcrt/observed_ptr.h" #include "core/fxcrt/retain_ptr.h" #include "core/fxcrt/tree_node.h" class CFX_XMLDocument; -class CFX_XMLNode : public TreeNode { +class CFX_XMLNode : public Observable, public TreeNode { public: enum class Type { kInstruction = 0, diff --git a/xfa/fgas/font/cfgas_pdffontmgr.cpp b/xfa/fgas/font/cfgas_pdffontmgr.cpp index 180837620f..1930ef10a0 100644 --- a/xfa/fgas/font/cfgas_pdffontmgr.cpp +++ b/xfa/fgas/font/cfgas_pdffontmgr.cpp @@ -133,18 +133,18 @@ bool PsNameMatchDRFontName(ByteStringView bsPsName, } // namespace -CFGAS_PDFFontMgr::CFGAS_PDFFontMgr(const CPDF_Document* doc) : doc_(doc) { - DCHECK(doc); -} +CFGAS_PDFFontMgr::CFGAS_PDFFontMgr() = default; CFGAS_PDFFontMgr::~CFGAS_PDFFontMgr() = default; -RetainPtr CFGAS_PDFFontMgr::FindFont(const ByteString& strPsName, +RetainPtr CFGAS_PDFFontMgr::FindFont(const CPDF_Document* doc, + const ByteString& strPsName, bool bBold, bool bItalic, bool bStrictMatch) { + DCHECK(doc); RetainPtr font_set_dict = - doc_->GetRoot()->GetDictFor(pdfium::catalog::kAcroForm)->GetDictFor("DR"); + doc->GetRoot()->GetDictFor(pdfium::catalog::kAcroForm)->GetDictFor("DR"); if (!font_set_dict) { return nullptr; } @@ -157,7 +157,7 @@ RetainPtr CFGAS_PDFFontMgr::FindFont(const ByteString& strPsName, ByteString name = strPsName; name.Remove(' '); - auto* pData = CPDF_DocPageData::FromDocument(doc_); + auto* pData = CPDF_DocPageData::FromDocument(doc); CPDF_DictionaryLocker locker(font_set_dict); for (const auto& it : locker) { const ByteString& key = it.first; @@ -183,9 +183,14 @@ RetainPtr CFGAS_PDFFontMgr::FindFont(const ByteString& strPsName, } RetainPtr CFGAS_PDFFontMgr::GetFont( + const CPDF_Document* doc, const WideString& wsFontFamily, uint32_t dwFontStyles, bool bStrictMatch) { + if (!doc) { + return nullptr; + } + auto key = std::make_pair(wsFontFamily, dwFontStyles); auto it = font_map_.find(key); if (it != font_map_.end()) { @@ -197,7 +202,7 @@ RetainPtr CFGAS_PDFFontMgr::GetFont( bool bItalic = FontStyleIsItalic(dwFontStyles); ByteString strFontName = PsNameToFontName(bsPsName, bBold, bItalic); RetainPtr font = - FindFont(strFontName, bBold, bItalic, bStrictMatch); + FindFont(doc, strFontName, bBold, bItalic, bStrictMatch); if (!font) { return nullptr; } diff --git a/xfa/fgas/font/cfgas_pdffontmgr.h b/xfa/fgas/font/cfgas_pdffontmgr.h index fac6829753..54ffdcc7fa 100644 --- a/xfa/fgas/font/cfgas_pdffontmgr.h +++ b/xfa/fgas/font/cfgas_pdffontmgr.h @@ -12,7 +12,6 @@ #include "core/fxcrt/fx_string.h" #include "core/fxcrt/retain_ptr.h" -#include "core/fxcrt/unowned_ptr.h" #include "xfa/fgas/font/cfgas_fontmgr.h" class CFGAS_GEFont; @@ -20,20 +19,21 @@ class CPDF_Document; class CFGAS_PDFFontMgr final { public: - explicit CFGAS_PDFFontMgr(const CPDF_Document* doc); + CFGAS_PDFFontMgr(); ~CFGAS_PDFFontMgr(); - RetainPtr GetFont(const WideString& wsFontFamily, + RetainPtr GetFont(const CPDF_Document* doc, + const WideString& wsFontFamily, uint32_t dwFontStyles, bool bStrictMatch); private: - RetainPtr FindFont(const ByteString& strFamilyName, + RetainPtr FindFont(const CPDF_Document* doc, + const ByteString& strFamilyName, bool bBold, bool bItalic, bool bStrictMatch); - UnownedPtr const doc_; std::map, RetainPtr> font_map_; }; diff --git a/xfa/fxfa/cxfa_ffapp.h b/xfa/fxfa/cxfa_ffapp.h index bc0245af01..243d6cab0e 100644 --- a/xfa/fxfa/cxfa_ffapp.h +++ b/xfa/fxfa/cxfa_ffapp.h @@ -7,7 +7,7 @@ #ifndef XFA_FXFA_CXFA_FFAPP_H_ #define XFA_FXFA_CXFA_FFAPP_H_ -#include "core/fxcrt/unowned_ptr.h" +#include "core/fxcrt/observed_ptr.h" #include "core/fxcrt/widestring.h" #include "fxjs/gc/heap.h" #include "v8/include/cppgc/garbage-collected.h" @@ -26,7 +26,7 @@ class CXFA_FWLTheme; class CXFA_FFApp final : public cppgc::GarbageCollected, public CFWL_App::AdapterIface { public: - class CallbackIface { + class CallbackIface : public Observable { public: virtual ~CallbackIface() = default; @@ -143,14 +143,14 @@ class CXFA_FFApp final : public cppgc::GarbageCollected, bool LoadFWLTheme(CXFA_FFDoc* doc); CFWL_WidgetMgr* GetFWLWidgetMgr() const { return fwlapp_->GetWidgetMgr(); } - CallbackIface* GetAppProvider() const { return provider_; } + CallbackIface* GetAppProvider() const { return provider_.Get(); } CFWL_App* GetFWLApp() const { return fwlapp_; } CXFA_FontMgr* GetXFAFontMgr() const { return xfafont_mgr_; } private: explicit CXFA_FFApp(CallbackIface* pProvider); - UnownedPtr const provider_; + ObservedPtr provider_; cppgc::Member xfafont_mgr_; cppgc::Member adapter_widget_mgr_; cppgc::Member fwltheme_; diff --git a/xfa/fxfa/cxfa_ffdoc.cpp b/xfa/fxfa/cxfa_ffdoc.cpp index 170bc6330b..d0909519e9 100644 --- a/xfa/fxfa/cxfa_ffdoc.cpp +++ b/xfa/fxfa/cxfa_ffdoc.cpp @@ -240,7 +240,7 @@ bool CXFA_FFDoc::OpenDoc(CFX_XMLDocument* pXML) { // At this point we've got an XFA document and we want to always return // true to signify the load succeeded. - pdffont_mgr_ = std::make_unique(GetPDFDoc()); + pdffont_mgr_ = std::make_unique(); form_type_ = FormType::kXFAForeground; CXFA_Node* pConfig = ToNode(document_->GetXFAObject(XFA_HASHCODE_Config)); if (!pConfig) { @@ -277,11 +277,11 @@ bool CXFA_FFDoc::OpenDoc(CFX_XMLDocument* pXML) { RetainPtr CXFA_FFDoc::GetPDFFont(const WideString& family, uint32_t styles, bool strict) { - if (!pdffont_mgr_) { + if (!pdffont_mgr_ || !pdfdoc_) { return nullptr; } - return pdffont_mgr_->GetFont(family, styles, strict); + return pdffont_mgr_->GetFont(pdfdoc_.Get(), family, styles, strict); } RetainPtr CXFA_FFDoc::GetPDFNamedImage(WideStringView wsName, @@ -295,7 +295,7 @@ RetainPtr CXFA_FFDoc::GetPDFNamedImage(WideStringView wsName, return it->second.pDibSource.As(); } - auto name_tree = CPDF_NameTree::Create(pdfdoc_, "XFAImages"); + auto name_tree = CPDF_NameTree::Create(pdfdoc_.Get(), "XFAImages"); size_t count = name_tree ? name_tree->GetCount() : 0; if (count == 0) { return nullptr; diff --git a/xfa/fxfa/cxfa_ffdoc.h b/xfa/fxfa/cxfa_ffdoc.h index e500083ed5..885cb41d43 100644 --- a/xfa/fxfa/cxfa_ffdoc.h +++ b/xfa/fxfa/cxfa_ffdoc.h @@ -13,6 +13,7 @@ #include "core/fxcrt/fx_coordinates.h" #include "core/fxcrt/fx_stream.h" #include "core/fxcrt/mask.h" +#include "core/fxcrt/observed_ptr.h" #include "core/fxcrt/retain_ptr.h" #include "core/fxcrt/unowned_ptr.h" #include "core/fxge/dib/fx_dib.h" @@ -62,7 +63,7 @@ class CXFA_FFDoc : public cppgc::GarbageCollected { kStopLayout = 4, }; - class CallbackIface { + class CallbackIface : public Observable { public: virtual ~CallbackIface() = default; @@ -162,7 +163,7 @@ class CXFA_FFDoc : public cppgc::GarbageCollected { FXGC_Heap* GetHeap() const { return heap_; } CXFA_Document* GetXFADoc() const { return document_; } CXFA_FFApp* GetApp() const { return app_; } - CPDF_Document* GetPDFDoc() const { return pdfdoc_; } + CPDF_Document* GetPDFDoc() const { return pdfdoc_.Get(); } CXFA_FFDocView* GetDocView(CXFA_LayoutProcessor* pLayout); CXFA_FFDocView* GetDocView(); RetainPtr GetPDFFont(const WideString& family, @@ -182,8 +183,8 @@ class CXFA_FFDoc : public cppgc::GarbageCollected { FXGC_Heap* pHeap); bool BuildDoc(CFX_XMLDocument* pXML); - UnownedPtr const doc_environment_; - UnownedPtr const pdfdoc_; + ObservedPtr doc_environment_; + ObservedPtr pdfdoc_; UnownedPtr const heap_; cppgc::Member const app_; cppgc::Member notify_; diff --git a/xfa/fxfa/parser/cxfa_node.cpp b/xfa/fxfa/parser/cxfa_node.cpp index 6431bde9a0..217ea05ae9 100644 --- a/xfa/fxfa/parser/cxfa_node.cpp +++ b/xfa/fxfa/parser/cxfa_node.cpp @@ -1706,8 +1706,8 @@ void CXFA_Node::InsertChildAndNotify(CXFA_Node* pNode, CXFA_Node* pBeforeNode) { } DCHECK(!pNode->xml_node_->GetParent()); - xml_node_->InsertBefore(pNode->xml_node_, - pBeforeNode ? pBeforeNode->xml_node_ : nullptr); + xml_node_->InsertBefore(pNode->xml_node_.Get(), + pBeforeNode ? pBeforeNode->xml_node_.Get() : nullptr); } void CXFA_Node::RemoveChildAndNotify(CXFA_Node* pNode, bool bNotify) { @@ -1725,12 +1725,12 @@ void CXFA_Node::RemoveChildAndNotify(CXFA_Node* pNode, bool bNotify) { } if (!pNode->IsAttributeInXML()) { - xml_node_->RemoveChild(pNode->xml_node_); + xml_node_->RemoveChild(pNode->xml_node_.Get()); return; } - DCHECK_EQ(pNode->xml_node_, xml_node_); - CFX_XMLElement* pXMLElement = ToXMLElement(pNode->xml_node_); + DCHECK_EQ(pNode->xml_node_.Get(), xml_node_.Get()); + CFX_XMLElement* pXMLElement = ToXMLElement(pNode->xml_node_.Get()); if (pXMLElement) { WideString wsAttributeName = pNode->JSObject()->GetCData(XFA_Attribute::QualifiedName); @@ -1747,7 +1747,7 @@ void CXFA_Node::RemoveChildAndNotify(CXFA_Node* pNode, bool bNotify) { auto* text = GetXMLDocument()->CreateNode(wsValue); pNewXMLElement->AppendLastChild(text); } - pNode->xml_node_ = pNewXMLElement; + pNode->xml_node_.Reset(pNewXMLElement); pNode->JSObject()->SetEnum(XFA_Attribute::Contains, XFA_AttributeValue::Unknown, false); } @@ -1946,12 +1946,16 @@ void CXFA_Node::UpdateNameHash() { name_hash_ = FX_HashCode_GetW(wsName.AsStringView()); } +void CXFA_Node::SetXMLMappingNode(CFX_XMLNode* node) { + xml_node_.Reset(node); +} + CFX_XMLNode* CXFA_Node::CreateXMLMappingNode() { if (!xml_node_) { - xml_node_ = GetXMLDocument()->CreateNode( - JSObject()->GetCData(XFA_Attribute::Name)); + xml_node_.Reset(GetXMLDocument()->CreateNode( + JSObject()->GetCData(XFA_Attribute::Name))); } - return xml_node_; + return xml_node_.Get(); } bool CXFA_Node::IsNeedSavingXMLNode() const { diff --git a/xfa/fxfa/parser/cxfa_node.h b/xfa/fxfa/parser/cxfa_node.h index cfaea5893f..f916492018 100644 --- a/xfa/fxfa/parser/cxfa_node.h +++ b/xfa/fxfa/parser/cxfa_node.h @@ -16,10 +16,10 @@ #include "core/fxcrt/fx_coordinates.h" #include "core/fxcrt/mask.h" +#include "core/fxcrt/observed_ptr.h" #include "core/fxcrt/raw_span.h" #include "core/fxcrt/retain_ptr.h" #include "core/fxcrt/span.h" -#include "core/fxcrt/unowned_ptr.h" #include "core/fxcrt/unowned_ptr_exclusion.h" #include "core/fxcrt/widestring.h" #include "core/fxge/dib/fx_dib.h" @@ -182,8 +182,8 @@ class CXFA_Node : public CXFA_Object, public GCedTreeNodeMixin { return packet_ == XFA_PacketType::Form && IsContainerNode(); } - void SetXMLMappingNode(CFX_XMLNode* node) { xml_node_ = node; } - CFX_XMLNode* GetXMLMappingNode() const { return xml_node_; } + void SetXMLMappingNode(CFX_XMLNode* node); + CFX_XMLNode* GetXMLMappingNode() const { return xml_node_.Get(); } CFX_XMLNode* CreateXMLMappingNode(); bool IsNeedSavingXMLNode() const; @@ -533,7 +533,7 @@ class CXFA_Node : public CXFA_Object, public GCedTreeNodeMixin { const pdfium::raw_span properties_; const pdfium::raw_span attributes_; const Mask valid_packets_; - UnownedPtr xml_node_; + ObservedPtr xml_node_; const XFA_PacketType packet_; uint8_t execute_recursion_depth_ = 0; Mask node_flags_ = XFA_NodeFlag::kNone; From 8952e43dcf5458e21189a1acfa5f5aa600fd8f91 Mon Sep 17 00:00:00 2001 From: Lei Zhang Date: Tue, 24 Nov 2026 10:18:37 -0800 Subject: [PATCH 2/6] Add CPDFPageContentGeneratorTest cases for unbalanced graphics states Add unit tests covering CPDF_PageContentGenerator::GenerateContent() when existing page content streams do not start with a "q" operator, contain unmatched "q" operators (with and without CTM changes), or lose their leading stream during removal while a new object is added. Document current behavior with TODO comments to be updated once graphics state balancing is implemented in a follow-up CL. This CL is AI generated. Bug: 578639452 Change-Id: I8a469cdd63fc140df4e119bac4bc10ad9fb01ead Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/164912 Commit-Queue: Andy Phan Auto-Submit: Lei Zhang Reviewed-by: Andy Phan --- .../cpdf_pagecontentgenerator_unittest.cpp | 235 ++++++++++++++++++ 1 file changed, 235 insertions(+) diff --git a/core/fpdfapi/edit/cpdf_pagecontentgenerator_unittest.cpp b/core/fpdfapi/edit/cpdf_pagecontentgenerator_unittest.cpp index 031d7edecb..205c7240e9 100644 --- a/core/fpdfapi/edit/cpdf_pagecontentgenerator_unittest.cpp +++ b/core/fpdfapi/edit/cpdf_pagecontentgenerator_unittest.cpp @@ -17,17 +17,29 @@ #include "core/fpdfapi/page/cpdf_textobject.h" #include "core/fpdfapi/page/cpdf_textstate.h" #include "core/fpdfapi/page/test_with_page_module.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_name.h" #include "core/fpdfapi/parser/cpdf_parser.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/fpdfapi/parser/cpdf_test_document.h" #include "core/fxcrt/data_vector.h" #include "core/fxge/cfx_fillrenderoptions.h" #include "testing/gtest/include/gtest/gtest.h" +namespace { + +ByteString GetStreamData(RetainPtr stream) { + auto acc = pdfium::MakeRetain(std::move(stream)); + acc->LoadAllDataFiltered(); + return ByteString(ByteStringView(acc->GetSpan())); +} + +} // namespace + class CPDFPageContentGeneratorTest : public TestWithPageModule { protected: void TestProcessPath(CPDF_PageContentGenerator* pGen, @@ -516,3 +528,226 @@ TEST_F(CPDFPageContentGeneratorTest, ProcessContentMarksWithProperties) { EXPECT_TRUE(content.Contains("/M1 /Property#20Name#20With#20Space BDC")); } + +TEST_F(CPDFPageContentGeneratorTest, + GenerateContentWrapsStreamWithoutLeadingSave) { + auto doc = std::make_unique(); + doc->CreateNewDoc(); + + RetainPtr page_dict(doc->CreateNewPage(0)); + static constexpr ByteStringView kOriginalContents = + "-100 Tz q 10 5 3 25 re f Q\n"; + auto original_stream = + doc->NewIndirect(kOriginalContents.unsigned_span()); + page_dict->SetNewFor("Contents", doc.get(), + original_stream->GetObjNum()); + + auto test_page = pdfium::MakeRetain(doc.get(), page_dict); + test_page->ParseContent(); + ASSERT_EQ(1u, test_page->GetPageObjectCount()); + EXPECT_EQ(0, test_page->GetPageObjectByIndex(0)->GetContentStream()); + + auto path_obj = std::make_unique(); + path_obj->set_filltype(CFX_FillRenderOptions::FillType::kWinding); + path_obj->path().AppendRect(0, 0, 10, 10); + path_obj->SetDirty(true); + test_page->AppendPageObject(std::move(path_obj)); + + CPDF_PageContentGenerator generator(test_page.Get()); + generator.GenerateContent(); + + RetainPtr contents_array = + page_dict->GetArrayFor("Contents"); + ASSERT_TRUE(contents_array); + // TODO(crbug.com/578639452): GenerateContent() should wrap + // `kOriginalContents` in "q\n" and "Q\n" streams so bottom-most graphics + // state changes do not leak into the newly appended stream. + ASSERT_EQ(2u, contents_array->size()); + + EXPECT_EQ(kOriginalContents, GetStreamData(contents_array->GetStreamAt(0))); + EXPECT_EQ( + "q\n0 0 0 RG 0 0 0 rg 1 w 0 J 0 j\n/FXE1 gs q 0 0 10 10 re f Q\nQ\n", + GetStreamData(contents_array->GetStreamAt(1))); + + ASSERT_EQ(2u, test_page->GetPageObjectCount()); + EXPECT_EQ(0, test_page->GetPageObjectByIndex(0)->GetContentStream()); + EXPECT_EQ(1, test_page->GetPageObjectByIndex(1)->GetContentStream()); +} + +TEST_F(CPDFPageContentGeneratorTest, GenerateContentBalancesUnmatchedSaves) { + auto doc = std::make_unique(); + doc->CreateNewDoc(); + + RetainPtr page_dict(doc->CreateNewPage(0)); + static constexpr ByteStringView kOriginalContents = + "q -100 Tz q 10 5 3 25 re f Q\n"; + auto original_stream = + doc->NewIndirect(kOriginalContents.unsigned_span()); + page_dict->SetNewFor("Contents", doc.get(), + original_stream->GetObjNum()); + + auto test_page = pdfium::MakeRetain(doc.get(), page_dict); + test_page->ParseContent(); + ASSERT_EQ(1u, test_page->GetPageObjectCount()); + EXPECT_EQ(0, test_page->GetPageObjectByIndex(0)->GetContentStream()); + + auto path_obj = std::make_unique(); + path_obj->set_filltype(CFX_FillRenderOptions::FillType::kWinding); + path_obj->path().AppendRect(0, 0, 10, 10); + path_obj->SetDirty(true); + test_page->AppendPageObject(std::move(path_obj)); + + CPDF_PageContentGenerator generator(test_page.Get()); + generator.GenerateContent(); + + RetainPtr contents_array = + page_dict->GetArrayFor("Contents"); + ASSERT_TRUE(contents_array); + // TODO(crbug.com/578639452): GenerateContent() should append a "Q\n" stream + // before the new stream to close the unmatched "q" operator in + // `kOriginalContents`. + ASSERT_EQ(2u, contents_array->size()); + + EXPECT_EQ(kOriginalContents, GetStreamData(contents_array->GetStreamAt(0))); + EXPECT_EQ( + "q\n0 0 0 RG 0 0 0 rg 1 w 0 J 0 j\n/FXE1 gs q 0 0 10 10 re f Q\nQ\n", + GetStreamData(contents_array->GetStreamAt(1))); + + ASSERT_EQ(2u, test_page->GetPageObjectCount()); + EXPECT_EQ(0, test_page->GetPageObjectByIndex(0)->GetContentStream()); + EXPECT_EQ(1, test_page->GetPageObjectByIndex(1)->GetContentStream()); +} + +TEST_F(CPDFPageContentGeneratorTest, + GenerateContentBalancesUnmatchedSavesWithCTM) { + auto doc = std::make_unique(); + doc->CreateNewDoc(); + + RetainPtr page_dict(doc->CreateNewPage(0)); + static constexpr ByteStringView kStream0 = + "q 1 0 0 1 50 50 cm 10 5 3 25 re f\n"; + static constexpr ByteStringView kStream1 = "30 30 10 10 re f\n"; + auto stream0 = doc->NewIndirect(kStream0.unsigned_span()); + auto stream1 = doc->NewIndirect(kStream1.unsigned_span()); + auto contents = doc->NewIndirect(); + contents->AppendNew(doc.get(), stream0->GetObjNum()); + contents->AppendNew(doc.get(), stream1->GetObjNum()); + page_dict->SetNewFor("Contents", doc.get(), + contents->GetObjNum()); + + auto test_page = pdfium::MakeRetain(doc.get(), page_dict); + test_page->ParseContent(); + ASSERT_EQ(2u, test_page->GetPageObjectCount()); + EXPECT_EQ(0, test_page->GetPageObjectByIndex(0)->GetContentStream()); + EXPECT_EQ(1, test_page->GetPageObjectByIndex(1)->GetContentStream()); + + auto path_obj = std::make_unique(); + path_obj->set_filltype(CFX_FillRenderOptions::FillType::kWinding); + path_obj->path().AppendRect(0, 0, 10, 10); + path_obj->SetDirty(true); + test_page->AppendPageObject(std::move(path_obj)); + + { + CPDF_PageContentGenerator generator(test_page.Get()); + generator.GenerateContent(); + } + + RetainPtr contents_array = + page_dict->GetArrayFor("Contents"); + ASSERT_TRUE(contents_array); + // TODO(crbug.com/578639452): GenerateContent() should append a "Q\n" stream + // at index 2 to close the unmatched "q" operator and restore the CTM to + // identity before appending the new stream. + ASSERT_EQ(3u, contents_array->size()); + + EXPECT_EQ(kStream0, GetStreamData(contents_array->GetStreamAt(0))); + EXPECT_EQ(kStream1, GetStreamData(contents_array->GetStreamAt(1))); + EXPECT_EQ( + "q\n1 0 0 1 -50 -50 cm\n0 0 0 RG 0 0 0 rg 1 w 0 J 0 j\n/FXE1 gs " + "q 0 0 10 10 re f Q\nQ\n", + GetStreamData(contents_array->GetStreamAt(2))); + + // Stream 1 has no CTM operators of its own, so its end CTM must still match + // stream 0's CTM even after a "Q\n" stream is appended at index 2. + const CFX_Matrix expected_ctm(1, 0, 0, 1, 50, 50); + EXPECT_EQ(expected_ctm, test_page->GetCTMAtEndOfStream(0)); + EXPECT_EQ(expected_ctm, test_page->GetCTMAtEndOfStream(1)); + // TODO(crbug.com/578639452): Once a "Q\n" stream is appended at index 2, its + // end CTM should be the identity matrix instead of `expected_ctm`. + EXPECT_EQ(expected_ctm, test_page->GetCTMAtEndOfStream(2)); + + // Regenerating stream 1 must invert `expected_ctm` inside its own `q ... Q`, + // without emitting a trailing `cm` operator after `Q`. + test_page->GetPageObjectByIndex(1)->SetDirty(true); + { + CPDF_PageContentGenerator generator(test_page.Get()); + generator.GenerateContent(); + } + + // TODO(crbug.com/578639452): Should be 4 streams with "Q\n" at index 2 and + // the new object's stream (without inverse `cm`) at index 3. + ASSERT_EQ(3u, contents_array->size()); + EXPECT_EQ(kStream0, GetStreamData(contents_array->GetStreamAt(0))); + EXPECT_EQ( + "q\n1 0 0 1 -50 -50 cm\n0 0 0 RG 0 0 0 rg 1 w 0 J 0 j\n/FXE1 gs " + "q 0 0 0 rg 0 0 0 RG 1 0 0 1 50 50 cm 30 30 10 10 re f Q\nQ\n", + GetStreamData(contents_array->GetStreamAt(1))); + EXPECT_EQ( + "q\n1 0 0 1 -50 -50 cm\n0 0 0 RG 0 0 0 rg 1 w 0 J 0 j\n/FXE1 gs " + "q 0 0 10 10 re f Q\nQ\n", + GetStreamData(contents_array->GetStreamAt(2))); +} + +TEST_F(CPDFPageContentGeneratorTest, + GenerateContentRemovesStreamAndAddsNewObject) { + auto doc = std::make_unique(); + doc->CreateNewDoc(); + + RetainPtr page_dict(doc->CreateNewPage(0)); + static constexpr ByteStringView kStream0 = "q 10 5 3 25 re f Q\n"; + static constexpr ByteStringView kStream1 = "-100 Tz 30 30 10 10 re f\n"; + auto stream0 = doc->NewIndirect(kStream0.unsigned_span()); + auto stream1 = doc->NewIndirect(kStream1.unsigned_span()); + auto contents = doc->NewIndirect(); + contents->AppendNew(doc.get(), stream0->GetObjNum()); + contents->AppendNew(doc.get(), stream1->GetObjNum()); + page_dict->SetNewFor("Contents", doc.get(), + contents->GetObjNum()); + + auto test_page = pdfium::MakeRetain(doc.get(), page_dict); + test_page->ParseContent(); + ASSERT_EQ(2u, test_page->GetPageObjectCount()); + EXPECT_EQ(0, test_page->GetPageObjectByIndex(0)->GetContentStream()); + EXPECT_EQ(1, test_page->GetPageObjectByIndex(1)->GetContentStream()); + + // Remove the only object from stream 0 so stream 0 will be deleted, and add + // a new streamless object in the same GenerateContent() call. + ASSERT_TRUE(test_page->RemovePageObject(test_page->GetPageObjectByIndex(0))); + + auto path_obj = std::make_unique(); + path_obj->set_filltype(CFX_FillRenderOptions::FillType::kWinding); + path_obj->path().AppendRect(0, 0, 10, 10); + path_obj->SetDirty(true); + test_page->AppendPageObject(std::move(path_obj)); + + CPDF_PageContentGenerator generator(test_page.Get()); + generator.GenerateContent(); + + RetainPtr contents_array = + page_dict->GetArrayFor("Contents"); + ASSERT_TRUE(contents_array); + // TODO(crbug.com/578639452): Deleting stream 0 makes `kStream1` (which lacks + // a leading "q") the first stream. GenerateContent() should wrap `kStream1` + // in "q\n" and "Q\n" streams before appending the newly added object's + // stream. + ASSERT_EQ(2u, contents_array->size()); + + EXPECT_EQ(kStream1, GetStreamData(contents_array->GetStreamAt(0))); + EXPECT_EQ( + "q\n0 0 0 RG 0 0 0 rg 1 w 0 J 0 j\n/FXE1 gs q 0 0 10 10 re f Q\nQ\n", + GetStreamData(contents_array->GetStreamAt(1))); + + ASSERT_EQ(2u, test_page->GetPageObjectCount()); + EXPECT_EQ(0, test_page->GetPageObjectByIndex(0)->GetContentStream()); + EXPECT_EQ(1, test_page->GetPageObjectByIndex(1)->GetContentStream()); +} From 25ff9814ceca2ecbfac94d21b3cf29e4b524c7d0 Mon Sep 17 00:00:00 2001 From: Tom Sepez Date: Tue, 24 Nov 2026 13:03:34 -0800 Subject: [PATCH 3/6] Make CFX_GEModule renderer/font-backend handling uniform Align CFX_GEModule::RendererType initialization and queries with the FontBackend pattern and reduce #ifdef clutter: - Alias CFX_GEModule::FontBackend to CFX_FontMgr::FontBackend so callers (fpdf_view.cpp, pdf_test_environment.cpp) depend only on CFX_GEModule instead of reaching into CFX_FontMgr. - Remove RendererType::kDefault and handle AGG+Skia, Skia-only, and AGG-only builds explicitly with #error guards in fpdf_view.cpp and testing/pdf_test_environment.cpp, avoiding static_cast from FPDF_RENDERERTYPE_* values. - Provide IsAggRenderer(), IsSkiaRenderer(), IsFontations(), and IsFreetype() across all valid build configurations, returning compile-time constants in single-backend builds. - Store renderer_type_ and font_backend_ unconditionally on CFX_GEModule with [[maybe_unused]], and drop the redundant font_backend_ member from CFX_FontMgr. Treating both renderer and font-backend selection symmetrically makes single- vs. dual-backend #ifdefs explicit, prevents invalid build configurations at compile time, and keeps public-to-internal enum mapping type-safe. Change-Id: I1ac728cf1524f9396cbef32755c9e69165b6c6a2 Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/165370 Commit-Queue: Tom Sepez Reviewed-by: Andy Phan --- core/fxge/cfx_fontmgr.cpp | 8 ++++--- core/fxge/cfx_fontmgr.h | 6 ++--- core/fxge/cfx_gemodule.cpp | 9 +++---- core/fxge/cfx_gemodule.h | 39 ++++++++++++++++++------------- fpdfsdk/fpdf_view.cpp | 37 ++++++++++++++++++----------- testing/pdf_test_environment.cpp | 40 +++++++++++++++++++++++++------- 6 files changed, 90 insertions(+), 49 deletions(-) diff --git a/core/fxge/cfx_fontmgr.cpp b/core/fxge/cfx_fontmgr.cpp index 1a9b2277c6..8d9a5ca942 100644 --- a/core/fxge/cfx_fontmgr.cpp +++ b/core/fxge/cfx_fontmgr.cpp @@ -11,6 +11,7 @@ #include "core/fxge/cfx_face.h" #include "core/fxge/cfx_font.h" #include "core/fxge/cfx_fontmapper.h" +#include "core/fxge/cfx_gemodule.h" #include "core/fxge/cfx_glyphcache.h" #include "core/fxge/fx_font.h" #include "core/fxge/systemfontinfo_iface.h" @@ -73,8 +74,7 @@ CFX_FontMgr::CFX_FontMgr([[maybe_unused]] FontBackend backend) ft_library_(InitializeFreeType()), #endif #if defined(PDF_USE_SKIA) - font_backend_(backend), - skia_fontmgr_(CreateSkiaFontManager(font_backend_)), + skia_fontmgr_(CreateSkiaFontManager(backend)), #endif builtin_mapper_(std::make_unique()) #if defined(PDF_ENABLE_FREETYPE) @@ -111,7 +111,9 @@ sk_sp CFX_FontMgr::MakeSkTypeface( // If DirectWrite or CoreText didn't work, try a fallback font manager. if (!result) { if (!skia_fontmgr_fallback_) { - skia_fontmgr_fallback_ = CreateSkiaFontManagerFallback(font_backend_); + skia_fontmgr_fallback_ = CreateSkiaFontManagerFallback( + CFX_GEModule::IsFontations() ? FontBackend::kFontations + : FontBackend::kFreeType); } result = skia_fontmgr_fallback_->makeFromStream( std::make_unique(font_span.data(), font_span.size())); diff --git a/core/fxge/cfx_fontmgr.h b/core/fxge/cfx_fontmgr.h index 1b755f487f..9a4f4e13b4 100644 --- a/core/fxge/cfx_fontmgr.h +++ b/core/fxge/cfx_fontmgr.h @@ -37,7 +37,9 @@ class SkTypeface; class CFX_FontMgr { public: - enum class FontBackend { kFreeType, kFontations }; + // This internal definition of font backends must stay updated with respect + // to the public definition of `FPDF_FONT_BACKEND_TYPE` in `fpdfview.h`. + enum class FontBackend { kFreeType = 0, kFontations = 1 }; explicit CFX_FontMgr(FontBackend backend); ~CFX_FontMgr(); @@ -49,7 +51,6 @@ class CFX_FontMgr { #if defined(PDF_ENABLE_FREETYPE) FXFT_LibraryRec* GetFTLibrary() const { return ft_library_.get(); } - bool FTLibrarySupportsHinting() const { return ft_library_supports_hinting_; } #endif @@ -63,7 +64,6 @@ class CFX_FontMgr { ScopedFXFTLibraryRec const ft_library_; #endif #if defined(PDF_USE_SKIA) - const FontBackend font_backend_; sk_sp skia_fontmgr_; sk_sp skia_fontmgr_fallback_; #endif diff --git a/core/fxge/cfx_gemodule.cpp b/core/fxge/cfx_gemodule.cpp index 701c4b7b19..ce42cb3f0e 100644 --- a/core/fxge/cfx_gemodule.cpp +++ b/core/fxge/cfx_gemodule.cpp @@ -35,7 +35,7 @@ WindowsPrintMode CFX_GEModule::GetPrintMode() { void CFX_GEModule::Create( std::optional> user_font_paths, RendererType renderer_type, - CFX_FontMgr::FontBackend font_backend) { + FontBackend font_backend) { DCHECK(!g_GEModule); g_GEModule = new CFX_GEModule(user_font_paths, renderer_type, font_backend); g_GEModule->platform_->Init(); @@ -60,14 +60,11 @@ CFX_GEModule* CFX_GEModule::Get() { CFX_GEModule::CFX_GEModule( std::optional> user_font_paths, RendererType renderer_type, - CFX_FontMgr::FontBackend font_backend) + FontBackend font_backend) : renderer_type_(renderer_type), -#if defined(PDF_ENABLE_FONTATIONS) font_backend_(font_backend), -#endif platform_(PlatformIface::Create()), font_mgr_(std::make_unique(font_backend)), - user_font_paths_(user_font_paths) { -} + user_font_paths_(user_font_paths) {} CFX_GEModule::~CFX_GEModule() = default; diff --git a/core/fxge/cfx_gemodule.h b/core/fxge/cfx_gemodule.h index 4275eaf88d..c6f1901b5e 100644 --- a/core/fxge/cfx_gemodule.h +++ b/core/fxge/cfx_gemodule.h @@ -70,19 +70,16 @@ class CFX_GEModule { enum class RendererType { kAgg = 0, kSkia = 1, -#if defined(PDF_USE_SKIA) - kDefault = kSkia, -#else - kDefault = kAgg, -#endif }; + using FontBackend = CFX_FontMgr::FontBackend; + // Uses other defaults when `user_font_paths` is nullopt, otherwise uses // only those in the span, which may lead to using none when empty. static void Create( std::optional> user_font_paths, RendererType renderer_type, - CFX_FontMgr::FontBackend font_backend); + FontBackend font_backend); static void Destroy(); static CFX_GEModule* Get(); @@ -103,38 +100,48 @@ class CFX_GEModule { static WindowsPrintMode GetPrintMode(); #endif -#if defined(PDF_USE_SKIA) - // Runtime check to see if Skia is the renderer variant in use. +#if defined(PDF_USE_AGG) && defined(PDF_USE_SKIA) + static bool IsAggRenderer() { + return Get()->renderer_type_ == RendererType::kAgg; + } static bool IsSkiaRenderer() { return Get()->renderer_type_ == RendererType::kSkia; } +#elif defined(PDF_USE_SKIA) + static bool IsAggRenderer() { return false; } + static bool IsSkiaRenderer() { return true; } +#elif defined(PDF_USE_AGG) + static bool IsAggRenderer() { return true; } + static bool IsSkiaRenderer() { return false; } +#else +#error "Define PDF_USE_AGG or PDF_USE_SKIA in the build env" #endif #if defined(PDF_ENABLE_FONTATIONS) && defined(PDF_ENABLE_FREETYPE) static bool IsFontations() { - return Get()->font_backend_ == CFX_FontMgr::FontBackend::kFontations; + return Get()->font_backend_ == FontBackend::kFontations; } static bool IsFreetype() { - return Get()->font_backend_ == CFX_FontMgr::FontBackend::kFreeType; + return Get()->font_backend_ == FontBackend::kFreeType; } #elif defined(PDF_ENABLE_FONTATIONS) static bool IsFontations() { return true; } + static bool IsFreetype() { return false; } #elif defined(PDF_ENABLE_FREETYPE) + static bool IsFontations() { return false; } static bool IsFreetype() { return true; } #else -#error "Neither freetype nor fontations available" +#error "Define PDF_ENABLE_FREETYPE or PDF_ENABLE_FONTATIONS in the build env" #endif private: CFX_GEModule(std::optional> user_font_paths, RendererType renderer_type, - CFX_FontMgr::FontBackend font_backend); + FontBackend font_backend); ~CFX_GEModule(); - const RendererType renderer_type_; -#if defined(PDF_ENABLE_FONTATIONS) - const CFX_FontMgr::FontBackend font_backend_; -#endif + [[maybe_unused]] const RendererType renderer_type_; + [[maybe_unused]] const FontBackend font_backend_; std::unique_ptr const platform_; // Must outlive `font_mgr_`. std::unique_ptr const font_mgr_; std::optional> user_font_paths_; diff --git a/fpdfsdk/fpdf_view.cpp b/fpdfsdk/fpdf_view.cpp index 1c7abd348e..94055a0a87 100644 --- a/fpdfsdk/fpdf_view.cpp +++ b/fpdfsdk/fpdf_view.cpp @@ -205,42 +205,52 @@ FPDF_InitLibraryWithConfig(const FPDF_LIBRARY_CONFIG* config) { } CFX_Timer::InitializeGlobals(); - CFX_GEModule::RendererType renderer_type = - CFX_GEModule::RendererType::kDefault; +#if defined(PDF_USE_SKIA) && defined(PDF_USE_AGG) + CFX_GEModule::RendererType renderer_type = CFX_GEModule::RendererType::kSkia; if (config && config->version >= 4) { -#if defined(PDF_USE_SKIA) CHECK(config->m_RendererType == FPDF_RENDERERTYPE_AGG || config->m_RendererType == FPDF_RENDERERTYPE_SKIA); - renderer_type = - static_cast(config->m_RendererType); -#else - // AGG-only builds should always use `FPDF_RENDERERTYPE_AGG`. + if (config->m_RendererType == FPDF_RENDERERTYPE_AGG) { + renderer_type = CFX_GEModule::RendererType::kAgg; + } + } +#elif defined(PDF_USE_SKIA) + CFX_GEModule::RendererType renderer_type = CFX_GEModule::RendererType::kSkia; + if (config && config->version >= 4) { + CHECK_EQ(config->m_RendererType, FPDF_RENDERERTYPE_SKIA); + } +#elif defined(PDF_USE_AGG) + CFX_GEModule::RendererType renderer_type = CFX_GEModule::RendererType::kAgg; + if (config && config->version >= 4) { CHECK_EQ(config->m_RendererType, FPDF_RENDERERTYPE_AGG); -#endif } +#else +#error "Define PDF_USE_AGG or PDF_USE_SKIA in the build env" +#endif #if defined(PDF_ENABLE_FONTATIONS) && defined(PDF_ENABLE_FREETYPE) - CFX_FontMgr::FontBackend backend = CFX_FontMgr::FontBackend::kFreeType; + CFX_GEModule::FontBackend backend = CFX_GEModule::FontBackend::kFreeType; if (config && config->version >= 5) { CHECK(config->m_FontLibraryType == FPDF_FONTBACKENDTYPE_FREETYPE || config->m_FontLibraryType == FPDF_FONTBACKENDTYPE_FONTATIONS); if (config->m_FontLibraryType == FPDF_FONTBACKENDTYPE_FONTATIONS) { - backend = CFX_FontMgr::FontBackend::kFontations; + backend = CFX_GEModule::FontBackend::kFontations; } } #elif defined(PDF_ENABLE_FONTATIONS) - CFX_FontMgr::FontBackend backend = CFX_FontMgr::FontBackend::kFontations; + CFX_GEModule::FontBackend backend = CFX_GEModule::FontBackend::kFontations; if (config && config->version >= 5) { CHECK_EQ(config->m_FontLibraryType, FPDF_FONTBACKENDTYPE_FONTATIONS); } #elif defined(PDF_ENABLE_FREETYPE) - CFX_FontMgr::FontBackend backend = CFX_FontMgr::FontBackend::kFreeType; + CFX_GEModule::FontBackend backend = CFX_GEModule::FontBackend::kFreeType; if (config && config->version >= 5) { CHECK_EQ(config->m_FontLibraryType, FPDF_FONTBACKENDTYPE_FREETYPE); } #else -#error "Neither FreeType nor Fontations enabled" +#error "Define PDF_ENABLE_FREETYPE or PDF_ENABLE_FONTATIONS in the build env" #endif + #if defined(PDF_ENABLE_BROTLI) BrotliDecoder::SetBrotliEnabled(config && config->version >= 6 && config->m_BrotliEnabled); @@ -277,6 +287,7 @@ FPDF_InitLibraryWithConfig(const FPDF_LIBRARY_CONFIG* config) { IJS_Runtime::Initialize(config->m_v8EmbedderSlot, config->m_pIsolate, platform, isolate_per_document); } + g_bLibraryInitialized = true; } diff --git a/testing/pdf_test_environment.cpp b/testing/pdf_test_environment.cpp index cf8ff83645..ab27cdec0e 100644 --- a/testing/pdf_test_environment.cpp +++ b/testing/pdf_test_environment.cpp @@ -11,22 +11,46 @@ #include "core/fxcrt/span.h" #include "core/fxge/cfx_gemodule.h" +namespace { + +CFX_GEModule::RendererType RendererTypeForTest() { +#if defined(PDF_USE_SKIA) + return CFX_GEModule::RendererType::kSkia; +#elif defined(PDF_USE_AGG) + return CFX_GEModule::RendererType::kAgg; +#else +#error "Define PDF_USE_AGG or PDF_USE_SKIA in the build env" +#endif +} + +CFX_GEModule::FontBackend FontBackendForTest([[maybe_unused]] bool fontations) { +#if defined(PDF_ENABLE_FONTATIONS) && defined(PDF_ENABLE_FREETYPE) + return fontations ? CFX_GEModule::FontBackend::kFontations + : CFX_GEModule::FontBackend::kFreeType; +#elif defined(PDF_ENABLE_FONTATIONS) + return CFX_GEModule::FontBackend::kFontations; +#elif defined(PDF_ENABLE_FREETYPE) + return CFX_GEModule::FontBackend::kFreeType; +#else +#error "Define PDF_ENABLE_FREETYPE or PDF_ENABLE_FONTATIONS in the build env" +#endif +} + +} // namespace + PDFTestEnvironment::PDFTestEnvironment() = default; PDFTestEnvironment::~PDFTestEnvironment() = default; // testing::Environment: void PDFTestEnvironment::SetUp() { - CFX_GEModule::Create(test_fonts_.FontPathsSpan(), - CFX_GEModule::RendererType::kDefault, -#if defined(PDF_ENABLE_FONTATIONS) && defined(PDF_ENABLE_FREETYPE) - fontations_ ? CFX_FontMgr::FontBackend::kFontations - : CFX_FontMgr::FontBackend::kFreeType); -#elif defined(PDF_ENABLE_FONTATIONS) - CFX_FontMgr::FontBackend::kFontations); +#if defined(PDF_ENABLE_FONTATIONS) + const bool fontations = fontations_; #else - CFX_FontMgr::FontBackend::kFreeType); + const bool fontations = false; #endif + CFX_GEModule::Create(test_fonts_.FontPathsSpan(), RendererTypeForTest(), + FontBackendForTest(fontations)); } void PDFTestEnvironment::TearDown() { From 60cd2f7e48747cfe85fa767abf4ce8ac9c551555 Mon Sep 17 00:00:00 2001 From: Tom Sepez Date: Wed, 25 Nov 2026 14:03:22 -0800 Subject: [PATCH 4/6] Support dynamic XFA pages in DisplayListRenderer Dynamic XFA pages return nullptr from CPDFPageFromFPDFPage() because CPDFXFA_Page::AsPDFPage() is null. DisplayListRenderer::Start() rejected those pages and DisplayListRenderer::Finish() dereferenced the null pointer. Use IPDFPageFromFPDFPage() in both methods and only invoke CPDFSDK_RenderPageWithContext() when AsPDFPage() is non-null. Change-Id: Ia4bcf202d1145148304b4082bbf00ba2e34d8a61 Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/165451 Reviewed-by: Andy Phan Commit-Queue: Tom Sepez --- fpdfsdk/display_list_renderer.cpp | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/fpdfsdk/display_list_renderer.cpp b/fpdfsdk/display_list_renderer.cpp index 3400d24537..93157fb5d4 100644 --- a/fpdfsdk/display_list_renderer.cpp +++ b/fpdfsdk/display_list_renderer.cpp @@ -8,6 +8,7 @@ #include "core/fpdfapi/page/cpdf_occontext.h" #include "core/fpdfapi/page/cpdf_page.h" +#include "core/fpdfapi/page/ipdf_page.h" #include "core/fpdfapi/parser/cpdf_document.h" #include "core/fpdfapi/render/cpdf_pagerendercontext.h" #include "core/fpdfapi/render/cpdf_renderoptions.h" @@ -52,17 +53,20 @@ bool DisplayListRenderer::Start() { if (!device_) { return false; } - CPDF_Page* cpdf_page = CPDFPageFromFPDFPage(page_); - if (!cpdf_page) { + IPDF_Page* ipdf_page = IPDFPageFromFPDFPage(page_); + if (!ipdf_page) { return false; } context_ = std::make_unique(); context_->device_ = std::move(device_); - CPDFSDK_RenderPageWithContext(context_.get(), cpdf_page, /*start_x=*/0, - /*start_y=*/0, /*size_x=*/width_, - /*size_y=*/height_, /*rotate=*/0, - /*flags=*/flags_, nullptr, - /*need_to_restore=*/true, nullptr); + CPDF_Page* cpdf_page = ipdf_page->AsPDFPage(); + if (cpdf_page) { + CPDFSDK_RenderPageWithContext(context_.get(), cpdf_page, /*start_x=*/0, + /*start_y=*/0, /*size_x=*/width_, + /*size_y=*/height_, /*rotate=*/0, + /*flags=*/flags_, nullptr, + /*need_to_restore=*/true, nullptr); + } return true; } @@ -75,9 +79,8 @@ void DisplayListRenderer::Finish(FPDF_FORMHANDLE form) { CPDFSDK_PageView* page_view = form_fill_env->GetOrCreatePageView(ipdf_page); if (page_view) { - CPDF_Page* cpdf_page = CPDFPageFromFPDFPage(page_); const FX_RECT rect(0, 0, width_, height_); - CFX_Matrix matrix = cpdf_page->GetDisplayMatrixForRect(rect, 0); + CFX_Matrix matrix = ipdf_page->GetDisplayMatrixForRect(rect, 0); CPDF_RenderOptions options; options.GetOptions().bClearType = !!(flags_ & FPDF_LCD_TEXT); if (flags_ & FPDF_GRAYSCALE) { @@ -85,7 +88,7 @@ void DisplayListRenderer::Finish(FPDF_FORMHANDLE form) { } options.SetDrawAnnots(flags_ & FPDF_ANNOT); options.SetOCContext(pdfium::MakeRetain( - cpdf_page->GetDocument(), CPDF_OCContext::kView)); + ipdf_page->GetDocument(), CPDF_OCContext::kView)); page_view->PageView_OnDraw(context_->device_.get(), matrix, &options, rect); } From 7cc6ea344921cc937162733e7cb6eb276ec78f6b Mon Sep 17 00:00:00 2001 From: Tom Sepez Date: Wed, 25 Nov 2026 15:09:27 -0800 Subject: [PATCH 5/6] Add XFA drawing tests for layout quick-wins Add tests (.in files) under testing/resources/drawing/xfa_specific to exercise high-impact layout edge cases: - xfa_maxw_maxh_autosize.in: Verifies that an auto-sizing subform containing smaller content auto-fits rather than expanding to maxW/maxH. - xfa_minw_minh_floor.in: Verifies that an auto-sizing subform respects minW/minH floor dimensions. - xfa_page_odd_even_break.in: Verifies that breakBefore targeting pageOdd advances pagination to an odd page, emitting a filler page. Change-Id: I11694e985a4f4a697648c5f2772bf75c20bcc798 Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/164651 Reviewed-by: Andy Phan Commit-Queue: Tom Sepez --- .../xfa_specific/xfa_maxw_maxh_autosize.in | 51 ++++++++++++++++ ...maxw_maxh_autosize_expected.pdf.0.dump.txt | 54 ++++++++++++++++ .../xfa_specific/xfa_minw_minh_floor.in | 51 ++++++++++++++++ ...fa_minw_minh_floor_expected.pdf.0.dump.txt | 54 ++++++++++++++++ .../xfa_specific/xfa_page_odd_even_break.in | 61 +++++++++++++++++++ ...age_odd_even_break_expected.pdf.0.dump.txt | 59 ++++++++++++++++++ 6 files changed, 330 insertions(+) create mode 100644 testing/resources/drawing/xfa_specific/xfa_maxw_maxh_autosize.in create mode 100644 testing/resources/drawing/xfa_specific/xfa_maxw_maxh_autosize_expected.pdf.0.dump.txt create mode 100644 testing/resources/drawing/xfa_specific/xfa_minw_minh_floor.in create mode 100644 testing/resources/drawing/xfa_specific/xfa_minw_minh_floor_expected.pdf.0.dump.txt create mode 100644 testing/resources/drawing/xfa_specific/xfa_page_odd_even_break.in create mode 100644 testing/resources/drawing/xfa_specific/xfa_page_odd_even_break_expected.pdf.0.dump.txt diff --git a/testing/resources/drawing/xfa_specific/xfa_maxw_maxh_autosize.in b/testing/resources/drawing/xfa_specific/xfa_maxw_maxh_autosize.in new file mode 100644 index 0000000000..7b85b5e44c --- /dev/null +++ b/testing/resources/drawing/xfa_specific/xfa_maxw_maxh_autosize.in @@ -0,0 +1,51 @@ +{{header}} +{{include ../../xfa_catalog_1_0.fragment}} +{{include ../../xfa_object_2_0.fragment}} +{{include ../../xfa_preamble_3_0.fragment}} +{{include ../../xfa_config_4_0.fragment}} +{{object 5 0}} << + {{streamlen}} +>> +stream + +endstream +endobj +{{include ../../xfa_locale_6_0.fragment}} +{{include ../../xfa_postamble_7_0.fragment}} +{{include ../../xfa_pages_8_0.fragment}} +{{xref}} +{{trailer}} +{{startxref}} +%%EOF diff --git a/testing/resources/drawing/xfa_specific/xfa_maxw_maxh_autosize_expected.pdf.0.dump.txt b/testing/resources/drawing/xfa_specific/xfa_maxw_maxh_autosize_expected.pdf.0.dump.txt new file mode 100644 index 0000000000..f25fe7d716 --- /dev/null +++ b/testing/resources/drawing/xfa_specific/xfa_maxw_maxh_autosize_expected.pdf.0.dump.txt @@ -0,0 +1,54 @@ +SetClip_PathFill rule=winding matrix=none points=[move(0.0000,792.0000) line(0.0000,0.0000) line(612.0000,0.0000) line(612.0000,792.0000) line(0.0000,792.0000,close)] +SaveState +DrawPath fill=0x00000000 stroke=0xff000000 rule=no-fill stroke_state=[width=0.0000 cap=butt join=miter miter=10.0000] matrix=[1.0000,0.0000,0.0000,1.0000,0.0000,0.0000] points=[move(18.0000,18.0000) line(18.0000,18.0000) line(18.0000,18.0000)] +RestoreState +SaveState +DrawPath fill=0x00000000 stroke=0xffff0000 rule=no-fill stroke_state=[width=2.0000 cap=butt join=miter miter=10.0000] matrix=[1.0000,0.0000,0.0000,1.0000,0.0000,0.0000] points=[move(17.0000,18.0000) line(319.0000,18.0000)] +RestoreState +SaveState +DrawPath fill=0x00000000 stroke=0xff000000 rule=no-fill stroke_state=[width=0.0000 cap=butt join=miter miter=10.0000] matrix=[1.0000,0.0000,0.0000,1.0000,0.0000,0.0000] points=[move(318.0000,18.0000) line(318.0000,18.0000) line(318.0000,18.0000)] +RestoreState +SaveState +DrawPath fill=0x00000000 stroke=0xffff0000 rule=no-fill stroke_state=[width=2.0000 cap=butt join=miter miter=10.0000] matrix=[1.0000,0.0000,0.0000,1.0000,0.0000,0.0000] points=[move(318.0000,17.0000) line(318.0000,219.0000)] +RestoreState +SaveState +DrawPath fill=0x00000000 stroke=0xff000000 rule=no-fill stroke_state=[width=0.0000 cap=butt join=miter miter=10.0000] matrix=[1.0000,0.0000,0.0000,1.0000,0.0000,0.0000] points=[move(318.0000,218.0000) line(318.0000,218.0000) line(318.0000,218.0000)] +RestoreState +SaveState +DrawPath fill=0x00000000 stroke=0xffff0000 rule=no-fill stroke_state=[width=2.0000 cap=butt join=miter miter=10.0000] matrix=[1.0000,0.0000,0.0000,1.0000,0.0000,0.0000] points=[move(319.0000,218.0000) line(17.0000,218.0000)] +RestoreState +SaveState +DrawPath fill=0x00000000 stroke=0xff000000 rule=no-fill stroke_state=[width=0.0000 cap=butt join=miter miter=10.0000] matrix=[1.0000,0.0000,0.0000,1.0000,0.0000,0.0000] points=[move(18.0000,218.0000) line(18.0000,218.0000) line(18.0000,218.0000)] +RestoreState +SaveState +DrawPath fill=0x00000000 stroke=0xffff0000 rule=no-fill stroke_state=[width=2.0000 cap=butt join=miter miter=10.0000] matrix=[1.0000,0.0000,0.0000,1.0000,0.0000,0.0000] points=[move(18.0000,219.0000) line(18.0000,17.0000)] +RestoreState +SaveState +SaveState +FillRect [18,18,118,68] color=0xffc8c8ff +RestoreState +RestoreState +SaveState +DrawPath fill=0x00000000 stroke=0xff000000 rule=no-fill stroke_state=[width=0.0000 cap=butt join=miter miter=10.0000] matrix=[1.0000,0.0000,0.0000,1.0000,0.0000,0.0000] points=[move(18.0000,18.0000) line(18.0000,18.0000) line(18.0000,18.0000)] +RestoreState +SaveState +DrawPath fill=0x00000000 stroke=0xff0000ff rule=no-fill stroke_state=[width=1.0000 cap=butt join=miter miter=10.0000] matrix=[1.0000,0.0000,0.0000,1.0000,0.0000,0.0000] points=[move(17.5000,18.0000) line(118.5000,18.0000)] +RestoreState +SaveState +DrawPath fill=0x00000000 stroke=0xff000000 rule=no-fill stroke_state=[width=0.0000 cap=butt join=miter miter=10.0000] matrix=[1.0000,0.0000,0.0000,1.0000,0.0000,0.0000] points=[move(118.0000,18.0000) line(118.0000,18.0000) line(118.0000,18.0000)] +RestoreState +SaveState +DrawPath fill=0x00000000 stroke=0xff0000ff rule=no-fill stroke_state=[width=1.0000 cap=butt join=miter miter=10.0000] matrix=[1.0000,0.0000,0.0000,1.0000,0.0000,0.0000] points=[move(118.0000,17.5000) line(118.0000,68.5000)] +RestoreState +SaveState +DrawPath fill=0x00000000 stroke=0xff000000 rule=no-fill stroke_state=[width=0.0000 cap=butt join=miter miter=10.0000] matrix=[1.0000,0.0000,0.0000,1.0000,0.0000,0.0000] points=[move(118.0000,68.0000) line(118.0000,68.0000) line(118.0000,68.0000)] +RestoreState +SaveState +DrawPath fill=0x00000000 stroke=0xff0000ff rule=no-fill stroke_state=[width=1.0000 cap=butt join=miter miter=10.0000] matrix=[1.0000,0.0000,0.0000,1.0000,0.0000,0.0000] points=[move(118.5000,68.0000) line(17.5000,68.0000)] +RestoreState +SaveState +DrawPath fill=0x00000000 stroke=0xff000000 rule=no-fill stroke_state=[width=0.0000 cap=butt join=miter miter=10.0000] matrix=[1.0000,0.0000,0.0000,1.0000,0.0000,0.0000] points=[move(18.0000,68.0000) line(18.0000,68.0000) line(18.0000,68.0000)] +RestoreState +SaveState +DrawPath fill=0x00000000 stroke=0xff0000ff rule=no-fill stroke_state=[width=1.0000 cap=butt join=miter miter=10.0000] matrix=[1.0000,0.0000,0.0000,1.0000,0.0000,0.0000] points=[move(18.0000,68.5000) line(18.0000,17.5000)] +RestoreState diff --git a/testing/resources/drawing/xfa_specific/xfa_minw_minh_floor.in b/testing/resources/drawing/xfa_specific/xfa_minw_minh_floor.in new file mode 100644 index 0000000000..14a06c2184 --- /dev/null +++ b/testing/resources/drawing/xfa_specific/xfa_minw_minh_floor.in @@ -0,0 +1,51 @@ +{{header}} +{{include ../../xfa_catalog_1_0.fragment}} +{{include ../../xfa_object_2_0.fragment}} +{{include ../../xfa_preamble_3_0.fragment}} +{{include ../../xfa_config_4_0.fragment}} +{{object 5 0}} << + {{streamlen}} +>> +stream + +endstream +endobj +{{include ../../xfa_locale_6_0.fragment}} +{{include ../../xfa_postamble_7_0.fragment}} +{{include ../../xfa_pages_8_0.fragment}} +{{xref}} +{{trailer}} +{{startxref}} +%%EOF diff --git a/testing/resources/drawing/xfa_specific/xfa_minw_minh_floor_expected.pdf.0.dump.txt b/testing/resources/drawing/xfa_specific/xfa_minw_minh_floor_expected.pdf.0.dump.txt new file mode 100644 index 0000000000..5a6e1b222e --- /dev/null +++ b/testing/resources/drawing/xfa_specific/xfa_minw_minh_floor_expected.pdf.0.dump.txt @@ -0,0 +1,54 @@ +SetClip_PathFill rule=winding matrix=none points=[move(0.0000,792.0000) line(0.0000,0.0000) line(612.0000,0.0000) line(612.0000,792.0000) line(0.0000,792.0000,close)] +SaveState +DrawPath fill=0x00000000 stroke=0xff000000 rule=no-fill stroke_state=[width=0.0000 cap=butt join=miter miter=10.0000] matrix=[1.0000,0.0000,0.0000,1.0000,0.0000,0.0000] points=[move(18.0000,18.0000) line(18.0000,18.0000) line(18.0000,18.0000)] +RestoreState +SaveState +DrawPath fill=0x00000000 stroke=0xff009600 rule=no-fill stroke_state=[width=2.0000 cap=butt join=miter miter=10.0000] matrix=[1.0000,0.0000,0.0000,1.0000,0.0000,0.0000] points=[move(17.0000,18.0000) line(79.0000,18.0000)] +RestoreState +SaveState +DrawPath fill=0x00000000 stroke=0xff000000 rule=no-fill stroke_state=[width=0.0000 cap=butt join=miter miter=10.0000] matrix=[1.0000,0.0000,0.0000,1.0000,0.0000,0.0000] points=[move(78.0000,18.0000) line(78.0000,18.0000) line(78.0000,18.0000)] +RestoreState +SaveState +DrawPath fill=0x00000000 stroke=0xff009600 rule=no-fill stroke_state=[width=2.0000 cap=butt join=miter miter=10.0000] matrix=[1.0000,0.0000,0.0000,1.0000,0.0000,0.0000] points=[move(78.0000,17.0000) line(78.0000,59.0000)] +RestoreState +SaveState +DrawPath fill=0x00000000 stroke=0xff000000 rule=no-fill stroke_state=[width=0.0000 cap=butt join=miter miter=10.0000] matrix=[1.0000,0.0000,0.0000,1.0000,0.0000,0.0000] points=[move(78.0000,58.0000) line(78.0000,58.0000) line(78.0000,58.0000)] +RestoreState +SaveState +DrawPath fill=0x00000000 stroke=0xff009600 rule=no-fill stroke_state=[width=2.0000 cap=butt join=miter miter=10.0000] matrix=[1.0000,0.0000,0.0000,1.0000,0.0000,0.0000] points=[move(79.0000,58.0000) line(17.0000,58.0000)] +RestoreState +SaveState +DrawPath fill=0x00000000 stroke=0xff000000 rule=no-fill stroke_state=[width=0.0000 cap=butt join=miter miter=10.0000] matrix=[1.0000,0.0000,0.0000,1.0000,0.0000,0.0000] points=[move(18.0000,58.0000) line(18.0000,58.0000) line(18.0000,58.0000)] +RestoreState +SaveState +DrawPath fill=0x00000000 stroke=0xff009600 rule=no-fill stroke_state=[width=2.0000 cap=butt join=miter miter=10.0000] matrix=[1.0000,0.0000,0.0000,1.0000,0.0000,0.0000] points=[move(18.0000,59.0000) line(18.0000,17.0000)] +RestoreState +SaveState +SaveState +FillRect [18,18,78,58] color=0xffffc8c8 +RestoreState +RestoreState +SaveState +DrawPath fill=0x00000000 stroke=0xff000000 rule=no-fill stroke_state=[width=0.0000 cap=butt join=miter miter=10.0000] matrix=[1.0000,0.0000,0.0000,1.0000,0.0000,0.0000] points=[move(18.0000,18.0000) line(18.0000,18.0000) line(18.0000,18.0000)] +RestoreState +SaveState +DrawPath fill=0x00000000 stroke=0xff000000 rule=no-fill stroke_state=[width=1.0000 cap=butt join=miter miter=10.0000] matrix=[1.0000,0.0000,0.0000,1.0000,0.0000,0.0000] points=[move(17.5000,18.0000) line(78.5000,18.0000)] +RestoreState +SaveState +DrawPath fill=0x00000000 stroke=0xff000000 rule=no-fill stroke_state=[width=0.0000 cap=butt join=miter miter=10.0000] matrix=[1.0000,0.0000,0.0000,1.0000,0.0000,0.0000] points=[move(78.0000,18.0000) line(78.0000,18.0000) line(78.0000,18.0000)] +RestoreState +SaveState +DrawPath fill=0x00000000 stroke=0xff000000 rule=no-fill stroke_state=[width=1.0000 cap=butt join=miter miter=10.0000] matrix=[1.0000,0.0000,0.0000,1.0000,0.0000,0.0000] points=[move(78.0000,17.5000) line(78.0000,58.5000)] +RestoreState +SaveState +DrawPath fill=0x00000000 stroke=0xff000000 rule=no-fill stroke_state=[width=0.0000 cap=butt join=miter miter=10.0000] matrix=[1.0000,0.0000,0.0000,1.0000,0.0000,0.0000] points=[move(78.0000,58.0000) line(78.0000,58.0000) line(78.0000,58.0000)] +RestoreState +SaveState +DrawPath fill=0x00000000 stroke=0xff000000 rule=no-fill stroke_state=[width=1.0000 cap=butt join=miter miter=10.0000] matrix=[1.0000,0.0000,0.0000,1.0000,0.0000,0.0000] points=[move(78.5000,58.0000) line(17.5000,58.0000)] +RestoreState +SaveState +DrawPath fill=0x00000000 stroke=0xff000000 rule=no-fill stroke_state=[width=0.0000 cap=butt join=miter miter=10.0000] matrix=[1.0000,0.0000,0.0000,1.0000,0.0000,0.0000] points=[move(18.0000,58.0000) line(18.0000,58.0000) line(18.0000,58.0000)] +RestoreState +SaveState +DrawPath fill=0x00000000 stroke=0xff000000 rule=no-fill stroke_state=[width=1.0000 cap=butt join=miter miter=10.0000] matrix=[1.0000,0.0000,0.0000,1.0000,0.0000,0.0000] points=[move(18.0000,58.5000) line(18.0000,17.5000)] +RestoreState diff --git a/testing/resources/drawing/xfa_specific/xfa_page_odd_even_break.in b/testing/resources/drawing/xfa_specific/xfa_page_odd_even_break.in new file mode 100644 index 0000000000..3eef4e243c --- /dev/null +++ b/testing/resources/drawing/xfa_specific/xfa_page_odd_even_break.in @@ -0,0 +1,61 @@ +{{header}} +{{include ../../xfa_catalog_1_0.fragment}} +{{include ../../xfa_object_2_0.fragment}} +{{include ../../xfa_preamble_3_0.fragment}} +{{include ../../xfa_config_4_0.fragment}} +{{object 5 0}} << + {{streamlen}} +>> +stream + +endstream +endobj +{{include ../../xfa_locale_6_0.fragment}} +{{include ../../xfa_postamble_7_0.fragment}} +{{include ../../xfa_pages_8_0.fragment}} +{{xref}} +{{trailer}} +{{startxref}} +%%EOF diff --git a/testing/resources/drawing/xfa_specific/xfa_page_odd_even_break_expected.pdf.0.dump.txt b/testing/resources/drawing/xfa_specific/xfa_page_odd_even_break_expected.pdf.0.dump.txt new file mode 100644 index 0000000000..0f0f188da6 --- /dev/null +++ b/testing/resources/drawing/xfa_specific/xfa_page_odd_even_break_expected.pdf.0.dump.txt @@ -0,0 +1,59 @@ +SetClip_PathFill rule=winding matrix=none points=[move(0.0000,792.0000) line(0.0000,0.0000) line(612.0000,0.0000) line(612.0000,792.0000) line(0.0000,792.0000,close)] +SaveState +SaveState +FillRect [68,68,268,168] color=0xffb4b4ff +RestoreState +RestoreState +SaveState +DrawPath fill=0x00000000 stroke=0xff000000 rule=no-fill stroke_state=[width=0.0000 cap=butt join=miter miter=10.0000] matrix=[1.0000,0.0000,0.0000,1.0000,0.0000,0.0000] points=[move(68.0000,68.0000) line(68.0000,68.0000) line(68.0000,68.0000)] +RestoreState +SaveState +DrawPath fill=0x00000000 stroke=0xff0000ff rule=no-fill stroke_state=[width=2.0000 cap=butt join=miter miter=10.0000] matrix=[1.0000,0.0000,0.0000,1.0000,0.0000,0.0000] points=[move(67.0000,68.0000) line(269.0000,68.0000)] +RestoreState +SaveState +DrawPath fill=0x00000000 stroke=0xff000000 rule=no-fill stroke_state=[width=0.0000 cap=butt join=miter miter=10.0000] matrix=[1.0000,0.0000,0.0000,1.0000,0.0000,0.0000] points=[move(268.0000,68.0000) line(268.0000,68.0000) line(268.0000,68.0000)] +RestoreState +SaveState +DrawPath fill=0x00000000 stroke=0xff0000ff rule=no-fill stroke_state=[width=2.0000 cap=butt join=miter miter=10.0000] matrix=[1.0000,0.0000,0.0000,1.0000,0.0000,0.0000] points=[move(268.0000,67.0000) line(268.0000,169.0000)] +RestoreState +SaveState +DrawPath fill=0x00000000 stroke=0xff000000 rule=no-fill stroke_state=[width=0.0000 cap=butt join=miter miter=10.0000] matrix=[1.0000,0.0000,0.0000,1.0000,0.0000,0.0000] points=[move(268.0000,168.0000) line(268.0000,168.0000) line(268.0000,168.0000)] +RestoreState +SaveState +DrawPath fill=0x00000000 stroke=0xff0000ff rule=no-fill stroke_state=[width=2.0000 cap=butt join=miter miter=10.0000] matrix=[1.0000,0.0000,0.0000,1.0000,0.0000,0.0000] points=[move(269.0000,168.0000) line(67.0000,168.0000)] +RestoreState +SaveState +DrawPath fill=0x00000000 stroke=0xff000000 rule=no-fill stroke_state=[width=0.0000 cap=butt join=miter miter=10.0000] matrix=[1.0000,0.0000,0.0000,1.0000,0.0000,0.0000] points=[move(68.0000,168.0000) line(68.0000,168.0000) line(68.0000,168.0000)] +RestoreState +SaveState +DrawPath fill=0x00000000 stroke=0xff0000ff rule=no-fill stroke_state=[width=2.0000 cap=butt join=miter miter=10.0000] matrix=[1.0000,0.0000,0.0000,1.0000,0.0000,0.0000] points=[move(68.0000,169.0000) line(68.0000,67.0000)] +RestoreState +SaveState +SaveState +FillRect [68,268,268,368] color=0xffffb4b4 +RestoreState +RestoreState +SaveState +DrawPath fill=0x00000000 stroke=0xff000000 rule=no-fill stroke_state=[width=0.0000 cap=butt join=miter miter=10.0000] matrix=[1.0000,0.0000,0.0000,1.0000,0.0000,0.0000] points=[move(68.0000,268.0000) line(68.0000,268.0000) line(68.0000,268.0000)] +RestoreState +SaveState +DrawPath fill=0x00000000 stroke=0xffff0000 rule=no-fill stroke_state=[width=2.0000 cap=butt join=miter miter=10.0000] matrix=[1.0000,0.0000,0.0000,1.0000,0.0000,0.0000] points=[move(67.0000,268.0000) line(269.0000,268.0000)] +RestoreState +SaveState +DrawPath fill=0x00000000 stroke=0xff000000 rule=no-fill stroke_state=[width=0.0000 cap=butt join=miter miter=10.0000] matrix=[1.0000,0.0000,0.0000,1.0000,0.0000,0.0000] points=[move(268.0000,268.0000) line(268.0000,268.0000) line(268.0000,268.0000)] +RestoreState +SaveState +DrawPath fill=0x00000000 stroke=0xffff0000 rule=no-fill stroke_state=[width=2.0000 cap=butt join=miter miter=10.0000] matrix=[1.0000,0.0000,0.0000,1.0000,0.0000,0.0000] points=[move(268.0000,267.0000) line(268.0000,369.0000)] +RestoreState +SaveState +DrawPath fill=0x00000000 stroke=0xff000000 rule=no-fill stroke_state=[width=0.0000 cap=butt join=miter miter=10.0000] matrix=[1.0000,0.0000,0.0000,1.0000,0.0000,0.0000] points=[move(268.0000,368.0000) line(268.0000,368.0000) line(268.0000,368.0000)] +RestoreState +SaveState +DrawPath fill=0x00000000 stroke=0xffff0000 rule=no-fill stroke_state=[width=2.0000 cap=butt join=miter miter=10.0000] matrix=[1.0000,0.0000,0.0000,1.0000,0.0000,0.0000] points=[move(269.0000,368.0000) line(67.0000,368.0000)] +RestoreState +SaveState +DrawPath fill=0x00000000 stroke=0xff000000 rule=no-fill stroke_state=[width=0.0000 cap=butt join=miter miter=10.0000] matrix=[1.0000,0.0000,0.0000,1.0000,0.0000,0.0000] points=[move(68.0000,368.0000) line(68.0000,368.0000) line(68.0000,368.0000)] +RestoreState +SaveState +DrawPath fill=0x00000000 stroke=0xffff0000 rule=no-fill stroke_state=[width=2.0000 cap=butt join=miter miter=10.0000] matrix=[1.0000,0.0000,0.0000,1.0000,0.0000,0.0000] points=[move(68.0000,369.0000) line(68.0000,267.0000)] +RestoreState From 31e80351ba22dc50160116aad33e2e5b329b2658 Mon Sep 17 00:00:00 2001 From: Tom Sepez Date: Wed, 25 Nov 2026 16:01:46 -0800 Subject: [PATCH 6/6] Support dynamic XFA pages in DisplayListRenderer, part 2. Dynamic XFA pages do not have an underlying PDF page object. Hence, DisplayListRenderer::Start() previously failed on all dynamic XFA documents. - Remove GetBitmap() calls when only dimensions are required. - No longer CHECK() that readback is required for xfa images. Change-Id: I41b544fd038305e5bd0b71d1cb63d46c0e9bb304 Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/164690 Reviewed-by: Andy Phan Commit-Queue: Tom Sepez --- core/fxge/display_list_device_driver.cpp | 4 ++++ core/fxge/display_list_device_driver.h | 1 + fpdfsdk/fpdf_formfill_embeddertest.cpp | 23 +++++++++++++++++++ .../bug_547706_expected.pdf.0.dump.txt | 1 + ...tion_stamp_with_ap_modified_image.dump.txt | 1 + ...nnotation_stamp_with_ap_new_image.dump.txt | 1 + xfa/fgas/graphics/cfgas_gegraphics.cpp | 10 ++++---- xfa/fxfa/cxfa_imagerenderer.cpp | 2 -- 8 files changed, 35 insertions(+), 8 deletions(-) diff --git a/core/fxge/display_list_device_driver.cpp b/core/fxge/display_list_device_driver.cpp index 1f5622f457..2290e610d5 100644 --- a/core/fxge/display_list_device_driver.cpp +++ b/core/fxge/display_list_device_driver.cpp @@ -142,6 +142,10 @@ int DisplayListDeviceDriver::GetBitsPerPixel() const { return 32; } +bool DisplayListDeviceDriver::RenderCapAlphaImage() const { + return true; +} + bool DisplayListDeviceDriver::RenderCapAlphaOutput() const { return true; } diff --git a/core/fxge/display_list_device_driver.h b/core/fxge/display_list_device_driver.h index 9587cc9bf5..2aa60b3475 100644 --- a/core/fxge/display_list_device_driver.h +++ b/core/fxge/display_list_device_driver.h @@ -40,6 +40,7 @@ class DisplayListDeviceDriver final : public RenderDeviceDriverIface { int GetPixelWidth() const override; int GetPixelHeight() const override; int GetBitsPerPixel() const override; + bool RenderCapAlphaImage() const override; bool RenderCapAlphaOutput() const override; #if defined(PDF_USE_SKIA) bool RenderCapFillStrokePath() const override; diff --git a/fpdfsdk/fpdf_formfill_embeddertest.cpp b/fpdfsdk/fpdf_formfill_embeddertest.cpp index c3a35fb92a..2660390108 100644 --- a/fpdfsdk/fpdf_formfill_embeddertest.cpp +++ b/fpdfsdk/fpdf_formfill_embeddertest.cpp @@ -1611,6 +1611,29 @@ TEST_F(FPDFFormFillEmbedderTest, HasFormFieldAtPointForXFADoc) { FPDFPage_HasFormFieldAtPoint(form_handle(), page.get(), 50, 30)); } +#ifdef PDF_ENABLE_XFA +TEST_F(FPDFFormFillEmbedderTest, RenderXfaPageToDisplayList) { + ASSERT_TRUE(OpenDocument("simple_xfa.pdf")); + ScopedPage page = LoadScopedPage(0); + ASSERT_TRUE(page); + + std::string display_list = RenderLoadedPageToDisplayList(page.get()); + EXPECT_NE(std::string::npos, + display_list.find("FillRect [18,18,342,54] color=0x64dde4ff")); + EXPECT_NE(std::string::npos, + display_list.find("FillRect [18,54,253,90] color=0x64dde4ff")); +} + +TEST_F(FPDFFormFillEmbedderTest, RenderXfaImageToDisplayList) { + ASSERT_TRUE(OpenDocument("xfa/xfa_image_edit.pdf")); + ScopedPage page = LoadScopedPage(0); + ASSERT_TRUE(page); + + std::string display_list = RenderLoadedPageToDisplayList(page.get()); + EXPECT_NE(std::string::npos, display_list.find("StartDIBits")); +} +#endif // PDF_ENABLE_XFA + TEST_F(FPDFFormFillEmbedderTest, SelectAllText) { ASSERT_TRUE(OpenDocument("text_form.pdf")); ScopedPage page = LoadScopedPage(0); diff --git a/testing/resources/drawing/bug_547706_expected.pdf.0.dump.txt b/testing/resources/drawing/bug_547706_expected.pdf.0.dump.txt index cbb48d7881..1e885e5377 100644 --- a/testing/resources/drawing/bug_547706_expected.pdf.0.dump.txt +++ b/testing/resources/drawing/bug_547706_expected.pdf.0.dump.txt @@ -3,5 +3,6 @@ SetBaseClip [0,0,612,792] SetClip_PathFill rule=winding matrix=none points=[move(0.0000,792.0000) line(0.0000,0.0000) line(612.0000,0.0000) line(612.0000,792.0000) line(0.0000,792.0000,close)] SaveState RestoreState +SetDIBits size=[479,26] bpp=32 dest=(2,766) src=[0,0,479,26] color=0x00000000 RestoreState RestoreState diff --git a/testing/resources/embedder_tests/annotation_stamp_with_ap_modified_image.dump.txt b/testing/resources/embedder_tests/annotation_stamp_with_ap_modified_image.dump.txt index 7c0bd39b5d..773ae19a2a 100644 --- a/testing/resources/embedder_tests/annotation_stamp_with_ap_modified_image.dump.txt +++ b/testing/resources/embedder_tests/annotation_stamp_with_ap_modified_image.dump.txt @@ -44,5 +44,6 @@ SaveState DrawPath fill=0xffff2968 stroke=0x00000000 rule=winding stroke_state=[width=1.0000 cap=butt join=miter miter=10.0000] matrix=[1.0000,0.0000,0.0000,-1.0000,349.5274,234.3295] points=[move(7.5274,4.6705) bezier(7.5274,3.0136) bezier(6.1843,1.6705) bezier(4.5274,1.6705) bezier(2.8706,1.6705) bezier(1.5274,3.0136) bezier(1.5274,4.6705) bezier(1.5274,6.3273) bezier(2.8706,7.6705) bezier(4.5274,7.6705) bezier(6.1843,7.6705) bezier(7.5274,6.3273) bezier(7.5274,4.6705,close)] RestoreState SaveState +SetDIBits size=[200,200] bpp=32 dest=(200,42) src=[0,0,200,200] color=0x00000000 RestoreState RestoreState diff --git a/testing/resources/embedder_tests/annotation_stamp_with_ap_new_image.dump.txt b/testing/resources/embedder_tests/annotation_stamp_with_ap_new_image.dump.txt index 7c0bd39b5d..773ae19a2a 100644 --- a/testing/resources/embedder_tests/annotation_stamp_with_ap_new_image.dump.txt +++ b/testing/resources/embedder_tests/annotation_stamp_with_ap_new_image.dump.txt @@ -44,5 +44,6 @@ SaveState DrawPath fill=0xffff2968 stroke=0x00000000 rule=winding stroke_state=[width=1.0000 cap=butt join=miter miter=10.0000] matrix=[1.0000,0.0000,0.0000,-1.0000,349.5274,234.3295] points=[move(7.5274,4.6705) bezier(7.5274,3.0136) bezier(6.1843,1.6705) bezier(4.5274,1.6705) bezier(2.8706,1.6705) bezier(1.5274,3.0136) bezier(1.5274,4.6705) bezier(1.5274,6.3273) bezier(2.8706,7.6705) bezier(4.5274,7.6705) bezier(6.1843,7.6705) bezier(7.5274,6.3273) bezier(7.5274,4.6705,close)] RestoreState SaveState +SetDIBits size=[200,200] bpp=32 dest=(200,42) src=[0,0,200,200] color=0x00000000 RestoreState RestoreState diff --git a/xfa/fgas/graphics/cfgas_gegraphics.cpp b/xfa/fgas/graphics/cfgas_gegraphics.cpp index 3ddbbd23c4..47686ce69b 100644 --- a/xfa/fgas/graphics/cfgas_gegraphics.cpp +++ b/xfa/fgas/graphics/cfgas_gegraphics.cpp @@ -245,9 +245,8 @@ void CFGAS_GEGraphics::FillPathWithPattern( const CFGAS_GEPath& path, const CFX_FillRenderOptions& fill_options, const CFX_Matrix& matrix) { - RetainPtr bitmap = render_device_->GetBitmap(); - int32_t width = bitmap->GetWidth(); - int32_t height = bitmap->GetHeight(); + int32_t width = render_device_->GetWidth(); + int32_t height = render_device_->GetHeight(); auto bmp = pdfium::MakeRetain(); // TODO(crbug.com/355630556): Consider adding support for // `FXDIB_Format::kBgraPremul` @@ -288,9 +287,8 @@ void CFGAS_GEGraphics::FillPathWithShading( const CFGAS_GEPath& path, const CFX_FillRenderOptions& fill_options, const CFX_Matrix& matrix) { - RetainPtr bitmap = render_device_->GetBitmap(); - int32_t width = bitmap->GetWidth(); - int32_t height = bitmap->GetHeight(); + int32_t width = render_device_->GetWidth(); + int32_t height = render_device_->GetHeight(); float start_x = info_.fillColor.GetShading()->GetBeginPoint().x; float start_y = info_.fillColor.GetShading()->GetBeginPoint().y; float end_x = info_.fillColor.GetShading()->GetEndPoint().x; diff --git a/xfa/fxfa/cxfa_imagerenderer.cpp b/xfa/fxfa/cxfa_imagerenderer.cpp index c40c819193..02cb75de33 100644 --- a/xfa/fxfa/cxfa_imagerenderer.cpp +++ b/xfa/fxfa/cxfa_imagerenderer.cpp @@ -19,9 +19,7 @@ CXFA_ImageRenderer::CXFA_ImageRenderer(CFX_RenderDevice* device, : image_matrix_(image_to_device), device_(device), bitmap_(std::move(bitmap)) { - // Assume this always draws into CFX_RenderDevice. CHECK(device_); - CHECK(device_->RenderCapGetBits()); CHECK(bitmap_); }