-
Notifications
You must be signed in to change notification settings - Fork 4
/
ubarcodes.pas
439 lines (384 loc) · 9.73 KB
/
ubarcodes.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
unit ubarcodes;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Controls, LResources, Graphics,
ubasic,uqr,zint,urender,uaztec,udatamatrix;
type
TBarcodeQR_ECCLevel= (eBarcodeQR_ECCLevel_Auto=0,
eBarcodeQR_ECCLevel_L=1,eBarcodeQR_ECCLevel_M=2,
eBarcodeQR_ECCLevel_Q=3,eBarcodeQR_ECCLevel_H=4);
TBarcodeAztecRune_Value= 0..999;
{ TLazBarcodeCustomBase }
TLazBarcodeCustomBase=class(TGraphicControl)
private
mIsPainting: Boolean;
function GetStrictSize: Boolean;
procedure SetStrictSize(const AValue: Boolean);
function GetBackgroundColor: TColor;
function GetForegroundColor: TColor;
procedure SetBackgroundColor(const AValue: TColor);
procedure SetForegroundColor(const AValue: TColor);
protected
FQR: PointerTo_zint_symbol;
FLastErrorString: UTF8String;
FBackgroundColor: TColor;
FForegroundColor: TColor;
FStrictSize: Boolean;
procedure GenerateAndInvalidate;
procedure Paint; override;
procedure Resize; override;
procedure intfPaintOnCanvas(const aTargetCanvas: TCanvas; const aRect: TRect);
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure PaintOnCanvas(const aTargetCanvas: TCanvas; const aRect: TRect);
procedure Generate; virtual; abstract;
published
property StrictSize: Boolean read GetStrictSize write SetStrictSize;
property BackgroundColor: TColor read GetBackgroundColor write SetBackgroundColor default clWhite;
property ForegroundColor: TColor read GetForegroundColor write SetForegroundColor default clBlack;
end;
{ TLazBarcodeCustomText }
TLazBarcodeCustomText=class(TLazBarcodeCustomBase)
private
function GetText: UTF8String;
procedure SetText(const AValue: UTF8String);
protected
FText: UTF8String;
public
constructor Create(AOwner: TComponent); override;
published
property Text: UTF8String read GetText write SetText;
end;
{ TBarcodeQR }
TBarcodeQR=class(TLazBarcodeCustomText)
private
procedure SetECCLevel(const AValue: TBarcodeQR_ECCLevel);
protected
FECCLevel: TBarcodeQR_ECCLevel;
procedure UpdateECCLevel;
public
procedure Generate; override;
published
property ECCLevel: TBarcodeQR_ECCLevel read FECCLevel write SetECCLevel default eBarcodeQR_ECCLevel_Auto;
end;
{ TBarcodeMicroQR }
TBarcodeMicroQR=class(TBarcodeQR)
private
protected
public
procedure Generate; override;
published
end;
{ TBarcodeAztec }
TBarcodeAztec=class(TLazBarcodeCustomText)
private
protected
public
procedure Generate; override;
published
end;
{ TBarcodeAztecRune }
TBarcodeAztecRune=class(TLazBarcodeCustomBase)
private
procedure SetValue(const AValue: TBarcodeAztecRune_Value);
protected
FValue: TBarcodeAztecRune_Value;
public
procedure Generate; override;
published
property Value: TBarcodeAztecRune_Value read FValue write SetValue;
end;
{ TBarcodeDataMatrix }
TBarcodeDataMatrix=class(TLazBarcodeCustomText)
private
protected
public
procedure Generate; override;
published
end;
procedure Register;
implementation
procedure Register;
begin
{$I tbarcodeqr.lrs}
RegisterComponents('Laz Barcodes 2D',[TBarcodeQR]);
{$I tbarcodemicroqr.lrs}
RegisterComponents('Laz Barcodes 2D',[TBarcodeMicroQR]);
{$I tbarcodeaztec.lrs}
RegisterComponents('Laz Barcodes 2D',[TBarcodeAztec]);
{$I tbarcodeaztecrune.lrs}
RegisterComponents('Laz Barcodes 2D',[TBarcodeAztecRune]);
{$I tbarcodedatamatrix.lrs}
RegisterComponents('Laz Barcodes 2D',[TBarcodeDataMatrix]);
end;
{ TBarcodeDataMatrix }
procedure TBarcodeDataMatrix.Generate;
var
ErrorCode: integer;
begin
if Assigned(FQR) then begin
ZBarcode_Delete(FQR);
FQR:=nil;
end;
if Length(FText)>0 then begin
FQR:=ZBarcode_Create();
with FQR^ do begin
border_width:=1;
end;
ErrorCode:=dmatrix(FQR,@FText[1],Length(FText));
if ErrorCode<>0 then begin
FLastErrorString:=FQR^.errtxt;
exit;
end;
end;
end;
{ TBarcodeAztecRune }
procedure TBarcodeAztecRune.SetValue(const AValue: TBarcodeAztecRune_Value);
begin
if FValue=AValue then exit;
FValue:=AValue;
GenerateAndInvalidate;
end;
procedure TBarcodeAztecRune.Generate;
var
ErrorCode: integer;
NumberText: UTF8String;
begin
if Assigned(FQR) then begin
ZBarcode_Delete(FQR);
FQR:=nil;
end;
NumberText:=IntToStr(FValue);
if (Length(NumberText)>0) and (Length(NumberText)<=3) then begin
FQR:=ZBarcode_Create();
with FQR^ do begin
border_width:=0;
end;
ErrorCode:=aztec_runes(FQR,@NumberText[1],Length(NumberText));
if ErrorCode<>0 then begin
FLastErrorString:=FQR^.errtxt;
exit;
end;
end;
end;
{ TBarcodeAztec }
procedure TBarcodeAztec.Generate;
var
ErrorCode: integer;
begin
if Assigned(FQR) then begin
ZBarcode_Delete(FQR);
FQR:=nil;
end;
if Length(FText)>0 then begin
FQR:=ZBarcode_Create();
with FQR^ do begin
border_width:=0;
end;
ErrorCode:=aztec(FQR,@FText[1],Length(FText));
if ErrorCode<>0 then begin
FLastErrorString:=FQR^.errtxt;
exit;
end;
end;
end;
{ TLazBarcodeCustomBase }
function TLazBarcodeCustomBase.GetStrictSize: Boolean;
begin
Result:=FStrictSize;
end;
procedure TLazBarcodeCustomBase.SetStrictSize(const AValue: Boolean);
begin
if FStrictSize<>AValue then begin
FStrictSize:=AValue;
GenerateAndInvalidate;
end;
end;
function TLazBarcodeCustomBase.GetBackgroundColor: TColor;
begin
Result:=FBackgroundColor;
end;
function TLazBarcodeCustomBase.GetForegroundColor: TColor;
begin
Result:=FForegroundColor;
end;
procedure TLazBarcodeCustomBase.SetBackgroundColor(const AValue: TColor);
begin
if FBackgroundColor<>AValue then begin
FBackgroundColor:=AValue;
GenerateAndInvalidate;
end;
end;
procedure TLazBarcodeCustomBase.SetForegroundColor(const AValue: TColor);
begin
if FForegroundColor<>AValue then begin
FForegroundColor:=AValue;
GenerateAndInvalidate;
end;
end;
procedure TLazBarcodeCustomBase.GenerateAndInvalidate;
begin
Generate;
Self.Invalidate;
end;
procedure TLazBarcodeCustomBase.Paint;
begin
if mIsPainting then exit;
mIsPainting:=true;
intfPaintOnCanvas(Canvas, self.ClientRect);
mIsPainting:=false;
end;
procedure TLazBarcodeCustomBase.Resize;
begin
inherited Resize;
Generate;
end;
constructor TLazBarcodeCustomBase.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FBackgroundColor:=clWhite;
FForegroundColor:=clBlack;
FStrictSize:=true;
end;
destructor TLazBarcodeCustomBase.Destroy;
begin
if Assigned(FQR) then begin
ZBarcode_Delete(FQR);
FQR:=nil;
end;
inherited Destroy;
end;
procedure TLazBarcodeCustomBase.PaintOnCanvas(const aTargetCanvas: TCanvas;
const aRect: TRect);
begin
//Destroy rendering
Generate;
//Create new rendering
intfPaintOnCanvas(aTargetCanvas,aRect);
//Destroy rendering, new rendering generated when paint called.
Generate;
end;
procedure TLazBarcodeCustomBase.intfPaintOnCanvas(const aTargetCanvas: TCanvas;
const aRect: TRect);
var
ErrorCode: integer;
Line: PointerTo_zint_render_line;
BaseX,BaseY: integer;
X,Y: integer;
procedure ClearBackground;
begin
aTargetCanvas.Brush.Color:=FBackgroundColor;
aTargetCanvas.FillRect(aRect);
end;
begin
if not aTargetCanvas.HandleAllocated then exit;
if not Assigned(FQR) then begin
ClearBackground;
exit;
end;
if not Assigned(FQR^.rendered) then begin
X:=aRect.Right-aRect.Left+1;
Y:=aRect.Bottom-aRect.Top+1;
if not FStrictSize then begin
ErrorCode:=render_plot(FQR,X,Y);
end else begin
BaseX:=FQR^.width+FQR^.border_width*2;
BaseY:=FQR^.rows+FQR^.border_width*2;
ErrorCode:=render_plot(FQR,X-(X mod BaseX),(Y-(Y mod BaseY)));
end;
if ErrorCode<>1 then begin
FLastErrorString:=FQR^.errtxt;
exit;
end else begin
FLastErrorString:='';
end;
end;
if Assigned(FQR^.rendered) then begin
Line:=FQR^.rendered^.lines;
ClearBackground;
aTargetCanvas.Brush.Color:=FForegroundColor;
while Assigned(Line) do begin
aTargetCanvas.FillRect( round(Line^.x)+aRect.Left,round(Line^.y)+aRect.Top,
round(Line^.x+Line^.width)+aRect.Left,round(Line^.y+Line^.length)+aRect.Top);
Line:=Line^.next;
end;
end;
end;
function TLazBarcodeCustomText.GetText: UTF8String;
begin
Result:=FText;
end;
procedure TLazBarcodeCustomText.SetText(const AValue: UTF8String);
begin
if FText<>AValue then begin
FText:=AValue;
GenerateAndInvalidate;
end;
end;
constructor TLazBarcodeCustomText.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FText:=ClassName;
end;
{ TBarcodeQR }
procedure TBarcodeQR.SetECCLevel(const AValue: TBarcodeQR_ECCLevel);
begin
if FECCLevel=AValue then exit;
FECCLevel:=AValue;
GenerateAndInvalidate;
end;
procedure TBarcodeQR.UpdateECCLevel;
begin
case FECCLevel of
eBarcodeQR_ECCLevel_L: FQR^.option_1:=1;
eBarcodeQR_ECCLevel_M: FQR^.option_1:=2;
eBarcodeQR_ECCLevel_Q: FQR^.option_1:=3;
eBarcodeQR_ECCLevel_H: FQR^.option_1:=4;
else begin
FQR^.option_1:=0;
FECCLevel:=eBarcodeQR_ECCLevel_Auto;
end;
end;
end;
procedure TBarcodeQR.Generate;
var
ErrorCode: integer;
begin
if Assigned(FQR) then begin
ZBarcode_Delete(FQR);
FQR:=nil;
end;
FQR:=ZBarcode_Create();
with FQR^ do begin
border_width:=4;
end;
UpdateECCLevel;
ErrorCode:=qr_code(FQR,@FText[1],Length(FText));
if ErrorCode<>0 then begin
FLastErrorString:=FQR^.errtxt;
exit;
end;
end;
{ TBarcodeMicroQR }
procedure TBarcodeMicroQR.Generate;
var
ErrorCode: integer;
begin
if Assigned(FQR) then begin
ZBarcode_Delete(FQR);
FQR:=nil;
end;
FQR:=ZBarcode_Create();
with FQR^ do begin
border_width:=4;
end;
UpdateECCLevel;
ErrorCode:=microqr(FQR,@FText[1],Length(FText));
if ErrorCode<>0 then begin
FLastErrorString:=FQR^.errtxt;
exit;
end;
end;
end.