summaryrefslogtreecommitdiff
path: root/xfa/fwl/basewidget/fxmath_barcodeimp.cpp
blob: 5ca09276710363845eb0476f03a40ed45de769b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
// Copyright 2014 PDFium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com

#include "xfa/fwl/basewidget/fxmath_barcodeimp.h"

#include "xfa/fxbarcode/cbc_codabar.h"
#include "xfa/fxbarcode/cbc_code128.h"
#include "xfa/fxbarcode/cbc_code39.h"
#include "xfa/fxbarcode/cbc_codebase.h"
#include "xfa/fxbarcode/cbc_datamatrix.h"
#include "xfa/fxbarcode/cbc_ean13.h"
#include "xfa/fxbarcode/cbc_ean8.h"
#include "xfa/fxbarcode/cbc_pdf417i.h"
#include "xfa/fxbarcode/cbc_qrcode.h"
#include "xfa/fxbarcode/cbc_upca.h"

static CBC_CodeBase* FX_Barcode_CreateBarCodeEngineObject(BC_TYPE type) {
  switch (type) {
    case BC_CODE39:
      return new CBC_Code39();
    case BC_CODABAR:
      return new CBC_Codabar();
    case BC_CODE128:
      return new CBC_Code128(BC_CODE128_B);
    case BC_CODE128_B:
      return new CBC_Code128(BC_CODE128_B);
    case BC_CODE128_C:
      return new CBC_Code128(BC_CODE128_C);
    case BC_EAN8:
      return new CBC_EAN8();
    case BC_UPCA:
      return new CBC_UPCA();
    case BC_EAN13:
      return new CBC_EAN13();
    case BC_QR_CODE:
      return new CBC_QRCode();
    case BC_PDF417:
      return new CBC_PDF417I();
    case BC_DATAMATRIX:
      return new CBC_DataMatrix();
    case BC_UNKNOWN:
    default:
      return NULL;
  }
}
CFX_Barcode::CFX_Barcode() {}
CFX_Barcode::~CFX_Barcode() {
  if (m_pBCEngine) {
    delete m_pBCEngine;
    m_pBCEngine = NULL;
  }
}
FX_BOOL CFX_Barcode::Create(BC_TYPE type) {
  m_pBCEngine = FX_Barcode_CreateBarCodeEngineObject(type);
  return m_pBCEngine != NULL;
}
void CFX_Barcode::Release() {
  delete this;
}
BC_TYPE CFX_Barcode::GetType() {
  return m_pBCEngine ? m_pBCEngine->GetType() : BC_UNKNOWN;
}
FX_BOOL CFX_Barcode::SetCharEncoding(BC_CHAR_ENCODING encoding) {
  return m_pBCEngine ? m_pBCEngine->SetCharEncoding(encoding) : FALSE;
}
FX_BOOL CFX_Barcode::SetModuleHeight(int32_t moduleHeight) {
  return m_pBCEngine ? m_pBCEngine->SetModuleHeight(moduleHeight) : FALSE;
}
FX_BOOL CFX_Barcode::SetModuleWidth(int32_t moduleWidth) {
  return m_pBCEngine ? m_pBCEngine->SetModuleWidth(moduleWidth) : FALSE;
}
FX_BOOL CFX_Barcode::SetHeight(int32_t height) {
  return m_pBCEngine ? m_pBCEngine->SetHeight(height) : FALSE;
}
FX_BOOL CFX_Barcode::SetWidth(int32_t width) {
  return m_pBCEngine ? m_pBCEngine->SetWidth(width) : FALSE;
}
FX_BOOL CFX_Barcode::CheckContentValidity(const CFX_WideStringC& contents) {
  switch (GetType()) {
    case BC_CODE39:
    case BC_CODABAR:
    case BC_CODE128:
    case BC_CODE128_B:
    case BC_CODE128_C:
    case BC_EAN8:
    case BC_EAN13:
    case BC_UPCA:
      return m_pBCEngine
                 ? static_cast<CBC_OneCode*>(m_pBCEngine)
                       ->CheckContentValidity(contents)
                 : TRUE;
    default:
      return TRUE;
  }
}
FX_BOOL CFX_Barcode::SetPrintChecksum(FX_BOOL checksum) {
  switch (GetType()) {
    case BC_CODE39:
    case BC_CODABAR:
    case BC_CODE128:
    case BC_CODE128_B:
    case BC_CODE128_C:
    case BC_EAN8:
    case BC_EAN13:
    case BC_UPCA:
      return m_pBCEngine ? (static_cast<CBC_OneCode*>(m_pBCEngine)
                                ->SetPrintChecksum(checksum),
                            TRUE)
                         : FALSE;
    default:
      return FALSE;
  }
}
FX_BOOL CFX_Barcode::SetDataLength(int32_t length) {
  switch (GetType()) {
    case BC_CODE39:
    case BC_CODABAR:
    case BC_CODE128:
    case BC_CODE128_B:
    case BC_CODE128_C:
    case BC_EAN8:
    case BC_EAN13:
    case BC_UPCA:
      return m_pBCEngine ? (static_cast<CBC_OneCode*>(m_pBCEngine)
                                ->SetDataLength(length),
                            TRUE)
                         : FALSE;
    default:
      return FALSE;
  }
}
FX_BOOL CFX_Barcode::SetCalChecksum(int32_t state) {
  switch (GetType()) {
    case BC_CODE39:
    case BC_CODABAR:
    case BC_CODE128:
    case BC_CODE128_B:
    case BC_CODE128_C:
    case BC_EAN8:
    case BC_EAN13:
    case BC_UPCA:
      return m_pBCEngine ? (static_cast<CBC_OneCode*>(m_pBCEngine)
                                ->SetCalChecksum(state),
                            TRUE)
                         : FALSE;
    default:
      return FALSE;
  }
}
FX_BOOL CFX_Barcode::SetFont(CFX_Font* pFont) {
  switch (GetType()) {
    case BC_CODE39:
    case BC_CODABAR:
    case BC_CODE128:
    case BC_CODE128_B:
    case BC_CODE128_C:
    case BC_EAN8:
    case BC_EAN13:
    case BC_UPCA:
      return m_pBCEngine
                 ? static_cast<CBC_OneCode*>(m_pBCEngine)->SetFont(pFont)
                 : FALSE;
    default:
      return FALSE;
  }
}
FX_BOOL CFX_Barcode::SetFontSize(FX_FLOAT size) {
  switch (GetType()) {
    case BC_CODE39:
    case BC_CODABAR:
    case BC_CODE128:
    case BC_CODE128_B:
    case BC_CODE128_C:
    case BC_EAN8:
    case BC_EAN13:
    case BC_UPCA:
      return m_pBCEngine
                 ? (static_cast<CBC_OneCode*>(m_pBCEngine)->SetFontSize(size),
                    TRUE)
                 : FALSE;
    default:
      return FALSE;
  }
}
FX_BOOL CFX_Barcode::SetFontStyle(int32_t style) {
  switch (GetType()) {
    case BC_CODE39:
    case BC_CODABAR:
    case BC_CODE128:
    case BC_CODE128_B:
    case BC_CODE128_C:
    case BC_EAN8:
    case BC_EAN13:
    case BC_UPCA:
      return m_pBCEngine
                 ? (static_cast<CBC_OneCode*>(m_pBCEngine)->SetFontStyle(style),
                    TRUE)
                 : FALSE;
    default:
      return FALSE;
  }
}
FX_BOOL CFX_Barcode::SetFontColor(FX_ARGB color) {
  switch (GetType()) {
    case BC_CODE39:
    case BC_CODABAR:
    case BC_CODE128:
    case BC_CODE128_B:
    case BC_CODE128_C:
    case BC_EAN8:
    case BC_EAN13:
    case BC_UPCA:
      return m_pBCEngine
                 ? (static_cast<CBC_OneCode*>(m_pBCEngine)->SetFontColor(color),
                    TRUE)
                 : FALSE;
    default:
      return FALSE;
  }
}
FX_BOOL CFX_Barcode::SetTextLocation(BC_TEXT_LOC location) {
  typedef FX_BOOL (CBC_CodeBase::*memptrtype)(BC_TEXT_LOC);
  memptrtype memptr = NULL;
  switch (GetType()) {
    case BC_CODE39:
      memptr = (memptrtype)&CBC_Code39::SetTextLocation;
      break;
    case BC_CODABAR:
      memptr = (memptrtype)&CBC_Codabar::SetTextLocation;
      break;
    case BC_CODE128:
    case BC_CODE128_B:
    case BC_CODE128_C:
      memptr = (memptrtype)&CBC_Code128::SetTextLocation;
      break;
    default:
      break;
  }
  return m_pBCEngine && memptr ? (m_pBCEngine->*memptr)(location) : FALSE;
}
FX_BOOL CFX_Barcode::SetWideNarrowRatio(int32_t ratio) {
  typedef FX_BOOL (CBC_CodeBase::*memptrtype)(int32_t);
  memptrtype memptr = NULL;
  switch (GetType()) {
    case BC_CODE39:
      memptr = (memptrtype)&CBC_Code39::SetWideNarrowRatio;
      break;
    case BC_CODABAR:
      memptr = (memptrtype)&CBC_Codabar::SetWideNarrowRatio;
      break;
    default:
      break;
  }
  return m_pBCEngine && memptr ? (m_pBCEngine->*memptr)(ratio) : FALSE;
}
FX_BOOL CFX_Barcode::SetStartChar(FX_CHAR start) {
  typedef FX_BOOL (CBC_CodeBase::*memptrtype)(FX_CHAR);
  memptrtype memptr = NULL;
  switch (GetType()) {
    case BC_CODABAR:
      memptr = (memptrtype)&CBC_Codabar::SetStartChar;
      break;
    default:
      break;
  }
  return m_pBCEngine && memptr ? (m_pBCEngine->*memptr)(start) : FALSE;
}
FX_BOOL CFX_Barcode::SetEndChar(FX_CHAR end) {
  typedef FX_BOOL (CBC_CodeBase::*memptrtype)(FX_CHAR);
  memptrtype memptr = NULL;
  switch (GetType()) {
    case BC_CODABAR:
      memptr = (memptrtype)&CBC_Codabar::SetEndChar;
      break;
    default:
      break;
  }
  return m_pBCEngine && memptr ? (m_pBCEngine->*memptr)(end) : FALSE;
}
FX_BOOL CFX_Barcode::SetVersion(int32_t version) {
  typedef FX_BOOL (CBC_CodeBase::*memptrtype)(int32_t);
  memptrtype memptr = NULL;
  switch (GetType()) {
    case BC_QR_CODE:
      memptr = (memptrtype)&CBC_QRCode::SetVersion;
      break;
    default:
      break;
  }
  return m_pBCEngine && memptr ? (m_pBCEngine->*memptr)(version) : FALSE;
}
FX_BOOL CFX_Barcode::SetErrorCorrectionLevel(int32_t level) {
  typedef FX_BOOL (CBC_CodeBase::*memptrtype)(int32_t);
  memptrtype memptr = NULL;
  switch (GetType()) {
    case BC_QR_CODE:
      memptr = (memptrtype)&CBC_QRCode::SetErrorCorrectionLevel;
      break;
    case BC_PDF417:
      memptr = (memptrtype)&CBC_PDF417I::SetErrorCorrectionLevel;
      break;
    default:
      return FALSE;
  }
  return m_pBCEngine && memptr ? (m_pBCEngine->*memptr)(level) : FALSE;
}
FX_BOOL CFX_Barcode::SetTruncated(FX_BOOL truncated) {
  typedef void (CBC_CodeBase::*memptrtype)(FX_BOOL);
  memptrtype memptr = NULL;
  switch (GetType()) {
    case BC_PDF417:
      memptr = (memptrtype)&CBC_PDF417I::SetTruncated;
      break;
    default:
      break;
  }
  return m_pBCEngine && memptr ? ((m_pBCEngine->*memptr)(truncated), TRUE)
                               : FALSE;
}
#ifndef BCExceptionNO
#define BCExceptionNO 0
#endif
#ifndef BCExceptionFormatException
#define BCExceptionFormatException 8
#endif
#ifndef BCExceptionUnSupportedBarcode
#define BCExceptionUnSupportedBarcode 18
#endif
FX_BOOL CFX_Barcode::Encode(const CFX_WideStringC& contents,
                            FX_BOOL isDevice,
                            int32_t& e) {
  if (!m_pBCEngine) {
    return FALSE;
  }
  return m_pBCEngine->Encode(contents, isDevice, e);
}
FX_BOOL CFX_Barcode::RenderDevice(CFX_RenderDevice* device,
                                  const CFX_Matrix* matirx,
                                  int32_t& e) {
  if (!m_pBCEngine) {
    return FALSE;
  }
  return m_pBCEngine->RenderDevice(device, matirx, e);
}
FX_BOOL CFX_Barcode::RenderBitmap(CFX_DIBitmap*& pOutBitmap, int32_t& e) {
  if (!m_pBCEngine) {
    return FALSE;
  }
  return m_pBCEngine->RenderBitmap(pOutBitmap, e);
}
#define BC_TYPE_MIN BC_CODE39
#define BC_TYPE_MAX BC_DATAMATRIX
CFX_WideString CFX_Barcode::Decode(uint8_t* buf,
                                   int32_t width,
                                   int32_t height,
                                   int32_t& errorCode) {
  for (BC_TYPE t = BC_TYPE_MIN; t <= BC_TYPE_MAX;
       t = (BC_TYPE)((int32_t)t + 1)) {
    CBC_CodeBase* pTmpEngine = FX_Barcode_CreateBarCodeEngineObject(t);
    if (!pTmpEngine) {
      continue;
    }
    CFX_WideString ret = pTmpEngine->Decode(buf, width, height, errorCode);
    if (errorCode == BCExceptionNO) {
      return ret;
    }
  }
  errorCode = BCExceptionUnSupportedBarcode;
  return CFX_WideString();
}
CFX_WideString CFX_Barcode::Decode(CFX_DIBitmap* pBitmap, int32_t& errorCode) {
  for (BC_TYPE t = BC_TYPE_MIN; t <= BC_TYPE_MAX;
       t = (BC_TYPE)((int32_t)t + 1)) {
    CBC_CodeBase* pTmpEngine = FX_Barcode_CreateBarCodeEngineObject(t);
    if (!pTmpEngine) {
      continue;
    }
    CFX_WideString ret = pTmpEngine->Decode(pBitmap, errorCode);
    if (errorCode == BCExceptionNO) {
      return ret;
    }
  }
  errorCode = BCExceptionUnSupportedBarcode;
  return CFX_WideString();
}