1 // Copyright 2016 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/layout/cxfa_contentlayoutitem.h"
8
9 #include <utility>
10
11 #include "fxjs/xfa/cjx_object.h"
12 #include "xfa/fxfa/cxfa_ffwidget.h"
13 #include "xfa/fxfa/parser/cxfa_margin.h"
14 #include "xfa/fxfa/parser/cxfa_node.h"
15
CXFA_ContentLayoutItem(CXFA_Node * pNode,std::unique_ptr<CXFA_FFWidget> pWidget)16 CXFA_ContentLayoutItem::CXFA_ContentLayoutItem(
17 CXFA_Node* pNode,
18 std::unique_ptr<CXFA_FFWidget> pWidget)
19 : CXFA_LayoutItem(pNode, kContentItem), m_pFFWidget(std::move(pWidget)) {
20 if (m_pFFWidget)
21 m_pFFWidget->SetLayoutItem(this);
22 }
23
~CXFA_ContentLayoutItem()24 CXFA_ContentLayoutItem::~CXFA_ContentLayoutItem() {
25 if (m_pFFWidget)
26 m_pFFWidget->SetLayoutItem(nullptr);
27
28 RemoveSelf();
29 }
30
GetFirst()31 CXFA_ContentLayoutItem* CXFA_ContentLayoutItem::GetFirst() {
32 CXFA_ContentLayoutItem* pCurNode = this;
33 while (auto* pPrev = pCurNode->GetPrev())
34 pCurNode = pPrev;
35
36 return pCurNode;
37 }
38
GetLast()39 CXFA_ContentLayoutItem* CXFA_ContentLayoutItem::GetLast() {
40 CXFA_ContentLayoutItem* pCurNode = this;
41 while (auto* pNext = pCurNode->GetNext())
42 pCurNode = pNext;
43
44 return pCurNode;
45 }
46
InsertAfter(CXFA_ContentLayoutItem * pItem)47 void CXFA_ContentLayoutItem::InsertAfter(CXFA_ContentLayoutItem* pItem) {
48 CHECK_NE(this, pItem);
49 pItem->RemoveSelf();
50 pItem->m_pNext = m_pNext;
51 pItem->m_pPrev = this;
52 m_pNext = pItem;
53 if (pItem->m_pNext)
54 pItem->m_pNext->m_pPrev = pItem;
55 }
56
RemoveSelf()57 void CXFA_ContentLayoutItem::RemoveSelf() {
58 if (m_pNext)
59 m_pNext->m_pPrev = m_pPrev;
60 if (m_pPrev)
61 m_pPrev->m_pNext = m_pNext;
62 }
63
GetRect(bool bRelative) const64 CFX_RectF CXFA_ContentLayoutItem::GetRect(bool bRelative) const {
65 CFX_PointF sPos = m_sPos;
66 CFX_SizeF sSize = m_sSize;
67 if (bRelative)
68 return CFX_RectF(sPos, sSize);
69
70 for (CXFA_LayoutItem* pLayoutItem = GetParent(); pLayoutItem;
71 pLayoutItem = pLayoutItem->GetParent()) {
72 if (CXFA_ContentLayoutItem* pContent = pLayoutItem->AsContentLayoutItem()) {
73 sPos += pContent->m_sPos;
74 CXFA_Margin* pMarginNode =
75 pContent->GetFormNode()->GetFirstChildByClass<CXFA_Margin>(
76 XFA_Element::Margin);
77 if (pMarginNode) {
78 sPos += CFX_PointF(pMarginNode->JSObject()->GetMeasureInUnit(
79 XFA_Attribute::LeftInset, XFA_Unit::Pt),
80 pMarginNode->JSObject()->GetMeasureInUnit(
81 XFA_Attribute::TopInset, XFA_Unit::Pt));
82 }
83 continue;
84 }
85
86 if (pLayoutItem->GetFormNode()->GetElementType() ==
87 XFA_Element::ContentArea) {
88 sPos +=
89 CFX_PointF(pLayoutItem->GetFormNode()->JSObject()->GetMeasureInUnit(
90 XFA_Attribute::X, XFA_Unit::Pt),
91 pLayoutItem->GetFormNode()->JSObject()->GetMeasureInUnit(
92 XFA_Attribute::Y, XFA_Unit::Pt));
93 break;
94 }
95 if (pLayoutItem->GetFormNode()->GetElementType() == XFA_Element::PageArea)
96 break;
97 }
98 return CFX_RectF(sPos, sSize);
99 }
100
GetIndex() const101 size_t CXFA_ContentLayoutItem::GetIndex() const {
102 size_t szIndex = 0;
103 const CXFA_ContentLayoutItem* pCurNode = this;
104 while (auto* pPrev = pCurNode->GetPrev()) {
105 pCurNode = pPrev;
106 ++szIndex;
107 }
108 return szIndex;
109 }
110