From 69bbfa8fb536143e045a9dce3006ddd91785adbb Mon Sep 17 00:00:00 2001 From: thestig Date: Thu, 10 Nov 2016 10:39:01 -0800 Subject: Use more unique_ptrs in CPVT_GenerateAP. Review-Url: https://codereview.chromium.org/2488103002 --- core/fpdfdoc/cpvt_generateap.cpp | 185 +++++++++++++++++++++------------------ 1 file changed, 101 insertions(+), 84 deletions(-) diff --git a/core/fpdfdoc/cpvt_generateap.cpp b/core/fpdfdoc/cpvt_generateap.cpp index 93ef9dc366..0802e907fd 100644 --- a/core/fpdfdoc/cpvt_generateap.cpp +++ b/core/fpdfdoc/cpvt_generateap.cpp @@ -7,6 +7,8 @@ #include "core/fpdfdoc/cpvt_generateap.h" #include +#include +#include #include "core/fpdfapi/font/cpdf_font.h" #include "core/fpdfapi/parser/cpdf_dictionary.h" @@ -19,6 +21,7 @@ #include "core/fpdfdoc/cpvt_color.h" #include "core/fpdfdoc/cpvt_fontmap.h" #include "core/fpdfdoc/cpvt_word.h" +#include "third_party/base/ptr_util.h" namespace { @@ -58,11 +61,14 @@ bool GenerateWidgetAP(CPDF_Document* pDoc, CPDF_Dictionary* pFontDict = pDRFontDict->GetDictFor(sFontName.Mid(1)); if (!pFontDict) { - pFontDict = new CPDF_Dictionary(pDoc->GetByteStringPool()); - pFontDict->SetNameFor("Type", "Font"); - pFontDict->SetNameFor("Subtype", "Type1"); - pFontDict->SetNameFor("BaseFont", "Helvetica"); - pFontDict->SetNameFor("Encoding", "WinAnsiEncoding"); + 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)); } @@ -163,12 +169,17 @@ bool GenerateWidgetAP(CPDF_Document* pDoc, rcBody.Normalize(); CPDF_Dictionary* pAPDict = pAnnotDict->GetDictFor("AP"); if (!pAPDict) { - pAPDict = new CPDF_Dictionary(pDoc->GetByteStringPool()); + auto pNewAPDict = + pdfium::MakeUnique(pDoc->GetByteStringPool()); + // Ownership passes to |pAnnotDict|. + pAPDict = pNewAPDict.release(); pAnnotDict->SetFor("AP", pAPDict); } CPDF_Stream* pNormalStream = pAPDict->GetStreamFor("N"); if (!pNormalStream) { - pNormalStream = new CPDF_Stream; + auto pNewNormalStream = pdfium::MakeUnique(); + // Ownership passes to AddIndirectObject(). + pNormalStream = pNewNormalStream.release(); pAPDict->SetReferenceFor("N", pDoc, pDoc->AddIndirectObject(pNormalStream)); } CPDF_Dictionary* pStreamDict = pNormalStream->GetDict(); @@ -179,7 +190,10 @@ bool GenerateWidgetAP(CPDF_Document* pDoc, if (pStreamResList) { CPDF_Dictionary* pStreamResFontList = pStreamResList->GetDictFor("Font"); if (!pStreamResFontList) { - pStreamResFontList = new CPDF_Dictionary(pDoc->GetByteStringPool()); + auto pNewStreamResFontList = + pdfium::MakeUnique(pDoc->GetByteStringPool()); + // Ownership passes to |pStreamResList|. + pStreamResFontList = pNewStreamResFontList.release(); pStreamResList->SetFor("Font", pStreamResFontList); } if (!pStreamResFontList->KeyExist(sFontName)) @@ -431,7 +445,10 @@ bool GenerateWidgetAP(CPDF_Document* pDoc, CPDF_Dictionary* pStreamResFontList = pStreamResList->GetDictFor("Font"); if (!pStreamResFontList) { - pStreamResFontList = new CPDF_Dictionary(pDoc->GetByteStringPool()); + auto pNewStreamResFontList = + pdfium::MakeUnique(pDoc->GetByteStringPool()); + // Ownership passes to |pStreamResList|. + pStreamResFontList = pNewStreamResFontList.release(); pStreamResList->SetFor("Font", pStreamResFontList); } if (!pStreamResFontList->KeyExist(sFontName)) @@ -539,11 +556,12 @@ CFX_ByteString GetPopupContentsString(CPDF_Document* pDoc, return sAppStream.MakeString(); } -CPDF_Dictionary* GenerateExtGStateDict(const CPDF_Dictionary& pAnnotDict, - const CFX_ByteString& sExtGSDictName, - const CFX_ByteString& sBlendMode) { - CPDF_Dictionary* pGSDict = - new CPDF_Dictionary(pAnnotDict.GetByteStringPool()); +std::unique_ptr GenerateExtGStateDict( + const CPDF_Dictionary& pAnnotDict, + const CFX_ByteString& sExtGSDictName, + const CFX_ByteString& sBlendMode) { + auto pGSDict = + pdfium::MakeUnique(pAnnotDict.GetByteStringPool()); pGSDict->SetStringFor("Type", "ExtGState"); FX_FLOAT fOpacity = @@ -553,56 +571,56 @@ CPDF_Dictionary* GenerateExtGStateDict(const CPDF_Dictionary& pAnnotDict, pGSDict->SetBooleanFor("AIS", false); pGSDict->SetStringFor("BM", sBlendMode); - CPDF_Dictionary* pExtGStateDict = - new CPDF_Dictionary(pAnnotDict.GetByteStringPool()); - pExtGStateDict->SetFor(sExtGSDictName, pGSDict); + auto pExtGStateDict = + pdfium::MakeUnique(pAnnotDict.GetByteStringPool()); + pExtGStateDict->SetFor(sExtGSDictName, pGSDict.release()); return pExtGStateDict; } -CPDF_Dictionary* GenerateResourceFontDict(CPDF_Document* pDoc, - const CFX_ByteString& sFontDictName) { - CPDF_Dictionary* pFontDict = new CPDF_Dictionary(pDoc->GetByteStringPool()); +std::unique_ptr GenerateResourceFontDict( + CPDF_Document* pDoc, + const CFX_ByteString& sFontDictName) { + auto pFontDict = + pdfium::MakeUnique(pDoc->GetByteStringPool()); pFontDict->SetNameFor("Type", "Font"); pFontDict->SetNameFor("Subtype", "Type1"); pFontDict->SetNameFor("BaseFont", "Helvetica"); pFontDict->SetNameFor("Encoding", "WinAnsiEncoding"); - CPDF_Dictionary* pResourceFontDict = - new CPDF_Dictionary(pDoc->GetByteStringPool()); - pResourceFontDict->SetReferenceFor(sFontDictName, pDoc, - pDoc->AddIndirectObject(pFontDict)); + auto pResourceFontDict = + pdfium::MakeUnique(pDoc->GetByteStringPool()); + pResourceFontDict->SetReferenceFor( + sFontDictName, pDoc, pDoc->AddIndirectObject(pFontDict.release())); return pResourceFontDict; } -// Takes ownership of |pExtGStateDict| and |pResourceFontDict|. -CPDF_Dictionary* GenerateResourceDict(CPDF_Document* pDoc, - CPDF_Dictionary* pExtGStateDict, - CPDF_Dictionary* pResourceFontDict) { - CPDF_Dictionary* pResourceDict = - new CPDF_Dictionary(pDoc->GetByteStringPool()); +std::unique_ptr GenerateResourceDict( + CPDF_Document* pDoc, + std::unique_ptr pExtGStateDict, + std::unique_ptr pResourceFontDict) { + auto pResourceDict = + pdfium::MakeUnique(pDoc->GetByteStringPool()); if (pExtGStateDict) - pResourceDict->SetFor("ExtGState", pExtGStateDict); - + pResourceDict->SetFor("ExtGState", pExtGStateDict.release()); if (pResourceFontDict) - pResourceDict->SetFor("Font", pResourceFontDict); - + pResourceDict->SetFor("Font", pResourceFontDict.release()); return pResourceDict; } -// Takes ownership of |pResourceDict|. void GenerateAndSetAPDict(CPDF_Document* pDoc, CPDF_Dictionary* pAnnotDict, const CFX_ByteTextBuf& sAppStream, - CPDF_Dictionary* pResourceDict, + std::unique_ptr pResourceDict, bool bIsTextMarkupAnnotation) { - CPDF_Dictionary* pAPDict = new CPDF_Dictionary(pDoc->GetByteStringPool()); - pAnnotDict->SetFor("AP", pAPDict); - - CPDF_Stream* pNormalStream = new CPDF_Stream; + auto pNormalStream = pdfium::MakeUnique(); pNormalStream->SetData(sAppStream.GetBuffer(), sAppStream.GetSize()); - pAPDict->SetReferenceFor("N", pDoc, pDoc->AddIndirectObject(pNormalStream)); - CPDF_Dictionary* pStreamDict = pNormalStream->GetDict(); + + auto pAPDict = pdfium::MakeUnique(pDoc->GetByteStringPool()); + pAPDict->SetReferenceFor("N", pDoc, + pDoc->AddIndirectObject(pNormalStream.release())); + pAnnotDict->SetFor("AP", pAPDict.release()); + pStreamDict->SetIntegerFor("FormType", 1); pStreamDict->SetStringFor("Subtype", "Form"); pStreamDict->SetMatrixFor("Matrix", CFX_Matrix()); @@ -611,7 +629,7 @@ void GenerateAndSetAPDict(CPDF_Document* pDoc, ? CPDF_Annot::RectFromQuadPoints(pAnnotDict) : pAnnotDict->GetRectFor("Rect"); pStreamDict->SetRectFor("BBox", rect); - pStreamDict->SetFor("Resources", pResourceDict); + pStreamDict->SetFor("Resources", pResourceDict.release()); } CFX_ByteString GetPaintOperatorString(bool bIsStrokeRect, bool bIsFillRect) { @@ -791,11 +809,11 @@ bool CPVT_GenerateAP::GenerateCircleAP(CPDF_Document* pDoc, bool bIsFillRect = pInteriorColor && !pInteriorColor->IsEmpty(); sAppStream << GetPaintOperatorString(bIsStrokeRect, bIsFillRect) << "\n"; - CPDF_Dictionary* pExtGStateDict = + auto pExtGStateDict = GenerateExtGStateDict(*pAnnotDict, sExtGSDictName, "Normal"); - CPDF_Dictionary* pResourceDict = - GenerateResourceDict(pDoc, pExtGStateDict, nullptr); - GenerateAndSetAPDict(pDoc, pAnnotDict, sAppStream, pResourceDict, + auto pResourceDict = + GenerateResourceDict(pDoc, std::move(pExtGStateDict), nullptr); + GenerateAndSetAPDict(pDoc, pAnnotDict, sAppStream, std::move(pResourceDict), false /*IsTextMarkupAnnotation*/); return true; } @@ -818,11 +836,11 @@ bool CPVT_GenerateAP::GenerateHighlightAP(CPDF_Document* pDoc, << rect.left << " " << rect.bottom << " l " << "h f\n"; - CPDF_Dictionary* pExtGStateDict = + auto pExtGStateDict = GenerateExtGStateDict(*pAnnotDict, sExtGSDictName, "Multiply"); - CPDF_Dictionary* pResourceDict = - GenerateResourceDict(pDoc, pExtGStateDict, nullptr); - GenerateAndSetAPDict(pDoc, pAnnotDict, sAppStream, pResourceDict, + auto pResourceDict = + GenerateResourceDict(pDoc, std::move(pExtGStateDict), nullptr); + GenerateAndSetAPDict(pDoc, pAnnotDict, sAppStream, std::move(pResourceDict), true /*IsTextMarkupAnnotation*/); return true; @@ -873,11 +891,11 @@ bool CPVT_GenerateAP::GenerateInkAP(CPDF_Document* pDoc, sAppStream << "S\n"; } - CPDF_Dictionary* pExtGStateDict = + auto pExtGStateDict = GenerateExtGStateDict(*pAnnotDict, sExtGSDictName, "Normal"); - CPDF_Dictionary* pResourceDict = - GenerateResourceDict(pDoc, pExtGStateDict, nullptr); - GenerateAndSetAPDict(pDoc, pAnnotDict, sAppStream, pResourceDict, + auto pResourceDict = + GenerateResourceDict(pDoc, std::move(pExtGStateDict), nullptr); + GenerateAndSetAPDict(pDoc, pAnnotDict, sAppStream, std::move(pResourceDict), false /*IsTextMarkupAnnotation*/); return true; } @@ -896,11 +914,11 @@ bool CPVT_GenerateAP::GenerateTextAP(CPDF_Document* pDoc, sAppStream << GenerateTextSymbolAP(noteRect); - CPDF_Dictionary* pExtGStateDict = + auto pExtGStateDict = GenerateExtGStateDict(*pAnnotDict, sExtGSDictName, "Normal"); - CPDF_Dictionary* pResourceDict = - GenerateResourceDict(pDoc, pExtGStateDict, nullptr); - GenerateAndSetAPDict(pDoc, pAnnotDict, sAppStream, pResourceDict, + auto pResourceDict = + GenerateResourceDict(pDoc, std::move(pExtGStateDict), nullptr); + GenerateAndSetAPDict(pDoc, pAnnotDict, sAppStream, std::move(pResourceDict), false /*IsTextMarkupAnnotation*/); return true; } @@ -923,11 +941,11 @@ bool CPVT_GenerateAP::GenerateUnderlineAP(CPDF_Document* pDoc, << rect.bottom + fLineWidth << " m " << rect.right << " " << rect.bottom + fLineWidth << " l S\n"; - CPDF_Dictionary* pExtGStateDict = + auto pExtGStateDict = GenerateExtGStateDict(*pAnnotDict, sExtGSDictName, "Normal"); - CPDF_Dictionary* pResourceDict = - GenerateResourceDict(pDoc, pExtGStateDict, nullptr); - GenerateAndSetAPDict(pDoc, pAnnotDict, sAppStream, pResourceDict, + auto pResourceDict = + GenerateResourceDict(pDoc, std::move(pExtGStateDict), nullptr); + GenerateAndSetAPDict(pDoc, pAnnotDict, sAppStream, std::move(pResourceDict), true /*IsTextMarkupAnnotation*/); return true; } @@ -954,19 +972,18 @@ bool CPVT_GenerateAP::GeneratePopupAP(CPDF_Document* pDoc, << rect.Height() << " re b\n"; CFX_ByteString sFontName = "FONT"; - CPDF_Dictionary* pExtGStateDict = - GenerateExtGStateDict(*pAnnotDict, sExtGSDictName, "Normal"); - CPDF_Dictionary* pResourceFontDict = - GenerateResourceFontDict(pDoc, sFontName); - CPDF_Dictionary* pResourceDict = - GenerateResourceDict(pDoc, pResourceFontDict, pExtGStateDict); - - CPDF_Font* pDefFont = pDoc->LoadFont(pResourceFontDict); + auto pResourceFontDict = GenerateResourceFontDict(pDoc, sFontName); + CPDF_Font* pDefFont = pDoc->LoadFont(pResourceFontDict.get()); if (!pDefFont) return false; + auto pExtGStateDict = + GenerateExtGStateDict(*pAnnotDict, sExtGSDictName, "Normal"); + auto pResourceDict = GenerateResourceDict(pDoc, std::move(pResourceFontDict), + std::move(pExtGStateDict)); + sAppStream << GetPopupContentsString(pDoc, *pAnnotDict, pDefFont, sFontName); - GenerateAndSetAPDict(pDoc, pAnnotDict, sAppStream, pResourceDict, + GenerateAndSetAPDict(pDoc, pAnnotDict, sAppStream, std::move(pResourceDict), false /*IsTextMarkupAnnotation*/); return true; } @@ -1010,11 +1027,11 @@ bool CPVT_GenerateAP::GenerateSquareAP(CPDF_Document* pDoc, << rect.Height() << " re " << GetPaintOperatorString(bIsStrokeRect, bIsFillRect) << "\n"; - CPDF_Dictionary* pExtGStateDict = + auto pExtGStateDict = GenerateExtGStateDict(*pAnnotDict, sExtGSDictName, "Normal"); - CPDF_Dictionary* pResourceDict = - GenerateResourceDict(pDoc, pExtGStateDict, nullptr); - GenerateAndSetAPDict(pDoc, pAnnotDict, sAppStream, pResourceDict, + auto pResourceDict = + GenerateResourceDict(pDoc, std::move(pExtGStateDict), nullptr); + GenerateAndSetAPDict(pDoc, pAnnotDict, sAppStream, std::move(pResourceDict), false /*IsTextMarkupAnnotation*/); return true; } @@ -1059,11 +1076,11 @@ bool CPVT_GenerateAP::GenerateSquigglyAP(CPDF_Document* pDoc, sAppStream << "S\n"; - CPDF_Dictionary* pExtGStateDict = + auto pExtGStateDict = GenerateExtGStateDict(*pAnnotDict, sExtGSDictName, "Normal"); - CPDF_Dictionary* pResourceDict = - GenerateResourceDict(pDoc, pExtGStateDict, nullptr); - GenerateAndSetAPDict(pDoc, pAnnotDict, sAppStream, pResourceDict, + auto pResourceDict = + GenerateResourceDict(pDoc, std::move(pExtGStateDict), nullptr); + GenerateAndSetAPDict(pDoc, pAnnotDict, sAppStream, std::move(pResourceDict), true /*IsTextMarkupAnnotation*/); return true; } @@ -1086,11 +1103,11 @@ bool CPVT_GenerateAP::GenerateStrikeOutAP(CPDF_Document* pDoc, sAppStream << fLineWidth << " w " << rect.left << " " << fY << " m " << rect.right << " " << fY << " l S\n"; - CPDF_Dictionary* pExtGStateDict = + auto pExtGStateDict = GenerateExtGStateDict(*pAnnotDict, sExtGSDictName, "Normal"); - CPDF_Dictionary* pResourceDict = - GenerateResourceDict(pDoc, pExtGStateDict, nullptr); - GenerateAndSetAPDict(pDoc, pAnnotDict, sAppStream, pResourceDict, + auto pResourceDict = + GenerateResourceDict(pDoc, std::move(pExtGStateDict), nullptr); + GenerateAndSetAPDict(pDoc, pAnnotDict, sAppStream, std::move(pResourceDict), true /*IsTextMarkupAnnotation*/); return true; } -- cgit v1.2.3