| OLD | NEW |
| (Empty) |
| 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/core/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/core/cfwl_notedriver.h" | |
| 14 #include "xfa/fwl/core/cfwl_themepart.h" | |
| 15 #include "xfa/fwl/core/cfx_barcode.h" | |
| 16 #include "xfa/fwl/core/ifwl_themeprovider.h" | |
| 17 | |
| 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 | |
| 24 CFWL_Barcode::~CFWL_Barcode() {} | |
| 25 | |
| 26 FWL_Type CFWL_Barcode::GetClassID() const { | |
| 27 return FWL_Type::Barcode; | |
| 28 } | |
| 29 | |
| 30 void CFWL_Barcode::Update() { | |
| 31 if (IsLocked()) | |
| 32 return; | |
| 33 | |
| 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) { | |
| 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 | |
| 98 void CFWL_Barcode::SetCharEncoding(BC_CHAR_ENCODING encoding) { | |
| 99 m_dwAttributeMask |= FWL_BCDATTRIBUTE_CHARENCODING; | |
| 100 m_eCharEncoding = encoding; | |
| 101 } | |
| 102 | |
| 103 void CFWL_Barcode::SetModuleHeight(int32_t height) { | |
| 104 m_dwAttributeMask |= FWL_BCDATTRIBUTE_MODULEHEIGHT; | |
| 105 m_nModuleHeight = height; | |
| 106 } | |
| 107 | |
| 108 void CFWL_Barcode::SetModuleWidth(int32_t width) { | |
| 109 m_dwAttributeMask |= FWL_BCDATTRIBUTE_MODULEWIDTH; | |
| 110 m_nModuleWidth = width; | |
| 111 } | |
| 112 | |
| 113 void CFWL_Barcode::SetDataLength(int32_t dataLength) { | |
| 114 m_dwAttributeMask |= FWL_BCDATTRIBUTE_DATALENGTH; | |
| 115 m_nDataLength = dataLength; | |
| 116 SetLimit(dataLength); | |
| 117 } | |
| 118 | |
| 119 void CFWL_Barcode::SetCalChecksum(bool calChecksum) { | |
| 120 m_dwAttributeMask |= FWL_BCDATTRIBUTE_CALCHECKSUM; | |
| 121 m_bCalChecksum = calChecksum; | |
| 122 } | |
| 123 | |
| 124 void CFWL_Barcode::SetPrintChecksum(bool printChecksum) { | |
| 125 m_dwAttributeMask |= FWL_BCDATTRIBUTE_PRINTCHECKSUM; | |
| 126 m_bPrintChecksum = printChecksum; | |
| 127 } | |
| 128 | |
| 129 void CFWL_Barcode::SetTextLocation(BC_TEXT_LOC location) { | |
| 130 m_dwAttributeMask |= FWL_BCDATTRIBUTE_TEXTLOCATION; | |
| 131 m_eTextLocation = location; | |
| 132 } | |
| 133 | |
| 134 void CFWL_Barcode::SetWideNarrowRatio(int32_t ratio) { | |
| 135 m_dwAttributeMask |= FWL_BCDATTRIBUTE_WIDENARROWRATIO; | |
| 136 m_nWideNarrowRatio = ratio; | |
| 137 } | |
| 138 | |
| 139 void CFWL_Barcode::SetStartChar(FX_CHAR startChar) { | |
| 140 m_dwAttributeMask |= FWL_BCDATTRIBUTE_STARTCHAR; | |
| 141 m_cStartChar = startChar; | |
| 142 } | |
| 143 | |
| 144 void CFWL_Barcode::SetEndChar(FX_CHAR endChar) { | |
| 145 m_dwAttributeMask |= FWL_BCDATTRIBUTE_ENDCHAR; | |
| 146 m_cEndChar = endChar; | |
| 147 } | |
| 148 | |
| 149 void CFWL_Barcode::SetErrorCorrectionLevel(int32_t ecLevel) { | |
| 150 m_dwAttributeMask |= FWL_BCDATTRIBUTE_ECLEVEL; | |
| 151 m_nECLevel = ecLevel; | |
| 152 } | |
| 153 | |
| 154 void CFWL_Barcode::SetTruncated(bool truncated) { | |
| 155 m_dwAttributeMask |= FWL_BCDATTRIBUTE_TRUNCATED; | |
| 156 m_bTruncated = truncated; | |
| 157 } | |
| 158 | |
| 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 CFX_WideString wsText = GetText(); | |
| 169 | |
| 170 CFWL_ThemePart part; | |
| 171 part.m_pWidget = this; | |
| 172 | |
| 173 IFWL_ThemeProvider* pTheme = GetAvailableTheme(); | |
| 174 CFGAS_GEFont* pFont = static_cast<CFGAS_GEFont*>( | |
| 175 pTheme->GetCapacity(&part, CFWL_WidgetCapacity::Font)); | |
| 176 CFX_Font* pCXFont = pFont ? pFont->GetDevFont() : nullptr; | |
| 177 if (pCXFont) | |
| 178 m_pBarcodeEngine->SetFont(pCXFont); | |
| 179 | |
| 180 FX_FLOAT* pFontSize = static_cast<FX_FLOAT*>( | |
| 181 pTheme->GetCapacity(&part, CFWL_WidgetCapacity::FontSize)); | |
| 182 if (pFontSize) | |
| 183 m_pBarcodeEngine->SetFontSize(*pFontSize); | |
| 184 | |
| 185 FX_ARGB* pFontColor = static_cast<FX_ARGB*>( | |
| 186 pTheme->GetCapacity(&part, CFWL_WidgetCapacity::TextColor)); | |
| 187 if (pFontColor) | |
| 188 m_pBarcodeEngine->SetFontColor(*pFontColor); | |
| 189 | |
| 190 m_pBarcodeEngine->SetHeight(int32_t(GetRTClient().height)); | |
| 191 m_pBarcodeEngine->SetWidth(int32_t(GetRTClient().width)); | |
| 192 if (m_dwAttributeMask & FWL_BCDATTRIBUTE_CHARENCODING) | |
| 193 m_pBarcodeEngine->SetCharEncoding(m_eCharEncoding); | |
| 194 if (m_dwAttributeMask & FWL_BCDATTRIBUTE_MODULEHEIGHT) | |
| 195 m_pBarcodeEngine->SetModuleHeight(m_nModuleHeight); | |
| 196 if (m_dwAttributeMask & FWL_BCDATTRIBUTE_MODULEWIDTH) | |
| 197 m_pBarcodeEngine->SetModuleWidth(m_nModuleWidth); | |
| 198 if (m_dwAttributeMask & FWL_BCDATTRIBUTE_DATALENGTH) | |
| 199 m_pBarcodeEngine->SetDataLength(m_nDataLength); | |
| 200 if (m_dwAttributeMask & FWL_BCDATTRIBUTE_CALCHECKSUM) | |
| 201 m_pBarcodeEngine->SetCalChecksum(m_bCalChecksum); | |
| 202 if (m_dwAttributeMask & FWL_BCDATTRIBUTE_PRINTCHECKSUM) | |
| 203 m_pBarcodeEngine->SetPrintChecksum(m_bPrintChecksum); | |
| 204 if (m_dwAttributeMask & FWL_BCDATTRIBUTE_TEXTLOCATION) | |
| 205 m_pBarcodeEngine->SetTextLocation(m_eTextLocation); | |
| 206 if (m_dwAttributeMask & FWL_BCDATTRIBUTE_WIDENARROWRATIO) | |
| 207 m_pBarcodeEngine->SetWideNarrowRatio(m_nWideNarrowRatio); | |
| 208 if (m_dwAttributeMask & FWL_BCDATTRIBUTE_STARTCHAR) | |
| 209 m_pBarcodeEngine->SetStartChar(m_cStartChar); | |
| 210 if (m_dwAttributeMask & FWL_BCDATTRIBUTE_ENDCHAR) | |
| 211 m_pBarcodeEngine->SetEndChar(m_cEndChar); | |
| 212 if (m_dwAttributeMask & FWL_BCDATTRIBUTE_VERSION) | |
| 213 m_pBarcodeEngine->SetVersion(0); | |
| 214 if (m_dwAttributeMask & FWL_BCDATTRIBUTE_ECLEVEL) | |
| 215 m_pBarcodeEngine->SetErrorCorrectionLevel(m_nECLevel); | |
| 216 if (m_dwAttributeMask & FWL_BCDATTRIBUTE_TRUNCATED) | |
| 217 m_pBarcodeEngine->SetTruncated(m_bTruncated); | |
| 218 | |
| 219 int32_t errorCode = 0; | |
| 220 m_dwStatus = m_pBarcodeEngine->Encode(wsText.AsStringC(), true, errorCode) | |
| 221 ? XFA_BCS_EncodeSuccess | |
| 222 : 0; | |
| 223 } | |
| 224 | |
| 225 void CFWL_Barcode::CreateBarcodeEngine() { | |
| 226 if (m_pBarcodeEngine || m_type == BC_UNKNOWN) | |
| 227 return; | |
| 228 | |
| 229 std::unique_ptr<CFX_Barcode> pBarcode(new CFX_Barcode); | |
| 230 if (pBarcode->Create(m_type)) | |
| 231 m_pBarcodeEngine = std::move(pBarcode); | |
| 232 } | |
| OLD | NEW |