Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / pdfium / core / src / fpdfapi / fpdf_page / fpdf_page_pattern.cpp
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.
4  
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
7 #include "../../../include/fpdfapi/fpdf_page.h"
8 #include "pageint.h"
9
10 CPDF_Pattern::CPDF_Pattern(const CFX_AffineMatrix* pParentMatrix) :
11     m_pPatternObj(NULL), m_PatternType(PATTERN_TILING), m_pDocument(NULL), m_pColor(NULL)
12 {
13     if (pParentMatrix) {
14         m_ParentMatrix = *pParentMatrix;
15     }
16 }
17
18 CPDF_Pattern::~CPDF_Pattern()
19 {
20     if (m_pColor) {
21         m_pColor->SetValue(NULL, NULL, 0);
22         m_pColor = NULL;
23     }
24 }
25 CPDF_TilingPattern::CPDF_TilingPattern(CPDF_Document* pDoc, CPDF_Object* pPatternObj, const CFX_AffineMatrix* parentMatrix) :
26     CPDF_Pattern(parentMatrix)
27 {
28     m_PatternType = PATTERN_TILING;
29     m_pPatternObj = pPatternObj;
30     m_pDocument = pDoc;
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;
35     if (parentMatrix) {
36         m_Pattern2Form.Concat(*parentMatrix);
37     }
38     m_pForm = NULL;
39 }
40 CPDF_TilingPattern::~CPDF_TilingPattern()
41 {
42     if (m_pForm) {
43         delete m_pForm;
44         m_pForm = NULL;
45     }
46 }
47 FX_BOOL CPDF_TilingPattern::Load()
48 {
49     if (m_pForm != NULL) {
50         return TRUE;
51     }
52     CPDF_Dictionary* pDict = m_pPatternObj->GetDict();
53     if (pDict == NULL) {
54         return FALSE;
55     }
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) {
60         return FALSE;
61     }
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"));
66     return TRUE;
67 }
68 CPDF_ShadingPattern::CPDF_ShadingPattern(CPDF_Document* pDoc, CPDF_Object* pPatternObj, FX_BOOL bShading, const CFX_AffineMatrix* parentMatrix) : CPDF_Pattern(parentMatrix)
69 {
70     m_PatternType = PATTERN_SHADING;
71     m_pPatternObj = bShading ? NULL : pPatternObj;
72     m_pDocument = pDoc;
73     m_bShadingObj = bShading;
74     if (!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"));
79         if (parentMatrix) {
80             m_Pattern2Form.Concat(*parentMatrix);
81         }
82     } else {
83         m_pShadingObj = pPatternObj;
84     }
85     m_ShadingType = 0;
86     m_pCS = NULL;
87     m_nFuncs = 0;
88     for (int i = 0; i < 4; i ++) {
89         m_pFunctions[i] = NULL;
90     }
91 }
92 CPDF_ShadingPattern::~CPDF_ShadingPattern()
93 {
94     Clear();
95 }
96 void CPDF_ShadingPattern::Clear()
97 {
98     for (int i = 0; i < m_nFuncs; i ++) {
99         if (m_pFunctions[i]) {
100             delete m_pFunctions[i];
101         }
102         m_pFunctions[i] = NULL;
103     }
104     CPDF_ColorSpace* pCS = m_pCS;
105     if (pCS && m_pDocument) {
106         m_pDocument->GetPageData()->ReleaseColorSpace(pCS->GetArray());
107     }
108     m_ShadingType = 0;
109     m_pCS = NULL;
110     m_nFuncs = 0;
111 }
112 FX_BOOL CPDF_ShadingPattern::Load()
113 {
114     if (m_ShadingType != 0) {
115         return TRUE;
116     }
117     CPDF_Dictionary* pShadingDict = m_pShadingObj ? m_pShadingObj->GetDict() : NULL;
118     if (pShadingDict == NULL) {
119         return FALSE;
120     }
121     if (m_nFuncs) {
122         for (int i = 0; i < m_nFuncs; i ++)
123             if (m_pFunctions[i]) {
124                 delete m_pFunctions[i];
125             }
126         m_nFuncs = 0;
127     }
128     CPDF_Object* pFunc = pShadingDict->GetElementValue(FX_BSTRC("Function"));
129     if (pFunc) {
130         if (pFunc->GetType() == PDFOBJ_ARRAY) {
131             m_nFuncs = ((CPDF_Array*)pFunc)->GetCount();
132             if (m_nFuncs > 4) {
133                 m_nFuncs = 4;
134             }
135             for (int i = 0; i < m_nFuncs; i ++) {
136                 m_pFunctions[i] = CPDF_Function::Load(((CPDF_Array*)pFunc)->GetElementValue(i));
137             }
138         } else {
139             m_pFunctions[0] = CPDF_Function::Load(pFunc);
140             m_nFuncs = 1;
141         }
142     }
143     CPDF_Object* pCSObj = pShadingDict->GetElementValue(FX_BSTRC("ColorSpace"));
144     if (pCSObj == NULL) {
145         return FALSE;
146     }
147     CPDF_DocPageData* pDocPageData = m_pDocument->GetPageData();
148     m_pCS = pDocPageData->GetColorSpace(pCSObj, NULL);
149     m_ShadingType = pShadingDict->GetInteger(FX_BSTRC("ShadingType"));
150     return TRUE;
151 }
152 FX_BOOL CPDF_ShadingPattern::Reload()
153 {
154     Clear();
155     return Load();
156 }
157 FX_BOOL CPDF_MeshStream::Load(CPDF_Stream* pShadingStream, CPDF_Function** pFuncs, int nFuncs, CPDF_ColorSpace* pCS)
158 {
159     m_Stream.LoadAllData(pShadingStream);
160     m_BitStream.Init(m_Stream.GetData(), m_Stream.GetSize());
161     m_pFuncs = pFuncs;
162     m_nFuncs = nFuncs;
163     m_pCS = pCS;
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) {
169         return FALSE;
170     }
171     int nComps = pCS->CountComponents();
172     if (nComps > 8) {
173         return FALSE;
174     }
175     m_nComps = nFuncs ? 1 : nComps;
176     if (((int)m_nComps < 0) || m_nComps > 8) {
177         return FALSE;
178     }
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) {
183         return FALSE;
184     }
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);
192     }
193     return TRUE;
194 }
195 FX_DWORD CPDF_MeshStream::GetFlag()
196 {
197     return m_BitStream.GetBits(m_nFlagBits) & 0x03;
198 }
199 void CPDF_MeshStream::GetCoords(FX_FLOAT& x, FX_FLOAT& y)
200 {
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);
204     } else {
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;
207     }
208 }
209 void CPDF_MeshStream::GetColor(FX_FLOAT& r, FX_FLOAT& g, FX_FLOAT& b)
210 {
211     FX_DWORD i;
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;
215     }
216     if (m_nFuncs) {
217         static const int kMaxResults = 8;
218         FX_FLOAT result[kMaxResults];
219         int nResults;
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);
224             }
225         }
226         m_pCS->GetRGB(result, r, g, b);
227     } else {
228         m_pCS->GetRGB(color_value, r, g, b);
229     }
230 }
231 FX_DWORD CPDF_MeshStream::GetVertex(CPDF_MeshVertex& vertex, CFX_AffineMatrix* pObject2Bitmap)
232 {
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();
238     return flag;
239 }
240 FX_BOOL CPDF_MeshStream::GetVertexRow(CPDF_MeshVertex* vertex, int count, CFX_AffineMatrix* pObject2Bitmap)
241 {
242     for (int i = 0; i < count; i ++) {
243         if (m_BitStream.IsEOF()) {
244             return FALSE;
245         }
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();
250     }
251     return TRUE;
252 }
253 CFX_FloatRect _GetShadingBBox(CPDF_Stream* pStream, int type, const CFX_AffineMatrix* pMatrix,
254                               CPDF_Function** pFuncs, int nFuncs, CPDF_ColorSpace* pCS)
255 {
256     if (pStream == NULL || pStream->GetType() != PDFOBJ_STREAM || pFuncs == NULL || pCS == NULL) {
257         return CFX_FloatRect(0, 0, 0, 0);
258     }
259     CPDF_MeshStream stream;
260     if (!stream.Load(pStream, pFuncs, nFuncs, pCS)) {
261         return CFX_FloatRect(0, 0, 0, 0);
262     }
263     CFX_FloatRect rect;
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()) {
269         FX_DWORD flag;
270         if (type != 5) {
271             flag = stream.GetFlag();
272         }
273         int point_count = full_point_count, color_count = full_color_count;
274         if (!bGouraud && flag) {
275             point_count -= 4;
276             color_count -= 2;
277         }
278         for (int i = 0; i < point_count; i ++) {
279             FX_FLOAT x, y;
280             stream.GetCoords(x, y);
281             if (bStarted) {
282                 rect.UpdateRect(x, y);
283             } else {
284                 rect.InitRect(x, y);
285                 bStarted = TRUE;
286             }
287         }
288         stream.m_BitStream.SkipBits(stream.m_nComps * stream.m_nCompBits * color_count);
289         if (bGouraud) {
290             stream.m_BitStream.ByteAlign();
291         }
292     }
293     rect.Transform(pMatrix);
294     return rect;
295 }