diff options
author | Lei Zhang <thestig@chromium.org> | 2017-11-07 14:22:19 +0000 |
---|---|---|
committer | Chromium commit bot <commit-bot@chromium.org> | 2017-11-07 14:22:19 +0000 |
commit | f2f12b32b601f022b5c10fddd8c4d19d00072630 (patch) | |
tree | 3f23bcaf245b1bc7d331638070a42ae0f40c3908 /core/fpdfapi/page | |
parent | a9fee44bc9f6fb09cdd2585a5649ecf2ad03ecdd (diff) | |
download | pdfium-f2f12b32b601f022b5c10fddd8c4d19d00072630.tar.xz |
Simplify CPDF_ContentParser.
Do more work in the ctor instead of a separate Start() method. Adjust
callers and member variables accordingly.
Change-Id: I310f27b3102a89e8e7b7ad31affd174f1fbbe500
Reviewed-on: https://pdfium-review.googlesource.com/17793
Commit-Queue: dsinclair <dsinclair@chromium.org>
Reviewed-by: dsinclair <dsinclair@chromium.org>
Diffstat (limited to 'core/fpdfapi/page')
-rw-r--r-- | core/fpdfapi/page/cpdf_contentparser.cpp | 103 | ||||
-rw-r--r-- | core/fpdfapi/page/cpdf_contentparser.h | 39 | ||||
-rw-r--r-- | core/fpdfapi/page/cpdf_form.cpp | 4 | ||||
-rw-r--r-- | core/fpdfapi/page/cpdf_page.cpp | 3 | ||||
-rw-r--r-- | core/fpdfapi/page/cpdf_pageobjectholder.cpp | 3 |
5 files changed, 69 insertions, 83 deletions
diff --git a/core/fpdfapi/page/cpdf_contentparser.cpp b/core/fpdfapi/page/cpdf_contentparser.cpp index bf2fc33538..0571405908 100644 --- a/core/fpdfapi/page/cpdf_contentparser.cpp +++ b/core/fpdfapi/page/cpdf_contentparser.cpp @@ -22,64 +22,49 @@ #define PARSE_STEP_LIMIT 100 -CPDF_ContentParser::CPDF_ContentParser() - : m_Status(Ready), - m_InternalStage(STAGE_GETCONTENT), - m_pObjectHolder(nullptr), - m_bForm(false), - m_pType3Char(nullptr), - m_pData(nullptr), - m_Size(0), - m_CurrentOffset(0) {} - -CPDF_ContentParser::~CPDF_ContentParser() { - if (!m_pSingleStream) - FX_Free(m_pData); -} - -void CPDF_ContentParser::Start(CPDF_Page* pPage) { - if (m_Status != Ready || !pPage || !pPage->m_pDocument || - !pPage->m_pFormDict) { - m_Status = Done; +CPDF_ContentParser::CPDF_ContentParser(CPDF_Page* pPage) + : m_InternalStage(STAGE_GETCONTENT), m_pObjectHolder(pPage) { + if (!pPage || !pPage->m_pDocument || !pPage->m_pFormDict) { + m_bIsDone = true; return; } - m_pObjectHolder = pPage; - m_bForm = false; - m_Status = ToBeContinued; - m_InternalStage = STAGE_GETCONTENT; - m_CurrentOffset = 0; CPDF_Object* pContent = pPage->m_pFormDict->GetDirectObjectFor("Contents"); if (!pContent) { - m_Status = Done; + m_bIsDone = true; return; } - if (CPDF_Stream* pStream = pContent->AsStream()) { - m_nStreams = 0; + CPDF_Stream* pStream = pContent->AsStream(); + if (pStream) { m_pSingleStream = pdfium::MakeRetain<CPDF_StreamAcc>(pStream); m_pSingleStream->LoadAllData(false); - } else if (CPDF_Array* pArray = pContent->AsArray()) { - m_nStreams = pArray->GetCount(); - if (m_nStreams) - m_StreamArray.resize(m_nStreams); - else - m_Status = Done; - } else { - m_Status = Done; + return; + } + CPDF_Array* pArray = pContent->AsArray(); + if (!pArray) { + m_bIsDone = true; + return; } + m_nStreams = pArray->GetCount(); + if (!m_nStreams) { + m_bIsDone = true; + return; + } + m_StreamArray.resize(m_nStreams); } -void CPDF_ContentParser::Start(CPDF_Form* pForm, - CPDF_AllStates* pGraphicStates, - const CFX_Matrix* pParentMatrix, - CPDF_Type3Char* pType3Char, - std::set<const uint8_t*>* parsedSet) { - m_pType3Char = pType3Char; - m_pObjectHolder = pForm; - m_bForm = true; +CPDF_ContentParser::CPDF_ContentParser(CPDF_Form* pForm, + CPDF_AllStates* pGraphicStates, + const CFX_Matrix* pParentMatrix, + CPDF_Type3Char* pType3Char, + std::set<const uint8_t*>* parsedSet) + : m_InternalStage(STAGE_PARSE), + m_pObjectHolder(pForm), + m_pType3Char(pType3Char) { CFX_Matrix form_matrix = pForm->m_pFormDict->GetMatrixFor("Matrix"); if (pGraphicStates) form_matrix.Concat(pGraphicStates->m_CTM); + CPDF_Array* pBBox = pForm->m_pFormDict->GetArrayFor("BBox"); CFX_FloatRect form_bbox; CPDF_Path ClipPath; @@ -115,20 +100,24 @@ void CPDF_ContentParser::Start(CPDF_Form* pForm, pState->SetFillAlpha(1.0f); pState->SetSoftMask(nullptr); } - m_nStreams = 0; m_pSingleStream = pdfium::MakeRetain<CPDF_StreamAcc>(pForm->m_pFormStream.Get()); m_pSingleStream->LoadAllData(false); - m_pData = (uint8_t*)m_pSingleStream->GetData(); + m_pData = const_cast<uint8_t*>(m_pSingleStream->GetData()); m_Size = m_pSingleStream->GetSize(); - m_Status = ToBeContinued; - m_InternalStage = STAGE_PARSE; - m_CurrentOffset = 0; } -void CPDF_ContentParser::Continue(IFX_PauseIndicator* pPause) { - int steps = 0; - while (m_Status == ToBeContinued) { +CPDF_ContentParser::~CPDF_ContentParser() { + // TODO(thestig): Switch |m_pData| to MaybeOwned? + if (!m_pSingleStream) + FX_Free(m_pData); +} + +bool CPDF_ContentParser::Continue(IFX_PauseIndicator* pPause) { + if (m_bIsDone) + return false; + + while (!m_bIsDone) { if (m_InternalStage == STAGE_GETCONTENT) { if (m_CurrentOffset == m_nStreams) { if (!m_StreamArray.empty()) { @@ -138,8 +127,8 @@ void CPDF_ContentParser::Continue(IFX_PauseIndicator* pPause) { safeSize += 1; } if (!safeSize.IsValid()) { - m_Status = Done; - return; + m_bIsDone = true; + return false; } m_Size = safeSize.ValueOrDie(); m_pData = FX_Alloc(uint8_t, m_Size); @@ -151,7 +140,7 @@ void CPDF_ContentParser::Continue(IFX_PauseIndicator* pPause) { } m_StreamArray.clear(); } else { - m_pData = (uint8_t*)m_pSingleStream->GetData(); + m_pData = const_cast<uint8_t*>(m_pSingleStream->GetData()); m_Size = m_pSingleStream->GetSize(); } m_InternalStage = STAGE_PARSE; @@ -218,11 +207,11 @@ void CPDF_ContentParser::Continue(IFX_PauseIndicator* pPause) { if (old_rect.Contains(obj_rect)) pObj->m_ClipPath.SetNull(); } - m_Status = Done; - return; + m_bIsDone = true; + return false; } - steps++; if (pPause && pPause->NeedToPauseNow()) break; } + return true; } diff --git a/core/fpdfapi/page/cpdf_contentparser.h b/core/fpdfapi/page/cpdf_contentparser.h index b1e601a0e1..c4fcb9060b 100644 --- a/core/fpdfapi/page/cpdf_contentparser.h +++ b/core/fpdfapi/page/cpdf_contentparser.h @@ -24,22 +24,19 @@ class CPDF_Type3Char; class CPDF_ContentParser { public: - enum ParseStatus { Ready, ToBeContinued, Done }; - - CPDF_ContentParser(); + explicit CPDF_ContentParser(CPDF_Page* pPage); + CPDF_ContentParser(CPDF_Form* pForm, + CPDF_AllStates* pGraphicStates, + const CFX_Matrix* pParentMatrix, + CPDF_Type3Char* pType3Char, + std::set<const uint8_t*>* parsedSet); ~CPDF_ContentParser(); - ParseStatus GetStatus() const { return m_Status; } const CPDF_AllStates* GetCurStates() const { return m_pParser ? m_pParser->GetCurStates() : nullptr; } - void Start(CPDF_Page* pPage); - void Start(CPDF_Form* pForm, - CPDF_AllStates* pGraphicStates, - const CFX_Matrix* pParentMatrix, - CPDF_Type3Char* pType3Char, - std::set<const uint8_t*>* parsedSet); - void Continue(IFX_PauseIndicator* pPause); + // Returns whether to continue or not. + bool Continue(IFX_PauseIndicator* pPause); private: enum InternalStage { @@ -48,19 +45,21 @@ class CPDF_ContentParser { STAGE_CHECKCLIP, }; - ParseStatus m_Status; + bool m_bIsDone = false; InternalStage m_InternalStage; - UnownedPtr<CPDF_PageObjectHolder> m_pObjectHolder; - bool m_bForm; - UnownedPtr<CPDF_Type3Char> m_pType3Char; - uint32_t m_nStreams; + UnownedPtr<CPDF_PageObjectHolder> const m_pObjectHolder; + UnownedPtr<CPDF_Type3Char> m_pType3Char; // Only used when parsing forms. + uint32_t m_nStreams = 0; RetainPtr<CPDF_StreamAcc> m_pSingleStream; std::vector<RetainPtr<CPDF_StreamAcc>> m_StreamArray; - uint8_t* m_pData; - uint32_t m_Size; - uint32_t m_CurrentOffset; + uint8_t* m_pData = nullptr; + uint32_t m_Size = 0; + uint32_t m_CurrentOffset = 0; + + // Only used when parsing pages. std::unique_ptr<std::set<const uint8_t*>> m_parsedSet; - // m_pParser has a reference to m_parsedSet, so must be below and thus + + // |m_pParser| has a reference to |m_parsedSet|, so must be below and thus // destroyed first. std::unique_ptr<CPDF_StreamContentParser> m_pParser; }; diff --git a/core/fpdfapi/page/cpdf_form.cpp b/core/fpdfapi/page/cpdf_form.cpp index ae06cb2f7e..bb06cbfe6f 100644 --- a/core/fpdfapi/page/cpdf_form.cpp +++ b/core/fpdfapi/page/cpdf_form.cpp @@ -44,8 +44,8 @@ void CPDF_Form::StartParse(CPDF_AllStates* pGraphicStates, parsedSet = m_ParsedSet.get(); } - m_pParser = pdfium::MakeUnique<CPDF_ContentParser>(); - m_pParser->Start(this, pGraphicStates, pParentMatrix, pType3Char, parsedSet); + m_pParser = pdfium::MakeUnique<CPDF_ContentParser>( + this, pGraphicStates, pParentMatrix, pType3Char, parsedSet); m_ParseState = CONTENT_PARSING; } diff --git a/core/fpdfapi/page/cpdf_page.cpp b/core/fpdfapi/page/cpdf_page.cpp index 3a7f8b5805..c6f4fb2a39 100644 --- a/core/fpdfapi/page/cpdf_page.cpp +++ b/core/fpdfapi/page/cpdf_page.cpp @@ -82,8 +82,7 @@ void CPDF_Page::StartParse() { if (m_ParseState == CONTENT_PARSED || m_ParseState == CONTENT_PARSING) return; - m_pParser = pdfium::MakeUnique<CPDF_ContentParser>(); - m_pParser->Start(this); + m_pParser = pdfium::MakeUnique<CPDF_ContentParser>(this); m_ParseState = CONTENT_PARSING; } diff --git a/core/fpdfapi/page/cpdf_pageobjectholder.cpp b/core/fpdfapi/page/cpdf_pageobjectholder.cpp index 4e9da63dea..51a67e8a15 100644 --- a/core/fpdfapi/page/cpdf_pageobjectholder.cpp +++ b/core/fpdfapi/page/cpdf_pageobjectholder.cpp @@ -37,8 +37,7 @@ void CPDF_PageObjectHolder::ContinueParse(IFX_PauseIndicator* pPause) { if (!m_pParser) return; - m_pParser->Continue(pPause); - if (m_pParser->GetStatus() != CPDF_ContentParser::Done) + if (m_pParser->Continue(pPause)) return; m_ParseState = CONTENT_PARSED; |