blob: cb0a625e2362abb7a1c088c3042c05a1d63a31e3 [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"
tsepez8a3aa452016-11-16 12:26:06 -080016#include "core/fpdfapi/parser/cpdf_reference.h"
dsinclair488b7ad2016-10-04 11:55:50 -070017#include "core/fpdfapi/parser/cpdf_stream.h"
18#include "core/fpdfapi/parser/cpdf_stream_acc.h"
dsinclair1727aee2016-09-29 13:12:56 -070019#include "core/fpdfdoc/cpdf_annot.h"
dsinclair114e46a2016-09-29 17:18:21 -070020#include "fpdfsdk/fsdk_define.h"
tsepez74b8c6e2016-10-12 09:38:41 -070021#include "third_party/base/stl_util.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070022
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070023enum FPDF_TYPE { MAX, MIN };
24enum FPDF_VALUE { TOP, LEFT, RIGHT, BOTTOM };
25
tsepez381fc832016-10-10 14:06:44 -070026namespace {
27
tsepez4cf55152016-11-02 14:37:54 -070028bool IsValiableRect(CFX_FloatRect rect, CFX_FloatRect rcPage) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070029 if (rect.left - rect.right > 0.000001f || rect.bottom - rect.top > 0.000001f)
tsepez4cf55152016-11-02 14:37:54 -070030 return false;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070031
Nico Weber9d8ec5a2015-08-04 13:00:21 -070032 if (rect.left == 0.0f && rect.top == 0.0f && rect.right == 0.0f &&
33 rect.bottom == 0.0f)
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 (!rcPage.IsEmpty()) {
37 if (rect.left - rcPage.left < -10.000001f ||
38 rect.right - rcPage.right > 10.000001f ||
39 rect.top - rcPage.top > 10.000001f ||
40 rect.bottom - rcPage.bottom < -10.000001f)
tsepez4cf55152016-11-02 14:37:54 -070041 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070042 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070043
tsepez4cf55152016-11-02 14:37:54 -070044 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070045}
46
Tom Sepez2398d892016-02-17 16:46:26 -080047void GetContentsRect(CPDF_Document* pDoc,
48 CPDF_Dictionary* pDict,
tsepez6173c9d2016-11-09 16:38:03 -080049 std::vector<CFX_FloatRect>* pRectArray) {
thestig5cc24652016-04-26 11:46:02 -070050 std::unique_ptr<CPDF_Page> pPDFPage(new CPDF_Page(pDoc, pDict, false));
51 pPDFPage->ParseContent();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070052
thestig2fad11a2016-06-16 16:39:25 -070053 for (const auto& pPageObject : *pPDFPage->GetPageObjectList()) {
Tom Sepez281a9ea2016-02-26 14:24:28 -080054 CFX_FloatRect rc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070055 rc.left = pPageObject->m_Left;
56 rc.right = pPageObject->m_Right;
57 rc.bottom = pPageObject->m_Bottom;
58 rc.top = pPageObject->m_Top;
dsinclair38fd8442016-09-15 10:15:32 -070059 if (IsValiableRect(rc, pDict->GetRectFor("MediaBox")))
tsepez6173c9d2016-11-09 16:38:03 -080060 pRectArray->push_back(rc);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070061 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070062}
63
Nico Weber9d8ec5a2015-08-04 13:00:21 -070064void ParserStream(CPDF_Dictionary* pPageDic,
65 CPDF_Dictionary* pStream,
tsepez6173c9d2016-11-09 16:38:03 -080066 std::vector<CFX_FloatRect>* pRectArray,
tsepez74b8c6e2016-10-12 09:38:41 -070067 std::vector<CPDF_Dictionary*>* pObjectArray) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070068 if (!pStream)
69 return;
Tom Sepez281a9ea2016-02-26 14:24:28 -080070 CFX_FloatRect rect;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070071 if (pStream->KeyExist("Rect"))
dsinclair38fd8442016-09-15 10:15:32 -070072 rect = pStream->GetRectFor("Rect");
Nico Weber9d8ec5a2015-08-04 13:00:21 -070073 else if (pStream->KeyExist("BBox"))
dsinclair38fd8442016-09-15 10:15:32 -070074 rect = pStream->GetRectFor("BBox");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070075
dsinclair38fd8442016-09-15 10:15:32 -070076 if (IsValiableRect(rect, pPageDic->GetRectFor("MediaBox")))
tsepez6173c9d2016-11-09 16:38:03 -080077 pRectArray->push_back(rect);
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070078
tsepez74b8c6e2016-10-12 09:38:41 -070079 pObjectArray->push_back(pStream);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070080}
81
Nico Weber9d8ec5a2015-08-04 13:00:21 -070082int ParserAnnots(CPDF_Document* pSourceDoc,
83 CPDF_Dictionary* pPageDic,
tsepez6173c9d2016-11-09 16:38:03 -080084 std::vector<CFX_FloatRect>* pRectArray,
tsepez74b8c6e2016-10-12 09:38:41 -070085 std::vector<CPDF_Dictionary*>* pObjectArray,
Nico Weber9d8ec5a2015-08-04 13:00:21 -070086 int nUsage) {
87 if (!pSourceDoc || !pPageDic)
88 return FLATTEN_FAIL;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070089
Nico Weber9d8ec5a2015-08-04 13:00:21 -070090 GetContentsRect(pSourceDoc, pPageDic, pRectArray);
dsinclair38fd8442016-09-15 10:15:32 -070091 CPDF_Array* pAnnots = pPageDic->GetArrayFor("Annots");
Nico Weber9d8ec5a2015-08-04 13:00:21 -070092 if (!pAnnots)
93 return FLATTEN_NOTHINGTODO;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070094
tsepezc3255f52016-03-25 14:52:27 -070095 uint32_t dwSize = pAnnots->GetCount();
Nico Weber9d8ec5a2015-08-04 13:00:21 -070096 for (int i = 0; i < (int)dwSize; i++) {
tsepezbd567552016-03-29 14:51:50 -070097 CPDF_Dictionary* pAnnotDic = ToDictionary(pAnnots->GetDirectObjectAt(i));
Dan Sinclairf1251c12015-10-20 16:24:45 -040098 if (!pAnnotDic)
Nico Weber9d8ec5a2015-08-04 13:00:21 -070099 continue;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700100
dsinclair38fd8442016-09-15 10:15:32 -0700101 CFX_ByteString sSubtype = pAnnotDic->GetStringFor("Subtype");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700102 if (sSubtype == "Popup")
103 continue;
104
dsinclair38fd8442016-09-15 10:15:32 -0700105 int nAnnotFlag = pAnnotDic->GetIntegerFor("F");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700106 if (nAnnotFlag & ANNOTFLAG_HIDDEN)
107 continue;
108
109 if (nUsage == FLAT_NORMALDISPLAY) {
110 if (nAnnotFlag & ANNOTFLAG_INVISIBLE)
111 continue;
112
113 ParserStream(pPageDic, pAnnotDic, pRectArray, pObjectArray);
114 } else {
115 if (nAnnotFlag & ANNOTFLAG_PRINT)
116 ParserStream(pPageDic, pAnnotDic, pRectArray, pObjectArray);
117 }
118 }
119 return FLATTEN_SUCCESS;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700120}
121
tsepez6173c9d2016-11-09 16:38:03 -0800122FX_FLOAT GetMinMaxValue(const std::vector<CFX_FloatRect>& array,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700123 FPDF_TYPE type,
124 FPDF_VALUE value) {
tsepez6173c9d2016-11-09 16:38:03 -0800125 size_t nRects = array.size();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700126 if (nRects <= 0)
127 return 0.0f;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700128
tsepez6173c9d2016-11-09 16:38:03 -0800129 std::vector<FX_FLOAT> pArray(nRects);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700130 switch (value) {
tsepez6173c9d2016-11-09 16:38:03 -0800131 case LEFT:
132 for (size_t i = 0; i < nRects; i++)
133 pArray[i] = array[i].left;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700134 break;
tsepez6173c9d2016-11-09 16:38:03 -0800135 case TOP:
136 for (size_t i = 0; i < nRects; i++)
137 pArray[i] = array[i].top;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700138 break;
tsepez6173c9d2016-11-09 16:38:03 -0800139 case RIGHT:
140 for (size_t i = 0; i < nRects; i++)
141 pArray[i] = array[i].right;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700142 break;
tsepez6173c9d2016-11-09 16:38:03 -0800143 case BOTTOM:
144 for (size_t i = 0; i < nRects; i++)
145 pArray[i] = array[i].bottom;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700146 break;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700147 default:
weili12367cb2016-06-03 11:22:16 -0700148 // Not reachable.
149 return 0.0f;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700150 }
tsepez6173c9d2016-11-09 16:38:03 -0800151
152 FX_FLOAT fRet = pArray[0];
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700153 if (type == MAX) {
tsepez6173c9d2016-11-09 16:38:03 -0800154 for (size_t i = 1; i < nRects; i++)
155 fRet = std::max(fRet, pArray[i]);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700156 } else {
tsepez6173c9d2016-11-09 16:38:03 -0800157 for (size_t i = 1; i < nRects; i++)
158 fRet = std::min(fRet, pArray[i]);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700159 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700160 return fRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700161}
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700162
tsepez6173c9d2016-11-09 16:38:03 -0800163CFX_FloatRect CalculateRect(std::vector<CFX_FloatRect>* pRectArray) {
Tom Sepez281a9ea2016-02-26 14:24:28 -0800164 CFX_FloatRect rcRet;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700165
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700166 rcRet.left = GetMinMaxValue(*pRectArray, MIN, LEFT);
167 rcRet.top = GetMinMaxValue(*pRectArray, MAX, TOP);
168 rcRet.right = GetMinMaxValue(*pRectArray, MAX, RIGHT);
169 rcRet.bottom = GetMinMaxValue(*pRectArray, MIN, BOTTOM);
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700170
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700171 return rcRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700172}
173
tsepez381fc832016-10-10 14:06:44 -0700174uint32_t NewIndirectContentsStream(const CFX_ByteString& key,
175 CPDF_Document* pDocument) {
tsepez70c4afd2016-11-15 11:33:44 -0800176 CPDF_Stream* pNewContents = pDocument->NewIndirect<CPDF_Stream>(
tsepez381fc832016-10-10 14:06:44 -0700177 nullptr, 0, new CPDF_Dictionary(pDocument->GetByteStringPool()));
178 CFX_ByteString sStream;
179 sStream.Format("q 1 0 0 1 0 0 cm /%s Do Q", key.c_str());
180 pNewContents->SetData(sStream.raw_str(), sStream.GetLength());
tsepez70c4afd2016-11-15 11:33:44 -0800181 return pNewContents->GetObjNum();
tsepez381fc832016-10-10 14:06:44 -0700182}
183
184void SetPageContents(const CFX_ByteString& key,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700185 CPDF_Dictionary* pPage,
186 CPDF_Document* pDocument) {
thestig1cd352e2016-06-07 17:53:06 -0700187 CPDF_Array* pContentsArray = nullptr;
tsepez381fc832016-10-10 14:06:44 -0700188 CPDF_Stream* pContentsStream = pPage->GetStreamFor("Contents");
189 if (!pContentsStream) {
190 pContentsArray = pPage->GetArrayFor("Contents");
191 if (!pContentsArray) {
192 if (!key.IsEmpty()) {
193 pPage->SetReferenceFor("Contents", pDocument,
194 NewIndirectContentsStream(key, pDocument));
195 }
196 return;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700197 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700198 }
tsepez381fc832016-10-10 14:06:44 -0700199 pPage->ConvertToIndirectObjectFor("Contents", pDocument);
200 if (!pContentsArray) {
tsepez70c4afd2016-11-15 11:33:44 -0800201 pContentsArray = pDocument->NewIndirect<CPDF_Array>();
tsepez381fc832016-10-10 14:06:44 -0700202 CPDF_StreamAcc acc;
203 acc.LoadAllData(pContentsStream);
204 CFX_ByteString sStream = "q\n";
Tom Sepezc25a4212016-10-14 17:45:56 -0700205 CFX_ByteString sBody =
206 CFX_ByteString((const FX_CHAR*)acc.GetData(), acc.GetSize());
tsepez381fc832016-10-10 14:06:44 -0700207 sStream = sStream + sBody + "\nQ";
208 pContentsStream->SetData(sStream.raw_str(), sStream.GetLength());
tsepez8a3aa452016-11-16 12:26:06 -0800209 pContentsArray->AddNew<CPDF_Reference>(pDocument,
210 pContentsStream->GetObjNum());
tsepez70c4afd2016-11-15 11:33:44 -0800211 pPage->SetReferenceFor("Contents", pDocument, pContentsArray);
tsepez381fc832016-10-10 14:06:44 -0700212 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700213 if (!key.IsEmpty()) {
tsepez8a3aa452016-11-16 12:26:06 -0800214 pContentsArray->AddNew<CPDF_Reference>(
215 pDocument, NewIndirectContentsStream(key, pDocument));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700216 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700217}
218
Tom Sepez281a9ea2016-02-26 14:24:28 -0800219CFX_Matrix GetMatrix(CFX_FloatRect rcAnnot,
220 CFX_FloatRect rcStream,
Tom Sepez60d909e2015-12-10 15:34:55 -0800221 const CFX_Matrix& matrix) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700222 if (rcStream.IsEmpty())
Tom Sepez60d909e2015-12-10 15:34:55 -0800223 return CFX_Matrix();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700224
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700225 matrix.TransformRect(rcStream);
226 rcStream.Normalize();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700227
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700228 FX_FLOAT a = rcAnnot.Width() / rcStream.Width();
229 FX_FLOAT d = rcAnnot.Height() / rcStream.Height();
Bo Xufdc00a72014-10-28 23:03:33 -0700230
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700231 FX_FLOAT e = rcAnnot.left - rcStream.left * a;
232 FX_FLOAT f = rcAnnot.bottom - rcStream.bottom * d;
Tom Sepez60d909e2015-12-10 15:34:55 -0800233 return CFX_Matrix(a, 0, 0, d, e, f);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700234}
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700235
tsepez381fc832016-10-10 14:06:44 -0700236} // namespace
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700237
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700238DLLEXPORT int STDCALL FPDFPage_Flatten(FPDF_PAGE page, int nFlag) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700239 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
tsepez6173c9d2016-11-09 16:38:03 -0800240 if (!page)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700241 return FLATTEN_FAIL;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700242
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700243 CPDF_Document* pDocument = pPage->m_pDocument;
244 CPDF_Dictionary* pPageDict = pPage->m_pFormDict;
tsepez6173c9d2016-11-09 16:38:03 -0800245 if (!pDocument || !pPageDict)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700246 return FLATTEN_FAIL;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700247
tsepez74b8c6e2016-10-12 09:38:41 -0700248 std::vector<CPDF_Dictionary*> ObjectArray;
tsepez6173c9d2016-11-09 16:38:03 -0800249 std::vector<CFX_FloatRect> RectArray;
250 int iRet =
251 ParserAnnots(pDocument, pPageDict, &RectArray, &ObjectArray, nFlag);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700252 if (iRet == FLATTEN_NOTHINGTODO || iRet == FLATTEN_FAIL)
253 return iRet;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700254
Tom Sepez281a9ea2016-02-26 14:24:28 -0800255 CFX_FloatRect rcOriginalCB;
256 CFX_FloatRect rcMerger = CalculateRect(&RectArray);
dsinclair38fd8442016-09-15 10:15:32 -0700257 CFX_FloatRect rcOriginalMB = pPageDict->GetRectFor("MediaBox");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700258 if (pPageDict->KeyExist("CropBox"))
dsinclair38fd8442016-09-15 10:15:32 -0700259 rcOriginalMB = pPageDict->GetRectFor("CropBox");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700260
tsepez6173c9d2016-11-09 16:38:03 -0800261 if (rcOriginalMB.IsEmpty())
Tom Sepez281a9ea2016-02-26 14:24:28 -0800262 rcOriginalMB = CFX_FloatRect(0.0f, 0.0f, 612.0f, 792.0f);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700263
tsepez6173c9d2016-11-09 16:38:03 -0800264 rcMerger.left = std::max(rcMerger.left, rcOriginalMB.left);
265 rcMerger.right = std::min(rcMerger.right, rcOriginalMB.right);
266 rcMerger.bottom = std::max(rcMerger.bottom, rcOriginalMB.bottom);
267 rcMerger.top = std::min(rcMerger.top, rcOriginalMB.top);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700268 if (pPageDict->KeyExist("ArtBox"))
dsinclair38fd8442016-09-15 10:15:32 -0700269 rcOriginalCB = pPageDict->GetRectFor("ArtBox");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700270 else
271 rcOriginalCB = rcOriginalMB;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700272
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700273 if (!rcOriginalMB.IsEmpty()) {
Tom Sepezae51c812015-08-05 12:34:06 -0700274 CPDF_Array* pMediaBox = new CPDF_Array();
tsepez8a3aa452016-11-16 12:26:06 -0800275 pMediaBox->AddNew<CPDF_Number>(rcOriginalMB.left);
276 pMediaBox->AddNew<CPDF_Number>(rcOriginalMB.bottom);
277 pMediaBox->AddNew<CPDF_Number>(rcOriginalMB.right);
278 pMediaBox->AddNew<CPDF_Number>(rcOriginalMB.top);
dsinclair38fd8442016-09-15 10:15:32 -0700279 pPageDict->SetFor("MediaBox", pMediaBox);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700280 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700281
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700282 if (!rcOriginalCB.IsEmpty()) {
Tom Sepezae51c812015-08-05 12:34:06 -0700283 CPDF_Array* pCropBox = new CPDF_Array();
tsepez8a3aa452016-11-16 12:26:06 -0800284 pCropBox->AddNew<CPDF_Number>(rcOriginalCB.left);
285 pCropBox->AddNew<CPDF_Number>(rcOriginalCB.bottom);
286 pCropBox->AddNew<CPDF_Number>(rcOriginalCB.right);
287 pCropBox->AddNew<CPDF_Number>(rcOriginalCB.top);
dsinclair38fd8442016-09-15 10:15:32 -0700288 pPageDict->SetFor("ArtBox", pCropBox);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700289 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700290
dsinclair38fd8442016-09-15 10:15:32 -0700291 CPDF_Dictionary* pRes = pPageDict->GetDictFor("Resources");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700292 if (!pRes) {
tsepez698c5712016-09-28 16:47:07 -0700293 pRes = new CPDF_Dictionary(pDocument->GetByteStringPool());
dsinclair38fd8442016-09-15 10:15:32 -0700294 pPageDict->SetFor("Resources", pRes);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700295 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700296
tsepez70c4afd2016-11-15 11:33:44 -0800297 CPDF_Stream* pNewXObject = pDocument->NewIndirect<CPDF_Stream>(
tsepez698c5712016-09-28 16:47:07 -0700298 nullptr, 0, new CPDF_Dictionary(pDocument->GetByteStringPool()));
299
tsepez70c4afd2016-11-15 11:33:44 -0800300 uint32_t dwObjNum = pNewXObject->GetObjNum();
dsinclair38fd8442016-09-15 10:15:32 -0700301 CPDF_Dictionary* pPageXObject = pRes->GetDictFor("XObject");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700302 if (!pPageXObject) {
tsepez698c5712016-09-28 16:47:07 -0700303 pPageXObject = new CPDF_Dictionary(pDocument->GetByteStringPool());
dsinclair38fd8442016-09-15 10:15:32 -0700304 pRes->SetFor("XObject", pPageXObject);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700305 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700306
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700307 CFX_ByteString key = "";
tsepez74b8c6e2016-10-12 09:38:41 -0700308 int nStreams = pdfium::CollectionSize<int>(ObjectArray);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700309 if (nStreams > 0) {
310 for (int iKey = 0; /*iKey < 100*/; iKey++) {
311 char sExtend[5] = {};
312 FXSYS_itoa(iKey, sExtend, 10);
313 key = CFX_ByteString("FFT") + CFX_ByteString(sExtend);
tsepez7b1ccf92016-04-14 11:04:57 -0700314 if (!pPageXObject->KeyExist(key))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700315 break;
316 }
317 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700318
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700319 SetPageContents(key, pPageDict, pDocument);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700320
thestig1cd352e2016-06-07 17:53:06 -0700321 CPDF_Dictionary* pNewXORes = nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700322 if (!key.IsEmpty()) {
dsinclair38fd8442016-09-15 10:15:32 -0700323 pPageXObject->SetReferenceFor(key, pDocument, dwObjNum);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700324 CPDF_Dictionary* pNewOXbjectDic = pNewXObject->GetDict();
tsepez698c5712016-09-28 16:47:07 -0700325 pNewXORes = new CPDF_Dictionary(pDocument->GetByteStringPool());
dsinclair38fd8442016-09-15 10:15:32 -0700326 pNewOXbjectDic->SetFor("Resources", pNewXORes);
327 pNewOXbjectDic->SetNameFor("Type", "XObject");
328 pNewOXbjectDic->SetNameFor("Subtype", "Form");
329 pNewOXbjectDic->SetIntegerFor("FormType", 1);
330 pNewOXbjectDic->SetNameFor("Name", "FRM");
331 CFX_FloatRect rcBBox = pPageDict->GetRectFor("ArtBox");
332 pNewOXbjectDic->SetRectFor("BBox", rcBBox);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700333 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700334
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700335 for (int i = 0; i < nStreams; i++) {
tsepez74b8c6e2016-10-12 09:38:41 -0700336 CPDF_Dictionary* pAnnotDic = ObjectArray[i];
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700337 if (!pAnnotDic)
338 continue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700339
dsinclair38fd8442016-09-15 10:15:32 -0700340 CFX_FloatRect rcAnnot = pAnnotDic->GetRectFor("Rect");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700341 rcAnnot.Normalize();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700342
dsinclair38fd8442016-09-15 10:15:32 -0700343 CFX_ByteString sAnnotState = pAnnotDic->GetStringFor("AS");
344 CPDF_Dictionary* pAnnotAP = pAnnotDic->GetDictFor("AP");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700345 if (!pAnnotAP)
346 continue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700347
dsinclair38fd8442016-09-15 10:15:32 -0700348 CPDF_Stream* pAPStream = pAnnotAP->GetStreamFor("N");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700349 if (!pAPStream) {
dsinclair38fd8442016-09-15 10:15:32 -0700350 CPDF_Dictionary* pAPDic = pAnnotAP->GetDictFor("N");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700351 if (!pAPDic)
352 continue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700353
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700354 if (!sAnnotState.IsEmpty()) {
dsinclair38fd8442016-09-15 10:15:32 -0700355 pAPStream = pAPDic->GetStreamFor(sAnnotState);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700356 } else {
Oliver Chang3f1c71f2016-01-11 08:45:31 -0800357 auto it = pAPDic->begin();
358 if (it != pAPDic->end()) {
tsepezd5bd8a12016-10-17 11:13:54 -0700359 CPDF_Object* pFirstObj = it->second;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700360 if (pFirstObj) {
Dan Sinclairbf81c142015-10-26 16:54:39 -0400361 if (pFirstObj->IsReference())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700362 pFirstObj = pFirstObj->GetDirect();
Dan Sinclairaa435ba2015-10-22 16:45:48 -0400363 if (!pFirstObj->IsStream())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700364 continue;
Dan Sinclairaa435ba2015-10-22 16:45:48 -0400365 pAPStream = pFirstObj->AsStream();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700366 }
367 }
368 }
369 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700370 if (!pAPStream)
371 continue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700372
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700373 CPDF_Dictionary* pAPDic = pAPStream->GetDict();
Tom Sepez281a9ea2016-02-26 14:24:28 -0800374 CFX_FloatRect rcStream;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700375 if (pAPDic->KeyExist("Rect"))
dsinclair38fd8442016-09-15 10:15:32 -0700376 rcStream = pAPDic->GetRectFor("Rect");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700377 else if (pAPDic->KeyExist("BBox"))
dsinclair38fd8442016-09-15 10:15:32 -0700378 rcStream = pAPDic->GetRectFor("BBox");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700379
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700380 if (rcStream.IsEmpty())
381 continue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700382
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700383 CPDF_Object* pObj = pAPStream;
tsepezd0ecd892016-11-08 17:30:04 -0800384 if (pObj->IsInline()) {
tsepez335cf092016-11-09 13:28:26 -0800385 std::unique_ptr<CPDF_Object> pNew = pObj->Clone();
386 pObj = pNew.get();
tsepez70c4afd2016-11-15 11:33:44 -0800387 pDocument->AddIndirectObject(std::move(pNew));
tsepezd0ecd892016-11-08 17:30:04 -0800388 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700389
tsepezd0ecd892016-11-08 17:30:04 -0800390 CPDF_Dictionary* pObjDic = pObj->GetDict();
391 if (pObjDic) {
392 pObjDic->SetNameFor("Type", "XObject");
393 pObjDic->SetNameFor("Subtype", "Form");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700394 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700395
dsinclair38fd8442016-09-15 10:15:32 -0700396 CPDF_Dictionary* pXObject = pNewXORes->GetDictFor("XObject");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700397 if (!pXObject) {
tsepez698c5712016-09-28 16:47:07 -0700398 pXObject = new CPDF_Dictionary(pDocument->GetByteStringPool());
dsinclair38fd8442016-09-15 10:15:32 -0700399 pNewXORes->SetFor("XObject", pXObject);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700400 }
401
402 CFX_ByteString sFormName;
403 sFormName.Format("F%d", i);
tsepezd0ecd892016-11-08 17:30:04 -0800404 pXObject->SetReferenceFor(sFormName, pDocument, pObj->GetObjNum());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700405
406 CPDF_StreamAcc acc;
407 acc.LoadAllData(pNewXObject);
408
409 const uint8_t* pData = acc.GetData();
410 CFX_ByteString sStream(pData, acc.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}