xref: /aosp_15_r20/external/pdfium/fxbarcode/datamatrix/BC_SymbolInfo.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 2006 Jeremias Maerki
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/datamatrix/BC_SymbolInfo.h"
24 
25 #include <iterator>
26 
27 #include "fxbarcode/common/BC_CommonBitMatrix.h"
28 #include "fxbarcode/datamatrix/BC_DataMatrixSymbolInfo144.h"
29 #include "fxbarcode/datamatrix/BC_Encoder.h"
30 #include "third_party/base/notreached.h"
31 
32 namespace {
33 
34 constexpr size_t kSymbolsCount = 30;
35 
36 CBC_SymbolInfo* g_symbols[kSymbolsCount] = {
37     nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
38     nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
39     nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
40     nullptr, nullptr, nullptr, nullptr, nullptr, nullptr};
41 
42 constexpr CBC_SymbolInfo::Data kSymbolData[] = {
43     {3, 5, 3, 5, 8, 8, 1},           {5, 7, 5, 7, 10, 10, 1},
44     {5, 7, 5, 7, 16, 6, 1},          {8, 10, 8, 10, 12, 12, 1},
45     {10, 11, 10, 11, 14, 6, 2},      {12, 12, 12, 12, 14, 14, 1},
46     {16, 14, 16, 14, 24, 10, 1},     {18, 14, 18, 14, 16, 16, 1},
47     {22, 18, 22, 18, 18, 18, 1},     {22, 18, 22, 18, 16, 10, 2},
48     {30, 20, 30, 20, 20, 20, 1},     {32, 24, 32, 24, 16, 14, 2},
49     {36, 24, 36, 24, 22, 22, 1},     {44, 28, 44, 28, 24, 24, 1},
50     {49, 28, 49, 28, 22, 14, 2},     {62, 36, 62, 36, 14, 14, 4},
51     {86, 42, 86, 42, 16, 16, 4},     {114, 48, 114, 48, 18, 18, 4},
52     {144, 56, 144, 56, 20, 20, 4},   {174, 68, 174, 68, 22, 22, 4},
53     {204, 84, 102, 42, 24, 24, 4},   {280, 112, 140, 56, 14, 14, 16},
54     {368, 144, 92, 36, 16, 16, 16},  {456, 192, 114, 48, 18, 18, 16},
55     {576, 224, 144, 56, 20, 20, 16}, {696, 272, 174, 68, 22, 22, 16},
56     {816, 336, 136, 56, 24, 24, 16}, {1050, 408, 175, 68, 18, 18, 36},
57     {1304, 496, 163, 62, 20, 20, 36}};
58 
59 constexpr size_t kSymbolDataSize = std::size(kSymbolData);
60 static_assert(kSymbolDataSize + 1 == kSymbolsCount, "Wrong kSymbolDataSize");
61 
62 }  // namespace
63 
64 // static
Initialize()65 void CBC_SymbolInfo::Initialize() {
66   for (size_t i = 0; i < kSymbolDataSize; ++i)
67     g_symbols[i] = new CBC_SymbolInfo(&kSymbolData[i]);
68   g_symbols[kSymbolDataSize] = new CBC_DataMatrixSymbolInfo144();
69 }
70 
71 // static
Finalize()72 void CBC_SymbolInfo::Finalize() {
73   for (size_t i = 0; i < kSymbolsCount; ++i) {
74     delete g_symbols[i];
75     g_symbols[i] = nullptr;
76   }
77 }
78 
CBC_SymbolInfo(const Data * data)79 CBC_SymbolInfo::CBC_SymbolInfo(const Data* data) : data_(data) {}
80 
81 CBC_SymbolInfo::~CBC_SymbolInfo() = default;
82 
Lookup(size_t data_codewords,bool allow_rectangular)83 const CBC_SymbolInfo* CBC_SymbolInfo::Lookup(size_t data_codewords,
84                                              bool allow_rectangular) {
85   for (size_t i = 0; i < kSymbolsCount; ++i) {
86     CBC_SymbolInfo* symbol = g_symbols[i];
87     if (symbol->is_rectangular() && !allow_rectangular)
88       continue;
89 
90     if (data_codewords <= symbol->data_capacity())
91       return symbol;
92   }
93   return nullptr;
94 }
95 
GetHorizontalDataRegions() const96 int32_t CBC_SymbolInfo::GetHorizontalDataRegions() const {
97   switch (data_->data_regions) {
98     case 1:
99       return 1;
100     case 2:
101       return 2;
102     case 4:
103       return 2;
104     case 16:
105       return 4;
106     case 36:
107       return 6;
108     default:
109       NOTREACHED_NORETURN();
110   }
111 }
112 
GetVerticalDataRegions() const113 int32_t CBC_SymbolInfo::GetVerticalDataRegions() const {
114   switch (data_->data_regions) {
115     case 1:
116       return 1;
117     case 2:
118       return 1;
119     case 4:
120       return 2;
121     case 16:
122       return 4;
123     case 36:
124       return 6;
125     default:
126       NOTREACHED_NORETURN();
127   }
128 }
129 
GetSymbolDataWidth() const130 int32_t CBC_SymbolInfo::GetSymbolDataWidth() const {
131   return GetHorizontalDataRegions() * data_->matrix_width;
132 }
133 
GetSymbolDataHeight() const134 int32_t CBC_SymbolInfo::GetSymbolDataHeight() const {
135   return GetVerticalDataRegions() * data_->matrix_height;
136 }
137 
GetSymbolWidth() const138 int32_t CBC_SymbolInfo::GetSymbolWidth() const {
139   return GetSymbolDataWidth() + (GetHorizontalDataRegions() * 2);
140 }
141 
GetSymbolHeight() const142 int32_t CBC_SymbolInfo::GetSymbolHeight() const {
143   return GetSymbolDataHeight() + (GetVerticalDataRegions() * 2);
144 }
145 
GetInterleavedBlockCount() const146 size_t CBC_SymbolInfo::GetInterleavedBlockCount() const {
147   return data_->data_capacity / data_->rs_block_data;
148 }
149 
GetDataLengthForInterleavedBlock() const150 size_t CBC_SymbolInfo::GetDataLengthForInterleavedBlock() const {
151   return data_->rs_block_data;
152 }
153 
GetErrorLengthForInterleavedBlock() const154 size_t CBC_SymbolInfo::GetErrorLengthForInterleavedBlock() const {
155   return data_->rs_block_error;
156 }
157