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