xref: /aosp_15_r20/external/pdfium/fxbarcode/qrcode/BC_QRCoderMode.cpp (revision 3ac0a46f773bac49fa9476ec2b1cf3f8da5ec3a4)
1 // Copyright 2014 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 // Original code is licensed as follows:
7 /*
8  * Copyright 2007 ZXing authors
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  *      http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  */
22 
23 #include "fxbarcode/qrcode/BC_QRCoderMode.h"
24 
25 #include <utility>
26 
27 #include "core/fxcrt/fx_system.h"
28 #include "third_party/base/check.h"
29 
30 CBC_QRCoderMode* CBC_QRCoderMode::sBYTE = nullptr;
31 CBC_QRCoderMode* CBC_QRCoderMode::sNUMERIC = nullptr;
32 CBC_QRCoderMode* CBC_QRCoderMode::sALPHANUMERIC = nullptr;
33 
CBC_QRCoderMode(std::vector<int32_t> charCountBits,int32_t bits)34 CBC_QRCoderMode::CBC_QRCoderMode(std::vector<int32_t> charCountBits,
35                                  int32_t bits)
36     : m_characterCountBitsForVersions(std::move(charCountBits)), m_bits(bits) {}
37 
38 CBC_QRCoderMode::~CBC_QRCoderMode() = default;
39 
Initialize()40 void CBC_QRCoderMode::Initialize() {
41   sBYTE = new CBC_QRCoderMode({8, 16, 16}, 0x4);
42   sALPHANUMERIC = new CBC_QRCoderMode({9, 11, 13}, 0x2);
43   sNUMERIC = new CBC_QRCoderMode({10, 12, 14}, 0x1);
44 }
45 
Finalize()46 void CBC_QRCoderMode::Finalize() {
47   delete sBYTE;
48   sBYTE = nullptr;
49   delete sALPHANUMERIC;
50   sALPHANUMERIC = nullptr;
51   delete sNUMERIC;
52   sNUMERIC = nullptr;
53 }
54 
GetBits() const55 int32_t CBC_QRCoderMode::GetBits() const {
56   return m_bits;
57 }
58 
GetCharacterCountBits(int32_t number) const59 int32_t CBC_QRCoderMode::GetCharacterCountBits(int32_t number) const {
60   if (m_characterCountBitsForVersions.empty())
61     return 0;
62 
63   int32_t offset;
64   if (number <= 9)
65     offset = 0;
66   else if (number <= 26)
67     offset = 1;
68   else
69     offset = 2;
70 
71   int32_t result = m_characterCountBitsForVersions[offset];
72   DCHECK(result != 0);
73   return result;
74 }
75