LazBarCodes: Some more refactoring for PDF417 family.

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@9334 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
wp_xxyyzz 2024-04-11 20:43:33 +00:00
parent c5b05ebc89
commit 62cfec5784
3 changed files with 50 additions and 89 deletions

View File

@ -704,11 +704,6 @@ begin
Result[i-1] := s[i]; Result[i-1] := s[i];
end; end;
function IfThen(ok: Boolean; a, b: String): String;
begin
if ok then Result := a else Result := b;
end;
function InchToMillimeters(AValue: Double): Double; function InchToMillimeters(AValue: Double): Double;
begin begin
Result := AValue * 25.4; Result := AValue * 25.4;
@ -930,6 +925,9 @@ begin
border_width := FMargin; border_width := FMargin;
font_height := GetTextHeight(Font); font_height := GetTextHeight(Font);
scale := FScale; scale := FScale;
option_1 := -1;
option_2 := 0;
option_3 := 928;
case FBearerBarMode of case FBearerBarMode of
bbmNone: bbmNone:
; ;
@ -1039,7 +1037,7 @@ begin
begin begin
s := ''; s := '';
for c := 0 to FSymbol^.width-1 do for c := 0 to FSymbol^.width-1 do
s := s + IfThen(module_is_set(FSymbol, r, c), OnStr, OffStr); s := s + iif(module_is_set(FSymbol, r, c), OnStr, OffStr);
WriteLn(F, s); WriteLn(F, s);
end; end;
CloseFile(F); CloseFile(F);

View File

@ -27,6 +27,7 @@ function IsTrue(aInteger: Integer): Boolean;
function iif(const aBoolean: integer; const aCh1,aCh2: pchar): pchar; function iif(const aBoolean: integer; const aCh1,aCh2: pchar): pchar;
function iif(const aBoolean: integer; const aCh1,aCh2: char): char; function iif(const aBoolean: integer; const aCh1,aCh2: char): char;
function iif(const aBoolean: Boolean; const aV1,aV2: Word): word; function iif(const aBoolean: Boolean; const aV1,aV2: Word): word;
function iif(const aBoolean: Boolean; const aStr1, aStr2: String): String;
procedure concat(const aText: pchar; const aChar: char); procedure concat(const aText: pchar; const aChar: char);
procedure concat(const aText: pchar; const aText2: pchar); procedure concat(const aText: pchar; const aText2: pchar);
procedure concat(var aText: array of char; const aText2: pchar); procedure concat(var aText: array of char; const aText2: pchar);
@ -140,6 +141,11 @@ begin
if aBoolean then Result:=aV1 else Result:=aV2; if aBoolean then Result:=aV1 else Result:=aV2;
end; end;
function iif(const aBoolean: Boolean; const aStr1, aStr2: String): String;
begin
if aBoolean then Result := aStr1 else Result := aStr2;
end;
procedure concat(const aText: pchar; const aChar: char); procedure concat(const aText: pchar; const aChar: char);
var var
i: integer; i: integer;

View File

