20713fe07d4b78ee7462132dced2383b2c1f8feb
[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 =
113       pPageDic ? pPageDic->GetElement("Contents") : nullptr;
114   if (!pContentObj)
115     pContentObj = pPageDic ? pPageDic->GetArray("Contents") : nullptr;
116   if (!pContentObj)
117     return FALSE;
118
119   CPDF_Dictionary* pDic = new CPDF_Dictionary;
120   CPDF_Stream* pStream = new CPDF_Stream(nullptr, 0, pDic);
121   pStream->SetData(textBuf.GetBuffer(), textBuf.GetSize(), FALSE, FALSE);
122   CPDF_Document* pDoc = pPage->m_pDocument;
123   if (!pDoc)
124     return FALSE;
125   pDoc->AddIndirectObject(pStream);
126
127   pDic = new CPDF_Dictionary;
128   CPDF_Stream* pEndStream = new CPDF_Stream(nullptr, 0, pDic);
129   pEndStream->SetData((const uint8_t*)" Q", 2, FALSE, FALSE);
130   pDoc->AddIndirectObject(pEndStream);
131
132   CPDF_Array* pContentArray = nullptr;
133   if (CPDF_Array* pArray = ToArray(pContentObj)) {
134     pContentArray = pArray;
135     CPDF_Reference* pRef = new CPDF_Reference(pDoc, pStream->GetObjNum());
136     pContentArray->InsertAt(0, pRef);
137     pContentArray->AddReference(pDoc, pEndStream);
138   } else if (pContentObj && pContentObj->GetType() == PDFOBJ_REFERENCE) {
139     CPDF_Reference* pReference = (CPDF_Reference*)pContentObj;
140     CPDF_Object* pDirectObj = pReference->GetDirect();
141     if (pDirectObj) {
142       if (CPDF_Array* pArray = pDirectObj->AsArray()) {
143         pContentArray = pArray;
144         CPDF_Reference* pRef = new CPDF_Reference(pDoc, pStream->GetObjNum());
145         pContentArray->InsertAt(0, pRef);
146         pContentArray->AddReference(pDoc, pEndStream);
147       } else if (pDirectObj->IsStream()) {
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 = nullptr;
166         CFX_ByteString key;
167         CPDF_Object* pObj = pPattenDict->GetNextElement(pos, key);
168         if (pObj->GetType() == PDFOBJ_REFERENCE)
169           pObj = pObj->GetDirect();
170
171         if (pObj->IsDictionary())
172           pDict = pObj->AsDictionary();
173         else if (CPDF_Stream* pStream = pObj->AsStream())
174           pDict = pStream->GetDict();
175         else
176           continue;
177
178         CFX_AffineMatrix m = pDict->GetMatrix(FX_BSTRC("Matrix"));
179         CFX_AffineMatrix t = *(CFX_AffineMatrix*)matrix;
180         m.Concat(t);
181         pDict->SetAtMatrix(FX_BSTRC("Matrix"), m);
182       }
183     }
184   }
185
186   return TRUE;
187 }
188
189 DLLEXPORT void STDCALL
190 FPDFPageObj_TransformClipPath(FPDF_PAGEOBJECT page_object,
191                               double a,
192                               double b,
193                               double c,
194                               double d,
195                               double e,
196                               double f) {
197   CPDF_PageObject* pPageObj = (CPDF_PageObject*)page_object;
198   if (pPageObj == NULL)
199     return;
200   CFX_AffineMatrix matrix((FX_FLOAT)a, (FX_FLOAT)b, (FX_FLOAT)c, (FX_FLOAT)d,
201                           (FX_FLOAT)e, (FX_FLOAT)f);
202
203   // Special treatment to shading object, because the ClipPath for shading
204   // object is already transformed.
205   if (pPageObj->m_Type != PDFPAGE_SHADING)
206     pPageObj->TransformClipPath(matrix);
207   pPageObj->TransformGeneralState(matrix);
208 }
209
210 DLLEXPORT FPDF_CLIPPATH STDCALL FPDF_CreateClipPath(float left,
211                                                     float bottom,
212                                                     float right,
213                                                     float top) {
214   CPDF_ClipPath* pNewClipPath = 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   delete (CPDF_ClipPath*)clipPath;
225 }
226
227 void OutputPath(CFX_ByteTextBuf& buf, CPDF_Path path) {
228   const CFX_PathData* pPathData = path;
229   if (pPathData == NULL)
230     return;
231
232   FX_PATHPOINT* pPoints = pPathData->GetPoints();
233
234   if (path.IsRect()) {
235     buf << (pPoints[0].m_PointX) << " " << (pPoints[0].m_PointY) << " "
236         << (pPoints[2].m_PointX - pPoints[0].m_PointX) << " "
237         << (pPoints[2].m_PointY - pPoints[0].m_PointY) << " re\n";
238     return;
239   }
240
241   CFX_ByteString temp;
242   for (int i = 0; i < pPathData->GetPointCount(); i++) {
243     buf << (pPoints[i].m_PointX) << " " << (pPoints[i].m_PointY);
244     int point_type = pPoints[i].m_Flag & FXPT_TYPE;
245     if (point_type == FXPT_MOVETO)
246       buf << " m\n";
247     else if (point_type == FXPT_BEZIERTO) {
248       buf << " " << (pPoints[i + 1].m_PointX) << " "
249           << (pPoints[i + 1].m_PointY) << " " << (pPoints[i + 2].m_PointX)
250           << " " << (pPoints[i + 2].m_PointY);
251       if (pPoints[i + 2].m_Flag & FXPT_CLOSEFIGURE)
252         buf << " c h\n";
253       else
254         buf << " c\n";
255       i += 2;
256     } else if (point_type == FXPT_LINETO) {
257       if (pPoints[i].m_Flag & FXPT_CLOSEFIGURE)
258         buf << " l h\n";
259       else
260         buf << " l\n";
261     }
262   }
263 }
264
265 DLLEXPORT void STDCALL FPDFPage_InsertClipPath(FPDF_PAGE page,
266                                                FPDF_CLIPPATH clipPath) {
267   CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
268   if (!pPage)
269     return;
270
271   CPDF_Dictionary* pPageDic = pPage->m_pFormDict;
272   CPDF_Object* pContentObj =
273       pPageDic ? pPageDic->GetElement("Contents") : nullptr;
274   if (!pContentObj)
275     pContentObj = pPageDic ? pPageDic->GetArray("Contents") : nullptr;
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 = new CPDF_Dictionary;
297   CPDF_Stream* pStream = new CPDF_Stream(nullptr, 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 = nullptr;
305   if (CPDF_Array* pArray = ToArray(pContentObj)) {
306     pContentArray = pArray;
307     CPDF_Reference* pRef = new CPDF_Reference(pDoc, pStream->GetObjNum());
308     pContentArray->InsertAt(0, pRef);
309   } else if (pContentObj && pContentObj->GetType() == PDFOBJ_REFERENCE) {
310     CPDF_Reference* pReference = (CPDF_Reference*)pContentObj;
311     CPDF_Object* pDirectObj = pReference->GetDirect();
312     if (pDirectObj) {
313       if (CPDF_Array* pArray = pDirectObj->AsArray()) {
314         pContentArray = pArray;
315         CPDF_Reference* pRef = new CPDF_Reference(pDoc, pStream->GetObjNum());
316         pContentArray->InsertAt(0, pRef);
317       } else if (pDirectObj->IsStream()) {
318         pContentArray = new CPDF_Array();
319         pContentArray->AddReference(pDoc, pStream->GetObjNum());
320         pContentArray->AddReference(pDoc, pDirectObj->GetObjNum());
321         pPageDic->SetAtReference("Contents", pDoc,
322                                  pDoc->AddIndirectObject(pContentArray));
323       }
324     }
325   }
326 }