From 898690313426bf1604e0bb6def684db84a782494 Mon Sep 17 00:00:00 2001 From: Lei Zhang Date: Mon, 6 Nov 2017 20:01:13 +0000 Subject: Add a CPDF_PSEngine test. Change-Id: Ia267f40dc4ebe9e1d28b6a333eddcff749414ffa Reviewed-on: https://pdfium-review.googlesource.com/17870 Reviewed-by: dsinclair Commit-Queue: Lei Zhang --- core/fpdfapi/page/cpdf_psengine.cpp | 127 ++++++++++++++++++------------------ 1 file changed, 64 insertions(+), 63 deletions(-) (limited to 'core/fpdfapi/page/cpdf_psengine.cpp') diff --git a/core/fpdfapi/page/cpdf_psengine.cpp b/core/fpdfapi/page/cpdf_psengine.cpp index d879ead9c1..9dfa9989f0 100644 --- a/core/fpdfapi/page/cpdf_psengine.cpp +++ b/core/fpdfapi/page/cpdf_psengine.cpp @@ -46,39 +46,32 @@ const PDF_PSOpName kPsOpNames[] = { } // namespace -class CPDF_PSOP { - public: - explicit CPDF_PSOP(PDF_PSOP op) : m_op(op), m_value(0) { - ASSERT(m_op != PSOP_CONST); - ASSERT(m_op != PSOP_PROC); - } - explicit CPDF_PSOP(float value) : m_op(PSOP_CONST), m_value(value) {} - CPDF_PSOP() - : m_op(PSOP_PROC), - m_value(0), - m_proc(pdfium::MakeUnique()) {} - - float GetFloatValue() const { - if (m_op == PSOP_CONST) - return m_value; - - NOTREACHED(); - return 0; - } - CPDF_PSProc* GetProc() const { - if (m_op == PSOP_PROC) - return m_proc.get(); - NOTREACHED(); - return nullptr; - } +CPDF_PSOP::CPDF_PSOP() + : m_op(PSOP_PROC), m_value(0), m_proc(pdfium::MakeUnique()) {} - PDF_PSOP GetOp() const { return m_op; } +CPDF_PSOP::CPDF_PSOP(PDF_PSOP op) : m_op(op), m_value(0) { + ASSERT(m_op != PSOP_CONST); + ASSERT(m_op != PSOP_PROC); +} - private: - const PDF_PSOP m_op; - const float m_value; - std::unique_ptr m_proc; -}; +CPDF_PSOP::CPDF_PSOP(float value) : m_op(PSOP_CONST), m_value(value) {} + +CPDF_PSOP::~CPDF_PSOP() {} + +float CPDF_PSOP::GetFloatValue() const { + if (m_op == PSOP_CONST) + return m_value; + + NOTREACHED(); + return 0; +} + +CPDF_PSProc* CPDF_PSOP::GetProc() const { + if (m_op == PSOP_PROC) + return m_proc.get(); + NOTREACHED(); + return nullptr; +} bool CPDF_PSEngine::Execute() { return m_MainProc.Execute(this); @@ -87,6 +80,29 @@ bool CPDF_PSEngine::Execute() { CPDF_PSProc::CPDF_PSProc() {} CPDF_PSProc::~CPDF_PSProc() {} +bool CPDF_PSProc::Parse(CPDF_SimpleParser* parser, int depth) { + if (depth > kMaxDepth) + return false; + + while (1) { + ByteStringView word = parser->GetWord(); + if (word.IsEmpty()) + return false; + + if (word == "}") + return true; + + if (word == "{") { + m_Operators.push_back(pdfium::MakeUnique()); + if (!m_Operators.back()->GetProc()->Parse(parser, depth + 1)) + return false; + continue; + } + + AddOperator(word); + } +} + bool CPDF_PSProc::Execute(CPDF_PSEngine* pEngine) { for (size_t i = 0; i < m_Operators.size(); ++i) { const PDF_PSOP op = m_Operators[i]->GetOp(); @@ -118,6 +134,23 @@ bool CPDF_PSProc::Execute(CPDF_PSEngine* pEngine) { return true; } +void CPDF_PSProc::AddOperatorForTesting(const ByteStringView& word) { + AddOperator(word); +} + +void CPDF_PSProc::AddOperator(const ByteStringView& word) { + std::unique_ptr op; + for (const PDF_PSOpName& op_name : kPsOpNames) { + if (word == ByteStringView(op_name.name)) { + op = pdfium::MakeUnique(op_name.op); + break; + } + } + if (!op) + op = pdfium::MakeUnique(FX_atof(word)); + m_Operators.push_back(std::move(op)); +} + CPDF_PSEngine::CPDF_PSEngine() : m_StackCount(0) {} CPDF_PSEngine::~CPDF_PSEngine() {} @@ -141,38 +174,6 @@ bool CPDF_PSEngine::Parse(const char* str, int size) { return word == "{" ? m_MainProc.Parse(&parser, 0) : false; } -bool CPDF_PSProc::Parse(CPDF_SimpleParser* parser, int depth) { - if (depth > kMaxDepth) - return false; - - while (1) { - ByteStringView word = parser->GetWord(); - if (word.IsEmpty()) - return false; - - if (word == "}") - return true; - - if (word == "{") { - m_Operators.push_back(pdfium::MakeUnique()); - if (!m_Operators.back()->GetProc()->Parse(parser, depth + 1)) - return false; - continue; - } - - std::unique_ptr op; - for (const PDF_PSOpName& op_name : kPsOpNames) { - if (word == ByteStringView(op_name.name)) { - op = pdfium::MakeUnique(op_name.op); - break; - } - } - if (!op) - op = pdfium::MakeUnique(FX_atof(word)); - m_Operators.push_back(std::move(op)); - } -} - bool CPDF_PSEngine::DoOperator(PDF_PSOP op) { int i1; int i2; -- cgit v1.2.3