1 // Copyright 2014 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/cxfa_ffbarcode.h"
8
9 #include <utility>
10
11 #include "core/fxcrt/fx_extension.h"
12 #include "third_party/base/ptr_util.h"
13 #include "xfa/fwl/cfwl_app.h"
14 #include "xfa/fwl/cfwl_barcode.h"
15 #include "xfa/fwl/cfwl_notedriver.h"
16 #include "xfa/fxfa/cxfa_fffield.h"
17 #include "xfa/fxfa/cxfa_ffpageview.h"
18 #include "xfa/fxfa/cxfa_ffwidget.h"
19 #include "xfa/fxfa/cxfa_fwladapterwidgetmgr.h"
20 #include "xfa/fxfa/parser/cxfa_border.h"
21
22 namespace {
23
24 const BarCodeInfo g_BarCodeData[] = {
25 {0x7fb4a18, L"ean13", BarcodeType::ean13, BC_EAN13},
26 {0x8d13a3d, L"code11", BarcodeType::code11, BC_UNKNOWN},
27 {0x8d149a8, L"code49", BarcodeType::code49, BC_UNKNOWN},
28 {0x8d16347, L"code93", BarcodeType::code93, BC_UNKNOWN},
29 {0x91a92e2, L"upsMaxicode", BarcodeType::upsMaxicode, BC_UNKNOWN},
30 {0xa7d48dc, L"fim", BarcodeType::fim, BC_UNKNOWN},
31 {0xb359fe9, L"msi", BarcodeType::msi, BC_UNKNOWN},
32 {0x121f738c, L"code2Of5Matrix", BarcodeType::code2Of5Matrix, BC_UNKNOWN},
33 {0x15358616, L"ucc128", BarcodeType::ucc128, BC_UNKNOWN},
34 {0x1f4bfa05, L"rfid", BarcodeType::rfid, BC_UNKNOWN},
35 {0x1fda71bc, L"rss14Stacked", BarcodeType::rss14Stacked, BC_UNKNOWN},
36 {0x22065087, L"ean8add2", BarcodeType::ean8add2, BC_UNKNOWN},
37 {0x2206508a, L"ean8add5", BarcodeType::ean8add5, BC_UNKNOWN},
38 {0x2278366c, L"codabar", BarcodeType::codabar, BC_CODABAR},
39 {0x2a039a8d, L"telepen", BarcodeType::telepen, BC_UNKNOWN},
40 {0x323ed337, L"upcApwcd", BarcodeType::upcApwcd, BC_UNKNOWN},
41 {0x347a1846, L"postUSIMB", BarcodeType::postUSIMB, BC_UNKNOWN},
42 {0x391bb836, L"code128", BarcodeType::code128, BC_CODE128},
43 {0x398eddaf, L"dataMatrix", BarcodeType::dataMatrix, BC_DATAMATRIX},
44 {0x3cff60a8, L"upcEadd2", BarcodeType::upcEadd2, BC_UNKNOWN},
45 {0x3cff60ab, L"upcEadd5", BarcodeType::upcEadd5, BC_UNKNOWN},
46 {0x402cb188, L"code2Of5Standard", BarcodeType::code2Of5Standard,
47 BC_UNKNOWN},
48 {0x411764f7, L"aztec", BarcodeType::aztec, BC_UNKNOWN},
49 {0x44d4e84c, L"ean8", BarcodeType::ean8, BC_EAN8},
50 {0x48468902, L"ucc128sscc", BarcodeType::ucc128sscc, BC_UNKNOWN},
51 {0x4880aea4, L"upcAadd2", BarcodeType::upcAadd2, BC_UNKNOWN},
52 {0x4880aea7, L"upcAadd5", BarcodeType::upcAadd5, BC_UNKNOWN},
53 {0x54f18256, L"code2Of5Industrial", BarcodeType::code2Of5Industrial,
54 BC_UNKNOWN},
55 {0x58e15f25, L"rss14Limited", BarcodeType::rss14Limited, BC_UNKNOWN},
56 {0x5c08d1b9, L"postAUSReplyPaid", BarcodeType::postAUSReplyPaid,
57 BC_UNKNOWN},
58 {0x5fa700bd, L"rss14", BarcodeType::rss14, BC_UNKNOWN},
59 {0x631a7e35, L"logmars", BarcodeType::logmars, BC_UNKNOWN},
60 {0x6a236236, L"pdf417", BarcodeType::pdf417, BC_PDF417},
61 {0x6d098ece, L"upcean2", BarcodeType::upcean2, BC_UNKNOWN},
62 {0x6d098ed1, L"upcean5", BarcodeType::upcean5, BC_UNKNOWN},
63 {0x76b04eed, L"code3Of9extended", BarcodeType::code3Of9extended,
64 BC_UNKNOWN},
65 {0x7c7db84a, L"maxicode", BarcodeType::maxicode, BC_UNKNOWN},
66 {0x8266f7f7, L"ucc128random", BarcodeType::ucc128random, BC_UNKNOWN},
67 {0x83eca147, L"postUSDPBC", BarcodeType::postUSDPBC, BC_UNKNOWN},
68 {0x8dd71de0, L"postAUSStandard", BarcodeType::postAUSStandard, BC_UNKNOWN},
69 {0x98adad85, L"plessey", BarcodeType::plessey, BC_UNKNOWN},
70 {0x9f84cce6, L"ean13pwcd", BarcodeType::ean13pwcd, BC_UNKNOWN},
71 {0xb514fbe9, L"upcA", BarcodeType::upcA, BC_UPCA},
72 {0xb514fbed, L"upcE", BarcodeType::upcE, BC_UNKNOWN},
73 {0xb5c6a853, L"ean13add2", BarcodeType::ean13add2, BC_UNKNOWN},
74 {0xb5c6a856, L"ean13add5", BarcodeType::ean13add5, BC_UNKNOWN},
75 {0xb81fc512, L"postUKRM4SCC", BarcodeType::postUKRM4SCC, BC_UNKNOWN},
76 {0xbad34b22, L"code128SSCC", BarcodeType::code128SSCC, BC_UNKNOWN},
77 {0xbfbe0cf6, L"postUS5Zip", BarcodeType::postUS5Zip, BC_UNKNOWN},
78 {0xc56618e8, L"pdf417macro", BarcodeType::pdf417macro, BC_UNKNOWN},
79 {0xca730f8a, L"code2Of5Interleaved", BarcodeType::code2Of5Interleaved,
80 BC_UNKNOWN},
81 {0xd0097ac6, L"rss14Expanded", BarcodeType::rss14Expanded, BC_UNKNOWN},
82 {0xd25a0240, L"postAUSCust2", BarcodeType::postAUSCust2, BC_UNKNOWN},
83 {0xd25a0241, L"postAUSCust3", BarcodeType::postAUSCust3, BC_UNKNOWN},
84 {0xd53ed3e7, L"rss14Truncated", BarcodeType::rss14Truncated, BC_UNKNOWN},
85 {0xe72bcd57, L"code128A", BarcodeType::code128A, BC_UNKNOWN},
86 {0xe72bcd58, L"code128B", BarcodeType::code128B, BC_CODE128_B},
87 {0xe72bcd59, L"code128C", BarcodeType::code128C, BC_CODE128_C},
88 {0xee83c50f, L"rss14StackedOmni", BarcodeType::rss14StackedOmni,
89 BC_UNKNOWN},
90 {0xf2a18f7e, L"QRCode", BarcodeType::QRCode, BC_QR_CODE},
91 {0xfaeaf37f, L"postUSStandard", BarcodeType::postUSStandard, BC_UNKNOWN},
92 {0xfb48155c, L"code3Of9", BarcodeType::code3Of9, BC_CODE39},
93 };
94
95 } // namespace.
96
97 // static
GetBarcodeTypeByName(const WideString & wsName)98 const BarCodeInfo* CXFA_FFBarcode::GetBarcodeTypeByName(
99 const WideString& wsName) {
100 if (wsName.IsEmpty())
101 return nullptr;
102
103 auto* it = std::lower_bound(
104 std::begin(g_BarCodeData), std::end(g_BarCodeData),
105 FX_HashCode_GetW(wsName.AsStringView(), true),
106 [](const BarCodeInfo& arg, uint32_t hash) { return arg.uHash < hash; });
107
108 if (it != std::end(g_BarCodeData) && wsName.AsStringView() == it->pName)
109 return it;
110
111 return nullptr;
112 }
113
CXFA_FFBarcode(CXFA_Node * pNode)114 CXFA_FFBarcode::CXFA_FFBarcode(CXFA_Node* pNode) : CXFA_FFTextEdit(pNode) {}
115
~CXFA_FFBarcode()116 CXFA_FFBarcode::~CXFA_FFBarcode() {}
117
LoadWidget()118 bool CXFA_FFBarcode::LoadWidget() {
119 auto pNew = pdfium::MakeUnique<CFWL_Barcode>(GetFWLApp());
120 CFWL_Barcode* pFWLBarcode = pNew.get();
121 m_pNormalWidget = std::move(pNew);
122 m_pNormalWidget->SetLayoutItem(this);
123
124 CFWL_NoteDriver* pNoteDriver =
125 m_pNormalWidget->GetOwnerApp()->GetNoteDriver();
126 pNoteDriver->RegisterEventTarget(m_pNormalWidget.get(),
127 m_pNormalWidget.get());
128 m_pOldDelegate = m_pNormalWidget->GetDelegate();
129 m_pNormalWidget->SetDelegate(this);
130 m_pNormalWidget->LockUpdate();
131
132 pFWLBarcode->SetText(
133 m_pNode->GetWidgetAcc()->GetValue(XFA_VALUEPICTURE_Display));
134 UpdateWidgetProperty();
135 m_pNormalWidget->UnlockUpdate();
136 return CXFA_FFField::LoadWidget();
137 }
138
RenderWidget(CXFA_Graphics * pGS,const CFX_Matrix & matrix,uint32_t dwStatus)139 void CXFA_FFBarcode::RenderWidget(CXFA_Graphics* pGS,
140 const CFX_Matrix& matrix,
141 uint32_t dwStatus) {
142 if (!IsMatchVisibleStatus(dwStatus))
143 return;
144
145 CFX_Matrix mtRotate = GetRotateMatrix();
146 mtRotate.Concat(matrix);
147
148 CXFA_FFWidget::RenderWidget(pGS, mtRotate, dwStatus);
149 DrawBorder(pGS, m_pNode->GetWidgetAcc()->GetUIBorder(), m_rtUI, mtRotate);
150 RenderCaption(pGS, &mtRotate);
151 CFX_RectF rtWidget = m_pNormalWidget->GetWidgetRect();
152
153 CFX_Matrix mt(1, 0, 0, 1, rtWidget.left, rtWidget.top);
154 mt.Concat(mtRotate);
155 m_pNormalWidget->DrawWidget(pGS, mt);
156 }
157
UpdateWidgetProperty()158 void CXFA_FFBarcode::UpdateWidgetProperty() {
159 CXFA_FFTextEdit::UpdateWidgetProperty();
160
161 auto* node = GetNode();
162 const BarCodeInfo* info = GetBarcodeTypeByName(node->GetBarcodeType());
163 if (!info)
164 return;
165
166 auto* pBarCodeWidget = static_cast<CFWL_Barcode*>(m_pNormalWidget.get());
167 pBarCodeWidget->SetType(info->eBCType);
168
169 Optional<BC_CHAR_ENCODING> encoding =
170 node->GetBarcodeAttribute_CharEncoding();
171 if (encoding)
172 pBarCodeWidget->SetCharEncoding(*encoding);
173
174 Optional<bool> calcChecksum = node->GetBarcodeAttribute_Checksum();
175 if (calcChecksum)
176 pBarCodeWidget->SetCalChecksum(*calcChecksum);
177
178 Optional<int32_t> dataLen = node->GetBarcodeAttribute_DataLength();
179 if (dataLen)
180 pBarCodeWidget->SetDataLength(*dataLen);
181
182 Optional<char> startChar = node->GetBarcodeAttribute_StartChar();
183 if (startChar)
184 pBarCodeWidget->SetStartChar(*startChar);
185
186 Optional<char> endChar = node->GetBarcodeAttribute_EndChar();
187 if (endChar)
188 pBarCodeWidget->SetEndChar(*endChar);
189
190 Optional<int32_t> ecLevel = node->GetBarcodeAttribute_ECLevel();
191 if (ecLevel)
192 pBarCodeWidget->SetErrorCorrectionLevel(*ecLevel);
193
194 Optional<int32_t> width = node->GetBarcodeAttribute_ModuleWidth();
195 if (width)
196 pBarCodeWidget->SetModuleWidth(*width);
197
198 Optional<int32_t> height = node->GetBarcodeAttribute_ModuleHeight();
199 if (height)
200 pBarCodeWidget->SetModuleHeight(*height);
201
202 Optional<bool> printCheck = node->GetBarcodeAttribute_PrintChecksum();
203 if (printCheck)
204 pBarCodeWidget->SetPrintChecksum(*printCheck);
205
206 Optional<BC_TEXT_LOC> textLoc = node->GetBarcodeAttribute_TextLocation();
207 if (textLoc)
208 pBarCodeWidget->SetTextLocation(*textLoc);
209
210 Optional<bool> truncate = node->GetBarcodeAttribute_Truncate();
211 if (truncate)
212 pBarCodeWidget->SetTruncated(*truncate);
213
214 Optional<int8_t> ratio = node->GetBarcodeAttribute_WideNarrowRatio();
215 if (ratio)
216 pBarCodeWidget->SetWideNarrowRatio(*ratio);
217
218 if (info->eName == BarcodeType::code3Of9 ||
219 info->eName == BarcodeType::ean8 || info->eName == BarcodeType::ean13 ||
220 info->eName == BarcodeType::upcA) {
221 pBarCodeWidget->SetPrintChecksum(true);
222 }
223 }
224
OnLButtonDown(uint32_t dwFlags,const CFX_PointF & point)225 bool CXFA_FFBarcode::OnLButtonDown(uint32_t dwFlags, const CFX_PointF& point) {
226 auto* pBarCodeWidget = static_cast<CFWL_Barcode*>(m_pNormalWidget.get());
227 if (!pBarCodeWidget || pBarCodeWidget->IsProtectedType())
228 return false;
229 if (!m_pNode->IsOpenAccess())
230 return false;
231 return CXFA_FFTextEdit::OnLButtonDown(dwFlags, point);
232 }
233
OnRButtonDown(uint32_t dwFlags,const CFX_PointF & point)234 bool CXFA_FFBarcode::OnRButtonDown(uint32_t dwFlags, const CFX_PointF& point) {
235 auto* pBarCodeWidget = static_cast<CFWL_Barcode*>(m_pNormalWidget.get());
236 if (!pBarCodeWidget || pBarCodeWidget->IsProtectedType())
237 return false;
238 return CXFA_FFTextEdit::OnRButtonDown(dwFlags, point);
239 }
240