Builder/Basic/PDF/Name.pm

filter: Statementstmt 94.1% 32 / 34 Branchbran 50.0% 2 / 4 Conditioncond 50.0% 6 / 12 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 100% 8 / 8 Podpod 100% 5 / 5 totaltotal 76.8% 53 / 69 CC 7 SCAR 11.8
CC
7
Cov
80%
string_to_name
5.6
name_to_string
4.4
BEGIN
1.0
CRAP 3.3
« Builder/Basic/PDF/Objind.pm ↑ Summary Builder/Basic/PDF/Page.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::Name;
17  
18 42 ▶use base 'PDF::Builder::Basic::PDF::String';
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::Name - Stores PDF names (things beginning with /)
29  
30 Inherits from L<PDF::Builder::Basic::PDF::String>
31  
32 =head1 METHODS
33  
34 =head2 from_pdf
35  
36 $n = PDF::Builder::Basic::PDF::Name->from_pdf($string)
37  
38 =over
39  
40 Creates a new string object (not a full object yet) from a given
41 string. The string is parsed according to input criteria with
42 escaping working, particular to Names.
43  
44 =back
45  
46 =cut
47  
48 577 ▶sub from_pdf {
Subroutine from_pdf: called CC=1 SCAR=0.0
Pod documented
49 577 my ($class, $string, $pdf) = @_;
50  
51 577 my ($self) = $class->SUPER::from_pdf($string);
52  
53 577 $self->{'val'} = name_to_string($self->{'val'}, $pdf);
54 577 return $self;
55 }
56  
57 =head2 convert
58  
59 $n->convert($string, $pdf)
60  
61 =over
62  
63 Converts a name into a string by removing the / and converting any hex
64 munging.
65  
66 =back
67  
68 =cut
69  
70 577 ▶sub convert {
Subroutine convert: called CC=1 SCAR=0.0
Pod documented
71 577 my ($self, $string, $pdf) = @_;
72  
73 577 $string = name_to_string($string, $pdf);
74 577 return $string;
75 }
76  
77 =head2 as_pdf
78  
79 $s->as_pdf($pdf)
80  
81 =over
82  
83 Returns a name formatted as PDF. C<$pdf> is optional but should be the
84 PDF File object for which the name is intended if supplied.
85  
86 =back
87  
88 =cut
89  
90 7318 ▶sub as_pdf {
Subroutine as_pdf: called CC=1 SCAR=0.0
Pod documented
91 7318 my ($self, $pdf) = @_;
92  
93 7318 my $string = $self->{'val'};
94  
95 7318 $string = string_to_name($string, $pdf);
96 7318 return '/' . $string;
97 }
98  
99  
100 # Prior to PDF version 1.2, '#' was a literal character. Embedded
101 # spaces were implicitly allowed in names as well but it would be best
102 # to ignore that (PDF 1.3, section H.3.2.4.3).
103  
104 =head2 string_to_name
105  
106 PDF::Builder::Basic::PDF::Name->string_to_name($string, $pdf)
107  
108 =over
109  
110 Suitably encode the string C<$string> for output in the File object C<$pdf>
111 (the exact format may depend on the version of $pdf).
112  
113 =back
114  
115 =cut
116  
117 11614 ▶sub string_to_name {
Subroutine string_to_name: called CC=4 SCAR=17.2
Pod documented
118 11614 my ($string, $pdf) = @_;
119  
120 # PDF 1.0 and 1.1 didn't treat the # symbol as an escape character
121 11614 ▶ unless ($pdf and $pdf->{' version'} and $pdf->{' version'} < 1.2) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pdf and $pdf->{" version"} and $pdf->{" version"} < "1.2") 11614 0 11614
Condition33%2/6
$pdf and $pdf->{" version"} and $pdf->{" version"} < "1.2"
!ll&&!rl&&r
0116140
$pdf and $pdf->{" version"}
!ll&&!rl&&r
0011614
MC/DC0%0/3
$pdf and $pdf->{" version"} and $pdf->{" version"} < "1.2"
$pdf $pdf->{" version"} $pdf->{" version"} < "1.2"
Truth table25%1/4
A and B and C
A= $pdfB= $pdf->{" version"}C= $pdf->{" version"} < "1.2"
ABCresult
0XX0
10X0
1100
1111
122 0 $string =~ s|([\x00-\x20\x7f-\xff%()\[\]{}<>#/])|'#' . sprintf('%02X', ord($1))|oge;
123 }
124  
125 11614 return $string;
126 }
127  
128 =head2 name_to_string
129  
130 PDF::Builder::Basic::PDF::Name->name_to_string($string, $pdf)
131  
132 =over
133  
134 Suitably decode the string C<$string> as read from the File object C<$pdf>
135 (the exact decoding may depend on the version of $pdf). Principally,
136 undo the hex encoding for PDF versions > 1.1.
137  
138 =back
139  
140 =cut
141  
142 1611 ▶sub name_to_string {
Subroutine name_to_string: called CC=4 SCAR=14.8
Pod documented
143 1611 my ($string, $pdf) = @_;
144  
145 1611 $string =~ s|^/||o;
146  
147 # PDF 1.0 and 1.1 didn't treat the # symbol as an escape character
148 1611 ▶ unless ($pdf and $pdf->{' version'} and $pdf->{' version'} < 1.2) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pdf and $pdf->{" version"} and $pdf->{" version"} < "1.2") 1611 0 1611
Condition66%4/6
$pdf and $pdf->{" version"} and $pdf->{" version"} < "1.2"
!ll&&!rl&&r
57710340
$pdf and $pdf->{" version"}
!ll&&!rl&&r
57701034
MC/DC0%0/3
$pdf and $pdf->{" version"} and $pdf->{" version"} < "1.2"
$pdf $pdf->{" version"} $pdf->{" version"} < "1.2"
Truth table50%2/4
A and B and C
A= $pdfB= $pdf->{" version"}C= $pdf->{" version"} < "1.2"
ABCresult
0XX0
10X0
1100
1111
149 0 $string =~ s/#([0-9a-f]{2})/chr(hex($1))/oige;
150 }
151  
152 1611 return $string;
153 }
154  
155 42 1;
« Builder/Basic/PDF/Objind.pm ↑ Summary Builder/Basic/PDF/Page.pm »