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 "fxbarcode/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 std::make_unique<CBC_Code39>();
30     case BC_CODABAR:
31       return std::make_unique<CBC_Codabar>();
32     case BC_CODE128:
33       return std::make_unique<CBC_Code128>(BC_CODE128_B);
34     case BC_CODE128_B:
35       return std::make_unique<CBC_Code128>(BC_CODE128_B);
36     case BC_CODE128_C:
37       return std::make_unique<CBC_Code128>(BC_CODE128_C);
38     case BC_EAN8:
39       return std::make_unique<CBC_EAN8>();
40     case BC_UPCA:
41       return std::make_unique<CBC_UPCA>();
42     case BC_EAN13:
43       return std::make_unique<CBC_EAN13>();
44     case BC_QR_CODE:
45       return std::make_unique<CBC_QRCode>();
46     case BC_PDF417:
47       return std::make_unique<CBC_PDF417I>();
48     case BC_DATAMATRIX:
49       return std::make_unique<CBC_DataMatrix>();
50     case BC_UNKNOWN:
51     default:
52       return nullptr;
53   }
54 }
55 
56 }  // namespace
57 
58 CFX_Barcode::CFX_Barcode() = default;
59 
60 CFX_Barcode::~CFX_Barcode() = default;
61 
Create(BC_TYPE type)62 std::unique_ptr<CFX_Barcode> CFX_Barcode::Create(BC_TYPE type) {
63   auto barcode = pdfium::WrapUnique(new CFX_Barcode());  // Private ctor.
64   barcode->m_pBCEngine = CreateBarCodeEngineObject(type);
65   return barcode;
66 }
67 
GetType()68 BC_TYPE CFX_Barcode::GetType() {
69   return m_pBCEngine ? m_pBCEngine->GetType() : BC_UNKNOWN;
70 }
71 
SetCharEncoding(BC_CHAR_ENCODING encoding)72 bool CFX_Barcode::SetCharEncoding(BC_CHAR_ENCODING encoding) {
73   return m_pBCEngine && m_pBCEngine->SetCharEncoding(encoding);
74 }
75 
SetModuleHeight(int32_t moduleHeight)76 bool CFX_Barcode::SetModuleHeight(int32_t moduleHeight) {
77   return m_pBCEngine && m_pBCEngine->SetModuleHeight(moduleHeight);
78 }
79 
SetModuleWidth(int32_t moduleWidth)80 bool CFX_Barcode::SetModuleWidth(int32_t moduleWidth) {
81   return m_pBCEngine && m_pBCEngine->SetModuleWidth(moduleWidth);
82 }
83 
SetHeight(int32_t height)84 bool CFX_Barcode::SetHeight(int32_t height) {
85   return m_pBCEngine && m_pBCEngine->SetHeight(height);
86 }
87 
SetWidth(int32_t width)88 bool CFX_Barcode::SetWidth(int32_t width) {
89   return m_pBCEngine && m_pBCEngine->SetWidth(width);
90 }
91 
SetPrintChecksum(bool checksum)92 bool CFX_Barcode::SetPrintChecksum(bool checksum) {
93   switch (GetType()) {
94     case BC_CODE39:
95     case BC_CODABAR:
96     case BC_CODE128:
97     case BC_CODE128_B:
98     case BC_CODE128_C:
99     case BC_EAN8:
100     case BC_EAN13:
101     case BC_UPCA:
102       return m_pBCEngine ? (static_cast<CBC_OneCode*>(m_pBCEngine.get())
103                                 ->SetPrintChecksum(checksum),
104                             true)
105                          : false;
106     default:
107       return false;
108   }
109 }
110 
SetDataLength(int32_t length)111 bool CFX_Barcode::SetDataLength(int32_t length) {
112   switch (GetType()) {
113     case BC_CODE39:
114     case BC_CODABAR:
115     case BC_CODE128:
116     case BC_CODE128_B:
117     case BC_CODE128_C:
118     case BC_EAN8:
119     case BC_EAN13:
120     case BC_UPCA:
121       return m_pBCEngine ? (static_cast<CBC_OneCode*>(m_pBCEngine.get())
122                                 ->SetDataLength(length),
123                             true)
124                          : false;
125     default:
126       return false;
127   }
128 }
129 
SetCalChecksum(bool state)130 bool CFX_Barcode::SetCalChecksum(bool state) {
131   switch (GetType()) {
132     case BC_CODE39:
133     case BC_CODABAR:
134     case BC_CODE128:
135     case BC_CODE128_B:
136     case BC_CODE128_C:
137     case BC_EAN8:
138     case BC_EAN13:
139     case BC_UPCA:
140       return m_pBCEngine ? (static_cast<CBC_OneCode*>(m_pBCEngine.get())
141                                 ->SetCalChecksum(state),
142                             true)
143                          : false;
144     default:
145       return false;
146   }
147 }
148 
SetFont(CFX_Font * pFont)149 bool CFX_Barcode::SetFont(CFX_Font* pFont) {
150   switch (GetType()) {
151     case BC_CODE39:
152     case BC_CODABAR:
153     case BC_CODE128:
154     case BC_CODE128_B:
155     case BC_CODE128_C:
156     case BC_EAN8:
157     case BC_EAN13:
158     case BC_UPCA:
159       return m_pBCEngine
160                  ? static_cast<CBC_OneCode*>(m_pBCEngine.get())->SetFont(pFont)
161                  : false;
162     default:
163       return false;
164   }
165 }
166 
SetFontSize(float size)167 bool CFX_Barcode::SetFontSize(float size) {
168   switch (GetType()) {
169     case BC_CODE39:
170     case BC_CODABAR:
171     case BC_CODE128:
172     case BC_CODE128_B:
173     case BC_CODE128_C:
174     case BC_EAN8:
175     case BC_EAN13:
176     case BC_UPCA:
177       return m_pBCEngine ? (static_cast<CBC_OneCode*>(m_pBCEngine.get())
178                                 ->SetFontSize(size),
179                             true)
180                          : false;
181     default:
182       return false;
183   }
184 }
185 
SetFontColor(FX_ARGB color)186 bool CFX_Barcode::SetFontColor(FX_ARGB color) {
187   switch (GetType()) {
188     case BC_CODE39:
189     case BC_CODABAR:
190     case BC_CODE128:
191     case BC_CODE128_B:
192     case BC_CODE128_C:
193     case BC_EAN8:
194     case BC_EAN13:
195     case BC_UPCA:
196       return m_pBCEngine ? (static_cast<CBC_OneCode*>(m_pBCEngine.get())
197                                 ->SetFontColor(color),
198                             true)
199                          : false;
200     default:
201       return false;
202   }
203 }
204 
SetTextLocation(BC_TEXT_LOC location)205 bool CFX_Barcode::SetTextLocation(BC_TEXT_LOC location) {
206   return m_pBCEngine && m_pBCEngine->SetTextLocation(location);
207 }
208 
SetWideNarrowRatio(int8_t ratio)209 bool CFX_Barcode::SetWideNarrowRatio(int8_t ratio) {
210   return m_pBCEngine && m_pBCEngine->SetWideNarrowRatio(ratio);
211 }
212 
SetStartChar(char start)213 bool CFX_Barcode::SetStartChar(char start) {
214   return m_pBCEngine && m_pBCEngine->SetStartChar(start);
215 }
216 
SetEndChar(char end)217 bool CFX_Barcode::SetEndChar(char end) {
218   return m_pBCEngine && m_pBCEngine->SetEndChar(end);
219 }
220 
SetErrorCorrectionLevel(int32_t level)221 bool CFX_Barcode::SetErrorCorrectionLevel(int32_t level) {
222   return m_pBCEngine && m_pBCEngine->SetErrorCorrectionLevel(level);
223 }
224 
Encode(WideStringView contents)225 bool CFX_Barcode::Encode(WideStringView contents) {
226   return m_pBCEngine && m_pBCEngine->Encode(contents);
227 }
228 
RenderDevice(CFX_RenderDevice * device,const CFX_Matrix * matrix)229 bool CFX_Barcode::RenderDevice(CFX_RenderDevice* device,
230                                const CFX_Matrix* matrix) {
231   return m_pBCEngine && m_pBCEngine->RenderDevice(device, matrix);
232 }
233