-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
zint_render_svg.pas
196 lines (164 loc) · 6.49 KB
/
zint_render_svg.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
191
192
193
194
195
196
unit zint_render_svg;
{
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
}
{$IFDEF FPC}
{$mode objfpc}{$H+}
{$ENDIF}
interface
uses
zint, Classes, SysUtils, StrUtils;
type
TZintSVGColor = String;
TZintSVGFont = String;
TZintSVGDocOption = (doSingleFile, //A brand new SVG Document is created including header etc. -- existing StringList content is deleted
doInsertIntoExisting, //Barcode will be inserted into existing SVG document before the closing </svg> tag
doAppendFlat //Barcode information will be written in SVG Format without and opening/closing tags
);
{ TZintRenderTargetSVG }
TZintRenderTargetSVG = class(TZintCustomRenderTarget)
private
FFontHeight: Single;
protected
FSVGFile : TStringList;
FFormatSettings : TFormatSettings;
FDocOption : TZintSVGDocOption;
FIncludeDocInfo : Boolean;
FFGColor: TZintSVGColor;
FBGColor: TZintSVGColor;
FFont: TZintSVGFont;
procedure DrawStart; override;
procedure DrawStop; override;
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 Inflate(const ANewWidth, ANewHeight : Single); override;
procedure DrawRingFull(const AParams : TZintDrawRingParams); override;
public
constructor Create(ASVGFile: TStringList); reintroduce; virtual;
procedure Render(ASymbol : TZintSymbol); override;
property ForegroundColor : TZintSVGColor read FFGColor write FFGColor;
property DocOption : TZintSVGDocOption read FDocOption write FDocOption;
property BackgroundColor : TZintSVGColor read FBGColor write FBGColor;
property Font: TZintSVGFont read FFont write FFont;
property FontHeight: Single read FFontHeight write FFontHeight;
end;
implementation
uses
Types, zint_helper;
{ TZintRenderTargetSVG }
function TZintRenderTargetSVG.CalcTextHeight(
const AParams: TZintCalcTextHeightParams): Single;
begin
Result:=FFontHeight;
end;
function TZintRenderTargetSVG.CalcTextWidth(
const AParams: TZintCalcTextWidthParams): Single;
begin
Result:=FFontHeight*Length(AParams.Text); //we guess that;
end;
procedure TZintRenderTargetSVG.ClearBackground(
const AParams: TZintClearBackgroundParams);
begin
FSVGFile.Append(
Format('<rect x="%f" y="%f" width="%f" height="%f" style="fill:%s;stroke-width:0"/>',
[FX,
FY,
FWidth,
FHeight,
FBGColor],FFormatSettings)
);
end;
constructor TZintRenderTargetSVG.Create(ASVGFile: TStringList);
begin
inherited Create(nil);
FSVGFile:=ASVGFile;
FDocOption:=doSingleFile;
FFGColor:='black';
FBGColor:='white';
FFont:='sans-serif';
FFontHeight:=11;
FFormatSettings.DecimalSeparator:='.';
FFormatSettings.ThousandSeparator:=#0;
end;
procedure TZintRenderTargetSVG.DrawHexagon(
const AParams: TZintDrawHexagonParams);
begin
FSVGFile.Append(
Format('<polygon fill="%s" points="%f,%f %f,%f %f,%f %f,%f %f,%f %f,%f" />',
[FFGColor,
AParams.x-(AParams.width/2), AParams.y - AParams.height/4,
AParams.x-(AParams.width/2), AParams.y + AParams.height/4,
AParams.x -(AParams.width/2) + sqrt(3) * AParams.height / 4, AParams.y + AParams.height / 2,
AParams.x -(AParams.width/2) + sqrt(3) * AParams.height / 2, AParams.y + AParams.height / 4,
AParams.x -(AParams.width/2) + sqrt(3) * AParams.height / 2, AParams.y - AParams.height / 4,
AParams.x -(AParams.width/2) + sqrt(3) * AParams.height / 4, AParams.y - AParams.height / 2], FFormatSettings)
);
end;
procedure TZintRenderTargetSVG.DrawRect(const AParams: TZintDrawRectParams);
begin
FSVGFile.Append(
Format('<rect x="%f" y="%f" width="%f" height="%f" style="fill:%s;stroke-width:0"/>',
[AParams.x,
AParams.y,
AParams.Width,
AParams.Height,
FFGColor],FFormatSettings)
);
end;
procedure TZintRenderTargetSVG.DrawRing(const AParams: TZintDrawRingParams);
begin
FSVGFile.Append(Format('<circle cx="%f" cy="%f" r="%f" stroke="%s" stroke-width="%f" fill="none"/>',
[AParams.x,
AParams.y,
AParams.InnerRadius + (AParams.OuterRadius - AParams.InnerRadius) / 2,
FFGColor,
AParams.OuterRadius-AParams.InnerRadius],FFormatSettings));
end;
procedure TZintRenderTargetSVG.DrawRingFull(const AParams: TZintDrawRingParams);
begin
raise Exception.Create('not implemented');
end;
procedure TZintRenderTargetSVG.DrawStart;
begin
case FDocOption of
doSingleFile: begin
FSVGFile.Text:=
format('<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="%f" height="%f">',[FWidth, FHeight], FFormatSettings);
end;
doInsertIntoExisting: begin
FSVGFile.Text:=ReplaceText(FSVGFile.Text, '</svg>', '');
end;
end;
FSVGFile.Append('<g><title>' + ArrayOfByteToString(FSymbol.text) + '</title><desc>Barcode generated using Zint</desc>');
end;
procedure TZintRenderTargetSVG.DrawStop;
begin
FSVGFile.Append('</g>');
if FDocOption<>doAppendFlat then
FSVGFile.Append('</svg>');
end;
procedure TZintRenderTargetSVG.DrawText(const AParams: TZintDrawTextParams);
begin
FSVGFile.Append(
Format('<text x="%f" y="%f" fill="%s" font-family="%s" font-size="%f" style="text-anchor:middle">%s</text>',
[AParams.x, AParams.y+AParams.Height/2, FFGColor, FFont, AParams.Height, AParams.Text], FFormatSettings)
);
end;
procedure TZintRenderTargetSVG.Inflate(const ANewWidth, ANewHeight: Single);
begin
inherited;
end;
procedure TZintRenderTargetSVG.Render(ASymbol: TZintSymbol);
begin
inherited;
end;
end.