Builder/Basic/PDF/String.pm

filter: Statementstmt 98.6% 75 / 76 Branchbran 95.8% 23 / 24 Conditioncond 66.6% 6 / 9 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 100% 9 / 9 Podpod 100% 6 / 6 totaltotal 93.0% 120 / 129 CC 16 SCAR 19.2
CC
16
Cov
95%
convert
11.0
as_pdf
6.0
BEGIN
1.0
CRAP 6.8
« Builder/Resource/XObject/Form/BarCode/ean13.pm ↑ Summary Builder/Lite.pm »
1 #=======================================================================
2 #
3 # THIS IS A REUSED PERL MODULE, FOR PROPER LICENCING TERMS SEE BELOW:
4 #
5 # Copyright Martin Hosken <Martin_Hosken@sil.org>
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::String;
17  
18 42 ▶use base 'PDF::Builder::Basic::PDF::Objind';
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 42 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 42 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 42 our $VERSION = '3.028'; # VERSION
24 42 our $LAST_UPDATE = '3.027'; # manually update whenever code is changed
25  
26 =head1 NAME
27  
28 PDF::Builder::Basic::PDF::String - PDF String type objects
29  
30 Superclass for simple objects that are basically stringlike (Number, Name, etc.)
31  
32 Inherits from L<PDF::Builder::Basic::PDF::Objind>
33  
34 =head1 METHODS
35  
36 =cut
37  
38 42 our %trans = (
39 'n' => "\n",
40 'r' => "\r",
41 't' => "\t",
42 'b' => "\b",
43 'f' => "\f",
44 "\\" => "\\",
45 '(' => '(',
46 ')' => ')',
47 );
48  
49 42 our %out_trans = (
50 "\n" => 'n',
51 "\r" => 'r',
52 "\t" => 't',
53 "\b" => 'b',
54 "\f" => 'f',
55 "\\" => "\\",
56 '(' => '(',
57 ')' => ')',
58 );
59  
60 =head2 from_pdf
61  
62 PDF::Builder::Basic::PDF::String->from_pdf($string)
63  
64 =over
65  
66 Creates a new string object (not a full object yet) from a given
67 string. The string is parsed according to input criteria with
68 escaping working.
69  
70 =back
71  
72 =cut
73  
74 973 ▶sub from_pdf {
Subroutine from_pdf: called CC=1 SCAR=0.0
Pod documented
75 973 my ($class, $str) = @_;
76 973 my $self = {};
77  
78 973 bless $self, $class;
79 973 $self->{'val'} = $self->convert($str);
80 973 $self->{' realised'} = 1;
81 973 return $self;
82 }
83  
84 =head2 new
85  
86 PDF::Builder::Basic::PDF::String->new($string)
87  
88 =over
89  
90 Creates a new string object (not a full object yet) from a given
91 string. The string is parsed according to input criteria with
92 escaping working.
93  
94 =back
95  
96 =cut
97  
98 24787 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
99 24787 my ($class, $str) = @_;
100 24787 my $self = {};
101  
102 24787 bless $self, $class;
103 24787 $self->{'val'} = $str;
104 24787 $self->{' realised'} = 1;
105 24787 return $self;
106 }
107  
108 =head2 convert
109  
110 $s->convert($str)
111  
112 =over
113  
114 Returns $str converted as per criteria for input from PDF file
115  
116 =back
117  
118 =cut
119  
120 30 ▶sub convert {
Subroutine convert: called CC=11 SCAR=24.0
Pod documented
121 30 my ($self, $input) = @_;
122 30 my $output = '';
123  
124 # Hexadecimal Strings (PDF 1.7 section 7.3.4.3)
125 30 ▶ if ($input =~ m|^\s*\<|o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($input =~ /^\s*\</o) { } 3 27 30
126 3 $self->{' ishex'} = 1;
127 3 $output = $input;
128  
129 # Remove any extraneous characters to simplify processing
130 3 $output =~ s/[^0-9a-f]+//gio;
131 3 $output = "<$output>";
132  
133 # Convert each sequence of two hexadecimal characters into a byte
134 3 ▶ 1 while $output =~ s/\<([0-9a-f]{2})/chr(hex($1)) . '<'/oige;
Branch: 1 branch
BranchTrueFalseTotal
$output =~ s/\<([0-9a-f]{2})/chr(hex $1) . '<';/egio and do { '???' } 7 3 10
135  
136 # If a single hexadecimal character remains, append 0 and
137 # convert it into a byte.
138 3 $output =~ s/\<([0-9a-f])\>/chr(hex($1 . '0'))/oige;
139  
140 # Remove surrounding angle brackets
141 3 $output =~ s/\<\>//og;
142 }
143  
144 # Literal Strings (PDF 1.7 section 7.3.4.2)
145 else {
146 # Remove surrounding parentheses
147 27 $input =~ s/^\s*\((.*)\)\s*$/$1/os;
148  
149 27 my $cr = '(?:\015\012|\015|\012)';
150 27 my $prev_input;
151 27 ▶ while ($input) {
Branch: 1 branch
BranchTrueFalseTotal
if ($input) 52 27 79
152 52 ▶ if (defined $prev_input and $input eq $prev_input) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $prev_input and $input eq $prev_input) 0 52 52
Condition66%2/3
defined $prev_input and $input eq $prev_input
!ll&&!rl&&r
26260
MC/DC0%0/2
defined $prev_input and $input eq $prev_input
defined $prev_input $input eq $prev_input
Truth table66%2/3
A and B
A= defined $prev_inputB= $input eq $prev_input
ABresult
0X0
100
111
153 0 die "Infinite loop while parsing literal string";
154 }
155 52 $prev_input = $input;
156  
157 # Convert bachslash followed by up to three octal digits
158 # into that binary byte
159 52 ▶ if ($input =~ /^\\([0-7]{1,3})(.*)/os) {
Branch: 5 branches
BranchTrueFalseTotal
if ($input =~ /^\\([0-7]{1,3})(.*)/so) { } 13 39 52
elsif ($input =~ /^\\([nrtbf\\\(\)])(.*)/iso) { } 9 30 39
elsif ($input =~ /^\\$cr(.*)/so) { } 5 25 30
elsif ($input =~ /^\015\012?(.*)/so) { } 2 23 25
elsif ($input =~ /^(.*?)((?:\\(?:[nrtbf\\\(\)0-7]|$cr)|\015\012?).*)/so) { } 10 13 23
160 13 $output .= chr(oct($1));
161 13 $input = $2;
162 }
163 # Convert backslash followed by an escaped character into that
164 # character
165 elsif ($input =~ /^\\([nrtbf\\\(\)])(.*)/osi) {
166 9 $output .= $trans{$1};
167 9 $input = $2;
168 }
169 # Ignore backslash followed by an end-of-line marker
170 elsif ($input =~ /^\\$cr(.*)/os) {
171 5 $input = $1;
172 }
173 # Convert an unescaped end-of-line marker to a line-feed
174 elsif ($input =~ /^\015\012?(.*)/os) {
175 2 $output .= "\012";
176 2 $input = $1;
177 }
178 # Check to see if there are any other special sequences
179 elsif ($input =~ /^(.*?)((?:\\(?:[nrtbf\\\(\)0-7]|$cr)|\015\012?).*)/os) {
180 10 $output .= $1;
181 10 $input = $2;
182 }
183 else {
184 13 $output .= $input;
185 13 $input = undef;
186 }
187 }
188 }
189  
190 30 return $output;
191 }
192  
193 =head2 val
194  
195 $s->val()
196  
197 =over
198  
199 Returns the value of this string (the string itself).
200  
201 =back
202  
203 =cut
204  
205 2574 ▶sub val {
Subroutine val: called CC=1 SCAR=0.0
Pod documented
206 2574 return $_[0]->{'val'};
207 }
208  
209 =head2 as_pdf
210  
211 $s->as_pdf()
212  
213 =over
214  
215 Returns the string formatted for output as PDF for PDF File object $pdf.
216  
217 =back
218  
219 =cut
220  
221 198 ▶sub as_pdf {
Subroutine as_pdf: called CC=6 SCAR=17.9
Pod documented
222 198 my ($self) = @_;
223 198 my $str = $self->{'val'};
224  
225 198 ▶ if ($self->{' ishex'}) { # imported as hex ?
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{' ishex'}) { } 1 197 198
elsif ($self->{' isutf'} or utf8::is_utf8($str) and $str =~ /[^[:ascii:]]/) { } 1 196 197
226 1 $str = unpack('H*', $str);
227 1 return "<$str>";
228 ▶ } elsif ($self->{' isutf'} or
Condition66%4/6
$self->{" isutf"} or utf8::is_utf8($str) and $str =~ /[^[:ascii:]]/
l!l&&r!l&&!r
01196
utf8::is_utf8($str) and $str =~ /[^[:ascii:]]/
!ll&&!rl&&r
19601
MC/DC33%1/3
$self->{" isutf"} or utf8::is_utf8($str) and $str =~ /[^[:ascii:]]/
$self->{" isutf"} utf8::is_utf8($str) $str =~ /[^[:ascii:]]/
Truth table50%2/4
A or B and C
A= $self->{" isutf"}B= utf8::is_utf8($str)C= $str =~ /[^[:ascii:]]/
ABCresult
1XX1
0111
00X0
0100
229 (utf8::is_utf8($str) and
230 $str =~ /[^[:ascii:]]/)) {
231 1 $str = join('', map { sprintf('%04X' , $_) } unpack('U*', $str) );
232 1 return "<FEFF$str>";
233 } else {
234 196 ▶ if ($str =~ m/[^\n\r\t\b\f\040-\176\200-\377]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /[^\n\r\t\b\f\040-\176\200-\377]/) { } 3 193 196
235 3 $str =~ s/(.)/sprintf('%02X', ord($1))/sge;
236 3 return "<$str>";
237 } else {
238 193 $str =~ s/([\n\r\t\b\f\\()])/\\$out_trans{$1}/g;
239 193 return "($str)";
240 }
241 }
242 }
243  
244 =head2 outobjdeep
245  
246 $s->outobjdeep($fh, $pdf)
247  
248 =over
249  
250 Outputs the string in PDF format, complete with necessary conversions.
251  
252 =back
253  
254 =cut
255  
256 12766 ▶sub outobjdeep {
Subroutine outobjdeep: called CC=1 SCAR=0.0
Pod documented
257 12766 my ($self, $fh, $pdf) = @_;
258  
259 12766 $fh->print($self->as_pdf($pdf));
260 12766 return;
261 }
262  
263 42 1;
« Builder/Resource/XObject/Form/BarCode/ean13.pm ↑ Summary Builder/Lite.pm »