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