1// Copyright 2016 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 * Copyright 2011 ZXing authors
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 *      http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 */
21
22#include "xfa/fxbarcode/cbc_codabar.h"
23
24#include "xfa/fxbarcode/oned/BC_OnedCodaBarWriter.h"
25
26CBC_Codabar::CBC_Codabar() : CBC_OneCode(new CBC_OnedCodaBarWriter) {}
27
28CBC_Codabar::~CBC_Codabar() {}
29
30bool CBC_Codabar::SetStartChar(FX_CHAR start) {
31  if (!m_pBCWriter)
32    return false;
33  return static_cast<CBC_OnedCodaBarWriter*>(m_pBCWriter.get())
34      ->SetStartChar(start);
35}
36
37bool CBC_Codabar::SetEndChar(FX_CHAR end) {
38  if (m_pBCWriter)
39    return static_cast<CBC_OnedCodaBarWriter*>(m_pBCWriter.get())
40        ->SetEndChar(end);
41  return false;
42}
43
44bool CBC_Codabar::SetTextLocation(BC_TEXT_LOC location) {
45  return static_cast<CBC_OnedCodaBarWriter*>(m_pBCWriter.get())
46      ->SetTextLocation(location);
47}
48
49bool CBC_Codabar::SetWideNarrowRatio(int32_t ratio) {
50  if (m_pBCWriter)
51    return static_cast<CBC_OnedCodaBarWriter*>(m_pBCWriter.get())
52        ->SetWideNarrowRatio(ratio);
53  return false;
54}
55
56bool CBC_Codabar::Encode(const CFX_WideStringC& contents,
57                         bool isDevice,
58                         int32_t& e) {
59  if (contents.IsEmpty()) {
60    e = BCExceptionNoContents;
61    return false;
62  }
63  BCFORMAT format = BCFORMAT_CODABAR;
64  int32_t outWidth = 0;
65  int32_t outHeight = 0;
66  CFX_WideString filtercontents =
67      static_cast<CBC_OneDimWriter*>(m_pBCWriter.get())
68          ->FilterContents(contents);
69  CFX_ByteString byteString = filtercontents.UTF8Encode();
70  m_renderContents = filtercontents;
71  uint8_t* data = static_cast<CBC_OnedCodaBarWriter*>(m_pBCWriter.get())
72                      ->Encode(byteString, format, outWidth, outHeight, e);
73  if (e != BCExceptionNO)
74    return false;
75  static_cast<CBC_OneDimWriter*>(m_pBCWriter.get())
76      ->RenderResult(filtercontents.AsStringC(), data, outWidth, isDevice, e);
77  FX_Free(data);
78  if (e != BCExceptionNO)
79    return false;
80  return true;
81}
82
83bool CBC_Codabar::RenderDevice(CFX_RenderDevice* device,
84                               const CFX_Matrix* matrix,
85                               int32_t& e) {
86  CFX_WideString renderCon =
87      static_cast<CBC_OnedCodaBarWriter*>(m_pBCWriter.get())
88          ->encodedContents(m_renderContents.AsStringC());
89  static_cast<CBC_OneDimWriter*>(m_pBCWriter.get())
90      ->RenderDeviceResult(device, matrix, renderCon.AsStringC(), e);
91  if (e != BCExceptionNO)
92    return false;
93  return true;
94}
95
96bool CBC_Codabar::RenderBitmap(CFX_DIBitmap*& pOutBitmap, int32_t& e) {
97  CFX_WideString renderCon =
98      static_cast<CBC_OnedCodaBarWriter*>(m_pBCWriter.get())
99          ->encodedContents(m_renderContents.AsStringC());
100  static_cast<CBC_OneDimWriter*>(m_pBCWriter.get())
101      ->RenderBitmapResult(pOutBitmap, renderCon.AsStringC(), e);
102  if (e != BCExceptionNO)
103    return false;
104  return true;
105}
106
107BC_TYPE CBC_Codabar::GetType() {
108  return BC_CODABAR;
109}
110