1 // Copyright 2017 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/cxfa_margin.h"
8 
9 #include "fxjs/xfa/cjx_node.h"
10 #include "third_party/base/ptr_util.h"
11 
12 namespace {
13 
14 const CXFA_Node::PropertyData kMarginPropertyData[] = {
15     {XFA_Element::Extras, 1, 0},
16 };
17 
18 const CXFA_Node::AttributeData kMarginAttributeData[] = {
19     {XFA_Attribute::Id, XFA_AttributeType::CData, nullptr},
20     {XFA_Attribute::Use, XFA_AttributeType::CData, nullptr},
21     {XFA_Attribute::LeftInset, XFA_AttributeType::Measure, (void*)L"0in"},
22     {XFA_Attribute::BottomInset, XFA_AttributeType::Measure, (void*)L"0in"},
23     {XFA_Attribute::TopInset, XFA_AttributeType::Measure, (void*)L"0in"},
24     {XFA_Attribute::RightInset, XFA_AttributeType::Measure, (void*)L"0in"},
25     {XFA_Attribute::Usehref, XFA_AttributeType::CData, nullptr},
26 };
27 
28 }  // namespace
29 
CXFA_Margin(CXFA_Document * doc,XFA_PacketType packet)30 CXFA_Margin::CXFA_Margin(CXFA_Document* doc, XFA_PacketType packet)
31     : CXFA_Node(doc,
32                 packet,
33                 (XFA_XDPPACKET_Template | XFA_XDPPACKET_Form),
34                 XFA_ObjectType::Node,
35                 XFA_Element::Margin,
36                 kMarginPropertyData,
37                 kMarginAttributeData,
38                 pdfium::MakeUnique<CJX_Node>(this)) {}
39 
40 CXFA_Margin::~CXFA_Margin() = default;
41 
GetLeftInset() const42 float CXFA_Margin::GetLeftInset() const {
43   return TryLeftInset().value_or(0);
44 }
45 
GetTopInset() const46 float CXFA_Margin::GetTopInset() const {
47   return TryTopInset().value_or(0);
48 }
49 
GetRightInset() const50 float CXFA_Margin::GetRightInset() const {
51   return TryRightInset().value_or(0);
52 }
53 
GetBottomInset() const54 float CXFA_Margin::GetBottomInset() const {
55   return TryBottomInset().value_or(0);
56 }
57 
TryLeftInset() const58 Optional<float> CXFA_Margin::TryLeftInset() const {
59   return JSObject()->TryMeasureAsFloat(XFA_Attribute::LeftInset);
60 }
61 
TryTopInset() const62 Optional<float> CXFA_Margin::TryTopInset() const {
63   return JSObject()->TryMeasureAsFloat(XFA_Attribute::TopInset);
64 }
65 
TryRightInset() const66 Optional<float> CXFA_Margin::TryRightInset() const {
67   return JSObject()->TryMeasureAsFloat(XFA_Attribute::RightInset);
68 }
69 
TryBottomInset() const70 Optional<float> CXFA_Margin::TryBottomInset() const {
71   return JSObject()->TryMeasureAsFloat(XFA_Attribute::BottomInset);
72 }
73