blob: 914008c1a32e5de95c8ab601f8f56f7ce1a66d0a [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>
tsepez0e606b52016-11-18 16:22:41 -080010#include <memory>
11#include <utility>
12#include <vector>
Lei Zhang375a8642016-01-11 11:59:17 -080013
dsinclair41872fa2016-10-04 11:29:35 -070014#include "core/fpdfapi/page/cpdf_page.h"
15#include "core/fpdfapi/page/cpdf_pageobject.h"
dsinclair488b7ad2016-10-04 11:55:50 -070016#include "core/fpdfapi/parser/cpdf_array.h"
17#include "core/fpdfapi/parser/cpdf_document.h"
tsepez0e606b52016-11-18 16:22:41 -080018#include "core/fpdfapi/parser/cpdf_name.h"
dsinclair488b7ad2016-10-04 11:55:50 -070019#include "core/fpdfapi/parser/cpdf_number.h"
tsepez8a3aa452016-11-16 12:26:06 -080020#include "core/fpdfapi/parser/cpdf_reference.h"
dsinclair488b7ad2016-10-04 11:55:50 -070021#include "core/fpdfapi/parser/cpdf_stream.h"
22#include "core/fpdfapi/parser/cpdf_stream_acc.h"
dsinclair1727aee2016-09-29 13:12:56 -070023#include "core/fpdfdoc/cpdf_annot.h"
dsinclair114e46a2016-09-29 17:18:21 -070024#include "fpdfsdk/fsdk_define.h"
tsepez74b8c6e2016-10-12 09:38:41 -070025#include "third_party/base/stl_util.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070026
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070027enum FPDF_TYPE { MAX, MIN };
28enum FPDF_VALUE { TOP, LEFT, RIGHT, BOTTOM };
29
tsepez381fc832016-10-10 14:06:44 -070030namespace {
31
tsepez4cf55152016-11-02 14:37:54 -070032bool IsValiableRect(CFX_FloatRect rect, CFX_FloatRect rcPage) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070033 if (rect.left - rect.right > 0.000001f || rect.bottom - rect.top > 0.000001f)
tsepez4cf55152016-11-02 14:37:54 -070034 return false;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070035
Nico Weber9d8ec5a2015-08-04 13:00:21 -070036 if (rect.left == 0.0f && rect.top == 0.0f && rect.right == 0.0f &&
37 rect.bottom == 0.0f)
tsepez4cf55152016-11-02 14:37:54 -070038 return false;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070039
Nico Weber9d8ec5a2015-08-04 13:00:21 -070040 if (!rcPage.IsEmpty()) {
41 if (rect.left - rcPage.left < -10.000001f ||
42 rect.right - rcPage.right > 10.000001f ||
43 rect.top - rcPage.top > 10.000001f ||
44 rect.bottom - rcPage.bottom < -10.000001f)
tsepez4cf55152016-11-02 14:37:54 -070045 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070046 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070047
tsepez4cf55152016-11-02 14:37:54 -070048 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070049}
50
Tom Sepez2398d892016-02-17 16:46:26 -080051void GetContentsRect(CPDF_Document* pDoc,
52 CPDF_Dictionary* pDict,
tsepez6173c9d2016-11-09 16:38:03 -080053 std::vector<CFX_FloatRect>* pRectArray) {
Dan Sinclair0bb13332017-03-30 16:12:02 -040054 auto pPDFPage = pdfium::MakeUnique<CPDF_Page>(pDoc, pDict, false);
thestig5cc24652016-04-26 11:46:02 -070055 pPDFPage->ParseContent();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070056
thestig2fad11a2016-06-16 16:39:25 -070057 for (const auto& pPageObject : *pPDFPage->GetPageObjectList()) {
Tom Sepez281a9ea2016-02-26 14:24:28 -080058 CFX_FloatRect rc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070059 rc.left = pPageObject->m_Left;
60 rc.right = pPageObject->m_Right;
61 rc.bottom = pPageObject->m_Bottom;
62 rc.top = pPageObject->m_Top;
dsinclair38fd8442016-09-15 10:15:32 -070063 if (IsValiableRect(rc, pDict->GetRectFor("MediaBox")))
tsepez6173c9d2016-11-09 16:38:03 -080064 pRectArray->push_back(rc);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070065 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070066}
67
Nico Weber9d8ec5a2015-08-04 13:00:21 -070068void ParserStream(CPDF_Dictionary* pPageDic,
69 CPDF_Dictionary* pStream,
tsepez6173c9d2016-11-09 16:38:03 -080070 std::vector<CFX_FloatRect>* pRectArray,
tsepez74b8c6e2016-10-12 09:38:41 -070071 std::vector<CPDF_Dictionary*>* pObjectArray) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070072 if (!pStream)
73 return;
Tom Sepez281a9ea2016-02-26 14:24:28 -080074 CFX_FloatRect rect;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070075 if (pStream->KeyExist("Rect"))
dsinclair38fd8442016-09-15 10:15:32 -070076 rect = pStream->GetRectFor("Rect");
Nico Weber9d8ec5a2015-08-04 13:00:21 -070077 else if (pStream->KeyExist("BBox"))
dsinclair38fd8442016-09-15 10:15:32 -070078 rect = pStream->GetRectFor("BBox");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070079
dsinclair38fd8442016-09-15 10:15:32 -070080 if (IsValiableRect(rect, pPageDic->GetRectFor("MediaBox")))
tsepez6173c9d2016-11-09 16:38:03 -080081 pRectArray->push_back(rect);
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070082
tsepez74b8c6e2016-10-12 09:38:41 -070083 pObjectArray->push_back(pStream);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070084}
85
Nico Weber9d8ec5a2015-08-04 13:00:21 -070086int ParserAnnots(CPDF_Document* pSourceDoc,
87 CPDF_Dictionary* pPageDic,
tsepez6173c9d2016-11-09 16:38:03 -080088 std::vector<CFX_FloatRect>* pRectArray,
tsepez74b8c6e2016-10-12 09:38:41 -070089 std::vector<CPDF_Dictionary*>* pObjectArray,
Nico Weber9d8ec5a2015-08-04 13:00:21 -070090 int nUsage) {
91 if (!pSourceDoc || !pPageDic)
92 return FLATTEN_FAIL;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070093
Nico Weber9d8ec5a2015-08-04 13:00:21 -070094 GetContentsRect(pSourceDoc, pPageDic, pRectArray);
dsinclair38fd8442016-09-15 10:15:32 -070095 CPDF_Array* pAnnots = pPageDic->GetArrayFor("Annots");
Nico Weber9d8ec5a2015-08-04 13:00:21 -070096 if (!pAnnots)
97 return FLATTEN_NOTHINGTODO;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070098
tsepezc3255f52016-03-25 14:52:27 -070099 uint32_t dwSize = pAnnots->GetCount();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700100 for (int i = 0; i < (int)dwSize; i++) {
tsepezbd567552016-03-29 14:51:50 -0700101 CPDF_Dictionary* pAnnotDic = ToDictionary(pAnnots->GetDirectObjectAt(i));
Dan Sinclairf1251c12015-10-20 16:24:45 -0400102 if (!pAnnotDic)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700103 continue;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700104
dsinclair38fd8442016-09-15 10:15:32 -0700105 CFX_ByteString sSubtype = pAnnotDic->GetStringFor("Subtype");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700106 if (sSubtype == "Popup")
107 continue;
108
dsinclair38fd8442016-09-15 10:15:32 -0700109 int nAnnotFlag = pAnnotDic->GetIntegerFor("F");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700110 if (nAnnotFlag & ANNOTFLAG_HIDDEN)
111 continue;
112
113 if (nUsage == FLAT_NORMALDISPLAY) {
114 if (nAnnotFlag & ANNOTFLAG_INVISIBLE)
115 continue;
116
117 ParserStream(pPageDic, pAnnotDic, pRectArray, pObjectArray);
118 } else {
119 if (nAnnotFlag & ANNOTFLAG_PRINT)
120 ParserStream(pPageDic, pAnnotDic, pRectArray, pObjectArray);
121 }
122 }
123 return FLATTEN_SUCCESS;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700124}
125
Dan Sinclair05df0752017-03-14 14:43:42 -0400126float GetMinMaxValue(const std::vector<CFX_FloatRect>& array,
127 FPDF_TYPE type,
128 FPDF_VALUE value) {
tsepez6173c9d2016-11-09 16:38:03 -0800129 size_t nRects = array.size();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700130 if (nRects <= 0)
131 return 0.0f;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700132
Dan Sinclair05df0752017-03-14 14:43:42 -0400133 std::vector<float> pArray(nRects);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700134 switch (value) {
tsepez6173c9d2016-11-09 16:38:03 -0800135 case LEFT:
136 for (size_t i = 0; i < nRects; i++)
137 pArray[i] = array[i].left;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700138 break;
tsepez6173c9d2016-11-09 16:38:03 -0800139 case TOP:
140 for (size_t i = 0; i < nRects; i++)
141 pArray[i] = array[i].top;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700142 break;
tsepez6173c9d2016-11-09 16:38:03 -0800143 case RIGHT:
144 for (size_t i = 0; i < nRects; i++)
145 pArray[i] = array[i].right;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700146 break;
tsepez6173c9d2016-11-09 16:38:03 -0800147 case BOTTOM:
148 for (size_t i = 0; i < nRects; i++)
149 pArray[i] = array[i].bottom;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700150 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700151 default:
weili12367cb2016-06-03 11:22:16 -0700152 // Not reachable.
153 return 0.0f;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700154 }
tsepez6173c9d2016-11-09 16:38:03 -0800155
Dan Sinclair05df0752017-03-14 14:43:42 -0400156 float fRet = pArray[0];
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700157 if (type == MAX) {
tsepez6173c9d2016-11-09 16:38:03 -0800158 for (size_t i = 1; i < nRects; i++)
159 fRet = std::max(fRet, pArray[i]);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700160 } else {
tsepez6173c9d2016-11-09 16:38:03 -0800161 for (size_t i = 1; i < nRects; i++)
162 fRet = std::min(fRet, pArray[i]);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700163 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700164 return fRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700165}
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700166
tsepez6173c9d2016-11-09 16:38:03 -0800167CFX_FloatRect CalculateRect(std::vector<CFX_FloatRect>* pRectArray) {
Tom Sepez281a9ea2016-02-26 14:24:28 -0800168 CFX_FloatRect rcRet;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700169
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700170 rcRet.left = GetMinMaxValue(*pRectArray, MIN, LEFT);
171 rcRet.top = GetMinMaxValue(*pRectArray, MAX, TOP);
172 rcRet.right = GetMinMaxValue(*pRectArray, MAX, RIGHT);
173 rcRet.bottom = GetMinMaxValue(*pRectArray, MIN, BOTTOM);
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700174
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700175 return rcRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700176}
177
tsepez381fc832016-10-10 14:06:44 -0700178uint32_t NewIndirectContentsStream(const CFX_ByteString& key,
179 CPDF_Document* pDocument) {
tsepez70c4afd2016-11-15 11:33:44 -0800180 CPDF_Stream* pNewContents = pDocument->NewIndirect<CPDF_Stream>(
tsepez9e05ee12016-11-21 13:19:10 -0800181 nullptr, 0,
182 pdfium::MakeUnique<CPDF_Dictionary>(pDocument->GetByteStringPool()));
tsepez381fc832016-10-10 14:06:44 -0700183 CFX_ByteString sStream;
184 sStream.Format("q 1 0 0 1 0 0 cm /%s Do Q", key.c_str());
185 pNewContents->SetData(sStream.raw_str(), sStream.GetLength());
tsepez70c4afd2016-11-15 11:33:44 -0800186 return pNewContents->GetObjNum();
tsepez381fc832016-10-10 14:06:44 -0700187}
188
189void SetPageContents(const CFX_ByteString& key,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700190 CPDF_Dictionary* pPage,
191 CPDF_Document* pDocument) {
thestig1cd352e2016-06-07 17:53:06 -0700192 CPDF_Array* pContentsArray = nullptr;
tsepez381fc832016-10-10 14:06:44 -0700193 CPDF_Stream* pContentsStream = pPage->GetStreamFor("Contents");
194 if (!pContentsStream) {
195 pContentsArray = pPage->GetArrayFor("Contents");
196 if (!pContentsArray) {
197 if (!key.IsEmpty()) {
tsepez0e606b52016-11-18 16:22:41 -0800198 pPage->SetNewFor<CPDF_Reference>(
199 "Contents", pDocument, NewIndirectContentsStream(key, pDocument));
tsepez381fc832016-10-10 14:06:44 -0700200 }
201 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700202 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700203 }
tsepez381fc832016-10-10 14:06:44 -0700204 pPage->ConvertToIndirectObjectFor("Contents", pDocument);
205 if (!pContentsArray) {
tsepez70c4afd2016-11-15 11:33:44 -0800206 pContentsArray = pDocument->NewIndirect<CPDF_Array>();
Tom Sepezafd0d1f2017-04-04 14:37:18 -0700207 auto pAcc = pdfium::MakeRetain<CPDF_StreamAcc>(pContentsStream);
208 pAcc->LoadAllData();
tsepez381fc832016-10-10 14:06:44 -0700209 CFX_ByteString sStream = "q\n";
Tom Sepezc25a4212016-10-14 17:45:56 -0700210 CFX_ByteString sBody =
Tom Sepezafd0d1f2017-04-04 14:37:18 -0700211 CFX_ByteString((const char*)pAcc->GetData(), pAcc->GetSize());
tsepez381fc832016-10-10 14:06:44 -0700212 sStream = sStream + sBody + "\nQ";
213 pContentsStream->SetData(sStream.raw_str(), sStream.GetLength());
tsepez8a3aa452016-11-16 12:26:06 -0800214 pContentsArray->AddNew<CPDF_Reference>(pDocument,
215 pContentsStream->GetObjNum());
tsepez0e606b52016-11-18 16:22:41 -0800216 pPage->SetNewFor<CPDF_Reference>("Contents", pDocument,
217 pContentsArray->GetObjNum());
tsepez381fc832016-10-10 14:06:44 -0700218 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700219 if (!key.IsEmpty()) {
tsepez8a3aa452016-11-16 12:26:06 -0800220 pContentsArray->AddNew<CPDF_Reference>(
221 pDocument, NewIndirectContentsStream(key, pDocument));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700222 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700223}
224
Tom Sepez281a9ea2016-02-26 14:24:28 -0800225CFX_Matrix GetMatrix(CFX_FloatRect rcAnnot,
226 CFX_FloatRect rcStream,
Tom Sepez60d909e2015-12-10 15:34:55 -0800227 const CFX_Matrix& matrix) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700228 if (rcStream.IsEmpty())
Tom Sepez60d909e2015-12-10 15:34:55 -0800229 return CFX_Matrix();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700230
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700231 matrix.TransformRect(rcStream);
232 rcStream.Normalize();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700233
Dan Sinclair05df0752017-03-14 14:43:42 -0400234 float a = rcAnnot.Width() / rcStream.Width();
235 float d = rcAnnot.Height() / rcStream.Height();
Bo Xufdc00a72014-10-28 23:03:33 -0700236
Dan Sinclair05df0752017-03-14 14:43:42 -0400237 float e = rcAnnot.left - rcStream.left * a;
238 float f = rcAnnot.bottom - rcStream.bottom * d;
Tom Sepez60d909e2015-12-10 15:34:55 -0800239 return CFX_Matrix(a, 0, 0, d, e, f);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700240}
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700241
tsepez381fc832016-10-10 14:06:44 -0700242} // namespace
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700243
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700244DLLEXPORT int STDCALL FPDFPage_Flatten(FPDF_PAGE page, int nFlag) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700245 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
tsepez6173c9d2016-11-09 16:38:03 -0800246 if (!page)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700247 return FLATTEN_FAIL;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700248
Tom Sepez4cb82ee2017-05-22 15:15:30 -0700249 CPDF_Document* pDocument = pPage->m_pDocument.Get();
250 CPDF_Dictionary* pPageDict = pPage->m_pFormDict.Get();
tsepez6173c9d2016-11-09 16:38:03 -0800251 if (!pDocument || !pPageDict)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700252 return FLATTEN_FAIL;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700253
tsepez74b8c6e2016-10-12 09:38:41 -0700254 std::vector<CPDF_Dictionary*> ObjectArray;
tsepez6173c9d2016-11-09 16:38:03 -0800255 std::vector<CFX_FloatRect> RectArray;
256 int iRet =
257 ParserAnnots(pDocument, pPageDict, &RectArray, &ObjectArray, nFlag);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700258 if (iRet == FLATTEN_NOTHINGTODO || iRet == FLATTEN_FAIL)
259 return iRet;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700260
Tom Sepez281a9ea2016-02-26 14:24:28 -0800261 CFX_FloatRect rcOriginalCB;
262 CFX_FloatRect rcMerger = CalculateRect(&RectArray);
dsinclair38fd8442016-09-15 10:15:32 -0700263 CFX_FloatRect rcOriginalMB = pPageDict->GetRectFor("MediaBox");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700264 if (pPageDict->KeyExist("CropBox"))
dsinclair38fd8442016-09-15 10:15:32 -0700265 rcOriginalMB = pPageDict->GetRectFor("CropBox");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700266
tsepez6173c9d2016-11-09 16:38:03 -0800267 if (rcOriginalMB.IsEmpty())
Tom Sepez281a9ea2016-02-26 14:24:28 -0800268 rcOriginalMB = CFX_FloatRect(0.0f, 0.0f, 612.0f, 792.0f);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700269
tsepez6173c9d2016-11-09 16:38:03 -0800270 rcMerger.left = std::max(rcMerger.left, rcOriginalMB.left);
271 rcMerger.right = std::min(rcMerger.right, rcOriginalMB.right);
272 rcMerger.bottom = std::max(rcMerger.bottom, rcOriginalMB.bottom);
273 rcMerger.top = std::min(rcMerger.top, rcOriginalMB.top);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700274 if (pPageDict->KeyExist("ArtBox"))
dsinclair38fd8442016-09-15 10:15:32 -0700275 rcOriginalCB = pPageDict->GetRectFor("ArtBox");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700276 else
277 rcOriginalCB = rcOriginalMB;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700278
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700279 if (!rcOriginalMB.IsEmpty()) {
tsepez0e606b52016-11-18 16:22:41 -0800280 CPDF_Array* pMediaBox = pPageDict->SetNewFor<CPDF_Array>("MediaBox");
tsepez8a3aa452016-11-16 12:26:06 -0800281 pMediaBox->AddNew<CPDF_Number>(rcOriginalMB.left);
282 pMediaBox->AddNew<CPDF_Number>(rcOriginalMB.bottom);
283 pMediaBox->AddNew<CPDF_Number>(rcOriginalMB.right);
284 pMediaBox->AddNew<CPDF_Number>(rcOriginalMB.top);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700285 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700286
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700287 if (!rcOriginalCB.IsEmpty()) {
tsepez0e606b52016-11-18 16:22:41 -0800288 CPDF_Array* pCropBox = pPageDict->SetNewFor<CPDF_Array>("ArtBox");
tsepez8a3aa452016-11-16 12:26:06 -0800289 pCropBox->AddNew<CPDF_Number>(rcOriginalCB.left);
290 pCropBox->AddNew<CPDF_Number>(rcOriginalCB.bottom);
291 pCropBox->AddNew<CPDF_Number>(rcOriginalCB.right);
292 pCropBox->AddNew<CPDF_Number>(rcOriginalCB.top);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700293 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700294
dsinclair38fd8442016-09-15 10:15:32 -0700295 CPDF_Dictionary* pRes = pPageDict->GetDictFor("Resources");
tsepez0e606b52016-11-18 16:22:41 -0800296 if (!pRes)
297 pRes = pPageDict->SetNewFor<CPDF_Dictionary>("Resources");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700298
tsepez70c4afd2016-11-15 11:33:44 -0800299 CPDF_Stream* pNewXObject = pDocument->NewIndirect<CPDF_Stream>(
tsepez9e05ee12016-11-21 13:19:10 -0800300 nullptr, 0,
301 pdfium::MakeUnique<CPDF_Dictionary>(pDocument->GetByteStringPool()));
tsepez698c5712016-09-28 16:47:07 -0700302
tsepez70c4afd2016-11-15 11:33:44 -0800303 uint32_t dwObjNum = pNewXObject->GetObjNum();
dsinclair38fd8442016-09-15 10:15:32 -0700304 CPDF_Dictionary* pPageXObject = pRes->GetDictFor("XObject");
tsepez0e606b52016-11-18 16:22:41 -0800305 if (!pPageXObject)
306 pPageXObject = pRes->SetNewFor<CPDF_Dictionary>("XObject");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700307
Lei Zhangf0f2a2a2017-06-13 22:57:46 -0700308 CFX_ByteString key;
tsepez74b8c6e2016-10-12 09:38:41 -0700309 int nStreams = pdfium::CollectionSize<int>(ObjectArray);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700310 if (nStreams > 0) {
Lei Zhangf0f2a2a2017-06-13 22:57:46 -0700311 CFX_ByteString sKey;
312 int i = 0;
313 while (i < INT_MAX) {
314 sKey.Format("FFT%d", i);
315 if (!pPageXObject->KeyExist(sKey)) {
316 key = sKey;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700317 break;
Lei Zhangf0f2a2a2017-06-13 22:57:46 -0700318 }
319 ++i;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700320 }
321 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700322
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700323 SetPageContents(key, pPageDict, pDocument);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700324
thestig1cd352e2016-06-07 17:53:06 -0700325 CPDF_Dictionary* pNewXORes = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700326 if (!key.IsEmpty()) {
tsepez0e606b52016-11-18 16:22:41 -0800327 pPageXObject->SetNewFor<CPDF_Reference>(key, pDocument, dwObjNum);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700328 CPDF_Dictionary* pNewOXbjectDic = pNewXObject->GetDict();
tsepez0e606b52016-11-18 16:22:41 -0800329 pNewXORes = pNewOXbjectDic->SetNewFor<CPDF_Dictionary>("Resources");
330 pNewOXbjectDic->SetNewFor<CPDF_Name>("Type", "XObject");
331 pNewOXbjectDic->SetNewFor<CPDF_Name>("Subtype", "Form");
332 pNewOXbjectDic->SetNewFor<CPDF_Number>("FormType", 1);
333 pNewOXbjectDic->SetNewFor<CPDF_Name>("Name", "FRM");
dsinclair38fd8442016-09-15 10:15:32 -0700334 CFX_FloatRect rcBBox = pPageDict->GetRectFor("ArtBox");
335 pNewOXbjectDic->SetRectFor("BBox", rcBBox);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700336 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700337
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700338 for (int i = 0; i < nStreams; i++) {
tsepez74b8c6e2016-10-12 09:38:41 -0700339 CPDF_Dictionary* pAnnotDic = ObjectArray[i];
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700340 if (!pAnnotDic)
341 continue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700342
dsinclair38fd8442016-09-15 10:15:32 -0700343 CFX_FloatRect rcAnnot = pAnnotDic->GetRectFor("Rect");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700344 rcAnnot.Normalize();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700345
dsinclair38fd8442016-09-15 10:15:32 -0700346 CFX_ByteString sAnnotState = pAnnotDic->GetStringFor("AS");
347 CPDF_Dictionary* pAnnotAP = pAnnotDic->GetDictFor("AP");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700348 if (!pAnnotAP)
349 continue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700350
dsinclair38fd8442016-09-15 10:15:32 -0700351 CPDF_Stream* pAPStream = pAnnotAP->GetStreamFor("N");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700352 if (!pAPStream) {
dsinclair38fd8442016-09-15 10:15:32 -0700353 CPDF_Dictionary* pAPDic = pAnnotAP->GetDictFor("N");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700354 if (!pAPDic)
355 continue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700356
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700357 if (!sAnnotState.IsEmpty()) {
dsinclair38fd8442016-09-15 10:15:32 -0700358 pAPStream = pAPDic->GetStreamFor(sAnnotState);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700359 } else {
Oliver Chang3f1c71f2016-01-11 08:45:31 -0800360 auto it = pAPDic->begin();
361 if (it != pAPDic->end()) {
tsepez0e606b52016-11-18 16:22:41 -0800362 CPDF_Object* pFirstObj = it->second.get();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700363 if (pFirstObj) {
Dan Sinclairbf81c142015-10-26 16:54:39 -0400364 if (pFirstObj->IsReference())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700365 pFirstObj = pFirstObj->GetDirect();
Dan Sinclairaa435ba2015-10-22 16:45:48 -0400366 if (!pFirstObj->IsStream())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700367 continue;
Dan Sinclairaa435ba2015-10-22 16:45:48 -0400368 pAPStream = pFirstObj->AsStream();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700369 }
370 }
371 }
372 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700373 if (!pAPStream)
374 continue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700375
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700376 CPDF_Dictionary* pAPDic = pAPStream->GetDict();
Tom Sepez281a9ea2016-02-26 14:24:28 -0800377 CFX_FloatRect rcStream;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700378 if (pAPDic->KeyExist("Rect"))
dsinclair38fd8442016-09-15 10:15:32 -0700379 rcStream = pAPDic->GetRectFor("Rect");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700380 else if (pAPDic->KeyExist("BBox"))
dsinclair38fd8442016-09-15 10:15:32 -0700381 rcStream = pAPDic->GetRectFor("BBox");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700382
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700383 if (rcStream.IsEmpty())
384 continue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700385
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700386 CPDF_Object* pObj = pAPStream;
tsepezd0ecd892016-11-08 17:30:04 -0800387 if (pObj->IsInline()) {
tsepez335cf092016-11-09 13:28:26 -0800388 std::unique_ptr<CPDF_Object> pNew = pObj->Clone();
389 pObj = pNew.get();
tsepez70c4afd2016-11-15 11:33:44 -0800390 pDocument->AddIndirectObject(std::move(pNew));
tsepezd0ecd892016-11-08 17:30:04 -0800391 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700392
tsepezd0ecd892016-11-08 17:30:04 -0800393 CPDF_Dictionary* pObjDic = pObj->GetDict();
394 if (pObjDic) {
tsepez0e606b52016-11-18 16:22:41 -0800395 pObjDic->SetNewFor<CPDF_Name>("Type", "XObject");
396 pObjDic->SetNewFor<CPDF_Name>("Subtype", "Form");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700397 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700398
dsinclair38fd8442016-09-15 10:15:32 -0700399 CPDF_Dictionary* pXObject = pNewXORes->GetDictFor("XObject");
tsepez0e606b52016-11-18 16:22:41 -0800400 if (!pXObject)
401 pXObject = pNewXORes->SetNewFor<CPDF_Dictionary>("XObject");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700402
403 CFX_ByteString sFormName;
404 sFormName.Format("F%d", i);
tsepez0e606b52016-11-18 16:22:41 -0800405 pXObject->SetNewFor<CPDF_Reference>(sFormName, pDocument,
406 pObj->GetObjNum());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700407
Tom Sepezafd0d1f2017-04-04 14:37:18 -0700408 auto pAcc = pdfium::MakeRetain<CPDF_StreamAcc>(pNewXObject);
409 pAcc->LoadAllData();
410 CFX_ByteString sStream(pAcc->GetData(), pAcc->GetSize());
tsepezd0ecd892016-11-08 17:30:04 -0800411 CFX_Matrix matrix = pAPDic->GetMatrixFor("Matrix");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700412 if (matrix.IsIdentity()) {
413 matrix.a = 1.0f;
414 matrix.b = 0.0f;
415 matrix.c = 0.0f;
416 matrix.d = 1.0f;
417 matrix.e = 0.0f;
418 matrix.f = 0.0f;
419 }
420
tsepezbb577af2016-09-21 19:10:19 -0700421 CFX_ByteString sTemp;
Tom Sepez60d909e2015-12-10 15:34:55 -0800422 CFX_Matrix m = GetMatrix(rcAnnot, rcStream, matrix);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700423 sTemp.Format("q %f 0 0 %f %f %f cm /%s Do Q\n", m.a, m.d, m.e, m.f,
424 sFormName.c_str());
425 sStream += sTemp;
tsepeze6db16e2016-09-19 10:45:09 -0700426 pNewXObject->SetData(sStream.raw_str(), sStream.GetLength());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700427 }
dsinclair38fd8442016-09-15 10:15:32 -0700428 pPageDict->RemoveFor("Annots");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700429 return FLATTEN_SUCCESS;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700430}