@ -930,19 +930,16 @@ function pdf417(ASymbol: PZintSymbol; ASource: PByte; ALength: Integer): Integer
var var
i, j, k, n: Integer; i, j, k, n: Integer;
index_src, index_list, mode, len, loop, offset : Integer; index_src, index_list, mode, len, loop, offset : Integer;
total, mc_length, c1, c2, c3, codeErr : Integer;
mccorrection : array[0..519] of Integer; mccorrection : array[0..519] of Integer;
total, mc_length, c1, c2, c3, codeerr : Integer;
chainemc: TIntegerDynArray = nil;
dummy: array[0..34] of Integer; dummy: array[0..34] of Integer;
chainemc: TIntegerDynArray = nil;
barcode: String = ''; barcode: String = '';
pattern: String = ''; pattern: String = '';
list: TGlobalList; list: TGlobalList;
s: String;
begin begin
SetLength(chainemc, 2700); SetLength(chainemc, 2700);
codeerr := 0; codeErr := 0;
{ 456 } { 456 }
index_list := 0; index_list := 0;
@ -1140,9 +1137,7 @@ begin
for loop := 1 to Length(pattern) do for loop := 1 to Length(pattern) do
if (pattern[loop] = '1') then if (pattern[loop] = '1') then
set_module(ASymbol, i, loop-1) set_module(ASymbol, i, loop-1);
else
unset_module(ASymbol, i, loop-1);
ASymbol^.row_height[i] := 3; ASymbol^.row_height[i] := 3;
end; end;
@ -1151,13 +1146,13 @@ begin
ASymbol^.width := Length(pattern); ASymbol^.width := Length(pattern);
{ 843 } { 843 }
result := codeerr; result := codeErr;
end; end;
{ 345 } { 345 }
function pdf417enc(ASymbol: PZintSymbol; ASource: PByte; ALength: Integer): Integer; function pdf417enc(ASymbol: PZintSymbol; ASource: PByte; ALength: Integer): Integer;
var var
codeerr: Integer; codeErr: Integer;
begin begin
Result := 0; Result := 0;
@ -1176,12 +1171,12 @@ begin
end; end;
{ 349 } { 349 }
codeerr := pdf417(ASymbol, ASource, ALength); codeErr := pdf417(ASymbol, ASource, ALength);
{ 352 } { 352 }
if (codeerr <> 0) then if (codeErr <> 0) then
begin begin
case codeerr of case codeErr of
1: 1:
begin begin
ASymbol^.SetErrorText('No such file or file unreadable'); ASymbol^.SetErrorText('No such file or file unreadable');
@ -1217,15 +1212,14 @@ var
total, mc_length, codeerr : Integer; total, mc_length, codeerr : Integer;
chainemc: TIntegerDynArray = nil; chainemc: TIntegerDynArray = nil;
dummy : array[0..5] of Integer; dummy : array[0..5] of Integer;
codebarre: TCharDynArray = nil;
pattern: TCharDynArray = nil;
variant, LeftRAPStart, CentreRAPStart, RightRAPStart, StartCluster : Integer; variant, LeftRAPStart, CentreRAPStart, RightRAPStart, StartCluster : Integer;
LeftRAP, CentreRAP, RightRAP, Cluster, writer, flip, loop : Integer; LeftRAP, CentreRAP, RightRAP, Cluster, loop : Integer;
flip: Boolean;
list: TGlobalList; list: TGlobalList;
barcode: String = '';
pattern: String = '';
begin begin
SetLength(chainemc, 2700); SetLength(chainemc, 2700);
SetLength(codebarre, 100);
SetLength(pattern, 580);
{ Encoding starts out the same as PDF417, so use the same code } { Encoding starts out the same as PDF417, so use the same code }
codeerr := 0; codeerr := 0;
@ -1280,7 +1274,7 @@ begin
if (mc_length > 126) then if (mc_length > 126) then
begin begin
ASymbol^.SetErrorText('Input data too long.'); ASymbol^.SetErrorText('Input data too long.');
result := ERROR_TOO_LONG; Result := ERROR_TOO_LONG;
exit; exit;
end; end;
@ -1288,7 +1282,7 @@ begin
begin begin
ASymbol^.SetErrorText('Specified width out of range'); ASymbol^.SetErrorText('Specified width out of range');
ASymbol^.option_2 := 0; ASymbol^.option_2 := 0;
codeerr := WARN_INVALID_OPTION; codeErr := WARN_INVALID_OPTION;
end; end;
{ Now figure out which variant of the symbol to use and load values accordingly } { Now figure out which variant of the symbol to use and load values accordingly }
@ -1300,7 +1294,7 @@ begin
{ the user specified 1 column but the data doesn't fit - go to automatic } { the user specified 1 column but the data doesn't fit - go to automatic }
ASymbol^.option_2 := 0; ASymbol^.option_2 := 0;
ASymbol^.SetErrorText('Specified symbol size too small for data'); ASymbol^.SetErrorText('Specified symbol size too small for data');
codeerr := WARN_INVALID_OPTION; codeErr := WARN_INVALID_OPTION;
end; end;
if ((ASymbol^.option_2 = 2) and (mc_length > 37)) then if ((ASymbol^.option_2 = 2) and (mc_length > 37)) then
@ -1308,7 +1302,7 @@ begin
{ the user specified 2 columns but the data doesn't fit - go to automatic } { the user specified 2 columns but the data doesn't fit - go to automatic }
ASymbol^.option_2 := 0; ASymbol^.option_2 := 0;
ASymbol^.SetErrorText('Specified symbol size too small for data'); ASymbol^.SetErrorText('Specified symbol size too small for data');
codeerr := WARN_INVALID_OPTION; codeErr := WARN_INVALID_OPTION;
end; end;
if ((ASymbol^.option_2 = 3) and (mc_length > 82)) then if ((ASymbol^.option_2 = 3) and (mc_length > 82)) then
@ -1316,7 +1310,7 @@ begin
{ the user specified 3 columns but the data doesn't fit - go to automatic } { the user specified 3 columns but the data doesn't fit - go to automatic }
ASymbol^.option_2 := 0; ASymbol^.option_2 := 0;
ASymbol^.SetErrorText('Specified symbol size too small for data'); ASymbol^.SetErrorText('Specified symbol size too small for data');
codeerr := WARN_INVALID_OPTION; codeErr := WARN_INVALID_OPTION;
end; end;
if (ASymbol^.option_2 = 1) then if (ASymbol^.option_2 = 1) then
@ -1442,7 +1436,7 @@ begin
for i := 0 to ASymbol^.rows - 1 do for i := 0 to ASymbol^.rows - 1 do
begin begin
strcpy(codebarre, ''); barcode := '';
offset := 929 * Cluster; offset := 929 * Cluster;
for j := 0 to 4 do for j := 0 to 4 do
dummy[j] := 0; dummy[j] := 0;
@ -1451,73 +1445,36 @@ begin
dummy[j + 1] := chainemc[i * ASymbol^.option_2 + j]; dummy[j + 1] := chainemc[i * ASymbol^.option_2 + j];
{ Copy the data into codebarre } { Copy the data into codebarre }
concat(codebarre, PChar(RAPLR[LeftRAP])); barcode := barcode + RAPLR[LeftRAP] + '1' + codagemc[offset + dummy[1]] + '1';
concat(codebarre, '1'); if (ASymbol^.option_2 = 3) then barcode := barcode + RAPC[CentreRAP];
concat(codebarre, PChar(codagemc[offset + dummy[1]])); if (ASymbol^.option_2 >= 2) then barcode := barcode + '1' + codagemc[offset + dummy[2]] + '1';
concat(codebarre, '1'); if (ASymbol^.option_2 = 4) then barcode := barcode + RAPC[CentreRAP];
if (ASymbol^.option_2 = 3) then if (ASymbol^.option_2 >= 3) then barcode := barcode + '1' + codagemc[offset + dummy[3]] + '1';
concat(codebarre, PChar(RAPC[CentreRAP])); if (ASymbol^.option_2 = 4) then barcode := barcode + '1' + codagemc[offset + dummy[4]] + '1';
barcode := barcode + RAPLR[RightRAP] + '1';
if (ASymbol^.option_2 >= 2) then
begin
concat(codebarre, '1');
concat(codebarre, PChar(codagemc[offset + dummy[2]]));
concat(codebarre, '1');
end;
if (ASymbol^.option_2 = 4) then
concat(codebarre, PChar(RAPC[CentreRAP]));
if (ASymbol^.option_2 >= 3) then
begin
concat(codebarre, '1');
concat(codebarre, PChar(codagemc[offset + dummy[3]]));
concat(codebarre, '1');
end;
if (ASymbol^.option_2 = 4) then
begin
concat(codebarre, '1');
concat(codebarre, PChar(codagemc[offset + dummy[4]]));
concat(codebarre, '1');
end;
concat(codebarre, PChar(RAPLR[RightRAP]));
concat(codebarre, '1'); { stop }
{ Now codebarre is a mixture of letters and numbers } { Now codebarre is a mixture of letters and numbers }
flip := true;
writer := 0; pattern := '';
flip := 1; for loop := 1 to Length(barcode) do
strcpy(pattern, '');
for loop := 0 to strlen(codebarre) - 1 do
begin begin
if ((codebarre[loop] >= '0') and (codebarre[loop] <= '9')) then if ((barcode[loop] >= '0') and (barcode[loop] <= '9')) then
begin begin
for k := 0 to ctoi(codebarre[loop]) - 1 do for k := 0 to StrToInt(barcode[loop]) - 1 do
begin pattern := pattern + iif(flip, '1', '0');
if (flip = 0) then flip := not flip;
pattern[writer] := '0'
else
pattern[writer] := '1';
Inc(writer);
end;
pattern[writer] := #0;
if (flip = 0) then
flip := 1
else
flip := 0;
end end
else else
begin Lookup(BRSET, PDFttf, barcode[loop], pattern);
lookup(PChar(BRSET), PDFttf, codebarre[loop], pattern);
Inc(writer, 5);
end; end;
end;
ASymbol^.width := writer;
{ so now pattern[] holds the string of '1's and '0's. - copy this to the symbol } { so now pattern[] holds the string of '1's and '0's. - copy this to the symbol }
for loop := 0 to strlen(pattern) - 1 do for loop := 1 to Length(pattern) do
if (pattern[loop] = '1') then set_module(ASymbol, i, loop); if (pattern[loop] = '1') then
set_module(ASymbol, i, loop-1);
Asymbol^.row_height[i] := 2; ASymbol^.width := Length(pattern);
ASymbol^.row_height[i] := 2;
{ Set up RAPs and Cluster for next row } { Set up RAPs and Cluster for next row }
Inc(LeftRAP); Inc(LeftRAP);
@ -1538,7 +1495,7 @@ begin
Cluster := 0; Cluster := 0;
end; end;
Result := codeerr; Result := codeErr;
end; end;
end. end.