-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathSynEditPrintMargins.pas
411 lines (361 loc) · 14 KB
/
SynEditPrintMargins.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
{-------------------------------------------------------------------------------
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: SynEditPrintMargins.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: SynEditPrintMargins.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:
-------------------------------------------------------------------------------}
{-------------------------------------------------------------------------------
CONTENTS:
Class handling all sizes involded when printing.
Design-time properties
UnitSystem : The units used to specify sizes in. Internally is allways used mm.
Left : Distance from left edge of paper to text.
Right : Distance from right edge of paper to text.
Top : Distance from top edge of paper to top of text.
Bottom : Distance from bottom edge of paper to bottom of text.
Gutter : Binding gutter - added to right margin (or left if 2-sided)
Header : Distance from top edge of paper to line below header.
Footer : Distance from bottom edge of paper to line above footer.
LeftHFTextIndent : Distance from left margin to first left-aligned character
in header or footer
RightHFTextIndent : Distance from right margin to last right-aligned character
in header or footer
HFInternalMargin : Internal margin between top-line and text in header and
footer AND between bottom-line and text in header and
footer.
MirrorMargins : Set if margins should be mirrored (i.e. when printing
2-sided).
Run-time properties
PLeft : Left position of text in device units (pixels) - this is the left
margin minus the left unprintable distance (+ gutter).
PRight : Right position of text in device units (pixels) - calculated form
left.
PTop : Top position of text in device units (pixels) - this is the top
margin minus the top unprintable distance.
PBottom : Bottom position of text in device units (pixels) - calculated form
top.
PGutter : Binding gutter in device units (pixels)
PHeader : Header in device units (pixels)
PFooter : Footer in device units (pixels) - calculated from top
PLeftHFTextIndent : Left position of text in header and footer in device
units (pixels). Calculated as Left margin + LeftHFTextIndent
PRightHFTextIndent : Right position of text in header and footer in device
units (pixels). Calculated from left
PHFInternalMargin : Internal margin in device units (pixels).
Run-time methods
InitPage : Called by TSynEditPrint class to initialize margins.
Assign : Assign values from another TSynEditPrintMargins object.
-------------------------------------------------------------------------------}
{$IFNDEF QSYNEDITPRINTMARGINS}
unit SynEditPrintMargins;
{$ENDIF}
{$M+}
{$I SynEdit.inc }
interface
uses
{$IFDEF SYN_CLX}
QGraphics,
QSynEditPrintTypes,
QSynEditPrinterInfo,
{$ELSE}
Graphics,
SynEditPrintTypes,
SynEditPrinterInfo,
{$ENDIF}
Classes,
SysUtils;
type
//Margins class - sorting out dimensions of printable area
TSynEditPrintMargins = class(TPersistent)
private
FLeft, FRight, FTop, FBottom: Double;
FHeader, FFooter: Double;
FLeftHFTextIndent: Double;
FRightHFTextIndent: Double;
FHFInternalMargin: Double;
FGutter: Double;
FMirrorMargins: Boolean;
FUnitSystem: TUnitSystem;
function ConvertTo(Value: Double): Double;
function ConvertFrom(Value: Double): Double;
function GetBottom: Double;
function GetFooter: Double;
function GetGutter: Double;
function GetHeader: Double;
function GetLeft: Double;
function GetRight: Double;
function GetTop: Double;
function GetLeftHFTextIndent: Double;
function GetRightHFTextIndent: Double;
function GetHFInternalMargin: Double;
procedure SetBottom(const Value: Double);
procedure SetFooter(const Value: Double);
procedure SetGutter(const Value: Double);
procedure SetHeader(const Value: Double);
procedure SetLeft(const Value: Double);
procedure SetRight(const Value: Double);
procedure SetTop(const Value: Double);
procedure SetLeftHFTextIndent(const Value: Double);
procedure SetRightHFTextIndent(const Value: Double);
procedure SetHFInternalMargin(const Value: Double);
public
//When initpage has been called, the following values will reflect the
//margins in paper units. Note that all values are calculated from
//left or top of paper (i.e. PRight is distance from left margin)
PLeft, PRight, PTop, PBottom: Integer;
PHeader, PFooter: Integer;
PLeftHFTextIndent: Integer;
PRightHFTextIndent: Integer;
PHFInternalMargin: Integer;
PGutter: Integer;
constructor Create;
procedure InitPage(ACanvas: TCanvas; PageNum: Integer;
PrinterInfo: TSynEditPrinterInfo; LineNumbers,
LineNumbersInMargin: Boolean; MaxLineNum: Integer);
procedure Assign(Source: TPersistent); override;
procedure LoadFromStream(AStream: TStream);
procedure SaveToStream(AStream: TStream);
published
property UnitSystem: TUnitSystem read FUnitSystem write FUnitSystem
default usMM;
property Left: Double read GetLeft write SetLeft;
property Right: Double read GetRight write SetRight;
property Top: Double read GetTop write SetTop;
property Bottom: Double read GetBottom write SetBottom;
property Header: Double read GetHeader write SetHeader;
property Footer: Double read GetFooter write SetFooter;
property LeftHFTextIndent: Double read GetLeftHFTextIndent
write SetLeftHFTextIndent;
property RightHFTextIndent: Double read GetRightHFTextIndent
write SetRightHFTextIndent;
property HFInternalMargin: Double read GetHFInternalMargin
write SetHFInternalMargin;
property Gutter: Double read GetGutter write SetGutter;
property MirrorMargins: Boolean read FMirrorMargins write FMirrorMargins;
end;
implementation
{ TSynEditPrintMargins }
const
mmPrInch = 25.4;
mmPrCm = 10;
constructor TSynEditPrintMargins.Create;
begin
inherited;
FUnitSystem := usMM;
FLeft := DefLeft;
FRight := DefRight;
FTop := DefTop;
FBottom := DefBottom;
FHeader := DefHeader;
FFooter := DefFooter;
FLeftHFTextIndent := DefLeftHFTextIndent;
FRightHFTextIndent := DefRightHFTextIndent;
FHFInternalMargin := DefHFInternalMargin;
FGutter := DefGutter;
FMirrorMargins := False;
end;
function TSynEditPrintMargins.ConvertTo(Value: Double): Double;
{Convert Value to mm}
begin
case FUnitSystem of
usCM: Result := Value * mmPrCm;
usInch: Result := Value * mmPrInch;
muThousandthsOfInches: Result := mmPrInch * Value / 1000;
else
Result := Value;
end;
end;
function TSynEditPrintMargins.ConvertFrom(Value: Double): Double;
{Convert from mm to selected UnitSystem}
begin
case FUnitSystem of
usCM: Result := Value / mmPrCm;
usInch: Result := Value / mmPrInch;
muThousandthsOfInches: Result := 1000 * Value / mmPrInch;
else
Result := Value;
end;
end;
function TSynEditPrintMargins.GetBottom: Double;
begin
Result := ConvertFrom(FBottom);
end;
function TSynEditPrintMargins.GetFooter: Double;
begin
Result := ConvertFrom(FFooter);
end;
function TSynEditPrintMargins.GetGutter: Double;
begin
Result := ConvertFrom(FGutter);
end;
function TSynEditPrintMargins.GetHeader: Double;
begin
Result := ConvertFrom(FHeader);
end;
function TSynEditPrintMargins.GetLeft: Double;
begin
Result := ConvertFrom(FLeft);
end;
function TSynEditPrintMargins.GetRight: Double;
begin
Result := ConvertFrom(FRight);
end;
function TSynEditPrintMargins.GetTop: Double;
begin
Result := ConvertFrom(FTop);
end;
function TSynEditPrintMargins.GetLeftHFTextIndent: Double;
begin
Result := ConvertFrom(FLeftHFTextIndent);
end;
function TSynEditPrintMargins.GetRightHFTextIndent: Double;
begin
Result := ConvertFrom(FRightHFTextIndent);
end;
function TSynEditPrintMargins.GetHFInternalMargin: Double;
begin
Result := ConvertFrom(FHFInternalMargin);
end;
procedure TSynEditPrintMargins.SetBottom(const Value: Double);
begin
FBottom := ConvertTo(Value);
end;
procedure TSynEditPrintMargins.SetFooter(const Value: Double);
begin
FFooter := ConvertTo(Value);
end;
procedure TSynEditPrintMargins.SetGutter(const Value: Double);
begin
FGutter := ConvertTo(Value);
end;
procedure TSynEditPrintMargins.SetHeader(const Value: Double);
begin
FHeader := ConvertTo(Value);
end;
procedure TSynEditPrintMargins.SetLeft(const Value: Double);
begin
FLeft := ConvertTo(Value);
end;
procedure TSynEditPrintMargins.SetRight(const Value: Double);
begin
FRight := ConvertTo(Value);
end;
procedure TSynEditPrintMargins.SetTop(const Value: Double);
begin
FTop := ConvertTo(Value);
end;
procedure TSynEditPrintMargins.SetLeftHFTextIndent(const Value: Double);
begin
FLeftHFTextIndent := ConvertTo(Value);
end;
procedure TSynEditPrintMargins.SetRightHFTextIndent(const Value: Double);
begin
FRightHFTextIndent := ConvertTo(Value);
end;
procedure TSynEditPrintMargins.SetHFInternalMargin(const Value: Double);
begin
FHFInternalMargin := ConvertTo(Value);
end;
procedure TSynEditPrintMargins.InitPage(ACanvas: TCanvas; PageNum: Integer;
PrinterInfo: TSynEditPrinterInfo; LineNumbers, LineNumbersInMargin: Boolean;
MaxLineNum: Integer);
//Calculate the P... values
begin
if FMirrorMargins and ((PageNum mod 2) = 0) then begin
PLeft := PrinterInfo.PixFromLeft(FRight);
PRight := PrinterInfo.PrintableWidth - PrinterInfo.PixFromRight(FLeft + FGutter);
end
else begin
PLeft := PrinterInfo.PixFromLeft(FLeft + FGutter);
PRight := PrinterInfo.PrintableWidth - PrinterInfo.PixFromRight(FRight);
end;
if LineNumbers and (not LineNumbersInMargin) then
PLeft := PLeft + ACanvas.TextWidth(IntToStr(MaxLineNum) + ': ');
PTop := PrinterInfo.PixFromTop(FTop);
PBottom := PrinterInfo.PrintableHeight - PrinterInfo.PixFromBottom(FBottom);
PHeader := PrinterInfo.PixFromTop(FHeader);
PFooter := PrinterInfo.PrintableHeight - PrinterInfo.PixFromBottom(FFooter);
PHFInternalMargin := Round(PrinterInfo.YPixPrmm * FHFInternalMargin);
PGutter := Round(PrinterInfo.XPixPrmm * FGutter);
PRightHFTextIndent := PRight - Round(PrinterInfo.XPixPrmm * FRightHFTextIndent);
PLeftHFTextIndent := PLeft + Round(PrinterInfo.XPixPrmm * FLeftHFTextIndent);
end;
procedure TSynEditPrintMargins.Assign(Source: TPersistent);
var
Src: TSynEditPrintMargins;
begin
if (Source <> nil) and (Source is TSynEditPrintMargins) then begin
Src := TSynEditPrintMargins(Source);
FLeft := Src.FLeft;
FRight := Src.FRight;
FTop := Src.FTop;
FBottom := Src.FBottom;
FHeader := Src.FHeader;
FFooter := Src.FFooter;
FLeftHFTextIndent := Src.FLeftHFTextIndent;
FRightHFTextIndent := Src.FRightHFTextIndent;
FHFInternalMargin := Src.FHFInternalMargin;
FGutter := Src.FGutter;
FMirrorMargins := Src.FMirrorMargins;
FUnitSystem := Src.FUnitSystem;
end else
inherited;
end;
procedure TSynEditPrintMargins.LoadFromStream(AStream: TStream);
begin
// we read all our values in MM
with AStream do begin
Read(FUnitSystem, SizeOf(FUnitSystem));
Read(FLeft, SizeOf(FLeft));
Read(FRight, SizeOf(FRight));
Read(FTop, SizeOf(FTop));
Read(FBottom, SizeOf(FBottom));
Read(FHeader, SizeOf(FHeader));
Read(FFooter, SizeOf(FFooter));
Read(FLeftHFTextIndent, SizeOf(FLeftHFTextIndent));
Read(FRightHFTextIndent, SizeOf(FRightHFTextIndent));
Read(FHFInternalMargin, SizeOf(FHFInternalMargin));
Read(FGutter, SizeOf(FGutter));
Read(FMirrorMargins, SizeOf(FMirrorMargins));
end;
end;
procedure TSynEditPrintMargins.SaveToStream(AStream: TStream);
begin
// we always write our values in MM
with AStream do begin
Write(FUnitSystem, SizeOf(FUnitSystem));
Write(FLeft, SizeOf(FLeft));
Write(FRight, SizeOf(FRight));
Write(FTop, SizeOf(FTop));
Write(FBottom, SizeOf(FBottom));
Write(FHeader, SizeOf(FHeader));
Write(FFooter, SizeOf(FFooter));
Write(FLeftHFTextIndent, SizeOf(FLeftHFTextIndent));
Write(FRightHFTextIndent, SizeOf(FRightHFTextIndent));
Write(FHFInternalMargin, SizeOf(FHFInternalMargin));
Write(FGutter, SizeOf(FGutter));
Write(FMirrorMargins, SizeOf(FMirrorMargins));
end;
end;
end.