blob: ccbb7b876586ffb59b34a122f71eb9cdfabd0c8e [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) {
thestig5cc24652016-04-26 11:46:02 -070054 std::unique_ptr<CPDF_Page> pPDFPage(new CPDF_Page(pDoc, pDict, false));
55 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
tsepez6173c9d2016-11-09 16:38:03 -0800126FX_FLOAT GetMinMaxValue(const std::vector<CFX_FloatRect>& array,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700127 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
tsepez6173c9d2016-11-09 16:38:03 -0800133 std::vector<FX_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
156 FX_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>(
tsepez381fc832016-10-10 14:06:44 -0700181 nullptr, 0, new CPDF_Dictionary(pDocument->GetByteStringPool()));
182 CFX_ByteString sStream;
183 sStream.Format("q 1 0 0 1 0 0 cm /%s Do Q", key.c_str());
184 pNewContents->SetData(sStream.raw_str(), sStream.GetLength());
tsepez70c4afd2016-11-15 11:33:44 -0800185 return pNewContents->GetObjNum();
tsepez381fc832016-10-10 14:06:44 -0700186}
187
188void SetPageContents(const CFX_ByteString& key,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700189 CPDF_Dictionary* pPage,
190 CPDF_Document* pDocument) {
thestig1cd352e2016-06-07 17:53:06 -0700191 CPDF_Array* pContentsArray = nullptr;
tsepez381fc832016-10-10 14:06:44 -0700192 CPDF_Stream* pContentsStream = pPage->GetStreamFor("Contents");
193 if (!pContentsStream) {
194 pContentsArray = pPage->GetArrayFor("Contents");
195 if (!pContentsArray) {
196 if (!key.IsEmpty()) {
tsepez0e606b52016-11-18 16:22:41 -0800197 pPage->SetNewFor<CPDF_Reference>(
198 "Contents", pDocument, NewIndirectContentsStream(key, pDocument));
tsepez381fc832016-10-10 14:06:44 -0700199 }
200 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700201 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700202 }
tsepez381fc832016-10-10 14:06:44 -0700203 pPage->ConvertToIndirectObjectFor("Contents", pDocument);
204 if (!pContentsArray) {
tsepez70c4afd2016-11-15 11:33:44 -0800205 pContentsArray = pDocument->NewIndirect<CPDF_Array>();
tsepez381fc832016-10-10 14:06:44 -0700206 CPDF_StreamAcc acc;
207 acc.LoadAllData(pContentsStream);
208 CFX_ByteString sStream = "q\n";
Tom Sepezc25a4212016-10-14 17:45:56 -0700209 CFX_ByteString sBody =
210 CFX_ByteString((const FX_CHAR*)acc.GetData(), acc.GetSize());
tsepez381fc832016-10-10 14:06:44 -0700211 sStream = sStream + sBody + "\nQ";
212 pContentsStream->SetData(sStream.raw_str(), sStream.GetLength());
tsepez8a3aa452016-11-16 12:26:06 -0800213 pContentsArray->AddNew<CPDF_Reference>(pDocument,
214 pContentsStream->GetObjNum());
tsepez0e606b52016-11-18 16:22:41 -0800215 pPage->SetNewFor<CPDF_Reference>("Contents", pDocument,
216 pContentsArray->GetObjNum());
tsepez381fc832016-10-10 14:06:44 -0700217 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700218 if (!key.IsEmpty()) {
tsepez8a3aa452016-11-16 12:26:06 -0800219 pContentsArray->AddNew<CPDF_Reference>(
220 pDocument, NewIndirectContentsStream(key, pDocument));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700221 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700222}
223
Tom Sepez281a9ea2016-02-26 14:24:28 -0800224CFX_Matrix GetMatrix(CFX_FloatRect rcAnnot,
225 CFX_FloatRect rcStream,
Tom Sepez60d909e2015-12-10 15:34:55 -0800226 const CFX_Matrix& matrix) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700227 if (rcStream.IsEmpty())
Tom Sepez60d909e2015-12-10 15:34:55 -0800228 return CFX_Matrix();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700229
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700230 matrix.TransformRect(rcStream);
231 rcStream.Normalize();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700232
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700233 FX_FLOAT a = rcAnnot.Width() / rcStream.Width();
234 FX_FLOAT d = rcAnnot.Height() / rcStream.Height();
Bo Xufdc00a72014-10-28 23:03:33 -0700235
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700236 FX_FLOAT e = rcAnnot.left - rcStream.left * a;
237 FX_FLOAT f = rcAnnot.bottom - rcStream.bottom * d;
Tom Sepez60d909e2015-12-10 15:34:55 -0800238 return CFX_Matrix(a, 0, 0, d, e, f);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700239}
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700240
tsepez381fc832016-10-10 14:06:44 -0700241} // namespace
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700242
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700243DLLEXPORT int STDCALL FPDFPage_Flatten(FPDF_PAGE page, int nFlag) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700244 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
tsepez6173c9d2016-11-09 16:38:03 -0800245 if (!page)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700246 return FLATTEN_FAIL;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700247
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700248 CPDF_Document* pDocument = pPage->m_pDocument;
249 CPDF_Dictionary* pPageDict = pPage->m_pFormDict;
tsepez6173c9d2016-11-09 16:38:03 -0800250 if (!pDocument || !pPageDict)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700251 return FLATTEN_FAIL;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700252
tsepez74b8c6e2016-10-12 09:38:41 -0700253 std::vector<CPDF_Dictionary*> ObjectArray;
tsepez6173c9d2016-11-09 16:38:03 -0800254 std::vector<CFX_FloatRect> RectArray;
255 int iRet =
256 ParserAnnots(pDocument, pPageDict, &RectArray, &ObjectArray, nFlag);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700257 if (iRet == FLATTEN_NOTHINGTODO || iRet == FLATTEN_FAIL)
258 return iRet;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700259
Tom Sepez281a9ea2016-02-26 14:24:28 -0800260 CFX_FloatRect rcOriginalCB;
261 CFX_FloatRect rcMerger = CalculateRect(&RectArray);
dsinclair38fd8442016-09-15 10:15:32 -0700262 CFX_FloatRect rcOriginalMB = pPageDict->GetRectFor("MediaBox");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700263 if (pPageDict->KeyExist("CropBox"))
dsinclair38fd8442016-09-15 10:15:32 -0700264 rcOriginalMB = pPageDict->GetRectFor("CropBox");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700265
tsepez6173c9d2016-11-09 16:38:03 -0800266 if (rcOriginalMB.IsEmpty())
Tom Sepez281a9ea2016-02-26 14:24:28 -0800267 rcOriginalMB = CFX_FloatRect(0.0f, 0.0f, 612.0f, 792.0f);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700268
tsepez6173c9d2016-11-09 16:38:03 -0800269 rcMerger.left = std::max(rcMerger.left, rcOriginalMB.left);
270 rcMerger.right = std::min(rcMerger.right, rcOriginalMB.right);
271 rcMerger.bottom = std::max(rcMerger.bottom, rcOriginalMB.bottom);
272 rcMerger.top = std::min(rcMerger.top, rcOriginalMB.top);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700273 if (pPageDict->KeyExist("ArtBox"))
dsinclair38fd8442016-09-15 10:15:32 -0700274 rcOriginalCB = pPageDict->GetRectFor("ArtBox");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700275 else
276 rcOriginalCB = rcOriginalMB;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700277
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700278 if (!rcOriginalMB.IsEmpty()) {
tsepez0e606b52016-11-18 16:22:41 -0800279 CPDF_Array* pMediaBox = pPageDict->SetNewFor<CPDF_Array>("MediaBox");
tsepez8a3aa452016-11-16 12:26:06 -0800280 pMediaBox->AddNew<CPDF_Number>(rcOriginalMB.left);
281 pMediaBox->AddNew<CPDF_Number>(rcOriginalMB.bottom);
282 pMediaBox->AddNew<CPDF_Number>(rcOriginalMB.right);
283 pMediaBox->AddNew<CPDF_Number>(rcOriginalMB.top);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700284 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700285
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700286 if (!rcOriginalCB.IsEmpty()) {
tsepez0e606b52016-11-18 16:22:41 -0800287 CPDF_Array* pCropBox = pPageDict->SetNewFor<CPDF_Array>("ArtBox");
tsepez8a3aa452016-11-16 12:26:06 -0800288 pCropBox->AddNew<CPDF_Number>(rcOriginalCB.left);
289 pCropBox->AddNew<CPDF_Number>(rcOriginalCB.bottom);
290 pCropBox->AddNew<CPDF_Number>(rcOriginalCB.right);
291 pCropBox->AddNew<CPDF_Number>(rcOriginalCB.top);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700292 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700293
dsinclair38fd8442016-09-15 10:15:32 -0700294 CPDF_Dictionary* pRes = pPageDict->GetDictFor("Resources");
tsepez0e606b52016-11-18 16:22:41 -0800295 if (!pRes)
296 pRes = pPageDict->SetNewFor<CPDF_Dictionary>("Resources");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700297
tsepez70c4afd2016-11-15 11:33:44 -0800298 CPDF_Stream* pNewXObject = pDocument->NewIndirect<CPDF_Stream>(
tsepez698c5712016-09-28 16:47:07 -0700299 nullptr, 0, new CPDF_Dictionary(pDocument->GetByteStringPool()));
300
tsepez70c4afd2016-11-15 11:33:44 -0800301 uint32_t dwObjNum = pNewXObject->GetObjNum();
dsinclair38fd8442016-09-15 10:15:32 -0700302 CPDF_Dictionary* pPageXObject = pRes->GetDictFor("XObject");
tsepez0e606b52016-11-18 16:22:41 -0800303 if (!pPageXObject)
304 pPageXObject = pRes->SetNewFor<CPDF_Dictionary>("XObject");
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()) {
tsepez0e606b52016-11-18 16:22:41 -0800322 pPageXObject->SetNewFor<CPDF_Reference>(key, pDocument, dwObjNum);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700323 CPDF_Dictionary* pNewOXbjectDic = pNewXObject->GetDict();
tsepez0e606b52016-11-18 16:22:41 -0800324 pNewXORes = pNewOXbjectDic->SetNewFor<CPDF_Dictionary>("Resources");
325 pNewOXbjectDic->SetNewFor<CPDF_Name>("Type", "XObject");
326 pNewOXbjectDic->SetNewFor<CPDF_Name>("Subtype", "Form");
327 pNewOXbjectDic->SetNewFor<CPDF_Number>("FormType", 1);
328 pNewOXbjectDic->SetNewFor<CPDF_Name>("Name", "FRM");
dsinclair38fd8442016-09-15 10:15:32 -0700329 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()) {
tsepez0e606b52016-11-18 16:22:41 -0800357 CPDF_Object* pFirstObj = it->second.get();
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) {
tsepez0e606b52016-11-18 16:22:41 -0800390 pObjDic->SetNewFor<CPDF_Name>("Type", "XObject");
391 pObjDic->SetNewFor<CPDF_Name>("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");
tsepez0e606b52016-11-18 16:22:41 -0800395 if (!pXObject)
396 pXObject = pNewXORes->SetNewFor<CPDF_Dictionary>("XObject");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700397
398 CFX_ByteString sFormName;
399 sFormName.Format("F%d", i);
tsepez0e606b52016-11-18 16:22:41 -0800400 pXObject->SetNewFor<CPDF_Reference>(sFormName, pDocument,
401 pObj->GetObjNum());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700402
403 CPDF_StreamAcc acc;
404 acc.LoadAllData(pNewXObject);
405
406 const uint8_t* pData = acc.GetData();
407 CFX_ByteString sStream(pData, acc.GetSize());
tsepezd0ecd892016-11-08 17:30:04 -0800408 CFX_Matrix matrix = pAPDic->GetMatrixFor("Matrix");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700409 if (matrix.IsIdentity()) {
410 matrix.a = 1.0f;
411 matrix.b = 0.0f;
412 matrix.c = 0.0f;
413 matrix.d = 1.0f;
414 matrix.e = 0.0f;
415 matrix.f = 0.0f;
416 }
417
tsepezbb577af2016-09-21 19:10:19 -0700418 CFX_ByteString sTemp;
Tom Sepez60d909e2015-12-10 15:34:55 -0800419 CFX_Matrix m = GetMatrix(rcAnnot, rcStream, matrix);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700420 sTemp.Format("q %f 0 0 %f %f %f cm /%s Do Q\n", m.a, m.d, m.e, m.f,
421 sFormName.c_str());
422 sStream += sTemp;
tsepeze6db16e2016-09-19 10:45:09 -0700423 pNewXObject->SetData(sStream.raw_str(), sStream.GetLength());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700424 }
dsinclair38fd8442016-09-15 10:15:32 -0700425 pPageDict->RemoveFor("Annots");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700426 return FLATTEN_SUCCESS;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700427}