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_caption.h"
8 
9 #include "fxjs/xfa/cjx_caption.h"
10 #include "third_party/base/ptr_util.h"
11 #include "xfa/fxfa/parser/cxfa_font.h"
12 #include "xfa/fxfa/parser/cxfa_margin.h"
13 #include "xfa/fxfa/parser/cxfa_measurement.h"
14 #include "xfa/fxfa/parser/cxfa_value.h"
15 
16 namespace {
17 
18 const CXFA_Node::PropertyData kPropertyData[] = {
19     {XFA_Element::Margin, 1, 0}, {XFA_Element::Para, 1, 0},
20     {XFA_Element::Font, 1, 0},   {XFA_Element::Value, 1, 0},
21     {XFA_Element::Extras, 1, 0}, {XFA_Element::Unknown, 0, 0}};
22 const CXFA_Node::AttributeData kAttributeData[] = {
23     {XFA_Attribute::Id, XFA_AttributeType::CData, nullptr},
24     {XFA_Attribute::Use, XFA_AttributeType::CData, nullptr},
25     {XFA_Attribute::Reserve, XFA_AttributeType::Measure, (void*)L"-1un"},
26     {XFA_Attribute::Presence, XFA_AttributeType::Enum,
27      (void*)XFA_AttributeEnum::Visible},
28     {XFA_Attribute::Usehref, XFA_AttributeType::CData, nullptr},
29     {XFA_Attribute::Placement, XFA_AttributeType::Enum,
30      (void*)XFA_AttributeEnum::Left},
31     {XFA_Attribute::Unknown, XFA_AttributeType::Integer, nullptr}};
32 
33 constexpr wchar_t kName[] = L"caption";
34 
35 }  // namespace
36 
CXFA_Caption(CXFA_Document * doc,XFA_PacketType packet)37 CXFA_Caption::CXFA_Caption(CXFA_Document* doc, XFA_PacketType packet)
38     : CXFA_Node(doc,
39                 packet,
40                 (XFA_XDPPACKET_Template | XFA_XDPPACKET_Form),
41                 XFA_ObjectType::Node,
42                 XFA_Element::Caption,
43                 kPropertyData,
44                 kAttributeData,
45                 kName,
46                 pdfium::MakeUnique<CJX_Caption>(this)) {}
47 
~CXFA_Caption()48 CXFA_Caption::~CXFA_Caption() {}
49 
IsVisible()50 bool CXFA_Caption::IsVisible() {
51   return JSObject()
52              ->TryEnum(XFA_Attribute::Presence, true)
53              .value_or(XFA_AttributeEnum::Visible) ==
54          XFA_AttributeEnum::Visible;
55 }
56 
IsHidden()57 bool CXFA_Caption::IsHidden() {
58   return JSObject()
59              ->TryEnum(XFA_Attribute::Presence, true)
60              .value_or(XFA_AttributeEnum::Visible) == XFA_AttributeEnum::Hidden;
61 }
62 
GetPlacementType()63 XFA_AttributeEnum CXFA_Caption::GetPlacementType() {
64   return JSObject()
65       ->TryEnum(XFA_Attribute::Placement, true)
66       .value_or(XFA_AttributeEnum::Left);
67 }
68 
GetReserve() const69 float CXFA_Caption::GetReserve() const {
70   return JSObject()->GetMeasure(XFA_Attribute::Reserve).ToUnit(XFA_Unit::Pt);
71 }
72 
GetMarginIfExists()73 CXFA_Margin* CXFA_Caption::GetMarginIfExists() {
74   return GetChild<CXFA_Margin>(0, XFA_Element::Margin, false);
75 }
76 
GetFontIfExists()77 CXFA_Font* CXFA_Caption::GetFontIfExists() {
78   return GetChild<CXFA_Font>(0, XFA_Element::Font, false);
79 }
80 
GetValueIfExists()81 CXFA_Value* CXFA_Caption::GetValueIfExists() {
82   return GetChild<CXFA_Value>(0, XFA_Element::Value, false);
83 }
84