1 // Copyright 2016 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
7 #include "core/fpdfapi/page/cpdf_colorstate.h"
8
9 #include "core/fpdfapi/page/cpdf_pattern.h"
10 #include "core/fpdfapi/page/cpdf_tilingpattern.h"
11 #include "core/fxge/fx_dib.h"
12
CPDF_ColorState()13 CPDF_ColorState::CPDF_ColorState() {}
14
CPDF_ColorState(const CPDF_ColorState & that)15 CPDF_ColorState::CPDF_ColorState(const CPDF_ColorState& that)
16 : m_Ref(that.m_Ref) {}
17
~CPDF_ColorState()18 CPDF_ColorState::~CPDF_ColorState() {}
19
Emplace()20 void CPDF_ColorState::Emplace() {
21 m_Ref.Emplace();
22 }
23
SetDefault()24 void CPDF_ColorState::SetDefault() {
25 m_Ref.GetPrivateCopy()->SetDefault();
26 }
27
GetFillRGB() const28 uint32_t CPDF_ColorState::GetFillRGB() const {
29 return m_Ref.GetObject()->m_FillRGB;
30 }
31
SetFillRGB(uint32_t rgb)32 void CPDF_ColorState::SetFillRGB(uint32_t rgb) {
33 m_Ref.GetPrivateCopy()->m_FillRGB = rgb;
34 }
35
GetStrokeRGB() const36 uint32_t CPDF_ColorState::GetStrokeRGB() const {
37 return m_Ref.GetObject()->m_StrokeRGB;
38 }
39
SetStrokeRGB(uint32_t rgb)40 void CPDF_ColorState::SetStrokeRGB(uint32_t rgb) {
41 m_Ref.GetPrivateCopy()->m_StrokeRGB = rgb;
42 }
43
GetFillColor() const44 const CPDF_Color* CPDF_ColorState::GetFillColor() const {
45 const ColorData* pData = m_Ref.GetObject();
46 return pData ? &pData->m_FillColor : nullptr;
47 }
48
GetMutableFillColor()49 CPDF_Color* CPDF_ColorState::GetMutableFillColor() {
50 return &m_Ref.GetPrivateCopy()->m_FillColor;
51 }
52
HasFillColor() const53 bool CPDF_ColorState::HasFillColor() const {
54 const CPDF_Color* pColor = GetFillColor();
55 return pColor && !pColor->IsNull();
56 }
57
GetStrokeColor() const58 const CPDF_Color* CPDF_ColorState::GetStrokeColor() const {
59 const ColorData* pData = m_Ref.GetObject();
60 return pData ? &pData->m_StrokeColor : nullptr;
61 }
62
GetMutableStrokeColor()63 CPDF_Color* CPDF_ColorState::GetMutableStrokeColor() {
64 return &m_Ref.GetPrivateCopy()->m_StrokeColor;
65 }
66
HasStrokeColor() const67 bool CPDF_ColorState::HasStrokeColor() const {
68 const CPDF_Color* pColor = GetStrokeColor();
69 return pColor && !pColor->IsNull();
70 }
71
SetFillColor(CPDF_ColorSpace * pCS,FX_FLOAT * pValue,uint32_t nValues)72 void CPDF_ColorState::SetFillColor(CPDF_ColorSpace* pCS,
73 FX_FLOAT* pValue,
74 uint32_t nValues) {
75 ColorData* pData = m_Ref.GetPrivateCopy();
76 SetColor(pData->m_FillColor, pData->m_FillRGB, pCS, pValue, nValues);
77 }
78
SetStrokeColor(CPDF_ColorSpace * pCS,FX_FLOAT * pValue,uint32_t nValues)79 void CPDF_ColorState::SetStrokeColor(CPDF_ColorSpace* pCS,
80 FX_FLOAT* pValue,
81 uint32_t nValues) {
82 ColorData* pData = m_Ref.GetPrivateCopy();
83 SetColor(pData->m_StrokeColor, pData->m_StrokeRGB, pCS, pValue, nValues);
84 }
85
SetFillPattern(CPDF_Pattern * pPattern,FX_FLOAT * pValue,uint32_t nValues)86 void CPDF_ColorState::SetFillPattern(CPDF_Pattern* pPattern,
87 FX_FLOAT* pValue,
88 uint32_t nValues) {
89 ColorData* pData = m_Ref.GetPrivateCopy();
90 pData->m_FillColor.SetValue(pPattern, pValue, nValues);
91 int R, G, B;
92 bool ret = pData->m_FillColor.GetRGB(R, G, B);
93 if (CPDF_TilingPattern* pTilingPattern = pPattern->AsTilingPattern()) {
94 if (!ret && pTilingPattern->colored()) {
95 pData->m_FillRGB = 0x00BFBFBF;
96 return;
97 }
98 }
99 pData->m_FillRGB = ret ? FXSYS_RGB(R, G, B) : (uint32_t)-1;
100 }
101
SetStrokePattern(CPDF_Pattern * pPattern,FX_FLOAT * pValue,uint32_t nValues)102 void CPDF_ColorState::SetStrokePattern(CPDF_Pattern* pPattern,
103 FX_FLOAT* pValue,
104 uint32_t nValues) {
105 ColorData* pData = m_Ref.GetPrivateCopy();
106 pData->m_StrokeColor.SetValue(pPattern, pValue, nValues);
107 int R, G, B;
108 bool ret = pData->m_StrokeColor.GetRGB(R, G, B);
109 if (CPDF_TilingPattern* pTilingPattern = pPattern->AsTilingPattern()) {
110 if (!ret && pTilingPattern->colored()) {
111 pData->m_StrokeRGB = 0x00BFBFBF;
112 return;
113 }
114 }
115 pData->m_StrokeRGB =
116 pData->m_StrokeColor.GetRGB(R, G, B) ? FXSYS_RGB(R, G, B) : (uint32_t)-1;
117 }
118
SetColor(CPDF_Color & color,uint32_t & rgb,CPDF_ColorSpace * pCS,FX_FLOAT * pValue,uint32_t nValues)119 void CPDF_ColorState::SetColor(CPDF_Color& color,
120 uint32_t& rgb,
121 CPDF_ColorSpace* pCS,
122 FX_FLOAT* pValue,
123 uint32_t nValues) {
124 if (pCS)
125 color.SetColorSpace(pCS);
126 else if (color.IsNull())
127 color.SetColorSpace(CPDF_ColorSpace::GetStockCS(PDFCS_DEVICEGRAY));
128
129 if (color.GetColorSpace()->CountComponents() > nValues)
130 return;
131
132 color.SetValue(pValue);
133 int R;
134 int G;
135 int B;
136 rgb = color.GetRGB(R, G, B) ? FXSYS_RGB(R, G, B) : (uint32_t)-1;
137 }
138
ColorData()139 CPDF_ColorState::ColorData::ColorData() : m_FillRGB(0), m_StrokeRGB(0) {}
140
ColorData(const ColorData & src)141 CPDF_ColorState::ColorData::ColorData(const ColorData& src)
142 : m_FillRGB(src.m_FillRGB), m_StrokeRGB(src.m_StrokeRGB) {
143 m_FillColor.Copy(&src.m_FillColor);
144 m_StrokeColor.Copy(&src.m_StrokeColor);
145 }
146
~ColorData()147 CPDF_ColorState::ColorData::~ColorData() {}
148
SetDefault()149 void CPDF_ColorState::ColorData::SetDefault() {
150 m_FillRGB = 0;
151 m_StrokeRGB = 0;
152 m_FillColor.SetColorSpace(CPDF_ColorSpace::GetStockCS(PDFCS_DEVICEGRAY));
153 m_StrokeColor.SetColorSpace(CPDF_ColorSpace::GetStockCS(PDFCS_DEVICEGRAY));
154 }
155