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 37     37   2232 use strict;
  37         85  
  37         1176  
19 37     37   255 use warnings;
  37         83  
  37         2117  
20              
21             our $VERSION = '3.023'; # VERSION
22             our $LAST_UPDATE = '3.022'; # 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             =cut
35              
36 37     37   17882 use PDF::Builder::Basic::PDF::Array;
  37         115  
  37         1232  
37 37     37   18294 use PDF::Builder::Basic::PDF::Bool;
  37         100  
  37         1228  
38 37     37   19030 use PDF::Builder::Basic::PDF::Dict;
  37         135  
  37         1576  
39 37     37   324 use PDF::Builder::Basic::PDF::Name;
  37         111  
  37         915  
40 37     37   19101 use PDF::Builder::Basic::PDF::Null;
  37         106  
  37         1069  
41 37     37   17217 use PDF::Builder::Basic::PDF::Number;
  37         106  
  37         1116  
42 37     37   237 use PDF::Builder::Basic::PDF::String;
  37         78  
  37         780  
43 37     37   16888 use PDF::Builder::Basic::PDF::Literal;
  37         111  
  37         1105  
44              
45 37     37   261 use Exporter;
  37         78  
  37         1485  
46 37     37   216 use vars qw(@EXPORT @ISA);
  37         72  
  37         19967  
47             @ISA = qw(Exporter);
48             @EXPORT = qw(PDFBool PDFArray PDFDict PDFName PDFNull
49             PDFNum PDFString PDFStr PDFStrHex PDFUtf);
50              
51             =head2 PDFBool()
52              
53             Creates a Bool via PDF::Builder::Basic::PDF::Bool->new()
54              
55             =cut
56              
57             sub PDFBool {
58 5     5 1 42 return PDF::Builder::Basic::PDF::Bool->new(@_);
59             }
60              
61             =head2 PDFArray()
62              
63             Creates an array via PDF::Builder::Basic::PDF::Array->new()
64              
65             =cut
66              
67             sub PDFArray {
68 929     929 1 3461 return PDF::Builder::Basic::PDF::Array->new(@_);
69             }
70              
71             =head2 PDFDict()
72              
73             Creates a dict via PDF::Builder::Basic::PDF::Dict->new()
74              
75             =cut
76              
77             sub PDFDict {
78 1045     1045 1 4820 return PDF::Builder::Basic::PDF::Dict->new(@_);
79             }
80              
81             =head2 PDFName()
82              
83             Creates a name via PDF::Builder::Basic::PDF::Name->new()
84              
85             =cut
86              
87             sub PDFName {
88 16464     16464 1 35588 return PDF::Builder::Basic::PDF::Name->new(@_);
89             }
90              
91             =head2 PDFNull()
92              
93             Creates a null via PDF::Builder::Basic::PDF::Null->new()
94              
95             =cut
96              
97             sub PDFNull {
98 3     3 1 13 return PDF::Builder::Basic::PDF::Null->new(@_);
99             }
100              
101             =head2 PDFNum()
102              
103             Creates a number via PDF::Builder::Basic::PDF::Number->new()
104              
105             =cut
106              
107             sub PDFNum {
108 13716     13716 1 30486 return PDF::Builder::Basic::PDF::Number->new(@_);
109             }
110              
111             =head2 PDFString($text, $usage)
112              
113             Returns either PDFStr($text) or PDFUtf($text), depending on whether C<$text>
114             is already in UTF-8 and whether the C<$usage> permits UTF-8. If UTF-8 is I
115             permitted, C will be called on a UTF-8 formatted C<$text>.
116              
117             C<$usage> is a single character string indicating the use for which C<$text>
118             is to be applied. Some uses permit UTF-8, while others (currently) forbid it:
119              
120             =over
121              
122             =item 's'
123              
124             An ordinary B, where UTF-8 text is permitted.
125              
126             =item 'n'
127              
128             A B, where UTF-8 text is permitted.
129              
130             =item 'o'
131              
132             An B, where UTF-8 text is permitted.
133              
134             =item 'p'
135              
136             A B, where UTF-8 text is permitted.
137              
138             =item 'm'
139              
140             B, where UTF-8 text is permitted.
141              
142             =item 'f'
143              
144             A B, where UTF-8 text is currently B permitted.
145              
146             =item 'u'
147              
148             A B, where UTF-8 text is currently B permitted.
149              
150             =item 'x'
151              
152             Any other usage where UTF-8 text is B permitted.
153              
154             =back
155              
156             =cut
157              
158             sub PDFString {
159 174     174 1 409 my ($text, $usage) = @_;
160              
161             # some old code also checked valid(), but that seems to always give a true
162             # return on non-UTF-8 text
163             #my $isUTF8 = utf8::is_utf8($text) || utf8::valid($text);
164 174         528 my $isUTF8 = utf8::is_utf8($text);
165 174         312 my $isPermitted = 0; # default NO
166             # someone is bound to forget whether it's upper or lowercase!
167 174 100       841 if ($usage =~ m/^[snopm]/i) {
168 167         327 $isPermitted = 1;
169             }
170              
171 174 100       471 if ($isPermitted) {
172 167 50       387 if ($isUTF8) {
173 0         0 return PDFUtf($text);
174             } else {
175 167         481 return PDFStr($text);
176             }
177             } else {
178 7 50       18 if ($isUTF8) {
179 0         0 utf8::downgrade($text); # force 7 bit ASCII
180             }
181 7         17 return PDFStr($text);
182             }
183             }
184              
185             =head2 PDFStr()
186              
187             Creates a string via PDF::Builder::Basic::PDF::String->new()
188              
189             B It is preferable that you use C instead.
190              
191             =cut
192              
193             sub PDFStr {
194 176     176 1 852 return PDF::Builder::Basic::PDF::String->new(@_);
195             }
196              
197             =head2 PDFStrHex()
198              
199             Creates a hex-string via PDF::Builder::Basic::PDF::String->new()
200              
201             =cut
202              
203             sub PDFStrHex {
204 1     1 1 6 my $string = PDF::Builder::Basic::PDF::String->new(@_);
205 1         3 $string->{' ishex'} = 1;
206 1         4 return $string;
207             }
208              
209             =head2 PDFUtf()
210              
211             Creates a utf8-string via PDF::Builder::Basic::PDF::String->new()
212              
213             B It is preferable that you use C instead.
214              
215             =cut
216              
217             sub PDFUtf {
218 0     0 1   my $string = PDF::Builder::Basic::PDF::String->new(@_);
219 0           $string->{' isutf'} = 1;
220 0           return $string;
221             }
222              
223             1;