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