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,float * pValue,uint32_t nValues)72 void CPDF_ColorState::SetFillColor(CPDF_ColorSpace* pCS,
73 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,float * pValue,uint32_t nValues)79 void CPDF_ColorState::SetStrokeColor(CPDF_ColorSpace* pCS,
80 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,float * pValue,uint32_t nValues)86 void CPDF_ColorState::SetFillPattern(CPDF_Pattern* pPattern,
87 float* pValue,
88 uint32_t nValues) {
89 ColorData* pData = m_Ref.GetPrivateCopy();
90 pData->m_FillColor.SetValue(pPattern, pValue, nValues);
91 int R;
92 int G;
93 int B;
94 bool ret = pData->m_FillColor.GetRGB(&R, &G, &B);
95 if (CPDF_TilingPattern* pTilingPattern = pPattern->AsTilingPattern()) {
96 if (!ret && pTilingPattern->colored()) {
97 pData->m_FillRGB = 0x00BFBFBF;
98 return;
99 }
100 }
101 pData->m_FillRGB = ret ? FXSYS_RGB(R, G, B) : 0xFFFFFFFF;
102 }
103
SetStrokePattern(CPDF_Pattern * pPattern,float * pValue,uint32_t nValues)104 void CPDF_ColorState::SetStrokePattern(CPDF_Pattern* pPattern,
105 float* pValue,
106 uint32_t nValues) {
107 ColorData* pData = m_Ref.GetPrivateCopy();
108 pData->m_StrokeColor.SetValue(pPattern, pValue, nValues);
109 int R;
110 int G;
111 int B;
112 bool ret = pData->m_StrokeColor.GetRGB(&R, &G, &B);
113 if (CPDF_TilingPattern* pTilingPattern = pPattern->AsTilingPattern()) {
114 if (!ret && pTilingPattern->colored()) {
115 pData->m_StrokeRGB = 0x00BFBFBF;
116 return;
117 }
118 }
119 pData->m_StrokeRGB =
120 pData->m_StrokeColor.GetRGB(&R, &G, &B) ? FXSYS_RGB(R, G, B) : 0xFFFFFFFF;
121 }
122
SetColor(CPDF_Color & color,uint32_t & rgb,CPDF_ColorSpace * pCS,float * pValue,uint32_t nValues)123 void CPDF_ColorState::SetColor(CPDF_Color& color,
124 uint32_t& rgb,
125 CPDF_ColorSpace* pCS,
126 float* pValue,
127 uint32_t nValues) {
128 if (pCS)
129 color.SetColorSpace(pCS);
130 else if (color.IsNull())
131 color.SetColorSpace(CPDF_ColorSpace::GetStockCS(PDFCS_DEVICEGRAY));
132
133 if (color.GetColorSpace()->CountComponents() > nValues)
134 return;
135
136 color.SetValue(pValue);
137 int R;
138 int G;
139 int B;
140 rgb = color.GetRGB(&R, &G, &B) ? FXSYS_RGB(R, G, B) : 0xFFFFFFFF;
141 }
142
ColorData()143 CPDF_ColorState::ColorData::ColorData() : m_FillRGB(0), m_StrokeRGB(0) {}
144
ColorData(const ColorData & src)145 CPDF_ColorState::ColorData::ColorData(const ColorData& src)
146 : m_FillRGB(src.m_FillRGB), m_StrokeRGB(src.m_StrokeRGB) {
147 m_FillColor.Copy(&src.m_FillColor);
148 m_StrokeColor.Copy(&src.m_StrokeColor);
149 }
150
~ColorData()151 CPDF_ColorState::ColorData::~ColorData() {}
152
SetDefault()153 void CPDF_ColorState::ColorData::SetDefault() {
154 m_FillRGB = 0;
155 m_StrokeRGB = 0;
156 m_FillColor.SetColorSpace(CPDF_ColorSpace::GetStockCS(PDFCS_DEVICEGRAY));
157 m_StrokeColor.SetColorSpace(CPDF_ColorSpace::GetStockCS(PDFCS_DEVICEGRAY));
158 }
159