xref: /aosp_15_r20/external/pdfium/xfa/fwl/cfwl_barcode.cpp (revision 3ac0a46f773bac49fa9476ec2b1cf3f8da5ec3a4)
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/fwl/cfwl_barcode.h"
8 
9 #include "fxbarcode/cfx_barcode.h"
10 #include "xfa/fgas/font/cfgas_gefont.h"
11 #include "xfa/fwl/cfwl_notedriver.h"
12 #include "xfa/fwl/cfwl_themepart.h"
13 #include "xfa/fwl/ifwl_themeprovider.h"
14 #include "xfa/fwl/theme/cfwl_utils.h"
15 
CFWL_Barcode(CFWL_App * app)16 CFWL_Barcode::CFWL_Barcode(CFWL_App* app)
17     : CFWL_Edit(app, Properties(), nullptr) {}
18 
19 CFWL_Barcode::~CFWL_Barcode() = default;
20 
GetClassID() const21 FWL_Type CFWL_Barcode::GetClassID() const {
22   return FWL_Type::Barcode;
23 }
24 
Update()25 void CFWL_Barcode::Update() {
26   if (IsLocked())
27     return;
28 
29   CFWL_Edit::Update();
30   GenerateBarcodeImageCache();
31 }
32 
DrawWidget(CFGAS_GEGraphics * pGraphics,const CFX_Matrix & matrix)33 void CFWL_Barcode::DrawWidget(CFGAS_GEGraphics* pGraphics,
34                               const CFX_Matrix& matrix) {
35   if (!pGraphics)
36     return;
37 
38   if ((m_Properties.m_dwStates & FWL_STATE_WGT_Focused) == 0) {
39     GenerateBarcodeImageCache();
40     if (!m_pBarcodeEngine || m_eStatus != Status::kEncodeSuccess)
41       return;
42 
43     CFX_Matrix mt;
44     mt.e = GetRTClient().left;
45     mt.f = GetRTClient().top;
46     mt.Concat(matrix);
47 
48     // TODO(tsepez): Curious as to why |mt| is unused?
49     m_pBarcodeEngine->RenderDevice(pGraphics->GetRenderDevice(), matrix);
50     return;
51   }
52   CFWL_Edit::DrawWidget(pGraphics, matrix);
53 }
54 
SetType(BC_TYPE type)55 void CFWL_Barcode::SetType(BC_TYPE type) {
56   if (m_type == type)
57     return;
58 
59   m_pBarcodeEngine.reset();
60   m_type = type;
61   m_eStatus = Status::kNeedUpdate;
62 }
63 
SetText(const WideString & wsText)64 void CFWL_Barcode::SetText(const WideString& wsText) {
65   m_pBarcodeEngine.reset();
66   m_eStatus = Status::kNeedUpdate;
67   CFWL_Edit::SetText(wsText);
68 }
69 
SetTextSkipNotify(const WideString & wsText)70 void CFWL_Barcode::SetTextSkipNotify(const WideString& wsText) {
71   m_pBarcodeEngine.reset();
72   m_eStatus = Status::kNeedUpdate;
73   CFWL_Edit::SetTextSkipNotify(wsText);
74 }
75 
IsProtectedType() const76 bool CFWL_Barcode::IsProtectedType() const {
77   if (!m_pBarcodeEngine)
78     return true;
79 
80   BC_TYPE tEngineType = m_pBarcodeEngine->GetType();
81   return tEngineType == BC_TYPE::kQRCode || tEngineType == BC_TYPE::kPDF417 ||
82          tEngineType == BC_TYPE::kDataMatrix;
83 }
84 
OnProcessEvent(CFWL_Event * pEvent)85 void CFWL_Barcode::OnProcessEvent(CFWL_Event* pEvent) {
86   if (pEvent->GetType() == CFWL_Event::Type::TextWillChange) {
87     m_pBarcodeEngine.reset();
88     m_eStatus = Status::kNeedUpdate;
89   }
90   CFWL_Edit::OnProcessEvent(pEvent);
91 }
92 
SetCharEncoding(BC_CHAR_ENCODING encoding)93 void CFWL_Barcode::SetCharEncoding(BC_CHAR_ENCODING encoding) {
94   m_eCharEncoding = encoding;
95 }
96 
SetModuleHeight(int32_t height)97 void CFWL_Barcode::SetModuleHeight(int32_t height) {
98   m_nModuleHeight = height;
99 }
100 
SetModuleWidth(int32_t width)101 void CFWL_Barcode::SetModuleWidth(int32_t width) {
102   m_nModuleWidth = width;
103 }
104 
SetDataLength(int32_t dataLength)105 void CFWL_Barcode::SetDataLength(int32_t dataLength) {
106   m_nDataLength = dataLength;
107   SetLimit(dataLength);
108 }
109 
SetCalChecksum(bool calChecksum)110 void CFWL_Barcode::SetCalChecksum(bool calChecksum) {
111   m_bCalChecksum = calChecksum;
112 }
113 
SetPrintChecksum(bool printChecksum)114 void CFWL_Barcode::SetPrintChecksum(bool printChecksum) {
115   m_bPrintChecksum = printChecksum;
116 }
117 
SetTextLocation(BC_TEXT_LOC location)118 void CFWL_Barcode::SetTextLocation(BC_TEXT_LOC location) {
119   m_eTextLocation = location;
120 }
121 
SetWideNarrowRatio(int8_t ratio)122 void CFWL_Barcode::SetWideNarrowRatio(int8_t ratio) {
123   m_nWideNarrowRatio = ratio;
124 }
125 
SetStartChar(char startChar)126 void CFWL_Barcode::SetStartChar(char startChar) {
127   m_cStartChar = startChar;
128 }
129 
SetEndChar(char endChar)130 void CFWL_Barcode::SetEndChar(char endChar) {
131   m_cEndChar = endChar;
132 }
133 
SetErrorCorrectionLevel(int32_t ecLevel)134 void CFWL_Barcode::SetErrorCorrectionLevel(int32_t ecLevel) {
135   m_nECLevel = ecLevel;
136 }
137 
GenerateBarcodeImageCache()138 void CFWL_Barcode::GenerateBarcodeImageCache() {
139   if (m_eStatus != Status::kNeedUpdate)
140     return;
141 
142   m_eStatus = Status::kNormal;
143   CreateBarcodeEngine();
144   if (!m_pBarcodeEngine)
145     return;
146 
147   IFWL_ThemeProvider* pTheme = GetThemeProvider();
148   CFWL_ThemePart part(CFWL_ThemePart::Part::kNone, this);
149   if (RetainPtr<CFGAS_GEFont> pFont = pTheme->GetFont(part)) {
150     if (CFX_Font* pCXFont = pFont->GetDevFont())
151       m_pBarcodeEngine->SetFont(pCXFont);
152   }
153   m_pBarcodeEngine->SetFontSize(pTheme->GetFontSize(part));
154   m_pBarcodeEngine->SetFontColor(pTheme->GetTextColor(part));
155   m_pBarcodeEngine->SetHeight(int32_t(GetRTClient().height));
156   m_pBarcodeEngine->SetWidth(int32_t(GetRTClient().width));
157   if (m_eCharEncoding.has_value())
158     m_pBarcodeEngine->SetCharEncoding(m_eCharEncoding.value());
159   if (m_nModuleHeight.has_value())
160     m_pBarcodeEngine->SetModuleHeight(m_nModuleHeight.value());
161   if (m_nModuleWidth.has_value())
162     m_pBarcodeEngine->SetModuleWidth(m_nModuleWidth.value());
163   if (m_nDataLength.has_value())
164     m_pBarcodeEngine->SetDataLength(m_nDataLength.value());
165   if (m_bCalChecksum.has_value())
166     m_pBarcodeEngine->SetCalChecksum(m_bCalChecksum.value());
167   if (m_bPrintChecksum.has_value())
168     m_pBarcodeEngine->SetPrintChecksum(m_bPrintChecksum.value());
169   if (m_eTextLocation.has_value())
170     m_pBarcodeEngine->SetTextLocation(m_eTextLocation.value());
171   if (m_nWideNarrowRatio.has_value())
172     m_pBarcodeEngine->SetWideNarrowRatio(m_nWideNarrowRatio.value());
173   if (m_cStartChar.has_value())
174     m_pBarcodeEngine->SetStartChar(m_cStartChar.value());
175   if (m_cEndChar.has_value())
176     m_pBarcodeEngine->SetEndChar(m_cEndChar.value());
177   if (m_nECLevel.has_value())
178     m_pBarcodeEngine->SetErrorCorrectionLevel(m_nECLevel.value());
179 
180   m_eStatus = m_pBarcodeEngine->Encode(GetText().AsStringView())
181                   ? Status::kEncodeSuccess
182                   : Status::kNormal;
183 }
184 
CreateBarcodeEngine()185 void CFWL_Barcode::CreateBarcodeEngine() {
186   if (m_pBarcodeEngine || m_type == BC_TYPE::kUnknown)
187     return;
188 
189   m_pBarcodeEngine = CFX_Barcode::Create(m_type);
190 }
191