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" |
dsinclair | 42cb645 | 2016-10-31 12:50:04 -0700 | [diff] [blame] | 17 | |
dsinclair | 1a7534a | 2016-11-22 15:56:11 -0800 | [diff] [blame] | 18 | CFWL_Barcode::CFWL_Barcode(const CFWL_App* app) |
dsinclair | 2c489cc | 2016-11-23 16:17:20 -0800 | [diff] [blame] | 19 | : CFWL_Edit(app, pdfium::MakeUnique<CFWL_WidgetProperties>(), nullptr), |
| 20 | m_dwStatus(0), |
| 21 | m_type(BC_UNKNOWN), |
| 22 | m_dwAttributeMask(FWL_BCDATTRIBUTE_NONE) {} |
dsinclair | 2085538 | 2016-10-31 07:29:34 -0700 | [diff] [blame] | 23 | |
| 24 | CFWL_Barcode::~CFWL_Barcode() {} |
| 25 | |
dsinclair | 2c489cc | 2016-11-23 16:17:20 -0800 | [diff] [blame] | 26 | FWL_Type CFWL_Barcode::GetClassID() const { |
| 27 | return FWL_Type::Barcode; |
| 28 | } |
dsinclair | 2085538 | 2016-10-31 07:29:34 -0700 | [diff] [blame] | 29 | |
dsinclair | 2c489cc | 2016-11-23 16:17:20 -0800 | [diff] [blame] | 30 | void CFWL_Barcode::Update() { |
| 31 | if (IsLocked()) |
| 32 | return; |
dsinclair | 6fe8795 | 2016-11-01 18:48:19 -0700 | [diff] [blame] | 33 | |
dsinclair | 2c489cc | 2016-11-23 16:17:20 -0800 | [diff] [blame] | 34 | CFWL_Edit::Update(); |
| 35 | GenerateBarcodeImageCache(); |
| 36 | } |
| 37 | |
| 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 | |
| 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 | |
| 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 | |
| 78 | 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 | |
| 90 | void CFWL_Barcode::OnProcessEvent(CFWL_Event* pEvent) { |
dsinclair | 4614b45 | 2016-12-07 17:01:58 -0800 | [diff] [blame] | 91 | if (pEvent->GetType() == CFWL_Event::Type::TextChanged) { |
dsinclair | 2c489cc | 2016-11-23 16:17:20 -0800 | [diff] [blame] | 92 | m_pBarcodeEngine.reset(); |
| 93 | m_dwStatus = XFA_BCS_NeedUpdate; |
| 94 | } |
| 95 | CFWL_Edit::OnProcessEvent(pEvent); |
dsinclair | 2085538 | 2016-10-31 07:29:34 -0700 | [diff] [blame] | 96 | } |
| 97 | |
dsinclair | 42cb645 | 2016-10-31 12:50:04 -0700 | [diff] [blame] | 98 | void CFWL_Barcode::SetCharEncoding(BC_CHAR_ENCODING encoding) { |
dsinclair | eb3f68c | 2016-11-07 10:28:47 -0800 | [diff] [blame] | 99 | m_dwAttributeMask |= FWL_BCDATTRIBUTE_CHARENCODING; |
| 100 | m_eCharEncoding = encoding; |
weili | 5d8e5aa | 2016-08-08 17:30:37 -0700 | [diff] [blame] | 101 | } |
dsinclair | eb3f68c | 2016-11-07 10:28:47 -0800 | [diff] [blame] | 102 | |
dsinclair | 42cb645 | 2016-10-31 12:50:04 -0700 | [diff] [blame] | 103 | void CFWL_Barcode::SetModuleHeight(int32_t height) { |
dsinclair | eb3f68c | 2016-11-07 10:28:47 -0800 | [diff] [blame] | 104 | m_dwAttributeMask |= FWL_BCDATTRIBUTE_MODULEHEIGHT; |
| 105 | m_nModuleHeight = height; |
dsinclair | 42cb645 | 2016-10-31 12:50:04 -0700 | [diff] [blame] | 106 | } |
dsinclair | eb3f68c | 2016-11-07 10:28:47 -0800 | [diff] [blame] | 107 | |
dsinclair | 42cb645 | 2016-10-31 12:50:04 -0700 | [diff] [blame] | 108 | void CFWL_Barcode::SetModuleWidth(int32_t width) { |
dsinclair | eb3f68c | 2016-11-07 10:28:47 -0800 | [diff] [blame] | 109 | m_dwAttributeMask |= FWL_BCDATTRIBUTE_MODULEWIDTH; |
| 110 | m_nModuleWidth = width; |
dsinclair | 42cb645 | 2016-10-31 12:50:04 -0700 | [diff] [blame] | 111 | } |
dsinclair | eb3f68c | 2016-11-07 10:28:47 -0800 | [diff] [blame] | 112 | |
dsinclair | 42cb645 | 2016-10-31 12:50:04 -0700 | [diff] [blame] | 113 | void CFWL_Barcode::SetDataLength(int32_t dataLength) { |
dsinclair | eb3f68c | 2016-11-07 10:28:47 -0800 | [diff] [blame] | 114 | m_dwAttributeMask |= FWL_BCDATTRIBUTE_DATALENGTH; |
| 115 | m_nDataLength = dataLength; |
dsinclair | 2c489cc | 2016-11-23 16:17:20 -0800 | [diff] [blame] | 116 | SetLimit(dataLength); |
dsinclair | 42cb645 | 2016-10-31 12:50:04 -0700 | [diff] [blame] | 117 | } |
dsinclair | eb3f68c | 2016-11-07 10:28:47 -0800 | [diff] [blame] | 118 | |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 119 | void CFWL_Barcode::SetCalChecksum(bool calChecksum) { |
dsinclair | eb3f68c | 2016-11-07 10:28:47 -0800 | [diff] [blame] | 120 | m_dwAttributeMask |= FWL_BCDATTRIBUTE_CALCHECKSUM; |
| 121 | m_bCalChecksum = calChecksum; |
dsinclair | 42cb645 | 2016-10-31 12:50:04 -0700 | [diff] [blame] | 122 | } |
dsinclair | eb3f68c | 2016-11-07 10:28:47 -0800 | [diff] [blame] | 123 | |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 124 | void CFWL_Barcode::SetPrintChecksum(bool printChecksum) { |
dsinclair | eb3f68c | 2016-11-07 10:28:47 -0800 | [diff] [blame] | 125 | m_dwAttributeMask |= FWL_BCDATTRIBUTE_PRINTCHECKSUM; |
| 126 | m_bPrintChecksum = printChecksum; |
dsinclair | 42cb645 | 2016-10-31 12:50:04 -0700 | [diff] [blame] | 127 | } |
dsinclair | eb3f68c | 2016-11-07 10:28:47 -0800 | [diff] [blame] | 128 | |
dsinclair | 42cb645 | 2016-10-31 12:50:04 -0700 | [diff] [blame] | 129 | void CFWL_Barcode::SetTextLocation(BC_TEXT_LOC location) { |
dsinclair | eb3f68c | 2016-11-07 10:28:47 -0800 | [diff] [blame] | 130 | m_dwAttributeMask |= FWL_BCDATTRIBUTE_TEXTLOCATION; |
| 131 | m_eTextLocation = location; |
dsinclair | 42cb645 | 2016-10-31 12:50:04 -0700 | [diff] [blame] | 132 | } |
dsinclair | eb3f68c | 2016-11-07 10:28:47 -0800 | [diff] [blame] | 133 | |
dsinclair | 42cb645 | 2016-10-31 12:50:04 -0700 | [diff] [blame] | 134 | void CFWL_Barcode::SetWideNarrowRatio(int32_t ratio) { |
dsinclair | eb3f68c | 2016-11-07 10:28:47 -0800 | [diff] [blame] | 135 | m_dwAttributeMask |= FWL_BCDATTRIBUTE_WIDENARROWRATIO; |
| 136 | m_nWideNarrowRatio = ratio; |
dsinclair | 42cb645 | 2016-10-31 12:50:04 -0700 | [diff] [blame] | 137 | } |
dsinclair | eb3f68c | 2016-11-07 10:28:47 -0800 | [diff] [blame] | 138 | |
Dan Sinclair | 812e96c | 2017-03-13 16:43:37 -0400 | [diff] [blame] | 139 | void CFWL_Barcode::SetStartChar(char startChar) { |
dsinclair | eb3f68c | 2016-11-07 10:28:47 -0800 | [diff] [blame] | 140 | m_dwAttributeMask |= FWL_BCDATTRIBUTE_STARTCHAR; |
| 141 | m_cStartChar = startChar; |
dsinclair | 42cb645 | 2016-10-31 12:50:04 -0700 | [diff] [blame] | 142 | } |
dsinclair | eb3f68c | 2016-11-07 10:28:47 -0800 | [diff] [blame] | 143 | |
Dan Sinclair | 812e96c | 2017-03-13 16:43:37 -0400 | [diff] [blame] | 144 | void CFWL_Barcode::SetEndChar(char endChar) { |
dsinclair | eb3f68c | 2016-11-07 10:28:47 -0800 | [diff] [blame] | 145 | m_dwAttributeMask |= FWL_BCDATTRIBUTE_ENDCHAR; |
| 146 | m_cEndChar = endChar; |
dsinclair | 42cb645 | 2016-10-31 12:50:04 -0700 | [diff] [blame] | 147 | } |
dsinclair | eb3f68c | 2016-11-07 10:28:47 -0800 | [diff] [blame] | 148 | |
dsinclair | 42cb645 | 2016-10-31 12:50:04 -0700 | [diff] [blame] | 149 | void CFWL_Barcode::SetErrorCorrectionLevel(int32_t ecLevel) { |
dsinclair | eb3f68c | 2016-11-07 10:28:47 -0800 | [diff] [blame] | 150 | m_dwAttributeMask |= FWL_BCDATTRIBUTE_ECLEVEL; |
| 151 | m_nECLevel = ecLevel; |
dsinclair | 42cb645 | 2016-10-31 12:50:04 -0700 | [diff] [blame] | 152 | } |
dsinclair | eb3f68c | 2016-11-07 10:28:47 -0800 | [diff] [blame] | 153 | |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 154 | void CFWL_Barcode::SetTruncated(bool truncated) { |
dsinclair | eb3f68c | 2016-11-07 10:28:47 -0800 | [diff] [blame] | 155 | m_dwAttributeMask |= FWL_BCDATTRIBUTE_TRUNCATED; |
| 156 | m_bTruncated = truncated; |
dsinclair | 42cb645 | 2016-10-31 12:50:04 -0700 | [diff] [blame] | 157 | } |
dsinclair | eb3f68c | 2016-11-07 10:28:47 -0800 | [diff] [blame] | 158 | |
dsinclair | 2c489cc | 2016-11-23 16:17:20 -0800 | [diff] [blame] | 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 | |
dsinclair | 2c489cc | 2016-11-23 16:17:20 -0800 | [diff] [blame] | 168 | IFWL_ThemeProvider* pTheme = GetAvailableTheme(); |
Dan Sinclair | c635c93 | 2017-01-03 15:46:55 -0500 | [diff] [blame] | 169 | if (pTheme) { |
| 170 | CFWL_ThemePart part; |
| 171 | part.m_pWidget = this; |
tsepez | e647799 | 2017-01-05 12:57:00 -0800 | [diff] [blame] | 172 | if (CFX_RetainPtr<CFGAS_GEFont> pFont = pTheme->GetFont(&part)) { |
Dan Sinclair | c635c93 | 2017-01-03 15:46:55 -0500 | [diff] [blame] | 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 | } |
dsinclair | 2c489cc | 2016-11-23 16:17:20 -0800 | [diff] [blame] | 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); |
dsinclair | 2c489cc | 2016-11-23 16:17:20 -0800 | [diff] [blame] | 204 | 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 Sinclair | c635c93 | 2017-01-03 15:46:55 -0500 | [diff] [blame] | 210 | m_dwStatus = m_pBarcodeEngine->Encode(GetText().AsStringC(), true, errorCode) |
dsinclair | 2c489cc | 2016-11-23 16:17:20 -0800 | [diff] [blame] | 211 | ? XFA_BCS_EncodeSuccess |
| 212 | : 0; |
Tom Sepez | 99ffdb0 | 2016-01-26 14:51:21 -0800 | [diff] [blame] | 213 | } |
dsinclair | 7f432a1 | 2016-03-29 12:38:01 -0700 | [diff] [blame] | 214 | |
dsinclair | 2c489cc | 2016-11-23 16:17:20 -0800 | [diff] [blame] | 215 | void CFWL_Barcode::CreateBarcodeEngine() { |
| 216 | if (m_pBarcodeEngine || m_type == BC_UNKNOWN) |
| 217 | return; |
weili | 4ce94e1 | 2016-06-18 06:21:57 -0700 | [diff] [blame] | 218 | |
Dan Sinclair | 0bb1333 | 2017-03-30 16:12:02 -0400 | [diff] [blame] | 219 | auto pBarcode = pdfium::MakeUnique<CFX_Barcode>(); |
dsinclair | 2c489cc | 2016-11-23 16:17:20 -0800 | [diff] [blame] | 220 | if (pBarcode->Create(m_type)) |
| 221 | m_pBarcodeEngine = std::move(pBarcode); |
weili | 4ce94e1 | 2016-06-18 06:21:57 -0700 | [diff] [blame] | 222 | } |