8a835fb220e238500209703cbd4f82497ab207e8
[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 (CPDF_Reference* pReference = ToReference(pContentObj)) {
139     CPDF_Object* pDirectObj = pReference->GetDirect();
140     if (pDirectObj) {
141       if (CPDF_Array* pArray = pDirectObj->AsArray()) {
142         pContentArray = pArray;
143         CPDF_Reference* pRef = new CPDF_Reference(pDoc, pStream->GetObjNum());
144         pContentArray->InsertAt(0, pRef);
145         pContentArray->AddReference(pDoc, pEndStream);
146       } else if (pDirectObj->IsStream()) {
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 = nullptr;
165         CFX_ByteString key;
166         CPDF_Object* pObj = pPattenDict->GetNextElement(pos, key);
167         if (pObj->IsReference())
168           pObj = pObj->GetDirect();
169
170         if (pObj->IsDictionary())
171           pDict = pObj->AsDictionary();
172         else if (CPDF_Stream* pStream = pObj->AsStream())
173           pDict = pStream->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   CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
267   if (!pPage)
268     return;
269
270   CPDF_Dictionary* pPageDic = pPage->m_pFormDict;
271   CPDF_Object* pContentObj =
272       pPageDic ? pPageDic->GetElement("Contents") : nullptr;
273   if (!pContentObj)
274     pContentObj = pPageDic ? pPageDic->GetArray("Contents") : nullptr;
275   if (!pContentObj)
276     return;
277
278   CFX_ByteTextBuf strClip;
279   CPDF_ClipPath* pClipPath = (CPDF_ClipPath*)clipPath;
280   FX_DWORD i;
281   for (i = 0; i < pClipPath->GetPathCount(); i++) {
282     CPDF_Path path = pClipPath->GetPath(i);
283     int iClipType = pClipPath->GetClipType(i);
284     if (path.GetPointCount() == 0) {
285       // Empty clipping (totally clipped out)
286       strClip << "0 0 m W n ";
287     } else {
288       OutputPath(strClip, path);
289       if (iClipType == FXFILL_WINDING)
290         strClip << "W n\n";
291       else
292         strClip << "W* n\n";
293     }
294   }
295   CPDF_Dictionary* pDic = new CPDF_Dictionary;
296   CPDF_Stream* pStream = new CPDF_Stream(nullptr, 0, pDic);
297   pStream->SetData(strClip.GetBuffer(), strClip.GetSize(), FALSE, FALSE);
298   CPDF_Document* pDoc = pPage->m_pDocument;
299   if (!pDoc)
300     return;
301   pDoc->AddIndirectObject(pStream);
302
303   CPDF_Array* pContentArray = nullptr;
304   if (CPDF_Array* pArray = ToArray(pContentObj)) {
305     pContentArray = pArray;
306     CPDF_Reference* pRef = new CPDF_Reference(pDoc, pStream->GetObjNum());
307     pContentArray->InsertAt(0, pRef);
308   } else if (CPDF_Reference* pReference = ToReference(pContentObj)) {
309     CPDF_Object* pDirectObj = pReference->GetDirect();
310     if (pDirectObj) {
311       if (CPDF_Array* pArray = pDirectObj->AsArray()) {
312         pContentArray = pArray;
313         CPDF_Reference* pRef = new CPDF_Reference(pDoc, pStream->GetObjNum());
314         pContentArray->InsertAt(0, pRef);
315       } else if (pDirectObj->IsStream()) {
316         pContentArray = new CPDF_Array();
317         pContentArray->AddReference(pDoc, pStream->GetObjNum());
318         pContentArray->AddReference(pDoc, pDirectObj->GetObjNum());
319         pPageDic->SetAtReference("Contents", pDoc,
320                                  pDoc->AddIndirectObject(pContentArray));
321       }
322     }
323   }
324 }