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_barcode.h"
21 #include "xfa/fxfa/parser/cxfa_border.h"
22 
23 namespace {
24 
25 const BarCodeInfo g_BarCodeData[] = {
26     {0x7fb4a18, "ean13", BarcodeType::ean13, BC_EAN13},
27     {0x8d13a3d, "code11", BarcodeType::code11, BC_UNKNOWN},
28     {0x8d149a8, "code49", BarcodeType::code49, BC_UNKNOWN},
29     {0x8d16347, "code93", BarcodeType::code93, BC_UNKNOWN},
30     {0x91a92e2, "upsMaxicode", BarcodeType::upsMaxicode, BC_UNKNOWN},
31     {0xa7d48dc, "fim", BarcodeType::fim, BC_UNKNOWN},
32     {0xb359fe9, "msi", BarcodeType::msi, BC_UNKNOWN},
33     {0x121f738c, "code2Of5Matrix", BarcodeType::code2Of5Matrix, BC_UNKNOWN},
34     {0x15358616, "ucc128", BarcodeType::ucc128, BC_UNKNOWN},
35     {0x1f4bfa05, "rfid", BarcodeType::rfid, BC_UNKNOWN},
36     {0x1fda71bc, "rss14Stacked", BarcodeType::rss14Stacked, BC_UNKNOWN},
37     {0x22065087, "ean8add2", BarcodeType::ean8add2, BC_UNKNOWN},
38     {0x2206508a, "ean8add5", BarcodeType::ean8add5, BC_UNKNOWN},
39     {0x2278366c, "codabar", BarcodeType::codabar, BC_CODABAR},
40     {0x2a039a8d, "telepen", BarcodeType::telepen, BC_UNKNOWN},
41     {0x323ed337, "upcApwcd", BarcodeType::upcApwcd, BC_UNKNOWN},
42     {0x347a1846, "postUSIMB", BarcodeType::postUSIMB, BC_UNKNOWN},
43     {0x391bb836, "code128", BarcodeType::code128, BC_CODE128},
44     {0x398eddaf, "dataMatrix", BarcodeType::dataMatrix, BC_DATAMATRIX},
45     {0x3cff60a8, "upcEadd2", BarcodeType::upcEadd2, BC_UNKNOWN},
46     {0x3cff60ab, "upcEadd5", BarcodeType::upcEadd5, BC_UNKNOWN},
47     {0x402cb188, "code2Of5Standard", BarcodeType::code2Of5Standard, BC_UNKNOWN},
48     {0x411764f7, "aztec", BarcodeType::aztec, BC_UNKNOWN},
49     {0x44d4e84c, "ean8", BarcodeType::ean8, BC_EAN8},
50     {0x48468902, "ucc128sscc", BarcodeType::ucc128sscc, BC_UNKNOWN},
51     {0x4880aea4, "upcAadd2", BarcodeType::upcAadd2, BC_UNKNOWN},
52     {0x4880aea7, "upcAadd5", BarcodeType::upcAadd5, BC_UNKNOWN},
53     {0x54f18256, "code2Of5Industrial", BarcodeType::code2Of5Industrial,
54      BC_UNKNOWN},
55     {0x58e15f25, "rss14Limited", BarcodeType::rss14Limited, BC_UNKNOWN},
56     {0x5c08d1b9, "postAUSReplyPaid", BarcodeType::postAUSReplyPaid, BC_UNKNOWN},
57     {0x5fa700bd, "rss14", BarcodeType::rss14, BC_UNKNOWN},
58     {0x631a7e35, "logmars", BarcodeType::logmars, BC_UNKNOWN},
59     {0x6a236236, "pdf417", BarcodeType::pdf417, BC_PDF417},
60     {0x6d098ece, "upcean2", BarcodeType::upcean2, BC_UNKNOWN},
61     {0x6d098ed1, "upcean5", BarcodeType::upcean5, BC_UNKNOWN},
62     {0x76b04eed, "code3Of9extended", BarcodeType::code3Of9extended, BC_UNKNOWN},
63     {0x7c7db84a, "maxicode", BarcodeType::maxicode, BC_UNKNOWN},
64     {0x8266f7f7, "ucc128random", BarcodeType::ucc128random, BC_UNKNOWN},
65     {0x83eca147, "postUSDPBC", BarcodeType::postUSDPBC, BC_UNKNOWN},
66     {0x8dd71de0, "postAUSStandard", BarcodeType::postAUSStandard, BC_UNKNOWN},
67     {0x98adad85, "plessey", BarcodeType::plessey, BC_UNKNOWN},
68     {0x9f84cce6, "ean13pwcd", BarcodeType::ean13pwcd, BC_UNKNOWN},
69     {0xb514fbe9, "upcA", BarcodeType::upcA, BC_UPCA},
70     {0xb514fbed, "upcE", BarcodeType::upcE, BC_UNKNOWN},
71     {0xb5c6a853, "ean13add2", BarcodeType::ean13add2, BC_UNKNOWN},
72     {0xb5c6a856, "ean13add5", BarcodeType::ean13add5, BC_UNKNOWN},
73     {0xb81fc512, "postUKRM4SCC", BarcodeType::postUKRM4SCC, BC_UNKNOWN},
74     {0xbad34b22, "code128SSCC", BarcodeType::code128SSCC, BC_UNKNOWN},
75     {0xbfbe0cf6, "postUS5Zip", BarcodeType::postUS5Zip, BC_UNKNOWN},
76     {0xc56618e8, "pdf417macro", BarcodeType::pdf417macro, BC_UNKNOWN},
77     {0xca730f8a, "code2Of5Interleaved", BarcodeType::code2Of5Interleaved,
78      BC_UNKNOWN},
79     {0xd0097ac6, "rss14Expanded", BarcodeType::rss14Expanded, BC_UNKNOWN},
80     {0xd25a0240, "postAUSCust2", BarcodeType::postAUSCust2, BC_UNKNOWN},
81     {0xd25a0241, "postAUSCust3", BarcodeType::postAUSCust3, BC_UNKNOWN},
82     {0xd53ed3e7, "rss14Truncated", BarcodeType::rss14Truncated, BC_UNKNOWN},
83     {0xe72bcd57, "code128A", BarcodeType::code128A, BC_UNKNOWN},
84     {0xe72bcd58, "code128B", BarcodeType::code128B, BC_CODE128_B},
85     {0xe72bcd59, "code128C", BarcodeType::code128C, BC_CODE128_C},
86     {0xee83c50f, "rss14StackedOmni", BarcodeType::rss14StackedOmni, BC_UNKNOWN},
87     {0xf2a18f7e, "QRCode", BarcodeType::QRCode, BC_QR_CODE},
88     {0xfaeaf37f, "postUSStandard", BarcodeType::postUSStandard, BC_UNKNOWN},
89     {0xfb48155c, "code3Of9", BarcodeType::code3Of9, BC_CODE39},
90 };
91 
CharEncodingFromString(const WideString & value)92 Optional<BC_CHAR_ENCODING> CharEncodingFromString(const WideString& value) {
93   if (value.CompareNoCase(L"UTF-16"))
94     return CHAR_ENCODING_UNICODE;
95   if (value.CompareNoCase(L"UTF-8"))
96     return CHAR_ENCODING_UTF8;
97   return {};
98 }
99 
TextLocFromAttribute(XFA_AttributeValue value)100 Optional<BC_TEXT_LOC> TextLocFromAttribute(XFA_AttributeValue value) {
101   switch (value) {
102     case XFA_AttributeValue::None:
103       return BC_TEXT_LOC_NONE;
104     case XFA_AttributeValue::Above:
105       return BC_TEXT_LOC_ABOVE;
106     case XFA_AttributeValue::Below:
107       return BC_TEXT_LOC_BELOW;
108     case XFA_AttributeValue::AboveEmbedded:
109       return BC_TEXT_LOC_ABOVEEMBED;
110     case XFA_AttributeValue::BelowEmbedded:
111       return BC_TEXT_LOC_BELOWEMBED;
112     default:
113       return {};
114   }
115 }
116 
117 }  // namespace.
118 
119 // static
GetBarcodeTypeByName(const WideString & wsName)120 const BarCodeInfo* CXFA_FFBarcode::GetBarcodeTypeByName(
121     const WideString& wsName) {
122   if (wsName.IsEmpty())
123     return nullptr;
124 
125   auto* it = std::lower_bound(
126       std::begin(g_BarCodeData), std::end(g_BarCodeData),
127       FX_HashCode_GetW(wsName.AsStringView(), true),
128       [](const BarCodeInfo& arg, uint32_t hash) { return arg.uHash < hash; });
129 
130   if (it != std::end(g_BarCodeData) && wsName.EqualsASCII(it->pName))
131     return it;
132 
133   return nullptr;
134 }
135 
CXFA_FFBarcode(CXFA_Node * pNode,CXFA_Barcode * barcode)136 CXFA_FFBarcode::CXFA_FFBarcode(CXFA_Node* pNode, CXFA_Barcode* barcode)
137     : CXFA_FFTextEdit(pNode), barcode_(barcode) {}
138 
139 CXFA_FFBarcode::~CXFA_FFBarcode() = default;
140 
LoadWidget()141 bool CXFA_FFBarcode::LoadWidget() {
142   ASSERT(!IsLoaded());
143 
144   // Prevents destruction of the CXFA_ContentLayoutItem that owns |this|.
145   RetainPtr<CXFA_ContentLayoutItem> retain_layout(m_pLayoutItem.Get());
146 
147   auto pNew = pdfium::MakeUnique<CFWL_Barcode>(GetFWLApp());
148   CFWL_Barcode* pFWLBarcode = pNew.get();
149   SetNormalWidget(std::move(pNew));
150   pFWLBarcode->SetAdapterIface(this);
151 
152   CFWL_NoteDriver* pNoteDriver = pFWLBarcode->GetOwnerApp()->GetNoteDriver();
153   pNoteDriver->RegisterEventTarget(pFWLBarcode, pFWLBarcode);
154   m_pOldDelegate = pFWLBarcode->GetDelegate();
155   pFWLBarcode->SetDelegate(this);
156 
157   {
158     CFWL_Widget::ScopedUpdateLock update_lock(pFWLBarcode);
159     pFWLBarcode->SetText(m_pNode->GetValue(XFA_VALUEPICTURE_Display));
160     UpdateWidgetProperty();
161   }
162 
163   return CXFA_FFField::LoadWidget();
164 }
165 
RenderWidget(CXFA_Graphics * pGS,const CFX_Matrix & matrix,HighlightOption highlight)166 void CXFA_FFBarcode::RenderWidget(CXFA_Graphics* pGS,
167                                   const CFX_Matrix& matrix,
168                                   HighlightOption highlight) {
169   if (!HasVisibleStatus())
170     return;
171 
172   CFX_Matrix mtRotate = GetRotateMatrix();
173   mtRotate.Concat(matrix);
174 
175   CXFA_FFWidget::RenderWidget(pGS, mtRotate, highlight);
176   DrawBorder(pGS, m_pNode->GetUIBorder(), m_rtUI, mtRotate);
177   RenderCaption(pGS, &mtRotate);
178   CFX_RectF rtWidget = GetNormalWidget()->GetWidgetRect();
179 
180   CFX_Matrix mt(1, 0, 0, 1, rtWidget.left, rtWidget.top);
181   mt.Concat(mtRotate);
182   GetNormalWidget()->DrawWidget(pGS, mt);
183 }
184 
UpdateWidgetProperty()185 void CXFA_FFBarcode::UpdateWidgetProperty() {
186   CXFA_FFTextEdit::UpdateWidgetProperty();
187 
188   const BarCodeInfo* info = GetBarcodeTypeByName(barcode_->GetBarcodeType());
189   if (!info)
190     return;
191 
192   auto* pBarCodeWidget = static_cast<CFWL_Barcode*>(GetNormalWidget());
193   pBarCodeWidget->SetType(info->eBCType);
194 
195   Optional<WideString> encoding_string = barcode_->GetCharEncoding();
196   if (encoding_string) {
197     Optional<BC_CHAR_ENCODING> encoding =
198         CharEncodingFromString(*encoding_string);
199     if (encoding)
200       pBarCodeWidget->SetCharEncoding(*encoding);
201   }
202 
203   Optional<bool> calcChecksum = barcode_->GetChecksum();
204   if (calcChecksum)
205     pBarCodeWidget->SetCalChecksum(*calcChecksum);
206 
207   Optional<int32_t> dataLen = barcode_->GetDataLength();
208   if (dataLen)
209     pBarCodeWidget->SetDataLength(*dataLen);
210 
211   Optional<char> startChar = barcode_->GetStartChar();
212   if (startChar)
213     pBarCodeWidget->SetStartChar(*startChar);
214 
215   Optional<char> endChar = barcode_->GetEndChar();
216   if (endChar)
217     pBarCodeWidget->SetEndChar(*endChar);
218 
219   Optional<int32_t> ecLevel = barcode_->GetECLevel();
220   if (ecLevel)
221     pBarCodeWidget->SetErrorCorrectionLevel(*ecLevel);
222 
223   Optional<int32_t> width = barcode_->GetModuleWidth();
224   if (width)
225     pBarCodeWidget->SetModuleWidth(*width);
226 
227   Optional<int32_t> height = barcode_->GetModuleHeight();
228   if (height)
229     pBarCodeWidget->SetModuleHeight(*height);
230 
231   Optional<bool> printCheck = barcode_->GetPrintChecksum();
232   if (printCheck)
233     pBarCodeWidget->SetPrintChecksum(*printCheck);
234 
235   Optional<XFA_AttributeValue> text_attr = barcode_->GetTextLocation();
236   if (text_attr) {
237     Optional<BC_TEXT_LOC> textLoc = TextLocFromAttribute(*text_attr);
238     if (textLoc)
239       pBarCodeWidget->SetTextLocation(*textLoc);
240   }
241 
242   // Truncated is currently not a supported flag.
243 
244   Optional<int8_t> ratio = barcode_->GetWideNarrowRatio();
245   if (ratio)
246     pBarCodeWidget->SetWideNarrowRatio(*ratio);
247 
248   if (info->eName == BarcodeType::code3Of9 ||
249       info->eName == BarcodeType::ean8 || info->eName == BarcodeType::ean13 ||
250       info->eName == BarcodeType::upcA) {
251     pBarCodeWidget->SetPrintChecksum(true);
252   }
253 }
254 
AcceptsFocusOnButtonDown(uint32_t dwFlags,const CFX_PointF & point,FWL_MouseCommand command)255 bool CXFA_FFBarcode::AcceptsFocusOnButtonDown(uint32_t dwFlags,
256                                               const CFX_PointF& point,
257                                               FWL_MouseCommand command) {
258   auto* pBarCodeWidget = static_cast<CFWL_Barcode*>(GetNormalWidget());
259   if (!pBarCodeWidget || pBarCodeWidget->IsProtectedType())
260     return false;
261   if (command == FWL_MouseCommand::LeftButtonDown && !m_pNode->IsOpenAccess())
262     return false;
263 
264   return CXFA_FFTextEdit::AcceptsFocusOnButtonDown(dwFlags, point, command);
265 }
266