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. 4 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com 6 7 #include "xfa/fde/fde_iterator.h" 8 9 #include "xfa/fgas/crt/fgas_utils.h" 10 CFDE_VisualSetIterator()11CFDE_VisualSetIterator::CFDE_VisualSetIterator() 12 : m_dwFilter(0), m_CanvasStack(100) {} 13 ~CFDE_VisualSetIterator()14CFDE_VisualSetIterator::~CFDE_VisualSetIterator() { 15 m_CanvasStack.RemoveAll(false); 16 } 17 AttachCanvas(IFDE_CanvasSet * pCanvas)18bool CFDE_VisualSetIterator::AttachCanvas(IFDE_CanvasSet* pCanvas) { 19 ASSERT(pCanvas); 20 21 m_CanvasStack.RemoveAll(false); 22 FDE_CANVASITEM canvas; 23 canvas.hCanvas = nullptr; 24 canvas.pCanvas = pCanvas; 25 canvas.hPos = pCanvas->GetFirstPosition(); 26 if (!canvas.hPos) 27 return false; 28 29 return m_CanvasStack.Push(canvas) == 0; 30 } 31 FilterObjects(uint32_t dwObjects)32bool CFDE_VisualSetIterator::FilterObjects(uint32_t dwObjects) { 33 if (m_CanvasStack.GetSize() == 0) 34 return false; 35 36 while (m_CanvasStack.GetSize() > 1) 37 m_CanvasStack.Pop(); 38 39 m_dwFilter = dwObjects; 40 41 FDE_CANVASITEM* pCanvas = m_CanvasStack.GetTopElement(); 42 ASSERT(pCanvas && pCanvas->pCanvas); 43 44 pCanvas->hPos = pCanvas->pCanvas->GetFirstPosition(); 45 return !!pCanvas->hPos; 46 } 47 Reset()48void CFDE_VisualSetIterator::Reset() { 49 FilterObjects(m_dwFilter); 50 } 51 GetNext(IFDE_VisualSet * & pVisualSet,FDE_TEXTEDITPIECE ** phCanvasObj,IFDE_CanvasSet ** ppCanvasSet)52FDE_TEXTEDITPIECE* CFDE_VisualSetIterator::GetNext( 53 IFDE_VisualSet*& pVisualSet, 54 FDE_TEXTEDITPIECE** phCanvasObj, 55 IFDE_CanvasSet** ppCanvasSet) { 56 while (m_CanvasStack.GetSize() > 0) { 57 FDE_CANVASITEM* pCanvas = m_CanvasStack.GetTopElement(); 58 ASSERT(pCanvas && pCanvas->pCanvas); 59 60 if (!pCanvas->hPos) { 61 if (m_CanvasStack.GetSize() == 1) 62 break; 63 64 m_CanvasStack.Pop(); 65 continue; 66 } 67 do { 68 FDE_TEXTEDITPIECE* pObj = 69 pCanvas->pCanvas->GetNext(pCanvas->hPos, pVisualSet); 70 ASSERT(pObj); 71 72 FDE_VISUALOBJTYPE eType = pVisualSet->GetType(); 73 if (eType == FDE_VISUALOBJ_Canvas) { 74 FDE_CANVASITEM canvas; 75 canvas.hCanvas = pObj; 76 canvas.pCanvas = static_cast<IFDE_CanvasSet*>(pVisualSet); 77 canvas.hPos = canvas.pCanvas->GetFirstPosition(); 78 m_CanvasStack.Push(canvas); 79 break; 80 } 81 uint32_t dwObj = (uint32_t)eType; 82 if ((m_dwFilter & dwObj) != 0) { 83 if (ppCanvasSet) 84 *ppCanvasSet = pCanvas->pCanvas; 85 if (phCanvasObj) 86 *phCanvasObj = pCanvas->hCanvas; 87 return pObj; 88 } 89 } while (pCanvas->hPos); 90 } 91 if (ppCanvasSet) 92 *ppCanvasSet = nullptr; 93 if (phCanvasObj) 94 *phCanvasObj = nullptr; 95 96 pVisualSet = nullptr; 97 return nullptr; 98 } 99