cbc_codabar.cpp revision 4d3acf4ec42bf6e838f9060103aff98fbf170794
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  BC_EXCEPTION_CHECK_ReturnValue(e, false);
74  static_cast<CBC_OneDimWriter*>(m_pBCWriter.get())
75      ->RenderResult(filtercontents.AsStringC(), data, outWidth, isDevice, e);
76  FX_Free(data);
77  BC_EXCEPTION_CHECK_ReturnValue(e, false);
78  return true;
79}
80
81bool CBC_Codabar::RenderDevice(CFX_RenderDevice* device,
82                               const CFX_Matrix* matrix,
83                               int32_t& e) {
84  CFX_WideString renderCon =
85      static_cast<CBC_OnedCodaBarWriter*>(m_pBCWriter.get())
86          ->encodedContents(m_renderContents.AsStringC());
87  static_cast<CBC_OneDimWriter*>(m_pBCWriter.get())
88      ->RenderDeviceResult(device, matrix, renderCon.AsStringC(), e);
89  BC_EXCEPTION_CHECK_ReturnValue(e, false);
90  return true;
91}
92
93bool CBC_Codabar::RenderBitmap(CFX_DIBitmap*& pOutBitmap, int32_t& e) {
94  CFX_WideString renderCon =
95      static_cast<CBC_OnedCodaBarWriter*>(m_pBCWriter.get())
96          ->encodedContents(m_renderContents.AsStringC());
97  static_cast<CBC_OneDimWriter*>(m_pBCWriter.get())
98      ->RenderBitmapResult(pOutBitmap, renderCon.AsStringC(), e);
99  BC_EXCEPTION_CHECK_ReturnValue(e, false);
100  return true;
101}
102
103BC_TYPE CBC_Codabar::GetType() {
104  return BC_CODABAR;
105}
106