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