fpdf_page_pattern.cpp revision e6986e1e8d4a57987f47c215490cb080a65ee29a
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
10CPDF_Pattern::CPDF_Pattern(const CFX_AffineMatrix* pParentMatrix) :
11    m_pPatternObj(NULL), m_PatternType(PATTERN_TILING), m_pDocument(NULL), m_bForceClear(FALSE)
12{
13    if (pParentMatrix) {
14        m_ParentMatrix = *pParentMatrix;
15    }
16}
17CPDF_Pattern::~CPDF_Pattern()
18{
19}
20CPDF_TilingPattern::CPDF_TilingPattern(CPDF_Document* pDoc, CPDF_Object* pPatternObj, const CFX_AffineMatrix* parentMatrix) :
21    CPDF_Pattern(parentMatrix)
22{
23    m_PatternType = PATTERN_TILING;
24    m_pPatternObj = pPatternObj;
25    m_pDocument = pDoc;
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;
30    if (parentMatrix) {
31        m_Pattern2Form.Concat(*parentMatrix);
32    }
33    m_pForm = NULL;
34}
35CPDF_TilingPattern::~CPDF_TilingPattern()
36{
37    if (m_pForm) {
38        delete m_pForm;
39        m_pForm = NULL;
40    }
41}
42FX_BOOL CPDF_TilingPattern::Load()
43{
44    if (m_pForm != NULL) {
45        return TRUE;
46    }
47    CPDF_Dictionary* pDict = m_pPatternObj->GetDict();
48    if (pDict == NULL) {
49        return FALSE;
50    }
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) {
55        return FALSE;
56    }
57    CPDF_Stream* pStream = (CPDF_Stream*)m_pPatternObj;
58    m_pForm = new CPDF_Form(m_pDocument, NULL, pStream);
59    m_pForm->ParseContent(NULL, &m_ParentMatrix, NULL, NULL);
60    m_BBox = pDict->GetRect(FX_BSTRC("BBox"));
61    return TRUE;
62}
63CPDF_ShadingPattern::CPDF_ShadingPattern(CPDF_Document* pDoc, CPDF_Object* pPatternObj, FX_BOOL bShading, const CFX_AffineMatrix* parentMatrix) : CPDF_Pattern(parentMatrix)
64{
65    m_PatternType = PATTERN_SHADING;
66    m_pPatternObj = bShading ? NULL : pPatternObj;
67    m_pDocument = pDoc;
68    m_bShadingObj = bShading;
69    if (!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"));
74        if (parentMatrix) {
75            m_Pattern2Form.Concat(*parentMatrix);
76        }
77    } else {
78        m_pShadingObj = pPatternObj;
79    }
80    m_ShadingType = 0;
81    m_pCS = NULL;
82    m_nFuncs = 0;
83    for (int i = 0; i < 4; i ++) {
84        m_pFunctions[i] = NULL;
85    }
86    m_pCountedCS = NULL;
87}
88CPDF_ShadingPattern::~CPDF_ShadingPattern()
89{
90    Clear();
91}
92void CPDF_ShadingPattern::Clear()
93{
94    for (int i = 0; i < m_nFuncs; i ++) {
95        if (m_pFunctions[i]) {
96            delete m_pFunctions[i];
97        }
98        m_pFunctions[i] = NULL;
99    }
100    CPDF_ColorSpace* pCS = m_pCountedCS ? m_pCountedCS->m_Obj : NULL;
101    if (pCS && m_pDocument) {
102        m_pDocument->GetPageData()->ReleaseColorSpace(pCS->GetArray());
103    }
104    m_ShadingType = 0;
105    m_pCS = NULL;
106    m_pCountedCS = NULL;
107    m_nFuncs = 0;
108}
109FX_BOOL CPDF_ShadingPattern::Load()
110{
111    if (m_ShadingType != 0) {
112        return TRUE;
113    }
114    CPDF_Dictionary* pShadingDict = m_pShadingObj ? m_pShadingObj->GetDict() : NULL;
115    if (pShadingDict == NULL) {
116        return FALSE;
117    }
118    if (m_nFuncs) {
119        for (int i = 0; i < m_nFuncs; i ++)
120            if (m_pFunctions[i]) {
121                delete m_pFunctions[i];
122            }
123        m_nFuncs = 0;
124    }
125    CPDF_Object* pFunc = pShadingDict->GetElementValue(FX_BSTRC("Function"));
126    if (pFunc) {
127        if (pFunc->GetType() == PDFOBJ_ARRAY) {
128            m_nFuncs = ((CPDF_Array*)pFunc)->GetCount();
129            if (m_nFuncs > 4) {
130                m_nFuncs = 4;
131            }
132            for (int i = 0; i < m_nFuncs; i ++) {
133                m_pFunctions[i] = CPDF_Function::Load(((CPDF_Array*)pFunc)->GetElementValue(i));
134            }
135        } else {
136            m_pFunctions[0] = CPDF_Function::Load(pFunc);
137            m_nFuncs = 1;
138        }
139    }
140    CPDF_Object* pCSObj = pShadingDict->GetElementValue(FX_BSTRC("ColorSpace"));
141    if (pCSObj == NULL) {
142        return FALSE;
143    }
144    CPDF_DocPageData* pDocPageData = m_pDocument->GetPageData();
145    m_pCS = pDocPageData->GetColorSpace(pCSObj, NULL);
146    if (m_pCS) {
147        m_pCountedCS = pDocPageData->FindColorSpacePtr(m_pCS->GetArray());
148    }
149    m_ShadingType = pShadingDict->GetInteger(FX_BSTRC("ShadingType"));
150    return TRUE;
151}
152FX_BOOL CPDF_ShadingPattern::Reload()
153{
154    Clear();
155    return Load();
156}
157FX_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}
195FX_DWORD CPDF_MeshStream::GetFlag()
196{
197    return m_BitStream.GetBits(m_nFlagBits) & 0x03;
198}
199void 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}
209void 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}
231FX_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}
240FX_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}
253CFX_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 = 0;
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}
296