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) { |
Tom Sepez | ae51c81 | 2015-08-05 12:34:06 -0700 | [diff] [blame] | 40 | CPDF_Page* pPDFPage = new CPDF_Page; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 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()) { |
Nico Weber | 077f1a3 | 2015-08-06 15:08:57 -0700 | [diff] [blame] | 199 | CPDF_Stream* pNewContents = new CPDF_Stream(NULL, 0, new CPDF_Dictionary); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 200 | pPage->SetAtReference("Contents", pDocument, |
| 201 | pDocument->AddIndirectObject(pNewContents)); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 202 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 203 | CFX_ByteString sStream; |
| 204 | sStream.Format("q 1 0 0 1 0 0 cm /%s Do Q", key.c_str()); |
| 205 | pNewContents->SetData((const uint8_t*)sStream, sStream.GetLength(), FALSE, |
| 206 | FALSE); |
| 207 | } |
| 208 | return; |
| 209 | } |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 210 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 211 | int iType = pContentsObj->GetType(); |
| 212 | CPDF_Array* pContentsArray = NULL; |
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 | switch (iType) { |
| 215 | case PDFOBJ_STREAM: { |
Tom Sepez | ae51c81 | 2015-08-05 12:34:06 -0700 | [diff] [blame] | 216 | pContentsArray = new CPDF_Array; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 217 | CPDF_Stream* pContents = (CPDF_Stream*)pContentsObj; |
| 218 | FX_DWORD dwObjNum = pDocument->AddIndirectObject(pContents); |
| 219 | CPDF_StreamAcc acc; |
| 220 | acc.LoadAllData(pContents); |
| 221 | CFX_ByteString sStream = "q\n"; |
| 222 | CFX_ByteString sBody = |
| 223 | CFX_ByteString((const FX_CHAR*)acc.GetData(), acc.GetSize()); |
| 224 | sStream = sStream + sBody + "\nQ"; |
| 225 | pContents->SetData((const uint8_t*)sStream, sStream.GetLength(), FALSE, |
| 226 | FALSE); |
| 227 | pContentsArray->AddReference(pDocument, dwObjNum); |
| 228 | break; |
| 229 | } |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 230 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 231 | case PDFOBJ_ARRAY: { |
| 232 | pContentsArray = (CPDF_Array*)pContentsObj; |
| 233 | break; |
| 234 | } |
| 235 | default: |
| 236 | break; |
| 237 | } |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 238 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 239 | if (!pContentsArray) |
| 240 | return; |
| 241 | |
| 242 | FX_DWORD dwObjNum = pDocument->AddIndirectObject(pContentsArray); |
| 243 | pPage->SetAtReference("Contents", pDocument, dwObjNum); |
| 244 | |
| 245 | if (!key.IsEmpty()) { |
Nico Weber | 077f1a3 | 2015-08-06 15:08:57 -0700 | [diff] [blame] | 246 | CPDF_Stream* pNewContents = new CPDF_Stream(NULL, 0, new CPDF_Dictionary); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 247 | dwObjNum = pDocument->AddIndirectObject(pNewContents); |
| 248 | pContentsArray->AddReference(pDocument, dwObjNum); |
| 249 | |
| 250 | CFX_ByteString sStream; |
| 251 | sStream.Format("q 1 0 0 1 0 0 cm /%s Do Q", key.c_str()); |
| 252 | pNewContents->SetData((const uint8_t*)sStream, sStream.GetLength(), FALSE, |
| 253 | FALSE); |
| 254 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 255 | } |
| 256 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 257 | CFX_AffineMatrix GetMatrix(CPDF_Rect rcAnnot, |
| 258 | CPDF_Rect rcStream, |
| 259 | CFX_AffineMatrix matrix) { |
| 260 | if (rcStream.IsEmpty()) |
| 261 | return CFX_AffineMatrix(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 262 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 263 | matrix.TransformRect(rcStream); |
| 264 | rcStream.Normalize(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 265 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 266 | FX_FLOAT a = rcAnnot.Width() / rcStream.Width(); |
| 267 | FX_FLOAT d = rcAnnot.Height() / rcStream.Height(); |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 268 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 269 | FX_FLOAT e = rcAnnot.left - rcStream.left * a; |
| 270 | FX_FLOAT f = rcAnnot.bottom - rcStream.bottom * d; |
| 271 | return CFX_AffineMatrix(a, 0, 0, d, e, f); |
| 272 | } |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 273 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 274 | void GetOffset(FX_FLOAT& fa, |
| 275 | FX_FLOAT& fd, |
| 276 | FX_FLOAT& fe, |
| 277 | FX_FLOAT& ff, |
| 278 | CPDF_Rect rcAnnot, |
| 279 | CPDF_Rect rcStream, |
| 280 | CFX_AffineMatrix matrix) { |
| 281 | FX_FLOAT fStreamWidth = 0.0f; |
| 282 | FX_FLOAT fStreamHeight = 0.0f; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 283 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 284 | if (matrix.a != 0 && matrix.d != 0) { |
| 285 | fStreamWidth = rcStream.right - rcStream.left; |
| 286 | fStreamHeight = rcStream.top - rcStream.bottom; |
| 287 | } else { |
| 288 | fStreamWidth = rcStream.top - rcStream.bottom; |
| 289 | fStreamHeight = rcStream.right - rcStream.left; |
| 290 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 291 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 292 | FX_FLOAT x1 = |
| 293 | matrix.a * rcStream.left + matrix.c * rcStream.bottom + matrix.e; |
| 294 | FX_FLOAT y1 = |
| 295 | matrix.b * rcStream.left + matrix.d * rcStream.bottom + matrix.f; |
| 296 | FX_FLOAT x2 = matrix.a * rcStream.left + matrix.c * rcStream.top + matrix.e; |
| 297 | FX_FLOAT y2 = matrix.b * rcStream.left + matrix.d * rcStream.top + matrix.f; |
| 298 | FX_FLOAT x3 = |
| 299 | matrix.a * rcStream.right + matrix.c * rcStream.bottom + matrix.e; |
| 300 | FX_FLOAT y3 = |
| 301 | matrix.b * rcStream.right + matrix.d * rcStream.bottom + matrix.f; |
| 302 | FX_FLOAT x4 = matrix.a * rcStream.right + matrix.c * rcStream.top + matrix.e; |
| 303 | 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] | 304 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 305 | FX_FLOAT left = FX_MIN(FX_MIN(x1, x2), FX_MIN(x3, x4)); |
| 306 | 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] | 307 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 308 | fa = (rcAnnot.right - rcAnnot.left) / fStreamWidth; |
| 309 | fd = (rcAnnot.top - rcAnnot.bottom) / fStreamHeight; |
| 310 | fe = rcAnnot.left - left * fa; |
| 311 | ff = rcAnnot.bottom - bottom * fd; |
| 312 | } |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 313 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 314 | DLLEXPORT int STDCALL FPDFPage_Flatten(FPDF_PAGE page, int nFlag) { |
| 315 | if (!page) { |
| 316 | return FLATTEN_FAIL; |
| 317 | } |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 318 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 319 | CPDF_Page* pPage = ((CPDFXFA_Page*)(page))->GetPDFPage(); |
| 320 | if (!pPage) |
| 321 | return FLATTEN_FAIL; |
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_Document* pDocument = pPage->m_pDocument; |
| 324 | CPDF_Dictionary* pPageDict = pPage->m_pFormDict; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 325 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 326 | if (!pDocument || !pPageDict) { |
| 327 | return FLATTEN_FAIL; |
| 328 | } |
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 | CPDF_ObjectArray ObjectArray; |
| 331 | CPDF_RectArray RectArray; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 332 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 333 | int iRet = FLATTEN_FAIL; |
| 334 | iRet = ParserAnnots(pDocument, pPageDict, &RectArray, &ObjectArray, nFlag); |
| 335 | if (iRet == FLATTEN_NOTHINGTODO || iRet == FLATTEN_FAIL) |
| 336 | return iRet; |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 337 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 338 | CPDF_Rect rcOriginalCB; |
| 339 | CPDF_Rect rcMerger = CalculateRect(&RectArray); |
| 340 | CPDF_Rect rcOriginalMB = pPageDict->GetRect("MediaBox"); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 341 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 342 | if (pPageDict->KeyExist("CropBox")) |
| 343 | rcOriginalMB = pPageDict->GetRect("CropBox"); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 344 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 345 | if (rcOriginalMB.IsEmpty()) { |
| 346 | rcOriginalMB = CPDF_Rect(0.0f, 0.0f, 612.0f, 792.0f); |
| 347 | } |
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 | rcMerger.left = |
| 350 | rcMerger.left < rcOriginalMB.left ? rcOriginalMB.left : rcMerger.left; |
| 351 | rcMerger.right = |
| 352 | rcMerger.right > rcOriginalMB.right ? rcOriginalMB.right : rcMerger.right; |
| 353 | rcMerger.top = |
| 354 | rcMerger.top > rcOriginalMB.top ? rcOriginalMB.top : rcMerger.top; |
| 355 | rcMerger.bottom = rcMerger.bottom < rcOriginalMB.bottom ? rcOriginalMB.bottom |
| 356 | : rcMerger.bottom; |
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 (pPageDict->KeyExist("ArtBox")) |
| 359 | rcOriginalCB = pPageDict->GetRect("ArtBox"); |
| 360 | else |
| 361 | rcOriginalCB = rcOriginalMB; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 362 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 363 | if (!rcOriginalMB.IsEmpty()) { |
Tom Sepez | ae51c81 | 2015-08-05 12:34:06 -0700 | [diff] [blame] | 364 | CPDF_Array* pMediaBox = new CPDF_Array(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 365 | |
Tom Sepez | ae51c81 | 2015-08-05 12:34:06 -0700 | [diff] [blame] | 366 | pMediaBox->Add(new CPDF_Number(rcOriginalMB.left)); |
| 367 | pMediaBox->Add(new CPDF_Number(rcOriginalMB.bottom)); |
| 368 | pMediaBox->Add(new CPDF_Number(rcOriginalMB.right)); |
| 369 | pMediaBox->Add(new CPDF_Number(rcOriginalMB.top)); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 370 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 371 | pPageDict->SetAt("MediaBox", pMediaBox); |
| 372 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 373 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 374 | if (!rcOriginalCB.IsEmpty()) { |
Tom Sepez | ae51c81 | 2015-08-05 12:34:06 -0700 | [diff] [blame] | 375 | CPDF_Array* pCropBox = new CPDF_Array(); |
| 376 | pCropBox->Add(new CPDF_Number(rcOriginalCB.left)); |
| 377 | pCropBox->Add(new CPDF_Number(rcOriginalCB.bottom)); |
| 378 | pCropBox->Add(new CPDF_Number(rcOriginalCB.right)); |
| 379 | pCropBox->Add(new CPDF_Number(rcOriginalCB.top)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 380 | pPageDict->SetAt("ArtBox", pCropBox); |
| 381 | } |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 382 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 383 | CPDF_Dictionary* pRes = NULL; |
| 384 | pRes = pPageDict->GetDict("Resources"); |
| 385 | if (!pRes) { |
Tom Sepez | ae51c81 | 2015-08-05 12:34:06 -0700 | [diff] [blame] | 386 | pRes = new CPDF_Dictionary; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 387 | pPageDict->SetAt("Resources", pRes); |
| 388 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 389 | |
Nico Weber | 077f1a3 | 2015-08-06 15:08:57 -0700 | [diff] [blame] | 390 | CPDF_Stream* pNewXObject = new CPDF_Stream(NULL, 0, new CPDF_Dictionary); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 391 | FX_DWORD dwObjNum = pDocument->AddIndirectObject(pNewXObject); |
| 392 | CPDF_Dictionary* pPageXObject = pRes->GetDict("XObject"); |
| 393 | if (!pPageXObject) { |
Tom Sepez | ae51c81 | 2015-08-05 12:34:06 -0700 | [diff] [blame] | 394 | pPageXObject = new CPDF_Dictionary; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 395 | pRes->SetAt("XObject", pPageXObject); |
| 396 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 397 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 398 | CFX_ByteString key = ""; |
| 399 | int nStreams = ObjectArray.GetSize(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 400 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 401 | if (nStreams > 0) { |
| 402 | for (int iKey = 0; /*iKey < 100*/; iKey++) { |
| 403 | char sExtend[5] = {}; |
| 404 | FXSYS_itoa(iKey, sExtend, 10); |
| 405 | key = CFX_ByteString("FFT") + CFX_ByteString(sExtend); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 406 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 407 | if (!pPageXObject->KeyExist(key)) |
| 408 | break; |
| 409 | } |
| 410 | } |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 411 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 412 | SetPageContents(key, pPageDict, pDocument); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 413 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 414 | CPDF_Dictionary* pNewXORes = NULL; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 415 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 416 | if (!key.IsEmpty()) { |
| 417 | pPageXObject->SetAtReference(key, pDocument, dwObjNum); |
| 418 | CPDF_Dictionary* pNewOXbjectDic = pNewXObject->GetDict(); |
Tom Sepez | ae51c81 | 2015-08-05 12:34:06 -0700 | [diff] [blame] | 419 | pNewXORes = new CPDF_Dictionary; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 420 | pNewOXbjectDic->SetAt("Resources", pNewXORes); |
| 421 | pNewOXbjectDic->SetAtName("Type", "XObject"); |
| 422 | pNewOXbjectDic->SetAtName("Subtype", "Form"); |
| 423 | pNewOXbjectDic->SetAtInteger("FormType", 1); |
| 424 | pNewOXbjectDic->SetAtName("Name", "FRM"); |
| 425 | CPDF_Rect rcBBox = pPageDict->GetRect("ArtBox"); |
| 426 | pNewOXbjectDic->SetAtRect("BBox", rcBBox); |
| 427 | } |
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 | for (int i = 0; i < nStreams; i++) { |
| 430 | CPDF_Dictionary* pAnnotDic = ObjectArray.GetAt(i); |
| 431 | if (!pAnnotDic) |
| 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_Rect rcAnnot = pAnnotDic->GetRect("Rect"); |
| 435 | rcAnnot.Normalize(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 436 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 437 | CFX_ByteString sAnnotState = pAnnotDic->GetString("AS"); |
| 438 | CPDF_Dictionary* pAnnotAP = pAnnotDic->GetDict("AP"); |
| 439 | if (!pAnnotAP) |
| 440 | continue; |
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 | CPDF_Stream* pAPStream = pAnnotAP->GetStream("N"); |
| 443 | if (!pAPStream) { |
| 444 | CPDF_Dictionary* pAPDic = pAnnotAP->GetDict("N"); |
| 445 | if (!pAPDic) |
| 446 | continue; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 447 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 448 | if (!sAnnotState.IsEmpty()) { |
| 449 | pAPStream = pAPDic->GetStream(sAnnotState); |
| 450 | } else { |
| 451 | FX_POSITION pos = pAPDic->GetStartPos(); |
| 452 | if (pos) { |
| 453 | CFX_ByteString sKey; |
| 454 | CPDF_Object* pFirstObj = pAPDic->GetNextElement(pos, sKey); |
| 455 | if (pFirstObj) { |
| 456 | if (pFirstObj->GetType() == PDFOBJ_REFERENCE) |
| 457 | pFirstObj = pFirstObj->GetDirect(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 458 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 459 | if (pFirstObj->GetType() != PDFOBJ_STREAM) |
| 460 | continue; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 461 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 462 | pAPStream = (CPDF_Stream*)pFirstObj; |
| 463 | } |
| 464 | } |
| 465 | } |
| 466 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 467 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 468 | if (!pAPStream) |
| 469 | continue; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 470 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 471 | CPDF_Dictionary* pAPDic = pAPStream->GetDict(); |
| 472 | CFX_AffineMatrix matrix = pAPDic->GetMatrix("Matrix"); |
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 | CPDF_Rect rcStream; |
| 475 | if (pAPDic->KeyExist("Rect")) |
| 476 | rcStream = pAPDic->GetRect("Rect"); |
| 477 | else if (pAPDic->KeyExist("BBox")) |
| 478 | rcStream = pAPDic->GetRect("BBox"); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 479 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 480 | if (rcStream.IsEmpty()) |
| 481 | continue; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 482 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 483 | CPDF_Object* pObj = pAPStream; |
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 (pObj) { |
| 486 | CPDF_Dictionary* pObjDic = pObj->GetDict(); |
| 487 | if (pObjDic) { |
| 488 | pObjDic->SetAtName("Type", "XObject"); |
| 489 | pObjDic->SetAtName("Subtype", "Form"); |
| 490 | } |
| 491 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 492 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 493 | CPDF_Dictionary* pXObject = pNewXORes->GetDict("XObject"); |
| 494 | if (!pXObject) { |
Tom Sepez | ae51c81 | 2015-08-05 12:34:06 -0700 | [diff] [blame] | 495 | pXObject = new CPDF_Dictionary; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 496 | pNewXORes->SetAt("XObject", pXObject); |
| 497 | } |
| 498 | |
| 499 | CFX_ByteString sFormName; |
| 500 | sFormName.Format("F%d", i); |
| 501 | FX_DWORD dwObjNum = pDocument->AddIndirectObject(pObj); |
| 502 | pXObject->SetAtReference(sFormName, pDocument, dwObjNum); |
| 503 | |
| 504 | CPDF_StreamAcc acc; |
| 505 | acc.LoadAllData(pNewXObject); |
| 506 | |
| 507 | const uint8_t* pData = acc.GetData(); |
| 508 | CFX_ByteString sStream(pData, acc.GetSize()); |
| 509 | CFX_ByteString sTemp; |
| 510 | |
| 511 | if (matrix.IsIdentity()) { |
| 512 | matrix.a = 1.0f; |
| 513 | matrix.b = 0.0f; |
| 514 | matrix.c = 0.0f; |
| 515 | matrix.d = 1.0f; |
| 516 | matrix.e = 0.0f; |
| 517 | matrix.f = 0.0f; |
| 518 | } |
| 519 | |
| 520 | CFX_AffineMatrix m = GetMatrix(rcAnnot, rcStream, matrix); |
| 521 | sTemp.Format("q %f 0 0 %f %f %f cm /%s Do Q\n", m.a, m.d, m.e, m.f, |
| 522 | sFormName.c_str()); |
| 523 | sStream += sTemp; |
| 524 | |
| 525 | pNewXObject->SetData((const uint8_t*)sStream, sStream.GetLength(), FALSE, |
| 526 | FALSE); |
| 527 | } |
| 528 | pPageDict->RemoveAt("Annots"); |
| 529 | |
| 530 | ObjectArray.RemoveAll(); |
| 531 | RectArray.RemoveAll(); |
| 532 | |
| 533 | return FLATTEN_SUCCESS; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 534 | } |