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 --- fpdfsdk/cpdfsdk_baannot.cpp | 10 +++++----- fpdfsdk/formfiller/cba_fontmap.cpp | 13 +++++-------- fpdfsdk/fpdf_flatten.cpp | 15 +++++++-------- fpdfsdk/fpdf_transformpage.cpp | 20 ++++++++------------ fpdfsdk/fpdfdoc_unittest.cpp | 4 ++-- fpdfsdk/fpdfppo.cpp | 16 ++++++++-------- fpdfsdk/fpdfsave.cpp | 16 ++++++---------- 7 files changed, 41 insertions(+), 53 deletions(-) (limited to 'fpdfsdk') diff --git a/fpdfsdk/cpdfsdk_baannot.cpp b/fpdfsdk/cpdfsdk_baannot.cpp index 0e6c53cc78..9436b3ba62 100644 --- a/fpdfsdk/cpdfsdk_baannot.cpp +++ b/fpdfsdk/cpdfsdk_baannot.cpp @@ -316,10 +316,9 @@ void CPDFSDK_BAAnnot::WriteAppearance(const CFX_ByteString& sAPType, } if (!pStream) { - pStream = new CPDF_Stream; CPDF_Document* pDoc = m_pPageView->GetPDFDocument(); - pParentDict->SetReferenceFor(sAPType, pDoc, - pDoc->AddIndirectObject(pStream)); + pStream = pDoc->NewIndirect(); + pParentDict->SetReferenceFor(sAPType, pDoc, pStream); } CPDF_Dictionary* pStreamDict = pStream->GetDict(); @@ -354,8 +353,9 @@ void CPDFSDK_BAAnnot::SetAction(const CPDF_Action& action) { CPDF_Dictionary* pDict = action.GetDict(); if (pDict != m_pAnnot->GetAnnotDict()->GetDictFor("A")) { CPDF_Document* pDoc = m_pPageView->GetPDFDocument(); - m_pAnnot->GetAnnotDict()->SetReferenceFor("A", pDoc, - pDoc->AddIndirectObject(pDict)); + if (pDict->IsInline()) + pDict = pDoc->AddIndirectObject(pDict->Clone())->AsDictionary(); + m_pAnnot->GetAnnotDict()->SetReferenceFor("A", pDoc, pDict); } } diff --git a/fpdfsdk/formfiller/cba_fontmap.cpp b/fpdfsdk/formfiller/cba_fontmap.cpp index 26feaf765d..2d962ad276 100644 --- a/fpdfsdk/formfiller/cba_fontmap.cpp +++ b/fpdfsdk/formfiller/cba_fontmap.cpp @@ -166,9 +166,8 @@ void CBA_FontMap::AddFontToAnnotDict(CPDF_Font* pFont, CPDF_Stream* pStream = pAPDict->GetStreamFor(m_sAPType); if (!pStream) { - pStream = new CPDF_Stream; - pAPDict->SetReferenceFor(m_sAPType, m_pDocument, - m_pDocument->AddIndirectObject(pStream)); + pStream = m_pDocument->NewIndirect(); + pAPDict->SetReferenceFor(m_sAPType, m_pDocument, pStream); } CPDF_Dictionary* pStreamDict = pStream->GetDict(); @@ -185,11 +184,9 @@ void CBA_FontMap::AddFontToAnnotDict(CPDF_Font* pFont, } CPDF_Dictionary* pStreamResFontList = pStreamResList->GetDictFor("Font"); if (!pStreamResFontList) { - pStreamResFontList = - new CPDF_Dictionary(m_pDocument->GetByteStringPool()); - pStreamResList->SetReferenceFor( - "Font", m_pDocument, - m_pDocument->AddIndirectObject(pStreamResFontList)); + pStreamResFontList = m_pDocument->NewIndirect( + m_pDocument->GetByteStringPool()); + pStreamResList->SetReferenceFor("Font", m_pDocument, pStreamResFontList); } if (!pStreamResFontList->KeyExist(sAlias)) { pStreamResFontList->SetReferenceFor(sAlias, m_pDocument, diff --git a/fpdfsdk/fpdf_flatten.cpp b/fpdfsdk/fpdf_flatten.cpp index 1b93bb4d88..b11e4a621d 100644 --- a/fpdfsdk/fpdf_flatten.cpp +++ b/fpdfsdk/fpdf_flatten.cpp @@ -172,12 +172,12 @@ CFX_FloatRect CalculateRect(std::vector* pRectArray) { uint32_t NewIndirectContentsStream(const CFX_ByteString& key, CPDF_Document* pDocument) { - CPDF_Stream* pNewContents = new CPDF_Stream( + CPDF_Stream* pNewContents = pDocument->NewIndirect( nullptr, 0, new CPDF_Dictionary(pDocument->GetByteStringPool())); CFX_ByteString sStream; sStream.Format("q 1 0 0 1 0 0 cm /%s Do Q", key.c_str()); pNewContents->SetData(sStream.raw_str(), sStream.GetLength()); - return pDocument->AddIndirectObject(pNewContents); + return pNewContents->GetObjNum(); } void SetPageContents(const CFX_ByteString& key, @@ -197,7 +197,7 @@ void SetPageContents(const CFX_ByteString& key, } pPage->ConvertToIndirectObjectFor("Contents", pDocument); if (!pContentsArray) { - pContentsArray = new CPDF_Array; + pContentsArray = pDocument->NewIndirect(); CPDF_StreamAcc acc; acc.LoadAllData(pContentsStream); CFX_ByteString sStream = "q\n"; @@ -206,8 +206,7 @@ void SetPageContents(const CFX_ByteString& key, sStream = sStream + sBody + "\nQ"; pContentsStream->SetData(sStream.raw_str(), sStream.GetLength()); pContentsArray->AddReference(pDocument, pContentsStream->GetObjNum()); - pPage->SetReferenceFor("Contents", pDocument, - pDocument->AddIndirectObject(pContentsArray)); + pPage->SetReferenceFor("Contents", pDocument, pContentsArray); } if (!key.IsEmpty()) { pContentsArray->AddReference(pDocument, @@ -293,10 +292,10 @@ DLLEXPORT int STDCALL FPDFPage_Flatten(FPDF_PAGE page, int nFlag) { pPageDict->SetFor("Resources", pRes); } - CPDF_Stream* pNewXObject = new CPDF_Stream( + CPDF_Stream* pNewXObject = pDocument->NewIndirect( nullptr, 0, new CPDF_Dictionary(pDocument->GetByteStringPool())); - uint32_t dwObjNum = pDocument->AddIndirectObject(pNewXObject); + uint32_t dwObjNum = pNewXObject->GetObjNum(); CPDF_Dictionary* pPageXObject = pRes->GetDictFor("XObject"); if (!pPageXObject) { pPageXObject = new CPDF_Dictionary(pDocument->GetByteStringPool()); @@ -383,7 +382,7 @@ DLLEXPORT int STDCALL FPDFPage_Flatten(FPDF_PAGE page, int nFlag) { if (pObj->IsInline()) { std::unique_ptr pNew = pObj->Clone(); pObj = pNew.get(); - pDocument->AddIndirectObject(pNew.release()); + pDocument->AddIndirectObject(std::move(pNew)); } CPDF_Dictionary* pObjDic = pObj->GetDict(); diff --git a/fpdfsdk/fpdf_transformpage.cpp b/fpdfsdk/fpdf_transformpage.cpp index 63a46dfe7a..76be4de092 100644 --- a/fpdfsdk/fpdf_transformpage.cpp +++ b/fpdfsdk/fpdf_transformpage.cpp @@ -130,14 +130,13 @@ DLLEXPORT FPDF_BOOL STDCALL FPDFPage_TransFormWithClip(FPDF_PAGE page, return false; CPDF_Dictionary* pDic = new CPDF_Dictionary(pDoc->GetByteStringPool()); - CPDF_Stream* pStream = new CPDF_Stream(nullptr, 0, pDic); + CPDF_Stream* pStream = pDoc->NewIndirect(nullptr, 0, pDic); pStream->SetData(textBuf.GetBuffer(), textBuf.GetSize()); - pDoc->AddIndirectObject(pStream); + pDic = new CPDF_Dictionary(pDoc->GetByteStringPool()); - CPDF_Stream* pEndStream = new CPDF_Stream(nullptr, 0, pDic); + CPDF_Stream* pEndStream = pDoc->NewIndirect(nullptr, 0, pDic); pEndStream->SetData((const uint8_t*)" Q", 2); - pDoc->AddIndirectObject(pEndStream); CPDF_Array* pContentArray = nullptr; CPDF_Array* pArray = ToArray(pContentObj); @@ -156,12 +155,11 @@ DLLEXPORT FPDF_BOOL STDCALL FPDFPage_TransFormWithClip(FPDF_PAGE page, pContentArray->InsertAt(0, pRef); pContentArray->AddReference(pDoc, pEndStream->GetObjNum()); } else if (pDirectObj->IsStream()) { - pContentArray = new CPDF_Array(); + pContentArray = pDoc->NewIndirect(); pContentArray->AddReference(pDoc, pStream->GetObjNum()); pContentArray->AddReference(pDoc, pDirectObj->GetObjNum()); pContentArray->AddReference(pDoc, pEndStream->GetObjNum()); - pPageDic->SetReferenceFor("Contents", pDoc, - pDoc->AddIndirectObject(pContentArray)); + pPageDic->SetReferenceFor("Contents", pDoc, pContentArray); } } } @@ -306,9 +304,8 @@ DLLEXPORT void STDCALL FPDFPage_InsertClipPath(FPDF_PAGE page, return; CPDF_Dictionary* pDic = new CPDF_Dictionary(pDoc->GetByteStringPool()); - CPDF_Stream* pStream = new CPDF_Stream(nullptr, 0, pDic); + CPDF_Stream* pStream = pDoc->NewIndirect(nullptr, 0, pDic); pStream->SetData(strClip.GetBuffer(), strClip.GetSize()); - pDoc->AddIndirectObject(pStream); CPDF_Array* pContentArray = nullptr; CPDF_Array* pArray = ToArray(pContentObj); @@ -325,11 +322,10 @@ DLLEXPORT void STDCALL FPDFPage_InsertClipPath(FPDF_PAGE page, CPDF_Reference* pRef = new CPDF_Reference(pDoc, pStream->GetObjNum()); pContentArray->InsertAt(0, pRef); } else if (pDirectObj->IsStream()) { - pContentArray = new CPDF_Array(); + pContentArray = pDoc->NewIndirect(); pContentArray->AddReference(pDoc, pStream->GetObjNum()); pContentArray->AddReference(pDoc, pDirectObj->GetObjNum()); - pPageDic->SetReferenceFor("Contents", pDoc, - pDoc->AddIndirectObject(pContentArray)); + pPageDic->SetReferenceFor("Contents", pDoc, pContentArray); } } } diff --git a/fpdfsdk/fpdfdoc_unittest.cpp b/fpdfsdk/fpdfdoc_unittest.cpp index fc902570b4..7d1d6b4ba8 100644 --- a/fpdfsdk/fpdfdoc_unittest.cpp +++ b/fpdfsdk/fpdfdoc_unittest.cpp @@ -81,8 +81,8 @@ class PDFDocTest : public testing::Test { std::vector info; for (int i = 0; i < num; ++i) { // Objects created will be released by the document. - CPDF_Dictionary* obj = new CPDF_Dictionary(); - info.push_back({m_pIndirectObjs->AddIndirectObject(obj), obj}); + CPDF_Dictionary* obj = m_pIndirectObjs->NewIndirect(); + info.push_back({obj->GetObjNum(), obj}); } return info; } diff --git a/fpdfsdk/fpdfppo.cpp b/fpdfsdk/fpdfppo.cpp index 705c7e21da..610aae7abd 100644 --- a/fpdfsdk/fpdfppo.cpp +++ b/fpdfsdk/fpdfppo.cpp @@ -170,9 +170,9 @@ bool CPDF_PageOrganizer::PDFDocInit() { CPDF_Dictionary* pNewPages = pElement ? ToDictionary(pElement->GetDirect()) : nullptr; if (!pNewPages) { - pNewPages = new CPDF_Dictionary(m_pDestPDFDoc->GetByteStringPool()); - pNewRoot->SetReferenceFor("Pages", m_pDestPDFDoc, - m_pDestPDFDoc->AddIndirectObject(pNewPages)); + pNewPages = m_pDestPDFDoc->NewIndirect( + m_pDestPDFDoc->GetByteStringPool()); + pNewRoot->SetReferenceFor("Pages", m_pDestPDFDoc, pNewPages); } CFX_ByteString cbPageType = pNewPages->GetStringFor("Type", ""); @@ -181,9 +181,8 @@ bool CPDF_PageOrganizer::PDFDocInit() { if (!pNewPages->GetArrayFor("Kids")) { pNewPages->SetIntegerFor("Count", 0); - pNewPages->SetReferenceFor( - "Kids", m_pDestPDFDoc, - m_pDestPDFDoc->AddIndirectObject(new CPDF_Array)); + pNewPages->SetReferenceFor("Kids", m_pDestPDFDoc, + m_pDestPDFDoc->NewIndirect()); } return true; @@ -331,8 +330,9 @@ uint32_t CPDF_PageOrganizer::GetNewObjId(ObjectNumberMap* pObjNumberMap, return 0; } } - CPDF_Object* pUnownedClone = pClone.get(); - dwNewObjNum = m_pDestPDFDoc->AddIndirectObject(pClone.release()); + CPDF_Object* pUnownedClone = + m_pDestPDFDoc->AddIndirectObject(std::move(pClone)); + dwNewObjNum = pUnownedClone->GetObjNum(); (*pObjNumberMap)[dwObjnum] = dwNewObjNum; if (!UpdateReference(pUnownedClone, pObjNumberMap)) return 0; diff --git a/fpdfsdk/fpdfsave.cpp b/fpdfsdk/fpdfsave.cpp index 1b639b7cc4..e5d7a409ae 100644 --- a/fpdfsdk/fpdfsave.cpp +++ b/fpdfsdk/fpdfsave.cpp @@ -180,14 +180,12 @@ bool SaveXFADocumentData(CPDFXFA_Context* pContext, if (pDataSetsStream) pDataSetsStream->InitStreamFromFile(pDsfileWrite.get(), pDataDict); } else { - CPDF_Stream* pData = new CPDF_Stream; + CPDF_Stream* pData = pPDFDocument->NewIndirect(); pData->InitStreamFromFile(pDsfileWrite.get(), pDataDict); iLast = pArray->GetCount() - 2; pArray->InsertAt(iLast, new CPDF_String("datasets", false)); - pArray->InsertAt( - iLast + 1, - new CPDF_Reference(pPDFDocument, - pPDFDocument->AddIndirectObject(pData))); + pArray->InsertAt(iLast + 1, + new CPDF_Reference(pPDFDocument, pData->GetObjNum())); } fileList->push_back(std::move(pDsfileWrite)); } @@ -204,14 +202,12 @@ bool SaveXFADocumentData(CPDFXFA_Context* pContext, if (pFormStream) pFormStream->InitStreamFromFile(pfileWrite.get(), pDataDict); } else { - CPDF_Stream* pData = new CPDF_Stream; + CPDF_Stream* pData = pPDFDocument->NewIndirect(); pData->InitStreamFromFile(pfileWrite.get(), pDataDict); iLast = pArray->GetCount() - 2; pArray->InsertAt(iLast, new CPDF_String("form", false)); - pArray->InsertAt( - iLast + 1, - new CPDF_Reference(pPDFDocument, - pPDFDocument->AddIndirectObject(pData))); + pArray->InsertAt(iLast + 1, + new CPDF_Reference(pPDFDocument, pData->GetObjNum())); } fileList->push_back(std::move(pfileWrite)); } -- cgit v1.2.3