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 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 "xfa/src/fxbarcode/barcode.h"
24 #include "xfa/src/fxbarcode/BC_Dimension.h"
25 #include "xfa/src/fxbarcode/common/BC_CommonBitMatrix.h"
26 #include "BC_Encoder.h"
27 #include "BC_SymbolShapeHint.h"
28 #include "BC_SymbolInfo.h"
29 #include "BC_DataMatrixSymbolInfo144.h"
30 #define SYMBOLS_COUNT 30
31 CBC_SymbolInfo* CBC_SymbolInfo::m_PROD_SYMBOLS[30] = {
32 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
33 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
34 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
35 CBC_SymbolInfo* CBC_SymbolInfo::m_symbols[30] = {
36 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
37 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
38 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
Initialize()39 void CBC_SymbolInfo::Initialize() {
40 m_PROD_SYMBOLS[0] = new CBC_SymbolInfo(FALSE, 3, 5, 8, 8, 1);
41 m_PROD_SYMBOLS[1] = new CBC_SymbolInfo(FALSE, 5, 7, 10, 10, 1);
42 m_PROD_SYMBOLS[2] = new CBC_SymbolInfo(TRUE, 5, 7, 16, 6, 1);
43 m_PROD_SYMBOLS[3] = new CBC_SymbolInfo(FALSE, 8, 10, 12, 12, 1);
44 m_PROD_SYMBOLS[4] = new CBC_SymbolInfo(TRUE, 10, 11, 14, 6, 2);
45 m_PROD_SYMBOLS[5] = new CBC_SymbolInfo(FALSE, 12, 12, 14, 14, 1);
46 m_PROD_SYMBOLS[6] = new CBC_SymbolInfo(TRUE, 16, 14, 24, 10, 1);
47 m_PROD_SYMBOLS[7] = new CBC_SymbolInfo(FALSE, 18, 14, 16, 16, 1);
48 m_PROD_SYMBOLS[8] = new CBC_SymbolInfo(FALSE, 22, 18, 18, 18, 1);
49 m_PROD_SYMBOLS[9] = new CBC_SymbolInfo(TRUE, 22, 18, 16, 10, 2);
50 m_PROD_SYMBOLS[10] = new CBC_SymbolInfo(FALSE, 30, 20, 20, 20, 1);
51 m_PROD_SYMBOLS[11] = new CBC_SymbolInfo(TRUE, 32, 24, 16, 14, 2);
52 m_PROD_SYMBOLS[12] = new CBC_SymbolInfo(FALSE, 36, 24, 22, 22, 1);
53 m_PROD_SYMBOLS[13] = new CBC_SymbolInfo(FALSE, 44, 28, 24, 24, 1);
54 m_PROD_SYMBOLS[14] = new CBC_SymbolInfo(TRUE, 49, 28, 22, 14, 2);
55 m_PROD_SYMBOLS[15] = new CBC_SymbolInfo(FALSE, 62, 36, 14, 14, 4);
56 m_PROD_SYMBOLS[16] = new CBC_SymbolInfo(FALSE, 86, 42, 16, 16, 4);
57 m_PROD_SYMBOLS[17] = new CBC_SymbolInfo(FALSE, 114, 48, 18, 18, 4);
58 m_PROD_SYMBOLS[18] = new CBC_SymbolInfo(FALSE, 144, 56, 20, 20, 4);
59 m_PROD_SYMBOLS[19] = new CBC_SymbolInfo(FALSE, 174, 68, 22, 22, 4);
60 m_PROD_SYMBOLS[20] = new CBC_SymbolInfo(FALSE, 204, 84, 24, 24, 4, 102, 42);
61 m_PROD_SYMBOLS[21] = new CBC_SymbolInfo(FALSE, 280, 112, 14, 14, 16, 140, 56);
62 m_PROD_SYMBOLS[22] = new CBC_SymbolInfo(FALSE, 368, 144, 16, 16, 16, 92, 36);
63 m_PROD_SYMBOLS[23] = new CBC_SymbolInfo(FALSE, 456, 192, 18, 18, 16, 114, 48);
64 m_PROD_SYMBOLS[24] = new CBC_SymbolInfo(FALSE, 576, 224, 20, 20, 16, 144, 56);
65 m_PROD_SYMBOLS[25] = new CBC_SymbolInfo(FALSE, 696, 272, 22, 22, 16, 174, 68);
66 m_PROD_SYMBOLS[26] = new CBC_SymbolInfo(FALSE, 816, 336, 24, 24, 16, 136, 56);
67 m_PROD_SYMBOLS[27] =
68 new CBC_SymbolInfo(FALSE, 1050, 408, 18, 18, 36, 175, 68);
69 m_PROD_SYMBOLS[28] =
70 new CBC_SymbolInfo(FALSE, 1304, 496, 20, 20, 36, 163, 62);
71 m_PROD_SYMBOLS[29] = new CBC_DataMatrixSymbolInfo144();
72 for (int32_t i = 0; i < SYMBOLS_COUNT; i++) {
73 m_symbols[i] = m_PROD_SYMBOLS[i];
74 }
75 }
Finalize()76 void CBC_SymbolInfo::Finalize() {
77 for (int32_t i = 0; i < SYMBOLS_COUNT; i++) {
78 delete m_PROD_SYMBOLS[i];
79 m_PROD_SYMBOLS[i] = NULL;
80 m_symbols[i] = NULL;
81 }
82 }
CBC_SymbolInfo(FX_BOOL rectangular,int32_t dataCapacity,int32_t errorCodewords,int32_t matrixWidth,int32_t matrixHeight,int32_t dataRegions)83 CBC_SymbolInfo::CBC_SymbolInfo(FX_BOOL rectangular,
84 int32_t dataCapacity,
85 int32_t errorCodewords,
86 int32_t matrixWidth,
87 int32_t matrixHeight,
88 int32_t dataRegions) {
89 m_rectangular = rectangular;
90 m_dataCapacity = dataCapacity;
91 m_errorCodewords = errorCodewords;
92 m_matrixWidth = matrixWidth;
93 m_matrixHeight = matrixHeight;
94 m_dataRegions = dataRegions;
95 m_rsBlockData = dataCapacity;
96 m_rsBlockError = errorCodewords;
97 }
CBC_SymbolInfo(FX_BOOL rectangular,int32_t dataCapacity,int32_t errorCodewords,int32_t matrixWidth,int32_t matrixHeight,int32_t dataRegions,int32_t rsBlockData,int32_t rsBlockError)98 CBC_SymbolInfo::CBC_SymbolInfo(FX_BOOL rectangular,
99 int32_t dataCapacity,
100 int32_t errorCodewords,
101 int32_t matrixWidth,
102 int32_t matrixHeight,
103 int32_t dataRegions,
104 int32_t rsBlockData,
105 int32_t rsBlockError) {
106 m_rectangular = rectangular;
107 m_dataCapacity = dataCapacity;
108 m_errorCodewords = errorCodewords;
109 m_matrixWidth = matrixWidth;
110 m_matrixHeight = matrixHeight;
111 m_dataRegions = dataRegions;
112 m_rsBlockData = rsBlockData;
113 m_rsBlockError = rsBlockError;
114 }
~CBC_SymbolInfo()115 CBC_SymbolInfo::~CBC_SymbolInfo() {}
116
lookup(int32_t dataCodewords,int32_t & e)117 CBC_SymbolInfo* CBC_SymbolInfo::lookup(int32_t dataCodewords, int32_t& e) {
118 return lookup(dataCodewords, FORCE_NONE, TRUE, e);
119 }
lookup(int32_t dataCodewords,SymbolShapeHint shape,int32_t & e)120 CBC_SymbolInfo* CBC_SymbolInfo::lookup(int32_t dataCodewords,
121 SymbolShapeHint shape,
122 int32_t& e) {
123 return lookup(dataCodewords, shape, TRUE, e);
124 }
lookup(int32_t dataCodewords,FX_BOOL allowRectangular,FX_BOOL fail,int32_t & e)125 CBC_SymbolInfo* CBC_SymbolInfo::lookup(int32_t dataCodewords,
126 FX_BOOL allowRectangular,
127 FX_BOOL fail,
128 int32_t& e) {
129 SymbolShapeHint shape = allowRectangular ? FORCE_NONE : FORCE_SQUARE;
130 return lookup(dataCodewords, shape, fail, e);
131 }
lookup(int32_t dataCodewords,SymbolShapeHint shape,FX_BOOL fail,int32_t & e)132 CBC_SymbolInfo* CBC_SymbolInfo::lookup(int32_t dataCodewords,
133 SymbolShapeHint shape,
134 FX_BOOL fail,
135 int32_t& e) {
136 return lookup(dataCodewords, shape, NULL, NULL, fail, e);
137 }
lookup(int32_t dataCodewords,SymbolShapeHint shape,CBC_Dimension * minSize,CBC_Dimension * maxSize,FX_BOOL fail,int32_t & e)138 CBC_SymbolInfo* CBC_SymbolInfo::lookup(int32_t dataCodewords,
139 SymbolShapeHint shape,
140 CBC_Dimension* minSize,
141 CBC_Dimension* maxSize,
142 FX_BOOL fail,
143 int32_t& e) {
144 for (int32_t i = 0; i < SYMBOLS_COUNT; i++) {
145 CBC_SymbolInfo* symbol = m_symbols[i];
146 if (shape == FORCE_SQUARE && symbol->m_rectangular) {
147 continue;
148 }
149 if (shape == FORCE_RECTANGLE && !symbol->m_rectangular) {
150 continue;
151 }
152 if (minSize != NULL &&
153 (symbol->getSymbolWidth(e) < minSize->getWidth() ||
154 symbol->getSymbolHeight(e) < minSize->getHeight())) {
155 BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
156 continue;
157 }
158 if (maxSize != NULL &&
159 (symbol->getSymbolWidth(e) > maxSize->getWidth() ||
160 symbol->getSymbolHeight(e) > maxSize->getHeight())) {
161 BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
162 continue;
163 }
164 if (dataCodewords <= symbol->m_dataCapacity) {
165 return symbol;
166 }
167 }
168 if (fail) {
169 e = BCExceptionIllegalDataCodewords;
170 return NULL;
171 }
172 return NULL;
173 }
getHorizontalDataRegions(int32_t & e)174 int32_t CBC_SymbolInfo::getHorizontalDataRegions(int32_t& e) {
175 switch (m_dataRegions) {
176 case 1:
177 return 1;
178 case 2:
179 return 2;
180 case 4:
181 return 2;
182 case 16:
183 return 4;
184 case 36:
185 return 6;
186 default:
187 e = BCExceptionCannotHandleThisNumberOfDataRegions;
188 return 0;
189 }
190 }
getVerticalDataRegions(int32_t & e)191 int32_t CBC_SymbolInfo::getVerticalDataRegions(int32_t& e) {
192 switch (m_dataRegions) {
193 case 1:
194 return 1;
195 case 2:
196 return 1;
197 case 4:
198 return 2;
199 case 16:
200 return 4;
201 case 36:
202 return 6;
203 default:
204 e = BCExceptionCannotHandleThisNumberOfDataRegions;
205 return 0;
206 }
207 }
getSymbolDataWidth(int32_t & e)208 int32_t CBC_SymbolInfo::getSymbolDataWidth(int32_t& e) {
209 return getHorizontalDataRegions(e) * m_matrixWidth;
210 }
getSymbolDataHeight(int32_t & e)211 int32_t CBC_SymbolInfo::getSymbolDataHeight(int32_t& e) {
212 return getVerticalDataRegions(e) * m_matrixHeight;
213 }
getSymbolWidth(int32_t & e)214 int32_t CBC_SymbolInfo::getSymbolWidth(int32_t& e) {
215 return getSymbolDataWidth(e) + (getHorizontalDataRegions(e) * 2);
216 }
getSymbolHeight(int32_t & e)217 int32_t CBC_SymbolInfo::getSymbolHeight(int32_t& e) {
218 return getSymbolDataHeight(e) + (getVerticalDataRegions(e) * 2);
219 }
getCodewordCount()220 int32_t CBC_SymbolInfo::getCodewordCount() {
221 return m_dataCapacity + m_errorCodewords;
222 }
getInterleavedBlockCount()223 int32_t CBC_SymbolInfo::getInterleavedBlockCount() {
224 return m_dataCapacity / m_rsBlockData;
225 }
getDataLengthForInterleavedBlock(int32_t index)226 int32_t CBC_SymbolInfo::getDataLengthForInterleavedBlock(int32_t index) {
227 return m_rsBlockData;
228 }
getErrorLengthForInterleavedBlock(int32_t index)229 int32_t CBC_SymbolInfo::getErrorLengthForInterleavedBlock(int32_t index) {
230 return m_rsBlockError;
231 }
toString(int32_t & e)232 CFX_WideString CBC_SymbolInfo::toString(int32_t& e) {
233 CFX_WideString sb;
234 sb += (FX_WCHAR*)(m_rectangular ? "Rectangular Symbol:" : "Square Symbol:");
235 sb += (FX_WCHAR*)" data region ";
236 sb += m_matrixWidth;
237 sb += (FX_WCHAR)'x';
238 sb += m_matrixHeight;
239 sb += (FX_WCHAR*)", symbol size ";
240 sb += getSymbolWidth(e);
241 BC_EXCEPTION_CHECK_ReturnValue(e, (FX_WCHAR*)"");
242 sb += (FX_WCHAR)'x';
243 sb += getSymbolHeight(e);
244 BC_EXCEPTION_CHECK_ReturnValue(e, (FX_WCHAR*)"");
245 sb += (FX_WCHAR*)", symbol data size ";
246 sb += getSymbolDataWidth(e);
247 BC_EXCEPTION_CHECK_ReturnValue(e, (FX_WCHAR*)"");
248 sb += (FX_WCHAR)'x';
249 sb += getSymbolDataHeight(e);
250 BC_EXCEPTION_CHECK_ReturnValue(e, (FX_WCHAR*)"");
251 sb += (FX_WCHAR*)", codewords ";
252 sb += m_dataCapacity;
253 sb += (FX_WCHAR)'+';
254 sb += m_errorCodewords;
255 return sb;
256 }
257