Merge XFA to PDFium master at 4dc95e7 on 10/28/2014
[pdfium.git] / fpdfsdk / src / fpdf_transformpage.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/fsdk_define.h"
8 #include "../include/fpdf_transformpage.h"
9 #include "../include/fpdfxfa/fpdfxfa_doc.h"
10 #include "../include/fpdfxfa/fpdfxfa_page.h"
11
12 DLLEXPORT void STDCALL FPDFPage_SetMediaBox(FPDF_PAGE page, float left, float bottom, float right, float top)
13 {
14         if(!page)
15                 return;
16         CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
17         if (!pPage) return;
18         CPDF_Dictionary* pPageDict = pPage->m_pFormDict;
19         CPDF_Array* pMediaBoxArray = FX_NEW CPDF_Array;
20         pMediaBoxArray->Add(FX_NEW CPDF_Number(left));
21         pMediaBoxArray->Add(FX_NEW CPDF_Number(bottom));
22         pMediaBoxArray->Add(FX_NEW CPDF_Number(FX_FLOAT(right)));
23         pMediaBoxArray->Add(FX_NEW CPDF_Number(FX_FLOAT(top)));
24         
25         pPageDict->SetAt("MediaBox", pMediaBoxArray);
26 }
27
28
29 DLLEXPORT void STDCALL FPDFPage_SetCropBox(FPDF_PAGE page, float left, float bottom, float right, float top)
30 {
31         if(!page)
32                 return;
33         CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
34         if (!pPage) return;
35         CPDF_Dictionary* pPageDict = pPage->m_pFormDict;
36         CPDF_Array* pCropBoxArray = FX_NEW CPDF_Array;
37         pCropBoxArray->Add(FX_NEW CPDF_Number(left));
38         pCropBoxArray->Add(FX_NEW CPDF_Number(bottom));
39         pCropBoxArray->Add(FX_NEW CPDF_Number(FX_FLOAT(right)));
40         pCropBoxArray->Add(FX_NEW CPDF_Number(FX_FLOAT(top)));
41         
42         
43         pPageDict->SetAt("CropBox", pCropBoxArray);
44 }
45
46
47 DLLEXPORT FX_BOOL STDCALL FPDFPage_GetMediaBox(FPDF_PAGE page, float* left, float* bottom, float* right, float* top)
48 {
49         if(!page)
50                 return FALSE;
51         CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
52         if (!pPage) return FALSE;
53         CPDF_Dictionary* pPageDict = pPage->m_pFormDict;
54         CPDF_Array* pArray = pPageDict->GetArray("MediaBox");
55         if(pArray)
56         {
57                 *left = pArray->GetFloat(0);
58                 *bottom = pArray->GetFloat(1);
59                 *right = pArray->GetFloat(2);
60                 *top = pArray->GetFloat(3);
61                 return TRUE;
62         }
63         return FALSE;
64 }
65
66 DLLEXPORT FPDF_BOOL STDCALL FPDFPage_GetCropBox(FPDF_PAGE page, float* left, float* bottom, float* right, float* top)
67 {
68         if(!page)
69                 return FALSE;
70         CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
71         if (!pPage) return FALSE;
72         CPDF_Dictionary* pPageDict = pPage->m_pFormDict;
73         CPDF_Array* pArray = pPageDict->GetArray("CropBox");
74         if(pArray)
75         {
76                 *left = pArray->GetFloat(0);
77                 *bottom = pArray->GetFloat(1);
78                 *right = pArray->GetFloat(2);
79                 *top = pArray->GetFloat(3);
80                 return TRUE;
81         }
82         return FALSE;
83 }
84
85 DLLEXPORT FPDF_BOOL STDCALL FPDFPage_TransFormWithClip(FPDF_PAGE page, FS_MATRIX* matrix, FS_RECTF* clipRect)
86 {
87         if(!page)
88                 return FALSE;
89
90         CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
91         if (!pPage)
92                 return FALSE;
93
94         CFX_ByteTextBuf textBuf;
95         textBuf<<"q ";
96         CFX_FloatRect rect(clipRect->left, clipRect->bottom, clipRect->right, clipRect->top);
97         rect.Normalize();
98         CFX_ByteString bsClipping;
99         bsClipping.Format("%f %f %f %f re W* n ", rect.left, rect.bottom, rect.Width(), rect.Height());
100         textBuf<<bsClipping;
101
102         CFX_ByteString bsMatix;
103         bsMatix.Format("%f %f %f %f %f %f cm ", matrix->a, matrix->b,matrix->c,matrix->d,matrix->e,matrix->f);
104         textBuf<<bsMatix;
105         
106
107         CPDF_Dictionary* pPageDic = pPage->m_pFormDict;
108         CPDF_Object* pContentObj = pPageDic ? pPageDic->GetElement("Contents") : NULL;
109         if(!pContentObj)
110                 pContentObj = pPageDic ? pPageDic->GetArray("Contents") : NULL;
111         if(!pContentObj)
112                 return FALSE;
113         
114         CPDF_Dictionary* pDic = FX_NEW CPDF_Dictionary;
115         CPDF_Stream* pStream = FX_NEW CPDF_Stream(NULL,0, pDic);
116         pStream->SetData(textBuf.GetBuffer(), textBuf.GetSize(), FALSE, FALSE);
117         CPDF_Document* pDoc = pPage->m_pDocument;
118         if(!pDoc)
119                 return FALSE;
120         pDoc->AddIndirectObject(pStream);
121
122         pDic = FX_NEW CPDF_Dictionary;
123         CPDF_Stream* pEndStream = FX_NEW CPDF_Stream(NULL,0, pDic);
124         pEndStream->SetData((FX_LPCBYTE)" Q", 2, FALSE, FALSE);
125         pDoc->AddIndirectObject(pEndStream);
126         
127         CPDF_Array* pContentArray = NULL;
128         if (pContentObj && pContentObj->GetType() == PDFOBJ_ARRAY)
129         {
130                 pContentArray = (CPDF_Array*)pContentObj;
131                 CPDF_Reference* pRef = FX_NEW CPDF_Reference(pDoc, pStream->GetObjNum());
132                 pContentArray->InsertAt(0, pRef);
133                 pContentArray->AddReference(pDoc,pEndStream);
134                 
135         }
136         else if(pContentObj && pContentObj->GetType() == PDFOBJ_REFERENCE)
137         {
138                 CPDF_Reference* pReference = (CPDF_Reference*)pContentObj;
139                 CPDF_Object* pDirectObj = pReference->GetDirect();
140                 if(pDirectObj != NULL)
141                 {
142                         if(pDirectObj->GetType() == PDFOBJ_ARRAY)
143                         {
144                                 pContentArray = (CPDF_Array*)pDirectObj;
145                                 CPDF_Reference* pRef = FX_NEW CPDF_Reference(pDoc, pStream->GetObjNum());
146                                 pContentArray->InsertAt(0, pRef);
147                                 pContentArray->AddReference(pDoc,pEndStream);
148                                 
149                         }
150                         else if(pDirectObj->GetType() == PDFOBJ_STREAM)
151                         {
152                                 pContentArray = FX_NEW CPDF_Array();
153                                 pContentArray->AddReference(pDoc,pStream->GetObjNum());
154                                 pContentArray->AddReference(pDoc,pDirectObj->GetObjNum());
155                                 pContentArray->AddReference(pDoc, pEndStream);
156                                 pPageDic->SetAtReference("Contents", pDoc, pDoc->AddIndirectObject(pContentArray));
157                         }
158                 }
159         }       
160
161         //Need to transform the patterns as well.
162         CPDF_Dictionary* pRes = pPageDic->GetDict(FX_BSTRC("Resources"));
163         if(pRes)
164         {
165                 CPDF_Dictionary* pPattenDict = pRes->GetDict(FX_BSTRC("Pattern"));
166                 if(pPattenDict)
167                 {
168                         FX_POSITION pos = pPattenDict->GetStartPos();
169                         while(pos)
170                         {
171                                 CPDF_Dictionary* pDict = NULL;
172                                 CFX_ByteString key;
173                                 CPDF_Object* pObj = pPattenDict->GetNextElement(pos, key);
174                                 if(pObj->GetType() == PDFOBJ_REFERENCE)
175                                         pObj = pObj->GetDirect();
176                                 if(pObj->GetType() == PDFOBJ_DICTIONARY)
177                                 {
178                                         pDict = (CPDF_Dictionary*)pObj;
179                                 }
180                                 else if(pObj->GetType() == PDFOBJ_STREAM)
181                                 {
182                                         pDict = ((CPDF_Stream*)pObj)->GetDict();
183                                 }
184                                 else
185                                         continue;
186                                 
187                                 CFX_AffineMatrix m = pDict->GetMatrix(FX_BSTRC("Matrix"));
188                                 CFX_AffineMatrix t = *(CFX_AffineMatrix*)matrix;
189                                 m.Concat(t);
190                                 pDict->SetAtMatrix(FX_BSTRC("Matrix"), m);
191                         }
192                 }
193         }
194
195         return TRUE;
196 }
197
198 DLLEXPORT void STDCALL FPDFPageObj_TransformClipPath(FPDF_PAGEOBJECT page_object,double a, double b, double c, double d, double e, double f)
199 {
200         CPDF_PageObject* pPageObj = (CPDF_PageObject*)page_object;
201         if(pPageObj == NULL)
202                 return;
203         CFX_AffineMatrix matrix((FX_FLOAT)a,(FX_FLOAT)b,(FX_FLOAT)c,(FX_FLOAT)d,(FX_FLOAT)e,(FX_FLOAT)f);
204         
205         //Special treatment to shading object, because the ClipPath for shading object is already transformed.
206         if(pPageObj->m_Type != PDFPAGE_SHADING)
207                 pPageObj->TransformClipPath(matrix);
208         pPageObj->TransformGeneralState(matrix);
209 }
210
211
212 DLLEXPORT FPDF_CLIPPATH STDCALL FPDF_CreateClipPath(float left, float bottom, float right, float top)
213 {
214         CPDF_ClipPath* pNewClipPath =  FX_NEW CPDF_ClipPath();
215         pNewClipPath->GetModify();
216         CPDF_Path Path;
217         Path.GetModify();
218         Path.AppendRect(left, bottom, right, top);
219         pNewClipPath->AppendPath(Path, FXFILL_ALTERNATE, FALSE);
220         return pNewClipPath;
221 }
222
223 DLLEXPORT void STDCALL FPDF_DestroyClipPath(FPDF_CLIPPATH clipPath)
224 {
225         if(clipPath)
226                 delete (CPDF_ClipPath*)clipPath;
227 }
228
229 void OutputPath(CFX_ByteTextBuf& buf, CPDF_Path path)
230 {
231         const CFX_PathData* pPathData = path;
232         if (pPathData == NULL) return;
233         
234         FX_PATHPOINT* pPoints = pPathData->GetPoints();
235         
236         if (path.IsRect()) {
237                 buf << (pPoints[0].m_PointX) << " " << (pPoints[0].m_PointY) << " " 
238                         << (pPoints[2].m_PointX - pPoints[0].m_PointX) << " " 
239                         << (pPoints[2].m_PointY - pPoints[0].m_PointY) << " re\n";
240                 return;
241         }
242         
243         CFX_ByteString temp;
244         for (int i = 0; i < pPathData->GetPointCount(); i ++) {
245                 buf << (pPoints[i].m_PointX) << " " << (pPoints[i].m_PointY);
246                 int point_type = pPoints[i].m_Flag & FXPT_TYPE;
247                 if (point_type == FXPT_MOVETO)
248                         buf << " m\n";
249                 else if (point_type == FXPT_BEZIERTO) {
250                         buf << " " << (pPoints[i+1].m_PointX) << " " << (pPoints[i+1].m_PointY) << " " << 
251                                 (pPoints[i+2].m_PointX) << " " << (pPoints[i+2].m_PointY);
252                         if (pPoints[i+2].m_Flag & FXPT_CLOSEFIGURE)
253                                 buf << " c h\n";
254                         else
255                                 buf << " c\n";
256                         i += 2;
257                 } else if (point_type == FXPT_LINETO) {
258                         if (pPoints[i].m_Flag & FXPT_CLOSEFIGURE)
259                                 buf << " l h\n";
260                         else
261                                 buf << " l\n";
262                 }
263         }
264 }
265
266 DLLEXPORT void STDCALL FPDFPage_InsertClipPath(FPDF_PAGE page,FPDF_CLIPPATH clipPath)
267 {
268         if(!page)
269                 return;
270         CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
271         if (!pPage) return;
272         CPDF_Dictionary* pPageDic = pPage->m_pFormDict;
273         CPDF_Object* pContentObj = pPageDic ? pPageDic->GetElement("Contents") : NULL;
274         if(!pContentObj)
275                 pContentObj = pPageDic ? pPageDic->GetArray("Contents") : NULL;
276         if(!pContentObj)
277                 return;
278
279         CFX_ByteTextBuf strClip;
280         CPDF_ClipPath* pClipPath = (CPDF_ClipPath*)clipPath;
281         FX_DWORD i;
282         for (i = 0; i < pClipPath->GetPathCount(); i ++) {
283                 CPDF_Path path = pClipPath->GetPath(i);
284                 int iClipType = pClipPath->GetClipType(i);
285                 if (path.GetPointCount() == 0) {
286                         // Empty clipping (totally clipped out)
287                         strClip << "0 0 m W n ";
288                 } else {
289                         OutputPath(strClip, path);
290                         if (iClipType == FXFILL_WINDING)
291                                 strClip << "W n\n";
292                         else
293                                 strClip << "W* n\n";
294                 }
295         }
296         CPDF_Dictionary* pDic = FX_NEW CPDF_Dictionary;
297         CPDF_Stream* pStream = FX_NEW CPDF_Stream(NULL,0, pDic);
298         pStream->SetData(strClip.GetBuffer(), strClip.GetSize(), FALSE, FALSE);
299         CPDF_Document* pDoc = pPage->m_pDocument;
300         if(!pDoc)
301                 return;
302         pDoc->AddIndirectObject(pStream);
303         
304         CPDF_Array* pContentArray = NULL;
305         if (pContentObj && pContentObj->GetType() == PDFOBJ_ARRAY)
306         {
307                 pContentArray = (CPDF_Array*)pContentObj;
308                 CPDF_Reference* pRef = FX_NEW CPDF_Reference(pDoc, pStream->GetObjNum());
309                 pContentArray->InsertAt(0, pRef);
310                 
311         }
312         else if(pContentObj && pContentObj->GetType() == PDFOBJ_REFERENCE)
313         {
314                 CPDF_Reference* pReference = (CPDF_Reference*)pContentObj;
315                 CPDF_Object* pDirectObj = pReference->GetDirect();
316                 if(pDirectObj != NULL)
317                 {
318                         if(pDirectObj->GetType() == PDFOBJ_ARRAY)
319                         {
320                                 pContentArray = (CPDF_Array*)pDirectObj;
321                                 CPDF_Reference* pRef = FX_NEW CPDF_Reference(pDoc, pStream->GetObjNum());
322                                 pContentArray->InsertAt(0, pRef);
323                                 
324                         }
325                         else if(pDirectObj->GetType() == PDFOBJ_STREAM)
326                         {
327                                 pContentArray = FX_NEW CPDF_Array();
328                                 pContentArray->AddReference(pDoc,pStream->GetObjNum());
329                                 pContentArray->AddReference(pDoc,pDirectObj->GetObjNum());
330                                 pPageDic->SetAtReference("Contents", pDoc, pDoc->AddIndirectObject(pContentArray));
331                         }
332                 }
333         }       
334 }
335