1 // Copyright 2017 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/parser/cxfa_barcode.h"
8
9 #include "fxjs/xfa/cjx_node.h"
10 #include "xfa/fxfa/parser/cxfa_document.h"
11 #include "xfa/fxfa/parser/cxfa_measurement.h"
12
13 namespace {
14
15 const CXFA_Node::AttributeData kBarcodeAttributeData[] = {
16 {XFA_Attribute::Id, XFA_AttributeType::CData, nullptr},
17 {XFA_Attribute::DataRowCount, XFA_AttributeType::CData, nullptr},
18 {XFA_Attribute::Use, XFA_AttributeType::CData, nullptr},
19 {XFA_Attribute::DataPrep, XFA_AttributeType::Enum,
20 (void*)XFA_AttributeValue::None},
21 {XFA_Attribute::Type, XFA_AttributeType::CData, (void*)nullptr},
22 {XFA_Attribute::TextLocation, XFA_AttributeType::Enum,
23 (void*)XFA_AttributeValue::Below},
24 {XFA_Attribute::ModuleWidth, XFA_AttributeType::Measure, (void*)L"0.25mm"},
25 {XFA_Attribute::PrintCheckDigit, XFA_AttributeType::Boolean, (void*)0},
26 {XFA_Attribute::ModuleHeight, XFA_AttributeType::Measure, (void*)L"5mm"},
27 {XFA_Attribute::StartChar, XFA_AttributeType::CData, nullptr},
28 {XFA_Attribute::Truncate, XFA_AttributeType::Boolean, (void*)0},
29 {XFA_Attribute::WideNarrowRatio, XFA_AttributeType::CData, (void*)L"3:1"},
30 {XFA_Attribute::ErrorCorrectionLevel, XFA_AttributeType::CData, nullptr},
31 {XFA_Attribute::UpsMode, XFA_AttributeType::Enum,
32 (void*)XFA_AttributeValue::UsCarrier},
33 {XFA_Attribute::Checksum, XFA_AttributeType::Enum,
34 (void*)XFA_AttributeValue::None},
35 {XFA_Attribute::CharEncoding, XFA_AttributeType::CData, (void*)L"UTF-8"},
36 {XFA_Attribute::Usehref, XFA_AttributeType::CData, nullptr},
37 {XFA_Attribute::DataColumnCount, XFA_AttributeType::CData, nullptr},
38 {XFA_Attribute::RowColumnRatio, XFA_AttributeType::CData, nullptr},
39 {XFA_Attribute::DataLength, XFA_AttributeType::CData, nullptr},
40 {XFA_Attribute::EndChar, XFA_AttributeType::CData, nullptr},
41 };
42
43 } // namespace
44
45 // static
FromNode(CXFA_Node * pNode)46 CXFA_Barcode* CXFA_Barcode::FromNode(CXFA_Node* pNode) {
47 return pNode && pNode->GetElementType() == XFA_Element::Barcode
48 ? static_cast<CXFA_Barcode*>(pNode)
49 : nullptr;
50 }
51
CXFA_Barcode(CXFA_Document * doc,XFA_PacketType packet)52 CXFA_Barcode::CXFA_Barcode(CXFA_Document* doc, XFA_PacketType packet)
53 : CXFA_Node(doc,
54 packet,
55 {XFA_XDPPACKET::kTemplate, XFA_XDPPACKET::kForm},
56 XFA_ObjectType::Node,
57 XFA_Element::Barcode,
58 {},
59 kBarcodeAttributeData,
60 cppgc::MakeGarbageCollected<CJX_Node>(
61 doc->GetHeap()->GetAllocationHandle(),
62 this)) {}
63
64 CXFA_Barcode::~CXFA_Barcode() = default;
65
GetDefaultFFWidgetType() const66 XFA_FFWidgetType CXFA_Barcode::GetDefaultFFWidgetType() const {
67 return XFA_FFWidgetType::kBarcode;
68 }
69
GetBarcodeType()70 WideString CXFA_Barcode::GetBarcodeType() {
71 return WideString(JSObject()->GetCData(XFA_Attribute::Type));
72 }
73
GetCharEncoding()74 absl::optional<WideString> CXFA_Barcode::GetCharEncoding() {
75 return JSObject()->TryCData(XFA_Attribute::CharEncoding, true);
76 }
77
GetChecksum()78 absl::optional<bool> CXFA_Barcode::GetChecksum() {
79 absl::optional<XFA_AttributeValue> checksum =
80 JSObject()->TryEnum(XFA_Attribute::Checksum, true);
81 if (!checksum.has_value())
82 return absl::nullopt;
83
84 switch (checksum.value()) {
85 case XFA_AttributeValue::None:
86 return {false};
87 case XFA_AttributeValue::Auto:
88 return {true};
89 case XFA_AttributeValue::Checksum_1mod10:
90 case XFA_AttributeValue::Checksum_1mod10_1mod11:
91 case XFA_AttributeValue::Checksum_2mod10:
92 default:
93 break;
94 }
95 return absl::nullopt;
96 }
97
GetDataLength()98 absl::optional<int32_t> CXFA_Barcode::GetDataLength() {
99 absl::optional<WideString> wsDataLength =
100 JSObject()->TryCData(XFA_Attribute::DataLength, true);
101 if (!wsDataLength.has_value())
102 return absl::nullopt;
103
104 return FXSYS_wtoi(wsDataLength->c_str());
105 }
106
GetStartChar()107 absl::optional<char> CXFA_Barcode::GetStartChar() {
108 absl::optional<WideString> wsStartEndChar =
109 JSObject()->TryCData(XFA_Attribute::StartChar, true);
110 if (!wsStartEndChar.has_value() || wsStartEndChar->IsEmpty())
111 return absl::nullopt;
112
113 return static_cast<char>(wsStartEndChar.value()[0]);
114 }
115
GetEndChar()116 absl::optional<char> CXFA_Barcode::GetEndChar() {
117 absl::optional<WideString> wsStartEndChar =
118 JSObject()->TryCData(XFA_Attribute::EndChar, true);
119 if (!wsStartEndChar.has_value() || wsStartEndChar->IsEmpty())
120 return absl::nullopt;
121
122 return static_cast<char>(wsStartEndChar.value()[0]);
123 }
124
GetECLevel()125 absl::optional<int32_t> CXFA_Barcode::GetECLevel() {
126 absl::optional<WideString> wsECLevel =
127 JSObject()->TryCData(XFA_Attribute::ErrorCorrectionLevel, true);
128 if (!wsECLevel.has_value())
129 return absl::nullopt;
130 return FXSYS_wtoi(wsECLevel->c_str());
131 }
132
GetModuleWidth()133 absl::optional<int32_t> CXFA_Barcode::GetModuleWidth() {
134 absl::optional<CXFA_Measurement> moduleWidthHeight =
135 JSObject()->TryMeasure(XFA_Attribute::ModuleWidth, true);
136 if (!moduleWidthHeight.has_value())
137 return absl::nullopt;
138
139 return static_cast<int32_t>(moduleWidthHeight->ToUnit(XFA_Unit::Pt));
140 }
141
GetModuleHeight()142 absl::optional<int32_t> CXFA_Barcode::GetModuleHeight() {
143 absl::optional<CXFA_Measurement> moduleWidthHeight =
144 JSObject()->TryMeasure(XFA_Attribute::ModuleHeight, true);
145 if (!moduleWidthHeight.has_value())
146 return absl::nullopt;
147
148 return static_cast<int32_t>(moduleWidthHeight->ToUnit(XFA_Unit::Pt));
149 }
150
GetPrintChecksum()151 absl::optional<bool> CXFA_Barcode::GetPrintChecksum() {
152 return JSObject()->TryBoolean(XFA_Attribute::PrintCheckDigit, true);
153 }
154
GetTextLocation()155 absl::optional<XFA_AttributeValue> CXFA_Barcode::GetTextLocation() {
156 return JSObject()->TryEnum(XFA_Attribute::TextLocation, true);
157 }
158
GetTruncate()159 absl::optional<bool> CXFA_Barcode::GetTruncate() {
160 return JSObject()->TryBoolean(XFA_Attribute::Truncate, true);
161 }
162
GetWideNarrowRatio()163 absl::optional<int8_t> CXFA_Barcode::GetWideNarrowRatio() {
164 absl::optional<WideString> wsWideNarrowRatio =
165 JSObject()->TryCData(XFA_Attribute::WideNarrowRatio, true);
166 if (!wsWideNarrowRatio.has_value())
167 return absl::nullopt;
168
169 absl::optional<size_t> ptPos = wsWideNarrowRatio->Find(':');
170 if (!ptPos.has_value())
171 return static_cast<int8_t>(FXSYS_wtoi(wsWideNarrowRatio->c_str()));
172
173 int32_t fB = FXSYS_wtoi(
174 wsWideNarrowRatio
175 ->Last(wsWideNarrowRatio->GetLength() - (ptPos.value() + 1))
176 .c_str());
177 if (!fB)
178 return 0;
179
180 int32_t fA = FXSYS_wtoi(wsWideNarrowRatio->First(ptPos.value()).c_str());
181 float result = static_cast<float>(fA) / static_cast<float>(fB);
182 return static_cast<int8_t>(result);
183 }
184