blob: 592bf2a702a3eadcda57f7dff6383a81e29c6f54 [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"
Henrique Nakashimacb327122017-09-13 13:00:58 -040017#include "xfa/fwl/theme/cfwl_utils.h"
dsinclair42cb6452016-10-31 12:50:04 -070018
dsinclair1a7534a2016-11-22 15:56:11 -080019CFWL_Barcode::CFWL_Barcode(const CFWL_App* app)
Lei Zhang41f22be2019-01-25 23:18:50 +000020 : CFWL_Edit(app, pdfium::MakeUnique<CFWL_WidgetProperties>(), nullptr) {}
dsinclair20855382016-10-31 07:29:34 -070021
Lei Zhang41f22be2019-01-25 23:18:50 +000022CFWL_Barcode::~CFWL_Barcode() = default;
dsinclair20855382016-10-31 07:29:34 -070023
dsinclair2c489cc2016-11-23 16:17:20 -080024FWL_Type CFWL_Barcode::GetClassID() const {
25 return FWL_Type::Barcode;
26}
dsinclair20855382016-10-31 07:29:34 -070027
dsinclair2c489cc2016-11-23 16:17:20 -080028void CFWL_Barcode::Update() {
29 if (IsLocked())
30 return;
dsinclair6fe87952016-11-01 18:48:19 -070031
dsinclair2c489cc2016-11-23 16:17:20 -080032 CFWL_Edit::Update();
33 GenerateBarcodeImageCache();
34}
35
Dan Sinclair2b918c82017-07-13 14:47:10 -040036void CFWL_Barcode::DrawWidget(CXFA_Graphics* pGraphics,
Lei Zhang4b472142017-08-17 14:30:08 -070037 const CFX_Matrix& matrix) {
dsinclair2c489cc2016-11-23 16:17:20 -080038 if (!pGraphics)
39 return;
40 if (!m_pProperties->m_pThemeProvider)
41 return;
42 if ((m_pProperties->m_dwStates & FWL_WGTSTATE_Focused) == 0) {
43 GenerateBarcodeImageCache();
Lei Zhang41f22be2019-01-25 23:18:50 +000044 if (!m_pBarcodeEngine || m_eStatus != Status::kEncodeSuccess)
dsinclair2c489cc2016-11-23 16:17:20 -080045 return;
46
47 CFX_Matrix mt;
48 mt.e = GetRTClient().left;
49 mt.f = GetRTClient().top;
Lei Zhang4b472142017-08-17 14:30:08 -070050 mt.Concat(matrix);
dsinclair2c489cc2016-11-23 16:17:20 -080051
Lei Zhang4b472142017-08-17 14:30:08 -070052 m_pBarcodeEngine->RenderDevice(pGraphics->GetRenderDevice(), &matrix);
dsinclair2c489cc2016-11-23 16:17:20 -080053 return;
54 }
Lei Zhang4b472142017-08-17 14:30:08 -070055 CFWL_Edit::DrawWidget(pGraphics, matrix);
dsinclair2c489cc2016-11-23 16:17:20 -080056}
57
58void CFWL_Barcode::SetType(BC_TYPE type) {
59 if (m_type == type)
60 return;
61
62 m_pBarcodeEngine.reset();
63 m_type = type;
Lei Zhang41f22be2019-01-25 23:18:50 +000064 m_eStatus = Status::kNeedUpdate;
dsinclair2c489cc2016-11-23 16:17:20 -080065}
66
Lei Zhangb600f152019-01-25 23:21:50 +000067void CFWL_Barcode::SetText(const WideString& wsText) {
dsinclair2c489cc2016-11-23 16:17:20 -080068 m_pBarcodeEngine.reset();
Lei Zhang41f22be2019-01-25 23:18:50 +000069 m_eStatus = Status::kNeedUpdate;
Lei Zhangb600f152019-01-25 23:21:50 +000070 CFWL_Edit::SetText(wsText);
71}
72
73void CFWL_Barcode::SetTextSkipNotify(const WideString& wsText) {
74 m_pBarcodeEngine.reset();
75 m_eStatus = Status::kNeedUpdate;
76 CFWL_Edit::SetTextSkipNotify(wsText);
dsinclair2c489cc2016-11-23 16:17:20 -080077}
78
79bool CFWL_Barcode::IsProtectedType() const {
80 if (!m_pBarcodeEngine)
81 return true;
82
83 BC_TYPE tEngineType = m_pBarcodeEngine->GetType();
Lei Zhang41f22be2019-01-25 23:18:50 +000084 return tEngineType == BC_QR_CODE || tEngineType == BC_PDF417 ||
85 tEngineType == BC_DATAMATRIX;
dsinclair2c489cc2016-11-23 16:17:20 -080086}
87
88void CFWL_Barcode::OnProcessEvent(CFWL_Event* pEvent) {
Dan Sinclair6af53692018-05-29 19:42:39 +000089 if (pEvent->GetType() == CFWL_Event::Type::TextWillChange) {
dsinclair2c489cc2016-11-23 16:17:20 -080090 m_pBarcodeEngine.reset();
Lei Zhang41f22be2019-01-25 23:18:50 +000091 m_eStatus = Status::kNeedUpdate;
dsinclair2c489cc2016-11-23 16:17:20 -080092 }
93 CFWL_Edit::OnProcessEvent(pEvent);
dsinclair20855382016-10-31 07:29:34 -070094}
95
dsinclair42cb6452016-10-31 12:50:04 -070096void CFWL_Barcode::SetCharEncoding(BC_CHAR_ENCODING encoding) {
dsinclaireb3f68c2016-11-07 10:28:47 -080097 m_dwAttributeMask |= FWL_BCDATTRIBUTE_CHARENCODING;
98 m_eCharEncoding = encoding;
weili5d8e5aa2016-08-08 17:30:37 -070099}
dsinclaireb3f68c2016-11-07 10:28:47 -0800100
dsinclair42cb6452016-10-31 12:50:04 -0700101void CFWL_Barcode::SetModuleHeight(int32_t height) {
dsinclaireb3f68c2016-11-07 10:28:47 -0800102 m_dwAttributeMask |= FWL_BCDATTRIBUTE_MODULEHEIGHT;
103 m_nModuleHeight = height;
dsinclair42cb6452016-10-31 12:50:04 -0700104}
dsinclaireb3f68c2016-11-07 10:28:47 -0800105
dsinclair42cb6452016-10-31 12:50:04 -0700106void CFWL_Barcode::SetModuleWidth(int32_t width) {
dsinclaireb3f68c2016-11-07 10:28:47 -0800107 m_dwAttributeMask |= FWL_BCDATTRIBUTE_MODULEWIDTH;
108 m_nModuleWidth = width;
dsinclair42cb6452016-10-31 12:50:04 -0700109}
dsinclaireb3f68c2016-11-07 10:28:47 -0800110
dsinclair42cb6452016-10-31 12:50:04 -0700111void CFWL_Barcode::SetDataLength(int32_t dataLength) {
dsinclaireb3f68c2016-11-07 10:28:47 -0800112 m_dwAttributeMask |= FWL_BCDATTRIBUTE_DATALENGTH;
113 m_nDataLength = dataLength;
dsinclair2c489cc2016-11-23 16:17:20 -0800114 SetLimit(dataLength);
dsinclair42cb6452016-10-31 12:50:04 -0700115}
dsinclaireb3f68c2016-11-07 10:28:47 -0800116
tsepezd19e9122016-11-02 15:43:18 -0700117void CFWL_Barcode::SetCalChecksum(bool calChecksum) {
dsinclaireb3f68c2016-11-07 10:28:47 -0800118 m_dwAttributeMask |= FWL_BCDATTRIBUTE_CALCHECKSUM;
119 m_bCalChecksum = calChecksum;
dsinclair42cb6452016-10-31 12:50:04 -0700120}
dsinclaireb3f68c2016-11-07 10:28:47 -0800121
tsepezd19e9122016-11-02 15:43:18 -0700122void CFWL_Barcode::SetPrintChecksum(bool printChecksum) {
dsinclaireb3f68c2016-11-07 10:28:47 -0800123 m_dwAttributeMask |= FWL_BCDATTRIBUTE_PRINTCHECKSUM;
124 m_bPrintChecksum = printChecksum;
dsinclair42cb6452016-10-31 12:50:04 -0700125}
dsinclaireb3f68c2016-11-07 10:28:47 -0800126
dsinclair42cb6452016-10-31 12:50:04 -0700127void CFWL_Barcode::SetTextLocation(BC_TEXT_LOC location) {
dsinclaireb3f68c2016-11-07 10:28:47 -0800128 m_dwAttributeMask |= FWL_BCDATTRIBUTE_TEXTLOCATION;
129 m_eTextLocation = location;
dsinclair42cb6452016-10-31 12:50:04 -0700130}
dsinclaireb3f68c2016-11-07 10:28:47 -0800131
Lei Zhang1badb852017-04-20 15:58:56 -0700132void CFWL_Barcode::SetWideNarrowRatio(int8_t ratio) {
dsinclaireb3f68c2016-11-07 10:28:47 -0800133 m_dwAttributeMask |= FWL_BCDATTRIBUTE_WIDENARROWRATIO;
134 m_nWideNarrowRatio = ratio;
dsinclair42cb6452016-10-31 12:50:04 -0700135}
dsinclaireb3f68c2016-11-07 10:28:47 -0800136
Dan Sinclair812e96c2017-03-13 16:43:37 -0400137void CFWL_Barcode::SetStartChar(char startChar) {
dsinclaireb3f68c2016-11-07 10:28:47 -0800138 m_dwAttributeMask |= FWL_BCDATTRIBUTE_STARTCHAR;
139 m_cStartChar = startChar;
dsinclair42cb6452016-10-31 12:50:04 -0700140}
dsinclaireb3f68c2016-11-07 10:28:47 -0800141
Dan Sinclair812e96c2017-03-13 16:43:37 -0400142void CFWL_Barcode::SetEndChar(char endChar) {
dsinclaireb3f68c2016-11-07 10:28:47 -0800143 m_dwAttributeMask |= FWL_BCDATTRIBUTE_ENDCHAR;
144 m_cEndChar = endChar;
dsinclair42cb6452016-10-31 12:50:04 -0700145}
dsinclaireb3f68c2016-11-07 10:28:47 -0800146
dsinclair42cb6452016-10-31 12:50:04 -0700147void CFWL_Barcode::SetErrorCorrectionLevel(int32_t ecLevel) {
dsinclaireb3f68c2016-11-07 10:28:47 -0800148 m_dwAttributeMask |= FWL_BCDATTRIBUTE_ECLEVEL;
149 m_nECLevel = ecLevel;
dsinclair42cb6452016-10-31 12:50:04 -0700150}
dsinclaireb3f68c2016-11-07 10:28:47 -0800151
dsinclair2c489cc2016-11-23 16:17:20 -0800152void CFWL_Barcode::GenerateBarcodeImageCache() {
Lei Zhang41f22be2019-01-25 23:18:50 +0000153 if (m_eStatus != Status::kNeedUpdate)
dsinclair2c489cc2016-11-23 16:17:20 -0800154 return;
155
Lei Zhang41f22be2019-01-25 23:18:50 +0000156 m_eStatus = Status::kNormal;
dsinclair2c489cc2016-11-23 16:17:20 -0800157 CreateBarcodeEngine();
158 if (!m_pBarcodeEngine)
159 return;
160
dsinclair2c489cc2016-11-23 16:17:20 -0800161 IFWL_ThemeProvider* pTheme = GetAvailableTheme();
Dan Sinclairc635c932017-01-03 15:46:55 -0500162 if (pTheme) {
163 CFWL_ThemePart part;
164 part.m_pWidget = this;
Tom Sepezf8014512019-01-17 21:59:24 +0000165 if (RetainPtr<CFGAS_GEFont> pFont = pTheme->GetFont(part)) {
Dan Sinclairc635c932017-01-03 15:46:55 -0500166 if (CFX_Font* pCXFont = pFont->GetDevFont())
167 m_pBarcodeEngine->SetFont(pCXFont);
168 }
Tom Sepezf8014512019-01-17 21:59:24 +0000169 m_pBarcodeEngine->SetFontSize(pTheme->GetFontSize(part));
170 m_pBarcodeEngine->SetFontColor(pTheme->GetTextColor(part));
Dan Sinclairc635c932017-01-03 15:46:55 -0500171 } else {
172 m_pBarcodeEngine->SetFontSize(FWLTHEME_CAPACITY_FontSize);
173 }
dsinclair2c489cc2016-11-23 16:17:20 -0800174
175 m_pBarcodeEngine->SetHeight(int32_t(GetRTClient().height));
176 m_pBarcodeEngine->SetWidth(int32_t(GetRTClient().width));
177 if (m_dwAttributeMask & FWL_BCDATTRIBUTE_CHARENCODING)
178 m_pBarcodeEngine->SetCharEncoding(m_eCharEncoding);
179 if (m_dwAttributeMask & FWL_BCDATTRIBUTE_MODULEHEIGHT)
180 m_pBarcodeEngine->SetModuleHeight(m_nModuleHeight);
181 if (m_dwAttributeMask & FWL_BCDATTRIBUTE_MODULEWIDTH)
182 m_pBarcodeEngine->SetModuleWidth(m_nModuleWidth);
183 if (m_dwAttributeMask & FWL_BCDATTRIBUTE_DATALENGTH)
184 m_pBarcodeEngine->SetDataLength(m_nDataLength);
185 if (m_dwAttributeMask & FWL_BCDATTRIBUTE_CALCHECKSUM)
186 m_pBarcodeEngine->SetCalChecksum(m_bCalChecksum);
187 if (m_dwAttributeMask & FWL_BCDATTRIBUTE_PRINTCHECKSUM)
188 m_pBarcodeEngine->SetPrintChecksum(m_bPrintChecksum);
189 if (m_dwAttributeMask & FWL_BCDATTRIBUTE_TEXTLOCATION)
190 m_pBarcodeEngine->SetTextLocation(m_eTextLocation);
191 if (m_dwAttributeMask & FWL_BCDATTRIBUTE_WIDENARROWRATIO)
192 m_pBarcodeEngine->SetWideNarrowRatio(m_nWideNarrowRatio);
193 if (m_dwAttributeMask & FWL_BCDATTRIBUTE_STARTCHAR)
194 m_pBarcodeEngine->SetStartChar(m_cStartChar);
195 if (m_dwAttributeMask & FWL_BCDATTRIBUTE_ENDCHAR)
196 m_pBarcodeEngine->SetEndChar(m_cEndChar);
dsinclair2c489cc2016-11-23 16:17:20 -0800197 if (m_dwAttributeMask & FWL_BCDATTRIBUTE_ECLEVEL)
198 m_pBarcodeEngine->SetErrorCorrectionLevel(m_nECLevel);
dsinclair2c489cc2016-11-23 16:17:20 -0800199
Lei Zhang41f22be2019-01-25 23:18:50 +0000200 m_eStatus = m_pBarcodeEngine->Encode(GetText().AsStringView())
201 ? Status::kEncodeSuccess
202 : Status::kNormal;
Tom Sepez99ffdb02016-01-26 14:51:21 -0800203}
dsinclair7f432a12016-03-29 12:38:01 -0700204
dsinclair2c489cc2016-11-23 16:17:20 -0800205void CFWL_Barcode::CreateBarcodeEngine() {
206 if (m_pBarcodeEngine || m_type == BC_UNKNOWN)
207 return;
weili4ce94e12016-06-18 06:21:57 -0700208
Ryan Harrison4b9d6982018-01-26 17:11:51 +0000209 m_pBarcodeEngine = CFX_Barcode::Create(m_type);
weili4ce94e12016-06-18 06:21:57 -0700210}