From 008b928ea39904374bc0dc8888e27bc48c812bda Mon Sep 17 00:00:00 2001 From: Lei Zhang Date: Thu, 12 Apr 2018 21:23:15 +0000 Subject: Rename one CPDF_Color::SetValue() variant. Rename it to SetValueForPattern() and combine the components parameters into a std::vector. Fix the callers to use std::vector as well. Change-Id: Ib3426e0ffdb164f0fbb10d462ad251bf91165925 Reviewed-on: https://pdfium-review.googlesource.com/30450 Commit-Queue: Lei Zhang Reviewed-by: Henrique Nakashima --- core/fpdfapi/page/cpdf_color.cpp | 13 ++-- core/fpdfapi/page/cpdf_color.h | 5 +- core/fpdfapi/page/cpdf_colorstate.cpp | 10 ++- core/fpdfapi/page/cpdf_colorstate.h | 7 ++- core/fpdfapi/page/cpdf_streamcontentparser.cpp | 86 +++++++++++++------------- core/fpdfapi/page/cpdf_streamcontentparser.h | 3 + 6 files changed, 65 insertions(+), 59 deletions(-) diff --git a/core/fpdfapi/page/cpdf_color.cpp b/core/fpdfapi/page/cpdf_color.cpp index 080df2fa28..26db5dec15 100644 --- a/core/fpdfapi/page/cpdf_color.cpp +++ b/core/fpdfapi/page/cpdf_color.cpp @@ -86,10 +86,9 @@ void CPDF_Color::SetValue(const float* comps) { memcpy(m_pBuffer, comps, m_pCS->CountComponents() * sizeof(float)); } -void CPDF_Color::SetValue(CPDF_Pattern* pPattern, - const float* comps, - uint32_t ncomps) { - if (ncomps > kMaxPatternColorComps) +void CPDF_Color::SetValueForPattern(CPDF_Pattern* pPattern, + const std::vector& values) { + if (values.size() > kMaxPatternColorComps) return; if (!IsPattern()) { @@ -104,10 +103,10 @@ void CPDF_Color::SetValue(CPDF_Pattern* pPattern, pDocPageData = pvalue->m_pPattern->document()->GetPageData(); pDocPageData->ReleasePattern(pvalue->m_pPattern->pattern_obj()); } - pvalue->m_nComps = ncomps; + pvalue->m_nComps = values.size(); pvalue->m_pPattern = pPattern; - if (ncomps) - memcpy(pvalue->m_Comps, comps, ncomps * sizeof(float)); + if (!values.empty()) + memcpy(pvalue->m_Comps, values.data(), values.size() * sizeof(float)); pvalue->m_pCountedPattern = nullptr; if (pPattern) { diff --git a/core/fpdfapi/page/cpdf_color.h b/core/fpdfapi/page/cpdf_color.h index 31bc125d83..a6e640c736 100644 --- a/core/fpdfapi/page/cpdf_color.h +++ b/core/fpdfapi/page/cpdf_color.h @@ -7,6 +7,8 @@ #ifndef CORE_FPDFAPI_PAGE_CPDF_COLOR_H_ #define CORE_FPDFAPI_PAGE_CPDF_COLOR_H_ +#include + #include "core/fpdfapi/page/cpdf_colorspace.h" #include "core/fxcrt/fx_system.h" @@ -24,7 +26,8 @@ class CPDF_Color { void SetColorSpace(CPDF_ColorSpace* pCS); void SetValue(const float* comp); - void SetValue(CPDF_Pattern* pPattern, const float* comp, uint32_t ncomps); + void SetValueForPattern(CPDF_Pattern* pPattern, + const std::vector& values); bool GetRGB(int* R, int* G, int* B) const; diff --git a/core/fpdfapi/page/cpdf_colorstate.cpp b/core/fpdfapi/page/cpdf_colorstate.cpp index b1f14c88b1..16d2770600 100644 --- a/core/fpdfapi/page/cpdf_colorstate.cpp +++ b/core/fpdfapi/page/cpdf_colorstate.cpp @@ -85,10 +85,9 @@ void CPDF_ColorState::SetStrokeColor(CPDF_ColorSpace* pCS, } void CPDF_ColorState::SetFillPattern(CPDF_Pattern* pPattern, - float* pValue, - uint32_t nValues) { + const std::vector& values) { ColorData* pData = m_Ref.GetPrivateCopy(); - pData->m_FillColor.SetValue(pPattern, pValue, nValues); + pData->m_FillColor.SetValueForPattern(pPattern, values); int R; int G; int B; @@ -103,10 +102,9 @@ void CPDF_ColorState::SetFillPattern(CPDF_Pattern* pPattern, } void CPDF_ColorState::SetStrokePattern(CPDF_Pattern* pPattern, - float* pValue, - uint32_t nValues) { + const std::vector& values) { ColorData* pData = m_Ref.GetPrivateCopy(); - pData->m_StrokeColor.SetValue(pPattern, pValue, nValues); + pData->m_StrokeColor.SetValueForPattern(pPattern, values); int R; int G; int B; diff --git a/core/fpdfapi/page/cpdf_colorstate.h b/core/fpdfapi/page/cpdf_colorstate.h index 591610a753..053e103ead 100644 --- a/core/fpdfapi/page/cpdf_colorstate.h +++ b/core/fpdfapi/page/cpdf_colorstate.h @@ -7,6 +7,8 @@ #ifndef CORE_FPDFAPI_PAGE_CPDF_COLORSTATE_H_ #define CORE_FPDFAPI_PAGE_CPDF_COLORSTATE_H_ +#include + #include "core/fpdfapi/page/cpdf_color.h" #include "core/fxcrt/fx_system.h" #include "core/fxcrt/shared_copy_on_write.h" @@ -41,8 +43,9 @@ class CPDF_ColorState { void SetFillColor(CPDF_ColorSpace* pCS, float* pValue, uint32_t nValues); void SetStrokeColor(CPDF_ColorSpace* pCS, float* pValue, uint32_t nValues); - void SetFillPattern(CPDF_Pattern* pattern, float* pValue, uint32_t nValues); - void SetStrokePattern(CPDF_Pattern* pattern, float* pValue, uint32_t nValues); + void SetFillPattern(CPDF_Pattern* pattern, const std::vector& values); + void SetStrokePattern(CPDF_Pattern* pattern, + const std::vector& values); bool HasRef() const { return !!m_Ref; } diff --git a/core/fpdfapi/page/cpdf_streamcontentparser.cpp b/core/fpdfapi/page/cpdf_streamcontentparser.cpp index 9cd05cbb2d..5d1f5efd49 100644 --- a/core/fpdfapi/page/cpdf_streamcontentparser.cpp +++ b/core/fpdfapi/page/cpdf_streamcontentparser.cpp @@ -832,6 +832,23 @@ CPDF_ImageObject* CPDF_StreamContentParser::AddImageObject( return pRet; } +std::vector CPDF_StreamContentParser::GetColors() const { + ASSERT(m_ParamCount > 0); + std::vector values(m_ParamCount); + for (size_t i = 0; i < m_ParamCount; ++i) + values[i] = GetNumber(m_ParamCount - i - 1); + return values; +} + +std::vector CPDF_StreamContentParser::GetNamedColors() const { + ASSERT(m_ParamCount > 0); + const uint32_t nvalues = m_ParamCount - 1; + std::vector values(nvalues); + for (size_t i = 0; i < nvalues; ++i) + values[i] = GetNumber(m_ParamCount - i - 1); + return values; +} + void CPDF_StreamContentParser::Handle_MarkPlace_Dictionary() {} void CPDF_StreamContentParser::Handle_EndImage() {} @@ -1049,57 +1066,40 @@ void CPDF_StreamContentParser::Handle_SetColor_Stroke() { void CPDF_StreamContentParser::Handle_SetColorPS_Fill() { CPDF_Object* pLastParam = GetObject(0); - if (!pLastParam) { + if (!pLastParam) + return; + + if (!pLastParam->IsName()) { + std::vector values = GetColors(); + m_pCurStates->m_ColorState.SetFillColor(nullptr, values.data(), + values.size()); return; } - uint32_t nargs = m_ParamCount; - uint32_t nvalues = nargs; - if (pLastParam->IsName()) - nvalues--; - float* values = nullptr; - if (nvalues) { - values = FX_Alloc(float, nvalues); - for (uint32_t i = 0; i < nvalues; i++) { - values[i] = GetNumber(nargs - i - 1); - } - } - if (nvalues != nargs) { - CPDF_Pattern* pPattern = FindPattern(GetString(0), false); - if (pPattern) { - m_pCurStates->m_ColorState.SetFillPattern(pPattern, values, nvalues); - } - } else { - m_pCurStates->m_ColorState.SetFillColor(nullptr, values, nvalues); - } - FX_Free(values); + + // A valid |pLastParam| implies |m_ParamCount| > 0, so GetNamedColors() call + // below is safe. + CPDF_Pattern* pPattern = FindPattern(GetString(0), false); + if (pPattern) + m_pCurStates->m_ColorState.SetFillPattern(pPattern, GetNamedColors()); } void CPDF_StreamContentParser::Handle_SetColorPS_Stroke() { CPDF_Object* pLastParam = GetObject(0); - if (!pLastParam) { + if (!pLastParam) + return; + + if (!pLastParam->IsName()) { + std::vector values = GetColors(); + m_pCurStates->m_ColorState.SetStrokeColor(nullptr, values.data(), + values.size()); return; } - int nargs = m_ParamCount; - int nvalues = nargs; - if (pLastParam->IsName()) - nvalues--; - - float* values = nullptr; - if (nvalues) { - values = FX_Alloc(float, nvalues); - for (int i = 0; i < nvalues; i++) { - values[i] = GetNumber(nargs - i - 1); - } - } - if (nvalues != nargs) { - CPDF_Pattern* pPattern = FindPattern(GetString(0), false); - if (pPattern) { - m_pCurStates->m_ColorState.SetStrokePattern(pPattern, values, nvalues); - } - } else { - m_pCurStates->m_ColorState.SetStrokeColor(nullptr, values, nvalues); - } - FX_Free(values); + + // A valid |pLastParam| implies |m_ParamCount| > 0, so GetNamedColors() call + // below is safe. + CPDF_Pattern* pPattern = FindPattern(GetString(0), false); + if (pPattern) + m_pCurStates->m_ColorState.SetStrokePattern(pPattern, GetNamedColors()); } void CPDF_StreamContentParser::Handle_ShadeFill() { diff --git a/core/fpdfapi/page/cpdf_streamcontentparser.h b/core/fpdfapi/page/cpdf_streamcontentparser.h index ff80ae9ef1..2711a39918 100644 --- a/core/fpdfapi/page/cpdf_streamcontentparser.h +++ b/core/fpdfapi/page/cpdf_streamcontentparser.h @@ -122,6 +122,9 @@ class CPDF_StreamContentParser { // Takes ownership of |pImageObj|, returns unowned pointer to it. CPDF_ImageObject* AddImageObject(std::unique_ptr pImageObj); + std::vector GetColors() const; + std::vector GetNamedColors() const; + void Handle_CloseFillStrokePath(); void Handle_FillStrokePath(); void Handle_CloseEOFillStrokePath(); -- cgit v1.2.3