Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
cxfa_ffbarcode.cpp
Go to the documentation of this file.
1// Copyright 2014 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/cxfa_ffbarcode.h"
8
9#include <utility>
10
11#include "core/fxcrt/fx_extension.h"
12#include "third_party/base/check.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
23namespace {
24
25struct BarCodeInfo {
26 uint32_t uHash; // `pName` hashed as if wide string.
27 const char pName[20]; // Inline string data reduces size for small strings.
28 BC_TYPE eBCType;
29};
30
31const BarCodeInfo kBarCodeData[] = {
32 {0x7fb4a18, "ean13", BC_TYPE::kEAN13},
33 {0x8d13a3d, "code11", BC_TYPE::kUnknown},
34 {0x8d149a8, "code49", BC_TYPE::kUnknown},
35 {0x8d16347, "code93", BC_TYPE::kUnknown},
36 {0x91a92e2, "upsMaxicode", BC_TYPE::kUnknown},
37 {0xa7d48dc, "fim", BC_TYPE::kUnknown},
38 {0xb359fe9, "msi", BC_TYPE::kUnknown},
39 {0x121f738c, "code2Of5Matrix", BC_TYPE::kUnknown},
40 {0x15358616, "ucc128", BC_TYPE::kUnknown},
41 {0x1f4bfa05, "rfid", BC_TYPE::kUnknown},
42 {0x1fda71bc, "rss14Stacked", BC_TYPE::kUnknown},
43 {0x22065087, "ean8add2", BC_TYPE::kUnknown},
44 {0x2206508a, "ean8add5", BC_TYPE::kUnknown},
45 {0x2278366c, "codabar", BC_TYPE::kCodabar},
46 {0x2a039a8d, "telepen", BC_TYPE::kUnknown},
47 {0x323ed337, "upcApwcd", BC_TYPE::kUnknown},
48 {0x347a1846, "postUSIMB", BC_TYPE::kUnknown},
49 {0x391bb836, "code128", BC_TYPE::kCode128},
50 {0x398eddaf, "dataMatrix", BC_TYPE::kDataMatrix},
51 {0x3cff60a8, "upcEadd2", BC_TYPE::kUnknown},
52 {0x3cff60ab, "upcEadd5", BC_TYPE::kUnknown},
53 {0x402cb188, "code2Of5Standard", BC_TYPE::kUnknown},
54 {0x411764f7, "aztec", BC_TYPE::kUnknown},
55 {0x44d4e84c, "ean8", BC_TYPE::kEAN8},
56 {0x48468902, "ucc128sscc", BC_TYPE::kUnknown},
57 {0x4880aea4, "upcAadd2", BC_TYPE::kUnknown},
58 {0x4880aea7, "upcAadd5", BC_TYPE::kUnknown},
59 {0x54f18256, "code2Of5Industrial", BC_TYPE::kUnknown},
60 {0x58e15f25, "rss14Limited", BC_TYPE::kUnknown},
61 {0x5c08d1b9, "postAUSReplyPaid", BC_TYPE::kUnknown},
62 {0x5fa700bd, "rss14", BC_TYPE::kUnknown},
63 {0x631a7e35, "logmars", BC_TYPE::kUnknown},
64 {0x6a236236, "pdf417", BC_TYPE::kPDF417},
65 {0x6d098ece, "upcean2", BC_TYPE::kUnknown},
66 {0x6d098ed1, "upcean5", BC_TYPE::kUnknown},
67 {0x76b04eed, "code3Of9extended", BC_TYPE::kUnknown},
68 {0x7c7db84a, "maxicode", BC_TYPE::kUnknown},
69 {0x8266f7f7, "ucc128random", BC_TYPE::kUnknown},
70 {0x83eca147, "postUSDPBC", BC_TYPE::kUnknown},
71 {0x8dd71de0, "postAUSStandard", BC_TYPE::kUnknown},
72 {0x98adad85, "plessey", BC_TYPE::kUnknown},
73 {0x9f84cce6, "ean13pwcd", BC_TYPE::kUnknown},
74 {0xb514fbe9, "upcA", BC_TYPE::kUPCA},
75 {0xb514fbed, "upcE", BC_TYPE::kUnknown},
76 {0xb5c6a853, "ean13add2", BC_TYPE::kUnknown},
77 {0xb5c6a856, "ean13add5", BC_TYPE::kUnknown},
78 {0xb81fc512, "postUKRM4SCC", BC_TYPE::kUnknown},
79 {0xbad34b22, "code128SSCC", BC_TYPE::kUnknown},
80 {0xbfbe0cf6, "postUS5Zip", BC_TYPE::kUnknown},
81 {0xc56618e8, "pdf417macro", BC_TYPE::kUnknown},
82 {0xca730f8a, "code2Of5Interleaved", BC_TYPE::kUnknown},
83 {0xd0097ac6, "rss14Expanded", BC_TYPE::kUnknown},
84 {0xd25a0240, "postAUSCust2", BC_TYPE::kUnknown},
85 {0xd25a0241, "postAUSCust3", BC_TYPE::kUnknown},
86 {0xd53ed3e7, "rss14Truncated", BC_TYPE::kUnknown},
87 {0xe72bcd57, "code128A", BC_TYPE::kUnknown},
88 {0xe72bcd58, "code128B", BC_TYPE::kCode128B},
89 {0xe72bcd59, "code128C", BC_TYPE::kCode128C},
90 {0xee83c50f, "rss14StackedOmni", BC_TYPE::kUnknown},
91 {0xf2a18f7e, "QRCode", BC_TYPE::kQRCode},
92 {0xfaeaf37f, "postUSStandard", BC_TYPE::kUnknown},
93 {0xfb48155c, "code3Of9", BC_TYPE::kCode39},
94};
95
96absl::optional<BC_CHAR_ENCODING> CharEncodingFromString(
97 const WideString& value) {
98 if (value.CompareNoCase(L"UTF-16"))
100 if (value.CompareNoCase(L"UTF-8"))
102 return absl::nullopt;
103}
104
105absl::optional<BC_TEXT_LOC> TextLocFromAttribute(XFA_AttributeValue value) {
106 switch (value) {
107 case XFA_AttributeValue::None:
108 return BC_TEXT_LOC::kNone;
109 case XFA_AttributeValue::Above:
110 return BC_TEXT_LOC::kAbove;
111 case XFA_AttributeValue::Below:
112 return BC_TEXT_LOC::kBelow;
113 case XFA_AttributeValue::AboveEmbedded:
115 case XFA_AttributeValue::BelowEmbedded:
117 default:
118 return absl::nullopt;
119 }
120}
121
122} // namespace.
123
124// static
125BC_TYPE CXFA_FFBarcode::GetBarcodeTypeByName(const WideString& wsName) {
126 if (wsName.IsEmpty())
127 return BC_TYPE::kUnknown;
128
129 auto* it = std::lower_bound(
130 std::begin(kBarCodeData), std::end(kBarCodeData),
131 FX_HashCode_GetLoweredW(wsName.AsStringView()),
132 [](const BarCodeInfo& arg, uint32_t hash) { return arg.uHash < hash; });
133
134 if (it != std::end(kBarCodeData) && wsName.EqualsASCII(it->pName))
135 return it->eBCType;
136
137 return BC_TYPE::kUnknown;
138}
139
140CXFA_FFBarcode::CXFA_FFBarcode(CXFA_Node* pNode, CXFA_Barcode* barcode)
141 : CXFA_FFTextEdit(pNode), barcode_(barcode) {}
142
143CXFA_FFBarcode::~CXFA_FFBarcode() = default;
144
145void CXFA_FFBarcode::Trace(cppgc::Visitor* visitor) const {
146 CXFA_FFTextEdit::Trace(visitor);
147 visitor->Trace(barcode_);
148}
149
150bool CXFA_FFBarcode::LoadWidget() {
151 DCHECK(!IsLoaded());
152
153 CFWL_Barcode* pFWLBarcode = cppgc::MakeGarbageCollected<CFWL_Barcode>(
154 GetFWLApp()->GetHeap()->GetAllocationHandle(), GetFWLApp());
155 SetNormalWidget(pFWLBarcode);
156 pFWLBarcode->SetAdapterIface(this);
157
158 CFWL_NoteDriver* pNoteDriver = pFWLBarcode->GetFWLApp()->GetNoteDriver();
159 pNoteDriver->RegisterEventTarget(pFWLBarcode, pFWLBarcode);
160 m_pOldDelegate = pFWLBarcode->GetDelegate();
161 pFWLBarcode->SetDelegate(this);
162
163 {
164 CFWL_Widget::ScopedUpdateLock update_lock(pFWLBarcode);
165 pFWLBarcode->SetText(m_pNode->GetValue(XFA_ValuePicture::kDisplay));
167 }
168
170}
171
172void CXFA_FFBarcode::RenderWidget(CFGAS_GEGraphics* pGS,
173 const CFX_Matrix& matrix,
174 HighlightOption highlight) {
175 if (!HasVisibleStatus())
176 return;
177
178 CFX_Matrix mtRotate = GetRotateMatrix();
179 mtRotate.Concat(matrix);
180
181 CXFA_FFWidget::RenderWidget(pGS, mtRotate, highlight);
182 DrawBorder(pGS, m_pNode->GetUIBorder(), m_UIRect, mtRotate);
183 RenderCaption(pGS, mtRotate);
184 CFX_RectF rtWidget = GetNormalWidget()->GetWidgetRect();
185
186 CFX_Matrix mt(1, 0, 0, 1, rtWidget.left, rtWidget.top);
187 mt.Concat(mtRotate);
188 GetNormalWidget()->DrawWidget(pGS, mt);
189}
190
191void CXFA_FFBarcode::UpdateWidgetProperty() {
193
194 BC_TYPE bc_type = GetBarcodeTypeByName(barcode_->GetBarcodeType());
195 if (bc_type == BC_TYPE::kUnknown)
196 return;
197
198 auto* pBarCodeWidget = static_cast<CFWL_Barcode*>(GetNormalWidget());
199 pBarCodeWidget->SetType(bc_type);
200
201 absl::optional<WideString> encoding_string = barcode_->GetCharEncoding();
202 if (encoding_string.has_value()) {
203 absl::optional<BC_CHAR_ENCODING> encoding =
204 CharEncodingFromString(encoding_string.value());
205 if (encoding.has_value())
206 pBarCodeWidget->SetCharEncoding(encoding.value());
207 }
208
209 absl::optional<bool> calcChecksum = barcode_->GetChecksum();
210 if (calcChecksum.has_value())
211 pBarCodeWidget->SetCalChecksum(calcChecksum.value());
212
213 absl::optional<int32_t> dataLen = barcode_->GetDataLength();
214 if (dataLen.has_value())
215 pBarCodeWidget->SetDataLength(dataLen.value());
216
217 absl::optional<char> startChar = barcode_->GetStartChar();
218 if (startChar.has_value())
219 pBarCodeWidget->SetStartChar(startChar.value());
220
221 absl::optional<char> endChar = barcode_->GetEndChar();
222 if (endChar.has_value())
223 pBarCodeWidget->SetEndChar(endChar.value());
224
225 absl::optional<int32_t> ecLevel = barcode_->GetECLevel();
226 if (ecLevel.has_value())
227 pBarCodeWidget->SetErrorCorrectionLevel(ecLevel.value());
228
229 absl::optional<int32_t> width = barcode_->GetModuleWidth();
230 if (width.has_value())
231 pBarCodeWidget->SetModuleWidth(width.value());
232
233 absl::optional<int32_t> height = barcode_->GetModuleHeight();
234 if (height.has_value())
235 pBarCodeWidget->SetModuleHeight(height.value());
236
237 absl::optional<bool> printCheck = barcode_->GetPrintChecksum();
238 if (printCheck.has_value())
239 pBarCodeWidget->SetPrintChecksum(printCheck.value());
240
241 absl::optional<XFA_AttributeValue> text_attr = barcode_->GetTextLocation();
242 if (text_attr.has_value()) {
243 absl::optional<BC_TEXT_LOC> textLoc =
244 TextLocFromAttribute(text_attr.value());
245 if (textLoc.has_value())
246 pBarCodeWidget->SetTextLocation(textLoc.value());
247 }
248
249 // Truncated is currently not a supported flag.
250
251 absl::optional<int8_t> ratio = barcode_->GetWideNarrowRatio();
252 if (ratio.has_value())
253 pBarCodeWidget->SetWideNarrowRatio(ratio.value());
254
255 if (bc_type == BC_TYPE::kCode39 || bc_type == BC_TYPE::kEAN8 ||
256 bc_type == BC_TYPE::kEAN13 || bc_type == BC_TYPE::kUPCA) {
257 pBarCodeWidget->SetPrintChecksum(true);
258 }
259}
260
261bool CXFA_FFBarcode::AcceptsFocusOnButtonDown(
262 Mask<XFA_FWL_KeyFlag> dwFlags,
263 const CFX_PointF& point,
264 CFWL_MessageMouse::MouseCommand command) {
265 auto* pBarCodeWidget = static_cast<CFWL_Barcode*>(GetNormalWidget());
266 if (!pBarCodeWidget || pBarCodeWidget->IsProtectedType())
267 return false;
268 if (command == CFWL_MessageMouse::MouseCommand::kLeftButtonDown &&
269 !m_pNode->IsOpenAccess()) {
270 return false;
271 }
272 return CXFA_FFTextEdit::AcceptsFocusOnButtonDown(dwFlags, point, command);
273}
BC_TEXT_LOC
Definition BC_Library.h:12
BC_TYPE
Definition BC_Library.h:25
BC_CHAR_ENCODING
Definition BC_Library.h:20
void RegisterEventTarget(CFWL_Widget *pListener, CFWL_Widget *pEventSource)
void SetDelegate(IFWL_WidgetDelegate *delegate)
void SetAdapterIface(AdapterIface *pItem)
CFWL_App * GetFWLApp() const
CFX_Matrix(float a1, float b1, float c1, float d1, float e1, float f1)
void Concat(const CFX_Matrix &right)
void RenderWidget(CFGAS_GEGraphics *pGS, const CFX_Matrix &matrix, HighlightOption highlight) override
bool AcceptsFocusOnButtonDown(Mask< XFA_FWL_KeyFlag > dwFlags, const CFX_PointF &point, CFWL_MessageMouse::MouseCommand command) override
static BC_TYPE GetBarcodeTypeByName(const WideString &wsName)
void UpdateWidgetProperty() override
bool LoadWidget() override
~CXFA_FFBarcode() override
bool LoadWidget() override
void SetNormalWidget(CFWL_Widget *widget)
void RenderCaption(CFGAS_GEGraphics *pGS, const CFX_Matrix &pMatrix)
bool AcceptsFocusOnButtonDown(Mask< XFA_FWL_KeyFlag > dwFlags, const CFX_PointF &point, CFWL_MessageMouse::MouseCommand command) override
CXFA_FFTextEdit(CXFA_Node *pNode)
void UpdateWidgetProperty() override
CFX_Matrix GetRotateMatrix() override
virtual void RenderWidget(CFGAS_GEGraphics *pGS, const CFX_Matrix &matrix, HighlightOption highlight)
bool HasVisibleStatus() const
bool IsEmpty() const
Definition widestring.h:118
int CompareNoCase(const wchar_t *str) const
bool EqualsASCII(ByteStringView that) const
Definition widestring.h:216
XFA_FWL_KeyFlag
XFA_AttributeValue
Definition fxfa_basic.h:60
Definition heap.h:12