LazBarCodes: Improved compatibility with previous version 1.0.4

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@8694 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
wp_xxyyzz
2023-01-22 10:54:37 +00:00
parent 7be2670cb8
commit 7a5a5868e5
4 changed files with 94 additions and 28 deletions

View File

@ -14,7 +14,7 @@
</SearchPaths>
</CompilerOptions>
<Description Value="Provides barcode 1D and 2D generation based in the &apos;C&apos; zint library."/>
<Version Major="2"/>
<Version Major="2" Release="1"/>
<Files Count="2">
<Item1>
<Filename Value="..\src\ubarcodes.pas"/>

View File

@ -28,7 +28,7 @@ Change to BSD-license is done for backend and therefore for ZINT shared library
the frontends and Qt4-backend the GPL is still valid. Since BSD-license is GPL-compatible
this gives the possibility to include ZINT library in own products or link against it from
own software."/>
<Version Major="2"/>
<Version Major="2" Release="1"/>
<Files Count="23">
<Item1>
<Filename Value="..\src\zint\zint.pp"/>

View File

@ -673,7 +673,8 @@ begin
TBarcodeMaxicode
]);
RegisterPropertyEditor(TypeInfo(TCaption), TLazBarcodeCustomText, 'Text', TCaptionPropertyEditor);
RegisterPropertyEditor(TypeInfo(TCaption), TLazBarcodeCustomText, 'Text', TCaptionPropertyEditor);
RegisterPropertyToSkip(TLazBarcodeCustomBase, 'StrictSize', 'Deprecated', '');
end;
function ColorToChars(AColor: TColor): TColorChars;
@ -1437,7 +1438,7 @@ begin
// Start drawing, clear background
ADrawer.BeginDrawing;
// Draw the lines (bars, bearing bars, box)
line := FSymbol^.Rendered^.lines;
while Assigned(line) do begin
@ -1449,7 +1450,7 @@ begin
);
line := line^.next;
end;
// Draw the text
if FShowHumanReadableText then begin
str := FSymbol^.Rendered^.strings;
@ -1458,7 +1459,7 @@ begin
str := str^.next;
end;
end;
// Finish drawing
ADrawer.EndDrawing;
end;
@ -1525,9 +1526,10 @@ var
drawer: TCanvasBarcodeDrawer;
begin
if FSymbol^.Rendered = nil then
raise Exception.Create('Bar code must have been rendered before drawing.');
Render(ARect.Width, ARect.Height);
// raise Exception.Create('Bar code must have been rendered before drawing.');
drawer := TCanvasBarcodeDrawer.Create(ATargetCanvas, ARect.Width, ARect.Height);
drawer := TCanvasBarcodeDrawer.Create(ATargetCanvas, ARect.Left, ARect.Top, ARect.Right, ARect.Bottom);
try
DrawBarcode(drawer, 1.0);
finally
@ -3020,6 +3022,11 @@ begin
FShowHumanReadableText := false;
FSymbolHeight := 0;
FWhiteSpaceWidth := 0;
if (Width = 0) or (Height = 0) then
begin
Width := 88;
Height := 88;
end;
end;
{ Calculates the pixel multiplication factor to fill the entire control as much
@ -3027,7 +3034,7 @@ end;
pixels}
function TBarcodeSquare.CalcFactor(AWidth, AHeight: Integer): Integer;
begin
if FSymbol^.Width = 0 then
if (FSymbol = nil) or (FSymbol^.Width = 0) then
Result := 1
else
begin
@ -3045,6 +3052,9 @@ procedure TBarcodeSquare.CalcSize(AFactor: Integer; out ATotalWidth, ATotalHeigh
ASymbolWidth, ASymbolHeight, ATextWidth, ATextHeight,
ABorderWidth, AWhitespaceWidth: Integer);
begin
if FSymbol = nil then
exit;
ABorderWidth := FMargin;
AWhiteSpaceWidth := 0;
@ -3072,13 +3082,22 @@ var
factor: Integer;
begin
inherited;
if FScale = 0 then
factor := CalcFactor(ClientWidth, ClientHeight)
if FSymbol = nil then
with GetControlClassDefaultSize do
begin
PreferredWidth := CX;
PreferredHeight := CY;
end
else
factor := FScale;
CalcSize(factor, wtot, htot, wsym, hsym, wtxt, htxt, wb, wws);
PreferredWidth := wtot;
PreferredHeight := htot;
begin
if FScale = 0 then
factor := CalcFactor(ClientWidth, ClientHeight)
else
factor := FScale;
CalcSize(factor, wtot, htot, wsym, hsym, wtxt, htxt, wb, wws);
PreferredWidth := wtot;
PreferredHeight := htot;
end;
end;
class function TBarcodeSquare.GetControlClassDefaultSize: TSize;

