Merge to XFA: Remove typdefs for pointer types in fx_system.h.
[pdfium.git] / fpdfsdk / src / fpdf_flatten.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_flatten.h"
8 #include "../include/fsdk_define.h"
9 #include "../include/fpdfxfa/fpdfxfa_doc.h"
10 #include "../include/fpdfxfa/fpdfxfa_page.h"
11
12 typedef CFX_ArrayTemplate<CPDF_Dictionary*> CPDF_ObjectArray;
13 typedef CFX_ArrayTemplate<CPDF_Rect> CPDF_RectArray;
14
15 enum FPDF_TYPE { MAX, MIN };
16 enum FPDF_VALUE { TOP, LEFT, RIGHT, BOTTOM };
17
18 FX_BOOL IsValiableRect(CPDF_Rect rect, CPDF_Rect rcPage)
19 {
20         if ( rect.left - rect.right > 0.000001f || 
21                  rect.bottom - rect.top > 0.000001f)
22                 return FALSE;
23         
24         if (rect.left == 0.0f &&
25                 rect.top == 0.0f &&
26                 rect.right == 0.0f &&
27                 rect.bottom == 0.0f)
28                 return FALSE;
29         
30         if (!rcPage.IsEmpty())
31         {
32                 if (rect.left - rcPage.left < -10.000001f ||
33                         rect.right - rcPage.right > 10.000001f ||
34                         rect.top - rcPage.top > 10.000001f ||
35                         rect.bottom - rcPage.bottom < -10.000001f)
36                         return FALSE;
37         }
38         
39         return TRUE;
40 }
41
42
43 FX_BOOL GetContentsRect( CPDF_Document * pDoc, CPDF_Dictionary* pDict, CPDF_RectArray * pRectArray )
44 {
45         CPDF_Page* pPDFPage = FX_NEW CPDF_Page;
46         pPDFPage->Load( pDoc, pDict, FALSE );
47         pPDFPage->ParseContent();
48
49         FX_POSITION pos = pPDFPage->GetFirstObjectPosition();
50         
51         while (pos)
52         {
53                 CPDF_PageObject* pPageObject = pPDFPage->GetNextObject(pos);
54                 if (!pPageObject)continue;
55                 
56                 CPDF_Rect rc;
57                 rc.left = pPageObject->m_Left;
58                 rc.right = pPageObject->m_Right;
59                 rc.bottom = pPageObject->m_Bottom;
60                 rc.top = pPageObject->m_Top;
61                 
62                 if (IsValiableRect(rc, pDict->GetRect("MediaBox")))
63                 {
64                         pRectArray->Add(rc);
65                 }
66         }
67         
68         delete pPDFPage;
69         return TRUE;
70 }
71
72
73 void ParserStream( CPDF_Dictionary * pPageDic, CPDF_Dictionary* pStream, CPDF_RectArray * pRectArray, CPDF_ObjectArray * pObjectArray )
74 {
75         if (!pStream)return;
76         CPDF_Rect rect;
77         if (pStream->KeyExist("Rect"))
78                 rect = pStream->GetRect("Rect");
79         else if (pStream->KeyExist("BBox"))
80                 rect = pStream->GetRect("BBox");
81         
82         if (IsValiableRect(rect, pPageDic->GetRect("MediaBox")))
83                 pRectArray->Add(rect);
84         
85         pObjectArray->Add(pStream);
86 }
87
88
89 int ParserAnnots( CPDF_Document* pSourceDoc, CPDF_Dictionary * pPageDic, CPDF_RectArray * pRectArray, CPDF_ObjectArray * pObjectArray, int nUsage)
90 {
91     if (!pSourceDoc || !pPageDic)
92         return FLATTEN_FAIL;
93
94     GetContentsRect( pSourceDoc, pPageDic, pRectArray );
95     CPDF_Array* pAnnots = pPageDic->GetArray("Annots");
96     if (!pAnnots)
97         return FLATTEN_NOTHINGTODO;
98
99     FX_DWORD dwSize = pAnnots->GetCount();
100     for (int i = 0; i < (int)dwSize; i++)
101     {
102         CPDF_Object* pObj = pAnnots->GetElementValue(i);
103         if (!pObj || pObj->GetType() != PDFOBJ_DICTIONARY)
104             continue;
105
106         CPDF_Dictionary* pAnnotDic = (CPDF_Dictionary*)pObj;
107         CFX_ByteString sSubtype = pAnnotDic->GetString("Subtype");
108         if (sSubtype == "Popup")
109             continue;
110
111         int nAnnotFlag = pAnnotDic->GetInteger("F");
112         if (nAnnotFlag & ANNOTFLAG_HIDDEN)
113             continue;
114
115         if(nUsage == FLAT_NORMALDISPLAY)
116         {
117             if (nAnnotFlag & ANNOTFLAG_INVISIBLE)
118                 continue;
119
120             ParserStream( pPageDic, pAnnotDic, pRectArray, pObjectArray );
121         }
122         else
123         {
124             if (nAnnotFlag & ANNOTFLAG_PRINT)
125                 ParserStream( pPageDic, pAnnotDic, pRectArray, pObjectArray );
126         }
127     }
128     return FLATTEN_SUCCESS;
129 }
130
131
132 FX_FLOAT GetMinMaxValue( CPDF_RectArray& array, FPDF_TYPE type, FPDF_VALUE value)
133 {
134         int nRects = array.GetSize();
135         FX_FLOAT fRet = 0.0f;
136         
137         if (nRects <= 0)return 0.0f;
138         
139         FX_FLOAT* pArray = new FX_FLOAT[nRects];
140         switch(value)
141         {
142         case LEFT:
143                 {
144                         for (int i = 0; i < nRects; i++)
145                                 pArray[i] = CPDF_Rect(array.GetAt(i)).left;
146                         
147                         break;
148                 }
149         case TOP:
150                 {
151                         for (int i = 0; i < nRects; i++)
152                                 pArray[i] = CPDF_Rect(array.GetAt(i)).top;
153                         
154                         break;
155                 }
156         case RIGHT:
157                 {
158                         for (int i = 0; i < nRects; i++)
159                                 pArray[i] = CPDF_Rect(array.GetAt(i)).right;
160                         
161                         break;
162                 }
163         case BOTTOM:
164                 {
165                         for (int i = 0; i < nRects; i++)
166                                 pArray[i] = CPDF_Rect(array.GetAt(i)).bottom;
167                         
168                         break;
169                 }
170         default:
171                 break;
172         }
173         fRet = pArray[0];
174         if (type == MAX)
175         {
176                 for (int i = 1; i < nRects; i++)
177                         if (fRet <= pArray[i])
178                                 fRet = pArray[i];
179         }
180         else
181         {
182                 for (int i = 1; i < nRects; i++)
183                         if (fRet >= pArray[i])
184                                 fRet = pArray[i];
185         }
186         delete[] pArray;
187         return fRet;
188 }
189
190 CPDF_Rect CalculateRect( CPDF_RectArray * pRectArray )
191 {
192
193         CPDF_Rect rcRet;
194         
195         rcRet.left = GetMinMaxValue(*pRectArray, MIN, LEFT);
196         rcRet.top = GetMinMaxValue(*pRectArray, MAX, TOP);
197         rcRet.right = GetMinMaxValue(*pRectArray, MAX, RIGHT);
198         rcRet.bottom = GetMinMaxValue(*pRectArray, MIN, BOTTOM);
199         
200         return rcRet;
201 }
202
203
204 void SetPageContents(CFX_ByteString key, CPDF_Dictionary* pPage, CPDF_Document* pDocument)
205 {
206         CPDF_Object* pContentsObj = pPage->GetStream("Contents");
207         if (!pContentsObj)
208         {
209                 pContentsObj = pPage->GetArray("Contents");
210         }
211         
212         if (!pContentsObj)
213         {
214                 //Create a new contents dictionary
215                 if (!key.IsEmpty())
216                 {
217                         CPDF_Stream* pNewContents = FX_NEW CPDF_Stream(NULL, 0, FX_NEW CPDF_Dictionary);
218                         if (!pNewContents)return;
219                         pPage->SetAtReference("Contents", pDocument, pDocument->AddIndirectObject(pNewContents));
220                         
221                         CFX_ByteString sStream;
222                         sStream.Format("q 1 0 0 1 0 0 cm /%s Do Q", key.c_str());
223                         pNewContents->SetData((const uint8_t*)sStream, sStream.GetLength(), FALSE, FALSE);
224                 }
225                 return;
226         }
227
228         int iType = pContentsObj->GetType();
229         CPDF_Array* pContentsArray = NULL;
230         
231         switch(iType)
232         {
233         case PDFOBJ_STREAM:
234                 {
235                         pContentsArray = FX_NEW CPDF_Array;
236                         CPDF_Stream* pContents = (CPDF_Stream*)pContentsObj;
237                         FX_DWORD dwObjNum = pDocument->AddIndirectObject(pContents);
238                         CPDF_StreamAcc acc;
239                         acc.LoadAllData(pContents);
240                         CFX_ByteString sStream = "q\n";
241                         CFX_ByteString sBody = CFX_ByteString((const FX_CHAR*)acc.GetData(), acc.GetSize());
242                         sStream = sStream + sBody + "\nQ";
243                         pContents->SetData((const uint8_t*)sStream, sStream.GetLength(), FALSE, FALSE);
244                         pContentsArray->AddReference(pDocument, dwObjNum);
245                         break;
246                 }
247                 
248         case PDFOBJ_ARRAY:
249                 {
250                         pContentsArray = (CPDF_Array*)pContentsObj;
251                         break;
252                 }
253         default:
254                 break;
255         }       
256         
257         if (!pContentsArray)return;
258         
259         FX_DWORD dwObjNum = pDocument->AddIndirectObject(pContentsArray);
260         pPage->SetAtReference("Contents", pDocument, dwObjNum);
261         
262         if (!key.IsEmpty())
263         {
264                 CPDF_Stream* pNewContents = FX_NEW CPDF_Stream(NULL, 0, FX_NEW CPDF_Dictionary);
265                 dwObjNum = pDocument->AddIndirectObject(pNewContents);
266                 pContentsArray->AddReference(pDocument, dwObjNum);
267                 
268                 CFX_ByteString sStream;
269                 sStream.Format("q 1 0 0 1 0 0 cm /%s Do Q", key.c_str());
270                 pNewContents->SetData((const uint8_t*)sStream, sStream.GetLength(), FALSE, FALSE);
271         }
272 }
273  
274 CFX_AffineMatrix GetMatrix(CPDF_Rect rcAnnot, CPDF_Rect rcStream, CFX_AffineMatrix matrix)
275 {
276         if(rcStream.IsEmpty())
277                 return CFX_AffineMatrix();
278         
279         matrix.TransformRect(rcStream);
280         rcStream.Normalize();
281         
282         FX_FLOAT a = rcAnnot.Width()/rcStream.Width();
283         FX_FLOAT d = rcAnnot.Height()/rcStream.Height();
284         
285         FX_FLOAT e = rcAnnot.left - rcStream.left * a;
286         FX_FLOAT f = rcAnnot.bottom - rcStream.bottom * d;
287         return CFX_AffineMatrix(a, 0, 0, d, e, f);
288 }
289
290 void GetOffset(FX_FLOAT& fa, FX_FLOAT& fd, FX_FLOAT& fe, FX_FLOAT& ff, CPDF_Rect rcAnnot, CPDF_Rect rcStream, CFX_AffineMatrix matrix)
291 {
292         FX_FLOAT fStreamWidth = 0.0f;
293         FX_FLOAT fStreamHeight = 0.0f;
294
295
296         
297         if (matrix.a != 0 && matrix.d != 0)
298         {
299                 fStreamWidth = rcStream.right - rcStream.left;
300                 fStreamHeight = rcStream.top - rcStream.bottom;
301         }
302         else
303         {
304                 fStreamWidth = rcStream.top - rcStream.bottom;
305                 fStreamHeight = rcStream.right - rcStream.left;
306         }
307         
308         FX_FLOAT x1 = matrix.a * rcStream.left + matrix.c * rcStream.bottom + matrix.e;
309         FX_FLOAT y1 = matrix.b * rcStream.left + matrix.d * rcStream.bottom + matrix.f;
310         FX_FLOAT x2 = matrix.a * rcStream.left + matrix.c * rcStream.top + matrix.e;
311         FX_FLOAT y2 = matrix.b * rcStream.left + matrix.d * rcStream.top + matrix.f;
312         FX_FLOAT x3 = matrix.a * rcStream.right + matrix.c * rcStream.bottom + matrix.e;
313         FX_FLOAT y3 = matrix.b * rcStream.right + matrix.d * rcStream.bottom + matrix.f;
314         FX_FLOAT x4 = matrix.a * rcStream.right + matrix.c * rcStream.top + matrix.e;
315         FX_FLOAT y4 = matrix.b * rcStream.right + matrix.d * rcStream.top + matrix.f;
316         
317         FX_FLOAT left = FX_MIN(FX_MIN(x1, x2), FX_MIN(x3, x4));
318         FX_FLOAT bottom = FX_MIN(FX_MIN(y1, y2), FX_MIN(y3, y4));
319         
320         fa = (rcAnnot.right - rcAnnot.left)/fStreamWidth;
321         fd = (rcAnnot.top - rcAnnot.bottom)/fStreamHeight;
322         fe = rcAnnot.left - left * fa;
323         ff = rcAnnot.bottom - bottom * fd;
324 }
325
326
327 DLLEXPORT int STDCALL FPDFPage_Flatten( FPDF_PAGE page, int nFlag)
328 {
329         if (!page)
330         {
331                 return FLATTEN_FAIL;
332         }
333
334         CPDF_Page * pPage = ((CPDFXFA_Page*)( page ))->GetPDFPage();
335         if (!pPage)
336                 return FLATTEN_FAIL;
337
338         CPDF_Document * pDocument = pPage->m_pDocument;
339         CPDF_Dictionary * pPageDict = pPage->m_pFormDict;
340         
341         if ( !pDocument || !pPageDict )
342         {
343                 return FLATTEN_FAIL;
344         }
345
346         CPDF_ObjectArray ObjectArray;
347         CPDF_RectArray  RectArray;
348
349         int iRet = FLATTEN_FAIL;
350         iRet = ParserAnnots( pDocument, pPageDict, &RectArray, &ObjectArray, nFlag);
351         if (iRet == FLATTEN_NOTHINGTODO || iRet == FLATTEN_FAIL)
352                 return iRet;
353
354         CPDF_Rect rcOriginalCB;
355         CPDF_Rect rcMerger = CalculateRect( &RectArray );
356         CPDF_Rect rcOriginalMB = pPageDict->GetRect("MediaBox");
357
358         if (pPageDict->KeyExist("CropBox"))
359                 rcOriginalMB = pPageDict->GetRect("CropBox");
360         
361         if (rcOriginalMB.IsEmpty())     
362         {
363                 rcOriginalMB = CPDF_Rect(0.0f, 0.0f, 612.0f, 792.0f);
364         }
365         
366         rcMerger.left = rcMerger.left < rcOriginalMB.left? rcOriginalMB.left : rcMerger.left;
367         rcMerger.right = rcMerger.right > rcOriginalMB.right? rcOriginalMB.right : rcMerger.right;
368         rcMerger.top = rcMerger.top > rcOriginalMB.top? rcOriginalMB.top : rcMerger.top;
369         rcMerger.bottom = rcMerger.bottom < rcOriginalMB.bottom? rcOriginalMB.bottom : rcMerger.bottom;
370         
371         if (pPageDict->KeyExist("ArtBox"))
372                 rcOriginalCB = pPageDict->GetRect("ArtBox");
373         else
374                 rcOriginalCB = rcOriginalMB;
375
376         if (!rcOriginalMB.IsEmpty())
377         {
378                 CPDF_Array* pMediaBox = FX_NEW CPDF_Array();    
379
380                 pMediaBox->Add(FX_NEW CPDF_Number(rcOriginalMB.left));
381                 pMediaBox->Add(FX_NEW CPDF_Number(rcOriginalMB.bottom));
382                 pMediaBox->Add(FX_NEW CPDF_Number(rcOriginalMB.right));
383                 pMediaBox->Add(FX_NEW CPDF_Number(rcOriginalMB.top));
384
385                 pPageDict->SetAt("MediaBox",pMediaBox);
386         }
387         
388         if (!rcOriginalCB.IsEmpty())
389         {
390                 CPDF_Array* pCropBox = FX_NEW CPDF_Array();
391                 pCropBox->Add(FX_NEW CPDF_Number(rcOriginalCB.left));
392                 pCropBox->Add(FX_NEW CPDF_Number(rcOriginalCB.bottom));
393                 pCropBox->Add(FX_NEW CPDF_Number(rcOriginalCB.right));
394                 pCropBox->Add(FX_NEW CPDF_Number(rcOriginalCB.top));
395                 pPageDict->SetAt("ArtBox", pCropBox);
396         }
397
398         CPDF_Dictionary* pRes = NULL;
399         pRes = pPageDict->GetDict("Resources");
400         if (!pRes)
401         {
402                 pRes = FX_NEW CPDF_Dictionary;
403                 pPageDict->SetAt( "Resources", pRes );
404         }
405
406         CPDF_Stream* pNewXObject = FX_NEW CPDF_Stream(NULL, 0, FX_NEW CPDF_Dictionary);
407         FX_DWORD dwObjNum = pDocument->AddIndirectObject(pNewXObject);
408         CPDF_Dictionary* pPageXObject = pRes->GetDict("XObject");
409         if (!pPageXObject)
410         {
411                 pPageXObject = FX_NEW CPDF_Dictionary;
412                 pRes->SetAt("XObject", pPageXObject);
413         }
414
415         CFX_ByteString key = "";
416         int nStreams = ObjectArray.GetSize();
417
418         if (nStreams > 0)
419         {
420                 for (int iKey = 0; /*iKey < 100*/; iKey++)
421                 {
422                         char sExtend[5] = {0};
423                         FXSYS_itoa(iKey, sExtend, 10);
424                         key = CFX_ByteString("FFT") + CFX_ByteString(sExtend);
425
426                         if (!pPageXObject->KeyExist(key))
427                                 break;
428                 }
429         }
430
431         SetPageContents(key, pPageDict, pDocument);
432
433         CPDF_Dictionary* pNewXORes = NULL;
434
435         if (!key.IsEmpty())
436         {
437                 pPageXObject->SetAtReference(key, pDocument, dwObjNum);
438                 CPDF_Dictionary* pNewOXbjectDic = pNewXObject->GetDict();
439                 pNewXORes = FX_NEW CPDF_Dictionary;
440                 pNewOXbjectDic->SetAt("Resources", pNewXORes);
441                 pNewOXbjectDic->SetAtName("Type", "XObject");
442                 pNewOXbjectDic->SetAtName("Subtype", "Form");
443                 pNewOXbjectDic->SetAtInteger("FormType", 1);
444                 pNewOXbjectDic->SetAtName("Name", "FRM");
445                 CPDF_Rect rcBBox = pPageDict->GetRect("ArtBox"); 
446                 pNewOXbjectDic->SetAtRect("BBox", rcBBox);
447         }
448         
449         for (int i = 0; i < nStreams; i++)
450         {
451                 CPDF_Dictionary* pAnnotDic = ObjectArray.GetAt(i);
452                 if (!pAnnotDic)continue;
453
454                 CPDF_Rect rcAnnot = pAnnotDic->GetRect("Rect");
455                 rcAnnot.Normalize();
456
457                 CFX_ByteString sAnnotState = pAnnotDic->GetString("AS");
458                 CPDF_Dictionary* pAnnotAP = pAnnotDic->GetDict("AP");
459                 if (!pAnnotAP)continue;
460
461                 CPDF_Stream* pAPStream = pAnnotAP->GetStream("N");
462                 if (!pAPStream)
463                 {
464                         CPDF_Dictionary* pAPDic = pAnnotAP->GetDict("N");
465                         if (!pAPDic)continue;
466
467                         if (!sAnnotState.IsEmpty())
468                         {
469                                 pAPStream = pAPDic->GetStream(sAnnotState);
470                         }
471                         else
472                         {
473                                 FX_POSITION pos = pAPDic->GetStartPos();
474                                 if (pos)
475                                 {
476                                         CFX_ByteString sKey;
477                                         CPDF_Object* pFirstObj = pAPDic->GetNextElement(pos, sKey);
478                                         if (pFirstObj)
479                                         {
480                                                 if (pFirstObj->GetType() == PDFOBJ_REFERENCE)
481                                                         pFirstObj = pFirstObj->GetDirect();
482                                                 
483                                                 if (pFirstObj->GetType() != PDFOBJ_STREAM)
484                                                         continue;
485
486                                                 pAPStream = (CPDF_Stream*)pFirstObj;
487                                         }
488                                 }
489                         }
490                 }
491
492                 if (!pAPStream)continue;
493
494                 CPDF_Dictionary* pAPDic = pAPStream->GetDict();
495                 CFX_AffineMatrix matrix = pAPDic->GetMatrix("Matrix");
496
497                 CPDF_Rect rcStream;
498                 if (pAPDic->KeyExist("Rect"))
499                         rcStream = pAPDic->GetRect("Rect");
500                 else if (pAPDic->KeyExist("BBox"))
501                         rcStream = pAPDic->GetRect("BBox");
502
503                 if (rcStream.IsEmpty())continue;
504
505                 CPDF_Object* pObj = pAPStream;
506
507                 if (pObj)
508                 {               
509                         CPDF_Dictionary* pObjDic = pObj->GetDict();
510                         if (pObjDic)
511                         {
512                                 pObjDic->SetAtName("Type", "XObject");
513                                 pObjDic->SetAtName("Subtype", "Form");
514                         }
515                 }
516
517                 CPDF_Dictionary* pXObject = pNewXORes->GetDict("XObject");
518                 if (!pXObject)
519                 {
520                         pXObject = FX_NEW CPDF_Dictionary;
521                         pNewXORes->SetAt("XObject", pXObject);
522                 }
523
524                 CFX_ByteString sFormName;
525                 sFormName.Format("F%d", i);
526                 FX_DWORD dwObjNum = pDocument->AddIndirectObject(pObj);
527                 pXObject->SetAtReference(sFormName, pDocument, dwObjNum);
528
529                 CPDF_StreamAcc acc;
530                 acc.LoadAllData(pNewXObject);
531
532                 const uint8_t* pData = acc.GetData();
533                 CFX_ByteString sStream(pData, acc.GetSize());
534                 CFX_ByteString sTemp;
535
536                 if (matrix.IsIdentity())
537                 {
538                         matrix.a = 1.0f;
539                         matrix.b = 0.0f;
540                         matrix.c = 0.0f;
541                         matrix.d = 1.0f;
542                         matrix.e = 0.0f;
543                         matrix.f = 0.0f;
544                 }
545
546                 CFX_AffineMatrix m = GetMatrix(rcAnnot, rcStream, matrix);
547                 sTemp.Format("q %f 0 0 %f %f %f cm /%s Do Q\n", m.a, m.d, m.e, m.f, sFormName.c_str());
548                 sStream += sTemp;
549
550                 pNewXObject->SetData((const uint8_t*)sStream, sStream.GetLength(), FALSE, FALSE);
551         }
552         pPageDict->RemoveAt( "Annots" );
553
554         ObjectArray.RemoveAll();
555         RectArray.RemoveAll();
556
557         return FLATTEN_SUCCESS;
558 }