• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/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/cfwl_notedriver.h"
14 #include "xfa/fwl/cfwl_themepart.h"
15 #include "xfa/fwl/cfx_barcode.h"
16 #include "xfa/fwl/ifwl_themeprovider.h"
17 #include "xfa/fwl/theme/cfwl_utils.h"
18 
CFWL_Barcode(const CFWL_App * app)19 CFWL_Barcode::CFWL_Barcode(const CFWL_App* app)
20     : CFWL_Edit(app, pdfium::MakeUnique<CFWL_WidgetProperties>(), nullptr),
21       m_dwStatus(0),
22       m_type(BC_UNKNOWN),
23       m_dwAttributeMask(FWL_BCDATTRIBUTE_NONE) {}
24 
~CFWL_Barcode()25 CFWL_Barcode::~CFWL_Barcode() {}
26 
GetClassID() const27 FWL_Type CFWL_Barcode::GetClassID() const {
28   return FWL_Type::Barcode;
29 }
30 
Update()31 void CFWL_Barcode::Update() {
32   if (IsLocked())
33     return;
34 
35   CFWL_Edit::Update();
36   GenerateBarcodeImageCache();
37 }
38 
DrawWidget(CXFA_Graphics * pGraphics,const CFX_Matrix & matrix)39 void CFWL_Barcode::DrawWidget(CXFA_Graphics* pGraphics,
40                               const CFX_Matrix& matrix) {
41   if (!pGraphics)
42     return;
43   if (!m_pProperties->m_pThemeProvider)
44     return;
45   if ((m_pProperties->m_dwStates & FWL_WGTSTATE_Focused) == 0) {
46     GenerateBarcodeImageCache();
47     if (!m_pBarcodeEngine || (m_dwStatus & XFA_BCS_EncodeSuccess) == 0)
48       return;
49 
50     CFX_Matrix mt;
51     mt.e = GetRTClient().left;
52     mt.f = GetRTClient().top;
53     mt.Concat(matrix);
54 
55     m_pBarcodeEngine->RenderDevice(pGraphics->GetRenderDevice(), &matrix);
56     return;
57   }
58   CFWL_Edit::DrawWidget(pGraphics, matrix);
59 }
60 
SetType(BC_TYPE type)61 void CFWL_Barcode::SetType(BC_TYPE type) {
62   if (m_type == type)
63     return;
64 
65   m_pBarcodeEngine.reset();
66   m_type = type;
67   m_dwStatus = XFA_BCS_NeedUpdate;
68 }
69 
SetText(const WideString & wsText)70 void CFWL_Barcode::SetText(const WideString& wsText) {
71   m_pBarcodeEngine.reset();
72   m_dwStatus = XFA_BCS_NeedUpdate;
73   CFWL_Edit::SetText(wsText);
74 }
75 
IsProtectedType() const76 bool CFWL_Barcode::IsProtectedType() const {
77   if (!m_pBarcodeEngine)
78     return true;
79 
80   BC_TYPE tEngineType = m_pBarcodeEngine->GetType();
81   if (tEngineType == BC_QR_CODE || tEngineType == BC_PDF417 ||
82       tEngineType == BC_DATAMATRIX) {
83     return true;
84   }
85   return false;
86 }
87 
OnProcessEvent(CFWL_Event * pEvent)88 void CFWL_Barcode::OnProcessEvent(CFWL_Event* pEvent) {
89   if (pEvent->GetType() == CFWL_Event::Type::TextChanged) {
90     m_pBarcodeEngine.reset();
91     m_dwStatus = XFA_BCS_NeedUpdate;
92   }
93   CFWL_Edit::OnProcessEvent(pEvent);
94 }
95 
SetCharEncoding(BC_CHAR_ENCODING encoding)96 void CFWL_Barcode::SetCharEncoding(BC_CHAR_ENCODING encoding) {
97   m_dwAttributeMask |= FWL_BCDATTRIBUTE_CHARENCODING;
98   m_eCharEncoding = encoding;
99 }
100 
SetModuleHeight(int32_t height)101 void CFWL_Barcode::SetModuleHeight(int32_t height) {
102   m_dwAttributeMask |= FWL_BCDATTRIBUTE_MODULEHEIGHT;
103   m_nModuleHeight = height;
104 }
105 
SetModuleWidth(int32_t width)106 void CFWL_Barcode::SetModuleWidth(int32_t width) {
107   m_dwAttributeMask |= FWL_BCDATTRIBUTE_MODULEWIDTH;
108   m_nModuleWidth = width;
109 }
110 
SetDataLength(int32_t dataLength)111 void CFWL_Barcode::SetDataLength(int32_t dataLength) {
112   m_dwAttributeMask |= FWL_BCDATTRIBUTE_DATALENGTH;
113   m_nDataLength = dataLength;
114   SetLimit(dataLength);
115 }
116 
SetCalChecksum(bool calChecksum)117 void CFWL_Barcode::SetCalChecksum(bool calChecksum) {
118   m_dwAttributeMask |= FWL_BCDATTRIBUTE_CALCHECKSUM;
119   m_bCalChecksum = calChecksum;
120 }
121 
SetPrintChecksum(bool printChecksum)122 void CFWL_Barcode::SetPrintChecksum(bool printChecksum) {
123   m_dwAttributeMask |= FWL_BCDATTRIBUTE_PRINTCHECKSUM;
124   m_bPrintChecksum = printChecksum;
125 }
126 
SetTextLocation(BC_TEXT_LOC location)127 void CFWL_Barcode::SetTextLocation(BC_TEXT_LOC location) {
128   m_dwAttributeMask |= FWL_BCDATTRIBUTE_TEXTLOCATION;
129   m_eTextLocation = location;
130 }
131 
SetWideNarrowRatio(int8_t ratio)132 void CFWL_Barcode::SetWideNarrowRatio(int8_t ratio) {
133   m_dwAttributeMask |= FWL_BCDATTRIBUTE_WIDENARROWRATIO;
134   m_nWideNarrowRatio = ratio;
135 }
136 
SetStartChar(char startChar)137 void CFWL_Barcode::SetStartChar(char startChar) {
138   m_dwAttributeMask |= FWL_BCDATTRIBUTE_STARTCHAR;
139   m_cStartChar = startChar;
140 }
141 
SetEndChar(char endChar)142 void CFWL_Barcode::SetEndChar(char endChar) {
143   m_dwAttributeMask |= FWL_BCDATTRIBUTE_ENDCHAR;
144   m_cEndChar = endChar;
145 }
146 
SetErrorCorrectionLevel(int32_t ecLevel)147 void CFWL_Barcode::SetErrorCorrectionLevel(int32_t ecLevel) {
148   m_dwAttributeMask |= FWL_BCDATTRIBUTE_ECLEVEL;
149   m_nECLevel = ecLevel;
150 }
151 
SetTruncated(bool truncated)152 void CFWL_Barcode::SetTruncated(bool truncated) {
153   m_dwAttributeMask |= FWL_BCDATTRIBUTE_TRUNCATED;
154   m_bTruncated = truncated;
155 }
156 
GenerateBarcodeImageCache()157 void CFWL_Barcode::GenerateBarcodeImageCache() {
158   if ((m_dwStatus & XFA_BCS_NeedUpdate) == 0)
159     return;
160 
161   m_dwStatus = 0;
162   CreateBarcodeEngine();
163   if (!m_pBarcodeEngine)
164     return;
165 
166   IFWL_ThemeProvider* pTheme = GetAvailableTheme();
167   if (pTheme) {
168     CFWL_ThemePart part;
169     part.m_pWidget = this;
170     if (RetainPtr<CFGAS_GEFont> pFont = pTheme->GetFont(&part)) {
171       if (CFX_Font* pCXFont = pFont->GetDevFont())
172         m_pBarcodeEngine->SetFont(pCXFont);
173     }
174     m_pBarcodeEngine->SetFontSize(pTheme->GetFontSize(&part));
175     m_pBarcodeEngine->SetFontColor(pTheme->GetTextColor(&part));
176   } else {
177     m_pBarcodeEngine->SetFontSize(FWLTHEME_CAPACITY_FontSize);
178   }
179 
180   m_pBarcodeEngine->SetHeight(int32_t(GetRTClient().height));
181   m_pBarcodeEngine->SetWidth(int32_t(GetRTClient().width));
182   if (m_dwAttributeMask & FWL_BCDATTRIBUTE_CHARENCODING)
183     m_pBarcodeEngine->SetCharEncoding(m_eCharEncoding);
184   if (m_dwAttributeMask & FWL_BCDATTRIBUTE_MODULEHEIGHT)
185     m_pBarcodeEngine->SetModuleHeight(m_nModuleHeight);
186   if (m_dwAttributeMask & FWL_BCDATTRIBUTE_MODULEWIDTH)
187     m_pBarcodeEngine->SetModuleWidth(m_nModuleWidth);
188   if (m_dwAttributeMask & FWL_BCDATTRIBUTE_DATALENGTH)
189     m_pBarcodeEngine->SetDataLength(m_nDataLength);
190   if (m_dwAttributeMask & FWL_BCDATTRIBUTE_CALCHECKSUM)
191     m_pBarcodeEngine->SetCalChecksum(m_bCalChecksum);
192   if (m_dwAttributeMask & FWL_BCDATTRIBUTE_PRINTCHECKSUM)
193     m_pBarcodeEngine->SetPrintChecksum(m_bPrintChecksum);
194   if (m_dwAttributeMask & FWL_BCDATTRIBUTE_TEXTLOCATION)
195     m_pBarcodeEngine->SetTextLocation(m_eTextLocation);
196   if (m_dwAttributeMask & FWL_BCDATTRIBUTE_WIDENARROWRATIO)
197     m_pBarcodeEngine->SetWideNarrowRatio(m_nWideNarrowRatio);
198   if (m_dwAttributeMask & FWL_BCDATTRIBUTE_STARTCHAR)
199     m_pBarcodeEngine->SetStartChar(m_cStartChar);
200   if (m_dwAttributeMask & FWL_BCDATTRIBUTE_ENDCHAR)
201     m_pBarcodeEngine->SetEndChar(m_cEndChar);
202   if (m_dwAttributeMask & FWL_BCDATTRIBUTE_ECLEVEL)
203     m_pBarcodeEngine->SetErrorCorrectionLevel(m_nECLevel);
204   if (m_dwAttributeMask & FWL_BCDATTRIBUTE_TRUNCATED)
205     m_pBarcodeEngine->SetTruncated(m_bTruncated);
206 
207   m_dwStatus = m_pBarcodeEngine->Encode(GetText().AsStringView())
208                    ? XFA_BCS_EncodeSuccess
209                    : 0;
210 }
211 
CreateBarcodeEngine()212 void CFWL_Barcode::CreateBarcodeEngine() {
213   if (m_pBarcodeEngine || m_type == BC_UNKNOWN)
214     return;
215 
216   auto pBarcode = pdfium::MakeUnique<CFX_Barcode>();
217   if (pBarcode->Create(m_type))
218     m_pBarcodeEngine = std::move(pBarcode);
219 }
220