• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2018 The PDFium Authors
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 #include "fxbarcode/cbc_eancode.h"
8 
9 #include <utility>
10 
11 #include "fxbarcode/BC_Library.h"
12 #include "fxbarcode/oned/BC_OnedEANWriter.h"
13 
CBC_EANCode(std::unique_ptr<CBC_OneDimEANWriter> pWriter)14 CBC_EANCode::CBC_EANCode(std::unique_ptr<CBC_OneDimEANWriter> pWriter)
15     : CBC_OneCode(std::move(pWriter)) {}
16 
17 CBC_EANCode::~CBC_EANCode() = default;
18 
GetOneDimEANWriter()19 CBC_OneDimEANWriter* CBC_EANCode::GetOneDimEANWriter() {
20   return static_cast<CBC_OneDimEANWriter*>(m_pBCWriter.get());
21 }
22 
Encode(WideStringView contents)23 bool CBC_EANCode::Encode(WideStringView contents) {
24   auto* pWriter = GetOneDimEANWriter();
25   if (!pWriter->CheckContentValidity(contents))
26     return false;
27 
28   m_renderContents = Preprocess(contents);
29   ByteString str = m_renderContents.ToUTF8();
30   pWriter->InitEANWriter();
31   return pWriter->RenderResult(m_renderContents.AsStringView(),
32                                pWriter->Encode(str));
33 }
34 
RenderDevice(CFX_RenderDevice * device,const CFX_Matrix & matrix)35 bool CBC_EANCode::RenderDevice(CFX_RenderDevice* device,
36                                const CFX_Matrix& matrix) {
37   return GetOneDimEANWriter()->RenderDeviceResult(
38       device, matrix, m_renderContents.AsStringView());
39 }
40 
Preprocess(WideStringView contents)41 WideString CBC_EANCode::Preprocess(WideStringView contents) {
42   auto* pWriter = GetOneDimEANWriter();
43   WideString encoded_contents = pWriter->FilterContents(contents);
44   size_t length = encoded_contents.GetLength();
45   size_t max_length = GetMaxLength();
46   if (length <= max_length) {
47     for (size_t i = 0; i < max_length - length; i++)
48       encoded_contents.InsertAtFront(L'0');
49 
50     ByteString str = encoded_contents.ToUTF8();
51     int32_t checksum = pWriter->CalcChecksum(str);
52     str += '0' + checksum;
53     encoded_contents = WideString::FromUTF8(str.AsStringView());
54   } else {
55     encoded_contents = encoded_contents.First(max_length + 1);
56   }
57 
58   return encoded_contents;
59 }
60