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