• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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::Usehref, XFA_AttributeType::CData, nullptr},
36     {XFA_Attribute::DataColumnCount, XFA_AttributeType::CData, nullptr},
37     {XFA_Attribute::RowColumnRatio, XFA_AttributeType::CData, nullptr},
38     {XFA_Attribute::DataLength, XFA_AttributeType::CData, nullptr},
39     {XFA_Attribute::EndChar, XFA_AttributeType::CData, nullptr},
40 };
41 
42 }  // namespace
43 
44 // static
FromNode(CXFA_Node * pNode)45 CXFA_Barcode* CXFA_Barcode::FromNode(CXFA_Node* pNode) {
46   return pNode && pNode->GetElementType() == XFA_Element::Barcode
47              ? static_cast<CXFA_Barcode*>(pNode)
48              : nullptr;
49 }
50 
CXFA_Barcode(CXFA_Document * doc,XFA_PacketType packet)51 CXFA_Barcode::CXFA_Barcode(CXFA_Document* doc, XFA_PacketType packet)
52     : CXFA_Node(doc,
53                 packet,
54                 {XFA_XDPPACKET::kTemplate, XFA_XDPPACKET::kForm},
55                 XFA_ObjectType::Node,
56                 XFA_Element::Barcode,
57                 {},
58                 kBarcodeAttributeData,
59                 cppgc::MakeGarbageCollected<CJX_Node>(
60                     doc->GetHeap()->GetAllocationHandle(),
61                     this)) {}
62 
63 CXFA_Barcode::~CXFA_Barcode() = default;
64 
GetDefaultFFWidgetType() const65 XFA_FFWidgetType CXFA_Barcode::GetDefaultFFWidgetType() const {
66   return XFA_FFWidgetType::kBarcode;
67 }
68 
GetBarcodeType()69 WideString CXFA_Barcode::GetBarcodeType() {
70   return WideString(JSObject()->GetCData(XFA_Attribute::Type));
71 }
72 
GetChecksum()73 std::optional<bool> CXFA_Barcode::GetChecksum() {
74   std::optional<XFA_AttributeValue> checksum =
75       JSObject()->TryEnum(XFA_Attribute::Checksum, true);
76   if (!checksum.has_value())
77     return std::nullopt;
78 
79   switch (checksum.value()) {
80     case XFA_AttributeValue::None:
81       return {false};
82     case XFA_AttributeValue::Auto:
83       return {true};
84     case XFA_AttributeValue::Checksum_1mod10:
85     case XFA_AttributeValue::Checksum_1mod10_1mod11:
86     case XFA_AttributeValue::Checksum_2mod10:
87     default:
88       break;
89   }
90   return std::nullopt;
91 }
92 
GetDataLength()93 std::optional<int32_t> CXFA_Barcode::GetDataLength() {
94   std::optional<WideString> wsDataLength =
95       JSObject()->TryCData(XFA_Attribute::DataLength, true);
96   if (!wsDataLength.has_value())
97     return std::nullopt;
98 
99   return FXSYS_wtoi(wsDataLength->c_str());
100 }
101 
GetStartChar()102 std::optional<char> CXFA_Barcode::GetStartChar() {
103   std::optional<WideString> wsStartEndChar =
104       JSObject()->TryCData(XFA_Attribute::StartChar, true);
105   if (!wsStartEndChar.has_value() || wsStartEndChar->IsEmpty())
106     return std::nullopt;
107 
108   return static_cast<char>(wsStartEndChar.value()[0]);
109 }
110 
GetEndChar()111 std::optional<char> CXFA_Barcode::GetEndChar() {
112   std::optional<WideString> wsStartEndChar =
113       JSObject()->TryCData(XFA_Attribute::EndChar, true);
114   if (!wsStartEndChar.has_value() || wsStartEndChar->IsEmpty())
115     return std::nullopt;
116 
117   return static_cast<char>(wsStartEndChar.value()[0]);
118 }
119 
GetECLevel()120 std::optional<int32_t> CXFA_Barcode::GetECLevel() {
121   std::optional<WideString> wsECLevel =
122       JSObject()->TryCData(XFA_Attribute::ErrorCorrectionLevel, true);
123   if (!wsECLevel.has_value())
124     return std::nullopt;
125   return FXSYS_wtoi(wsECLevel->c_str());
126 }
127 
GetModuleWidth()128 std::optional<int32_t> CXFA_Barcode::GetModuleWidth() {
129   std::optional<CXFA_Measurement> moduleWidthHeight =
130       JSObject()->TryMeasure(XFA_Attribute::ModuleWidth, true);
131   if (!moduleWidthHeight.has_value())
132     return std::nullopt;
133 
134   return static_cast<int32_t>(moduleWidthHeight->ToUnit(XFA_Unit::Pt));
135 }
136 
GetModuleHeight()137 std::optional<int32_t> CXFA_Barcode::GetModuleHeight() {
138   std::optional<CXFA_Measurement> moduleWidthHeight =
139       JSObject()->TryMeasure(XFA_Attribute::ModuleHeight, true);
140   if (!moduleWidthHeight.has_value())
141     return std::nullopt;
142 
143   return static_cast<int32_t>(moduleWidthHeight->ToUnit(XFA_Unit::Pt));
144 }
145 
GetPrintChecksum()146 std::optional<bool> CXFA_Barcode::GetPrintChecksum() {
147   return JSObject()->TryBoolean(XFA_Attribute::PrintCheckDigit, true);
148 }
149 
GetTextLocation()150 std::optional<XFA_AttributeValue> CXFA_Barcode::GetTextLocation() {
151   return JSObject()->TryEnum(XFA_Attribute::TextLocation, true);
152 }
153 
GetTruncate()154 std::optional<bool> CXFA_Barcode::GetTruncate() {
155   return JSObject()->TryBoolean(XFA_Attribute::Truncate, true);
156 }
157 
GetWideNarrowRatio()158 std::optional<int8_t> CXFA_Barcode::GetWideNarrowRatio() {
159   std::optional<WideString> wsWideNarrowRatio =
160       JSObject()->TryCData(XFA_Attribute::WideNarrowRatio, true);
161   if (!wsWideNarrowRatio.has_value())
162     return std::nullopt;
163 
164   std::optional<size_t> ptPos = wsWideNarrowRatio->Find(':');
165   if (!ptPos.has_value())
166     return static_cast<int8_t>(FXSYS_wtoi(wsWideNarrowRatio->c_str()));
167 
168   int32_t fB = FXSYS_wtoi(
169       wsWideNarrowRatio
170           ->Last(wsWideNarrowRatio->GetLength() - (ptPos.value() + 1))
171           .c_str());
172   if (!fB)
173     return 0;
174 
175   int32_t fA = FXSYS_wtoi(wsWideNarrowRatio->First(ptPos.value()).c_str());
176   float result = static_cast<float>(fA) / static_cast<float>(fB);
177   return static_cast<int8_t>(result);
178 }
179