File Coverage

blib/lib/PDF/Builder/Basic/PDF/Name.pm
Criterion Covered Total %
statement 29 31 93.5
branch 2 4 50.0
condition 5 12 41.6
subroutine 8 8 100.0
pod 5 5 100.0
total 49 60 81.6


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 <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     42   286 use base 'PDF::Builder::Basic::PDF::String';
  42         119  
  42         5808  
19              
20 42     42   286 use strict;
  42         83  
  42         1195  
21 42     42   256 use warnings;
  42         98  
  42         30168  
22              
23             our $VERSION = '3.028'; # VERSION
24             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             sub from_pdf {
49 577     577 1 1435 my ($class, $string, $pdf) = @_;
50              
51 577         2724 my ($self) = $class->SUPER::from_pdf($string);
52              
53 577         1306 $self->{'val'} = name_to_string($self->{'val'}, $pdf);
54 577         1581 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             sub convert {
71 577     577 1 1234 my ($self, $string, $pdf) = @_;
72              
73 577         1349 $string = name_to_string($string, $pdf);
74 577         2295 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             sub as_pdf {
91 7318     7318 1 13186 my ($self, $pdf) = @_;
92              
93 7318         14004 my $string = $self->{'val'};
94              
95 7318         12751 $string = string_to_name($string, $pdf);
96 7318         25271 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             sub string_to_name {
118 11614     11614 1 22127 my ($string, $pdf) = @_;
119              
120             # PDF 1.0 and 1.1 didn't treat the # symbol as an escape character
121 11614 50 33     70834 unless ($pdf and $pdf->{' version'} and $pdf->{' version'} < 1.2) {
      33        
122 11614         23728 $string =~ s|([\x00-\x20\x7f-\xff%()\[\]{}<>#/])|'#' . sprintf('%02X', ord($1))|oge;
  0         0  
123             }
124              
125 11614         34103 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             sub name_to_string {
143 1611     1611 1 3793 my ($string, $pdf) = @_;
144              
145 1611         2971 $string =~ s|^/||o;
146              
147             # PDF 1.0 and 1.1 didn't treat the # symbol as an escape character
148 1611 50 66     8187 unless ($pdf and $pdf->{' version'} and $pdf->{' version'} < 1.2) {
      33        
149 1611         3031 $string =~ s/#([0-9a-f]{2})/chr(hex($1))/oige;
  0         0  
150             }
151              
152 1611         4381 return $string;
153             }
154              
155             1;