From 996c93068bfc8b443c77b735bc6400285bc8a407 Mon Sep 17 00:00:00 2001 From: Lei Zhang Date: Fri, 13 Apr 2018 15:44:36 +0000 Subject: Rename the other CPDF_Color::SetValue() variant. Rename it to SetValueForNonePattern() and combine the components parameters into a std::vector. Fix the callers to use std::vector as well. Change-Id: I0edd7e7876e47b56821e63cc5073fc21fd4098ee Reviewed-on: https://pdfium-review.googlesource.com/30470 Commit-Queue: Lei Zhang Reviewed-by: Henrique Nakashima --- .../edit/cpdf_pagecontentgenerator_unittest.cpp | 18 +++--- core/fpdfapi/page/cpdf_color.cpp | 10 +-- core/fpdfapi/page/cpdf_color.h | 2 +- core/fpdfapi/page/cpdf_colorstate.cpp | 36 +++++------ core/fpdfapi/page/cpdf_colorstate.h | 13 ++-- core/fpdfapi/page/cpdf_streamcontentparser.cpp | 73 +++++++--------------- core/fpdfapi/page/cpdf_streamcontentparser.h | 3 + fpdfsdk/fpdf_editpage.cpp | 5 +- fpdfsdk/fpdf_editpath.cpp | 4 +- 9 files changed, 68 insertions(+), 96 deletions(-) diff --git a/core/fpdfapi/edit/cpdf_pagecontentgenerator_unittest.cpp b/core/fpdfapi/edit/cpdf_pagecontentgenerator_unittest.cpp index 403bd6ab03..e9b3d15d30 100644 --- a/core/fpdfapi/edit/cpdf_pagecontentgenerator_unittest.cpp +++ b/core/fpdfapi/edit/cpdf_pagecontentgenerator_unittest.cpp @@ -117,12 +117,12 @@ TEST_F(CPDF_PageContentGeneratorTest, ProcessGraphics) { pPathObj->m_FillType = FXFILL_WINDING; pPathObj->m_bStroke = true; - float rgb[3] = {0.5f, 0.7f, 0.35f}; + static const std::vector rgb = {0.5f, 0.7f, 0.35f}; CPDF_ColorSpace* pCS = CPDF_ColorSpace::GetStockCS(PDFCS_DEVICERGB); - pPathObj->m_ColorState.SetFillColor(pCS, rgb, 3); + pPathObj->m_ColorState.SetFillColor(pCS, rgb); - float rgb2[3] = {1, 0.9f, 0}; - pPathObj->m_ColorState.SetStrokeColor(pCS, rgb2, 3); + static const std::vector rgb2 = {1, 0.9f, 0}; + pPathObj->m_ColorState.SetStrokeColor(pCS, rgb2); pPathObj->m_GeneralState.SetFillAlpha(0.5f); pPathObj->m_GeneralState.SetStrokeAlpha(0.8f); @@ -140,7 +140,7 @@ TEST_F(CPDF_PageContentGeneratorTest, ProcessGraphics) { pathString.Left(48)); EXPECT_EQ(" gs 1 0 0 1 0 0 cm 1 2 m 3 4 l 5 6 l h B Q\n", pathString.Right(43)); - ASSERT_TRUE(pathString.GetLength() > 91); + ASSERT_GT(pathString.GetLength(), 91U); CPDF_Dictionary* externalGS = TestGetResource( &generator, "ExtGState", pathString.Mid(48, pathString.GetLength() - 91)); ASSERT_TRUE(externalGS); @@ -174,12 +174,12 @@ TEST_F(CPDF_PageContentGeneratorTest, ProcessStandardText) { CPDF_Font* pFont = CPDF_Font::GetStockFont(pDoc.get(), "Times-Roman"); pTextObj->m_TextState.SetFont(pFont); pTextObj->m_TextState.SetFontSize(10.0f); - float rgb[3] = {0.5f, 0.7f, 0.35f}; + static const std::vector rgb = {0.5f, 0.7f, 0.35f}; CPDF_ColorSpace* pCS = CPDF_ColorSpace::GetStockCS(PDFCS_DEVICERGB); - pTextObj->m_ColorState.SetFillColor(pCS, rgb, 3); + pTextObj->m_ColorState.SetFillColor(pCS, rgb); - float rgb2[3] = {1, 0.9f, 0}; - pTextObj->m_ColorState.SetStrokeColor(pCS, rgb2, 3); + static const std::vector rgb2 = {1, 0.9f, 0}; + pTextObj->m_ColorState.SetStrokeColor(pCS, rgb2); pTextObj->m_GeneralState.SetFillAlpha(0.5f); pTextObj->m_GeneralState.SetStrokeAlpha(0.8f); pTextObj->Transform(CFX_Matrix(1, 0, 0, 1, 100, 100)); 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& 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& values); void SetValueForPattern(CPDF_Pattern* pPattern, const std::vector& 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& 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& 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& 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& values); + void SetStrokeColor(CPDF_ColorSpace* pCS, const std::vector& values); void SetFillPattern(CPDF_Pattern* pattern, const std::vector& values); void SetStrokePattern(CPDF_Pattern* pattern, const std::vector& 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& values, + CPDF_Color* color, + FX_COLORREF* colorref); SharedCopyOnWrite 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 #include #include #include @@ -420,6 +421,13 @@ float CPDF_StreamContentParser::GetNumber(uint32_t index) const { return 0; } +std::vector CPDF_StreamContentParser::GetNumbers(size_t count) const { + std::vector 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 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; + return GetNumbers(m_ParamCount); } std::vector 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 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 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 GetNumbers(size_t count) const; int GetInteger(uint32_t index) const { return static_cast(GetNumber(index)); } diff --git a/fpdfsdk/fpdf_editpage.cpp b/fpdfsdk/fpdf_editpage.cpp index bc494f8d85..20d88942c8 100644 --- a/fpdfsdk/fpdf_editpage.cpp +++ b/fpdfsdk/fpdf_editpage.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include "core/fpdfapi/edit/cpdf_pagecontentgenerator.h" #include "core/fpdfapi/page/cpdf_form.h" @@ -399,11 +400,11 @@ FPDF_BOOL FPDFPageObj_SetFillColor(FPDF_PAGEOBJECT page_object, if (!page_object || R > 255 || G > 255 || B > 255 || A > 255) return false; - float rgb[3] = {R / 255.f, G / 255.f, B / 255.f}; + std::vector rgb = {R / 255.f, G / 255.f, B / 255.f}; auto* pPageObj = CPDFPageObjectFromFPDFPageObject(page_object); pPageObj->m_GeneralState.SetFillAlpha(A / 255.f); pPageObj->m_ColorState.SetFillColor( - CPDF_ColorSpace::GetStockCS(PDFCS_DEVICERGB), rgb, 3); + CPDF_ColorSpace::GetStockCS(PDFCS_DEVICERGB), rgb); pPageObj->SetDirty(true); return true; } diff --git a/fpdfsdk/fpdf_editpath.cpp b/fpdfsdk/fpdf_editpath.cpp index 2daec67e36..82c7ca13ef 100644 --- a/fpdfsdk/fpdf_editpath.cpp +++ b/fpdfsdk/fpdf_editpath.cpp @@ -80,10 +80,10 @@ FPDFPath_SetStrokeColor(FPDF_PAGEOBJECT path, if (!pPathObj || R > 255 || G > 255 || B > 255 || A > 255) return false; - float rgb[3] = {R / 255.f, G / 255.f, B / 255.f}; + std::vector rgb = {R / 255.f, G / 255.f, B / 255.f}; pPathObj->m_GeneralState.SetStrokeAlpha(A / 255.f); pPathObj->m_ColorState.SetStrokeColor( - CPDF_ColorSpace::GetStockCS(PDFCS_DEVICERGB), rgb, 3); + CPDF_ColorSpace::GetStockCS(PDFCS_DEVICERGB), rgb); pPathObj->SetDirty(true); return true; } -- cgit v1.2.3