OLD | NEW |
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 "xfa/fxfa/include/xfa_ffdoc.h" | 7 #include "xfa/fxfa/include/xfa_ffdoc.h" |
8 | 8 |
| 9 #include <algorithm> |
| 10 |
9 #include "core/fpdfapi/fpdf_parser/include/cpdf_array.h" | 11 #include "core/fpdfapi/fpdf_parser/include/cpdf_array.h" |
10 #include "core/fpdfapi/fpdf_parser/include/cpdf_document.h" | 12 #include "core/fpdfapi/fpdf_parser/include/cpdf_document.h" |
11 #include "core/fpdfdoc/include/fpdf_doc.h" | 13 #include "core/fpdfdoc/include/fpdf_doc.h" |
12 #include "core/fxcrt/include/fx_ext.h" | 14 #include "core/fxcrt/include/fx_ext.h" |
13 #include "core/fxcrt/include/fx_memory.h" | 15 #include "core/fxcrt/include/fx_memory.h" |
14 #include "xfa/fde/xml/fde_xml_imp.h" | 16 #include "xfa/fde/xml/fde_xml_imp.h" |
15 #include "xfa/fgas/crt/fgas_algorithm.h" | 17 #include "xfa/fgas/crt/fgas_algorithm.h" |
16 #include "xfa/fwl/core/ifwl_notedriver.h" | 18 #include "xfa/fwl/core/ifwl_notedriver.h" |
17 #include "xfa/fxfa/app/xfa_ffnotify.h" | 19 #include "xfa/fxfa/app/xfa_ffnotify.h" |
18 #include "xfa/fxfa/include/xfa_checksum.h" | 20 #include "xfa/fxfa/include/xfa_checksum.h" |
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
181 CXFA_Node* pDynamicRender = | 183 CXFA_Node* pDynamicRender = |
182 pAcrobat7->GetFirstChildByClass(XFA_ELEMENT_DynamicRender); | 184 pAcrobat7->GetFirstChildByClass(XFA_ELEMENT_DynamicRender); |
183 if (!pDynamicRender) { | 185 if (!pDynamicRender) { |
184 return; | 186 return; |
185 } | 187 } |
186 CFX_WideString wsType; | 188 CFX_WideString wsType; |
187 if (pDynamicRender->TryContent(wsType) && wsType == FX_WSTRC(L"required")) { | 189 if (pDynamicRender->TryContent(wsType) && wsType == FX_WSTRC(L"required")) { |
188 m_dwDocType = XFA_DOCTYPE_Dynamic; | 190 m_dwDocType = XFA_DOCTYPE_Dynamic; |
189 } | 191 } |
190 } | 192 } |
| 193 |
191 CXFA_FFDocView* CXFA_FFDoc::CreateDocView(uint32_t dwView) { | 194 CXFA_FFDocView* CXFA_FFDoc::CreateDocView(uint32_t dwView) { |
192 CXFA_FFDocView* pDocView = | 195 if (!m_TypeToDocViewMap[dwView]) |
193 (CXFA_FFDocView*)m_mapTypeToDocView.GetValueAt((void*)(uintptr_t)dwView); | 196 m_TypeToDocViewMap[dwView].reset(new CXFA_FFDocView(this)); |
194 if (!pDocView) { | 197 |
195 pDocView = new CXFA_FFDocView(this); | 198 return m_TypeToDocViewMap[dwView].get(); |
196 m_mapTypeToDocView.SetAt((void*)(uintptr_t)dwView, pDocView); | 199 } |
| 200 |
| 201 CXFA_FFDocView* CXFA_FFDoc::GetDocView(CXFA_LayoutProcessor* pLayout) { |
| 202 for (const auto& pair : m_TypeToDocViewMap) { |
| 203 if (pair.second->GetXFALayout() == pLayout) |
| 204 return pair.second.get(); |
197 } | 205 } |
198 return pDocView; | 206 return nullptr; |
199 } | 207 } |
200 CXFA_FFDocView* CXFA_FFDoc::GetDocView(CXFA_LayoutProcessor* pLayout) { | 208 |
201 FX_POSITION ps = m_mapTypeToDocView.GetStartPosition(); | 209 CXFA_FFDocView* CXFA_FFDoc::GetDocView() { |
202 while (ps) { | 210 auto it = m_TypeToDocViewMap.begin(); |
203 void* pType; | 211 return it != m_TypeToDocViewMap.end() ? it->second.get() : nullptr; |
204 CXFA_FFDocView* pDocView; | |
205 m_mapTypeToDocView.GetNextAssoc(ps, pType, (void*&)pDocView); | |
206 if (pDocView->GetXFALayout() == pLayout) { | |
207 return pDocView; | |
208 } | |
209 } | |
210 return NULL; | |
211 } | 212 } |
212 CXFA_FFDocView* CXFA_FFDoc::GetDocView() { | 213 |
213 FX_POSITION ps = m_mapTypeToDocView.GetStartPosition(); | |
214 if (ps) { | |
215 void* pType; | |
216 CXFA_FFDocView* pDocView; | |
217 m_mapTypeToDocView.GetNextAssoc(ps, pType, (void*&)pDocView); | |
218 return pDocView; | |
219 } | |
220 return NULL; | |
221 } | |
222 FX_BOOL CXFA_FFDoc::OpenDoc(IFX_FileRead* pStream, FX_BOOL bTakeOverFile) { | 214 FX_BOOL CXFA_FFDoc::OpenDoc(IFX_FileRead* pStream, FX_BOOL bTakeOverFile) { |
223 m_bOwnStream = bTakeOverFile; | 215 m_bOwnStream = bTakeOverFile; |
224 m_pStream = pStream; | 216 m_pStream = pStream; |
225 return TRUE; | 217 return TRUE; |
226 } | 218 } |
227 FX_BOOL CXFA_FFDoc::OpenDoc(CPDF_Document* pPDFDoc) { | 219 FX_BOOL CXFA_FFDoc::OpenDoc(CPDF_Document* pPDFDoc) { |
228 if (pPDFDoc == NULL) { | 220 if (pPDFDoc == NULL) { |
229 return FALSE; | 221 return FALSE; |
230 } | 222 } |
231 CPDF_Dictionary* pRoot = pPDFDoc->GetRoot(); | 223 CPDF_Dictionary* pRoot = pPDFDoc->GetRoot(); |
(...skipping 25 matching lines...) Expand all Loading... |
257 m_pPDFDoc = pPDFDoc; | 249 m_pPDFDoc = pPDFDoc; |
258 if (m_pStream) { | 250 if (m_pStream) { |
259 m_pStream->Release(); | 251 m_pStream->Release(); |
260 m_pStream = NULL; | 252 m_pStream = NULL; |
261 } | 253 } |
262 m_pStream = pFileRead; | 254 m_pStream = pFileRead; |
263 m_bOwnStream = TRUE; | 255 m_bOwnStream = TRUE; |
264 return TRUE; | 256 return TRUE; |
265 } | 257 } |
266 FX_BOOL CXFA_FFDoc::CloseDoc() { | 258 FX_BOOL CXFA_FFDoc::CloseDoc() { |
267 FX_POSITION psClose = m_mapTypeToDocView.GetStartPosition(); | 259 for (const auto& pair : m_TypeToDocViewMap) |
268 while (psClose) { | 260 pair.second->RunDocClose(); |
269 void* pType; | 261 |
270 CXFA_FFDocView* pDocView; | 262 if (m_pDocument) |
271 m_mapTypeToDocView.GetNextAssoc(psClose, pType, (void*&)pDocView); | 263 m_pDocument->ClearLayoutData(); |
272 pDocView->RunDocClose(); | 264 |
| 265 m_TypeToDocViewMap.clear(); |
| 266 |
| 267 if (m_pDocument) { |
| 268 m_pDocument->GetParser()->Release(); |
| 269 m_pDocument = nullptr; |
273 } | 270 } |
274 if (m_pDocument) { | 271 |
275 m_pDocument->ClearLayoutData(); | 272 delete m_pNotify; |
276 } | 273 m_pNotify = nullptr; |
277 FX_POSITION ps = m_mapTypeToDocView.GetStartPosition(); | 274 |
278 while (ps) { | |
279 void* pType; | |
280 CXFA_FFDocView* pDocView; | |
281 m_mapTypeToDocView.GetNextAssoc(ps, pType, (void*&)pDocView); | |
282 delete pDocView; | |
283 } | |
284 m_mapTypeToDocView.RemoveAll(); | |
285 if (m_pDocument) { | |
286 IXFA_Parser* pParser = m_pDocument->GetParser(); | |
287 pParser->Release(); | |
288 m_pDocument = NULL; | |
289 } | |
290 if (m_pNotify) { | |
291 delete m_pNotify; | |
292 m_pNotify = NULL; | |
293 } | |
294 m_pApp->GetXFAFontMgr()->ReleaseDocFonts(this); | 275 m_pApp->GetXFAFontMgr()->ReleaseDocFonts(this); |
| 276 |
295 if (m_dwDocType != XFA_DOCTYPE_XDP && m_pStream && m_bOwnStream) { | 277 if (m_dwDocType != XFA_DOCTYPE_XDP && m_pStream && m_bOwnStream) { |
296 m_pStream->Release(); | 278 m_pStream->Release(); |
297 m_pStream = NULL; | 279 m_pStream = nullptr; |
298 } | 280 } |
299 ps = m_mapNamedImages.GetStartPosition(); | 281 |
300 while (ps) { | 282 for (const auto& pair : m_HashToDibDpiMap) |
301 void* pName; | 283 delete pair.second.pDibSource; |
302 FX_IMAGEDIB_AND_DPI* pImage = NULL; | 284 |
303 m_mapNamedImages.GetNextAssoc(ps, pName, (void*&)pImage); | 285 m_HashToDibDpiMap.clear(); |
304 if (pImage) { | 286 |
305 delete pImage->pDibSource; | 287 FWL_GetApp()->GetNoteDriver()->ClearEventTargets(FALSE); |
306 pImage->pDibSource = NULL; | |
307 FX_Free(pImage); | |
308 pImage = NULL; | |
309 } | |
310 } | |
311 m_mapNamedImages.RemoveAll(); | |
312 IFWL_NoteDriver* pNoteDriver = FWL_GetApp()->GetNoteDriver(); | |
313 pNoteDriver->ClearEventTargets(FALSE); | |
314 return TRUE; | 288 return TRUE; |
315 } | 289 } |
316 void CXFA_FFDoc::SetDocType(uint32_t dwType) { | 290 void CXFA_FFDoc::SetDocType(uint32_t dwType) { |
317 m_dwDocType = dwType; | 291 m_dwDocType = dwType; |
318 } | 292 } |
319 CPDF_Document* CXFA_FFDoc::GetPDFDoc() { | 293 CPDF_Document* CXFA_FFDoc::GetPDFDoc() { |
320 return m_pPDFDoc; | 294 return m_pPDFDoc; |
321 } | 295 } |
322 | 296 |
323 CFX_DIBitmap* CXFA_FFDoc::GetPDFNamedImage(const CFX_WideStringC& wsName, | 297 CFX_DIBitmap* CXFA_FFDoc::GetPDFNamedImage(const CFX_WideStringC& wsName, |
324 int32_t& iImageXDpi, | 298 int32_t& iImageXDpi, |
325 int32_t& iImageYDpi) { | 299 int32_t& iImageYDpi) { |
326 if (!m_pPDFDoc) | 300 if (!m_pPDFDoc) |
327 return nullptr; | 301 return nullptr; |
328 | 302 |
329 uint32_t dwHash = FX_HashCode_GetW(wsName, false); | 303 uint32_t dwHash = FX_HashCode_GetW(wsName, false); |
330 FX_IMAGEDIB_AND_DPI* imageDIBDpi = nullptr; | 304 auto it = m_HashToDibDpiMap.find(dwHash); |
331 if (m_mapNamedImages.Lookup((void*)(uintptr_t)dwHash, (void*&)imageDIBDpi)) { | 305 if (it != m_HashToDibDpiMap.end()) { |
332 iImageXDpi = imageDIBDpi->iImageXDpi; | 306 iImageXDpi = it->second.iImageXDpi; |
333 iImageYDpi = imageDIBDpi->iImageYDpi; | 307 iImageYDpi = it->second.iImageYDpi; |
334 return static_cast<CFX_DIBitmap*>(imageDIBDpi->pDibSource); | 308 return static_cast<CFX_DIBitmap*>(it->second.pDibSource); |
335 } | 309 } |
336 | 310 |
337 CPDF_Dictionary* pRoot = m_pPDFDoc->GetRoot(); | 311 CPDF_Dictionary* pRoot = m_pPDFDoc->GetRoot(); |
338 if (!pRoot) | 312 if (!pRoot) |
339 return nullptr; | 313 return nullptr; |
340 | 314 |
341 CPDF_Dictionary* pNames = pRoot->GetDictBy("Names"); | 315 CPDF_Dictionary* pNames = pRoot->GetDictBy("Names"); |
342 if (!pNames) | 316 if (!pNames) |
343 return nullptr; | 317 return nullptr; |
344 | 318 |
345 CPDF_Dictionary* pXFAImages = pNames->GetDictBy("XFAImages"); | 319 CPDF_Dictionary* pXFAImages = pNames->GetDictBy("XFAImages"); |
346 if (!pXFAImages) | 320 if (!pXFAImages) |
347 return nullptr; | 321 return nullptr; |
348 | 322 |
349 CPDF_NameTree nametree(pXFAImages); | 323 CPDF_NameTree nametree(pXFAImages); |
350 CFX_ByteString bsName = PDF_EncodeText(wsName.c_str(), wsName.GetLength()); | 324 CFX_ByteString bsName = PDF_EncodeText(wsName.c_str(), wsName.GetLength()); |
351 CPDF_Object* pObject = nametree.LookupValue(bsName); | 325 CPDF_Object* pObject = nametree.LookupValue(bsName); |
352 if (!pObject) { | 326 if (!pObject) { |
353 for (size_t i = 0; i < nametree.GetCount(); i++) { | 327 for (size_t i = 0; i < nametree.GetCount(); i++) { |
354 CFX_ByteString bsTemp; | 328 CFX_ByteString bsTemp; |
355 CPDF_Object* pTempObject = nametree.LookupValue(i, bsTemp); | 329 CPDF_Object* pTempObject = nametree.LookupValue(i, bsTemp); |
356 if (bsTemp == bsName) { | 330 if (bsTemp == bsName) { |
357 pObject = pTempObject; | 331 pObject = pTempObject; |
358 break; | 332 break; |
359 } | 333 } |
360 } | 334 } |
361 } | 335 } |
362 | 336 |
363 if (!pObject || !pObject->IsStream()) | 337 CPDF_Stream* pStream = ToStream(pObject); |
| 338 if (!pStream) |
364 return nullptr; | 339 return nullptr; |
365 | 340 |
366 if (!imageDIBDpi) { | 341 CPDF_StreamAcc streamAcc; |
367 imageDIBDpi = FX_Alloc(FX_IMAGEDIB_AND_DPI, 1); | 342 streamAcc.LoadAllData(pStream); |
368 imageDIBDpi->pDibSource = nullptr; | 343 |
369 imageDIBDpi->iImageXDpi = 0; | 344 IFX_FileRead* pImageFileRead = |
370 imageDIBDpi->iImageYDpi = 0; | 345 FX_CreateMemoryStream((uint8_t*)streamAcc.GetData(), streamAcc.GetSize()); |
371 CPDF_StreamAcc streamAcc; | 346 |
372 streamAcc.LoadAllData((CPDF_Stream*)pObject); | 347 CFX_DIBitmap* pDibSource = XFA_LoadImageFromBuffer( |
373 IFX_FileRead* pImageFileRead = FX_CreateMemoryStream( | 348 pImageFileRead, FXCODEC_IMAGE_UNKNOWN, iImageXDpi, iImageYDpi); |
374 (uint8_t*)streamAcc.GetData(), streamAcc.GetSize()); | 349 m_HashToDibDpiMap[dwHash] = {pDibSource, iImageXDpi, iImageYDpi}; |
375 imageDIBDpi->pDibSource = XFA_LoadImageFromBuffer( | 350 pImageFileRead->Release(); |
376 pImageFileRead, FXCODEC_IMAGE_UNKNOWN, iImageXDpi, iImageYDpi); | 351 return pDibSource; |
377 imageDIBDpi->iImageXDpi = iImageXDpi; | |
378 imageDIBDpi->iImageYDpi = iImageYDpi; | |
379 pImageFileRead->Release(); | |
380 } | |
381 m_mapNamedImages.SetAt((void*)(uintptr_t)dwHash, imageDIBDpi); | |
382 return (CFX_DIBitmap*)imageDIBDpi->pDibSource; | |
383 } | 352 } |
384 | 353 |
385 CFDE_XMLElement* CXFA_FFDoc::GetPackageData(const CFX_WideStringC& wsPackage) { | 354 CFDE_XMLElement* CXFA_FFDoc::GetPackageData(const CFX_WideStringC& wsPackage) { |
386 uint32_t packetHash = FX_HashCode_GetW(wsPackage, false); | 355 uint32_t packetHash = FX_HashCode_GetW(wsPackage, false); |
387 CXFA_Node* pNode = ToNode(m_pDocument->GetXFAObject(packetHash)); | 356 CXFA_Node* pNode = ToNode(m_pDocument->GetXFAObject(packetHash)); |
388 if (!pNode) { | 357 if (!pNode) { |
389 return NULL; | 358 return NULL; |
390 } | 359 } |
391 CFDE_XMLNode* pXMLNode = pNode->GetXMLMappingNode(); | 360 CFDE_XMLNode* pXMLNode = pNode->GetXMLMappingNode(); |
392 return (pXMLNode && pXMLNode->GetType() == FDE_XMLNODE_Element) | 361 return (pXMLNode && pXMLNode->GetType() == FDE_XMLNODE_Element) |
(...skipping 24 matching lines...) Expand all Loading... |
417 } | 386 } |
418 pExport->Release(); | 387 pExport->Release(); |
419 return bFlags; | 388 return bFlags; |
420 } | 389 } |
421 FX_BOOL CXFA_FFDoc::ImportData(IFX_FileRead* pStream, FX_BOOL bXDP) { | 390 FX_BOOL CXFA_FFDoc::ImportData(IFX_FileRead* pStream, FX_BOOL bXDP) { |
422 std::unique_ptr<CXFA_DataImporter, ReleaseDeleter<CXFA_DataImporter>> | 391 std::unique_ptr<CXFA_DataImporter, ReleaseDeleter<CXFA_DataImporter>> |
423 importer(new CXFA_DataImporter(m_pDocument)); | 392 importer(new CXFA_DataImporter(m_pDocument)); |
424 | 393 |
425 return importer->ImportData(pStream); | 394 return importer->ImportData(pStream); |
426 } | 395 } |
OLD | NEW |