diff options
Diffstat (limited to 'core/fpdfapi/page')
-rw-r--r-- | core/fpdfapi/page/cpdf_color.cpp | 10 | ||||
-rw-r--r-- | core/fpdfapi/page/cpdf_color.h | 2 | ||||
-rw-r--r-- | core/fpdfapi/page/cpdf_colorstate.cpp | 36 | ||||
-rw-r--r-- | core/fpdfapi/page/cpdf_colorstate.h | 13 | ||||
-rw-r--r-- | core/fpdfapi/page/cpdf_streamcontentparser.cpp | 73 | ||||
-rw-r--r-- | core/fpdfapi/page/cpdf_streamcontentparser.h | 3 |
6 files changed, 54 insertions, 83 deletions
diff --git a/core/fpdfapi/page/cpdf_color.cpp b/core/fpdfapi/page/cpdf_color.cpp index 26db5dec15..3c87457d1b 100644 --- a/core/fpdfapi/page/cpdf_color.cpp +++ b/core/fpdfapi/page/cpdf_color.cpp @@ -79,11 +79,11 @@ void CPDF_Color::SetColorSpace(CPDF_ColorSpace* pCS) { m_pBuffer = pCS->CreateBufAndSetDefaultColor(); } -void CPDF_Color::SetValue(const float* comps) { - if (!m_pBuffer) - return; - if (!IsPatternInternal()) - memcpy(m_pBuffer, comps, m_pCS->CountComponents() * sizeof(float)); +void CPDF_Color::SetValueForNonPattern(const std::vector<float>& values) { + ASSERT(m_pBuffer); + ASSERT(!IsPatternInternal()); + ASSERT(m_pCS->CountComponents() <= values.size()); + memcpy(m_pBuffer, values.data(), m_pCS->CountComponents() * sizeof(float)); } void CPDF_Color::SetValueForPattern(CPDF_Pattern* pPattern, diff --git a/core/fpdfapi/page/cpdf_color.h b/core/fpdfapi/page/cpdf_color.h index a6e640c736..9b94f0d53b 100644 --- a/core/fpdfapi/page/cpdf_color.h +++ b/core/fpdfapi/page/cpdf_color.h @@ -25,7 +25,7 @@ class CPDF_Color { void Copy(const CPDF_Color& src); void SetColorSpace(CPDF_ColorSpace* pCS); - void SetValue(const float* comp); + void SetValueForNonPattern(const std::vector<float>& values); void SetValueForPattern(CPDF_Pattern* pPattern, const std::vector<float>& values); diff --git a/core/fpdfapi/page/cpdf_colorstate.cpp b/core/fpdfapi/page/cpdf_colorstate.cpp index 16d2770600..492114ab00 100644 --- a/core/fpdfapi/page/cpdf_colorstate.cpp +++ b/core/fpdfapi/page/cpdf_colorstate.cpp @@ -70,18 +70,15 @@ bool CPDF_ColorState::HasStrokeColor() const { } void CPDF_ColorState::SetFillColor(CPDF_ColorSpace* pCS, - float* pValue, - uint32_t nValues) { + const std::vector<float>& values) { ColorData* pData = m_Ref.GetPrivateCopy(); - SetColor(pData->m_FillColor, &pData->m_FillColorRef, pCS, pValue, nValues); + SetColor(pCS, values, &pData->m_FillColor, &pData->m_FillColorRef); } void CPDF_ColorState::SetStrokeColor(CPDF_ColorSpace* pCS, - float* pValue, - uint32_t nValues) { + const std::vector<float>& values) { ColorData* pData = m_Ref.GetPrivateCopy(); - SetColor(pData->m_StrokeColor, &pData->m_StrokeColorRef, pCS, pValue, - nValues); + SetColor(pCS, values, &pData->m_StrokeColor, &pData->m_StrokeColorRef); } void CPDF_ColorState::SetFillPattern(CPDF_Pattern* pPattern, @@ -119,24 +116,27 @@ void CPDF_ColorState::SetStrokePattern(CPDF_Pattern* pPattern, pData->m_StrokeColor.GetRGB(&R, &G, &B) ? FXSYS_BGR(B, G, R) : 0xFFFFFFFF; } -void CPDF_ColorState::SetColor(CPDF_Color& color, - FX_COLORREF* colorref, - CPDF_ColorSpace* pCS, - float* pValue, - uint32_t nValues) { +void CPDF_ColorState::SetColor(CPDF_ColorSpace* pCS, + const std::vector<float>& values, + CPDF_Color* color, + FX_COLORREF* colorref) { + ASSERT(color); + ASSERT(colorref); + if (pCS) - color.SetColorSpace(pCS); - else if (color.IsNull()) - color.SetColorSpace(CPDF_ColorSpace::GetStockCS(PDFCS_DEVICEGRAY)); + color->SetColorSpace(pCS); + else if (color->IsNull()) + color->SetColorSpace(CPDF_ColorSpace::GetStockCS(PDFCS_DEVICEGRAY)); - if (color.GetColorSpace()->CountComponents() > nValues) + if (color->GetColorSpace()->CountComponents() > values.size()) return; - color.SetValue(pValue); + if (!color->IsPattern()) + color->SetValueForNonPattern(values); int R; int G; int B; - *colorref = color.GetRGB(&R, &G, &B) ? FXSYS_BGR(B, G, R) : 0xFFFFFFFF; + *colorref = color->GetRGB(&R, &G, &B) ? FXSYS_BGR(B, G, R) : 0xFFFFFFFF; } CPDF_ColorState::ColorData::ColorData() diff --git a/core/fpdfapi/page/cpdf_colorstate.h b/core/fpdfapi/page/cpdf_colorstate.h index 053e103ead..a617409268 100644 --- a/core/fpdfapi/page/cpdf_colorstate.h +++ b/core/fpdfapi/page/cpdf_colorstate.h @@ -41,8 +41,8 @@ class CPDF_ColorState { CPDF_Color* GetMutableStrokeColor(); bool HasStrokeColor() const; - void SetFillColor(CPDF_ColorSpace* pCS, float* pValue, uint32_t nValues); - void SetStrokeColor(CPDF_ColorSpace* pCS, float* pValue, uint32_t nValues); + void SetFillColor(CPDF_ColorSpace* pCS, const std::vector<float>& values); + void SetStrokeColor(CPDF_ColorSpace* pCS, const std::vector<float>& values); void SetFillPattern(CPDF_Pattern* pattern, const std::vector<float>& values); void SetStrokePattern(CPDF_Pattern* pattern, const std::vector<float>& values); @@ -64,11 +64,10 @@ class CPDF_ColorState { CPDF_Color m_StrokeColor; }; - void SetColor(CPDF_Color& color, - FX_COLORREF* colorref, - CPDF_ColorSpace* pCS, - float* pValue, - uint32_t nValues); + void SetColor(CPDF_ColorSpace* pCS, + const std::vector<float>& values, + CPDF_Color* color, + FX_COLORREF* colorref); SharedCopyOnWrite<ColorData> m_Ref; }; diff --git a/core/fpdfapi/page/cpdf_streamcontentparser.cpp b/core/fpdfapi/page/cpdf_streamcontentparser.cpp index 5d1f5efd49..c369604340 100644 --- a/core/fpdfapi/page/cpdf_streamcontentparser.cpp +++ b/core/fpdfapi/page/cpdf_streamcontentparser.cpp @@ -6,6 +6,7 @@ #include "core/fpdfapi/page/cpdf_streamcontentparser.h" +#include <algorithm> #include <memory> #include <utility> #include <vector> @@ -420,6 +421,13 @@ float CPDF_StreamContentParser::GetNumber(uint32_t index) const { return 0; } +std::vector<float> CPDF_StreamContentParser::GetNumbers(size_t count) const { + std::vector<float> values(count); + for (size_t i = 0; i < count; ++i) + values[i] = GetNumber(count - i - 1); + return values; +} + void CPDF_StreamContentParser::SetGraphicStates(CPDF_PageObject* pObj, bool bColor, bool bText, @@ -834,10 +842,7 @@ CPDF_ImageObject* CPDF_StreamContentParser::AddImageObject( std::vector<float> CPDF_StreamContentParser::GetColors() const { ASSERT(m_ParamCount > 0); - std::vector<float> values(m_ParamCount); - for (size_t i = 0; i < m_ParamCount; ++i) - values[i] = GetNumber(m_ParamCount - i - 1); - return values; + return GetNumbers(m_ParamCount); } std::vector<float> CPDF_StreamContentParser::GetNamedColors() const { @@ -881,15 +886,13 @@ void CPDF_StreamContentParser::Handle_EOFillPath() { } void CPDF_StreamContentParser::Handle_SetGray_Fill() { - float value = GetNumber(0); CPDF_ColorSpace* pCS = CPDF_ColorSpace::GetStockCS(PDFCS_DEVICEGRAY); - m_pCurStates->m_ColorState.SetFillColor(pCS, &value, 1); + m_pCurStates->m_ColorState.SetFillColor(pCS, GetNumbers(1)); } void CPDF_StreamContentParser::Handle_SetGray_Stroke() { - float value = GetNumber(0); CPDF_ColorSpace* pCS = CPDF_ColorSpace::GetStockCS(PDFCS_DEVICEGRAY); - m_pCurStates->m_ColorState.SetStrokeColor(pCS, &value, 1); + m_pCurStates->m_ColorState.SetStrokeColor(pCS, GetNumbers(1)); } void CPDF_StreamContentParser::Handle_SetExtendGraphState() { @@ -932,24 +935,16 @@ void CPDF_StreamContentParser::Handle_SetCMYKColor_Fill() { if (m_ParamCount != 4) return; - float values[4]; - for (int i = 0; i < 4; i++) { - values[i] = GetNumber(3 - i); - } CPDF_ColorSpace* pCS = CPDF_ColorSpace::GetStockCS(PDFCS_DEVICECMYK); - m_pCurStates->m_ColorState.SetFillColor(pCS, values, 4); + m_pCurStates->m_ColorState.SetFillColor(pCS, GetNumbers(4)); } void CPDF_StreamContentParser::Handle_SetCMYKColor_Stroke() { if (m_ParamCount != 4) return; - float values[4]; - for (int i = 0; i < 4; i++) { - values[i] = GetNumber(3 - i); - } CPDF_ColorSpace* pCS = CPDF_ColorSpace::GetStockCS(PDFCS_DEVICECMYK); - m_pCurStates->m_ColorState.SetStrokeColor(pCS, values, 4); + m_pCurStates->m_ColorState.SetStrokeColor(pCS, GetNumbers(4)); } void CPDF_StreamContentParser::Handle_LineTo() { @@ -1009,24 +1004,16 @@ void CPDF_StreamContentParser::Handle_SetRGBColor_Fill() { if (m_ParamCount != 3) return; - float values[3]; - for (int i = 0; i < 3; i++) { - values[i] = GetNumber(2 - i); - } CPDF_ColorSpace* pCS = CPDF_ColorSpace::GetStockCS(PDFCS_DEVICERGB); - m_pCurStates->m_ColorState.SetFillColor(pCS, values, 3); + m_pCurStates->m_ColorState.SetFillColor(pCS, GetNumbers(3)); } void CPDF_StreamContentParser::Handle_SetRGBColor_Stroke() { if (m_ParamCount != 3) return; - float values[3]; - for (int i = 0; i < 3; i++) { - values[i] = GetNumber(2 - i); - } CPDF_ColorSpace* pCS = CPDF_ColorSpace::GetStockCS(PDFCS_DEVICERGB); - m_pCurStates->m_ColorState.SetStrokeColor(pCS, values, 3); + m_pCurStates->m_ColorState.SetStrokeColor(pCS, GetNumbers(3)); } void CPDF_StreamContentParser::Handle_SetRenderIntent() {} @@ -1041,27 +1028,13 @@ void CPDF_StreamContentParser::Handle_StrokePath() { } void CPDF_StreamContentParser::Handle_SetColor_Fill() { - float values[4]; - int nargs = m_ParamCount; - if (nargs > 4) { - nargs = 4; - } - for (int i = 0; i < nargs; i++) { - values[i] = GetNumber(nargs - i - 1); - } - m_pCurStates->m_ColorState.SetFillColor(nullptr, values, nargs); + int nargs = std::min(m_ParamCount, 4U); + m_pCurStates->m_ColorState.SetFillColor(nullptr, GetNumbers(nargs)); } void CPDF_StreamContentParser::Handle_SetColor_Stroke() { - float values[4]; - int nargs = m_ParamCount; - if (nargs > 4) { - nargs = 4; - } - for (int i = 0; i < nargs; i++) { - values[i] = GetNumber(nargs - i - 1); - } - m_pCurStates->m_ColorState.SetStrokeColor(nullptr, values, nargs); + int nargs = std::min(m_ParamCount, 4U); + m_pCurStates->m_ColorState.SetStrokeColor(nullptr, GetNumbers(nargs)); } void CPDF_StreamContentParser::Handle_SetColorPS_Fill() { @@ -1070,9 +1043,7 @@ void CPDF_StreamContentParser::Handle_SetColorPS_Fill() { return; if (!pLastParam->IsName()) { - std::vector<float> values = GetColors(); - m_pCurStates->m_ColorState.SetFillColor(nullptr, values.data(), - values.size()); + m_pCurStates->m_ColorState.SetFillColor(nullptr, GetColors()); return; } @@ -1089,9 +1060,7 @@ void CPDF_StreamContentParser::Handle_SetColorPS_Stroke() { return; if (!pLastParam->IsName()) { - std::vector<float> values = GetColors(); - m_pCurStates->m_ColorState.SetStrokeColor(nullptr, values.data(), - values.size()); + m_pCurStates->m_ColorState.SetStrokeColor(nullptr, GetColors()); return; } diff --git a/core/fpdfapi/page/cpdf_streamcontentparser.h b/core/fpdfapi/page/cpdf_streamcontentparser.h index 2711a39918..438be024cc 100644 --- a/core/fpdfapi/page/cpdf_streamcontentparser.h +++ b/core/fpdfapi/page/cpdf_streamcontentparser.h @@ -92,6 +92,9 @@ class CPDF_StreamContentParser { CPDF_Object* GetObject(uint32_t index); ByteString GetString(uint32_t index) const; float GetNumber(uint32_t index) const; + // Calls GetNumber() |count| times and returns the values in reverse order. + // e.g. for |count| = 3, returns [GetNumber(2), GetNumber(1), GetNumber(0)]. + std::vector<float> GetNumbers(size_t count) const; int GetInteger(uint32_t index) const { return static_cast<int>(GetNumber(index)); } |