blob: b11e4a621d5099c16f2d1a8041c5a18140f4e736 [file] [log] [blame]
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001// 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.
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07004
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07005// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
Lei Zhangb4e7f302015-11-06 15:52:32 -08007#include "public/fpdf_flatten.h"
8
Lei Zhang375a8642016-01-11 11:59:17 -08009#include <algorithm>
10
dsinclair41872fa2016-10-04 11:29:35 -070011#include "core/fpdfapi/page/cpdf_page.h"
12#include "core/fpdfapi/page/cpdf_pageobject.h"
dsinclair488b7ad2016-10-04 11:55:50 -070013#include "core/fpdfapi/parser/cpdf_array.h"
14#include "core/fpdfapi/parser/cpdf_document.h"
15#include "core/fpdfapi/parser/cpdf_number.h"
16#include "core/fpdfapi/parser/cpdf_stream.h"
17#include "core/fpdfapi/parser/cpdf_stream_acc.h"
dsinclair1727aee2016-09-29 13:12:56 -070018#include "core/fpdfdoc/cpdf_annot.h"
dsinclair114e46a2016-09-29 17:18:21 -070019#include "fpdfsdk/fsdk_define.h"
tsepez74b8c6e2016-10-12 09:38:41 -070020#include "third_party/base/stl_util.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070021
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070022enum FPDF_TYPE { MAX, MIN };
23enum FPDF_VALUE { TOP, LEFT, RIGHT, BOTTOM };
24
tsepez381fc832016-10-10 14:06:44 -070025namespace {
26
tsepez4cf55152016-11-02 14:37:54 -070027bool IsValiableRect(CFX_FloatRect rect, CFX_FloatRect rcPage) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070028 if (rect.left - rect.right > 0.000001f || rect.bottom - rect.top > 0.000001f)
tsepez4cf55152016-11-02 14:37:54 -070029 return false;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070030
Nico Weber9d8ec5a2015-08-04 13:00:21 -070031 if (rect.left == 0.0f && rect.top == 0.0f && rect.right == 0.0f &&
32 rect.bottom == 0.0f)
tsepez4cf55152016-11-02 14:37:54 -070033 return false;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070034
Nico Weber9d8ec5a2015-08-04 13:00:21 -070035 if (!rcPage.IsEmpty()) {
36 if (rect.left - rcPage.left < -10.000001f ||
37 rect.right - rcPage.right > 10.000001f ||
38 rect.top - rcPage.top > 10.000001f ||
39 rect.bottom - rcPage.bottom < -10.000001f)
tsepez4cf55152016-11-02 14:37:54 -070040 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070041 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070042
tsepez4cf55152016-11-02 14:37:54 -070043 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070044}
45
Tom Sepez2398d892016-02-17 16:46:26 -080046void GetContentsRect(CPDF_Document* pDoc,
47 CPDF_Dictionary* pDict,
tsepez6173c9d2016-11-09 16:38:03 -080048 std::vector<CFX_FloatRect>* pRectArray) {
thestig5cc24652016-04-26 11:46:02 -070049 std::unique_ptr<CPDF_Page> pPDFPage(new CPDF_Page(pDoc, pDict, false));
50 pPDFPage->ParseContent();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070051
thestig2fad11a2016-06-16 16:39:25 -070052 for (const auto& pPageObject : *pPDFPage->GetPageObjectList()) {
Tom Sepez281a9ea2016-02-26 14:24:28 -080053 CFX_FloatRect rc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070054 rc.left = pPageObject->m_Left;
55 rc.right = pPageObject->m_Right;
56 rc.bottom = pPageObject->m_Bottom;
57 rc.top = pPageObject->m_Top;
dsinclair38fd8442016-09-15 10:15:32 -070058 if (IsValiableRect(rc, pDict->GetRectFor("MediaBox")))
tsepez6173c9d2016-11-09 16:38:03 -080059 pRectArray->push_back(rc);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070060 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070061}
62
Nico Weber9d8ec5a2015-08-04 13:00:21 -070063void ParserStream(CPDF_Dictionary* pPageDic,
64 CPDF_Dictionary* pStream,
tsepez6173c9d2016-11-09 16:38:03 -080065 std::vector<CFX_FloatRect>* pRectArray,
tsepez74b8c6e2016-10-12 09:38:41 -070066 std::vector<CPDF_Dictionary*>* pObjectArray) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070067 if (!pStream)
68 return;
Tom Sepez281a9ea2016-02-26 14:24:28 -080069 CFX_FloatRect rect;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070070 if (pStream->KeyExist("Rect"))
dsinclair38fd8442016-09-15 10:15:32 -070071 rect = pStream->GetRectFor("Rect");
Nico Weber9d8ec5a2015-08-04 13:00:21 -070072 else if (pStream->KeyExist("BBox"))
dsinclair38fd8442016-09-15 10:15:32 -070073 rect = pStream->GetRectFor("BBox");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070074
dsinclair38fd8442016-09-15 10:15:32 -070075 if (IsValiableRect(rect, pPageDic->GetRectFor("MediaBox")))
tsepez6173c9d2016-11-09 16:38:03 -080076 pRectArray->push_back(rect);
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070077
tsepez74b8c6e2016-10-12 09:38:41 -070078 pObjectArray->push_back(pStream);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070079}
80
Nico Weber9d8ec5a2015-08-04 13:00:21 -070081int ParserAnnots(CPDF_Document* pSourceDoc,
82 CPDF_Dictionary* pPageDic,
tsepez6173c9d2016-11-09 16:38:03 -080083 std::vector<CFX_FloatRect>* pRectArray,
tsepez74b8c6e2016-10-12 09:38:41 -070084 std::vector<CPDF_Dictionary*>* pObjectArray,
Nico Weber9d8ec5a2015-08-04 13:00:21 -070085 int nUsage) {
86 if (!pSourceDoc || !pPageDic)
87 return FLATTEN_FAIL;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070088
Nico Weber9d8ec5a2015-08-04 13:00:21 -070089 GetContentsRect(pSourceDoc, pPageDic, pRectArray);
dsinclair38fd8442016-09-15 10:15:32 -070090 CPDF_Array* pAnnots = pPageDic->GetArrayFor("Annots");
Nico Weber9d8ec5a2015-08-04 13:00:21 -070091 if (!pAnnots)
92 return FLATTEN_NOTHINGTODO;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070093
tsepezc3255f52016-03-25 14:52:27 -070094 uint32_t dwSize = pAnnots->GetCount();
Nico Weber9d8ec5a2015-08-04 13:00:21 -070095 for (int i = 0; i < (int)dwSize; i++) {
tsepezbd567552016-03-29 14:51:50 -070096 CPDF_Dictionary* pAnnotDic = ToDictionary(pAnnots->GetDirectObjectAt(i));
Dan Sinclairf1251c12015-10-20 16:24:45 -040097 if (!pAnnotDic)
Nico Weber9d8ec5a2015-08-04 13:00:21 -070098 continue;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070099
dsinclair38fd8442016-09-15 10:15:32 -0700100 CFX_ByteString sSubtype = pAnnotDic->GetStringFor("Subtype");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700101 if (sSubtype == "Popup")
102 continue;
103
dsinclair38fd8442016-09-15 10:15:32 -0700104 int nAnnotFlag = pAnnotDic->GetIntegerFor("F");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700105 if (nAnnotFlag & ANNOTFLAG_HIDDEN)
106 continue;
107
108 if (nUsage == FLAT_NORMALDISPLAY) {
109 if (nAnnotFlag & ANNOTFLAG_INVISIBLE)
110 continue;
111
112 ParserStream(pPageDic, pAnnotDic, pRectArray, pObjectArray);
113 } else {
114 if (nAnnotFlag & ANNOTFLAG_PRINT)
115 ParserStream(pPageDic, pAnnotDic, pRectArray, pObjectArray);
116 }
117 }
118 return FLATTEN_SUCCESS;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700119}
120
tsepez6173c9d2016-11-09 16:38:03 -0800121FX_FLOAT GetMinMaxValue(const std::vector<CFX_FloatRect>& array,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700122 FPDF_TYPE type,
123 FPDF_VALUE value) {
tsepez6173c9d2016-11-09 16:38:03 -0800124 size_t nRects = array.size();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700125 if (nRects <= 0)
126 return 0.0f;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700127
tsepez6173c9d2016-11-09 16:38:03 -0800128 std::vector<FX_FLOAT> pArray(nRects);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700129 switch (value) {
tsepez6173c9d2016-11-09 16:38:03 -0800130 case LEFT:
131 for (size_t i = 0; i < nRects; i++)
132 pArray[i] = array[i].left;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700133 break;
tsepez6173c9d2016-11-09 16:38:03 -0800134 case TOP:
135 for (size_t i = 0; i < nRects; i++)
136 pArray[i] = array[i].top;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700137 break;
tsepez6173c9d2016-11-09 16:38:03 -0800138 case RIGHT:
139 for (size_t i = 0; i < nRects; i++)
140 pArray[i] = array[i].right;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700141 break;
tsepez6173c9d2016-11-09 16:38:03 -0800142 case BOTTOM:
143 for (size_t i = 0; i < nRects; i++)
144 pArray[i] = array[i].bottom;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700145 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700146 default:
weili12367cb2016-06-03 11:22:16 -0700147 // Not reachable.
148 return 0.0f;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700149 }
tsepez6173c9d2016-11-09 16:38:03 -0800150
151 FX_FLOAT fRet = pArray[0];
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700152 if (type == MAX) {
tsepez6173c9d2016-11-09 16:38:03 -0800153 for (size_t i = 1; i < nRects; i++)
154 fRet = std::max(fRet, pArray[i]);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700155 } else {
tsepez6173c9d2016-11-09 16:38:03 -0800156 for (size_t i = 1; i < nRects; i++)
157 fRet = std::min(fRet, pArray[i]);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700158 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700159 return fRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700160}
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700161
tsepez6173c9d2016-11-09 16:38:03 -0800162CFX_FloatRect CalculateRect(std::vector<CFX_FloatRect>* pRectArray) {
Tom Sepez281a9ea2016-02-26 14:24:28 -0800163 CFX_FloatRect rcRet;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700164
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700165 rcRet.left = GetMinMaxValue(*pRectArray, MIN, LEFT);
166 rcRet.top = GetMinMaxValue(*pRectArray, MAX, TOP);
167 rcRet.right = GetMinMaxValue(*pRectArray, MAX, RIGHT);
168 rcRet.bottom = GetMinMaxValue(*pRectArray, MIN, BOTTOM);
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700169
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700170 return rcRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700171}
172
tsepez381fc832016-10-10 14:06:44 -0700173uint32_t NewIndirectContentsStream(const CFX_ByteString& key,
174 CPDF_Document* pDocument) {
tsepez70c4afd2016-11-15 11:33:44 -0800175 CPDF_Stream* pNewContents = pDocument->NewIndirect<CPDF_Stream>(
tsepez381fc832016-10-10 14:06:44 -0700176 nullptr, 0, new CPDF_Dictionary(pDocument->GetByteStringPool()));
177 CFX_ByteString sStream;
178 sStream.Format("q 1 0 0 1 0 0 cm /%s Do Q", key.c_str());
179 pNewContents->SetData(sStream.raw_str(), sStream.GetLength());
tsepez70c4afd2016-11-15 11:33:44 -0800180 return pNewContents->GetObjNum();
tsepez381fc832016-10-10 14:06:44 -0700181}
182
183void SetPageContents(const CFX_ByteString& key,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700184 CPDF_Dictionary* pPage,
185 CPDF_Document* pDocument) {
thestig1cd352e2016-06-07 17:53:06 -0700186 CPDF_Array* pContentsArray = nullptr;
tsepez381fc832016-10-10 14:06:44 -0700187 CPDF_Stream* pContentsStream = pPage->GetStreamFor("Contents");
188 if (!pContentsStream) {
189 pContentsArray = pPage->GetArrayFor("Contents");
190 if (!pContentsArray) {
191 if (!key.IsEmpty()) {
192 pPage->SetReferenceFor("Contents", pDocument,
193 NewIndirectContentsStream(key, pDocument));
194 }
195 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700196 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700197 }
tsepez381fc832016-10-10 14:06:44 -0700198 pPage->ConvertToIndirectObjectFor("Contents", pDocument);
199 if (!pContentsArray) {
tsepez70c4afd2016-11-15 11:33:44 -0800200 pContentsArray = pDocument->NewIndirect<CPDF_Array>();
tsepez381fc832016-10-10 14:06:44 -0700201 CPDF_StreamAcc acc;
202 acc.LoadAllData(pContentsStream);
203 CFX_ByteString sStream = "q\n";
Tom Sepezc25a4212016-10-14 17:45:56 -0700204 CFX_ByteString sBody =
205 CFX_ByteString((const FX_CHAR*)acc.GetData(), acc.GetSize());
tsepez381fc832016-10-10 14:06:44 -0700206 sStream = sStream + sBody + "\nQ";
207 pContentsStream->SetData(sStream.raw_str(), sStream.GetLength());
208 pContentsArray->AddReference(pDocument, pContentsStream->GetObjNum());
tsepez70c4afd2016-11-15 11:33:44 -0800209 pPage->SetReferenceFor("Contents", pDocument, pContentsArray);
tsepez381fc832016-10-10 14:06:44 -0700210 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700211 if (!key.IsEmpty()) {
tsepezbb577af2016-09-21 19:10:19 -0700212 pContentsArray->AddReference(pDocument,
tsepez381fc832016-10-10 14:06:44 -0700213 NewIndirectContentsStream(key, pDocument));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700214 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700215}
216
Tom Sepez281a9ea2016-02-26 14:24:28 -0800217CFX_Matrix GetMatrix(CFX_FloatRect rcAnnot,
218 CFX_FloatRect rcStream,
Tom Sepez60d909e2015-12-10 15:34:55 -0800219 const CFX_Matrix& matrix) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700220 if (rcStream.IsEmpty())
Tom Sepez60d909e2015-12-10 15:34:55 -0800221 return CFX_Matrix();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700222
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700223 matrix.TransformRect(rcStream);
224 rcStream.Normalize();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700225
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700226 FX_FLOAT a = rcAnnot.Width() / rcStream.Width();
227 FX_FLOAT d = rcAnnot.Height() / rcStream.Height();
Bo Xufdc00a72014-10-28 23:03:33 -0700228
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700229 FX_FLOAT e = rcAnnot.left - rcStream.left * a;
230 FX_FLOAT f = rcAnnot.bottom - rcStream.bottom * d;
Tom Sepez60d909e2015-12-10 15:34:55 -0800231 return CFX_Matrix(a, 0, 0, d, e, f);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700232}
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700233
tsepez381fc832016-10-10 14:06:44 -0700234} // namespace
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700235
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700236DLLEXPORT int STDCALL FPDFPage_Flatten(FPDF_PAGE page, int nFlag) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700237 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
tsepez6173c9d2016-11-09 16:38:03 -0800238 if (!page)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700239 return FLATTEN_FAIL;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700240
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700241 CPDF_Document* pDocument = pPage->m_pDocument;
242 CPDF_Dictionary* pPageDict = pPage->m_pFormDict;
tsepez6173c9d2016-11-09 16:38:03 -0800243 if (!pDocument || !pPageDict)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700244 return FLATTEN_FAIL;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700245
tsepez74b8c6e2016-10-12 09:38:41 -0700246 std::vector<CPDF_Dictionary*> ObjectArray;
tsepez6173c9d2016-11-09 16:38:03 -0800247 std::vector<CFX_FloatRect> RectArray;
248 int iRet =
249 ParserAnnots(pDocument, pPageDict, &RectArray, &ObjectArray, nFlag);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700250 if (iRet == FLATTEN_NOTHINGTODO || iRet == FLATTEN_FAIL)
251 return iRet;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700252
Tom Sepez281a9ea2016-02-26 14:24:28 -0800253 CFX_FloatRect rcOriginalCB;
254 CFX_FloatRect rcMerger = CalculateRect(&RectArray);
dsinclair38fd8442016-09-15 10:15:32 -0700255 CFX_FloatRect rcOriginalMB = pPageDict->GetRectFor("MediaBox");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700256 if (pPageDict->KeyExist("CropBox"))
dsinclair38fd8442016-09-15 10:15:32 -0700257 rcOriginalMB = pPageDict->GetRectFor("CropBox");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700258
tsepez6173c9d2016-11-09 16:38:03 -0800259 if (rcOriginalMB.IsEmpty())
Tom Sepez281a9ea2016-02-26 14:24:28 -0800260 rcOriginalMB = CFX_FloatRect(0.0f, 0.0f, 612.0f, 792.0f);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700261
tsepez6173c9d2016-11-09 16:38:03 -0800262 rcMerger.left = std::max(rcMerger.left, rcOriginalMB.left);
263 rcMerger.right = std::min(rcMerger.right, rcOriginalMB.right);
264 rcMerger.bottom = std::max(rcMerger.bottom, rcOriginalMB.bottom);
265 rcMerger.top = std::min(rcMerger.top, rcOriginalMB.top);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700266 if (pPageDict->KeyExist("ArtBox"))
dsinclair38fd8442016-09-15 10:15:32 -0700267 rcOriginalCB = pPageDict->GetRectFor("ArtBox");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700268 else
269 rcOriginalCB = rcOriginalMB;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700270
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700271 if (!rcOriginalMB.IsEmpty()) {
Tom Sepezae51c812015-08-05 12:34:06 -0700272 CPDF_Array* pMediaBox = new CPDF_Array();
Tom Sepezae51c812015-08-05 12:34:06 -0700273 pMediaBox->Add(new CPDF_Number(rcOriginalMB.left));
274 pMediaBox->Add(new CPDF_Number(rcOriginalMB.bottom));
275 pMediaBox->Add(new CPDF_Number(rcOriginalMB.right));
276 pMediaBox->Add(new CPDF_Number(rcOriginalMB.top));
dsinclair38fd8442016-09-15 10:15:32 -0700277 pPageDict->SetFor("MediaBox", pMediaBox);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700278 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700279
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700280 if (!rcOriginalCB.IsEmpty()) {
Tom Sepezae51c812015-08-05 12:34:06 -0700281 CPDF_Array* pCropBox = new CPDF_Array();
282 pCropBox->Add(new CPDF_Number(rcOriginalCB.left));
283 pCropBox->Add(new CPDF_Number(rcOriginalCB.bottom));
284 pCropBox->Add(new CPDF_Number(rcOriginalCB.right));
285 pCropBox->Add(new CPDF_Number(rcOriginalCB.top));
dsinclair38fd8442016-09-15 10:15:32 -0700286 pPageDict->SetFor("ArtBox", pCropBox);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700287 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700288
dsinclair38fd8442016-09-15 10:15:32 -0700289 CPDF_Dictionary* pRes = pPageDict->GetDictFor("Resources");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700290 if (!pRes) {
tsepez698c5712016-09-28 16:47:07 -0700291 pRes = new CPDF_Dictionary(pDocument->GetByteStringPool());
dsinclair38fd8442016-09-15 10:15:32 -0700292 pPageDict->SetFor("Resources", pRes);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700293 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700294
tsepez70c4afd2016-11-15 11:33:44 -0800295 CPDF_Stream* pNewXObject = pDocument->NewIndirect<CPDF_Stream>(
tsepez698c5712016-09-28 16:47:07 -0700296 nullptr, 0, new CPDF_Dictionary(pDocument->GetByteStringPool()));
297
tsepez70c4afd2016-11-15 11:33:44 -0800298 uint32_t dwObjNum = pNewXObject->GetObjNum();
dsinclair38fd8442016-09-15 10:15:32 -0700299 CPDF_Dictionary* pPageXObject = pRes->GetDictFor("XObject");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700300 if (!pPageXObject) {
tsepez698c5712016-09-28 16:47:07 -0700301 pPageXObject = new CPDF_Dictionary(pDocument->GetByteStringPool());
dsinclair38fd8442016-09-15 10:15:32 -0700302 pRes->SetFor("XObject", pPageXObject);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700303 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700304
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700305 CFX_ByteString key = "";
tsepez74b8c6e2016-10-12 09:38:41 -0700306 int nStreams = pdfium::CollectionSize<int>(ObjectArray);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700307 if (nStreams > 0) {
308 for (int iKey = 0; /*iKey < 100*/; iKey++) {
309 char sExtend[5] = {};
310 FXSYS_itoa(iKey, sExtend, 10);
311 key = CFX_ByteString("FFT") + CFX_ByteString(sExtend);
tsepez7b1ccf92016-04-14 11:04:57 -0700312 if (!pPageXObject->KeyExist(key))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700313 break;
314 }
315 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700316
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700317 SetPageContents(key, pPageDict, pDocument);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700318
thestig1cd352e2016-06-07 17:53:06 -0700319 CPDF_Dictionary* pNewXORes = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700320 if (!key.IsEmpty()) {
dsinclair38fd8442016-09-15 10:15:32 -0700321 pPageXObject->SetReferenceFor(key, pDocument, dwObjNum);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700322 CPDF_Dictionary* pNewOXbjectDic = pNewXObject->GetDict();
tsepez698c5712016-09-28 16:47:07 -0700323 pNewXORes = new CPDF_Dictionary(pDocument->GetByteStringPool());
dsinclair38fd8442016-09-15 10:15:32 -0700324 pNewOXbjectDic->SetFor("Resources", pNewXORes);
325 pNewOXbjectDic->SetNameFor("Type", "XObject");
326 pNewOXbjectDic->SetNameFor("Subtype", "Form");
327 pNewOXbjectDic->SetIntegerFor("FormType", 1);
328 pNewOXbjectDic->SetNameFor("Name", "FRM");
329 CFX_FloatRect rcBBox = pPageDict->GetRectFor("ArtBox");
330 pNewOXbjectDic->SetRectFor("BBox", rcBBox);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700331 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700332
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700333 for (int i = 0; i < nStreams; i++) {
tsepez74b8c6e2016-10-12 09:38:41 -0700334 CPDF_Dictionary* pAnnotDic = ObjectArray[i];
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700335 if (!pAnnotDic)
336 continue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700337
dsinclair38fd8442016-09-15 10:15:32 -0700338 CFX_FloatRect rcAnnot = pAnnotDic->GetRectFor("Rect");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700339 rcAnnot.Normalize();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700340
dsinclair38fd8442016-09-15 10:15:32 -0700341 CFX_ByteString sAnnotState = pAnnotDic->GetStringFor("AS");
342 CPDF_Dictionary* pAnnotAP = pAnnotDic->GetDictFor("AP");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700343 if (!pAnnotAP)
344 continue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700345
dsinclair38fd8442016-09-15 10:15:32 -0700346 CPDF_Stream* pAPStream = pAnnotAP->GetStreamFor("N");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700347 if (!pAPStream) {
dsinclair38fd8442016-09-15 10:15:32 -0700348 CPDF_Dictionary* pAPDic = pAnnotAP->GetDictFor("N");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700349 if (!pAPDic)
350 continue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700351
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700352 if (!sAnnotState.IsEmpty()) {
dsinclair38fd8442016-09-15 10:15:32 -0700353 pAPStream = pAPDic->GetStreamFor(sAnnotState);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700354 } else {
Oliver Chang3f1c71f2016-01-11 08:45:31 -0800355 auto it = pAPDic->begin();
356 if (it != pAPDic->end()) {
tsepezd5bd8a12016-10-17 11:13:54 -0700357 CPDF_Object* pFirstObj = it->second;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700358 if (pFirstObj) {
Dan Sinclairbf81c142015-10-26 16:54:39 -0400359 if (pFirstObj->IsReference())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700360 pFirstObj = pFirstObj->GetDirect();
Dan Sinclairaa435ba2015-10-22 16:45:48 -0400361 if (!pFirstObj->IsStream())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700362 continue;
Dan Sinclairaa435ba2015-10-22 16:45:48 -0400363 pAPStream = pFirstObj->AsStream();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700364 }
365 }
366 }
367 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700368 if (!pAPStream)
369 continue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700370
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700371 CPDF_Dictionary* pAPDic = pAPStream->GetDict();
Tom Sepez281a9ea2016-02-26 14:24:28 -0800372 CFX_FloatRect rcStream;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700373 if (pAPDic->KeyExist("Rect"))
dsinclair38fd8442016-09-15 10:15:32 -0700374 rcStream = pAPDic->GetRectFor("Rect");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700375 else if (pAPDic->KeyExist("BBox"))
dsinclair38fd8442016-09-15 10:15:32 -0700376 rcStream = pAPDic->GetRectFor("BBox");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700377
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700378 if (rcStream.IsEmpty())
379 continue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700380
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700381 CPDF_Object* pObj = pAPStream;
tsepezd0ecd892016-11-08 17:30:04 -0800382 if (pObj->IsInline()) {
tsepez335cf092016-11-09 13:28:26 -0800383 std::unique_ptr<CPDF_Object> pNew = pObj->Clone();
384 pObj = pNew.get();
tsepez70c4afd2016-11-15 11:33:44 -0800385 pDocument->AddIndirectObject(std::move(pNew));
tsepezd0ecd892016-11-08 17:30:04 -0800386 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700387
tsepezd0ecd892016-11-08 17:30:04 -0800388 CPDF_Dictionary* pObjDic = pObj->GetDict();
389 if (pObjDic) {
390 pObjDic->SetNameFor("Type", "XObject");
391 pObjDic->SetNameFor("Subtype", "Form");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700392 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700393
dsinclair38fd8442016-09-15 10:15:32 -0700394 CPDF_Dictionary* pXObject = pNewXORes->GetDictFor("XObject");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700395 if (!pXObject) {
tsepez698c5712016-09-28 16:47:07 -0700396 pXObject = new CPDF_Dictionary(pDocument->GetByteStringPool());
dsinclair38fd8442016-09-15 10:15:32 -0700397 pNewXORes->SetFor("XObject", pXObject);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700398 }
399
400 CFX_ByteString sFormName;
401 sFormName.Format("F%d", i);
tsepezd0ecd892016-11-08 17:30:04 -0800402 pXObject->SetReferenceFor(sFormName, pDocument, pObj->GetObjNum());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700403
404 CPDF_StreamAcc acc;
405 acc.LoadAllData(pNewXObject);
406
407 const uint8_t* pData = acc.GetData();
408 CFX_ByteString sStream(pData, acc.GetSize());
tsepezd0ecd892016-11-08 17:30:04 -0800409 CFX_Matrix matrix = pAPDic->GetMatrixFor("Matrix");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700410 if (matrix.IsIdentity()) {
411 matrix.a = 1.0f;
412 matrix.b = 0.0f;
413 matrix.c = 0.0f;
414 matrix.d = 1.0f;
415 matrix.e = 0.0f;
416 matrix.f = 0.0f;
417 }
418
tsepezbb577af2016-09-21 19:10:19 -0700419 CFX_ByteString sTemp;
Tom Sepez60d909e2015-12-10 15:34:55 -0800420 CFX_Matrix m = GetMatrix(rcAnnot, rcStream, matrix);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700421 sTemp.Format("q %f 0 0 %f %f %f cm /%s Do Q\n", m.a, m.d, m.e, m.f,
422 sFormName.c_str());
423 sStream += sTemp;
tsepeze6db16e2016-09-19 10:45:09 -0700424 pNewXObject->SetData(sStream.raw_str(), sStream.GetLength());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700425 }
dsinclair38fd8442016-09-15 10:15:32 -0700426 pPageDict->RemoveFor("Annots");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700427 return FLATTEN_SUCCESS;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700428}