Tom Sepez | 99ffdb0 | 2016-01-26 14:51:21 -0800 | [diff] [blame] | 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 | |
dsinclair | 447b1f3 | 2016-12-08 10:06:32 -0800 | [diff] [blame] | 7 | #include "xfa/fwl/cfwl_barcode.h" |
Dan Sinclair | e73c5ce | 2016-02-25 13:38:37 -0500 | [diff] [blame] | 8 | |
dsinclair | 7c47e1d | 2016-11-23 10:49:33 -0800 | [diff] [blame] | 9 | #include <utility> |
Tom Sepez | 99ffdb0 | 2016-01-26 14:51:21 -0800 | [diff] [blame] | 10 | |
dsinclair | 6fe8795 | 2016-11-01 18:48:19 -0700 | [diff] [blame] | 11 | #include "third_party/base/ptr_util.h" |
dsinclair | 2c489cc | 2016-11-23 16:17:20 -0800 | [diff] [blame] | 12 | #include "xfa/fgas/font/cfgas_gefont.h" |
dsinclair | 447b1f3 | 2016-12-08 10:06:32 -0800 | [diff] [blame] | 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" |
Henrique Nakashima | cb32712 | 2017-09-13 13:00:58 -0400 | [diff] [blame] | 17 | #include "xfa/fwl/theme/cfwl_utils.h" |
dsinclair | 42cb645 | 2016-10-31 12:50:04 -0700 | [diff] [blame] | 18 | |
dsinclair | 1a7534a | 2016-11-22 15:56:11 -0800 | [diff] [blame] | 19 | CFWL_Barcode::CFWL_Barcode(const CFWL_App* app) |
Lei Zhang | 41f22be | 2019-01-25 23:18:50 +0000 | [diff] [blame] | 20 | : CFWL_Edit(app, pdfium::MakeUnique<CFWL_WidgetProperties>(), nullptr) {} |
dsinclair | 2085538 | 2016-10-31 07:29:34 -0700 | [diff] [blame] | 21 | |
Lei Zhang | 41f22be | 2019-01-25 23:18:50 +0000 | [diff] [blame] | 22 | CFWL_Barcode::~CFWL_Barcode() = default; |
dsinclair | 2085538 | 2016-10-31 07:29:34 -0700 | [diff] [blame] | 23 | |
dsinclair | 2c489cc | 2016-11-23 16:17:20 -0800 | [diff] [blame] | 24 | FWL_Type CFWL_Barcode::GetClassID() const { |
| 25 | return FWL_Type::Barcode; |
| 26 | } |
dsinclair | 2085538 | 2016-10-31 07:29:34 -0700 | [diff] [blame] | 27 | |
dsinclair | 2c489cc | 2016-11-23 16:17:20 -0800 | [diff] [blame] | 28 | void CFWL_Barcode::Update() { |
| 29 | if (IsLocked()) |
| 30 | return; |
dsinclair | 6fe8795 | 2016-11-01 18:48:19 -0700 | [diff] [blame] | 31 | |
dsinclair | 2c489cc | 2016-11-23 16:17:20 -0800 | [diff] [blame] | 32 | CFWL_Edit::Update(); |
| 33 | GenerateBarcodeImageCache(); |
| 34 | } |
| 35 | |
Dan Sinclair | 2b918c8 | 2017-07-13 14:47:10 -0400 | [diff] [blame] | 36 | void CFWL_Barcode::DrawWidget(CXFA_Graphics* pGraphics, |
Lei Zhang | 4b47214 | 2017-08-17 14:30:08 -0700 | [diff] [blame] | 37 | const CFX_Matrix& matrix) { |
dsinclair | 2c489cc | 2016-11-23 16:17:20 -0800 | [diff] [blame] | 38 | 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 Zhang | 41f22be | 2019-01-25 23:18:50 +0000 | [diff] [blame] | 44 | if (!m_pBarcodeEngine || m_eStatus != Status::kEncodeSuccess) |
dsinclair | 2c489cc | 2016-11-23 16:17:20 -0800 | [diff] [blame] | 45 | return; |
| 46 | |
| 47 | CFX_Matrix mt; |
| 48 | mt.e = GetRTClient().left; |
| 49 | mt.f = GetRTClient().top; |
Lei Zhang | 4b47214 | 2017-08-17 14:30:08 -0700 | [diff] [blame] | 50 | mt.Concat(matrix); |
dsinclair | 2c489cc | 2016-11-23 16:17:20 -0800 | [diff] [blame] | 51 | |
Lei Zhang | 4b47214 | 2017-08-17 14:30:08 -0700 | [diff] [blame] | 52 | m_pBarcodeEngine->RenderDevice(pGraphics->GetRenderDevice(), &matrix); |
dsinclair | 2c489cc | 2016-11-23 16:17:20 -0800 | [diff] [blame] | 53 | return; |
| 54 | } |
Lei Zhang | 4b47214 | 2017-08-17 14:30:08 -0700 | [diff] [blame] | 55 | CFWL_Edit::DrawWidget(pGraphics, matrix); |
dsinclair | 2c489cc | 2016-11-23 16:17:20 -0800 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | void CFWL_Barcode::SetType(BC_TYPE type) { |
| 59 | if (m_type == type) |
| 60 | return; |
| 61 | |
| 62 | m_pBarcodeEngine.reset(); |
| 63 | m_type = type; |
Lei Zhang | 41f22be | 2019-01-25 23:18:50 +0000 | [diff] [blame] | 64 | m_eStatus = Status::kNeedUpdate; |
dsinclair | 2c489cc | 2016-11-23 16:17:20 -0800 | [diff] [blame] | 65 | } |
| 66 | |
Lei Zhang | b600f15 | 2019-01-25 23:21:50 +0000 | [diff] [blame] | 67 | void CFWL_Barcode::SetText(const WideString& wsText) { |
dsinclair | 2c489cc | 2016-11-23 16:17:20 -0800 | [diff] [blame] | 68 | m_pBarcodeEngine.reset(); |
Lei Zhang | 41f22be | 2019-01-25 23:18:50 +0000 | [diff] [blame] | 69 | m_eStatus = Status::kNeedUpdate; |
Lei Zhang | b600f15 | 2019-01-25 23:21:50 +0000 | [diff] [blame] | 70 | CFWL_Edit::SetText(wsText); |
| 71 | } |
| 72 | |
| 73 | void CFWL_Barcode::SetTextSkipNotify(const WideString& wsText) { |
| 74 | m_pBarcodeEngine.reset(); |
| 75 | m_eStatus = Status::kNeedUpdate; |
| 76 | CFWL_Edit::SetTextSkipNotify(wsText); |
dsinclair | 2c489cc | 2016-11-23 16:17:20 -0800 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | bool CFWL_Barcode::IsProtectedType() const { |
| 80 | if (!m_pBarcodeEngine) |
| 81 | return true; |
| 82 | |
| 83 | BC_TYPE tEngineType = m_pBarcodeEngine->GetType(); |
Lei Zhang | 41f22be | 2019-01-25 23:18:50 +0000 | [diff] [blame] | 84 | return tEngineType == BC_QR_CODE || tEngineType == BC_PDF417 || |
| 85 | tEngineType == BC_DATAMATRIX; |
dsinclair | 2c489cc | 2016-11-23 16:17:20 -0800 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | void CFWL_Barcode::OnProcessEvent(CFWL_Event* pEvent) { |
Dan Sinclair | 6af5369 | 2018-05-29 19:42:39 +0000 | [diff] [blame] | 89 | if (pEvent->GetType() == CFWL_Event::Type::TextWillChange) { |
dsinclair | 2c489cc | 2016-11-23 16:17:20 -0800 | [diff] [blame] | 90 | m_pBarcodeEngine.reset(); |
Lei Zhang | 41f22be | 2019-01-25 23:18:50 +0000 | [diff] [blame] | 91 | m_eStatus = Status::kNeedUpdate; |
dsinclair | 2c489cc | 2016-11-23 16:17:20 -0800 | [diff] [blame] | 92 | } |
| 93 | CFWL_Edit::OnProcessEvent(pEvent); |
dsinclair | 2085538 | 2016-10-31 07:29:34 -0700 | [diff] [blame] | 94 | } |
| 95 | |
dsinclair | 42cb645 | 2016-10-31 12:50:04 -0700 | [diff] [blame] | 96 | void CFWL_Barcode::SetCharEncoding(BC_CHAR_ENCODING encoding) { |
dsinclair | eb3f68c | 2016-11-07 10:28:47 -0800 | [diff] [blame] | 97 | m_dwAttributeMask |= FWL_BCDATTRIBUTE_CHARENCODING; |
| 98 | m_eCharEncoding = encoding; |
weili | 5d8e5aa | 2016-08-08 17:30:37 -0700 | [diff] [blame] | 99 | } |
dsinclair | eb3f68c | 2016-11-07 10:28:47 -0800 | [diff] [blame] | 100 | |
dsinclair | 42cb645 | 2016-10-31 12:50:04 -0700 | [diff] [blame] | 101 | void CFWL_Barcode::SetModuleHeight(int32_t height) { |
dsinclair | eb3f68c | 2016-11-07 10:28:47 -0800 | [diff] [blame] | 102 | m_dwAttributeMask |= FWL_BCDATTRIBUTE_MODULEHEIGHT; |
| 103 | m_nModuleHeight = height; |
dsinclair | 42cb645 | 2016-10-31 12:50:04 -0700 | [diff] [blame] | 104 | } |
dsinclair | eb3f68c | 2016-11-07 10:28:47 -0800 | [diff] [blame] | 105 | |
dsinclair | 42cb645 | 2016-10-31 12:50:04 -0700 | [diff] [blame] | 106 | void CFWL_Barcode::SetModuleWidth(int32_t width) { |
dsinclair | eb3f68c | 2016-11-07 10:28:47 -0800 | [diff] [blame] | 107 | m_dwAttributeMask |= FWL_BCDATTRIBUTE_MODULEWIDTH; |
| 108 | m_nModuleWidth = width; |
dsinclair | 42cb645 | 2016-10-31 12:50:04 -0700 | [diff] [blame] | 109 | } |
dsinclair | eb3f68c | 2016-11-07 10:28:47 -0800 | [diff] [blame] | 110 | |
dsinclair | 42cb645 | 2016-10-31 12:50:04 -0700 | [diff] [blame] | 111 | void CFWL_Barcode::SetDataLength(int32_t dataLength) { |
dsinclair | eb3f68c | 2016-11-07 10:28:47 -0800 | [diff] [blame] | 112 | m_dwAttributeMask |= FWL_BCDATTRIBUTE_DATALENGTH; |
| 113 | m_nDataLength = dataLength; |
dsinclair | 2c489cc | 2016-11-23 16:17:20 -0800 | [diff] [blame] | 114 | SetLimit(dataLength); |
dsinclair | 42cb645 | 2016-10-31 12:50:04 -0700 | [diff] [blame] | 115 | } |
dsinclair | eb3f68c | 2016-11-07 10:28:47 -0800 | [diff] [blame] | 116 | |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 117 | void CFWL_Barcode::SetCalChecksum(bool calChecksum) { |
dsinclair | eb3f68c | 2016-11-07 10:28:47 -0800 | [diff] [blame] | 118 | m_dwAttributeMask |= FWL_BCDATTRIBUTE_CALCHECKSUM; |
| 119 | m_bCalChecksum = calChecksum; |
dsinclair | 42cb645 | 2016-10-31 12:50:04 -0700 | [diff] [blame] | 120 | } |
dsinclair | eb3f68c | 2016-11-07 10:28:47 -0800 | [diff] [blame] | 121 | |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 122 | void CFWL_Barcode::SetPrintChecksum(bool printChecksum) { |
dsinclair | eb3f68c | 2016-11-07 10:28:47 -0800 | [diff] [blame] | 123 | m_dwAttributeMask |= FWL_BCDATTRIBUTE_PRINTCHECKSUM; |
| 124 | m_bPrintChecksum = printChecksum; |
dsinclair | 42cb645 | 2016-10-31 12:50:04 -0700 | [diff] [blame] | 125 | } |
dsinclair | eb3f68c | 2016-11-07 10:28:47 -0800 | [diff] [blame] | 126 | |
dsinclair | 42cb645 | 2016-10-31 12:50:04 -0700 | [diff] [blame] | 127 | void CFWL_Barcode::SetTextLocation(BC_TEXT_LOC location) { |
dsinclair | eb3f68c | 2016-11-07 10:28:47 -0800 | [diff] [blame] | 128 | m_dwAttributeMask |= FWL_BCDATTRIBUTE_TEXTLOCATION; |
| 129 | m_eTextLocation = location; |
dsinclair | 42cb645 | 2016-10-31 12:50:04 -0700 | [diff] [blame] | 130 | } |
dsinclair | eb3f68c | 2016-11-07 10:28:47 -0800 | [diff] [blame] | 131 | |
Lei Zhang | 1badb85 | 2017-04-20 15:58:56 -0700 | [diff] [blame] | 132 | void CFWL_Barcode::SetWideNarrowRatio(int8_t ratio) { |
dsinclair | eb3f68c | 2016-11-07 10:28:47 -0800 | [diff] [blame] | 133 | m_dwAttributeMask |= FWL_BCDATTRIBUTE_WIDENARROWRATIO; |
| 134 | m_nWideNarrowRatio = ratio; |
dsinclair | 42cb645 | 2016-10-31 12:50:04 -0700 | [diff] [blame] | 135 | } |
dsinclair | eb3f68c | 2016-11-07 10:28:47 -0800 | [diff] [blame] | 136 | |
Dan Sinclair | 812e96c | 2017-03-13 16:43:37 -0400 | [diff] [blame] | 137 | void CFWL_Barcode::SetStartChar(char startChar) { |
dsinclair | eb3f68c | 2016-11-07 10:28:47 -0800 | [diff] [blame] | 138 | m_dwAttributeMask |= FWL_BCDATTRIBUTE_STARTCHAR; |
| 139 | m_cStartChar = startChar; |
dsinclair | 42cb645 | 2016-10-31 12:50:04 -0700 | [diff] [blame] | 140 | } |
dsinclair | eb3f68c | 2016-11-07 10:28:47 -0800 | [diff] [blame] | 141 | |
Dan Sinclair | 812e96c | 2017-03-13 16:43:37 -0400 | [diff] [blame] | 142 | void CFWL_Barcode::SetEndChar(char endChar) { |
dsinclair | eb3f68c | 2016-11-07 10:28:47 -0800 | [diff] [blame] | 143 | m_dwAttributeMask |= FWL_BCDATTRIBUTE_ENDCHAR; |
| 144 | m_cEndChar = endChar; |
dsinclair | 42cb645 | 2016-10-31 12:50:04 -0700 | [diff] [blame] | 145 | } |
dsinclair | eb3f68c | 2016-11-07 10:28:47 -0800 | [diff] [blame] | 146 | |
dsinclair | 42cb645 | 2016-10-31 12:50:04 -0700 | [diff] [blame] | 147 | void CFWL_Barcode::SetErrorCorrectionLevel(int32_t ecLevel) { |
dsinclair | eb3f68c | 2016-11-07 10:28:47 -0800 | [diff] [blame] | 148 | m_dwAttributeMask |= FWL_BCDATTRIBUTE_ECLEVEL; |
| 149 | m_nECLevel = ecLevel; |
dsinclair | 42cb645 | 2016-10-31 12:50:04 -0700 | [diff] [blame] | 150 | } |
dsinclair | eb3f68c | 2016-11-07 10:28:47 -0800 | [diff] [blame] | 151 | |
dsinclair | 2c489cc | 2016-11-23 16:17:20 -0800 | [diff] [blame] | 152 | void CFWL_Barcode::GenerateBarcodeImageCache() { |
Lei Zhang | 41f22be | 2019-01-25 23:18:50 +0000 | [diff] [blame] | 153 | if (m_eStatus != Status::kNeedUpdate) |
dsinclair | 2c489cc | 2016-11-23 16:17:20 -0800 | [diff] [blame] | 154 | return; |
| 155 | |
Lei Zhang | 41f22be | 2019-01-25 23:18:50 +0000 | [diff] [blame] | 156 | m_eStatus = Status::kNormal; |
dsinclair | 2c489cc | 2016-11-23 16:17:20 -0800 | [diff] [blame] | 157 | CreateBarcodeEngine(); |
| 158 | if (!m_pBarcodeEngine) |
| 159 | return; |
| 160 | |
dsinclair | 2c489cc | 2016-11-23 16:17:20 -0800 | [diff] [blame] | 161 | IFWL_ThemeProvider* pTheme = GetAvailableTheme(); |
Dan Sinclair | c635c93 | 2017-01-03 15:46:55 -0500 | [diff] [blame] | 162 | if (pTheme) { |
| 163 | CFWL_ThemePart part; |
| 164 | part.m_pWidget = this; |
Tom Sepez | f801451 | 2019-01-17 21:59:24 +0000 | [diff] [blame] | 165 | if (RetainPtr<CFGAS_GEFont> pFont = pTheme->GetFont(part)) { |
Dan Sinclair | c635c93 | 2017-01-03 15:46:55 -0500 | [diff] [blame] | 166 | if (CFX_Font* pCXFont = pFont->GetDevFont()) |
| 167 | m_pBarcodeEngine->SetFont(pCXFont); |
| 168 | } |
Tom Sepez | f801451 | 2019-01-17 21:59:24 +0000 | [diff] [blame] | 169 | m_pBarcodeEngine->SetFontSize(pTheme->GetFontSize(part)); |
| 170 | m_pBarcodeEngine->SetFontColor(pTheme->GetTextColor(part)); |
Dan Sinclair | c635c93 | 2017-01-03 15:46:55 -0500 | [diff] [blame] | 171 | } else { |
| 172 | m_pBarcodeEngine->SetFontSize(FWLTHEME_CAPACITY_FontSize); |
| 173 | } |
dsinclair | 2c489cc | 2016-11-23 16:17:20 -0800 | [diff] [blame] | 174 | |
| 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); |
dsinclair | 2c489cc | 2016-11-23 16:17:20 -0800 | [diff] [blame] | 197 | if (m_dwAttributeMask & FWL_BCDATTRIBUTE_ECLEVEL) |
| 198 | m_pBarcodeEngine->SetErrorCorrectionLevel(m_nECLevel); |
dsinclair | 2c489cc | 2016-11-23 16:17:20 -0800 | [diff] [blame] | 199 | |
Lei Zhang | 41f22be | 2019-01-25 23:18:50 +0000 | [diff] [blame] | 200 | m_eStatus = m_pBarcodeEngine->Encode(GetText().AsStringView()) |
| 201 | ? Status::kEncodeSuccess |
| 202 | : Status::kNormal; |
Tom Sepez | 99ffdb0 | 2016-01-26 14:51:21 -0800 | [diff] [blame] | 203 | } |
dsinclair | 7f432a1 | 2016-03-29 12:38:01 -0700 | [diff] [blame] | 204 | |
dsinclair | 2c489cc | 2016-11-23 16:17:20 -0800 | [diff] [blame] | 205 | void CFWL_Barcode::CreateBarcodeEngine() { |
| 206 | if (m_pBarcodeEngine || m_type == BC_UNKNOWN) |
| 207 | return; |
weili | 4ce94e1 | 2016-06-18 06:21:57 -0700 | [diff] [blame] | 208 | |
Ryan Harrison | 4b9d698 | 2018-01-26 17:11:51 +0000 | [diff] [blame] | 209 | m_pBarcodeEngine = CFX_Barcode::Create(m_type); |
weili | 4ce94e1 | 2016-06-18 06:21:57 -0700 | [diff] [blame] | 210 | } |