line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
#======================================================================= |
2
|
|
|
|
|
|
|
# |
3
|
|
|
|
|
|
|
# THIS IS A REUSED PERL MODULE, FOR PROPER LICENCING TERMS SEE BELOW: |
4
|
|
|
|
|
|
|
# |
5
|
|
|
|
|
|
|
# Copyright Martin Hosken |
6
|
|
|
|
|
|
|
# |
7
|
|
|
|
|
|
|
# No warranty or expression of effectiveness, least of all regarding |
8
|
|
|
|
|
|
|
# anyone's safety, is implied in this software or documentation. |
9
|
|
|
|
|
|
|
# |
10
|
|
|
|
|
|
|
# This specific module is licensed under the Perl Artistic License. |
11
|
|
|
|
|
|
|
# Effective 28 January 2021, the original author and copyright holder, |
12
|
|
|
|
|
|
|
# Martin Hosken, has given permission to use and redistribute this module |
13
|
|
|
|
|
|
|
# under the MIT license. |
14
|
|
|
|
|
|
|
# |
15
|
|
|
|
|
|
|
#======================================================================= |
16
|
|
|
|
|
|
|
package PDF::Builder::Basic::PDF::Utils; |
17
|
|
|
|
|
|
|
|
18
|
41
|
|
|
41
|
|
1895
|
use strict; |
|
41
|
|
|
|
|
75
|
|
|
41
|
|
|
|
|
1212
|
|
19
|
41
|
|
|
41
|
|
194
|
use warnings; |
|
41
|
|
|
|
|
82
|
|
|
41
|
|
|
|
|
2159
|
|
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
our $VERSION = '3.024'; # VERSION |
22
|
|
|
|
|
|
|
our $LAST_UPDATE = '3.024'; # manually update whenever code is changed |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
=head1 NAME |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
PDF::Builder::Basic::PDF::Utils - Utility functions for PDF library |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
=head1 DESCRIPTION |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
A set of utility functions to save the fingers of the PDF library users! |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
=head1 FUNCTIONS |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
=over |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
=cut |
37
|
|
|
|
|
|
|
|
38
|
41
|
|
|
41
|
|
16014
|
use PDF::Builder::Basic::PDF::Array; |
|
41
|
|
|
|
|
98
|
|
|
41
|
|
|
|
|
1157
|
|
39
|
41
|
|
|
41
|
|
16882
|
use PDF::Builder::Basic::PDF::Bool; |
|
41
|
|
|
|
|
90
|
|
|
41
|
|
|
|
|
1194
|
|
40
|
41
|
|
|
41
|
|
17181
|
use PDF::Builder::Basic::PDF::Dict; |
|
41
|
|
|
|
|
126
|
|
|
41
|
|
|
|
|
1542
|
|
41
|
41
|
|
|
41
|
|
297
|
use PDF::Builder::Basic::PDF::Name; |
|
41
|
|
|
|
|
79
|
|
|
41
|
|
|
|
|
848
|
|
42
|
41
|
|
|
41
|
|
17372
|
use PDF::Builder::Basic::PDF::Null; |
|
41
|
|
|
|
|
94
|
|
|
41
|
|
|
|
|
1028
|
|
43
|
41
|
|
|
41
|
|
15462
|
use PDF::Builder::Basic::PDF::Number; |
|
41
|
|
|
|
|
98
|
|
|
41
|
|
|
|
|
1157
|
|
44
|
41
|
|
|
41
|
|
225
|
use PDF::Builder::Basic::PDF::String; |
|
41
|
|
|
|
|
77
|
|
|
41
|
|
|
|
|
717
|
|
45
|
41
|
|
|
41
|
|
15306
|
use PDF::Builder::Basic::PDF::Literal; |
|
41
|
|
|
|
|
103
|
|
|
41
|
|
|
|
|
1070
|
|
46
|
|
|
|
|
|
|
|
47
|
41
|
|
|
41
|
|
230
|
use Exporter; |
|
41
|
|
|
|
|
71
|
|
|
41
|
|
|
|
|
1404
|
|
48
|
41
|
|
|
41
|
|
208
|
use vars qw(@EXPORT @ISA); |
|
41
|
|
|
|
|
76
|
|
|
41
|
|
|
|
|
18913
|
|
49
|
|
|
|
|
|
|
@ISA = qw(Exporter); |
50
|
|
|
|
|
|
|
@EXPORT = qw(PDFBool PDFArray PDFDict PDFName PDFNull |
51
|
|
|
|
|
|
|
PDFNum PDFString PDFStr PDFStrHex PDFUtf); |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
=item PDFBool() |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
Creates a Bool via PDF::Builder::Basic::PDF::Bool->new() |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
=cut |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
sub PDFBool { |
60
|
5
|
|
|
5
|
1
|
19
|
return PDF::Builder::Basic::PDF::Bool->new(@_); |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
=item PDFArray() |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
Creates an array via PDF::Builder::Basic::PDF::Array->new() |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
=cut |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
sub PDFArray { |
70
|
1156
|
|
|
1156
|
1
|
3261
|
return PDF::Builder::Basic::PDF::Array->new(@_); |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
=item PDFDict() |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
Creates a dict via PDF::Builder::Basic::PDF::Dict->new() |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
=cut |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
sub PDFDict { |
80
|
1357
|
|
|
1357
|
1
|
5159
|
return PDF::Builder::Basic::PDF::Dict->new(@_); |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
=item PDFName() |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
Creates a name via PDF::Builder::Basic::PDF::Name->new() |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
=cut |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
sub PDFName { |
90
|
17412
|
|
|
17412
|
1
|
31144
|
return PDF::Builder::Basic::PDF::Name->new(@_); |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
=item PDFNull() |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
Creates a null via PDF::Builder::Basic::PDF::Null->new() |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
=cut |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
sub PDFNull { |
100
|
12
|
|
|
12
|
1
|
40
|
return PDF::Builder::Basic::PDF::Null->new(@_); |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
=item PDFNum() |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
Creates a number via PDF::Builder::Basic::PDF::Number->new() |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
=cut |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
sub PDFNum { |
110
|
14236
|
|
|
14236
|
1
|
26122
|
return PDF::Builder::Basic::PDF::Number->new(@_); |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
=item PDFString($text, $usage) |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
Returns either PDFStr($text) or PDFUtf($text), depending on whether C<$text> |
116
|
|
|
|
|
|
|
is already in UTF-8 and whether the C<$usage> permits UTF-8. If UTF-8 is I |
117
|
|
|
|
|
|
|
permitted, C will be called on a UTF-8 formatted C<$text>. |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
C<$usage> is a single character string indicating the use for which C<$text> |
120
|
|
|
|
|
|
|
is to be applied. Some uses permit UTF-8, while others (currently) forbid it: |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=over |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=item 's' |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
An ordinary B, where UTF-8 text is permitted. |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
=item 'n' |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
A B, where UTF-8 text is permitted. |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
=item 'o' |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
An B, where UTF-8 text is permitted. |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=item 'p' |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
A B, where UTF-8 text is permitted. |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
=item 'm' |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
B, where UTF-8 text is permitted. |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
=item 'f' |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
A B, where UTF-8 text is currently B permitted. |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
=item 'u' |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
A B, where UTF-8 text is currently B permitted. |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
=item 'x' |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
Any other usage where UTF-8 text is B permitted. |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
=back |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
=cut |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
sub PDFString { |
161
|
234
|
|
|
234
|
1
|
500
|
my ($text, $usage) = @_; |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
# some old code also checked valid(), but that seems to always give a true |
164
|
|
|
|
|
|
|
# return on non-UTF-8 text |
165
|
|
|
|
|
|
|
#my $isUTF8 = utf8::is_utf8($text) || utf8::valid($text); |
166
|
234
|
|
|
|
|
711
|
my $isUTF8 = utf8::is_utf8($text); |
167
|
234
|
|
|
|
|
352
|
my $isPermitted = 0; # default NO |
168
|
|
|
|
|
|
|
# someone is bound to forget whether it's upper or lowercase! |
169
|
234
|
100
|
|
|
|
992
|
if ($usage =~ m/^[snopm]/i) { |
170
|
224
|
|
|
|
|
431
|
$isPermitted = 1; |
171
|
|
|
|
|
|
|
} |
172
|
|
|
|
|
|
|
|
173
|
234
|
100
|
|
|
|
554
|
if ($isPermitted) { |
174
|
224
|
50
|
|
|
|
451
|
if ($isUTF8) { |
175
|
0
|
|
|
|
|
0
|
return PDFUtf($text); |
176
|
|
|
|
|
|
|
} else { |
177
|
224
|
|
|
|
|
536
|
return PDFStr($text); |
178
|
|
|
|
|
|
|
} |
179
|
|
|
|
|
|
|
} else { |
180
|
10
|
50
|
|
|
|
29
|
if ($isUTF8) { |
181
|
0
|
|
|
|
|
0
|
utf8::downgrade($text); # force 7 bit ASCII |
182
|
|
|
|
|
|
|
} |
183
|
10
|
|
|
|
|
21
|
return PDFStr($text); |
184
|
|
|
|
|
|
|
} |
185
|
|
|
|
|
|
|
} |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
=item PDFStr() |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
Creates a string via PDF::Builder::Basic::PDF::String->new() |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
B It is preferable that you use C instead. |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
=cut |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
sub PDFStr { |
196
|
238
|
|
|
238
|
1
|
1195
|
return PDF::Builder::Basic::PDF::String->new(@_); |
197
|
|
|
|
|
|
|
} |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
=item PDFStrHex() |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
Creates a hex-string via PDF::Builder::Basic::PDF::String->new() |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
=cut |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
sub PDFStrHex { |
206
|
1
|
|
|
1
|
1
|
4
|
my $string = PDF::Builder::Basic::PDF::String->new(@_); |
207
|
1
|
|
|
|
|
3
|
$string->{' ishex'} = 1; |
208
|
1
|
|
|
|
|
3
|
return $string; |
209
|
|
|
|
|
|
|
} |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
=item PDFUtf() |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
Creates a utf8-string via PDF::Builder::Basic::PDF::String->new() |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
B It is preferable that you use C instead. |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
=cut |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
sub PDFUtf { |
220
|
0
|
|
|
0
|
1
|
|
my $string = PDF::Builder::Basic::PDF::String->new(@_); |
221
|
0
|
|
|
|
|
|
$string->{' isutf'} = 1; |
222
|
0
|
|
|
|
|
|
return $string; |
223
|
|
|
|
|
|
|
} |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
=back |
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
=cut |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
1; |