Make (and verify) public/ files compile under C.
[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
10 typedef CFX_ArrayTemplate<CPDF_Dictionary*> CPDF_ObjectArray;
11 typedef CFX_ArrayTemplate<CPDF_Rect> CPDF_RectArray;
12
13 enum FPDF_TYPE { MAX, MIN };
14 enum FPDF_VALUE { TOP, LEFT, RIGHT, BOTTOM };
15
16 FX_BOOL IsValiableRect(CPDF_Rect rect, CPDF_Rect rcPage)
17 {
18         if ( rect.left - rect.right > 0.000001f || 
19                  rect.bottom - rect.top > 0.000001f)
20                 return FALSE;
21         
22         if (rect.left == 0.0f &&
23                 rect.top == 0.0f &&
24                 rect.right == 0.0f &&
25                 rect.bottom == 0.0f)
26                 return FALSE;
27         
28         if (!rcPage.IsEmpty())
29         {
30                 if (rect.left - rcPage.left < -10.000001f ||
31                         rect.right - rcPage.right > 10.000001f ||
32                         rect.top - rcPage.top > 10.000001f ||
33                         rect.bottom - rcPage.bottom < -10.000001f)
34                         return FALSE;
35         }
36         
37         return TRUE;
38 }
39
40
41 FX_BOOL GetContentsRect( CPDF_Document * pDoc, CPDF_Dictionary* pDict, CPDF_RectArray * pRectArray )
42 {
43         CPDF_Page* pPDFPage = new CPDF_Page;
44         pPDFPage->Load( pDoc, pDict, FALSE );
45         pPDFPage->ParseContent();
46
47         FX_POSITION pos = pPDFPage->GetFirstObjectPosition();
48         
49         while (pos)
50         {
51                 CPDF_PageObject* pPageObject = pPDFPage->GetNextObject(pos);
52                 if (!pPageObject)continue;
53                 
54                 CPDF_Rect rc;
55                 rc.left = pPageObject->m_Left;
56                 rc.right = pPageObject->m_Right;
57                 rc.bottom = pPageObject->m_Bottom;
58                 rc.top = pPageObject->m_Top;
59                 
60                 if (IsValiableRect(rc, pDict->GetRect("MediaBox")))
61                 {
62                         pRectArray->Add(rc);
63                 }
64         }
65         
66         delete pPDFPage;
67         return TRUE;
68 }
69
70
71 void ParserStream( CPDF_Dictionary * pPageDic, CPDF_Dictionary* pStream, CPDF_RectArray * pRectArray, CPDF_ObjectArray * pObjectArray )
72 {
73         if (!pStream)return;
74         CPDF_Rect rect;
75         if (pStream->KeyExist("Rect"))
76                 rect = pStream->GetRect("Rect");
77         else if (pStream->KeyExist("BBox"))
78                 rect = pStream->GetRect("BBox");
79         
80         if (IsValiableRect(rect, pPageDic->GetRect("MediaBox")))
81                 pRectArray->Add(rect);
82         
83         pObjectArray->Add(pStream);
84 }
85
86
87 int ParserAnnots( CPDF_Document* pSourceDoc, CPDF_Dictionary * pPageDic, CPDF_RectArray * pRectArray, CPDF_ObjectArray * pObjectArray, int nUsage)
88 {
89         if (!pSourceDoc || !pPageDic) return FLATTEN_FAIL;
90         
91         GetContentsRect( pSourceDoc, pPageDic, pRectArray );
92         CPDF_Array* pAnnots = pPageDic->GetArray("Annots");
93         if (pAnnots)
94         {
95                 FX_DWORD dwSize = pAnnots->GetCount();
96                 
97                 for (int i = 0; i < (int)dwSize; i++)
98                 {
99                         CPDF_Object* pObj = pAnnots->GetElementValue(i);
100                         
101                         if (!pObj)continue;
102                         
103                         if (pObj->GetType() == PDFOBJ_DICTIONARY)
104                         {
105                                 CPDF_Dictionary* pAnnotDic = (CPDF_Dictionary*)pObj;
106                                 CFX_ByteString sSubtype = pAnnotDic->GetString("Subtype");
107                                 if (sSubtype == "Popup")continue;
108
109                                 int nAnnotFlag = pAnnotDic->GetInteger("F");
110
111                                 if(nAnnotFlag & ANNOTFLAG_HIDDEN) 
112                                         continue;
113                                 if(nUsage == FLAT_NORMALDISPLAY)
114                                 {
115                                         if(nAnnotFlag & ANNOTFLAG_INVISIBLE)
116                                                 continue;
117                                         ParserStream( pPageDic, pAnnotDic, pRectArray, pObjectArray );          
118                                 }
119                                 else
120                                 {
121                                         if(nAnnotFlag & ANNOTFLAG_PRINT)
122                                                 ParserStream( pPageDic, pAnnotDic, pRectArray, pObjectArray );
123                                 }                       
124                         }
125                 }
126                 return FLATTEN_SUCCESS;
127         }else{
128                 return FLATTEN_NOTINGTODO;
129         }
130 }
131
132
133 FX_FLOAT GetMinMaxValue( CPDF_RectArray& array, FPDF_TYPE type, FPDF_VALUE value)
134 {
135         int nRects = array.GetSize();
136         FX_FLOAT fRet = 0.0f;
137         
138         if (nRects <= 0)return 0.0f;
139         
140         FX_FLOAT* pArray = new FX_FLOAT[nRects];
141         switch(value)
142         {
143         case LEFT:
144                 {
145                         for (int i = 0; i < nRects; i++)
146                                 pArray[i] = CPDF_Rect(array.GetAt(i)).left;
147                         
148                         break;
149                 }
150         case TOP:
151                 {
152                         for (int i = 0; i < nRects; i++)
153                                 pArray[i] = CPDF_Rect(array.GetAt(i)).top;
154                         
155                         break;
156                 }
157         case RIGHT:
158                 {
159                         for (int i = 0; i < nRects; i++)
160                                 pArray[i] = CPDF_Rect(array.GetAt(i)).right;
161                         
162                         break;
163                 }
164         case BOTTOM:
165                 {
166                         for (int i = 0; i < nRects; i++)
167                                 pArray[i] = CPDF_Rect(array.GetAt(i)).bottom;
168                         
169                         break;
170                 }
171         default:
172                 break;
173         }
174         fRet = pArray[0];
175         if (type == MAX)
176         {
177                 for (int i = 1; i < nRects; i++)
178                         if (fRet <= pArray[i])
179                                 fRet = pArray[i];
180         }
181         else
182         {
183                 for (int i = 1; i < nRects; i++)
184                         if (fRet >= pArray[i])
185                                 fRet = pArray[i];
186         }
187         delete[] pArray;
188         return fRet;
189 }
190
191 CPDF_Rect CalculateRect( CPDF_RectArray * pRectArray )
192 {
193
194         CPDF_Rect rcRet;
195         
196         rcRet.left = GetMinMaxValue(*pRectArray, MIN, LEFT);
197         rcRet.top = GetMinMaxValue(*pRectArray, MAX, TOP);
198         rcRet.right = GetMinMaxValue(*pRectArray, MAX, RIGHT);
199         rcRet.bottom = GetMinMaxValue(*pRectArray, MIN, BOTTOM);
200         
201         return rcRet;
202 }
203
204
205 void SetPageContents(CFX_ByteString key, CPDF_Dictionary* pPage, CPDF_Document* pDocument)
206 {
207         CPDF_Object* pContentsObj = pPage->GetStream("Contents");
208         if (!pContentsObj)
209         {
210                 pContentsObj = pPage->GetArray("Contents");
211         }
212         
213         if (!pContentsObj)
214         {
215                 //Create a new contents dictionary
216                 if (!key.IsEmpty())
217                 {
218                         CPDF_Stream* pNewContents = new CPDF_Stream(NULL, 0, new CPDF_Dictionary);
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((FX_LPCBYTE)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 = 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((FX_LPCSTR)acc.GetData(), acc.GetSize());
242                         sStream = sStream + sBody + "\nQ";
243                         pContents->SetData((FX_LPCBYTE)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 = new CPDF_Stream(NULL, 0, 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((FX_LPCBYTE)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 = (CPDF_Page*)( page );
335         CPDF_Document * pDocument = pPage->m_pDocument;
336         CPDF_Dictionary * pPageDict = pPage->m_pFormDict;
337         
338         if ( !pDocument || !pPageDict )
339         {
340                 return FLATTEN_FAIL;
341         }
342
343         CPDF_ObjectArray ObjectArray;
344         CPDF_RectArray  RectArray;
345
346         int iRet = FLATTEN_FAIL;
347         iRet = ParserAnnots( pDocument, pPageDict, &RectArray, &ObjectArray, nFlag);
348         if (iRet == FLATTEN_NOTINGTODO)
349         {
350                 return FLATTEN_NOTINGTODO;
351         }else if (iRet == FLATTEN_FAIL)
352         {
353                 return FLATTEN_FAIL;
354         }
355         
356         CPDF_Rect rcOriginalCB;
357         CPDF_Rect rcMerger = CalculateRect( &RectArray );
358         CPDF_Rect rcOriginalMB = pPageDict->GetRect("MediaBox");
359
360         if (pPageDict->KeyExist("CropBox"))
361                 rcOriginalMB = pPageDict->GetRect("CropBox");
362         
363         if (rcOriginalMB.IsEmpty())     
364         {
365                 rcOriginalMB = CPDF_Rect(0.0f, 0.0f, 612.0f, 792.0f);
366         }
367         
368         rcMerger.left = rcMerger.left < rcOriginalMB.left? rcOriginalMB.left : rcMerger.left;
369         rcMerger.right = rcMerger.right > rcOriginalMB.right? rcOriginalMB.right : rcMerger.right;
370         rcMerger.top = rcMerger.top > rcOriginalMB.top? rcOriginalMB.top : rcMerger.top;
371         rcMerger.bottom = rcMerger.bottom < rcOriginalMB.bottom? rcOriginalMB.bottom : rcMerger.bottom;
372         
373         if (pPageDict->KeyExist("ArtBox"))
374                 rcOriginalCB = pPageDict->GetRect("ArtBox");
375         else
376                 rcOriginalCB = rcOriginalMB;
377
378         if (!rcOriginalMB.IsEmpty())
379         {
380                 CPDF_Array* pMediaBox = new CPDF_Array();
381                 pMediaBox->Add(new CPDF_Number(rcOriginalMB.left));
382                 pMediaBox->Add(new CPDF_Number(rcOriginalMB.bottom));
383                 pMediaBox->Add(new CPDF_Number(rcOriginalMB.right));
384                 pMediaBox->Add(new CPDF_Number(rcOriginalMB.top));
385                 pPageDict->SetAt("MediaBox",pMediaBox);
386         }
387
388         if (!rcOriginalCB.IsEmpty())
389         {
390                 CPDF_Array* pCropBox = new CPDF_Array();
391                 pCropBox->Add(new CPDF_Number(rcOriginalCB.left));
392                 pCropBox->Add(new CPDF_Number(rcOriginalCB.bottom));
393                 pCropBox->Add(new CPDF_Number(rcOriginalCB.right));
394                 pCropBox->Add(new CPDF_Number(rcOriginalCB.top));
395                 pPageDict->SetAt("ArtBox", pCropBox);
396         }
397
398         CPDF_Dictionary* pRes = pPageDict->GetDict("Resources");
399         if (!pRes)
400         {
401                 pRes = new CPDF_Dictionary;
402                 pPageDict->SetAt("Resources", pRes );
403         }
404
405         CPDF_Stream* pNewXObject = new CPDF_Stream(NULL, 0, new CPDF_Dictionary);
406         FX_DWORD dwObjNum = pDocument->AddIndirectObject(pNewXObject);
407         CPDF_Dictionary* pPageXObject = pRes->GetDict("XObject");
408         if (!pPageXObject)
409         {
410                 pPageXObject = new CPDF_Dictionary;
411                 pRes->SetAt("XObject", pPageXObject);
412         }
413
414         CFX_ByteString key = "";
415         int nStreams = ObjectArray.GetSize();
416
417         if (nStreams > 0)
418         {
419                 for (int iKey = 0; /*iKey < 100*/; iKey++)
420                 {
421                         char sExtend[5] = {0};
422                         FXSYS_itoa(iKey, sExtend, 10);
423                         key = CFX_ByteString("FFT") + CFX_ByteString(sExtend);
424
425                         if (!pPageXObject->KeyExist(key))
426                                 break;
427                 }
428         }
429
430         SetPageContents(key, pPageDict, pDocument);
431
432         CPDF_Dictionary* pNewXORes = NULL;
433
434         if (!key.IsEmpty())
435         {
436                 pPageXObject->SetAtReference(key, pDocument, dwObjNum);
437                 CPDF_Dictionary* pNewOXbjectDic = pNewXObject->GetDict();
438                 pNewXORes = new CPDF_Dictionary;
439                 pNewOXbjectDic->SetAt("Resources", pNewXORes);
440                 pNewOXbjectDic->SetAtName("Type", "XObject");
441                 pNewOXbjectDic->SetAtName("Subtype", "Form");
442                 pNewOXbjectDic->SetAtInteger("FormType", 1);
443                 pNewOXbjectDic->SetAtName("Name", "FRM");
444                 CPDF_Rect rcBBox = pPageDict->GetRect("ArtBox");
445                 pNewOXbjectDic->SetAtRect("BBox", rcBBox);
446         }
447
448         for (int i = 0; i < nStreams; i++)
449         {
450                 CPDF_Dictionary* pAnnotDic = ObjectArray.GetAt(i);
451                 if (!pAnnotDic)continue;
452
453                 CPDF_Rect rcAnnot = pAnnotDic->GetRect("Rect");
454                 rcAnnot.Normalize();
455
456                 CFX_ByteString sAnnotState = pAnnotDic->GetString("AS");
457                 CPDF_Dictionary* pAnnotAP = pAnnotDic->GetDict("AP");
458                 if (!pAnnotAP)continue;
459
460                 CPDF_Stream* pAPStream = pAnnotAP->GetStream("N");
461                 if (!pAPStream)
462                 {
463                         CPDF_Dictionary* pAPDic = pAnnotAP->GetDict("N");
464                         if (!pAPDic)continue;
465
466                         if (!sAnnotState.IsEmpty())
467                         {
468                                 pAPStream = pAPDic->GetStream(sAnnotState);
469                         }
470                         else
471                         {
472                                 FX_POSITION pos = pAPDic->GetStartPos();
473                                 if (pos)
474                                 {
475                                         CFX_ByteString sKey;
476                                         CPDF_Object* pFirstObj = pAPDic->GetNextElement(pos, sKey);
477                                         if (pFirstObj)
478                                         {
479                                                 if (pFirstObj->GetType() == PDFOBJ_REFERENCE)
480                                                         pFirstObj = pFirstObj->GetDirect();
481                                                 
482                                                 if (pFirstObj->GetType() != PDFOBJ_STREAM)
483                                                         continue;
484
485                                                 pAPStream = (CPDF_Stream*)pFirstObj;
486                                         }
487                                 }
488                         }
489                 }
490
491                 if (!pAPStream)continue;
492
493                 CPDF_Dictionary* pAPDic = pAPStream->GetDict();
494                 CFX_AffineMatrix matrix = pAPDic->GetMatrix("Matrix");
495
496                 CPDF_Rect rcStream;
497                 if (pAPDic->KeyExist("Rect"))
498                         rcStream = pAPDic->GetRect("Rect");
499                 else if (pAPDic->KeyExist("BBox"))
500                         rcStream = pAPDic->GetRect("BBox");
501
502                 if (rcStream.IsEmpty())continue;
503
504                 CPDF_Object* pObj = pAPStream;
505
506                 if (pObj)
507                 {               
508                         CPDF_Dictionary* pObjDic = pObj->GetDict();
509                         if (pObjDic)
510                         {
511                                 pObjDic->SetAtName("Type", "XObject");
512                                 pObjDic->SetAtName("Subtype", "Form");
513                         }
514                 }
515
516                 CPDF_Dictionary* pXObject = pNewXORes->GetDict("XObject");
517                 if (!pXObject)
518                 {
519                         pXObject = new CPDF_Dictionary;
520                         pNewXORes->SetAt("XObject", pXObject);
521                 }
522
523                 CFX_ByteString sFormName;
524                 sFormName.Format("F%d", i);
525                 FX_DWORD dwObjNum = pDocument->AddIndirectObject(pObj);
526                 pXObject->SetAtReference(sFormName, pDocument, dwObjNum);
527
528                 CPDF_StreamAcc acc;
529                 acc.LoadAllData(pNewXObject);
530
531                 FX_LPCBYTE pData = acc.GetData();
532                 CFX_ByteString sStream(pData, acc.GetSize());
533                 CFX_ByteString sTemp;
534
535                 if (matrix.IsIdentity())
536                 {
537                         matrix.a = 1.0f;
538                         matrix.b = 0.0f;
539                         matrix.c = 0.0f;
540                         matrix.d = 1.0f;
541                         matrix.e = 0.0f;
542                         matrix.f = 0.0f;
543                 }
544
545                 CFX_AffineMatrix m = GetMatrix(rcAnnot, rcStream, matrix);
546                 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());
547                 sStream += sTemp;
548
549                 pNewXObject->SetData((FX_LPCBYTE)sStream, sStream.GetLength(), FALSE, FALSE);
550         }
551         pPageDict->RemoveAt( "Annots" );
552
553         ObjectArray.RemoveAll();
554         RectArray.RemoveAll();
555
556         return FLATTEN_SUCCESS;
557 }