| OLD | NEW |
| (Empty) |
| 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/fxfa/parser/xfa_document_layout_imp.h" | |
| 8 | |
| 9 #include "xfa/fxfa/parser/cxfa_measurement.h" | |
| 10 #include "xfa/fxfa/parser/xfa_doclayout.h" | |
| 11 #include "xfa/fxfa/parser/xfa_document.h" | |
| 12 #include "xfa/fxfa/parser/xfa_document_datamerger_imp.h" | |
| 13 #include "xfa/fxfa/parser/xfa_layout_appadapter.h" | |
| 14 #include "xfa/fxfa/parser/xfa_layout_itemlayout.h" | |
| 15 #include "xfa/fxfa/parser/xfa_layout_pagemgr_new.h" | |
| 16 #include "xfa/fxfa/parser/xfa_localemgr.h" | |
| 17 #include "xfa/fxfa/parser/xfa_object.h" | |
| 18 #include "xfa/fxfa/parser/xfa_utils.h" | |
| 19 | |
| 20 CXFA_LayoutProcessor* CXFA_Document::GetLayoutProcessor() { | |
| 21 if (!m_pLayoutProcessor) { | |
| 22 m_pLayoutProcessor = new CXFA_LayoutProcessor(this); | |
| 23 ASSERT(m_pLayoutProcessor); | |
| 24 } | |
| 25 return m_pLayoutProcessor; | |
| 26 } | |
| 27 CXFA_LayoutProcessor* CXFA_Document::GetDocLayout() { | |
| 28 return GetLayoutProcessor(); | |
| 29 } | |
| 30 CXFA_LayoutProcessor::CXFA_LayoutProcessor(CXFA_Document* pDocument) | |
| 31 : m_pDocument(pDocument), | |
| 32 m_pRootItemLayoutProcessor(nullptr), | |
| 33 m_pLayoutPageMgr(nullptr), | |
| 34 m_nProgressCounter(0), | |
| 35 m_bNeeLayout(TRUE) {} | |
| 36 CXFA_LayoutProcessor::~CXFA_LayoutProcessor() { | |
| 37 ClearLayoutData(); | |
| 38 } | |
| 39 CXFA_Document* CXFA_LayoutProcessor::GetDocument() const { | |
| 40 return m_pDocument; | |
| 41 } | |
| 42 | |
| 43 int32_t CXFA_LayoutProcessor::StartLayout(FX_BOOL bForceRestart) { | |
| 44 if (!bForceRestart && !IsNeedLayout()) | |
| 45 return 100; | |
| 46 | |
| 47 delete m_pRootItemLayoutProcessor; | |
| 48 m_pRootItemLayoutProcessor = nullptr; | |
| 49 m_nProgressCounter = 0; | |
| 50 CXFA_Node* pFormPacketNode = | |
| 51 ToNode(m_pDocument->GetXFAObject(XFA_HASHCODE_Form)); | |
| 52 if (!pFormPacketNode) { | |
| 53 return -1; | |
| 54 } | |
| 55 CXFA_Node* pFormRoot = | |
| 56 pFormPacketNode->GetFirstChildByClass(XFA_Element::Subform); | |
| 57 if (!pFormRoot) { | |
| 58 return -1; | |
| 59 } | |
| 60 if (!m_pLayoutPageMgr) { | |
| 61 m_pLayoutPageMgr = new CXFA_LayoutPageMgr(this); | |
| 62 } | |
| 63 if (!m_pLayoutPageMgr->InitLayoutPage(pFormRoot)) { | |
| 64 return -1; | |
| 65 } | |
| 66 if (!m_pLayoutPageMgr->PrepareFirstPage(pFormRoot)) { | |
| 67 return -1; | |
| 68 } | |
| 69 m_pRootItemLayoutProcessor = | |
| 70 new CXFA_ItemLayoutProcessor(pFormRoot, m_pLayoutPageMgr); | |
| 71 m_nProgressCounter = 1; | |
| 72 return 0; | |
| 73 } | |
| 74 int32_t CXFA_LayoutProcessor::DoLayout(IFX_Pause* pPause) { | |
| 75 if (m_nProgressCounter < 1) { | |
| 76 return -1; | |
| 77 } | |
| 78 XFA_ItemLayoutProcessorResult eStatus; | |
| 79 CXFA_Node* pFormNode = m_pRootItemLayoutProcessor->GetFormNode(); | |
| 80 FX_FLOAT fPosX = pFormNode->GetMeasure(XFA_ATTRIBUTE_X).ToUnit(XFA_UNIT_Pt); | |
| 81 FX_FLOAT fPosY = pFormNode->GetMeasure(XFA_ATTRIBUTE_Y).ToUnit(XFA_UNIT_Pt); | |
| 82 do { | |
| 83 FX_FLOAT fAvailHeight = m_pLayoutPageMgr->GetAvailHeight(); | |
| 84 eStatus = | |
| 85 m_pRootItemLayoutProcessor->DoLayout(TRUE, fAvailHeight, fAvailHeight); | |
| 86 if (eStatus != XFA_ItemLayoutProcessorResult_Done) { | |
| 87 m_nProgressCounter++; | |
| 88 } | |
| 89 CXFA_ContentLayoutItem* pLayoutItem = | |
| 90 m_pRootItemLayoutProcessor->ExtractLayoutItem(); | |
| 91 if (pLayoutItem) { | |
| 92 pLayoutItem->m_sPos = CFX_PointF(fPosX, fPosY); | |
| 93 } | |
| 94 m_pLayoutPageMgr->SubmitContentItem(pLayoutItem, eStatus); | |
| 95 } while (eStatus != XFA_ItemLayoutProcessorResult_Done && | |
| 96 (!pPause || !pPause->NeedToPauseNow())); | |
| 97 if (eStatus == XFA_ItemLayoutProcessorResult_Done) { | |
| 98 m_pLayoutPageMgr->FinishPaginatedPageSets(); | |
| 99 m_pLayoutPageMgr->SyncLayoutData(); | |
| 100 m_bNeeLayout = FALSE; | |
| 101 m_rgChangedContainers.RemoveAll(); | |
| 102 } | |
| 103 return 100 * (eStatus == XFA_ItemLayoutProcessorResult_Done | |
| 104 ? m_nProgressCounter | |
| 105 : m_nProgressCounter - 1) / | |
| 106 m_nProgressCounter; | |
| 107 } | |
| 108 FX_BOOL CXFA_LayoutProcessor::IncrementLayout() { | |
| 109 if (m_bNeeLayout) { | |
| 110 StartLayout(TRUE); | |
| 111 return DoLayout(nullptr) == 100; | |
| 112 } | |
| 113 for (int32_t i = 0, c = m_rgChangedContainers.GetSize(); i < c; i++) { | |
| 114 CXFA_Node* pNode = m_rgChangedContainers[i]; | |
| 115 CXFA_Node* pParentNode = | |
| 116 pNode->GetNodeItem(XFA_NODEITEM_Parent, XFA_ObjectType::ContainerNode); | |
| 117 if (!pParentNode) { | |
| 118 return FALSE; | |
| 119 } | |
| 120 if (!CXFA_ItemLayoutProcessor::IncrementRelayoutNode(this, pNode, | |
| 121 pParentNode)) { | |
| 122 return FALSE; | |
| 123 } | |
| 124 } | |
| 125 m_rgChangedContainers.RemoveAll(); | |
| 126 return TRUE; | |
| 127 } | |
| 128 int32_t CXFA_LayoutProcessor::CountPages() const { | |
| 129 return m_pLayoutPageMgr ? m_pLayoutPageMgr->GetPageCount() : 0; | |
| 130 } | |
| 131 CXFA_ContainerLayoutItem* CXFA_LayoutProcessor::GetPage(int32_t index) const { | |
| 132 return m_pLayoutPageMgr ? m_pLayoutPageMgr->GetPage(index) : nullptr; | |
| 133 } | |
| 134 CXFA_LayoutItem* CXFA_LayoutProcessor::GetLayoutItem(CXFA_Node* pFormItem) { | |
| 135 return static_cast<CXFA_LayoutItem*>( | |
| 136 pFormItem->GetUserData(XFA_LAYOUTITEMKEY)); | |
| 137 } | |
| 138 void CXFA_LayoutProcessor::AddChangedContainer(CXFA_Node* pContainer) { | |
| 139 if (m_rgChangedContainers.Find(pContainer) < 0) { | |
| 140 m_rgChangedContainers.Add(pContainer); | |
| 141 } | |
| 142 } | |
| 143 CXFA_ContainerLayoutItem* CXFA_LayoutProcessor::GetRootLayoutItem() const { | |
| 144 return m_pLayoutPageMgr ? m_pLayoutPageMgr->GetRootLayoutItem() : nullptr; | |
| 145 } | |
| 146 | |
| 147 void CXFA_LayoutProcessor::ClearLayoutData() { | |
| 148 delete m_pLayoutPageMgr; | |
| 149 m_pLayoutPageMgr = nullptr; | |
| 150 delete m_pRootItemLayoutProcessor; | |
| 151 m_pRootItemLayoutProcessor = nullptr; | |
| 152 m_nProgressCounter = 0; | |
| 153 } | |
| 154 | |
| 155 FX_BOOL CXFA_LayoutProcessor::IsNeedLayout() { | |
| 156 return m_bNeeLayout || m_rgChangedContainers.GetSize() > 0; | |
| 157 } | |
| 158 CXFA_LayoutItem::CXFA_LayoutItem(CXFA_Node* pNode, FX_BOOL bIsContentLayoutItem) | |
| 159 : m_pFormNode(pNode), | |
| 160 m_pParent(nullptr), | |
| 161 m_pNextSibling(nullptr), | |
| 162 m_pFirstChild(nullptr), | |
| 163 m_bIsContentLayoutItem(bIsContentLayoutItem) {} | |
| 164 CXFA_LayoutItem::~CXFA_LayoutItem() {} | |
| 165 CXFA_ContainerLayoutItem::CXFA_ContainerLayoutItem(CXFA_Node* pNode) | |
| 166 : CXFA_LayoutItem(pNode, FALSE), m_pOldSubform(nullptr) {} | |
| 167 CXFA_LayoutProcessor* CXFA_ContainerLayoutItem::GetLayout() const { | |
| 168 return m_pFormNode->GetDocument()->GetLayoutProcessor(); | |
| 169 } | |
| 170 int32_t CXFA_ContainerLayoutItem::GetPageIndex() const { | |
| 171 return m_pFormNode->GetDocument() | |
| 172 ->GetLayoutProcessor() | |
| 173 ->GetLayoutPageMgr() | |
| 174 ->GetPageIndex(this); | |
| 175 } | |
| 176 | |
| 177 void CXFA_ContainerLayoutItem::GetPageSize(CFX_SizeF& size) const { | |
| 178 size.clear(); | |
| 179 CXFA_Node* pMedium = m_pFormNode->GetFirstChildByClass(XFA_Element::Medium); | |
| 180 if (!pMedium) | |
| 181 return; | |
| 182 | |
| 183 size = CFX_SizeF(pMedium->GetMeasure(XFA_ATTRIBUTE_Short).ToUnit(XFA_UNIT_Pt), | |
| 184 pMedium->GetMeasure(XFA_ATTRIBUTE_Long).ToUnit(XFA_UNIT_Pt)); | |
| 185 if (pMedium->GetEnum(XFA_ATTRIBUTE_Orientation) == | |
| 186 XFA_ATTRIBUTEENUM_Landscape) { | |
| 187 size = CFX_SizeF(size.y, size.x); | |
| 188 } | |
| 189 } | |
| 190 | |
| 191 CXFA_Node* CXFA_ContainerLayoutItem::GetMasterPage() const { | |
| 192 return m_pFormNode; | |
| 193 } | |
| 194 CXFA_ContentLayoutItem::CXFA_ContentLayoutItem(CXFA_Node* pNode) | |
| 195 : CXFA_LayoutItem(pNode, TRUE), | |
| 196 m_pPrev(nullptr), | |
| 197 m_pNext(nullptr), | |
| 198 m_dwStatus(0) {} | |
| 199 CXFA_ContentLayoutItem::~CXFA_ContentLayoutItem() { | |
| 200 if (m_pFormNode->GetUserData(XFA_LAYOUTITEMKEY) == this) { | |
| 201 m_pFormNode->SetUserData(XFA_LAYOUTITEMKEY, nullptr); | |
| 202 } | |
| 203 } | |
| OLD | NEW |