-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprnmain.pas
More file actions
1120 lines (831 loc) · 32 KB
/
prnmain.pas
File metadata and controls
1120 lines (831 loc) · 32 KB
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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{******* prnMain.pas *******}
{Formatting and Printing can sometimes be a challenging process. Here
is an example that prints columns that are right, left, and center
justified. There are headers, footers, and, generally, a bunch o'
things here. This application encapsulates functionality to print
text, lines, boxes and shaded boxes. Text can be left or right
justified and centered. Columns can be created and text can be left
or right justified within the columns or text can be centered.
Lines of any thickness can be drawn. Boxes can be drawn with any
thickness. The boxes can be shaded if desired. Headers and footers
can be created and the header/footer areas can be shaded if desired.
Page numbering can contain custom text and can be placed anywhere
desired.
Adapted for lazarus by Alan Chamberlain 12/5/2008}
unit prnmain;
{$mode DELPHI}{$H+}
interface
uses
SysUtils, Classes, Graphics, Printers;
const
HeaderLines = 5; { Number of allowable header lines }
FooterLines = 5; { Number of allowable footer lines }
Columns = 20; { Number of allowable columns }
type
THeaderRecord = Record
Text: String[240]; { Header text }
YPosition: Single; { Inches from the top }
Alignment: Integer; { 0:Left 1:Center 2:Right }
FontName: String[80]; { Font name }
FontSize: Integer; { Font size }
FontStyle: TFontStyles; { Font style }
End;
TFooterRecord = Record
Text: String[240]; { Footer text }
YPosition: Single; { Inches from the top }
Alignment: Integer; { 0:Left 1:Center 2:Right }
FontName: String[80]; { Font name }
FontSize: Integer; { Font size }
FontStyle: TFontStyles; { Font style }
End;
THeaderCoordinates = Record
XTop: Single;
YTop: Single;
XBottom: Single;
YBottom: Single;
Boxed: Boolean;
Shading: Word;
LineWidth: Word;
End;
TFooterCoordinates = Record
XTop: Single;
YTop: Single;
XBottom: Single;
YBottom: Single;
Boxed: Boolean;
Shading: Word;
LineWidth: Word;
End;
TPageNumberRecord = Record
YPosition: Single;
Text: String[240];
Alignment: Word;
FontName: String[80];
FontSize: Word;
FontStyle: TFontStyles;
End;
TColumnInformationRecord = Record
XPosition: Single;
Length: Single;
End;
TPrintObject = class
private
TopMargin: Integer; { Top margin in pixels }
BottomMargin: Integer; { Bottom margin in pixels }
LeftMargin: Integer; { Left margin in pixels }
RightMargin: Integer; { Right margin in pixels }
LineSpacing: Integer; { Line spacing in pixels }
PixelsPerInchVertical: Integer; { Number of pixels per inch
along Y axis }
PixelsPerInchHorizontal: Integer; { Number of pixels per inch
along X axis }
TotalPageWidthPixels: Integer; { Full width of page in pixels
includes gutters }
TotalPageHeightPixels: Integer; { Full height of page in pixels
includes gutters }
TotalPageHeightInches: Single; { Height of page in inches }
TotalPageWidthInches: Single; { Width of page in inches }
GutterLeft: Integer; { Unprintable area on left }
GutterRight: Integer; { Unprintable area on right }
GutterTop: Integer; { Unprintable area on top }
GutterBottom: Integer; { Unprintable area on bottom }
DetailTop: Single; { Inches from the top where the
detail section starts }
DetailBottom: Single; { Inches from the top where the
detail section ends }
LastYPosition: Single; { The Y position where the last
write occurred }
AutoPaging: Boolean; { Are new pages automatically generated? }
CurrentTab: Single; { The value of the current tab }
CurrentFontName: String[30];
CurrentFontSize: Integer;
CurrentFontStyle: TFontStyles;
Header: Array[1..HeaderLines] of THeaderRecord;
Footer: Array[1..FooterLines] of TFooterRecord;
ColumnInformation: Array[1..Columns] of TColumnInformationRecord;
PageNumber: TPageNumberRecord;
HeaderCoordinates: THeaderCoordinates;
FooterCoordinates: TFooterCoordinates;
function CalculateLineHeight: Integer;
function InchesToPixelsHorizontal( Inches: Single ): Integer;
function InchesToPixelsVertical( Inches: Single ): Integer;
function PixelsToInchesHorizontal( Pixels: Integer ): Single;
function PixelsToInchesVertical( Pixels: Integer ): Single;
function LinesToPixels( Line:Integer ): Integer;
procedure CalculateMeasurements;
procedure _DrawBox( XTop:Word; YTop:Word; XBottom:Word; YBottom:Word;
LineWidth:Word; Shading:Word );
public
procedure Start;
procedure Quit;
procedure Abort;
procedure SetMargins( Top:Single; Bottom:Single; Left:Single; Right:Single );
procedure SetLineSpacing(Spacing:single);
procedure SetFontInformation( Name:String; Size:Word; Style: TFontStyles );
procedure WriteLine( X:Single; Y:Single; Text:String );
procedure WriteLineRight( Y:Single; Text:String );
procedure WriteLineCenter( Y:Single; Text:String );
procedure WriteLineColumnRight( ColumnNumber:Word; Y:Single; Text:String );
procedure WriteLineColumnCenter( ColumnNumber:Word; Y:Single; Text:String );
procedure DrawLine( TopX:Single; TopY:Single; BottomX:Single; BottomY:Single;
LineWidth:Word );
procedure SetLineWidth( Width:Word );
function GetLineWidth: Word;
procedure SetTab( Inches:Single );
procedure NewPage;
function GetLinesPerPage: Integer;
procedure GetPixelsPerInch( var X:Word; var Y:Word );
procedure GetPixelsPerPage( var X:Word; var Y:Word );
procedure GetGutter( var Top:Word; var Bottom:Word; var Left:Word;
var Right:Word );
function GetTextWidth( Text:String ): Integer;
function GetLineHeightPixels: Word;
function GetLineHeightInches: Single;
function GetPageNumber:Integer;
function GetColumnsPerLine: Integer;
procedure SetOrientation( Orient: TPrinterOrientation );
procedure SetHeaderInformation( Line:Integer; YPosition: Single;
Text:String; Alignment:Word; FontName:String; FontSize: Word;
FontStyle: TFontStyles );
procedure SetFooterInformation( Line:Integer; YPosition: Single; Text:String;
Alignment:Word; FontName:String; FontSize: Word;
FontStyle: TFontStyles );
procedure WriteHeader;
procedure WriteFooter;
procedure SaveCurrentFont;
procedure RestoreCurrentFont;
procedure SetDetailTopBottom( Top: Single; Bottom: Single );
procedure SetAutoPaging( Value: Boolean );
procedure SetPageNumberInformation( YPosition:Single; Text:String;
Alignment:Word; FontName:String; FontSize:Word; FontStyle:TFontStyles );
procedure WritePageNumber;
procedure WriteLineColumn( ColumnNumber:Word; Y:Single; Text:String );
procedure DrawBox( XTop:Single; YTop:Single; XBottom:Single; YBottom:Single;
LineWidth:Word );
procedure DrawBoxShaded( XTop:Single; YTop:Single; XBottom:Single;
YBottom:Single; LineWidth:Word; Shading:Word );
procedure SetHeaderDimensions( XTop:Single; YTop:Single; XBottom:Single;
YBottom:Single; Boxed: Boolean; LineWidth:Word; Shading:Word );
procedure SetFooterDimensions( XTop:Single; YTop:Single; XBottom:Single;
YBottom:Single; Boxed: Boolean; LineWidth:Word; Shading:Word );
procedure CreateColumn( Number:Word; XPosition:Single; Length:Single );
procedure SetYPosition( YPosition:Single );
function GetYPosition: Single;
procedure NextLine;
function GetLinesLeft: Word;
function GetLinesInDetailArea: Word;
procedure SetTopOfPage;
procedure NewLines( Number:Word );
function GetFontName: String;
function GetFontSize: Word;
End;
implementation
procedure TPrintObject.Start;
{ This function MUST be called first before any other printing function }
var
Top,Bottom,Left,Right: Single;
I: Integer;
Begin
Printer.BeginDoc;
AutoPaging := True;
CalculateMeasurements;
PageNumber.Text := '';
Top := PixelsToInchesVertical( GutterTop );
Bottom := PixelsToInchesVertical( GutterBottom );
Left := PixelsToInchesHorizontal( GutterLeft );
Right := PixelsToInchesHorizontal( GutterRight );
SetMargins( Top,Bottom,Left,Right );
For I := 1 To HeaderLines Do
Header[I].Text := '';
HeaderCoordinates.Boxed := False;
HeaderCoordinates.Shading := 0;
For I := 1 To FooterLines Do
Footer[I].Text := '';
FooterCoordinates.Boxed := False;
FooterCoordinates.Shading := 0;
CurrentTab := 0.0;
LastYPosition := 0.0;
End;
procedure TPrintObject.Quit;
{ 'Quit' must always be called when printing is completed }
Begin
WriteHeader;
WriteFooter;
WritePageNumber;
Printer.EndDoc
End;
procedure TPrintObject.SetMargins( Top:Single; Bottom:Single; Left:Single;
Right:Single );
{ Set the top, bottom, left and right margins in inches }
var
Value: Single;
Buffer: String;
Begin
{ If the sum of the left and right margins exceeds the width of the page,
set the left margin to the value of 'GutterLeft' and set the right
margin to the value of 'GutterRight' }
If ( Left + Right >= TotalPageWidthInches ) Then
Begin
Left := PixelsToInchesHorizontal(GutterLeft);
Right := PixelsToInchesHorizontal(GutterRight);
End;
If ( Left <= 0 ) Then
Left := PixelsToInchesHorizontal(GutterLeft);
If ( Right <= 0 ) Then
Right := PixelsToInchesHorizontal(GutterRight);
{ If the sum of the top and bottom margins exceeds the height of the
page, set the top margin to the value of 'GutterTop' and set the
bottom margin to the value of 'GutterBottom' }
If ( Top + Bottom >= TotalPageHeightInches ) Then
Begin
Top := PixelsToInchesVertical(GutterTop);
Bottom := PixelsToInchesVertical(GutterBottom);
End;
If ( Top <= 0 ) Then
Top := PixelsToInchesVertical(GutterTop);
If ( Bottom <= 0 ) Then
Bottom := PixelsToInchesVertical(GutterBottom);
{ Convert everything to pixels }
TopMargin := InchesToPixelsVertical( Top );
If ( TopMargin < GutterTop ) Then
TopMargin := GutterTop;
BottomMargin := InchesToPixelsVertical( Bottom );
If ( BottomMargin < GutterBottom ) Then
BottomMargin := GutterBottom;
LeftMargin := InchesToPixelsHorizontal( Left );
If ( LeftMargin < GutterLeft ) Then
LeftMargin := GutterLeft;
RightMargin := InchesToPixelsHorizontal( Right );
If ( RightMargin < GutterRight ) Then
RightMargin := GutterRight;
End;
procedure TPrintObject.SetLineSpacing(Spacing:Single);
{Set line spacing to fraction of line height, i.e. 1 for single, 1.5 for one
and a half, 2 for double, etc. Held internally as integer}
begin
LineSpacing := Trunc(Spacing*10);
end;
procedure TPrintObject.WriteLine( X:Single; Y:Single; Text:String );
{ Write some text. The parameters represent inches from the left ('X')
and top ('Y') margins. }
var
XPixels: Integer;
YPixels: Integer;
Begin
{ How many pixels are there in the inches represented by 'X'? }
If ( X >= 0.0 ) Then
XPixels := InchesToPixelsHorizontal( X )
Else
XPixels := LeftMargin;
If ( XPixels < GutterLeft ) Then
XPixels := GutterLeft;
{ If there is a tab set, increase 'XPixels' by the amount of the tab }
If ( CurrentTab > 0.0 ) Then
Inc( XPixels,InchesToPixelsHorizontal(CurrentTab) );
{ How many pixels are there in the inches represented by 'Y'? }
If ( Y > -0.01 ) Then
{ Printing will occur at an absolute location from the top of the
page. }
Begin
YPixels := InchesToPixelsVertical( Y );
If ( YPixels < GutterTop ) Then
YPixels := GutterTop;
If ( YPixels > TotalPageHeightPixels ) Then
YPixels := TotalPageHeightPixels - GutterBottom;
LastYPosition := Y;
End;
If ( Y = -1.0 ) Then
{ Write the text at the next line }
Begin
If ( AutoPaging = True ) Then
Begin
{ If the next line we're going to write to exceeds beyond the
bottom of the detail section, issue a new page }
If ( LastYPosition + GetLineHeightInches > DetailBottom ) Then
NewPage;
End;
YPixels := InchesToPixelsVertical( LastYPosition + GetLineHeightInches );
LastYPosition := LastYPosition + GetLineHeightInches;
End;
If ( Y = -2.0 ) Then
{ Write the text on the current line }
YPixels := InchesToPixelsVertical( LastYPosition );
Printer.Canvas.TextOut( XPixels,YPixels,Text );
End;
procedure TPrintObject.WriteLineColumn( ColumnNumber:Word; Y:Single; Text:String );
{ Write text, left aligned against the column represented by
'ColumnInformation[ColumnNumber]' }
Begin
WriteLine( ColumnInformation[ColumnNumber].XPosition,Y,Text );
End;
procedure TPrintObject.WriteLineColumnRight( ColumnNumber:Word; Y:Single; Text:String );
{ Write text, right aligned against the column represented by
'ColumnInformation[ColumnNumber]' }
var
PixelLength: Word;
StartPixel: Word;
Begin
{ How many pixels does the text in 'Text' require? }
PixelLength := Printer.Canvas.TextWidth( Text );
{ Calculate where printing should start }
StartPixel := InchesToPixelsHorizontal( ColumnInformation[ColumnNumber].XPosition +
ColumnInformation[ColumnNumber].Length ) - PixelLength;
SetTab( 0.0 );
WriteLine( PixelsToInchesHorizontal(StartPixel),Y,Text );
SetTab( CurrentTab );
End;
procedure TPrintObject.WriteLineRight( Y:Single; Text:String );
{ Print a line of text right justified 'Y' inches from the top }
var
PixelLength: Word;
StartPixel: Word;
Begin
{ How many pixels does the text in 'Text' require? }
PixelLength := Printer.Canvas.TextWidth( Text );
{ Calculate where printing should start }
StartPixel := (TotalPageWidthPixels - RightMargin) - PixelLength;
SetTab( 0.0 );
WriteLine( PixelsToInchesHorizontal(StartPixel),Y,Text );
SetTab( CurrentTab );
End;
procedure TPrintObject.WriteLineCenter( Y:Single; Text:String );
{ Print a line of text centered at Y inches from the top }
var
PixelLength: Integer;
StartPixel: Integer;
Begin
{ How many pixels does the text in 'Text' require? }
PixelLength := Printer.Canvas.TextWidth( Text );
{ Calculate where printing should start }
StartPixel := (TotalPageWidthPixels Div 2) - (PixelLength Div 2);
SetTab( 0.0 );
WriteLine( PixelsToInchesHorizontal(StartPixel),Y,Text );
SetTab( CurrentTab );
End;
procedure TPrintObject.WriteLineColumnCenter( ColumnNumber:Word; Y:Single; Text:String );
{ Print a line of text centered within the column number represented by
'ColumnNumber', at Y inches from the top }
var
PixelLength: Integer;
StartPixel: Integer;
Pixels: Integer;
Begin
{ How many pixels does the text in 'Text' require? }
PixelLength := Printer.Canvas.TextWidth( Text );
{ Calculate where printing should start }
Pixels := InchesToPixelsHorizontal( ColumnInformation[ColumnNumber].Length );
StartPixel := (InchesToPixelsHorizontal( ColumnInformation[ColumnNumber].Length )
Div 2) + InchesToPixelsHorizontal(ColumnInformation[ColumnNumber].XPosition) -
(PixelLength Div 2);
SetTab( 0.0 );
WriteLine( PixelsToInchesHorizontal(StartPixel),Y,Text );
SetTab( CurrentTab );
End;
procedure TPrintObject.DrawLine( TopX:Single; TopY:Single; BottomX:Single;
BottomY:Single; LineWidth:Word );
{ Draw a line beginning at a particular X,Y coordinate and ending at a
particular X,Y coordinate. }
var
TopXPixels, BottomXPixels, TopYPixels, BottomYPixels: Integer;
Begin
TopXPixels := InchesToPixelsHorizontal( TopX );
BottomXPixels := InchesToPixelsHorizontal( BottomX );
TopYPixels := InchesToPixelsVertical( TopY );
BottomYPixels := InchesToPixelsVertical( BottomY );
Printer.Canvas.Pen.Width := LineWidth;
Printer.Canvas.MoveTo( TopXPixels,TopYPixels );
Printer.Canvas.LineTo( BottomXPixels,BottomYPixels );
End;
procedure TPrintObject.SetFontInformation( Name:String; Size:Word; Style: TFontStyles );
{ Change the current font information }
Begin
Printer.Canvas.Font.Name := Name;
Printer.Canvas.Font.Size := Size;
Printer.Canvas.Font.Style := Style;
CalculateMeasurements;
End;
function TPrintObject.GetFontName: String;
{ Return the current font name }
Begin
Result := Printer.Canvas.Font.Name;
End;
function TPrintObject.GetFontSize: Word;
{ Return the current font size }
Begin
Result := Printer.Canvas.Font.Size;
End;
procedure TPrintObject.SetOrientation( Orient: TPrinterOrientation );
Begin
Printer.Orientation := Orient;
CalculateMeasurements;
End;
function TPrintObject.CalculateLineHeight: Integer;
{ Calculate the height of a line plus the normal amount of space between
each line }
var CharHeight: integer;
Begin
CharHeight := Printer.Canvas.TextHeight('Gg') + 2;
Result := CharHeight*LineSpacing div 10
End;
procedure TPrintObject.NewPage;
{ Issue a new page }
Begin
WriteHeader;
WriteFooter;
WritePageNumber;
LastYPosition := DetailTop - GetLineHeightInches;
Printer.NewPage;
End;
function TPrintObject.GetPageNumber;
{ Return the current page number }
Begin
Result := Printer.PageNumber;
End;
function TPrintObject.GetTextWidth( Text:String ): Integer;
{ Return the width of the text contained in 'Text' in pixels }
Begin
Result := Printer.Canvas.TextWidth( Text );
End;
function TPrintObject.GetLineHeightPixels: Word;
Begin
Result := CalculateLineHeight;
End;
function TPrintObject.GetLineHeightInches: Single;
Begin
Result := PixelsToInchesVertical( GetLineHeightPixels );
End;
procedure TPrintObject._DrawBox( XTop:Word; YTop:Word; XBottom:Word; YBottom:Word;
LineWidth:Word; Shading:Word );
{ The low level routine which actually draws the box and shades it as
desired. The paramaters are in pixels and not inches. }
var Colour:TColor;
Begin
Colour := Printer.Canvas.Brush.Color;
Printer.Canvas.Pen.Width := LineWidth;
Printer.Canvas.Brush.Color := RGBToColor( Shading,Shading,Shading );
Printer.Canvas.Rectangle( XTop,YTop,XBottom,YBottom );
Printer.Canvas.Brush.Color := Colour;
End;
procedure TPrintObject.DrawBox( XTop:Single; YTop:Single; XBottom:Single;
YBottom:Single; LineWidth:Word );
{ Draw a box at the X,Y coordinates passed in the parameters }
var
BLinePixels,BColPixels,ELinePixels,EColPixels: Integer;
Begin
BLinePixels := InchesToPixelsVertical( YTop );
ELinePixels := InchesToPixelsVertical( YBottom );
BColPixels := InchesToPixelsHorizontal( XTop );
EColPixels := InchesToPixelsHorizontal( XBottom );
_DrawBox( BColPixels,BLinePixels,EColPixels,ELinePixels,LineWidth,255 );
End;
procedure TPrintObject.DrawBoxShaded( XTop:Single; YTop:Single; XBottom:Single;
YBottom:Single; LineWidth:Word; Shading:Word );
{ Draw a box at the X,Y coordinates passed in the parameters }
var
BLinePixels,BColPixels,ELinePixels,EColPixels: Integer;
Begin
BLinePixels := InchesToPixelsVertical( YTop );
ELinePixels := InchesToPixelsVertical( YBottom );
BColPixels := InchesToPixelsHorizontal( XTop );
EColPixels := InchesToPixelsHorizontal( XBottom );
_DrawBox( BColPixels,BLinePixels,EColPixels,ELinePixels,LineWidth,Shading );
End;
function TPrintObject.GetLinesPerPage: Integer;
{ Return the number of lines on the entire page }
Begin
Result := (TotalPageHeightPixels - GutterTop - GutterBottom) Div CalculateLineHeight;
End;
function TPrintObject.GetLinesInDetailArea: Word;
{ Return the number of lines in the detail area }
Begin
Result := InchesToPixelsVertical( DetailBottom - DetailTop ) Div CalculateLineHeight;
End;
procedure TPrintObject.GetPixelsPerInch( var X:Word; var Y:Word );
Begin
X := PixelsPerInchHorizontal;
Y := PixelsPerInchVertical;
End;
procedure TPrintObject.GetPixelsPerPage( var X:Word; var Y:Word );
Begin
X := TotalPageWidthPixels - GutterLeft - GutterRight;
Y := TotalPageHeightPixels - GutterTop - GutterBottom;
End;
procedure TPrintObject.GetGutter( var Top:Word; var Bottom:Word; var Left:Word;
var Right:Word );
Begin
Top := GutterTop;
Bottom := GutterBottom;
Left := GutterLeft;
Right := GutterRight;
End;
procedure TPrintObject.Abort;
Begin
Printer.Abort;
End;
function TPrintObject.GetColumnsPerLine: Integer;
{ How many columns are there in a Line? }
var
Pixels: Integer;
Begin
Pixels := TotalPageWidthPixels - GutterLeft - GutterRight;
Result := Pixels Div Printer.Canvas.TextWidth( 'B' );
End;
function TPrintObject.InchesToPixelsHorizontal( Inches: Single ): Integer;
{ Convert the horizontal inches represented in 'Inches' to pixels }
var
Value: Single;
Begin
Value := Inches * PixelsPerInchHorizontal;
Result := Trunc(Value);
End;
function TPrintObject.InchesToPixelsVertical( Inches: Single ): Integer;
{ Convert the vertical inches represented in 'Inches' to pixels }
var
Value: Single;
Buffer: String;
I: Integer;
Begin
Value := Inches * PixelsPerInchVertical;
Result := Trunc(Value);
End;
function TPrintObject.PixelsToInchesHorizontal( Pixels: Integer ): Single;
Begin
Result := Pixels / PixelsPerInchHorizontal;
End;
function TPrintObject.PixelsToInchesVertical( Pixels: Integer ): Single;
Begin
Result := Pixels / PixelsPerInchVertical;
End;
function TPrintObject.LinesToPixels( Line:Integer ): Integer;
{ Calculate the number of vertical pixels in 'Line' }
Begin
If ( Line <= 0 ) Then
Line := 1;
Result := (Line-1) * CalculateLineHeight;
End;
procedure TPrintObject.SetLineWidth( Width:Word );
Begin
Printer.Canvas.Pen.Width := Width;
End;
function TPrintObject.GetLineWidth: Word;
Begin
Result := Printer.Canvas.Pen.Width;
End;
procedure TPrintObject.CalculateMeasurements;
{ Calculate some necessary measurements. }
Begin
{ Calculate the number of pixels per inch vertical and horizontal}
PixelsPerInchVertical := Printer.XDPI;
PixelsPerInchHorizontal := Printer.YDPI;
{ Get the gutter on the left and top.}
with Printer.PaperSize.PaperRect do
begin
GutterLeft := WorkRect.Left;
GutterTop := WorkRect.Top;
TotalPageWidthPixels := PhysicalRect.Right - PhysicalRect.Left;
TotalPageHeightPixels := PhysicalRect.Bottom - PhysicalRect.Top;
end;
TotalPageWidthInches := TotalPageWidthPixels / PixelsPerInchHorizontal;
TotalPageHeightInches := TotalPageHeightPixels / PixelsPerInchVertical;
GutterRight := TotalPageWidthPixels - GutterLeft - Printer.PageWidth;
GutterBottom := TotalPageHeightPixels - GutterTop - Printer.PageHeight;
If ( TopMargin < GutterTop ) Then
TopMargin := GutterTop;
If ( BottomMargin < GutterBottom ) Then
BottomMargin := GutterBottom;
If ( LeftMargin < GutterLeft ) Then
LeftMargin := GutterLeft;
If ( RightMargin < GutterRight ) Then
RightMargin := GutterRight;
End;
procedure TPrintObject.SetHeaderInformation( Line:Integer; YPosition: Single;
Text:String; Alignment:Word; FontName:String; FontSize: Word;
FontStyle: TFontStyles );
Begin
If ( Line > HeaderLines ) Then
Exit;
Header[Line].Text := Text;
Header[Line].YPosition := YPosition;
Header[Line].Alignment := Alignment;
Header[Line].FontName := FontName;
Header[Line].FontSize := FontSize;
Header[Line].FontStyle := FontStyle;
End;
procedure TPrintObject.SetFooterInformation( Line:Integer; YPosition: Single;
Text:String; Alignment:Word; FontName:String; FontSize: Word;
FontStyle: TFontStyles );
Begin
If ( Line > FooterLines ) Then
Exit;
Footer[Line].Text := Text;
Footer[Line].YPosition := YPosition;
Footer[Line].Alignment := Alignment;
Footer[Line].FontName := FontName;
Footer[Line].FontSize := FontSize;
Footer[Line].FontStyle := FontStyle;
End;
procedure TPrintObject.WriteHeader;
{ If any headers are defined, write them }
var
I: Integer;
Begin
{ Does the user desire a box around the header? }
If ( HeaderCoordinates.Boxed = True ) Then
Begin
If ( HeaderCoordinates.Shading > 0 ) Then
DrawBoxShaded( HeaderCoordinates.XTop,HeaderCoordinates.YTop,
HeaderCoordinates.XBottom,HeaderCoordinates.YBottom,
HeaderCoordinates.LineWidth,HeaderCoordinates.Shading)
Else
DrawBox( HeaderCoordinates.XTop,HeaderCoordinates.YTop,
HeaderCoordinates.XBottom,HeaderCoordinates.YBottom,
HeaderCoordinates.LineWidth );
End;
SaveCurrentFont;
For I := 1 To HeaderLines Do
Begin
If ( Length(Header[I].Text) > 0 ) Then
Begin
With Header[I] Do
Begin
SetFontInformation( FontName,FontSize,FontStyle );
If ( Alignment = 0 ) Then
WriteLine( LeftMargin, YPosition, Text );
If ( Alignment = 1 ) Then
WriteLineCenter( YPosition, Text );
If ( Alignment = 2 ) Then
WriteLineRight( YPosition, Text );
End;
End;
RestoreCurrentFont;
End;
End;
procedure TPrintObject.WriteFooter;
{ If any footers are defined, write them }
var
I: Integer;
Temp: Boolean;
Begin
SaveCurrentFont;
{ Set 'AutoPaging' off. Otherwise the footer will not get written
correctly. }
Temp := AutoPaging;
AutoPaging := False;
For I := 1 To FooterLines Do
Begin
If ( Length(Footer[I].Text) > 0 ) Then
Begin
With Footer[I] Do
Begin
SetFontInformation( FontName,FontSize,FontStyle );
If ( Alignment = 0 ) Then
WriteLine( LeftMargin, YPosition, Text );
If ( Alignment = 1 ) Then
WriteLineCenter( YPosition, Text );
If ( Alignment = 2 ) Then
WriteLineRight( YPosition, Text );
End;
End;
RestoreCurrentFont;
End;
{ Does the user desire a box around the footer? }
If ( FooterCoordinates.Boxed = True ) Then
Begin
If ( FooterCoordinates.Shading > 0 ) Then
DrawBoxShaded( FooterCoordinates.XTop,FooterCoordinates.YTop,
FooterCoordinates.XBottom,FooterCoordinates.YBottom,
FooterCoordinates.LineWidth,FooterCoordinates.Shading
)
Else
DrawBox( FooterCoordinates.XTop,FooterCoordinates.YTop,
FooterCoordinates.XBottom,FooterCoordinates.YBottom,
FooterCoordinates.LineWidth );
End;
AutoPaging := Temp;
End;
procedure TPrintObject.SaveCurrentFont;
Begin
CurrentFontName := Printer.Canvas.Font.Name;
CurrentFontSize := Printer.Canvas.Font.Size;
CurrentFontStyle := Printer.Canvas.Font.Style;
End;
procedure TPrintObject.RestoreCurrentFont;
Begin
SetFontInformation( CurrentFontName,CurrentFontSize,CurrentFontStyle );
End;
procedure TPrintObject.SetDetailTopBottom( Top: Single; Bottom: Single );
Begin
DetailTop := Top;
DetailBottom := Bottom;
LastYPosition := Top - GetLineHeightInches;
End;
procedure TPrintObject.SetAutoPaging( Value: Boolean );
Begin
AutoPaging := Value;
End;
procedure TPrintObject.SetPageNumberInformation( YPosition:Single;
Text:String; Alignment:Word; FontName:String;
FontSize:Word; FontStyle:TFontStyles );
Begin
PageNumber.Text := Text;
PageNumber.YPosition := YPosition;
PageNumber.Alignment := Alignment;
PageNumber.FontName := FontName;
PageNumber.FontSize := FontSize;
PageNumber.FontStyle := FontStyle;
End;
procedure TPrintObject.WritePageNumber;
var
Buffer: String;
Temp: Boolean;
Begin
Buffer := Format( PageNumber.Text,[Printer.PageNumber] );
SaveCurrentFont;
SetFontInformation( PageNumber.FontName,PageNumber.FontSize,PageNumber.FontStyle );
Temp := AutoPaging;