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