• 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/cfx_barcode.h"
8 
9 #include <memory>
10 
11 #include "fxbarcode/cbc_codabar.h"
12 #include "fxbarcode/cbc_code128.h"
13 #include "fxbarcode/cbc_code39.h"
14 #include "fxbarcode/cbc_codebase.h"
15 #include "fxbarcode/cbc_datamatrix.h"
16 #include "fxbarcode/cbc_ean13.h"
17 #include "fxbarcode/cbc_ean8.h"
18 #include "fxbarcode/cbc_pdf417i.h"
19 #include "fxbarcode/cbc_qrcode.h"
20 #include "fxbarcode/cbc_upca.h"
21 #include "fxbarcode/utils.h"
22 #include "third_party/base/ptr_util.h"
23 
24 namespace {
25 
CreateBarCodeEngineObject(BC_TYPE type)26 std::unique_ptr<CBC_CodeBase> CreateBarCodeEngineObject(BC_TYPE type) {
27   switch (type) {
28     case BC_CODE39:
29       return pdfium::MakeUnique<CBC_Code39>();
30     case BC_CODABAR:
31       return pdfium::MakeUnique<CBC_Codabar>();
32     case BC_CODE128:
33       return pdfium::MakeUnique<CBC_Code128>(BC_CODE128_B);
34     case BC_CODE128_B:
35       return pdfium::MakeUnique<CBC_Code128>(BC_CODE128_B);
36     case BC_CODE128_C:
37       return pdfium::MakeUnique<CBC_Code128>(BC_CODE128_C);
38     case BC_EAN8:
39       return pdfium::MakeUnique<CBC_EAN8>();
40     case BC_UPCA:
41       return pdfium::MakeUnique<CBC_UPCA>();
42     case BC_EAN13:
43       return pdfium::MakeUnique<CBC_EAN13>();
44     case BC_QR_CODE:
45       return pdfium::MakeUnique<CBC_QRCode>();
46     case BC_PDF417:
47       return pdfium::MakeUnique<CBC_PDF417I>();
48     case BC_DATAMATRIX:
49       return pdfium::MakeUnique<CBC_DataMatrix>();
50     case BC_UNKNOWN:
51     default:
52       return nullptr;
53   }
54 }
55 
56 }  // namespace
57 
CFX_Barcode()58 CFX_Barcode::CFX_Barcode() {}
59 
~CFX_Barcode()60 CFX_Barcode::~CFX_Barcode() {}
61 
Create(BC_TYPE type)62 bool CFX_Barcode::Create(BC_TYPE type) {
63   m_pBCEngine = CreateBarCodeEngineObject(type);
64   return !!m_pBCEngine;
65 }
66 
GetType()67 BC_TYPE CFX_Barcode::GetType() {
68   return m_pBCEngine ? m_pBCEngine->GetType() : BC_UNKNOWN;
69 }
70 
SetCharEncoding(BC_CHAR_ENCODING encoding)71 bool CFX_Barcode::SetCharEncoding(BC_CHAR_ENCODING encoding) {
72   return m_pBCEngine ? m_pBCEngine->SetCharEncoding(encoding) : false;
73 }
74 
SetModuleHeight(int32_t moduleHeight)75 bool CFX_Barcode::SetModuleHeight(int32_t moduleHeight) {
76   return m_pBCEngine ? m_pBCEngine->SetModuleHeight(moduleHeight) : false;
77 }
78 
SetModuleWidth(int32_t moduleWidth)79 bool CFX_Barcode::SetModuleWidth(int32_t moduleWidth) {
80   return m_pBCEngine ? m_pBCEngine->SetModuleWidth(moduleWidth) : false;
81 }
82 
SetHeight(int32_t height)83 bool CFX_Barcode::SetHeight(int32_t height) {
84   return m_pBCEngine ? m_pBCEngine->SetHeight(height) : false;
85 }
86 
SetWidth(int32_t width)87 bool CFX_Barcode::SetWidth(int32_t width) {
88   return m_pBCEngine ? m_pBCEngine->SetWidth(width) : false;
89 }
90 
SetPrintChecksum(bool checksum)91 bool CFX_Barcode::SetPrintChecksum(bool checksum) {
92   switch (GetType()) {
93     case BC_CODE39:
94     case BC_CODABAR:
95     case BC_CODE128:
96     case BC_CODE128_B:
97     case BC_CODE128_C:
98     case BC_EAN8:
99     case BC_EAN13:
100     case BC_UPCA:
101       return m_pBCEngine ? (static_cast<CBC_OneCode*>(m_pBCEngine.get())
102                                 ->SetPrintChecksum(checksum),
103                             true)
104                          : false;
105     default:
106       return false;
107   }
108 }
109 
SetDataLength(int32_t length)110 bool CFX_Barcode::SetDataLength(int32_t length) {
111   switch (GetType()) {
112     case BC_CODE39:
113     case BC_CODABAR:
114     case BC_CODE128:
115     case BC_CODE128_B:
116     case BC_CODE128_C:
117     case BC_EAN8:
118     case BC_EAN13:
119     case BC_UPCA:
120       return m_pBCEngine ? (static_cast<CBC_OneCode*>(m_pBCEngine.get())
121                                 ->SetDataLength(length),
122                             true)
123                          : false;
124     default:
125       return false;
126   }
127 }
128 
SetCalChecksum(bool state)129 bool CFX_Barcode::SetCalChecksum(bool state) {
130   switch (GetType()) {
131     case BC_CODE39:
132     case BC_CODABAR:
133     case BC_CODE128:
134     case BC_CODE128_B:
135     case BC_CODE128_C:
136     case BC_EAN8:
137     case BC_EAN13:
138     case BC_UPCA:
139       return m_pBCEngine ? (static_cast<CBC_OneCode*>(m_pBCEngine.get())
140                                 ->SetCalChecksum(state),
141                             true)
142                          : false;
143     default:
144       return false;
145   }
146 }
147 
SetFont(CFX_Font * pFont)148 bool CFX_Barcode::SetFont(CFX_Font* pFont) {
149   switch (GetType()) {
150     case BC_CODE39:
151     case BC_CODABAR:
152     case BC_CODE128:
153     case BC_CODE128_B:
154     case BC_CODE128_C:
155     case BC_EAN8:
156     case BC_EAN13:
157     case BC_UPCA:
158       return m_pBCEngine
159                  ? static_cast<CBC_OneCode*>(m_pBCEngine.get())->SetFont(pFont)
160                  : false;
161     default:
162       return false;
163   }
164 }
165 
SetFontSize(float size)166 bool CFX_Barcode::SetFontSize(float size) {
167   switch (GetType()) {
168     case BC_CODE39:
169     case BC_CODABAR:
170     case BC_CODE128:
171     case BC_CODE128_B:
172     case BC_CODE128_C:
173     case BC_EAN8:
174     case BC_EAN13:
175     case BC_UPCA:
176       return m_pBCEngine ? (static_cast<CBC_OneCode*>(m_pBCEngine.get())
177                                 ->SetFontSize(size),
178                             true)
179                          : false;
180     default:
181       return false;
182   }
183 }
184 
SetFontColor(FX_ARGB color)185 bool CFX_Barcode::SetFontColor(FX_ARGB color) {
186   switch (GetType()) {
187     case BC_CODE39:
188     case BC_CODABAR:
189     case BC_CODE128:
190     case BC_CODE128_B:
191     case BC_CODE128_C:
192     case BC_EAN8:
193     case BC_EAN13:
194     case BC_UPCA:
195       return m_pBCEngine ? (static_cast<CBC_OneCode*>(m_pBCEngine.get())
196                                 ->SetFontColor(color),
197                             true)
198                          : false;
199     default:
200       return false;
201   }
202 }
203 
SetTextLocation(BC_TEXT_LOC location)204 bool CFX_Barcode::SetTextLocation(BC_TEXT_LOC location) {
205   typedef bool (CBC_CodeBase::*memptrtype)(BC_TEXT_LOC);
206   memptrtype memptr = nullptr;
207   switch (GetType()) {
208     case BC_CODE39:
209       memptr = (memptrtype)&CBC_Code39::SetTextLocation;
210       break;
211     case BC_CODABAR:
212       memptr = (memptrtype)&CBC_Codabar::SetTextLocation;
213       break;
214     case BC_CODE128:
215     case BC_CODE128_B:
216     case BC_CODE128_C:
217       memptr = (memptrtype)&CBC_Code128::SetTextLocation;
218       break;
219     default:
220       break;
221   }
222   return m_pBCEngine && memptr ? (m_pBCEngine.get()->*memptr)(location) : false;
223 }
224 
SetWideNarrowRatio(int8_t ratio)225 bool CFX_Barcode::SetWideNarrowRatio(int8_t ratio) {
226   typedef bool (CBC_CodeBase::*memptrtype)(int8_t);
227   memptrtype memptr = nullptr;
228   switch (GetType()) {
229     case BC_CODE39:
230       memptr = (memptrtype)&CBC_Code39::SetWideNarrowRatio;
231       break;
232     case BC_CODABAR:
233       memptr = (memptrtype)&CBC_Codabar::SetWideNarrowRatio;
234       break;
235     default:
236       break;
237   }
238   return m_pBCEngine && memptr ? (m_pBCEngine.get()->*memptr)(ratio) : false;
239 }
240 
SetStartChar(char start)241 bool CFX_Barcode::SetStartChar(char start) {
242   typedef bool (CBC_CodeBase::*memptrtype)(char);
243   memptrtype memptr = nullptr;
244   switch (GetType()) {
245     case BC_CODABAR:
246       memptr = (memptrtype)&CBC_Codabar::SetStartChar;
247       break;
248     default:
249       break;
250   }
251   return m_pBCEngine && memptr ? (m_pBCEngine.get()->*memptr)(start) : false;
252 }
253 
SetEndChar(char end)254 bool CFX_Barcode::SetEndChar(char end) {
255   typedef bool (CBC_CodeBase::*memptrtype)(char);
256   memptrtype memptr = nullptr;
257   switch (GetType()) {
258     case BC_CODABAR:
259       memptr = (memptrtype)&CBC_Codabar::SetEndChar;
260       break;
261     default:
262       break;
263   }
264   return m_pBCEngine && memptr ? (m_pBCEngine.get()->*memptr)(end) : false;
265 }
266 
SetErrorCorrectionLevel(int32_t level)267 bool CFX_Barcode::SetErrorCorrectionLevel(int32_t level) {
268   typedef bool (CBC_CodeBase::*memptrtype)(int32_t);
269   memptrtype memptr = nullptr;
270   switch (GetType()) {
271     case BC_QR_CODE:
272       memptr = (memptrtype)&CBC_QRCode::SetErrorCorrectionLevel;
273       break;
274     case BC_PDF417:
275       memptr = (memptrtype)&CBC_PDF417I::SetErrorCorrectionLevel;
276       break;
277     default:
278       return false;
279   }
280   return m_pBCEngine && memptr ? (m_pBCEngine.get()->*memptr)(level) : false;
281 }
282 
SetTruncated(bool truncated)283 bool CFX_Barcode::SetTruncated(bool truncated) {
284   typedef void (CBC_CodeBase::*memptrtype)(bool);
285   memptrtype memptr = nullptr;
286   switch (GetType()) {
287     case BC_PDF417:
288       memptr = (memptrtype)&CBC_PDF417I::SetTruncated;
289       break;
290     default:
291       break;
292   }
293   return m_pBCEngine && memptr ? ((m_pBCEngine.get()->*memptr)(truncated), true)
294                                : false;
295 }
296 
Encode(const WideStringView & contents)297 bool CFX_Barcode::Encode(const WideStringView& contents) {
298   return m_pBCEngine && m_pBCEngine->Encode(contents);
299 }
300 
RenderDevice(CFX_RenderDevice * device,const CFX_Matrix * matrix)301 bool CFX_Barcode::RenderDevice(CFX_RenderDevice* device,
302                                const CFX_Matrix* matrix) {
303   return m_pBCEngine && m_pBCEngine->RenderDevice(device, matrix);
304 }
305