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 | |
dsinclair | 8a752ab | 2016-09-29 11:59:54 -0700 | [diff] [blame] | 11 | #include "core/fpdfapi/fpdf_page/cpdf_page.h" |
| 12 | #include "core/fpdfapi/fpdf_page/cpdf_pageobject.h" |
dsinclair | c6c425a | 2016-09-29 12:01:30 -0700 | [diff] [blame] | 13 | #include "core/fpdfapi/fpdf_parser/cpdf_array.h" |
| 14 | #include "core/fpdfapi/fpdf_parser/cpdf_document.h" |
| 15 | #include "core/fpdfapi/fpdf_parser/cpdf_number.h" |
| 16 | #include "core/fpdfapi/fpdf_parser/cpdf_stream.h" |
| 17 | #include "core/fpdfapi/fpdf_parser/cpdf_stream_acc.h" |
dsinclair | 1727aee | 2016-09-29 13:12:56 -0700 | [diff] [blame^] | 18 | #include "core/fpdfdoc/cpdf_annot.h" |
Lei Zhang | bde53d2 | 2015-11-12 22:21:30 -0800 | [diff] [blame] | 19 | #include "fpdfsdk/include/fsdk_define.h" |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 20 | |
| 21 | typedef CFX_ArrayTemplate<CPDF_Dictionary*> CPDF_ObjectArray; |
Tom Sepez | 281a9ea | 2016-02-26 14:24:28 -0800 | [diff] [blame] | 22 | typedef CFX_ArrayTemplate<CFX_FloatRect> CPDF_RectArray; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 23 | |
| 24 | enum FPDF_TYPE { MAX, MIN }; |
| 25 | enum FPDF_VALUE { TOP, LEFT, RIGHT, BOTTOM }; |
| 26 | |
Tom Sepez | 281a9ea | 2016-02-26 14:24:28 -0800 | [diff] [blame] | 27 | FX_BOOL IsValiableRect(CFX_FloatRect rect, CFX_FloatRect rcPage) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 28 | if (rect.left - rect.right > 0.000001f || rect.bottom - rect.top > 0.000001f) |
| 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 (rect.left == 0.0f && rect.top == 0.0f && rect.right == 0.0f && |
| 32 | rect.bottom == 0.0f) |
| 33 | return FALSE; |
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 | if (!rcPage.IsEmpty()) { |
| 36 | if (rect.left - rcPage.left < -10.000001f || |
| 37 | rect.right - rcPage.right > 10.000001f || |
| 38 | rect.top - rcPage.top > 10.000001f || |
| 39 | rect.bottom - rcPage.bottom < -10.000001f) |
| 40 | return FALSE; |
| 41 | } |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 42 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 43 | return TRUE; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 44 | } |
| 45 | |
Tom Sepez | 2398d89 | 2016-02-17 16:46:26 -0800 | [diff] [blame] | 46 | void GetContentsRect(CPDF_Document* pDoc, |
| 47 | CPDF_Dictionary* pDict, |
| 48 | CPDF_RectArray* pRectArray) { |
thestig | 5cc2465 | 2016-04-26 11:46:02 -0700 | [diff] [blame] | 49 | std::unique_ptr<CPDF_Page> pPDFPage(new CPDF_Page(pDoc, pDict, false)); |
| 50 | pPDFPage->ParseContent(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 51 | |
thestig | 2fad11a | 2016-06-16 16:39:25 -0700 | [diff] [blame] | 52 | for (const auto& pPageObject : *pPDFPage->GetPageObjectList()) { |
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; |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 58 | if (IsValiableRect(rc, pDict->GetRectFor("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")) |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 71 | rect = pStream->GetRectFor("Rect"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 72 | else if (pStream->KeyExist("BBox")) |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 73 | rect = pStream->GetRectFor("BBox"); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 74 | |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 75 | if (IsValiableRect(rect, pPageDic->GetRectFor("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); |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 90 | CPDF_Array* pAnnots = pPageDic->GetArrayFor("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 | |
tsepez | c3255f5 | 2016-03-25 14:52:27 -0700 | [diff] [blame] | 94 | uint32_t dwSize = pAnnots->GetCount(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 95 | for (int i = 0; i < (int)dwSize; i++) { |
tsepez | bd56755 | 2016-03-29 14:51:50 -0700 | [diff] [blame] | 96 | CPDF_Dictionary* pAnnotDic = ToDictionary(pAnnots->GetDirectObjectAt(i)); |
Dan Sinclair | f1251c1 | 2015-10-20 16:24:45 -0400 | [diff] [blame] | 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 | |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 100 | CFX_ByteString sSubtype = pAnnotDic->GetStringFor("Subtype"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 101 | if (sSubtype == "Popup") |
| 102 | continue; |
| 103 | |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 104 | int nAnnotFlag = pAnnotDic->GetIntegerFor("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: |
weili | 12367cb | 2016-06-03 11:22:16 -0700 | [diff] [blame] | 157 | // Not reachable. |
| 158 | return 0.0f; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 159 | } |
| 160 | fRet = pArray[0]; |
| 161 | if (type == MAX) { |
| 162 | for (int i = 1; i < nRects; i++) |
| 163 | if (fRet <= pArray[i]) |
| 164 | fRet = pArray[i]; |
| 165 | } else { |
| 166 | for (int i = 1; i < nRects; i++) |
| 167 | if (fRet >= pArray[i]) |
| 168 | fRet = pArray[i]; |
| 169 | } |
| 170 | delete[] pArray; |
| 171 | return fRet; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 172 | } |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 173 | |
Tom Sepez | 281a9ea | 2016-02-26 14:24:28 -0800 | [diff] [blame] | 174 | CFX_FloatRect CalculateRect(CPDF_RectArray* pRectArray) { |
| 175 | CFX_FloatRect rcRet; |
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 | rcRet.left = GetMinMaxValue(*pRectArray, MIN, LEFT); |
| 178 | rcRet.top = GetMinMaxValue(*pRectArray, MAX, TOP); |
| 179 | rcRet.right = GetMinMaxValue(*pRectArray, MAX, RIGHT); |
| 180 | rcRet.bottom = GetMinMaxValue(*pRectArray, MIN, BOTTOM); |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 181 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 182 | return rcRet; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 183 | } |
| 184 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 185 | void SetPageContents(CFX_ByteString key, |
| 186 | CPDF_Dictionary* pPage, |
| 187 | CPDF_Document* pDocument) { |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 188 | CPDF_Object* pContentsObj = pPage->GetStreamFor("Contents"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 189 | if (!pContentsObj) { |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 190 | pContentsObj = pPage->GetArrayFor("Contents"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 191 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 192 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 193 | if (!pContentsObj) { |
| 194 | // Create a new contents dictionary |
| 195 | if (!key.IsEmpty()) { |
tsepez | 698c571 | 2016-09-28 16:47:07 -0700 | [diff] [blame] | 196 | CPDF_Stream* pNewContents = new CPDF_Stream( |
| 197 | nullptr, 0, new CPDF_Dictionary(pDocument->GetByteStringPool())); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 198 | CFX_ByteString sStream; |
| 199 | sStream.Format("q 1 0 0 1 0 0 cm /%s Do Q", key.c_str()); |
tsepez | e6db16e | 2016-09-19 10:45:09 -0700 | [diff] [blame] | 200 | pNewContents->SetData(sStream.raw_str(), sStream.GetLength()); |
tsepez | bb577af | 2016-09-21 19:10:19 -0700 | [diff] [blame] | 201 | pPage->SetReferenceFor("Contents", pDocument, |
| 202 | pDocument->AddIndirectObject(pNewContents)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 203 | } |
| 204 | return; |
| 205 | } |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 206 | |
thestig | 1cd352e | 2016-06-07 17:53:06 -0700 | [diff] [blame] | 207 | CPDF_Array* pContentsArray = nullptr; |
Tom Sepez | 8e5cd19 | 2016-01-26 13:20:26 -0800 | [diff] [blame] | 208 | switch (pContentsObj->GetType()) { |
| 209 | case CPDF_Object::STREAM: { |
Tom Sepez | ae51c81 | 2015-08-05 12:34:06 -0700 | [diff] [blame] | 210 | pContentsArray = new CPDF_Array; |
Dan Sinclair | aa435ba | 2015-10-22 16:45:48 -0400 | [diff] [blame] | 211 | CPDF_Stream* pContents = pContentsObj->AsStream(); |
tsepez | c3255f5 | 2016-03-25 14:52:27 -0700 | [diff] [blame] | 212 | uint32_t dwObjNum = pDocument->AddIndirectObject(pContents); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 213 | CPDF_StreamAcc acc; |
| 214 | acc.LoadAllData(pContents); |
| 215 | CFX_ByteString sStream = "q\n"; |
| 216 | CFX_ByteString sBody = |
| 217 | CFX_ByteString((const FX_CHAR*)acc.GetData(), acc.GetSize()); |
| 218 | sStream = sStream + sBody + "\nQ"; |
tsepez | e6db16e | 2016-09-19 10:45:09 -0700 | [diff] [blame] | 219 | pContents->SetData(sStream.raw_str(), sStream.GetLength()); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 220 | pContentsArray->AddReference(pDocument, dwObjNum); |
| 221 | break; |
| 222 | } |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 223 | |
Tom Sepez | 8e5cd19 | 2016-01-26 13:20:26 -0800 | [diff] [blame] | 224 | case CPDF_Object::ARRAY: { |
Dan Sinclair | 2b11dc1 | 2015-10-22 15:02:06 -0400 | [diff] [blame] | 225 | pContentsArray = pContentsObj->AsArray(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 226 | break; |
| 227 | } |
| 228 | default: |
| 229 | break; |
| 230 | } |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 231 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 232 | if (!pContentsArray) |
| 233 | return; |
| 234 | |
tsepez | bb577af | 2016-09-21 19:10:19 -0700 | [diff] [blame] | 235 | pPage->SetReferenceFor("Contents", pDocument, |
| 236 | pDocument->AddIndirectObject(pContentsArray)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 237 | |
| 238 | if (!key.IsEmpty()) { |
tsepez | 698c571 | 2016-09-28 16:47:07 -0700 | [diff] [blame] | 239 | CPDF_Stream* pNewContents = new CPDF_Stream( |
| 240 | nullptr, 0, new CPDF_Dictionary(pDocument->GetByteStringPool())); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 241 | CFX_ByteString sStream; |
| 242 | sStream.Format("q 1 0 0 1 0 0 cm /%s Do Q", key.c_str()); |
tsepez | e6db16e | 2016-09-19 10:45:09 -0700 | [diff] [blame] | 243 | pNewContents->SetData(sStream.raw_str(), sStream.GetLength()); |
tsepez | bb577af | 2016-09-21 19:10:19 -0700 | [diff] [blame] | 244 | pContentsArray->AddReference(pDocument, |
| 245 | pDocument->AddIndirectObject(pNewContents)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 246 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 247 | } |
| 248 | |
Tom Sepez | 281a9ea | 2016-02-26 14:24:28 -0800 | [diff] [blame] | 249 | CFX_Matrix GetMatrix(CFX_FloatRect rcAnnot, |
| 250 | CFX_FloatRect rcStream, |
Tom Sepez | 60d909e | 2015-12-10 15:34:55 -0800 | [diff] [blame] | 251 | const CFX_Matrix& matrix) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 252 | if (rcStream.IsEmpty()) |
Tom Sepez | 60d909e | 2015-12-10 15:34:55 -0800 | [diff] [blame] | 253 | return CFX_Matrix(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 254 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 255 | matrix.TransformRect(rcStream); |
| 256 | rcStream.Normalize(); |
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 | FX_FLOAT a = rcAnnot.Width() / rcStream.Width(); |
| 259 | FX_FLOAT d = rcAnnot.Height() / rcStream.Height(); |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 260 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 261 | FX_FLOAT e = rcAnnot.left - rcStream.left * a; |
| 262 | FX_FLOAT f = rcAnnot.bottom - rcStream.bottom * d; |
Tom Sepez | 60d909e | 2015-12-10 15:34:55 -0800 | [diff] [blame] | 263 | return CFX_Matrix(a, 0, 0, d, e, f); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 264 | } |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 265 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 266 | void GetOffset(FX_FLOAT& fa, |
| 267 | FX_FLOAT& fd, |
| 268 | FX_FLOAT& fe, |
| 269 | FX_FLOAT& ff, |
Tom Sepez | 281a9ea | 2016-02-26 14:24:28 -0800 | [diff] [blame] | 270 | CFX_FloatRect rcAnnot, |
| 271 | CFX_FloatRect rcStream, |
Tom Sepez | 60d909e | 2015-12-10 15:34:55 -0800 | [diff] [blame] | 272 | const CFX_Matrix& matrix) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 273 | FX_FLOAT fStreamWidth = 0.0f; |
| 274 | FX_FLOAT fStreamHeight = 0.0f; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 275 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 276 | if (matrix.a != 0 && matrix.d != 0) { |
| 277 | fStreamWidth = rcStream.right - rcStream.left; |
| 278 | fStreamHeight = rcStream.top - rcStream.bottom; |
| 279 | } else { |
| 280 | fStreamWidth = rcStream.top - rcStream.bottom; |
| 281 | fStreamHeight = rcStream.right - rcStream.left; |
| 282 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 283 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 284 | FX_FLOAT x1 = |
| 285 | matrix.a * rcStream.left + matrix.c * rcStream.bottom + matrix.e; |
| 286 | FX_FLOAT y1 = |
| 287 | matrix.b * rcStream.left + matrix.d * rcStream.bottom + matrix.f; |
| 288 | FX_FLOAT x2 = matrix.a * rcStream.left + matrix.c * rcStream.top + matrix.e; |
| 289 | FX_FLOAT y2 = matrix.b * rcStream.left + matrix.d * rcStream.top + matrix.f; |
| 290 | FX_FLOAT x3 = |
| 291 | matrix.a * rcStream.right + matrix.c * rcStream.bottom + matrix.e; |
| 292 | FX_FLOAT y3 = |
| 293 | matrix.b * rcStream.right + matrix.d * rcStream.bottom + matrix.f; |
| 294 | FX_FLOAT x4 = matrix.a * rcStream.right + matrix.c * rcStream.top + matrix.e; |
| 295 | 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] | 296 | |
Lei Zhang | 375a864 | 2016-01-11 11:59:17 -0800 | [diff] [blame] | 297 | FX_FLOAT left = std::min(std::min(x1, x2), std::min(x3, x4)); |
| 298 | 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] | 299 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 300 | fa = (rcAnnot.right - rcAnnot.left) / fStreamWidth; |
| 301 | fd = (rcAnnot.top - rcAnnot.bottom) / fStreamHeight; |
| 302 | fe = rcAnnot.left - left * fa; |
| 303 | ff = rcAnnot.bottom - bottom * fd; |
| 304 | } |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 305 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 306 | DLLEXPORT int STDCALL FPDFPage_Flatten(FPDF_PAGE page, int nFlag) { |
Tom Sepez | db0be96 | 2015-10-16 14:00:21 -0700 | [diff] [blame] | 307 | CPDF_Page* pPage = CPDFPageFromFPDFPage(page); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 308 | if (!page) { |
| 309 | return FLATTEN_FAIL; |
| 310 | } |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 311 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 312 | CPDF_Document* pDocument = pPage->m_pDocument; |
| 313 | CPDF_Dictionary* pPageDict = pPage->m_pFormDict; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 314 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 315 | if (!pDocument || !pPageDict) { |
| 316 | return FLATTEN_FAIL; |
| 317 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 318 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 319 | CPDF_ObjectArray ObjectArray; |
| 320 | CPDF_RectArray RectArray; |
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 | int iRet = FLATTEN_FAIL; |
| 323 | iRet = ParserAnnots(pDocument, pPageDict, &RectArray, &ObjectArray, nFlag); |
| 324 | if (iRet == FLATTEN_NOTHINGTODO || iRet == FLATTEN_FAIL) |
| 325 | return iRet; |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 326 | |
Tom Sepez | 281a9ea | 2016-02-26 14:24:28 -0800 | [diff] [blame] | 327 | CFX_FloatRect rcOriginalCB; |
| 328 | CFX_FloatRect rcMerger = CalculateRect(&RectArray); |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 329 | CFX_FloatRect rcOriginalMB = pPageDict->GetRectFor("MediaBox"); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 330 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 331 | if (pPageDict->KeyExist("CropBox")) |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 332 | rcOriginalMB = pPageDict->GetRectFor("CropBox"); |
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 (rcOriginalMB.IsEmpty()) { |
Tom Sepez | 281a9ea | 2016-02-26 14:24:28 -0800 | [diff] [blame] | 335 | rcOriginalMB = CFX_FloatRect(0.0f, 0.0f, 612.0f, 792.0f); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 336 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 337 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 338 | rcMerger.left = |
| 339 | rcMerger.left < rcOriginalMB.left ? rcOriginalMB.left : rcMerger.left; |
| 340 | rcMerger.right = |
| 341 | rcMerger.right > rcOriginalMB.right ? rcOriginalMB.right : rcMerger.right; |
| 342 | rcMerger.top = |
| 343 | rcMerger.top > rcOriginalMB.top ? rcOriginalMB.top : rcMerger.top; |
| 344 | rcMerger.bottom = rcMerger.bottom < rcOriginalMB.bottom ? rcOriginalMB.bottom |
| 345 | : rcMerger.bottom; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 346 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 347 | if (pPageDict->KeyExist("ArtBox")) |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 348 | rcOriginalCB = pPageDict->GetRectFor("ArtBox"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 349 | else |
| 350 | rcOriginalCB = rcOriginalMB; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 351 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 352 | if (!rcOriginalMB.IsEmpty()) { |
Tom Sepez | ae51c81 | 2015-08-05 12:34:06 -0700 | [diff] [blame] | 353 | CPDF_Array* pMediaBox = new CPDF_Array(); |
Tom Sepez | ae51c81 | 2015-08-05 12:34:06 -0700 | [diff] [blame] | 354 | pMediaBox->Add(new CPDF_Number(rcOriginalMB.left)); |
| 355 | pMediaBox->Add(new CPDF_Number(rcOriginalMB.bottom)); |
| 356 | pMediaBox->Add(new CPDF_Number(rcOriginalMB.right)); |
| 357 | pMediaBox->Add(new CPDF_Number(rcOriginalMB.top)); |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 358 | pPageDict->SetFor("MediaBox", pMediaBox); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 359 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 360 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 361 | if (!rcOriginalCB.IsEmpty()) { |
Tom Sepez | ae51c81 | 2015-08-05 12:34:06 -0700 | [diff] [blame] | 362 | CPDF_Array* pCropBox = new CPDF_Array(); |
| 363 | pCropBox->Add(new CPDF_Number(rcOriginalCB.left)); |
| 364 | pCropBox->Add(new CPDF_Number(rcOriginalCB.bottom)); |
| 365 | pCropBox->Add(new CPDF_Number(rcOriginalCB.right)); |
| 366 | pCropBox->Add(new CPDF_Number(rcOriginalCB.top)); |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 367 | pPageDict->SetFor("ArtBox", pCropBox); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 368 | } |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 369 | |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 370 | CPDF_Dictionary* pRes = pPageDict->GetDictFor("Resources"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 371 | if (!pRes) { |
tsepez | 698c571 | 2016-09-28 16:47:07 -0700 | [diff] [blame] | 372 | pRes = new CPDF_Dictionary(pDocument->GetByteStringPool()); |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 373 | pPageDict->SetFor("Resources", pRes); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 374 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 375 | |
tsepez | 698c571 | 2016-09-28 16:47:07 -0700 | [diff] [blame] | 376 | CPDF_Stream* pNewXObject = new CPDF_Stream( |
| 377 | nullptr, 0, new CPDF_Dictionary(pDocument->GetByteStringPool())); |
| 378 | |
tsepez | c3255f5 | 2016-03-25 14:52:27 -0700 | [diff] [blame] | 379 | uint32_t dwObjNum = pDocument->AddIndirectObject(pNewXObject); |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 380 | CPDF_Dictionary* pPageXObject = pRes->GetDictFor("XObject"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 381 | if (!pPageXObject) { |
tsepez | 698c571 | 2016-09-28 16:47:07 -0700 | [diff] [blame] | 382 | pPageXObject = new CPDF_Dictionary(pDocument->GetByteStringPool()); |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 383 | pRes->SetFor("XObject", pPageXObject); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 384 | } |
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 | CFX_ByteString key = ""; |
| 387 | int nStreams = ObjectArray.GetSize(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 388 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 389 | if (nStreams > 0) { |
| 390 | for (int iKey = 0; /*iKey < 100*/; iKey++) { |
| 391 | char sExtend[5] = {}; |
| 392 | FXSYS_itoa(iKey, sExtend, 10); |
| 393 | key = CFX_ByteString("FFT") + CFX_ByteString(sExtend); |
tsepez | 7b1ccf9 | 2016-04-14 11:04:57 -0700 | [diff] [blame] | 394 | if (!pPageXObject->KeyExist(key)) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 395 | break; |
| 396 | } |
| 397 | } |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 398 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 399 | SetPageContents(key, pPageDict, pDocument); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 400 | |
thestig | 1cd352e | 2016-06-07 17:53:06 -0700 | [diff] [blame] | 401 | CPDF_Dictionary* pNewXORes = nullptr; |
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 | if (!key.IsEmpty()) { |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 404 | pPageXObject->SetReferenceFor(key, pDocument, dwObjNum); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 405 | CPDF_Dictionary* pNewOXbjectDic = pNewXObject->GetDict(); |
tsepez | 698c571 | 2016-09-28 16:47:07 -0700 | [diff] [blame] | 406 | pNewXORes = new CPDF_Dictionary(pDocument->GetByteStringPool()); |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 407 | pNewOXbjectDic->SetFor("Resources", pNewXORes); |
| 408 | pNewOXbjectDic->SetNameFor("Type", "XObject"); |
| 409 | pNewOXbjectDic->SetNameFor("Subtype", "Form"); |
| 410 | pNewOXbjectDic->SetIntegerFor("FormType", 1); |
| 411 | pNewOXbjectDic->SetNameFor("Name", "FRM"); |
| 412 | CFX_FloatRect rcBBox = pPageDict->GetRectFor("ArtBox"); |
| 413 | pNewOXbjectDic->SetRectFor("BBox", rcBBox); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 414 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 415 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 416 | for (int i = 0; i < nStreams; i++) { |
| 417 | CPDF_Dictionary* pAnnotDic = ObjectArray.GetAt(i); |
| 418 | if (!pAnnotDic) |
| 419 | continue; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 420 | |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 421 | CFX_FloatRect rcAnnot = pAnnotDic->GetRectFor("Rect"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 422 | rcAnnot.Normalize(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 423 | |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 424 | CFX_ByteString sAnnotState = pAnnotDic->GetStringFor("AS"); |
| 425 | CPDF_Dictionary* pAnnotAP = pAnnotDic->GetDictFor("AP"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 426 | if (!pAnnotAP) |
| 427 | continue; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 428 | |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 429 | CPDF_Stream* pAPStream = pAnnotAP->GetStreamFor("N"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 430 | if (!pAPStream) { |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 431 | CPDF_Dictionary* pAPDic = pAnnotAP->GetDictFor("N"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 432 | if (!pAPDic) |
| 433 | continue; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 434 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 435 | if (!sAnnotState.IsEmpty()) { |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 436 | pAPStream = pAPDic->GetStreamFor(sAnnotState); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 437 | } else { |
Oliver Chang | 3f1c71f | 2016-01-11 08:45:31 -0800 | [diff] [blame] | 438 | auto it = pAPDic->begin(); |
| 439 | if (it != pAPDic->end()) { |
| 440 | CPDF_Object* pFirstObj = it->second; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 441 | if (pFirstObj) { |
Dan Sinclair | bf81c14 | 2015-10-26 16:54:39 -0400 | [diff] [blame] | 442 | if (pFirstObj->IsReference()) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 443 | pFirstObj = pFirstObj->GetDirect(); |
Dan Sinclair | aa435ba | 2015-10-22 16:45:48 -0400 | [diff] [blame] | 444 | if (!pFirstObj->IsStream()) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 445 | continue; |
Dan Sinclair | aa435ba | 2015-10-22 16:45:48 -0400 | [diff] [blame] | 446 | pAPStream = pFirstObj->AsStream(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 447 | } |
| 448 | } |
| 449 | } |
| 450 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 451 | if (!pAPStream) |
| 452 | continue; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 453 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 454 | CPDF_Dictionary* pAPDic = pAPStream->GetDict(); |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 455 | CFX_Matrix matrix = pAPDic->GetMatrixFor("Matrix"); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 456 | |
Tom Sepez | 281a9ea | 2016-02-26 14:24:28 -0800 | [diff] [blame] | 457 | CFX_FloatRect rcStream; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 458 | if (pAPDic->KeyExist("Rect")) |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 459 | rcStream = pAPDic->GetRectFor("Rect"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 460 | else if (pAPDic->KeyExist("BBox")) |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 461 | rcStream = pAPDic->GetRectFor("BBox"); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 462 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 463 | if (rcStream.IsEmpty()) |
| 464 | continue; |
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 | CPDF_Object* pObj = pAPStream; |
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 | if (pObj) { |
| 469 | CPDF_Dictionary* pObjDic = pObj->GetDict(); |
| 470 | if (pObjDic) { |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 471 | pObjDic->SetNameFor("Type", "XObject"); |
| 472 | pObjDic->SetNameFor("Subtype", "Form"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 473 | } |
| 474 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 475 | |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 476 | CPDF_Dictionary* pXObject = pNewXORes->GetDictFor("XObject"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 477 | if (!pXObject) { |
tsepez | 698c571 | 2016-09-28 16:47:07 -0700 | [diff] [blame] | 478 | pXObject = new CPDF_Dictionary(pDocument->GetByteStringPool()); |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 479 | pNewXORes->SetFor("XObject", pXObject); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | CFX_ByteString sFormName; |
| 483 | sFormName.Format("F%d", i); |
tsepez | bb577af | 2016-09-21 19:10:19 -0700 | [diff] [blame] | 484 | pXObject->SetReferenceFor(sFormName, pDocument, |
| 485 | pDocument->AddIndirectObject(pObj)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 486 | |
| 487 | CPDF_StreamAcc acc; |
| 488 | acc.LoadAllData(pNewXObject); |
| 489 | |
| 490 | const uint8_t* pData = acc.GetData(); |
| 491 | CFX_ByteString sStream(pData, acc.GetSize()); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 492 | |
| 493 | if (matrix.IsIdentity()) { |
| 494 | matrix.a = 1.0f; |
| 495 | matrix.b = 0.0f; |
| 496 | matrix.c = 0.0f; |
| 497 | matrix.d = 1.0f; |
| 498 | matrix.e = 0.0f; |
| 499 | matrix.f = 0.0f; |
| 500 | } |
| 501 | |
tsepez | bb577af | 2016-09-21 19:10:19 -0700 | [diff] [blame] | 502 | CFX_ByteString sTemp; |
Tom Sepez | 60d909e | 2015-12-10 15:34:55 -0800 | [diff] [blame] | 503 | CFX_Matrix m = GetMatrix(rcAnnot, rcStream, matrix); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 504 | sTemp.Format("q %f 0 0 %f %f %f cm /%s Do Q\n", m.a, m.d, m.e, m.f, |
| 505 | sFormName.c_str()); |
| 506 | sStream += sTemp; |
tsepez | e6db16e | 2016-09-19 10:45:09 -0700 | [diff] [blame] | 507 | pNewXObject->SetData(sStream.raw_str(), sStream.GetLength()); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 508 | } |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 509 | pPageDict->RemoveFor("Annots"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 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 | } |