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> |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame] | 10 | #include <memory> |
| 11 | #include <utility> |
| 12 | #include <vector> |
Lei Zhang | 375a864 | 2016-01-11 11:59:17 -0800 | [diff] [blame] | 13 | |
dsinclair | 41872fa | 2016-10-04 11:29:35 -0700 | [diff] [blame] | 14 | #include "core/fpdfapi/page/cpdf_page.h" |
| 15 | #include "core/fpdfapi/page/cpdf_pageobject.h" |
dsinclair | 488b7ad | 2016-10-04 11:55:50 -0700 | [diff] [blame] | 16 | #include "core/fpdfapi/parser/cpdf_array.h" |
| 17 | #include "core/fpdfapi/parser/cpdf_document.h" |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame] | 18 | #include "core/fpdfapi/parser/cpdf_name.h" |
dsinclair | 488b7ad | 2016-10-04 11:55:50 -0700 | [diff] [blame] | 19 | #include "core/fpdfapi/parser/cpdf_number.h" |
tsepez | 8a3aa45 | 2016-11-16 12:26:06 -0800 | [diff] [blame] | 20 | #include "core/fpdfapi/parser/cpdf_reference.h" |
dsinclair | 488b7ad | 2016-10-04 11:55:50 -0700 | [diff] [blame] | 21 | #include "core/fpdfapi/parser/cpdf_stream.h" |
| 22 | #include "core/fpdfapi/parser/cpdf_stream_acc.h" |
dsinclair | 1727aee | 2016-09-29 13:12:56 -0700 | [diff] [blame] | 23 | #include "core/fpdfdoc/cpdf_annot.h" |
dsinclair | 114e46a | 2016-09-29 17:18:21 -0700 | [diff] [blame] | 24 | #include "fpdfsdk/fsdk_define.h" |
tsepez | 74b8c6e | 2016-10-12 09:38:41 -0700 | [diff] [blame] | 25 | #include "third_party/base/stl_util.h" |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 26 | |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 27 | enum FPDF_TYPE { MAX, MIN }; |
| 28 | enum FPDF_VALUE { TOP, LEFT, RIGHT, BOTTOM }; |
| 29 | |
tsepez | 381fc83 | 2016-10-10 14:06:44 -0700 | [diff] [blame] | 30 | namespace { |
| 31 | |
Lei Zhang | d361005 | 2017-06-19 12:25:54 -0700 | [diff] [blame] | 32 | bool IsValidRect(const CFX_FloatRect& rect, const CFX_FloatRect& rcPage) { |
| 33 | constexpr float kMinSize = 0.000001f; |
| 34 | if (rect.IsEmpty() || rect.Width() < kMinSize || rect.Height() < kMinSize) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 35 | return false; |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 36 | |
Lei Zhang | d361005 | 2017-06-19 12:25:54 -0700 | [diff] [blame] | 37 | if (rcPage.IsEmpty()) |
| 38 | return true; |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 39 | |
Lei Zhang | d361005 | 2017-06-19 12:25:54 -0700 | [diff] [blame] | 40 | constexpr float kMinBorderSize = 10.000001f; |
| 41 | return rect.left - rcPage.left >= -kMinBorderSize && |
| 42 | rect.right - rcPage.right <= kMinBorderSize && |
| 43 | rect.top - rcPage.top <= kMinBorderSize && |
| 44 | rect.bottom - rcPage.bottom >= -kMinBorderSize; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 45 | } |
| 46 | |
Tom Sepez | 2398d89 | 2016-02-17 16:46:26 -0800 | [diff] [blame] | 47 | void GetContentsRect(CPDF_Document* pDoc, |
| 48 | CPDF_Dictionary* pDict, |
tsepez | 6173c9d | 2016-11-09 16:38:03 -0800 | [diff] [blame] | 49 | std::vector<CFX_FloatRect>* pRectArray) { |
Dan Sinclair | 0bb1333 | 2017-03-30 16:12:02 -0400 | [diff] [blame] | 50 | auto pPDFPage = pdfium::MakeUnique<CPDF_Page>(pDoc, pDict, false); |
thestig | 5cc2465 | 2016-04-26 11:46:02 -0700 | [diff] [blame] | 51 | pPDFPage->ParseContent(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 52 | |
thestig | 2fad11a | 2016-06-16 16:39:25 -0700 | [diff] [blame] | 53 | for (const auto& pPageObject : *pPDFPage->GetPageObjectList()) { |
Tom Sepez | 281a9ea | 2016-02-26 14:24:28 -0800 | [diff] [blame] | 54 | CFX_FloatRect rc; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 55 | rc.left = pPageObject->m_Left; |
| 56 | rc.right = pPageObject->m_Right; |
| 57 | rc.bottom = pPageObject->m_Bottom; |
| 58 | rc.top = pPageObject->m_Top; |
Lei Zhang | d361005 | 2017-06-19 12:25:54 -0700 | [diff] [blame] | 59 | if (IsValidRect(rc, pDict->GetRectFor("MediaBox"))) |
tsepez | 6173c9d | 2016-11-09 16:38:03 -0800 | [diff] [blame] | 60 | pRectArray->push_back(rc); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 61 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 64 | void ParserStream(CPDF_Dictionary* pPageDic, |
| 65 | CPDF_Dictionary* pStream, |
tsepez | 6173c9d | 2016-11-09 16:38:03 -0800 | [diff] [blame] | 66 | std::vector<CFX_FloatRect>* pRectArray, |
tsepez | 74b8c6e | 2016-10-12 09:38:41 -0700 | [diff] [blame] | 67 | std::vector<CPDF_Dictionary*>* pObjectArray) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 68 | if (!pStream) |
| 69 | return; |
Tom Sepez | 281a9ea | 2016-02-26 14:24:28 -0800 | [diff] [blame] | 70 | CFX_FloatRect rect; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 71 | if (pStream->KeyExist("Rect")) |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 72 | rect = pStream->GetRectFor("Rect"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 73 | else if (pStream->KeyExist("BBox")) |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 74 | rect = pStream->GetRectFor("BBox"); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 75 | |
Lei Zhang | d361005 | 2017-06-19 12:25:54 -0700 | [diff] [blame] | 76 | if (IsValidRect(rect, pPageDic->GetRectFor("MediaBox"))) |
tsepez | 6173c9d | 2016-11-09 16:38:03 -0800 | [diff] [blame] | 77 | pRectArray->push_back(rect); |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 78 | |
tsepez | 74b8c6e | 2016-10-12 09:38:41 -0700 | [diff] [blame] | 79 | pObjectArray->push_back(pStream); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 82 | int ParserAnnots(CPDF_Document* pSourceDoc, |
| 83 | CPDF_Dictionary* pPageDic, |
tsepez | 6173c9d | 2016-11-09 16:38:03 -0800 | [diff] [blame] | 84 | std::vector<CFX_FloatRect>* pRectArray, |
tsepez | 74b8c6e | 2016-10-12 09:38:41 -0700 | [diff] [blame] | 85 | std::vector<CPDF_Dictionary*>* pObjectArray, |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 86 | int nUsage) { |
| 87 | if (!pSourceDoc || !pPageDic) |
| 88 | return FLATTEN_FAIL; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 89 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 90 | GetContentsRect(pSourceDoc, pPageDic, pRectArray); |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 91 | CPDF_Array* pAnnots = pPageDic->GetArrayFor("Annots"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 92 | if (!pAnnots) |
| 93 | return FLATTEN_NOTHINGTODO; |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 94 | |
Lei Zhang | d361005 | 2017-06-19 12:25:54 -0700 | [diff] [blame] | 95 | for (const auto& pAnnot : *pAnnots) { |
| 96 | CPDF_Dictionary* pAnnotDic = ToDictionary(pAnnot->GetDirect()); |
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 | |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 100 | 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 | |
Lei Zhang | d361005 | 2017-06-19 12:25:54 -0700 | [diff] [blame] | 108 | bool bParseStream; |
| 109 | if (nUsage == FLAT_NORMALDISPLAY) |
| 110 | bParseStream = !(nAnnotFlag & ANNOTFLAG_INVISIBLE); |
| 111 | else |
| 112 | bParseStream = !!(nAnnotFlag & ANNOTFLAG_PRINT); |
| 113 | if (bParseStream) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 114 | ParserStream(pPageDic, pAnnotDic, pRectArray, pObjectArray); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 115 | } |
| 116 | return FLATTEN_SUCCESS; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Dan Sinclair | 05df075 | 2017-03-14 14:43:42 -0400 | [diff] [blame] | 119 | float GetMinMaxValue(const std::vector<CFX_FloatRect>& array, |
| 120 | FPDF_TYPE type, |
| 121 | FPDF_VALUE value) { |
Lei Zhang | d361005 | 2017-06-19 12:25:54 -0700 | [diff] [blame] | 122 | if (array.empty()) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 123 | return 0.0f; |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 124 | |
Lei Zhang | d361005 | 2017-06-19 12:25:54 -0700 | [diff] [blame] | 125 | size_t nRects = array.size(); |
Dan Sinclair | 05df075 | 2017-03-14 14:43:42 -0400 | [diff] [blame] | 126 | std::vector<float> pArray(nRects); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 127 | switch (value) { |
tsepez | 6173c9d | 2016-11-09 16:38:03 -0800 | [diff] [blame] | 128 | case LEFT: |
| 129 | for (size_t i = 0; i < nRects; i++) |
| 130 | pArray[i] = array[i].left; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 131 | break; |
tsepez | 6173c9d | 2016-11-09 16:38:03 -0800 | [diff] [blame] | 132 | case TOP: |
| 133 | for (size_t i = 0; i < nRects; i++) |
| 134 | pArray[i] = array[i].top; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 135 | break; |
tsepez | 6173c9d | 2016-11-09 16:38:03 -0800 | [diff] [blame] | 136 | case RIGHT: |
| 137 | for (size_t i = 0; i < nRects; i++) |
| 138 | pArray[i] = array[i].right; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 139 | break; |
tsepez | 6173c9d | 2016-11-09 16:38:03 -0800 | [diff] [blame] | 140 | case BOTTOM: |
| 141 | for (size_t i = 0; i < nRects; i++) |
| 142 | pArray[i] = array[i].bottom; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 143 | break; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 144 | default: |
Lei Zhang | d361005 | 2017-06-19 12:25:54 -0700 | [diff] [blame] | 145 | NOTREACHED(); |
weili | 12367cb | 2016-06-03 11:22:16 -0700 | [diff] [blame] | 146 | return 0.0f; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 147 | } |
tsepez | 6173c9d | 2016-11-09 16:38:03 -0800 | [diff] [blame] | 148 | |
Dan Sinclair | 05df075 | 2017-03-14 14:43:42 -0400 | [diff] [blame] | 149 | float fRet = pArray[0]; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 150 | if (type == MAX) { |
tsepez | 6173c9d | 2016-11-09 16:38:03 -0800 | [diff] [blame] | 151 | for (size_t i = 1; i < nRects; i++) |
| 152 | fRet = std::max(fRet, pArray[i]); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 153 | } else { |
tsepez | 6173c9d | 2016-11-09 16:38:03 -0800 | [diff] [blame] | 154 | for (size_t i = 1; i < nRects; i++) |
| 155 | fRet = std::min(fRet, pArray[i]); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 156 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 157 | return fRet; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 158 | } |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 159 | |
tsepez | 6173c9d | 2016-11-09 16:38:03 -0800 | [diff] [blame] | 160 | CFX_FloatRect CalculateRect(std::vector<CFX_FloatRect>* pRectArray) { |
Tom Sepez | 281a9ea | 2016-02-26 14:24:28 -0800 | [diff] [blame] | 161 | CFX_FloatRect rcRet; |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 162 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 163 | rcRet.left = GetMinMaxValue(*pRectArray, MIN, LEFT); |
| 164 | rcRet.top = GetMinMaxValue(*pRectArray, MAX, TOP); |
| 165 | rcRet.right = GetMinMaxValue(*pRectArray, MAX, RIGHT); |
| 166 | rcRet.bottom = GetMinMaxValue(*pRectArray, MIN, BOTTOM); |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 167 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 168 | return rcRet; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 171 | uint32_t NewIndirectContentsStream(const ByteString& key, |
tsepez | 381fc83 | 2016-10-10 14:06:44 -0700 | [diff] [blame] | 172 | CPDF_Document* pDocument) { |
tsepez | 70c4afd | 2016-11-15 11:33:44 -0800 | [diff] [blame] | 173 | CPDF_Stream* pNewContents = pDocument->NewIndirect<CPDF_Stream>( |
tsepez | 9e05ee1 | 2016-11-21 13:19:10 -0800 | [diff] [blame] | 174 | nullptr, 0, |
| 175 | pdfium::MakeUnique<CPDF_Dictionary>(pDocument->GetByteStringPool())); |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 176 | ByteString sStream; |
tsepez | 381fc83 | 2016-10-10 14:06:44 -0700 | [diff] [blame] | 177 | sStream.Format("q 1 0 0 1 0 0 cm /%s Do Q", key.c_str()); |
| 178 | pNewContents->SetData(sStream.raw_str(), sStream.GetLength()); |
tsepez | 70c4afd | 2016-11-15 11:33:44 -0800 | [diff] [blame] | 179 | return pNewContents->GetObjNum(); |
tsepez | 381fc83 | 2016-10-10 14:06:44 -0700 | [diff] [blame] | 180 | } |
| 181 | |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 182 | void SetPageContents(const ByteString& key, |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 183 | CPDF_Dictionary* pPage, |
| 184 | CPDF_Document* pDocument) { |
thestig | 1cd352e | 2016-06-07 17:53:06 -0700 | [diff] [blame] | 185 | CPDF_Array* pContentsArray = nullptr; |
tsepez | 381fc83 | 2016-10-10 14:06:44 -0700 | [diff] [blame] | 186 | CPDF_Stream* pContentsStream = pPage->GetStreamFor("Contents"); |
| 187 | if (!pContentsStream) { |
| 188 | pContentsArray = pPage->GetArrayFor("Contents"); |
| 189 | if (!pContentsArray) { |
| 190 | if (!key.IsEmpty()) { |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame] | 191 | pPage->SetNewFor<CPDF_Reference>( |
| 192 | "Contents", pDocument, NewIndirectContentsStream(key, pDocument)); |
tsepez | 381fc83 | 2016-10-10 14:06:44 -0700 | [diff] [blame] | 193 | } |
| 194 | return; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 195 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 196 | } |
tsepez | 381fc83 | 2016-10-10 14:06:44 -0700 | [diff] [blame] | 197 | pPage->ConvertToIndirectObjectFor("Contents", pDocument); |
| 198 | if (!pContentsArray) { |
tsepez | 70c4afd | 2016-11-15 11:33:44 -0800 | [diff] [blame] | 199 | pContentsArray = pDocument->NewIndirect<CPDF_Array>(); |
Tom Sepez | afd0d1f | 2017-04-04 14:37:18 -0700 | [diff] [blame] | 200 | auto pAcc = pdfium::MakeRetain<CPDF_StreamAcc>(pContentsStream); |
| 201 | pAcc->LoadAllData(); |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 202 | ByteString sStream = "q\n"; |
| 203 | ByteString sBody = |
| 204 | ByteString((const char*)pAcc->GetData(), pAcc->GetSize()); |
tsepez | 381fc83 | 2016-10-10 14:06:44 -0700 | [diff] [blame] | 205 | sStream = sStream + sBody + "\nQ"; |
Artem Strygin | 90555e0 | 2017-07-28 19:41:59 +0300 | [diff] [blame] | 206 | pContentsStream->SetDataAndRemoveFilter(sStream.raw_str(), |
| 207 | sStream.GetLength()); |
tsepez | 8a3aa45 | 2016-11-16 12:26:06 -0800 | [diff] [blame] | 208 | pContentsArray->AddNew<CPDF_Reference>(pDocument, |
| 209 | pContentsStream->GetObjNum()); |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame] | 210 | pPage->SetNewFor<CPDF_Reference>("Contents", pDocument, |
| 211 | pContentsArray->GetObjNum()); |
tsepez | 381fc83 | 2016-10-10 14:06:44 -0700 | [diff] [blame] | 212 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 213 | if (!key.IsEmpty()) { |
tsepez | 8a3aa45 | 2016-11-16 12:26:06 -0800 | [diff] [blame] | 214 | pContentsArray->AddNew<CPDF_Reference>( |
| 215 | pDocument, NewIndirectContentsStream(key, pDocument)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 216 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 217 | } |
| 218 | |
Tom Sepez | 281a9ea | 2016-02-26 14:24:28 -0800 | [diff] [blame] | 219 | CFX_Matrix GetMatrix(CFX_FloatRect rcAnnot, |
| 220 | CFX_FloatRect rcStream, |
Tom Sepez | 60d909e | 2015-12-10 15:34:55 -0800 | [diff] [blame] | 221 | const CFX_Matrix& matrix) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 222 | if (rcStream.IsEmpty()) |
Tom Sepez | 60d909e | 2015-12-10 15:34:55 -0800 | [diff] [blame] | 223 | return CFX_Matrix(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 224 | |
Jane Liu | 878b27d | 2017-08-22 10:50:06 -0400 | [diff] [blame] | 225 | rcStream = matrix.TransformRect(rcStream); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 226 | rcStream.Normalize(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 227 | |
Dan Sinclair | 05df075 | 2017-03-14 14:43:42 -0400 | [diff] [blame] | 228 | float a = rcAnnot.Width() / rcStream.Width(); |
| 229 | float d = rcAnnot.Height() / rcStream.Height(); |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 230 | |
Dan Sinclair | 05df075 | 2017-03-14 14:43:42 -0400 | [diff] [blame] | 231 | float e = rcAnnot.left - rcStream.left * a; |
| 232 | float f = rcAnnot.bottom - rcStream.bottom * d; |
Tom Sepez | 60d909e | 2015-12-10 15:34:55 -0800 | [diff] [blame] | 233 | return CFX_Matrix(a, 0, 0, d, e, f); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 234 | } |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 235 | |
tsepez | 381fc83 | 2016-10-10 14:06:44 -0700 | [diff] [blame] | 236 | } // namespace |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 237 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 238 | FPDF_EXPORT int FPDF_CALLCONV FPDFPage_Flatten(FPDF_PAGE page, int nFlag) { |
Tom Sepez | db0be96 | 2015-10-16 14:00:21 -0700 | [diff] [blame] | 239 | CPDF_Page* pPage = CPDFPageFromFPDFPage(page); |
tsepez | 6173c9d | 2016-11-09 16:38:03 -0800 | [diff] [blame] | 240 | if (!page) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 241 | return FLATTEN_FAIL; |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 242 | |
Tom Sepez | 4cb82ee | 2017-05-22 15:15:30 -0700 | [diff] [blame] | 243 | CPDF_Document* pDocument = pPage->m_pDocument.Get(); |
| 244 | CPDF_Dictionary* pPageDict = pPage->m_pFormDict.Get(); |
tsepez | 6173c9d | 2016-11-09 16:38:03 -0800 | [diff] [blame] | 245 | if (!pDocument || !pPageDict) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 246 | return FLATTEN_FAIL; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 247 | |
tsepez | 74b8c6e | 2016-10-12 09:38:41 -0700 | [diff] [blame] | 248 | std::vector<CPDF_Dictionary*> ObjectArray; |
tsepez | 6173c9d | 2016-11-09 16:38:03 -0800 | [diff] [blame] | 249 | std::vector<CFX_FloatRect> RectArray; |
| 250 | int iRet = |
| 251 | ParserAnnots(pDocument, pPageDict, &RectArray, &ObjectArray, nFlag); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 252 | if (iRet == FLATTEN_NOTHINGTODO || iRet == FLATTEN_FAIL) |
| 253 | return iRet; |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 254 | |
Tom Sepez | 281a9ea | 2016-02-26 14:24:28 -0800 | [diff] [blame] | 255 | CFX_FloatRect rcOriginalCB; |
| 256 | CFX_FloatRect rcMerger = CalculateRect(&RectArray); |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 257 | CFX_FloatRect rcOriginalMB = pPageDict->GetRectFor("MediaBox"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 258 | if (pPageDict->KeyExist("CropBox")) |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 259 | rcOriginalMB = pPageDict->GetRectFor("CropBox"); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 260 | |
tsepez | 6173c9d | 2016-11-09 16:38:03 -0800 | [diff] [blame] | 261 | if (rcOriginalMB.IsEmpty()) |
Tom Sepez | 281a9ea | 2016-02-26 14:24:28 -0800 | [diff] [blame] | 262 | rcOriginalMB = CFX_FloatRect(0.0f, 0.0f, 612.0f, 792.0f); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 263 | |
tsepez | 6173c9d | 2016-11-09 16:38:03 -0800 | [diff] [blame] | 264 | rcMerger.left = std::max(rcMerger.left, rcOriginalMB.left); |
| 265 | rcMerger.right = std::min(rcMerger.right, rcOriginalMB.right); |
| 266 | rcMerger.bottom = std::max(rcMerger.bottom, rcOriginalMB.bottom); |
| 267 | rcMerger.top = std::min(rcMerger.top, rcOriginalMB.top); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 268 | if (pPageDict->KeyExist("ArtBox")) |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 269 | rcOriginalCB = pPageDict->GetRectFor("ArtBox"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 270 | else |
| 271 | rcOriginalCB = rcOriginalMB; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 272 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 273 | if (!rcOriginalMB.IsEmpty()) { |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame] | 274 | CPDF_Array* pMediaBox = pPageDict->SetNewFor<CPDF_Array>("MediaBox"); |
tsepez | 8a3aa45 | 2016-11-16 12:26:06 -0800 | [diff] [blame] | 275 | pMediaBox->AddNew<CPDF_Number>(rcOriginalMB.left); |
| 276 | pMediaBox->AddNew<CPDF_Number>(rcOriginalMB.bottom); |
| 277 | pMediaBox->AddNew<CPDF_Number>(rcOriginalMB.right); |
| 278 | pMediaBox->AddNew<CPDF_Number>(rcOriginalMB.top); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 279 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 280 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 281 | if (!rcOriginalCB.IsEmpty()) { |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame] | 282 | CPDF_Array* pCropBox = pPageDict->SetNewFor<CPDF_Array>("ArtBox"); |
tsepez | 8a3aa45 | 2016-11-16 12:26:06 -0800 | [diff] [blame] | 283 | pCropBox->AddNew<CPDF_Number>(rcOriginalCB.left); |
| 284 | pCropBox->AddNew<CPDF_Number>(rcOriginalCB.bottom); |
| 285 | pCropBox->AddNew<CPDF_Number>(rcOriginalCB.right); |
| 286 | pCropBox->AddNew<CPDF_Number>(rcOriginalCB.top); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 287 | } |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 288 | |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 289 | CPDF_Dictionary* pRes = pPageDict->GetDictFor("Resources"); |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame] | 290 | if (!pRes) |
| 291 | pRes = pPageDict->SetNewFor<CPDF_Dictionary>("Resources"); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 292 | |
tsepez | 70c4afd | 2016-11-15 11:33:44 -0800 | [diff] [blame] | 293 | CPDF_Stream* pNewXObject = pDocument->NewIndirect<CPDF_Stream>( |
tsepez | 9e05ee1 | 2016-11-21 13:19:10 -0800 | [diff] [blame] | 294 | nullptr, 0, |
| 295 | pdfium::MakeUnique<CPDF_Dictionary>(pDocument->GetByteStringPool())); |
tsepez | 698c571 | 2016-09-28 16:47:07 -0700 | [diff] [blame] | 296 | |
tsepez | 70c4afd | 2016-11-15 11:33:44 -0800 | [diff] [blame] | 297 | uint32_t dwObjNum = pNewXObject->GetObjNum(); |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 298 | CPDF_Dictionary* pPageXObject = pRes->GetDictFor("XObject"); |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame] | 299 | if (!pPageXObject) |
| 300 | pPageXObject = pRes->SetNewFor<CPDF_Dictionary>("XObject"); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 301 | |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 302 | ByteString key; |
tsepez | 74b8c6e | 2016-10-12 09:38:41 -0700 | [diff] [blame] | 303 | int nStreams = pdfium::CollectionSize<int>(ObjectArray); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 304 | if (nStreams > 0) { |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 305 | ByteString sKey; |
Lei Zhang | f0f2a2a | 2017-06-13 22:57:46 -0700 | [diff] [blame] | 306 | int i = 0; |
| 307 | while (i < INT_MAX) { |
| 308 | sKey.Format("FFT%d", i); |
| 309 | if (!pPageXObject->KeyExist(sKey)) { |
| 310 | key = sKey; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 311 | break; |
Lei Zhang | f0f2a2a | 2017-06-13 22:57:46 -0700 | [diff] [blame] | 312 | } |
| 313 | ++i; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 314 | } |
| 315 | } |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 316 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 317 | SetPageContents(key, pPageDict, pDocument); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 318 | |
thestig | 1cd352e | 2016-06-07 17:53:06 -0700 | [diff] [blame] | 319 | CPDF_Dictionary* pNewXORes = nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 320 | if (!key.IsEmpty()) { |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame] | 321 | pPageXObject->SetNewFor<CPDF_Reference>(key, pDocument, dwObjNum); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 322 | CPDF_Dictionary* pNewOXbjectDic = pNewXObject->GetDict(); |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame] | 323 | pNewXORes = pNewOXbjectDic->SetNewFor<CPDF_Dictionary>("Resources"); |
| 324 | pNewOXbjectDic->SetNewFor<CPDF_Name>("Type", "XObject"); |
| 325 | pNewOXbjectDic->SetNewFor<CPDF_Name>("Subtype", "Form"); |
| 326 | pNewOXbjectDic->SetNewFor<CPDF_Number>("FormType", 1); |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 327 | CFX_FloatRect rcBBox = pPageDict->GetRectFor("ArtBox"); |
| 328 | pNewOXbjectDic->SetRectFor("BBox", rcBBox); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 329 | } |
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 | for (int i = 0; i < nStreams; i++) { |
tsepez | 74b8c6e | 2016-10-12 09:38:41 -0700 | [diff] [blame] | 332 | CPDF_Dictionary* pAnnotDic = ObjectArray[i]; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 333 | if (!pAnnotDic) |
| 334 | continue; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 335 | |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 336 | CFX_FloatRect rcAnnot = pAnnotDic->GetRectFor("Rect"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 337 | rcAnnot.Normalize(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 338 | |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 339 | ByteString sAnnotState = pAnnotDic->GetStringFor("AS"); |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 340 | CPDF_Dictionary* pAnnotAP = pAnnotDic->GetDictFor("AP"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 341 | if (!pAnnotAP) |
| 342 | continue; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 343 | |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 344 | CPDF_Stream* pAPStream = pAnnotAP->GetStreamFor("N"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 345 | if (!pAPStream) { |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 346 | CPDF_Dictionary* pAPDic = pAnnotAP->GetDictFor("N"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 347 | if (!pAPDic) |
| 348 | continue; |
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 (!sAnnotState.IsEmpty()) { |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 351 | pAPStream = pAPDic->GetStreamFor(sAnnotState); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 352 | } else { |
Lei Zhang | d361005 | 2017-06-19 12:25:54 -0700 | [diff] [blame] | 353 | if (pAPDic->GetCount() > 0) { |
| 354 | CPDF_Object* pFirstObj = pAPDic->begin()->second.get(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 355 | if (pFirstObj) { |
Dan Sinclair | bf81c14 | 2015-10-26 16:54:39 -0400 | [diff] [blame] | 356 | if (pFirstObj->IsReference()) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 357 | pFirstObj = pFirstObj->GetDirect(); |
Dan Sinclair | aa435ba | 2015-10-22 16:45:48 -0400 | [diff] [blame] | 358 | if (!pFirstObj->IsStream()) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 359 | continue; |
Dan Sinclair | aa435ba | 2015-10-22 16:45:48 -0400 | [diff] [blame] | 360 | pAPStream = pFirstObj->AsStream(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 361 | } |
| 362 | } |
| 363 | } |
| 364 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 365 | if (!pAPStream) |
| 366 | continue; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 367 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 368 | CPDF_Dictionary* pAPDic = pAPStream->GetDict(); |
Tom Sepez | 281a9ea | 2016-02-26 14:24:28 -0800 | [diff] [blame] | 369 | CFX_FloatRect rcStream; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 370 | if (pAPDic->KeyExist("Rect")) |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 371 | rcStream = pAPDic->GetRectFor("Rect"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 372 | else if (pAPDic->KeyExist("BBox")) |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 373 | rcStream = pAPDic->GetRectFor("BBox"); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 374 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 375 | if (rcStream.IsEmpty()) |
| 376 | continue; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 377 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 378 | CPDF_Object* pObj = pAPStream; |
tsepez | d0ecd89 | 2016-11-08 17:30:04 -0800 | [diff] [blame] | 379 | if (pObj->IsInline()) { |
tsepez | 335cf09 | 2016-11-09 13:28:26 -0800 | [diff] [blame] | 380 | std::unique_ptr<CPDF_Object> pNew = pObj->Clone(); |
| 381 | pObj = pNew.get(); |
tsepez | 70c4afd | 2016-11-15 11:33:44 -0800 | [diff] [blame] | 382 | pDocument->AddIndirectObject(std::move(pNew)); |
tsepez | d0ecd89 | 2016-11-08 17:30:04 -0800 | [diff] [blame] | 383 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 384 | |
tsepez | d0ecd89 | 2016-11-08 17:30:04 -0800 | [diff] [blame] | 385 | CPDF_Dictionary* pObjDic = pObj->GetDict(); |
| 386 | if (pObjDic) { |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame] | 387 | pObjDic->SetNewFor<CPDF_Name>("Type", "XObject"); |
| 388 | pObjDic->SetNewFor<CPDF_Name>("Subtype", "Form"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 389 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 390 | |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 391 | CPDF_Dictionary* pXObject = pNewXORes->GetDictFor("XObject"); |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame] | 392 | if (!pXObject) |
| 393 | pXObject = pNewXORes->SetNewFor<CPDF_Dictionary>("XObject"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 394 | |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 395 | ByteString sFormName; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 396 | sFormName.Format("F%d", i); |
tsepez | 0e606b5 | 2016-11-18 16:22:41 -0800 | [diff] [blame] | 397 | pXObject->SetNewFor<CPDF_Reference>(sFormName, pDocument, |
| 398 | pObj->GetObjNum()); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 399 | |
Tom Sepez | afd0d1f | 2017-04-04 14:37:18 -0700 | [diff] [blame] | 400 | auto pAcc = pdfium::MakeRetain<CPDF_StreamAcc>(pNewXObject); |
| 401 | pAcc->LoadAllData(); |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 402 | ByteString sStream(pAcc->GetData(), pAcc->GetSize()); |
tsepez | d0ecd89 | 2016-11-08 17:30:04 -0800 | [diff] [blame] | 403 | CFX_Matrix matrix = pAPDic->GetMatrixFor("Matrix"); |
Tom Sepez | 60d909e | 2015-12-10 15:34:55 -0800 | [diff] [blame] | 404 | CFX_Matrix m = GetMatrix(rcAnnot, rcStream, matrix); |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 405 | ByteString sTemp; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 406 | sTemp.Format("q %f 0 0 %f %f %f cm /%s Do Q\n", m.a, m.d, m.e, m.f, |
| 407 | sFormName.c_str()); |
| 408 | sStream += sTemp; |
Artem Strygin | 90555e0 | 2017-07-28 19:41:59 +0300 | [diff] [blame] | 409 | pNewXObject->SetDataAndRemoveFilter(sStream.raw_str(), sStream.GetLength()); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 410 | } |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 411 | pPageDict->RemoveFor("Annots"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 412 | return FLATTEN_SUCCESS; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 413 | } |