1 | unit lbc_helper;
|
---|
2 |
|
---|
3 | {$mode objfpc}{$H+}
|
---|
4 |
|
---|
5 | interface
|
---|
6 |
|
---|
7 | uses
|
---|
8 | sysutils, types, zint;
|
---|
9 |
|
---|
10 | type
|
---|
11 | TCheckSumFunc = function(ASource: String): String;
|
---|
12 |
|
---|
13 | const
|
---|
14 | RHODIUM = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:';
|
---|
15 | SILVER = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%abcd'; // 47 chars
|
---|
16 | NEON = '0123456789';
|
---|
17 | SODIUM = '0123456789-';
|
---|
18 |
|
---|
19 | OPTION_ADD_CHECKSUM = 1;
|
---|
20 | OPTION_DISPLAY_CHECKSUM = 2;
|
---|
21 | OPTION_GROUPED_CHARS = 4;
|
---|
22 |
|
---|
23 | NO_PADDING = #0;
|
---|
24 |
|
---|
25 | function IsTrue(aBoolean: Boolean): Boolean;
|
---|
26 | function IsTrue(aInteger: Integer): Boolean;
|
---|
27 | function iif(const aBoolean: integer; const aCh1,aCh2: pchar): pchar;
|
---|
28 | function iif(const aBoolean: integer; const aCh1,aCh2: char): char;
|
---|
29 | function iif(const aBoolean: Boolean; const aV1,aV2: Word): word;
|
---|
30 | procedure concat(const aText: pchar; const aChar: char);
|
---|
31 | procedure concat(const aText: pchar; const aText2: pchar);
|
---|
32 | procedure concat(var aText: array of char; const aText2: pchar);
|
---|
33 | procedure strcpy(const aText: pchar; const aText2: pchar);
|
---|
34 | procedure strcpy(const aText: pchar; const aText2: TByteDynArray);
|
---|
35 | procedure strcpy(var aText: array of char; const aText2: pchar);
|
---|
36 | procedure strcpy(var aText: array of byte; const aText2: pchar);
|
---|
37 | procedure strcpy(var AText: array of byte; const aText2: TByteDynArray);
|
---|
38 | function posn(const aText: pchar; const aChar: Char): integer;
|
---|
39 | function strlen(const aText: array of char): integer;
|
---|
40 | function is_sane(test_string: PChar; source: PByte; length: Integer): Integer; overload;
|
---|
41 | function is_sane(test_string: PChar; const source: TByteDynArray; length: Integer): Integer; overload;
|
---|
42 | function is_sane(const AllowedChars, ASource: String): Integer; overload;
|
---|
43 | function utf8toutf16(symbol: PointerTo_zint_symbol; source: PByte; vals: PInteger; length: PInteger): Integer;
|
---|
44 | procedure set_module(symbol: PointerTo_zint_symbol; y_coord: Integer; x_coord: Integer);
|
---|
45 | procedure unset_module(symbol: PointerTo_zint_symbol; y_coord: Integer; x_coord: Integer);
|
---|
46 | function module_is_set(symbol: PointerTo_zint_symbol; y_coord: Integer; x_coord: Integer): Boolean;
|
---|
47 | function PostInc(var v: integer): integer;
|
---|
48 | function PostDec(var v: integer): integer;
|
---|
49 | function is_extendable(symbology: Integer): Boolean;
|
---|
50 | function is_stackable(symbology: Integer): Boolean;
|
---|
51 | function NotBoolean(const aValue: integer): Boolean;
|
---|
52 | function NotBoolean(const aValue: Boolean): Boolean;
|
---|
53 | function latin1_process(symbol: PointerTo_zint_symbol; source: PBYTE; preprocessed: PBYTE; length: PInteger): Integer;
|
---|
54 | function ctoi(c: char): integer;
|
---|
55 | function ctoi(c: BYTE): integer;
|
---|
56 | function BooleanNot(const aValue: integer): Boolean;
|
---|
57 | procedure memset(const p: Pointer; const aValue: BYTE; const aSize: integer);
|
---|
58 | function istwodigits(source: PByte; position: Integer): Boolean;
|
---|
59 | procedure to_upper(a: PByte);
|
---|
60 | procedure to_upper(const a: TByteDynArray);
|
---|
61 | procedure Lookup(const Set_String: PChar; const ATable: array of String; const AData: Char; var ADest: TCharDynArray);
|
---|
62 | procedure Lookup(const Set_String: PChar; const ATable: array of String; const AData: Byte; var ADest: TCharDynArray);
|
---|
63 |
|
---|
64 | procedure Lookup(const Set_String: String; const ATable: array of String; const AData: Char; var ADest: String);
|
---|
65 | {
|
---|
66 | procedure lookup(const set_string : TArrayOfChar; const table : array of String; const data : Char; var dest : TArrayOfChar); overload;
|
---|
67 | procedure lookup(const set_string : TArrayOfChar; const table : array of String; const data : Byte; var dest : TArrayOfChar); overload;
|
---|
68 | procedure lookup(const ASet_string : String; const ATable : array of String; const AData : Byte; var ADest : TArrayOfChar); overload;
|
---|
69 | procedure lookup(const ASet_string : String; const ATable : array of String; const AData : Char; var ADest : TArrayOfChar); overload;
|
---|
70 | }
|
---|
71 | function itoc(AValue: Integer): Char;
|
---|
72 |
|
---|
73 | procedure expand(ASymbol: PointerTo_zint_symbol; AData: TCharDynArray);
|
---|
74 | procedure expand(ASymbol: PointerTo_zint_symbol; const AData: string);
|
---|
75 |
|
---|
76 | function CharArrayToStr(AData: TCharDynArray): String;
|
---|
77 | function StrToCharArray(AText: String): TCharDynArray;
|
---|
78 |
|
---|
79 | function basic_encoder(ASymbol: PZIntSymbol; const ASource: String;
|
---|
80 | MaxLen: Integer; const AllowedChars, StartCode: String;
|
---|
81 | const CharCodes: array of string; const StopCode: String;
|
---|
82 | CheckSumFunc: TCheckSumFunc; SourceInverted: Boolean): Integer;
|
---|
83 |
|
---|
84 | implementation
|
---|
85 |
|
---|
86 | uses
|
---|
87 | Math;
|
---|
88 |
|
---|
89 | function istwodigits(source: PBYTE; position: Integer): Boolean;
|
---|
90 | begin
|
---|
91 | if (Char(source[position]) in ['0'..'9']) and (Char(source[position+1]) in ['0'..'9']) then begin
|
---|
92 | Result:=true;
|
---|
93 | end else begin
|
---|
94 | Result:=false;
|
---|
95 | end;
|
---|
96 | end;
|
---|
97 |
|
---|
98 | function PostInc(var v: integer): integer;
|
---|
99 | begin
|
---|
100 | Result:=v;
|
---|
101 | inc(v);
|
---|
102 | end;
|
---|
103 |
|
---|
104 | function PostDec(var v: integer): integer;
|
---|
105 | begin
|
---|
106 | Result:=v;
|
---|
107 | Dec(v);
|
---|
108 | end;
|
---|
109 |
|
---|
110 | function IsTrue(aBoolean: Boolean): Boolean;
|
---|
111 | begin
|
---|
112 | Result:=aBoolean;
|
---|
113 | end;
|
---|
114 |
|
---|
115 | function IsTrue(aInteger: Integer): Boolean;
|
---|
116 | begin
|
---|
117 | if aInteger=0 then Result:=false else Result:=true;
|
---|
118 | end;
|
---|
119 |
|
---|
120 | function iif(const aBoolean: integer; const aCh1, aCh2: pchar): pchar;
|
---|
121 | begin
|
---|
122 | if aBoolean=0 then begin
|
---|
123 | Result:=aCh2;
|
---|
124 | end else begin
|
---|
125 | Result:=aCh1;
|
---|
126 | end;
|
---|
127 | end;
|
---|
128 |
|
---|
129 | function iif(const aBoolean: integer; const aCh1,aCh2: char): char;
|
---|
130 | begin
|
---|
131 | if aBoolean=0 then begin
|
---|
132 | Result:=aCh2;
|
---|
133 | end else begin
|
---|
134 | Result:=aCh1;
|
---|
135 | end;
|
---|
136 | end;
|
---|
137 |
|
---|
138 | function iif(const aBoolean: Boolean; const aV1,aV2: Word): word;
|
---|
139 | begin
|
---|
140 | if aBoolean then Result:=aV1 else Result:=aV2;
|
---|
141 | end;
|
---|
142 |
|
---|
143 | procedure concat(const aText: pchar; const aChar: char);
|
---|
144 | var
|
---|
145 | i: integer;
|
---|
146 | begin
|
---|
147 | i:=sysutils.strlen(aText);
|
---|
148 | aText[i]:=aChar;
|
---|
149 | inc(i);
|
---|
150 | aText[i]:=#0;
|
---|
151 | end;
|
---|
152 |
|
---|
153 | procedure concat(const aText: pchar; const aText2: pchar);
|
---|
154 | var
|
---|
155 | i: integer;
|
---|
156 | begin
|
---|
157 | i:=sysutils.strlen(aText);
|
---|
158 | move(aText2^,aText[i],sysutils.strlen(aText2)+1);
|
---|
159 | end;
|
---|
160 |
|
---|
161 | procedure concat(var aText: array of char; const aText2: pchar);
|
---|
162 | begin
|
---|
163 | concat(pchar(@aText[0]), aText2);
|
---|
164 | end;
|
---|
165 |
|
---|
166 | procedure strcpy(const aText: pchar; const aText2: pchar);
|
---|
167 | begin
|
---|
168 | move(aText2^,aText^,sysutils.strlen(aText2)+1);
|
---|
169 | end;
|
---|
170 |
|
---|
171 | procedure strcpy(const aText: pchar; const aText2: TByteDynArray);
|
---|
172 | var
|
---|
173 | len: Integer;
|
---|
174 | begin
|
---|
175 | len := sysutils.strlen(PChar(@aText2[0]));
|
---|
176 | move(aText2[0], aText^, len+1);
|
---|
177 | end;
|
---|
178 |
|
---|
179 | procedure strcpy(var aText: array of char; const aText2: pchar);
|
---|
180 | begin
|
---|
181 | move(aText2^,aText[0],sysutils.strlen(aText2)+1);
|
---|
182 | end;
|
---|
183 |
|
---|
184 | procedure strcpy(var AText: array of byte; const aText2: PChar);
|
---|
185 | begin
|
---|
186 | Move(aText2^, aText[0], sysUtils.strLen(aText2)+1);
|
---|
187 | end;
|
---|
188 |
|
---|
189 | procedure strcpy(var AText: array of byte; const aText2: TByteDynArray);
|
---|
190 | var
|
---|
191 | len: Integer;
|
---|
192 | begin
|
---|
193 | len := sysUtils.strLen(PChar(@aText2[0]));
|
---|
194 | Move(aText2[0], aText[0], len+1);
|
---|
195 | end;
|
---|
196 |
|
---|
197 | function posn(const aText: pchar; const aChar: Char): integer;
|
---|
198 | var
|
---|
199 | p: Pchar;
|
---|
200 | c: integer;
|
---|
201 | begin
|
---|
202 | p:=aText;
|
---|
203 | c:=0;
|
---|
204 | while p^<>#0 do begin
|
---|
205 | if p^<>aChar then begin
|
---|
206 | inc(c);
|
---|
207 | inc(p);
|
---|
208 | end else begin
|
---|
209 | Exit(c);
|
---|
210 | end;
|
---|
211 | end;
|
---|
212 | Result:=0;
|
---|
213 | end;
|
---|
214 |
|
---|
215 | function strlen(const aText: array of char): integer;
|
---|
216 | begin
|
---|
217 | Result:=sysutils.strlen(pchar(@aText[0]));
|
---|
218 | end;
|
---|
219 |
|
---|
220 | { Checks whether each character of ASource is contained in string AllowedChars.
|
---|
221 | Returns 0 if successful, otherwise error code ERROR_INVALID_DATA. }
|
---|
222 | function is_sane(const AllowedChars, ASource: String): Integer;
|
---|
223 | var
|
---|
224 | i: Integer;
|
---|
225 | begin
|
---|
226 | for i := 1 to Length(ASource) do
|
---|
227 | if pos(ASource[i], AllowedChars) = 0 then
|
---|
228 | begin
|
---|
229 | Result := ERROR_INVALID_DATA;
|
---|
230 | exit;
|
---|
231 | end;
|
---|
232 | Result := 0;
|
---|
233 | end;
|
---|
234 |
|
---|
235 | function is_sane(test_string: PChar; source: PByte; length: Integer): Integer;
|
---|
236 | var
|
---|
237 | latch: Boolean;
|
---|
238 | j: Cardinal;
|
---|
239 | i: Integer;
|
---|
240 | lt: Cardinal;
|
---|
241 | begin
|
---|
242 | {INITCODE} lt := sysutils.strlen (test_string);
|
---|
243 | i := 0;
|
---|
244 | while i < length do
|
---|
245 | begin
|
---|
246 | latch := FALSE;
|
---|
247 | j := 0;
|
---|
248 | while j < lt do
|
---|
249 | begin
|
---|
250 | if Boolean(source[i] = BYTE(test_string[j])) then
|
---|
251 | begin
|
---|
252 | latch := TRUE;
|
---|
253 | break;
|
---|
254 | end;
|
---|
255 | Inc (j);
|
---|
256 | end;
|
---|
257 | if Boolean(NotBoolean (latch)) then
|
---|
258 | begin
|
---|
259 | exit (ERROR_INVALID_DATA);
|
---|
260 | end;
|
---|
261 | Inc (i);
|
---|
262 | end;
|
---|
263 | exit (0);
|
---|
264 | end;
|
---|
265 |
|
---|
266 | function is_sane(test_string: PChar; const source: TByteDynArray; Length: Integer): Integer;
|
---|
267 | begin
|
---|
268 | Result := is_sane(test_string, PByte(@source[0]), Length);
|
---|
269 | end;
|
---|
270 |
|
---|
271 | function utf8toutf16(symbol: PointerTo_zint_symbol; source: PBYTE; vals: PInteger; length: PInteger): Integer;
|
---|
272 | var
|
---|
273 | error_number: Integer;
|
---|
274 | jpos: Integer;
|
---|
275 | bpos: Integer;
|
---|
276 | next: Integer;
|
---|
277 | begin
|
---|
278 | bpos := 0;
|
---|
279 | jpos := 0;
|
---|
280 | error_number := 0;
|
---|
281 | next := 0;
|
---|
282 | repeat
|
---|
283 | if source[bpos] <= $7F then
|
---|
284 | begin
|
---|
285 | vals[jpos] := source[bpos];
|
---|
286 | next := bpos + 1;
|
---|
287 | Inc (jpos);
|
---|
288 | end else begin
|
---|
289 | if (source[bpos] >= $80) and (source[bpos] <= $BF) then
|
---|
290 | begin
|
---|
291 | strcpy (symbol^.errtxt, 'Corrupt Unicode data');
|
---|
292 | exit (ERROR_INVALID_DATA);
|
---|
293 | end;
|
---|
294 | if (source[bpos] >= $C0) and (source[bpos] <= $C1) then
|
---|
295 | begin
|
---|
296 | strcpy (symbol^.errtxt, 'Overlong encoding not supported');
|
---|
297 | exit (ERROR_INVALID_DATA);
|
---|
298 | end;
|
---|
299 | if (source[bpos] >= $C2) and (source[bpos] <= $DF) then
|
---|
300 | begin
|
---|
301 | vals[jpos] := ((source[bpos] and $1F) shl 6) + (source[bpos + 1] and $3F);
|
---|
302 | next := bpos + 2;
|
---|
303 | Inc (jpos);
|
---|
304 | end else begin
|
---|
305 | if (source[bpos] >= $E0) and (source[bpos] <= $EF) then
|
---|
306 | begin
|
---|
307 | vals[jpos] := ((source[bpos] and $0F) shl 12) + ((source[bpos + 1] and $3F) shl 6) + (source[bpos + 2] and $3F);
|
---|
308 | next := bpos + 3;
|
---|
309 | Inc (jpos);
|
---|
310 | end else begin
|
---|
311 | if source[bpos] >= $F0 then
|
---|
312 | begin
|
---|
313 | strcpy (symbol^.errtxt, 'Unicode sequences of more than 3 bytes not supported');
|
---|
314 | exit (ERROR_INVALID_DATA);
|
---|
315 | end;
|
---|
316 | end;
|
---|
317 | end;
|
---|
318 | end;
|
---|
319 | bpos := next;
|
---|
320 | until not (bpos < length^);
|
---|
321 | length^ := jpos;
|
---|
322 | exit (error_number);
|
---|
323 | end;
|
---|
324 |
|
---|
325 | function module_is_set(symbol: PointerTo_zint_symbol; y_coord, x_coord: Integer): Boolean;
|
---|
326 | begin
|
---|
327 | Result := symbol^.encoded_data[y_coord, x_coord];
|
---|
328 | end;
|
---|
329 |
|
---|
330 | procedure set_module(symbol: PointerTo_zint_symbol; y_coord, x_coord: Integer);
|
---|
331 | begin
|
---|
332 | symbol^.encoded_data[y_coord, x_coord] := true;
|
---|
333 | end;
|
---|
334 |
|
---|
335 | procedure unset_module(symbol: PointerTo_zint_symbol; y_coord, x_coord: Integer);
|
---|
336 | begin
|
---|
337 | symbol^.encoded_data[y_coord, x_coord] := false;
|
---|
338 | end;
|
---|
339 |
|
---|
340 | procedure to_upper(a: PByte);
|
---|
341 | begin
|
---|
342 | while a^<>0 do begin
|
---|
343 | if char(a^) in ['a'..'z'] then begin
|
---|
344 | a^:=Byte(upCase(char(a^)));
|
---|
345 | end;
|
---|
346 | inc(a);
|
---|
347 | end;
|
---|
348 | end;
|
---|
349 |
|
---|
350 | procedure to_upper(const a: TByteDynArray);
|
---|
351 | begin
|
---|
352 | to_upper(PByte(@a[0]));
|
---|
353 | end;
|
---|
354 |
|
---|
355 | function ctoi(c: char): integer;
|
---|
356 | begin
|
---|
357 | if c in ['0'..'9'] then begin
|
---|
358 | Result:=BYTE(c)-48;
|
---|
359 | end else begin
|
---|
360 | Result:=-1;
|
---|
361 | end;
|
---|
362 | end;
|
---|
363 |
|
---|
364 | function ctoi(c: BYTE): integer;
|
---|
365 | begin
|
---|
366 | Result:=ctoi(char(c));
|
---|
367 | end;
|
---|
368 |
|
---|
369 | function BooleanNot(const aValue: integer): Boolean;
|
---|
370 | begin
|
---|
371 | Result:=NotBoolean(aValue);
|
---|
372 | end;
|
---|
373 |
|
---|
374 | procedure memset(const p: Pointer; const aValue: BYTE; const aSize: integer);
|
---|
375 | begin
|
---|
376 | FillByte(PBYTE(p)^,aSize,aValue);
|
---|
377 | end;
|
---|
378 |
|
---|
379 | function is_extendable(symbology: Integer): Boolean;
|
---|
380 | begin
|
---|
381 | Result := true;
|
---|
382 | if symbology = BARCODE_EANX then exit;
|
---|
383 | if symbology = BARCODE_UPCA then exit;
|
---|
384 | if symbology = BARCODE_UPCE then exit;
|
---|
385 | if symbology = BARCODE_ISBNX then exit;
|
---|
386 | if symbology = BARCODE_UPCA_CC then exit;
|
---|
387 | if symbology = BARCODE_UPCE_CC then exit;
|
---|
388 | if symbology = BARCODE_EANX_CC then exit;
|
---|
389 | Result := false;
|
---|
390 | end;
|
---|
391 |
|
---|
392 | function is_stackable(symbology: Integer): Boolean;
|
---|
393 | begin
|
---|
394 | Result := true;
|
---|
395 | if symbology < BARCODE_PDF417 then exit;
|
---|
396 | if symbology = BARCODE_CODE128B then exit;
|
---|
397 | if symbology = BARCODE_ISBNX then exit;
|
---|
398 | if symbology = BARCODE_EAN14 then exit;
|
---|
399 | if symbology = BARCODE_NVE18 then exit;
|
---|
400 | if symbology = BARCODE_KOREAPOST then exit;
|
---|
401 | if symbology = BARCODE_PLESSEY then exit;
|
---|
402 | if symbology = BARCODE_TELEPEN_NUM then exit;
|
---|
403 | if symbology = BARCODE_ITF14 then exit;
|
---|
404 | if symbology = BARCODE_CODE32 then exit;
|
---|
405 | Result := false;
|
---|
406 | end;
|
---|
407 |
|
---|
408 | function NotBoolean(const aValue: integer): Boolean;
|
---|
409 | begin
|
---|
410 | Result := (aValue = 0);
|
---|
411 | end;
|
---|
412 |
|
---|
413 | function NotBoolean(const aValue: Boolean): Boolean;
|
---|
414 | begin
|
---|
415 | Result := not aValue;
|
---|
416 | end;
|
---|
417 |
|
---|
418 | function latin1_process(symbol: PointerTo_zint_symbol; source: PBYTE; preprocessed: PBYTE; length: PInteger): Integer;
|
---|
419 | var
|
---|
420 | next: Integer;
|
---|
421 | i: Integer;
|
---|
422 | j: Integer;
|
---|
423 | begin
|
---|
424 | j := 0;
|
---|
425 | i := 0;
|
---|
426 | repeat
|
---|
427 | next := -1;
|
---|
428 | if Boolean(source[i] < 128) then
|
---|
429 | begin
|
---|
430 | preprocessed[j] := source[i];
|
---|
431 | Inc (j);
|
---|
432 | next := i + 1;
|
---|
433 | end else begin
|
---|
434 | if Boolean(source[i] = $C2) then
|
---|
435 | begin
|
---|
436 | preprocessed[j] := source[i + 1];
|
---|
437 | Inc (j);
|
---|
438 | next := i + 2;
|
---|
439 | end;
|
---|
440 | if Boolean(source[i] = $C3) then
|
---|
441 | begin
|
---|
442 | preprocessed[j] := source[i + 1] + 64;
|
---|
443 | Inc (j);
|
---|
444 | next := i + 2;
|
---|
445 | end;
|
---|
446 | end;
|
---|
447 | if Boolean(next = -1) then
|
---|
448 | begin
|
---|
449 | strcpy (symbol^.errtxt, 'error: Invalid character in input string (only Latin-1 characters supported)');
|
---|
450 | exit (ERROR_INVALID_DATA);
|
---|
451 | end;
|
---|
452 | i := next;
|
---|
453 | until not (i < length^);
|
---|
454 | preprocessed[j] := 0;
|
---|
455 | length^ := j;
|
---|
456 | exit (0);
|
---|
457 | end;
|
---|
458 |
|
---|
459 | { Replaces huge switch statements for looking up in tables }
|
---|
460 | procedure Lookup(const Set_String: PChar; const ATable: array of String;
|
---|
461 | const AData: Char; var ADest: TCharDynArray);
|
---|
462 | var
|
---|
463 | i, n: integer;
|
---|
464 | begin
|
---|
465 | n := System.Strlen(Set_String);
|
---|
466 | for i := 0 to n-1 do
|
---|
467 | if AData = Set_String[i] then
|
---|
468 | concat(ADest, PChar(ATable[i]));
|
---|
469 | end;
|
---|
470 |
|
---|
471 | procedure Lookup(const Set_String: PChar; const ATable: array of String;
|
---|
472 | const AData: Byte; var ADest: TCharDynArray);
|
---|
473 | begin
|
---|
474 | LookUp(set_string, ATable, char(AData), ADest);
|
---|
475 | end;
|
---|
476 |
|
---|
477 | { Set_string is a string with the allowed characters.
|
---|
478 | ATable is the bar-space pattern for each of these allowed characters.
|
---|
479 | AData is the character to be encoded.
|
---|
480 | ADest is the output string.
|
---|
481 | The routines searches the character in the list of allowed characters and
|
---|
482 | appends its bar-space pattern to ADest. }
|
---|
483 | procedure Lookup(const Set_String: String; const ATable: array of String;
|
---|
484 | const AData: Char; var ADest: String);
|
---|
485 | var
|
---|
486 | i: Integer;
|
---|
487 | begin
|
---|
488 | i := pos(AData, Set_String);
|
---|
489 | if i > 0 then
|
---|
490 | ADest := ADest + ATable[i-1];
|
---|
491 | end;
|
---|
492 |
|
---|
493 |
|
---|
494 | { Converts an integer value to its hexadecimal character }
|
---|
495 | function itoc(AValue: Integer): Char;
|
---|
496 | begin
|
---|
497 | if (AValue >= 0) and (AValue <= 9) then
|
---|
498 | Result := Char(Ord('0') + AValue)
|
---|
499 | else
|
---|
500 | Result := Char(Ord('A') + (AValue - 10));
|
---|
501 | end;
|
---|
502 |
|
---|
503 |
|
---|
504 | { Expands from a width pattern to a bit pattern */ }
|
---|
505 | procedure expand(ASymbol: PointerTo_zint_symbol; const AData: String);
|
---|
506 | var
|
---|
507 | reader, writer, i: Integer;
|
---|
508 | latch: boolean;
|
---|
509 | begin
|
---|
510 | writer := 0;
|
---|
511 | latch := true;
|
---|
512 |
|
---|
513 | for reader := 1 to Length(AData) do
|
---|
514 | begin
|
---|
515 | for i := 1 to ctoi(AData[reader]) do
|
---|
516 | begin
|
---|
517 | if latch then
|
---|
518 | set_module(ASymbol, ASymbol^.rows, writer);
|
---|
519 | inc(writer);
|
---|
520 | end;
|
---|
521 | latch := not latch;
|
---|
522 | end;
|
---|
523 |
|
---|
524 | if ASymbol^.symbology <> BARCODE_PHARMA then
|
---|
525 | begin
|
---|
526 | if writer > ASymbol^.width then
|
---|
527 | ASymbol^.width := writer;
|
---|
528 | end else
|
---|
529 | begin
|
---|
530 | { Pharmacode One ends with a space - adjust for this }
|
---|
531 | if (writer > ASymbol^.width + 2) then
|
---|
532 | ASymbol^.width := writer - 2;
|
---|
533 | end;
|
---|
534 |
|
---|
535 | ASymbol^.rows := ASymbol^.rows + 1;
|
---|
536 | end;
|
---|
537 |
|
---|
538 | procedure expand(ASymbol: PointerTo_zint_symbol; AData: TCharDynArray);
|
---|
539 | var
|
---|
540 | reader, n : Cardinal;
|
---|
541 | writer, i : Integer;
|
---|
542 | latch : Char;
|
---|
543 | begin
|
---|
544 | n := strlen(AData);
|
---|
545 | writer := 0;
|
---|
546 | latch := '1';
|
---|
547 |
|
---|
548 | for reader := 0 to n - 1 do
|
---|
549 | begin
|
---|
550 | for i := 0 to ctoi(AData[reader]) - 1 do
|
---|
551 | begin
|
---|
552 | if (latch = '1') then
|
---|
553 | set_module(ASymbol, ASymbol^.rows, writer);
|
---|
554 | Inc(writer);
|
---|
555 | end;
|
---|
556 |
|
---|
557 | if latch = '1' then latch := '0' else latch := '1';
|
---|
558 | end;
|
---|
559 |
|
---|
560 | if(ASymbol^.symbology <> BARCODE_PHARMA) then
|
---|
561 | begin
|
---|
562 | if(writer > ASymbol^.width) then
|
---|
563 | ASymbol^.width := writer;
|
---|
564 | end else
|
---|
565 | begin
|
---|
566 | { Pharmacode One ends with a space - adjust for this }
|
---|
567 | if(writer > ASymbol^.width + 2) then
|
---|
568 | ASymbol^.width := writer - 2;
|
---|
569 | end;
|
---|
570 |
|
---|
571 | ASymbol^.rows := ASymbol^.rows + 1;
|
---|
572 | end;
|
---|
573 |
|
---|
574 | function CharArrayToStr(AData: TCharDynArray): String;
|
---|
575 | var
|
---|
576 | len: Integer;
|
---|
577 | begin
|
---|
578 | Result := '';
|
---|
579 | len := Length(AData);
|
---|
580 | SetLength(Result, len);
|
---|
581 | if len > 0 then
|
---|
582 | Move(AData[0], Result[1], len*SizeOf(Char));
|
---|
583 | end;
|
---|
584 |
|
---|
585 | function StrToCharArray(AText: String): TCharDynArray;
|
---|
586 | var
|
---|
587 | len: Integer;
|
---|
588 | begin
|
---|
589 | Result := nil;
|
---|
590 | len := Length(AText);
|
---|
591 | SetLength(Result, len + 1);
|
---|
592 | if len > 0 then
|
---|
593 | Move(AText[1], Result[0], len*SizeOf(Char));
|
---|
594 | Result[len] := #0;
|
---|
595 | end;
|
---|
596 |
|
---|
597 |
|
---|
598 | { Basic encoding routine for barcodes - most of them work all in the same way
|
---|
599 | only with different parameters:
|
---|
600 | MaxLen - maximum length of input string
|
---|
601 | AllowedChars - a string containing the allowed characters for this barcode type.
|
---|
602 | If empty, all characters are allowed (any check musts be done by the caller).
|
---|
603 | StartCode - starting code pattern
|
---|
604 | CharCodes - code pattern for each allowed character
|
---|
605 | StopCode - ending code pattern
|
---|
606 | CheckSumFunc - function to calculate a checksum which is appended to the
|
---|
607 | input string
|
---|
608 | SourceInverted - input string is encoded from last to first char
|
---|
609 | }
|
---|
610 | function basic_encoder(ASymbol: PZIntSymbol; const ASource: String;
|
---|
611 | MaxLen: Integer; const AllowedChars, StartCode: String;
|
---|
612 | const CharCodes: array of string; const StopCode: String;
|
---|
613 | CheckSumFunc: TCheckSumFunc; SourceInverted: Boolean): Integer;
|
---|
614 | var
|
---|
615 | i: Integer;
|
---|
616 | src, dest: String;
|
---|
617 | check: String;
|
---|
618 | begin
|
---|
619 | Result := 0;
|
---|
620 |
|
---|
621 | if Length(ASource) > MaxLen then
|
---|
622 | begin
|
---|
623 | ASymbol^.SetErrorText('Input too long (max ' + IntToStr(MaxLen) + ' digits).');
|
---|
624 | Result := ERROR_TOO_LONG;
|
---|
625 | exit;
|
---|
626 | end;
|
---|
627 |
|
---|
628 | src := ASource;
|
---|
629 |
|
---|
630 | // START character
|
---|
631 | dest := StartCode;
|
---|
632 |
|
---|
633 | // Check whether all characters of the input string are allowed.
|
---|
634 | if AllowedChars <> '' then
|
---|
635 | begin
|
---|
636 | Result := is_sane(AllowedChars, ASource);
|
---|
637 | if Result = ERROR_INVALID_DATA then
|
---|
638 | begin
|
---|
639 | ASymbol^.SetErrorText('Invalid characters in data.');
|
---|
640 | exit;
|
---|
641 | end;
|
---|
642 |
|
---|
643 | // Add encoded input string characters to destination string
|
---|
644 | if SourceInverted then
|
---|
645 | for i := Length(src) downto 1 do
|
---|
646 | lookup(AllowedChars, CharCodes, src[i], dest)
|
---|
647 | else
|
---|
648 | for i := 1 to Length(src) do
|
---|
649 | lookup(AllowedChars, CharCodes, src[i], dest);
|
---|
650 |
|
---|
651 | // Add check digit(s) if a calculation function is provided
|
---|
652 | if (ASymbol^.Option and OPTION_ADD_CHECKSUM = OPTION_ADD_CHECKSUM) and
|
---|
653 | (CheckSumFunc <> nil) then
|
---|
654 | begin
|
---|
655 | check := CheckSumFunc(src);
|
---|
656 | src := src + check;
|
---|
657 | for i := 1 to Length(check) do
|
---|
658 | lookup(AllowedChars, CharCodes, check[i], dest);
|
---|
659 | end;
|
---|
660 | end
|
---|
661 | else
|
---|
662 | // All characters are allowed -> lookup not needed
|
---|
663 | begin
|
---|
664 | // Add encoded input string characters to destination string
|
---|
665 | if SourceInverted then
|
---|
666 | for i := Length(src) downto 1 do
|
---|
667 | dest := dest + CharCodes[ord(src[i])]
|
---|
668 | else
|
---|
669 | for i := 1 to Length(src) do
|
---|
670 | dest := dest + CharCodes[ord(src[i])];
|
---|
671 |
|
---|
672 | // Add check digit(s) if a calculation function is provided
|
---|
673 | if (ASymbol^.Option and OPTION_ADD_CHECKSUM = OPTION_ADD_CHECKSUM) and
|
---|
674 | (ChecksumFunc <> nil) then
|
---|
675 | begin
|
---|
676 | check := CheckSumFunc(src);
|
---|
677 | src := src + check;
|
---|
678 | for i := 1 to Length(check) do
|
---|
679 | dest := dest + CharCodes[ord(check[i])];
|
---|
680 | end;
|
---|
681 | end;
|
---|
682 |
|
---|
683 | // STOP character
|
---|
684 | dest := dest + StopCode;
|
---|
685 |
|
---|
686 | // Expand the RLE-encoded bar/space widths to modules
|
---|
687 | // Example: '121' --> 1001 where 1 = black, 0 = white
|
---|
688 | // This information is stored in the ZintSymbol.
|
---|
689 | expand(ASymbol, dest);
|
---|
690 |
|
---|
691 | // Store human-readable text
|
---|
692 | if ASymbol^.Option and OPTION_DISPLAY_CHECKSUM = OPTION_DISPLAY_CHECKSUM then
|
---|
693 | ASymbol^.SetText(src)
|
---|
694 | else
|
---|
695 | ASymbol^.SetText(ASource);
|
---|
696 | end;
|
---|
697 |
|
---|
698 |
|
---|
699 | end.
|
---|