File Coverage

blib/lib/PDF/Builder/Basic/PDF/Utils.pm
Criterion Covered Total %
statement 56 61 91.8
branch 6 8 75.0
condition n/a
subroutine 21 22 95.4
pod 10 10 100.0
total 93 101 92.0


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   2113 use strict;
  41         105  
  41         1310  
19 41     41   253 use warnings;
  41         83  
  41         2444  
20              
21             our $VERSION = '3.025'; # 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   19260 use PDF::Builder::Basic::PDF::Array;
  41         116  
  41         1266  
39 41     41   17990 use PDF::Builder::Basic::PDF::Bool;
  41         119  
  41         1263  
40 41     41   19538 use PDF::Builder::Basic::PDF::Dict;
  41         169  
  41         1804  
41 41     41   293 use PDF::Builder::Basic::PDF::Name;
  41         99  
  41         1046  
42 41     41   18214 use PDF::Builder::Basic::PDF::Null;
  41         124  
  41         1198  
43 41     41   18169 use PDF::Builder::Basic::PDF::Number;
  41         136  
  41         1287  
44 41     41   277 use PDF::Builder::Basic::PDF::String;
  41         80  
  41         915  
45 41     41   18371 use PDF::Builder::Basic::PDF::Literal;
  41         113  
  41         1246  
46              
47 41     41   297 use Exporter;
  41         79  
  41         1724  
48 41     41   254 use vars qw(@EXPORT @ISA);
  41         77  
  41         22917  
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 34 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 3857 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 5432 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 37518 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 48 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 31511 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 555 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         673 my $isUTF8 = utf8::is_utf8($text);
167 234         452 my $isPermitted = 0; # default NO
168             # someone is bound to forget whether it's upper or lowercase!
169 234 100       1099 if ($usage =~ m/^[snopm]/i) {
170 224         441 $isPermitted = 1;
171             }
172              
173 234 100       595 if ($isPermitted) {
174 224 50       549 if ($isUTF8) {
175 0         0 return PDFUtf($text);
176             } else {
177 224         633 return PDFStr($text);
178             }
179             } else {
180 10 50       24 if ($isUTF8) {
181 0         0 utf8::downgrade($text); # force 7 bit ASCII
182             }
183 10         23 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 1143 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 5 my $string = PDF::Builder::Basic::PDF::String->new(@_);
207 1         8 $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;