• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   auto pNew = pdfium::MakeUnique<CFWL_Barcode>(GetFWLApp());
144   CFWL_Barcode* pFWLBarcode = pNew.get();
145   SetNormalWidget(std::move(pNew));
146   pFWLBarcode->SetAdapterIface(this);
147 
148   CFWL_NoteDriver* pNoteDriver = pFWLBarcode->GetOwnerApp()->GetNoteDriver();
149   pNoteDriver->RegisterEventTarget(pFWLBarcode, pFWLBarcode);
150   m_pOldDelegate = pFWLBarcode->GetDelegate();
151   pFWLBarcode->SetDelegate(this);
152 
153   {
154     CFWL_Widget::ScopedUpdateLock update_lock(pFWLBarcode);
155     pFWLBarcode->SetText(m_pNode->GetValue(XFA_VALUEPICTURE_Display));
156     UpdateWidgetProperty();
157   }
158 
159   return CXFA_FFField::LoadWidget();
160 }
161 
RenderWidget(CXFA_Graphics * pGS,const CFX_Matrix & matrix,HighlightOption highlight)162 void CXFA_FFBarcode::RenderWidget(CXFA_Graphics* pGS,
163                                   const CFX_Matrix& matrix,
164                                   HighlightOption highlight) {
165   if (!HasVisibleStatus())
166     return;
167 
168   CFX_Matrix mtRotate = GetRotateMatrix();
169   mtRotate.Concat(matrix);
170 
171   CXFA_FFWidget::RenderWidget(pGS, mtRotate, highlight);
172   DrawBorder(pGS, m_pNode->GetUIBorder(), m_rtUI, mtRotate);
173   RenderCaption(pGS, &mtRotate);
174   CFX_RectF rtWidget = GetNormalWidget()->GetWidgetRect();
175 
176   CFX_Matrix mt(1, 0, 0, 1, rtWidget.left, rtWidget.top);
177   mt.Concat(mtRotate);
178   GetNormalWidget()->DrawWidget(pGS, mt);
179 }
180 
UpdateWidgetProperty()181 void CXFA_FFBarcode::UpdateWidgetProperty() {
182   CXFA_FFTextEdit::UpdateWidgetProperty();
183 
184   const BarCodeInfo* info = GetBarcodeTypeByName(barcode_->GetBarcodeType());
185   if (!info)
186     return;
187 
188   auto* pBarCodeWidget = static_cast<CFWL_Barcode*>(GetNormalWidget());
189   pBarCodeWidget->SetType(info->eBCType);
190 
191   Optional<WideString> encoding_string = barcode_->GetCharEncoding();
192   if (encoding_string) {
193     Optional<BC_CHAR_ENCODING> encoding =
194         CharEncodingFromString(*encoding_string);
195     if (encoding)
196       pBarCodeWidget->SetCharEncoding(*encoding);
197   }
198 
199   Optional<bool> calcChecksum = barcode_->GetChecksum();
200   if (calcChecksum)
201     pBarCodeWidget->SetCalChecksum(*calcChecksum);
202 
203   Optional<int32_t> dataLen = barcode_->GetDataLength();
204   if (dataLen)
205     pBarCodeWidget->SetDataLength(*dataLen);
206 
207   Optional<char> startChar = barcode_->GetStartChar();
208   if (startChar)
209     pBarCodeWidget->SetStartChar(*startChar);
210 
211   Optional<char> endChar = barcode_->GetEndChar();
212   if (endChar)
213     pBarCodeWidget->SetEndChar(*endChar);
214 
215   Optional<int32_t> ecLevel = barcode_->GetECLevel();
216   if (ecLevel)
217     pBarCodeWidget->SetErrorCorrectionLevel(*ecLevel);
218 
219   Optional<int32_t> width = barcode_->GetModuleWidth();
220   if (width)
221     pBarCodeWidget->SetModuleWidth(*width);
222 
223   Optional<int32_t> height = barcode_->GetModuleHeight();
224   if (height)
225     pBarCodeWidget->SetModuleHeight(*height);
226 
227   Optional<bool> printCheck = barcode_->GetPrintChecksum();
228   if (printCheck)
229     pBarCodeWidget->SetPrintChecksum(*printCheck);
230 
231   Optional<XFA_AttributeValue> text_attr = barcode_->GetTextLocation();
232   if (text_attr) {
233     Optional<BC_TEXT_LOC> textLoc = TextLocFromAttribute(*text_attr);
234     if (textLoc)
235       pBarCodeWidget->SetTextLocation(*textLoc);
236   }
237 
238   // Truncated is currently not a supported flag.
239 
240   Optional<int8_t> ratio = barcode_->GetWideNarrowRatio();
241   if (ratio)
242     pBarCodeWidget->SetWideNarrowRatio(*ratio);
243 
244   if (info->eName == BarcodeType::code3Of9 ||
245       info->eName == BarcodeType::ean8 || info->eName == BarcodeType::ean13 ||
246       info->eName == BarcodeType::upcA) {
247     pBarCodeWidget->SetPrintChecksum(true);
248   }
249 }
250 
AcceptsFocusOnButtonDown(uint32_t dwFlags,const CFX_PointF & point,FWL_MouseCommand command)251 bool CXFA_FFBarcode::AcceptsFocusOnButtonDown(uint32_t dwFlags,
252                                               const CFX_PointF& point,
253                                               FWL_MouseCommand command) {
254   auto* pBarCodeWidget = static_cast<CFWL_Barcode*>(GetNormalWidget());
255   if (!pBarCodeWidget || pBarCodeWidget->IsProtectedType())
256     return false;
257   if (command == FWL_MouseCommand::LeftButtonDown && !m_pNode->IsOpenAccess())
258     return false;
259 
260   return CXFA_FFTextEdit::AcceptsFocusOnButtonDown(dwFlags, point, command);
261 }
262