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