blob: 039e37275d9e0f47a8394712ed5eedc093225052 [file] [log] [blame]
Dan Sinclaira98600a2016-03-21 15:15:56 -04001// 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_code128.h"
23
24#include "xfa/fxbarcode/BC_BinaryBitmap.h"
25#include "xfa/fxbarcode/BC_BufferedImageLuminanceSource.h"
26#include "xfa/fxbarcode/common/BC_GlobalHistogramBinarizer.h"
27#include "xfa/fxbarcode/oned/BC_OnedCode128Reader.h"
28#include "xfa/fxbarcode/oned/BC_OnedCode128Writer.h"
29
30CBC_Code128::CBC_Code128(BC_TYPE type) {
31 m_pBCReader = (CBC_Reader*)new (CBC_OnedCode128Reader);
32 m_pBCWriter = (CBC_Writer*)new CBC_OnedCode128Writer(type);
33}
34
35CBC_Code128::~CBC_Code128() {
36 delete (m_pBCReader);
37 delete (m_pBCWriter);
38}
39
40FX_BOOL CBC_Code128::SetTextLocation(BC_TEXT_LOC location) {
41 if (m_pBCWriter)
42 return ((CBC_OnedCode128Writer*)m_pBCWriter)->SetTextLocation(location);
43 return FALSE;
44}
45
46FX_BOOL CBC_Code128::Encode(const CFX_WideStringC& contents,
47 FX_BOOL isDevice,
48 int32_t& e) {
49 if (contents.IsEmpty()) {
50 e = BCExceptionNoContents;
51 return FALSE;
52 }
53 BCFORMAT format = BCFORMAT_CODE_128;
54 int32_t outWidth = 0;
55 int32_t outHeight = 0;
56 CFX_WideString content = contents;
57 if (contents.GetLength() % 2 &&
58 ((CBC_OnedCode128Writer*)m_pBCWriter)->GetType() == BC_CODE128_C) {
59 content += '0';
60 }
tsepezfc58ad12016-04-05 12:22:15 -070061 CFX_WideString encodeContents = ((CBC_OnedCode128Writer*)m_pBCWriter)
tsepez4c3debb2016-04-08 12:20:38 -070062 ->FilterContents(content.AsStringC());
Dan Sinclaira98600a2016-03-21 15:15:56 -040063 m_renderContents = encodeContents;
64 CFX_ByteString byteString = encodeContents.UTF8Encode();
65 uint8_t* data = static_cast<CBC_OnedCode128Writer*>(m_pBCWriter)
66 ->Encode(byteString, format, outWidth, outHeight, e);
67 BC_EXCEPTION_CHECK_ReturnValue(e, FALSE);
68 ((CBC_OneDimWriter*)m_pBCWriter)
tsepez4c3debb2016-04-08 12:20:38 -070069 ->RenderResult(encodeContents.AsStringC(), data, outWidth, isDevice, e);
Dan Sinclaira98600a2016-03-21 15:15:56 -040070 FX_Free(data);
71 BC_EXCEPTION_CHECK_ReturnValue(e, FALSE);
72 return TRUE;
73}
74
75FX_BOOL CBC_Code128::RenderDevice(CFX_RenderDevice* device,
thestigfbe14b92016-05-02 13:31:10 -070076 const CFX_Matrix* matrix,
Dan Sinclaira98600a2016-03-21 15:15:56 -040077 int32_t& e) {
78 ((CBC_OneDimWriter*)m_pBCWriter)
thestigfbe14b92016-05-02 13:31:10 -070079 ->RenderDeviceResult(device, matrix, m_renderContents.AsStringC(), e);
Dan Sinclaira98600a2016-03-21 15:15:56 -040080 BC_EXCEPTION_CHECK_ReturnValue(e, FALSE);
81 return TRUE;
82}
83
84FX_BOOL CBC_Code128::RenderBitmap(CFX_DIBitmap*& pOutBitmap, int32_t& e) {
85 ((CBC_OneDimWriter*)m_pBCWriter)
tsepez4c3debb2016-04-08 12:20:38 -070086 ->RenderBitmapResult(pOutBitmap, m_renderContents.AsStringC(), e);
Dan Sinclaira98600a2016-03-21 15:15:56 -040087 BC_EXCEPTION_CHECK_ReturnValue(e, FALSE);
88 return TRUE;
89}
90
91CFX_WideString CBC_Code128::Decode(uint8_t* buf,
92 int32_t width,
thestigfbe14b92016-05-02 13:31:10 -070093 int32_t height,
Dan Sinclaira98600a2016-03-21 15:15:56 -040094 int32_t& e) {
95 CFX_WideString str;
96 return str;
97}
98
99CFX_WideString CBC_Code128::Decode(CFX_DIBitmap* pBitmap, int32_t& e) {
100 CBC_BufferedImageLuminanceSource source(pBitmap);
101 CBC_GlobalHistogramBinarizer binarizer(&source);
102 CBC_BinaryBitmap bitmap(&binarizer);
103 CFX_ByteString str = m_pBCReader->Decode(&bitmap, 0, e);
104 BC_EXCEPTION_CHECK_ReturnValue(e, FX_WSTRC(L""));
tsepez4c3debb2016-04-08 12:20:38 -0700105 return CFX_WideString::FromUTF8(str.AsStringC());
Dan Sinclaira98600a2016-03-21 15:15:56 -0400106}