• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2016 The PDFium Authors
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 "fxjs/xfa/cjx_object.h"
10 #include "third_party/base/check_op.h"
11 #include "xfa/fxfa/cxfa_ffwidget.h"
12 #include "xfa/fxfa/parser/cxfa_margin.h"
13 #include "xfa/fxfa/parser/cxfa_node.h"
14 
CXFA_ContentLayoutItem(CXFA_Node * pNode,CXFA_FFWidget * pWidget)15 CXFA_ContentLayoutItem::CXFA_ContentLayoutItem(CXFA_Node* pNode,
16                                                CXFA_FFWidget* pWidget)
17     : CXFA_LayoutItem(pNode, kContentItem), m_pFFWidget(pWidget) {
18   if (m_pFFWidget)
19     m_pFFWidget->SetLayoutItem(this);
20 }
21 
22 CXFA_ContentLayoutItem::~CXFA_ContentLayoutItem() = default;
23 
Trace(cppgc::Visitor * visitor) const24 void CXFA_ContentLayoutItem::Trace(cppgc::Visitor* visitor) const {
25   CXFA_LayoutItem::Trace(visitor);
26   visitor->Trace(m_pPrev);
27   visitor->Trace(m_pNext);
28   visitor->Trace(m_pFFWidget);
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 
GetRelativeRect() const64 CFX_RectF CXFA_ContentLayoutItem::GetRelativeRect() const {
65   return CFX_RectF(m_sPos, m_sSize);
66 }
67 
GetAbsoluteRect() const68 CFX_RectF CXFA_ContentLayoutItem::GetAbsoluteRect() const {
69   CFX_PointF sPos = m_sPos;
70   CFX_SizeF sSize = m_sSize;
71 
72   for (CXFA_LayoutItem* pLayoutItem = GetParent(); pLayoutItem;
73        pLayoutItem = pLayoutItem->GetParent()) {
74     if (CXFA_ContentLayoutItem* pContent = pLayoutItem->AsContentLayoutItem()) {
75       sPos += pContent->m_sPos;
76       CXFA_Margin* pMarginNode =
77           pContent->GetFormNode()->GetFirstChildByClass<CXFA_Margin>(
78               XFA_Element::Margin);
79       if (pMarginNode) {
80         sPos += CFX_PointF(pMarginNode->JSObject()->GetMeasureInUnit(
81                                XFA_Attribute::LeftInset, XFA_Unit::Pt),
82                            pMarginNode->JSObject()->GetMeasureInUnit(
83                                XFA_Attribute::TopInset, XFA_Unit::Pt));
84       }
85       continue;
86     }
87 
88     if (pLayoutItem->GetFormNode()->GetElementType() ==
89         XFA_Element::ContentArea) {
90       sPos +=
91           CFX_PointF(pLayoutItem->GetFormNode()->JSObject()->GetMeasureInUnit(
92                          XFA_Attribute::X, XFA_Unit::Pt),
93                      pLayoutItem->GetFormNode()->JSObject()->GetMeasureInUnit(
94                          XFA_Attribute::Y, XFA_Unit::Pt));
95       break;
96     }
97     if (pLayoutItem->GetFormNode()->GetElementType() == XFA_Element::PageArea)
98       break;
99   }
100   return CFX_RectF(sPos, sSize);
101 }
102 
GetIndex() const103 size_t CXFA_ContentLayoutItem::GetIndex() const {
104   size_t szIndex = 0;
105   const CXFA_ContentLayoutItem* pCurNode = this;
106   while (auto* pPrev = pCurNode->GetPrev()) {
107     pCurNode = pPrev;
108     ++szIndex;
109   }
110   return szIndex;
111 }
112