-
Notifications
You must be signed in to change notification settings - Fork 0
/
tclfpdf.1.6.tcl
2544 lines (2424 loc) · 70.3 KB
/
tclfpdf.1.6.tcl
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
;# *******************************************************************************
;# tclfpdf.tcl
;# Version: 1.6 (2023)
;# Ported to TCL by L. A. Muzzachiodi
;# Credits:
;# Based on tFPDF 1.33 by Ian Back <[email protected]>
;# and Tycho Veltmeijer <[email protected]> (versions 1.30+)
;# wich is based on fpdf.php 1.8.2 by Olivier Plathey
;# Parse of JPEG based on pdf4tcl 0.8 by Peter Spjuth
;# *******************************************************************************
;# Note:
;# the definition of core fonts have a diference: the uv index in FPDF, not tfpdf, cause a bigger file (?)
package provide tclfpdf 1.6
package require Tk
namespace eval ::tclfpdf:: {
namespace export \
Init \
SetMargins \
SetLeftMargin \
SetTopMargin \
SetRightMargin \
SetAutoPageBreak \
SetDisplayMode \
SetCompression \
SetTitle \
SetSubject \
SetAuthor \
SetKeywords \
SetCreator \
AliasNbPages \
Error \
Open \
Close \
AddPage \
Header \
Footer \
PageNo \
SetDrawColor \
SetFillColor \
SetTextColor \
GetStringWidth \
SetLineWidth \
Line \
Rect \
AddFont \
SetFont \
SetFontSize \
AddLink \
SetLink \
Link \
Text \
AcceptPageBreak \
Cell \
MultiCell \
Write \
Ln \
Image \
GetX \
SetX \
GetY \
SetY \
SetXY \
Output \
variable TCLFPDF_FONTPATH "[file join [pwd] [file dirname [info script]]]/font"
set SYSTEM_TTFONTS ""
switch -- $::tcl_platform(platform) {
windows { set SYSTEM_TTFONTS "[file normalize $::env(SystemRoot)/fonts]" }
unix { set SYSTEM_TTFONTS "/usr/share/fonts"}
macintosh {set SYSTEM_TTFONTS "/System/Library/Fonts"}
default { Error "Missing system path font.\n The platform: $::tc_platform(platform) isn't defined."}
}
variable VERSION 1.6
variable unifontSubset ;#
variable page ;# current page number
variable n ;# current object number
variable offsets ;# array of object offsets
variable buffer ;# buffer holding in-memory PDF
variable pages ;# array containing pages
variable state ;# current document state
variable compress ;# compression flag
variable k ;# scale factor (number of points in user unit)
variable DefOrientation ;# default orientation
variable CurOrientation ;# current orientation
variable StdPageSizes ;# standard page sizes
variable DefPageSize ;# default page size
variable CurPageSize ;# current page size
variable CurRotation ;#current page rotation
variable PageInfo ;#page-related data
variable wPt
variable hPt ;# dimensions of current page in points
variable w
variable h ;# dimensions of current page in user unit
variable lMargin ;# left margin
variable tMargin ;# top margin
variable rMargin ;# right margin
variable bMargin ;# page break margin
variable cMargin ;# cell margin
variable x
variable y ;# current position in user unit
variable lasth ;# height of last printed cell
variable LineWidth ;# line width in user unit
variable fontpath ;# path containing fonts
variable CoreFonts ;# array of core font names
variable fonts ;# array of used fonts
variable FontFiles ;# array of font files
variable encodings ;#array of encodings
variable cmaps ;#array of ToUnicode CMaps
variable FontFamily ;# current font family
variable FontStyle ;# current font style
variable underline ;# underlining flag
variable CurrentFont ;# current font info
variable FontSizePt ;# current font size in points
variable FontSize ;# current font size in user unit
variable DrawColor ;# commands for drawing color
variable FillColor ;# commands for filling color
variable TextColor ;# commands for text color
variable ColorFlag ;# indicates whether fill and text colors are different
variable WithAlpha ;# indicates whether alpha channel is used
variable ws ;# word spacing
variable images ;# array of used images
variable PageLinks ;# array of links in pages
variable links ;# array of internal links
variable AutoPageBreak ;# automatic page breaking
variable PageBreakTrigger ;# threshold used to trigger page breaks
variable InHeader ;# flag set when processing header
variable InFooter ;# flag set when processing footer
variable AliasNbPages ;# alias for total number of pages
variable ZoomMode ;# zoom display mode
variable LayoutMode ;# layout display mode
variable metadata ;# document properties
variable CreationDate ;#document creation date
variable PDFVersion ;# PDF version number
variable Spaces4Tab ;#How spaces are a Tab ?
variable TAB ;# Constant with spaces according Space4Tab
}
source -encoding utf-8 [file join [pwd] [file dirname [info script]]/misc/util.tcl]
foreach addon [glob [file join [pwd] [file dirname [info script]]/addons/*.tcl]] {
source -encoding utf-8 $addon
}
proc ::tclfpdf::Init { { orientation P } { unit mm } { size A4 } } {
variable w; variable h; variable StdPageSizes;
;# Initialization of properties
variable state 0;
variable page 0;
variable n 2;
variable buffer "";
variable pages ; array unset pages *;
variable PageInfo; array unset PageInfo *;
variable fonts ; array unset fonts *;
variable FontFiles; array unset FontFiles *;
variable encodings; array unset encodings *;
variable cmaps; array unset cmaps *;
variable images ; array unset images *;
variable links ; array unset links *;
variable InHeader 0;
variable InFooter 0;
variable lasth 0;
variable FontFamily "";
variable FontStyle "";
variable FontSizePt 12;
variable underline 0;
variable DrawColor "0 G";
variable FillColor "0 g";
variable TextColor "0 g";
variable ColorFlag 0;
variable WithAlpha 0;
variable ws 0;
;# Added because tab is showed as square
variable Spaces4Tab 4;
variable TAB [string repeat " " $Spaces4Tab];
variable x -1; # In PHP could be null not in tcl, i.e. if no there are page added
variable y -1; # Idem
;# Font path
variable fontpath;
variable TCLFPDF_FONTPATH;
set fontpath $TCLFPDF_FONTPATH;
;# Core fonts
variable CoreFonts [list courier helvetica times symbol zapfdingbats];
;# Scale factor
variable k;
if {$unit == "pt"} {
set k 1;
} elseif {$unit=="mm"} {
set k [expr 72/25.4];
} elseif {$unit=="cm"} {
set k [expr 72/2.54];
} elseif {$unit=="in"} {
set k 72;
} else {
Error "Incorrect unit: $unit";
}
;# Page sizes
array set StdPageSizes { a3 {841.89 1190.55 } a4 {595.28 841.89} a5 {420.94 595.28} letter {612 792} legal {612 1008} };
set size [ _getpagesize $size];
variable DefPageSize $size;
variable CurPageSize $size;
;# Page orientation
variable DefOrientation;
set orientation [ string tolower $orientation];
if {$orientation=="p" || $orientation=="portrait"} {
set DefOrientation "P";
set w [lindex $size 0];
set h [lindex $size 1];
} elseif {$orientation=="l" || $orientation=="landscape"} {
set DefOrientation "L";
set w [lindex $size 1] ;
set h [lindex $size 0];
} else {
Error "Incorrect orientation: $orientation";
}
variable CurOrientation $DefOrientation;
variable wPt [expr $w*$k];
variable hPt [expr $h*$k];
;#Page rotation
variable CurRotation 0;
;# Page margins (1 cm)
set margin [expr 28.35/$k];
SetMargins $margin $margin;
;# Interior cell margin (1 mm)
variable cMargin [expr $margin/10.0];
;# Line width (0.2 mm)
variable LineWidth [expr .567/$k];
;# Automatic page break
SetAutoPageBreak 1 [expr 2*$margin];
;# Default display mode
SetDisplayMode "default" ;
;# Enable compression
SetCompression 1;
;#Metadata
variable VERSION;
variable metadata;
array set metadata "Producer tFPDF$VERSION";
;# Set default PDF version number
variable PDFVersion "1.3";
if {[namespace which -command Header]== "::Header"} {
if {[namespace which -command Header] =="::tclfpf::Header"} {
rename ::tclfpdf::Header "";
}
rename Header ::tclfpdf::Header;
}
if {[namespace which -command Footer]== "::Footer"} {
if {[namespace which -command Footer] == "::tclfpf::Footer"} {
rename ::tclfpdf::Footer "" ;
}
rename Footer ::tclfpdf::Footer;
};
variable unifontSubset 0;
variable AliasNbPages "";
}
proc ::tclfpdf::SetMargins { left top {right ""} } {
variable lMargin; variable tMargin; variable rMargin;
;# Set left, top and right margins
set lMargin $left;
set tMargin $top;
if {$right==""} {
set right $left;
}
set rMargin $right;
}
proc ::tclfpdf::SetLeftMargin { margin } {
variable page; variable lMargin;variable x;
;# Set left margin
set lMargin $margin;
if {$page>0 && $x<$margin} {
set x $margin;
}
}
proc ::tclfpdf::SetTopMargin { margin } {
variable tMargin;
;# Set top margin
set tMargin $margin;
}
proc ::tclfpdf::SetRightMargin { margin } {
variable rMargin;
;# Set right margin
set rMargin $margin;
}
proc ::tclfpdf::SetAutoPageBreak { auto { margin 0} } {
variable bMargin; variable h; variable AutoPageBreak; variable PageBreakTrigger;
;# Set auto page break mode and triggering margin
set AutoPageBreak $auto;
set bMargin $margin;
set PageBreakTrigger [expr $h-$margin];
}
proc ::tclfpdf::SetDisplayMode { zoom { layout default }} {
variable ZoomMode; variable LayoutMode;
;# Set display mode in viewer
if {$zoom=="fullpage" || $zoom=="fullwidth" || $zoom=="real" || $zoom=="default" || [string is integer $zoom]} {
set ZoomMode $zoom;
} else {
Error "Incorrect zoom display mode: $zoom" ;
}
if {$layout=="single" || $layout=="continuous" || $layout=="two" || $layout=="default"} {
set LayoutMode $layout;
} else {
Error "Incorrect layout display mode: $layout" ;
}
}
proc ::tclfpdf::SetCompression { compress1 } {
variable compress;
;# Set page compression
set compress $compress1;
}
proc ::tclfpdf::SetTitle { title1 } {
variable metadata;
;# Title of document
set metadat(title) $title1;
}
proc ::tclfpdf::SetSubject { subject1 } {
;# Subject of document
variable metadata;
set metadata(subject) $subject1;
}
proc ::tclfpdf::SetAuthor { author1 } {
;# Author of document
variable metadata;
set metadata(author) $author1;
}
proc ::tclfpdf::SetKeywords { keywords1 } {
;# Keywords of document
variable metadata;
set metadata(keywords) $keywords1;
}
proc ::tclfpdf::SetCreator { creator1 } {
;# Creator of document
variable metadata;
set metadata(creator) $creator1;
}
proc ::tclfpdf::AliasNbPages { {alias "%nb%"} } {
;# Define an alias for total number of pages
variable AliasNbPages;
set AliasNbPages $alias;
}
proc ::tclfpdf::Error { msg } {
;# Fatal error
set mode "std";
;#Disable if necessary and set mode according
if {[string match *wish* [file tail [info nameofexecutable] ]]} {
set mode "gui";
}
switch -- $mode {
gui {
tk_messageBox -icon error -message $msg -title "TCLFPDF error";
}
std {
puts "TCLFPDF error: $msg";
}
}
exit
}
proc ::tclfpdf::Close { } {
variable page; variable state;variable InFooter;
;# Terminate document
if {$state==3} {
return;
}
if {$page==0} {
AddPage;
}
;# Page footer
set InFooter 1;
if {[namespace which -command Footer] ne "" } {
Footer;
}
set InFooter 0;
;# Close page
_endpage ;
;# Close document
_enddoc ;
}
proc ::tclfpdf::AddPage { {orientation ""} {size ""} {rotation 0}} {
variable state; variable FontFamily; variable underline; variable FontSizePt;
variable LineWidth; variable DrawColor; variable FillColor; variable TextColor;
variable ColorFlag; variable page;variable k;variable FontStyle; variable InHeader;
variable InFooter;
;# Start a new page
if { $state==3} {
Error "The document is closed";
}
set family $FontFamily;
if {$underline == 1} {
set underlined "U"
} else {
set underlined "";
}
set style "$FontStyle$underlined";
set fontsize $FontSizePt;
set lw $LineWidth;
set dc $DrawColor;
set fc $FillColor;
set tc $TextColor;
set cf $ColorFlag;
if { $page>0} {
;# Page footer
set InFooter 1;
if {[namespace which -command Footer] ne "" } {
Footer;
}
set InFooter 0;
;# Close page
_endpage;
}
;# Start new page
_beginpage $orientation $size $rotation;
;# Set line cap style to square
_out "2 J";
;# Set line width
set LineWidth $lw;
_out [format "%.2f w" [expr $lw*$k]];
;# Set font
if {$family !=""} {
SetFont $family $style $fontsize;
}
;# Set colors
set DrawColor $dc;
if {$dc !="0 G"} {
_out $dc;
}
set FillColor $fc;
if {$fc !="0 g"} {
_out $fc;
}
set TextColor $tc;
set ColorFlag $cf;
;# Page header
set InHeader 1;
if {[namespace which -command Header] ne "" } {
Header;
}
set InHeader 0;
;# Restore line width
if { $LineWidth!=$lw} {
set LineWidth $lw;
_out [format "%.2f w" [expr $lw*$k]];
}
;# Restore font
if {$family!=""} {
SetFont $family $style $fontsize;
}
;# Restore colors
if { $DrawColor!=$dc} {
set DrawColor $dc;
_out $dc;
}
if { $FillColor!=$fc} {
set FillColor $fc;
_out $fc;
}
set TextColor $tc;
set ColorFlag $cf;
}
;# proc ::tclfpdf::Header { } {
;# To be implemented in your own proc
;# }
;# proc ::tclfpdf::Footer { } {
;# To be implemented in your own proc
;# }
proc ::tclfpdf::PageNo { } {
;# Get current page number
variable page;
return $page;
}
proc ::tclfpdf::SetDrawColor { r {g ""} {b ""} } {
variable page; variable DrawColor;
;# Set color for all stroking operations
if {($r==0 && $g==0 && $b==0) || $g==""} {
set DrawColor [format "%.3f G" [expr $r/255.00]];
} else {
set DrawColor [format "%.3f %.3f %.3f RG" [expr $r/255.00] [expr $g/255.00] [expr $b/255.00]];
}
if { $page>0} {
_out $DrawColor;
}
}
proc ::tclfpdf::SetFillColor { r {g ""} {b ""} } {
variable FillColor; variable page; variable TextColor;variable ColorFlag;
;# Set color for all filling operations
if { ($r==0 && $g==0 && $b==0) || $g==""} {
set FillColor [format "%.3f g" [expr $r/255.00]];
} else {
set FillColor [format "%.3f %.3f %.3f rg" [expr $r/255.00] [ expr $g/255.00] [ expr $b/255.00]];
}
set ColorFlag [ expr {$FillColor !=$TextColor} ];
if { $page >0 } {
_out $FillColor;
}
}
proc ::tclfpdf::SetTextColor { r {g ""} {b ""} } {
variable TextColor; variable ColorFlag;variable FillColor;
;# Set color for text
if { ($r==0 && $g==0 && $b==0) || $g==""} {
set TextColor [format "%.3f g" [expr $r/255.00]];
} else {
set TextColor [format "%.3f %.3f %.3f rg" [expr $r/255.00] [expr $g/255.00] [expr $b/255.00]];
}
set ColorFlag [expr {$FillColor!=$TextColor} ]
}
proc ::tclfpdf::GetStringWidth { s } {
variable page; variable CurrentFont; variable FontSize; variable unifontSubset;
;# Get width of a string in the current font
set cw [ _getList2Arr $CurrentFont cw];
set CurrentFont_desc [_getList2Arr $CurrentFont desc]
set CurrentFont_MW [_getList2Arr $CurrentFont MissingWidth]
set w 0;
if { $unifontSubset == 1 } {
set unicode [_UTF8StringToArray $s];
foreach char $unicode {
if { [_getchar $cw [expr 2*$char]] ne "" } {
set w [expr $w+ ([scan [_getchar $cw [expr 2*$char]] %c] << 8) + [scan [_getchar $cw [expr 2*$char+1]] %c ]];
} elseif { $char>0 && $char<128 && [ _getchar $cw [format %c $char]] ne "" } {
set w [expr $w + [_getchar $cw [format %c $char]]];
} elseif { [isset CurrentFont_desc)] && [MissingWidth in $CurrentFont_desc]} {
set mw [lindex $CurrentFont_desc [lsearch $CurrentFont_desc MissingWidth ] ];
set w [expr $w+ $mw];
} elseif { [isset CurrentFont_MW)]} {
set w [expr $w+ $CurrentFont_MW];
} else {
set w [expr $w+ 500];
}
}
} else {
set l [string length $s];
for { set i 0} {$i<$l} {incr i} {
set w [expr $w + [_findchar $cw [_getchar $s $i]]];
}
}
return [expr $w*$FontSize/1000.00];
}
proc ::tclfpdf::SetLineWidth { width } {
variable page; variable LineWidth;variable k;
;# Set line width
set LineWidth $width;
if { $page>0 } {
_out [format "%.2f w" [expr $width*$k]];
}
}
proc ::tclfpdf::Line { x1 y1 x2 y2 } {
variable k; variable h;
;# Draw a line
_out [format "%.2f %.2f m %.2f %.2f l S" [expr $x1*$k] [expr ($h-$y1)*$k] [expr $x2*$k] [expr ($h-$y2)*$k]];
}
proc ::tclfpdf::Rect { x1 y1 w1 h1 {style "" } } {
variable k; variable h;
;# Draw a rectangle
if {$style=="F"} {
set op "f";
} elseif {$style=="FD" || $style=="DF"} {
set op "B";
} else {
set op "S";
}
_out [format "%.2f %.2f %.2f %.2f re %s" [expr $x1*$k] [expr ($h-$y1)*$k] [expr $w1*$k] [expr -$h1*$k] $op ];
}
proc ::tclfpdf::AddFont { family {style ""} {file ""} {uni 0}} {
variable fonts; variable FontFiles; variable SYSTEM_TTFONTS; variable fontpath; variable AliasNbPages;
;# Add a TrueType, OpenType or Type1 font
set family [string tolower $family];
set style [string toupper $style];
if {$style=="IB"} {
set style "BI";
}
if {$file==""} {
set ffamily [string map {" " ""} $family];
set fstyle [string tolower $style];
if {$uni} {
set file "$ffamily$fstyle.ttf";
} else {
set file "$ffamily$fstyle.tcl";
}
}
set fontkey "$family$style";
if {[isset fonts($fontkey)]} {
return;
}
if {$uni } {
if {$SYSTEM_TTFONTS!="" && [file exists "$SYSTEM_TTFONTS/$file"]} {
set ttffilename "$SYSTEM_TTFONTS/$file";
} else {
set ttffilename "$fontpath/$file";
}
set unifilename "$fontpath/[string tolower [file rootname $file]]";
set name "";
set originalsize 0;
file stat $ttffilename ttfstat;
if { [file exists $unifilename.mtx.tcl]} {
source -encoding utf-8 $unifilename.mtx.tcl;
}
if {! [isset type] || ![isset name] || $originalsize != $ttfstat(size)} {
set ttffile $ttffilename;
source -encoding utf-8 "$fontpath/ttf_font.tcl";
ttf_Init;
ttf_getMetrics $ttffile;
set long [array size ttf_charWidths];
for {set x 0} {$x < $long} {incr x} {
append cw $ttf_charWidths($x);
}
set name [ string map { \[ {} \( {} \) {} \] {} } $ttf_fullName];
array set desc [list "Ascent" [expr round($ttf_ascent)] "Descent" [expr round($ttf_descent)] "CapHeight" [expr round($ttf_capHeight)] "Flags" $ttf_flags "FontBBox" "\[ [expr round($ttf_bbox(0))] [expr round($ttf_bbox(1))] [expr round($ttf_bbox(2))] [expr round($ttf_bbox(3))] \]" "ItalicAngle" $ttf_italicAngle "StemV" [expr round($ttf_stemV)] "MissingWidth" [expr round($ttf_defaultWidth)]];
set up [expr round($ttf_underlinePosition)];
set ut [expr round($ttf_underlineThickness)];
set originalsize [expr $ttfstat(size)+0 ];# 0? sic
set type "TTF";
;#Generate metrics .tcl file
append s "set name \"$name\" ;\n";
append s "set type \"$type\";\n";
append s "array set desc {[array get desc]};\n";
append s "set up $up;\n";
append s "set ut $ut;\n";
append s "set ttffile \"$ttffile\";\n";
append s "set originalsize $originalsize;\n";
append s "set fontkey \"$fontkey\";\n";
set fh [open "$unifilename.mtx.tcl" wb];
puts -nonewline $fh $s ;#[string length $s];
close $fh;
set fh [open "$unifilename.cw.dat" wb];
puts -nonewline $fh $cw;#[string length $cw];
close $fh;
} else {
set fcw [open $unifilename.cw.dat rb]
set cw [read $fcw [file size $unifilename.cw.dat]];
close $fcw;
}
set i [expr [array size fonts] +1];
set j -1;
if { $AliasNbPages != ""} {
;#range(0,57)
while (1) {
lappend sbarr [incr j] ;
if {$j==57} break;
}
} else {
;#range(0,32);
while (1) {
lappend sbarr [incr j] ;
if {$j==32} break;
}
}
set fonts($fontkey) [list "i" $i "type" $type "name" $name "desc" [array get desc] "up" $up "ut" $ut "cw" $cw "ttffile" $ttffile "fontkey" $fontkey "subset" $sbarr "unifilename" $unifilename];
set FontFiles($fontkey) [list "length1" $originalsize "type" "TTF" "ttffile" $ttffile];
set FontFiles($file) "type TTF";
unset cw;
} else {
array set info [ _loadfont $file];
set i [expr [array size fonts]+1];
set info(i) $i;
if {[isset info(file) 0]} {
;# Embedded font
if {$info(type)=="TrueType"} {
set FontFiles($info(file)) [list length1 $info(originalsize)];
} else {
set FontFiles($info(file)) [list length1 $info(size1) length2 $info(size2)];
}
}
set fonts($fontkey) [array get info];
}
}
proc ::tclfpdf::SetFont { family {style ""} {size 0} } {
variable FontFamily; variable underline;variable fonts; variable k; variable CurrentFont;
variable CoreFonts;variable page; variable FontSize; variable FontSizePt; variable FontStyle;
variable unifontSubset;
;# Select a font; size given in points
if {$family==""} {
set family $FontFamily;
} else {
set family [string tolower $family];
}
set style [string toupper $style];
if {[string first "U" $style] !=-1} {
set underline 1;
set style [string map {"U" ""} $style];
} else {
set underline 0;
}
if {$style=="IB"} {
set style "BI";
}
if {$size==0} {
set size $FontSizePt;
}
;# Test if font is already selected
if { $FontFamily==$family && $FontStyle==$style && $FontSizePt==$size} {
return;
}
;# Test if font is already loaded
set fontkey "$family$style";
if {[lsearch [array names fonts] $fontkey ] ==-1} {
;# Test if one of the core fonts
if {$family=="arial"} {
set family "helvetica";
}
if {[lsearch $CoreFonts $family]!=-1} {
if {$family=="symbol" || $family=="zapfdingbats"} {
set style "";
}
set fontkey "$family$style";
if {[lsearch [array names fonts] $fontkey ] ==-1} {
AddFont $family $style;
}
} else {
Error "Undefined font: $family $style";
}
}
;# Select it
set FontFamily $family;
set FontStyle $style;
set FontSizePt $size;
set FontSize [expr $size/$k];
upvar 0 fonts($fontkey) ::tclfpdf::CurrentFont;
if {[ _getList2Arr $fonts($fontkey) type]=="TTF"} {
set unifontSubset 1;
} else {
set unifontSubset 0;
}
if {$page>0} {
_out [format "BT /F%d %.2f Tf ET" [_getList2Arr $::tclfpdf::CurrentFont i] $FontSizePt];
}
}
proc ::tclfpdf::SetFontSize {size} {
;# Set font size in points
variable FontSize;variable page;variable k;variable FontSizePt; variable CurrentFont;
if { $FontSizePt==$size} {
return;
}
set FontSizePt $size;
set FontSize [expr $size/$k];
if { $page>0} {
_out [format "BT /F%d %.2f Tf ET" [_getList2Arr $CurrentFont i ] $FontSizePt];
}
}
proc ::tclfpdf::AddLink { } {
;# Create a new internal link
variable links;
set n [expr [array size links] +1];
array set links [list $n {}];
return $n;
}
proc ::tclfpdf::SetLink { link {y1 {0}} {page1 {-1}}} {
;# Set destination of internal link
variable links; variable y; variable page;
if {$y1 == -1} {
set y1 $y;
}
if {$page1 == -1} {
set page1 $page;
}
array set links [list $link [list $page1 $y1]];
}
proc ::tclfpdf::Link { x y w h link} {
;# Put a link on the page
variable PageLinks; variable page; variable k; variable hPt;
set nl 0
set i 0
while (1) {
if {[array names PageLinks -exact $page,$i ] eq {}} {
set nl $i;
break;
} else {
incr i;
}
}
set PageLinks($page,$nl) [list [expr $x*$k] [expr $hPt-$y*$k] [expr $w*$k] [expr $h*$k] $link];
}
proc ::tclfpdf::Text {x y txt } {
;# Output a string
variable k; variable h; variable underline; variable ColorFlag;
variable unifontSubset; variable CurrentFont; variable fonts;
if {![isset CurrentFont]} {
Error "No font has been set";
}
if {$unifontSubset == 1} {
set txt2 "([_escape [_UTF8toUTF16BE $txt 0]])";
foreach uni [_UTF8StringToArray $txt] {
if {$uni ni [_getList2Arr $CurrentFont subset]} {
_setSubList CurrentFont subset $uni;
}
}
} else {
set txt2 "([_escape $txt])";
}
set s [format "BT %.2f %.2f Td %s Tj ET" [expr $x*$k] [expr ($h-$y)*$k] $txt2];
if { $underline == 1 && $txt!="" } {
append s " [_dounderline $x $y $txt]";
}
if { $ColorFlag == 1 } {
set s "$q $TextColor $s $Q";
}
_out $s;
}
proc ::tclfpdf::AcceptPageBreak { } {
variable AutoPageBreak;
;# Accept automatic page break or not
return $AutoPageBreak;
}
proc ::tclfpdf::Cell {w1 {h1 0} {txt ""} {border 0} {ln 0} {align ""} {fill 0} {link ""}} {
variable k;variable y;variable InFooter;variable InHeader;variable h;
variable x; variable ws; variable rMargin; variable cMargin; variable PageBreakTrigger;
variable ColorFlag;variable TextColor;variable lasth; variable w; variable FontSize;
variable underline;variable lMargin;variable CurOrientation; variable CurPageSize;
variable CurRotation; variable unifontSubset; variable CurrentFont; variable fonts;
;# Output a cell
set k1 $k;
if { [expr $y+$h1] >$PageBreakTrigger && !$InHeader && !$InFooter && [AcceptPageBreak] } {
;# Automatic page break
set x1 $x;
set ws1 $ws;
if {$ws1>0} {
set ws 0;
_out "0 Tw";
}
AddPage $CurOrientation $CurPageSize $CurRotation;
set x $x1;
if {$ws1>0} {
set ws $ws1;
_out [format "%.3f Tw" [expr $ws1*$k1]];
}
}
if {$w1==0} {
set w1 [expr $w-$rMargin-$x];
}
set s "";
if {$fill !=0 || $border==1} {
if {$fill !=0} {
set op [expr {$border==1 ? "B" : "f"}];
} else {
set op "S";
}
set s [format "%.2f %.2f %.2f %.2f re %s " [expr $x*$k1] [expr ($h-$y)*$k1] [expr $w1*$k1] [expr -$h1*$k1] $op];
}
if {[string is alpha $border]} {
set x1 $x;
set y1 $y;
if {[string first "L" $border]!=-1} {
append s [format "%.2f %.2f m %.2f %.2f l S " [expr $x1*$k1] [expr ($h-$y1)*$k1] [ expr $x1*$k1] [expr ($h-($y1+$h1))*$k1]];
}
if {[string first "T" $border]!=-1} {
append s [format "%.2f %.2f m %.2f %.2f l S " [expr $x1*$k1] [expr ($h-$y1)*$k1] [expr ($x1+$w1)*$k1] [expr ($h-$y1)*$k1] ];
}
if {[string first "R" $border]!=-1} {
append s [format "%.2f %.2f m %.2f %.2f l S " [expr ($x1+$w1)*$k1] [ expr ($h-$y1)*$k1] [ expr ($x1+$w1)*$k1] [ expr ($h-($y1+$h1))*$k1]];
}
if {[string first "B" $border]!=-1} {
append s [format "%.2f %.2f m %.2f %.2f l S " [expr $x1*$k1] [expr ($h-($y1+$h1))*$k1] [expr ($x1+$w1)*$k1] [expr ($h-($y1+$h1))*$k1]];
}
}
if {$txt != ""} {
if {![isset CurrentFont]} {
Error " No font has been set";
}
if {$align=="R"} {
set dx [expr $w1-$cMargin- [GetStringWidth $txt]];
} elseif {$align=="C"} {
set dx [expr ($w1-[GetStringWidth $txt ])/2.00];
} else {
set dx $cMargin;
}
if {$ColorFlag ==1 } {
append s " q $TextColor ";
}
#If multibyte, Tw has no effect - do word spacing using an adjustment before each space
if {$ws !=0 && $unifontSubset ==1} {
foreach uni [_UTF8StringToArray $txt] {
if {$uni ni [_getList2Arr $CurrentFont subset]} {
_setSubList CurrentFont subset $uni;
}
}
set space [_escape [_UTF8toUTF16BE " " 0]];
append s [format "BT 0 Tw %.2f %.2f Td \[" [expr ($x+$dx)*$k] [expr $h-($y+.5*$h1+.3*$FontSize)*$k]];
set t [split $txt ];
set numt [llength $t];
for {set i 0} {$i<$numt} {incr i} {
set tx [lindex $t $];
set tx "([_escape [_UTF8toUTF16BE $tx 0]])";
append s [format "%s" $tx];
if { ($i+1)<$numt} {
set adj [expr -($w*$k)*1000.00/$FontSizePt];
append s [format "%d(%s) " $adj $space];
}
}
append s "\] TJ";
append s " ET";
} else {
if {$unifontSubset==1} {
set txt2 "([_escape [_UTF8toUTF16BE $txt 0]])";
foreach uni [_UTF8StringToArray $txt] {
if {$uni ni [_getList2Arr $CurrentFont subset]} {
_setSubList CurrentFont subset $uni;
}
}
} else {
set txt2 "([ _escape $txt])";
}
append s [format "BT %.2f %.2f Td %s Tj ET" [expr ($x+$dx)*$k] [expr ($h-($y+.5*$h1+.3*$FontSize))*$k] $txt2];
}
if {$underline == 1} {
append s " [_dounderline [expr $x+$dx] [ expr $y+0.5*$h1+0.3*$FontSize] $txt]";
}
if {$ColorFlag == 1} {
append s " Q";
}
if {$link!=""} {
Link [expr $x+$dx] [expr $y+0.5*$h1-0.5*$FontSize] [GetStringWidth $txt ] $FontSize $link;
}
}
if {$s!=""} {
_out $s;
}
set lasth $h1;
if {$ln>0} {
;# Go to next line
set y [expr $y+$h1];
if {$ln==1} {
set x $lMargin;
}
} else {
set x [expr $x+$w1];
}
}
proc ::tclfpdf::MultiCell {w1 h1 txt {border 0} {align "J"} {fill 0}} {
variable CurrentFont; variable v; variable rMargin; variable x; variable w; variable unifontSubset;
variable FontSize;variable cMargin; variable ws; variable h; variable lMargin; variable k;
;# Output text with automatic or explicit line breaks
if {![isset CurrentFont]} {
Error "No font has been set";
}
set cw [_getList2Arr $CurrentFont cw];
if {$w1==0} {
set w1 [expr $w-$rMargin-$x];
}
set wmax [expr ($w1-2*$cMargin)];
set s [string map [list \r "" \t $TAB] $txt];
if {$unifontSubset == 1} {
set nb [utf8len $s];
while ($nb >0 && [utf8subtr $s [expr $nb -1] 1] ne "\n" {
incr nb -1;
}
} else {
set nb [string length $s];
set idx [expr $nb-1]
if {$nb>0 && [string index $s $idx] =="\n"} {
incr nb -1;
}
}
set b 0;
if {$border!=0} {
if {$border==1} {
set border "LTRB";
set b "LRT";
set b2 "LR";
} else {
set b2 "";
if {[string first "L" $border]!=-1} {
append b2 L;