View File

@ -21,8 +21,11 @@ type
FFontStyle: TFontStyles;
FWidth: Double;
FHeight: Double;
FLeft: Double;
FTop: Double;
public
constructor Create(AWidth, AHeight: Double);
constructor Create(ALeft, ATop, ARight, ABottom: Double);
procedure BeginDrawing; virtual; abstract;
procedure EndDrawing; virtual; abstract;
procedure DrawBar(x1, y1, x2, y2: Double); virtual; abstract;
@ -49,6 +52,7 @@ type
property Canvas: TCanvas read FCanvas;
public
constructor Create(ACanvas: TCanvas; AWidth, AHeight: Double);
constructor Create(ACanvas: TCanvas; ALeft, ATop, ARight, ABottom: Double);
procedure BeginDrawing; override;
procedure EndDrawing; override;
procedure DrawBar(x1, y1, x2, y2: double); override;
@ -67,6 +71,7 @@ type
FFormatSettings: TFormatSettings;
public
constructor Create(AWidth, AHeight: Double; const ATitle: String);
constructor Create(ALeft, ATop, ARight, ABottom: Double; const ATitle: String);
destructor Destroy; override;
procedure SaveToFile(const AFileName: String);
procedure SaveToStream(const AStream: TStream);
@ -116,7 +121,7 @@ const
HEXAGON: array[0..5] of TDblPoint = ( // 0
(X: 0.0; Y: HH), // / \
(X:-1.0; Y: H), // 1 5
(X:-1.0; Y:-H), // | |
(X:-1.0; Y:-H), // | |
(X: 0.0; Y:-HH), // 2 4
(X: 1.0; Y:-H), // \ /
(X: 1.0; Y: H) // 3
@ -124,12 +129,35 @@ const
{ TBasicBarcodeDrawer }
constructor TBasicBarcodeDrawer.Create(AWidth, AHeight: Double);
constructor TBasicBarcodeDrawer.Create(AWidth, AHeight: Double);
begin
Create(0.0, 0.0, AWidth, AHeight);
end;
// The origin of the coordinate system is in the top/left corner.
constructor TBasicBarcodeDrawer.Create(ALeft, ATop, ARight, ABottom: Double);
begin
inherited Create;
FWidth := AWidth;
FHeight := AHeight;
if ALeft < ARight then
begin
FLeft := ALeft;
FWidth := ARight - ALeft;
end else
begin
FLeft := ARight;
FWidth := ALeft - ARight;
end;
if ATop < ABottom then
begin
FTop := ATop;
FHeight := ABottom - ATop;
end else
begin
FTop := ABottom;
FHeight := ATop - ABottom;
end;
FBarColor := clBlack;
FTextColor := clBlack;
@ -147,13 +175,20 @@ begin
FCanvas := ACanvas;
end;
constructor TCanvasBarcodeDrawer.Create(ACanvas: TCanvas;
ALeft, ATop, ARight, ABottom: Double);
begin
inherited Create(ALeft, ATop, ARight, ABottom);
FCanvas := ACanvas;
end;
// Fill the background
procedure TCanvasBarcodeDrawer.BeginDrawing;
begin
FCanvas.Pen.Style := psClear;
FCanvas.Brush.Color := FBackColor;
FCanvas.Brush.Style := bsSolid;
FCanvas.FillRect(0, 0, round(FWidth), round(FHeight));
FCanvas.FillRect(round(FLeft), round(FTop), round(FLeft + FWidth), round(FTop + FHeight));
end;
procedure TCanvasBarcodeDrawer.EndDrawing;
@ -165,7 +200,7 @@ procedure TCanvasBarcodeDrawer.DrawBar(x1, y1, x2, y2: double);
begin
FCanvas.Brush.Color := FBarColor;
FCanvas.Brush.Style := bsSolid;
FCanvas.FillRect(round(x1), round(y1), round(x2), round(y2));
FCanvas.FillRect(round(FLeft + x1), round(FTop + y1), round(FLeft + x2), round(FTop + y2));
end;
procedure TCanvasBarcodeDrawer.DrawCenteredText(x, y: double; const AText: String);
@ -178,7 +213,7 @@ begin
FCanvas.Font.Color := FTextColor;
FCanvas.Brush.Style := bsClear;
w := FCanvas.TextWidth(AText);
FCanvas.TextOut(round(x - w/2), round(y), AText);
FCanvas.TextOut(round(FLeft + x - w/2), round(FTop + y), AText);
end;
procedure TCanvasBarcodeDrawer.DrawHexagon(x, y, wx: Double);
@ -186,6 +221,8 @@ var
P: array[0..5] of TPoint;
i: Integer;
begin
x := FLeft + x;
y := FTop + y;
for i := 0 to 5 do
P[i] := Point(round(x + HEXAGON[i].X * wx), round(y + HEXAGON[i].Y * wx));
FCanvas.Brush.Color := FBarColor;
@ -208,7 +245,9 @@ begin
FCanvas.Pen.Width := round(rInner - rOuter);
r := (rOuter + rInner) / 2;
FCanvas.Ellipse(round(x+r), round(y+r), round(x-r), round(y-r));
x := FLeft + x;
y := FTop + y;
FCanvas.Ellipse(round(x + r), round(y + r), round(x - r), round(y - r));
end;
@ -216,7 +255,13 @@ end;
constructor TTextBarcodeDrawer.Create(AWidth, AHeight: Double; const ATitle: String);
begin
inherited Create(AWidth, AHeight);
Create(0.0, 0.0, AWidth, AHeight, ATitle);
end;
constructor TTextBarcodeDrawer.Create(ALeft, ATop, ARight, ABottom: Double;
const ATitle: String);
begin
inherited Create(ALeft, ATop, ARight, ABottom);
FTitle := ATitle;
FList := TStringList.Create;
FFormatSettings := DefaultFormatSettings;
@ -283,7 +328,7 @@ procedure TSvgBarcodeDrawer.DrawBar(x1, y1, x2, y2: Double);
begin
FList.Add(Format(
' <rect x="%.3fmm" y="%.3fmm" width="%.3fmm" height="%.3fmm" fill="#%s" />', [
x1, y1, x2-x1, y2-y1, SvgColor(FBarColor)],
FLeft + x1, FTop + y1, x2-x1, y2-y1, SvgColor(FBarColor)],
FFormatSettings
));
end;
@ -295,7 +340,7 @@ begin
y := y + FFontSize / 72 * 25.4; // Convert Fontsize (pts) to mm
FList.Add(Format(
' <text x="%.3fmm" y="%.3fmm" text-anchor="middle" font-family="%s" font-size="%.1fpt" fill="#%s">%s</text>', [
x, y, FFontName, 1.0*FFontSize, SvgColor(FTextColor), AText],
FLeft + x, FTop + y, FFontName, 1.0*FFontSize, SvgColor(FTextColor), AText],
FFormatSettings
));
end;
@ -305,6 +350,8 @@ var
P: array[0..5] of TDblPoint;
i: Integer;
begin
x := FLeft + x;
y := FTop + y;
for i := 0 to 5 do
begin
P[i].X := x + HEXAGON[i].X * wx;
@ -322,7 +369,7 @@ procedure TSvgBarcodeDrawer.DrawRing(x, y, rOuter, rInner: Double);
begin
FList.Add(Format(
' <circle cx="%.3fmm" cy="%.3fmm" r="%.3fmm" stroke-width="%.3fmm" stroke="%s" fill="none" />', [
x, y, (rOuter + rInner)/2, abs(rOuter - rInner), SvgColor(FBarColor)],
FLeft + x, FTop + y, (rOuter + rInner)/2, abs(rOuter - rInner), SvgColor(FBarColor)],
FFormatSettings
));
end;