blob: 87ecee13b5f49042846ce90c8979f1c5ffb2c71c [file] [log] [blame]
Tom Sepez99ffdb02016-01-26 14:51:21 -08001// 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
dsinclair447b1f32016-12-08 10:06:32 -08007#include "xfa/fwl/cfwl_barcode.h"
Dan Sinclaire73c5ce2016-02-25 13:38:37 -05008
dsinclair7c47e1d2016-11-23 10:49:33 -08009#include <utility>
Tom Sepez99ffdb02016-01-26 14:51:21 -080010
dsinclair6fe87952016-11-01 18:48:19 -070011#include "third_party/base/ptr_util.h"
dsinclair2c489cc2016-11-23 16:17:20 -080012#include "xfa/fgas/font/cfgas_gefont.h"
dsinclair447b1f32016-12-08 10:06:32 -080013#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"
dsinclair42cb6452016-10-31 12:50:04 -070017
dsinclair1a7534a2016-11-22 15:56:11 -080018CFWL_Barcode::CFWL_Barcode(const CFWL_App* app)
dsinclair2c489cc2016-11-23 16:17:20 -080019 : CFWL_Edit(app, pdfium::MakeUnique<CFWL_WidgetProperties>(), nullptr),
20 m_dwStatus(0),
21 m_type(BC_UNKNOWN),
22 m_dwAttributeMask(FWL_BCDATTRIBUTE_NONE) {}
dsinclair20855382016-10-31 07:29:34 -070023
24CFWL_Barcode::~CFWL_Barcode() {}
25
dsinclair2c489cc2016-11-23 16:17:20 -080026FWL_Type CFWL_Barcode::GetClassID() const {
27 return FWL_Type::Barcode;
28}
dsinclair20855382016-10-31 07:29:34 -070029
dsinclair2c489cc2016-11-23 16:17:20 -080030void CFWL_Barcode::Update() {
31 if (IsLocked())
32 return;
dsinclair6fe87952016-11-01 18:48:19 -070033
dsinclair2c489cc2016-11-23 16:17:20 -080034 CFWL_Edit::Update();
35 GenerateBarcodeImageCache();
36}
37
38void 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
63void 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
72void CFWL_Barcode::SetText(const CFX_WideString& wsText) {
73 m_pBarcodeEngine.reset();
74 m_dwStatus = XFA_BCS_NeedUpdate;
75 CFWL_Edit::SetText(wsText);
76}
77
78bool 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
90void CFWL_Barcode::OnProcessEvent(CFWL_Event* pEvent) {
dsinclair4614b452016-12-07 17:01:58 -080091 if (pEvent->GetType() == CFWL_Event::Type::TextChanged) {
dsinclair2c489cc2016-11-23 16:17:20 -080092 m_pBarcodeEngine.reset();
93 m_dwStatus = XFA_BCS_NeedUpdate;
94 }
95 CFWL_Edit::OnProcessEvent(pEvent);
dsinclair20855382016-10-31 07:29:34 -070096}
97
dsinclair42cb6452016-10-31 12:50:04 -070098void CFWL_Barcode::SetCharEncoding(BC_CHAR_ENCODING encoding) {
dsinclaireb3f68c2016-11-07 10:28:47 -080099 m_dwAttributeMask |= FWL_BCDATTRIBUTE_CHARENCODING;
100 m_eCharEncoding = encoding;
weili5d8e5aa2016-08-08 17:30:37 -0700101}
dsinclaireb3f68c2016-11-07 10:28:47 -0800102
dsinclair42cb6452016-10-31 12:50:04 -0700103void CFWL_Barcode::SetModuleHeight(int32_t height) {
dsinclaireb3f68c2016-11-07 10:28:47 -0800104 m_dwAttributeMask |= FWL_BCDATTRIBUTE_MODULEHEIGHT;
105 m_nModuleHeight = height;
dsinclair42cb6452016-10-31 12:50:04 -0700106}
dsinclaireb3f68c2016-11-07 10:28:47 -0800107
dsinclair42cb6452016-10-31 12:50:04 -0700108void CFWL_Barcode::SetModuleWidth(int32_t width) {
dsinclaireb3f68c2016-11-07 10:28:47 -0800109 m_dwAttributeMask |= FWL_BCDATTRIBUTE_MODULEWIDTH;
110 m_nModuleWidth = width;
dsinclair42cb6452016-10-31 12:50:04 -0700111}
dsinclaireb3f68c2016-11-07 10:28:47 -0800112
dsinclair42cb6452016-10-31 12:50:04 -0700113void CFWL_Barcode::SetDataLength(int32_t dataLength) {
dsinclaireb3f68c2016-11-07 10:28:47 -0800114 m_dwAttributeMask |= FWL_BCDATTRIBUTE_DATALENGTH;
115 m_nDataLength = dataLength;
dsinclair2c489cc2016-11-23 16:17:20 -0800116 SetLimit(dataLength);
dsinclair42cb6452016-10-31 12:50:04 -0700117}
dsinclaireb3f68c2016-11-07 10:28:47 -0800118
tsepezd19e9122016-11-02 15:43:18 -0700119void CFWL_Barcode::SetCalChecksum(bool calChecksum) {
dsinclaireb3f68c2016-11-07 10:28:47 -0800120 m_dwAttributeMask |= FWL_BCDATTRIBUTE_CALCHECKSUM;
121 m_bCalChecksum = calChecksum;
dsinclair42cb6452016-10-31 12:50:04 -0700122}
dsinclaireb3f68c2016-11-07 10:28:47 -0800123
tsepezd19e9122016-11-02 15:43:18 -0700124void CFWL_Barcode::SetPrintChecksum(bool printChecksum) {
dsinclaireb3f68c2016-11-07 10:28:47 -0800125 m_dwAttributeMask |= FWL_BCDATTRIBUTE_PRINTCHECKSUM;
126 m_bPrintChecksum = printChecksum;
dsinclair42cb6452016-10-31 12:50:04 -0700127}
dsinclaireb3f68c2016-11-07 10:28:47 -0800128
dsinclair42cb6452016-10-31 12:50:04 -0700129void CFWL_Barcode::SetTextLocation(BC_TEXT_LOC location) {
dsinclaireb3f68c2016-11-07 10:28:47 -0800130 m_dwAttributeMask |= FWL_BCDATTRIBUTE_TEXTLOCATION;
131 m_eTextLocation = location;
dsinclair42cb6452016-10-31 12:50:04 -0700132}
dsinclaireb3f68c2016-11-07 10:28:47 -0800133
dsinclair42cb6452016-10-31 12:50:04 -0700134void CFWL_Barcode::SetWideNarrowRatio(int32_t ratio) {
dsinclaireb3f68c2016-11-07 10:28:47 -0800135 m_dwAttributeMask |= FWL_BCDATTRIBUTE_WIDENARROWRATIO;
136 m_nWideNarrowRatio = ratio;
dsinclair42cb6452016-10-31 12:50:04 -0700137}
dsinclaireb3f68c2016-11-07 10:28:47 -0800138
Dan Sinclair812e96c2017-03-13 16:43:37 -0400139void CFWL_Barcode::SetStartChar(char startChar) {
dsinclaireb3f68c2016-11-07 10:28:47 -0800140 m_dwAttributeMask |= FWL_BCDATTRIBUTE_STARTCHAR;
141 m_cStartChar = startChar;
dsinclair42cb6452016-10-31 12:50:04 -0700142}
dsinclaireb3f68c2016-11-07 10:28:47 -0800143
Dan Sinclair812e96c2017-03-13 16:43:37 -0400144void CFWL_Barcode::SetEndChar(char endChar) {
dsinclaireb3f68c2016-11-07 10:28:47 -0800145 m_dwAttributeMask |= FWL_BCDATTRIBUTE_ENDCHAR;
146 m_cEndChar = endChar;
dsinclair42cb6452016-10-31 12:50:04 -0700147}
dsinclaireb3f68c2016-11-07 10:28:47 -0800148
dsinclair42cb6452016-10-31 12:50:04 -0700149void CFWL_Barcode::SetErrorCorrectionLevel(int32_t ecLevel) {
dsinclaireb3f68c2016-11-07 10:28:47 -0800150 m_dwAttributeMask |= FWL_BCDATTRIBUTE_ECLEVEL;
151 m_nECLevel = ecLevel;
dsinclair42cb6452016-10-31 12:50:04 -0700152}
dsinclaireb3f68c2016-11-07 10:28:47 -0800153
tsepezd19e9122016-11-02 15:43:18 -0700154void CFWL_Barcode::SetTruncated(bool truncated) {
dsinclaireb3f68c2016-11-07 10:28:47 -0800155 m_dwAttributeMask |= FWL_BCDATTRIBUTE_TRUNCATED;
156 m_bTruncated = truncated;
dsinclair42cb6452016-10-31 12:50:04 -0700157}
dsinclaireb3f68c2016-11-07 10:28:47 -0800158
dsinclair2c489cc2016-11-23 16:17:20 -0800159void 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
dsinclair2c489cc2016-11-23 16:17:20 -0800168 IFWL_ThemeProvider* pTheme = GetAvailableTheme();
Dan Sinclairc635c932017-01-03 15:46:55 -0500169 if (pTheme) {
170 CFWL_ThemePart part;
171 part.m_pWidget = this;
tsepeze6477992017-01-05 12:57:00 -0800172 if (CFX_RetainPtr<CFGAS_GEFont> pFont = pTheme->GetFont(&part)) {
Dan Sinclairc635c932017-01-03 15:46:55 -0500173 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 }
dsinclair2c489cc2016-11-23 16:17:20 -0800181
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);
dsinclair2c489cc2016-11-23 16:17:20 -0800204 if (m_dwAttributeMask & FWL_BCDATTRIBUTE_ECLEVEL)
205 m_pBarcodeEngine->SetErrorCorrectionLevel(m_nECLevel);
206 if (m_dwAttributeMask & FWL_BCDATTRIBUTE_TRUNCATED)
207 m_pBarcodeEngine->SetTruncated(m_bTruncated);
208
209 int32_t errorCode = 0;
Dan Sinclairc635c932017-01-03 15:46:55 -0500210 m_dwStatus = m_pBarcodeEngine->Encode(GetText().AsStringC(), true, errorCode)
dsinclair2c489cc2016-11-23 16:17:20 -0800211 ? XFA_BCS_EncodeSuccess
212 : 0;
Tom Sepez99ffdb02016-01-26 14:51:21 -0800213}
dsinclair7f432a12016-03-29 12:38:01 -0700214
dsinclair2c489cc2016-11-23 16:17:20 -0800215void CFWL_Barcode::CreateBarcodeEngine() {
216 if (m_pBarcodeEngine || m_type == BC_UNKNOWN)
217 return;
weili4ce94e12016-06-18 06:21:57 -0700218
Dan Sinclair0bb13332017-03-30 16:12:02 -0400219 auto pBarcode = pdfium::MakeUnique<CFX_Barcode>();
dsinclair2c489cc2016-11-23 16:17:20 -0800220 if (pBarcode->Create(m_type))
221 m_pBarcodeEngine = std::move(pBarcode);
weili4ce94e12016-06-18 06:21:57 -0700222}