1 // Copyright 2014 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.
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
7 #include "../../../include/fpdfapi/fpdf_page.h"
10 CPDF_Pattern::CPDF_Pattern(const CFX_AffineMatrix* pParentMatrix) :
11 m_pPatternObj(NULL), m_PatternType(PATTERN_TILING), m_pDocument(NULL), m_pColor(NULL)
14 m_ParentMatrix = *pParentMatrix;
18 CPDF_Pattern::~CPDF_Pattern()
21 m_pColor->SetValue(NULL, NULL, 0);
25 CPDF_TilingPattern::CPDF_TilingPattern(CPDF_Document* pDoc, CPDF_Object* pPatternObj, const CFX_AffineMatrix* parentMatrix) :
26 CPDF_Pattern(parentMatrix)
28 m_PatternType = PATTERN_TILING;
29 m_pPatternObj = pPatternObj;
31 CPDF_Dictionary* pDict = m_pPatternObj->GetDict();
32 ASSERT(pDict != NULL);
33 m_Pattern2Form = pDict->GetMatrix(FX_BSTRC("Matrix"));
34 m_bColored = pDict->GetInteger(FX_BSTRC("PaintType")) == 1;
36 m_Pattern2Form.Concat(*parentMatrix);
40 CPDF_TilingPattern::~CPDF_TilingPattern()
47 FX_BOOL CPDF_TilingPattern::Load()
49 if (m_pForm != NULL) {
52 CPDF_Dictionary* pDict = m_pPatternObj->GetDict();
56 m_bColored = pDict->GetInteger(FX_BSTRC("PaintType")) == 1;
57 m_XStep = (FX_FLOAT)FXSYS_fabs(pDict->GetNumber(FX_BSTRC("XStep")));
58 m_YStep = (FX_FLOAT)FXSYS_fabs(pDict->GetNumber(FX_BSTRC("YStep")));
59 if (m_pPatternObj->GetType() != PDFOBJ_STREAM) {
62 CPDF_Stream* pStream = (CPDF_Stream*)m_pPatternObj;
63 m_pForm = FX_NEW CPDF_Form(m_pDocument, NULL, pStream);
64 m_pForm->ParseContent(NULL, &m_ParentMatrix, NULL, NULL);
65 m_BBox = pDict->GetRect(FX_BSTRC("BBox"));
68 CPDF_ShadingPattern::CPDF_ShadingPattern(CPDF_Document* pDoc, CPDF_Object* pPatternObj, FX_BOOL bShading, const CFX_AffineMatrix* parentMatrix) : CPDF_Pattern(parentMatrix)
70 m_PatternType = PATTERN_SHADING;
71 m_pPatternObj = bShading ? NULL : pPatternObj;
73 m_bShadingObj = bShading;
75 CPDF_Dictionary* pDict = m_pPatternObj->GetDict();
76 ASSERT(pDict != NULL);
77 m_Pattern2Form = pDict->GetMatrix(FX_BSTRC("Matrix"));
78 m_pShadingObj = pDict->GetElementValue(FX_BSTRC("Shading"));
80 m_Pattern2Form.Concat(*parentMatrix);
83 m_pShadingObj = pPatternObj;
88 for (int i = 0; i < 4; i ++) {
89 m_pFunctions[i] = NULL;
92 CPDF_ShadingPattern::~CPDF_ShadingPattern()
96 void CPDF_ShadingPattern::Clear()
98 for (int i = 0; i < m_nFuncs; i ++) {
99 if (m_pFunctions[i]) {
100 delete m_pFunctions[i];
102 m_pFunctions[i] = NULL;
104 CPDF_ColorSpace* pCS = m_pCS;
105 if (pCS && m_pDocument) {
106 m_pDocument->GetPageData()->ReleaseColorSpace(pCS->GetArray());
112 FX_BOOL CPDF_ShadingPattern::Load()
114 if (m_ShadingType != 0) {
117 CPDF_Dictionary* pShadingDict = m_pShadingObj ? m_pShadingObj->GetDict() : NULL;
118 if (pShadingDict == NULL) {
122 for (int i = 0; i < m_nFuncs; i ++)
123 if (m_pFunctions[i]) {
124 delete m_pFunctions[i];
128 CPDF_Object* pFunc = pShadingDict->GetElementValue(FX_BSTRC("Function"));
130 if (pFunc->GetType() == PDFOBJ_ARRAY) {
131 m_nFuncs = ((CPDF_Array*)pFunc)->GetCount();
135 for (int i = 0; i < m_nFuncs; i ++) {
136 m_pFunctions[i] = CPDF_Function::Load(((CPDF_Array*)pFunc)->GetElementValue(i));
139 m_pFunctions[0] = CPDF_Function::Load(pFunc);
143 CPDF_Object* pCSObj = pShadingDict->GetElementValue(FX_BSTRC("ColorSpace"));
144 if (pCSObj == NULL) {
147 CPDF_DocPageData* pDocPageData = m_pDocument->GetPageData();
148 m_pCS = pDocPageData->GetColorSpace(pCSObj, NULL);
149 m_ShadingType = pShadingDict->GetInteger(FX_BSTRC("ShadingType"));
152 FX_BOOL CPDF_ShadingPattern::Reload()
157 FX_BOOL CPDF_MeshStream::Load(CPDF_Stream* pShadingStream, CPDF_Function** pFuncs, int nFuncs, CPDF_ColorSpace* pCS)
159 m_Stream.LoadAllData(pShadingStream);
160 m_BitStream.Init(m_Stream.GetData(), m_Stream.GetSize());
164 CPDF_Dictionary* pDict = pShadingStream->GetDict();
165 m_nCoordBits = pDict->GetInteger(FX_BSTRC("BitsPerCoordinate"));
166 m_nCompBits = pDict->GetInteger(FX_BSTRC("BitsPerComponent"));
167 m_nFlagBits = pDict->GetInteger(FX_BSTRC("BitsPerFlag"));
168 if (!m_nCoordBits || !m_nCompBits) {
171 int nComps = pCS->CountComponents();
175 m_nComps = nFuncs ? 1 : nComps;
176 if (((int)m_nComps < 0) || m_nComps > 8) {
179 m_CoordMax = m_nCoordBits == 32 ? -1 : (1 << m_nCoordBits) - 1;
180 m_CompMax = (1 << m_nCompBits) - 1;
181 CPDF_Array* pDecode = pDict->GetArray(FX_BSTRC("Decode"));
182 if (pDecode == NULL || pDecode->GetCount() != 4 + m_nComps * 2) {
185 m_xmin = pDecode->GetNumber(0);
186 m_xmax = pDecode->GetNumber(1);
187 m_ymin = pDecode->GetNumber(2);
188 m_ymax = pDecode->GetNumber(3);
189 for (FX_DWORD i = 0; i < m_nComps; i ++) {
190 m_ColorMin[i] = pDecode->GetNumber(i * 2 + 4);
191 m_ColorMax[i] = pDecode->GetNumber(i * 2 + 5);
195 FX_DWORD CPDF_MeshStream::GetFlag()
197 return m_BitStream.GetBits(m_nFlagBits) & 0x03;
199 void CPDF_MeshStream::GetCoords(FX_FLOAT& x, FX_FLOAT& y)
201 if (m_nCoordBits == 32) {
202 x = m_xmin + (FX_FLOAT)(m_BitStream.GetBits(m_nCoordBits) * (m_xmax - m_xmin) / (double)m_CoordMax);
203 y = m_ymin + (FX_FLOAT)(m_BitStream.GetBits(m_nCoordBits) * (m_ymax - m_ymin) / (double)m_CoordMax);
205 x = m_xmin + m_BitStream.GetBits(m_nCoordBits) * (m_xmax - m_xmin) / m_CoordMax;
206 y = m_ymin + m_BitStream.GetBits(m_nCoordBits) * (m_ymax - m_ymin) / m_CoordMax;
209 void CPDF_MeshStream::GetColor(FX_FLOAT& r, FX_FLOAT& g, FX_FLOAT& b)
212 FX_FLOAT color_value[8];
213 for (i = 0; i < m_nComps; i ++) {
214 color_value[i] = m_ColorMin[i] + m_BitStream.GetBits(m_nCompBits) * (m_ColorMax[i] - m_ColorMin[i]) / m_CompMax;
217 static const int kMaxResults = 8;
218 FX_FLOAT result[kMaxResults];
220 FXSYS_memset32(result, 0, sizeof(result));
221 for (FX_DWORD i = 0; i < m_nFuncs; i ++) {
222 if (m_pFuncs[i] && m_pFuncs[i]->CountOutputs() <= kMaxResults) {
223 m_pFuncs[i]->Call(color_value, 1, result, nResults);
226 m_pCS->GetRGB(result, r, g, b);
228 m_pCS->GetRGB(color_value, r, g, b);
231 FX_DWORD CPDF_MeshStream::GetVertex(CPDF_MeshVertex& vertex, CFX_AffineMatrix* pObject2Bitmap)
233 FX_DWORD flag = GetFlag();
234 GetCoords(vertex.x, vertex.y);
235 pObject2Bitmap->Transform(vertex.x, vertex.y);
236 GetColor(vertex.r, vertex.g, vertex.b);
237 m_BitStream.ByteAlign();
240 FX_BOOL CPDF_MeshStream::GetVertexRow(CPDF_MeshVertex* vertex, int count, CFX_AffineMatrix* pObject2Bitmap)
242 for (int i = 0; i < count; i ++) {
243 if (m_BitStream.IsEOF()) {
246 GetCoords(vertex[i].x, vertex[i].y);
247 pObject2Bitmap->Transform(vertex[i].x, vertex[i].y);
248 GetColor(vertex[i].r, vertex[i].g, vertex[i].b);
249 m_BitStream.ByteAlign();
253 CFX_FloatRect _GetShadingBBox(CPDF_Stream* pStream, int type, const CFX_AffineMatrix* pMatrix,
254 CPDF_Function** pFuncs, int nFuncs, CPDF_ColorSpace* pCS)
256 if (pStream == NULL || pStream->GetType() != PDFOBJ_STREAM || pFuncs == NULL || pCS == NULL) {
257 return CFX_FloatRect(0, 0, 0, 0);
259 CPDF_MeshStream stream;
260 if (!stream.Load(pStream, pFuncs, nFuncs, pCS)) {
261 return CFX_FloatRect(0, 0, 0, 0);
264 FX_BOOL bStarted = FALSE;
265 FX_BOOL bGouraud = type == 4 || type == 5;
266 int full_point_count = type == 7 ? 16 : (type == 6 ? 12 : 1);
267 int full_color_count = (type == 6 || type == 7) ? 4 : 1;
268 while (!stream.m_BitStream.IsEOF()) {
271 flag = stream.GetFlag();
273 int point_count = full_point_count, color_count = full_color_count;
274 if (!bGouraud && flag) {
278 for (int i = 0; i < point_count; i ++) {
280 stream.GetCoords(x, y);
282 rect.UpdateRect(x, y);
288 stream.m_BitStream.SkipBits(stream.m_nComps * stream.m_nCompBits * color_count);
290 stream.m_BitStream.ByteAlign();
293 rect.Transform(pMatrix);