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