From 673b90ad5b21a3d17e64ef65d7053d582920fcbe Mon Sep 17 00:00:00 2001 From: Henrique Nakashima Date: Wed, 10 Oct 2018 23:17:03 +0000 Subject: Rename CPDF_ContentMark to CPDF_ContentMarks. Also change variable names and member names of this type. This better reflects the fact that this class contains all the marks in a page objects, not just one mark. Change-Id: I4fe3d2620e78cbe423f18634f19fa82530d7efe1 Reviewed-on: https://pdfium-review.googlesource.com/c/43813 Reviewed-by: Lei Zhang Commit-Queue: Henrique Nakashima --- core/fpdfapi/page/cpdf_contentmark.cpp | 171 ------------------------ core/fpdfapi/page/cpdf_contentmark.h | 72 ----------- core/fpdfapi/page/cpdf_contentmarks.cpp | 172 +++++++++++++++++++++++++ core/fpdfapi/page/cpdf_contentmarks.h | 72 +++++++++++ core/fpdfapi/page/cpdf_pageobject.h | 4 +- core/fpdfapi/page/cpdf_streamcontentparser.cpp | 8 +- core/fpdfapi/page/cpdf_streamcontentparser.h | 4 +- 7 files changed, 252 insertions(+), 251 deletions(-) delete mode 100644 core/fpdfapi/page/cpdf_contentmark.cpp delete mode 100644 core/fpdfapi/page/cpdf_contentmark.h create mode 100644 core/fpdfapi/page/cpdf_contentmarks.cpp create mode 100644 core/fpdfapi/page/cpdf_contentmarks.h (limited to 'core/fpdfapi/page') diff --git a/core/fpdfapi/page/cpdf_contentmark.cpp b/core/fpdfapi/page/cpdf_contentmark.cpp deleted file mode 100644 index c1cdf392e0..0000000000 --- a/core/fpdfapi/page/cpdf_contentmark.cpp +++ /dev/null @@ -1,171 +0,0 @@ -// Copyright 2016 PDFium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com - -#include "core/fpdfapi/page/cpdf_contentmark.h" - -#include -#include - -#include "core/fpdfapi/parser/cpdf_dictionary.h" -#include "third_party/base/ptr_util.h" - -CPDF_ContentMark::CPDF_ContentMark() {} - -CPDF_ContentMark::~CPDF_ContentMark() {} - -std::unique_ptr CPDF_ContentMark::Clone() { - auto result = pdfium::MakeUnique(); - if (m_pMarkData) - result->m_pMarkData = pdfium::MakeRetain(*m_pMarkData); - return result; -} - -size_t CPDF_ContentMark::CountItems() const { - return m_pMarkData ? m_pMarkData->CountItems() : 0; -} - -bool CPDF_ContentMark::ContainsItem(const CPDF_ContentMarkItem* pItem) const { - return m_pMarkData && m_pMarkData->ContainsItem(pItem); -} - -CPDF_ContentMarkItem* CPDF_ContentMark::GetItem(size_t index) { - return const_cast( - static_cast(this)->GetItem(index)); -} - -const CPDF_ContentMarkItem* CPDF_ContentMark::GetItem(size_t index) const { - ASSERT(index < CountItems()); - return m_pMarkData->GetItem(index); -} - -int CPDF_ContentMark::GetMarkedContentID() const { - return m_pMarkData ? m_pMarkData->GetMarkedContentID() : -1; -} - -void CPDF_ContentMark::AddMark(ByteString name) { - EnsureMarkDataExists(); - m_pMarkData->AddMark(std::move(name)); -} - -void CPDF_ContentMark::AddMarkWithDirectDict(ByteString name, - CPDF_Dictionary* pDict) { - EnsureMarkDataExists(); - m_pMarkData->AddMarkWithDirectDict(std::move(name), pDict); -} - -void CPDF_ContentMark::AddMarkWithPropertiesDict( - ByteString name, - CPDF_Dictionary* pDict, - const ByteString& property_name) { - EnsureMarkDataExists(); - m_pMarkData->AddMarkWithPropertiesDict(std::move(name), pDict, property_name); -} - -bool CPDF_ContentMark::RemoveMark(CPDF_ContentMarkItem* pMarkItem) { - return m_pMarkData && m_pMarkData->RemoveMark(pMarkItem); -} - -void CPDF_ContentMark::EnsureMarkDataExists() { - if (!m_pMarkData) - m_pMarkData = pdfium::MakeRetain(); -} - -void CPDF_ContentMark::DeleteLastMark() { - if (!m_pMarkData) - return; - - m_pMarkData->DeleteLastMark(); - if (CountItems() == 0) - m_pMarkData.Reset(); -} - -size_t CPDF_ContentMark::FindFirstDifference( - const CPDF_ContentMark* other) const { - if (m_pMarkData == other->m_pMarkData) - return CountItems(); - - size_t min_len = std::min(CountItems(), other->CountItems()); - - for (size_t i = 0; i < min_len; ++i) { - if (GetItem(i) != other->GetItem(i)) - return i; - } - return min_len; -} - -CPDF_ContentMark::MarkData::MarkData() {} - -CPDF_ContentMark::MarkData::MarkData(const MarkData& src) - : m_Marks(src.m_Marks) {} - -CPDF_ContentMark::MarkData::~MarkData() {} - -size_t CPDF_ContentMark::MarkData::CountItems() const { - return m_Marks.size(); -} - -bool CPDF_ContentMark::MarkData::ContainsItem( - const CPDF_ContentMarkItem* pItem) const { - for (const auto pMark : m_Marks) { - if (pMark.Get() == pItem) - return true; - } - return false; -} - -CPDF_ContentMarkItem* CPDF_ContentMark::MarkData::GetItem(size_t index) { - return m_Marks[index].Get(); -} - -const CPDF_ContentMarkItem* CPDF_ContentMark::MarkData::GetItem( - size_t index) const { - return m_Marks[index].Get(); -} - -int CPDF_ContentMark::MarkData::GetMarkedContentID() const { - for (const auto pMark : m_Marks) { - const CPDF_Dictionary* pDict = pMark->GetParam(); - if (pDict && pDict->KeyExist("MCID")) - return pDict->GetIntegerFor("MCID"); - } - return -1; -} - -void CPDF_ContentMark::MarkData::AddMark(ByteString name) { - auto pItem = pdfium::MakeRetain(std::move(name)); - m_Marks.push_back(pItem); -} - -void CPDF_ContentMark::MarkData::AddMarkWithDirectDict(ByteString name, - CPDF_Dictionary* pDict) { - auto pItem = pdfium::MakeRetain(std::move(name)); - pItem->SetDirectDict(ToDictionary(pDict->Clone())); - m_Marks.push_back(pItem); -} - -void CPDF_ContentMark::MarkData::AddMarkWithPropertiesDict( - ByteString name, - CPDF_Dictionary* pDict, - const ByteString& property_name) { - auto pItem = pdfium::MakeRetain(std::move(name)); - pItem->SetPropertiesDict(pDict, property_name); - m_Marks.push_back(pItem); -} - -bool CPDF_ContentMark::MarkData::RemoveMark(CPDF_ContentMarkItem* pMarkItem) { - for (auto it = m_Marks.begin(); it != m_Marks.end(); ++it) { - if (it->Get() == pMarkItem) { - m_Marks.erase(it); - return true; - } - } - return false; -} - -void CPDF_ContentMark::MarkData::DeleteLastMark() { - if (!m_Marks.empty()) - m_Marks.pop_back(); -} diff --git a/core/fpdfapi/page/cpdf_contentmark.h b/core/fpdfapi/page/cpdf_contentmark.h deleted file mode 100644 index 1395db12f8..0000000000 --- a/core/fpdfapi/page/cpdf_contentmark.h +++ /dev/null @@ -1,72 +0,0 @@ -// Copyright 2016 PDFium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com - -#ifndef CORE_FPDFAPI_PAGE_CPDF_CONTENTMARK_H_ -#define CORE_FPDFAPI_PAGE_CPDF_CONTENTMARK_H_ - -#include -#include - -#include "core/fpdfapi/page/cpdf_contentmarkitem.h" -#include "core/fxcrt/fx_system.h" -#include "core/fxcrt/retain_ptr.h" - -class CPDF_Dictionary; - -class CPDF_ContentMark { - public: - CPDF_ContentMark(); - ~CPDF_ContentMark(); - - std::unique_ptr Clone(); - int GetMarkedContentID() const; - size_t CountItems() const; - bool ContainsItem(const CPDF_ContentMarkItem* pItem) const; - - // The returned pointer is never null. - CPDF_ContentMarkItem* GetItem(size_t index); - const CPDF_ContentMarkItem* GetItem(size_t index) const; - - void AddMark(ByteString name); - void AddMarkWithDirectDict(ByteString name, CPDF_Dictionary* pDict); - void AddMarkWithPropertiesDict(ByteString name, - CPDF_Dictionary* pDict, - const ByteString& property_name); - bool RemoveMark(CPDF_ContentMarkItem* pMarkItem); - void DeleteLastMark(); - size_t FindFirstDifference(const CPDF_ContentMark* other) const; - - private: - class MarkData final : public Retainable { - public: - MarkData(); - MarkData(const MarkData& src); - ~MarkData() override; - - size_t CountItems() const; - bool ContainsItem(const CPDF_ContentMarkItem* pItem) const; - CPDF_ContentMarkItem* GetItem(size_t index); - const CPDF_ContentMarkItem* GetItem(size_t index) const; - - int GetMarkedContentID() const; - void AddMark(ByteString name); - void AddMarkWithDirectDict(ByteString name, CPDF_Dictionary* pDict); - void AddMarkWithPropertiesDict(ByteString name, - CPDF_Dictionary* pDict, - const ByteString& property_name); - bool RemoveMark(CPDF_ContentMarkItem* pMarkItem); - void DeleteLastMark(); - - private: - std::vector> m_Marks; - }; - - void EnsureMarkDataExists(); - - RetainPtr m_pMarkData; -}; - -#endif // CORE_FPDFAPI_PAGE_CPDF_CONTENTMARK_H_ diff --git a/core/fpdfapi/page/cpdf_contentmarks.cpp b/core/fpdfapi/page/cpdf_contentmarks.cpp new file mode 100644 index 0000000000..caaf6e391d --- /dev/null +++ b/core/fpdfapi/page/cpdf_contentmarks.cpp @@ -0,0 +1,172 @@ +// Copyright 2016 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "core/fpdfapi/page/cpdf_contentmarks.h" + +#include +#include + +#include "core/fpdfapi/parser/cpdf_dictionary.h" +#include "third_party/base/ptr_util.h" + +CPDF_ContentMarks::CPDF_ContentMarks() {} + +CPDF_ContentMarks::~CPDF_ContentMarks() {} + +std::unique_ptr CPDF_ContentMarks::Clone() { + auto result = pdfium::MakeUnique(); + if (m_pMarkData) + result->m_pMarkData = pdfium::MakeRetain(*m_pMarkData); + return result; +} + +size_t CPDF_ContentMarks::CountItems() const { + return m_pMarkData ? m_pMarkData->CountItems() : 0; +} + +bool CPDF_ContentMarks::ContainsItem(const CPDF_ContentMarkItem* pItem) const { + return m_pMarkData && m_pMarkData->ContainsItem(pItem); +} + +CPDF_ContentMarkItem* CPDF_ContentMarks::GetItem(size_t index) { + return const_cast( + static_cast(this)->GetItem(index)); +} + +const CPDF_ContentMarkItem* CPDF_ContentMarks::GetItem(size_t index) const { + ASSERT(index < CountItems()); + return m_pMarkData->GetItem(index); +} + +int CPDF_ContentMarks::GetMarkedContentID() const { + return m_pMarkData ? m_pMarkData->GetMarkedContentID() : -1; +} + +void CPDF_ContentMarks::AddMark(ByteString name) { + EnsureMarkDataExists(); + m_pMarkData->AddMark(std::move(name)); +} + +void CPDF_ContentMarks::AddMarkWithDirectDict(ByteString name, + CPDF_Dictionary* pDict) { + EnsureMarkDataExists(); + m_pMarkData->AddMarkWithDirectDict(std::move(name), pDict); +} + +void CPDF_ContentMarks::AddMarkWithPropertiesDict( + ByteString name, + CPDF_Dictionary* pDict, + const ByteString& property_name) { + EnsureMarkDataExists(); + m_pMarkData->AddMarkWithPropertiesDict(std::move(name), pDict, property_name); +} + +bool CPDF_ContentMarks::RemoveMark(CPDF_ContentMarkItem* pMarkItem) { + return m_pMarkData && m_pMarkData->RemoveMark(pMarkItem); +} + +void CPDF_ContentMarks::EnsureMarkDataExists() { + if (!m_pMarkData) + m_pMarkData = pdfium::MakeRetain(); +} + +void CPDF_ContentMarks::DeleteLastMark() { + if (!m_pMarkData) + return; + + m_pMarkData->DeleteLastMark(); + if (CountItems() == 0) + m_pMarkData.Reset(); +} + +size_t CPDF_ContentMarks::FindFirstDifference( + const CPDF_ContentMarks* other) const { + if (m_pMarkData == other->m_pMarkData) + return CountItems(); + + size_t min_len = std::min(CountItems(), other->CountItems()); + + for (size_t i = 0; i < min_len; ++i) { + if (GetItem(i) != other->GetItem(i)) + return i; + } + return min_len; +} + +CPDF_ContentMarks::MarkData::MarkData() {} + +CPDF_ContentMarks::MarkData::MarkData(const MarkData& src) + : m_Marks(src.m_Marks) {} + +CPDF_ContentMarks::MarkData::~MarkData() {} + +size_t CPDF_ContentMarks::MarkData::CountItems() const { + return m_Marks.size(); +} + +bool CPDF_ContentMarks::MarkData::ContainsItem( + const CPDF_ContentMarkItem* pItem) const { + for (const auto pMark : m_Marks) { + if (pMark.Get() == pItem) + return true; + } + return false; +} + +CPDF_ContentMarkItem* CPDF_ContentMarks::MarkData::GetItem(size_t index) { + return m_Marks[index].Get(); +} + +const CPDF_ContentMarkItem* CPDF_ContentMarks::MarkData::GetItem( + size_t index) const { + return m_Marks[index].Get(); +} + +int CPDF_ContentMarks::MarkData::GetMarkedContentID() const { + for (const auto pMark : m_Marks) { + const CPDF_Dictionary* pDict = pMark->GetParam(); + if (pDict && pDict->KeyExist("MCID")) + return pDict->GetIntegerFor("MCID"); + } + return -1; +} + +void CPDF_ContentMarks::MarkData::AddMark(ByteString name) { + auto pItem = pdfium::MakeRetain(std::move(name)); + m_Marks.push_back(pItem); +} + +void CPDF_ContentMarks::MarkData::AddMarkWithDirectDict( + ByteString name, + CPDF_Dictionary* pDict) { + auto pItem = pdfium::MakeRetain(std::move(name)); + pItem->SetDirectDict(ToDictionary(pDict->Clone())); + m_Marks.push_back(pItem); +} + +void CPDF_ContentMarks::MarkData::AddMarkWithPropertiesDict( + ByteString name, + CPDF_Dictionary* pDict, + const ByteString& property_name) { + auto pItem = pdfium::MakeRetain(std::move(name)); + pItem->SetPropertiesDict(pDict, property_name); + m_Marks.push_back(pItem); +} + +bool CPDF_ContentMarks::MarkData::RemoveMark(CPDF_ContentMarkItem* pMarkItem) { + for (auto it = m_Marks.begin(); it != m_Marks.end(); ++it) { + if (it->Get() == pMarkItem) { + m_Marks.erase(it); + return true; + } + } + return false; +} + +void CPDF_ContentMarks::MarkData::DeleteLastMark() { + if (!m_Marks.empty()) + m_Marks.pop_back(); +} diff --git a/core/fpdfapi/page/cpdf_contentmarks.h b/core/fpdfapi/page/cpdf_contentmarks.h new file mode 100644 index 0000000000..335592b940 --- /dev/null +++ b/core/fpdfapi/page/cpdf_contentmarks.h @@ -0,0 +1,72 @@ +// Copyright 2016 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef CORE_FPDFAPI_PAGE_CPDF_CONTENTMARKS_H_ +#define CORE_FPDFAPI_PAGE_CPDF_CONTENTMARKS_H_ + +#include +#include + +#include "core/fpdfapi/page/cpdf_contentmarkitem.h" +#include "core/fxcrt/fx_system.h" +#include "core/fxcrt/retain_ptr.h" + +class CPDF_Dictionary; + +class CPDF_ContentMarks { + public: + CPDF_ContentMarks(); + ~CPDF_ContentMarks(); + + std::unique_ptr Clone(); + int GetMarkedContentID() const; + size_t CountItems() const; + bool ContainsItem(const CPDF_ContentMarkItem* pItem) const; + + // The returned pointer is never null. + CPDF_ContentMarkItem* GetItem(size_t index); + const CPDF_ContentMarkItem* GetItem(size_t index) const; + + void AddMark(ByteString name); + void AddMarkWithDirectDict(ByteString name, CPDF_Dictionary* pDict); + void AddMarkWithPropertiesDict(ByteString name, + CPDF_Dictionary* pDict, + const ByteString& property_name); + bool RemoveMark(CPDF_ContentMarkItem* pMarkItem); + void DeleteLastMark(); + size_t FindFirstDifference(const CPDF_ContentMarks* other) const; + + private: + class MarkData final : public Retainable { + public: + MarkData(); + MarkData(const MarkData& src); + ~MarkData() override; + + size_t CountItems() const; + bool ContainsItem(const CPDF_ContentMarkItem* pItem) const; + CPDF_ContentMarkItem* GetItem(size_t index); + const CPDF_ContentMarkItem* GetItem(size_t index) const; + + int GetMarkedContentID() const; + void AddMark(ByteString name); + void AddMarkWithDirectDict(ByteString name, CPDF_Dictionary* pDict); + void AddMarkWithPropertiesDict(ByteString name, + CPDF_Dictionary* pDict, + const ByteString& property_name); + bool RemoveMark(CPDF_ContentMarkItem* pMarkItem); + void DeleteLastMark(); + + private: + std::vector> m_Marks; + }; + + void EnsureMarkDataExists(); + + RetainPtr m_pMarkData; +}; + +#endif // CORE_FPDFAPI_PAGE_CPDF_CONTENTMARKS_H_ diff --git a/core/fpdfapi/page/cpdf_pageobject.h b/core/fpdfapi/page/cpdf_pageobject.h index 013ef0d03b..6bbaff3c58 100644 --- a/core/fpdfapi/page/cpdf_pageobject.h +++ b/core/fpdfapi/page/cpdf_pageobject.h @@ -7,7 +7,7 @@ #ifndef CORE_FPDFAPI_PAGE_CPDF_PAGEOBJECT_H_ #define CORE_FPDFAPI_PAGE_CPDF_PAGEOBJECT_H_ -#include "core/fpdfapi/page/cpdf_contentmark.h" +#include "core/fpdfapi/page/cpdf_contentmarks.h" #include "core/fpdfapi/page/cpdf_graphicstates.h" #include "core/fxcrt/fx_coordinates.h" #include "core/fxcrt/fx_system.h" @@ -74,7 +74,7 @@ class CPDF_PageObject : public CPDF_GraphicStates { m_ContentStream = new_content_stream; } - CPDF_ContentMark m_ContentMark; + CPDF_ContentMarks m_ContentMarks; protected: void CopyData(const CPDF_PageObject* pSrcObject); diff --git a/core/fpdfapi/page/cpdf_streamcontentparser.cpp b/core/fpdfapi/page/cpdf_streamcontentparser.cpp index b381705123..d84d4d62ec 100644 --- a/core/fpdfapi/page/cpdf_streamcontentparser.cpp +++ b/core/fpdfapi/page/cpdf_streamcontentparser.cpp @@ -290,7 +290,7 @@ CPDF_StreamContentParser::CPDF_StreamContentParser( } // Add the sentinel. - m_ContentMarksStack.push(pdfium::MakeUnique()); + m_ContentMarksStack.push(pdfium::MakeUnique()); } CPDF_StreamContentParser::~CPDF_StreamContentParser() { @@ -428,7 +428,7 @@ void CPDF_StreamContentParser::SetGraphicStates(CPDF_PageObject* pObj, bool bGraph) { pObj->m_GeneralState = m_pCurStates->m_GeneralState; pObj->m_ClipPath = m_pCurStates->m_ClipPath; - pObj->m_ContentMark = *m_ContentMarksStack.top(); + pObj->m_ContentMarks = *m_ContentMarksStack.top(); if (bColor) { pObj->m_ColorState = m_pCurStates->m_ColorState; } @@ -602,7 +602,7 @@ void CPDF_StreamContentParser::Handle_BeginMarkedContent_Dictionary() { return; } if (CPDF_Dictionary* pDict = pProperty->AsDictionary()) { - std::unique_ptr new_marks = + std::unique_ptr new_marks = m_ContentMarksStack.top()->Clone(); if (bIndirect) { new_marks->AddMarkWithPropertiesDict(std::move(tag), pDict, @@ -675,7 +675,7 @@ void CPDF_StreamContentParser::Handle_BeginImage() { } void CPDF_StreamContentParser::Handle_BeginMarkedContent() { - std::unique_ptr new_marks = + std::unique_ptr new_marks = m_ContentMarksStack.top()->Clone(); new_marks->AddMark(GetString(0)); m_ContentMarksStack.push(std::move(new_marks)); diff --git a/core/fpdfapi/page/cpdf_streamcontentparser.h b/core/fpdfapi/page/cpdf_streamcontentparser.h index d9239fd6fa..05556ff5d2 100644 --- a/core/fpdfapi/page/cpdf_streamcontentparser.h +++ b/core/fpdfapi/page/cpdf_streamcontentparser.h @@ -13,7 +13,7 @@ #include #include -#include "core/fpdfapi/page/cpdf_contentmark.h" +#include "core/fpdfapi/page/cpdf_contentmarks.h" #include "core/fpdfapi/parser/cpdf_stream.h" #include "core/fxcrt/fx_number.h" #include "core/fxcrt/fx_string.h" @@ -213,7 +213,7 @@ class CPDF_StreamContentParser { uint32_t m_ParamCount; UnownedPtr m_pSyntax; std::unique_ptr m_pCurStates; - std::stack> m_ContentMarksStack; + std::stack> m_ContentMarksStack; std::vector> m_ClipTextList; UnownedPtr m_pLastTextObject; float m_DefFontSize; -- cgit v1.2.3