diff options
author | tsepez <tsepez@chromium.org> | 2016-08-31 10:41:00 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2016-08-31 10:41:00 -0700 |
commit | ca90b8742ac780d492f96181bdd52b8d9c17a227 (patch) | |
tree | d0f0417ce3d36ae9181df09317de016fb64caec6 /core/fpdfapi | |
parent | 4d9df422c340b3bc33e044d3d94e1ff9582e1260 (diff) | |
download | pdfium-ca90b8742ac780d492f96181bdd52b8d9c17a227.tar.xz |
Make CPDF_ColorState have a CFX_ColorStateData rather than inheriting
Review-Url: https://codereview.chromium.org/2294133002
Diffstat (limited to 'core/fpdfapi')
-rw-r--r-- | core/fpdfapi/fpdf_page/cpdf_colorstate.cpp | 105 | ||||
-rw-r--r-- | core/fpdfapi/fpdf_page/cpdf_colorstate.h | 36 | ||||
-rw-r--r-- | core/fpdfapi/fpdf_page/cpdf_graphicstates.cpp | 3 | ||||
-rw-r--r-- | core/fpdfapi/fpdf_page/fpdf_page_parser.cpp | 18 | ||||
-rw-r--r-- | core/fpdfapi/fpdf_page/fpdf_page_parser_old.cpp | 2 | ||||
-rw-r--r-- | core/fpdfapi/fpdf_render/fpdf_render.cpp | 37 |
6 files changed, 137 insertions, 64 deletions
diff --git a/core/fpdfapi/fpdf_page/cpdf_colorstate.cpp b/core/fpdfapi/fpdf_page/cpdf_colorstate.cpp index a2234378eb..e4e266785a 100644 --- a/core/fpdfapi/fpdf_page/cpdf_colorstate.cpp +++ b/core/fpdfapi/fpdf_page/cpdf_colorstate.cpp @@ -10,42 +10,83 @@ #include "core/fpdfapi/fpdf_page/cpdf_tilingpattern.h" #include "core/fxge/include/fx_dib.h" +CPDF_ColorState::CPDF_ColorState() {} + +CPDF_ColorState::CPDF_ColorState(const CPDF_ColorState& that) + : m_Ref(that.m_Ref) {} + +CPDF_ColorState::~CPDF_ColorState() {} + +void CPDF_ColorState::Emplace() { + m_Ref.Emplace(); +} + +void CPDF_ColorState::SetDefault() { + m_Ref.GetPrivateCopy()->Default(); +} + +uint32_t CPDF_ColorState::GetFillRGB() const { + return m_Ref.GetObject()->m_FillRGB; +} + +void CPDF_ColorState::SetFillRGB(uint32_t rgb) { + m_Ref.GetPrivateCopy()->m_FillRGB = rgb; +} + +uint32_t CPDF_ColorState::GetStrokeRGB() const { + return m_Ref.GetObject()->m_StrokeRGB; +} + +void CPDF_ColorState::SetStrokeRGB(uint32_t rgb) { + m_Ref.GetPrivateCopy()->m_StrokeRGB = rgb; +} + +const CPDF_Color* CPDF_ColorState::GetFillColor() const { + const CPDF_ColorStateData* pData = m_Ref.GetObject(); + return pData ? &pData->m_FillColor : nullptr; +} + +CPDF_Color* CPDF_ColorState::GetMutableFillColor() { + return &m_Ref.GetPrivateCopy()->m_FillColor; +} + +bool CPDF_ColorState::HasFillColor() const { + const CPDF_Color* pColor = GetFillColor(); + return pColor && !pColor->IsNull(); +} + +const CPDF_Color* CPDF_ColorState::GetStrokeColor() const { + const CPDF_ColorStateData* pData = m_Ref.GetObject(); + return pData ? &pData->m_StrokeColor : nullptr; +} + +CPDF_Color* CPDF_ColorState::GetMutableStrokeColor() { + return &m_Ref.GetPrivateCopy()->m_StrokeColor; +} + +bool CPDF_ColorState::HasStrokeColor() const { + const CPDF_Color* pColor = GetStrokeColor(); + return pColor && !pColor->IsNull(); +} + void CPDF_ColorState::SetFillColor(CPDF_ColorSpace* pCS, FX_FLOAT* pValue, uint32_t nValues) { - CPDF_ColorStateData* pData = GetPrivateCopy(); + CPDF_ColorStateData* pData = m_Ref.GetPrivateCopy(); SetColor(pData->m_FillColor, pData->m_FillRGB, pCS, pValue, nValues); } void CPDF_ColorState::SetStrokeColor(CPDF_ColorSpace* pCS, FX_FLOAT* pValue, uint32_t nValues) { - CPDF_ColorStateData* pData = GetPrivateCopy(); + CPDF_ColorStateData* pData = m_Ref.GetPrivateCopy(); SetColor(pData->m_StrokeColor, pData->m_StrokeRGB, pCS, pValue, nValues); } -void CPDF_ColorState::SetColor(CPDF_Color& color, - uint32_t& rgb, - CPDF_ColorSpace* pCS, - FX_FLOAT* pValue, - uint32_t nValues) { - if (pCS) { - color.SetColorSpace(pCS); - } else if (color.IsNull()) { - color.SetColorSpace(CPDF_ColorSpace::GetStockCS(PDFCS_DEVICEGRAY)); - } - if (color.GetColorSpace()->CountComponents() > nValues) - return; - - color.SetValue(pValue); - int R, G, B; - rgb = color.GetRGB(R, G, B) ? FXSYS_RGB(R, G, B) : (uint32_t)-1; -} - void CPDF_ColorState::SetFillPattern(CPDF_Pattern* pPattern, FX_FLOAT* pValue, uint32_t nValues) { - CPDF_ColorStateData* pData = GetPrivateCopy(); + CPDF_ColorStateData* pData = m_Ref.GetPrivateCopy(); pData->m_FillColor.SetValue(pPattern, pValue, nValues); int R, G, B; FX_BOOL ret = pData->m_FillColor.GetRGB(R, G, B); @@ -61,7 +102,7 @@ void CPDF_ColorState::SetFillPattern(CPDF_Pattern* pPattern, void CPDF_ColorState::SetStrokePattern(CPDF_Pattern* pPattern, FX_FLOAT* pValue, uint32_t nValues) { - CPDF_ColorStateData* pData = GetPrivateCopy(); + CPDF_ColorStateData* pData = m_Ref.GetPrivateCopy(); pData->m_StrokeColor.SetValue(pPattern, pValue, nValues); int R, G, B; FX_BOOL ret = pData->m_StrokeColor.GetRGB(R, G, B); @@ -74,3 +115,23 @@ void CPDF_ColorState::SetStrokePattern(CPDF_Pattern* pPattern, pData->m_StrokeRGB = pData->m_StrokeColor.GetRGB(R, G, B) ? FXSYS_RGB(R, G, B) : (uint32_t)-1; } + +void CPDF_ColorState::SetColor(CPDF_Color& color, + uint32_t& rgb, + CPDF_ColorSpace* pCS, + FX_FLOAT* pValue, + uint32_t nValues) { + if (pCS) + color.SetColorSpace(pCS); + else if (color.IsNull()) + color.SetColorSpace(CPDF_ColorSpace::GetStockCS(PDFCS_DEVICEGRAY)); + + if (color.GetColorSpace()->CountComponents() > nValues) + return; + + color.SetValue(pValue); + int R; + int G; + int B; + rgb = color.GetRGB(R, G, B) ? FXSYS_RGB(R, G, B) : (uint32_t)-1; +} diff --git a/core/fpdfapi/fpdf_page/cpdf_colorstate.h b/core/fpdfapi/fpdf_page/cpdf_colorstate.h index 128d67558d..d928dbdb2e 100644 --- a/core/fpdfapi/fpdf_page/cpdf_colorstate.h +++ b/core/fpdfapi/fpdf_page/cpdf_colorstate.h @@ -16,17 +16,28 @@ class CPDF_Color; class CPDF_ColorSpace; class CPDF_Pattern; -class CPDF_ColorState : public CFX_CountRef<CPDF_ColorStateData> { +class CPDF_ColorState { public: - const CPDF_Color* GetFillColor() const { - const CPDF_ColorStateData* pData = GetObject(); - return pData ? &pData->m_FillColor : nullptr; - } + CPDF_ColorState(); + CPDF_ColorState(const CPDF_ColorState& that); + ~CPDF_ColorState(); - const CPDF_Color* GetStrokeColor() const { - const CPDF_ColorStateData* pData = GetObject(); - return pData ? &pData->m_StrokeColor : nullptr; - } + void Emplace(); + void SetDefault(); + + uint32_t GetFillRGB() const; + void SetFillRGB(uint32_t rgb); + + uint32_t GetStrokeRGB() const; + void SetStrokeRGB(uint32_t rgb); + + const CPDF_Color* GetFillColor() const; + CPDF_Color* GetMutableFillColor(); + bool HasFillColor() const; + + const CPDF_Color* GetStrokeColor() const; + CPDF_Color* GetMutableStrokeColor(); + bool HasStrokeColor() const; void SetFillColor(CPDF_ColorSpace* pCS, FX_FLOAT* pValue, uint32_t nValues); void SetStrokeColor(CPDF_ColorSpace* pCS, FX_FLOAT* pValue, uint32_t nValues); @@ -37,12 +48,19 @@ class CPDF_ColorState : public CFX_CountRef<CPDF_ColorStateData> { FX_FLOAT* pValue, uint32_t nValues); + // TODO(tsepez): Stop leaking ColorStateData outside this class. + const CPDF_ColorStateData* GetObject() const { return m_Ref.GetObject(); } + + operator bool() const { return !!m_Ref; } + private: void SetColor(CPDF_Color& color, uint32_t& rgb, CPDF_ColorSpace* pCS, FX_FLOAT* pValue, uint32_t nValues); + + CFX_CountRef<CPDF_ColorStateData> m_Ref; }; #endif // CORE_FPDFAPI_FPDF_PAGE_CPDF_COLORSTATE_H_ diff --git a/core/fpdfapi/fpdf_page/cpdf_graphicstates.cpp b/core/fpdfapi/fpdf_page/cpdf_graphicstates.cpp index e411b53a51..42f26c1135 100644 --- a/core/fpdfapi/fpdf_page/cpdf_graphicstates.cpp +++ b/core/fpdfapi/fpdf_page/cpdf_graphicstates.cpp @@ -11,7 +11,8 @@ CPDF_GraphicStates::CPDF_GraphicStates() {} CPDF_GraphicStates::~CPDF_GraphicStates() {} void CPDF_GraphicStates::DefaultStates() { - m_ColorState.Emplace()->Default(); + m_ColorState.Emplace(); + m_ColorState.SetDefault(); } void CPDF_GraphicStates::CopyStates(const CPDF_GraphicStates& src) { diff --git a/core/fpdfapi/fpdf_page/fpdf_page_parser.cpp b/core/fpdfapi/fpdf_page/fpdf_page_parser.cpp index 70e1d1aea6..5272a4464b 100644 --- a/core/fpdfapi/fpdf_page/fpdf_page_parser.cpp +++ b/core/fpdfapi/fpdf_page/fpdf_page_parser.cpp @@ -686,21 +686,19 @@ void CPDF_StreamContentParser::Handle_ConcatMatrix() { } void CPDF_StreamContentParser::Handle_SetColorSpace_Fill() { - CFX_ByteString csname = GetString(0); - CPDF_ColorSpace* pCS = FindColorSpace(csname); - if (!pCS) { + CPDF_ColorSpace* pCS = FindColorSpace(GetString(0)); + if (!pCS) return; - } - m_pCurStates->m_ColorState.GetPrivateCopy()->m_FillColor.SetColorSpace(pCS); + + m_pCurStates->m_ColorState.GetMutableFillColor()->SetColorSpace(pCS); } void CPDF_StreamContentParser::Handle_SetColorSpace_Stroke() { - CFX_ByteString csname = GetString(0); - CPDF_ColorSpace* pCS = FindColorSpace(csname); - if (!pCS) { + CPDF_ColorSpace* pCS = FindColorSpace(GetString(0)); + if (!pCS) return; - } - m_pCurStates->m_ColorState.GetPrivateCopy()->m_StrokeColor.SetColorSpace(pCS); + + m_pCurStates->m_ColorState.GetMutableStrokeColor()->SetColorSpace(pCS); } void CPDF_StreamContentParser::Handle_SetDash() { diff --git a/core/fpdfapi/fpdf_page/fpdf_page_parser_old.cpp b/core/fpdfapi/fpdf_page/fpdf_page_parser_old.cpp index e261ce5bb5..92a9d6c92e 100644 --- a/core/fpdfapi/fpdf_page/fpdf_page_parser_old.cpp +++ b/core/fpdfapi/fpdf_page/fpdf_page_parser_old.cpp @@ -774,7 +774,7 @@ void CPDF_ContentParser::Continue(IFX_Pause* pPause) { m_pObjectHolder->m_pDocument, m_pObjectHolder->m_pPageResources, nullptr, nullptr, m_pObjectHolder, m_pObjectHolder->m_pResources, &m_pObjectHolder->m_BBox, nullptr, 0)); - m_pParser->GetCurStates()->m_ColorState.GetPrivateCopy()->Default(); + m_pParser->GetCurStates()->m_ColorState.SetDefault(); } if (m_CurrentOffset >= m_Size) { m_InternalStage = STAGE_CHECKCLIP; diff --git a/core/fpdfapi/fpdf_render/fpdf_render.cpp b/core/fpdfapi/fpdf_render/fpdf_render.cpp index c36d7f4cd3..23443b15c5 100644 --- a/core/fpdfapi/fpdf_render/fpdf_render.cpp +++ b/core/fpdfapi/fpdf_render/fpdf_render.cpp @@ -194,21 +194,17 @@ FX_BOOL CPDF_RenderStatus::Initialize(CPDF_RenderContext* pContext, if (pInitialStates && !m_pType3Char) { m_InitialStates.CopyStates(*pInitialStates); if (pParentState) { - const CPDF_ColorStateData* pColorData = - m_InitialStates.m_ColorState.GetObject(); - const CPDF_ColorStateData* pParentData = - pParentState->m_InitialStates.m_ColorState.GetObject(); - if (!pColorData || pColorData->m_FillColor.IsNull()) { - CPDF_ColorStateData* pData = - m_InitialStates.m_ColorState.GetPrivateCopy(); - pData->m_FillRGB = pParentData->m_FillRGB; - pData->m_FillColor.Copy(&pParentData->m_FillColor); + if (!m_InitialStates.m_ColorState.HasFillColor()) { + m_InitialStates.m_ColorState.SetFillRGB( + pParentState->m_InitialStates.m_ColorState.GetFillRGB()); + m_InitialStates.m_ColorState.GetMutableFillColor()->Copy( + pParentState->m_InitialStates.m_ColorState.GetFillColor()); } - if (!pColorData || pColorData->m_StrokeColor.IsNull()) { - CPDF_ColorStateData* pData = - m_InitialStates.m_ColorState.GetPrivateCopy(); - pData->m_StrokeRGB = pParentData->m_FillRGB; - pData->m_StrokeColor.Copy(&pParentData->m_StrokeColor); + if (!m_InitialStates.m_ColorState.HasStrokeColor()) { + m_InitialStates.m_ColorState.SetStrokeRGB( + pParentState->m_InitialStates.m_ColorState.GetFillRGB()); + m_InitialStates.m_ColorState.GetMutableStrokeColor()->Copy( + pParentState->m_InitialStates.m_ColorState.GetStrokeColor()); } } } else { @@ -912,20 +908,19 @@ void CPDF_RenderContext::GetBackground(CFX_DIBitmap* pBuffer, CPDF_GraphicStates* CPDF_RenderStatus::CloneObjStates( const CPDF_GraphicStates* pSrcStates, FX_BOOL bStroke) { - if (!pSrcStates) { + if (!pSrcStates) return nullptr; - } + CPDF_GraphicStates* pStates = new CPDF_GraphicStates; pStates->CopyStates(*pSrcStates); const CPDF_Color* pObjColor = bStroke ? pSrcStates->m_ColorState.GetStrokeColor() : pSrcStates->m_ColorState.GetFillColor(); if (!pObjColor->IsNull()) { - CPDF_ColorStateData* pColorData = pStates->m_ColorState.GetPrivateCopy(); - pColorData->m_FillRGB = - bStroke ? pSrcStates->m_ColorState.GetObject()->m_StrokeRGB - : pSrcStates->m_ColorState.GetObject()->m_FillRGB; - pColorData->m_StrokeRGB = pColorData->m_FillRGB; + pStates->m_ColorState.SetFillRGB( + bStroke ? pSrcStates->m_ColorState.GetStrokeRGB() + : pSrcStates->m_ColorState.GetFillRGB()); + pStates->m_ColorState.SetStrokeRGB(pStates->m_ColorState.GetFillRGB()); } return pStates; } |