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_bForceClear(FALSE)
14 m_ParentMatrix = *pParentMatrix;
17 CPDF_Pattern::~CPDF_Pattern()
20 CPDF_TilingPattern::CPDF_TilingPattern(CPDF_Document* pDoc, CPDF_Object* pPatternObj, const CFX_AffineMatrix* parentMatrix) :
21 CPDF_Pattern(parentMatrix)
23 m_PatternType = PATTERN_TILING;
24 m_pPatternObj = pPatternObj;
26 CPDF_Dictionary* pDict = m_pPatternObj->GetDict();
27 ASSERT(pDict != NULL);
28 m_Pattern2Form = pDict->GetMatrix(FX_BSTRC("Matrix"));
29 m_bColored = pDict->GetInteger(FX_BSTRC("PaintType")) == 1;
31 m_Pattern2Form.Concat(*parentMatrix);
35 CPDF_TilingPattern::~CPDF_TilingPattern()
42 FX_BOOL CPDF_TilingPattern::Load()
44 if (m_pForm != NULL) {
47 CPDF_Dictionary* pDict = m_pPatternObj->GetDict();
51 m_bColored = pDict->GetInteger(FX_BSTRC("PaintType")) == 1;
52 m_XStep = (FX_FLOAT)FXSYS_fabs(pDict->GetNumber(FX_BSTRC("XStep")));
53 m_YStep = (FX_FLOAT)FXSYS_fabs(pDict->GetNumber(FX_BSTRC("YStep")));
54 if (m_pPatternObj->GetType() != PDFOBJ_STREAM) {
57 CPDF_Stream* pStream = (CPDF_Stream*)m_pPatternObj;
58 m_pForm = FX_NEW CPDF_Form(m_pDocument, NULL, pStream);
59 m_pForm->ParseContent(NULL, &m_ParentMatrix, NULL, NULL);
60 m_BBox = pDict->GetRect(FX_BSTRC("BBox"));
63 CPDF_ShadingPattern::CPDF_ShadingPattern(CPDF_Document* pDoc, CPDF_Object* pPatternObj, FX_BOOL bShading, const CFX_AffineMatrix* parentMatrix) : CPDF_Pattern(parentMatrix)
65 m_PatternType = PATTERN_SHADING;
66 m_pPatternObj = bShading ? NULL : pPatternObj;
68 m_bShadingObj = bShading;
70 CPDF_Dictionary* pDict = m_pPatternObj->GetDict();
71 ASSERT(pDict != NULL);
72 m_Pattern2Form = pDict->GetMatrix(FX_BSTRC("Matrix"));
73 m_pShadingObj = pDict->GetElementValue(FX_BSTRC("Shading"));
75 m_Pattern2Form.Concat(*parentMatrix);
78 m_pShadingObj = pPatternObj;
83 for (int i = 0; i < 4; i ++) {
84 m_pFunctions[i] = NULL;
88 CPDF_ShadingPattern::~CPDF_ShadingPattern()
92 void CPDF_ShadingPattern::Clear()
94 for (int i = 0; i < m_nFuncs; i ++) {
95 if (m_pFunctions[i]) {
96 delete m_pFunctions[i];
98 m_pFunctions[i] = NULL;
100 CPDF_ColorSpace* pCS = m_pCountedCS ? m_pCountedCS->m_Obj : NULL;
101 if (pCS && m_pDocument) {
102 m_pDocument->GetPageData()->ReleaseColorSpace(pCS->GetArray());
109 FX_BOOL CPDF_ShadingPattern::Load()
111 if (m_ShadingType != 0) {
114 CPDF_Dictionary* pShadingDict = m_pShadingObj ? m_pShadingObj->GetDict() : NULL;
115 if (pShadingDict == NULL) {
119 for (int i = 0; i < m_nFuncs; i ++)
120 if (m_pFunctions[i]) {
121 delete m_pFunctions[i];
125 CPDF_Object* pFunc = pShadingDict->GetElementValue(FX_BSTRC("Function"));
127 if (pFunc->GetType() == PDFOBJ_ARRAY) {
128 m_nFuncs = ((CPDF_Array*)pFunc)->GetCount();
132 for (int i = 0; i < m_nFuncs; i ++) {
133 m_pFunctions[i] = CPDF_Function::Load(((CPDF_Array*)pFunc)->GetElementValue(i));
136 m_pFunctions[0] = CPDF_Function::Load(pFunc);
140 CPDF_Object* pCSObj = pShadingDict->GetElementValue(FX_BSTRC("ColorSpace"));
141 if (pCSObj == NULL) {
144 CPDF_DocPageData* pDocPageData = m_pDocument->GetPageData();
145 m_pCS = pDocPageData->GetColorSpace(pCSObj, NULL);
147 m_pCountedCS = pDocPageData->FindColorSpacePtr(m_pCS->GetArray());
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);