/
libjpfpdf.pas
2307 lines (2156 loc) · 66.7 KB
/
libjpfpdf.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
{
Free JPDF Pascal
Based on the library FPDF written in PHP by Olivier PLATHEY and
the Code25 method was based on PHP script created by Matthias Lau
Author: Jean Patrick - jpsoft-sac-pa@hotmail.com - www.jeansistemas.net
Contribution: Gilson Nunes - Use of enumerators and resolved bug related to decimal point.
Date: 08/06/2012
Version: 1.33 Stable
License: You can freely use and modify this library for commercial purposes or not,
provided you keep the credits to the author and his contributors.
}
unit libjpfpdf;
{$mode objfpc}{$H+}
interface
uses
{$IFDEF UNIX}{$IFDEF UseCThreads}
cthreads,
{$ENDIF}{$ENDIF}
Classes, SysUtils, zstream, FPimage, FPReadPNG, FPReadBMP, FPReadGif,
FPWriteJPEG, FPReadJPEG, ssl_openssl, httpsend;
type
TJPImageInfo = record
filePath: string;
imgSource: TMemoryStream;
sizebits: integer;
n: integer;
w: double;
h: double;
cs: string;
bpc: integer;
f: string;
parms: string;
pal: string;
trns: string;
end;
TJPFont = record
Name: string;
number: integer;
end;
TCW = array [0..255] of integer;
TJPColor = (cBlack, cSilver, cGray, cWhite, cMaroon, cRed, cPurple, cFuchsia,
cGreen, cLime, cOlive, cYellow, cNavy, cBlue, cTeal, cAqua, cLightGrey);
TPDFOrientation = (poPortrait, poLandscape, poDefault);
TPDFUnit = (puPT, puMM, puCM, puIN, puPX);
TPDFPageFormat = (pfA3, pfA4, pfA5, pfLetter, pfLegal);
TPDFFontFamily = (ffCourier, ffHelvetica, ffTimes, ffSymbol, ffZapfdingbats);
TPDFFontStyle = (fsNormal, fsBold, fsItalic, fsBoldItalic);
TPDFDisplayMode = (dmFullPage, dmFullWidth, dmReal, dmDefault, dmZoom);
TPDFContentStream = (csToViewBrowser, csToDownload);
TPDFBarCode128 = (pdfbcCode128A, pdfbcCode128B, pdfbcCode128C);
{ TJPFpdf }
TJPFpdf = class
private
FProxyHost: string;
FProxyPass: string;
FProxyPort: string;
FProxyUser: string;
function FontWasUsed(font: string): boolean;
function GetImageFromURL(const aURL: string; const aResponse: TStream): Boolean;
function GetInfoImage(imgFile: string): TJPImageInfo;
function GetInfoImage(vImageStream: TStream; vTypeImageExt: String): TJPImageInfo; overload;
function GzCompress(StrIn: string; CompLevel: TCompressionLevel = clMax): string;
function GzDecompress(StrIn: string): string;
function _dounderline(vX, vY: double; vText: string): string;
procedure _begindoc;
procedure _enddoc;
procedure _beginpage(orientation: string);
procedure _endpage;
procedure _newobj;
function _setfont(fFamily: TPDFFontFamily; fStyle: TPDFFontStyle;
fSize: double): boolean;
function _setfontsize(fSize: double): boolean;
protected
function FloatToStr(Value: double): string;
function _escape(sText: string): string;
procedure _out(sText: string);
public
page: integer; // current page number
numObj: integer; // current object number
offsets: array of integer; // array of object offsets
buffer: TMemoryStream; // buffer holding in-memory PDF
pages: array of string; // array containing pages
state: integer; // current document state
compress: boolean; // compression flag
DefOrientation: TPDFOrientation; // default orientation
CurOrientation: TPDFOrientation; // current orientation
OrientationChanges: array of boolean; // array indicating orientation changes
fwPt, fhPt: double; // dimensions of page format in points
fw, fh: double; // dimensions of page format in user unit
wPt, hPt: double; // current dimensions of page in points
dw, dh: double; // current dimensions of page in user unit
lMargin: double; // left margin
tMargin: double; // top margin
rMargin: double; // right margin
bMargin: double; // page break margin
cMargin: double; // cell margin
cpX, cpY: double; // current position in user unit for cell positionning
hLasth: double; // height of last cell printed
pgK: double; // scale factor (number of points in user unit)
pLineWidth: double; // line width in user unit
pUTF8: boolean; // Set Utf8ToAnsi to suport unicode
pFonts: array of TJPFont; // array of used fonts
pImages: array of TJPImageInfo; // array of used images
cFontFamily: TPDFFontFamily; // current font family
cFontStyle: TPDFFontStyle; // current font style
cFontSizePt: double; // current font size in points
cFontSize: double; // current font size in user unit
pUnderlineFlag: boolean; // underlining flag
pDrawColor: string; // commands for drawing color
pFillColor: string; // commands for filling color
pTextColor: string; // commands for text color
pColorFlag: boolean; // indicates whether fill and text colors are different
pgWs: double; // word spacing
AutoPageBreak: boolean; // automatic page breaking
PageBreakTrigger: double; // threshold used to trigger page breaks
InFooter: boolean; // flag set when processing footer
DocDisplayMode: string; // display mode
DocTitle: string; // title
DocSubject: string; // subject
DocAuthor: string; // author
DocKeywords: string; // keywords
DocCreator: string; // creator
DocAliasNbPages: string; // alias for total number of pages
Jpdf_charwidths: array[TPDFFontFamily] of array[TPDFFontStyle] of
array [0..255] of integer; // widths of the characters of fonts
constructor Create;
constructor Create(orientation: TPDFOrientation; pageUnit: TPDFUnit;
pageFormat: TPDFPageFormat); overload;
constructor Create(orientation: TPDFOrientation; pageUnit: TPDFUnit;
pageSizeW: Double; pageSizeH: Double = 0); overload;
destructor Destroy; override;
procedure SetMargins(marginLeft: double; marginTop: double;
marginRight: double = -1);
procedure SetUTF8(mode: Boolean = True);
procedure SetLeftMargin(marginLeft: double);
procedure SetRightMargin(marginRight: double);
procedure SetAutoPageBreak(vAuto: boolean; vMargin: double = 0.0);
procedure SetDisplayMode(mode: TPDFDisplayMode; zoom: smallint = 100);
procedure SetCompression(scompress: boolean);
procedure SetTitle(vTitle: string);
procedure SetSubject(ssubject: string);
procedure SetAuthor(vAuthor: string);
procedure SetKeywords(vKeywords: string);
procedure SetCreator(vCreator: string);
procedure AliasNbPages(vAlias: string = '{nb}');
procedure Error(TextMsg: string);
procedure Open;
procedure Close;
procedure AddPage(Orientation: TPDFOrientation = poDefault);
function PageNo: integer;
procedure SetDrawColor(ValR: integer; ValG: integer = -1; ValB: integer = -1);
procedure SetFillColor(ValR: integer; ValG: integer = -1; ValB: integer = -1);
procedure SetTextColor(ValR: integer; ValG: integer = -1; ValB: integer = -1);
procedure SetTextColor(color: TJPColor);
procedure SetFillColor(color: TJPColor);
procedure SetDrawColor(color: TJPColor);
function GetStringWidth(vText: string): double;
procedure SetLineWidth(vWidth: double);
procedure Line(vX1, vY1, vX2, vY2: double);
procedure Rect(vX, vY, vWidht, vHeight: double; vStyle: string = '');
procedure SetFont(fFamily: TPDFFontFamily; fStyle: TPDFFontStyle;
fSize: double = 0.0; fUnderline: boolean = False);
procedure SetFont(fFamily: TPDFFontFamily; fSize: double = 0.0;
fUnderline: boolean = False);
procedure SetFontSize(fSize: double; fUnderline: boolean = False);
procedure SetUnderline(fUnderline: boolean = False);
procedure Text(vX, vY: double; vText: string);
procedure Writer(vHeight: double; vText: string);
function AcceptPageBreak: boolean;
procedure Cell(vWidth: double; vHeight: double = 0.0; vText: string = '';
vBorder: string = '0'; vLineBreak: integer = 0; vAlign: string = '';
vFill: integer = 0);
procedure MultiCell(vWidth, vHeight: double; vText: string;
vBorder: string = '0'; vAlign: string = 'J'; vFill: integer = 0);
procedure Image(vFileOrURL: string; vX: double; vY: double; vWidth: double;
vHeight: double = 0.0);
procedure Image(vImageStream: TStream; vTypeImageExt: String; vX: double;
vY: double; vWidth: double; vHeight: double = 0.0); overload;
procedure Ln(vHeight: double = 0);
function GetX: double;
procedure SetX(vX: double);
function GetY: double;
procedure SetY(vY: double);
procedure SetXY(vX, vY: double);
procedure SaveToFile(vFile: string);
function SaveToStream: TStream;
function SaveToString: string;
function CreateContentStream(cs: TPDFContentStream = csToViewBrowser): TStream;
procedure Code25(vXPos, vYPos: double; vTextCode: string; vBaseWidth:
double = 1.00; vHeight: double = 10.00; vViewNum: Boolean = False;
vZeroWhenInvalidDigit: Boolean = True);
procedure Code128(vXPos, vYPos: Double; vTextCode: string; vWidth: Double;
vHeight: Double);
function CurrentFontCW: TCW;
procedure Header; Virtual;
procedure Footer; Virtual;
property ProxyHost: string read FProxyHost Write FProxyHost;
property ProxyPort: string read FProxyPort Write FProxyPort;
property ProxyUser: string read FProxyUser Write FProxyUser;
property ProxyPass: string read FProxyPass Write FProxyPass;
end;
implementation
uses strutils, math;
{ TJPFpdf }
const
{$i inc_fontes.inc}
TPDFFormatSetings: TFormatSettings = (
CurrencyFormat: 1;
NegCurrFormat: 5;
ThousandSeparator: #0;
DecimalSeparator: '.';
CurrencyDecimals: 2;
DateSeparator: '-';
TimeSeparator: ':';
ListSeparator: ',';
CurrencyString: '$';
ShortDateFormat: 'd/m/y';
LongDateFormat: 'dd" "mmmm" "yyyy';
TimeAMString: 'AM';
TimePMString: 'PM';
ShortTimeFormat: 'hh:nn';
LongTimeFormat: 'hh:nn:ss';
ShortMonthNames: ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
LongMonthNames: ('January', 'February', 'March', 'April', 'May', 'June',
'July', 'August', 'September', 'October', 'November', 'December');
ShortDayNames: ('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat');
LongDayNames: ('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday',
'Friday', 'Saturday');
TwoDigitYearCenturyWindow: 50;
);
JORIENTATION: array[TPDFOrientation] of char = ('P', 'L', #0);
JUNIT: array[TPDFUnit] of double = (1, 72 / 25.4, 72 / 2.54, 72, 0.75);
JFORMAT_W: array[TPDFPageFormat] of double = (841.89, 595.28, 420.94, 612, 612);
JFORMAT_H: array[TPDFPageFormat] of double = (1190.55, 841.89, 595.28, 792, 1008);
JCOLOR_R: array[TJPColor] of smallint =
(0, 192, 128, 255, 128, 255, 128, 255, 0, 0, 128, 255, 0, 0, 0, 0, 220);
JCOLOR_G: array[TJPColor] of smallint =
(0, 192, 128, 255, 0, 0, 0, 0, 128, 255, 128, 255, 0, 0, 128, 255, 220);
JCOLOR_B: array[TJPColor] of smallint =
(0, 192, 128, 255, 0, 0, 128, 255, 0, 0, 0, 0, 128, 255, 128, 255, 220);
JFONTFAMILY: array[TPDFFontFamily] of shortstring =
('Courier', 'Helvetica', 'Times', 'Symbol', 'Zapfdingbats');
JFONTSTYLE: array[TPDFFontStyle] of shortstring =
('', '-Bold', '-Oblique', '-BoldOblique');
JDISPLAYMODE: array[TPDFDisplayMode] of shortstring =
('fullpage', 'fullwidth', 'real', 'default', 'zoom');
FREE_JPDF_PASCAL_VERSION = '1.0 Stable';
JBC128Start: array[TPDFBarCode128] of smallint = (103,104,105); // Set selection characters at the start of C128
JBC128Swap: array[TPDFBarCode128] of smallint = (101,100,99); // Set change characters
JBC128: array [0..107,0..5] of Integer = // Code table 128
(
(2, 1, 2, 2, 2, 2), //0 : [ ]
(2, 2, 2, 1, 2, 2), //1 : [!]
(2, 2, 2, 2, 2, 1), //2 : ["]
(1, 2, 1, 2, 2, 3), //3 : [#]
(1, 2, 1, 3, 2, 2), //4 : [$]
(1, 3, 1, 2, 2, 2), //5 : [%]
(1, 2, 2, 2, 1, 3), //6 : [&]
(1, 2, 2, 3, 1, 2), //7 : [']
(1, 3, 2, 2, 1, 2), //8 : [(]
(2, 2, 1, 2, 1, 3), //9 : [)]
(2, 2, 1, 3, 1, 2), //10 : [*]
(2, 3, 1, 2, 1, 2), //11 : [+]
(1, 1, 2, 2, 3, 2), //12 : [,]
(1, 2, 2, 1, 3, 2), //13 : [-]
(1, 2, 2, 2, 3, 1), //14 : [.]
(1, 1, 3, 2, 2, 2), //15 : [/]
(1, 2, 3, 1, 2, 2), //16 : [0]
(1, 2, 3, 2, 2, 1), //17 : [1]
(2, 2, 3, 2, 1, 1), //18 : [2]
(2, 2, 1, 1, 3, 2), //19 : [3]
(2, 2, 1, 2, 3, 1), //20 : [4]
(2, 1, 3, 2, 1, 2), //21 : [5]
(2, 2, 3, 1, 1, 2), //22 : [6]
(3, 1, 2, 1, 3, 1), //23 : [7]
(3, 1, 1, 2, 2, 2), //24 : [8]
(3, 2, 1, 1, 2, 2), //25 : [9]
(3, 2, 1, 2, 2, 1), //26 : [:]
(3, 1, 2, 2, 1, 2), //27 : [,]
(3, 2, 2, 1, 1, 2), //28 : [<]
(3, 2, 2, 2, 1, 1), //29 : [=]
(2, 1, 2, 1, 2, 3), //30 : [>]
(2, 1, 2, 3, 2, 1), //31 : [?]
(2, 3, 2, 1, 2, 1), //32 : [@]
(1, 1, 1, 3, 2, 3), //33 : [A]
(1, 3, 1, 1, 2, 3), //34 : [B]
(1, 3, 1, 3, 2, 1), //35 : [C]
(1, 1, 2, 3, 1, 3), //36 : [D]
(1, 3, 2, 1, 1, 3), //37 : [E]
(1, 3, 2, 3, 1, 1), //38 : [F]
(2, 1, 1, 3, 1, 3), //39 : [G]
(2, 3, 1, 1, 1, 3), //40 : [H]
(2, 3, 1, 3, 1, 1), //41 : [I]
(1, 1, 2, 1, 3, 3), //42 : [J]
(1, 1, 2, 3, 3, 1), //43 : [K]
(1, 3, 2, 1, 3, 1), //44 : [L]
(1, 1, 3, 1, 2, 3), //45 : [M]
(1, 1, 3, 3, 2, 1), //46 : [N]
(1, 3, 3, 1, 2, 1), //47 : [O]
(3, 1, 3, 1, 2, 1), //48 : [P]
(2, 1, 1, 3, 3, 1), //49 : [Q]
(2, 3, 1, 1, 3, 1), //50 : [R]
(2, 1, 3, 1, 1, 3), //51 : [S]
(2, 1, 3, 3, 1, 1), //52 : [T]
(2, 1, 3, 1, 3, 1), //53 : [U]
(3, 1, 1, 1, 2, 3), //54 : [V]
(3, 1, 1, 3, 2, 1), //55 : [W]
(3, 3, 1, 1, 2, 1), //56 : [X]
(3, 1, 2, 1, 1, 3), //57 : [Y]
(3, 1, 2, 3, 1, 1), //58 : [Z]
(3, 3, 2, 1, 1, 1), //59 : [[]
(3, 1, 4, 1, 1, 1), //60 : [\]
(2, 2, 1, 4, 1, 1), //61 : []]
(4, 3, 1, 1, 1, 1), //62 : [^]
(1, 1, 1, 2, 2, 4), //63 : [_]
(1, 1, 1, 4, 2, 2), //64 : [`]
(1, 2, 1, 1, 2, 4), //65 : [a]
(1, 2, 1, 4, 2, 1), //66 : [b]
(1, 4, 1, 1, 2, 2), //67 : [c]
(1, 4, 1, 2, 2, 1), //68 : [d]
(1, 1, 2, 2, 1, 4), //69 : [e]
(1, 1, 2, 4, 1, 2), //70 : [f]
(1, 2, 2, 1, 1, 4), //71 : [g]
(1, 2, 2, 4, 1, 1), //72 : [h]
(1, 4, 2, 1, 1, 2), //73 : [i]
(1, 4, 2, 2, 1, 1), //74 : [j]
(2, 4, 1, 2, 1, 1), //75 : [k]
(2, 2, 1, 1, 1, 4), //76 : [l]
(4, 1, 3, 1, 1, 1), //77 : [m]
(2, 4, 1, 1, 1, 2), //78 : [n]
(1, 3, 4, 1, 1, 1), //79 : [o]
(1, 1, 1, 2, 4, 2), //80 : [p]
(1, 2, 1, 1, 4, 2), //81 : [q]
(1, 2, 1, 2, 4, 1), //82 : [r]
(1, 1, 4, 2, 1, 2), //83 : [s]
(1, 2, 4, 1, 1, 2), //84 : [t]
(1, 2, 4, 2, 1, 1), //85 : [u]
(4, 1, 1, 2, 1, 2), //86 : [v]
(4, 2, 1, 1, 1, 2), //87 : [w]
(4, 2, 1, 2, 1, 1), //88 : [x]
(2, 1, 2, 1, 4, 1), //89 : [y]
(2, 1, 4, 1, 2, 1), //90 : [z]
(4, 1, 2, 1, 2, 1), //91 : [{]
(1, 1, 1, 1, 4, 3), //92 : [|]
(1, 1, 1, 3, 4, 1), //93 : [}]
(1, 3, 1, 1, 4, 1), //94 : [~]
(1, 1, 4, 1, 1, 3), //95 : [DEL]
(1, 1, 4, 3, 1, 1), //96 : [FNC3]
(4, 1, 1, 1, 1, 3), //97 : [FNC2]
(4, 1, 1, 3, 1, 1), //98 : [SHIFT]
(1, 1, 3, 1, 4, 1), //99 : [Cswap]
(1, 1, 4, 1, 3, 1), //100 : [Bswap]
(3, 1, 1, 1, 4, 1), //101 : [Aswap]
(4, 1, 1, 1, 3, 1), //102 : [FNC1]
(2, 1, 1, 4, 1, 2), //103 : [Astart]
(2, 1, 1, 2, 1, 4), //104 : [Bstart]
(2, 1, 1, 2, 3, 2), //105 : [Cstart]
(2, 3, 3, 1, 1, 1), //106 : [STOP]
(2, 1, 0, 0, 0, 0) //107 : [END BAR]
);
constructor TJPFpdf.Create(orientation: TPDFOrientation; pageUnit: TPDFUnit;
pageFormat: TPDFPageFormat);
begin
Create(orientation, pageUnit, JFORMAT_W[pageFormat], JFORMAT_H[pageFormat]);
end;
destructor TJPFpdf.Destroy;
begin
Self.buffer.Free;
inherited Destroy;
end;
procedure TJPFpdf.SetMargins(marginLeft: double; marginTop: double; marginRight: double);
begin
//Set left and top margins
Self.lMargin := marginLeft;
Self.tMargin := marginTop;
if (marginRight = -1) then
Self.rMargin := Self.lMargin
else
Self.rMargin := marginRight;
end;
procedure TJPFpdf.SetUTF8(mode: Boolean);
begin
pUTF8 := mode;
end;
procedure TJPFpdf.SetLeftMargin(marginLeft: double);
begin
//Set left margin
Self.lMargin := marginLeft;
if ((Self.page > 0) and (Self.cpX < marginLeft)) then
Self.cpX := marginLeft;
end;
procedure TJPFpdf.SetRightMargin(marginRight: double);
begin
//Set right margin
Self.rMargin := marginRight;
end;
procedure TJPFpdf.SetAutoPageBreak(vAuto: boolean; vMargin: double);
begin
//Set auto page break mode and triggering margin
Self.AutoPageBreak := vAuto;
Self.bMargin := vMargin;
Self.PageBreakTrigger := Self.dh - vMargin;
end;
procedure TJPFpdf.SetDisplayMode(mode: TPDFDisplayMode; zoom: smallint);
begin
//Set display mode in viewer
if (mode = dmZoom) then
Self.DocDisplayMode := IntToStr(zoom)
else
Self.DocDisplayMode := JDISPLAYMODE[mode];
end;
procedure TJPFpdf.SetCompression(scompress: boolean);
begin
//Set page compression
Self.compress := scompress;
end;
procedure TJPFpdf.SetTitle(vTitle: string);
begin
//Title of document
Self.DocTitle := vTitle;
end;
procedure TJPFpdf.SetSubject(ssubject: string);
begin
//Subject of document
Self.DocSubject := ssubject;
end;
procedure TJPFpdf.SetAuthor(vAuthor: string);
begin
//Author of document
Self.DocAuthor := vAuthor;
end;
procedure TJPFpdf.SetKeywords(vKeywords: string);
begin
//Keywords of document
Self.DocKeywords := vKeywords;
end;
procedure TJPFpdf.SetCreator(vCreator: string);
begin
//Creator of document
Self.DocCreator := vCreator;
end;
procedure TJPFpdf.AliasNbPages(vAlias: string);
begin
//Define an alias for total number of pages
Self.DocAliasNbPages := vAlias;
end;
procedure TJPFpdf.Error(TextMsg: string);
begin
//Fatal error
raise Exception.Create('JPFPDF error: ' + TextMsg);
end;
procedure TJPFpdf.Open;
begin
Header;
//Begin document
_begindoc;
end;
procedure TJPFpdf.Close;
begin
//Terminate document
if (Self.page = 0) then
Error('Document contains no page');
//Page footer
Self.InFooter := True;
Footer;
Self.InFooter := False;
//Close page
_endpage;
//Close document
_enddoc;
end;
procedure TJPFpdf.AddPage(Orientation: TPDFOrientation);
var
vdc, vfc, vtc: string;
vfamily: TPDFFontFamily;
vstyle: TPDFFontStyle;
vsize: double;
vlw: double;
vcf: boolean;
begin
if ((Self.page = 1) and (Self.fhPt = 0)) then
Exit;
//Start a new page
if (Self.state = 0) then
Self.Open();
vfamily := Self.cFontFamily;
vstyle := Self.cFontStyle;
vsize := Self.cFontSizePt;
vlw := Self.pLineWidth;
vdc := Self.pDrawColor;
vfc := Self.pFillColor;
vtc := Self.pTextColor;
vcf := Self.pColorFlag;
if (Self.page > 0) then
begin
//Page footer
Self.InFooter := True;
Footer;
Self.InFooter := False;
//Close page
_endpage;
end;
//Start new page
_beginpage(JORIENTATION[orientation]);
//Set line cap style to square
_out('2 J');
//Set line width
_out(FloatToStr(vlw) + ' w');
//Set font
SetFont(vfamily, vstyle, vsize);
//Set colors
if (vdc <> '0 G') then
_out(vdc);
if (vfc <> '0 g') then
_out(vfc);
Self.pTextColor := vtc;
Self.pColorFlag := vcf;
//Page header
Header;
//Restore line width
if (Self.pLineWidth <> vlw) then
begin
Self.pLineWidth := vlw;
_out(FloatToStr(vlw) + ' w');
end;
//Restore font
SetFont(vfamily, vstyle, vsize);
//Restore colors
if (Self.pDrawColor <> vdc) then
begin
Self.pDrawColor := vdc;
_out(vdc);
end;
if (Self.pFillColor <> vfc) then
begin
Self.pFillColor := vfc;
_out(vfc);
end;
Self.pTextColor := vtc;
Self.pColorFlag := vcf;
end;
function TJPFpdf.PageNo: integer;
begin
//Get current page number
Result := Self.page;
end;
procedure TJPFpdf.SetDrawColor(ValR: integer; ValG: integer; ValB: integer);
begin
//Set color for all stroking operations
if (((ValR = 0) and (ValG = 0) and (ValB = 0)) or (ValG = -1)) then
Self.pDrawColor := Copy(FloatToStr(ValR / 255), 0, 5) + ' G'
else
Self.pDrawColor := Copy(FloatToStr(ValR / 255), 0, 5) + ' ' +
Copy(FloatToStr(ValG / 255), 0, 5) + ' ' +
Copy(FloatToStr(ValB / 255), 0, 5) + ' RG';
if (Self.page > 0) then
_out(Self.pDrawColor);
end;
procedure TJPFpdf.SetFillColor(ValR: integer; ValG: integer; ValB: integer);
begin
//Set color for all filling operations
if (((ValR = 0) and (ValG = 0) and (ValB = 0)) or (ValG = -1)) then
Self.pFillColor := Copy(FloatToStr(ValR / 255), 0, 5) + ' g'
else
Self.pFillColor := Copy(FloatToStr(ValR / 255), 0, 5) + ' ' +
Copy(FloatToStr(ValG / 255), 0, 5) + ' ' +
Copy(FloatToStr(ValB / 255), 0, 5) + ' rg';
Self.pColorFlag := (Self.pFillColor <> Self.pTextColor);
if (Self.page > 0) then
_out(Self.pFillColor);
end;
procedure TJPFpdf.SetTextColor(ValR: integer; ValG: integer; ValB: integer);
begin
//Set color for text
if (((ValR = 0) and (ValG = 0) and (ValB = 0)) or (ValG = -1)) then
Self.pTextColor := Copy(FloatToStr(ValR / 255), 0, 5) + ' g'
else
Self.pTextColor := Copy(FloatToStr(ValR / 255), 0, 5) + ' ' +
Copy(FloatToStr(ValG / 255), 0, 5) + ' ' +
Copy(FloatToStr(ValB / 255), 0, 5) + ' rg';
Self.pColorFlag := (Self.pFillColor <> Self.pTextColor);
end;
procedure TJPFpdf.SetTextColor(color: TJPColor);
begin
SetTextColor(JCOLOR_R[color], JCOLOR_G[color], JCOLOR_B[color]);
end;
procedure TJPFpdf.SetFillColor(color: TJPColor);
begin
SetFillColor(JCOLOR_R[color], JCOLOR_G[color], JCOLOR_B[color]);
end;
procedure TJPFpdf.SetDrawColor(color: TJPColor);
begin
SetDrawColor(JCOLOR_R[color], JCOLOR_G[color], JCOLOR_B[color]);
end;
function TJPFpdf.GetStringWidth(vText: string): double;
var
vfamily: TPDFFontFamily;
vstyle: TPDFFontStyle;
vl, vi: integer;
vw: double;
begin
vfamily := Self.cFontFamily;
vstyle := Self.cFontStyle;
if (vfamily in [ffCourier, ffSymbol, ffZapfdingbats]) then
vstyle := fsNormal;
vw := 0;
vl := Length(vText);
for vi := 1 to vl do
vw += Self.Jpdf_charwidths[vfamily][vstyle][Ord(vText[vi])];
Result := vw * Self.cFontSize / 1000;
end;
procedure TJPFpdf.SetLineWidth(vWidth: double);
begin
//Set line width
Self.pLineWidth := vWidth;
if (Self.page > 0) then
_out(FloatToStr(vWidth) + ' w');
end;
procedure TJPFpdf.Line(vX1, vY1, vX2, vY2: double);
begin
//Draw a line
_out(FloatToStr(vX1) + ' -' + FloatToStr(vY1) + ' m ' + FloatToStr(vX2) +
' -' + FloatToStr(vY2) + ' l S');
end;
procedure TJPFpdf.Rect(vX, vY, vWidht, vHeight: double; vStyle: string);
var
vop: string;
begin
//Draw a rectangle
vStyle := UpperCase(vStyle);
if (vStyle = 'F') then
vop := 'f'
else if ((vStyle = 'FD') or (vStyle = 'DF')) then
vop := 'B'
else
vop := 'S';
_out(FloatToStr(vX) + ' -' + FloatToStr(vY) + ' ' + FloatToStr(vWidht) +
' -' + FloatToStr(vHeight) + ' re ' + vop);
end;
procedure TJPFpdf.SetFont(fFamily: TPDFFontFamily; fStyle: TPDFFontStyle;
fSize: double; fUnderline: boolean);
begin
//Select a font; size given in points
_setfont(fFamily, fStyle, fSize);
Self.pUnderlineFlag := fUnderline;
end;
procedure TJPFpdf.SetFont(fFamily: TPDFFontFamily; fSize: double; fUnderline: boolean);
begin
_setfont(fFamily, fsNormal, fSize);
Self.pUnderlineFlag := fUnderline;
end;
procedure TJPFpdf.SetFontSize(fSize: double; fUnderline: boolean);
begin
//Set font size in points
_setfontsize(fSize);
Self.pUnderlineFlag := fUnderline;
end;
procedure TJPFpdf.SetUnderline(fUnderline: boolean);
begin
Self.pUnderlineFlag := fUnderline;
end;
procedure TJPFpdf.Text(vX, vY: double; vText: string);
var
sss: string;
begin
if (pUTF8) then vText := Utf8ToAnsi(vText);
//Output a string
vText := _escape(vText);
sss := 'BT ' + FloatToStr(vX) + ' -' + FloatToStr(vY) + ' Td (' + vText + ') Tj ET';
if ((Self.pUnderlineFlag) and (vText <> '')) then
sss += ' ' + _dounderline(vX, vY, vText);
if (Self.pColorFlag) then
sss := 'q ' + Self.pTextColor + ' ' + sss + ' Q';
_out(sss);
end;
procedure TJPFpdf.Writer(vHeight: double; vText: string);
var
vfamily: TPDFFontFamily;
vstyle: TPDFFontStyle;
vw: extended;
vwmax: extended;
vs: string;
vnb: integer;
vnl: integer;
vl: integer;
vj: integer;
vi: integer;
vsep: integer;
vc: char;
fUTF8: Boolean;
begin
fUTF8 := False;
if (pUTF8) then begin
vText := Utf8ToAnsi(vText);
SetUTF8(False);
fUTF8 := True;
end;
//Output text in flowing mode
vfamily := Self.cFontFamily;
vstyle := Self.cFontStyle;
if (vfamily in [ffCourier, ffSymbol, ffZapfdingbats]) then
vstyle := fsNormal;
vw := Self.dw - Self.rMargin - Self.cpX;
vwmax := (vw - 2 * Self.cMargin) * 1000 / Self.cFontSize;
vs := StringReplace(vText, #13, '', [rfReplaceAll]); // #13 = "\r"
vnb := Length(vs);
vsep := -1;
vi := 0;
vj := 0;
vl := 0;
vnl := 1;
while (vi < vnb) do
begin
//Get next character
vc := vs[vi + 1];
if (vc = #10) then // #10 = "\n"
begin
//Explicit line break
Cell(vw, vHeight, Copy(vs, vj, vi - vj), '0', 2, '', 0);
vi := vi + 1;
vsep := -1;
vj := vi;
vl := 0;
if (vnl = 1) then
begin
Self.cpX := Self.lMargin;
vw := Self.dw - Self.rMargin - Self.cpX;
vwmax := (vw - 2 * Self.cMargin) * 1000 / Self.cFontSize;
end;
vnl := vnl + 1;
continue;
end;
if (vc = ' ') then
vsep := vi;
vl += Self.Jpdf_charwidths[vfamily][vstyle][Ord(vc)];
if (vl > vwmax) then
begin
//Automatic line break
if (vsep = -1) then
begin
if (Self.cpX > Self.lMargin) then
begin
//Move to next line
Self.cpX := Self.lMargin;
Self.cpY += vHeight;
vw := Self.dw - Self.rMargin - Self.cpX;
vwmax := (vw - 2 * Self.cMargin) * 1000 / Self.cFontSize;
vi := vi + 1;
vnl := vnl + 1;
continue;
end;
if (vi = vj) then
vi := vi + 1;
Cell(vw, vHeight, Copy(vs, vj, vi - vj), '0', 2, '', 0);
end
else
begin
Cell(vw, vHeight, Copy(vs, vj, vsep - vj), '0', 2, '', 0);
vi := vsep + 1;
end;
vsep := -1;
vj := vi;
vl := 0;
if (vnl = 1) then
begin
Self.cpX := Self.lMargin;
vw := Self.dw - Self.rMargin - Self.cpX;
vwmax := (vw - 2 * Self.cMargin) * 1000 / Self.cFontSize;
end;
vnl := vnl + 1;
end
else
vi := vi + 1;
end;
//Last chunk
if (vi <> vj) then
begin
vw := StrToFloat(FloatToStrF(vl / 1000 * Self.cFontSize, ffNumber,
14, 2, TPDFFormatSetings), TPDFFormatSetings);
Cell(vw, vHeight, Copy(vs, vj, vi), '0', 0, '', 0);
end;
if (fUTF8) then SetUTF8(True);
end;
function TJPFpdf.AcceptPageBreak: boolean;
begin
//Accept automatic page break or not
Result := Self.AutoPageBreak;
end;
procedure TJPFpdf.Image(vFileOrURL: string; vX: double; vY: double;
vWidth: double; vHeight: double);
var
i: integer;
img: TJPImageInfo;
flag: boolean;
begin
//Put an image on the page
flag := False;
if (Length(Self.pImages) > 0) then
for i := 0 to Length(Self.pImages) - 1 do
begin
if (Self.pImages[i].filePath = vFileOrURL) then
begin
flag := True;
img := Self.pImages[i];
break;
end;
end;
if not (flag) then
begin
//First use of image, get info
SetLength(Self.pImages, Length(Self.pImages) + 1);
Self.pImages[Length(Self.pImages) - 1] := GetInfoImage(vFileOrURL);
Self.pImages[Length(Self.pImages) - 1].n := Length(Self.pImages);
Self.pImages[Length(Self.pImages) - 1].filePath := vFileOrURL;
img := Self.pImages[Length(Self.pImages) - 1];
end
else
//Automatic width or height calculus
if (vWidth = 0) then
vWidth := StrToFloat(FloatToStrF((vHeight * img.w / img.h), ffNumber,
14, 2, TPDFFormatSetings), TPDFFormatSetings);
if (vHeight = 0) then
vHeight := StrToFloat(FloatToStrF((vWidth * img.h / img.w), ffNumber,
14, 2, TPDFFormatSetings), TPDFFormatSetings);
_out('q ' + FloatToStr(vWidth) + ' 0 0 ' + FloatToStr(vHeight) +
' ' + FloatToStr(vX) + ' -' + FloatToStr(vY + vHeight) + ' cm /I' +
IntToStr(Length(Self.pImages)) + ' Do Q');
end;
procedure TJPFpdf.Image(vImageStream: TStream; vTypeImageExt: String;
vX: double; vY: double; vWidth: double; vHeight: double);
var
img: TJPImageInfo;
begin
SetLength(Self.pImages, Length(Self.pImages) + 1);
Self.pImages[Length(Self.pImages) - 1] := GetInfoImage(vImageStream, vTypeImageExt);
Self.pImages[Length(Self.pImages) - 1].n := Length(Self.pImages);
Self.pImages[Length(Self.pImages) - 1].filePath := '';
img := Self.pImages[Length(Self.pImages) - 1];
//Automatic width or height calculus
if (vWidth = 0) then
vWidth := StrToFloat(FloatToStrF((vHeight * img.w / img.h), ffNumber,
14, 2, TPDFFormatSetings), TPDFFormatSetings);
if (vHeight = 0) then
vHeight := StrToFloat(FloatToStrF((vWidth * img.h / img.w), ffNumber,
14, 2, TPDFFormatSetings), TPDFFormatSetings);
_out('q ' + FloatToStr(vWidth) + ' 0 0 ' + FloatToStr(vHeight) +
' ' + FloatToStr(vX) + ' -' + FloatToStr(vY + vHeight) + ' cm /I' +
IntToStr(Length(Self.pImages)) + ' Do Q');
end;
procedure TJPFpdf.Cell(vWidth: double; vHeight: double; vText: string;
vBorder: string; vLineBreak: integer; vAlign: string; vFill: integer);
var
vws, vx, vy, vdx: double;
sss: string;
begin
if (pUTF8) then vText := Utf8ToAnsi(vText);
//Output a cell
if (((Self.cpY + vHeight) > Self.PageBreakTrigger) and not
(Self.InFooter) and (AcceptPageBreak())) then
begin
vx := Self.cpX;
vws := Self.pgWs;
if (vws > 0) then
begin
Self.pgWs := 0;
_out('0 Tw');
end;
AddPage(Self.CurOrientation);
Self.cpX := vx;
if (vws > 0) then
begin
Self.pgWs := vws;
_out(FloatToStr(vws) + ' Tw');
end;
end;
if (vWidth = 0) then
vWidth := Self.dw - Self.rMargin - Self.cpX;
sss := '';
if ((vFill = 1) or (vBorder = '1')) then
begin
sss += FloatToStr(Self.cpX) + ' -' + FloatToStr(Self.cpY) + ' ' +
FloatToStr(vWidth) + ' -' + FloatToStr(vHeight) + ' re ';
if (vFill = 1) then
if (vBorder = '1') then
sss += 'B '
else
sss += 'f '
else
sss += 'S ';
end;
if ((Pos('L', vBorder) > 0) or (Pos('T', vBorder) > 0) or
(Pos('R', vBorder) > 0) or (Pos('B', vBorder) > 0)) then
begin
vx := Self.cpX;
vy := Self.cpY;
if (Pos('L', vBorder) > 0) then
sss += FloatToStr(vx) + ' -' + FloatToStr(vy) + ' m ' +
FloatToStr(vx) + ' -' + FloatToStr((vy + vHeight)) + ' l S ';
if (Pos('T', vBorder) > 0) then
sss += FloatToStr(vx) + ' -' + FloatToStr(vy) + ' m ' + FloatToStr(
(vx + vWidth)) + ' -' + FloatToStr(vy) + ' l S ';
if (Pos('R', vBorder) > 0) then
sss += FloatToStr((vx + vWidth)) + ' -' + FloatToStr(vy) +
' m ' + FloatToStr((vx + vWidth)) + ' -' + FloatToStr((vy + vHeight)) + ' l S ';
if (Pos('B', vBorder) > 0) then
sss += FloatToStr(vx) + ' -' + FloatToStr((vy + vHeight)) +
' m ' + FloatToStr((vx + vWidth)) + ' -' + FloatToStr((vy + vHeight)) + ' l S ';
end;
if (vText <> '') then
begin
if (vAlign = 'R') then
vdx := vWidth - Self.cMargin - GetStringWidth(vText)
else if (vAlign = 'C') then
vdx := (vWidth - GetStringWidth(vText)) / 2
else
vdx := Self.cMargin;
vText := _escape(vText);
if (Self.pColorFlag) then
sss += 'q ' + Self.pTextColor + ' ';
sss += 'BT ' + FloatToStr((Self.cpX + vdx)) + ' -' + FloatToStr(
(Self.cpY + 0.5 * vHeight + 0.3 * Self.cFontSize)) + ' Td (' + vText + ') Tj ET';
if (pUnderlineFlag) then
sss += ' ' + _dounderline(Self.cpX + vdx, Self.cpY + 0.5 *
vHeight + 0.3 * Self.cFontSize, vText);
if (Self.pColorFlag) then
sss += ' Q';
end;
if (sss <> '') then
_out(sss);
Self.hLasth := vHeight;
if (vLineBreak > 0) then
begin
//Go to next line
Self.cpY += vHeight;