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