1 // Copyright 2014 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 // 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_QRCoderVersion.h" 24 25 #include <memory> 26 #include <vector> 27 28 #include "fxbarcode/common/BC_CommonBitMatrix.h" 29 #include "fxbarcode/qrcode/BC_QRCoderBitVector.h" 30 #include "fxbarcode/qrcode/BC_QRCoderECBlocksData.h" 31 #include "fxbarcode/qrcode/BC_QRCoderErrorCorrectionLevel.h" 32 #include "third_party/base/ptr_util.h" 33 34 namespace { 35 36 std::vector<std::unique_ptr<CBC_QRCoderVersion>>* g_VERSION = nullptr; 37 38 } // namespace 39 CBC_QRCoderVersion(int32_t versionNumber,const CBC_QRCoderECBlockData data[4])40CBC_QRCoderVersion::CBC_QRCoderVersion(int32_t versionNumber, 41 const CBC_QRCoderECBlockData data[4]) 42 : m_versionNumber(versionNumber) { 43 m_ecBlocksArray[0] = pdfium::MakeUnique<CBC_QRCoderECBlocks>(data[0]); 44 m_ecBlocksArray[1] = pdfium::MakeUnique<CBC_QRCoderECBlocks>(data[1]); 45 m_ecBlocksArray[2] = pdfium::MakeUnique<CBC_QRCoderECBlocks>(data[2]); 46 m_ecBlocksArray[3] = pdfium::MakeUnique<CBC_QRCoderECBlocks>(data[3]); 47 m_totalCodeWords = m_ecBlocksArray[0]->GetTotalDataCodeWords(); 48 } 49 50 CBC_QRCoderVersion::~CBC_QRCoderVersion() = default; 51 52 // static Initialize()53void CBC_QRCoderVersion::Initialize() { 54 g_VERSION = new std::vector<std::unique_ptr<CBC_QRCoderVersion>>(); 55 } 56 57 // static Finalize()58void CBC_QRCoderVersion::Finalize() { 59 delete g_VERSION; 60 g_VERSION = nullptr; 61 } 62 63 // static GetVersionForNumber(int32_t versionNumber)64const CBC_QRCoderVersion* CBC_QRCoderVersion::GetVersionForNumber( 65 int32_t versionNumber) { 66 if (g_VERSION->empty()) { 67 for (int i = 0; i < kMaxVersion; ++i) { 68 g_VERSION->push_back( 69 pdfium::MakeUnique<CBC_QRCoderVersion>(i + 1, g_ECBData[i])); 70 } 71 } 72 if (versionNumber < 1 || versionNumber > kMaxVersion) 73 return nullptr; 74 return (*g_VERSION)[versionNumber - 1].get(); 75 } 76 GetVersionNumber() const77int32_t CBC_QRCoderVersion::GetVersionNumber() const { 78 return m_versionNumber; 79 } 80 GetTotalCodeWords() const81int32_t CBC_QRCoderVersion::GetTotalCodeWords() const { 82 return m_totalCodeWords; 83 } 84 GetDimensionForVersion() const85int32_t CBC_QRCoderVersion::GetDimensionForVersion() const { 86 return 17 + 4 * m_versionNumber; 87 } 88 GetECBlocksForLevel(const CBC_QRCoderErrorCorrectionLevel & ecLevel) const89const CBC_QRCoderECBlocks* CBC_QRCoderVersion::GetECBlocksForLevel( 90 const CBC_QRCoderErrorCorrectionLevel& ecLevel) const { 91 return m_ecBlocksArray[ecLevel.Ordinal()].get(); 92 } 93