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/check.h"
12#include "core/fxcrt/fx_extension.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
96std::optional<BC_TEXT_LOC> TextLocFromAttribute(XFA_AttributeValue value) {
97 switch (value) {
98 case XFA_AttributeValue::None:
99 return BC_TEXT_LOC::kNone;
100 case XFA_AttributeValue::Above:
101 return BC_TEXT_LOC::kAbove;
102 case XFA_AttributeValue::Below:
103 return BC_TEXT_LOC::kBelow;
104 case XFA_AttributeValue::AboveEmbedded:
106 case XFA_AttributeValue::BelowEmbedded:
108 default:
109 return std::nullopt;
110 }
111}
112
113} // namespace.
114
115// static
116BC_TYPE CXFA_FFBarcode::GetBarcodeTypeByName(const WideString& wsName) {
117 if (wsName.IsEmpty())
118 return BC_TYPE::kUnknown;
119
120 auto* it = std::lower_bound(
121 std::begin(kBarCodeData), std::end(kBarCodeData),
122 FX_HashCode_GetLoweredW(wsName.AsStringView()),
123 [](const BarCodeInfo& arg, uint32_t hash) { return arg.uHash < hash; });
124
125 if (it != std::end(kBarCodeData) && wsName.EqualsASCII(it->pName))
126 return it->eBCType;
127
128 return BC_TYPE::kUnknown;
129}
130
131CXFA_FFBarcode::CXFA_FFBarcode(CXFA_Node* pNode, CXFA_Barcode* barcode)
132 : CXFA_FFTextEdit(pNode), barcode_(barcode) {}
133
134CXFA_FFBarcode::~CXFA_FFBarcode() = default;
135
136void CXFA_FFBarcode::Trace(cppgc::Visitor* visitor) const {
137 CXFA_FFTextEdit::Trace(visitor);
138 visitor->Trace(barcode_);
139}
140
141bool CXFA_FFBarcode::LoadWidget() {
142 DCHECK(!IsLoaded());
143
144 CFWL_Barcode* pFWLBarcode = cppgc::MakeGarbageCollected<CFWL_Barcode>(
145 GetFWLApp()->GetHeap()->GetAllocationHandle(), GetFWLApp());
146 SetNormalWidget(pFWLBarcode);
147 pFWLBarcode->SetAdapterIface(this);
148
149 CFWL_NoteDriver* pNoteDriver = pFWLBarcode->GetFWLApp()->GetNoteDriver();
150 pNoteDriver->RegisterEventTarget(pFWLBarcode, pFWLBarcode);
151 m_pOldDelegate = pFWLBarcode->GetDelegate();
152 pFWLBarcode->SetDelegate(this);
153
154 {
155 CFWL_Widget::ScopedUpdateLock update_lock(pFWLBarcode);
156 pFWLBarcode->SetText(m_pNode->GetValue(XFA_ValuePicture::kDisplay));
158 }
159
161}
162
163void CXFA_FFBarcode::RenderWidget(CFGAS_GEGraphics* pGS,
164 const CFX_Matrix& matrix,
165 HighlightOption highlight) {
166 if (!HasVisibleStatus())
167 return;
168
169 CFX_Matrix mtRotate = GetRotateMatrix();
170 mtRotate.Concat(matrix);
171
172 CXFA_FFWidget::RenderWidget(pGS, mtRotate, highlight);
173 DrawBorder(pGS, m_pNode->GetUIBorder(), m_UIRect, mtRotate);
174 RenderCaption(pGS, mtRotate);
175 CFX_RectF rtWidget = GetNormalWidget()->GetWidgetRect();
176
177 CFX_Matrix mt(1, 0, 0, 1, rtWidget.left, rtWidget.top);
178 mt.Concat(mtRotate);
179 GetNormalWidget()->DrawWidget(pGS, mt);
180}
181
182void CXFA_FFBarcode::UpdateWidgetProperty() {
184
185 BC_TYPE bc_type = GetBarcodeTypeByName(barcode_->GetBarcodeType());
186 if (bc_type == BC_TYPE::kUnknown)
187 return;
188
189 auto* pBarCodeWidget = static_cast<CFWL_Barcode*>(GetNormalWidget());
190 pBarCodeWidget->SetType(bc_type);
191
192 std::optional<bool> calcChecksum = barcode_->GetChecksum();
193 if (calcChecksum.has_value())
194 pBarCodeWidget->SetCalChecksum(calcChecksum.value());
195
196 std::optional<int32_t> dataLen = barcode_->GetDataLength();
197 if (dataLen.has_value())
198 pBarCodeWidget->SetDataLength(dataLen.value());
199
200 std::optional<char> startChar = barcode_->GetStartChar();
201 if (startChar.has_value())
202 pBarCodeWidget->SetStartChar(startChar.value());
203
204 std::optional<char> endChar = barcode_->GetEndChar();
205 if (endChar.has_value())
206 pBarCodeWidget->SetEndChar(endChar.value());
207
208 std::optional<int32_t> ecLevel = barcode_->GetECLevel();
209 if (ecLevel.has_value())
210 pBarCodeWidget->SetErrorCorrectionLevel(ecLevel.value());
211
212 std::optional<int32_t> width = barcode_->GetModuleWidth();
213 if (width.has_value())
214 pBarCodeWidget->SetModuleWidth(width.value());
215
216 std::optional<int32_t> height = barcode_->GetModuleHeight();
217 if (height.has_value())
218 pBarCodeWidget->SetModuleHeight(height.value());
219
220 std::optional<bool> printCheck = barcode_->GetPrintChecksum();
221 if (printCheck.has_value())
222 pBarCodeWidget->SetPrintChecksum(printCheck.value());
223
224 std::optional<XFA_AttributeValue> text_attr = barcode_->GetTextLocation();
225 if (text_attr.has_value()) {
226 std::optional<BC_TEXT_LOC> textLoc =
227 TextLocFromAttribute(text_attr.value());
228 if (textLoc.has_value())
229 pBarCodeWidget->SetTextLocation(textLoc.value());
230 }
231
232 // Truncated is currently not a supported flag.
233
234 std::optional<int8_t> ratio = barcode_->GetWideNarrowRatio();
235 if (ratio.has_value())
236 pBarCodeWidget->SetWideNarrowRatio(ratio.value());
237
238 if (bc_type == BC_TYPE::kCode39 || bc_type == BC_TYPE::kEAN8 ||
239 bc_type == BC_TYPE::kEAN13 || bc_type == BC_TYPE::kUPCA) {
240 pBarCodeWidget->SetPrintChecksum(true);
241 }
242}
243
244bool CXFA_FFBarcode::AcceptsFocusOnButtonDown(
245 Mask<XFA_FWL_KeyFlag> dwFlags,
246 const CFX_PointF& point,
247 CFWL_MessageMouse::MouseCommand command) {
248 auto* pBarCodeWidget = static_cast<CFWL_Barcode*>(GetNormalWidget());
249 if (!pBarCodeWidget || pBarCodeWidget->IsProtectedType())
250 return false;
251 if (command == CFWL_MessageMouse::MouseCommand::kLeftButtonDown &&
252 !m_pNode->IsOpenAccess()) {
253 return false;
254 }
255 return CXFA_FFTextEdit::AcceptsFocusOnButtonDown(dwFlags, point, command);
256}
BC_TEXT_LOC
Definition BC_Library.h:12
BC_TYPE
Definition BC_Library.h:20
@ kCode128C
Definition BC_Library.h:26
@ kDataMatrix
Definition BC_Library.h:32
@ kCode128B
Definition BC_Library.h:25
#define DCHECK
Definition check.h:33
constexpr 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 EqualsASCII(ByteStringView that) const
Definition widestring.h:111
void RegisterEventTarget(CFWL_Widget *pListener, CFWL_Widget *pEventSource)
void SetDelegate(IFWL_WidgetDelegate *delegate)
void SetAdapterIface(AdapterIface *pItem)
CFWL_App * GetFWLApp() const
CFX_PTemplate< float > CFX_PointF
XFA_AttributeValue
Definition fxfa_basic.h:60
Definition heap.h:12
fxcrt::WideString WideString
Definition widestring.h:207