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 "fxbarcode/cbc_ean8.h"
23
24 #include <memory>
25
26 #include "fxbarcode/oned/BC_OnedEAN8Writer.h"
27 #include "third_party/base/ptr_util.h"
28
CBC_EAN8()29 CBC_EAN8::CBC_EAN8() : CBC_OneCode(pdfium::MakeUnique<CBC_OnedEAN8Writer>()) {}
30
~CBC_EAN8()31 CBC_EAN8::~CBC_EAN8() {}
32
Preprocess(const WideStringView & contents)33 WideString CBC_EAN8::Preprocess(const WideStringView& contents) {
34 auto* pWriter = GetOnedEAN8Writer();
35 WideString encodeContents = pWriter->FilterContents(contents);
36 int32_t length = encodeContents.GetLength();
37 if (length <= 7) {
38 for (int32_t i = 0; i < 7 - length; i++)
39 encodeContents = wchar_t('0') + encodeContents;
40
41 ByteString byteString = encodeContents.UTF8Encode();
42 int32_t checksum = pWriter->CalcChecksum(byteString);
43 encodeContents += wchar_t(checksum - 0 + '0');
44 }
45 if (length > 8)
46 encodeContents = encodeContents.Left(8);
47
48 return encodeContents;
49 }
50
Encode(const WideStringView & contents)51 bool CBC_EAN8::Encode(const WideStringView& contents) {
52 if (contents.IsEmpty())
53 return false;
54
55 BCFORMAT format = BCFORMAT_EAN_8;
56 int32_t outWidth = 0;
57 int32_t outHeight = 0;
58 WideString encodeContents = Preprocess(contents);
59 ByteString byteString = encodeContents.UTF8Encode();
60 m_renderContents = encodeContents;
61 auto* pWriter = GetOnedEAN8Writer();
62 std::unique_ptr<uint8_t, FxFreeDeleter> data(
63 pWriter->Encode(byteString, format, outWidth, outHeight));
64 if (!data)
65 return false;
66 return pWriter->RenderResult(encodeContents.AsStringView(), data.get(),
67 outWidth);
68 }
69
RenderDevice(CFX_RenderDevice * device,const CFX_Matrix * matrix)70 bool CBC_EAN8::RenderDevice(CFX_RenderDevice* device,
71 const CFX_Matrix* matrix) {
72 return GetOnedEAN8Writer()->RenderDeviceResult(
73 device, matrix, m_renderContents.AsStringView());
74 }
75
GetType()76 BC_TYPE CBC_EAN8::GetType() {
77 return BC_EAN8;
78 }
79
GetOnedEAN8Writer()80 CBC_OnedEAN8Writer* CBC_EAN8::GetOnedEAN8Writer() {
81 return static_cast<CBC_OnedEAN8Writer*>(m_pBCWriter.get());
82 }
83