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