From 70c4afd5c3c5c44cd24f814423a23a6ef05bba02 Mon Sep 17 00:00:00 2001 From: tsepez Date: Tue, 15 Nov 2016 11:33:44 -0800 Subject: Make AddIndirectObject() take a unique_ptr. Add convenience routines to create and add object in one step. Review-Url: https://codereview.chromium.org/2489283003 --- core/fpdfdoc/cpdf_formfield_unittest.cpp | 14 +++++------ core/fpdfdoc/cpdf_interform.cpp | 7 +++--- core/fpdfdoc/cpvt_generateap.cpp | 42 ++++++++++++-------------------- 3 files changed, 26 insertions(+), 37 deletions(-) (limited to 'core/fpdfdoc') diff --git a/core/fpdfdoc/cpdf_formfield_unittest.cpp b/core/fpdfdoc/cpdf_formfield_unittest.cpp index 11cccf19b8..f69df1d064 100644 --- a/core/fpdfdoc/cpdf_formfield_unittest.cpp +++ b/core/fpdfdoc/cpdf_formfield_unittest.cpp @@ -12,15 +12,13 @@ TEST(cpdf_formfield, FPDF_GetFullName) { EXPECT_TRUE(name.IsEmpty()); CPDF_IndirectObjectHolder obj_holder; - CPDF_Dictionary* root = new CPDF_Dictionary(); - obj_holder.AddIndirectObject(root); + CPDF_Dictionary* root = obj_holder.NewIndirect(); root->SetNameFor("T", "foo"); name = FPDF_GetFullName(root); EXPECT_STREQ("foo", name.UTF8Encode().c_str()); - CPDF_Dictionary* dict1 = new CPDF_Dictionary(); - root->SetReferenceFor("Parent", &obj_holder, - obj_holder.AddIndirectObject(dict1)); + CPDF_Dictionary* dict1 = obj_holder.NewIndirect(); + root->SetReferenceFor("Parent", &obj_holder, dict1); dict1->SetNameFor("T", "bar"); name = FPDF_GetFullName(root); EXPECT_STREQ("bar.foo", name.UTF8Encode().c_str()); @@ -30,9 +28,9 @@ TEST(cpdf_formfield, FPDF_GetFullName) { name = FPDF_GetFullName(root); EXPECT_STREQ("bar.foo", name.UTF8Encode().c_str()); - CPDF_Dictionary* dict3 = new CPDF_Dictionary(); - dict2->SetReferenceFor("Parent", &obj_holder, - obj_holder.AddIndirectObject(dict3)); + CPDF_Dictionary* dict3 = obj_holder.NewIndirect(); + dict2->SetReferenceFor("Parent", &obj_holder, dict3); + dict3->SetNameFor("T", "qux"); name = FPDF_GetFullName(root); EXPECT_STREQ("qux.bar.foo", name.UTF8Encode().c_str()); diff --git a/core/fpdfdoc/cpdf_interform.cpp b/core/fpdfdoc/cpdf_interform.cpp index 2a02e890c3..64d15a0d88 100644 --- a/core/fpdfdoc/cpdf_interform.cpp +++ b/core/fpdfdoc/cpdf_interform.cpp @@ -59,9 +59,10 @@ void InitDict(CPDF_Dictionary*& pFormDict, CPDF_Document* pDocument) { return; if (!pFormDict) { - pFormDict = new CPDF_Dictionary(pDocument->GetByteStringPool()); - pDocument->GetRoot()->SetReferenceFor( - "AcroForm", pDocument, pDocument->AddIndirectObject(pFormDict)); + pFormDict = + pDocument->NewIndirect(pDocument->GetByteStringPool()); + pDocument->GetRoot()->SetReferenceFor("AcroForm", pDocument, + pFormDict->GetObjNum()); } CFX_ByteString csDA; diff --git a/core/fpdfdoc/cpvt_generateap.cpp b/core/fpdfdoc/cpvt_generateap.cpp index 0802e907fd..c8bba64b1e 100644 --- a/core/fpdfdoc/cpvt_generateap.cpp +++ b/core/fpdfdoc/cpvt_generateap.cpp @@ -61,16 +61,12 @@ bool GenerateWidgetAP(CPDF_Document* pDoc, CPDF_Dictionary* pFontDict = pDRFontDict->GetDictFor(sFontName.Mid(1)); if (!pFontDict) { - auto pNewFontDict = - pdfium::MakeUnique(pDoc->GetByteStringPool()); - pNewFontDict->SetNameFor("Type", "Font"); - pNewFontDict->SetNameFor("Subtype", "Type1"); - pNewFontDict->SetNameFor("BaseFont", "Helvetica"); - pNewFontDict->SetNameFor("Encoding", "WinAnsiEncoding"); - // Ownership passes to AddIndirectObject(). - pFontDict = pNewFontDict.release(); - pDRFontDict->SetReferenceFor(sFontName.Mid(1), pDoc, - pDoc->AddIndirectObject(pFontDict)); + pFontDict = pDoc->NewIndirect(pDoc->GetByteStringPool()); + pFontDict->SetNameFor("Type", "Font"); + pFontDict->SetNameFor("Subtype", "Type1"); + pFontDict->SetNameFor("BaseFont", "Helvetica"); + pFontDict->SetNameFor("Encoding", "WinAnsiEncoding"); + pDRFontDict->SetReferenceFor(sFontName.Mid(1), pDoc, pFontDict); } CPDF_Font* pDefFont = pDoc->LoadFont(pFontDict); if (!pDefFont) @@ -177,10 +173,8 @@ bool GenerateWidgetAP(CPDF_Document* pDoc, } CPDF_Stream* pNormalStream = pAPDict->GetStreamFor("N"); if (!pNormalStream) { - auto pNewNormalStream = pdfium::MakeUnique(); - // Ownership passes to AddIndirectObject(). - pNormalStream = pNewNormalStream.release(); - pAPDict->SetReferenceFor("N", pDoc, pDoc->AddIndirectObject(pNormalStream)); + pNormalStream = pDoc->NewIndirect(); + pAPDict->SetReferenceFor("N", pDoc, pNormalStream); } CPDF_Dictionary* pStreamDict = pNormalStream->GetDict(); if (pStreamDict) { @@ -197,8 +191,7 @@ bool GenerateWidgetAP(CPDF_Document* pDoc, pStreamResList->SetFor("Font", pStreamResFontList); } if (!pStreamResFontList->KeyExist(sFontName)) - pStreamResFontList->SetReferenceFor(sFontName, pDoc, - pFontDict->GetObjNum()); + pStreamResFontList->SetReferenceFor(sFontName, pDoc, pFontDict); } else { pStreamDict->SetFor("Resources", pFormDict->GetDictFor("DR")->Clone().release()); @@ -452,8 +445,7 @@ bool GenerateWidgetAP(CPDF_Document* pDoc, pStreamResList->SetFor("Font", pStreamResFontList); } if (!pStreamResFontList->KeyExist(sFontName)) - pStreamResFontList->SetReferenceFor(sFontName, pDoc, - pFontDict->GetObjNum()); + pStreamResFontList->SetReferenceFor(sFontName, pDoc, pFontDict); } else { pStreamDict->SetFor("Resources", pFormDict->GetDictFor("DR")->Clone().release()); @@ -580,8 +572,8 @@ std::unique_ptr GenerateExtGStateDict( std::unique_ptr GenerateResourceFontDict( CPDF_Document* pDoc, const CFX_ByteString& sFontDictName) { - auto pFontDict = - pdfium::MakeUnique(pDoc->GetByteStringPool()); + CPDF_Dictionary* pFontDict = + pDoc->NewIndirect(pDoc->GetByteStringPool()); pFontDict->SetNameFor("Type", "Font"); pFontDict->SetNameFor("Subtype", "Type1"); pFontDict->SetNameFor("BaseFont", "Helvetica"); @@ -589,8 +581,7 @@ std::unique_ptr GenerateResourceFontDict( auto pResourceFontDict = pdfium::MakeUnique(pDoc->GetByteStringPool()); - pResourceFontDict->SetReferenceFor( - sFontDictName, pDoc, pDoc->AddIndirectObject(pFontDict.release())); + pResourceFontDict->SetReferenceFor(sFontDictName, pDoc, pFontDict); return pResourceFontDict; } @@ -612,15 +603,14 @@ void GenerateAndSetAPDict(CPDF_Document* pDoc, const CFX_ByteTextBuf& sAppStream, std::unique_ptr pResourceDict, bool bIsTextMarkupAnnotation) { - auto pNormalStream = pdfium::MakeUnique(); + CPDF_Stream* pNormalStream = pDoc->NewIndirect(); pNormalStream->SetData(sAppStream.GetBuffer(), sAppStream.GetSize()); - CPDF_Dictionary* pStreamDict = pNormalStream->GetDict(); auto pAPDict = pdfium::MakeUnique(pDoc->GetByteStringPool()); - pAPDict->SetReferenceFor("N", pDoc, - pDoc->AddIndirectObject(pNormalStream.release())); + pAPDict->SetReferenceFor("N", pDoc, pNormalStream); pAnnotDict->SetFor("AP", pAPDict.release()); + CPDF_Dictionary* pStreamDict = pNormalStream->GetDict(); pStreamDict->SetIntegerFor("FormType", 1); pStreamDict->SetStringFor("Subtype", "Form"); pStreamDict->SetMatrixFor("Matrix", CFX_Matrix()); -- cgit v1.2.3