-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathSynEditPrint.pas
1014 lines (958 loc) · 32.6 KB
/
SynEditPrint.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
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
{-------------------------------------------------------------------------------
The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
the specific language governing rights and limitations under the License.
The Original Code is: SynEditPrint.pas, released 2000-06-01.
The Initial Author of the Original Code is Morten J. Skovrup.
Portions written by Morten J. Skovrup are copyright 2000 Morten J. Skovrup.
All Rights Reserved.
Contributors to the SynEdit project are listed in the Contributors.txt file.
Alternatively, the contents of this file may be used under the terms of the
GNU General Public License Version 2 or later (the "GPL"), in which case
the provisions of the GPL are applicable instead of those above.
If you wish to allow use of your version of this file only under the terms
of the GPL and not to allow others to use your version of this file
under the MPL, indicate your decision by deleting the provisions above and
replace them with the notice and other provisions required by the GPL.
If you do not delete the provisions above, a recipient may use your version
of this file under either the MPL or the GPL.
$Id: SynEditPrint.pas,v 1.2 2004/07/11 02:01:05 andersonrb Exp $
You may retrieve the latest version of this file at the SynEdit home page,
located at http://SynEdit.SourceForge.net
Known Issues:
Wrapping across page boundaries is not supported
-------------------------------------------------------------------------------}
{-------------------------------------------------------------------------------
CONTENTS:
Print controller component.
Allows setting margins, headers and footers.
Design time properties:
Header : Class property to set properties for headers -
see CSynEditHeaderFooter.pas
Footer : Class property to set properties for footers -
see CSynEditHeaderFooter.pas
Margins : Class property to set properties for margins -
see CSynEditPrintMargins.pas
Lines : The lines that should be printed (see also SynEdit the
property below)
Font : The font the lines should be printed in (see also SynEdit
the property below)
Title : A title - can be referenced in headers/footers by using the
$TITLE$ macro
Wrap : Wrap text to margins
Highlight : Highlight text
Colors : Print in colors
LineNumbers : Print line numbers
LineOffset : Value added to linenumbers when printing
PageOffset : Value added to pagenumbers when printing
OnPrintLine : Fired when a line is printed
OnPrintStatus : Fired at Beginning, End and when a new page is started
Highlighter : The highlighter used for highlighting the text (see also the
SynEdit property below)
LineNumbersInMargin : If true line numbers are printed in the left margin,
else left margin is increased by width of line
number text.
SelectedOnly : Print only the selected Area
Run-time properties:
DocTitle : Used to display the document name in the print queue monitor //JJV 2000-10-13
PrinterInfo : Read only. Returns info on printer (used internally)
PageCount : Returns the total number of pages;
SynEdit : By setting SynEdit to a specific TSynEdit component, the
properties Lines, Font and Highlighter are automatically
set to the corresponding values of the TSynEdit component
Run-time methods:
UpdatePages : Used internally by the TSynEditPrintPreview component
PrintToCanvas : Used internally by the TSynEditPrintPreview component
Print : Prints the contents of the Lines property
PrintRange(StartPage,EndPage) : Prints the specified page-range (both inclusive)
-------------------------------------------------------------------------------}
{$IFNDEF QSYNEDITPRINT}
unit SynEditPrint;
{$ENDIF}
{$M+}
{$I SynEdit.inc}
interface
uses
{$IFDEF SYN_CLX}
Qt,
QGraphics,
QPrinters,
Types,
QSynEdit,
QSynEditTypes,
QSynEditPrintTypes,
QSynEditPrintHeaderFooter,
QSynEditPrinterInfo,
QSynEditPrintMargins,
QSynEditMiscProcs,
QSynEditHighlighter,
{$ELSE}
Windows,
Graphics,
Printers,
SynEdit,
SynEditTypes,
SynEditPrintTypes,
SynEditPrintHeaderFooter,
SynEditPrinterInfo,
SynEditPrintMargins,
SynEditMiscProcs,
SynEditHighlighter,
{$ENDIF}
SysUtils,
Classes;
type
TPageLine =
class
public
FirstLine: Integer;
end;
//The actual print controller object
TSynEditPrint =
class(TComponent)
private
FCopies: integer; //EK 10/16/01
FFooter: TFooter;
FHeader: THeader;
FLines: TStrings;
FMargins: TSynEditPrintMargins;
FPageCount: Integer;
FFont: TFont;
FTitle: string;
FDocTitle: string; //JJV 2000-10-13
FPrinterInfo: TSynEditPrinterInfo;
FPages: TList;
FCanvas: TCanvas;
{************}
FCharWidth: Integer;
FMaxLeftChar: Integer;
FETODist: PIntArray;
FWrap: Boolean;
FOnPrintLine: TPrintLineEvent;
FOnPrintStatus: TPrintStatusEvent;
FYPos: Integer;
FLineHeight: Integer;
FHighlight: Boolean;
FColors: Boolean;
FHighlighter: TSynCustomHighlighter;
FOldFont: TFont;
FSynOK: Boolean;
FLineNumbers: Boolean;
FLineNumber: Integer;
FLineOffset: Integer;
FAbort: Boolean;
FPrinting: Boolean;
FDefaultBG: TColor;
FPageOffset: Integer;
FRangesOK: Boolean;
FMaxWidth: integer;
FMaxCol: Integer;
FPagesCounted: Boolean;
FLineNumbersInMargin: Boolean;
FTabWidth: integer;
fFontColor: TColor; // djlp 2000-09-20
fSelectedOnly: Boolean; // jj 2001-07-23
fSelAvail: Boolean;
fSelMode: TSynSelectionMode;
fBlockBegin: TBufferCoord;
fBlockEnd: TBufferCoord;
procedure CalcPages;
procedure SetLines(const Value: TStrings);
procedure SetFont(const Value: TFont);
procedure SetCharWidth(const Value: Integer);
procedure SetMaxLeftChar(const Value: Integer);
procedure PrintPage(Num: Integer);
procedure WriteLine(Text: string);
procedure WriteLineNumber;
procedure HandleWrap(Text: string; MaxWidth: Integer);
procedure TextOut(Text: string; AList: TList);
procedure SetHighlighter(const Value: TSynCustomHighlighter);
procedure RestoreCurrentFont;
procedure SaveCurrentFont;
procedure SetPixelsPrInch;
procedure InitPrint;
procedure InitRanges;
function GetPageCount: Integer;
procedure SetSynEdit(const Value: TCustomSynEdit);
procedure SetFooter(const Value: TFooter);
procedure SetHeader(const Value: THeader);
procedure SetMargins(const Value: TSynEditPrintMargins);
protected
property MaxLeftChar: Integer read FMaxLeftChar write SetMaxLeftChar;
property CharWidth: Integer read FCharWidth write SetCharWidth;
procedure PrintStatus(Status: TSynPrintStatus; PageNumber: integer;
var Abort: boolean); virtual;
procedure PrintLine(LineNumber, PageNumber: Integer); virtual;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure UpdatePages(ACanvas: TCanvas);
procedure PrintToCanvas(ACanvas: TCanvas; PageNumber: Integer);
procedure Print;
procedure PrintRange(StartPage, EndPage: Integer);
property PrinterInfo: TSynEditPrinterInfo read FPrinterInfo;
property PageCount: Integer read GetPageCount;
property SynEdit: TCustomSynEdit write SetSynEdit;
procedure LoadFromStream(AStream: TStream);
procedure SaveToStream(AStream: TStream);
published
property Copies: integer read FCopies write FCopies;
property Header: THeader read FHeader write SetHeader;
property Footer: TFooter read FFooter write SetFooter;
property Margins: TSynEditPrintMargins read FMargins write SetMargins;
property Lines: TStrings read FLines write SetLines;
property Font: TFont read FFont write SetFont;
property Title: string read FTitle write FTitle;
property DocTitle: string read FDocTitle write FDocTitle; //JJV 2000-10-13
property Wrap: Boolean read FWrap write FWrap default True;
property Highlight: Boolean read FHighlight write FHighlight default True;
property SelectedOnly: Boolean read FSelectedOnly write FSelectedOnly // jj 2001-07-23
default False;
property Colors: Boolean read FColors write FColors default False;
property LineNumbers: Boolean read FLineNumbers write FLineNumbers
default False;
property LineOffset: Integer read FLineOffset write FLineOffset default 0;
property PageOffset: Integer read FPageOffset write FPageOffset default 0;
property OnPrintLine: TPrintLineEvent read FOnPrintLine write FOnPrintLine;
property OnPrintStatus: TPrintStatusEvent read FOnPrintStatus
write FOnPrintStatus;
property Highlighter: TSynCustomHighlighter read FHighlighter
write SetHighlighter;
property LineNumbersInMargin: Boolean read FLineNumbersInMargin
write FLineNumbersInMargin default False;
property TabWidth: integer read fTabWidth write fTabWidth; // djlp 2000-09-19
property Color: TColor read fDefaultBG write fDefaultBG; // djlp 2000-09-19
end;
implementation
{ TSynEditPrint }
constructor TSynEditPrint.Create(AOwner: TComponent);
begin
inherited;
FCopies := 1;
FFooter := TFooter.Create;
FHeader := THeader.Create;
FLines := TStringList.Create;
FMargins := TSynEditPrintMargins.Create;
FPrinterInfo := TSynEditPrinterInfo.Create;
FFont := TFont.Create;
FOldFont := TFont.Create;
FETODist := AllocMem(1);
MaxLeftChar := 1024;
FWrap := True;
FHighlight := True;
FColors := False;
FLineNumbers := False;
FLineOffset := 0;
FPageOffset := 0;
FLineNumbersInMargin := False;
FPages := TList.Create;
FTabWidth := 8;
FDefaultBG := clWhite; // djlp 2000-09-19
end;
destructor TSynEditPrint.Destroy;
var
i: Integer;
begin
FFooter.Free;
FHeader.Free;
FLines.Free;
FMargins.Free;
FPrinterInfo.Free;
FFont.Free;
FOldFont.Free;
for i := 0 to FPages.Count - 1 do
TPageLine(FPages[i]).Free;
FPages.Free;
FreeMem(FETODist);
inherited;
end;
procedure TSynEditPrint.SetLines(const Value: TStrings);
var
i,j: integer;
ConvertTabsProc: TConvertTabsProc;
TmpString: String;
begin
ConvertTabsProc := GetBestConvertTabsProc(FTabWidth);
with FLines do begin
BeginUpdate;
try
Clear;
for i := 0 to Value.Count - 1 do
begin
TmpString := ConvertTabsProc(Value[i], FTabWidth);
j := pos(TSynTabChar, TmpString);
While j > 0 do
begin
TmpString[j] := ' ';
j := pos(TSynTabChar, TmpString);
end;
Add(TmpString);
end;
finally
EndUpdate;
end;
end;
FRangesOK := False;
FPagesCounted := False;
end;
procedure TSynEditPrint.SetFont(const Value: TFont);
begin
FFont.Assign(Value);
FPagesCounted := False;
end;
procedure TSynEditPrint.SetCharWidth(const Value: Integer);
var
i: Integer;
begin
if FCharWidth <> Value then begin
FCharWidth := Value;
// Must have range checking turned off here!
for i := 0 to FMaxLeftChar - 1 do
{$IFOPT R+}{$DEFINE SYN_RESET_RANGE_CHECK}{$R-}{$ENDIF}
FETODist[i] := FCharWidth;
{$IFDEF SYN_RESET_RANGE_CHECK}{$R+}{$UNDEF SYN_RESET_RANGE_CHECK}{$ENDIF}
end;
end;
procedure TSynEditPrint.SetMaxLeftChar(const Value: Integer);
var
i: Integer;
begin
if FMaxLeftChar <> Value then begin
FMaxLeftChar := Value;
ReallocMem(FETODist, FMaxLeftChar * SizeOf(Integer));
for i := 0 to FMaxLeftChar - 1 do
{$IFOPT R+}{$DEFINE SYN_RESET_RANGE_CHECK}{$R-}{$ENDIF}
FETODist[i] := FCharWidth;
{$IFDEF SYN_RESET_RANGE_CHECK}{$R+}{$UNDEF SYN_RESET_RANGE_CHECK}{$ENDIF}
end;
end;
procedure TSynEditPrint.SetHighlighter(const Value: TSynCustomHighlighter);
begin
FHighlighter := Value;
FRangesOK := False;
FPagesCounted := False;
end;
procedure TSynEditPrint.InitPrint;
{ Initialize Font.PixelsPerInch, Character widths, Margins, Total Page count,
headers and footers}
var
TmpSize: Integer;
{$IFNDEF SYN_CLX}
TmpTextMetrics: TTextMetric;
{$ENDIF}
begin
// FDefaultBG := FCanvas.Brush.Color; // djlp 2000-09-20
fFontColor := FFont.Color; // djlp 2000-09-20
FCanvas.Font.Assign(FFont);
if not FPrinting then
begin
SetPixelsPrInch;
TmpSize := FCanvas.Font.Size;
FCanvas.Font.PixelsPerInch := FFont.PixelsPerInch;
FCanvas.Font.Size := TmpSize;
end;
{************}
// Calculate TextMetrics with the (probably) most wider text styles so text is
// never clipped (although potentially wasting space)
FCanvas.Font.Style := [fsBold, fsItalic, fsUnderline, fsStrikeOut];
{$IFDEF SYN_CLX}
CharWidth := FCanvas.TextWidth( 'W' );
FLineHeight := FCanvas.TextHeight( 'Wp¹' );
{$ELSE}
GetTextMetrics(FCanvas.Handle, TmpTextMetrics);
CharWidth := TmpTextMetrics.tmAveCharWidth;
FLineHeight := TmpTextMetrics.tmHeight + TmpTextMetrics.tmExternalLeading;
{$ENDIF}
FCanvas.Font.Style := FFont.Style;
FMargins.InitPage(FCanvas, 1, FPrinterInfo, FLineNumbers, FLineNumbersInMargin,
FLines.Count - 1 + FLineOffset);
CalcPages;
FHeader.InitPrint(FCanvas, FPageCount, FTitle, FMargins);
FFooter.InitPrint(FCanvas, FPageCount, FTitle, FMargins);
FSynOK := Highlight and Assigned(FHighLighter) and (FLines.Count > 0);
end;
procedure TSynEditPrint.SetPixelsPrInch;
var
TmpSize: Integer;
begin
FHeader.SetPixPrInch(FPrinterInfo.YPixPrInch);
FFooter.SetPixPrInch(FPrinterInfo.YPixPrInch);
//This should be necessary - else size would be changed...
TmpSize := FFont.Size;
FFont.PixelsPerInch := FPrinterInfo.YPixPrInch;
FFont.Size := TmpSize;
end;
procedure TSynEditPrint.InitRanges;
//Initialize ranges in Highlighter
var
i: Integer;
begin
if (not FRangesOK) and Assigned(FHighlighter) and (Lines.Count > 0) then begin
FHighlighter.ResetRange;
FLines.Objects[0] := fHighlighter.GetRange;
i := 1;
while (i < Lines.Count) do begin
FHighlighter.SetLine(FLines[i - 1], i - 1);
FHighlighter.NextToEol;
FLines.Objects[i] := FHighlighter.GetRange;
Inc(i);
end;
FRangesOK := True;
end;
end;
procedure TSynEditPrint.CalcPages;
{Calculates the total number of pages.}
var
AStr, Text: string;
StrWidth, i: Integer;
j: Integer;
AList: TList;
YPos: Integer;
PageLine: TPageLine;
procedure CountWrapped;
//Counts the number of lines a line is wrapped to
var
j: Integer;
begin
for j := 0 to AList.Count - 1 do
YPos := YPos + FLineHeight;
end;
var
iStartLine, iEndLine: integer;
iSelStart, iSelLen: integer;
begin
InitRanges;
for i := 0 to FPages.Count - 1 do
TPageLine(FPages[i]).Free;
FPages.Clear;
FMaxWidth := FMargins.PRight - FMargins.PLeft;
AStr := '';
FMaxCol := 0;
while FCanvas.TextWidth(AStr) < FMaxWidth do begin
AStr := AStr + 'W';
FMaxCol := FMaxCol + 1;
end;
FMaxCol := FMaxCol - 1;
{FTestString is used to Calculate MaxWidth when prewiewing and printing -
else the length is not calculated correctly when prewiewing and the
zoom is different from 0.25,0.5,1,2,4 (as for example 1.20) - WHY???}
// fTestString := StringOfChar('W', FMaxCol);
AStr := StringOfChar('W', FMaxCol);
FMaxWidth := FCanvas.TextWidth(AStr);
{************}
FPageCount := 1;
PageLine := TPageLine.Create;
PageLine.FirstLine := 0;
FPages.Add(PageLine);
YPos := FMargins.PTop;
if SelectedOnly then
begin
iStartLine := fBlockBegin.Line -1;
iEndLine := fBlockEnd.Line -1;
end
else begin
iStartLine := 0;
iEndLine := Lines.Count -1;
end;
for i := iStartLine to iEndLine do
begin
if (not fSelectedOnly or (fSelMode = smLine)) then
Text := Lines[i]
else
begin
if (fSelMode = smColumn) or (i = fBlockBegin.Line -1) then
iSelStart := fBlockBegin.Char
else
iSelStart := 1;
if (fSelMode = smColumn) or (i = fBlockEnd.Line -1) then
iSelLen := fBlockEnd.Char - iSelStart
else
iSelLen := MaxInt;
Text := Copy( Lines[i], iSelStart, iSelLen );
end;
{if new page then increase FPageCount and save the top-line number in
FPages}
if (YPos + FLineHeight > FMargins.PBottom) then
begin
YPos := FMargins.PTop;
FPageCount := FPageCount + 1;
PageLine := TPageLine.Create;
PageLine.FirstLine := i;
FPages.Add(PageLine);
end;
StrWidth := FCanvas.TextWidth(Text);
{Check for wrap}
if Wrap and (StrWidth > FMaxWidth) then begin
AList := TList.Create;
if WrapTextEx(Text, [' ', '-', #9, ','], FMaxCol, AList) then
CountWrapped
else begin
{If WrapTextToList didn't succed with the first set of breakchars
then try this one:}
if WrapTextEx(Text, [';', ')', '.'], FMaxCol, AList) then
CountWrapped
else begin
{If WrapTextToList didn't succed at all, then do it the
primitive way}
while Length(Text) > 0 do begin
AStr := Copy(Text, 1, FMaxCol);
Delete(Text, 1, FMaxCol);
if Length(Text) > 0 then
YPos := YPos + FLineHeight;
end;
end;
end;
for j := 0 to AList.Count - 1 do
TWrapPos(AList[j]).Free;
AList.Free;
end;
YPos := YPos + FLineHeight;
end;
FPagesCounted := True;
end;
procedure TSynEditPrint.WriteLineNumber;
{Writes the line number. FMargins. PLeft is the position of the left margin
(which is automatically incremented by the length of the linenumber text, if
the linenumbers should not be placed in the margin)}
var
AStr: string;
begin
SaveCurrentFont;
AStr := IntToStr(FLineNumber + FLineOffset) + ': ';
FCanvas.Brush.Color := FDefaultBG;
FCanvas.Font.Style := [];
FCanvas.Font.Color := clBlack;
FCanvas.TextOut(FMargins.PLeft - FCanvas.TextWidth(AStr), FYPos, AStr);
RestoreCurrentFont;
end;
procedure TSynEditPrint.HandleWrap(Text: string; MaxWidth: Integer);
//Handles wrapping when printing
var
AStr: string;
AList: TList;
j: Integer;
procedure WrapPrimitive;
var
i: Integer;
WrapPos: TWrapPos;
begin
i := 1;
while i <= Length(Text) do begin
AStr := '';
while (Length(AStr) < FMaxCol) and (i <= Length(Text)) do begin
AStr := AStr + Text[i];
i := i + 1;
end;
WrapPos := TWrapPos.Create;
WrapPos.Index := i - 1;
AList.Add(WrapPos);
if (Length(AStr) - i) <= FMaxCol then
Break;
end;
end;
begin
AStr := '';
//First try to break the string at the following chars:
AList := TList.Create;
if WrapTextEx(Text, [' ', '-', #9, ','], FMaxCol, AList) then
TextOut(Text, AList)
else begin
//Then try to break the string at the following chars:
if WrapTextEx(Text, [';', ')', '.'], FMaxCol, AList) then
TextOut(Text, AList)
else begin
WrapPrimitive;
TextOut(Text, AList)
end;
end;
for j := 0 to AList.Count - 1 do
TWrapPos(Alist[j]).Free;
AList.Free;
end;
procedure TSynEditPrint.SaveCurrentFont;
//Used to temporarely save the font of the canvas
begin
FOldFont.Assign(FCanvas.Font);
end;
procedure TSynEditPrint.RestoreCurrentFont;
//Used to restore the font of the canvas
begin
FCanvas.Font.Assign(FOldFont);
end;
procedure TSynEditPrint.TextOut(Text: string; AList: TList);
//Does the actual printing
var
Token: string;
TokenPos: Integer;
Attr: TSynHighlighterAttributes;
AColor: TColor;
TokenStart: Integer;
LCount: Integer;
Handled: Boolean;
aStr : String;
procedure SplitToken;
var
AStr: string;
Last: Integer;
FirstPos: Integer;
TokenEnd: Integer;
begin
Last := TokenPos;
FirstPos := TokenPos;
TokenEnd := TokenPos + Length(Token);
while (LCount < AList.Count) and (TokenEnd > TWrapPos(AList[LCount]).Index) do begin
AStr := Copy(Text, Last + 1, TWrapPos(AList[LCount]).Index - Last); //DDH 10/16/01 added fix from Oliver Grahl
Last := TWrapPos(AList[LCount]).Index; //DDH 10/16/01 added fix from Oliver Grahl
{************}
{$IFDEF SYN_CLX}
FCanvas.TextOut(FMargins.PLeft + FirstPos * FCharWidth, FYPos, AStr);
{$ELSE}
ExtTextOut(FCanvas.Handle, FMargins.PLeft + FirstPos * FCharWidth, FYPos, 0, nil, PChar(AStr), Length(AStr), @FETODist[0]);
{$ENDIF}
FirstPos := 0;
LCount := LCount + 1;
FYPos := FYPos + FLineHeight;
end;
AStr := Copy(Text, Last + 1, TokenEnd - Last); //DDH 10/16/01 added fix from Oliver Grahl
{************}
{$IFDEF SYN_CLX}
FCanvas.TextOut(FMargins.PLeft + FirstPos * FCharWidth, FYPos, AStr);
{$ELSE}
ExtTextOut(FCanvas.Handle, FMargins.PLeft + FirstPos * FCharWidth, FYPos, 0, nil, PChar(AStr), Length(AStr), @FETODist[0]);
{$ENDIF}
//Ready for next token:
TokenStart := TokenPos + Length(Token) - Length(AStr);
end;
begin
if FSynOK then begin
SaveCurrentFont;
FHighlighter.SetRange(FLines.Objects[FLineNumber - 1]);
FHighlighter.SetLine(Text, FLineNumber);
Token := '';
TokenStart := 0;
LCount := 0;
while not FHighLighter.GetEol do begin
Token := FHighLighter.GetToken;
TokenPos := FHighLighter.GetTokenPos;
Attr := FHighLighter.GetTokenAttribute;
if Assigned(Attr) then
begin
FCanvas.Font.Style := Attr.Style;
if FColors then
begin
AColor := Attr.Foreground;
if AColor = clNone then
AColor := FFont.Color;
FCanvas.Font.Color := AColor;
AColor := Attr.Background;
if AColor = clNone then
AColor := FDefaultBG;
FCanvas.Brush.Color := AColor;
end else begin
FCanvas.Font.Color := fFontColor;
FCanvas.Brush.Color := FDefaultBG;
end;
end else begin
// FCanvas.Font.Color := clBlack; // djlp 2000-09-20
FCanvas.Font.Color := fFontColor; // djlp 2000-09-20
FCanvas.Brush.Color := FDefaultBG;
end;
Handled := False;
if (AList <> nil) then begin
if (LCount < AList.Count) then begin
//Split between tokens:
if (TokenPos >= TWrapPos(AList[LCount]).Index) then begin
LCount := LCount + 1;
TokenStart := TokenPos;
FYPos := FYPos + FLineHeight;
end
else begin
//Split in the middle of a token:
if (TokenPos + Length(Token) > TWrapPos(AList[LCount]).Index) then begin
Handled := True;
SplitToken;
end;
end;
end;
end;
if not Handled then
{************}
{$IFNDEF SYN_CLX}
ExtTextOut(FCanvas.Handle, FMargins.PLeft + (TokenPos - TokenStart) * FCharWidth, FYPos, 0, nil, PChar(Token), Length(Token), @FETODist[0]);
{$ELSE}
;
{$ENDIF}
FHighLighter.Next;
end;
RestoreCurrentFont;
end else
// same procedure at lines 408-413
while Length(Text) > 0 do begin
AStr := Copy(Text, 1, FMaxCol);
{$IFDEF SYN_CLX}
FCanvas.TextOut(FMargins.PLeft, FYPos, aStr);
{$ELSE}
ExtTextOut(FCanvas.Handle, FMargins.PLeft, FYPos, 0, nil, PChar(AStr), Length(AStr), @FETODist[0]);
{$ENDIF}
if not Wrap then
break;
Delete(Text, 1, FMaxCol);
if Length(Text) > 0 then
FYPos := FYPos + FLineHeight;
end;
end;
procedure TSynEditPrint.WriteLine(Text: string);
//Prints a line of text
var
StrWidth: integer;
begin
if FLineNumbers then WriteLineNumber;
StrWidth := FCanvas.TextWidth(Text);
{Note that MaxWidth is calculated, using FTestString found in CalcPages -
else the length is not calculated correctly when prewiewing and the
zoom is different from 0.25,0.5,1,2,4 (as for example 1.20) - WHY???
}
if Wrap and (StrWidth > FMaxWidth) then
HandleWrap(Text, FMaxWidth)
else
TextOut(Text, nil);
FYPos := FYPos + FLineHeight;
end;
procedure TSynEditPrint.PrintPage(Num: Integer);
//Prints a page
var
i, iEnd: Integer;
iSelStart, iSelLen: integer;
begin
PrintStatus(psNewPage, Num, FAbort);
if not FAbort then begin
{begin} // djlp 2000-09-20
FCanvas.Brush.Color := Color;
with FMargins do
FCanvas.FillRect(Rect(PLeft, PTop, PRight, PBottom));
{end} // djlp 2000-09-20
FMargins.InitPage(FCanvas, Num, FPrinterInfo, FLineNumbers,
FLineNumbersInMargin, FLines.Count - 1 + FLineOffset);
FHeader.Print(FCanvas, Num + FPageOffset);
if FPages.Count > 0 then begin
FYPos := FMargins.PTop;
if Num = FPageCount then
iEnd := FLines.Count - 1
else
iEnd := TPageLine(FPages[Num]).FirstLine - 1;
for i := TPageLine(FPages[Num - 1]).FirstLine to iEnd do begin
FLineNumber := i + 1;
if (not fSelectedOnly or ((i >= fBlockBegin.Line - 1) and (i <= fBlockEnd.Line - 1))) then begin
if (not fSelectedOnly or (fSelMode = smLine)) then
WriteLine(Lines[i])
else
begin
if (fSelMode = smColumn) or (i = fBlockBegin.Line -1) then
iSelStart := fBlockBegin.Char
else
iSelStart := 1;
if (fSelMode = smColumn) or (i = fBlockEnd.Line -1) then
iSelLen := fBlockEnd.Char - iSelStart
else
iSelLen := MaxInt;
WriteLine( Copy( Lines[i], iSelStart, iSelLen ) );
end;
PrintLine(i + 1, Num);
end;
end;
end;
FFooter.Print(FCanvas, Num + FPageOffset);
end;
end;
procedure TSynEditPrint.UpdatePages(ACanvas: TCanvas);
//Update pages (called explicitly by preview component)
begin
FCanvas := ACanvas;
FPrinterInfo.UpdatePrinter;
InitPrint;
end;
procedure TSynEditPrint.PrintToCanvas(ACanvas: TCanvas; PageNumber: Integer);
//Print to specified canvas. Used by preview component
begin
FAbort := False;
FPrinting := False;
FCanvas := ACanvas;
PrintPage(PageNumber);
end;
procedure TSynEditPrint.Print;
begin
PrintRange(1, -1);
end;
procedure TSynEditPrint.PrintRange(StartPage, EndPage: Integer);
//Prints the pages in the specified range
var
i, ii: Integer;
begin
if fSelectedOnly and not fSelAvail then // jj 2001-07-23
exit;
FPrinting := True;
FAbort := False;
// The next part sets the document title that is used by the printer queue.
if FDocTitle <> '' then //JJV 2000-10-13
Printer.Title := FDocTitle
else
Printer.Title := FTitle;
Printer.BeginDoc;
PrintStatus(psBegin, StartPage, FAbort);
UpdatePages(Printer.Canvas);
for ii:=1 to Copies do begin //EK 10/16/01
i := StartPage;
if EndPage < 0 then
EndPage := FPageCount;
while (i <= EndPage) and (not FAbort) do begin
PrintPage(i);
if ((i < EndPage) or (ii<Copies)) and not(FAbort) then //DDH 2001-10-25
Printer.NewPage;
i := i + 1;
end;
end;
if not(FAbort) then //DDH 2001-10-25
PrintStatus(psEnd, EndPage, FAbort);
Printer.EndDoc;
FPrinting := False;
end;
procedure TSynEditPrint.PrintLine(LineNumber, PageNumber: Integer);
//Fires the OnPrintLine event
begin
if Assigned(FOnPrintLine) then
FOnPrintLine(Self, LineNumber, PageNumber);
end;
procedure TSynEditPrint.PrintStatus(Status: TSynPrintStatus;
PageNumber: integer; var Abort: boolean);
//Fires the OnPrintStatus event
begin
Abort := False;
if Assigned(FOnPrintStatus) then
FOnPrintStatus(Self, Status, PageNumber, Abort);
if Abort then begin
if FPrinting then
Printer.Abort;
end;
end;
function TSynEditPrint.GetPageCount: Integer;
{Returns total page count. If pages hasn't been counted before,
then a UpdatePages is called with a temporary canvas}
var
TmpCanvas: TCanvas;
{$IFNDEF SYN_CLX}
DC: HDC;
{$ENDIF}
begin
Result := 0;
if FPagesCounted then
Result := FPageCount
else begin
TmpCanvas := TCanvas.Create;
try
{************}
{$IFNDEF SYN_CLX}
DC := GetDC(0);
try
if DC <> 0 then
begin
TmpCanvas.Handle := DC;
UpdatePages(TmpCanvas);
TmpCanvas.Handle := 0;
Result := FPageCount;
FPagesCounted := True;
end;
finally
ReleaseDC(0, DC);
end;
{$ENDIF}
finally
TmpCanvas.Free;
end;
end;
end;
procedure TSynEditPrint.SetSynEdit(const Value: TCustomSynEdit);
begin
// Lines := Value.Lines; // sb 2001-11-09
HighLighter := Value.Highlighter;
Font := Value.Font;
FTabWidth := Value.TabWidth;
Lines := Value.Lines; // sb 2001-11-09
fSelAvail := Value.SelAvail; // jj 2001-07-23
fBlockBegin := Value.BlockBegin;
fBlockEnd := Value.BlockEnd;
fSelMode := Value.SelectionMode;
end;
procedure TSynEditPrint.LoadFromStream(AStream: TStream);
var
bufSize : integer;
buffer : PChar;
begin
FHeader.LoadFromStream(AStream);
FFooter.LoadFromStream(AStream);
FMargins.LoadFromStream(AStream);
with AStream do begin
Read(bufSize, SizeOf(bufSize));
GetMem(buffer, bufSize+1);
try
Read(buffer^, bufSize);
buffer[bufSize] := #0;
FTitle := buffer;
finally
FreeMem(buffer);
end;
Read(bufSize, SizeOf(bufSize));
GetMem(buffer, bufSize+1);
try
Read(buffer^, bufSize);
buffer[bufSize] := #0;
FDocTitle := buffer;
finally
FreeMem(buffer);
end;
Read(FWrap, SizeOf(FWrap));
Read(FHighlight, SizeOf(FHighlight));
Read(FColors, SizeOf(FColors));
Read(FLineNumbers, SizeOf(FLineNumbers));
Read(FLineOffset, SizeOf(FLineOffset));
Read(FPageOffset, SizeOf(FPageOffset));
end;
end;
procedure TSynEditPrint.SaveToStream(AStream: TStream);
var
aLen : integer;
begin
FHeader.SaveToStream(AStream);
FFooter.SaveToStream(AStream);
FMargins.SaveToStream(AStream);
with AStream do begin
aLen := Length(FTitle);
Write(aLen, SizeOf(aLen));
Write(PChar(FTitle)^, aLen);
aLen := Length(FDocTitle);
Write(aLen, SizeOf(aLen));
Write(PChar(FDocTitle)^, aLen);
Write(FWrap, SizeOf(FWrap));
Write(FHighlight, SizeOf(FHighlight));
Write(FColors, SizeOf(FColors));
Write(FLineNumbers, SizeOf(FLineNumbers));
Write(FLineOffset, SizeOf(FLineOffset));
Write(FPageOffset, SizeOf(FPageOffset));
end;
end;
procedure TSynEditPrint.SetFooter(const Value: TFooter);
begin
FFooter.Assign( Value );