Allow compiling PDFium without V8.
[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   if (!page)
53     return;
54
55   SetBoundingBox(static_cast<CPDF_Page*>(page), "MediaBox", left, bottom, right,
56                  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   if (!page)
65     return;
66
67   SetBoundingBox(static_cast<CPDF_Page*>(page), "CropBox", left, bottom, right,
68                  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   return page && GetBoundingBox(static_cast<CPDF_Page*>(page), "MediaBox", left,
77                                 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   return page && GetBoundingBox(static_cast<CPDF_Page*>(page), "CropBox", left,
86                                 bottom, right, top);
87 }
88
89 DLLEXPORT FPDF_BOOL STDCALL FPDFPage_TransFormWithClip(FPDF_PAGE page,
90                                                        FS_MATRIX* matrix,
91                                                        FS_RECTF* clipRect) {
92   if (!page)
93     return FALSE;
94
95   CFX_ByteTextBuf textBuf;
96   textBuf << "q ";
97   CFX_FloatRect rect(clipRect->left, clipRect->bottom, clipRect->right,
98                      clipRect->top);
99   rect.Normalize();
100   CFX_ByteString bsClipping;
101   bsClipping.Format("%f %f %f %f re W* n ", rect.left, rect.bottom,
102                     rect.Width(), rect.Height());
103   textBuf << bsClipping;
104
105   CFX_ByteString bsMatix;
106   bsMatix.Format("%f %f %f %f %f %f cm ", matrix->a, matrix->b, matrix->c,
107                  matrix->d, matrix->e, matrix->f);
108   textBuf << bsMatix;
109
110   CPDF_Page* pPage = (CPDF_Page*)page;
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
138   } else if (pContentObj && pContentObj->GetType() == PDFOBJ_REFERENCE) {
139     CPDF_Reference* pReference = (CPDF_Reference*)pContentObj;
140     CPDF_Object* pDirectObj = pReference->GetDirect();
141     if (pDirectObj != NULL) {
142       if (pDirectObj->GetType() == PDFOBJ_ARRAY) {
143         pContentArray = (CPDF_Array*)pDirectObj;
144         CPDF_Reference* pRef = new CPDF_Reference(pDoc, pStream->GetObjNum());
145         pContentArray->InsertAt(0, pRef);
146         pContentArray->AddReference(pDoc, pEndStream);
147       } else if (pDirectObj->GetType() == PDFOBJ_STREAM) {
148         pContentArray = new CPDF_Array();
149         pContentArray->AddReference(pDoc, pStream->GetObjNum());
150         pContentArray->AddReference(pDoc, pDirectObj->GetObjNum());
151         pContentArray->AddReference(pDoc, pEndStream);
152         pPageDic->SetAtReference("Contents", pDoc,
153                                  pDoc->AddIndirectObject(pContentArray));
154       }
155     }
156   }
157
158   // Need to transform the patterns as well.
159   CPDF_Dictionary* pRes = pPageDic->GetDict(FX_BSTRC("Resources"));
160   if (pRes) {
161     CPDF_Dictionary* pPattenDict = pRes->GetDict(FX_BSTRC("Pattern"));
162     if (pPattenDict) {
163       FX_POSITION pos = pPattenDict->GetStartPos();
164       while (pos) {
165         CPDF_Dictionary* pDict = NULL;
166         CFX_ByteString key;
167         CPDF_Object* pObj = pPattenDict->GetNextElement(pos, key);
168         if (pObj->GetType() == PDFOBJ_REFERENCE)
169           pObj = pObj->GetDirect();
170         if (pObj->GetType() == PDFOBJ_DICTIONARY) {
171           pDict = (CPDF_Dictionary*)pObj;
172         } else if (pObj->GetType() == PDFOBJ_STREAM) {
173           pDict = ((CPDF_Stream*)pObj)->GetDict();
174         } else
175           continue;
176
177         CFX_AffineMatrix m = pDict->GetMatrix(FX_BSTRC("Matrix"));
178         CFX_AffineMatrix t = *(CFX_AffineMatrix*)matrix;
179         m.Concat(t);
180         pDict->SetAtMatrix(FX_BSTRC("Matrix"), m);
181       }
182     }
183   }
184
185   return TRUE;
186 }
187
188 DLLEXPORT void STDCALL
189 FPDFPageObj_TransformClipPath(FPDF_PAGEOBJECT page_object,
190                               double a,
191                               double b,
192                               double c,
193                               double d,
194                               double e,
195                               double f) {
196   CPDF_PageObject* pPageObj = (CPDF_PageObject*)page_object;
197   if (pPageObj == NULL)
198     return;
199   CFX_AffineMatrix matrix((FX_FLOAT)a, (FX_FLOAT)b, (FX_FLOAT)c, (FX_FLOAT)d,
200                           (FX_FLOAT)e, (FX_FLOAT)f);
201
202   // Special treatment to shading object, because the ClipPath for shading
203   // object is already transformed.
204   if (pPageObj->m_Type != PDFPAGE_SHADING)
205     pPageObj->TransformClipPath(matrix);
206   pPageObj->TransformGeneralState(matrix);
207 }
208
209 DLLEXPORT FPDF_CLIPPATH STDCALL FPDF_CreateClipPath(float left,
210                                                     float bottom,
211                                                     float right,
212                                                     float top) {
213   CPDF_ClipPath* pNewClipPath = new CPDF_ClipPath();
214   pNewClipPath->GetModify();
215   CPDF_Path Path;
216   Path.GetModify();
217   Path.AppendRect(left, bottom, right, top);
218   pNewClipPath->AppendPath(Path, FXFILL_ALTERNATE, FALSE);
219   return pNewClipPath;
220 }
221
222 DLLEXPORT void STDCALL FPDF_DestroyClipPath(FPDF_CLIPPATH clipPath) {
223   delete (CPDF_ClipPath*)clipPath;
224 }
225
226 void OutputPath(CFX_ByteTextBuf& buf, CPDF_Path path) {
227   const CFX_PathData* pPathData = path;
228   if (pPathData == NULL)
229     return;
230
231   FX_PATHPOINT* pPoints = pPathData->GetPoints();
232
233   if (path.IsRect()) {
234     buf << (pPoints[0].m_PointX) << " " << (pPoints[0].m_PointY) << " "
235         << (pPoints[2].m_PointX - pPoints[0].m_PointX) << " "
236         << (pPoints[2].m_PointY - pPoints[0].m_PointY) << " re\n";
237     return;
238   }
239
240   CFX_ByteString temp;
241   for (int i = 0; i < pPathData->GetPointCount(); i++) {
242     buf << (pPoints[i].m_PointX) << " " << (pPoints[i].m_PointY);
243     int point_type = pPoints[i].m_Flag & FXPT_TYPE;
244     if (point_type == FXPT_MOVETO)
245       buf << " m\n";
246     else if (point_type == FXPT_BEZIERTO) {
247       buf << " " << (pPoints[i + 1].m_PointX) << " "
248           << (pPoints[i + 1].m_PointY) << " " << (pPoints[i + 2].m_PointX)
249           << " " << (pPoints[i + 2].m_PointY);
250       if (pPoints[i + 2].m_Flag & FXPT_CLOSEFIGURE)
251         buf << " c h\n";
252       else
253         buf << " c\n";
254       i += 2;
255     } else if (point_type == FXPT_LINETO) {
256       if (pPoints[i].m_Flag & FXPT_CLOSEFIGURE)
257         buf << " l h\n";
258       else
259         buf << " l\n";
260     }
261   }
262 }
263
264 DLLEXPORT void STDCALL FPDFPage_InsertClipPath(FPDF_PAGE page,
265                                                FPDF_CLIPPATH clipPath) {
266   if (!page)
267     return;
268   CPDF_Page* pPage = (CPDF_Page*)page;
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 }