• 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/fwl/cfwl_barcode.h"
8 
9 #include <utility>
10 
11 #include "third_party/base/ptr_util.h"
12 #include "xfa/fgas/font/cfgas_gefont.h"
13 #include "xfa/fwl/cfwl_notedriver.h"
14 #include "xfa/fwl/cfwl_themepart.h"
15 #include "xfa/fwl/cfx_barcode.h"
16 #include "xfa/fwl/ifwl_themeprovider.h"
17 
CFWL_Barcode(const CFWL_App * app)18 CFWL_Barcode::CFWL_Barcode(const CFWL_App* app)
19     : CFWL_Edit(app, pdfium::MakeUnique<CFWL_WidgetProperties>(), nullptr),
20       m_dwStatus(0),
21       m_type(BC_UNKNOWN),
22       m_dwAttributeMask(FWL_BCDATTRIBUTE_NONE) {}
23 
~CFWL_Barcode()24 CFWL_Barcode::~CFWL_Barcode() {}
25 
GetClassID() const26 FWL_Type CFWL_Barcode::GetClassID() const {
27   return FWL_Type::Barcode;
28 }
29 
Update()30 void CFWL_Barcode::Update() {
31   if (IsLocked())
32     return;
33 
34   CFWL_Edit::Update();
35   GenerateBarcodeImageCache();
36 }
37 
DrawWidget(CFX_Graphics * pGraphics,const CFX_Matrix * pMatrix)38 void CFWL_Barcode::DrawWidget(CFX_Graphics* pGraphics,
39                               const CFX_Matrix* pMatrix) {
40   if (!pGraphics)
41     return;
42   if (!m_pProperties->m_pThemeProvider)
43     return;
44   if ((m_pProperties->m_dwStates & FWL_WGTSTATE_Focused) == 0) {
45     GenerateBarcodeImageCache();
46     if (!m_pBarcodeEngine || (m_dwStatus & XFA_BCS_EncodeSuccess) == 0)
47       return;
48 
49     CFX_Matrix mt;
50     mt.e = GetRTClient().left;
51     mt.f = GetRTClient().top;
52     if (pMatrix)
53       mt.Concat(*pMatrix);
54 
55     int32_t errorCode = 0;
56     m_pBarcodeEngine->RenderDevice(pGraphics->GetRenderDevice(), pMatrix,
57                                    errorCode);
58     return;
59   }
60   CFWL_Edit::DrawWidget(pGraphics, pMatrix);
61 }
62 
SetType(BC_TYPE type)63 void CFWL_Barcode::SetType(BC_TYPE type) {
64   if (m_type == type)
65     return;
66 
67   m_pBarcodeEngine.reset();
68   m_type = type;
69   m_dwStatus = XFA_BCS_NeedUpdate;
70 }
71 
SetText(const CFX_WideString & wsText)72 void CFWL_Barcode::SetText(const CFX_WideString& wsText) {
73   m_pBarcodeEngine.reset();
74   m_dwStatus = XFA_BCS_NeedUpdate;
75   CFWL_Edit::SetText(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   if (tEngineType == BC_QR_CODE || tEngineType == BC_PDF417 ||
84       tEngineType == BC_DATAMATRIX) {
85     return true;
86   }
87   return false;
88 }
89 
OnProcessEvent(CFWL_Event * pEvent)90 void CFWL_Barcode::OnProcessEvent(CFWL_Event* pEvent) {
91   if (pEvent->GetType() == CFWL_Event::Type::TextChanged) {
92     m_pBarcodeEngine.reset();
93     m_dwStatus = XFA_BCS_NeedUpdate;
94   }
95   CFWL_Edit::OnProcessEvent(pEvent);
96 }
97 
SetCharEncoding(BC_CHAR_ENCODING encoding)98 void CFWL_Barcode::SetCharEncoding(BC_CHAR_ENCODING encoding) {
99   m_dwAttributeMask |= FWL_BCDATTRIBUTE_CHARENCODING;
100   m_eCharEncoding = encoding;
101 }
102 
SetModuleHeight(int32_t height)103 void CFWL_Barcode::SetModuleHeight(int32_t height) {
104   m_dwAttributeMask |= FWL_BCDATTRIBUTE_MODULEHEIGHT;
105   m_nModuleHeight = height;
106 }
107 
SetModuleWidth(int32_t width)108 void CFWL_Barcode::SetModuleWidth(int32_t width) {
109   m_dwAttributeMask |= FWL_BCDATTRIBUTE_MODULEWIDTH;
110   m_nModuleWidth = width;
111 }
112 
SetDataLength(int32_t dataLength)113 void CFWL_Barcode::SetDataLength(int32_t dataLength) {
114   m_dwAttributeMask |= FWL_BCDATTRIBUTE_DATALENGTH;
115   m_nDataLength = dataLength;
116   SetLimit(dataLength);
117 }
118 
SetCalChecksum(bool calChecksum)119 void CFWL_Barcode::SetCalChecksum(bool calChecksum) {
120   m_dwAttributeMask |= FWL_BCDATTRIBUTE_CALCHECKSUM;
121   m_bCalChecksum = calChecksum;
122 }
123 
SetPrintChecksum(bool printChecksum)124 void CFWL_Barcode::SetPrintChecksum(bool printChecksum) {
125   m_dwAttributeMask |= FWL_BCDATTRIBUTE_PRINTCHECKSUM;
126   m_bPrintChecksum = printChecksum;
127 }
128 
SetTextLocation(BC_TEXT_LOC location)129 void CFWL_Barcode::SetTextLocation(BC_TEXT_LOC location) {
130   m_dwAttributeMask |= FWL_BCDATTRIBUTE_TEXTLOCATION;
131   m_eTextLocation = location;
132 }
133 
SetWideNarrowRatio(int32_t ratio)134 void CFWL_Barcode::SetWideNarrowRatio(int32_t ratio) {
135   m_dwAttributeMask |= FWL_BCDATTRIBUTE_WIDENARROWRATIO;
136   m_nWideNarrowRatio = ratio;
137 }
138 
SetStartChar(FX_CHAR startChar)139 void CFWL_Barcode::SetStartChar(FX_CHAR startChar) {
140   m_dwAttributeMask |= FWL_BCDATTRIBUTE_STARTCHAR;
141   m_cStartChar = startChar;
142 }
143 
SetEndChar(FX_CHAR endChar)144 void CFWL_Barcode::SetEndChar(FX_CHAR endChar) {
145   m_dwAttributeMask |= FWL_BCDATTRIBUTE_ENDCHAR;
146   m_cEndChar = endChar;
147 }
148 
SetErrorCorrectionLevel(int32_t ecLevel)149 void CFWL_Barcode::SetErrorCorrectionLevel(int32_t ecLevel) {
150   m_dwAttributeMask |= FWL_BCDATTRIBUTE_ECLEVEL;
151   m_nECLevel = ecLevel;
152 }
153 
SetTruncated(bool truncated)154 void CFWL_Barcode::SetTruncated(bool truncated) {
155   m_dwAttributeMask |= FWL_BCDATTRIBUTE_TRUNCATED;
156   m_bTruncated = truncated;
157 }
158 
GenerateBarcodeImageCache()159 void CFWL_Barcode::GenerateBarcodeImageCache() {
160   if ((m_dwStatus & XFA_BCS_NeedUpdate) == 0)
161     return;
162 
163   m_dwStatus = 0;
164   CreateBarcodeEngine();
165   if (!m_pBarcodeEngine)
166     return;
167 
168   IFWL_ThemeProvider* pTheme = GetAvailableTheme();
169   if (pTheme) {
170     CFWL_ThemePart part;
171     part.m_pWidget = this;
172     if (CFX_RetainPtr<CFGAS_GEFont> pFont = pTheme->GetFont(&part)) {
173       if (CFX_Font* pCXFont = pFont->GetDevFont())
174         m_pBarcodeEngine->SetFont(pCXFont);
175     }
176     m_pBarcodeEngine->SetFontSize(pTheme->GetFontSize(&part));
177     m_pBarcodeEngine->SetFontColor(pTheme->GetTextColor(&part));
178   } else {
179     m_pBarcodeEngine->SetFontSize(FWLTHEME_CAPACITY_FontSize);
180   }
181 
182   m_pBarcodeEngine->SetHeight(int32_t(GetRTClient().height));
183   m_pBarcodeEngine->SetWidth(int32_t(GetRTClient().width));
184   if (m_dwAttributeMask & FWL_BCDATTRIBUTE_CHARENCODING)
185     m_pBarcodeEngine->SetCharEncoding(m_eCharEncoding);
186   if (m_dwAttributeMask & FWL_BCDATTRIBUTE_MODULEHEIGHT)
187     m_pBarcodeEngine->SetModuleHeight(m_nModuleHeight);
188   if (m_dwAttributeMask & FWL_BCDATTRIBUTE_MODULEWIDTH)
189     m_pBarcodeEngine->SetModuleWidth(m_nModuleWidth);
190   if (m_dwAttributeMask & FWL_BCDATTRIBUTE_DATALENGTH)
191     m_pBarcodeEngine->SetDataLength(m_nDataLength);
192   if (m_dwAttributeMask & FWL_BCDATTRIBUTE_CALCHECKSUM)
193     m_pBarcodeEngine->SetCalChecksum(m_bCalChecksum);
194   if (m_dwAttributeMask & FWL_BCDATTRIBUTE_PRINTCHECKSUM)
195     m_pBarcodeEngine->SetPrintChecksum(m_bPrintChecksum);
196   if (m_dwAttributeMask & FWL_BCDATTRIBUTE_TEXTLOCATION)
197     m_pBarcodeEngine->SetTextLocation(m_eTextLocation);
198   if (m_dwAttributeMask & FWL_BCDATTRIBUTE_WIDENARROWRATIO)
199     m_pBarcodeEngine->SetWideNarrowRatio(m_nWideNarrowRatio);
200   if (m_dwAttributeMask & FWL_BCDATTRIBUTE_STARTCHAR)
201     m_pBarcodeEngine->SetStartChar(m_cStartChar);
202   if (m_dwAttributeMask & FWL_BCDATTRIBUTE_ENDCHAR)
203     m_pBarcodeEngine->SetEndChar(m_cEndChar);
204   if (m_dwAttributeMask & FWL_BCDATTRIBUTE_VERSION)
205     m_pBarcodeEngine->SetVersion(0);
206   if (m_dwAttributeMask & FWL_BCDATTRIBUTE_ECLEVEL)
207     m_pBarcodeEngine->SetErrorCorrectionLevel(m_nECLevel);
208   if (m_dwAttributeMask & FWL_BCDATTRIBUTE_TRUNCATED)
209     m_pBarcodeEngine->SetTruncated(m_bTruncated);
210 
211   int32_t errorCode = 0;
212   m_dwStatus = m_pBarcodeEngine->Encode(GetText().AsStringC(), true, errorCode)
213                    ? XFA_BCS_EncodeSuccess
214                    : 0;
215 }
216 
CreateBarcodeEngine()217 void CFWL_Barcode::CreateBarcodeEngine() {
218   if (m_pBarcodeEngine || m_type == BC_UNKNOWN)
219     return;
220 
221   std::unique_ptr<CFX_Barcode> pBarcode(new CFX_Barcode);
222   if (pBarcode->Create(m_type))
223     m_pBarcodeEngine = std::move(pBarcode);
224 }
225