forked from makiuchi-d/gozxing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
symbol_info.go
218 lines (186 loc) · 6.16 KB
/
symbol_info.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package encoder
import (
"fmt"
"github.com/kaxap/gozxing"
)
// Symbol info table for DataMatrix.
var symbols = []*SymbolInfo{
NewSymbolInfo(false, 3, 5, 8, 8, 1),
NewSymbolInfo(false, 5, 7, 10, 10, 1),
/*rect*/ NewSymbolInfo(true, 5, 7, 16, 6, 1),
NewSymbolInfo(false, 8, 10, 12, 12, 1),
/*rect*/ NewSymbolInfo(true, 10, 11, 14, 6, 2),
NewSymbolInfo(false, 12, 12, 14, 14, 1),
/*rect*/ NewSymbolInfo(true, 16, 14, 24, 10, 1),
NewSymbolInfo(false, 18, 14, 16, 16, 1),
NewSymbolInfo(false, 22, 18, 18, 18, 1),
/*rect*/ NewSymbolInfo(true, 22, 18, 16, 10, 2),
NewSymbolInfo(false, 30, 20, 20, 20, 1),
/*rect*/ NewSymbolInfo(true, 32, 24, 16, 14, 2),
NewSymbolInfo(false, 36, 24, 22, 22, 1),
NewSymbolInfo(false, 44, 28, 24, 24, 1),
/*rect*/ NewSymbolInfo(true, 49, 28, 22, 14, 2),
NewSymbolInfo(false, 62, 36, 14, 14, 4),
NewSymbolInfo(false, 86, 42, 16, 16, 4),
NewSymbolInfo(false, 114, 48, 18, 18, 4),
NewSymbolInfo(false, 144, 56, 20, 20, 4),
NewSymbolInfo(false, 174, 68, 22, 22, 4),
NewSymbolInfoRS(false, 204, 84, 24, 24, 4, 102, 42),
NewSymbolInfoRS(false, 280, 112, 14, 14, 16, 140, 56),
NewSymbolInfoRS(false, 368, 144, 16, 16, 16, 92, 36),
NewSymbolInfoRS(false, 456, 192, 18, 18, 16, 114, 48),
NewSymbolInfoRS(false, 576, 224, 20, 20, 16, 144, 56),
NewSymbolInfoRS(false, 696, 272, 22, 22, 16, 174, 68),
NewSymbolInfoRS(false, 816, 336, 24, 24, 16, 136, 56),
NewSymbolInfoRS(false, 1050, 408, 18, 18, 36, 175, 68),
NewSymbolInfoRS(false, 1304, 496, 20, 20, 36, 163, 62),
NewDataMatrixSymbolInfo144(),
}
type SymbolInfo struct {
rectangular bool
dataCapacity int
errorCodewords int
matrixWidth int
matrixHeight int
dataRegions int
rsBlockData int
rsBlockError int
funcGetInterleavedBlockCount func(*SymbolInfo) int
funcGetDataLengthForInterleavedBlock func(*SymbolInfo, int) int
}
func NewSymbolInfo(rectangular bool, dataCapacity, errorCodewords,
matrixWidth, matrixHeight, dataRegions int) *SymbolInfo {
return NewSymbolInfoRS(rectangular, dataCapacity, errorCodewords,
matrixWidth, matrixHeight, dataRegions, dataCapacity, errorCodewords)
}
func NewSymbolInfoRS(rectangular bool, dataCapacity, errorCodewords,
matrixWidth, matrixHeight, dataRegions, rsBlockData, rsBlockError int) *SymbolInfo {
return &SymbolInfo{
rectangular: rectangular,
dataCapacity: dataCapacity,
errorCodewords: errorCodewords,
matrixWidth: matrixWidth,
matrixHeight: matrixHeight,
dataRegions: dataRegions,
rsBlockData: rsBlockData,
rsBlockError: rsBlockError,
funcGetInterleavedBlockCount: defaultGetInterleavedBlockCount,
funcGetDataLengthForInterleavedBlock: defaultGetDataLengthForInterleavedBlock,
}
}
// public static SymbolInfo lookup(int dataCodewords)
// public static SymbolInfo lookup(int dataCodewords, SymbolShapeHint shape)
// public static SymbolInfo lookup(int dataCodewords, boolean allowRectangular, boolean fail)
// private static SymbolInfo lookup(int dataCodewords, SymbolShapeHint shape, boolean fail)
func SymbolInfo_Lookup(dataCodewords int, shape SymbolShapeHint,
minSize, maxSize *gozxing.Dimension, fail bool) (*SymbolInfo, error) {
for _, symbol := range symbols {
if shape == SymbolShapeHint_FORCE_SQUARE && symbol.rectangular {
continue
}
if shape == SymbolShapeHint_FORCE_RECTANGLE && !symbol.rectangular {
continue
}
if minSize != nil &&
(symbol.GetSymbolWidth() < minSize.GetWidth() ||
symbol.GetSymbolHeight() < minSize.GetHeight()) {
continue
}
if maxSize != nil &&
(symbol.GetSymbolWidth() > maxSize.GetWidth() ||
symbol.GetSymbolHeight() > maxSize.GetHeight()) {
continue
}
if dataCodewords <= symbol.dataCapacity {
return symbol, nil
}
}
if fail {
return nil, gozxing.NewWriterException("IllegalArgumentException: "+
"Can't find a symbol arrangement that matches the message. Data codewords: %d",
dataCodewords)
}
return nil, nil
}
func (this *SymbolInfo) getHorizontalDataRegions() int {
switch this.dataRegions {
case 1:
return 1
case 2, 4:
return 2
case 16:
return 4
case 36:
return 6
default:
return 0 // Cannot handle this number of data regions
}
}
func (this *SymbolInfo) getVerticalDataRegions() int {
switch this.dataRegions {
case 1, 2:
return 1
case 4:
return 2
case 16:
return 4
case 36:
return 6
default:
return 0 // Cannot handle this number of data regions
}
}
func (this *SymbolInfo) GetSymbolDataWidth() int {
return this.getHorizontalDataRegions() * this.matrixWidth
}
func (this *SymbolInfo) GetSymbolDataHeight() int {
return this.getVerticalDataRegions() * this.matrixHeight
}
func (this *SymbolInfo) GetSymbolWidth() int {
return this.GetSymbolDataWidth() + (this.getHorizontalDataRegions() * 2)
}
func (this *SymbolInfo) GetSymbolHeight() int {
return this.GetSymbolDataHeight() + (this.getVerticalDataRegions() * 2)
}
func (this *SymbolInfo) GetCodewordCount() int {
return this.dataCapacity + this.errorCodewords
}
func (this *SymbolInfo) GetInterleavedBlockCount() int {
return this.funcGetInterleavedBlockCount(this)
}
func defaultGetInterleavedBlockCount(this *SymbolInfo) int {
return this.dataCapacity / this.rsBlockData
}
func (this *SymbolInfo) GetDataCapacity() int {
return this.dataCapacity
}
func (this *SymbolInfo) GetErrorCodewords() int {
return this.errorCodewords
}
func (this *SymbolInfo) GetMatrixWidth() int {
return this.matrixWidth
}
func (this *SymbolInfo) GetMatrixHeight() int {
return this.matrixHeight
}
func (this *SymbolInfo) GetDataLengthForInterleavedBlock(index int) int {
return this.funcGetDataLengthForInterleavedBlock(this, index)
}
func defaultGetDataLengthForInterleavedBlock(this *SymbolInfo, index int) int {
return this.rsBlockData
}
func (this *SymbolInfo) GetErrorLengthForInterleavedBlock(index int) int {
return this.rsBlockError
}
func (this *SymbolInfo) String() string {
shape := "Square"
if this.rectangular {
shape = "Rectangular"
}
return fmt.Sprintf(
"%s Symbpl: data region %dx%d, symbol size %dx%d, symbol data size %dx%d, codewords %d+%d",
shape, this.matrixWidth, this.matrixHeight,
this.GetSymbolWidth(), this.GetSymbolHeight(),
this.GetSymbolDataWidth(), this.GetSymbolDataHeight(),
this.dataCapacity, this.errorCodewords)
}