xref: /aosp_15_r20/external/pdfium/fxbarcode/cbc_code128.cpp (revision 3ac0a46f773bac49fa9476ec2b1cf3f8da5ec3a4)
1*3ac0a46fSAndroid Build Coastguard Worker // Copyright 2016 The PDFium Authors
2*3ac0a46fSAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*3ac0a46fSAndroid Build Coastguard Worker // found in the LICENSE file.
4*3ac0a46fSAndroid Build Coastguard Worker 
5*3ac0a46fSAndroid Build Coastguard Worker // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6*3ac0a46fSAndroid Build Coastguard Worker /*
7*3ac0a46fSAndroid Build Coastguard Worker  * Copyright 2011 ZXing authors
8*3ac0a46fSAndroid Build Coastguard Worker  *
9*3ac0a46fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
10*3ac0a46fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
11*3ac0a46fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
12*3ac0a46fSAndroid Build Coastguard Worker  *
13*3ac0a46fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
14*3ac0a46fSAndroid Build Coastguard Worker  *
15*3ac0a46fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
16*3ac0a46fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
17*3ac0a46fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18*3ac0a46fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
19*3ac0a46fSAndroid Build Coastguard Worker  * limitations under the License.
20*3ac0a46fSAndroid Build Coastguard Worker  */
21*3ac0a46fSAndroid Build Coastguard Worker 
22*3ac0a46fSAndroid Build Coastguard Worker #include "fxbarcode/cbc_code128.h"
23*3ac0a46fSAndroid Build Coastguard Worker 
24*3ac0a46fSAndroid Build Coastguard Worker #include <memory>
25*3ac0a46fSAndroid Build Coastguard Worker 
26*3ac0a46fSAndroid Build Coastguard Worker #include "core/fxcrt/fx_coordinates.h"
27*3ac0a46fSAndroid Build Coastguard Worker #include "fxbarcode/oned/BC_OnedCode128Writer.h"
28*3ac0a46fSAndroid Build Coastguard Worker 
CBC_Code128(BC_TYPE type)29*3ac0a46fSAndroid Build Coastguard Worker CBC_Code128::CBC_Code128(BC_TYPE type)
30*3ac0a46fSAndroid Build Coastguard Worker     : CBC_OneCode(std::make_unique<CBC_OnedCode128Writer>(type)) {}
31*3ac0a46fSAndroid Build Coastguard Worker 
32*3ac0a46fSAndroid Build Coastguard Worker CBC_Code128::~CBC_Code128() = default;
33*3ac0a46fSAndroid Build Coastguard Worker 
Encode(WideStringView contents)34*3ac0a46fSAndroid Build Coastguard Worker bool CBC_Code128::Encode(WideStringView contents) {
35*3ac0a46fSAndroid Build Coastguard Worker   auto* pWriter = GetOnedCode128Writer();
36*3ac0a46fSAndroid Build Coastguard Worker   if (!pWriter->CheckContentValidity(contents))
37*3ac0a46fSAndroid Build Coastguard Worker     return false;
38*3ac0a46fSAndroid Build Coastguard Worker 
39*3ac0a46fSAndroid Build Coastguard Worker   WideString content(contents);
40*3ac0a46fSAndroid Build Coastguard Worker   if (contents.GetLength() % 2 && pWriter->GetType() == BC_TYPE::kCode128C)
41*3ac0a46fSAndroid Build Coastguard Worker     content += '0';
42*3ac0a46fSAndroid Build Coastguard Worker 
43*3ac0a46fSAndroid Build Coastguard Worker   m_renderContents = pWriter->FilterContents(content.AsStringView());
44*3ac0a46fSAndroid Build Coastguard Worker   ByteString byteString = m_renderContents.ToUTF8();
45*3ac0a46fSAndroid Build Coastguard Worker   return pWriter->RenderResult(m_renderContents.AsStringView(),
46*3ac0a46fSAndroid Build Coastguard Worker                                pWriter->Encode(byteString));
47*3ac0a46fSAndroid Build Coastguard Worker }
48*3ac0a46fSAndroid Build Coastguard Worker 
RenderDevice(CFX_RenderDevice * device,const CFX_Matrix & matrix)49*3ac0a46fSAndroid Build Coastguard Worker bool CBC_Code128::RenderDevice(CFX_RenderDevice* device,
50*3ac0a46fSAndroid Build Coastguard Worker                                const CFX_Matrix& matrix) {
51*3ac0a46fSAndroid Build Coastguard Worker   return GetOnedCode128Writer()->RenderDeviceResult(
52*3ac0a46fSAndroid Build Coastguard Worker       device, matrix, m_renderContents.AsStringView());
53*3ac0a46fSAndroid Build Coastguard Worker }
54*3ac0a46fSAndroid Build Coastguard Worker 
GetType()55*3ac0a46fSAndroid Build Coastguard Worker BC_TYPE CBC_Code128::GetType() {
56*3ac0a46fSAndroid Build Coastguard Worker   return BC_TYPE::kCode128;
57*3ac0a46fSAndroid Build Coastguard Worker }
58*3ac0a46fSAndroid Build Coastguard Worker 
GetOnedCode128Writer()59*3ac0a46fSAndroid Build Coastguard Worker CBC_OnedCode128Writer* CBC_Code128::GetOnedCode128Writer() {
60*3ac0a46fSAndroid Build Coastguard Worker   return static_cast<CBC_OnedCode128Writer*>(m_pBCWriter.get());
61*3ac0a46fSAndroid Build Coastguard Worker }
62