blob: 91d53d462b7a1fb8e828ff239a83efc79716c8fb [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
Dan Sinclaire7786682017-03-29 15:18:41 -040022#include "fxbarcode/cbc_code128.h"
Dan Sinclaira98600a2016-03-21 15:15:56 -040023
Dan Sinclaire7786682017-03-29 15:18:41 -040024#include "fxbarcode/oned/BC_OnedCode128Writer.h"
Dan Sinclaira98600a2016-03-21 15:15:56 -040025
tsepez52d78562016-06-06 12:57:44 -070026CBC_Code128::CBC_Code128(BC_TYPE type)
dsinclaira2615342016-06-16 12:29:07 -070027 : CBC_OneCode(new CBC_OnedCode128Writer(type)) {}
Dan Sinclaira98600a2016-03-21 15:15:56 -040028
tsepez52d78562016-06-06 12:57:44 -070029CBC_Code128::~CBC_Code128() {}
Dan Sinclaira98600a2016-03-21 15:15:56 -040030
tsepezd19e9122016-11-02 15:43:18 -070031bool CBC_Code128::SetTextLocation(BC_TEXT_LOC location) {
Dan Sinclaira98600a2016-03-21 15:15:56 -040032 if (m_pBCWriter)
tsepez52d78562016-06-06 12:57:44 -070033 return static_cast<CBC_OnedCode128Writer*>(m_pBCWriter.get())
34 ->SetTextLocation(location);
tsepezd19e9122016-11-02 15:43:18 -070035 return false;
Dan Sinclaira98600a2016-03-21 15:15:56 -040036}
37
tsepezd19e9122016-11-02 15:43:18 -070038bool CBC_Code128::Encode(const CFX_WideStringC& contents,
39 bool isDevice,
40 int32_t& e) {
Dan Sinclaira98600a2016-03-21 15:15:56 -040041 if (contents.IsEmpty()) {
42 e = BCExceptionNoContents;
tsepezd19e9122016-11-02 15:43:18 -070043 return false;
Dan Sinclaira98600a2016-03-21 15:15:56 -040044 }
45 BCFORMAT format = BCFORMAT_CODE_128;
46 int32_t outWidth = 0;
47 int32_t outHeight = 0;
tsepezafe94302016-05-13 17:21:31 -070048 CFX_WideString content(contents);
Dan Sinclaira98600a2016-03-21 15:15:56 -040049 if (contents.GetLength() % 2 &&
tsepez52d78562016-06-06 12:57:44 -070050 static_cast<CBC_OnedCode128Writer*>(m_pBCWriter.get())->GetType() ==
51 BC_CODE128_C) {
Dan Sinclaira98600a2016-03-21 15:15:56 -040052 content += '0';
53 }
tsepez52d78562016-06-06 12:57:44 -070054 CFX_WideString encodeContents =
55 static_cast<CBC_OnedCode128Writer*>(m_pBCWriter.get())
56 ->FilterContents(content.AsStringC());
Dan Sinclaira98600a2016-03-21 15:15:56 -040057 m_renderContents = encodeContents;
58 CFX_ByteString byteString = encodeContents.UTF8Encode();
tsepez52d78562016-06-06 12:57:44 -070059 uint8_t* data = static_cast<CBC_OnedCode128Writer*>(m_pBCWriter.get())
Dan Sinclaira98600a2016-03-21 15:15:56 -040060 ->Encode(byteString, format, outWidth, outHeight, e);
Tom Sepezc8017b22017-01-31 13:02:10 -080061 if (e != BCExceptionNO)
62 return false;
tsepez52d78562016-06-06 12:57:44 -070063 static_cast<CBC_OneDimWriter*>(m_pBCWriter.get())
tsepez4c3debb2016-04-08 12:20:38 -070064 ->RenderResult(encodeContents.AsStringC(), data, outWidth, isDevice, e);
Dan Sinclaira98600a2016-03-21 15:15:56 -040065 FX_Free(data);
Tom Sepezc8017b22017-01-31 13:02:10 -080066 if (e != BCExceptionNO)
67 return false;
tsepezd19e9122016-11-02 15:43:18 -070068 return true;
Dan Sinclaira98600a2016-03-21 15:15:56 -040069}
70
tsepezd19e9122016-11-02 15:43:18 -070071bool CBC_Code128::RenderDevice(CFX_RenderDevice* device,
72 const CFX_Matrix* matrix,
73 int32_t& e) {
tsepez52d78562016-06-06 12:57:44 -070074 static_cast<CBC_OneDimWriter*>(m_pBCWriter.get())
thestigfbe14b92016-05-02 13:31:10 -070075 ->RenderDeviceResult(device, matrix, m_renderContents.AsStringC(), e);
Tom Sepezc8017b22017-01-31 13:02:10 -080076 if (e != BCExceptionNO)
77 return false;
tsepezd19e9122016-11-02 15:43:18 -070078 return true;
Dan Sinclaira98600a2016-03-21 15:15:56 -040079}
80
Tom Sepezf0799fe2017-03-28 09:31:32 -070081bool CBC_Code128::RenderBitmap(CFX_RetainPtr<CFX_DIBitmap>& pOutBitmap,
82 int32_t& e) {
tsepez52d78562016-06-06 12:57:44 -070083 static_cast<CBC_OneDimWriter*>(m_pBCWriter.get())
tsepez4c3debb2016-04-08 12:20:38 -070084 ->RenderBitmapResult(pOutBitmap, m_renderContents.AsStringC(), e);
Tom Sepezf0799fe2017-03-28 09:31:32 -070085 return e == BCExceptionNO;
Dan Sinclaira98600a2016-03-21 15:15:56 -040086}
87
weili29b8ad02016-06-14 18:20:04 -070088BC_TYPE CBC_Code128::GetType() {
89 return BC_CODE128;
90}