John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1 | // 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 Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 4 | |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 5 | // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | |
Lei Zhang | b4e7f30 | 2015-11-06 15:52:32 -0800 | [diff] [blame] | 7 | #include "public/fpdf_flatten.h" |
| 8 | |
Lei Zhang | 375a864 | 2016-01-11 11:59:17 -0800 | [diff] [blame] | 9 | #include <algorithm> |
| 10 | |
Dan Sinclair | aa403d3 | 2016-03-15 14:57:22 -0400 | [diff] [blame^] | 11 | #include "core/fpdfapi/fpdf_parser/include/cpdf_array.h" |
| 12 | #include "core/fpdfapi/fpdf_parser/include/cpdf_document.h" |
| 13 | #include "core/fpdfapi/fpdf_parser/include/cpdf_number.h" |
| 14 | #include "core/include/fpdfapi/fpdf_page.h" |
Lei Zhang | bde53d2 | 2015-11-12 22:21:30 -0800 | [diff] [blame] | 15 | #include "fpdfsdk/include/fsdk_define.h" |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 16 | |
| 17 | typedef CFX_ArrayTemplate<CPDF_Dictionary*> CPDF_ObjectArray; |
Tom Sepez | 281a9ea | 2016-02-26 14:24:28 -0800 | [diff] [blame] | 18 | typedef CFX_ArrayTemplate<CFX_FloatRect> CPDF_RectArray; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 19 | |
| 20 | enum FPDF_TYPE { MAX, MIN }; |
| 21 | enum FPDF_VALUE { TOP, LEFT, RIGHT, BOTTOM }; |
| 22 | |
Tom Sepez | 281a9ea | 2016-02-26 14:24:28 -0800 | [diff] [blame] | 23 | FX_BOOL IsValiableRect(CFX_FloatRect rect, CFX_FloatRect rcPage) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 24 | if (rect.left - rect.right > 0.000001f || rect.bottom - rect.top > 0.000001f) |
| 25 | return FALSE; |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 26 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 27 | if (rect.left == 0.0f && rect.top == 0.0f && rect.right == 0.0f && |
| 28 | rect.bottom == 0.0f) |
| 29 | return FALSE; |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 30 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 31 | if (!rcPage.IsEmpty()) { |
| 32 | if (rect.left - rcPage.left < -10.000001f || |
| 33 | rect.right - rcPage.right > 10.000001f || |
| 34 | rect.top - rcPage.top > 10.000001f || |
| 35 | rect.bottom - rcPage.bottom < -10.000001f) |
| 36 | return FALSE; |
| 37 | } |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 38 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 39 | return TRUE; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 40 | } |
| 41 | |
Tom Sepez | 2398d89 | 2016-02-17 16:46:26 -0800 | [diff] [blame] | 42 | void GetContentsRect(CPDF_Document* pDoc, |
| 43 | CPDF_Dictionary* pDict, |
| 44 | CPDF_RectArray* pRectArray) { |
| 45 | std::unique_ptr<CPDF_Page> pPDFPage(new CPDF_Page); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 46 | pPDFPage->Load(pDoc, pDict, FALSE); |
Tom Sepez | b5b2a91 | 2016-01-21 11:04:37 -0800 | [diff] [blame] | 47 | pPDFPage->ParseContent(nullptr); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 48 | |
Tom Sepez | 2398d89 | 2016-02-17 16:46:26 -0800 | [diff] [blame] | 49 | for (auto& pPageObject : *pPDFPage->GetPageObjectList()) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 50 | if (!pPageObject) |
| 51 | continue; |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 52 | |
Tom Sepez | 281a9ea | 2016-02-26 14:24:28 -0800 | [diff] [blame] | 53 | CFX_FloatRect rc; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 54 | rc.left = pPageObject->m_Left; |
| 55 | rc.right = pPageObject->m_Right; |
| 56 | rc.bottom = pPageObject->m_Bottom; |
| 57 | rc.top = pPageObject->m_Top; |
Tom Sepez | 2398d89 | 2016-02-17 16:46:26 -0800 | [diff] [blame] | 58 | if (IsValiableRect(rc, pDict->GetRectBy("MediaBox"))) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 59 | pRectArray->Add(rc); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 60 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 63 | void ParserStream(CPDF_Dictionary* pPageDic, |
| 64 | CPDF_Dictionary* pStream, |
| 65 | CPDF_RectArray* pRectArray, |
| 66 | CPDF_ObjectArray* pObjectArray) { |
| 67 | if (!pStream) |
| 68 | return; |
Tom Sepez | 281a9ea | 2016-02-26 14:24:28 -0800 | [diff] [blame] | 69 | CFX_FloatRect rect; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 70 | if (pStream->KeyExist("Rect")) |
Wei Li | 9b76113 | 2016-01-29 15:44:20 -0800 | [diff] [blame] | 71 | rect = pStream->GetRectBy("Rect"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 72 | else if (pStream->KeyExist("BBox")) |
Wei Li | 9b76113 | 2016-01-29 15:44:20 -0800 | [diff] [blame] | 73 | rect = pStream->GetRectBy("BBox"); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 74 | |
Wei Li | 9b76113 | 2016-01-29 15:44:20 -0800 | [diff] [blame] | 75 | if (IsValiableRect(rect, pPageDic->GetRectBy("MediaBox"))) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 76 | pRectArray->Add(rect); |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 77 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 78 | pObjectArray->Add(pStream); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 81 | int ParserAnnots(CPDF_Document* pSourceDoc, |
| 82 | CPDF_Dictionary* pPageDic, |
| 83 | CPDF_RectArray* pRectArray, |
| 84 | CPDF_ObjectArray* pObjectArray, |
| 85 | int nUsage) { |
| 86 | if (!pSourceDoc || !pPageDic) |
| 87 | return FLATTEN_FAIL; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 88 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 89 | GetContentsRect(pSourceDoc, pPageDic, pRectArray); |
Wei Li | 9b76113 | 2016-01-29 15:44:20 -0800 | [diff] [blame] | 90 | CPDF_Array* pAnnots = pPageDic->GetArrayBy("Annots"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 91 | if (!pAnnots) |
| 92 | return FLATTEN_NOTHINGTODO; |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 93 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 94 | FX_DWORD dwSize = pAnnots->GetCount(); |
| 95 | for (int i = 0; i < (int)dwSize; i++) { |
Dan Sinclair | f1251c1 | 2015-10-20 16:24:45 -0400 | [diff] [blame] | 96 | CPDF_Dictionary* pAnnotDic = ToDictionary(pAnnots->GetElementValue(i)); |
| 97 | if (!pAnnotDic) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 98 | continue; |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 99 | |
Wei Li | 9b76113 | 2016-01-29 15:44:20 -0800 | [diff] [blame] | 100 | CFX_ByteString sSubtype = pAnnotDic->GetStringBy("Subtype"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 101 | if (sSubtype == "Popup") |
| 102 | continue; |
| 103 | |
Wei Li | 9b76113 | 2016-01-29 15:44:20 -0800 | [diff] [blame] | 104 | int nAnnotFlag = pAnnotDic->GetIntegerBy("F"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 105 | 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-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 119 | } |
| 120 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 121 | FX_FLOAT GetMinMaxValue(CPDF_RectArray& array, |
| 122 | FPDF_TYPE type, |
| 123 | FPDF_VALUE value) { |
| 124 | int nRects = array.GetSize(); |
| 125 | FX_FLOAT fRet = 0.0f; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 126 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 127 | if (nRects <= 0) |
| 128 | return 0.0f; |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 129 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 130 | FX_FLOAT* pArray = new FX_FLOAT[nRects]; |
| 131 | switch (value) { |
| 132 | case LEFT: { |
| 133 | for (int i = 0; i < nRects; i++) |
Tom Sepez | 281a9ea | 2016-02-26 14:24:28 -0800 | [diff] [blame] | 134 | pArray[i] = CFX_FloatRect(array.GetAt(i)).left; |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 135 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 136 | break; |
| 137 | } |
| 138 | case TOP: { |
| 139 | for (int i = 0; i < nRects; i++) |
Tom Sepez | 281a9ea | 2016-02-26 14:24:28 -0800 | [diff] [blame] | 140 | pArray[i] = CFX_FloatRect(array.GetAt(i)).top; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 141 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 142 | break; |
| 143 | } |
| 144 | case RIGHT: { |
| 145 | for (int i = 0; i < nRects; i++) |
Tom Sepez | 281a9ea | 2016-02-26 14:24:28 -0800 | [diff] [blame] | 146 | pArray[i] = CFX_FloatRect(array.GetAt(i)).right; |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 147 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 148 | break; |
| 149 | } |
| 150 | case BOTTOM: { |
| 151 | for (int i = 0; i < nRects; i++) |
Tom Sepez | 281a9ea | 2016-02-26 14:24:28 -0800 | [diff] [blame] | 152 | pArray[i] = CFX_FloatRect(array.GetAt(i)).bottom; |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 153 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 154 | break; |
| 155 | } |
| 156 | default: |
| 157 | break; |
| 158 | } |
| 159 | fRet = pArray[0]; |
| 160 | if (type == MAX) { |
| 161 | for (int i = 1; i < nRects; i++) |
| 162 | if (fRet <= pArray[i]) |
| 163 | fRet = pArray[i]; |
| 164 | } else { |
| 165 | for (int i = 1; i < nRects; i++) |
| 166 | if (fRet >= pArray[i]) |
| 167 | fRet = pArray[i]; |
| 168 | } |
| 169 | delete[] pArray; |
| 170 | return fRet; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 171 | } |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 172 | |
Tom Sepez | 281a9ea | 2016-02-26 14:24:28 -0800 | [diff] [blame] | 173 | CFX_FloatRect CalculateRect(CPDF_RectArray* pRectArray) { |
| 174 | CFX_FloatRect rcRet; |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 175 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 176 | rcRet.left = GetMinMaxValue(*pRectArray, MIN, LEFT); |
| 177 | rcRet.top = GetMinMaxValue(*pRectArray, MAX, TOP); |
| 178 | rcRet.right = GetMinMaxValue(*pRectArray, MAX, RIGHT); |
| 179 | rcRet.bottom = GetMinMaxValue(*pRectArray, MIN, BOTTOM); |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 180 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 181 | return rcRet; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 184 | void SetPageContents(CFX_ByteString key, |
| 185 | CPDF_Dictionary* pPage, |
| 186 | CPDF_Document* pDocument) { |
Wei Li | 9b76113 | 2016-01-29 15:44:20 -0800 | [diff] [blame] | 187 | CPDF_Object* pContentsObj = pPage->GetStreamBy("Contents"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 188 | if (!pContentsObj) { |
Wei Li | 9b76113 | 2016-01-29 15:44:20 -0800 | [diff] [blame] | 189 | pContentsObj = pPage->GetArrayBy("Contents"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 190 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 191 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 192 | if (!pContentsObj) { |
| 193 | // Create a new contents dictionary |
| 194 | if (!key.IsEmpty()) { |
Nico Weber | 077f1a3 | 2015-08-06 15:08:57 -0700 | [diff] [blame] | 195 | CPDF_Stream* pNewContents = new CPDF_Stream(NULL, 0, new CPDF_Dictionary); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 196 | pPage->SetAtReference("Contents", pDocument, |
| 197 | pDocument->AddIndirectObject(pNewContents)); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 198 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 199 | CFX_ByteString sStream; |
| 200 | sStream.Format("q 1 0 0 1 0 0 cm /%s Do Q", key.c_str()); |
| 201 | pNewContents->SetData((const uint8_t*)sStream, sStream.GetLength(), FALSE, |
| 202 | FALSE); |
| 203 | } |
| 204 | return; |
| 205 | } |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 206 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 207 | CPDF_Array* pContentsArray = NULL; |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 208 | |
Tom Sepez | 8e5cd19 | 2016-01-26 13:20:26 -0800 | [diff] [blame] | 209 | switch (pContentsObj->GetType()) { |
| 210 | case CPDF_Object::STREAM: { |
Tom Sepez | ae51c81 | 2015-08-05 12:34:06 -0700 | [diff] [blame] | 211 | pContentsArray = new CPDF_Array; |
Dan Sinclair | aa435ba | 2015-10-22 16:45:48 -0400 | [diff] [blame] | 212 | CPDF_Stream* pContents = pContentsObj->AsStream(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 213 | FX_DWORD dwObjNum = pDocument->AddIndirectObject(pContents); |
| 214 | CPDF_StreamAcc acc; |
| 215 | acc.LoadAllData(pContents); |
| 216 | CFX_ByteString sStream = "q\n"; |
| 217 | CFX_ByteString sBody = |
| 218 | CFX_ByteString((const FX_CHAR*)acc.GetData(), acc.GetSize()); |
| 219 | sStream = sStream + sBody + "\nQ"; |
| 220 | pContents->SetData((const uint8_t*)sStream, sStream.GetLength(), FALSE, |
| 221 | FALSE); |
| 222 | pContentsArray->AddReference(pDocument, dwObjNum); |
| 223 | break; |
| 224 | } |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 225 | |
Tom Sepez | 8e5cd19 | 2016-01-26 13:20:26 -0800 | [diff] [blame] | 226 | case CPDF_Object::ARRAY: { |
Dan Sinclair | 2b11dc1 | 2015-10-22 15:02:06 -0400 | [diff] [blame] | 227 | pContentsArray = pContentsObj->AsArray(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 228 | break; |
| 229 | } |
| 230 | default: |
| 231 | break; |
| 232 | } |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 233 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 234 | if (!pContentsArray) |
| 235 | return; |
| 236 | |
| 237 | FX_DWORD dwObjNum = pDocument->AddIndirectObject(pContentsArray); |
| 238 | pPage->SetAtReference("Contents", pDocument, dwObjNum); |
| 239 | |
| 240 | if (!key.IsEmpty()) { |
Nico Weber | 077f1a3 | 2015-08-06 15:08:57 -0700 | [diff] [blame] | 241 | CPDF_Stream* pNewContents = new CPDF_Stream(NULL, 0, new CPDF_Dictionary); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 242 | dwObjNum = pDocument->AddIndirectObject(pNewContents); |
| 243 | pContentsArray->AddReference(pDocument, dwObjNum); |
| 244 | |
| 245 | CFX_ByteString sStream; |
| 246 | sStream.Format("q 1 0 0 1 0 0 cm /%s Do Q", key.c_str()); |
| 247 | pNewContents->SetData((const uint8_t*)sStream, sStream.GetLength(), FALSE, |
| 248 | FALSE); |
| 249 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 250 | } |
| 251 | |
Tom Sepez | 281a9ea | 2016-02-26 14:24:28 -0800 | [diff] [blame] | 252 | CFX_Matrix GetMatrix(CFX_FloatRect rcAnnot, |
| 253 | CFX_FloatRect rcStream, |
Tom Sepez | 60d909e | 2015-12-10 15:34:55 -0800 | [diff] [blame] | 254 | const CFX_Matrix& matrix) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 255 | if (rcStream.IsEmpty()) |
Tom Sepez | 60d909e | 2015-12-10 15:34:55 -0800 | [diff] [blame] | 256 | return CFX_Matrix(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 257 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 258 | matrix.TransformRect(rcStream); |
| 259 | rcStream.Normalize(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 260 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 261 | FX_FLOAT a = rcAnnot.Width() / rcStream.Width(); |
| 262 | FX_FLOAT d = rcAnnot.Height() / rcStream.Height(); |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 263 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 264 | FX_FLOAT e = rcAnnot.left - rcStream.left * a; |
| 265 | FX_FLOAT f = rcAnnot.bottom - rcStream.bottom * d; |
Tom Sepez | 60d909e | 2015-12-10 15:34:55 -0800 | [diff] [blame] | 266 | return CFX_Matrix(a, 0, 0, d, e, f); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 267 | } |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 268 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 269 | void GetOffset(FX_FLOAT& fa, |
| 270 | FX_FLOAT& fd, |
| 271 | FX_FLOAT& fe, |
| 272 | FX_FLOAT& ff, |
Tom Sepez | 281a9ea | 2016-02-26 14:24:28 -0800 | [diff] [blame] | 273 | CFX_FloatRect rcAnnot, |
| 274 | CFX_FloatRect rcStream, |
Tom Sepez | 60d909e | 2015-12-10 15:34:55 -0800 | [diff] [blame] | 275 | const CFX_Matrix& matrix) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 276 | FX_FLOAT fStreamWidth = 0.0f; |
| 277 | FX_FLOAT fStreamHeight = 0.0f; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 278 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 279 | if (matrix.a != 0 && matrix.d != 0) { |
| 280 | fStreamWidth = rcStream.right - rcStream.left; |
| 281 | fStreamHeight = rcStream.top - rcStream.bottom; |
| 282 | } else { |
| 283 | fStreamWidth = rcStream.top - rcStream.bottom; |
| 284 | fStreamHeight = rcStream.right - rcStream.left; |
| 285 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 286 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 287 | FX_FLOAT x1 = |
| 288 | matrix.a * rcStream.left + matrix.c * rcStream.bottom + matrix.e; |
| 289 | FX_FLOAT y1 = |
| 290 | matrix.b * rcStream.left + matrix.d * rcStream.bottom + matrix.f; |
| 291 | FX_FLOAT x2 = matrix.a * rcStream.left + matrix.c * rcStream.top + matrix.e; |
| 292 | FX_FLOAT y2 = matrix.b * rcStream.left + matrix.d * rcStream.top + matrix.f; |
| 293 | FX_FLOAT x3 = |
| 294 | matrix.a * rcStream.right + matrix.c * rcStream.bottom + matrix.e; |
| 295 | FX_FLOAT y3 = |
| 296 | matrix.b * rcStream.right + matrix.d * rcStream.bottom + matrix.f; |
| 297 | FX_FLOAT x4 = matrix.a * rcStream.right + matrix.c * rcStream.top + matrix.e; |
| 298 | FX_FLOAT y4 = matrix.b * rcStream.right + matrix.d * rcStream.top + matrix.f; |
Tom Sepez | 3c3201f | 2015-05-20 10:20:35 -0700 | [diff] [blame] | 299 | |
Lei Zhang | 375a864 | 2016-01-11 11:59:17 -0800 | [diff] [blame] | 300 | FX_FLOAT left = std::min(std::min(x1, x2), std::min(x3, x4)); |
| 301 | FX_FLOAT bottom = std::min(std::min(y1, y2), std::min(y3, y4)); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 302 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 303 | fa = (rcAnnot.right - rcAnnot.left) / fStreamWidth; |
| 304 | fd = (rcAnnot.top - rcAnnot.bottom) / fStreamHeight; |
| 305 | fe = rcAnnot.left - left * fa; |
| 306 | ff = rcAnnot.bottom - bottom * fd; |
| 307 | } |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 308 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 309 | DLLEXPORT int STDCALL FPDFPage_Flatten(FPDF_PAGE page, int nFlag) { |
Tom Sepez | db0be96 | 2015-10-16 14:00:21 -0700 | [diff] [blame] | 310 | CPDF_Page* pPage = CPDFPageFromFPDFPage(page); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 311 | if (!page) { |
| 312 | return FLATTEN_FAIL; |
| 313 | } |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 314 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 315 | CPDF_Document* pDocument = pPage->m_pDocument; |
| 316 | CPDF_Dictionary* pPageDict = pPage->m_pFormDict; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 317 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 318 | if (!pDocument || !pPageDict) { |
| 319 | return FLATTEN_FAIL; |
| 320 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 321 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 322 | CPDF_ObjectArray ObjectArray; |
| 323 | CPDF_RectArray RectArray; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 324 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 325 | int iRet = FLATTEN_FAIL; |
| 326 | iRet = ParserAnnots(pDocument, pPageDict, &RectArray, &ObjectArray, nFlag); |
| 327 | if (iRet == FLATTEN_NOTHINGTODO || iRet == FLATTEN_FAIL) |
| 328 | return iRet; |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 329 | |
Tom Sepez | 281a9ea | 2016-02-26 14:24:28 -0800 | [diff] [blame] | 330 | CFX_FloatRect rcOriginalCB; |
| 331 | CFX_FloatRect rcMerger = CalculateRect(&RectArray); |
| 332 | CFX_FloatRect rcOriginalMB = pPageDict->GetRectBy("MediaBox"); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 333 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 334 | if (pPageDict->KeyExist("CropBox")) |
Wei Li | 9b76113 | 2016-01-29 15:44:20 -0800 | [diff] [blame] | 335 | rcOriginalMB = pPageDict->GetRectBy("CropBox"); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 336 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 337 | if (rcOriginalMB.IsEmpty()) { |
Tom Sepez | 281a9ea | 2016-02-26 14:24:28 -0800 | [diff] [blame] | 338 | rcOriginalMB = CFX_FloatRect(0.0f, 0.0f, 612.0f, 792.0f); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 339 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 340 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 341 | rcMerger.left = |
| 342 | rcMerger.left < rcOriginalMB.left ? rcOriginalMB.left : rcMerger.left; |
| 343 | rcMerger.right = |
| 344 | rcMerger.right > rcOriginalMB.right ? rcOriginalMB.right : rcMerger.right; |
| 345 | rcMerger.top = |
| 346 | rcMerger.top > rcOriginalMB.top ? rcOriginalMB.top : rcMerger.top; |
| 347 | rcMerger.bottom = rcMerger.bottom < rcOriginalMB.bottom ? rcOriginalMB.bottom |
| 348 | : rcMerger.bottom; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 349 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 350 | if (pPageDict->KeyExist("ArtBox")) |
Wei Li | 9b76113 | 2016-01-29 15:44:20 -0800 | [diff] [blame] | 351 | rcOriginalCB = pPageDict->GetRectBy("ArtBox"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 352 | else |
| 353 | rcOriginalCB = rcOriginalMB; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 354 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 355 | if (!rcOriginalMB.IsEmpty()) { |
Tom Sepez | ae51c81 | 2015-08-05 12:34:06 -0700 | [diff] [blame] | 356 | CPDF_Array* pMediaBox = new CPDF_Array(); |
Tom Sepez | ae51c81 | 2015-08-05 12:34:06 -0700 | [diff] [blame] | 357 | pMediaBox->Add(new CPDF_Number(rcOriginalMB.left)); |
| 358 | pMediaBox->Add(new CPDF_Number(rcOriginalMB.bottom)); |
| 359 | pMediaBox->Add(new CPDF_Number(rcOriginalMB.right)); |
| 360 | pMediaBox->Add(new CPDF_Number(rcOriginalMB.top)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 361 | pPageDict->SetAt("MediaBox", pMediaBox); |
| 362 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 363 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 364 | if (!rcOriginalCB.IsEmpty()) { |
Tom Sepez | ae51c81 | 2015-08-05 12:34:06 -0700 | [diff] [blame] | 365 | CPDF_Array* pCropBox = new CPDF_Array(); |
| 366 | pCropBox->Add(new CPDF_Number(rcOriginalCB.left)); |
| 367 | pCropBox->Add(new CPDF_Number(rcOriginalCB.bottom)); |
| 368 | pCropBox->Add(new CPDF_Number(rcOriginalCB.right)); |
| 369 | pCropBox->Add(new CPDF_Number(rcOriginalCB.top)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 370 | pPageDict->SetAt("ArtBox", pCropBox); |
| 371 | } |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 372 | |
Wei Li | 9b76113 | 2016-01-29 15:44:20 -0800 | [diff] [blame] | 373 | CPDF_Dictionary* pRes = pPageDict->GetDictBy("Resources"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 374 | if (!pRes) { |
Tom Sepez | ae51c81 | 2015-08-05 12:34:06 -0700 | [diff] [blame] | 375 | pRes = new CPDF_Dictionary; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 376 | pPageDict->SetAt("Resources", pRes); |
| 377 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 378 | |
Nico Weber | 077f1a3 | 2015-08-06 15:08:57 -0700 | [diff] [blame] | 379 | CPDF_Stream* pNewXObject = new CPDF_Stream(NULL, 0, new CPDF_Dictionary); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 380 | FX_DWORD dwObjNum = pDocument->AddIndirectObject(pNewXObject); |
Wei Li | 9b76113 | 2016-01-29 15:44:20 -0800 | [diff] [blame] | 381 | CPDF_Dictionary* pPageXObject = pRes->GetDictBy("XObject"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 382 | if (!pPageXObject) { |
Tom Sepez | ae51c81 | 2015-08-05 12:34:06 -0700 | [diff] [blame] | 383 | pPageXObject = new CPDF_Dictionary; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 384 | pRes->SetAt("XObject", pPageXObject); |
| 385 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 386 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 387 | CFX_ByteString key = ""; |
| 388 | int nStreams = ObjectArray.GetSize(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 389 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 390 | if (nStreams > 0) { |
| 391 | for (int iKey = 0; /*iKey < 100*/; iKey++) { |
| 392 | char sExtend[5] = {}; |
| 393 | FXSYS_itoa(iKey, sExtend, 10); |
| 394 | key = CFX_ByteString("FFT") + CFX_ByteString(sExtend); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 395 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 396 | if (!pPageXObject->KeyExist(key)) |
| 397 | break; |
| 398 | } |
| 399 | } |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 400 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 401 | SetPageContents(key, pPageDict, pDocument); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 402 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 403 | CPDF_Dictionary* pNewXORes = NULL; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 404 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 405 | if (!key.IsEmpty()) { |
| 406 | pPageXObject->SetAtReference(key, pDocument, dwObjNum); |
| 407 | CPDF_Dictionary* pNewOXbjectDic = pNewXObject->GetDict(); |
Tom Sepez | ae51c81 | 2015-08-05 12:34:06 -0700 | [diff] [blame] | 408 | pNewXORes = new CPDF_Dictionary; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 409 | pNewOXbjectDic->SetAt("Resources", pNewXORes); |
| 410 | pNewOXbjectDic->SetAtName("Type", "XObject"); |
| 411 | pNewOXbjectDic->SetAtName("Subtype", "Form"); |
| 412 | pNewOXbjectDic->SetAtInteger("FormType", 1); |
| 413 | pNewOXbjectDic->SetAtName("Name", "FRM"); |
Tom Sepez | 281a9ea | 2016-02-26 14:24:28 -0800 | [diff] [blame] | 414 | CFX_FloatRect rcBBox = pPageDict->GetRectBy("ArtBox"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 415 | pNewOXbjectDic->SetAtRect("BBox", rcBBox); |
| 416 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 417 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 418 | for (int i = 0; i < nStreams; i++) { |
| 419 | CPDF_Dictionary* pAnnotDic = ObjectArray.GetAt(i); |
| 420 | if (!pAnnotDic) |
| 421 | continue; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 422 | |
Tom Sepez | 281a9ea | 2016-02-26 14:24:28 -0800 | [diff] [blame] | 423 | CFX_FloatRect rcAnnot = pAnnotDic->GetRectBy("Rect"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 424 | rcAnnot.Normalize(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 425 | |
Wei Li | 9b76113 | 2016-01-29 15:44:20 -0800 | [diff] [blame] | 426 | CFX_ByteString sAnnotState = pAnnotDic->GetStringBy("AS"); |
| 427 | CPDF_Dictionary* pAnnotAP = pAnnotDic->GetDictBy("AP"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 428 | if (!pAnnotAP) |
| 429 | continue; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 430 | |
Wei Li | 9b76113 | 2016-01-29 15:44:20 -0800 | [diff] [blame] | 431 | CPDF_Stream* pAPStream = pAnnotAP->GetStreamBy("N"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 432 | if (!pAPStream) { |
Wei Li | 9b76113 | 2016-01-29 15:44:20 -0800 | [diff] [blame] | 433 | CPDF_Dictionary* pAPDic = pAnnotAP->GetDictBy("N"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 434 | if (!pAPDic) |
| 435 | continue; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 436 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 437 | if (!sAnnotState.IsEmpty()) { |
Wei Li | 9b76113 | 2016-01-29 15:44:20 -0800 | [diff] [blame] | 438 | pAPStream = pAPDic->GetStreamBy(sAnnotState); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 439 | } else { |
Oliver Chang | 3f1c71f | 2016-01-11 08:45:31 -0800 | [diff] [blame] | 440 | auto it = pAPDic->begin(); |
| 441 | if (it != pAPDic->end()) { |
| 442 | CPDF_Object* pFirstObj = it->second; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 443 | if (pFirstObj) { |
Dan Sinclair | bf81c14 | 2015-10-26 16:54:39 -0400 | [diff] [blame] | 444 | if (pFirstObj->IsReference()) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 445 | pFirstObj = pFirstObj->GetDirect(); |
Dan Sinclair | aa435ba | 2015-10-22 16:45:48 -0400 | [diff] [blame] | 446 | if (!pFirstObj->IsStream()) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 447 | continue; |
Dan Sinclair | aa435ba | 2015-10-22 16:45:48 -0400 | [diff] [blame] | 448 | pAPStream = pFirstObj->AsStream(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 449 | } |
| 450 | } |
| 451 | } |
| 452 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 453 | if (!pAPStream) |
| 454 | continue; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 455 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 456 | CPDF_Dictionary* pAPDic = pAPStream->GetDict(); |
Wei Li | 9b76113 | 2016-01-29 15:44:20 -0800 | [diff] [blame] | 457 | CFX_Matrix matrix = pAPDic->GetMatrixBy("Matrix"); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 458 | |
Tom Sepez | 281a9ea | 2016-02-26 14:24:28 -0800 | [diff] [blame] | 459 | CFX_FloatRect rcStream; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 460 | if (pAPDic->KeyExist("Rect")) |
Wei Li | 9b76113 | 2016-01-29 15:44:20 -0800 | [diff] [blame] | 461 | rcStream = pAPDic->GetRectBy("Rect"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 462 | else if (pAPDic->KeyExist("BBox")) |
Wei Li | 9b76113 | 2016-01-29 15:44:20 -0800 | [diff] [blame] | 463 | rcStream = pAPDic->GetRectBy("BBox"); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 464 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 465 | if (rcStream.IsEmpty()) |
| 466 | continue; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 467 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 468 | CPDF_Object* pObj = pAPStream; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 469 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 470 | if (pObj) { |
| 471 | CPDF_Dictionary* pObjDic = pObj->GetDict(); |
| 472 | if (pObjDic) { |
| 473 | pObjDic->SetAtName("Type", "XObject"); |
| 474 | pObjDic->SetAtName("Subtype", "Form"); |
| 475 | } |
| 476 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 477 | |
Wei Li | 9b76113 | 2016-01-29 15:44:20 -0800 | [diff] [blame] | 478 | CPDF_Dictionary* pXObject = pNewXORes->GetDictBy("XObject"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 479 | if (!pXObject) { |
Tom Sepez | ae51c81 | 2015-08-05 12:34:06 -0700 | [diff] [blame] | 480 | pXObject = new CPDF_Dictionary; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 481 | pNewXORes->SetAt("XObject", pXObject); |
| 482 | } |
| 483 | |
| 484 | CFX_ByteString sFormName; |
| 485 | sFormName.Format("F%d", i); |
| 486 | FX_DWORD dwObjNum = pDocument->AddIndirectObject(pObj); |
| 487 | pXObject->SetAtReference(sFormName, pDocument, dwObjNum); |
| 488 | |
| 489 | CPDF_StreamAcc acc; |
| 490 | acc.LoadAllData(pNewXObject); |
| 491 | |
| 492 | const uint8_t* pData = acc.GetData(); |
| 493 | CFX_ByteString sStream(pData, acc.GetSize()); |
| 494 | CFX_ByteString sTemp; |
| 495 | |
| 496 | if (matrix.IsIdentity()) { |
| 497 | matrix.a = 1.0f; |
| 498 | matrix.b = 0.0f; |
| 499 | matrix.c = 0.0f; |
| 500 | matrix.d = 1.0f; |
| 501 | matrix.e = 0.0f; |
| 502 | matrix.f = 0.0f; |
| 503 | } |
| 504 | |
Tom Sepez | 60d909e | 2015-12-10 15:34:55 -0800 | [diff] [blame] | 505 | CFX_Matrix m = GetMatrix(rcAnnot, rcStream, matrix); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 506 | sTemp.Format("q %f 0 0 %f %f %f cm /%s Do Q\n", m.a, m.d, m.e, m.f, |
| 507 | sFormName.c_str()); |
| 508 | sStream += sTemp; |
| 509 | |
| 510 | pNewXObject->SetData((const uint8_t*)sStream, sStream.GetLength(), FALSE, |
| 511 | FALSE); |
| 512 | } |
| 513 | pPageDict->RemoveAt("Annots"); |
| 514 | |
| 515 | ObjectArray.RemoveAll(); |
| 516 | RectArray.RemoveAll(); |
| 517 | |
| 518 | return FLATTEN_SUCCESS; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 519 | } |