-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
zint_2of5.pas
404 lines (335 loc) · 10.3 KB
/
zint_2of5.pas
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
unit zint_2of5;
{
Based on Zint (done by Robin Stuart and the Zint team)
http://github.com/zint/zint
Translation by TheUnknownOnes
http://theunknownones.net
License: Apache License 2.0
Status:
3432bc9aff311f2aea40f0e9883abfe6564c080b complete
}
{$IFDEF FPC}
{$mode objfpc}{$H+}
{$ENDIF}
interface
uses
zint;
function matrix_two_of_five(symbol : zint_symbol; source : TArrayOfByte; _length : Integer) : Integer;
function industrial_two_of_five(symbol : zint_symbol; source : TArrayOfByte; _length : Integer) : Integer;
function iata_two_of_five(symbol : zint_symbol; source : TArrayOfByte; _length : Integer) : Integer;
function logic_two_of_five(symbol : zint_symbol; source : TArrayOfByte; _length : Integer) : Integer;
function interleaved_two_of_five(symbol : zint_symbol; source : TArrayOfByte; _length : Integer) : Integer;
function itf14(symbol : zint_symbol; source : TArrayOfByte; _length : Integer) : Integer;
function dpleit(symbol : zint_symbol; source : TArrayOfByte; _length : Integer) : Integer;
function dpident(symbol : zint_symbol; source : TArrayOfByte; _length : Integer) : Integer;
implementation
uses zint_common, zint_helper;
const
C25MatrixTable : array[0..9] of String = ('113311', '311131', '131131', '331111', '113131', '313111',
'133111', '111331', '311311', '131311');
C25IndustTable : array[0..9] of String = ('1111313111', '3111111131', '1131111131', '3131111111', '1111311131',
'3111311111', '1131311111', '1111113131', '3111113111', '1131113111');
C25InterTable : array[0..9] of String = ('11331', '31113', '13113', '33111', '11313', '31311', '13311', '11133',
'31131', '13131');
function check_digit(count : Cardinal) : Char; inline;
begin
Result := itoc((10 - (count mod 10)) mod 10);
end;
{ Code 2 of 5 Standard (Code 2 of 5 Matrix) }
function matrix_two_of_five(symbol : zint_symbol; source : TArrayOfByte; _length : Integer) : Integer;
var
i, error_number : Integer;
dest : TArrayOfChar; { 6 + 80 * 6 + 6 + 1 ~ 512 }
begin
SetLength(dest, 512);
//error_number := 0;
if(_length > 80) then
begin
strcpy(symbol.errtxt, 'Input too long');
result := ZERROR_TOO_LONG; exit;
end;
error_number := is_sane(NEON, source, _length);
if(error_number = ZERROR_INVALID_DATA) then
begin
strcpy(symbol.errtxt, 'Invalid characters in data');
result := error_number; exit;
end;
{ start character }
strcpy(dest, '411111');
for i := 0 to _length - 1 do
begin
lookup(NEON, C25MatrixTable, source[i], dest);
end;
{ Stop character }
concat(dest, '41111');
expand(symbol, dest);
ustrcpy(symbol.text, source);
result := error_number; exit;
end;
{ Code 2 of 5 Industrial }
function industrial_two_of_five(symbol : zint_symbol; source : TArrayOfByte; _length : Integer) : Integer;
var
error_number : Integer;
dest : TArrayOfChar; { 6 + 40 * 10 + 6 + 1 }
i : Integer;
begin
SetLength(dest, 512);
//error_number := 0;
if (_length > 45) then
begin
strcpy(symbol.errtxt, 'Input too long');
result := ZERROR_TOO_LONG; exit;
end;
error_number := is_sane(NEON, source, _length);
if (error_number = ZERROR_INVALID_DATA) then
begin
strcpy(symbol.errtxt, 'Invalid character in data');
result := error_number; exit;
end;
{ start character }
strcpy(dest, '313111');
for i := 0 to _length - 1 do
lookup(NEON, C25IndustTable, source[i], dest);
{ Stop character }
concat(dest, '31113');
expand(symbol, dest);
ustrcpy(symbol.text, source);
result := error_number; exit;
end;
{ Code 2 of 5 IATA }
function iata_two_of_five(symbol : zint_symbol; source : TArrayOfByte; _length : Integer) : Integer;
var
error_number : Integer;
dest : TArrayOfChar; { 4 + 45 * 10 + 3 + 1 }
i : Integer;
begin
SetLength(dest, 512);
//error_number := 0;
if (_length > 45) then
begin
strcpy(symbol.errtxt, 'Input too long');
result := ZERROR_TOO_LONG; exit;
end;
error_number := is_sane(NEON, source, _length);
if (error_number = ZERROR_INVALID_DATA) then
begin
strcpy(symbol.errtxt, 'Invalid characters in data');
result := error_number; exit;
end;
{ start }
strcpy(dest, '1111');
for i := 0 to _length - 1 do
lookup(NEON, C25IndustTable, source[i], dest);
{ stop }
concat(dest, '311');
expand(symbol, dest);
ustrcpy(symbol.text, source);
result := error_number; exit;
end;
{ Code 2 of 5 Data Logic }
function logic_two_of_five(symbol : zint_symbol; source : TArrayOfByte; _length : Integer) : Integer;
var
error_number : Integer;
dest : TArrayOfChar; { 4 + 80 * 6 + 3 + 1 }
i : Integer;
begin
SetLength(dest, 512);
//error_number := 0;
if (_length > 80) then
begin
strcpy(symbol.errtxt, 'Input too long');
result := ZERROR_TOO_LONG; exit;
end;
error_number := is_sane(NEON, source, _length);
if (error_number = ZERROR_INVALID_DATA) then
begin
strcpy(symbol.errtxt, 'Invalid characters in data');
result := error_number; exit;
end;
{ start character }
strcpy(dest, '1111');
for i := 0 to _length - 1 do
lookup(NEON, C25MatrixTable, source[i], dest);
{ Stop character }
concat (dest, '311');
expand(symbol, dest);
ustrcpy(symbol.text, source);
result := error_number; exit;
end;
{ Code 2 of 5 Interleaved }
function interleaved_two_of_five(symbol : zint_symbol; source : TArrayOfByte; _length : Integer) : Integer;
var
error_number : Integer;
bars, spaces, mixed, dest : TArrayOfChar;
temp : TArrayOfByte;
i, j, k : Integer;
begin
SetLength(bars, 7); SetLength(spaces, 7); SetLength(mixed, 14); SetLength(dest, 1000);
SetLength(temp, _length + 2);
//error_number := 0;
if (_length > 89) then
begin
strcpy(symbol.errtxt, 'Input too long');
result := ZERROR_TOO_LONG; exit;
end;
error_number := is_sane(NEON, source, _length);
if (error_number = ZERROR_INVALID_DATA) then
begin
strcpy(symbol.errtxt, 'Invalid characters in data');
result := error_number; exit;
end;
ustrcpy(temp, '');
{ Input must be an even number of characters for Interlaced 2 of 5 to work:
if an odd number of characters has been entered then add a leading zero }
if (_length and 1) <> 0 then
begin
ustrcpy(temp, '0');
Inc(_length);
end;
uconcat(temp, source);
{ start character }
strcpy(dest, '1111');
i := 0;
while i < _length do
begin
{ look up the bars and the spaces and put them in two strings }
strcpy(bars, '');
lookup(NEON, C25InterTable, temp[i], bars);
strcpy(spaces, '');
lookup(NEON, C25InterTable, temp[i + 1], spaces);
k := 0;
{ then merge (interlace) the strings together }
for j := 0 to 4 do
begin
mixed[k] := bars[j]; Inc(k);
mixed[k] := spaces[j]; Inc(k);
end;
mixed[k] := #0;
concat(dest, mixed);
Inc(i, 2);
end;
{ Stop character }
concat (dest, '311');
expand(symbol, dest);
ustrcpy(symbol.text, temp);
result := error_number; exit;
end;
function itf14(symbol : zint_symbol; source : TArrayOfByte; _length : Integer) : Integer;
var
error_number, zeroes : Integer;
count : Cardinal;
localstr : TArrayOfChar;
i : Integer;
begin
SetLength(localstr, 16);
//error_number := 0;
count := 0;
if (_length > 13) then
begin
strcpy(symbol.errtxt, 'Input too long');
result := ZERROR_TOO_LONG; exit;
end;
error_number := is_sane(NEON, source, _length);
if (error_number = ZERROR_INVALID_DATA) then
begin
strcpy(symbol.errtxt, 'Invalid character in data');
result := error_number; exit;
end;
{ Add leading zeros as required }
zeroes := 13 - _length;
for i := 0 to zeroes - 1 do
localstr[i] := '0';
localstr[zeroes] := #0;
concat(localstr, source);
{ Calculate the check digit - the same method used for EAN-13 }
for i := 12 downto 0 do
begin
Inc(count, ctoi(localstr[i]));
if (i and 1) = 0 then
Inc(count, 2 * ctoi(localstr[i]));
end;
localstr[13] := check_digit(count);
localstr[14] := #0;
error_number := interleaved_two_of_five(symbol, ArrayOfCharToArrayOfByte(localstr), strlen(localstr));
ustrcpy(symbol.text, ArrayOfCharToString(localstr));
result := error_number; exit;
end;
{ Deutsche Post Leitcode }
function dpleit(symbol : zint_symbol; source : TArrayOfByte; _length : Integer) : Integer;
var
error_number : Integer;
count : Cardinal;
localstr : TArrayOfChar;
zeroes : Integer;
i : Integer;
begin
SetLength(localstr, 16);
//error_number := 0;
count := 0;
if (_length > 13) then
begin
strcpy(symbol.errtxt, 'Input wrong _length');
result := ZERROR_TOO_LONG; exit;
end;
error_number := is_sane(NEON, source, _length);
if (error_number = ZERROR_INVALID_DATA) then
begin
strcpy(symbol.errtxt, 'Invalid characters in data');
result := error_number; exit;
end;
zeroes := 13 - _length;
for i := 0 to zeroes - 1 do
localstr[i] := '0';
localstr[zeroes] := #0;
concat(localstr, source);
for i := 12 downto 0 do
begin
Inc(count, 4 * ctoi(localstr[i]));
if (i and 1) <> 0 then
Inc(count, 5 * ctoi(localstr[i]));
end;
localstr[13] := check_digit(count);
localstr[14] := #0;
error_number := interleaved_two_of_five(symbol, ArrayOfCharToArrayOfByte(localstr), strlen(localstr));
ustrcpy(symbol.text, ArrayOfCharToString(localstr));
result := error_number; exit;
end;
{ Deutsche Post Identcode }
function dpident(symbol : zint_symbol; source : TArrayOfByte; _length : Integer) : Integer;
var
error_number, zeroes : Integer;
count : Cardinal;
localstr : TArrayOfChar;
i : Integer;
begin
SetLength(localstr, 16);
count := 0;
if (_length > 11) then
begin
strcpy(symbol.errtxt, 'Input wrong _length');
result := ZERROR_TOO_LONG; exit;
end;
error_number := is_sane(NEON, source, _length);
if (error_number = ZERROR_INVALID_DATA) then
begin
strcpy(symbol.errtxt, 'Invalid characters in data');
//result := error_number;
end;
zeroes := 11 - _length;
for i := 0 to zeroes - 1 do
localstr[i] := '0';
localstr[zeroes] := #0;
concat(localstr, source);
for i := 10 downto 0 do
begin
Inc(count, 4 * ctoi(localstr[i]));
if (i and 1) <> 0 then
Inc(count, 5 * ctoi(localstr[i]));
end;
localstr[11] := check_digit(count);
localstr[12] := #0;
error_number := interleaved_two_of_five(symbol, ArrayOfCharToArrayOfByte(localstr), strlen(localstr));
ustrcpy(symbol.text, ArrayOfCharToString(localstr));
result := error_number; exit;
end;
end.