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// Original code is licensed as follows:
7/*
8 * Copyright 2011 ZXing authors
9 *
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 *      http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 */
22
23#include "xfa/src/fxbarcode/barcode.h"
24#include "xfa/src/fxbarcode/BC_Reader.h"
25#include "xfa/src/fxbarcode/BC_Writer.h"
26#include "xfa/src/fxbarcode/BC_DecoderResult.h"
27#include "xfa/src/fxbarcode/BC_LuminanceSource.h"
28#include "xfa/src/fxbarcode/BC_BufferedImageLuminanceSource.h"
29#include "xfa/src/fxbarcode/BC_Binarizer.h"
30#include "xfa/src/fxbarcode/BC_BinaryBitmap.h"
31#include "xfa/src/fxbarcode/BC_UtilCodingConvert.h"
32#include "xfa/src/fxbarcode/BC_ResultPoint.h"
33#include "xfa/src/fxbarcode/BC_BinaryBitmap.h"
34#include "xfa/src/fxbarcode/BC_TwoDimWriter.h"
35#include "xfa/src/fxbarcode/common/BC_GlobalHistogramBinarizer.h"
36#include "xfa/src/fxbarcode/common/BC_CommonBitMatrix.h"
37#include "xfa/src/fxbarcode/common/BC_CommonBitArray.h"
38#include "xfa/src/fxbarcode/common/BC_CommonBitMatrix.h"
39#include "xfa/src/fxbarcode/common/BC_CommonDecoderResult.h"
40#include "xfa/src/fxbarcode/datamatrix/BC_DataMatrixReader.h"
41#include "xfa/src/fxbarcode/datamatrix/BC_DataMatrixWriter.h"
42#include "xfa/src/fxbarcode/oned/BC_OneDReader.h"
43#include "xfa/src/fxbarcode/oned/BC_OneDimReader.h"
44#include "xfa/src/fxbarcode/oned/BC_OneDimWriter.h"
45#include "xfa/src/fxbarcode/oned/BC_OnedCode39Reader.h"
46#include "xfa/src/fxbarcode/oned/BC_OnedCode39Writer.h"
47#include "xfa/src/fxbarcode/oned/BC_OnedCodaBarReader.h"
48#include "xfa/src/fxbarcode/oned/BC_OnedCodaBarWriter.h"
49#include "xfa/src/fxbarcode/oned/BC_OnedCode128Reader.h"
50#include "xfa/src/fxbarcode/oned/BC_OnedCode128Writer.h"
51#include "xfa/src/fxbarcode/oned/BC_OnedEAN8Reader.h"
52#include "xfa/src/fxbarcode/oned/BC_OnedEAN8Writer.h"
53#include "xfa/src/fxbarcode/oned/BC_OnedEAN13Reader.h"
54#include "xfa/src/fxbarcode/oned/BC_OnedEAN13Writer.h"
55#include "xfa/src/fxbarcode/oned/BC_OnedUPCAReader.h"
56#include "xfa/src/fxbarcode/oned/BC_OnedUPCAWriter.h"
57#include "xfa/src/fxbarcode/pdf417/BC_PDF417DetectorResult.h"
58#include "xfa/src/fxbarcode/pdf417/BC_PDF417Compaction.h"
59#include "xfa/src/fxbarcode/pdf417/BC_PDF417.h"
60#include "xfa/src/fxbarcode/pdf417/BC_PDF417Writer.h"
61#include "xfa/src/fxbarcode/pdf417/BC_PDF417Detector.h"
62#include "xfa/src/fxbarcode/pdf417/BC_PDF417DetectorResult.h"
63#include "xfa/src/fxbarcode/pdf417/BC_PDF417Codeword.h"
64#include "xfa/src/fxbarcode/pdf417/BC_PDF417Common.h"
65#include "xfa/src/fxbarcode/pdf417/BC_PDF417BarcodeValue.h"
66#include "xfa/src/fxbarcode/pdf417/BC_PDF417BarcodeMetadata.h"
67#include "xfa/src/fxbarcode/pdf417/BC_PDF417BoundingBox.h"
68#include "xfa/src/fxbarcode/pdf417/BC_PDF417DetectionResultColumn.h"
69#include "xfa/src/fxbarcode/pdf417/BC_PDF417DetectionResultRowIndicatorColumn.h"
70#include "xfa/src/fxbarcode/pdf417/BC_PDF417DetectionResult.h"
71#include "xfa/src/fxbarcode/pdf417/BC_PDF417DecodedBitStreamParser.h"
72#include "xfa/src/fxbarcode/pdf417/BC_PDF417CodewordDecoder.h"
73#include "xfa/src/fxbarcode/pdf417/BC_PDF417DecodedBitStreamParser.h"
74#include "xfa/src/fxbarcode/pdf417/BC_PDF417ECModulusPoly.h"
75#include "xfa/src/fxbarcode/pdf417/BC_PDF417ECModulusGF.h"
76#include "xfa/src/fxbarcode/pdf417/BC_PDF417ECErrorCorrection.h"
77#include "xfa/src/fxbarcode/pdf417/BC_PDF417DecodedBitStreamParser.h"
78#include "xfa/src/fxbarcode/pdf417/BC_PDF417ScanningDecoder.h"
79#include "xfa/src/fxbarcode/pdf417/BC_PDF417Reader.h"
80#include "xfa/src/fxbarcode/pdf417/BC_PDF417HighLevelEncoder.h"
81#include "xfa/src/fxbarcode/qrcode/BC_QRCodeReader.h"
82#include "xfa/src/fxbarcode/qrcode/BC_QRCodeWriter.h"
83CBC_CodeBase::CBC_CodeBase() {}
84CBC_CodeBase::~CBC_CodeBase() {}
85FX_BOOL CBC_CodeBase::SetCharEncoding(int32_t encoding) {
86  if (m_pBCWriter) {
87    return m_pBCWriter->SetCharEncoding(encoding);
88  }
89  return FALSE;
90}
91FX_BOOL CBC_CodeBase::SetModuleHeight(int32_t moduleHeight) {
92  if (m_pBCWriter) {
93    return m_pBCWriter->SetModuleHeight(moduleHeight);
94  }
95  return FALSE;
96}
97FX_BOOL CBC_CodeBase::SetModuleWidth(int32_t moduleWidth) {
98  if (m_pBCWriter) {
99    return m_pBCWriter->SetModuleWidth(moduleWidth);
100  }
101  return FALSE;
102}
103FX_BOOL CBC_CodeBase::SetHeight(int32_t height) {
104  if (m_pBCWriter) {
105    return m_pBCWriter->SetHeight(height);
106  }
107  return FALSE;
108}
109FX_BOOL CBC_CodeBase::SetWidth(int32_t width) {
110  if (m_pBCWriter) {
111    return m_pBCWriter->SetWidth(width);
112  }
113  return FALSE;
114}
115void CBC_CodeBase::SetBackgroundColor(FX_ARGB backgroundColor) {
116  if (m_pBCWriter) {
117    m_pBCWriter->SetBackgroundColor(backgroundColor);
118  }
119}
120void CBC_CodeBase::SetBarcodeColor(FX_ARGB foregroundColor) {
121  if (m_pBCWriter) {
122    m_pBCWriter->SetBarcodeColor(foregroundColor);
123  }
124}
125CBC_OneCode::CBC_OneCode(){};
126CBC_OneCode::~CBC_OneCode() {}
127FX_BOOL CBC_OneCode::CheckContentValidity(const CFX_WideStringC& contents) {
128  if (m_pBCWriter) {
129    return ((CBC_OneDimWriter*)m_pBCWriter)->CheckContentValidity(contents);
130  }
131  return FALSE;
132}
133CFX_WideString CBC_OneCode::FilterContents(const CFX_WideStringC& contents) {
134  CFX_WideString tmp;
135  if (m_pBCWriter == NULL) {
136    return tmp;
137  }
138  return ((CBC_OneDimWriter*)m_pBCWriter)->FilterContents(contents);
139}
140void CBC_OneCode::SetPrintChecksum(FX_BOOL checksum) {
141  if (m_pBCWriter) {
142    ((CBC_OneDimWriter*)m_pBCWriter)->SetPrintChecksum(checksum);
143  }
144}
145void CBC_OneCode::SetDataLength(int32_t length) {
146  if (m_pBCWriter) {
147    ((CBC_OneDimWriter*)m_pBCWriter)->SetDataLength(length);
148  }
149}
150void CBC_OneCode::SetCalChecksum(FX_BOOL calc) {
151  if (m_pBCWriter) {
152    ((CBC_OneDimWriter*)m_pBCWriter)->SetCalcChecksum(calc);
153  }
154}
155FX_BOOL CBC_OneCode::SetFont(CFX_Font* cFont) {
156  if (m_pBCWriter) {
157    return ((CBC_OneDimWriter*)m_pBCWriter)->SetFont(cFont);
158  }
159  return FALSE;
160}
161void CBC_OneCode::SetFontSize(FX_FLOAT size) {
162  if (m_pBCWriter) {
163    ((CBC_OneDimWriter*)m_pBCWriter)->SetFontSize(size);
164  }
165}
166void CBC_OneCode::SetFontStyle(int32_t style) {
167  if (m_pBCWriter) {
168    ((CBC_OneDimWriter*)m_pBCWriter)->SetFontStyle(style);
169  }
170}
171void CBC_OneCode::SetFontColor(FX_ARGB color) {
172  if (m_pBCWriter) {
173    ((CBC_OneDimWriter*)m_pBCWriter)->SetFontColor(color);
174  }
175}
176CBC_Code39::CBC_Code39() {
177  m_pBCReader = (CBC_Reader*)new (CBC_OnedCode39Reader);
178  m_pBCWriter = (CBC_Writer*)new (CBC_OnedCode39Writer);
179}
180CBC_Code39::CBC_Code39(FX_BOOL usingCheckDigit) {
181  m_pBCReader = (CBC_Reader*)new CBC_OnedCode39Reader(usingCheckDigit);
182  m_pBCWriter = (CBC_Writer*)new CBC_OnedCode39Writer;
183}
184CBC_Code39::CBC_Code39(FX_BOOL usingCheckDigit, FX_BOOL extendedMode) {
185  m_pBCReader =
186      (CBC_Reader*)new CBC_OnedCode39Reader(usingCheckDigit, extendedMode);
187  m_pBCWriter = (CBC_Writer*)new CBC_OnedCode39Writer(extendedMode);
188}
189CBC_Code39::~CBC_Code39() {
190  if (m_pBCReader) {
191    delete (m_pBCReader);
192    m_pBCReader = NULL;
193  }
194  if (m_pBCWriter) {
195    delete (m_pBCWriter);
196    m_pBCWriter = NULL;
197  }
198}
199FX_BOOL CBC_Code39::Encode(const CFX_WideStringC& contents,
200                           FX_BOOL isDevice,
201                           int32_t& e) {
202  if (contents.IsEmpty()) {
203    e = BCExceptionNoContents;
204    return FALSE;
205  }
206  BCFORMAT format = BCFORMAT_CODE_39;
207  int32_t outWidth = 0;
208  int32_t outHeight = 0;
209  CFX_WideString filtercontents =
210      ((CBC_OnedCode39Writer*)m_pBCWriter)->FilterContents(contents);
211  CFX_WideString renderContents =
212      ((CBC_OnedCode39Writer*)m_pBCWriter)->RenderTextContents(contents);
213  m_renderContents = renderContents;
214  CFX_ByteString byteString = filtercontents.UTF8Encode();
215  uint8_t* data = static_cast<CBC_OnedCode39Writer*>(m_pBCWriter)
216                      ->Encode(byteString, format, outWidth, outHeight, e);
217  BC_EXCEPTION_CHECK_ReturnValue(e, FALSE);
218  ((CBC_OneDimWriter*)m_pBCWriter)
219      ->RenderResult(renderContents, data, outWidth, isDevice, e);
220  FX_Free(data);
221  BC_EXCEPTION_CHECK_ReturnValue(e, FALSE);
222  return TRUE;
223}
224FX_BOOL CBC_Code39::RenderDevice(CFX_RenderDevice* device,
225                                 const CFX_Matrix* matirx,
226                                 int32_t& e) {
227  CFX_WideString renderCon = ((CBC_OnedCode39Writer*)m_pBCWriter)
228                                 ->encodedContents(m_renderContents, e);
229  ((CBC_OneDimWriter*)m_pBCWriter)
230      ->RenderDeviceResult(device, matirx, renderCon, e);
231  BC_EXCEPTION_CHECK_ReturnValue(e, FALSE);
232  return TRUE;
233}
234FX_BOOL CBC_Code39::RenderBitmap(CFX_DIBitmap*& pOutBitmap, int32_t& e) {
235  CFX_WideString renderCon = ((CBC_OnedCode39Writer*)m_pBCWriter)
236                                 ->encodedContents(m_renderContents, e);
237  ((CBC_OneDimWriter*)m_pBCWriter)
238      ->RenderBitmapResult(pOutBitmap, renderCon, e);
239  BC_EXCEPTION_CHECK_ReturnValue(e, FALSE);
240  return TRUE;
241}
242CFX_WideString CBC_Code39::Decode(uint8_t* buf,
243                                  int32_t width,
244                                  int32_t hight,
245                                  int32_t& e) {
246  CFX_WideString str;
247  return str;
248}
249CFX_WideString CBC_Code39::Decode(CFX_DIBitmap* pBitmap, int32_t& e) {
250  CBC_BufferedImageLuminanceSource source(pBitmap);
251  CBC_GlobalHistogramBinarizer binarizer(&source);
252  CBC_BinaryBitmap bitmap(&binarizer);
253  CFX_ByteString str = m_pBCReader->Decode(&bitmap, 0, e);
254  BC_EXCEPTION_CHECK_ReturnValue(e, FX_WSTRC(L""));
255  return CFX_WideString::FromUTF8(str, str.GetLength());
256}
257FX_BOOL CBC_Code39::SetTextLocation(BC_TEXT_LOC location) {
258  if (m_pBCWriter) {
259    return ((CBC_OnedCode39Writer*)m_pBCWriter)->SetTextLocation(location);
260  }
261  return FALSE;
262}
263FX_BOOL CBC_Code39::SetWideNarrowRatio(int32_t ratio) {
264  if (m_pBCWriter) {
265    return ((CBC_OnedCode39Writer*)m_pBCWriter)->SetWideNarrowRatio(ratio);
266  }
267  return FALSE;
268}
269CBC_Codabar::CBC_Codabar() {
270  m_pBCReader = (CBC_Reader*)new (CBC_OnedCodaBarReader);
271  m_pBCWriter = (CBC_Writer*)new (CBC_OnedCodaBarWriter);
272}
273CBC_Codabar::~CBC_Codabar() {
274  if (m_pBCReader) {
275    delete (m_pBCReader);
276    m_pBCReader = NULL;
277  }
278  if (m_pBCWriter) {
279    delete (m_pBCWriter);
280    m_pBCWriter = NULL;
281  }
282}
283FX_BOOL CBC_Codabar::SetStartChar(FX_CHAR start) {
284  if (m_pBCWriter) {
285    return ((CBC_OnedCodaBarWriter*)m_pBCWriter)->SetStartChar(start);
286  }
287  return FALSE;
288}
289FX_BOOL CBC_Codabar::SetEndChar(FX_CHAR end) {
290  if (m_pBCWriter) {
291    return ((CBC_OnedCodaBarWriter*)m_pBCWriter)->SetEndChar(end);
292  }
293  return FALSE;
294}
295FX_BOOL CBC_Codabar::SetTextLocation(BC_TEXT_LOC location) {
296  return ((CBC_OnedCodaBarWriter*)m_pBCWriter)->SetTextLocation(location);
297}
298FX_BOOL CBC_Codabar::SetWideNarrowRatio(int32_t ratio) {
299  if (m_pBCWriter) {
300    return ((CBC_OnedCodaBarWriter*)m_pBCWriter)->SetWideNarrowRatio(ratio);
301  }
302  return FALSE;
303}
304FX_BOOL CBC_Codabar::Encode(const CFX_WideStringC& contents,
305                            FX_BOOL isDevice,
306                            int32_t& e) {
307  if (contents.IsEmpty()) {
308    e = BCExceptionNoContents;
309    return FALSE;
310  }
311  BCFORMAT format = BCFORMAT_CODABAR;
312  int32_t outWidth = 0;
313  int32_t outHeight = 0;
314  CFX_WideString filtercontents =
315      ((CBC_OneDimWriter*)m_pBCWriter)->FilterContents(contents);
316  CFX_ByteString byteString = filtercontents.UTF8Encode();
317  m_renderContents = filtercontents;
318  uint8_t* data = static_cast<CBC_OnedCodaBarWriter*>(m_pBCWriter)
319                      ->Encode(byteString, format, outWidth, outHeight, e);
320  BC_EXCEPTION_CHECK_ReturnValue(e, FALSE);
321  ((CBC_OneDimWriter*)m_pBCWriter)
322      ->RenderResult(filtercontents, data, outWidth, isDevice, e);
323  FX_Free(data);
324  BC_EXCEPTION_CHECK_ReturnValue(e, FALSE);
325  return TRUE;
326}
327FX_BOOL CBC_Codabar::RenderDevice(CFX_RenderDevice* device,
328                                  const CFX_Matrix* matirx,
329                                  int32_t& e) {
330  CFX_WideString renderCon =
331      ((CBC_OnedCodaBarWriter*)m_pBCWriter)->encodedContents(m_renderContents);
332  ((CBC_OneDimWriter*)m_pBCWriter)
333      ->RenderDeviceResult(device, matirx, renderCon, e);
334  BC_EXCEPTION_CHECK_ReturnValue(e, FALSE);
335  return TRUE;
336}
337FX_BOOL CBC_Codabar::RenderBitmap(CFX_DIBitmap*& pOutBitmap, int32_t& e) {
338  CFX_WideString renderCon =
339      ((CBC_OnedCodaBarWriter*)m_pBCWriter)->encodedContents(m_renderContents);
340  ((CBC_OneDimWriter*)m_pBCWriter)
341      ->RenderBitmapResult(pOutBitmap, renderCon, e);
342  BC_EXCEPTION_CHECK_ReturnValue(e, FALSE);
343  return TRUE;
344}
345CFX_WideString CBC_Codabar::Decode(uint8_t* buf,
346                                   int32_t width,
347                                   int32_t hight,
348                                   int32_t& e) {
349  CFX_WideString str;
350  return str;
351}
352CFX_WideString CBC_Codabar::Decode(CFX_DIBitmap* pBitmap, int32_t& e) {
353  CBC_BufferedImageLuminanceSource source(pBitmap);
354  CBC_GlobalHistogramBinarizer binarizer(&source);
355  CBC_BinaryBitmap bitmap(&binarizer);
356  CFX_ByteString str = m_pBCReader->Decode(&bitmap, 0, e);
357  BC_EXCEPTION_CHECK_ReturnValue(e, FX_WSTRC(L""));
358  return CFX_WideString::FromUTF8(str, str.GetLength());
359}
360CBC_Code128::CBC_Code128(BC_TYPE type) {
361  m_pBCReader = (CBC_Reader*)new (CBC_OnedCode128Reader);
362  m_pBCWriter = (CBC_Writer*)new CBC_OnedCode128Writer(type);
363}
364CBC_Code128::~CBC_Code128() {
365  if (m_pBCReader) {
366    delete (m_pBCReader);
367    m_pBCReader = NULL;
368  }
369  if (m_pBCWriter) {
370    delete (m_pBCWriter);
371    m_pBCWriter = NULL;
372  }
373}
374FX_BOOL CBC_Code128::SetTextLocation(BC_TEXT_LOC location) {
375  if (m_pBCWriter) {
376    return ((CBC_OnedCode128Writer*)m_pBCWriter)->SetTextLocation(location);
377  }
378  return FALSE;
379}
380FX_BOOL CBC_Code128::Encode(const CFX_WideStringC& contents,
381                            FX_BOOL isDevice,
382                            int32_t& e) {
383  if (contents.IsEmpty()) {
384    e = BCExceptionNoContents;
385    return FALSE;
386  }
387  BCFORMAT format = BCFORMAT_CODE_128;
388  int32_t outWidth = 0;
389  int32_t outHeight = 0;
390  CFX_WideString content = contents;
391  if (contents.GetLength() % 2 &&
392      ((CBC_OnedCode128Writer*)m_pBCWriter)->GetType() == BC_CODE128_C) {
393    content += '0';
394  }
395  CFX_WideString encodeContents =
396      ((CBC_OnedCode128Writer*)m_pBCWriter)->FilterContents(content);
397  m_renderContents = encodeContents;
398  CFX_ByteString byteString = encodeContents.UTF8Encode();
399  uint8_t* data = static_cast<CBC_OnedCode128Writer*>(m_pBCWriter)
400                      ->Encode(byteString, format, outWidth, outHeight, e);
401  BC_EXCEPTION_CHECK_ReturnValue(e, FALSE);
402  ((CBC_OneDimWriter*)m_pBCWriter)
403      ->RenderResult(encodeContents, data, outWidth, isDevice, e);
404  FX_Free(data);
405  BC_EXCEPTION_CHECK_ReturnValue(e, FALSE);
406  return TRUE;
407}
408FX_BOOL CBC_Code128::RenderDevice(CFX_RenderDevice* device,
409                                  const CFX_Matrix* matirx,
410                                  int32_t& e) {
411  ((CBC_OneDimWriter*)m_pBCWriter)
412      ->RenderDeviceResult(device, matirx, m_renderContents, e);
413  BC_EXCEPTION_CHECK_ReturnValue(e, FALSE);
414  return TRUE;
415}
416FX_BOOL CBC_Code128::RenderBitmap(CFX_DIBitmap*& pOutBitmap, int32_t& e) {
417  ((CBC_OneDimWriter*)m_pBCWriter)
418      ->RenderBitmapResult(pOutBitmap, m_renderContents, e);
419  BC_EXCEPTION_CHECK_ReturnValue(e, FALSE);
420  return TRUE;
421}
422CFX_WideString CBC_Code128::Decode(uint8_t* buf,
423                                   int32_t width,
424                                   int32_t hight,
425                                   int32_t& e) {
426  CFX_WideString str;
427  return str;
428}
429CFX_WideString CBC_Code128::Decode(CFX_DIBitmap* pBitmap, int32_t& e) {
430  CBC_BufferedImageLuminanceSource source(pBitmap);
431  CBC_GlobalHistogramBinarizer binarizer(&source);
432  CBC_BinaryBitmap bitmap(&binarizer);
433  CFX_ByteString str = m_pBCReader->Decode(&bitmap, 0, e);
434  BC_EXCEPTION_CHECK_ReturnValue(e, FX_WSTRC(L""));
435  return CFX_WideString::FromUTF8(str, str.GetLength());
436}
437CBC_EAN8::CBC_EAN8() {
438  m_pBCReader = (CBC_Reader*)new (CBC_OnedEAN8Reader);
439  m_pBCWriter = (CBC_Writer*)new (CBC_OnedEAN8Writer);
440}
441CBC_EAN8::~CBC_EAN8() {
442  if (m_pBCReader) {
443    delete (m_pBCReader);
444    m_pBCReader = NULL;
445  }
446  if (m_pBCWriter) {
447    delete (m_pBCWriter);
448    m_pBCWriter = NULL;
449  }
450}
451CFX_WideString CBC_EAN8::Preprocess(const CFX_WideStringC& contents) {
452  CFX_WideString encodeContents =
453      ((CBC_OnedEAN8Writer*)m_pBCWriter)->FilterContents(contents);
454  int32_t length = encodeContents.GetLength();
455  if (length <= 7) {
456    for (int32_t i = 0; i < 7 - length; i++) {
457      encodeContents = FX_WCHAR('0') + encodeContents;
458    }
459    CFX_ByteString byteString = encodeContents.UTF8Encode();
460    int32_t checksum =
461        ((CBC_OnedEAN8Writer*)m_pBCWriter)->CalcChecksum(byteString);
462    encodeContents += FX_WCHAR(checksum - 0 + '0');
463  }
464  if (length > 8) {
465    encodeContents = encodeContents.Mid(0, 8);
466  }
467  return encodeContents;
468}
469FX_BOOL CBC_EAN8::Encode(const CFX_WideStringC& contents,
470                         FX_BOOL isDevice,
471                         int32_t& e) {
472  if (contents.IsEmpty()) {
473    e = BCExceptionNoContents;
474    return FALSE;
475  }
476  BCFORMAT format = BCFORMAT_EAN_8;
477  int32_t outWidth = 0;
478  int32_t outHeight = 0;
479  CFX_WideString encodeContents = Preprocess(contents);
480  CFX_ByteString byteString = encodeContents.UTF8Encode();
481  m_renderContents = encodeContents;
482  uint8_t* data = static_cast<CBC_OnedEAN8Writer*>(m_pBCWriter)
483                      ->Encode(byteString, format, outWidth, outHeight, e);
484  BC_EXCEPTION_CHECK_ReturnValue(e, FALSE);
485  ((CBC_OneDimWriter*)m_pBCWriter)
486      ->RenderResult(encodeContents, data, outWidth, isDevice, e);
487  FX_Free(data);
488  BC_EXCEPTION_CHECK_ReturnValue(e, FALSE);
489  return TRUE;
490}
491FX_BOOL CBC_EAN8::RenderDevice(CFX_RenderDevice* device,
492                               const CFX_Matrix* matirx,
493                               int32_t& e) {
494  ((CBC_OneDimWriter*)m_pBCWriter)
495      ->RenderDeviceResult(device, matirx, m_renderContents, e);
496  BC_EXCEPTION_CHECK_ReturnValue(e, FALSE);
497  return TRUE;
498}
499FX_BOOL CBC_EAN8::RenderBitmap(CFX_DIBitmap*& pOutBitmap, int32_t& e) {
500  ((CBC_OneDimWriter*)m_pBCWriter)
501      ->RenderBitmapResult(pOutBitmap, m_renderContents, e);
502  BC_EXCEPTION_CHECK_ReturnValue(e, FALSE);
503  return TRUE;
504}
505CFX_WideString CBC_EAN8::Decode(uint8_t* buf,
506                                int32_t width,
507                                int32_t hight,
508                                int32_t& e) {
509  CFX_WideString str;
510  return str;
511}
512CFX_WideString CBC_EAN8::Decode(CFX_DIBitmap* pBitmap, int32_t& e) {
513  CBC_BufferedImageLuminanceSource source(pBitmap);
514  CBC_GlobalHistogramBinarizer binarizer(&source);
515  CBC_BinaryBitmap bitmap(&binarizer);
516  CFX_ByteString str = m_pBCReader->Decode(&bitmap, 0, e);
517  BC_EXCEPTION_CHECK_ReturnValue(e, FX_WSTRC(L""));
518  return CFX_WideString::FromUTF8(str, str.GetLength());
519}
520CBC_EAN13::CBC_EAN13() {
521  m_pBCReader = (CBC_Reader*)new (CBC_OnedEAN13Reader);
522  m_pBCWriter = (CBC_Writer*)new (CBC_OnedEAN13Writer);
523}
524CBC_EAN13::~CBC_EAN13() {
525  if (m_pBCReader) {
526    delete (m_pBCReader);
527    m_pBCReader = NULL;
528  }
529  if (m_pBCWriter) {
530    delete (m_pBCWriter);
531    m_pBCWriter = NULL;
532  }
533}
534CFX_WideString CBC_EAN13::Preprocess(const CFX_WideStringC& contents) {
535  CFX_WideString encodeContents =
536      ((CBC_OnedEAN8Writer*)m_pBCWriter)->FilterContents(contents);
537  int32_t length = encodeContents.GetLength();
538  if (length <= 12) {
539    for (int32_t i = 0; i < 12 - length; i++) {
540      encodeContents = FX_WCHAR('0') + encodeContents;
541    }
542    CFX_ByteString byteString = encodeContents.UTF8Encode();
543    int32_t checksum =
544        ((CBC_OnedEAN13Writer*)m_pBCWriter)->CalcChecksum(byteString);
545    byteString += checksum - 0 + '0';
546    encodeContents = byteString.UTF8Decode();
547  }
548  if (length > 13) {
549    encodeContents = encodeContents.Mid(0, 13);
550  }
551  return encodeContents;
552}
553FX_BOOL CBC_EAN13::Encode(const CFX_WideStringC& contents,
554                          FX_BOOL isDevice,
555                          int32_t& e) {
556  if (contents.IsEmpty()) {
557    e = BCExceptionNoContents;
558    return FALSE;
559  }
560  BCFORMAT format = BCFORMAT_EAN_13;
561  int32_t outWidth = 0;
562  int32_t outHeight = 0;
563  CFX_WideString encodeContents = Preprocess(contents);
564  CFX_ByteString byteString = encodeContents.UTF8Encode();
565  m_renderContents = encodeContents;
566  uint8_t* data = static_cast<CBC_OnedEAN13Writer*>(m_pBCWriter)
567                      ->Encode(byteString, format, outWidth, outHeight, e);
568  BC_EXCEPTION_CHECK_ReturnValue(e, FALSE);
569  ((CBC_OneDimWriter*)m_pBCWriter)
570      ->RenderResult(encodeContents, data, outWidth, isDevice, e);
571  FX_Free(data);
572  BC_EXCEPTION_CHECK_ReturnValue(e, FALSE);
573  return TRUE;
574}
575FX_BOOL CBC_EAN13::RenderDevice(CFX_RenderDevice* device,
576                                const CFX_Matrix* matirx,
577                                int32_t& e) {
578  ((CBC_OneDimWriter*)m_pBCWriter)
579      ->RenderDeviceResult(device, matirx, m_renderContents, e);
580  BC_EXCEPTION_CHECK_ReturnValue(e, FALSE);
581  return TRUE;
582}
583FX_BOOL CBC_EAN13::RenderBitmap(CFX_DIBitmap*& pOutBitmap, int32_t& e) {
584  ((CBC_OneDimWriter*)m_pBCWriter)
585      ->RenderBitmapResult(pOutBitmap, m_renderContents, e);
586  BC_EXCEPTION_CHECK_ReturnValue(e, FALSE);
587  return TRUE;
588}
589CFX_WideString CBC_EAN13::Decode(uint8_t* buf,
590                                 int32_t width,
591                                 int32_t hight,
592                                 int32_t& e) {
593  CFX_WideString str;
594  return str;
595}
596CFX_WideString CBC_EAN13::Decode(CFX_DIBitmap* pBitmap, int32_t& e) {
597  CBC_BufferedImageLuminanceSource source(pBitmap);
598  CBC_GlobalHistogramBinarizer binarizer(&source);
599  CBC_BinaryBitmap bitmap(&binarizer);
600  CFX_ByteString str = m_pBCReader->Decode(&bitmap, 0, e);
601  BC_EXCEPTION_CHECK_ReturnValue(e, FX_WSTRC(L""));
602  return CFX_WideString::FromUTF8(str, str.GetLength());
603}
604CBC_UPCA::CBC_UPCA() {
605  m_pBCReader = (CBC_Reader*)new (CBC_OnedUPCAReader);
606  ((CBC_OnedUPCAReader*)m_pBCReader)->Init();
607  m_pBCWriter = (CBC_Writer*)new (CBC_OnedUPCAWriter);
608}
609CBC_UPCA::~CBC_UPCA() {
610  if (m_pBCReader) {
611    delete (m_pBCReader);
612    m_pBCReader = NULL;
613  }
614  if (m_pBCWriter) {
615    delete (m_pBCWriter);
616    m_pBCWriter = NULL;
617  }
618}
619CFX_WideString CBC_UPCA::Preprocess(const CFX_WideStringC& contents) {
620  CFX_WideString encodeContents =
621      ((CBC_OnedEAN8Writer*)m_pBCWriter)->FilterContents(contents);
622  int32_t length = encodeContents.GetLength();
623  if (length <= 11) {
624    for (int32_t i = 0; i < 11 - length; i++) {
625      encodeContents = FX_WCHAR('0') + encodeContents;
626    }
627    CFX_ByteString byteString = encodeContents.UTF8Encode();
628    int32_t checksum =
629        ((CBC_OnedUPCAWriter*)m_pBCWriter)->CalcChecksum(byteString);
630    byteString += checksum - 0 + '0';
631    encodeContents = byteString.UTF8Decode();
632  }
633  if (length > 12) {
634    encodeContents = encodeContents.Mid(0, 12);
635  }
636  return encodeContents;
637}
638FX_BOOL CBC_UPCA::Encode(const CFX_WideStringC& contents,
639                         FX_BOOL isDevice,
640                         int32_t& e) {
641  if (contents.IsEmpty()) {
642    e = BCExceptionNoContents;
643    return FALSE;
644  }
645  BCFORMAT format = BCFORMAT_UPC_A;
646  int32_t outWidth = 0;
647  int32_t outHeight = 0;
648  CFX_WideString encodeContents = Preprocess(contents);
649  CFX_ByteString byteString = encodeContents.UTF8Encode();
650  m_renderContents = encodeContents;
651  ((CBC_OnedUPCAWriter*)m_pBCWriter)->Init();
652  uint8_t* data = static_cast<CBC_OnedUPCAWriter*>(m_pBCWriter)
653                      ->Encode(byteString, format, outWidth, outHeight, e);
654  BC_EXCEPTION_CHECK_ReturnValue(e, FALSE);
655  ((CBC_OneDimWriter*)m_pBCWriter)
656      ->RenderResult(encodeContents, data, outWidth, isDevice, e);
657  FX_Free(data);
658  BC_EXCEPTION_CHECK_ReturnValue(e, FALSE);
659  return TRUE;
660}
661FX_BOOL CBC_UPCA::RenderDevice(CFX_RenderDevice* device,
662                               const CFX_Matrix* matirx,
663                               int32_t& e) {
664  ((CBC_OneDimWriter*)m_pBCWriter)
665      ->RenderDeviceResult(device, matirx, m_renderContents, e);
666  BC_EXCEPTION_CHECK_ReturnValue(e, FALSE);
667  return TRUE;
668}
669FX_BOOL CBC_UPCA::RenderBitmap(CFX_DIBitmap*& pOutBitmap, int32_t& e) {
670  ((CBC_OneDimWriter*)m_pBCWriter)
671      ->RenderBitmapResult(pOutBitmap, m_renderContents, e);
672  BC_EXCEPTION_CHECK_ReturnValue(e, FALSE);
673  return TRUE;
674}
675CFX_WideString CBC_UPCA::Decode(uint8_t* buf,
676                                int32_t width,
677                                int32_t hight,
678                                int32_t& e) {
679  CFX_WideString str;
680  return str;
681}
682CFX_WideString CBC_UPCA::Decode(CFX_DIBitmap* pBitmap, int32_t& e) {
683  CBC_BufferedImageLuminanceSource source(pBitmap);
684  CBC_GlobalHistogramBinarizer binarizer(&source);
685  CBC_BinaryBitmap bitmap(&binarizer);
686  CFX_ByteString str = m_pBCReader->Decode(&bitmap, 0, e);
687  BC_EXCEPTION_CHECK_ReturnValue(e, FX_WSTRC(L""));
688  return CFX_WideString::FromUTF8(str, str.GetLength());
689}
690CBC_QRCode::CBC_QRCode() {
691  m_pBCReader = (CBC_Reader*)new (CBC_QRCodeReader);
692  ((CBC_QRCodeReader*)m_pBCReader)->Init();
693  m_pBCWriter = (CBC_Writer*)new (CBC_QRCodeWriter);
694}
695CBC_QRCode::~CBC_QRCode() {
696  if (m_pBCReader) {
697    delete (m_pBCReader);
698    m_pBCReader = NULL;
699  }
700  if (m_pBCWriter) {
701    delete (m_pBCWriter);
702    m_pBCWriter = NULL;
703  }
704}
705FX_BOOL CBC_QRCode::SetVersion(int32_t version) {
706  if (version < 0 || version > 40) {
707    return FALSE;
708  }
709  if (m_pBCWriter == NULL) {
710    return FALSE;
711  }
712  return ((CBC_QRCodeWriter*)m_pBCWriter)->SetVersion(version);
713}
714FX_BOOL CBC_QRCode::SetErrorCorrectionLevel(int32_t level) {
715  if (level < 0 || level > 3) {
716    return FALSE;
717  }
718  if (m_pBCWriter == NULL) {
719    return FALSE;
720  }
721  return ((CBC_TwoDimWriter*)m_pBCWriter)->SetErrorCorrectionLevel(level);
722}
723FX_BOOL CBC_QRCode::Encode(const CFX_WideStringC& contents,
724                           FX_BOOL isDevice,
725                           int32_t& e) {
726  int32_t outWidth = 0;
727  int32_t outHeight = 0;
728  uint8_t* data = ((CBC_QRCodeWriter*)m_pBCWriter)
729                      ->Encode(contents, ((CBC_QRCodeWriter*)m_pBCWriter)
730                                             ->GetErrorCorrectionLevel(),
731                               outWidth, outHeight, e);
732  BC_EXCEPTION_CHECK_ReturnValue(e, FALSE);
733  ((CBC_TwoDimWriter*)m_pBCWriter)->RenderResult(data, outWidth, outHeight, e);
734  FX_Free(data);
735  BC_EXCEPTION_CHECK_ReturnValue(e, FALSE);
736  return TRUE;
737}
738FX_BOOL CBC_QRCode::RenderDevice(CFX_RenderDevice* device,
739                                 const CFX_Matrix* matirx,
740                                 int32_t& e) {
741  ((CBC_TwoDimWriter*)m_pBCWriter)->RenderDeviceResult(device, matirx);
742  return TRUE;
743}
744FX_BOOL CBC_QRCode::RenderBitmap(CFX_DIBitmap*& pOutBitmap, int32_t& e) {
745  ((CBC_TwoDimWriter*)m_pBCWriter)->RenderBitmapResult(pOutBitmap, e);
746  BC_EXCEPTION_CHECK_ReturnValue(e, FALSE);
747  return TRUE;
748}
749CFX_WideString CBC_QRCode::Decode(uint8_t* buf,
750                                  int32_t width,
751                                  int32_t hight,
752                                  int32_t& e) {
753  CFX_WideString str;
754  return str;
755}
756CFX_WideString CBC_QRCode::Decode(CFX_DIBitmap* pBitmap, int32_t& e) {
757  CBC_BufferedImageLuminanceSource source(pBitmap);
758  CBC_GlobalHistogramBinarizer binarizer(&source);
759  CBC_BinaryBitmap bitmap(&binarizer);
760  CFX_ByteString retStr = m_pBCReader->Decode(&bitmap, 0, e);
761  BC_EXCEPTION_CHECK_ReturnValue(e, FX_WSTRC(L""));
762  return CFX_WideString::FromUTF8(retStr, retStr.GetLength());
763}
764CBC_PDF417I::CBC_PDF417I() {
765  m_pBCReader = (CBC_Reader*)new (CBC_PDF417Reader);
766  m_pBCWriter = (CBC_Writer*)new (CBC_PDF417Writer);
767}
768CBC_PDF417I::~CBC_PDF417I() {
769  if (m_pBCReader) {
770    delete (m_pBCReader);
771    m_pBCReader = NULL;
772  }
773  if (m_pBCWriter) {
774    delete (m_pBCWriter);
775    m_pBCWriter = NULL;
776  }
777}
778FX_BOOL CBC_PDF417I::SetErrorCorrectionLevel(int32_t level) {
779  ((CBC_PDF417Writer*)m_pBCWriter)->SetErrorCorrectionLevel(level);
780  return TRUE;
781}
782void CBC_PDF417I::SetTruncated(FX_BOOL truncated) {
783  ((CBC_PDF417Writer*)m_pBCWriter)->SetTruncated(truncated);
784}
785FX_BOOL CBC_PDF417I::Encode(const CFX_WideStringC& contents,
786                            FX_BOOL isDevice,
787                            int32_t& e) {
788  int32_t outWidth = 0;
789  int32_t outHeight = 0;
790  uint8_t* data = ((CBC_PDF417Writer*)m_pBCWriter)
791                      ->Encode(contents, outWidth, outHeight, e);
792  BC_EXCEPTION_CHECK_ReturnValue(e, FALSE);
793  ((CBC_TwoDimWriter*)m_pBCWriter)->RenderResult(data, outWidth, outHeight, e);
794  FX_Free(data);
795  BC_EXCEPTION_CHECK_ReturnValue(e, FALSE);
796  return TRUE;
797}
798FX_BOOL CBC_PDF417I::RenderDevice(CFX_RenderDevice* device,
799                                  const CFX_Matrix* matirx,
800                                  int32_t& e) {
801  ((CBC_TwoDimWriter*)m_pBCWriter)->RenderDeviceResult(device, matirx);
802  return TRUE;
803}
804FX_BOOL CBC_PDF417I::RenderBitmap(CFX_DIBitmap*& pOutBitmap, int32_t& e) {
805  ((CBC_TwoDimWriter*)m_pBCWriter)->RenderBitmapResult(pOutBitmap, e);
806  BC_EXCEPTION_CHECK_ReturnValue(e, FALSE);
807  return TRUE;
808}
809CFX_WideString CBC_PDF417I::Decode(uint8_t* buf,
810                                   int32_t width,
811                                   int32_t hight,
812                                   int32_t& e) {
813  CFX_WideString str;
814  return str;
815}
816CFX_WideString CBC_PDF417I::Decode(CFX_DIBitmap* pBitmap, int32_t& e) {
817  CBC_BufferedImageLuminanceSource source(pBitmap);
818  CBC_GlobalHistogramBinarizer binarizer(&source);
819  CBC_BinaryBitmap bitmap(&binarizer);
820  CFX_ByteString bytestring = m_pBCReader->Decode(&bitmap, 0, e);
821  BC_EXCEPTION_CHECK_ReturnValue(e, FX_WSTRC(L""));
822  return CFX_WideString::FromUTF8(bytestring, bytestring.GetLength());
823}
824CBC_DataMatrix::CBC_DataMatrix() {
825  m_pBCReader = (CBC_Reader*)new (CBC_DataMatrixReader);
826  ((CBC_DataMatrixReader*)m_pBCReader)->Init();
827  m_pBCWriter = (CBC_Writer*)new (CBC_DataMatrixWriter);
828}
829CBC_DataMatrix::~CBC_DataMatrix() {
830  if (m_pBCReader) {
831    delete (m_pBCReader);
832    m_pBCReader = NULL;
833  }
834  if (m_pBCWriter) {
835    delete (m_pBCWriter);
836    m_pBCWriter = NULL;
837  }
838}
839FX_BOOL CBC_DataMatrix::Encode(const CFX_WideStringC& contents,
840                               FX_BOOL isDevice,
841                               int32_t& e) {
842  int32_t outWidth = 0;
843  int32_t outHeight = 0;
844  uint8_t* data = ((CBC_DataMatrixWriter*)m_pBCWriter)
845                      ->Encode(contents, outWidth, outHeight, e);
846  BC_EXCEPTION_CHECK_ReturnValue(e, FALSE);
847  ((CBC_TwoDimWriter*)m_pBCWriter)->RenderResult(data, outWidth, outHeight, e);
848  FX_Free(data);
849  BC_EXCEPTION_CHECK_ReturnValue(e, FALSE);
850  return TRUE;
851}
852FX_BOOL CBC_DataMatrix::RenderDevice(CFX_RenderDevice* device,
853                                     const CFX_Matrix* matirx,
854                                     int32_t& e) {
855  ((CBC_TwoDimWriter*)m_pBCWriter)->RenderDeviceResult(device, matirx);
856  return TRUE;
857}
858FX_BOOL CBC_DataMatrix::RenderBitmap(CFX_DIBitmap*& pOutBitmap, int32_t& e) {
859  ((CBC_TwoDimWriter*)m_pBCWriter)->RenderBitmapResult(pOutBitmap, e);
860  BC_EXCEPTION_CHECK_ReturnValue(e, FALSE);
861  return TRUE;
862}
863CFX_WideString CBC_DataMatrix::Decode(uint8_t* buf,
864                                      int32_t width,
865                                      int32_t hight,
866                                      int32_t& e) {
867  CFX_WideString str;
868  return str;
869}
870CFX_WideString CBC_DataMatrix::Decode(CFX_DIBitmap* pBitmap, int32_t& e) {
871  CBC_BufferedImageLuminanceSource source(pBitmap);
872  CBC_GlobalHistogramBinarizer binarizer(&source);
873  CBC_BinaryBitmap bitmap(&binarizer);
874  CFX_ByteString retStr = m_pBCReader->Decode(&bitmap, 0, e);
875  BC_EXCEPTION_CHECK_ReturnValue(e, FX_WSTRC(L""));
876  return CFX_WideString::FromUTF8(retStr, retStr.GetLength());
877}
878