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