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];
end;
function IfThen(ok: Boolean; a, b: String): String;
begin
if ok then Result := a else Result := b;
end;
function InchToMillimeters(AValue: Double): Double;
begin
Result := AValue * 25.4;
@ -930,6 +925,9 @@ begin
border_width := FMargin;
font_height := GetTextHeight(Font);
scale := FScale;
option_1 := -1;
option_2 := 0;
option_3 := 928;
case FBearerBarMode of
bbmNone:
;
@ -1039,7 +1037,7 @@ begin
begin
s := '';
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);
end;
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: char): char;
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 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;
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);
var
i: integer;

View File

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