Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(72)

Side by Side Diff: fpdfsdk/src/fpdf_flatten.cpp

Issue 453133004: clang-format all code (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@master
Patch Set: Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 PDFium Authors. All rights reserved. 1 // Copyright 2014 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6 6
7 #include "../include/fsdk_define.h" 7 #include "../include/fsdk_define.h"
8 #include "../include/fpdf_flatten.h" 8 #include "../include/fpdf_flatten.h"
9 9
10 typedef CFX_ArrayTemplate<CPDF_Dictionary*> CPDF_ObjectArray; 10 typedef CFX_ArrayTemplate<CPDF_Dictionary*> CPDF_ObjectArray;
11 typedef CFX_ArrayTemplate<CPDF_Rect> CPDF_RectArray; 11 typedef CFX_ArrayTemplate<CPDF_Rect> CPDF_RectArray;
12 12
13 enum FPDF_TYPE { MAX, MIN }; 13 enum FPDF_TYPE { MAX, MIN };
14 enum FPDF_VALUE { TOP, LEFT, RIGHT, BOTTOM }; 14 enum FPDF_VALUE { TOP, LEFT, RIGHT, BOTTOM };
15 15
16 FX_BOOL IsValiableRect(CPDF_Rect rect, CPDF_Rect rcPage) 16 FX_BOOL IsValiableRect(CPDF_Rect rect, CPDF_Rect rcPage) {
17 { 17 if (rect.left - rect.right > 0.000001f || rect.bottom - rect.top > 0.000001f)
18 if ( rect.left - rect.right > 0.000001f || 18 return FALSE;
19 rect.bottom - rect.top > 0.000001f) 19
20 return FALSE; 20 if (rect.left == 0.0f && rect.top == 0.0f && rect.right == 0.0f &&
21 21 rect.bottom == 0.0f)
22 if (rect.left == 0.0f && 22 return FALSE;
23 rect.top == 0.0f && 23
24 rect.right == 0.0f && 24 if (!rcPage.IsEmpty()) {
25 rect.bottom == 0.0f) 25 if (rect.left - rcPage.left < -10.000001f ||
26 return FALSE; 26 rect.right - rcPage.right > 10.000001f ||
27 27 rect.top - rcPage.top > 10.000001f ||
28 if (!rcPage.IsEmpty()) 28 rect.bottom - rcPage.bottom < -10.000001f)
29 { 29 return FALSE;
30 if (rect.left - rcPage.left < -10.000001f || 30 }
31 rect.right - rcPage.right > 10.000001f || 31
32 rect.top - rcPage.top > 10.000001f || 32 return TRUE;
33 rect.bottom - rcPage.bottom < -10.000001f) 33 }
34 return FALSE; 34
35 } 35 FX_BOOL GetContentsRect(CPDF_Document* pDoc,
36 36 CPDF_Dictionary* pDict,
37 return TRUE; 37 CPDF_RectArray* pRectArray) {
38 } 38 CPDF_Page* pPDFPage = FX_NEW CPDF_Page;
39 39 pPDFPage->Load(pDoc, pDict, FALSE);
40 40 pPDFPage->ParseContent();
41 FX_BOOL GetContentsRect( CPDF_Document * pDoc, CPDF_Dictionary* pDict, CPDF_Rect Array * pRectArray ) 41
42 { 42 FX_POSITION pos = pPDFPage->GetFirstObjectPosition();
43 CPDF_Page* pPDFPage = FX_NEW CPDF_Page; 43
44 pPDFPage->Load( pDoc, pDict, FALSE ); 44 while (pos) {
45 pPDFPage->ParseContent(); 45 CPDF_PageObject* pPageObject = pPDFPage->GetNextObject(pos);
46 46 if (!pPageObject)
47 FX_POSITION pos = pPDFPage->GetFirstObjectPosition(); 47 continue;
48 48
49 while (pos) 49 CPDF_Rect rc;
50 { 50 rc.left = pPageObject->m_Left;
51 CPDF_PageObject* pPageObject = pPDFPage->GetNextObject(pos); 51 rc.right = pPageObject->m_Right;
52 if (!pPageObject)continue; 52 rc.bottom = pPageObject->m_Bottom;
53 53 rc.top = pPageObject->m_Top;
54 CPDF_Rect rc; 54
55 rc.left = pPageObject->m_Left; 55 if (IsValiableRect(rc, pDict->GetRect("MediaBox"))) {
56 rc.right = pPageObject->m_Right; 56 pRectArray->Add(rc);
57 rc.bottom = pPageObject->m_Bottom; 57 }
58 rc.top = pPageObject->m_Top; 58 }
59 59
60 if (IsValiableRect(rc, pDict->GetRect("MediaBox"))) 60 delete pPDFPage;
61 { 61 return TRUE;
62 pRectArray->Add(rc); 62 }
63 } 63
64 } 64 void ParserStream(CPDF_Dictionary* pPageDic,
65 65 CPDF_Dictionary* pStream,
66 delete pPDFPage; 66 CPDF_RectArray* pRectArray,
67 return TRUE; 67 CPDF_ObjectArray* pObjectArray) {
68 } 68 if (!pStream)
69 69 return;
70 70 CPDF_Rect rect;
71 void ParserStream( CPDF_Dictionary * pPageDic, CPDF_Dictionary* pStream, CPDF_Re ctArray * pRectArray, CPDF_ObjectArray * pObjectArray ) 71 if (pStream->KeyExist("Rect"))
72 { 72 rect = pStream->GetRect("Rect");
73 if (!pStream)return; 73 else if (pStream->KeyExist("BBox"))
74 CPDF_Rect rect; 74 rect = pStream->GetRect("BBox");
75 if (pStream->KeyExist("Rect")) 75
76 rect = pStream->GetRect("Rect"); 76 if (IsValiableRect(rect, pPageDic->GetRect("MediaBox")))
77 else if (pStream->KeyExist("BBox")) 77 pRectArray->Add(rect);
78 rect = pStream->GetRect("BBox"); 78
79 79 pObjectArray->Add(pStream);
80 if (IsValiableRect(rect, pPageDic->GetRect("MediaBox"))) 80 }
81 pRectArray->Add(rect); 81
82 82 int ParserAnnots(CPDF_Document* pSourceDoc,
83 pObjectArray->Add(pStream); 83 CPDF_Dictionary* pPageDic,
84 } 84 CPDF_RectArray* pRectArray,
85 85 CPDF_ObjectArray* pObjectArray,
86 86 int nUsage) {
87 int ParserAnnots( CPDF_Document* pSourceDoc, CPDF_Dictionary * pPageDic, CPDF_Re ctArray * pRectArray, CPDF_ObjectArray * pObjectArray, int nUsage) 87 if (!pSourceDoc || !pPageDic)
88 { 88 return FLATTEN_FAIL;
89 if (!pSourceDoc || !pPageDic) return FLATTEN_FAIL; 89
90 90 GetContentsRect(pSourceDoc, pPageDic, pRectArray);
91 GetContentsRect( pSourceDoc, pPageDic, pRectArray ); 91 CPDF_Array* pAnnots = pPageDic->GetArray("Annots");
92 CPDF_Array* pAnnots = pPageDic->GetArray("Annots"); 92 if (pAnnots) {
93 if (pAnnots) 93 FX_DWORD dwSize = pAnnots->GetCount();
94 { 94
95 FX_DWORD dwSize = pAnnots->GetCount(); 95 for (int i = 0; i < (int)dwSize; i++) {
96 96 CPDF_Object* pObj = pAnnots->GetElementValue(i);
97 for (int i = 0; i < (int)dwSize; i++) 97
98 { 98 if (!pObj)
99 CPDF_Object* pObj = pAnnots->GetElementValue(i); 99 continue;
100 100
101 if (!pObj)continue; 101 if (pObj->GetType() == PDFOBJ_DICTIONARY) {
102 102 CPDF_Dictionary* pAnnotDic = (CPDF_Dictionary*)pObj;
103 if (pObj->GetType() == PDFOBJ_DICTIONARY) 103 CFX_ByteString sSubtype = pAnnotDic->GetString("Subtype");
104 { 104 if (sSubtype == "Popup")
105 CPDF_Dictionary* pAnnotDic = (CPDF_Dictionary*)p Obj; 105 continue;
106 CFX_ByteString sSubtype = pAnnotDic->GetString(" Subtype"); 106
107 if (sSubtype == "Popup")continue; 107 int nAnnotFlag = pAnnotDic->GetInteger("F");
108 108
109 int nAnnotFlag = pAnnotDic->GetInteger("F"); 109 if (nAnnotFlag & ANNOTFLAG_HIDDEN)
110 110 continue;
111 if(nAnnotFlag & ANNOTFLAG_HIDDEN) 111 if (nUsage == FLAT_NORMALDISPLAY) {
112 continue; 112 if (nAnnotFlag & ANNOTFLAG_INVISIBLE)
113 if(nUsage == FLAT_NORMALDISPLAY) 113 continue;
114 { 114 ParserStream(pPageDic, pAnnotDic, pRectArray, pObjectArray);
115 if(nAnnotFlag & ANNOTFLAG_INVISIBLE) 115 } else {
116 continue; 116 if (nAnnotFlag & ANNOTFLAG_PRINT)
117 ParserStream( pPageDic, pAnnotDic, pRect Array, pObjectArray ); 117 ParserStream(pPageDic, pAnnotDic, pRectArray, pObjectArray);
118 } 118 }
119 else 119 }
120 { 120 }
121 if(nAnnotFlag & ANNOTFLAG_PRINT) 121 return FLATTEN_SUCCESS;
122 ParserStream( pPageDic, pAnnotDi c, pRectArray, pObjectArray ); 122 } else {
123 } 123 return FLATTEN_NOTINGTODO;
124 } 124 }
125 } 125 }
126 return FLATTEN_SUCCESS; 126
127 }else{ 127 FX_FLOAT GetMinMaxValue(CPDF_RectArray& array,
128 return FLATTEN_NOTINGTODO; 128 FPDF_TYPE type,
129 } 129 FPDF_VALUE value) {
130 } 130 int nRects = array.GetSize();
131 131 FX_FLOAT fRet = 0.0f;
132 132
133 FX_FLOAT GetMinMaxValue( CPDF_RectArray& array, FPDF_TYPE type, FPDF_VALUE value ) 133 if (nRects <= 0)
134 { 134 return 0.0f;
135 int nRects = array.GetSize(); 135
136 FX_FLOAT fRet = 0.0f; 136 FX_FLOAT* pArray = new FX_FLOAT[nRects];
137 137 switch (value) {
138 if (nRects <= 0)return 0.0f; 138 case LEFT: {
139 139 for (int i = 0; i < nRects; i++)
140 FX_FLOAT* pArray = new FX_FLOAT[nRects]; 140 pArray[i] = CPDF_Rect(array.GetAt(i)).left;
141 switch(value) 141
142 { 142 break;
143 case LEFT: 143 }
144 { 144 case TOP: {
145 for (int i = 0; i < nRects; i++) 145 for (int i = 0; i < nRects; i++)
146 pArray[i] = CPDF_Rect(array.GetAt(i)).left; 146 pArray[i] = CPDF_Rect(array.GetAt(i)).top;
147 147
148 break; 148 break;
149 } 149 }
150 case TOP: 150 case RIGHT: {
151 { 151 for (int i = 0; i < nRects; i++)
152 for (int i = 0; i < nRects; i++) 152 pArray[i] = CPDF_Rect(array.GetAt(i)).right;
153 pArray[i] = CPDF_Rect(array.GetAt(i)).top; 153
154 154 break;
155 break; 155 }
156 } 156 case BOTTOM: {
157 case RIGHT: 157 for (int i = 0; i < nRects; i++)
158 { 158 pArray[i] = CPDF_Rect(array.GetAt(i)).bottom;
159 for (int i = 0; i < nRects; i++) 159
160 pArray[i] = CPDF_Rect(array.GetAt(i)).right; 160 break;
161 161 }
162 break; 162 default:
163 } 163 break;
164 case BOTTOM: 164 }
165 { 165 fRet = pArray[0];
166 for (int i = 0; i < nRects; i++) 166 if (type == MAX) {
167 pArray[i] = CPDF_Rect(array.GetAt(i)).bottom; 167 for (int i = 1; i < nRects; i++)
168 168 if (fRet <= pArray[i])
169 break; 169 fRet = pArray[i];
170 } 170 } else {
171 default: 171 for (int i = 1; i < nRects; i++)
172 break; 172 if (fRet >= pArray[i])
173 } 173 fRet = pArray[i];
174 fRet = pArray[0]; 174 }
175 if (type == MAX) 175 delete[] pArray;
176 { 176 return fRet;
177 for (int i = 1; i < nRects; i++) 177 }
178 if (fRet <= pArray[i]) 178
179 fRet = pArray[i]; 179 CPDF_Rect CalculateRect(CPDF_RectArray* pRectArray) {
180 } 180 CPDF_Rect rcRet;
181 else 181
182 { 182 rcRet.left = GetMinMaxValue(*pRectArray, MIN, LEFT);
183 for (int i = 1; i < nRects; i++) 183 rcRet.top = GetMinMaxValue(*pRectArray, MAX, TOP);
184 if (fRet >= pArray[i]) 184 rcRet.right = GetMinMaxValue(*pRectArray, MAX, RIGHT);
185 fRet = pArray[i]; 185 rcRet.bottom = GetMinMaxValue(*pRectArray, MIN, BOTTOM);
186 } 186
187 delete[] pArray; 187 return rcRet;
188 return fRet; 188 }
189 } 189
190 190 void SetPageContents(CFX_ByteString key,
191 CPDF_Rect CalculateRect( CPDF_RectArray * pRectArray ) 191 CPDF_Dictionary* pPage,
192 { 192 CPDF_Document* pDocument) {
193 193 CPDF_Object* pContentsObj = pPage->GetStream("Contents");
194 CPDF_Rect rcRet; 194 if (!pContentsObj) {
195 195 pContentsObj = pPage->GetArray("Contents");
196 rcRet.left = GetMinMaxValue(*pRectArray, MIN, LEFT); 196 }
197 rcRet.top = GetMinMaxValue(*pRectArray, MAX, TOP); 197
198 rcRet.right = GetMinMaxValue(*pRectArray, MAX, RIGHT); 198 if (!pContentsObj) {
199 rcRet.bottom = GetMinMaxValue(*pRectArray, MIN, BOTTOM); 199 // Create a new contents dictionary
200 200 if (!key.IsEmpty()) {
201 return rcRet; 201 CPDF_Stream* pNewContents =
202 } 202 FX_NEW CPDF_Stream(NULL, 0, FX_NEW CPDF_Dictionary);
203 203 if (!pNewContents)
204 204 return;
205 void SetPageContents(CFX_ByteString key, CPDF_Dictionary* pPage, CPDF_Document* pDocument) 205 pPage->SetAtReference(
206 { 206 "Contents", pDocument, pDocument->AddIndirectObject(pNewContents));
207 CPDF_Object* pContentsObj = pPage->GetStream("Contents"); 207
208 if (!pContentsObj) 208 CFX_ByteString sStream;
209 { 209 sStream.Format("q 1 0 0 1 0 0 cm /%s Do Q", (FX_LPCSTR)key);
210 pContentsObj = pPage->GetArray("Contents"); 210 pNewContents->SetData(
211 } 211 (FX_LPCBYTE)sStream, sStream.GetLength(), FALSE, FALSE);
212 212 }
213 if (!pContentsObj) 213 return;
214 { 214 }
215 //Create a new contents dictionary 215
216 if (!key.IsEmpty()) 216 int iType = pContentsObj->GetType();
217 { 217 CPDF_Array* pContentsArray = NULL;
218 CPDF_Stream* pNewContents = FX_NEW CPDF_Stream(NULL, 0, FX_NEW CPDF_Dictionary); 218
219 if (!pNewContents)return; 219 switch (iType) {
220 pPage->SetAtReference("Contents", pDocument, pDocument-> AddIndirectObject(pNewContents)); 220 case PDFOBJ_STREAM: {
221 221 pContentsArray = FX_NEW CPDF_Array;
222 CFX_ByteString sStream; 222 CPDF_Stream* pContents = (CPDF_Stream*)pContentsObj;
223 sStream.Format("q 1 0 0 1 0 0 cm /%s Do Q", (FX_LPCSTR)k ey); 223 FX_DWORD dwObjNum = pDocument->AddIndirectObject(pContents);
224 pNewContents->SetData((FX_LPCBYTE)sStream, sStream.GetLe ngth(), FALSE, FALSE); 224 CPDF_StreamAcc acc;
225 } 225 acc.LoadAllData(pContents);
226 return; 226 CFX_ByteString sStream = "q\n";
227 } 227 CFX_ByteString sBody =
228 228 CFX_ByteString((FX_LPCSTR)acc.GetData(), acc.GetSize());
229 int iType = pContentsObj->GetType(); 229 sStream = sStream + sBody + "\nQ";
230 CPDF_Array* pContentsArray = NULL; 230 pContents->SetData(
231 231 (FX_LPCBYTE)sStream, sStream.GetLength(), FALSE, FALSE);
232 switch(iType) 232 pContentsArray->AddReference(pDocument, dwObjNum);
233 { 233 break;
234 case PDFOBJ_STREAM: 234 }
235 { 235
236 pContentsArray = FX_NEW CPDF_Array; 236 case PDFOBJ_ARRAY: {
237 CPDF_Stream* pContents = (CPDF_Stream*)pContentsObj; 237 pContentsArray = (CPDF_Array*)pContentsObj;
238 FX_DWORD dwObjNum = pDocument->AddIndirectObject(pConten ts); 238 break;
239 CPDF_StreamAcc acc; 239 }
240 acc.LoadAllData(pContents); 240 default:
241 CFX_ByteString sStream = "q\n"; 241 break;
242 CFX_ByteString sBody = CFX_ByteString((FX_LPCSTR)acc.Get Data(), acc.GetSize()); 242 }
243 sStream = sStream + sBody + "\nQ"; 243
244 pContents->SetData((FX_LPCBYTE)sStream, sStream.GetLengt h(), FALSE, FALSE); 244 if (!pContentsArray)
245 pContentsArray->AddReference(pDocument, dwObjNum); 245 return;
246 break; 246
247 } 247 FX_DWORD dwObjNum = pDocument->AddIndirectObject(pContentsArray);
248 248 pPage->SetAtReference("Contents", pDocument, dwObjNum);
249 case PDFOBJ_ARRAY: 249
250 { 250 if (!key.IsEmpty()) {
251 pContentsArray = (CPDF_Array*)pContentsObj; 251 CPDF_Stream* pNewContents =
252 break; 252 FX_NEW CPDF_Stream(NULL, 0, FX_NEW CPDF_Dictionary);
253 } 253 dwObjNum = pDocument->AddIndirectObject(pNewContents);
254 default: 254 pContentsArray->AddReference(pDocument, dwObjNum);
255 break; 255
256 } 256 CFX_ByteString sStream;
257 257 sStream.Format("q 1 0 0 1 0 0 cm /%s Do Q", (FX_LPCSTR)key);
258 if (!pContentsArray)return; 258 pNewContents->SetData(
259 259 (FX_LPCBYTE)sStream, sStream.GetLength(), FALSE, FALSE);
260 FX_DWORD dwObjNum = pDocument->AddIndirectObject(pContentsArray); 260 }
261 pPage->SetAtReference("Contents", pDocument, dwObjNum); 261 }
262 262
263 if (!key.IsEmpty()) 263 CFX_AffineMatrix GetMatrix(CPDF_Rect rcAnnot,
264 { 264 CPDF_Rect rcStream,
265 CPDF_Stream* pNewContents = FX_NEW CPDF_Stream(NULL, 0, FX_NEW C PDF_Dictionary); 265 CFX_AffineMatrix matrix) {
266 dwObjNum = pDocument->AddIndirectObject(pNewContents); 266 if (rcStream.IsEmpty())
267 pContentsArray->AddReference(pDocument, dwObjNum); 267 return CFX_AffineMatrix();
268 268
269 CFX_ByteString sStream; 269 matrix.TransformRect(rcStream);
270 sStream.Format("q 1 0 0 1 0 0 cm /%s Do Q", (FX_LPCSTR)key); 270 rcStream.Normalize();
271 pNewContents->SetData((FX_LPCBYTE)sStream, sStream.GetLength(), FALSE, FALSE); 271
272 } 272 FX_FLOAT a = rcAnnot.Width() / rcStream.Width();
273 } 273 FX_FLOAT d = rcAnnot.Height() / rcStream.Height();
274 274
275 CFX_AffineMatrix GetMatrix(CPDF_Rect rcAnnot, CPDF_Rect rcStream, CFX_AffineMatr ix matrix) 275 FX_FLOAT e = rcAnnot.left - rcStream.left * a;
276 { 276 FX_FLOAT f = rcAnnot.bottom - rcStream.bottom * d;
277 if(rcStream.IsEmpty()) 277 return CFX_AffineMatrix(a, 0, 0, d, e, f);
278 return CFX_AffineMatrix(); 278 }
279 279
280 matrix.TransformRect(rcStream); 280 void GetOffset(FX_FLOAT& fa,
281 rcStream.Normalize(); 281 FX_FLOAT& fd,
282 282 FX_FLOAT& fe,
283 FX_FLOAT a = rcAnnot.Width()/rcStream.Width(); 283 FX_FLOAT& ff,
284 FX_FLOAT d = rcAnnot.Height()/rcStream.Height(); 284 CPDF_Rect rcAnnot,
285 285 CPDF_Rect rcStream,
286 FX_FLOAT e = rcAnnot.left - rcStream.left * a; 286 CFX_AffineMatrix matrix) {
287 FX_FLOAT f = rcAnnot.bottom - rcStream.bottom * d; 287 FX_FLOAT fStreamWidth = 0.0f;
288 return CFX_AffineMatrix(a, 0, 0, d, e, f); 288 FX_FLOAT fStreamHeight = 0.0f;
289 } 289
290 290 if (matrix.a != 0 && matrix.d != 0) {
291 void GetOffset(FX_FLOAT& fa, FX_FLOAT& fd, FX_FLOAT& fe, FX_FLOAT& ff, CPDF_Rect rcAnnot, CPDF_Rect rcStream, CFX_AffineMatrix matrix) 291 fStreamWidth = rcStream.right - rcStream.left;
292 { 292 fStreamHeight = rcStream.top - rcStream.bottom;
293 FX_FLOAT fStreamWidth = 0.0f; 293 } else {
294 FX_FLOAT fStreamHeight = 0.0f; 294 fStreamWidth = rcStream.top - rcStream.bottom;
295 295 fStreamHeight = rcStream.right - rcStream.left;
296 296 }
297 297
298 if (matrix.a != 0 && matrix.d != 0) 298 FX_FLOAT x1 =
299 { 299 matrix.a * rcStream.left + matrix.c * rcStream.bottom + matrix.e;
300 fStreamWidth = rcStream.right - rcStream.left; 300 FX_FLOAT y1 =
301 fStreamHeight = rcStream.top - rcStream.bottom; 301 matrix.b * rcStream.left + matrix.d * rcStream.bottom + matrix.f;
302 } 302 FX_FLOAT x2 = matrix.a * rcStream.left + matrix.c * rcStream.top + matrix.e;
303 else 303 FX_FLOAT y2 = matrix.b * rcStream.left + matrix.d * rcStream.top + matrix.f;
304 { 304 FX_FLOAT x3 =
305 fStreamWidth = rcStream.top - rcStream.bottom; 305 matrix.a * rcStream.right + matrix.c * rcStream.bottom + matrix.e;
306 fStreamHeight = rcStream.right - rcStream.left; 306 FX_FLOAT y3 =
307 } 307 matrix.b * rcStream.right + matrix.d * rcStream.bottom + matrix.f;
308 308 FX_FLOAT x4 = matrix.a * rcStream.right + matrix.c * rcStream.top + matrix.e;
309 FX_FLOAT x1 = matrix.a * rcStream.left + matrix.c * rcStream.bottom + ma trix.e; 309 FX_FLOAT y4 = matrix.b * rcStream.right + matrix.d * rcStream.top + matrix.f;
310 FX_FLOAT y1 = matrix.b * rcStream.left + matrix.d * rcStream.bottom + ma trix.f; 310
311 FX_FLOAT x2 = matrix.a * rcStream.left + matrix.c * rcStream.top + matri x.e; 311 FX_FLOAT left = FX_MIN(FX_MIN(x1, x2), FX_MIN(x3, x4));
312 FX_FLOAT y2 = matrix.b * rcStream.left + matrix.d * rcStream.top + matri x.f; 312 FX_FLOAT bottom = FX_MIN(FX_MIN(y1, y2), FX_MIN(y3, y4));
313 FX_FLOAT x3 = matrix.a * rcStream.right + matrix.c * rcStream.bottom + m atrix.e; 313
314 FX_FLOAT y3 = matrix.b * rcStream.right + matrix.d * rcStream.bottom + m atrix.f; 314 fa = (rcAnnot.right - rcAnnot.left) / fStreamWidth;
315 FX_FLOAT x4 = matrix.a * rcStream.right + matrix.c * rcStream.top + matr ix.e; 315 fd = (rcAnnot.top - rcAnnot.bottom) / fStreamHeight;
316 FX_FLOAT y4 = matrix.b * rcStream.right + matrix.d * rcStream.top + matr ix.f; 316 fe = rcAnnot.left - left * fa;
317 317 ff = rcAnnot.bottom - bottom * fd;
318 FX_FLOAT left = FX_MIN(FX_MIN(x1, x2), FX_MIN(x3, x4)); 318 }
319 FX_FLOAT bottom = FX_MIN(FX_MIN(y1, y2), FX_MIN(y3, y4)); 319
320 320 DLLEXPORT int STDCALL FPDFPage_Flatten(FPDF_PAGE page, int nFlag) {
321 fa = (rcAnnot.right - rcAnnot.left)/fStreamWidth; 321 if (!page) {
322 fd = (rcAnnot.top - rcAnnot.bottom)/fStreamHeight; 322 return FLATTEN_FAIL;
323 fe = rcAnnot.left - left * fa; 323 }
324 ff = rcAnnot.bottom - bottom * fd; 324
325 } 325 CPDF_Page* pPage = (CPDF_Page*)(page);
326 326 CPDF_Document* pDocument = pPage->m_pDocument;
327 327 CPDF_Dictionary* pPageDict = pPage->m_pFormDict;
328 DLLEXPORT int STDCALL FPDFPage_Flatten( FPDF_PAGE page, int nFlag) 328
329 { 329 if (!pDocument || !pPageDict) {
330 if (!page) 330 return FLATTEN_FAIL;
331 { 331 }
332 return FLATTEN_FAIL; 332
333 } 333 CPDF_ObjectArray ObjectArray;
334 334 CPDF_RectArray RectArray;
335 CPDF_Page * pPage = (CPDF_Page*)( page ); 335
336 CPDF_Document * pDocument = pPage->m_pDocument; 336 int iRet = FLATTEN_FAIL;
337 CPDF_Dictionary * pPageDict = pPage->m_pFormDict; 337 iRet = ParserAnnots(pDocument, pPageDict, &RectArray, &ObjectArray, nFlag);
338 338 if (iRet == FLATTEN_NOTINGTODO) {
339 if ( !pDocument || !pPageDict ) 339 return FLATTEN_NOTINGTODO;
340 { 340 } else if (iRet == FLATTEN_FAIL) {
341 return FLATTEN_FAIL; 341 return FLATTEN_FAIL;
342 } 342 }
343 343
344 CPDF_ObjectArray ObjectArray; 344 CPDF_Rect rcOriginalCB;
345 CPDF_RectArray RectArray; 345 CPDF_Rect rcMerger = CalculateRect(&RectArray);
346 346 CPDF_Rect rcOriginalMB = pPageDict->GetRect("MediaBox");
347 int iRet = FLATTEN_FAIL; 347
348 iRet = ParserAnnots( pDocument, pPageDict, &RectArray, &ObjectArray, nFl ag); 348 if (pPageDict->KeyExist("CropBox"))
349 if (iRet == FLATTEN_NOTINGTODO) 349 rcOriginalMB = pPageDict->GetRect("CropBox");
350 { 350
351 return FLATTEN_NOTINGTODO; 351 if (rcOriginalMB.IsEmpty()) {
352 }else if (iRet == FLATTEN_FAIL) 352 rcOriginalMB = CPDF_Rect(0.0f, 0.0f, 612.0f, 792.0f);
353 { 353 }
354 return FLATTEN_FAIL; 354
355 } 355 rcMerger.left =
356 356 rcMerger.left < rcOriginalMB.left ? rcOriginalMB.left : rcMerger.left;
357 CPDF_Rect rcOriginalCB; 357 rcMerger.right =
358 CPDF_Rect rcMerger = CalculateRect( &RectArray ); 358 rcMerger.right > rcOriginalMB.right ? rcOriginalMB.right : rcMerger.right;
359 CPDF_Rect rcOriginalMB = pPageDict->GetRect("MediaBox"); 359 rcMerger.top =
360 360 rcMerger.top > rcOriginalMB.top ? rcOriginalMB.top : rcMerger.top;
361 if (pPageDict->KeyExist("CropBox")) 361 rcMerger.bottom = rcMerger.bottom < rcOriginalMB.bottom ? rcOriginalMB.bottom
362 rcOriginalMB = pPageDict->GetRect("CropBox"); 362 : rcMerger.bottom;
363 363
364 if (rcOriginalMB.IsEmpty()) 364 if (pPageDict->KeyExist("ArtBox"))
365 { 365 rcOriginalCB = pPageDict->GetRect("ArtBox");
366 rcOriginalMB = CPDF_Rect(0.0f, 0.0f, 612.0f, 792.0f); 366 else
367 } 367 rcOriginalCB = rcOriginalMB;
368 368
369 rcMerger.left = rcMerger.left < rcOriginalMB.left? rcOriginalMB.left : r cMerger.left; 369 if (!rcOriginalMB.IsEmpty()) {
370 rcMerger.right = rcMerger.right > rcOriginalMB.right? rcOriginalMB.right : rcMerger.right; 370 CPDF_Array* pMediaBox = FX_NEW CPDF_Array();
371 rcMerger.top = rcMerger.top > rcOriginalMB.top? rcOriginalMB.top : rcMer ger.top; 371
372 rcMerger.bottom = rcMerger.bottom < rcOriginalMB.bottom? rcOriginalMB.bo ttom : rcMerger.bottom; 372 pMediaBox->Add(FX_NEW CPDF_Number(rcOriginalMB.left));
373 373 pMediaBox->Add(FX_NEW CPDF_Number(rcOriginalMB.bottom));
374 if (pPageDict->KeyExist("ArtBox")) 374 pMediaBox->Add(FX_NEW CPDF_Number(rcOriginalMB.right));
375 rcOriginalCB = pPageDict->GetRect("ArtBox"); 375 pMediaBox->Add(FX_NEW CPDF_Number(rcOriginalMB.top));
376 else 376
377 rcOriginalCB = rcOriginalMB; 377 pPageDict->SetAt("MediaBox", pMediaBox);
378 378 }
379 if (!rcOriginalMB.IsEmpty()) 379
380 { 380 if (!rcOriginalCB.IsEmpty()) {
381 CPDF_Array* pMediaBox = FX_NEW CPDF_Array(); 381 CPDF_Array* pCropBox = FX_NEW CPDF_Array();
382 382 pCropBox->Add(FX_NEW CPDF_Number(rcOriginalCB.left));
383 pMediaBox->Add(FX_NEW CPDF_Number(rcOriginalMB.left)); 383 pCropBox->Add(FX_NEW CPDF_Number(rcOriginalCB.bottom));
384 pMediaBox->Add(FX_NEW CPDF_Number(rcOriginalMB.bottom)); 384 pCropBox->Add(FX_NEW CPDF_Number(rcOriginalCB.right));
385 pMediaBox->Add(FX_NEW CPDF_Number(rcOriginalMB.right)); 385 pCropBox->Add(FX_NEW CPDF_Number(rcOriginalCB.top));
386 pMediaBox->Add(FX_NEW CPDF_Number(rcOriginalMB.top)); 386 pPageDict->SetAt("ArtBox", pCropBox);
387 387 }
388 pPageDict->SetAt("MediaBox",pMediaBox); 388
389 } 389 CPDF_Dictionary* pRes = NULL;
390 390 pRes = pPageDict->GetDict("Resources");
391 if (!rcOriginalCB.IsEmpty()) 391 if (!pRes) {
392 { 392 pRes = FX_NEW CPDF_Dictionary;
393 CPDF_Array* pCropBox = FX_NEW CPDF_Array(); 393 pPageDict->SetAt("Resources", pRes);
394 pCropBox->Add(FX_NEW CPDF_Number(rcOriginalCB.left)); 394 }
395 pCropBox->Add(FX_NEW CPDF_Number(rcOriginalCB.bottom)); 395
396 pCropBox->Add(FX_NEW CPDF_Number(rcOriginalCB.right)); 396 CPDF_Stream* pNewXObject =
397 pCropBox->Add(FX_NEW CPDF_Number(rcOriginalCB.top)); 397 FX_NEW CPDF_Stream(NULL, 0, FX_NEW CPDF_Dictionary);
398 pPageDict->SetAt("ArtBox", pCropBox); 398 FX_DWORD dwObjNum = pDocument->AddIndirectObject(pNewXObject);
399 } 399 CPDF_Dictionary* pPageXObject = pRes->GetDict("XObject");
400 400 if (!pPageXObject) {
401 CPDF_Dictionary* pRes = NULL; 401 pPageXObject = FX_NEW CPDF_Dictionary;
402 pRes = pPageDict->GetDict("Resources"); 402 pRes->SetAt("XObject", pPageXObject);
403 if (!pRes) 403 }
404 { 404
405 pRes = FX_NEW CPDF_Dictionary; 405 CFX_ByteString key = "";
406 pPageDict->SetAt( "Resources", pRes ); 406 int nStreams = ObjectArray.GetSize();
407 } 407
408 408 if (nStreams > 0) {
409 CPDF_Stream* pNewXObject = FX_NEW CPDF_Stream(NULL, 0, FX_NEW CPDF_Dicti onary); 409 for (int iKey = 0; /*iKey < 100*/; iKey++) {
410 FX_DWORD dwObjNum = pDocument->AddIndirectObject(pNewXObject); 410 char sExtend[5] = { 0 };
411 CPDF_Dictionary* pPageXObject = pRes->GetDict("XObject"); 411 FXSYS_itoa(iKey, sExtend, 10);
412 if (!pPageXObject) 412 key = CFX_ByteString("FFT") + CFX_ByteString(sExtend);
413 { 413
414 pPageXObject = FX_NEW CPDF_Dictionary; 414 if (!pPageXObject->KeyExist(key))
415 pRes->SetAt("XObject", pPageXObject); 415 break;
416 } 416 }
417 417 }
418 CFX_ByteString key = ""; 418
419 int nStreams = ObjectArray.GetSize(); 419 SetPageContents(key, pPageDict, pDocument);
420 420
421 if (nStreams > 0) 421 CPDF_Dictionary* pNewXORes = NULL;
422 { 422
423 for (int iKey = 0; /*iKey < 100*/; iKey++) 423 if (!key.IsEmpty()) {
424 { 424 pPageXObject->SetAtReference(key, pDocument, dwObjNum);
425 char sExtend[5] = {0}; 425 CPDF_Dictionary* pNewOXbjectDic = pNewXObject->GetDict();
426 FXSYS_itoa(iKey, sExtend, 10); 426 pNewXORes = FX_NEW CPDF_Dictionary;
427 key = CFX_ByteString("FFT") + CFX_ByteString(sExtend); 427 pNewOXbjectDic->SetAt("Resources", pNewXORes);
428 428 pNewOXbjectDic->SetAtName("Type", "XObject");
429 if (!pPageXObject->KeyExist(key)) 429 pNewOXbjectDic->SetAtName("Subtype", "Form");
430 break; 430 pNewOXbjectDic->SetAtInteger("FormType", 1);
431 } 431 pNewOXbjectDic->SetAtName("Name", "FRM");
432 } 432 CPDF_Rect rcBBox = pPageDict->GetRect("ArtBox");
433 433 pNewOXbjectDic->SetAtRect("BBox", rcBBox);
434 SetPageContents(key, pPageDict, pDocument); 434 }
435 435
436 CPDF_Dictionary* pNewXORes = NULL; 436 for (int i = 0; i < nStreams; i++) {
437 437 CPDF_Dictionary* pAnnotDic = ObjectArray.GetAt(i);
438 if (!key.IsEmpty()) 438 if (!pAnnotDic)
439 { 439 continue;
440 pPageXObject->SetAtReference(key, pDocument, dwObjNum); 440
441 CPDF_Dictionary* pNewOXbjectDic = pNewXObject->GetDict(); 441 CPDF_Rect rcAnnot = pAnnotDic->GetRect("Rect");
442 pNewXORes = FX_NEW CPDF_Dictionary; 442 rcAnnot.Normalize();
443 pNewOXbjectDic->SetAt("Resources", pNewXORes); 443
444 pNewOXbjectDic->SetAtName("Type", "XObject"); 444 CFX_ByteString sAnnotState = pAnnotDic->GetString("AS");
445 pNewOXbjectDic->SetAtName("Subtype", "Form"); 445 CPDF_Dictionary* pAnnotAP = pAnnotDic->GetDict("AP");
446 pNewOXbjectDic->SetAtInteger("FormType", 1); 446 if (!pAnnotAP)
447 pNewOXbjectDic->SetAtName("Name", "FRM"); 447 continue;
448 CPDF_Rect rcBBox = pPageDict->GetRect("ArtBox"); 448
449 pNewOXbjectDic->SetAtRect("BBox", rcBBox); 449 CPDF_Stream* pAPStream = pAnnotAP->GetStream("N");
450 } 450 if (!pAPStream) {
451 451 CPDF_Dictionary* pAPDic = pAnnotAP->GetDict("N");
452 for (int i = 0; i < nStreams; i++) 452 if (!pAPDic)
453 { 453 continue;
454 CPDF_Dictionary* pAnnotDic = ObjectArray.GetAt(i); 454
455 if (!pAnnotDic)continue; 455 if (!sAnnotState.IsEmpty()) {
456 456 pAPStream = pAPDic->GetStream(sAnnotState);
457 CPDF_Rect rcAnnot = pAnnotDic->GetRect("Rect"); 457 } else {
458 rcAnnot.Normalize(); 458 FX_POSITION pos = pAPDic->GetStartPos();
459 459 if (pos) {
460 CFX_ByteString sAnnotState = pAnnotDic->GetString("AS"); 460 CFX_ByteString sKey;
461 CPDF_Dictionary* pAnnotAP = pAnnotDic->GetDict("AP"); 461 CPDF_Object* pFirstObj = pAPDic->GetNextElement(pos, sKey);
462 if (!pAnnotAP)continue; 462 if (pFirstObj) {
463 463 if (pFirstObj->GetType() == PDFOBJ_REFERENCE)
464 CPDF_Stream* pAPStream = pAnnotAP->GetStream("N"); 464 pFirstObj = pFirstObj->GetDirect();
465 if (!pAPStream) 465
466 { 466 if (pFirstObj->GetType() != PDFOBJ_STREAM)
467 CPDF_Dictionary* pAPDic = pAnnotAP->GetDict("N"); 467 continue;
468 if (!pAPDic)continue; 468
469 469 pAPStream = (CPDF_Stream*)pFirstObj;
470 if (!sAnnotState.IsEmpty()) 470 }
471 { 471 }
472 pAPStream = pAPDic->GetStream(sAnnotState); 472 }
473 } 473 }
474 else 474
475 { 475 if (!pAPStream)
476 FX_POSITION pos = pAPDic->GetStartPos(); 476 continue;
477 if (pos) 477
478 { 478 CPDF_Dictionary* pAPDic = pAPStream->GetDict();
479 CFX_ByteString sKey; 479 CFX_AffineMatrix matrix = pAPDic->GetMatrix("Matrix");
480 CPDF_Object* pFirstObj = pAPDic->GetNext Element(pos, sKey); 480
481 if (pFirstObj) 481 CPDF_Rect rcStream;
482 { 482 if (pAPDic->KeyExist("Rect"))
483 if (pFirstObj->GetType() == PDFO BJ_REFERENCE) 483 rcStream = pAPDic->GetRect("Rect");
484 pFirstObj = pFirstObj->G etDirect(); 484 else if (pAPDic->KeyExist("BBox"))
485 485 rcStream = pAPDic->GetRect("BBox");
486 if (pFirstObj->GetType() != PDFO BJ_STREAM) 486
487 continue; 487 if (rcStream.IsEmpty())
488 488 continue;
489 pAPStream = (CPDF_Stream*)pFirst Obj; 489
490 } 490 CPDF_Object* pObj = pAPStream;
491 } 491
492 } 492 if (pObj) {
493 } 493 CPDF_Dictionary* pObjDic = pObj->GetDict();
494 494 if (pObjDic) {
495 if (!pAPStream)continue; 495 pObjDic->SetAtName("Type", "XObject");
496 496 pObjDic->SetAtName("Subtype", "Form");
497 CPDF_Dictionary* pAPDic = pAPStream->GetDict(); 497 }
498 CFX_AffineMatrix matrix = pAPDic->GetMatrix("Matrix"); 498 }
499 499
500 CPDF_Rect rcStream; 500 CPDF_Dictionary* pXObject = pNewXORes->GetDict("XObject");
501 if (pAPDic->KeyExist("Rect")) 501 if (!pXObject) {
502 rcStream = pAPDic->GetRect("Rect"); 502 pXObject = FX_NEW CPDF_Dictionary;
503 else if (pAPDic->KeyExist("BBox")) 503 pNewXORes->SetAt("XObject", pXObject);
504 rcStream = pAPDic->GetRect("BBox"); 504 }
505 505
506 if (rcStream.IsEmpty())continue; 506 CFX_ByteString sFormName;
507 507 sFormName.Format("F%d", i);
508 CPDF_Object* pObj = pAPStream; 508 FX_DWORD dwObjNum = pDocument->AddIndirectObject(pObj);
509 509 pXObject->SetAtReference(sFormName, pDocument, dwObjNum);
510 if (pObj) 510
511 { 511 CPDF_StreamAcc acc;
512 CPDF_Dictionary* pObjDic = pObj->GetDict(); 512 acc.LoadAllData(pNewXObject);
513 if (pObjDic) 513
514 { 514 FX_LPCBYTE pData = acc.GetData();
515 pObjDic->SetAtName("Type", "XObject"); 515 CFX_ByteString sStream(pData, acc.GetSize());
516 pObjDic->SetAtName("Subtype", "Form"); 516 CFX_ByteString sTemp;
517 } 517
518 } 518 if (matrix.IsIdentity()) {
519 519 matrix.a = 1.0f;
520 CPDF_Dictionary* pXObject = pNewXORes->GetDict("XObject"); 520 matrix.b = 0.0f;
521 if (!pXObject) 521 matrix.c = 0.0f;
522 { 522 matrix.d = 1.0f;
523 pXObject = FX_NEW CPDF_Dictionary; 523 matrix.e = 0.0f;
524 pNewXORes->SetAt("XObject", pXObject); 524 matrix.f = 0.0f;
525 } 525 }
526 526
527 CFX_ByteString sFormName; 527 CFX_AffineMatrix m = GetMatrix(rcAnnot, rcStream, matrix);
528 sFormName.Format("F%d", i); 528 sTemp.Format("q %f 0 0 %f %f %f cm /%s Do Q\n",
529 FX_DWORD dwObjNum = pDocument->AddIndirectObject(pObj); 529 m.a,
530 pXObject->SetAtReference(sFormName, pDocument, dwObjNum); 530 m.d,
531 531 m.e,
532 CPDF_StreamAcc acc; 532 m.f,
533 acc.LoadAllData(pNewXObject); 533 (FX_LPCSTR)sFormName);
534 534 sStream += sTemp;
535 FX_LPCBYTE pData = acc.GetData(); 535
536 CFX_ByteString sStream(pData, acc.GetSize()); 536 pNewXObject->SetData(
537 CFX_ByteString sTemp; 537 (FX_LPCBYTE)sStream, sStream.GetLength(), FALSE, FALSE);
538 538 }
539 if (matrix.IsIdentity()) 539 pPageDict->RemoveAt("Annots");
540 { 540
541 matrix.a = 1.0f; 541 ObjectArray.RemoveAll();
542 matrix.b = 0.0f; 542 RectArray.RemoveAll();
543 matrix.c = 0.0f; 543
544 matrix.d = 1.0f; 544 return FLATTEN_SUCCESS;
545 matrix.e = 0.0f; 545 }
546 matrix.f = 0.0f;
547 }
548
549 CFX_AffineMatrix m = GetMatrix(rcAnnot, rcStream, matrix);
550 sTemp.Format("q %f 0 0 %f %f %f cm /%s Do Q\n", m.a, m.d, m.e, m .f, (FX_LPCSTR)sFormName);
551 sStream += sTemp;
552
553 pNewXObject->SetData((FX_LPCBYTE)sStream, sStream.GetLength(), F ALSE, FALSE);
554 }
555 pPageDict->RemoveAt( "Annots" );
556
557 ObjectArray.RemoveAll();
558 RectArray.RemoveAll();
559
560 return FLATTEN_SUCCESS;
561 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698