-
-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathzint_render_fmx_canvas.pas
190 lines (155 loc) · 5.59 KB
/
zint_render_fmx_canvas.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
unit zint_render_fmx_canvas;
{
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
}
interface
uses
zint, FMX.Types, System.UITypes, System.Classes, FMX.Graphics,
System.Math.Vectors,System.Types;
type
{ TZintCanvasRenderTarget }
TZintCanvasRenderTarget = class(TZintCustomRenderTarget)
private
procedure SetCanvas(const Value: TCanvas);
protected
FCanvas : TCanvas;
FFGColor: TAlphaColor;
FBGColor: TAlphaColor;
FFont: TFont;
procedure ClearBackground(const AParams : TZintClearBackgroundParams); override;
procedure DrawRect(const AParams : TZintDrawRectParams); override;
procedure DrawHexagon(const AParams : TZintDrawHexagonParams); override;
procedure DrawRing(const AParams : TZintDrawRingParams); override;
procedure DrawText(const AParams: TZintDrawTextParams); override;
function CalcTextHeight(const AParams : TZintCalcTextHeightParams) : Single; override;
function CalcTextWidth(const AParams : TZintCalcTextWidthParams) : Single; override;
procedure SetFont(const Value: TFont);
procedure DrawStart; override;
procedure DrawStop; override;
public
constructor Create(AOwner : TPersistent); override;
property Canvas : TCanvas read FCanvas write SetCanvas;
destructor Destroy; override;
published
property ForegroundColor : TAlphaColor read FFGColor write FFGColor;
property BackgroundColor : TAlphaColor read FBGColor write FBGColor;
property Font: TFont read FFont write SetFont;
end;
implementation
{ TZintCanvasRenderTarget }
procedure TZintCanvasRenderTarget.ClearBackground(const AParams : TZintClearBackgroundParams);
begin
FCanvas.Fill.Color:=FBGColor;
FCanvas.Fill.Kind:=TBrushKind.Solid;
FCanvas.Stroke.Kind:=TBrushKind.None;
FCanvas.FillRect(RectF(FX,
FY,
FWidth,
FHeight), 0, 0, [], 1);
end;
procedure TZintCanvasRenderTarget.DrawRect(const AParams : TZintDrawRectParams);
begin
FCanvas.Stroke.Kind:=TBrushKind.Solid;
FCanvas.Stroke.Color:=ffgcolor;
FCanvas.Fill.Color:=FFGColor;
FCanvas.Fill.Kind:=TBrushKind.Solid;
FCanvas.FillRect(RectF(AParams.x,
AParams.y,
AParams.X + AParams.Width,
AParams.y + AParams.Height), 0, 0, [], 1);
FCanvas.DrawRect(RectF(AParams.x,
AParams.y,
AParams.X + AParams.Width,
AParams.y + AParams.Height), 0, 0, [], 1);
end;
procedure TZintCanvasRenderTarget.DrawHexagon(const AParams : TZintDrawHexagonParams);
var
Points : TPolygon;
begin
FCanvas.Fill.Color:=ffgcolor;
FCanvas.Fill.Kind:=TBrushKind.Solid;
SetLength(Points, 6);
Points[0] := PointF(AParams.x-(AParams.width/2), AParams.y - AParams.height / 4);
Points[1] := PointF(AParams.x-(AParams.width/2), AParams.y + AParams.height / 4);
Points[2] := PointF(AParams.x -(AParams.width/2) + sqrt(3) * AParams.height / 4, AParams.y + AParams.height / 2);
Points[3] := PointF(AParams.x -(AParams.width/2) + sqrt(3) * AParams.height / 2, AParams.y + AParams.height / 4);
Points[4] := PointF(AParams.x -(AParams.width/2) + sqrt(3) * AParams.height / 2, AParams.y - AParams.height / 4);
Points[5] := PointF(AParams.x -(AParams.width/2) + sqrt(3) * AParams.height / 4, AParams.y - AParams.height / 2);
FCanvas.FillPolygon(Points, 1);
end;
procedure TZintCanvasRenderTarget.DrawRing(const AParams : TZintDrawRingParams);
var
Radius : Single;
begin
FCanvas.Stroke.Kind:=TBrushKind.Solid;
FCanvas.Stroke.Color:=ffgcolor;
FCanvas.Fill.Kind:=TBrushKind.None;
FCanvas.Stroke.Thickness:=AParams.OuterRadius-AParams.InnerRadius;
Radius:=AParams.InnerRadius+FCanvas.Stroke.Thickness/2;
FCanvas.DrawEllipse(RectF(AParams.X-radius,
AParams.y-radius,
AParams.x + radius,
AParams.y + radius),1);
end;
procedure TZintCanvasRenderTarget.DrawStart;
begin
inherited;
FCanvas.BeginScene;
end;
procedure TZintCanvasRenderTarget.DrawStop;
begin
inherited;
FCanvas.EndScene;
end;
procedure TZintCanvasRenderTarget.DrawText(const AParams: TZintDrawTextParams);
begin
FCanvas.Font.Assign(FFont);
FCanvas.Fill.Kind:=TBrushKind.Solid;
FCanvas.Fill.Color:=FFGColor;
FCanvas.Font.Size:=AParams.Height;
FCanvas.FillText(RectF(AParams.x,
AParams.y,
AParams.x+AParams.width,
AParams.y+AParams.Height),
AParams.text, false,1, [], TTextAlign.Center);
end;
function TZintCanvasRenderTarget.CalcTextHeight(const AParams : TZintCalcTextHeightParams) : Single;
begin
FCanvas.Font.Assign(FFont);
Result:=FCanvas.TextHeight(AParams.Text);
end;
function TZintCanvasRenderTarget.CalcTextWidth(const AParams : TZintCalcTextWidthParams) : Single;
begin
FCanvas.Font.Assign(FFont);
Result:=FCanvas.TextWidth(AParams.Text);
end;
procedure TZintCanvasRenderTarget.SetCanvas(const Value: TCanvas);
begin
if Assigned(Value) then
begin
FWidthDesired:=Value.Width;
FHeightDesired:=Value.Height;
end;
FCanvas := Value;
end;
procedure TZintCanvasRenderTarget.SetFont(const Value: TFont);
begin
FFont.Assign(Value);
end;
constructor TZintCanvasRenderTarget.Create(AOwner : TPersistent);
begin
inherited;
FFont:=TFont.Create;
FFGColor := TAlphaColorRec.Black;
FBGColor := TAlphaColorRec.White;
end;
destructor TZintCanvasRenderTarget.Destroy;
begin
FFont.Free;
inherited Destroy;
end;
end.