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/src/fwl/basewidget/fxmath_barcodeimp.h" | |
8 | |
9 static CBC_CodeBase* FX_Barcode_CreateBarCodeEngineObject(BC_TYPE type) { | |
10 switch (type) { | |
11 case BC_CODE39: | |
12 return new CBC_Code39(); | |
13 case BC_CODABAR: | |
14 return new CBC_Codabar(); | |
15 case BC_CODE128: | |
16 return new CBC_Code128(BC_CODE128_B); | |
17 case BC_CODE128_B: | |
18 return new CBC_Code128(BC_CODE128_B); | |
19 case BC_CODE128_C: | |
20 return new CBC_Code128(BC_CODE128_C); | |
21 case BC_EAN8: | |
22 return new CBC_EAN8(); | |
23 case BC_UPCA: | |
24 return new CBC_UPCA(); | |
25 case BC_EAN13: | |
26 return new CBC_EAN13(); | |
27 case BC_QR_CODE: | |
28 return new CBC_QRCode(); | |
29 case BC_PDF417: | |
30 return new CBC_PDF417I(); | |
31 case BC_DATAMATRIX: | |
32 return new CBC_DataMatrix(); | |
33 case BC_UNKNOWN: | |
34 default: | |
35 return NULL; | |
36 } | |
37 } | |
38 CFX_Barcode::CFX_Barcode() {} | |
39 CFX_Barcode::~CFX_Barcode() { | |
40 if (m_pBCEngine) { | |
41 delete m_pBCEngine; | |
42 m_pBCEngine = NULL; | |
43 } | |
44 } | |
45 FX_BOOL CFX_Barcode::Crreate(BC_TYPE type) { | |
46 m_pBCEngine = FX_Barcode_CreateBarCodeEngineObject(type); | |
47 return m_pBCEngine != NULL; | |
48 } | |
49 void CFX_Barcode::Release() { | |
50 delete this; | |
51 } | |
52 BC_TYPE CFX_Barcode::GetType() { | |
53 return m_pBCEngine ? m_pBCEngine->GetType() : BC_UNKNOWN; | |
54 } | |
55 FX_BOOL CFX_Barcode::SetCharEncoding(BC_CHAR_ENCODING encoding) { | |
56 return m_pBCEngine ? m_pBCEngine->SetCharEncoding(encoding) : FALSE; | |
57 } | |
58 FX_BOOL CFX_Barcode::SetModuleHeight(int32_t moduleHeight) { | |
59 return m_pBCEngine ? m_pBCEngine->SetModuleHeight(moduleHeight) : FALSE; | |
60 } | |
61 FX_BOOL CFX_Barcode::SetModuleWidth(int32_t moduleWidth) { | |
62 return m_pBCEngine ? m_pBCEngine->SetModuleWidth(moduleWidth) : FALSE; | |
63 } | |
64 FX_BOOL CFX_Barcode::SetHeight(int32_t height) { | |
65 return m_pBCEngine ? m_pBCEngine->SetHeight(height) : FALSE; | |
66 } | |
67 FX_BOOL CFX_Barcode::SetWidth(int32_t width) { | |
68 return m_pBCEngine ? m_pBCEngine->SetWidth(width) : FALSE; | |
69 } | |
70 FX_BOOL CFX_Barcode::CheckContentValidity(const CFX_WideStringC& contents) { | |
71 switch (GetType()) { | |
72 case BC_CODE39: | |
73 case BC_CODABAR: | |
74 case BC_CODE128: | |
75 case BC_CODE128_B: | |
76 case BC_CODE128_C: | |
77 case BC_EAN8: | |
78 case BC_EAN13: | |
79 case BC_UPCA: | |
80 return m_pBCEngine | |
81 ? static_cast<CBC_OneCode*>(m_pBCEngine) | |
82 ->CheckContentValidity(contents) | |
83 : TRUE; | |
84 default: | |
85 return TRUE; | |
86 } | |
87 } | |
88 FX_BOOL CFX_Barcode::SetPrintChecksum(FX_BOOL checksum) { | |
89 switch (GetType()) { | |
90 case BC_CODE39: | |
91 case BC_CODABAR: | |
92 case BC_CODE128: | |
93 case BC_CODE128_B: | |
94 case BC_CODE128_C: | |
95 case BC_EAN8: | |
96 case BC_EAN13: | |
97 case BC_UPCA: | |
98 return m_pBCEngine ? (static_cast<CBC_OneCode*>(m_pBCEngine) | |
99 ->SetPrintChecksum(checksum), | |
100 TRUE) | |
101 : FALSE; | |
102 default: | |
103 return FALSE; | |
104 } | |
105 } | |
106 FX_BOOL CFX_Barcode::SetDataLength(int32_t length) { | |
107 switch (GetType()) { | |
108 case BC_CODE39: | |
109 case BC_CODABAR: | |
110 case BC_CODE128: | |
111 case BC_CODE128_B: | |
112 case BC_CODE128_C: | |
113 case BC_EAN8: | |
114 case BC_EAN13: | |
115 case BC_UPCA: | |
116 return m_pBCEngine ? (static_cast<CBC_OneCode*>(m_pBCEngine) | |
117 ->SetDataLength(length), | |
118 TRUE) | |
119 : FALSE; | |
120 default: | |
121 return FALSE; | |
122 } | |
123 } | |
124 FX_BOOL CFX_Barcode::SetCalChecksum(int32_t state) { | |
125 switch (GetType()) { | |
126 case BC_CODE39: | |
127 case BC_CODABAR: | |
128 case BC_CODE128: | |
129 case BC_CODE128_B: | |
130 case BC_CODE128_C: | |
131 case BC_EAN8: | |
132 case BC_EAN13: | |
133 case BC_UPCA: | |
134 return m_pBCEngine ? (static_cast<CBC_OneCode*>(m_pBCEngine) | |
135 ->SetCalChecksum(state), | |
136 TRUE) | |
137 : FALSE; | |
138 default: | |
139 return FALSE; | |
140 } | |
141 } | |
142 FX_BOOL CFX_Barcode::SetFont(CFX_Font* pFont) { | |
143 switch (GetType()) { | |
144 case BC_CODE39: | |
145 case BC_CODABAR: | |
146 case BC_CODE128: | |
147 case BC_CODE128_B: | |
148 case BC_CODE128_C: | |
149 case BC_EAN8: | |
150 case BC_EAN13: | |
151 case BC_UPCA: | |
152 return m_pBCEngine | |
153 ? static_cast<CBC_OneCode*>(m_pBCEngine)->SetFont(pFont) | |
154 : FALSE; | |
155 default: | |
156 return FALSE; | |
157 } | |
158 } | |
159 FX_BOOL CFX_Barcode::SetFontSize(FX_FLOAT size) { | |
160 switch (GetType()) { | |
161 case BC_CODE39: | |
162 case BC_CODABAR: | |
163 case BC_CODE128: | |
164 case BC_CODE128_B: | |
165 case BC_CODE128_C: | |
166 case BC_EAN8: | |
167 case BC_EAN13: | |
168 case BC_UPCA: | |
169 return m_pBCEngine | |
170 ? (static_cast<CBC_OneCode*>(m_pBCEngine)->SetFontSize(size), | |
171 TRUE) | |
172 : FALSE; | |
173 default: | |
174 return FALSE; | |
175 } | |
176 } | |
177 FX_BOOL CFX_Barcode::SetFontStyle(int32_t style) { | |
178 switch (GetType()) { | |
179 case BC_CODE39: | |
180 case BC_CODABAR: | |
181 case BC_CODE128: | |
182 case BC_CODE128_B: | |
183 case BC_CODE128_C: | |
184 case BC_EAN8: | |
185 case BC_EAN13: | |
186 case BC_UPCA: | |
187 return m_pBCEngine | |
188 ? (static_cast<CBC_OneCode*>(m_pBCEngine)->SetFontStyle(style), | |
189 TRUE) | |
190 : FALSE; | |
191 default: | |
192 return FALSE; | |
193 } | |
194 } | |
195 FX_BOOL CFX_Barcode::SetFontColor(FX_ARGB color) { | |
196 switch (GetType()) { | |
197 case BC_CODE39: | |
198 case BC_CODABAR: | |
199 case BC_CODE128: | |
200 case BC_CODE128_B: | |
201 case BC_CODE128_C: | |
202 case BC_EAN8: | |
203 case BC_EAN13: | |
204 case BC_UPCA: | |
205 return m_pBCEngine | |
206 ? (static_cast<CBC_OneCode*>(m_pBCEngine)->SetFontColor(color), | |
207 TRUE) | |
208 : FALSE; | |
209 default: | |
210 return FALSE; | |
211 } | |
212 } | |
213 FX_BOOL CFX_Barcode::SetTextLocation(BC_TEXT_LOC location) { | |
214 typedef FX_BOOL (CBC_CodeBase::*memptrtype)(BC_TEXT_LOC); | |
215 memptrtype memptr = NULL; | |
216 switch (GetType()) { | |
217 case BC_CODE39: | |
218 memptr = (memptrtype)&CBC_Code39::SetTextLocation; | |
219 break; | |
220 case BC_CODABAR: | |
221 memptr = (memptrtype)&CBC_Codabar::SetTextLocation; | |
222 break; | |
223 case BC_CODE128: | |
224 case BC_CODE128_B: | |
225 case BC_CODE128_C: | |
226 memptr = (memptrtype)&CBC_Code128::SetTextLocation; | |
227 break; | |
228 default: | |
229 break; | |
230 } | |
231 return m_pBCEngine && memptr ? (m_pBCEngine->*memptr)(location) : FALSE; | |
232 } | |
233 FX_BOOL CFX_Barcode::SetWideNarrowRatio(int32_t ratio) { | |
234 typedef FX_BOOL (CBC_CodeBase::*memptrtype)(int32_t); | |
235 memptrtype memptr = NULL; | |
236 switch (GetType()) { | |
237 case BC_CODE39: | |
238 memptr = (memptrtype)&CBC_Code39::SetWideNarrowRatio; | |
239 break; | |
240 case BC_CODABAR: | |
241 memptr = (memptrtype)&CBC_Codabar::SetWideNarrowRatio; | |
242 break; | |
243 default: | |
244 break; | |
245 } | |
246 return m_pBCEngine && memptr ? (m_pBCEngine->*memptr)(ratio) : FALSE; | |
247 } | |
248 FX_BOOL CFX_Barcode::SetStartChar(FX_CHAR start) { | |
249 typedef FX_BOOL (CBC_CodeBase::*memptrtype)(FX_CHAR); | |
250 memptrtype memptr = NULL; | |
251 switch (GetType()) { | |
252 case BC_CODABAR: | |
253 memptr = (memptrtype)&CBC_Codabar::SetStartChar; | |
254 break; | |
255 default: | |
256 break; | |
257 } | |
258 return m_pBCEngine && memptr ? (m_pBCEngine->*memptr)(start) : FALSE; | |
259 } | |
260 FX_BOOL CFX_Barcode::SetEndChar(FX_CHAR end) { | |
261 typedef FX_BOOL (CBC_CodeBase::*memptrtype)(FX_CHAR); | |
262 memptrtype memptr = NULL; | |
263 switch (GetType()) { | |
264 case BC_CODABAR: | |
265 memptr = (memptrtype)&CBC_Codabar::SetEndChar; | |
266 break; | |
267 default: | |
268 break; | |
269 } | |
270 return m_pBCEngine && memptr ? (m_pBCEngine->*memptr)(end) : FALSE; | |
271 } | |
272 FX_BOOL CFX_Barcode::SetVersion(int32_t version) { | |
273 typedef FX_BOOL (CBC_CodeBase::*memptrtype)(int32_t); | |
274 memptrtype memptr = NULL; | |
275 switch (GetType()) { | |
276 case BC_QR_CODE: | |
277 memptr = (memptrtype)&CBC_QRCode::SetVersion; | |
278 break; | |
279 default: | |
280 break; | |
281 } | |
282 return m_pBCEngine && memptr ? (m_pBCEngine->*memptr)(version) : FALSE; | |
283 } | |
284 FX_BOOL CFX_Barcode::SetErrorCorrectionLevel(int32_t level) { | |
285 typedef FX_BOOL (CBC_CodeBase::*memptrtype)(int32_t); | |
286 memptrtype memptr = NULL; | |
287 switch (GetType()) { | |
288 case BC_QR_CODE: | |
289 memptr = (memptrtype)&CBC_QRCode::SetErrorCorrectionLevel; | |
290 break; | |
291 case BC_PDF417: | |
292 memptr = (memptrtype)&CBC_PDF417I::SetErrorCorrectionLevel; | |
293 break; | |
294 default: | |
295 return FALSE; | |
296 } | |
297 return m_pBCEngine && memptr ? (m_pBCEngine->*memptr)(level) : FALSE; | |
298 } | |
299 FX_BOOL CFX_Barcode::SetTruncated(FX_BOOL truncated) { | |
300 typedef void (CBC_CodeBase::*memptrtype)(FX_BOOL); | |
301 memptrtype memptr = NULL; | |
302 switch (GetType()) { | |
303 case BC_PDF417: | |
304 memptr = (memptrtype)&CBC_PDF417I::SetTruncated; | |
305 break; | |
306 default: | |
307 break; | |
308 } | |
309 return m_pBCEngine && memptr ? ((m_pBCEngine->*memptr)(truncated), TRUE) | |
310 : FALSE; | |
311 } | |
312 #ifndef BCExceptionNO | |
313 #define BCExceptionNO 0 | |
314 #endif | |
315 #ifndef BCExceptionFormatException | |
316 #define BCExceptionFormatException 8 | |
317 #endif | |
318 #ifndef BCExceptionUnSupportedBarcode | |
319 #define BCExceptionUnSupportedBarcode 18 | |
320 #endif | |
321 FX_BOOL CFX_Barcode::Encode(const CFX_WideStringC& contents, | |
322 FX_BOOL isDevice, | |
323 int32_t& e) { | |
324 if (!m_pBCEngine) { | |
325 return FALSE; | |
326 } | |
327 return m_pBCEngine->Encode(contents, isDevice, e); | |
328 } | |
329 FX_BOOL CFX_Barcode::RenderDevice(CFX_RenderDevice* device, | |
330 const CFX_Matrix* matirx, | |
331 int32_t& e) { | |
332 if (!m_pBCEngine) { | |
333 return FALSE; | |
334 } | |
335 return m_pBCEngine->RenderDevice(device, matirx, e); | |
336 } | |
337 FX_BOOL CFX_Barcode::RenderBitmap(CFX_DIBitmap*& pOutBitmap, int32_t& e) { | |
338 if (!m_pBCEngine) { | |
339 return FALSE; | |
340 } | |
341 return m_pBCEngine->RenderBitmap(pOutBitmap, e); | |
342 } | |
343 #define BC_TYPE_MIN BC_CODE39 | |
344 #define BC_TYPE_MAX BC_DATAMATRIX | |
345 CFX_WideString CFX_Barcode::Decode(uint8_t* buf, | |
346 int32_t width, | |
347 int32_t height, | |
348 int32_t& errorCode) { | |
349 for (BC_TYPE t = BC_TYPE_MIN; t <= BC_TYPE_MAX; | |
350 t = (BC_TYPE)((int32_t)t + 1)) { | |
351 CBC_CodeBase* pTmpEngine = FX_Barcode_CreateBarCodeEngineObject(t); | |
352 if (!pTmpEngine) { | |
353 continue; | |
354 } | |
355 CFX_WideString ret = pTmpEngine->Decode(buf, width, height, errorCode); | |
356 if (errorCode == BCExceptionNO) { | |
357 return ret; | |
358 } | |
359 } | |
360 errorCode = BCExceptionUnSupportedBarcode; | |
361 return CFX_WideString(); | |
362 } | |
363 CFX_WideString CFX_Barcode::Decode(CFX_DIBitmap* pBitmap, int32_t& errorCode) { | |
364 for (BC_TYPE t = BC_TYPE_MIN; t <= BC_TYPE_MAX; | |
365 t = (BC_TYPE)((int32_t)t + 1)) { | |
366 CBC_CodeBase* pTmpEngine = FX_Barcode_CreateBarCodeEngineObject(t); | |
367 if (!pTmpEngine) { | |
368 continue; | |
369 } | |
370 CFX_WideString ret = pTmpEngine->Decode(pBitmap, errorCode); | |
371 if (errorCode == BCExceptionNO) { | |
372 return ret; | |
373 } | |
374 } | |
375 errorCode = BCExceptionUnSupportedBarcode; | |
376 return CFX_WideString(); | |
377 } | |
378 IFX_Barcode* FX_Barcode_Create(BC_TYPE type) { | |
379 CFX_Barcode* pBarcode = new CFX_Barcode; | |
380 if (pBarcode->Crreate(type)) { | |
381 return pBarcode; | |
382 } | |
383 pBarcode->Release(); | |
384 return NULL; | |
385 } | |
OLD | NEW |