From f3480661a20c259581e7c100b0d5d06950a9559b Mon Sep 17 00:00:00 2001 From: thestig Date: Thu, 10 Nov 2016 10:47:21 -0800 Subject: Use more unique_ptrs for CPDF_Annot and friends. Review-Url: https://codereview.chromium.org/2494583002 --- core/fpdfdoc/cpdf_annot.cpp | 80 ++++++++++++++++++++++------------------- core/fpdfdoc/cpdf_annot.h | 9 +++-- core/fpdfdoc/cpdf_annotlist.cpp | 12 +++---- 3 files changed, 55 insertions(+), 46 deletions(-) diff --git a/core/fpdfdoc/cpdf_annot.cpp b/core/fpdfdoc/cpdf_annot.cpp index 2f3fc804f3..a213f51a6d 100644 --- a/core/fpdfdoc/cpdf_annot.cpp +++ b/core/fpdfdoc/cpdf_annot.cpp @@ -39,20 +39,36 @@ bool ShouldGenerateAPForAnnotation(CPDF_Dictionary* pAnnotDict) { return !CPDF_Annot::IsAnnotationHidden(pAnnotDict); } +CPDF_Form* AnnotGetMatrix(const CPDF_Page* pPage, + CPDF_Annot* pAnnot, + CPDF_Annot::AppearanceMode mode, + const CFX_Matrix* pUser2Device, + CFX_Matrix* matrix) { + CPDF_Form* pForm = pAnnot->GetAPForm(pPage, mode); + if (!pForm) + return nullptr; + + CFX_FloatRect form_bbox = pForm->m_pFormDict->GetRectFor("BBox"); + CFX_Matrix form_matrix = pForm->m_pFormDict->GetMatrixFor("Matrix"); + form_matrix.TransformRect(form_bbox); + matrix->MatchRect(pAnnot->GetRect(), form_bbox); + matrix->Concat(*pUser2Device); + return pForm; +} + } // namespace -CPDF_Annot::CPDF_Annot(CPDF_Dictionary* pDict, - CPDF_Document* pDocument, - bool bToOwnDict) - : m_bOwnedAnnotDict(bToOwnDict), - m_pAnnotDict(pDict), - m_pDocument(pDocument), - m_bOpenState(false), - m_pPopupAnnot(nullptr) { - m_nSubtype = StringToAnnotSubtype(m_pAnnotDict->GetStringFor("Subtype")); - m_bIsTextMarkupAnnotation = IsTextMarkupAnnotation(m_nSubtype); - m_bHasGeneratedAP = m_pAnnotDict->GetBooleanFor(kPDFiumKey_HasGeneratedAP); - GenerateAPIfNeeded(); +CPDF_Annot::CPDF_Annot(std::unique_ptr pDict, + CPDF_Document* pDocument) + : m_bOwnedAnnotDict(true), + m_pAnnotDict(pDict.release()), + m_pDocument(pDocument) { + Init(); +} + +CPDF_Annot::CPDF_Annot(CPDF_Dictionary* pDict, CPDF_Document* pDocument) + : m_bOwnedAnnotDict(false), m_pAnnotDict(pDict), m_pDocument(pDocument) { + Init(); } CPDF_Annot::~CPDF_Annot() { @@ -61,6 +77,13 @@ CPDF_Annot::~CPDF_Annot() { ClearCachedAP(); } +void CPDF_Annot::Init() { + m_nSubtype = StringToAnnotSubtype(m_pAnnotDict->GetStringFor("Subtype")); + m_bIsTextMarkupAnnotation = IsTextMarkupAnnotation(m_nSubtype); + m_bHasGeneratedAP = m_pAnnotDict->GetBooleanFor(kPDFiumKey_HasGeneratedAP); + GenerateAPIfNeeded(); +} + void CPDF_Annot::GenerateAPIfNeeded() { if (!ShouldGenerateAPForAnnotation(m_pAnnotDict)) return; @@ -188,23 +211,6 @@ CPDF_Form* CPDF_Annot::GetAPForm(const CPDF_Page* pPage, AppearanceMode mode) { return pNewForm; } -static CPDF_Form* FPDFDOC_Annot_GetMatrix(const CPDF_Page* pPage, - CPDF_Annot* pAnnot, - CPDF_Annot::AppearanceMode mode, - const CFX_Matrix* pUser2Device, - CFX_Matrix& matrix) { - CPDF_Form* pForm = pAnnot->GetAPForm(pPage, mode); - if (!pForm) { - return nullptr; - } - CFX_FloatRect form_bbox = pForm->m_pFormDict->GetRectFor("BBox"); - CFX_Matrix form_matrix = pForm->m_pFormDict->GetMatrixFor("Matrix"); - form_matrix.TransformRect(form_bbox); - matrix.MatchRect(pAnnot->GetRect(), form_bbox); - matrix.Concat(*pUser2Device); - return pForm; -} - // Static. CFX_FloatRect CPDF_Annot::RectFromQuadPoints(CPDF_Dictionary* pAnnotDict) { CPDF_Array* pArray = pAnnotDict->GetArrayFor("QuadPoints"); @@ -365,16 +371,16 @@ bool CPDF_Annot::DrawAppearance(CPDF_Page* pPage, GenerateAPIfNeeded(); CFX_Matrix matrix; - CPDF_Form* pForm = - FPDFDOC_Annot_GetMatrix(pPage, this, mode, pUser2Device, matrix); - if (!pForm) { + CPDF_Form* pForm = AnnotGetMatrix(pPage, this, mode, pUser2Device, &matrix); + if (!pForm) return false; - } + CPDF_RenderContext context(pPage); context.AppendLayer(pForm, &matrix); context.Render(pDevice, pOptions, nullptr); return true; } + bool CPDF_Annot::DrawInContext(const CPDF_Page* pPage, CPDF_RenderContext* pContext, const CFX_Matrix* pUser2Device, @@ -390,14 +396,14 @@ bool CPDF_Annot::DrawInContext(const CPDF_Page* pPage, GenerateAPIfNeeded(); CFX_Matrix matrix; - CPDF_Form* pForm = - FPDFDOC_Annot_GetMatrix(pPage, this, mode, pUser2Device, matrix); - if (!pForm) { + CPDF_Form* pForm = AnnotGetMatrix(pPage, this, mode, pUser2Device, &matrix); + if (!pForm) return false; - } + pContext->AppendLayer(pForm, &matrix); return true; } + void CPDF_Annot::DrawBorder(CFX_RenderDevice* pDevice, const CFX_Matrix* pUser2Device, const CPDF_RenderOptions* pOptions) { diff --git a/core/fpdfdoc/cpdf_annot.h b/core/fpdfdoc/cpdf_annot.h index e5404a8c9e..2dcddb0ef8 100644 --- a/core/fpdfdoc/cpdf_annot.h +++ b/core/fpdfdoc/cpdf_annot.h @@ -73,7 +73,9 @@ class CPDF_Annot { static CFX_ByteString AnnotSubtypeToString(CPDF_Annot::Subtype nSubtype); static CFX_FloatRect RectFromQuadPoints(CPDF_Dictionary* pAnnotDict); - CPDF_Annot(CPDF_Dictionary* pDict, CPDF_Document* pDocument, bool bToOwnDict); + // The second constructor does not take ownership of the dictionary. + CPDF_Annot(std::unique_ptr pDict, CPDF_Document* pDocument); + CPDF_Annot(CPDF_Dictionary* pDict, CPDF_Document* pDocument); ~CPDF_Annot(); CPDF_Annot::Subtype GetSubtype() const; @@ -103,6 +105,7 @@ class CPDF_Annot { void SetPopupAnnot(CPDF_Annot* pAnnot) { m_pPopupAnnot = pAnnot; } private: + void Init(); void GenerateAPIfNeeded(); bool ShouldDrawAnnotation(); @@ -117,12 +120,12 @@ class CPDF_Annot { CPDF_Annot::Subtype m_nSubtype; std::map> m_APMap; // |m_bOpenState| is only set for popup annotations. - bool m_bOpenState; + bool m_bOpenState = false; bool m_bHasGeneratedAP; bool m_bIsTextMarkupAnnotation; // Not owned. If there is a valid pointer in |m_pPopupAnnot|, // then this annot is never a popup. - CPDF_Annot* m_pPopupAnnot; + CPDF_Annot* m_pPopupAnnot = nullptr; }; CPDF_Stream* FPDFDOC_GetAnnotAP(CPDF_Dictionary* pAnnotDict, diff --git a/core/fpdfdoc/cpdf_annotlist.cpp b/core/fpdfdoc/cpdf_annotlist.cpp index e89b47705e..91cff454e3 100644 --- a/core/fpdfdoc/cpdf_annotlist.cpp +++ b/core/fpdfdoc/cpdf_annotlist.cpp @@ -18,6 +18,7 @@ #include "core/fpdfdoc/cpdf_occontext.h" #include "core/fpdfdoc/cpvt_generateap.h" #include "core/fxge/cfx_renderdevice.h" +#include "third_party/base/ptr_util.h" namespace { @@ -33,8 +34,8 @@ std::unique_ptr CreatePopupAnnot(CPDF_Annot* pAnnot, if (sContents.IsEmpty()) return nullptr; - CPDF_Dictionary* pAnnotDict = - new CPDF_Dictionary(pDocument->GetByteStringPool()); + auto pAnnotDict = + pdfium::MakeUnique(pDocument->GetByteStringPool()); pAnnotDict->SetNameFor("Type", "Annot"); pAnnotDict->SetNameFor("Subtype", "Popup"); pAnnotDict->SetStringFor("T", pParentDict->GetStringFor("T")); @@ -48,8 +49,8 @@ std::unique_ptr CreatePopupAnnot(CPDF_Annot* pAnnot, pAnnotDict->SetRectFor("Rect", popupRect); pAnnotDict->SetIntegerFor("F", 0); - std::unique_ptr pPopupAnnot( - new CPDF_Annot(pAnnotDict, pDocument, true)); + auto pPopupAnnot = + pdfium::MakeUnique(std::move(pAnnotDict), pDocument); pAnnot->SetPopupAnnot(pPopupAnnot.get()); return pPopupAnnot; } @@ -79,8 +80,7 @@ CPDF_AnnotList::CPDF_AnnotList(CPDF_Page* pPage) continue; } pAnnots->ConvertToIndirectObjectAt(i, m_pDocument); - m_AnnotList.push_back( - std::unique_ptr(new CPDF_Annot(pDict, m_pDocument, false))); + m_AnnotList.push_back(pdfium::MakeUnique(pDict, m_pDocument)); if (bRegenerateAP && subtype == "Widget" && CPDF_InterForm::IsUpdateAPEnabled()) { FPDF_GenerateAP(m_pDocument, pDict); -- cgit v1.2.3