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
|
// Copyright 2017 PDFium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <cstring>
#include "fxbarcode/oned/BC_OnedCode39Writer.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
// 3 wide and 6 narrow modules per char. 1 space between chars.
const int MODULES_PER_CHAR = 3 * 3 + 6 * 1 + 1;
// '*' is added as the first and last char.
const int DELIMITER_CHARS = 2;
// Last char may serve as checksum.
const int CHECKSUM_CHARS = 1;
TEST(OnedCode39WriterTest, SetWideNarrowRatio) {
// Code 39 barcodes encode strings of any size into modules in a
// unidimensional disposition.
// Each module is either: a narrow bar, a narrow space, a wide
// bar, or a wide space. Accepted wide-to-narrow ratios are between 2 and 3.
// This writer in particular only takes integer ratios, so it's either 2 or 3.
CBC_OnedCode39Writer writer;
EXPECT_FALSE(writer.SetWideNarrowRatio(0));
EXPECT_FALSE(writer.SetWideNarrowRatio(1));
EXPECT_TRUE(writer.SetWideNarrowRatio(2));
EXPECT_TRUE(writer.SetWideNarrowRatio(3));
EXPECT_FALSE(writer.SetWideNarrowRatio(4));
EXPECT_FALSE(writer.SetWideNarrowRatio(100));
writer.SetWideNarrowRatio(3);
int32_t width;
int32_t height;
uint8_t* encoded;
const char* expected;
encoded = writer.Encode("PDFIUM", BCFORMAT_CODE_39, width, height);
expected =
"# # ### ### # " // * Start
"# ### ### # # " // P
"# # ### # ### " // D
"# ### ### # # " // F
"# ### # ### # " // I
"### # # # ### " // U
"### ### # # # " // M
"# # ### ### #"; // * End
FX_Free(encoded);
writer.SetWideNarrowRatio(2);
encoded = writer.Encode("PDFIUM", BCFORMAT_CODE_39, width, height);
expected =
"# # ## ## # " // * Start
"# ## ## # # " // P
"# # ## # ## " // D
"# ## ## # # " // F
"# ## # ## # " // I
"## # # # ## " // U
"## ## # # # " // M
"# # ## ## #"; // * End
FX_Free(encoded);
}
TEST(OnedCode39WriterTest, Encode) {
CBC_OnedCode39Writer writer;
int32_t width;
int32_t height;
uint8_t* encoded;
const char* expected;
encoded = writer.Encode("", BCFORMAT_CODE_39, width, height);
EXPECT_EQ(1, height);
EXPECT_EQ((0 + DELIMITER_CHARS) * MODULES_PER_CHAR - 1, width);
expected =
"# # ### ### # " // * Start
"# # ### ### #"; // * End
for (size_t i = 0; i < strlen(expected); i++) {
EXPECT_EQ(expected[i] != ' ', !!encoded[i]) << i;
}
FX_Free(encoded);
encoded = writer.Encode("123", BCFORMAT_CODE_39, width, height);
EXPECT_EQ(1, height);
EXPECT_EQ((3 + DELIMITER_CHARS) * MODULES_PER_CHAR - 1, width);
expected =
"# # ### ### # " // * Start
"### # # # ### " // 1
"# ### # # ### " // 2
"### ### # # # " // 3
"# # ### ### #"; // * End
for (size_t i = 0; i < strlen(expected); i++) {
EXPECT_EQ(expected[i] != ' ', !!encoded[i]) << i;
}
FX_Free(encoded);
encoded = writer.Encode("PDFIUM", BCFORMAT_CODE_39, width, height);
EXPECT_EQ(1, height);
EXPECT_EQ((6 + DELIMITER_CHARS) * MODULES_PER_CHAR - 1, width);
expected =
"# # ### ### # " // * Start
"# ### ### # # " // P
"# # ### # ### " // D
"# ### ### # # " // F
"# ### # ### # " // I
"### # # # ### " // U
"### ### # # # " // M
"# # ### ### #"; // * End
for (size_t i = 0; i < strlen(expected); i++) {
EXPECT_EQ(expected[i] != ' ', !!encoded[i]) << i;
}
FX_Free(encoded);
encoded = writer.Encode("A -$%./+Z", BCFORMAT_CODE_39, width, height);
EXPECT_EQ(1, height);
EXPECT_EQ((9 + DELIMITER_CHARS) * MODULES_PER_CHAR - 1, width);
expected =
"# # ### ### # " // * Start
"### # # # ### " // A
"# ### # ### # " // Space
"# # # ### ### " // -
"# # # # # " // $
"# # # # # " // %
"### # # ### # " // .
"# # # # # " // /
"# # # # # " // +
"# ### ### # # " // Z
"# # ### ### #"; // * End
for (size_t i = 0; i < strlen(expected); i++) {
EXPECT_EQ(expected[i] != ' ', !!encoded[i]) << i;
}
FX_Free(encoded);
}
TEST(OnedCode39WriterTest, Checksum) {
CBC_OnedCode39Writer writer;
int32_t width;
int32_t height;
uint8_t* encoded;
const char* expected;
writer.SetCalcChecksum(true);
encoded = writer.Encode("123", BCFORMAT_CODE_39, width, height);
EXPECT_EQ(1, height);
EXPECT_EQ((3 + CHECKSUM_CHARS + DELIMITER_CHARS) * MODULES_PER_CHAR - 1,
width);
expected =
"# # ### ### # " // * Start
"### # # # ### " // 1 (1)
"# ### # # ### " // 2 (2)
"### ### # # # " // 3 (3)
"# ### ### # # " // 6 (6 = (1 + 2 + 3) % 43)
"# # ### ### #"; // * End
for (size_t i = 0; i < strlen(expected); i++) {
EXPECT_EQ(expected[i] != ' ', !!encoded[i]) << i;
}
FX_Free(encoded);
encoded = writer.Encode("PDFIUM", BCFORMAT_CODE_39, width, height);
EXPECT_EQ(1, height);
EXPECT_EQ((6 + CHECKSUM_CHARS + DELIMITER_CHARS) * MODULES_PER_CHAR - 1,
width);
expected =
"# # ### ### # " // * Start
"# ### ### # # " // P (25)
"# # ### # ### " // D (13)
"# ### ### # # " // F (15)
"# ### # ### # " // I (18)
"### # # # ### " // U (30)
"### ### # # # " // M (22)
"### # # ### # " // . (37 = (25 + 13 + 15 + 18 + 30 + 22) % 43)
"# # ### ### #"; // * End
for (size_t i = 0; i < strlen(expected); i++) {
EXPECT_EQ(expected[i] != ' ', !!encoded[i]) << i;
}
FX_Free(encoded);
}
} // namespace
|