blob: 1b93bb4d882b63ebb282925917af04112f94b3c9 [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) {
Tom Sepezc25a4212016-10-14 17:45:56 -0700175 CPDF_Stream* pNewContents = new 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());
Tom Sepezc25a4212016-10-14 17:45:56 -0700180 return pDocument->AddIndirectObject(pNewContents);
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) {
Tom Sepezc25a4212016-10-14 17:45:56 -0700200 pContentsArray = new 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());
Tom Sepezc25a4212016-10-14 17:45:56 -0700209 pPage->SetReferenceFor("Contents", pDocument,
210 pDocument->AddIndirectObject(pContentsArray));
tsepez381fc832016-10-10 14:06:44 -0700211 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700212 if (!key.IsEmpty()) {
tsepezbb577af2016-09-21 19:10:19 -0700213 pContentsArray->AddReference(pDocument,
tsepez381fc832016-10-10 14:06:44 -0700214 NewIndirectContentsStream(key, pDocument));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700215 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700216}
217
Tom Sepez281a9ea2016-02-26 14:24:28 -0800218CFX_Matrix GetMatrix(CFX_FloatRect rcAnnot,
219 CFX_FloatRect rcStream,
Tom Sepez60d909e2015-12-10 15:34:55 -0800220 const CFX_Matrix& matrix) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700221 if (rcStream.IsEmpty())
Tom Sepez60d909e2015-12-10 15:34:55 -0800222 return CFX_Matrix();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700223
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700224 matrix.TransformRect(rcStream);
225 rcStream.Normalize();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700226
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700227 FX_FLOAT a = rcAnnot.Width() / rcStream.Width();
228 FX_FLOAT d = rcAnnot.Height() / rcStream.Height();
Bo Xufdc00a72014-10-28 23:03:33 -0700229
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700230 FX_FLOAT e = rcAnnot.left - rcStream.left * a;
231 FX_FLOAT f = rcAnnot.bottom - rcStream.bottom * d;
Tom Sepez60d909e2015-12-10 15:34:55 -0800232 return CFX_Matrix(a, 0, 0, d, e, f);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700233}
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700234
tsepez381fc832016-10-10 14:06:44 -0700235} // namespace
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700236
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700237DLLEXPORT int STDCALL FPDFPage_Flatten(FPDF_PAGE page, int nFlag) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700238 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
tsepez6173c9d2016-11-09 16:38:03 -0800239 if (!page)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700240 return FLATTEN_FAIL;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700241
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700242 CPDF_Document* pDocument = pPage->m_pDocument;
243 CPDF_Dictionary* pPageDict = pPage->m_pFormDict;
tsepez6173c9d2016-11-09 16:38:03 -0800244 if (!pDocument || !pPageDict)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700245 return FLATTEN_FAIL;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700246
tsepez74b8c6e2016-10-12 09:38:41 -0700247 std::vector<CPDF_Dictionary*> ObjectArray;
tsepez6173c9d2016-11-09 16:38:03 -0800248 std::vector<CFX_FloatRect> RectArray;
249 int iRet =
250 ParserAnnots(pDocument, pPageDict, &RectArray, &ObjectArray, nFlag);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700251 if (iRet == FLATTEN_NOTHINGTODO || iRet == FLATTEN_FAIL)
252 return iRet;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700253
Tom Sepez281a9ea2016-02-26 14:24:28 -0800254 CFX_FloatRect rcOriginalCB;
255 CFX_FloatRect rcMerger = CalculateRect(&RectArray);
dsinclair38fd8442016-09-15 10:15:32 -0700256 CFX_FloatRect rcOriginalMB = pPageDict->GetRectFor("MediaBox");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700257 if (pPageDict->KeyExist("CropBox"))
dsinclair38fd8442016-09-15 10:15:32 -0700258 rcOriginalMB = pPageDict->GetRectFor("CropBox");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700259
tsepez6173c9d2016-11-09 16:38:03 -0800260 if (rcOriginalMB.IsEmpty())
Tom Sepez281a9ea2016-02-26 14:24:28 -0800261 rcOriginalMB = CFX_FloatRect(0.0f, 0.0f, 612.0f, 792.0f);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700262
tsepez6173c9d2016-11-09 16:38:03 -0800263 rcMerger.left = std::max(rcMerger.left, rcOriginalMB.left);
264 rcMerger.right = std::min(rcMerger.right, rcOriginalMB.right);
265 rcMerger.bottom = std::max(rcMerger.bottom, rcOriginalMB.bottom);
266 rcMerger.top = std::min(rcMerger.top, rcOriginalMB.top);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700267 if (pPageDict->KeyExist("ArtBox"))
dsinclair38fd8442016-09-15 10:15:32 -0700268 rcOriginalCB = pPageDict->GetRectFor("ArtBox");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700269 else
270 rcOriginalCB = rcOriginalMB;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700271
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700272 if (!rcOriginalMB.IsEmpty()) {
Tom Sepezae51c812015-08-05 12:34:06 -0700273 CPDF_Array* pMediaBox = new CPDF_Array();
Tom Sepezae51c812015-08-05 12:34:06 -0700274 pMediaBox->Add(new CPDF_Number(rcOriginalMB.left));
275 pMediaBox->Add(new CPDF_Number(rcOriginalMB.bottom));
276 pMediaBox->Add(new CPDF_Number(rcOriginalMB.right));
277 pMediaBox->Add(new CPDF_Number(rcOriginalMB.top));
dsinclair38fd8442016-09-15 10:15:32 -0700278 pPageDict->SetFor("MediaBox", pMediaBox);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700279 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700280
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700281 if (!rcOriginalCB.IsEmpty()) {
Tom Sepezae51c812015-08-05 12:34:06 -0700282 CPDF_Array* pCropBox = new CPDF_Array();
283 pCropBox->Add(new CPDF_Number(rcOriginalCB.left));
284 pCropBox->Add(new CPDF_Number(rcOriginalCB.bottom));
285 pCropBox->Add(new CPDF_Number(rcOriginalCB.right));
286 pCropBox->Add(new CPDF_Number(rcOriginalCB.top));
dsinclair38fd8442016-09-15 10:15:32 -0700287 pPageDict->SetFor("ArtBox", pCropBox);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700288 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700289
dsinclair38fd8442016-09-15 10:15:32 -0700290 CPDF_Dictionary* pRes = pPageDict->GetDictFor("Resources");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700291 if (!pRes) {
tsepez698c5712016-09-28 16:47:07 -0700292 pRes = new CPDF_Dictionary(pDocument->GetByteStringPool());
dsinclair38fd8442016-09-15 10:15:32 -0700293 pPageDict->SetFor("Resources", pRes);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700294 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700295
Tom Sepezc25a4212016-10-14 17:45:56 -0700296 CPDF_Stream* pNewXObject = new CPDF_Stream(
tsepez698c5712016-09-28 16:47:07 -0700297 nullptr, 0, new CPDF_Dictionary(pDocument->GetByteStringPool()));
298
Tom Sepezc25a4212016-10-14 17:45:56 -0700299 uint32_t dwObjNum = pDocument->AddIndirectObject(pNewXObject);
dsinclair38fd8442016-09-15 10:15:32 -0700300 CPDF_Dictionary* pPageXObject = pRes->GetDictFor("XObject");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700301 if (!pPageXObject) {
tsepez698c5712016-09-28 16:47:07 -0700302 pPageXObject = new CPDF_Dictionary(pDocument->GetByteStringPool());
dsinclair38fd8442016-09-15 10:15:32 -0700303 pRes->SetFor("XObject", pPageXObject);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700304 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700305
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700306 CFX_ByteString key = "";
tsepez74b8c6e2016-10-12 09:38:41 -0700307 int nStreams = pdfium::CollectionSize<int>(ObjectArray);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700308 if (nStreams > 0) {
309 for (int iKey = 0; /*iKey < 100*/; iKey++) {
310 char sExtend[5] = {};
311 FXSYS_itoa(iKey, sExtend, 10);
312 key = CFX_ByteString("FFT") + CFX_ByteString(sExtend);
tsepez7b1ccf92016-04-14 11:04:57 -0700313 if (!pPageXObject->KeyExist(key))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700314 break;
315 }
316 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700317
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700318 SetPageContents(key, pPageDict, pDocument);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700319
thestig1cd352e2016-06-07 17:53:06 -0700320 CPDF_Dictionary* pNewXORes = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700321 if (!key.IsEmpty()) {
dsinclair38fd8442016-09-15 10:15:32 -0700322 pPageXObject->SetReferenceFor(key, pDocument, dwObjNum);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700323 CPDF_Dictionary* pNewOXbjectDic = pNewXObject->GetDict();
tsepez698c5712016-09-28 16:47:07 -0700324 pNewXORes = new CPDF_Dictionary(pDocument->GetByteStringPool());
dsinclair38fd8442016-09-15 10:15:32 -0700325 pNewOXbjectDic->SetFor("Resources", pNewXORes);
326 pNewOXbjectDic->SetNameFor("Type", "XObject");
327 pNewOXbjectDic->SetNameFor("Subtype", "Form");
328 pNewOXbjectDic->SetIntegerFor("FormType", 1);
329 pNewOXbjectDic->SetNameFor("Name", "FRM");
330 CFX_FloatRect rcBBox = pPageDict->GetRectFor("ArtBox");
331 pNewOXbjectDic->SetRectFor("BBox", rcBBox);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700332 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700333
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700334 for (int i = 0; i < nStreams; i++) {
tsepez74b8c6e2016-10-12 09:38:41 -0700335 CPDF_Dictionary* pAnnotDic = ObjectArray[i];
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700336 if (!pAnnotDic)
337 continue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700338
dsinclair38fd8442016-09-15 10:15:32 -0700339 CFX_FloatRect rcAnnot = pAnnotDic->GetRectFor("Rect");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700340 rcAnnot.Normalize();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700341
dsinclair38fd8442016-09-15 10:15:32 -0700342 CFX_ByteString sAnnotState = pAnnotDic->GetStringFor("AS");
343 CPDF_Dictionary* pAnnotAP = pAnnotDic->GetDictFor("AP");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700344 if (!pAnnotAP)
345 continue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700346
dsinclair38fd8442016-09-15 10:15:32 -0700347 CPDF_Stream* pAPStream = pAnnotAP->GetStreamFor("N");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700348 if (!pAPStream) {
dsinclair38fd8442016-09-15 10:15:32 -0700349 CPDF_Dictionary* pAPDic = pAnnotAP->GetDictFor("N");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700350 if (!pAPDic)
351 continue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700352
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700353 if (!sAnnotState.IsEmpty()) {
dsinclair38fd8442016-09-15 10:15:32 -0700354 pAPStream = pAPDic->GetStreamFor(sAnnotState);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700355 } else {
Oliver Chang3f1c71f2016-01-11 08:45:31 -0800356 auto it = pAPDic->begin();
357 if (it != pAPDic->end()) {
tsepezd5bd8a12016-10-17 11:13:54 -0700358 CPDF_Object* pFirstObj = it->second;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700359 if (pFirstObj) {
Dan Sinclairbf81c142015-10-26 16:54:39 -0400360 if (pFirstObj->IsReference())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700361 pFirstObj = pFirstObj->GetDirect();
Dan Sinclairaa435ba2015-10-22 16:45:48 -0400362 if (!pFirstObj->IsStream())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700363 continue;
Dan Sinclairaa435ba2015-10-22 16:45:48 -0400364 pAPStream = pFirstObj->AsStream();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700365 }
366 }
367 }
368 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700369 if (!pAPStream)
370 continue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700371
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700372 CPDF_Dictionary* pAPDic = pAPStream->GetDict();
Tom Sepez281a9ea2016-02-26 14:24:28 -0800373 CFX_FloatRect rcStream;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700374 if (pAPDic->KeyExist("Rect"))
dsinclair38fd8442016-09-15 10:15:32 -0700375 rcStream = pAPDic->GetRectFor("Rect");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700376 else if (pAPDic->KeyExist("BBox"))
dsinclair38fd8442016-09-15 10:15:32 -0700377 rcStream = pAPDic->GetRectFor("BBox");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700378
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700379 if (rcStream.IsEmpty())
380 continue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700381
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700382 CPDF_Object* pObj = pAPStream;
tsepezd0ecd892016-11-08 17:30:04 -0800383 if (pObj->IsInline()) {
tsepez335cf092016-11-09 13:28:26 -0800384 std::unique_ptr<CPDF_Object> pNew = pObj->Clone();
385 pObj = pNew.get();
386 pDocument->AddIndirectObject(pNew.release());
tsepezd0ecd892016-11-08 17:30:04 -0800387 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700388
tsepezd0ecd892016-11-08 17:30:04 -0800389 CPDF_Dictionary* pObjDic = pObj->GetDict();
390 if (pObjDic) {
391 pObjDic->SetNameFor("Type", "XObject");
392 pObjDic->SetNameFor("Subtype", "Form");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700393 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700394
dsinclair38fd8442016-09-15 10:15:32 -0700395 CPDF_Dictionary* pXObject = pNewXORes->GetDictFor("XObject");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700396 if (!pXObject) {
tsepez698c5712016-09-28 16:47:07 -0700397 pXObject = new CPDF_Dictionary(pDocument->GetByteStringPool());
dsinclair38fd8442016-09-15 10:15:32 -0700398 pNewXORes->SetFor("XObject", pXObject);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700399 }
400
401 CFX_ByteString sFormName;
402 sFormName.Format("F%d", i);
tsepezd0ecd892016-11-08 17:30:04 -0800403 pXObject->SetReferenceFor(sFormName, pDocument, pObj->GetObjNum());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700404
405 CPDF_StreamAcc acc;
406 acc.LoadAllData(pNewXObject);
407
408 const uint8_t* pData = acc.GetData();
409 CFX_ByteString sStream(pData, acc.GetSize());
tsepezd0ecd892016-11-08 17:30:04 -0800410 CFX_Matrix matrix = pAPDic->GetMatrixFor("Matrix");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700411 if (matrix.IsIdentity()) {
412 matrix.a = 1.0f;
413 matrix.b = 0.0f;
414 matrix.c = 0.0f;
415 matrix.d = 1.0f;
416 matrix.e = 0.0f;
417 matrix.f = 0.0f;
418 }
419
tsepezbb577af2016-09-21 19:10:19 -0700420 CFX_ByteString sTemp;
Tom Sepez60d909e2015-12-10 15:34:55 -0800421 CFX_Matrix m = GetMatrix(rcAnnot, rcStream, matrix);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700422 sTemp.Format("q %f 0 0 %f %f %f cm /%s Do Q\n", m.a, m.d, m.e, m.f,
423 sFormName.c_str());
424 sStream += sTemp;
tsepeze6db16e2016-09-19 10:45:09 -0700425 pNewXObject->SetData(sStream.raw_str(), sStream.GetLength());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700426 }
dsinclair38fd8442016-09-15 10:15:32 -0700427 pPageDict->RemoveFor("Annots");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700428 return FLATTEN_SUCCESS;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700429}