PDF/Name.pm

filter: Statementstmt 65.6% 21 / 32 Branchbran 50.0% 2 / 4 Conditioncond 33.3% 4 / 12 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 75.0% 6 / 8 Podpod 100% 5 / 5 totaltotal 56.7% 38 / 67 CC 7 SCAR 14.2
CC
7
Cov
56%
string_to_name
6.5
name_to_string
6.0
from_pdf
2.0
CRAP 4.1
« PDF/String.pm ↑ Summary PDF/Page.pm »
1 package Text::PDF::Name;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use vars qw(@ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 # no warnings qw(uninitialized);
6  
7 1 ▶use Text::PDF::String;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 @ISA = qw(Text::PDF::String);
9  
10 =head1 NAME
11  
12 Text::PDF::Name - Inherits from L<Text::PDF::String> and stores PDF names (things
13 beginning with /)
14  
15 =head1 METHODS
16  
17 =head2 Text::PDF::Name->from_pdf($string)
18  
19 Creates a new string object (not a full object yet) from a given string.
20 The string is parsed according to input criteria with escaping working, particular
21 to Names.
22  
23 =cut
24  
25  
26 sub from_pdf
27 {
28 0 ▶ my ($class, $str, $pdf) = @_;
Subroutine from_pdf: not called CC=1 SCAR=6.9
Pod documented
29 0 my ($self) = $class->SUPER::from_pdf($str);
30  
31 0 $self->{'val'} = name_to_string ($self->{'val'}, $pdf);
32 0 $self;
33 }
34  
35 =head2 $n->convert ($str, $pdf)
36  
37 Converts a name into a string by removing the / and converting any hex
38 munging unless $pdf is supplied and its version is less than 1.2.
39  
40 =cut
41  
42 sub convert
43 {
44 0 ▶ my ($self, $str, $pdf) = @_;
Subroutine convert: not called CC=1 SCAR=6.9
Pod documented
45  
46 0 $str = name_to_string ($str, $pdf);
47 0 return $str;
48 }
49  
50  
51 =head2 $s->as_pdf ($pdf)
52  
53 Returns a name formatted as PDF. $pdf is optional but should be the
54 PDF File object for which the name is intended if supplied.
55  
56 =cut
57  
58 sub as_pdf
59 {
60 9 ▶ my ($self, $pdf) = @_;
Subroutine as_pdf: called CC=1 SCAR=0.0
Pod documented
61 9 my ($str) = $self->{'val'};
62  
63 9 $str = string_to_name ($str, $pdf);
64 9 return ("/" . $str);
65 }
66  
67  
68 # Prior to PDF version 1.2, `#' was a literal character. Embedded
69 # spaces were implicitly allowed in names as well but it would be best
70 # to ignore that (PDF reference 2nd edition, Appendix H, section 3.2.4.3).
71  
72 =head2 Text::PDF::Name->string_to_name ($str, $pdf)
73  
74 Suitably encode the string $str for output in the File object $pdf
75 (the exact format may depend on the version of $pdf). Prinicipally,
76 encode certain characters in hex if the version is greater than 1.1.
77  
78 =cut
79  
80 sub string_to_name ($;$)
81 {
82 24 ▶ my ($str, $pdf) = @_;
Subroutine string_to_name: called CC=4 SCAR=18.7
Pod documented
83 24 ▶ if (!defined($pdf) || (defined $pdf->{' version'} && $pdf->{' version'} >= 2))
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $pdf or defined $pdf->{" version"} and $pdf->{" version"} >= 2) 0 24 24
Condition33%2/6
not defined $pdf or defined $pdf->{" version"} and $pdf->{" version"} >= 2
l!l&&r!l&&!r
0024
defined $pdf->{" version"} and $pdf->{" version"} >= 2
!ll&&!rl&&r
2400
MC/DC0%0/3
not defined $pdf or defined $pdf->{" version"} and $pdf->{" version"} >= 2
not defined $pdf defined $pdf->{" version"} $pdf->{" version"} >= 2
Truth table0%0/4
not(A) or B and C
A= not defined $pdfB= defined $pdf->{" version"}C= $pdf->{" version"} >= 2
ABCresult
1XX1
0111
00X0
0100
84 0 { $str =~ s|([\001-\040\177-\377%()\[\]{}<>#/])|"#".sprintf("%02X", ord($1))|oge; }
85 24 return $str;
86 }
87  
88 =head2 Text::PDF::Name->name_to_string ($str, $pdf)
89  
90 Suitably decode the string $str as read from the File object $pdf (the
91 exact decoding may depend on the version of $pdf). Principally, undo
92 the hex encoding for PDF versions > 1.1.
93  
94 =cut
95
96 sub name_to_string ($;$)
97 {
98 3 ▶ my ($str, $pdf) = @_;
Subroutine name_to_string: called CC=4 SCAR=17.9
Pod documented
99 3 $str =~ s|^/||o;
100  
101 3 ▶ if (!defined($pdf) || (defined $pdf->{' version'} && $pdf->{' version'} >= 2))
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $pdf or defined $pdf->{" version"} and $pdf->{" version"} >= 2) 0 3 3
Condition33%2/6
not defined $pdf or defined $pdf->{" version"} and $pdf->{" version"} >= 2
l!l&&r!l&&!r
003
defined $pdf->{" version"} and $pdf->{" version"} >= 2
!ll&&!rl&&r
300
MC/DC0%0/3
not defined $pdf or defined $pdf->{" version"} and $pdf->{" version"} >= 2
not defined $pdf defined $pdf->{" version"} $pdf->{" version"} >= 2
Truth table0%0/4
not(A) or B and C
A= not defined $pdfB= defined $pdf->{" version"}C= $pdf->{" version"} >= 2
ABCresult
1XX1
0111
00X0
0100
102 0 { $str =~ s/#([0-9a-f]{2})/chr(hex($1))/oige; }
103 3 return $str;
104 }
105  
« PDF/String.pm ↑ Summary PDF/Page.pm »